Move DWARF line_header to new file
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
82ca8957
TT
35#include "dwarf2/index-cache.h"
36#include "dwarf2/index-common.h"
f4382c45 37#include "dwarf2/leb.h"
8fdd972c 38#include "dwarf2/line-header.h"
4de283e4
TT
39#include "bfd.h"
40#include "elf-bfd.h"
41#include "symtab.h"
42#include "gdbtypes.h"
43#include "objfiles.h"
d55e5aa6 44#include "dwarf2.h"
4de283e4
TT
45#include "buildsym.h"
46#include "demangle.h"
47#include "gdb-demangle.h"
4de283e4
TT
48#include "filenames.h" /* for DOSish file names */
49#include "macrotab.h"
50#include "language.h"
51#include "complaints.h"
82ca8957
TT
52#include "dwarf2/expr.h"
53#include "dwarf2/loc.h"
4de283e4
TT
54#include "cp-support.h"
55#include "hashtab.h"
56#include "command.h"
d55e5aa6 57#include "gdbcmd.h"
4de283e4
TT
58#include "block.h"
59#include "addrmap.h"
60#include "typeprint.h"
61#include "psympriv.h"
4de283e4 62#include "c-lang.h"
d55e5aa6 63#include "go-lang.h"
4de283e4
TT
64#include "valprint.h"
65#include "gdbcore.h" /* for gnutarget */
66#include "gdb/gdb-index.h"
4de283e4
TT
67#include "gdb_bfd.h"
68#include "f-lang.h"
69#include "source.h"
4de283e4 70#include "build-id.h"
d55e5aa6 71#include "namespace.h"
268a13a5
TT
72#include "gdbsupport/function-view.h"
73#include "gdbsupport/gdb_optional.h"
74#include "gdbsupport/underlying.h"
268a13a5 75#include "gdbsupport/hash_enum.h"
4de283e4 76#include "filename-seen-cache.h"
b32b108a 77#include "producer.h"
4de283e4 78#include <fcntl.h>
4de283e4 79#include <algorithm>
4de283e4 80#include <unordered_map>
268a13a5 81#include "gdbsupport/selftest.h"
c9317f21 82#include "rust-lang.h"
268a13a5 83#include "gdbsupport/pathstuff.h"
437afbb8 84
73be47f5
DE
85/* When == 1, print basic high level tracing messages.
86 When > 1, be more verbose.
b4f54984
DE
87 This is in contrast to the low level DIE reading of dwarf_die_debug. */
88static unsigned int dwarf_read_debug = 0;
45cfd468 89
d97bc12b 90/* When non-zero, dump DIEs after they are read in. */
b4f54984 91static unsigned int dwarf_die_debug = 0;
d97bc12b 92
27e0867f 93/* When non-zero, dump line number entries as they are read in. */
8fdd972c 94unsigned int dwarf_line_debug = 0;
27e0867f 95
491144b5
CB
96/* When true, cross-check physname against demangler. */
97static bool check_physname = false;
900e11f9 98
491144b5
CB
99/* When true, do not reject deprecated .gdb_index sections. */
100static bool use_deprecated_index_sections = false;
481860b3 101
5bfd760d 102static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 103
f1e6e072
TT
104/* The "aclass" indices for various kinds of computed DWARF symbols. */
105
106static int dwarf2_locexpr_index;
107static int dwarf2_loclist_index;
108static int dwarf2_locexpr_block_index;
109static int dwarf2_loclist_block_index;
110
3f563c84
PA
111/* An index into a (C++) symbol name component in a symbol name as
112 recorded in the mapped_index's symbol table. For each C++ symbol
113 in the symbol table, we record one entry for the start of each
114 component in the symbol in a table of name components, and then
115 sort the table, in order to be able to binary search symbol names,
116 ignoring leading namespaces, both completion and regular look up.
117 For example, for symbol "A::B::C", we'll have an entry that points
118 to "A::B::C", another that points to "B::C", and another for "C".
119 Note that function symbols in GDB index have no parameter
120 information, just the function/method names. You can convert a
121 name_component to a "const char *" using the
122 'mapped_index::symbol_name_at(offset_type)' method. */
123
124struct name_component
125{
126 /* Offset in the symbol name where the component starts. Stored as
127 a (32-bit) offset instead of a pointer to save memory and improve
128 locality on 64-bit architectures. */
129 offset_type name_offset;
130
131 /* The symbol's index in the symbol and constant pool tables of a
132 mapped_index. */
133 offset_type idx;
134};
135
44ed8f3e
PA
136/* Base class containing bits shared by both .gdb_index and
137 .debug_name indexes. */
138
139struct mapped_index_base
140{
22ca247e
TT
141 mapped_index_base () = default;
142 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
143
44ed8f3e
PA
144 /* The name_component table (a sorted vector). See name_component's
145 description above. */
146 std::vector<name_component> name_components;
147
148 /* How NAME_COMPONENTS is sorted. */
149 enum case_sensitivity name_components_casing;
150
151 /* Return the number of names in the symbol table. */
152 virtual size_t symbol_name_count () const = 0;
153
154 /* Get the name of the symbol at IDX in the symbol table. */
155 virtual const char *symbol_name_at (offset_type idx) const = 0;
156
157 /* Return whether the name at IDX in the symbol table should be
158 ignored. */
159 virtual bool symbol_name_slot_invalid (offset_type idx) const
160 {
161 return false;
162 }
163
164 /* Build the symbol name component sorted vector, if we haven't
165 yet. */
166 void build_name_components ();
167
168 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
169 possible matches for LN_NO_PARAMS in the name component
170 vector. */
171 std::pair<std::vector<name_component>::const_iterator,
172 std::vector<name_component>::const_iterator>
3b00ef10
TT
173 find_name_components_bounds (const lookup_name_info &ln_no_params,
174 enum language lang) const;
44ed8f3e
PA
175
176 /* Prevent deleting/destroying via a base class pointer. */
177protected:
178 ~mapped_index_base() = default;
179};
180
9291a0cd
TT
181/* A description of the mapped index. The file format is described in
182 a comment by the code that writes the index. */
fc898b42 183struct mapped_index final : public mapped_index_base
9291a0cd 184{
f00a2de2
PA
185 /* A slot/bucket in the symbol table hash. */
186 struct symbol_table_slot
187 {
188 const offset_type name;
189 const offset_type vec;
190 };
191
559a7a62 192 /* Index data format version. */
3063847f 193 int version = 0;
559a7a62 194
f00a2de2
PA
195 /* The address table data. */
196 gdb::array_view<const gdb_byte> address_table;
b11b1f88 197
3876f04e 198 /* The symbol table, implemented as a hash table. */
f00a2de2 199 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 200
9291a0cd 201 /* A pointer to the constant pool. */
3063847f 202 const char *constant_pool = nullptr;
3f563c84 203
44ed8f3e
PA
204 bool symbol_name_slot_invalid (offset_type idx) const override
205 {
206 const auto &bucket = this->symbol_table[idx];
9ab08412 207 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 208 }
5c58de74 209
3f563c84
PA
210 /* Convenience method to get at the name of the symbol at IDX in the
211 symbol table. */
44ed8f3e 212 const char *symbol_name_at (offset_type idx) const override
f00a2de2 213 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 214
44ed8f3e
PA
215 size_t symbol_name_count () const override
216 { return this->symbol_table.size (); }
9291a0cd
TT
217};
218
927aa2e7
JK
219/* A description of the mapped .debug_names.
220 Uninitialized map has CU_COUNT 0. */
fc898b42 221struct mapped_debug_names final : public mapped_index_base
927aa2e7 222{
ed2dc618
SM
223 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
224 : dwarf2_per_objfile (dwarf2_per_objfile_)
225 {}
226
227 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
228 bfd_endian dwarf5_byte_order;
229 bool dwarf5_is_dwarf64;
230 bool augmentation_is_gdb;
231 uint8_t offset_size;
232 uint32_t cu_count = 0;
233 uint32_t tu_count, bucket_count, name_count;
234 const gdb_byte *cu_table_reordered, *tu_table_reordered;
235 const uint32_t *bucket_table_reordered, *hash_table_reordered;
236 const gdb_byte *name_table_string_offs_reordered;
237 const gdb_byte *name_table_entry_offs_reordered;
238 const gdb_byte *entry_pool;
239
240 struct index_val
241 {
242 ULONGEST dwarf_tag;
243 struct attr
244 {
245 /* Attribute name DW_IDX_*. */
246 ULONGEST dw_idx;
247
248 /* Attribute form DW_FORM_*. */
249 ULONGEST form;
250
251 /* Value if FORM is DW_FORM_implicit_const. */
252 LONGEST implicit_const;
253 };
254 std::vector<attr> attr_vec;
255 };
256
257 std::unordered_map<ULONGEST, index_val> abbrev_map;
258
259 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
260
261 /* Implementation of the mapped_index_base virtual interface, for
262 the name_components cache. */
263
264 const char *symbol_name_at (offset_type idx) const override
265 { return namei_to_name (idx); }
266
267 size_t symbol_name_count () const override
268 { return this->name_count; }
927aa2e7
JK
269};
270
cd4fb1b2 271/* See dwarf2read.h. */
ed2dc618 272
cd4fb1b2 273dwarf2_per_objfile *
ed2dc618
SM
274get_dwarf2_per_objfile (struct objfile *objfile)
275{
5bfd760d 276 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 277}
c906108c 278
251d32d9 279/* Default names of the debugging sections. */
c906108c 280
233a11ab
CS
281/* Note that if the debugging section has been compressed, it might
282 have a name like .zdebug_info. */
283
9cdd5dbd
DE
284static const struct dwarf2_debug_sections dwarf2_elf_names =
285{
251d32d9
TG
286 { ".debug_info", ".zdebug_info" },
287 { ".debug_abbrev", ".zdebug_abbrev" },
288 { ".debug_line", ".zdebug_line" },
289 { ".debug_loc", ".zdebug_loc" },
43988095 290 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 291 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 292 { ".debug_macro", ".zdebug_macro" },
251d32d9 293 { ".debug_str", ".zdebug_str" },
18a8505e 294 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 295 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 296 { ".debug_ranges", ".zdebug_ranges" },
43988095 297 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 298 { ".debug_types", ".zdebug_types" },
3019eac3 299 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
300 { ".debug_frame", ".zdebug_frame" },
301 { ".eh_frame", NULL },
24d3216f 302 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
303 { ".debug_names", ".zdebug_names" },
304 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 305 23
251d32d9 306};
c906108c 307
80626a55 308/* List of DWO/DWP sections. */
3019eac3 309
80626a55 310static const struct dwop_section_names
3019eac3
DE
311{
312 struct dwarf2_section_names abbrev_dwo;
313 struct dwarf2_section_names info_dwo;
314 struct dwarf2_section_names line_dwo;
315 struct dwarf2_section_names loc_dwo;
43988095 316 struct dwarf2_section_names loclists_dwo;
09262596
DE
317 struct dwarf2_section_names macinfo_dwo;
318 struct dwarf2_section_names macro_dwo;
3019eac3
DE
319 struct dwarf2_section_names str_dwo;
320 struct dwarf2_section_names str_offsets_dwo;
321 struct dwarf2_section_names types_dwo;
80626a55
DE
322 struct dwarf2_section_names cu_index;
323 struct dwarf2_section_names tu_index;
3019eac3 324}
80626a55 325dwop_section_names =
3019eac3
DE
326{
327 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
328 { ".debug_info.dwo", ".zdebug_info.dwo" },
329 { ".debug_line.dwo", ".zdebug_line.dwo" },
330 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 331 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
339};
340
c906108c
SS
341/* local data types */
342
107d2387
AC
343/* The data in a compilation unit header, after target2host
344 translation, looks like this. */
c906108c 345struct comp_unit_head
a738430d 346{
c764a876 347 unsigned int length;
a738430d 348 short version;
a738430d
MK
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
9c541725 351 sect_offset abbrev_sect_off;
57349743 352
a738430d
MK
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
57349743 355
a738430d
MK
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
57349743 358
43988095
JK
359 enum dwarf_unit_type unit_type;
360
a738430d
MK
361 /* Offset to the first byte of this compilation unit header in the
362 .debug_info section, for resolving relative reference dies. */
9c541725 363 sect_offset sect_off;
57349743 364
d00adf39
DE
365 /* Offset to first die in this cu from the start of the cu.
366 This will be the first byte following the compilation unit header. */
9c541725 367 cu_offset first_die_cu_offset;
43988095 368
a084a2a6
AT
369
370 /* 64-bit signature of this unit. For type units, it denotes the signature of
371 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
372 Also used in DWARF 5, to denote the dwo id when the unit type is
373 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
374 ULONGEST signature;
375
376 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 377 cu_offset type_cu_offset_in_tu;
a738430d 378};
c906108c 379
3da10d80
KS
380/* Type used for delaying computation of method physnames.
381 See comments for compute_delayed_physnames. */
382struct delayed_method_info
383{
384 /* The type to which the method is attached, i.e., its parent class. */
385 struct type *type;
386
387 /* The index of the method in the type's function fieldlists. */
388 int fnfield_index;
389
390 /* The index of the method in the fieldlist. */
391 int index;
392
393 /* The name of the DIE. */
394 const char *name;
395
396 /* The DIE associated with this method. */
397 struct die_info *die;
398};
399
e7c27a73
DJ
400/* Internal state when decoding a particular compilation unit. */
401struct dwarf2_cu
402{
fcd3b13d
SM
403 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
d00adf39 424 /* The header of the compilation unit. */
fcd3b13d 425 struct comp_unit_head header {};
e142c38c 426
d00adf39 427 /* Base address of this compilation unit. */
fcd3b13d 428 CORE_ADDR base_address = 0;
d00adf39
DE
429
430 /* Non-zero if base_address has been set. */
fcd3b13d 431 int base_known = 0;
d00adf39 432
e142c38c 433 /* The language we are debugging. */
fcd3b13d
SM
434 enum language language = language_unknown;
435 const struct language_defn *language_defn = nullptr;
e142c38c 436
fcd3b13d 437 const char *producer = nullptr;
b0f35d58 438
c24bdb02 439private:
804d2729
TT
440 /* The symtab builder for this CU. This is only non-NULL when full
441 symbols are being read. */
c24bdb02 442 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 443
c24bdb02 444public:
e142c38c
DJ
445 /* The generic symbol table building routines have separate lists for
446 file scope symbols and all all other scopes (local scopes). So
447 we need to select the right one to pass to add_symbol_to_list().
448 We do it by keeping a pointer to the correct list in list_in_scope.
449
450 FIXME: The original dwarf code just treated the file scope as the
451 first local scope, and all other local scopes as nested local
452 scopes, and worked fine. Check to see if we really need to
453 distinguish these in buildsym.c. */
fcd3b13d 454 struct pending **list_in_scope = nullptr;
e142c38c 455
b64f50a1
JK
456 /* Hash table holding all the loaded partial DIEs
457 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 458 htab_t partial_dies = nullptr;
72bf9492
DJ
459
460 /* Storage for things with the same lifetime as this read-in compilation
461 unit, including partial DIEs. */
fcd3b13d 462 auto_obstack comp_unit_obstack;
72bf9492 463
ae038cb0
DJ
464 /* When multiple dwarf2_cu structures are living in memory, this field
465 chains them all together, so that they can be released efficiently.
466 We will probably also want a generation counter so that most-recently-used
467 compilation units are cached... */
fcd3b13d 468 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 469
69d751e3 470 /* Backlink to our per_cu entry. */
ae038cb0
DJ
471 struct dwarf2_per_cu_data *per_cu;
472
473 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 474 int last_used = 0;
ae038cb0 475
b64f50a1
JK
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
fcd3b13d 478 htab_t die_hash = nullptr;
10b3939b
DJ
479
480 /* Full DIEs if read in. */
fcd3b13d 481 struct die_info *dies = nullptr;
10b3939b
DJ
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
fcd3b13d 486 htab_t dependencies = nullptr;
10b3939b 487
cb1df416 488 /* Header data from the line table, during full symbol processing. */
fcd3b13d 489 struct line_header *line_header = nullptr;
4c8aa72d
PA
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
fcd3b13d 495 die_info *line_header_die_owner = nullptr;
cb1df416 496
3da10d80
KS
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
c89b44cd 499 std::vector<delayed_method_info> method_list;
3da10d80 500
96408a79 501 /* To be copied to symtab->call_site_htab. */
fcd3b13d 502 htab_t call_site_htab = nullptr;
96408a79 503
034e5797
DE
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
18a8505e 508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
fcd3b13d 513 struct dwo_unit *dwo_unit = nullptr;
3019eac3 514
18a8505e 515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 517 gdb::optional<ULONGEST> addr_base;
3019eac3 518
18a8505e 519 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 520 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 521 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 528 ULONGEST ranges_base = 0;
2e3cf129 529
c9317f21
TT
530 /* When reading debug info generated by older versions of rustc, we
531 have to rewrite some union types to be struct types with a
532 variant part. This rewriting must be done after the CU is fully
533 read in, because otherwise at the point of rewriting some struct
534 type might not have been fully processed. So, we keep a list of
535 all such types here and process them after expansion. */
536 std::vector<struct type *> rust_unions;
537
18a8505e
AT
538 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
539 files, the value is implicitly zero. For DWARF 5 version DWO files, the
540 value is often implicit and is the size of the header of
541 .debug_str_offsets section (8 or 4, depending on the address size). */
542 gdb::optional<ULONGEST> str_offsets_base;
543
ae038cb0 544 /* Mark used when releasing cached dies. */
9068261f 545 bool mark : 1;
ae038cb0 546
8be455d7
JK
547 /* This CU references .debug_loc. See the symtab->locations_valid field.
548 This test is imperfect as there may exist optimized debug code not using
549 any location list and still facing inlining issues if handled as
550 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 551 bool has_loclist : 1;
ba919b58 552
9068261f 553 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
554 if all the producer_is_* fields are valid. This information is cached
555 because profiling CU expansion showed excessive time spent in
556 producer_is_gxx_lt_4_6. */
9068261f
AB
557 bool checked_producer : 1;
558 bool producer_is_gxx_lt_4_6 : 1;
559 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 560 bool producer_is_icc : 1;
9068261f 561 bool producer_is_icc_lt_14 : 1;
c258c396 562 bool producer_is_codewarrior : 1;
4d4ec4e5 563
9068261f 564 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
565 debugging info for C++ namespaces. GCC 3.3.x did not produce
566 this information, but later versions do. */
567
9068261f 568 bool processing_has_namespace_info : 1;
d590ff25
YQ
569
570 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
571
572 /* If this CU was inherited by another CU (via specification,
573 abstract_origin, etc), this is the ancestor CU. */
574 dwarf2_cu *ancestor;
575
576 /* Get the buildsym_compunit for this CU. */
577 buildsym_compunit *get_builder ()
578 {
579 /* If this CU has a builder associated with it, use that. */
580 if (m_builder != nullptr)
581 return m_builder.get ();
582
583 /* Otherwise, search ancestors for a valid builder. */
584 if (ancestor != nullptr)
585 return ancestor->get_builder ();
586
587 return nullptr;
588 }
e7c27a73
DJ
589};
590
094b34ac
DE
591/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
592 This includes type_unit_group and quick_file_names. */
593
594struct stmt_list_hash
595{
596 /* The DWO unit this table is from or NULL if there is none. */
597 struct dwo_unit *dwo_unit;
598
599 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 600 sect_offset line_sect_off;
094b34ac
DE
601};
602
f4dc4d17
DE
603/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
604 an object of this type. */
605
606struct type_unit_group
607{
0186c6a7 608 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
609 To simplify things we create an artificial CU that "includes" all the
610 type units using this stmt_list so that the rest of the code still has
611 a "per_cu" handle on the symtab.
612 This PER_CU is recognized by having no section. */
8a0459fd 613#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
614 struct dwarf2_per_cu_data per_cu;
615
0186c6a7
DE
616 /* The TUs that share this DW_AT_stmt_list entry.
617 This is added to while parsing type units to build partial symtabs,
618 and is deleted afterwards and not used again. */
a8b3b8e9 619 std::vector<signatured_type *> *tus;
f4dc4d17 620
43f3e411 621 /* The compunit symtab.
094b34ac 622 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
623 so we create an essentially anonymous symtab as the compunit symtab. */
624 struct compunit_symtab *compunit_symtab;
f4dc4d17 625
094b34ac
DE
626 /* The data used to construct the hash key. */
627 struct stmt_list_hash hash;
f4dc4d17
DE
628
629 /* The number of symtabs from the line header.
630 The value here must match line_header.num_file_names. */
631 unsigned int num_symtabs;
632
633 /* The symbol tables for this TU (obtained from the files listed in
634 DW_AT_stmt_list).
635 WARNING: The order of entries here must match the order of entries
636 in the line header. After the first TU using this type_unit_group, the
637 line header for the subsequent TUs is recreated from this. This is done
638 because we need to use the same symtabs for each TU using the same
639 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
640 there's no guarantee the line header doesn't have duplicate entries. */
641 struct symtab **symtabs;
642};
643
73869dc2 644/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
645
646struct dwo_sections
647{
648 struct dwarf2_section_info abbrev;
3019eac3
DE
649 struct dwarf2_section_info line;
650 struct dwarf2_section_info loc;
43988095 651 struct dwarf2_section_info loclists;
09262596
DE
652 struct dwarf2_section_info macinfo;
653 struct dwarf2_section_info macro;
3019eac3
DE
654 struct dwarf2_section_info str;
655 struct dwarf2_section_info str_offsets;
80626a55
DE
656 /* In the case of a virtual DWO file, these two are unused. */
657 struct dwarf2_section_info info;
fd5866f6 658 std::vector<dwarf2_section_info> types;
3019eac3
DE
659};
660
c88ee1f0 661/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
662
663struct dwo_unit
664{
665 /* Backlink to the containing struct dwo_file. */
666 struct dwo_file *dwo_file;
667
668 /* The "id" that distinguishes this CU/TU.
669 .debug_info calls this "dwo_id", .debug_types calls this "signature".
670 Since signatures came first, we stick with it for consistency. */
671 ULONGEST signature;
672
673 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 674 struct dwarf2_section_info *section;
3019eac3 675
9c541725
PA
676 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
677 sect_offset sect_off;
3019eac3
DE
678 unsigned int length;
679
680 /* For types, offset in the type's DIE of the type defined by this TU. */
681 cu_offset type_offset_in_tu;
682};
683
73869dc2
DE
684/* include/dwarf2.h defines the DWP section codes.
685 It defines a max value but it doesn't define a min value, which we
686 use for error checking, so provide one. */
687
688enum dwp_v2_section_ids
689{
690 DW_SECT_MIN = 1
691};
692
80626a55 693/* Data for one DWO file.
57d63ce2
DE
694
695 This includes virtual DWO files (a virtual DWO file is a DWO file as it
696 appears in a DWP file). DWP files don't really have DWO files per se -
697 comdat folding of types "loses" the DWO file they came from, and from
698 a high level view DWP files appear to contain a mass of random types.
699 However, to maintain consistency with the non-DWP case we pretend DWP
700 files contain virtual DWO files, and we assign each TU with one virtual
701 DWO file (generally based on the line and abbrev section offsets -
702 a heuristic that seems to work in practice). */
3019eac3
DE
703
704struct dwo_file
705{
51ac9db5
SM
706 dwo_file () = default;
707 DISABLE_COPY_AND_ASSIGN (dwo_file);
708
18a8505e 709 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
710 For virtual DWO files the name is constructed from the section offsets
711 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
712 from related CU+TUs. */
51ac9db5 713 const char *dwo_name = nullptr;
0ac5b59e
DE
714
715 /* The DW_AT_comp_dir attribute. */
51ac9db5 716 const char *comp_dir = nullptr;
3019eac3 717
80626a55
DE
718 /* The bfd, when the file is open. Otherwise this is NULL.
719 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 720 gdb_bfd_ref_ptr dbfd;
3019eac3 721
73869dc2
DE
722 /* The sections that make up this DWO file.
723 Remember that for virtual DWO files in DWP V2, these are virtual
724 sections (for lack of a better name). */
51ac9db5 725 struct dwo_sections sections {};
3019eac3 726
33c5cd75
DB
727 /* The CUs in the file.
728 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
729 an extension to handle LLVM's Link Time Optimization output (where
730 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 731 htab_up cus;
3019eac3
DE
732
733 /* Table of TUs in the file.
734 Each element is a struct dwo_unit. */
b0b6a987 735 htab_up tus;
3019eac3
DE
736};
737
80626a55
DE
738/* These sections are what may appear in a DWP file. */
739
740struct dwp_sections
741{
73869dc2 742 /* These are used by both DWP version 1 and 2. */
80626a55
DE
743 struct dwarf2_section_info str;
744 struct dwarf2_section_info cu_index;
745 struct dwarf2_section_info tu_index;
73869dc2
DE
746
747 /* These are only used by DWP version 2 files.
748 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
749 sections are referenced by section number, and are not recorded here.
750 In DWP version 2 there is at most one copy of all these sections, each
751 section being (effectively) comprised of the concatenation of all of the
752 individual sections that exist in the version 1 format.
753 To keep the code simple we treat each of these concatenated pieces as a
754 section itself (a virtual section?). */
755 struct dwarf2_section_info abbrev;
756 struct dwarf2_section_info info;
757 struct dwarf2_section_info line;
758 struct dwarf2_section_info loc;
759 struct dwarf2_section_info macinfo;
760 struct dwarf2_section_info macro;
761 struct dwarf2_section_info str_offsets;
762 struct dwarf2_section_info types;
80626a55
DE
763};
764
73869dc2
DE
765/* These sections are what may appear in a virtual DWO file in DWP version 1.
766 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 767
73869dc2 768struct virtual_v1_dwo_sections
80626a55
DE
769{
770 struct dwarf2_section_info abbrev;
771 struct dwarf2_section_info line;
772 struct dwarf2_section_info loc;
773 struct dwarf2_section_info macinfo;
774 struct dwarf2_section_info macro;
775 struct dwarf2_section_info str_offsets;
776 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 777 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
778 struct dwarf2_section_info info_or_types;
779};
780
73869dc2
DE
781/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
782 In version 2, the sections of the DWO files are concatenated together
783 and stored in one section of that name. Thus each ELF section contains
784 several "virtual" sections. */
785
786struct virtual_v2_dwo_sections
787{
788 bfd_size_type abbrev_offset;
789 bfd_size_type abbrev_size;
790
791 bfd_size_type line_offset;
792 bfd_size_type line_size;
793
794 bfd_size_type loc_offset;
795 bfd_size_type loc_size;
796
797 bfd_size_type macinfo_offset;
798 bfd_size_type macinfo_size;
799
800 bfd_size_type macro_offset;
801 bfd_size_type macro_size;
802
803 bfd_size_type str_offsets_offset;
804 bfd_size_type str_offsets_size;
805
806 /* Each DWP hash table entry records one CU or one TU.
807 That is recorded here, and copied to dwo_unit.section. */
808 bfd_size_type info_or_types_offset;
809 bfd_size_type info_or_types_size;
810};
811
80626a55
DE
812/* Contents of DWP hash tables. */
813
814struct dwp_hash_table
815{
73869dc2 816 uint32_t version, nr_columns;
80626a55 817 uint32_t nr_units, nr_slots;
73869dc2
DE
818 const gdb_byte *hash_table, *unit_table;
819 union
820 {
821 struct
822 {
823 const gdb_byte *indices;
824 } v1;
825 struct
826 {
827 /* This is indexed by column number and gives the id of the section
828 in that column. */
829#define MAX_NR_V2_DWO_SECTIONS \
830 (1 /* .debug_info or .debug_types */ \
831 + 1 /* .debug_abbrev */ \
832 + 1 /* .debug_line */ \
833 + 1 /* .debug_loc */ \
834 + 1 /* .debug_str_offsets */ \
835 + 1 /* .debug_macro or .debug_macinfo */)
836 int section_ids[MAX_NR_V2_DWO_SECTIONS];
837 const gdb_byte *offsets;
838 const gdb_byte *sizes;
839 } v2;
840 } section_pool;
80626a55
DE
841};
842
843/* Data for one DWP file. */
844
845struct dwp_file
846{
400174b1
TT
847 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
848 : name (name_),
849 dbfd (std::move (abfd))
850 {
851 }
852
80626a55
DE
853 /* Name of the file. */
854 const char *name;
855
73869dc2 856 /* File format version. */
400174b1 857 int version = 0;
73869dc2 858
93417882 859 /* The bfd. */
400174b1 860 gdb_bfd_ref_ptr dbfd;
80626a55
DE
861
862 /* Section info for this file. */
400174b1 863 struct dwp_sections sections {};
80626a55 864
57d63ce2 865 /* Table of CUs in the file. */
400174b1 866 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
867
868 /* Table of TUs in the file. */
400174b1 869 const struct dwp_hash_table *tus = nullptr;
80626a55 870
19ac8c2e 871 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
872 htab_up loaded_cus;
873 htab_up loaded_tus;
80626a55 874
73869dc2
DE
875 /* Table to map ELF section numbers to their sections.
876 This is only needed for the DWP V1 file format. */
400174b1
TT
877 unsigned int num_sections = 0;
878 asection **elf_sections = nullptr;
80626a55
DE
879};
880
0963b4bd
MS
881/* Struct used to pass misc. parameters to read_die_and_children, et
882 al. which are used for both .debug_info and .debug_types dies.
883 All parameters here are unchanging for the life of the call. This
dee91e82 884 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
885
886struct die_reader_specs
887{
a32a8923 888 /* The bfd of die_section. */
93311388
DE
889 bfd* abfd;
890
891 /* The CU of the DIE we are parsing. */
892 struct dwarf2_cu *cu;
893
80626a55 894 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
895 struct dwo_file *dwo_file;
896
dee91e82 897 /* The section the die comes from.
3019eac3 898 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
899 struct dwarf2_section_info *die_section;
900
901 /* die_section->buffer. */
d521ce57 902 const gdb_byte *buffer;
f664829e
DE
903
904 /* The end of the buffer. */
905 const gdb_byte *buffer_end;
a2ce51a0 906
685af9cd
TT
907 /* The abbreviation table to use when reading the DIEs. */
908 struct abbrev_table *abbrev_table;
93311388
DE
909};
910
c0ab21c2
TT
911/* A subclass of die_reader_specs that holds storage and has complex
912 constructor and destructor behavior. */
913
914class cutu_reader : public die_reader_specs
915{
916public:
917
918 cutu_reader (struct dwarf2_per_cu_data *this_cu,
919 struct abbrev_table *abbrev_table,
920 int use_existing_cu, int keep,
921 bool skip_partial);
922
923 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
924 struct dwarf2_cu *parent_cu = nullptr,
925 struct dwo_file *dwo_file = nullptr);
926
927 ~cutu_reader ();
928
929 DISABLE_COPY_AND_ASSIGN (cutu_reader);
930
931 const gdb_byte *info_ptr = nullptr;
932 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
933 bool dummy_p = false;
934
935private:
936 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
937 int use_existing_cu, int keep);
938
939 struct dwarf2_per_cu_data *m_this_cu;
940 int m_keep = 0;
941 std::unique_ptr<dwarf2_cu> m_new_cu;
942
943 /* The ordinary abbreviation table. */
944 abbrev_table_up m_abbrev_table_holder;
945
946 /* The DWO abbreviation table. */
947 abbrev_table_up m_dwo_abbrev_table;
948};
dee91e82 949
c906108c 950/* When we construct a partial symbol table entry we only
0963b4bd 951 need this much information. */
6f06d47b 952struct partial_die_info : public allocate_on_obstack
c906108c 953 {
6f06d47b
YQ
954 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
955
956 /* Disable assign but still keep copy ctor, which is needed
957 load_partial_dies. */
958 partial_die_info& operator=(const partial_die_info& rhs) = delete;
959
52356b79
YQ
960 /* Adjust the partial die before generating a symbol for it. This
961 function may set the is_external flag or change the DIE's
962 name. */
963 void fixup (struct dwarf2_cu *cu);
964
48fbe735
YQ
965 /* Read a minimal amount of information into the minimal die
966 structure. */
967 const gdb_byte *read (const struct die_reader_specs *reader,
968 const struct abbrev_info &abbrev,
969 const gdb_byte *info_ptr);
970
72bf9492 971 /* Offset of this DIE. */
6f06d47b 972 const sect_offset sect_off;
72bf9492
DJ
973
974 /* DWARF-2 tag for this DIE. */
6f06d47b 975 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 976
72bf9492 977 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
978 const unsigned int has_children : 1;
979
72bf9492
DJ
980 unsigned int is_external : 1;
981 unsigned int is_declaration : 1;
982 unsigned int has_type : 1;
983 unsigned int has_specification : 1;
984 unsigned int has_pc_info : 1;
481860b3 985 unsigned int may_be_inlined : 1;
72bf9492 986
0c1b455e
TT
987 /* This DIE has been marked DW_AT_main_subprogram. */
988 unsigned int main_subprogram : 1;
989
72bf9492
DJ
990 /* Flag set if the SCOPE field of this structure has been
991 computed. */
992 unsigned int scope_set : 1;
993
fa4028e9
JB
994 /* Flag set if the DIE has a byte_size attribute. */
995 unsigned int has_byte_size : 1;
996
ff908ebf
AW
997 /* Flag set if the DIE has a DW_AT_const_value attribute. */
998 unsigned int has_const_value : 1;
999
98bfdba5
PA
1000 /* Flag set if any of the DIE's children are template arguments. */
1001 unsigned int has_template_arguments : 1;
1002
52356b79 1003 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1004 unsigned int fixup_called : 1;
1005
36586728
TT
1006 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1007 unsigned int is_dwz : 1;
1008
1009 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1010 unsigned int spec_is_dwz : 1;
1011
72bf9492 1012 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1013 sometimes a default name for unnamed DIEs. */
6f06d47b 1014 const char *name = nullptr;
72bf9492 1015
abc72ce4 1016 /* The linkage name, if present. */
6f06d47b 1017 const char *linkage_name = nullptr;
abc72ce4 1018
72bf9492
DJ
1019 /* The scope to prepend to our children. This is generally
1020 allocated on the comp_unit_obstack, so will disappear
1021 when this compilation unit leaves the cache. */
6f06d47b 1022 const char *scope = nullptr;
72bf9492 1023
95554aad
TT
1024 /* Some data associated with the partial DIE. The tag determines
1025 which field is live. */
1026 union
1027 {
1028 /* The location description associated with this DIE, if any. */
1029 struct dwarf_block *locdesc;
1030 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1031 sect_offset sect_off;
6f06d47b 1032 } d {};
72bf9492
DJ
1033
1034 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1035 CORE_ADDR lowpc = 0;
1036 CORE_ADDR highpc = 0;
72bf9492 1037
93311388 1038 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1039 DW_AT_sibling, if any. */
48fbe735
YQ
1040 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1041 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1042 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1043
1044 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1045 DW_AT_specification (or DW_AT_abstract_origin or
1046 DW_AT_extension). */
6f06d47b 1047 sect_offset spec_offset {};
72bf9492
DJ
1048
1049 /* Pointers to this DIE's parent, first child, and next sibling,
1050 if any. */
6f06d47b
YQ
1051 struct partial_die_info *die_parent = nullptr;
1052 struct partial_die_info *die_child = nullptr;
1053 struct partial_die_info *die_sibling = nullptr;
1054
1055 friend struct partial_die_info *
1056 dwarf2_cu::find_partial_die (sect_offset sect_off);
1057
1058 private:
1059 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1060 partial_die_info (sect_offset sect_off)
1061 : partial_die_info (sect_off, DW_TAG_padding, 0)
1062 {
1063 }
1064
1065 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1066 int has_children_)
1067 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1068 {
1069 is_external = 0;
1070 is_declaration = 0;
1071 has_type = 0;
1072 has_specification = 0;
1073 has_pc_info = 0;
1074 may_be_inlined = 0;
1075 main_subprogram = 0;
1076 scope_set = 0;
1077 has_byte_size = 0;
1078 has_const_value = 0;
1079 has_template_arguments = 0;
1080 fixup_called = 0;
1081 is_dwz = 0;
1082 spec_is_dwz = 0;
1083 }
c906108c
SS
1084 };
1085
0963b4bd 1086/* This data structure holds a complete die structure. */
c906108c
SS
1087struct die_info
1088 {
76815b17
DE
1089 /* DWARF-2 tag for this DIE. */
1090 ENUM_BITFIELD(dwarf_tag) tag : 16;
1091
1092 /* Number of attributes */
98bfdba5
PA
1093 unsigned char num_attrs;
1094
1095 /* True if we're presently building the full type name for the
1096 type derived from this DIE. */
1097 unsigned char building_fullname : 1;
76815b17 1098
adde2bff
DE
1099 /* True if this die is in process. PR 16581. */
1100 unsigned char in_process : 1;
1101
3e225074
TT
1102 /* True if this DIE has children. */
1103 unsigned char has_children : 1;
1104
76815b17
DE
1105 /* Abbrev number */
1106 unsigned int abbrev;
1107
93311388 1108 /* Offset in .debug_info or .debug_types section. */
9c541725 1109 sect_offset sect_off;
78ba4af6
JB
1110
1111 /* The dies in a compilation unit form an n-ary tree. PARENT
1112 points to this die's parent; CHILD points to the first child of
1113 this node; and all the children of a given node are chained
4950bc1c 1114 together via their SIBLING fields. */
639d11d3
DC
1115 struct die_info *child; /* Its first child, if any. */
1116 struct die_info *sibling; /* Its next sibling, if any. */
1117 struct die_info *parent; /* Its parent, if any. */
c906108c 1118
b60c80d6
DJ
1119 /* An array of attributes, with NUM_ATTRS elements. There may be
1120 zero, but it's not common and zero-sized arrays are not
1121 sufficiently portable C. */
1122 struct attribute attrs[1];
c906108c
SS
1123 };
1124
c906108c
SS
1125/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1126 but this would require a corresponding change in unpack_field_as_long
1127 and friends. */
1128static int bits_per_byte = 8;
1129
2ddeaf8a
TT
1130/* When reading a variant or variant part, we track a bit more
1131 information about the field, and store it in an object of this
1132 type. */
1133
1134struct variant_field
1135{
1136 /* If we see a DW_TAG_variant, then this will be the discriminant
1137 value. */
1138 ULONGEST discriminant_value;
1139 /* If we see a DW_TAG_variant, then this will be set if this is the
1140 default branch. */
1141 bool default_branch;
1142 /* While reading a DW_TAG_variant_part, this will be set if this
1143 field is the discriminant. */
1144 bool is_discriminant;
1145};
1146
52059ffd
TT
1147struct nextfield
1148{
be2daae6
TT
1149 int accessibility = 0;
1150 int virtuality = 0;
2ddeaf8a 1151 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1152 struct variant_field variant {};
1153 struct field field {};
52059ffd
TT
1154};
1155
1156struct fnfieldlist
1157{
be2daae6
TT
1158 const char *name = nullptr;
1159 std::vector<struct fn_field> fnfields;
52059ffd
TT
1160};
1161
c906108c
SS
1162/* The routines that read and process dies for a C struct or C++ class
1163 pass lists of data member fields and lists of member function fields
1164 in an instance of a field_info structure, as defined below. */
1165struct field_info
c5aa993b 1166 {
0963b4bd 1167 /* List of data member and baseclasses fields. */
be2daae6
TT
1168 std::vector<struct nextfield> fields;
1169 std::vector<struct nextfield> baseclasses;
c906108c 1170
7d0ccb61 1171 /* Number of fields (including baseclasses). */
be2daae6 1172 int nfields = 0;
c906108c 1173
85102364 1174 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1175 int non_public_fields = 0;
c906108c 1176
c5aa993b
JM
1177 /* Member function fieldlist array, contains name of possibly overloaded
1178 member function, number of overloaded member functions and a pointer
1179 to the head of the member function field chain. */
be2daae6 1180 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1181
1182 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1183 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1184 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1185
1186 /* Nested types defined by this class and the number of elements in this
1187 list. */
be2daae6 1188 std::vector<struct decl_field> nested_types_list;
c5aa993b 1189 };
c906108c 1190
ae038cb0
DJ
1191/* Loaded secondary compilation units are kept in memory until they
1192 have not been referenced for the processing of this many
1193 compilation units. Set this to zero to disable caching. Cache
1194 sizes of up to at least twenty will improve startup time for
1195 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1196static int dwarf_max_cache_age = 5;
920d2a44 1197static void
b4f54984
DE
1198show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1199 struct cmd_list_element *c, const char *value)
920d2a44 1200{
3e43a32a 1201 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1202 "DWARF compilation units is %s.\n"),
920d2a44
AC
1203 value);
1204}
4390d890 1205\f
c906108c
SS
1206/* local function prototypes */
1207
918dd910
JK
1208static void dwarf2_find_base_address (struct die_info *die,
1209 struct dwarf2_cu *cu);
1210
891813be 1211static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1212 (struct dwarf2_per_cu_data *per_cu, const char *name);
1213
f1902523
JK
1214static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1215 const gdb_byte *info_ptr,
3e225074 1216 struct die_info *type_unit_die);
f1902523 1217
ed2dc618
SM
1218static void dwarf2_build_psymtabs_hard
1219 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1220
72bf9492
DJ
1221static void scan_partial_symbols (struct partial_die_info *,
1222 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1223 int, struct dwarf2_cu *);
c906108c 1224
72bf9492
DJ
1225static void add_partial_symbol (struct partial_die_info *,
1226 struct dwarf2_cu *);
63d06c5c 1227
72bf9492
DJ
1228static void add_partial_namespace (struct partial_die_info *pdi,
1229 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1230 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1231
5d7cb8df 1232static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1233 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1234 struct dwarf2_cu *cu);
1235
72bf9492
DJ
1236static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1237 struct dwarf2_cu *cu);
91c24f0a 1238
bc30ff58
JB
1239static void add_partial_subprogram (struct partial_die_info *pdi,
1240 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1241 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1242
d521ce57 1243static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1244
dee91e82 1245static struct partial_die_info *load_partial_dies
d521ce57 1246 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1247
fb816e8b
TV
1248/* A pair of partial_die_info and compilation unit. */
1249struct cu_partial_die_info
1250{
1251 /* The compilation unit of the partial_die_info. */
1252 struct dwarf2_cu *cu;
1253 /* A partial_die_info. */
1254 struct partial_die_info *pdi;
122cf0f2
AB
1255
1256 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1257 : cu (cu),
1258 pdi (pdi)
405feb71 1259 { /* Nothing. */ }
122cf0f2
AB
1260
1261private:
1262 cu_partial_die_info () = delete;
fb816e8b
TV
1263};
1264
122cf0f2
AB
1265static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1266 struct dwarf2_cu *);
72bf9492 1267
d521ce57
TT
1268static const gdb_byte *read_attribute (const struct die_reader_specs *,
1269 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1270 const gdb_byte *, bool *need_reprocess);
1271
1272static void read_attribute_reprocess (const struct die_reader_specs *reader,
1273 struct attribute *attr);
1274
1275static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1276
d521ce57 1277static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1278 unsigned int *);
c906108c 1279
d521ce57 1280static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1281
1282static LONGEST read_checked_initial_length_and_offset
d521ce57 1283 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1284 unsigned int *, unsigned int *);
613e1657 1285
d521ce57
TT
1286static LONGEST read_offset (bfd *, const gdb_byte *,
1287 const struct comp_unit_head *,
c764a876
DE
1288 unsigned int *);
1289
d521ce57 1290static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1291
ed2dc618
SM
1292static sect_offset read_abbrev_offset
1293 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1294 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1295
d521ce57 1296static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1297
d521ce57 1298static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1299
ed2dc618
SM
1300static const char *read_indirect_string
1301 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1302 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1303
ed2dc618
SM
1304static const char *read_indirect_line_string
1305 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1306 const struct comp_unit_head *, unsigned int *);
36586728 1307
ed2dc618
SM
1308static const char *read_indirect_string_at_offset
1309 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1310 LONGEST str_offset);
927aa2e7 1311
ed2dc618
SM
1312static const char *read_indirect_string_from_dwz
1313 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1314
d521ce57
TT
1315static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1316 const gdb_byte *,
3019eac3
DE
1317 unsigned int *);
1318
18a8505e
AT
1319static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1320 ULONGEST str_index);
1321
1322static const char *read_stub_str_index (struct dwarf2_cu *cu,
1323 ULONGEST str_index);
3019eac3 1324
e142c38c 1325static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1326
e142c38c
DJ
1327static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1328 struct dwarf2_cu *);
c906108c 1329
348e048f 1330static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1331 unsigned int);
348e048f 1332
7d45c7c3
KB
1333static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1334 struct dwarf2_cu *cu);
1335
a084a2a6
AT
1336static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1337
05cf31d1
JB
1338static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1339 struct dwarf2_cu *cu);
1340
e142c38c 1341static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1342
e142c38c 1343static struct die_info *die_specification (struct die_info *die,
f2f0e013 1344 struct dwarf2_cu **);
63d06c5c 1345
9c541725 1346static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1347 struct dwarf2_cu *cu);
debd256d 1348
f3f5162e 1349static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1350 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1351 CORE_ADDR, int decode_mapping);
c906108c 1352
804d2729
TT
1353static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1354 const char *);
c906108c 1355
a14ed312 1356static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1357 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1358
ff39bb5e 1359static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1360 struct dwarf2_cu *);
c906108c 1361
ff39bb5e 1362static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1363 struct type *type,
1364 const char *name,
1365 struct obstack *obstack,
12df843f 1366 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1367 const gdb_byte **bytes,
98bfdba5 1368 struct dwarf2_locexpr_baton **baton);
2df3850c 1369
e7c27a73 1370static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1371
b4ba55a1
JB
1372static int need_gnat_info (struct dwarf2_cu *);
1373
3e43a32a
MS
1374static struct type *die_descriptive_type (struct die_info *,
1375 struct dwarf2_cu *);
b4ba55a1
JB
1376
1377static void set_descriptive_type (struct type *, struct die_info *,
1378 struct dwarf2_cu *);
1379
e7c27a73
DJ
1380static struct type *die_containing_type (struct die_info *,
1381 struct dwarf2_cu *);
c906108c 1382
ff39bb5e 1383static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1384 struct dwarf2_cu *);
c906108c 1385
f792889a 1386static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1387
673bfd45
DE
1388static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1389
0d5cff50 1390static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1391
6e70227d 1392static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1393 const char *suffix, int physname,
1394 struct dwarf2_cu *cu);
63d06c5c 1395
e7c27a73 1396static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1397
348e048f
DE
1398static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1399
e7c27a73 1400static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1401
e7c27a73 1402static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1403
96408a79
SA
1404static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1405
71a3c369
TT
1406static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1407
ff013f42 1408static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1409 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1410
3a2b436a 1411/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1412 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1413enum pc_bounds_kind
1414{
e385593e 1415 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1416 PC_BOUNDS_NOT_PRESENT,
1417
e385593e
JK
1418 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1419 were present but they do not form a valid range of PC addresses. */
1420 PC_BOUNDS_INVALID,
1421
3a2b436a
JK
1422 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1423 PC_BOUNDS_RANGES,
1424
1425 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1426 PC_BOUNDS_HIGH_LOW,
1427};
1428
1429static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1430 CORE_ADDR *, CORE_ADDR *,
1431 struct dwarf2_cu *,
891813be 1432 dwarf2_psymtab *);
c906108c 1433
fae299cd
DC
1434static void get_scope_pc_bounds (struct die_info *,
1435 CORE_ADDR *, CORE_ADDR *,
1436 struct dwarf2_cu *);
1437
801e3a5b
JB
1438static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1439 CORE_ADDR, struct dwarf2_cu *);
1440
a14ed312 1441static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1442 struct dwarf2_cu *);
c906108c 1443
a14ed312 1444static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1445 struct type *, struct dwarf2_cu *);
c906108c 1446
a14ed312 1447static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1448 struct die_info *, struct type *,
e7c27a73 1449 struct dwarf2_cu *);
c906108c 1450
a14ed312 1451static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1452 struct type *,
1453 struct dwarf2_cu *);
c906108c 1454
134d01f1 1455static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1456
e7c27a73 1457static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1458
e7c27a73 1459static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1460
5d7cb8df
JK
1461static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1462
804d2729 1463static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1464
27aa8d6a
SW
1465static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1466
74921315
KS
1467static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1468
f55ee35c
JK
1469static struct type *read_module_type (struct die_info *die,
1470 struct dwarf2_cu *cu);
1471
38d518c9 1472static const char *namespace_name (struct die_info *die,
e142c38c 1473 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1474
134d01f1 1475static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1476
e7c27a73 1477static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1478
6e70227d 1479static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1480 struct dwarf2_cu *);
1481
bf6af496 1482static struct die_info *read_die_and_siblings_1
d521ce57 1483 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1484 struct die_info *);
639d11d3 1485
dee91e82 1486static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1487 const gdb_byte *info_ptr,
1488 const gdb_byte **new_info_ptr,
639d11d3
DC
1489 struct die_info *parent);
1490
d521ce57
TT
1491static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1492 struct die_info **, const gdb_byte *,
3e225074 1493 int);
3019eac3 1494
d521ce57 1495static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1496 struct die_info **, const gdb_byte *);
93311388 1497
e7c27a73 1498static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1499
15d034d0
TT
1500static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1501 struct obstack *);
71c25dea 1502
15d034d0 1503static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1504
15d034d0 1505static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1506 struct die_info *die,
1507 struct dwarf2_cu *cu);
1508
ca69b9e6
DE
1509static const char *dwarf2_physname (const char *name, struct die_info *die,
1510 struct dwarf2_cu *cu);
1511
e142c38c 1512static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1513 struct dwarf2_cu **);
9219021c 1514
f39c6ffd 1515static const char *dwarf_tag_name (unsigned int);
c906108c 1516
f39c6ffd 1517static const char *dwarf_attr_name (unsigned int);
c906108c 1518
a084a2a6
AT
1519static const char *dwarf_unit_type_name (int unit_type);
1520
f39c6ffd 1521static const char *dwarf_form_name (unsigned int);
c906108c 1522
a121b7c1 1523static const char *dwarf_bool_name (unsigned int);
c906108c 1524
f39c6ffd 1525static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1526
f9aca02d 1527static struct die_info *sibling_die (struct die_info *);
c906108c 1528
d97bc12b
DE
1529static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1530
1531static void dump_die_for_error (struct die_info *);
1532
1533static void dump_die_1 (struct ui_file *, int level, int max_level,
1534 struct die_info *);
c906108c 1535
d97bc12b 1536/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1537
51545339 1538static void store_in_ref_table (struct die_info *,
10b3939b 1539 struct dwarf2_cu *);
c906108c 1540
ff39bb5e 1541static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1542
ff39bb5e 1543static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1544
348e048f 1545static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1546 const struct attribute *,
348e048f
DE
1547 struct dwarf2_cu **);
1548
10b3939b 1549static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1550 const struct attribute *,
f2f0e013 1551 struct dwarf2_cu **);
c906108c 1552
348e048f 1553static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1554 const struct attribute *,
348e048f
DE
1555 struct dwarf2_cu **);
1556
ac9ec31b
DE
1557static struct type *get_signatured_type (struct die_info *, ULONGEST,
1558 struct dwarf2_cu *);
1559
1560static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1561 const struct attribute *,
ac9ec31b
DE
1562 struct dwarf2_cu *);
1563
e5fe5e75 1564static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1565
52dc124a 1566static void read_signatured_type (struct signatured_type *);
348e048f 1567
63e43d3a
PMR
1568static int attr_to_dynamic_prop (const struct attribute *attr,
1569 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1570 struct dynamic_prop *prop, struct type *type);
63e43d3a 1571
c906108c
SS
1572/* memory allocation interface */
1573
7b5a2f43 1574static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1575
b60c80d6 1576static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1577
43f3e411 1578static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1579
8cf6f0b1
TT
1580static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1581 struct dwarf2_loclist_baton *baton,
ff39bb5e 1582 const struct attribute *attr);
8cf6f0b1 1583
ff39bb5e 1584static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1585 struct symbol *sym,
f1e6e072
TT
1586 struct dwarf2_cu *cu,
1587 int is_block);
4c2df51b 1588
d521ce57
TT
1589static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1590 const gdb_byte *info_ptr,
1591 struct abbrev_info *abbrev);
4bb7a0a7 1592
72bf9492
DJ
1593static hashval_t partial_die_hash (const void *item);
1594
1595static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1596
ae038cb0 1597static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1598 (sect_offset sect_off, unsigned int offset_in_dwz,
1599 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1600
9816fde3 1601static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1602 struct die_info *comp_unit_die,
1603 enum language pretend_language);
93311388 1604
ed2dc618 1605static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1606
dee91e82 1607static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1608
f792889a
DJ
1609static struct type *set_die_type (struct die_info *, struct type *,
1610 struct dwarf2_cu *);
1c379e20 1611
ed2dc618 1612static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1613
ed2dc618 1614static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1615
58f0c718 1616static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1617 enum language);
10b3939b 1618
95554aad
TT
1619static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1620 enum language);
10b3939b 1621
f4dc4d17
DE
1622static void process_full_type_unit (struct dwarf2_per_cu_data *,
1623 enum language);
1624
10b3939b
DJ
1625static void dwarf2_add_dependence (struct dwarf2_cu *,
1626 struct dwarf2_per_cu_data *);
1627
ae038cb0
DJ
1628static void dwarf2_mark (struct dwarf2_cu *);
1629
1630static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1631
b64f50a1 1632static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1633 struct dwarf2_per_cu_data *);
673bfd45 1634
f792889a 1635static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1636
95554aad
TT
1637static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1638 enum language pretend_language);
1639
ed2dc618 1640static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1641
9a49df9d
AB
1642static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1643static struct type *dwarf2_per_cu_addr_sized_int_type
1644 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
11a8b164
AB
1645static struct type *dwarf2_per_cu_int_type
1646 (struct dwarf2_per_cu_data *per_cu, int size_in_bytes,
1647 bool unsigned_p);
9a49df9d 1648
b303c6f6
AB
1649/* Class, the destructor of which frees all allocated queue entries. This
1650 will only have work to do if an error was thrown while processing the
1651 dwarf. If no error was thrown then the queue entries should have all
1652 been processed, and freed, as we went along. */
1653
1654class dwarf2_queue_guard
1655{
1656public:
39856def
TT
1657 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1658 : m_per_objfile (per_objfile)
1659 {
1660 }
b303c6f6
AB
1661
1662 /* Free any entries remaining on the queue. There should only be
1663 entries left if we hit an error while processing the dwarf. */
1664 ~dwarf2_queue_guard ()
1665 {
39856def
TT
1666 /* Ensure that no memory is allocated by the queue. */
1667 std::queue<dwarf2_queue_item> empty;
1668 std::swap (m_per_objfile->queue, empty);
1669 }
b303c6f6 1670
39856def 1671 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1672
39856def
TT
1673private:
1674 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1675};
1676
39856def
TT
1677dwarf2_queue_item::~dwarf2_queue_item ()
1678{
1679 /* Anything still marked queued is likely to be in an
1680 inconsistent state, so discard it. */
1681 if (per_cu->queued)
1682 {
1683 if (per_cu->cu != NULL)
1684 free_one_cached_comp_unit (per_cu);
1685 per_cu->queued = 0;
1686 }
1687}
1688
d721ba37
PA
1689/* The return type of find_file_and_directory. Note, the enclosed
1690 string pointers are only valid while this object is valid. */
1691
1692struct file_and_directory
1693{
1694 /* The filename. This is never NULL. */
1695 const char *name;
1696
1697 /* The compilation directory. NULL if not known. If we needed to
1698 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1699 points directly to the DW_AT_comp_dir string attribute owned by
1700 the obstack that owns the DIE. */
1701 const char *comp_dir;
1702
1703 /* If we needed to build a new string for comp_dir, this is what
1704 owns the storage. */
1705 std::string comp_dir_storage;
1706};
1707
1708static file_and_directory find_file_and_directory (struct die_info *die,
1709 struct dwarf2_cu *cu);
9291a0cd 1710
43988095
JK
1711/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1712enum class rcuh_kind { COMPILE, TYPE };
1713
d521ce57 1714static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1715 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1716 struct comp_unit_head *header,
36586728 1717 struct dwarf2_section_info *section,
d521ce57 1718 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1719 rcuh_kind section_kind);
36586728 1720
b0b6a987 1721static htab_up allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1722
b0b6a987 1723static htab_up allocate_dwo_unit_table (struct objfile *objfile);
3019eac3 1724
57d63ce2 1725static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1726 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1727 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1728 ULONGEST signature, int is_debug_types);
a2ce51a0 1729
ed2dc618
SM
1730static struct dwp_file *get_dwp_file
1731 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1732
3019eac3 1733static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1734 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1735
1736static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1737 (struct signatured_type *, const char *, const char *);
3019eac3 1738
89e63ee4
DE
1739static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1740
263db9a1
TT
1741/* A unique pointer to a dwo_file. */
1742
51ac9db5 1743typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1744
ed2dc618 1745static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1746
1b80a9fa 1747static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1748
1749static void free_line_header_voidp (void *arg);
4390d890
DE
1750\f
1751/* Various complaints about symbol reading that don't abort the process. */
1752
1753static void
1754dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1755{
b98664d3 1756 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
1757}
1758
1759static void
1760dwarf2_debug_line_missing_file_complaint (void)
1761{
b98664d3 1762 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1763}
1764
1765static void
1766dwarf2_debug_line_missing_end_sequence_complaint (void)
1767{
b98664d3 1768 complaint (_(".debug_line section has line "
4390d890
DE
1769 "program sequence without an end"));
1770}
1771
1772static void
1773dwarf2_complex_location_expr_complaint (void)
1774{
b98664d3 1775 complaint (_("location expression too complex"));
4390d890
DE
1776}
1777
1778static void
1779dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1780 int arg3)
1781{
b98664d3 1782 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1783 arg1, arg2, arg3);
1784}
1785
1786static void
1787dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1788{
b98664d3 1789 complaint (_("debug info runs off end of %s section"
4390d890 1790 " [in module %s]"),
96b79293
TT
1791 section->get_name (),
1792 section->get_file_name ());
4390d890 1793}
1b80a9fa 1794
4390d890
DE
1795static void
1796dwarf2_macro_malformed_definition_complaint (const char *arg1)
1797{
b98664d3 1798 complaint (_("macro debug info contains a "
4390d890
DE
1799 "malformed macro definition:\n`%s'"),
1800 arg1);
1801}
1802
1803static void
1804dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1805{
b98664d3 1806 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1807 arg1, arg2);
1808}
527f3840
JK
1809
1810/* Hash function for line_header_hash. */
1811
1812static hashval_t
1813line_header_hash (const struct line_header *ofs)
1814{
9c541725 1815 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1816}
1817
1818/* Hash function for htab_create_alloc_ex for line_header_hash. */
1819
1820static hashval_t
1821line_header_hash_voidp (const void *item)
1822{
9a3c8263 1823 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1824
1825 return line_header_hash (ofs);
1826}
1827
1828/* Equality function for line_header_hash. */
1829
1830static int
1831line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1832{
9a3c8263
SM
1833 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1834 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1835
9c541725 1836 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1837 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1838}
1839
4390d890 1840\f
9291a0cd 1841
330cdd98
PA
1842/* See declaration. */
1843
1844dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1845 const dwarf2_debug_sections *names,
1846 bool can_copy_)
1847 : objfile (objfile_),
1848 can_copy (can_copy_)
330cdd98
PA
1849{
1850 if (names == NULL)
1851 names = &dwarf2_elf_names;
1852
1853 bfd *obfd = objfile->obfd;
1854
1855 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1856 locate_sections (obfd, sec, *names);
1857}
1858
1859dwarf2_per_objfile::~dwarf2_per_objfile ()
1860{
1861 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1862 free_cached_comp_units ();
1863
b76e467d 1864 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1865 per_cu->imported_symtabs_free ();
fc8e7e75 1866
b2bdb8cf 1867 for (signatured_type *sig_type : all_type_units)
ae640021 1868 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1869
330cdd98
PA
1870 /* Everything else should be on the objfile obstack. */
1871}
1872
1873/* See declaration. */
1874
1875void
1876dwarf2_per_objfile::free_cached_comp_units ()
1877{
1878 dwarf2_per_cu_data *per_cu = read_in_chain;
1879 dwarf2_per_cu_data **last_chain = &read_in_chain;
1880 while (per_cu != NULL)
1881 {
1882 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1883
fcd3b13d 1884 delete per_cu->cu;
330cdd98
PA
1885 *last_chain = next_cu;
1886 per_cu = next_cu;
1887 }
1888}
1889
11ed8cad
TT
1890/* A helper class that calls free_cached_comp_units on
1891 destruction. */
1892
1893class free_cached_comp_units
1894{
1895public:
1896
1897 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1898 : m_per_objfile (per_objfile)
1899 {
1900 }
1901
1902 ~free_cached_comp_units ()
1903 {
1904 m_per_objfile->free_cached_comp_units ();
1905 }
1906
1907 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1908
1909private:
1910
1911 dwarf2_per_objfile *m_per_objfile;
1912};
1913
c906108c 1914/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1915 information and return true if we have enough to do something.
1916 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1917 ELF names are used. CAN_COPY is true for formats where symbol
1918 interposition is possible and so symbol values must follow copy
1919 relocation rules. */
c906108c
SS
1920
1921int
251d32d9 1922dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1923 const struct dwarf2_debug_sections *names,
1924 bool can_copy)
c906108c 1925{
97cbe998
SDJ
1926 if (objfile->flags & OBJF_READNEVER)
1927 return 0;
1928
ed2dc618
SM
1929 struct dwarf2_per_objfile *dwarf2_per_objfile
1930 = get_dwarf2_per_objfile (objfile);
1931
1932 if (dwarf2_per_objfile == NULL)
5bfd760d 1933 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1934 names,
1935 can_copy);
5bfd760d 1936
73869dc2 1937 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1938 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1939 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1940 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1941}
1942
251d32d9
TG
1943/* When loading sections, we look either for uncompressed section or for
1944 compressed section names. */
233a11ab
CS
1945
1946static int
251d32d9
TG
1947section_is_p (const char *section_name,
1948 const struct dwarf2_section_names *names)
233a11ab 1949{
251d32d9
TG
1950 if (names->normal != NULL
1951 && strcmp (section_name, names->normal) == 0)
1952 return 1;
1953 if (names->compressed != NULL
1954 && strcmp (section_name, names->compressed) == 0)
1955 return 1;
1956 return 0;
233a11ab
CS
1957}
1958
330cdd98 1959/* See declaration. */
c906108c 1960
330cdd98
PA
1961void
1962dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1963 const dwarf2_debug_sections &names)
c906108c 1964{
fd361982 1965 flagword aflag = bfd_section_flags (sectp);
251d32d9 1966
dc7650b8
JK
1967 if ((aflag & SEC_HAS_CONTENTS) == 0)
1968 {
1969 }
950b7495
KS
1970 else if (elf_section_data (sectp)->this_hdr.sh_size
1971 > bfd_get_file_size (abfd))
1972 {
1973 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1974 warning (_("Discarding section %s which has a section size (%s"
1975 ") larger than the file size [in module %s]"),
1976 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1977 bfd_get_filename (abfd));
1978 }
330cdd98 1979 else if (section_is_p (sectp->name, &names.info))
c906108c 1980 {
330cdd98 1981 this->info.s.section = sectp;
fd361982 1982 this->info.size = bfd_section_size (sectp);
c906108c 1983 }
330cdd98 1984 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1985 {
330cdd98 1986 this->abbrev.s.section = sectp;
fd361982 1987 this->abbrev.size = bfd_section_size (sectp);
c906108c 1988 }
330cdd98 1989 else if (section_is_p (sectp->name, &names.line))
c906108c 1990 {
330cdd98 1991 this->line.s.section = sectp;
fd361982 1992 this->line.size = bfd_section_size (sectp);
c906108c 1993 }
330cdd98 1994 else if (section_is_p (sectp->name, &names.loc))
c906108c 1995 {
330cdd98 1996 this->loc.s.section = sectp;
fd361982 1997 this->loc.size = bfd_section_size (sectp);
c906108c 1998 }
330cdd98 1999 else if (section_is_p (sectp->name, &names.loclists))
43988095 2000 {
330cdd98 2001 this->loclists.s.section = sectp;
fd361982 2002 this->loclists.size = bfd_section_size (sectp);
43988095 2003 }
330cdd98 2004 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2005 {
330cdd98 2006 this->macinfo.s.section = sectp;
fd361982 2007 this->macinfo.size = bfd_section_size (sectp);
c906108c 2008 }
330cdd98 2009 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2010 {
330cdd98 2011 this->macro.s.section = sectp;
fd361982 2012 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2013 }
330cdd98 2014 else if (section_is_p (sectp->name, &names.str))
c906108c 2015 {
330cdd98 2016 this->str.s.section = sectp;
fd361982 2017 this->str.size = bfd_section_size (sectp);
c906108c 2018 }
18a8505e
AT
2019 else if (section_is_p (sectp->name, &names.str_offsets))
2020 {
2021 this->str_offsets.s.section = sectp;
2022 this->str_offsets.size = bfd_section_size (sectp);
2023 }
330cdd98 2024 else if (section_is_p (sectp->name, &names.line_str))
43988095 2025 {
330cdd98 2026 this->line_str.s.section = sectp;
fd361982 2027 this->line_str.size = bfd_section_size (sectp);
43988095 2028 }
330cdd98 2029 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2030 {
330cdd98 2031 this->addr.s.section = sectp;
fd361982 2032 this->addr.size = bfd_section_size (sectp);
3019eac3 2033 }
330cdd98 2034 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2035 {
330cdd98 2036 this->frame.s.section = sectp;
fd361982 2037 this->frame.size = bfd_section_size (sectp);
b6af0555 2038 }
330cdd98 2039 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2040 {
330cdd98 2041 this->eh_frame.s.section = sectp;
fd361982 2042 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2043 }
330cdd98 2044 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2045 {
330cdd98 2046 this->ranges.s.section = sectp;
fd361982 2047 this->ranges.size = bfd_section_size (sectp);
af34e669 2048 }
330cdd98 2049 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2050 {
330cdd98 2051 this->rnglists.s.section = sectp;
fd361982 2052 this->rnglists.size = bfd_section_size (sectp);
43988095 2053 }
330cdd98 2054 else if (section_is_p (sectp->name, &names.types))
348e048f 2055 {
8b70b953
TT
2056 struct dwarf2_section_info type_section;
2057
2058 memset (&type_section, 0, sizeof (type_section));
049412e3 2059 type_section.s.section = sectp;
fd361982 2060 type_section.size = bfd_section_size (sectp);
8b70b953 2061
fd5866f6 2062 this->types.push_back (type_section);
348e048f 2063 }
330cdd98 2064 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2065 {
330cdd98 2066 this->gdb_index.s.section = sectp;
fd361982 2067 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2068 }
927aa2e7
JK
2069 else if (section_is_p (sectp->name, &names.debug_names))
2070 {
2071 this->debug_names.s.section = sectp;
fd361982 2072 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2073 }
2074 else if (section_is_p (sectp->name, &names.debug_aranges))
2075 {
2076 this->debug_aranges.s.section = sectp;
fd361982 2077 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2078 }
dce234bc 2079
fd361982
AM
2080 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2081 && bfd_section_vma (sectp) == 0)
330cdd98 2082 this->has_section_at_zero = true;
c906108c
SS
2083}
2084
9e0ac564
TT
2085/* A helper function that returns the size of a section in a safe way.
2086 If you are positive that the section has been read before using the
2087 size, then it is safe to refer to the dwarf2_section_info object's
2088 "size" field directly. In other cases, you must call this
2089 function, because for compressed sections the size field is not set
2090 correctly until the section has been read. */
2091
2092static bfd_size_type
2093dwarf2_section_size (struct objfile *objfile,
2094 struct dwarf2_section_info *info)
2095{
2096 if (!info->readin)
96b79293 2097 info->read (objfile);
9e0ac564
TT
2098 return info->size;
2099}
2100
dce234bc 2101/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2102 SECTION_NAME. */
af34e669 2103
dce234bc 2104void
3017a003
TG
2105dwarf2_get_section_info (struct objfile *objfile,
2106 enum dwarf2_section_enum sect,
d521ce57 2107 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2108 bfd_size_type *sizep)
2109{
5bfd760d 2110 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2111 struct dwarf2_section_info *info;
a3b2a86b
TT
2112
2113 /* We may see an objfile without any DWARF, in which case we just
2114 return nothing. */
2115 if (data == NULL)
2116 {
2117 *sectp = NULL;
2118 *bufp = NULL;
2119 *sizep = 0;
2120 return;
2121 }
3017a003
TG
2122 switch (sect)
2123 {
2124 case DWARF2_DEBUG_FRAME:
2125 info = &data->frame;
2126 break;
2127 case DWARF2_EH_FRAME:
2128 info = &data->eh_frame;
2129 break;
2130 default:
2131 gdb_assert_not_reached ("unexpected section");
2132 }
dce234bc 2133
96b79293 2134 info->read (objfile);
dce234bc 2135
96b79293 2136 *sectp = info->get_bfd_section ();
dce234bc
PP
2137 *bufp = info->buffer;
2138 *sizep = info->size;
2139}
2140
36586728
TT
2141/* A helper function to find the sections for a .dwz file. */
2142
2143static void
2144locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2145{
9a3c8263 2146 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2147
2148 /* Note that we only support the standard ELF names, because .dwz
2149 is ELF-only (at the time of writing). */
2150 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2151 {
049412e3 2152 dwz_file->abbrev.s.section = sectp;
fd361982 2153 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2154 }
2155 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2156 {
049412e3 2157 dwz_file->info.s.section = sectp;
fd361982 2158 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2159 }
2160 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2161 {
049412e3 2162 dwz_file->str.s.section = sectp;
fd361982 2163 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2164 }
2165 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2166 {
049412e3 2167 dwz_file->line.s.section = sectp;
fd361982 2168 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2169 }
2170 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2171 {
049412e3 2172 dwz_file->macro.s.section = sectp;
fd361982 2173 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2174 }
2ec9a5e0
TT
2175 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2176 {
049412e3 2177 dwz_file->gdb_index.s.section = sectp;
fd361982 2178 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2179 }
927aa2e7
JK
2180 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2181 {
2182 dwz_file->debug_names.s.section = sectp;
fd361982 2183 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2184 }
36586728
TT
2185}
2186
c4973306 2187/* See dwarf2read.h. */
36586728 2188
c4973306 2189struct dwz_file *
ed2dc618 2190dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2191{
36586728 2192 const char *filename;
acd13123 2193 bfd_size_type buildid_len_arg;
dc294be5
TT
2194 size_t buildid_len;
2195 bfd_byte *buildid;
36586728
TT
2196
2197 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2198 return dwarf2_per_objfile->dwz_file.get ();
36586728 2199
4db1a1dc 2200 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2201 gdb::unique_xmalloc_ptr<char> data
2202 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2203 &buildid_len_arg, &buildid));
4db1a1dc
TT
2204 if (data == NULL)
2205 {
2206 if (bfd_get_error () == bfd_error_no_error)
2207 return NULL;
2208 error (_("could not read '.gnu_debugaltlink' section: %s"),
2209 bfd_errmsg (bfd_get_error ()));
2210 }
791afaa2
TT
2211
2212 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2213
acd13123
TT
2214 buildid_len = (size_t) buildid_len_arg;
2215
791afaa2 2216 filename = data.get ();
d721ba37
PA
2217
2218 std::string abs_storage;
36586728
TT
2219 if (!IS_ABSOLUTE_PATH (filename))
2220 {
14278e1f
TT
2221 gdb::unique_xmalloc_ptr<char> abs
2222 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2223
14278e1f 2224 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2225 filename = abs_storage.c_str ();
36586728
TT
2226 }
2227
dc294be5
TT
2228 /* First try the file name given in the section. If that doesn't
2229 work, try to use the build-id instead. */
192b62ce 2230 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2231 if (dwz_bfd != NULL)
36586728 2232 {
192b62ce 2233 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2234 dwz_bfd.reset (nullptr);
36586728
TT
2235 }
2236
dc294be5
TT
2237 if (dwz_bfd == NULL)
2238 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2239
2240 if (dwz_bfd == NULL)
2241 error (_("could not find '.gnu_debugaltlink' file for %s"),
2242 objfile_name (dwarf2_per_objfile->objfile));
2243
7ff8cb8c
TT
2244 std::unique_ptr<struct dwz_file> result
2245 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2246
7ff8cb8c
TT
2247 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2248 result.get ());
36586728 2249
7ff8cb8c
TT
2250 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2251 result->dwz_bfd.get ());
2252 dwarf2_per_objfile->dwz_file = std::move (result);
2253 return dwarf2_per_objfile->dwz_file.get ();
36586728 2254}
9291a0cd 2255\f
7b9f3c50
DE
2256/* DWARF quick_symbols_functions support. */
2257
2258/* TUs can share .debug_line entries, and there can be a lot more TUs than
2259 unique line tables, so we maintain a separate table of all .debug_line
2260 derived entries to support the sharing.
2261 All the quick functions need is the list of file names. We discard the
2262 line_header when we're done and don't need to record it here. */
2263struct quick_file_names
2264{
094b34ac
DE
2265 /* The data used to construct the hash key. */
2266 struct stmt_list_hash hash;
7b9f3c50
DE
2267
2268 /* The number of entries in file_names, real_names. */
2269 unsigned int num_file_names;
2270
2271 /* The file names from the line table, after being run through
2272 file_full_name. */
2273 const char **file_names;
2274
2275 /* The file names from the line table after being run through
2276 gdb_realpath. These are computed lazily. */
2277 const char **real_names;
2278};
2279
2280/* When using the index (and thus not using psymtabs), each CU has an
2281 object of this type. This is used to hold information needed by
2282 the various "quick" methods. */
2283struct dwarf2_per_cu_quick_data
2284{
2285 /* The file table. This can be NULL if there was no file table
2286 or it's currently not read in.
2287 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2288 struct quick_file_names *file_names;
2289
2290 /* The corresponding symbol table. This is NULL if symbols for this
2291 CU have not yet been read. */
43f3e411 2292 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2293
2294 /* A temporary mark bit used when iterating over all CUs in
2295 expand_symtabs_matching. */
2296 unsigned int mark : 1;
2297
2298 /* True if we've tried to read the file table and found there isn't one.
2299 There will be no point in trying to read it again next time. */
2300 unsigned int no_file_data : 1;
2301};
2302
094b34ac
DE
2303/* Utility hash function for a stmt_list_hash. */
2304
2305static hashval_t
2306hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2307{
2308 hashval_t v = 0;
2309
2310 if (stmt_list_hash->dwo_unit != NULL)
2311 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2312 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2313 return v;
2314}
2315
2316/* Utility equality function for a stmt_list_hash. */
2317
2318static int
2319eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2320 const struct stmt_list_hash *rhs)
2321{
2322 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2323 return 0;
2324 if (lhs->dwo_unit != NULL
2325 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2326 return 0;
2327
9c541725 2328 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2329}
2330
7b9f3c50
DE
2331/* Hash function for a quick_file_names. */
2332
2333static hashval_t
2334hash_file_name_entry (const void *e)
2335{
9a3c8263
SM
2336 const struct quick_file_names *file_data
2337 = (const struct quick_file_names *) e;
7b9f3c50 2338
094b34ac 2339 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2340}
2341
2342/* Equality function for a quick_file_names. */
2343
2344static int
2345eq_file_name_entry (const void *a, const void *b)
2346{
9a3c8263
SM
2347 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2348 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2349
094b34ac 2350 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2351}
2352
2353/* Delete function for a quick_file_names. */
2354
2355static void
2356delete_file_name_entry (void *e)
2357{
9a3c8263 2358 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2359 int i;
2360
2361 for (i = 0; i < file_data->num_file_names; ++i)
2362 {
2363 xfree ((void*) file_data->file_names[i]);
2364 if (file_data->real_names)
2365 xfree ((void*) file_data->real_names[i]);
2366 }
2367
2368 /* The space for the struct itself lives on objfile_obstack,
2369 so we don't free it here. */
2370}
2371
2372/* Create a quick_file_names hash table. */
2373
5895093f 2374static htab_up
7b9f3c50
DE
2375create_quick_file_names_table (unsigned int nr_initial_entries)
2376{
5895093f
TT
2377 return htab_up (htab_create_alloc (nr_initial_entries,
2378 hash_file_name_entry, eq_file_name_entry,
2379 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2380}
9291a0cd 2381
918dd910
JK
2382/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2383 have to be created afterwards. You should call age_cached_comp_units after
2384 processing PER_CU->CU. dw2_setup must have been already called. */
2385
2386static void
58f0c718 2387load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2388{
3019eac3 2389 if (per_cu->is_debug_types)
e5fe5e75 2390 load_full_type_unit (per_cu);
918dd910 2391 else
58f0c718 2392 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2393
cc12ce38
DE
2394 if (per_cu->cu == NULL)
2395 return; /* Dummy CU. */
2dc860c0
DE
2396
2397 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2398}
2399
a0f42c21 2400/* Read in the symbols for PER_CU. */
2fdf6df6 2401
9291a0cd 2402static void
58f0c718 2403dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2404{
ed2dc618 2405 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2406
f4dc4d17
DE
2407 /* Skip type_unit_groups, reading the type units they contain
2408 is handled elsewhere. */
2409 if (IS_TYPE_UNIT_GROUP (per_cu))
2410 return;
2411
b303c6f6
AB
2412 /* The destructor of dwarf2_queue_guard frees any entries left on
2413 the queue. After this point we're guaranteed to leave this function
2414 with the dwarf queue empty. */
39856def 2415 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2416
95554aad 2417 if (dwarf2_per_objfile->using_index
43f3e411 2418 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2419 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2420 {
2421 queue_comp_unit (per_cu, language_minimal);
58f0c718 2422 load_cu (per_cu, skip_partial);
89e63ee4
DE
2423
2424 /* If we just loaded a CU from a DWO, and we're working with an index
2425 that may badly handle TUs, load all the TUs in that DWO as well.
2426 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2427 if (!per_cu->is_debug_types
cc12ce38 2428 && per_cu->cu != NULL
89e63ee4
DE
2429 && per_cu->cu->dwo_unit != NULL
2430 && dwarf2_per_objfile->index_table != NULL
2431 && dwarf2_per_objfile->index_table->version <= 7
2432 /* DWP files aren't supported yet. */
ed2dc618 2433 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2434 queue_and_load_all_dwo_tus (per_cu);
95554aad 2435 }
9291a0cd 2436
ed2dc618 2437 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2438
2439 /* Age the cache, releasing compilation units that have not
2440 been used recently. */
ed2dc618 2441 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2442}
2443
2444/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2445 the objfile from which this CU came. Returns the resulting symbol
2446 table. */
2fdf6df6 2447
43f3e411 2448static struct compunit_symtab *
58f0c718 2449dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2450{
ed2dc618
SM
2451 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2452
95554aad 2453 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2454 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2455 {
11ed8cad 2456 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2457 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2458 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2459 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2460 }
f194fefb 2461
43f3e411 2462 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2463}
2464
ff4c9fec 2465/* See declaration. */
f4dc4d17 2466
ff4c9fec
SM
2467dwarf2_per_cu_data *
2468dwarf2_per_objfile::get_cutu (int index)
2469{
b76e467d 2470 if (index >= this->all_comp_units.size ())
ff4c9fec 2471 {
b76e467d 2472 index -= this->all_comp_units.size ();
b2bdb8cf 2473 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2474 return &this->all_type_units[index]->per_cu;
2475 }
f4dc4d17 2476
ff4c9fec
SM
2477 return this->all_comp_units[index];
2478}
f4dc4d17 2479
ff4c9fec 2480/* See declaration. */
2fdf6df6 2481
ff4c9fec
SM
2482dwarf2_per_cu_data *
2483dwarf2_per_objfile::get_cu (int index)
1fd400ff 2484{
b76e467d 2485 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2486
ff4c9fec 2487 return this->all_comp_units[index];
f4dc4d17
DE
2488}
2489
ff4c9fec 2490/* See declaration. */
f4dc4d17 2491
ff4c9fec
SM
2492signatured_type *
2493dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2494{
b2bdb8cf 2495 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2496
ff4c9fec 2497 return this->all_type_units[index];
1fd400ff
TT
2498}
2499
4b514bc8
JK
2500/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2501 objfile_obstack, and constructed with the specified field
2502 values. */
2503
2504static dwarf2_per_cu_data *
ed2dc618 2505create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2506 struct dwarf2_section_info *section,
2507 int is_dwz,
2508 sect_offset sect_off, ULONGEST length)
2509{
ed2dc618 2510 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2511 dwarf2_per_cu_data *the_cu
2512 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2513 struct dwarf2_per_cu_data);
2514 the_cu->sect_off = sect_off;
2515 the_cu->length = length;
e3b94546 2516 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2517 the_cu->section = section;
2518 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct dwarf2_per_cu_quick_data);
2520 the_cu->is_dwz = is_dwz;
2521 return the_cu;
2522}
2523
2ec9a5e0
TT
2524/* A helper for create_cus_from_index that handles a given list of
2525 CUs. */
2fdf6df6 2526
74a0d9f6 2527static void
12359b5e 2528create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2529 const gdb_byte *cu_list, offset_type n_elements,
2530 struct dwarf2_section_info *section,
b76e467d 2531 int is_dwz)
9291a0cd 2532{
12359b5e 2533 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2534 {
74a0d9f6 2535 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2536
2537 sect_offset sect_off
2538 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2539 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2540 cu_list += 2 * 8;
2541
b76e467d 2542 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2543 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2544 sect_off, length);
b76e467d 2545 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2546 }
9291a0cd
TT
2547}
2548
2ec9a5e0 2549/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2550 the CU objects for this objfile. */
2ec9a5e0 2551
74a0d9f6 2552static void
12359b5e 2553create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2554 const gdb_byte *cu_list, offset_type cu_list_elements,
2555 const gdb_byte *dwz_list, offset_type dwz_elements)
2556{
b76e467d
SM
2557 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2558 dwarf2_per_objfile->all_comp_units.reserve
2559 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2560
12359b5e 2561 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2562 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2563
2564 if (dwz_elements == 0)
74a0d9f6 2565 return;
2ec9a5e0 2566
12359b5e
SM
2567 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2568 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2569 &dwz->info, 1);
2ec9a5e0
TT
2570}
2571
1fd400ff 2572/* Create the signatured type hash table from the index. */
673bfd45 2573
74a0d9f6 2574static void
12359b5e
SM
2575create_signatured_type_table_from_index
2576 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2577 struct dwarf2_section_info *section,
2578 const gdb_byte *bytes,
2579 offset_type elements)
1fd400ff 2580{
12359b5e 2581 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2582
b2bdb8cf
SM
2583 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2584 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2585
b0b6a987 2586 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 2587
12359b5e 2588 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2589 {
52dc124a 2590 struct signatured_type *sig_type;
9c541725 2591 ULONGEST signature;
1fd400ff 2592 void **slot;
9c541725 2593 cu_offset type_offset_in_tu;
1fd400ff 2594
74a0d9f6 2595 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2596 sect_offset sect_off
2597 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2598 type_offset_in_tu
2599 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2600 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2601 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2602 bytes += 3 * 8;
2603
52dc124a 2604 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2605 struct signatured_type);
52dc124a 2606 sig_type->signature = signature;
9c541725 2607 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2608 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2609 sig_type->per_cu.section = section;
9c541725 2610 sig_type->per_cu.sect_off = sect_off;
e3b94546 2611 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2612 sig_type->per_cu.v.quick
1fd400ff
TT
2613 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2614 struct dwarf2_per_cu_quick_data);
2615
b0b6a987 2616 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2617 *slot = sig_type;
1fd400ff 2618
b2bdb8cf 2619 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2620 }
2621
b0b6a987 2622 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2623}
2624
927aa2e7
JK
2625/* Create the signatured type hash table from .debug_names. */
2626
2627static void
2628create_signatured_type_table_from_debug_names
ed2dc618 2629 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2630 const mapped_debug_names &map,
2631 struct dwarf2_section_info *section,
2632 struct dwarf2_section_info *abbrev_section)
2633{
ed2dc618
SM
2634 struct objfile *objfile = dwarf2_per_objfile->objfile;
2635
96b79293
TT
2636 section->read (objfile);
2637 abbrev_section->read (objfile);
927aa2e7 2638
b2bdb8cf
SM
2639 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2640 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2641
b0b6a987 2642 htab_up sig_types_hash = allocate_signatured_type_table (objfile);
927aa2e7
JK
2643
2644 for (uint32_t i = 0; i < map.tu_count; ++i)
2645 {
2646 struct signatured_type *sig_type;
927aa2e7 2647 void **slot;
927aa2e7
JK
2648
2649 sect_offset sect_off
2650 = (sect_offset) (extract_unsigned_integer
2651 (map.tu_table_reordered + i * map.offset_size,
2652 map.offset_size,
2653 map.dwarf5_byte_order));
2654
2655 comp_unit_head cu_header;
ed2dc618
SM
2656 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2657 abbrev_section,
927aa2e7
JK
2658 section->buffer + to_underlying (sect_off),
2659 rcuh_kind::TYPE);
2660
2661 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2662 struct signatured_type);
2663 sig_type->signature = cu_header.signature;
2664 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2665 sig_type->per_cu.is_debug_types = 1;
2666 sig_type->per_cu.section = section;
2667 sig_type->per_cu.sect_off = sect_off;
e3b94546 2668 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2669 sig_type->per_cu.v.quick
2670 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2671 struct dwarf2_per_cu_quick_data);
2672
b0b6a987 2673 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2674 *slot = sig_type;
2675
b2bdb8cf 2676 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2677 }
2678
b0b6a987 2679 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2680}
2681
9291a0cd
TT
2682/* Read the address map data from the mapped index, and use it to
2683 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2684
9291a0cd 2685static void
ed2dc618
SM
2686create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2687 struct mapped_index *index)
9291a0cd 2688{
ed2dc618 2689 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2691 const gdb_byte *iter, *end;
9291a0cd 2692 struct addrmap *mutable_map;
9291a0cd
TT
2693 CORE_ADDR baseaddr;
2694
8268c778
PA
2695 auto_obstack temp_obstack;
2696
9291a0cd
TT
2697 mutable_map = addrmap_create_mutable (&temp_obstack);
2698
f00a2de2
PA
2699 iter = index->address_table.data ();
2700 end = iter + index->address_table.size ();
9291a0cd 2701
b3b3bada 2702 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2703
2704 while (iter < end)
2705 {
2706 ULONGEST hi, lo, cu_index;
2707 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2708 iter += 8;
2709 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2710 iter += 8;
2711 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2712 iter += 4;
f652bce2 2713
24a55014 2714 if (lo > hi)
f652bce2 2715 {
b98664d3 2716 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2717 hex_string (lo), hex_string (hi));
24a55014 2718 continue;
f652bce2 2719 }
24a55014 2720
b76e467d 2721 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2722 {
b98664d3 2723 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2724 (unsigned) cu_index);
24a55014 2725 continue;
f652bce2 2726 }
24a55014 2727
79748972
TT
2728 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2729 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2730 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2731 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2732 }
2733
d320c2b5 2734 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2735 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2736}
2737
927aa2e7
JK
2738/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2739 populate the objfile's psymtabs_addrmap. */
2740
2741static void
ed2dc618 2742create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2743 struct dwarf2_section_info *section)
2744{
ed2dc618 2745 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2746 bfd *abfd = objfile->obfd;
2747 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2748 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2749
2750 auto_obstack temp_obstack;
2751 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2752
2753 std::unordered_map<sect_offset,
2754 dwarf2_per_cu_data *,
2755 gdb::hash_enum<sect_offset>>
2756 debug_info_offset_to_per_cu;
b76e467d 2757 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2758 {
927aa2e7
JK
2759 const auto insertpair
2760 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2761 if (!insertpair.second)
2762 {
2763 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2764 "debug_info_offset %s, ignoring .debug_aranges."),
2765 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2766 return;
2767 }
2768 }
2769
96b79293 2770 section->read (objfile);
927aa2e7
JK
2771
2772 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2773
2774 const gdb_byte *addr = section->buffer;
2775
2776 while (addr < section->buffer + section->size)
2777 {
2778 const gdb_byte *const entry_addr = addr;
2779 unsigned int bytes_read;
2780
2781 const LONGEST entry_length = read_initial_length (abfd, addr,
2782 &bytes_read);
2783 addr += bytes_read;
2784
2785 const gdb_byte *const entry_end = addr + entry_length;
2786 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2787 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2788 if (addr + entry_length > section->buffer + section->size)
2789 {
47e3f474 2790 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2791 "length %s exceeds section length %s, "
2792 "ignoring .debug_aranges."),
47e3f474
TV
2793 objfile_name (objfile),
2794 plongest (entry_addr - section->buffer),
927aa2e7
JK
2795 plongest (bytes_read + entry_length),
2796 pulongest (section->size));
2797 return;
2798 }
2799
2800 /* The version number. */
2801 const uint16_t version = read_2_bytes (abfd, addr);
2802 addr += 2;
2803 if (version != 2)
2804 {
47e3f474 2805 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2806 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2807 objfile_name (objfile),
2808 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2809 return;
2810 }
2811
2812 const uint64_t debug_info_offset
2813 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2814 addr += offset_size;
2815 const auto per_cu_it
2816 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2817 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2818 {
47e3f474 2819 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2820 "debug_info_offset %s does not exists, "
2821 "ignoring .debug_aranges."),
47e3f474
TV
2822 objfile_name (objfile),
2823 plongest (entry_addr - section->buffer),
927aa2e7
JK
2824 pulongest (debug_info_offset));
2825 return;
2826 }
2827 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2828
2829 const uint8_t address_size = *addr++;
2830 if (address_size < 1 || address_size > 8)
2831 {
47e3f474 2832 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2833 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2834 objfile_name (objfile),
2835 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2836 return;
2837 }
2838
2839 const uint8_t segment_selector_size = *addr++;
2840 if (segment_selector_size != 0)
2841 {
47e3f474 2842 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2843 "segment_selector_size %u is not supported, "
2844 "ignoring .debug_aranges."),
47e3f474
TV
2845 objfile_name (objfile),
2846 plongest (entry_addr - section->buffer),
927aa2e7
JK
2847 segment_selector_size);
2848 return;
2849 }
2850
2851 /* Must pad to an alignment boundary that is twice the address
2852 size. It is undocumented by the DWARF standard but GCC does
2853 use it. */
2854 for (size_t padding = ((-(addr - section->buffer))
2855 & (2 * address_size - 1));
2856 padding > 0; padding--)
2857 if (*addr++ != 0)
2858 {
47e3f474 2859 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2860 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2861 objfile_name (objfile),
2862 plongest (entry_addr - section->buffer));
927aa2e7
JK
2863 return;
2864 }
2865
2866 for (;;)
2867 {
2868 if (addr + 2 * address_size > entry_end)
2869 {
47e3f474 2870 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2871 "address list is not properly terminated, "
2872 "ignoring .debug_aranges."),
47e3f474
TV
2873 objfile_name (objfile),
2874 plongest (entry_addr - section->buffer));
927aa2e7
JK
2875 return;
2876 }
2877 ULONGEST start = extract_unsigned_integer (addr, address_size,
2878 dwarf5_byte_order);
2879 addr += address_size;
2880 ULONGEST length = extract_unsigned_integer (addr, address_size,
2881 dwarf5_byte_order);
2882 addr += address_size;
2883 if (start == 0 && length == 0)
2884 break;
2885 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2886 {
2887 /* Symbol was eliminated due to a COMDAT group. */
2888 continue;
2889 }
2890 ULONGEST end = start + length;
79748972
TT
2891 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2892 - baseaddr);
2893 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2894 - baseaddr);
927aa2e7
JK
2895 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2896 }
2897 }
2898
d320c2b5 2899 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2900 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2901}
2902
9291a0cd
TT
2903/* Find a slot in the mapped index INDEX for the object named NAME.
2904 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2905 constant pool and return true. If NAME cannot be found, return
2906 false. */
2fdf6df6 2907
109483d9 2908static bool
9291a0cd
TT
2909find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2910 offset_type **vec_out)
2911{
0cf03b49 2912 offset_type hash;
9291a0cd 2913 offset_type slot, step;
559a7a62 2914 int (*cmp) (const char *, const char *);
9291a0cd 2915
791afaa2 2916 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2917 if (current_language->la_language == language_cplus
45280282
IB
2918 || current_language->la_language == language_fortran
2919 || current_language->la_language == language_d)
0cf03b49
JK
2920 {
2921 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2922 not contain any. */
a8719064 2923
72998fb3 2924 if (strchr (name, '(') != NULL)
0cf03b49 2925 {
109483d9 2926 without_params = cp_remove_params (name);
0cf03b49 2927
72998fb3 2928 if (without_params != NULL)
791afaa2 2929 name = without_params.get ();
0cf03b49
JK
2930 }
2931 }
2932
559a7a62 2933 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2934 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2935 simulate our NAME being searched is also lowercased. */
2936 hash = mapped_index_string_hash ((index->version == 4
2937 && case_sensitivity == case_sensitive_off
2938 ? 5 : index->version),
2939 name);
2940
f00a2de2
PA
2941 slot = hash & (index->symbol_table.size () - 1);
2942 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2943 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2944
2945 for (;;)
2946 {
9291a0cd 2947 const char *str;
f00a2de2
PA
2948
2949 const auto &bucket = index->symbol_table[slot];
2950 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2951 return false;
9291a0cd 2952
f00a2de2 2953 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2954 if (!cmp (name, str))
9291a0cd
TT
2955 {
2956 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2957 + MAYBE_SWAP (bucket.vec));
109483d9 2958 return true;
9291a0cd
TT
2959 }
2960
f00a2de2 2961 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2962 }
2963}
2964
4485a1c1
SM
2965/* A helper function that reads the .gdb_index from BUFFER and fills
2966 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2967 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2968 ok to use deprecated sections.
2969
2970 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2971 out parameters that are filled in with information about the CU and
2972 TU lists in the section.
2973
4485a1c1 2974 Returns true if all went well, false otherwise. */
2fdf6df6 2975
d33bc52e 2976static bool
4485a1c1
SM
2977read_gdb_index_from_buffer (struct objfile *objfile,
2978 const char *filename,
2979 bool deprecated_ok,
2980 gdb::array_view<const gdb_byte> buffer,
2981 struct mapped_index *map,
2982 const gdb_byte **cu_list,
2983 offset_type *cu_list_elements,
2984 const gdb_byte **types_list,
2985 offset_type *types_list_elements)
2986{
2987 const gdb_byte *addr = &buffer[0];
82430852 2988
9291a0cd 2989 /* Version check. */
4485a1c1 2990 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2991 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2992 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2993 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2994 indices. */
831adc1f 2995 if (version < 4)
481860b3
GB
2996 {
2997 static int warning_printed = 0;
2998 if (!warning_printed)
2999 {
3000 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3001 filename);
481860b3
GB
3002 warning_printed = 1;
3003 }
3004 return 0;
3005 }
3006 /* Index version 4 uses a different hash function than index version
3007 5 and later.
3008
3009 Versions earlier than 6 did not emit psymbols for inlined
3010 functions. Using these files will cause GDB not to be able to
3011 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3012 indices unless the user has done
3013 "set use-deprecated-index-sections on". */
2ec9a5e0 3014 if (version < 6 && !deprecated_ok)
481860b3
GB
3015 {
3016 static int warning_printed = 0;
3017 if (!warning_printed)
3018 {
e615022a
DE
3019 warning (_("\
3020Skipping deprecated .gdb_index section in %s.\n\
3021Do \"set use-deprecated-index-sections on\" before the file is read\n\
3022to use the section anyway."),
2ec9a5e0 3023 filename);
481860b3
GB
3024 warning_printed = 1;
3025 }
3026 return 0;
3027 }
796a7ff8 3028 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3029 of the TU (for symbols coming from TUs),
3030 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3031 Plus gold-generated indices can have duplicate entries for global symbols,
3032 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3033 These are just performance bugs, and we can't distinguish gdb-generated
3034 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3035
481860b3 3036 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3037 longer backward compatible. */
796a7ff8 3038 if (version > 8)
594e8718 3039 return 0;
9291a0cd 3040
559a7a62 3041 map->version = version;
9291a0cd 3042
4485a1c1 3043 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3044
4485a1c1 3045 int i = 0;
2ec9a5e0
TT
3046 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3047 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3048 / 8);
1fd400ff
TT
3049 ++i;
3050
2ec9a5e0
TT
3051 *types_list = addr + MAYBE_SWAP (metadata[i]);
3052 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3053 - MAYBE_SWAP (metadata[i]))
3054 / 8);
987d643c 3055 ++i;
1fd400ff 3056
f00a2de2
PA
3057 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3058 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3059 map->address_table
3060 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3061 ++i;
3062
f00a2de2
PA
3063 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3064 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3065 map->symbol_table
3066 = gdb::array_view<mapped_index::symbol_table_slot>
3067 ((mapped_index::symbol_table_slot *) symbol_table,
3068 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3069
f00a2de2 3070 ++i;
f9d83a0b 3071 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3072
2ec9a5e0
TT
3073 return 1;
3074}
3075
4485a1c1
SM
3076/* Callback types for dwarf2_read_gdb_index. */
3077
3078typedef gdb::function_view
3079 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3080 get_gdb_index_contents_ftype;
3081typedef gdb::function_view
3082 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3083 get_gdb_index_contents_dwz_ftype;
3084
927aa2e7 3085/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3086 elements of all the CUs and return 1. Otherwise, return 0. */
3087
3088static int
4485a1c1
SM
3089dwarf2_read_gdb_index
3090 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3091 get_gdb_index_contents_ftype get_gdb_index_contents,
3092 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3093{
2ec9a5e0
TT
3094 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3095 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3096 struct dwz_file *dwz;
12359b5e 3097 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3098
4485a1c1
SM
3099 gdb::array_view<const gdb_byte> main_index_contents
3100 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3101
3102 if (main_index_contents.empty ())
3103 return 0;
3104
3063847f 3105 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3106 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3107 use_deprecated_index_sections,
3108 main_index_contents, map.get (), &cu_list,
3109 &cu_list_elements, &types_list,
3110 &types_list_elements))
2ec9a5e0
TT
3111 return 0;
3112
0fefef59 3113 /* Don't use the index if it's empty. */
3063847f 3114 if (map->symbol_table.empty ())
0fefef59
DE
3115 return 0;
3116
2ec9a5e0
TT
3117 /* If there is a .dwz file, read it so we can get its CU list as
3118 well. */
ed2dc618 3119 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3120 if (dwz != NULL)
2ec9a5e0 3121 {
2ec9a5e0
TT
3122 struct mapped_index dwz_map;
3123 const gdb_byte *dwz_types_ignore;
3124 offset_type dwz_types_elements_ignore;
3125
4485a1c1
SM
3126 gdb::array_view<const gdb_byte> dwz_index_content
3127 = get_gdb_index_contents_dwz (objfile, dwz);
3128
3129 if (dwz_index_content.empty ())
3130 return 0;
3131
3132 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3133 bfd_get_filename (dwz->dwz_bfd.get ()),
3134 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3135 &dwz_list, &dwz_list_elements,
3136 &dwz_types_ignore,
3137 &dwz_types_elements_ignore))
2ec9a5e0
TT
3138 {
3139 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3140 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3141 return 0;
3142 }
3143 }
3144
12359b5e
SM
3145 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3146 dwz_list, dwz_list_elements);
1fd400ff 3147
8b70b953
TT
3148 if (types_list_elements)
3149 {
8b70b953
TT
3150 /* We can only handle a single .debug_types when we have an
3151 index. */
fd5866f6 3152 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3153 return 0;
3154
fd5866f6 3155 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3156
12359b5e
SM
3157 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3158 types_list, types_list_elements);
8b70b953 3159 }
9291a0cd 3160
3063847f 3161 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3162
3063847f 3163 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3164 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3165 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3166 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3167
3168 return 1;
3169}
3170
dee91e82 3171/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3172
dee91e82
DE
3173static void
3174dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3175 const gdb_byte *info_ptr,
3e225074 3176 struct die_info *comp_unit_die)
9291a0cd 3177{
dee91e82 3178 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3179 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3180 struct dwarf2_per_objfile *dwarf2_per_objfile
3181 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3182 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3183 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3184 struct attribute *attr;
7b9f3c50
DE
3185 void **slot;
3186 struct quick_file_names *qfn;
9291a0cd 3187
0186c6a7
DE
3188 gdb_assert (! this_cu->is_debug_types);
3189
07261596
TT
3190 /* Our callers never want to match partial units -- instead they
3191 will match the enclosing full CU. */
3192 if (comp_unit_die->tag == DW_TAG_partial_unit)
3193 {
3194 this_cu->v.quick->no_file_data = 1;
3195 return;
3196 }
3197
0186c6a7 3198 lh_cu = this_cu;
7b9f3c50 3199 slot = NULL;
dee91e82 3200
fff8551c 3201 line_header_up lh;
9c541725 3202 sect_offset line_offset {};
fff8551c 3203
dee91e82 3204 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3205 if (attr != nullptr)
9291a0cd 3206 {
7b9f3c50
DE
3207 struct quick_file_names find_entry;
3208
9c541725 3209 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3210
3211 /* We may have already read in this line header (TU line header sharing).
3212 If we have we're done. */
094b34ac 3213 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3214 find_entry.hash.line_sect_off = line_offset;
5895093f 3215 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3216 &find_entry, INSERT);
3217 if (*slot != NULL)
3218 {
9a3c8263 3219 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3220 return;
7b9f3c50
DE
3221 }
3222
3019eac3 3223 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3224 }
3225 if (lh == NULL)
3226 {
094b34ac 3227 lh_cu->v.quick->no_file_data = 1;
dee91e82 3228 return;
9291a0cd
TT
3229 }
3230
8d749320 3231 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3232 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3233 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3234 gdb_assert (slot != NULL);
3235 *slot = qfn;
9291a0cd 3236
d721ba37 3237 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3238
aa391654
TT
3239 int offset = 0;
3240 if (strcmp (fnd.name, "<unknown>") != 0)
3241 ++offset;
3242
7ba99d21 3243 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3244 qfn->file_names =
aa391654
TT
3245 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3246 if (offset != 0)
3247 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3248 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3249 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3250 fnd.comp_dir).release ();
7b9f3c50 3251 qfn->real_names = NULL;
9291a0cd 3252
094b34ac 3253 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3254}
3255
3256/* A helper for the "quick" functions which attempts to read the line
3257 table for THIS_CU. */
3258
3259static struct quick_file_names *
e4a48d9d 3260dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3261{
0186c6a7
DE
3262 /* This should never be called for TUs. */
3263 gdb_assert (! this_cu->is_debug_types);
3264 /* Nor type unit groups. */
3265 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3266
dee91e82
DE
3267 if (this_cu->v.quick->file_names != NULL)
3268 return this_cu->v.quick->file_names;
3269 /* If we know there is no line data, no point in looking again. */
3270 if (this_cu->v.quick->no_file_data)
3271 return NULL;
3272
c0ab21c2
TT
3273 cutu_reader reader (this_cu);
3274 if (!reader.dummy_p)
3e225074 3275 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3276
3277 if (this_cu->v.quick->no_file_data)
3278 return NULL;
3279 return this_cu->v.quick->file_names;
9291a0cd
TT
3280}
3281
3282/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3283 real path for a given file name from the line table. */
2fdf6df6 3284
9291a0cd 3285static const char *
7b9f3c50
DE
3286dw2_get_real_path (struct objfile *objfile,
3287 struct quick_file_names *qfn, int index)
9291a0cd 3288{
7b9f3c50
DE
3289 if (qfn->real_names == NULL)
3290 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3291 qfn->num_file_names, const char *);
9291a0cd 3292
7b9f3c50 3293 if (qfn->real_names[index] == NULL)
14278e1f 3294 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3295
7b9f3c50 3296 return qfn->real_names[index];
9291a0cd
TT
3297}
3298
3299static struct symtab *
3300dw2_find_last_source_symtab (struct objfile *objfile)
3301{
ed2dc618
SM
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
b76e467d 3304 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3305 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3306
43f3e411
DE
3307 if (cust == NULL)
3308 return NULL;
ed2dc618 3309
43f3e411 3310 return compunit_primary_filetab (cust);
9291a0cd
TT
3311}
3312
7b9f3c50
DE
3313/* Traversal function for dw2_forget_cached_source_info. */
3314
3315static int
3316dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3317{
7b9f3c50 3318 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3319
7b9f3c50 3320 if (file_data->real_names)
9291a0cd 3321 {
7b9f3c50 3322 int i;
9291a0cd 3323
7b9f3c50 3324 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3325 {
7b9f3c50
DE
3326 xfree ((void*) file_data->real_names[i]);
3327 file_data->real_names[i] = NULL;
9291a0cd
TT
3328 }
3329 }
7b9f3c50
DE
3330
3331 return 1;
3332}
3333
3334static void
3335dw2_forget_cached_source_info (struct objfile *objfile)
3336{
ed2dc618
SM
3337 struct dwarf2_per_objfile *dwarf2_per_objfile
3338 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3339
5895093f 3340 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3341 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3342}
3343
f8eba3c6
TT
3344/* Helper function for dw2_map_symtabs_matching_filename that expands
3345 the symtabs and calls the iterator. */
3346
3347static int
3348dw2_map_expand_apply (struct objfile *objfile,
3349 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3350 const char *name, const char *real_path,
14bc53a8 3351 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3352{
43f3e411 3353 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3354
3355 /* Don't visit already-expanded CUs. */
43f3e411 3356 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3357 return 0;
3358
3359 /* This may expand more than one symtab, and we want to iterate over
3360 all of them. */
58f0c718 3361 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3362
14bc53a8
PA
3363 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3364 last_made, callback);
f8eba3c6
TT
3365}
3366
3367/* Implementation of the map_symtabs_matching_filename method. */
3368
14bc53a8
PA
3369static bool
3370dw2_map_symtabs_matching_filename
3371 (struct objfile *objfile, const char *name, const char *real_path,
3372 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3373{
c011a4f4 3374 const char *name_basename = lbasename (name);
ed2dc618
SM
3375 struct dwarf2_per_objfile *dwarf2_per_objfile
3376 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3377
848e3e78
DE
3378 /* The rule is CUs specify all the files, including those used by
3379 any TU, so there's no need to scan TUs here. */
f4dc4d17 3380
b76e467d 3381 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3382 {
3d7bb9d9 3383 /* We only need to look at symtabs not already expanded. */
43f3e411 3384 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3385 continue;
3386
b76e467d 3387 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3388 if (file_data == NULL)
9291a0cd
TT
3389 continue;
3390
b76e467d 3391 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3392 {
7b9f3c50 3393 const char *this_name = file_data->file_names[j];
da235a7c 3394 const char *this_real_name;
9291a0cd 3395
af529f8f 3396 if (compare_filenames_for_search (this_name, name))
9291a0cd 3397 {
f5b95b50 3398 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3399 callback))
3400 return true;
288e77a7 3401 continue;
4aac40c8 3402 }
9291a0cd 3403
c011a4f4
DE
3404 /* Before we invoke realpath, which can get expensive when many
3405 files are involved, do a quick comparison of the basenames. */
3406 if (! basenames_may_differ
3407 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3408 continue;
3409
da235a7c
JK
3410 this_real_name = dw2_get_real_path (objfile, file_data, j);
3411 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3412 {
da235a7c 3413 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3414 callback))
3415 return true;
288e77a7 3416 continue;
da235a7c 3417 }
9291a0cd 3418
da235a7c
JK
3419 if (real_path != NULL)
3420 {
af529f8f
JK
3421 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3422 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3423 if (this_real_name != NULL
af529f8f 3424 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3425 {
f5b95b50 3426 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3427 callback))
3428 return true;
288e77a7 3429 continue;
9291a0cd
TT
3430 }
3431 }
3432 }
3433 }
3434
14bc53a8 3435 return false;
9291a0cd
TT
3436}
3437
da51c347
DE
3438/* Struct used to manage iterating over all CUs looking for a symbol. */
3439
3440struct dw2_symtab_iterator
9291a0cd 3441{
ed2dc618
SM
3442 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3443 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3444 /* If set, only look for symbols that match that block. Valid values are
3445 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3446 gdb::optional<block_enum> block_index;
da51c347
DE
3447 /* The kind of symbol we're looking for. */
3448 domain_enum domain;
3449 /* The list of CUs from the index entry of the symbol,
3450 or NULL if not found. */
3451 offset_type *vec;
3452 /* The next element in VEC to look at. */
3453 int next;
3454 /* The number of elements in VEC, or zero if there is no match. */
3455 int length;
8943b874
DE
3456 /* Have we seen a global version of the symbol?
3457 If so we can ignore all further global instances.
3458 This is to work around gold/15646, inefficient gold-generated
3459 indices. */
3460 int global_seen;
da51c347 3461};
9291a0cd 3462
2b79f376 3463/* Initialize the index symtab iterator ITER. */
2fdf6df6 3464
9291a0cd 3465static void
da51c347 3466dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3467 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3468 gdb::optional<block_enum> block_index,
da51c347
DE
3469 domain_enum domain,
3470 const char *name)
3471{
ed2dc618 3472 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3473 iter->block_index = block_index;
3474 iter->domain = domain;
3475 iter->next = 0;
8943b874 3476 iter->global_seen = 0;
da51c347 3477
3063847f 3478 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3479
3480 /* index is NULL if OBJF_READNOW. */
3481 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3482 iter->length = MAYBE_SWAP (*iter->vec);
3483 else
3484 {
3485 iter->vec = NULL;
3486 iter->length = 0;
3487 }
3488}
3489
3490/* Return the next matching CU or NULL if there are no more. */
3491
3492static struct dwarf2_per_cu_data *
3493dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3494{
ed2dc618
SM
3495 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3496
da51c347
DE
3497 for ( ; iter->next < iter->length; ++iter->next)
3498 {
3499 offset_type cu_index_and_attrs =
3500 MAYBE_SWAP (iter->vec[iter->next + 1]);
3501 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3502 gdb_index_symbol_kind symbol_kind =
3503 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3504 /* Only check the symbol attributes if they're present.
3505 Indices prior to version 7 don't record them,
3506 and indices >= 7 may elide them for certain symbols
3507 (gold does this). */
3508 int attrs_valid =
ed2dc618 3509 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3510 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3511
3190f0c6 3512 /* Don't crash on bad data. */
b76e467d 3513 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3514 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3515 {
b98664d3 3516 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3517 " [in module %s]"),
3518 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3519 continue;
3520 }
3521
ff4c9fec 3522 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3523
da51c347 3524 /* Skip if already read in. */
43f3e411 3525 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3526 continue;
3527
8943b874
DE
3528 /* Check static vs global. */
3529 if (attrs_valid)
3530 {
2b79f376
SM
3531 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3532
3533 if (iter->block_index.has_value ())
3534 {
3535 bool want_static = *iter->block_index == STATIC_BLOCK;
3536
3537 if (is_static != want_static)
3538 continue;
3539 }
3540
8943b874
DE
3541 /* Work around gold/15646. */
3542 if (!is_static && iter->global_seen)
3543 continue;
3544 if (!is_static)
3545 iter->global_seen = 1;
3546 }
da51c347
DE
3547
3548 /* Only check the symbol's kind if it has one. */
3549 if (attrs_valid)
3550 {
3551 switch (iter->domain)
3552 {
3553 case VAR_DOMAIN:
3554 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3555 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3556 /* Some types are also in VAR_DOMAIN. */
3557 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3558 continue;
3559 break;
3560 case STRUCT_DOMAIN:
3561 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3562 continue;
3563 break;
3564 case LABEL_DOMAIN:
3565 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3566 continue;
3567 break;
59c35742
AB
3568 case MODULE_DOMAIN:
3569 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3570 continue;
3571 break;
da51c347
DE
3572 default:
3573 break;
3574 }
3575 }
3576
3577 ++iter->next;
3578 return per_cu;
3579 }
3580
3581 return NULL;
3582}
3583
43f3e411 3584static struct compunit_symtab *
c7f839cb 3585dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3586 const char *name, domain_enum domain)
9291a0cd 3587{
43f3e411 3588 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3589 struct dwarf2_per_objfile *dwarf2_per_objfile
3590 = get_dwarf2_per_objfile (objfile);
9291a0cd 3591
b5ec771e
PA
3592 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3593
ed2dc618
SM
3594 struct dw2_symtab_iterator iter;
3595 struct dwarf2_per_cu_data *per_cu;
da51c347 3596
2b79f376 3597 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3598
ed2dc618
SM
3599 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3600 {
3601 struct symbol *sym, *with_opaque = NULL;
58f0c718 3602 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3603 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3604 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3605
ed2dc618
SM
3606 sym = block_find_symbol (block, name, domain,
3607 block_find_non_opaque_type_preferred,
3608 &with_opaque);
b2e2f908 3609
ed2dc618
SM
3610 /* Some caution must be observed with overloaded functions
3611 and methods, since the index will not contain any overload
3612 information (but NAME might contain it). */
da51c347 3613
ed2dc618
SM
3614 if (sym != NULL
3615 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3616 return stab;
3617 if (with_opaque != NULL
3618 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3619 stab_best = stab;
da51c347 3620
ed2dc618 3621 /* Keep looking through other CUs. */
9291a0cd 3622 }
9291a0cd 3623
da51c347 3624 return stab_best;
9291a0cd
TT
3625}
3626
3627static void
3628dw2_print_stats (struct objfile *objfile)
3629{
ed2dc618
SM
3630 struct dwarf2_per_objfile *dwarf2_per_objfile
3631 = get_dwarf2_per_objfile (objfile);
b76e467d 3632 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3633 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3634 int count = 0;
9291a0cd 3635
ed2dc618 3636 for (int i = 0; i < total; ++i)
9291a0cd 3637 {
ff4c9fec 3638 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3639
43f3e411 3640 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3641 ++count;
3642 }
e4a48d9d 3643 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3644 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3645}
3646
779bd270
DE
3647/* This dumps minimal information about the index.
3648 It is called via "mt print objfiles".
3649 One use is to verify .gdb_index has been loaded by the
3650 gdb.dwarf2/gdb-index.exp testcase. */
3651
9291a0cd
TT
3652static void
3653dw2_dump (struct objfile *objfile)
3654{
ed2dc618
SM
3655 struct dwarf2_per_objfile *dwarf2_per_objfile
3656 = get_dwarf2_per_objfile (objfile);
3657
779bd270
DE
3658 gdb_assert (dwarf2_per_objfile->using_index);
3659 printf_filtered (".gdb_index:");
3660 if (dwarf2_per_objfile->index_table != NULL)
3661 {
3662 printf_filtered (" version %d\n",
3663 dwarf2_per_objfile->index_table->version);
3664 }
3665 else
3666 printf_filtered (" faked for \"readnow\"\n");
3667 printf_filtered ("\n");
9291a0cd
TT
3668}
3669
9291a0cd
TT
3670static void
3671dw2_expand_symtabs_for_function (struct objfile *objfile,
3672 const char *func_name)
3673{
ed2dc618
SM
3674 struct dwarf2_per_objfile *dwarf2_per_objfile
3675 = get_dwarf2_per_objfile (objfile);
da51c347 3676
ed2dc618
SM
3677 struct dw2_symtab_iterator iter;
3678 struct dwarf2_per_cu_data *per_cu;
da51c347 3679
2b79f376 3680 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3681
ed2dc618 3682 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3683 dw2_instantiate_symtab (per_cu, false);
da51c347 3684
9291a0cd
TT
3685}
3686
3687static void
3688dw2_expand_all_symtabs (struct objfile *objfile)
3689{
ed2dc618
SM
3690 struct dwarf2_per_objfile *dwarf2_per_objfile
3691 = get_dwarf2_per_objfile (objfile);
b76e467d 3692 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3693 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3694
ed2dc618 3695 for (int i = 0; i < total_units; ++i)
9291a0cd 3696 {
ff4c9fec 3697 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3698
58f0c718
TT
3699 /* We don't want to directly expand a partial CU, because if we
3700 read it with the wrong language, then assertion failures can
3701 be triggered later on. See PR symtab/23010. So, tell
3702 dw2_instantiate_symtab to skip partial CUs -- any important
3703 partial CU will be read via DW_TAG_imported_unit anyway. */
3704 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3705 }
3706}
3707
3708static void
652a8996
JK
3709dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3710 const char *fullname)
9291a0cd 3711{
ed2dc618
SM
3712 struct dwarf2_per_objfile *dwarf2_per_objfile
3713 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3714
3715 /* We don't need to consider type units here.
3716 This is only called for examining code, e.g. expand_line_sal.
3717 There can be an order of magnitude (or more) more type units
3718 than comp units, and we avoid them if we can. */
3719
b76e467d 3720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3721 {
3d7bb9d9 3722 /* We only need to look at symtabs not already expanded. */
43f3e411 3723 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3724 continue;
3725
b76e467d 3726 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3727 if (file_data == NULL)
9291a0cd
TT
3728 continue;
3729
b76e467d 3730 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3731 {
652a8996
JK
3732 const char *this_fullname = file_data->file_names[j];
3733
3734 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3735 {
58f0c718 3736 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3737 break;
3738 }
3739 }
3740 }
3741}
3742
9291a0cd 3743static void
199b4314
TT
3744dw2_map_matching_symbols
3745 (struct objfile *objfile,
b054970d 3746 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3747 int global,
3748 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3749 symbol_compare_ftype *ordered_compare)
9291a0cd 3750{
40658b94 3751 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3752 current language is Ada for a non-Ada objfile using GNU index. As Ada
3753 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3754}
3755
e1ef7d7a
PA
3756/* Starting from a search name, return the string that finds the upper
3757 bound of all strings that start with SEARCH_NAME in a sorted name
3758 list. Returns the empty string to indicate that the upper bound is
3759 the end of the list. */
3760
3761static std::string
3762make_sort_after_prefix_name (const char *search_name)
3763{
3764 /* When looking to complete "func", we find the upper bound of all
3765 symbols that start with "func" by looking for where we'd insert
3766 the closest string that would follow "func" in lexicographical
3767 order. Usually, that's "func"-with-last-character-incremented,
3768 i.e. "fund". Mind non-ASCII characters, though. Usually those
3769 will be UTF-8 multi-byte sequences, but we can't be certain.
3770 Especially mind the 0xff character, which is a valid character in
3771 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3772 rule out compilers allowing it in identifiers. Note that
3773 conveniently, strcmp/strcasecmp are specified to compare
3774 characters interpreted as unsigned char. So what we do is treat
3775 the whole string as a base 256 number composed of a sequence of
3776 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3777 to 0, and carries 1 to the following more-significant position.
3778 If the very first character in SEARCH_NAME ends up incremented
3779 and carries/overflows, then the upper bound is the end of the
3780 list. The string after the empty string is also the empty
3781 string.
3782
3783 Some examples of this operation:
3784
3785 SEARCH_NAME => "+1" RESULT
3786
3787 "abc" => "abd"
3788 "ab\xff" => "ac"
3789 "\xff" "a" "\xff" => "\xff" "b"
3790 "\xff" => ""
3791 "\xff\xff" => ""
3792 "" => ""
3793
3794 Then, with these symbols for example:
3795
3796 func
3797 func1
3798 fund
3799
3800 completing "func" looks for symbols between "func" and
3801 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3802 which finds "func" and "func1", but not "fund".
3803
3804 And with:
3805
3806 funcÿ (Latin1 'ÿ' [0xff])
3807 funcÿ1
3808 fund
3809
3810 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3811 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3812
3813 And with:
3814
3815 ÿÿ (Latin1 'ÿ' [0xff])
3816 ÿÿ1
3817
3818 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3819 the end of the list.
3820 */
3821 std::string after = search_name;
3822 while (!after.empty () && (unsigned char) after.back () == 0xff)
3823 after.pop_back ();
3824 if (!after.empty ())
3825 after.back () = (unsigned char) after.back () + 1;
3826 return after;
3827}
3828
5c58de74 3829/* See declaration. */
61d96d7e 3830
5c58de74
PA
3831std::pair<std::vector<name_component>::const_iterator,
3832 std::vector<name_component>::const_iterator>
44ed8f3e 3833mapped_index_base::find_name_components_bounds
3b00ef10 3834 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3835{
5c58de74
PA
3836 auto *name_cmp
3837 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3838
3b00ef10
TT
3839 const char *lang_name
3840 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 3841
3f563c84
PA
3842 /* Comparison function object for lower_bound that matches against a
3843 given symbol name. */
3844 auto lookup_compare_lower = [&] (const name_component &elem,
3845 const char *name)
3846 {
5c58de74 3847 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3848 const char *elem_name = elem_qualified + elem.name_offset;
3849 return name_cmp (elem_name, name) < 0;
3850 };
3851
3852 /* Comparison function object for upper_bound that matches against a
3853 given symbol name. */
3854 auto lookup_compare_upper = [&] (const char *name,
3855 const name_component &elem)
3856 {
5c58de74 3857 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3858 const char *elem_name = elem_qualified + elem.name_offset;
3859 return name_cmp (name, elem_name) < 0;
3860 };
3861
5c58de74
PA
3862 auto begin = this->name_components.begin ();
3863 auto end = this->name_components.end ();
3f563c84
PA
3864
3865 /* Find the lower bound. */
3866 auto lower = [&] ()
3867 {
3b00ef10 3868 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3869 return begin;
3870 else
3b00ef10 3871 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3872 } ();
3873
3874 /* Find the upper bound. */
3875 auto upper = [&] ()
3876 {
5c58de74 3877 if (lookup_name_without_params.completion_mode ())
3f563c84 3878 {
e1ef7d7a
PA
3879 /* In completion mode, we want UPPER to point past all
3880 symbols names that have the same prefix. I.e., with
3881 these symbols, and completing "func":
3882
3883 function << lower bound
3884 function1
3885 other_function << upper bound
3886
3887 We find the upper bound by looking for the insertion
3888 point of "func"-with-last-character-incremented,
3889 i.e. "fund". */
3b00ef10 3890 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3891 if (after.empty ())
3f563c84 3892 return end;
e6b2f5ef
PA
3893 return std::lower_bound (lower, end, after.c_str (),
3894 lookup_compare_lower);
3f563c84
PA
3895 }
3896 else
3b00ef10 3897 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3898 } ();
3899
5c58de74
PA
3900 return {lower, upper};
3901}
3902
3903/* See declaration. */
3904
3905void
44ed8f3e 3906mapped_index_base::build_name_components ()
5c58de74
PA
3907{
3908 if (!this->name_components.empty ())
3909 return;
3910
3911 this->name_components_casing = case_sensitivity;
3912 auto *name_cmp
3913 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3914
3915 /* The code below only knows how to break apart components of C++
3916 symbol names (and other languages that use '::' as
3b00ef10 3917 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3918 auto count = this->symbol_name_count ();
3919 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3920 {
44ed8f3e 3921 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3922 continue;
3923
3924 const char *name = this->symbol_name_at (idx);
3925
3926 /* Add each name component to the name component table. */
3927 unsigned int previous_len = 0;
3b00ef10
TT
3928
3929 if (strstr (name, "::") != nullptr)
3930 {
3931 for (unsigned int current_len = cp_find_first_component (name);
3932 name[current_len] != '\0';
3933 current_len += cp_find_first_component (name + current_len))
3934 {
3935 gdb_assert (name[current_len] == ':');
3936 this->name_components.push_back ({previous_len, idx});
3937 /* Skip the '::'. */
3938 current_len += 2;
3939 previous_len = current_len;
3940 }
3941 }
3942 else
5c58de74 3943 {
3b00ef10
TT
3944 /* Handle the Ada encoded (aka mangled) form here. */
3945 for (const char *iter = strstr (name, "__");
3946 iter != nullptr;
3947 iter = strstr (iter, "__"))
3948 {
3949 this->name_components.push_back ({previous_len, idx});
3950 iter += 2;
3951 previous_len = iter - name;
3952 }
5c58de74 3953 }
3b00ef10 3954
5c58de74
PA
3955 this->name_components.push_back ({previous_len, idx});
3956 }
3957
3958 /* Sort name_components elements by name. */
3959 auto name_comp_compare = [&] (const name_component &left,
3960 const name_component &right)
3961 {
3962 const char *left_qualified = this->symbol_name_at (left.idx);
3963 const char *right_qualified = this->symbol_name_at (right.idx);
3964
3965 const char *left_name = left_qualified + left.name_offset;
3966 const char *right_name = right_qualified + right.name_offset;
3967
3968 return name_cmp (left_name, right_name) < 0;
3969 };
3970
3971 std::sort (this->name_components.begin (),
3972 this->name_components.end (),
3973 name_comp_compare);
3974}
3975
3976/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3977 mapped_index_base instead of the containing objfile. This is split
3978 to a separate function in order to be able to unit test the
3979 name_components matching using a mock mapped_index_base. For each
5c58de74 3980 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3981 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3982
3983static void
3984dw2_expand_symtabs_matching_symbol
44ed8f3e 3985 (mapped_index_base &index,
5c58de74
PA
3986 const lookup_name_info &lookup_name_in,
3987 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3988 enum search_domain kind,
3b00ef10 3989 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3990{
3991 lookup_name_info lookup_name_without_params
3992 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3993
3994 /* Build the symbol name component sorted vector, if we haven't
3995 yet. */
3996 index.build_name_components ();
3997
3f563c84
PA
3998 /* The same symbol may appear more than once in the range though.
3999 E.g., if we're looking for symbols that complete "w", and we have
4000 a symbol named "w1::w2", we'll find the two name components for
4001 that same symbol in the range. To be sure we only call the
4002 callback once per symbol, we first collect the symbol name
4003 indexes that matched in a temporary vector and ignore
4004 duplicates. */
4005 std::vector<offset_type> matches;
3f563c84 4006
3b00ef10
TT
4007 struct name_and_matcher
4008 {
4009 symbol_name_matcher_ftype *matcher;
4010 const std::string &name;
4011
4012 bool operator== (const name_and_matcher &other) const
3f563c84 4013 {
3b00ef10
TT
4014 return matcher == other.matcher && name == other.name;
4015 }
4016 };
4017
4018 /* A vector holding all the different symbol name matchers, for all
4019 languages. */
4020 std::vector<name_and_matcher> matchers;
4021
4022 for (int i = 0; i < nr_languages; i++)
4023 {
4024 enum language lang_e = (enum language) i;
4025
4026 const language_defn *lang = language_def (lang_e);
4027 symbol_name_matcher_ftype *name_matcher
4028 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4029
3b00ef10
TT
4030 name_and_matcher key {
4031 name_matcher,
4032 lookup_name_without_params.language_lookup_name (lang_e)
4033 };
4034
4035 /* Don't insert the same comparison routine more than once.
4036 Note that we do this linear walk. This is not a problem in
4037 practice because the number of supported languages is
4038 low. */
4039 if (std::find (matchers.begin (), matchers.end (), key)
4040 != matchers.end ())
9291a0cd 4041 continue;
3b00ef10
TT
4042 matchers.push_back (std::move (key));
4043
4044 auto bounds
4045 = index.find_name_components_bounds (lookup_name_without_params,
4046 lang_e);
4047
4048 /* Now for each symbol name in range, check to see if we have a name
4049 match, and if so, call the MATCH_CALLBACK callback. */
4050
4051 for (; bounds.first != bounds.second; ++bounds.first)
4052 {
4053 const char *qualified = index.symbol_name_at (bounds.first->idx);
4054
4055 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4056 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4057 continue;
9291a0cd 4058
3b00ef10
TT
4059 matches.push_back (bounds.first->idx);
4060 }
3f563c84
PA
4061 }
4062
4063 std::sort (matches.begin (), matches.end ());
4064
4065 /* Finally call the callback, once per match. */
4066 ULONGEST prev = -1;
4067 for (offset_type idx : matches)
4068 {
4069 if (prev != idx)
4070 {
3b00ef10
TT
4071 if (!match_callback (idx))
4072 break;
3f563c84
PA
4073 prev = idx;
4074 }
4075 }
4076
4077 /* Above we use a type wider than idx's for 'prev', since 0 and
4078 (offset_type)-1 are both possible values. */
4079 static_assert (sizeof (prev) > sizeof (offset_type), "");
4080}
4081
c62446b1
PA
4082#if GDB_SELF_TEST
4083
4084namespace selftests { namespace dw2_expand_symtabs_matching {
4085
a3c5fafd
PA
4086/* A mock .gdb_index/.debug_names-like name index table, enough to
4087 exercise dw2_expand_symtabs_matching_symbol, which works with the
4088 mapped_index_base interface. Builds an index from the symbol list
4089 passed as parameter to the constructor. */
4090class mock_mapped_index : public mapped_index_base
c62446b1
PA
4091{
4092public:
a3c5fafd
PA
4093 mock_mapped_index (gdb::array_view<const char *> symbols)
4094 : m_symbol_table (symbols)
c62446b1
PA
4095 {}
4096
a3c5fafd 4097 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4098
a3c5fafd 4099 /* Return the number of names in the symbol table. */
632e107b 4100 size_t symbol_name_count () const override
c62446b1 4101 {
a3c5fafd 4102 return m_symbol_table.size ();
c62446b1
PA
4103 }
4104
a3c5fafd 4105 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4106 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4107 {
4108 return m_symbol_table[idx];
4109 }
c62446b1 4110
a3c5fafd
PA
4111private:
4112 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4113};
4114
4115/* Convenience function that converts a NULL pointer to a "<null>"
4116 string, to pass to print routines. */
4117
4118static const char *
4119string_or_null (const char *str)
4120{
4121 return str != NULL ? str : "<null>";
4122}
4123
4124/* Check if a lookup_name_info built from
4125 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4126 index. EXPECTED_LIST is the list of expected matches, in expected
4127 matching order. If no match expected, then an empty list is
4128 specified. Returns true on success. On failure prints a warning
4129 indicating the file:line that failed, and returns false. */
4130
4131static bool
4132check_match (const char *file, int line,
4133 mock_mapped_index &mock_index,
4134 const char *name, symbol_name_match_type match_type,
4135 bool completion_mode,
4136 std::initializer_list<const char *> expected_list)
4137{
4138 lookup_name_info lookup_name (name, match_type, completion_mode);
4139
4140 bool matched = true;
4141
4142 auto mismatch = [&] (const char *expected_str,
4143 const char *got)
4144 {
4145 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4146 "expected=\"%s\", got=\"%s\"\n"),
4147 file, line,
4148 (match_type == symbol_name_match_type::FULL
4149 ? "FULL" : "WILD"),
4150 name, string_or_null (expected_str), string_or_null (got));
4151 matched = false;
4152 };
4153
4154 auto expected_it = expected_list.begin ();
4155 auto expected_end = expected_list.end ();
4156
a3c5fafd 4157 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4158 NULL, ALL_DOMAIN,
4159 [&] (offset_type idx)
4160 {
a3c5fafd 4161 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4162 const char *expected_str
4163 = expected_it == expected_end ? NULL : *expected_it++;
4164
4165 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4166 mismatch (expected_str, matched_name);
3b00ef10 4167 return true;
c62446b1
PA
4168 });
4169
4170 const char *expected_str
4171 = expected_it == expected_end ? NULL : *expected_it++;
4172 if (expected_str != NULL)
4173 mismatch (expected_str, NULL);
4174
4175 return matched;
4176}
4177
4178/* The symbols added to the mock mapped_index for testing (in
4179 canonical form). */
4180static const char *test_symbols[] = {
4181 "function",
4182 "std::bar",
4183 "std::zfunction",
4184 "std::zfunction2",
4185 "w1::w2",
4186 "ns::foo<char*>",
4187 "ns::foo<int>",
4188 "ns::foo<long>",
a20714ff
PA
4189 "ns2::tmpl<int>::foo2",
4190 "(anonymous namespace)::A::B::C",
c62446b1 4191
e1ef7d7a
PA
4192 /* These are used to check that the increment-last-char in the
4193 matching algorithm for completion doesn't match "t1_fund" when
4194 completing "t1_func". */
4195 "t1_func",
4196 "t1_func1",
4197 "t1_fund",
4198 "t1_fund1",
4199
4200 /* A UTF-8 name with multi-byte sequences to make sure that
4201 cp-name-parser understands this as a single identifier ("função"
4202 is "function" in PT). */
4203 u8"u8função",
4204
4205 /* \377 (0xff) is Latin1 'ÿ'. */
4206 "yfunc\377",
4207
4208 /* \377 (0xff) is Latin1 'ÿ'. */
4209 "\377",
4210 "\377\377123",
4211
c62446b1
PA
4212 /* A name with all sorts of complications. Starts with "z" to make
4213 it easier for the completion tests below. */
4214#define Z_SYM_NAME \
4215 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4216 "::tuple<(anonymous namespace)::ui*, " \
4217 "std::default_delete<(anonymous namespace)::ui>, void>"
4218
4219 Z_SYM_NAME
4220};
4221
a3c5fafd
PA
4222/* Returns true if the mapped_index_base::find_name_component_bounds
4223 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4224 in completion mode. */
5c58de74
PA
4225
4226static bool
a3c5fafd 4227check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4228 const char *search_name,
4229 gdb::array_view<const char *> expected_syms)
4230{
4231 lookup_name_info lookup_name (search_name,
4232 symbol_name_match_type::FULL, true);
4233
3b00ef10
TT
4234 auto bounds = index.find_name_components_bounds (lookup_name,
4235 language_cplus);
5c58de74
PA
4236
4237 size_t distance = std::distance (bounds.first, bounds.second);
4238 if (distance != expected_syms.size ())
4239 return false;
4240
4241 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4242 {
4243 auto nc_elem = bounds.first + exp_elem;
4244 const char *qualified = index.symbol_name_at (nc_elem->idx);
4245 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4246 return false;
4247 }
4248
4249 return true;
4250}
4251
4252/* Test the lower-level mapped_index::find_name_component_bounds
4253 method. */
4254
c62446b1 4255static void
5c58de74
PA
4256test_mapped_index_find_name_component_bounds ()
4257{
4258 mock_mapped_index mock_index (test_symbols);
4259
a3c5fafd 4260 mock_index.build_name_components ();
5c58de74
PA
4261
4262 /* Test the lower-level mapped_index::find_name_component_bounds
4263 method in completion mode. */
4264 {
4265 static const char *expected_syms[] = {
4266 "t1_func",
4267 "t1_func1",
5c58de74
PA
4268 };
4269
a3c5fafd 4270 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4271 "t1_func", expected_syms));
4272 }
4273
4274 /* Check that the increment-last-char in the name matching algorithm
4275 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4276 {
4277 static const char *expected_syms1[] = {
4278 "\377",
4279 "\377\377123",
4280 };
a3c5fafd 4281 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4282 "\377", expected_syms1));
4283
4284 static const char *expected_syms2[] = {
4285 "\377\377123",
4286 };
a3c5fafd 4287 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4288 "\377\377", expected_syms2));
4289 }
4290}
4291
4292/* Test dw2_expand_symtabs_matching_symbol. */
4293
4294static void
4295test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4296{
4297 mock_mapped_index mock_index (test_symbols);
4298
4299 /* We let all tests run until the end even if some fails, for debug
4300 convenience. */
4301 bool any_mismatch = false;
4302
4303 /* Create the expected symbols list (an initializer_list). Needed
4304 because lists have commas, and we need to pass them to CHECK,
4305 which is a macro. */
4306#define EXPECT(...) { __VA_ARGS__ }
4307
4308 /* Wrapper for check_match that passes down the current
4309 __FILE__/__LINE__. */
4310#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4311 any_mismatch |= !check_match (__FILE__, __LINE__, \
4312 mock_index, \
4313 NAME, MATCH_TYPE, COMPLETION_MODE, \
4314 EXPECTED_LIST)
4315
4316 /* Identity checks. */
4317 for (const char *sym : test_symbols)
4318 {
4319 /* Should be able to match all existing symbols. */
4320 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4321 EXPECT (sym));
4322
4323 /* Should be able to match all existing symbols with
4324 parameters. */
4325 std::string with_params = std::string (sym) + "(int)";
4326 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4327 EXPECT (sym));
4328
4329 /* Should be able to match all existing symbols with
4330 parameters and qualifiers. */
4331 with_params = std::string (sym) + " ( int ) const";
4332 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4333 EXPECT (sym));
4334
4335 /* This should really find sym, but cp-name-parser.y doesn't
4336 know about lvalue/rvalue qualifiers yet. */
4337 with_params = std::string (sym) + " ( int ) &&";
4338 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4339 {});
4340 }
4341
e1ef7d7a
PA
4342 /* Check that the name matching algorithm for completion doesn't get
4343 confused with Latin1 'ÿ' / 0xff. */
4344 {
4345 static const char str[] = "\377";
4346 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4347 EXPECT ("\377", "\377\377123"));
4348 }
4349
4350 /* Check that the increment-last-char in the matching algorithm for
4351 completion doesn't match "t1_fund" when completing "t1_func". */
4352 {
4353 static const char str[] = "t1_func";
4354 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4355 EXPECT ("t1_func", "t1_func1"));
4356 }
4357
c62446b1
PA
4358 /* Check that completion mode works at each prefix of the expected
4359 symbol name. */
4360 {
4361 static const char str[] = "function(int)";
4362 size_t len = strlen (str);
4363 std::string lookup;
4364
4365 for (size_t i = 1; i < len; i++)
4366 {
4367 lookup.assign (str, i);
4368 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4369 EXPECT ("function"));
4370 }
4371 }
4372
4373 /* While "w" is a prefix of both components, the match function
4374 should still only be called once. */
4375 {
4376 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4377 EXPECT ("w1::w2"));
a20714ff
PA
4378 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4379 EXPECT ("w1::w2"));
c62446b1
PA
4380 }
4381
4382 /* Same, with a "complicated" symbol. */
4383 {
4384 static const char str[] = Z_SYM_NAME;
4385 size_t len = strlen (str);
4386 std::string lookup;
4387
4388 for (size_t i = 1; i < len; i++)
4389 {
4390 lookup.assign (str, i);
4391 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4392 EXPECT (Z_SYM_NAME));
4393 }
4394 }
4395
4396 /* In FULL mode, an incomplete symbol doesn't match. */
4397 {
4398 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4399 {});
4400 }
4401
4402 /* A complete symbol with parameters matches any overload, since the
4403 index has no overload info. */
4404 {
4405 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4406 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4407 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4408 EXPECT ("std::zfunction", "std::zfunction2"));
4409 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4410 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4411 }
4412
4413 /* Check that whitespace is ignored appropriately. A symbol with a
4414 template argument list. */
4415 {
4416 static const char expected[] = "ns::foo<int>";
4417 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4418 EXPECT (expected));
a20714ff
PA
4419 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4420 EXPECT (expected));
c62446b1
PA
4421 }
4422
4423 /* Check that whitespace is ignored appropriately. A symbol with a
4424 template argument list that includes a pointer. */
4425 {
4426 static const char expected[] = "ns::foo<char*>";
4427 /* Try both completion and non-completion modes. */
4428 static const bool completion_mode[2] = {false, true};
4429 for (size_t i = 0; i < 2; i++)
4430 {
4431 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4432 completion_mode[i], EXPECT (expected));
a20714ff
PA
4433 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4434 completion_mode[i], EXPECT (expected));
c62446b1
PA
4435
4436 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4437 completion_mode[i], EXPECT (expected));
a20714ff
PA
4438 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4439 completion_mode[i], EXPECT (expected));
c62446b1
PA
4440 }
4441 }
4442
4443 {
4444 /* Check method qualifiers are ignored. */
4445 static const char expected[] = "ns::foo<char*>";
4446 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4447 symbol_name_match_type::FULL, true, EXPECT (expected));
4448 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4449 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4450 CHECK_MATCH ("foo < char * > ( int ) const",
4451 symbol_name_match_type::WILD, true, EXPECT (expected));
4452 CHECK_MATCH ("foo < char * > ( int ) &&",
4453 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4454 }
4455
4456 /* Test lookup names that don't match anything. */
4457 {
a20714ff
PA
4458 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4459 {});
4460
c62446b1
PA
4461 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4462 {});
4463 }
4464
a20714ff
PA
4465 /* Some wild matching tests, exercising "(anonymous namespace)",
4466 which should not be confused with a parameter list. */
4467 {
4468 static const char *syms[] = {
4469 "A::B::C",
4470 "B::C",
4471 "C",
4472 "A :: B :: C ( int )",
4473 "B :: C ( int )",
4474 "C ( int )",
4475 };
4476
4477 for (const char *s : syms)
4478 {
4479 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4480 EXPECT ("(anonymous namespace)::A::B::C"));
4481 }
4482 }
4483
4484 {
4485 static const char expected[] = "ns2::tmpl<int>::foo2";
4486 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4487 EXPECT (expected));
4488 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4489 EXPECT (expected));
4490 }
4491
c62446b1
PA
4492 SELF_CHECK (!any_mismatch);
4493
4494#undef EXPECT
4495#undef CHECK_MATCH
4496}
4497
5c58de74
PA
4498static void
4499run_test ()
4500{
4501 test_mapped_index_find_name_component_bounds ();
4502 test_dw2_expand_symtabs_matching_symbol ();
4503}
4504
c62446b1
PA
4505}} // namespace selftests::dw2_expand_symtabs_matching
4506
4507#endif /* GDB_SELF_TEST */
4508
4b514bc8
JK
4509/* If FILE_MATCHER is NULL or if PER_CU has
4510 dwarf2_per_cu_quick_data::MARK set (see
4511 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4512 EXPANSION_NOTIFY on it. */
4513
4514static void
4515dw2_expand_symtabs_matching_one
4516 (struct dwarf2_per_cu_data *per_cu,
4517 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4518 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4519{
4520 if (file_matcher == NULL || per_cu->v.quick->mark)
4521 {
4522 bool symtab_was_null
4523 = (per_cu->v.quick->compunit_symtab == NULL);
4524
58f0c718 4525 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4526
4527 if (expansion_notify != NULL
4528 && symtab_was_null
4529 && per_cu->v.quick->compunit_symtab != NULL)
4530 expansion_notify (per_cu->v.quick->compunit_symtab);
4531 }
4532}
4533
3f563c84
PA
4534/* Helper for dw2_expand_matching symtabs. Called on each symbol
4535 matched, to expand corresponding CUs that were marked. IDX is the
4536 index of the symbol name that matched. */
4537
4538static void
4539dw2_expand_marked_cus
ed2dc618 4540 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4541 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4542 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4543 search_domain kind)
4544{
3f563c84
PA
4545 offset_type *vec, vec_len, vec_idx;
4546 bool global_seen = false;
ed2dc618 4547 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4548
61920122 4549 vec = (offset_type *) (index.constant_pool
f00a2de2 4550 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4551 vec_len = MAYBE_SWAP (vec[0]);
4552 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4553 {
61920122
PA
4554 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4555 /* This value is only valid for index versions >= 7. */
4556 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4557 gdb_index_symbol_kind symbol_kind =
4558 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4559 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4560 /* Only check the symbol attributes if they're present.
4561 Indices prior to version 7 don't record them,
4562 and indices >= 7 may elide them for certain symbols
4563 (gold does this). */
4564 int attrs_valid =
4565 (index.version >= 7
4566 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4567
4568 /* Work around gold/15646. */
4569 if (attrs_valid)
9291a0cd 4570 {
61920122
PA
4571 if (!is_static && global_seen)
4572 continue;
4573 if (!is_static)
4574 global_seen = true;
4575 }
3190f0c6 4576
61920122
PA
4577 /* Only check the symbol's kind if it has one. */
4578 if (attrs_valid)
4579 {
4580 switch (kind)
8943b874 4581 {
61920122
PA
4582 case VARIABLES_DOMAIN:
4583 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4584 continue;
4585 break;
4586 case FUNCTIONS_DOMAIN:
4587 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4588 continue;
61920122
PA
4589 break;
4590 case TYPES_DOMAIN:
4591 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4592 continue;
4593 break;
59c35742
AB
4594 case MODULES_DOMAIN:
4595 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4596 continue;
4597 break;
61920122
PA
4598 default:
4599 break;
8943b874 4600 }
61920122 4601 }
8943b874 4602
61920122 4603 /* Don't crash on bad data. */
b76e467d 4604 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4605 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4606 {
b98664d3 4607 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4608 " [in module %s]"),
4609 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4610 continue;
4611 }
4612
ff4c9fec 4613 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4614 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4615 expansion_notify);
61920122
PA
4616 }
4617}
4618
4b514bc8
JK
4619/* If FILE_MATCHER is non-NULL, set all the
4620 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4621 that match FILE_MATCHER. */
4622
61920122 4623static void
4b514bc8 4624dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4625 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4626 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4627{
4b514bc8 4628 if (file_matcher == NULL)
61920122
PA
4629 return;
4630
4b514bc8
JK
4631 objfile *const objfile = dwarf2_per_objfile->objfile;
4632
4633 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4634 htab_eq_pointer,
4635 NULL, xcalloc, xfree));
4636 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4637 htab_eq_pointer,
4638 NULL, xcalloc, xfree));
61920122 4639
4b514bc8
JK
4640 /* The rule is CUs specify all the files, including those used by
4641 any TU, so there's no need to scan TUs here. */
61920122 4642
b76e467d 4643 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4644 {
927aa2e7
JK
4645 QUIT;
4646
4647 per_cu->v.quick->mark = 0;
4648
4649 /* We only need to look at symtabs not already expanded. */
4650 if (per_cu->v.quick->compunit_symtab)
4651 continue;
4652
b76e467d 4653 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4654 if (file_data == NULL)
4655 continue;
4656
4657 if (htab_find (visited_not_found.get (), file_data) != NULL)
4658 continue;
4659 else if (htab_find (visited_found.get (), file_data) != NULL)
4660 {
4661 per_cu->v.quick->mark = 1;
4662 continue;
4663 }
4664
b76e467d 4665 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4666 {
4667 const char *this_real_name;
4668
4669 if (file_matcher (file_data->file_names[j], false))
4670 {
4671 per_cu->v.quick->mark = 1;
4672 break;
4673 }
4674
4675 /* Before we invoke realpath, which can get expensive when many
4676 files are involved, do a quick comparison of the basenames. */
4677 if (!basenames_may_differ
4678 && !file_matcher (lbasename (file_data->file_names[j]),
4679 true))
4680 continue;
4681
4682 this_real_name = dw2_get_real_path (objfile, file_data, j);
4683 if (file_matcher (this_real_name, false))
4684 {
4685 per_cu->v.quick->mark = 1;
4686 break;
4687 }
4688 }
4689
b76e467d
SM
4690 void **slot = htab_find_slot (per_cu->v.quick->mark
4691 ? visited_found.get ()
4692 : visited_not_found.get (),
4693 file_data, INSERT);
927aa2e7
JK
4694 *slot = file_data;
4695 }
4696}
4697
4698static void
4699dw2_expand_symtabs_matching
4700 (struct objfile *objfile,
4701 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4702 const lookup_name_info &lookup_name,
4703 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4704 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4705 enum search_domain kind)
4706{
ed2dc618
SM
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4709
4710 /* index_table is NULL if OBJF_READNOW. */
4711 if (!dwarf2_per_objfile->index_table)
4712 return;
4713
ed2dc618 4714 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4715
4716 mapped_index &index = *dwarf2_per_objfile->index_table;
4717
4718 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4719 symbol_matcher,
4720 kind, [&] (offset_type idx)
4721 {
ed2dc618 4722 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4723 expansion_notify, kind);
3b00ef10 4724 return true;
927aa2e7
JK
4725 });
4726}
4727
4728/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4729 symtab. */
4730
4731static struct compunit_symtab *
4732recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4733 CORE_ADDR pc)
4734{
4735 int i;
4736
4737 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4738 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4739 return cust;
4740
4741 if (cust->includes == NULL)
4742 return NULL;
4743
4744 for (i = 0; cust->includes[i]; ++i)
4745 {
4746 struct compunit_symtab *s = cust->includes[i];
4747
4748 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4749 if (s != NULL)
4750 return s;
4751 }
4752
4753 return NULL;
4754}
4755
4756static struct compunit_symtab *
4757dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4758 struct bound_minimal_symbol msymbol,
4759 CORE_ADDR pc,
4760 struct obj_section *section,
4761 int warn_if_readin)
4762{
4763 struct dwarf2_per_cu_data *data;
4764 struct compunit_symtab *result;
4765
d320c2b5 4766 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4767 return NULL;
4768
b3b3bada 4769 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4770 data = (struct dwarf2_per_cu_data *) addrmap_find
4771 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4772 if (!data)
4773 return NULL;
4774
4775 if (warn_if_readin && data->v.quick->compunit_symtab)
4776 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4777 paddress (get_objfile_arch (objfile), pc));
4778
4779 result
58f0c718
TT
4780 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4781 false),
927aa2e7
JK
4782 pc);
4783 gdb_assert (result != NULL);
4784 return result;
4785}
4786
4787static void
4788dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4789 void *data, int need_fullname)
4790{
ed2dc618
SM
4791 struct dwarf2_per_objfile *dwarf2_per_objfile
4792 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4793
4794 if (!dwarf2_per_objfile->filenames_cache)
4795 {
4796 dwarf2_per_objfile->filenames_cache.emplace ();
4797
4798 htab_up visited (htab_create_alloc (10,
4799 htab_hash_pointer, htab_eq_pointer,
4800 NULL, xcalloc, xfree));
4801
4802 /* The rule is CUs specify all the files, including those used
4803 by any TU, so there's no need to scan TUs here. We can
4804 ignore file names coming from already-expanded CUs. */
4805
b76e467d 4806 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4807 {
927aa2e7
JK
4808 if (per_cu->v.quick->compunit_symtab)
4809 {
4810 void **slot = htab_find_slot (visited.get (),
4811 per_cu->v.quick->file_names,
4812 INSERT);
4813
4814 *slot = per_cu->v.quick->file_names;
4815 }
4816 }
4817
b76e467d 4818 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4819 {
927aa2e7
JK
4820 /* We only need to look at symtabs not already expanded. */
4821 if (per_cu->v.quick->compunit_symtab)
4822 continue;
4823
b76e467d 4824 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4825 if (file_data == NULL)
4826 continue;
4827
b76e467d 4828 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4829 if (*slot)
4830 {
4831 /* Already visited. */
4832 continue;
4833 }
4834 *slot = file_data;
4835
4836 for (int j = 0; j < file_data->num_file_names; ++j)
4837 {
4838 const char *filename = file_data->file_names[j];
4839 dwarf2_per_objfile->filenames_cache->seen (filename);
4840 }
4841 }
4842 }
4843
4844 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4845 {
4846 gdb::unique_xmalloc_ptr<char> this_real_name;
4847
4848 if (need_fullname)
4849 this_real_name = gdb_realpath (filename);
4850 (*fun) (filename, this_real_name.get (), data);
4851 });
4852}
4853
4854static int
4855dw2_has_symbols (struct objfile *objfile)
4856{
4857 return 1;
4858}
4859
4860const struct quick_symbol_functions dwarf2_gdb_index_functions =
4861{
4862 dw2_has_symbols,
4863 dw2_find_last_source_symtab,
4864 dw2_forget_cached_source_info,
4865 dw2_map_symtabs_matching_filename,
4866 dw2_lookup_symbol,
4867 dw2_print_stats,
4868 dw2_dump,
927aa2e7
JK
4869 dw2_expand_symtabs_for_function,
4870 dw2_expand_all_symtabs,
4871 dw2_expand_symtabs_with_fullname,
4872 dw2_map_matching_symbols,
4873 dw2_expand_symtabs_matching,
4874 dw2_find_pc_sect_compunit_symtab,
4875 NULL,
4876 dw2_map_symbol_filenames
4877};
4878
4879/* DWARF-5 debug_names reader. */
4880
4881/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4882static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4883
4884/* A helper function that reads the .debug_names section in SECTION
4885 and fills in MAP. FILENAME is the name of the file containing the
4886 section; it is used for error reporting.
4887
4888 Returns true if all went well, false otherwise. */
4889
4890static bool
4891read_debug_names_from_section (struct objfile *objfile,
4892 const char *filename,
4893 struct dwarf2_section_info *section,
4894 mapped_debug_names &map)
4895{
96b79293 4896 if (section->empty ())
927aa2e7
JK
4897 return false;
4898
4899 /* Older elfutils strip versions could keep the section in the main
4900 executable while splitting it for the separate debug info file. */
96b79293 4901 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4902 return false;
4903
96b79293 4904 section->read (objfile);
927aa2e7
JK
4905
4906 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4907
4908 const gdb_byte *addr = section->buffer;
4909
96b79293 4910 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4911
4912 unsigned int bytes_read;
4913 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4914 addr += bytes_read;
4915
4916 map.dwarf5_is_dwarf64 = bytes_read != 4;
4917 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4918 if (bytes_read + length != section->size)
4919 {
4920 /* There may be multiple per-CU indices. */
4921 warning (_("Section .debug_names in %s length %s does not match "
4922 "section length %s, ignoring .debug_names."),
4923 filename, plongest (bytes_read + length),
4924 pulongest (section->size));
4925 return false;
4926 }
4927
4928 /* The version number. */
4929 uint16_t version = read_2_bytes (abfd, addr);
4930 addr += 2;
4931 if (version != 5)
4932 {
4933 warning (_("Section .debug_names in %s has unsupported version %d, "
4934 "ignoring .debug_names."),
4935 filename, version);
4936 return false;
4937 }
4938
4939 /* Padding. */
4940 uint16_t padding = read_2_bytes (abfd, addr);
4941 addr += 2;
4942 if (padding != 0)
4943 {
4944 warning (_("Section .debug_names in %s has unsupported padding %d, "
4945 "ignoring .debug_names."),
4946 filename, padding);
4947 return false;
4948 }
4949
4950 /* comp_unit_count - The number of CUs in the CU list. */
4951 map.cu_count = read_4_bytes (abfd, addr);
4952 addr += 4;
4953
4954 /* local_type_unit_count - The number of TUs in the local TU
4955 list. */
4956 map.tu_count = read_4_bytes (abfd, addr);
4957 addr += 4;
4958
4959 /* foreign_type_unit_count - The number of TUs in the foreign TU
4960 list. */
4961 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4962 addr += 4;
4963 if (foreign_tu_count != 0)
4964 {
4965 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4966 "ignoring .debug_names."),
4967 filename, static_cast<unsigned long> (foreign_tu_count));
4968 return false;
4969 }
4970
4971 /* bucket_count - The number of hash buckets in the hash lookup
4972 table. */
4973 map.bucket_count = read_4_bytes (abfd, addr);
4974 addr += 4;
4975
4976 /* name_count - The number of unique names in the index. */
4977 map.name_count = read_4_bytes (abfd, addr);
4978 addr += 4;
4979
4980 /* abbrev_table_size - The size in bytes of the abbreviations
4981 table. */
4982 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4983 addr += 4;
4984
4985 /* augmentation_string_size - The size in bytes of the augmentation
4986 string. This value is rounded up to a multiple of 4. */
4987 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4988 addr += 4;
4989 map.augmentation_is_gdb = ((augmentation_string_size
4990 == sizeof (dwarf5_augmentation))
4991 && memcmp (addr, dwarf5_augmentation,
4992 sizeof (dwarf5_augmentation)) == 0);
4993 augmentation_string_size += (-augmentation_string_size) & 3;
4994 addr += augmentation_string_size;
4995
4996 /* List of CUs */
4997 map.cu_table_reordered = addr;
4998 addr += map.cu_count * map.offset_size;
4999
5000 /* List of Local TUs */
5001 map.tu_table_reordered = addr;
5002 addr += map.tu_count * map.offset_size;
5003
5004 /* Hash Lookup Table */
5005 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5006 addr += map.bucket_count * 4;
5007 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5008 addr += map.name_count * 4;
5009
5010 /* Name Table */
5011 map.name_table_string_offs_reordered = addr;
5012 addr += map.name_count * map.offset_size;
5013 map.name_table_entry_offs_reordered = addr;
5014 addr += map.name_count * map.offset_size;
5015
5016 const gdb_byte *abbrev_table_start = addr;
5017 for (;;)
5018 {
927aa2e7
JK
5019 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5020 addr += bytes_read;
5021 if (index_num == 0)
5022 break;
5023
5024 const auto insertpair
5025 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5026 if (!insertpair.second)
5027 {
5028 warning (_("Section .debug_names in %s has duplicate index %s, "
5029 "ignoring .debug_names."),
5030 filename, pulongest (index_num));
5031 return false;
5032 }
5033 mapped_debug_names::index_val &indexval = insertpair.first->second;
5034 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5035 addr += bytes_read;
5036
5037 for (;;)
5038 {
5039 mapped_debug_names::index_val::attr attr;
5040 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5041 addr += bytes_read;
5042 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5043 addr += bytes_read;
5044 if (attr.form == DW_FORM_implicit_const)
5045 {
5046 attr.implicit_const = read_signed_leb128 (abfd, addr,
5047 &bytes_read);
5048 addr += bytes_read;
5049 }
5050 if (attr.dw_idx == 0 && attr.form == 0)
5051 break;
5052 indexval.attr_vec.push_back (std::move (attr));
5053 }
5054 }
5055 if (addr != abbrev_table_start + abbrev_table_size)
5056 {
5057 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5058 "of size %s vs. written as %u, ignoring .debug_names."),
5059 filename, plongest (addr - abbrev_table_start),
5060 abbrev_table_size);
927aa2e7
JK
5061 return false;
5062 }
5063 map.entry_pool = addr;
5064
5065 return true;
5066}
5067
5068/* A helper for create_cus_from_debug_names that handles the MAP's CU
5069 list. */
5070
5071static void
ed2dc618 5072create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5073 const mapped_debug_names &map,
5074 dwarf2_section_info &section,
b76e467d 5075 bool is_dwz)
927aa2e7
JK
5076{
5077 sect_offset sect_off_prev;
5078 for (uint32_t i = 0; i <= map.cu_count; ++i)
5079 {
5080 sect_offset sect_off_next;
5081 if (i < map.cu_count)
5082 {
5083 sect_off_next
5084 = (sect_offset) (extract_unsigned_integer
5085 (map.cu_table_reordered + i * map.offset_size,
5086 map.offset_size,
5087 map.dwarf5_byte_order));
5088 }
5089 else
5090 sect_off_next = (sect_offset) section.size;
5091 if (i >= 1)
5092 {
5093 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5094 dwarf2_per_cu_data *per_cu
ed2dc618 5095 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5096 sect_off_prev, length);
b76e467d 5097 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5098 }
5099 sect_off_prev = sect_off_next;
5100 }
5101}
5102
5103/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5104 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5105
5106static void
ed2dc618 5107create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5108 const mapped_debug_names &map,
5109 const mapped_debug_names &dwz_map)
5110{
b76e467d
SM
5111 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5112 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5113
ed2dc618
SM
5114 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5115 dwarf2_per_objfile->info,
b76e467d 5116 false /* is_dwz */);
927aa2e7
JK
5117
5118 if (dwz_map.cu_count == 0)
5119 return;
5120
ed2dc618
SM
5121 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5122 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5123 true /* is_dwz */);
927aa2e7
JK
5124}
5125
5126/* Read .debug_names. If everything went ok, initialize the "quick"
5127 elements of all the CUs and return true. Otherwise, return false. */
5128
5129static bool
ed2dc618 5130dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5131{
22ca247e
TT
5132 std::unique_ptr<mapped_debug_names> map
5133 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5134 mapped_debug_names dwz_map (dwarf2_per_objfile);
5135 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5136
5137 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5138 &dwarf2_per_objfile->debug_names,
22ca247e 5139 *map))
927aa2e7
JK
5140 return false;
5141
5142 /* Don't use the index if it's empty. */
22ca247e 5143 if (map->name_count == 0)
927aa2e7
JK
5144 return false;
5145
5146 /* If there is a .dwz file, read it so we can get its CU list as
5147 well. */
ed2dc618 5148 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5149 if (dwz != NULL)
5150 {
5151 if (!read_debug_names_from_section (objfile,
00f93c44 5152 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5153 &dwz->debug_names, dwz_map))
5154 {
5155 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5156 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5157 return false;
5158 }
5159 }
5160
22ca247e 5161 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5162
22ca247e 5163 if (map->tu_count != 0)
927aa2e7
JK
5164 {
5165 /* We can only handle a single .debug_types when we have an
5166 index. */
fd5866f6 5167 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5168 return false;
5169
fd5866f6 5170 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5171
5172 create_signatured_type_table_from_debug_names
22ca247e 5173 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5174 }
5175
ed2dc618
SM
5176 create_addrmap_from_aranges (dwarf2_per_objfile,
5177 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5178
22ca247e 5179 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5180 dwarf2_per_objfile->using_index = 1;
5181 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5182 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5183
5184 return true;
5185}
5186
927aa2e7
JK
5187/* Type used to manage iterating over all CUs looking for a symbol for
5188 .debug_names. */
5189
5190class dw2_debug_names_iterator
5191{
5192public:
927aa2e7 5193 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5194 gdb::optional<block_enum> block_index,
5195 domain_enum domain,
927aa2e7 5196 const char *name)
2b79f376 5197 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5198 m_addr (find_vec_in_debug_names (map, name))
5199 {}
5200
5201 dw2_debug_names_iterator (const mapped_debug_names &map,
5202 search_domain search, uint32_t namei)
5203 : m_map (map),
5204 m_search (search),
5205 m_addr (find_vec_in_debug_names (map, namei))
5206 {}
5207
3b00ef10
TT
5208 dw2_debug_names_iterator (const mapped_debug_names &map,
5209 block_enum block_index, domain_enum domain,
5210 uint32_t namei)
5211 : m_map (map), m_block_index (block_index), m_domain (domain),
5212 m_addr (find_vec_in_debug_names (map, namei))
5213 {}
5214
927aa2e7
JK
5215 /* Return the next matching CU or NULL if there are no more. */
5216 dwarf2_per_cu_data *next ();
5217
5218private:
5219 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5220 const char *name);
5221 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5222 uint32_t namei);
5223
5224 /* The internalized form of .debug_names. */
5225 const mapped_debug_names &m_map;
5226
2b79f376
SM
5227 /* If set, only look for symbols that match that block. Valid values are
5228 GLOBAL_BLOCK and STATIC_BLOCK. */
5229 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5230
5231 /* The kind of symbol we're looking for. */
5232 const domain_enum m_domain = UNDEF_DOMAIN;
5233 const search_domain m_search = ALL_DOMAIN;
5234
5235 /* The list of CUs from the index entry of the symbol, or NULL if
5236 not found. */
5237 const gdb_byte *m_addr;
5238};
5239
5240const char *
5241mapped_debug_names::namei_to_name (uint32_t namei) const
5242{
5243 const ULONGEST namei_string_offs
5244 = extract_unsigned_integer ((name_table_string_offs_reordered
5245 + namei * offset_size),
5246 offset_size,
5247 dwarf5_byte_order);
5248 return read_indirect_string_at_offset
ed2dc618 5249 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5250}
5251
5252/* Find a slot in .debug_names for the object named NAME. If NAME is
5253 found, return pointer to its pool data. If NAME cannot be found,
5254 return NULL. */
5255
5256const gdb_byte *
5257dw2_debug_names_iterator::find_vec_in_debug_names
5258 (const mapped_debug_names &map, const char *name)
5259{
5260 int (*cmp) (const char *, const char *);
5261
54ee4252 5262 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5263 if (current_language->la_language == language_cplus
5264 || current_language->la_language == language_fortran
5265 || current_language->la_language == language_d)
5266 {
5267 /* NAME is already canonical. Drop any qualifiers as
5268 .debug_names does not contain any. */
5269
5270 if (strchr (name, '(') != NULL)
5271 {
54ee4252 5272 without_params = cp_remove_params (name);
927aa2e7 5273 if (without_params != NULL)
54ee4252 5274 name = without_params.get ();
927aa2e7
JK
5275 }
5276 }
5277
5278 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5279
5280 const uint32_t full_hash = dwarf5_djb_hash (name);
5281 uint32_t namei
5282 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5283 (map.bucket_table_reordered
5284 + (full_hash % map.bucket_count)), 4,
5285 map.dwarf5_byte_order);
5286 if (namei == 0)
5287 return NULL;
5288 --namei;
5289 if (namei >= map.name_count)
5290 {
b98664d3 5291 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5292 "[in module %s]"),
5293 namei, map.name_count,
ed2dc618 5294 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5295 return NULL;
5296 }
5297
5298 for (;;)
5299 {
5300 const uint32_t namei_full_hash
5301 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5302 (map.hash_table_reordered + namei), 4,
5303 map.dwarf5_byte_order);
5304 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5305 return NULL;
5306
5307 if (full_hash == namei_full_hash)
5308 {
5309 const char *const namei_string = map.namei_to_name (namei);
5310
5311#if 0 /* An expensive sanity check. */
5312 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5313 {
b98664d3 5314 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5315 "[in module %s]"),
5316 namei, objfile_name (dwarf2_per_objfile->objfile));
5317 return NULL;
5318 }
5319#endif
5320
5321 if (cmp (namei_string, name) == 0)
5322 {
5323 const ULONGEST namei_entry_offs
5324 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5325 + namei * map.offset_size),
5326 map.offset_size, map.dwarf5_byte_order);
5327 return map.entry_pool + namei_entry_offs;
5328 }
5329 }
5330
5331 ++namei;
5332 if (namei >= map.name_count)
5333 return NULL;
5334 }
5335}
5336
5337const gdb_byte *
5338dw2_debug_names_iterator::find_vec_in_debug_names
5339 (const mapped_debug_names &map, uint32_t namei)
5340{
5341 if (namei >= map.name_count)
5342 {
b98664d3 5343 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5344 "[in module %s]"),
5345 namei, map.name_count,
ed2dc618 5346 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5347 return NULL;
5348 }
5349
5350 const ULONGEST namei_entry_offs
5351 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5352 + namei * map.offset_size),
5353 map.offset_size, map.dwarf5_byte_order);
5354 return map.entry_pool + namei_entry_offs;
5355}
5356
5357/* See dw2_debug_names_iterator. */
5358
5359dwarf2_per_cu_data *
5360dw2_debug_names_iterator::next ()
5361{
5362 if (m_addr == NULL)
5363 return NULL;
5364
ed2dc618
SM
5365 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5366 struct objfile *objfile = dwarf2_per_objfile->objfile;
5367 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5368
5369 again:
5370
5371 unsigned int bytes_read;
5372 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5373 m_addr += bytes_read;
5374 if (abbrev == 0)
5375 return NULL;
5376
5377 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5378 if (indexval_it == m_map.abbrev_map.cend ())
5379 {
b98664d3 5380 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5381 "[in module %s]"),
ed2dc618 5382 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5383 return NULL;
5384 }
5385 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5386 enum class symbol_linkage {
5387 unknown,
5388 static_,
5389 extern_,
23c13d42 5390 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5391 dwarf2_per_cu_data *per_cu = NULL;
5392 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5393 {
5394 ULONGEST ull;
5395 switch (attr.form)
5396 {
5397 case DW_FORM_implicit_const:
5398 ull = attr.implicit_const;
5399 break;
5400 case DW_FORM_flag_present:
5401 ull = 1;
5402 break;
5403 case DW_FORM_udata:
5404 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5405 m_addr += bytes_read;
5406 break;
5407 default:
b98664d3 5408 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5409 dwarf_form_name (attr.form),
ed2dc618 5410 objfile_name (objfile));
927aa2e7
JK
5411 return NULL;
5412 }
5413 switch (attr.dw_idx)
5414 {
5415 case DW_IDX_compile_unit:
5416 /* Don't crash on bad data. */
b76e467d 5417 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5418 {
b98664d3 5419 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5420 " [in module %s]"),
5421 pulongest (ull),
5422 objfile_name (dwarf2_per_objfile->objfile));
5423 continue;
5424 }
ff4c9fec 5425 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5426 break;
8af5c486
JK
5427 case DW_IDX_type_unit:
5428 /* Don't crash on bad data. */
b2bdb8cf 5429 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5430 {
b98664d3 5431 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5432 " [in module %s]"),
5433 pulongest (ull),
5434 objfile_name (dwarf2_per_objfile->objfile));
5435 continue;
5436 }
ff4c9fec 5437 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5438 break;
927aa2e7
JK
5439 case DW_IDX_GNU_internal:
5440 if (!m_map.augmentation_is_gdb)
5441 break;
23c13d42 5442 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5443 break;
5444 case DW_IDX_GNU_external:
5445 if (!m_map.augmentation_is_gdb)
5446 break;
23c13d42 5447 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5448 break;
5449 }
5450 }
5451
5452 /* Skip if already read in. */
5453 if (per_cu->v.quick->compunit_symtab)
5454 goto again;
5455
5456 /* Check static vs global. */
23c13d42 5457 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5458 {
2b79f376 5459 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5460 const bool symbol_is_static =
5461 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5462 if (want_static != symbol_is_static)
2b79f376 5463 goto again;
927aa2e7
JK
5464 }
5465
5466 /* Match dw2_symtab_iter_next, symbol_kind
5467 and debug_names::psymbol_tag. */
5468 switch (m_domain)
5469 {
5470 case VAR_DOMAIN:
5471 switch (indexval.dwarf_tag)
5472 {
5473 case DW_TAG_variable:
5474 case DW_TAG_subprogram:
5475 /* Some types are also in VAR_DOMAIN. */
5476 case DW_TAG_typedef:
5477 case DW_TAG_structure_type:
5478 break;
5479 default:
5480 goto again;
5481 }
5482 break;
5483 case STRUCT_DOMAIN:
5484 switch (indexval.dwarf_tag)
5485 {
5486 case DW_TAG_typedef:
5487 case DW_TAG_structure_type:
5488 break;
5489 default:
5490 goto again;
5491 }
5492 break;
5493 case LABEL_DOMAIN:
5494 switch (indexval.dwarf_tag)
5495 {
5496 case 0:
5497 case DW_TAG_variable:
5498 break;
5499 default:
5500 goto again;
5501 }
5502 break;
59c35742
AB
5503 case MODULE_DOMAIN:
5504 switch (indexval.dwarf_tag)
5505 {
5506 case DW_TAG_module:
5507 break;
5508 default:
5509 goto again;
5510 }
5511 break;
927aa2e7
JK
5512 default:
5513 break;
5514 }
5515
5516 /* Match dw2_expand_symtabs_matching, symbol_kind and
5517 debug_names::psymbol_tag. */
5518 switch (m_search)
4b514bc8 5519 {
927aa2e7
JK
5520 case VARIABLES_DOMAIN:
5521 switch (indexval.dwarf_tag)
4b514bc8 5522 {
927aa2e7
JK
5523 case DW_TAG_variable:
5524 break;
5525 default:
5526 goto again;
4b514bc8 5527 }
927aa2e7
JK
5528 break;
5529 case FUNCTIONS_DOMAIN:
5530 switch (indexval.dwarf_tag)
4b514bc8 5531 {
927aa2e7
JK
5532 case DW_TAG_subprogram:
5533 break;
5534 default:
5535 goto again;
4b514bc8 5536 }
927aa2e7
JK
5537 break;
5538 case TYPES_DOMAIN:
5539 switch (indexval.dwarf_tag)
5540 {
5541 case DW_TAG_typedef:
5542 case DW_TAG_structure_type:
5543 break;
5544 default:
5545 goto again;
5546 }
5547 break;
59c35742
AB
5548 case MODULES_DOMAIN:
5549 switch (indexval.dwarf_tag)
5550 {
5551 case DW_TAG_module:
5552 break;
5553 default:
5554 goto again;
5555 }
927aa2e7
JK
5556 default:
5557 break;
4b514bc8 5558 }
927aa2e7
JK
5559
5560 return per_cu;
4b514bc8 5561}
61920122 5562
927aa2e7 5563static struct compunit_symtab *
c7f839cb 5564dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5565 const char *name, domain_enum domain)
4b514bc8 5566{
ed2dc618
SM
5567 struct dwarf2_per_objfile *dwarf2_per_objfile
5568 = get_dwarf2_per_objfile (objfile);
61920122 5569
927aa2e7
JK
5570 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5571 if (!mapp)
61920122 5572 {
927aa2e7
JK
5573 /* index is NULL if OBJF_READNOW. */
5574 return NULL;
5575 }
5576 const auto &map = *mapp;
9291a0cd 5577
2b79f376 5578 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5579
927aa2e7
JK
5580 struct compunit_symtab *stab_best = NULL;
5581 struct dwarf2_per_cu_data *per_cu;
5582 while ((per_cu = iter.next ()) != NULL)
5583 {
5584 struct symbol *sym, *with_opaque = NULL;
58f0c718 5585 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5586 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5587 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5588
927aa2e7
JK
5589 sym = block_find_symbol (block, name, domain,
5590 block_find_non_opaque_type_preferred,
5591 &with_opaque);
9703b513 5592
927aa2e7
JK
5593 /* Some caution must be observed with overloaded functions and
5594 methods, since the index will not contain any overload
5595 information (but NAME might contain it). */
a3ec0bb1 5596
927aa2e7 5597 if (sym != NULL
987012b8 5598 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5599 return stab;
5600 if (with_opaque != NULL
987012b8 5601 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5602 stab_best = stab;
9703b513 5603
927aa2e7 5604 /* Keep looking through other CUs. */
9703b513
TT
5605 }
5606
927aa2e7 5607 return stab_best;
9703b513
TT
5608}
5609
927aa2e7
JK
5610/* This dumps minimal information about .debug_names. It is called
5611 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5612 uses this to verify that .debug_names has been loaded. */
9291a0cd 5613
927aa2e7
JK
5614static void
5615dw2_debug_names_dump (struct objfile *objfile)
5616{
ed2dc618
SM
5617 struct dwarf2_per_objfile *dwarf2_per_objfile
5618 = get_dwarf2_per_objfile (objfile);
5619
927aa2e7
JK
5620 gdb_assert (dwarf2_per_objfile->using_index);
5621 printf_filtered (".debug_names:");
5622 if (dwarf2_per_objfile->debug_names_table)
5623 printf_filtered (" exists\n");
5624 else
5625 printf_filtered (" faked for \"readnow\"\n");
5626 printf_filtered ("\n");
9291a0cd
TT
5627}
5628
9291a0cd 5629static void
927aa2e7
JK
5630dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5631 const char *func_name)
9291a0cd 5632{
ed2dc618
SM
5633 struct dwarf2_per_objfile *dwarf2_per_objfile
5634 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5635
927aa2e7
JK
5636 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5637 if (dwarf2_per_objfile->debug_names_table)
24c79950 5638 {
927aa2e7 5639 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5640
2b79f376 5641 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5642
927aa2e7
JK
5643 struct dwarf2_per_cu_data *per_cu;
5644 while ((per_cu = iter.next ()) != NULL)
58f0c718 5645 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5646 }
5647}
24c79950 5648
3b00ef10
TT
5649static void
5650dw2_debug_names_map_matching_symbols
5651 (struct objfile *objfile,
5652 const lookup_name_info &name, domain_enum domain,
5653 int global,
5654 gdb::function_view<symbol_found_callback_ftype> callback,
5655 symbol_compare_ftype *ordered_compare)
5656{
5657 struct dwarf2_per_objfile *dwarf2_per_objfile
5658 = get_dwarf2_per_objfile (objfile);
5659
5660 /* debug_names_table is NULL if OBJF_READNOW. */
5661 if (!dwarf2_per_objfile->debug_names_table)
5662 return;
5663
5664 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5665 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5666
5667 const char *match_name = name.ada ().lookup_name ().c_str ();
5668 auto matcher = [&] (const char *symname)
5669 {
5670 if (ordered_compare == nullptr)
5671 return true;
5672 return ordered_compare (symname, match_name) == 0;
5673 };
5674
5675 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5676 [&] (offset_type namei)
5677 {
5678 /* The name was matched, now expand corresponding CUs that were
5679 marked. */
5680 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5681
5682 struct dwarf2_per_cu_data *per_cu;
5683 while ((per_cu = iter.next ()) != NULL)
5684 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5685 return true;
5686 });
5687
5688 /* It's a shame we couldn't do this inside the
5689 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5690 that have already been expanded. Instead, this loop matches what
5691 the psymtab code does. */
5692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5693 {
5694 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5695 if (cust != nullptr)
5696 {
5697 const struct block *block
5698 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5699 if (!iterate_over_symbols_terminated (block, name,
5700 domain, callback))
5701 break;
5702 }
5703 }
5704}
5705
927aa2e7
JK
5706static void
5707dw2_debug_names_expand_symtabs_matching
5708 (struct objfile *objfile,
5709 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5710 const lookup_name_info &lookup_name,
5711 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5712 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5713 enum search_domain kind)
5714{
ed2dc618
SM
5715 struct dwarf2_per_objfile *dwarf2_per_objfile
5716 = get_dwarf2_per_objfile (objfile);
9291a0cd 5717
927aa2e7
JK
5718 /* debug_names_table is NULL if OBJF_READNOW. */
5719 if (!dwarf2_per_objfile->debug_names_table)
5720 return;
9291a0cd 5721
ed2dc618 5722 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5723
44ed8f3e 5724 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5725
44ed8f3e
PA
5726 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5727 symbol_matcher,
5728 kind, [&] (offset_type namei)
927aa2e7 5729 {
927aa2e7
JK
5730 /* The name was matched, now expand corresponding CUs that were
5731 marked. */
5732 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5733
927aa2e7
JK
5734 struct dwarf2_per_cu_data *per_cu;
5735 while ((per_cu = iter.next ()) != NULL)
5736 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5737 expansion_notify);
3b00ef10 5738 return true;
44ed8f3e 5739 });
9291a0cd
TT
5740}
5741
927aa2e7 5742const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5743{
5744 dw2_has_symbols,
5745 dw2_find_last_source_symtab,
5746 dw2_forget_cached_source_info,
f8eba3c6 5747 dw2_map_symtabs_matching_filename,
927aa2e7 5748 dw2_debug_names_lookup_symbol,
9291a0cd 5749 dw2_print_stats,
927aa2e7 5750 dw2_debug_names_dump,
927aa2e7 5751 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5752 dw2_expand_all_symtabs,
652a8996 5753 dw2_expand_symtabs_with_fullname,
3b00ef10 5754 dw2_debug_names_map_matching_symbols,
927aa2e7 5755 dw2_debug_names_expand_symtabs_matching,
43f3e411 5756 dw2_find_pc_sect_compunit_symtab,
71a3c369 5757 NULL,
9291a0cd
TT
5758 dw2_map_symbol_filenames
5759};
5760
4485a1c1
SM
5761/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5762 to either a dwarf2_per_objfile or dwz_file object. */
5763
5764template <typename T>
5765static gdb::array_view<const gdb_byte>
5766get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5767{
5768 dwarf2_section_info *section = &section_owner->gdb_index;
5769
96b79293 5770 if (section->empty ())
4485a1c1
SM
5771 return {};
5772
5773 /* Older elfutils strip versions could keep the section in the main
5774 executable while splitting it for the separate debug info file. */
96b79293 5775 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5776 return {};
5777
96b79293 5778 section->read (obj);
4485a1c1 5779
8bebfcda
PA
5780 /* dwarf2_section_info::size is a bfd_size_type, while
5781 gdb::array_view works with size_t. On 32-bit hosts, with
5782 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5783 is 32-bit. So we need an explicit narrowing conversion here.
5784 This is fine, because it's impossible to allocate or mmap an
5785 array/buffer larger than what size_t can represent. */
5786 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5787}
5788
87d6a7aa
SM
5789/* Lookup the index cache for the contents of the index associated to
5790 DWARF2_OBJ. */
5791
5792static gdb::array_view<const gdb_byte>
5793get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5794{
5795 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5796 if (build_id == nullptr)
5797 return {};
5798
5799 return global_index_cache.lookup_gdb_index (build_id,
5800 &dwarf2_obj->index_cache_res);
5801}
5802
5803/* Same as the above, but for DWZ. */
5804
5805static gdb::array_view<const gdb_byte>
5806get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5807{
5808 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5809 if (build_id == nullptr)
5810 return {};
5811
5812 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5813}
5814
3c0aa29a 5815/* See symfile.h. */
9291a0cd 5816
3c0aa29a
PA
5817bool
5818dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5819{
ed2dc618
SM
5820 struct dwarf2_per_objfile *dwarf2_per_objfile
5821 = get_dwarf2_per_objfile (objfile);
5822
9291a0cd
TT
5823 /* If we're about to read full symbols, don't bother with the
5824 indices. In this case we also don't care if some other debug
5825 format is making psymtabs, because they are all about to be
5826 expanded anyway. */
5827 if ((objfile->flags & OBJF_READNOW))
5828 {
9291a0cd 5829 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5830 create_all_comp_units (dwarf2_per_objfile);
5831 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5832 dwarf2_per_objfile->quick_file_names_table
5833 = create_quick_file_names_table
5834 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5835
b76e467d 5836 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5837 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5838 {
ff4c9fec 5839 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5840
e254ef6a
DE
5841 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5842 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5843 }
5844
5845 /* Return 1 so that gdb sees the "quick" functions. However,
5846 these functions will be no-ops because we will have expanded
5847 all symtabs. */
3c0aa29a
PA
5848 *index_kind = dw_index_kind::GDB_INDEX;
5849 return true;
9291a0cd
TT
5850 }
5851
ed2dc618 5852 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5853 {
5854 *index_kind = dw_index_kind::DEBUG_NAMES;
5855 return true;
5856 }
927aa2e7 5857
4485a1c1
SM
5858 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5859 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5860 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5861 {
5862 *index_kind = dw_index_kind::GDB_INDEX;
5863 return true;
5864 }
9291a0cd 5865
87d6a7aa
SM
5866 /* ... otherwise, try to find the index in the index cache. */
5867 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5868 get_gdb_index_contents_from_cache,
5869 get_gdb_index_contents_from_cache_dwz))
5870 {
5871 global_index_cache.hit ();
5872 *index_kind = dw_index_kind::GDB_INDEX;
5873 return true;
5874 }
5875
5876 global_index_cache.miss ();
3c0aa29a 5877 return false;
9291a0cd
TT
5878}
5879
5880\f
5881
dce234bc
PP
5882/* Build a partial symbol table. */
5883
5884void
f29dff0a 5885dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5886{
ed2dc618
SM
5887 struct dwarf2_per_objfile *dwarf2_per_objfile
5888 = get_dwarf2_per_objfile (objfile);
c9bf0622 5889
6eee24ce 5890 init_psymbol_list (objfile, 1024);
c906108c 5891
a70b8144 5892 try
c9bf0622
TT
5893 {
5894 /* This isn't really ideal: all the data we allocate on the
5895 objfile's obstack is still uselessly kept around. However,
5896 freeing it seems unsafe. */
906768f9 5897 psymtab_discarder psymtabs (objfile);
ed2dc618 5898 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5899 psymtabs.keep ();
87d6a7aa
SM
5900
5901 /* (maybe) store an index in the cache. */
5902 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5903 }
230d2906 5904 catch (const gdb_exception_error &except)
492d29ea
PA
5905 {
5906 exception_print (gdb_stderr, except);
5907 }
c906108c 5908}
c906108c 5909
1ce1cefd
DE
5910/* Return the total length of the CU described by HEADER. */
5911
5912static unsigned int
5913get_cu_length (const struct comp_unit_head *header)
5914{
5915 return header->initial_length_size + header->length;
5916}
5917
9c541725 5918/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 5919
9c541725
PA
5920static inline bool
5921offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 5922{
9c541725
PA
5923 sect_offset bottom = cu_header->sect_off;
5924 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 5925
9c541725 5926 return sect_off >= bottom && sect_off < top;
45452591
DE
5927}
5928
3b80fe9b
DE
5929/* Find the base address of the compilation unit for range lists and
5930 location lists. It will normally be specified by DW_AT_low_pc.
5931 In DWARF-3 draft 4, the base address could be overridden by
5932 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5933 compilation units with discontinuous ranges. */
5934
5935static void
5936dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5937{
5938 struct attribute *attr;
5939
5940 cu->base_known = 0;
5941 cu->base_address = 0;
5942
5943 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5944 if (attr != nullptr)
3b80fe9b 5945 {
cd6c91b4 5946 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5947 cu->base_known = 1;
5948 }
5949 else
5950 {
5951 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5952 if (attr != nullptr)
3b80fe9b 5953 {
cd6c91b4 5954 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5955 cu->base_known = 1;
5956 }
5957 }
5958}
5959
93311388 5960/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 5961 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
5962 NOTE: This leaves members offset, first_die_offset to be filled in
5963 by the caller. */
107d2387 5964
d521ce57 5965static const gdb_byte *
107d2387 5966read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
5967 const gdb_byte *info_ptr,
5968 struct dwarf2_section_info *section,
5969 rcuh_kind section_kind)
107d2387
AC
5970{
5971 int signed_addr;
891d2f0b 5972 unsigned int bytes_read;
96b79293
TT
5973 const char *filename = section->get_file_name ();
5974 bfd *abfd = section->get_bfd_owner ();
c764a876
DE
5975
5976 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
5977 cu_header->initial_length_size = bytes_read;
5978 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 5979 info_ptr += bytes_read;
107d2387 5980 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
5981 if (cu_header->version < 2 || cu_header->version > 5)
5982 error (_("Dwarf Error: wrong version in compilation unit header "
5983 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
5984 cu_header->version, filename);
107d2387 5985 info_ptr += 2;
43988095
JK
5986 if (cu_header->version < 5)
5987 switch (section_kind)
5988 {
5989 case rcuh_kind::COMPILE:
5990 cu_header->unit_type = DW_UT_compile;
5991 break;
5992 case rcuh_kind::TYPE:
5993 cu_header->unit_type = DW_UT_type;
5994 break;
5995 default:
5996 internal_error (__FILE__, __LINE__,
5997 _("read_comp_unit_head: invalid section_kind"));
5998 }
5999 else
6000 {
6001 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6002 (read_1_byte (abfd, info_ptr));
6003 info_ptr += 1;
6004 switch (cu_header->unit_type)
6005 {
6006 case DW_UT_compile:
a084a2a6
AT
6007 case DW_UT_partial:
6008 case DW_UT_skeleton:
6009 case DW_UT_split_compile:
43988095
JK
6010 if (section_kind != rcuh_kind::COMPILE)
6011 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6012 "(is %s, should be %s) [in module %s]"),
6013 dwarf_unit_type_name (cu_header->unit_type),
6014 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6015 break;
6016 case DW_UT_type:
a084a2a6 6017 case DW_UT_split_type:
43988095
JK
6018 section_kind = rcuh_kind::TYPE;
6019 break;
6020 default:
6021 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6022 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6023 "[in module %s]"), cu_header->unit_type,
6024 dwarf_unit_type_name (DW_UT_compile),
6025 dwarf_unit_type_name (DW_UT_skeleton),
6026 dwarf_unit_type_name (DW_UT_split_compile),
6027 dwarf_unit_type_name (DW_UT_type),
6028 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6029 }
6030
6031 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6032 info_ptr += 1;
6033 }
9c541725
PA
6034 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6035 cu_header,
6036 &bytes_read);
613e1657 6037 info_ptr += bytes_read;
43988095
JK
6038 if (cu_header->version < 5)
6039 {
6040 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6041 info_ptr += 1;
6042 }
107d2387
AC
6043 signed_addr = bfd_get_sign_extend_vma (abfd);
6044 if (signed_addr < 0)
8e65ff28 6045 internal_error (__FILE__, __LINE__,
e2e0b3e5 6046 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6047 cu_header->signed_addr_p = signed_addr;
c764a876 6048
a084a2a6
AT
6049 bool header_has_signature = section_kind == rcuh_kind::TYPE
6050 || cu_header->unit_type == DW_UT_skeleton
6051 || cu_header->unit_type == DW_UT_split_compile;
43988095 6052
a084a2a6
AT
6053 if (header_has_signature)
6054 {
43988095
JK
6055 cu_header->signature = read_8_bytes (abfd, info_ptr);
6056 info_ptr += 8;
a084a2a6 6057 }
43988095 6058
a084a2a6
AT
6059 if (section_kind == rcuh_kind::TYPE)
6060 {
6061 LONGEST type_offset;
43988095
JK
6062 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6063 info_ptr += bytes_read;
9c541725
PA
6064 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6065 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6066 error (_("Dwarf Error: Too big type_offset in compilation unit "
6067 "header (is %s) [in module %s]"), plongest (type_offset),
6068 filename);
6069 }
6070
107d2387
AC
6071 return info_ptr;
6072}
6073
36586728
TT
6074/* Helper function that returns the proper abbrev section for
6075 THIS_CU. */
6076
6077static struct dwarf2_section_info *
6078get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6079{
6080 struct dwarf2_section_info *abbrev;
ed2dc618 6081 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6082
6083 if (this_cu->is_dwz)
ed2dc618 6084 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6085 else
6086 abbrev = &dwarf2_per_objfile->abbrev;
6087
6088 return abbrev;
6089}
6090
9ff913ba
DE
6091/* Subroutine of read_and_check_comp_unit_head and
6092 read_and_check_type_unit_head to simplify them.
6093 Perform various error checking on the header. */
6094
6095static void
ed2dc618
SM
6096error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6097 struct comp_unit_head *header,
4bdcc0c1
DE
6098 struct dwarf2_section_info *section,
6099 struct dwarf2_section_info *abbrev_section)
9ff913ba 6100{
96b79293 6101 const char *filename = section->get_file_name ();
9ff913ba 6102
9c541725 6103 if (to_underlying (header->abbrev_sect_off)
36586728 6104 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6105 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6106 "(offset %s + 6) [in module %s]"),
6107 sect_offset_str (header->abbrev_sect_off),
6108 sect_offset_str (header->sect_off),
9ff913ba
DE
6109 filename);
6110
9c541725 6111 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6112 avoid potential 32-bit overflow. */
9c541725 6113 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6114 > section->size)
9c541725 6115 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6116 "(offset %s + 0) [in module %s]"),
6117 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6118 filename);
6119}
6120
6121/* Read in a CU/TU header and perform some basic error checking.
6122 The contents of the header are stored in HEADER.
6123 The result is a pointer to the start of the first DIE. */
adabb602 6124
d521ce57 6125static const gdb_byte *
ed2dc618
SM
6126read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6127 struct comp_unit_head *header,
9ff913ba 6128 struct dwarf2_section_info *section,
4bdcc0c1 6129 struct dwarf2_section_info *abbrev_section,
d521ce57 6130 const gdb_byte *info_ptr,
43988095 6131 rcuh_kind section_kind)
72bf9492 6132{
d521ce57 6133 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6134
9c541725 6135 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6136
43988095 6137 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6138
9c541725 6139 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6140
ed2dc618
SM
6141 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6142 abbrev_section);
9ff913ba
DE
6143
6144 return info_ptr;
348e048f
DE
6145}
6146
f4dc4d17
DE
6147/* Fetch the abbreviation table offset from a comp or type unit header. */
6148
6149static sect_offset
ed2dc618
SM
6150read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6151 struct dwarf2_section_info *section,
9c541725 6152 sect_offset sect_off)
f4dc4d17 6153{
96b79293 6154 bfd *abfd = section->get_bfd_owner ();
d521ce57 6155 const gdb_byte *info_ptr;
ac298888 6156 unsigned int initial_length_size, offset_size;
43988095 6157 uint16_t version;
f4dc4d17 6158
96b79293 6159 section->read (dwarf2_per_objfile->objfile);
9c541725 6160 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6161 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6162 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6163 info_ptr += initial_length_size;
6164
6165 version = read_2_bytes (abfd, info_ptr);
6166 info_ptr += 2;
6167 if (version >= 5)
6168 {
6169 /* Skip unit type and address size. */
6170 info_ptr += 2;
6171 }
6172
9c541725 6173 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6174}
6175
aaa75496
JB
6176/* Allocate a new partial symtab for file named NAME and mark this new
6177 partial symtab as being an include of PST. */
6178
6179static void
891813be 6180dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6181 struct objfile *objfile)
6182{
891813be 6183 dwarf2_psymtab *subpst = new dwarf2_psymtab (name, objfile);
aaa75496 6184
fbd9ab74
JK
6185 if (!IS_ABSOLUTE_PATH (subpst->filename))
6186 {
6187 /* It shares objfile->objfile_obstack. */
6188 subpst->dirname = pst->dirname;
6189 }
6190
a9342b62 6191 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6192 subpst->dependencies[0] = pst;
6193 subpst->number_of_dependencies = 1;
6194
aaa75496
JB
6195 /* No private part is necessary for include psymtabs. This property
6196 can be used to differentiate between such include psymtabs and
10b3939b 6197 the regular ones. */
891813be 6198 subpst->per_cu_data = nullptr;
aaa75496
JB
6199}
6200
6201/* Read the Line Number Program data and extract the list of files
6202 included by the source file represented by PST. Build an include
d85a05f0 6203 partial symtab for each of these included files. */
aaa75496
JB
6204
6205static void
6206dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6207 struct die_info *die,
891813be 6208 dwarf2_psymtab *pst)
aaa75496 6209{
fff8551c 6210 line_header_up lh;
d85a05f0 6211 struct attribute *attr;
aaa75496 6212
d85a05f0 6213 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6214 if (attr != nullptr)
9c541725 6215 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6216 if (lh == NULL)
6217 return; /* No linetable, so no includes. */
6218
79748972
TT
6219 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6220 that we pass in the raw text_low here; that is ok because we're
6221 only decoding the line table to make include partial symtabs, and
6222 so the addresses aren't really used. */
4ae976d1 6223 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6224 pst->raw_text_low (), 1);
aaa75496
JB
6225}
6226
348e048f 6227static hashval_t
52dc124a 6228hash_signatured_type (const void *item)
348e048f 6229{
9a3c8263
SM
6230 const struct signatured_type *sig_type
6231 = (const struct signatured_type *) item;
9a619af0 6232
348e048f 6233 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6234 return sig_type->signature;
348e048f
DE
6235}
6236
6237static int
52dc124a 6238eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6239{
9a3c8263
SM
6240 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6241 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6242
348e048f
DE
6243 return lhs->signature == rhs->signature;
6244}
6245
1fd400ff
TT
6246/* Allocate a hash table for signatured types. */
6247
b0b6a987 6248static htab_up
673bfd45 6249allocate_signatured_type_table (struct objfile *objfile)
1fd400ff 6250{
b0b6a987
TT
6251 return htab_up (htab_create_alloc (41,
6252 hash_signatured_type,
6253 eq_signatured_type,
6254 NULL, xcalloc, xfree));
1fd400ff
TT
6255}
6256
d467dd73 6257/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6258
6259static int
d467dd73 6260add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6261{
9a3c8263 6262 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6263 std::vector<signatured_type *> *all_type_units
6264 = (std::vector<signatured_type *> *) datum;
1fd400ff 6265
b2bdb8cf 6266 all_type_units->push_back (sigt);
1fd400ff
TT
6267
6268 return 1;
6269}
6270
78d4d2c5 6271/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6272 and fill them into TYPES_HTAB. It will process only type units,
6273 therefore DW_UT_type. */
c88ee1f0 6274
78d4d2c5 6275static void
ed2dc618
SM
6276create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6277 struct dwo_file *dwo_file,
b0b6a987 6278 dwarf2_section_info *section, htab_up &types_htab,
43988095 6279 rcuh_kind section_kind)
348e048f 6280{
3019eac3 6281 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6282 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6283 bfd *abfd;
6284 const gdb_byte *info_ptr, *end_ptr;
348e048f 6285
4bdcc0c1
DE
6286 abbrev_section = (dwo_file != NULL
6287 ? &dwo_file->sections.abbrev
6288 : &dwarf2_per_objfile->abbrev);
6289
b4f54984 6290 if (dwarf_read_debug)
43988095 6291 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6292 section->get_name (),
6293 abbrev_section->get_file_name ());
09406207 6294
96b79293 6295 section->read (objfile);
78d4d2c5 6296 info_ptr = section->buffer;
348e048f 6297
78d4d2c5
JK
6298 if (info_ptr == NULL)
6299 return;
348e048f 6300
78d4d2c5
JK
6301 /* We can't set abfd until now because the section may be empty or
6302 not present, in which case the bfd is unknown. */
96b79293 6303 abfd = section->get_bfd_owner ();
348e048f 6304
c0ab21c2
TT
6305 /* We don't use cutu_reader here because we don't need to read
6306 any dies: the signature is in the header. */
3019eac3 6307
78d4d2c5
JK
6308 end_ptr = info_ptr + section->size;
6309 while (info_ptr < end_ptr)
6310 {
78d4d2c5
JK
6311 struct signatured_type *sig_type;
6312 struct dwo_unit *dwo_tu;
6313 void **slot;
6314 const gdb_byte *ptr = info_ptr;
6315 struct comp_unit_head header;
6316 unsigned int length;
8b70b953 6317
9c541725 6318 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6319
a49dd8dd
JK
6320 /* Initialize it due to a false compiler warning. */
6321 header.signature = -1;
9c541725 6322 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6323
78d4d2c5
JK
6324 /* We need to read the type's signature in order to build the hash
6325 table, but we don't need anything else just yet. */
348e048f 6326
ed2dc618 6327 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6328 abbrev_section, ptr, section_kind);
348e048f 6329
78d4d2c5 6330 length = get_cu_length (&header);
6caca83c 6331
78d4d2c5
JK
6332 /* Skip dummy type units. */
6333 if (ptr >= info_ptr + length
43988095
JK
6334 || peek_abbrev_code (abfd, ptr) == 0
6335 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6336 {
6337 info_ptr += length;
6338 continue;
6339 }
dee91e82 6340
78d4d2c5
JK
6341 if (types_htab == NULL)
6342 {
6343 if (dwo_file)
6344 types_htab = allocate_dwo_unit_table (objfile);
6345 else
6346 types_htab = allocate_signatured_type_table (objfile);
6347 }
8b70b953 6348
78d4d2c5
JK
6349 if (dwo_file)
6350 {
6351 sig_type = NULL;
6352 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6353 struct dwo_unit);
6354 dwo_tu->dwo_file = dwo_file;
43988095 6355 dwo_tu->signature = header.signature;
9c541725 6356 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6357 dwo_tu->section = section;
9c541725 6358 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6359 dwo_tu->length = length;
6360 }
6361 else
6362 {
6363 /* N.B.: type_offset is not usable if this type uses a DWO file.
6364 The real type_offset is in the DWO file. */
6365 dwo_tu = NULL;
6366 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6367 struct signatured_type);
43988095 6368 sig_type->signature = header.signature;
9c541725 6369 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6370 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6371 sig_type->per_cu.is_debug_types = 1;
6372 sig_type->per_cu.section = section;
9c541725 6373 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6374 sig_type->per_cu.length = length;
6375 }
6376
b0b6a987 6377 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6378 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6379 INSERT);
6380 gdb_assert (slot != NULL);
6381 if (*slot != NULL)
6382 {
9c541725 6383 sect_offset dup_sect_off;
0349ea22 6384
3019eac3
DE
6385 if (dwo_file)
6386 {
78d4d2c5
JK
6387 const struct dwo_unit *dup_tu
6388 = (const struct dwo_unit *) *slot;
6389
9c541725 6390 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6391 }
6392 else
6393 {
78d4d2c5
JK
6394 const struct signatured_type *dup_tu
6395 = (const struct signatured_type *) *slot;
6396
9c541725 6397 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6398 }
8b70b953 6399
b98664d3 6400 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6401 " the entry at offset %s, signature %s"),
6402 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6403 hex_string (header.signature));
78d4d2c5
JK
6404 }
6405 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6406
78d4d2c5 6407 if (dwarf_read_debug > 1)
9d8780f0
SM
6408 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6409 sect_offset_str (sect_off),
43988095 6410 hex_string (header.signature));
3019eac3 6411
78d4d2c5
JK
6412 info_ptr += length;
6413 }
6414}
3019eac3 6415
78d4d2c5
JK
6416/* Create the hash table of all entries in the .debug_types
6417 (or .debug_types.dwo) section(s).
6418 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6419 otherwise it is NULL.
b3c8eb43 6420
78d4d2c5 6421 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6422
78d4d2c5 6423 Note: This function processes DWO files only, not DWP files. */
348e048f 6424
78d4d2c5 6425static void
ed2dc618
SM
6426create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6427 struct dwo_file *dwo_file,
fd5866f6 6428 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6429 htab_up &types_htab)
78d4d2c5 6430{
fd5866f6
SM
6431 for (dwarf2_section_info &section : type_sections)
6432 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6433 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6434}
6435
6436/* Create the hash table of all entries in the .debug_types section,
6437 and initialize all_type_units.
6438 The result is zero if there is an error (e.g. missing .debug_types section),
6439 otherwise non-zero. */
6440
6441static int
ed2dc618 6442create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6443{
b0b6a987 6444 htab_up types_htab;
3019eac3 6445
ed2dc618
SM
6446 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6447 &dwarf2_per_objfile->info, types_htab,
43988095 6448 rcuh_kind::COMPILE);
ed2dc618
SM
6449 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6450 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6451 if (types_htab == NULL)
6452 {
6453 dwarf2_per_objfile->signatured_types = NULL;
6454 return 0;
6455 }
6456
b0b6a987 6457 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6458
b2bdb8cf 6459 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6460 dwarf2_per_objfile->all_type_units.reserve
6461 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6462
b0b6a987
TT
6463 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6464 add_signatured_type_cu_to_table,
b2bdb8cf 6465 &dwarf2_per_objfile->all_type_units);
1fd400ff 6466
348e048f
DE
6467 return 1;
6468}
6469
6aa5f3a6
DE
6470/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6471 If SLOT is non-NULL, it is the entry to use in the hash table.
6472 Otherwise we find one. */
6473
6474static struct signatured_type *
ed2dc618
SM
6475add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6476 void **slot)
6aa5f3a6
DE
6477{
6478 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6479
b2bdb8cf
SM
6480 if (dwarf2_per_objfile->all_type_units.size ()
6481 == dwarf2_per_objfile->all_type_units.capacity ())
6482 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6483
b2bdb8cf
SM
6484 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6485 struct signatured_type);
6486
6487 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6488 sig_type->signature = sig;
6489 sig_type->per_cu.is_debug_types = 1;
6490 if (dwarf2_per_objfile->using_index)
6491 {
6492 sig_type->per_cu.v.quick =
6493 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6494 struct dwarf2_per_cu_quick_data);
6495 }
6496
6497 if (slot == NULL)
6498 {
b0b6a987 6499 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6500 sig_type, INSERT);
6501 }
6502 gdb_assert (*slot == NULL);
6503 *slot = sig_type;
6504 /* The rest of sig_type must be filled in by the caller. */
6505 return sig_type;
6506}
6507
a2ce51a0
DE
6508/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6509 Fill in SIG_ENTRY with DWO_ENTRY. */
6510
6511static void
ed2dc618 6512fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6513 struct signatured_type *sig_entry,
6514 struct dwo_unit *dwo_entry)
6515{
7ee85ab1 6516 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6517 gdb_assert (! sig_entry->per_cu.queued);
6518 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6519 if (dwarf2_per_objfile->using_index)
6520 {
6521 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6522 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6523 }
6524 else
6525 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6526 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6527 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6528 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6529 gdb_assert (sig_entry->dwo_unit == NULL);
6530
6531 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6532 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6533 sig_entry->per_cu.length = dwo_entry->length;
6534 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6535 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6536 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6537 sig_entry->dwo_unit = dwo_entry;
6538}
6539
6540/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6541 If we haven't read the TU yet, create the signatured_type data structure
6542 for a TU to be read in directly from a DWO file, bypassing the stub.
6543 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6544 using .gdb_index, then when reading a CU we want to stay in the DWO file
6545 containing that CU. Otherwise we could end up reading several other DWO
6546 files (due to comdat folding) to process the transitive closure of all the
6547 mentioned TUs, and that can be slow. The current DWO file will have every
6548 type signature that it needs.
a2ce51a0
DE
6549 We only do this for .gdb_index because in the psymtab case we already have
6550 to read all the DWOs to build the type unit groups. */
6551
6552static struct signatured_type *
6553lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6554{
518817b3
SM
6555 struct dwarf2_per_objfile *dwarf2_per_objfile
6556 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6557 struct objfile *objfile = dwarf2_per_objfile->objfile;
6558 struct dwo_file *dwo_file;
6559 struct dwo_unit find_dwo_entry, *dwo_entry;
6560 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6561 void **slot;
a2ce51a0
DE
6562
6563 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6564
6aa5f3a6
DE
6565 /* If TU skeletons have been removed then we may not have read in any
6566 TUs yet. */
6567 if (dwarf2_per_objfile->signatured_types == NULL)
6568 {
6569 dwarf2_per_objfile->signatured_types
6570 = allocate_signatured_type_table (objfile);
6571 }
a2ce51a0
DE
6572
6573 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6574 Use the global signatured_types array to do our own comdat-folding
6575 of types. If this is the first time we're reading this TU, and
6576 the TU has an entry in .gdb_index, replace the recorded data from
6577 .gdb_index with this TU. */
a2ce51a0 6578
a2ce51a0 6579 find_sig_entry.signature = sig;
b0b6a987 6580 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6581 &find_sig_entry, INSERT);
9a3c8263 6582 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6583
6584 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6585 read. Don't reassign the global entry to point to this DWO if that's
6586 the case. Also note that if the TU is already being read, it may not
6587 have come from a DWO, the program may be a mix of Fission-compiled
6588 code and non-Fission-compiled code. */
6589
6590 /* Have we already tried to read this TU?
6591 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6592 needn't exist in the global table yet). */
6593 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6594 return sig_entry;
6595
6aa5f3a6
DE
6596 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6597 dwo_unit of the TU itself. */
6598 dwo_file = cu->dwo_unit->dwo_file;
6599
a2ce51a0
DE
6600 /* Ok, this is the first time we're reading this TU. */
6601 if (dwo_file->tus == NULL)
6602 return NULL;
6603 find_dwo_entry.signature = sig;
b0b6a987
TT
6604 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6605 &find_dwo_entry);
a2ce51a0
DE
6606 if (dwo_entry == NULL)
6607 return NULL;
6608
6aa5f3a6
DE
6609 /* If the global table doesn't have an entry for this TU, add one. */
6610 if (sig_entry == NULL)
ed2dc618 6611 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6612
ed2dc618 6613 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6614 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6615 return sig_entry;
6616}
6617
a2ce51a0
DE
6618/* Subroutine of lookup_signatured_type.
6619 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6620 then try the DWP file. If the TU stub (skeleton) has been removed then
6621 it won't be in .gdb_index. */
a2ce51a0
DE
6622
6623static struct signatured_type *
6624lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6625{
518817b3
SM
6626 struct dwarf2_per_objfile *dwarf2_per_objfile
6627 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6628 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6629 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6630 struct dwo_unit *dwo_entry;
6631 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6632 void **slot;
a2ce51a0
DE
6633
6634 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6635 gdb_assert (dwp_file != NULL);
6636
6aa5f3a6
DE
6637 /* If TU skeletons have been removed then we may not have read in any
6638 TUs yet. */
6639 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6640 {
6aa5f3a6
DE
6641 dwarf2_per_objfile->signatured_types
6642 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6643 }
6644
6aa5f3a6 6645 find_sig_entry.signature = sig;
b0b6a987 6646 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6647 &find_sig_entry, INSERT);
9a3c8263 6648 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6649
6650 /* Have we already tried to read this TU?
6651 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6652 needn't exist in the global table yet). */
6653 if (sig_entry != NULL)
6654 return sig_entry;
6655
a2ce51a0
DE
6656 if (dwp_file->tus == NULL)
6657 return NULL;
ed2dc618 6658 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6659 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6660 if (dwo_entry == NULL)
6661 return NULL;
6662
ed2dc618
SM
6663 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6664 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6665
a2ce51a0
DE
6666 return sig_entry;
6667}
6668
380bca97 6669/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6670 Returns NULL if signature SIG is not present in the table.
6671 It is up to the caller to complain about this. */
348e048f
DE
6672
6673static struct signatured_type *
a2ce51a0 6674lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6675{
518817b3
SM
6676 struct dwarf2_per_objfile *dwarf2_per_objfile
6677 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6678
a2ce51a0
DE
6679 if (cu->dwo_unit
6680 && dwarf2_per_objfile->using_index)
6681 {
6682 /* We're in a DWO/DWP file, and we're using .gdb_index.
6683 These cases require special processing. */
ed2dc618 6684 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6685 return lookup_dwo_signatured_type (cu, sig);
6686 else
6687 return lookup_dwp_signatured_type (cu, sig);
6688 }
6689 else
6690 {
6691 struct signatured_type find_entry, *entry;
348e048f 6692
a2ce51a0
DE
6693 if (dwarf2_per_objfile->signatured_types == NULL)
6694 return NULL;
6695 find_entry.signature = sig;
9a3c8263 6696 entry = ((struct signatured_type *)
b0b6a987
TT
6697 htab_find (dwarf2_per_objfile->signatured_types.get (),
6698 &find_entry));
a2ce51a0
DE
6699 return entry;
6700 }
348e048f 6701}
18a8505e
AT
6702
6703/* Return the address base of the compile unit, which, if exists, is stored
6704 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6705static gdb::optional<ULONGEST>
6706lookup_addr_base (struct die_info *comp_unit_die)
6707{
6708 struct attribute *attr;
6709 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_addr_base);
6710 if (attr == nullptr)
6711 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_addr_base);
6712 if (attr == nullptr)
6713 return gdb::optional<ULONGEST> ();
6714 return DW_UNSND (attr);
6715}
6716
6717/* Return range lists base of the compile unit, which, if exists, is stored
6718 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6719static ULONGEST
6720lookup_ranges_base (struct die_info *comp_unit_die)
6721{
6722 struct attribute *attr;
6723 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_rnglists_base);
6724 if (attr == nullptr)
6725 attr = dwarf2_attr_no_follow (comp_unit_die, DW_AT_GNU_ranges_base);
6726 if (attr == nullptr)
6727 return 0;
6728 return DW_UNSND (attr);
6729}
6730
42e7ad6c 6731/* Low level DIE reading support. */
348e048f 6732
d85a05f0
DJ
6733/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6734
6735static void
6736init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6737 struct dwarf2_cu *cu,
3019eac3 6738 struct dwarf2_section_info *section,
685af9cd
TT
6739 struct dwo_file *dwo_file,
6740 struct abbrev_table *abbrev_table)
d85a05f0 6741{
fceca515 6742 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6743 reader->abfd = section->get_bfd_owner ();
d85a05f0 6744 reader->cu = cu;
3019eac3 6745 reader->dwo_file = dwo_file;
dee91e82
DE
6746 reader->die_section = section;
6747 reader->buffer = section->buffer;
f664829e 6748 reader->buffer_end = section->buffer + section->size;
685af9cd 6749 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6750}
6751
c0ab21c2 6752/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6753 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6754 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6755 already.
6756
6757 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6758 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6759 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6760 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6761 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6762 STUB_COMP_DIR may be non-NULL.
3e225074 6763 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6764 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6765 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6766 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6767 kept around for at least as long as *RESULT_READER.
6768
b0c7bfa9
DE
6769 The result is non-zero if a valid (non-dummy) DIE was found. */
6770
6771static int
6772read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6773 struct dwo_unit *dwo_unit,
b0c7bfa9 6774 struct die_info *stub_comp_unit_die,
a2ce51a0 6775 const char *stub_comp_dir,
b0c7bfa9 6776 struct die_reader_specs *result_reader,
d521ce57 6777 const gdb_byte **result_info_ptr,
b0c7bfa9 6778 struct die_info **result_comp_unit_die,
685af9cd 6779 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6780{
ed2dc618 6781 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6782 struct objfile *objfile = dwarf2_per_objfile->objfile;
6783 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6784 bfd *abfd;
d521ce57 6785 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6786 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6787 int i,num_extra_attrs;
6788 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6789 struct die_info *comp_unit_die;
6790
b0aeadb3
DE
6791 /* At most one of these may be provided. */
6792 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6793
b0c7bfa9
DE
6794 /* These attributes aren't processed until later:
6795 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6796 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6797 referenced later. However, these attributes are found in the stub
6798 which we won't have later. In order to not impose this complication
6799 on the rest of the code, we read them here and copy them to the
6800 DWO CU/TU die. */
b0c7bfa9
DE
6801
6802 stmt_list = NULL;
6803 low_pc = NULL;
6804 high_pc = NULL;
6805 ranges = NULL;
6806 comp_dir = NULL;
6807
6808 if (stub_comp_unit_die != NULL)
6809 {
6810 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6811 DWO file. */
6812 if (! this_cu->is_debug_types)
6813 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6814 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6815 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6816 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6817 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6818
18a8505e 6819 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
b0c7bfa9 6820
18a8505e
AT
6821 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6822 here (if needed). We need the value before we can process
6823 DW_AT_ranges. */
6824 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
b0c7bfa9 6825 }
a2ce51a0
DE
6826 else if (stub_comp_dir != NULL)
6827 {
6828 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6829 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6830 comp_dir->name = DW_AT_comp_dir;
6831 comp_dir->form = DW_FORM_string;
6832 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6833 DW_STRING (comp_dir) = stub_comp_dir;
6834 }
b0c7bfa9
DE
6835
6836 /* Set up for reading the DWO CU/TU. */
6837 cu->dwo_unit = dwo_unit;
685af9cd 6838 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6839 section->read (objfile);
6840 abfd = section->get_bfd_owner ();
9c541725
PA
6841 begin_info_ptr = info_ptr = (section->buffer
6842 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6843 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6844
6845 if (this_cu->is_debug_types)
6846 {
b0c7bfa9
DE
6847 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6848
ed2dc618
SM
6849 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6850 &cu->header, section,
b0c7bfa9 6851 dwo_abbrev_section,
43988095 6852 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6853 /* This is not an assert because it can be caused by bad debug info. */
43988095 6854 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6855 {
6856 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6857 " TU at offset %s [in module %s]"),
a2ce51a0 6858 hex_string (sig_type->signature),
43988095 6859 hex_string (cu->header.signature),
9d8780f0 6860 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6861 bfd_get_filename (abfd));
6862 }
9c541725 6863 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6864 /* For DWOs coming from DWP files, we don't know the CU length
6865 nor the type's offset in the TU until now. */
6866 dwo_unit->length = get_cu_length (&cu->header);
9c541725 6867 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6868
6869 /* Establish the type offset that can be used to lookup the type.
6870 For DWO files, we don't know it until now. */
9c541725
PA
6871 sig_type->type_offset_in_section
6872 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6873 }
6874 else
6875 {
ed2dc618
SM
6876 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6877 &cu->header, section,
b0c7bfa9 6878 dwo_abbrev_section,
43988095 6879 info_ptr, rcuh_kind::COMPILE);
9c541725 6880 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6881 /* For DWOs coming from DWP files, we don't know the CU length
6882 until now. */
6883 dwo_unit->length = get_cu_length (&cu->header);
6884 }
6885
685af9cd 6886 *result_dwo_abbrev_table
86de1d91
TT
6887 = abbrev_table::read (objfile, dwo_abbrev_section,
6888 cu->header.abbrev_sect_off);
685af9cd
TT
6889 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6890 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6891
6892 /* Read in the die, but leave space to copy over the attributes
6893 from the stub. This has the benefit of simplifying the rest of
6894 the code - all the work to maintain the illusion of a single
6895 DW_TAG_{compile,type}_unit DIE is done here. */
6896 num_extra_attrs = ((stmt_list != NULL)
6897 + (low_pc != NULL)
6898 + (high_pc != NULL)
6899 + (ranges != NULL)
6900 + (comp_dir != NULL));
6901 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6902 num_extra_attrs);
b0c7bfa9
DE
6903
6904 /* Copy over the attributes from the stub to the DIE we just read in. */
6905 comp_unit_die = *result_comp_unit_die;
6906 i = comp_unit_die->num_attrs;
6907 if (stmt_list != NULL)
6908 comp_unit_die->attrs[i++] = *stmt_list;
6909 if (low_pc != NULL)
6910 comp_unit_die->attrs[i++] = *low_pc;
6911 if (high_pc != NULL)
6912 comp_unit_die->attrs[i++] = *high_pc;
6913 if (ranges != NULL)
6914 comp_unit_die->attrs[i++] = *ranges;
6915 if (comp_dir != NULL)
6916 comp_unit_die->attrs[i++] = *comp_dir;
6917 comp_unit_die->num_attrs += num_extra_attrs;
6918
b4f54984 6919 if (dwarf_die_debug)
bf6af496
DE
6920 {
6921 fprintf_unfiltered (gdb_stdlog,
6922 "Read die from %s@0x%x of %s:\n",
96b79293 6923 section->get_name (),
bf6af496
DE
6924 (unsigned) (begin_info_ptr - section->buffer),
6925 bfd_get_filename (abfd));
b4f54984 6926 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6927 }
6928
b0c7bfa9
DE
6929 /* Skip dummy compilation units. */
6930 if (info_ptr >= begin_info_ptr + dwo_unit->length
6931 || peek_abbrev_code (abfd, info_ptr) == 0)
6932 return 0;
6933
6934 *result_info_ptr = info_ptr;
6935 return 1;
6936}
6937
a084a2a6
AT
6938/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6939 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6940 signature is part of the header. */
6941static gdb::optional<ULONGEST>
6942lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6943{
6944 if (cu->header.version >= 5)
6945 return cu->header.signature;
6946 struct attribute *attr;
6947 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6948 if (attr == nullptr)
6949 return gdb::optional<ULONGEST> ();
6950 return DW_UNSND (attr);
6951}
6952
c0ab21c2 6953/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6954 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6955 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6956
6957static struct dwo_unit *
6958lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6959 struct die_info *comp_unit_die,
6960 const char *dwo_name)
b0c7bfa9
DE
6961{
6962 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6963 struct dwo_unit *dwo_unit;
c0ab21c2 6964 const char *comp_dir;
b0c7bfa9 6965
a2ce51a0
DE
6966 gdb_assert (cu != NULL);
6967
b0c7bfa9 6968 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6969 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6970 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6971
6972 if (this_cu->is_debug_types)
6973 {
6974 struct signatured_type *sig_type;
6975
6976 /* Since this_cu is the first member of struct signatured_type,
6977 we can go from a pointer to one to a pointer to the other. */
6978 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6979 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6980 }
6981 else
6982 {
a084a2a6
AT
6983 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6984 if (!signature.has_value ())
b0c7bfa9
DE
6985 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6986 " [in module %s]"),
e3b94546 6987 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6988 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6989 *signature);
b0c7bfa9
DE
6990 }
6991
b0c7bfa9
DE
6992 return dwo_unit;
6993}
6994
c0ab21c2 6995/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6996 See it for a description of the parameters.
fcd3b13d 6997 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6998
c0ab21c2
TT
6999void
7000cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7001 int use_existing_cu, int keep)
a2ce51a0 7002{
a2ce51a0 7003 struct signatured_type *sig_type;
a2ce51a0 7004 struct die_reader_specs reader;
a2ce51a0
DE
7005
7006 /* Verify we can do the following downcast, and that we have the
7007 data we need. */
7008 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7009 sig_type = (struct signatured_type *) this_cu;
7010 gdb_assert (sig_type->dwo_unit != NULL);
7011
6aa5f3a6
DE
7012 if (use_existing_cu && this_cu->cu != NULL)
7013 {
7014 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7015 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7016 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7017 }
7018 else
7019 {
7020 /* If !use_existing_cu, this_cu->cu must be NULL. */
7021 gdb_assert (this_cu->cu == NULL);
c0ab21c2 7022 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7023 }
7024
7025 /* A future optimization, if needed, would be to use an existing
7026 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7027 could share abbrev tables. */
a2ce51a0
DE
7028
7029 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7030 NULL /* stub_comp_unit_die */,
7031 sig_type->dwo_unit->dwo_file->comp_dir,
7032 &reader, &info_ptr,
3e225074 7033 &comp_unit_die,
c0ab21c2 7034 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7035 {
7036 /* Dummy die. */
c0ab21c2 7037 dummy_p = true;
a2ce51a0 7038 }
a2ce51a0
DE
7039}
7040
fd820528 7041/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7042 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7043
f4dc4d17
DE
7044 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7045 Otherwise the table specified in the comp unit header is read in and used.
7046 This is an optimization for when we already have the abbrev table.
7047
dee91e82
DE
7048 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7049 Otherwise, a new CU is allocated with xmalloc.
7050
7051 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
c0ab21c2
TT
7052 read_in_chain. Otherwise the dwarf2_cu data is freed at the
7053 end. */
aaa75496 7054
c0ab21c2
TT
7055cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7056 struct abbrev_table *abbrev_table,
7057 int use_existing_cu, int keep,
7058 bool skip_partial)
7059 : die_reader_specs {},
7060 m_this_cu (this_cu),
7061 m_keep (keep)
c906108c 7062{
ed2dc618 7063 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7064 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7065 struct dwarf2_section_info *section = this_cu->section;
96b79293 7066 bfd *abfd = section->get_bfd_owner ();
dee91e82 7067 struct dwarf2_cu *cu;
c0ab21c2 7068 const gdb_byte *begin_info_ptr;
dee91e82 7069 struct signatured_type *sig_type = NULL;
4bdcc0c1 7070 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7071 /* Non-zero if CU currently points to a DWO file and we need to
7072 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7073 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7074 int rereading_dwo_cu = 0;
c906108c 7075
b4f54984 7076 if (dwarf_die_debug)
9d8780f0 7077 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7078 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7079 sect_offset_str (this_cu->sect_off));
09406207 7080
dee91e82
DE
7081 if (use_existing_cu)
7082 gdb_assert (keep);
23745b47 7083
a2ce51a0
DE
7084 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7085 file (instead of going through the stub), short-circuit all of this. */
7086 if (this_cu->reading_dwo_directly)
7087 {
7088 /* Narrow down the scope of possibilities to have to understand. */
7089 gdb_assert (this_cu->is_debug_types);
7090 gdb_assert (abbrev_table == NULL);
c0ab21c2 7091 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep);
a2ce51a0
DE
7092 return;
7093 }
7094
dee91e82 7095 /* This is cheap if the section is already read in. */
96b79293 7096 section->read (objfile);
dee91e82 7097
9c541725 7098 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7099
7100 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7101
7102 if (use_existing_cu && this_cu->cu != NULL)
7103 {
7104 cu = this_cu->cu;
42e7ad6c
DE
7105 /* If this CU is from a DWO file we need to start over, we need to
7106 refetch the attributes from the skeleton CU.
7107 This could be optimized by retrieving those attributes from when we
7108 were here the first time: the previous comp_unit_die was stored in
7109 comp_unit_obstack. But there's no data yet that we need this
7110 optimization. */
7111 if (cu->dwo_unit != NULL)
7112 rereading_dwo_cu = 1;
dee91e82
DE
7113 }
7114 else
7115 {
7116 /* If !use_existing_cu, this_cu->cu must be NULL. */
7117 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7118 m_new_cu.reset (new dwarf2_cu (this_cu));
7119 cu = m_new_cu.get ();
42e7ad6c 7120 }
dee91e82 7121
b0c7bfa9 7122 /* Get the header. */
9c541725 7123 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7124 {
7125 /* We already have the header, there's no need to read it in again. */
9c541725 7126 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7127 }
7128 else
7129 {
3019eac3 7130 if (this_cu->is_debug_types)
dee91e82 7131 {
ed2dc618
SM
7132 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7133 &cu->header, section,
4bdcc0c1 7134 abbrev_section, info_ptr,
43988095 7135 rcuh_kind::TYPE);
dee91e82 7136
42e7ad6c
DE
7137 /* Since per_cu is the first member of struct signatured_type,
7138 we can go from a pointer to one to a pointer to the other. */
7139 sig_type = (struct signatured_type *) this_cu;
43988095 7140 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7141 gdb_assert (sig_type->type_offset_in_tu
7142 == cu->header.type_cu_offset_in_tu);
7143 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7144
42e7ad6c
DE
7145 /* LENGTH has not been set yet for type units if we're
7146 using .gdb_index. */
1ce1cefd 7147 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7148
7149 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7150 sig_type->type_offset_in_section =
7151 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7152
7153 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7154 }
7155 else
7156 {
ed2dc618
SM
7157 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7158 &cu->header, section,
4bdcc0c1 7159 abbrev_section,
43988095
JK
7160 info_ptr,
7161 rcuh_kind::COMPILE);
dee91e82 7162
9c541725 7163 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7164 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7165 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7166 }
7167 }
10b3939b 7168
6caca83c 7169 /* Skip dummy compilation units. */
dee91e82 7170 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7171 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7172 {
7173 dummy_p = true;
7174 return;
7175 }
6caca83c 7176
433df2d4
DE
7177 /* If we don't have them yet, read the abbrevs for this compilation unit.
7178 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7179 done. */
f4dc4d17 7180 if (abbrev_table != NULL)
685af9cd
TT
7181 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7182 else
f4dc4d17 7183 {
c0ab21c2 7184 m_abbrev_table_holder
86de1d91
TT
7185 = abbrev_table::read (objfile, abbrev_section,
7186 cu->header.abbrev_sect_off);
c0ab21c2 7187 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7188 }
af703f96 7189
dee91e82 7190 /* Read the top level CU/TU die. */
c0ab21c2 7191 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7192 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7193
58f0c718 7194 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7195 {
7196 dummy_p = true;
7197 return;
7198 }
58f0c718 7199
b0c7bfa9 7200 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7201 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7202 table from the DWO file and pass the ownership over to us. It will be
7203 referenced from READER, so we must make sure to free it after we're done
7204 with READER.
7205
b0c7bfa9
DE
7206 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7207 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7208 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7209 if (dwo_name != nullptr)
3019eac3 7210 {
3019eac3 7211 struct dwo_unit *dwo_unit;
b0c7bfa9 7212 struct die_info *dwo_comp_unit_die;
3019eac3 7213
3e225074 7214 if (comp_unit_die->has_children)
6a506a2d 7215 {
b98664d3 7216 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7217 " has children (offset %s) [in module %s]"),
7218 sect_offset_str (this_cu->sect_off),
7219 bfd_get_filename (abfd));
6a506a2d 7220 }
c0ab21c2 7221 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7222 if (dwo_unit != NULL)
3019eac3 7223 {
6a506a2d 7224 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7225 comp_unit_die, NULL,
c0ab21c2 7226 this, &info_ptr,
3e225074 7227 &dwo_comp_unit_die,
c0ab21c2 7228 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7229 {
7230 /* Dummy die. */
c0ab21c2 7231 dummy_p = true;
6a506a2d
DE
7232 return;
7233 }
7234 comp_unit_die = dwo_comp_unit_die;
7235 }
7236 else
7237 {
7238 /* Yikes, we couldn't find the rest of the DIE, we only have
7239 the stub. A complaint has already been logged. There's
7240 not much more we can do except pass on the stub DIE to
7241 die_reader_func. We don't want to throw an error on bad
7242 debug info. */
3019eac3
DE
7243 }
7244 }
c0ab21c2 7245}
3019eac3 7246
c0ab21c2
TT
7247cutu_reader::~cutu_reader ()
7248{
b0c7bfa9 7249 /* Done, clean up. */
c0ab21c2 7250 if (m_new_cu != NULL && m_keep && !dummy_p)
348e048f 7251 {
c0ab21c2
TT
7252 struct dwarf2_per_objfile *dwarf2_per_objfile
7253 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7254 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7255 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7256 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7257 /* The chain owns it now. */
c0ab21c2 7258 m_new_cu.release ();
348e048f 7259 }
dee91e82
DE
7260}
7261
18a8505e
AT
7262/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7263 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7264 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7265
7266 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7267 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7268
7269 We fill in THIS_CU->length.
7270
dee91e82 7271 THIS_CU->cu is always freed when done.
3019eac3 7272 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7273 to care whether it refers to the "main" CU or the DWO CU.
7274
7275 When parent_cu is passed, it is used to provide a default value for
7276 str_offsets_base and addr_base from the parent. */
dee91e82 7277
c0ab21c2
TT
7278cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7279 struct dwarf2_cu *parent_cu,
7280 struct dwo_file *dwo_file)
7281 : die_reader_specs {},
7282 m_this_cu (this_cu)
dee91e82 7283{
ed2dc618 7284 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7285 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7286 struct dwarf2_section_info *section = this_cu->section;
96b79293 7287 bfd *abfd = section->get_bfd_owner ();
33e80786 7288 struct dwarf2_section_info *abbrev_section;
d521ce57 7289 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7290
b4f54984 7291 if (dwarf_die_debug)
9d8780f0 7292 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7293 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7294 sect_offset_str (this_cu->sect_off));
09406207 7295
dee91e82
DE
7296 gdb_assert (this_cu->cu == NULL);
7297
33e80786
DE
7298 abbrev_section = (dwo_file != NULL
7299 ? &dwo_file->sections.abbrev
7300 : get_abbrev_section_for_cu (this_cu));
7301
dee91e82 7302 /* This is cheap if the section is already read in. */
96b79293 7303 section->read (objfile);
dee91e82 7304
c0ab21c2 7305 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7306
9c541725 7307 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7308 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7309 &m_new_cu->header, section,
4bdcc0c1 7310 abbrev_section, info_ptr,
43988095
JK
7311 (this_cu->is_debug_types
7312 ? rcuh_kind::TYPE
7313 : rcuh_kind::COMPILE));
dee91e82 7314
18a8505e
AT
7315 if (parent_cu != nullptr)
7316 {
c0ab21c2
TT
7317 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7318 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7319 }
c0ab21c2 7320 this_cu->length = get_cu_length (&m_new_cu->header);
dee91e82
DE
7321
7322 /* Skip dummy compilation units. */
7323 if (info_ptr >= begin_info_ptr + this_cu->length
7324 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7325 {
7326 dummy_p = true;
7327 return;
7328 }
72bf9492 7329
c0ab21c2 7330 m_abbrev_table_holder
86de1d91
TT
7331 = abbrev_table::read (objfile, abbrev_section,
7332 m_new_cu->header.abbrev_sect_off);
dee91e82 7333
c0ab21c2
TT
7334 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7335 m_abbrev_table_holder.get ());
3e225074 7336 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7337}
7338
0018ea6f
DE
7339\f
7340/* Type Unit Groups.
dee91e82 7341
0018ea6f
DE
7342 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7343 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7344 so that all types coming from the same compilation (.o file) are grouped
7345 together. A future step could be to put the types in the same symtab as
7346 the CU the types ultimately came from. */
ff013f42 7347
f4dc4d17
DE
7348static hashval_t
7349hash_type_unit_group (const void *item)
7350{
9a3c8263
SM
7351 const struct type_unit_group *tu_group
7352 = (const struct type_unit_group *) item;
f4dc4d17 7353
094b34ac 7354 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7355}
348e048f
DE
7356
7357static int
f4dc4d17 7358eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7359{
9a3c8263
SM
7360 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7361 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7362
094b34ac 7363 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7364}
348e048f 7365
f4dc4d17
DE
7366/* Allocate a hash table for type unit groups. */
7367
eaa5fa8b 7368static htab_up
ed2dc618 7369allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17 7370{
eaa5fa8b
TT
7371 return htab_up (htab_create_alloc (3,
7372 hash_type_unit_group,
7373 eq_type_unit_group,
7374 NULL, xcalloc, xfree));
f4dc4d17 7375}
dee91e82 7376
f4dc4d17
DE
7377/* Type units that don't have DW_AT_stmt_list are grouped into their own
7378 partial symtabs. We combine several TUs per psymtab to not let the size
7379 of any one psymtab grow too big. */
7380#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7381#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7382
094b34ac 7383/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7384 Create the type_unit_group object used to hold one or more TUs. */
7385
7386static struct type_unit_group *
094b34ac 7387create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7388{
518817b3
SM
7389 struct dwarf2_per_objfile *dwarf2_per_objfile
7390 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7391 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7392 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7393 struct type_unit_group *tu_group;
f4dc4d17
DE
7394
7395 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7396 struct type_unit_group);
094b34ac 7397 per_cu = &tu_group->per_cu;
518817b3 7398 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7399
094b34ac
DE
7400 if (dwarf2_per_objfile->using_index)
7401 {
7402 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7403 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7404 }
7405 else
7406 {
9c541725 7407 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7408 dwarf2_psymtab *pst;
528e1572 7409 std::string name;
094b34ac
DE
7410
7411 /* Give the symtab a useful name for debug purposes. */
7412 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7413 name = string_printf ("<type_units_%d>",
7414 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7415 else
528e1572 7416 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7417
528e1572 7418 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7419 pst->anonymous = true;
094b34ac 7420 }
f4dc4d17 7421
094b34ac 7422 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7423 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7424
7425 return tu_group;
7426}
7427
094b34ac
DE
7428/* Look up the type_unit_group for type unit CU, and create it if necessary.
7429 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7430
7431static struct type_unit_group *
ff39bb5e 7432get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7433{
518817b3
SM
7434 struct dwarf2_per_objfile *dwarf2_per_objfile
7435 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7436 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7437 struct type_unit_group *tu_group;
7438 void **slot;
7439 unsigned int line_offset;
7440 struct type_unit_group type_unit_group_for_lookup;
7441
7442 if (dwarf2_per_objfile->type_unit_groups == NULL)
7443 {
7444 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7445 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7446 }
7447
7448 /* Do we need to create a new group, or can we use an existing one? */
7449
7450 if (stmt_list)
7451 {
7452 line_offset = DW_UNSND (stmt_list);
7453 ++tu_stats->nr_symtab_sharers;
7454 }
7455 else
7456 {
7457 /* Ugh, no stmt_list. Rare, but we have to handle it.
7458 We can do various things here like create one group per TU or
7459 spread them over multiple groups to split up the expansion work.
7460 To avoid worst case scenarios (too many groups or too large groups)
7461 we, umm, group them in bunches. */
7462 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7463 | (tu_stats->nr_stmt_less_type_units
7464 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7465 ++tu_stats->nr_stmt_less_type_units;
7466 }
7467
094b34ac 7468 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7469 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7470 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7471 &type_unit_group_for_lookup, INSERT);
7472 if (*slot != NULL)
7473 {
9a3c8263 7474 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7475 gdb_assert (tu_group != NULL);
7476 }
7477 else
7478 {
9c541725 7479 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7480 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7481 *slot = tu_group;
7482 ++tu_stats->nr_symtabs;
7483 }
7484
7485 return tu_group;
7486}
0018ea6f
DE
7487\f
7488/* Partial symbol tables. */
7489
7490/* Create a psymtab named NAME and assign it to PER_CU.
7491
7492 The caller must fill in the following details:
7493 dirname, textlow, texthigh. */
7494
891813be 7495static dwarf2_psymtab *
0018ea6f
DE
7496create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7497{
e3b94546 7498 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7499 dwarf2_psymtab *pst;
0018ea6f 7500
891813be 7501 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7502
6d94535f 7503 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7504
7505 /* This is the glue that links PST into GDB's symbol API. */
891813be 7506 pst->per_cu_data = per_cu;
0018ea6f
DE
7507 per_cu->v.psymtab = pst;
7508
7509 return pst;
7510}
7511
c0ab21c2 7512/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7513
7514static void
7515process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7516 const gdb_byte *info_ptr,
0018ea6f 7517 struct die_info *comp_unit_die,
c0ab21c2
TT
7518 int want_partial_unit,
7519 enum language pretend_language)
0018ea6f
DE
7520{
7521 struct dwarf2_cu *cu = reader->cu;
518817b3 7522 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7523 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7524 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7525 CORE_ADDR baseaddr;
7526 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7527 dwarf2_psymtab *pst;
3a2b436a 7528 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7529 const char *filename;
0018ea6f 7530
c0ab21c2 7531 if (comp_unit_die->tag == DW_TAG_partial_unit && !want_partial_unit)
0018ea6f
DE
7532 return;
7533
7534 gdb_assert (! per_cu->is_debug_types);
7535
c0ab21c2 7536 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7537
0018ea6f 7538 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7539 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7540 if (filename == NULL)
0018ea6f 7541 filename = "";
0018ea6f
DE
7542
7543 pst = create_partial_symtab (per_cu, filename);
7544
7545 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7546 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7547
b3b3bada 7548 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7549
7550 dwarf2_find_base_address (comp_unit_die, cu);
7551
7552 /* Possibly set the default values of LOWPC and HIGHPC from
7553 `DW_AT_ranges'. */
3a2b436a
JK
7554 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7555 &best_highpc, cu, pst);
7556 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7557 {
7558 CORE_ADDR low
7559 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7560 - baseaddr);
7561 CORE_ADDR high
7562 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7563 - baseaddr - 1);
7564 /* Store the contiguous range if it is not empty; it can be
7565 empty for CUs with no code. */
d320c2b5
TT
7566 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7567 low, high, pst);
79748972 7568 }
0018ea6f
DE
7569
7570 /* Check if comp unit has_children.
7571 If so, read the rest of the partial symbols from this comp unit.
7572 If not, there's no more debug_info for this comp unit. */
3e225074 7573 if (comp_unit_die->has_children)
0018ea6f
DE
7574 {
7575 struct partial_die_info *first_die;
7576 CORE_ADDR lowpc, highpc;
7577
7578 lowpc = ((CORE_ADDR) -1);
7579 highpc = ((CORE_ADDR) 0);
7580
7581 first_die = load_partial_dies (reader, info_ptr, 1);
7582
7583 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7584 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7585
7586 /* If we didn't find a lowpc, set it to highpc to avoid
7587 complaints from `maint check'. */
7588 if (lowpc == ((CORE_ADDR) -1))
7589 lowpc = highpc;
7590
7591 /* If the compilation unit didn't have an explicit address range,
7592 then use the information extracted from its child dies. */
e385593e 7593 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7594 {
7595 best_lowpc = lowpc;
7596 best_highpc = highpc;
7597 }
7598 }
4ae976d1 7599 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7600 best_lowpc + baseaddr)
7601 - baseaddr);
4ae976d1 7602 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7603 best_highpc + baseaddr)
7604 - baseaddr);
0018ea6f 7605
8763cede 7606 end_psymtab_common (objfile, pst);
0018ea6f 7607
ae640021 7608 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7609 {
7610 int i;
ae640021 7611 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7612
7613 /* Fill in 'dependencies' here; we fill in 'users' in a
7614 post-pass. */
7615 pst->number_of_dependencies = len;
a9342b62
TT
7616 pst->dependencies
7617 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7618 for (i = 0; i < len; ++i)
7619 {
7620 pst->dependencies[i]
7621 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7622 }
0018ea6f 7623
ae640021 7624 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7625 }
7626
7627 /* Get the list of files included in the current compilation unit,
7628 and build a psymtab for each of them. */
7629 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7630
b4f54984 7631 if (dwarf_read_debug)
b926417a
TT
7632 fprintf_unfiltered (gdb_stdlog,
7633 "Psymtab for %s unit @%s: %s - %s"
7634 ", %d global, %d static syms\n",
7635 per_cu->is_debug_types ? "type" : "comp",
7636 sect_offset_str (per_cu->sect_off),
7637 paddress (gdbarch, pst->text_low (objfile)),
7638 paddress (gdbarch, pst->text_high (objfile)),
7639 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7640}
7641
7642/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7643 Process compilation unit THIS_CU for a psymtab. */
7644
7645static void
7646process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
7647 int want_partial_unit,
7648 enum language pretend_language)
0018ea6f
DE
7649{
7650 /* If this compilation unit was already read in, free the
7651 cached copy in order to read it in again. This is
7652 necessary because we skipped some symbols when we first
7653 read in the compilation unit (see load_partial_dies).
7654 This problem could be avoided, but the benefit is unclear. */
7655 if (this_cu->cu != NULL)
7656 free_one_cached_comp_unit (this_cu);
7657
c0ab21c2
TT
7658 cutu_reader reader (this_cu, NULL, 0, 0, false);
7659
7660 if (reader.dummy_p)
f1902523 7661 {
c0ab21c2 7662 /* Nothing. */
f1902523 7663 }
c0ab21c2 7664 else if (this_cu->is_debug_types)
3e225074
TT
7665 build_type_psymtabs_reader (&reader, reader.info_ptr,
7666 reader.comp_unit_die);
c0ab21c2
TT
7667 else
7668 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7669 reader.comp_unit_die,
c0ab21c2
TT
7670 want_partial_unit,
7671 pretend_language);
0018ea6f
DE
7672
7673 /* Age out any secondary CUs. */
ed2dc618 7674 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7675}
f4dc4d17
DE
7676
7677/* Reader function for build_type_psymtabs. */
7678
7679static void
7680build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7681 const gdb_byte *info_ptr,
3e225074 7682 struct die_info *type_unit_die)
f4dc4d17 7683{
ed2dc618 7684 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7685 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7686 struct objfile *objfile = dwarf2_per_objfile->objfile;
7687 struct dwarf2_cu *cu = reader->cu;
7688 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7689 struct signatured_type *sig_type;
f4dc4d17
DE
7690 struct type_unit_group *tu_group;
7691 struct attribute *attr;
7692 struct partial_die_info *first_die;
7693 CORE_ADDR lowpc, highpc;
891813be 7694 dwarf2_psymtab *pst;
f4dc4d17 7695
0186c6a7
DE
7696 gdb_assert (per_cu->is_debug_types);
7697 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7698
3e225074 7699 if (! type_unit_die->has_children)
f4dc4d17
DE
7700 return;
7701
7702 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 7703 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7704
df07e2c7 7705 if (tu_group->tus == nullptr)
a8b3b8e9 7706 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7707 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7708
7709 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7710 pst = create_partial_symtab (per_cu, "");
6d94535f 7711 pst->anonymous = true;
f4dc4d17
DE
7712
7713 first_die = load_partial_dies (reader, info_ptr, 1);
7714
7715 lowpc = (CORE_ADDR) -1;
7716 highpc = (CORE_ADDR) 0;
7717 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7718
8763cede 7719 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7720}
7721
73051182
DE
7722/* Struct used to sort TUs by their abbreviation table offset. */
7723
7724struct tu_abbrev_offset
7725{
b2bdb8cf
SM
7726 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7727 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7728 {}
7729
7730 signatured_type *sig_type;
73051182
DE
7731 sect_offset abbrev_offset;
7732};
7733
484cf504 7734/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7735
484cf504
TT
7736static bool
7737sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7738 const struct tu_abbrev_offset &b)
73051182 7739{
484cf504 7740 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7741}
7742
7743/* Efficiently read all the type units.
7744 This does the bulk of the work for build_type_psymtabs.
7745
7746 The efficiency is because we sort TUs by the abbrev table they use and
7747 only read each abbrev table once. In one program there are 200K TUs
7748 sharing 8K abbrev tables.
7749
7750 The main purpose of this function is to support building the
7751 dwarf2_per_objfile->type_unit_groups table.
7752 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7753 can collapse the search space by grouping them by stmt_list.
7754 The savings can be significant, in the same program from above the 200K TUs
7755 share 8K stmt_list tables.
7756
7757 FUNC is expected to call get_type_unit_group, which will create the
7758 struct type_unit_group if necessary and add it to
7759 dwarf2_per_objfile->type_unit_groups. */
7760
7761static void
ed2dc618 7762build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7763{
73051182 7764 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7765 abbrev_table_up abbrev_table;
73051182 7766 sect_offset abbrev_offset;
73051182
DE
7767
7768 /* It's up to the caller to not call us multiple times. */
7769 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7770
b2bdb8cf 7771 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7772 return;
7773
7774 /* TUs typically share abbrev tables, and there can be way more TUs than
7775 abbrev tables. Sort by abbrev table to reduce the number of times we
7776 read each abbrev table in.
7777 Alternatives are to punt or to maintain a cache of abbrev tables.
7778 This is simpler and efficient enough for now.
7779
7780 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7781 symtab to use). Typically TUs with the same abbrev offset have the same
7782 stmt_list value too so in practice this should work well.
7783
7784 The basic algorithm here is:
7785
7786 sort TUs by abbrev table
7787 for each TU with same abbrev table:
7788 read abbrev table if first user
7789 read TU top level DIE
7790 [IWBN if DWO skeletons had DW_AT_stmt_list]
7791 call FUNC */
7792
b4f54984 7793 if (dwarf_read_debug)
73051182
DE
7794 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7795
7796 /* Sort in a separate table to maintain the order of all_type_units
7797 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7798 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7799 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7800
7801 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7802 sorted_by_abbrev.emplace_back
7803 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7804 sig_type->per_cu.section,
7805 sig_type->per_cu.sect_off));
73051182 7806
484cf504
TT
7807 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7808 sort_tu_by_abbrev_offset);
73051182 7809
9c541725 7810 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7811
b2bdb8cf 7812 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7813 {
73051182
DE
7814 /* Switch to the next abbrev table if necessary. */
7815 if (abbrev_table == NULL
b2bdb8cf 7816 || tu.abbrev_offset != abbrev_offset)
73051182 7817 {
b2bdb8cf 7818 abbrev_offset = tu.abbrev_offset;
73051182 7819 abbrev_table =
86de1d91
TT
7820 abbrev_table::read (dwarf2_per_objfile->objfile,
7821 &dwarf2_per_objfile->abbrev,
7822 abbrev_offset);
73051182
DE
7823 ++tu_stats->nr_uniq_abbrev_tables;
7824 }
7825
c0ab21c2
TT
7826 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7827 0, 0, false);
7828 if (!reader.dummy_p)
7829 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7830 reader.comp_unit_die);
73051182 7831 }
6aa5f3a6 7832}
73051182 7833
6aa5f3a6
DE
7834/* Print collected type unit statistics. */
7835
7836static void
ed2dc618 7837print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7838{
7839 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7840
7841 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7842 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7843 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7844 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7845 tu_stats->nr_uniq_abbrev_tables);
7846 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7847 tu_stats->nr_symtabs);
7848 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7849 tu_stats->nr_symtab_sharers);
7850 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7851 tu_stats->nr_stmt_less_type_units);
7852 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7853 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7854}
7855
f4dc4d17
DE
7856/* Traversal function for build_type_psymtabs. */
7857
7858static int
7859build_type_psymtab_dependencies (void **slot, void *info)
7860{
ed2dc618
SM
7861 struct dwarf2_per_objfile *dwarf2_per_objfile
7862 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7863 struct objfile *objfile = dwarf2_per_objfile->objfile;
7864 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7865 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7866 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7867 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7868 int i;
7869
7870 gdb_assert (len > 0);
0186c6a7 7871 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
7872
7873 pst->number_of_dependencies = len;
a9342b62 7874 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7875 for (i = 0; i < len; ++i)
f4dc4d17 7876 {
df07e2c7 7877 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7878 gdb_assert (iter->per_cu.is_debug_types);
7879 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7880 iter->type_unit_group = tu_group;
f4dc4d17
DE
7881 }
7882
df07e2c7
AB
7883 delete tu_group->tus;
7884 tu_group->tus = nullptr;
348e048f
DE
7885
7886 return 1;
7887}
7888
7889/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7890 Build partial symbol tables for the .debug_types comp-units. */
7891
7892static void
ed2dc618 7893build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7894{
ed2dc618 7895 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7896 return;
7897
ed2dc618 7898 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7899}
f4dc4d17 7900
6aa5f3a6
DE
7901/* Traversal function for process_skeletonless_type_unit.
7902 Read a TU in a DWO file and build partial symbols for it. */
7903
7904static int
7905process_skeletonless_type_unit (void **slot, void *info)
7906{
7907 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7908 struct dwarf2_per_objfile *dwarf2_per_objfile
7909 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7910 struct signatured_type find_entry, *entry;
7911
7912 /* If this TU doesn't exist in the global table, add it and read it in. */
7913
7914 if (dwarf2_per_objfile->signatured_types == NULL)
7915 {
7916 dwarf2_per_objfile->signatured_types
ed2dc618 7917 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
7918 }
7919
7920 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7921 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7922 &find_entry, INSERT);
6aa5f3a6
DE
7923 /* If we've already seen this type there's nothing to do. What's happening
7924 is we're doing our own version of comdat-folding here. */
7925 if (*slot != NULL)
7926 return 1;
7927
7928 /* This does the job that create_all_type_units would have done for
7929 this TU. */
ed2dc618
SM
7930 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7931 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7932 *slot = entry;
7933
7934 /* This does the job that build_type_psymtabs_1 would have done. */
c0ab21c2
TT
7935 cutu_reader reader (&entry->per_cu, NULL, 0, 0, false);
7936 if (!reader.dummy_p)
7937 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7938 reader.comp_unit_die);
6aa5f3a6
DE
7939
7940 return 1;
7941}
7942
7943/* Traversal function for process_skeletonless_type_units. */
7944
7945static int
7946process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7947{
7948 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7949
7950 if (dwo_file->tus != NULL)
b0b6a987
TT
7951 htab_traverse_noresize (dwo_file->tus.get (),
7952 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7953
7954 return 1;
7955}
7956
7957/* Scan all TUs of DWO files, verifying we've processed them.
7958 This is needed in case a TU was emitted without its skeleton.
7959 Note: This can't be done until we know what all the DWO files are. */
7960
7961static void
ed2dc618 7962process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7963{
7964 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7965 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7966 && dwarf2_per_objfile->dwo_files != NULL)
7967 {
51ac9db5 7968 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7969 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7970 dwarf2_per_objfile);
6aa5f3a6 7971 }
348e048f
DE
7972}
7973
ed2dc618 7974/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7975
7976static void
ed2dc618 7977set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7978{
b76e467d 7979 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7980 {
891813be 7981 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7982
36586728
TT
7983 if (pst == NULL)
7984 continue;
7985
b76e467d 7986 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7987 {
7988 /* Set the 'user' field only if it is not already set. */
7989 if (pst->dependencies[j]->user == NULL)
7990 pst->dependencies[j]->user = pst;
7991 }
7992 }
7993}
7994
93311388
DE
7995/* Build the partial symbol table by doing a quick pass through the
7996 .debug_info and .debug_abbrev sections. */
72bf9492 7997
93311388 7998static void
ed2dc618 7999dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8000{
ed2dc618 8001 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8002
b4f54984 8003 if (dwarf_read_debug)
45cfd468
DE
8004 {
8005 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8006 objfile_name (objfile));
45cfd468
DE
8007 }
8008
98bfdba5
PA
8009 dwarf2_per_objfile->reading_partial_symbols = 1;
8010
96b79293 8011 dwarf2_per_objfile->info.read (objfile);
91c24f0a 8012
93311388
DE
8013 /* Any cached compilation units will be linked by the per-objfile
8014 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8015 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8016
ed2dc618 8017 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8018
ed2dc618 8019 create_all_comp_units (dwarf2_per_objfile);
c906108c 8020
60606b2c
TT
8021 /* Create a temporary address map on a temporary obstack. We later
8022 copy this to the final obstack. */
8268c778 8023 auto_obstack temp_obstack;
791afaa2
TT
8024
8025 scoped_restore save_psymtabs_addrmap
d320c2b5 8026 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8027 addrmap_create_mutable (&temp_obstack));
72bf9492 8028
b76e467d
SM
8029 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8030 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8031
6aa5f3a6 8032 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8033 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8034
8035 /* Now that all TUs have been processed we can fill in the dependencies. */
8036 if (dwarf2_per_objfile->type_unit_groups != NULL)
8037 {
eaa5fa8b 8038 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 8039 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8040 }
8041
b4f54984 8042 if (dwarf_read_debug)
ed2dc618 8043 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8044
ed2dc618 8045 set_partial_user (dwarf2_per_objfile);
95554aad 8046
d320c2b5
TT
8047 objfile->partial_symtabs->psymtabs_addrmap
8048 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8049 objfile->partial_symtabs->obstack ());
791afaa2
TT
8050 /* At this point we want to keep the address map. */
8051 save_psymtabs_addrmap.release ();
ff013f42 8052
b4f54984 8053 if (dwarf_read_debug)
45cfd468 8054 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8055 objfile_name (objfile));
ae038cb0
DJ
8056}
8057
dee91e82
DE
8058/* Load the partial DIEs for a secondary CU into memory.
8059 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8060
dee91e82
DE
8061static void
8062load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8063{
c0ab21c2
TT
8064 cutu_reader reader (this_cu, NULL, 1, 1, false);
8065
8066 if (!reader.dummy_p)
8067 {
8068 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8069 language_minimal);
8070
8071 /* Check if comp unit has_children.
8072 If so, read the rest of the partial symbols from this comp unit.
8073 If not, there's no more debug_info for this comp unit. */
3e225074 8074 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8075 load_partial_dies (&reader, reader.info_ptr, 0);
8076 }
ae038cb0
DJ
8077}
8078
ae038cb0 8079static void
ed2dc618 8080read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8081 struct dwarf2_section_info *section,
f1902523 8082 struct dwarf2_section_info *abbrev_section,
b76e467d 8083 unsigned int is_dwz)
ae038cb0 8084{
d521ce57 8085 const gdb_byte *info_ptr;
ed2dc618 8086 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8087
b4f54984 8088 if (dwarf_read_debug)
bf6af496 8089 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8090 section->get_name (),
8091 section->get_file_name ());
bf6af496 8092
96b79293 8093 section->read (objfile);
ae038cb0 8094
36586728 8095 info_ptr = section->buffer;
6e70227d 8096
36586728 8097 while (info_ptr < section->buffer + section->size)
ae038cb0 8098 {
ae038cb0 8099 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8100
9c541725 8101 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8102
f1902523 8103 comp_unit_head cu_header;
ed2dc618
SM
8104 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8105 abbrev_section, info_ptr,
8106 rcuh_kind::COMPILE);
ae038cb0
DJ
8107
8108 /* Save the compilation unit for later lookup. */
f1902523
JK
8109 if (cu_header.unit_type != DW_UT_type)
8110 {
8111 this_cu = XOBNEW (&objfile->objfile_obstack,
8112 struct dwarf2_per_cu_data);
8113 memset (this_cu, 0, sizeof (*this_cu));
8114 }
8115 else
8116 {
8117 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8118 struct signatured_type);
8119 memset (sig_type, 0, sizeof (*sig_type));
8120 sig_type->signature = cu_header.signature;
8121 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8122 this_cu = &sig_type->per_cu;
8123 }
8124 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8125 this_cu->sect_off = sect_off;
f1902523 8126 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8127 this_cu->is_dwz = is_dwz;
e3b94546 8128 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8129 this_cu->section = section;
ae038cb0 8130
b76e467d 8131 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8132
8133 info_ptr = info_ptr + this_cu->length;
8134 }
36586728
TT
8135}
8136
8137/* Create a list of all compilation units in OBJFILE.
8138 This is only done for -readnow and building partial symtabs. */
8139
8140static void
ed2dc618 8141create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8142{
b76e467d 8143 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8144 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8145 &dwarf2_per_objfile->abbrev, 0);
36586728 8146
b76e467d 8147 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8148 if (dwz != NULL)
ed2dc618 8149 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8150 1);
c906108c
SS
8151}
8152
5734ee8b 8153/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8154 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8155 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8156 DW_AT_ranges). See the comments of add_partial_subprogram on how
8157 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8158
72bf9492
DJ
8159static void
8160scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8161 CORE_ADDR *highpc, int set_addrmap,
8162 struct dwarf2_cu *cu)
c906108c 8163{
72bf9492 8164 struct partial_die_info *pdi;
c906108c 8165
91c24f0a
DC
8166 /* Now, march along the PDI's, descending into ones which have
8167 interesting children but skipping the children of the other ones,
8168 until we reach the end of the compilation unit. */
c906108c 8169
72bf9492 8170 pdi = first_die;
91c24f0a 8171
72bf9492
DJ
8172 while (pdi != NULL)
8173 {
52356b79 8174 pdi->fixup (cu);
c906108c 8175
f55ee35c 8176 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8177 children, so we need to look at them. Ditto for anonymous
8178 enums. */
933c6fe4 8179
72bf9492 8180 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8181 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8182 || pdi->tag == DW_TAG_imported_unit
8183 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8184 {
72bf9492 8185 switch (pdi->tag)
c906108c
SS
8186 {
8187 case DW_TAG_subprogram:
b1dc1806 8188 case DW_TAG_inlined_subroutine:
cdc07690 8189 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8190 break;
72929c62 8191 case DW_TAG_constant:
c906108c
SS
8192 case DW_TAG_variable:
8193 case DW_TAG_typedef:
91c24f0a 8194 case DW_TAG_union_type:
72bf9492 8195 if (!pdi->is_declaration)
63d06c5c 8196 {
72bf9492 8197 add_partial_symbol (pdi, cu);
63d06c5c
DC
8198 }
8199 break;
c906108c 8200 case DW_TAG_class_type:
680b30c7 8201 case DW_TAG_interface_type:
c906108c 8202 case DW_TAG_structure_type:
72bf9492 8203 if (!pdi->is_declaration)
c906108c 8204 {
72bf9492 8205 add_partial_symbol (pdi, cu);
c906108c 8206 }
b7fee5a3
KS
8207 if ((cu->language == language_rust
8208 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8209 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8210 set_addrmap, cu);
c906108c 8211 break;
91c24f0a 8212 case DW_TAG_enumeration_type:
72bf9492
DJ
8213 if (!pdi->is_declaration)
8214 add_partial_enumeration (pdi, cu);
c906108c
SS
8215 break;
8216 case DW_TAG_base_type:
a02abb62 8217 case DW_TAG_subrange_type:
c906108c 8218 /* File scope base type definitions are added to the partial
c5aa993b 8219 symbol table. */
72bf9492 8220 add_partial_symbol (pdi, cu);
c906108c 8221 break;
d9fa45fe 8222 case DW_TAG_namespace:
cdc07690 8223 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8224 break;
5d7cb8df 8225 case DW_TAG_module:
59c35742
AB
8226 if (!pdi->is_declaration)
8227 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8228 break;
95554aad
TT
8229 case DW_TAG_imported_unit:
8230 {
8231 struct dwarf2_per_cu_data *per_cu;
8232
f4dc4d17
DE
8233 /* For now we don't handle imported units in type units. */
8234 if (cu->per_cu->is_debug_types)
8235 {
8236 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8237 " supported in type units [in module %s]"),
518817b3 8238 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8239 }
8240
e3b94546
SM
8241 per_cu = dwarf2_find_containing_comp_unit
8242 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8243 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8244
8245 /* Go read the partial unit, if needed. */
8246 if (per_cu->v.psymtab == NULL)
b93601f3 8247 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8248
ae640021 8249 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8250 }
8251 break;
74921315
KS
8252 case DW_TAG_imported_declaration:
8253 add_partial_symbol (pdi, cu);
8254 break;
c906108c
SS
8255 default:
8256 break;
8257 }
8258 }
8259
72bf9492
DJ
8260 /* If the die has a sibling, skip to the sibling. */
8261
8262 pdi = pdi->die_sibling;
8263 }
8264}
8265
8266/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8267
72bf9492 8268 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8269 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8270 Enumerators are an exception; they use the scope of their parent
8271 enumeration type, i.e. the name of the enumeration type is not
8272 prepended to the enumerator.
91c24f0a 8273
72bf9492
DJ
8274 There are two complexities. One is DW_AT_specification; in this
8275 case "parent" means the parent of the target of the specification,
8276 instead of the direct parent of the DIE. The other is compilers
8277 which do not emit DW_TAG_namespace; in this case we try to guess
8278 the fully qualified name of structure types from their members'
8279 linkage names. This must be done using the DIE's children rather
8280 than the children of any DW_AT_specification target. We only need
8281 to do this for structures at the top level, i.e. if the target of
8282 any DW_AT_specification (if any; otherwise the DIE itself) does not
8283 have a parent. */
8284
8285/* Compute the scope prefix associated with PDI's parent, in
8286 compilation unit CU. The result will be allocated on CU's
8287 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8288 field. NULL is returned if no prefix is necessary. */
15d034d0 8289static const char *
72bf9492
DJ
8290partial_die_parent_scope (struct partial_die_info *pdi,
8291 struct dwarf2_cu *cu)
8292{
15d034d0 8293 const char *grandparent_scope;
72bf9492 8294 struct partial_die_info *parent, *real_pdi;
91c24f0a 8295
72bf9492
DJ
8296 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8297 then this means the parent of the specification DIE. */
8298
8299 real_pdi = pdi;
72bf9492 8300 while (real_pdi->has_specification)
fb816e8b 8301 {
122cf0f2
AB
8302 auto res = find_partial_die (real_pdi->spec_offset,
8303 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8304 real_pdi = res.pdi;
8305 cu = res.cu;
8306 }
72bf9492
DJ
8307
8308 parent = real_pdi->die_parent;
8309 if (parent == NULL)
8310 return NULL;
8311
8312 if (parent->scope_set)
8313 return parent->scope;
8314
52356b79 8315 parent->fixup (cu);
72bf9492 8316
10b3939b 8317 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8318
acebe513
UW
8319 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8320 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8321 Work around this problem here. */
8322 if (cu->language == language_cplus
6e70227d 8323 && parent->tag == DW_TAG_namespace
acebe513
UW
8324 && strcmp (parent->name, "::") == 0
8325 && grandparent_scope == NULL)
8326 {
8327 parent->scope = NULL;
8328 parent->scope_set = 1;
8329 return NULL;
8330 }
8331
0a4b0913 8332 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8333 if (pdi->tag == DW_TAG_enumerator)
8334 /* Enumerators should not get the name of the enumeration as a prefix. */
8335 parent->scope = grandparent_scope;
8336 else if (parent->tag == DW_TAG_namespace
f55ee35c 8337 || parent->tag == DW_TAG_module
72bf9492
DJ
8338 || parent->tag == DW_TAG_structure_type
8339 || parent->tag == DW_TAG_class_type
680b30c7 8340 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8341 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8342 || parent->tag == DW_TAG_enumeration_type
8343 || (cu->language == language_fortran
8344 && parent->tag == DW_TAG_subprogram
8345 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8346 {
8347 if (grandparent_scope == NULL)
8348 parent->scope = parent->name;
8349 else
3e43a32a
MS
8350 parent->scope = typename_concat (&cu->comp_unit_obstack,
8351 grandparent_scope,
f55ee35c 8352 parent->name, 0, cu);
72bf9492 8353 }
72bf9492
DJ
8354 else
8355 {
8356 /* FIXME drow/2004-04-01: What should we be doing with
8357 function-local names? For partial symbols, we should probably be
8358 ignoring them. */
fa9c3fa0
TT
8359 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8360 dwarf_tag_name (parent->tag),
8361 sect_offset_str (pdi->sect_off));
72bf9492 8362 parent->scope = grandparent_scope;
c906108c
SS
8363 }
8364
72bf9492
DJ
8365 parent->scope_set = 1;
8366 return parent->scope;
8367}
8368
8369/* Return the fully scoped name associated with PDI, from compilation unit
8370 CU. The result will be allocated with malloc. */
4568ecf9 8371
43816ebc 8372static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8373partial_die_full_name (struct partial_die_info *pdi,
8374 struct dwarf2_cu *cu)
8375{
15d034d0 8376 const char *parent_scope;
72bf9492 8377
98bfdba5
PA
8378 /* If this is a template instantiation, we can not work out the
8379 template arguments from partial DIEs. So, unfortunately, we have
8380 to go through the full DIEs. At least any work we do building
8381 types here will be reused if full symbols are loaded later. */
8382 if (pdi->has_template_arguments)
8383 {
52356b79 8384 pdi->fixup (cu);
98bfdba5
PA
8385
8386 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8387 {
8388 struct die_info *die;
8389 struct attribute attr;
8390 struct dwarf2_cu *ref_cu = cu;
8391
b64f50a1 8392 /* DW_FORM_ref_addr is using section offset. */
b4069958 8393 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8394 attr.form = DW_FORM_ref_addr;
9c541725 8395 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8396 die = follow_die_ref (NULL, &attr, &ref_cu);
8397
43816ebc 8398 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8399 }
8400 }
8401
72bf9492
DJ
8402 parent_scope = partial_die_parent_scope (pdi, cu);
8403 if (parent_scope == NULL)
8404 return NULL;
8405 else
43816ebc
TT
8406 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8407 pdi->name, 0, cu));
c906108c
SS
8408}
8409
8410static void
72bf9492 8411add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8412{
518817b3
SM
8413 struct dwarf2_per_objfile *dwarf2_per_objfile
8414 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8415 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8416 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8417 CORE_ADDR addr = 0;
15d034d0 8418 const char *actual_name = NULL;
e142c38c
DJ
8419 CORE_ADDR baseaddr;
8420
b3b3bada 8421 baseaddr = objfile->text_section_offset ();
c906108c 8422
43816ebc
TT
8423 gdb::unique_xmalloc_ptr<char> built_actual_name
8424 = partial_die_full_name (pdi, cu);
15d034d0 8425 if (built_actual_name != NULL)
43816ebc 8426 actual_name = built_actual_name.get ();
63d06c5c 8427
72bf9492
DJ
8428 if (actual_name == NULL)
8429 actual_name = pdi->name;
8430
c906108c
SS
8431 switch (pdi->tag)
8432 {
b1dc1806 8433 case DW_TAG_inlined_subroutine:
c906108c 8434 case DW_TAG_subprogram:
79748972
TT
8435 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8436 - baseaddr);
0a4b0913
AB
8437 if (pdi->is_external
8438 || cu->language == language_ada
8439 || (cu->language == language_fortran
8440 && pdi->die_parent != NULL
8441 && pdi->die_parent->tag == DW_TAG_subprogram))
8442 {
8443 /* Normally, only "external" DIEs are part of the global scope.
8444 But in Ada and Fortran, we want to be able to access nested
8445 procedures globally. So all Ada and Fortran subprograms are
8446 stored in the global scope. */
31edb802 8447 add_psymbol_to_list (actual_name,
15d034d0 8448 built_actual_name != NULL,
f47fb265 8449 VAR_DOMAIN, LOC_BLOCK,
79748972 8450 SECT_OFF_TEXT (objfile),
75aedd27 8451 psymbol_placement::GLOBAL,
79748972
TT
8452 addr,
8453 cu->language, objfile);
c906108c
SS
8454 }
8455 else
8456 {
31edb802 8457 add_psymbol_to_list (actual_name,
15d034d0 8458 built_actual_name != NULL,
f47fb265 8459 VAR_DOMAIN, LOC_BLOCK,
79748972 8460 SECT_OFF_TEXT (objfile),
75aedd27 8461 psymbol_placement::STATIC,
1762568f 8462 addr, cu->language, objfile);
c906108c 8463 }
0c1b455e
TT
8464
8465 if (pdi->main_subprogram && actual_name != NULL)
8466 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8467 break;
72929c62 8468 case DW_TAG_constant:
31edb802 8469 add_psymbol_to_list (actual_name,
75aedd27
TT
8470 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8471 -1, (pdi->is_external
8472 ? psymbol_placement::GLOBAL
8473 : psymbol_placement::STATIC),
8474 0, cu->language, objfile);
72929c62 8475 break;
c906108c 8476 case DW_TAG_variable:
95554aad
TT
8477 if (pdi->d.locdesc)
8478 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8479
95554aad 8480 if (pdi->d.locdesc
caac4577
JG
8481 && addr == 0
8482 && !dwarf2_per_objfile->has_section_at_zero)
8483 {
8484 /* A global or static variable may also have been stripped
8485 out by the linker if unused, in which case its address
8486 will be nullified; do not add such variables into partial
8487 symbol table then. */
8488 }
8489 else if (pdi->is_external)
c906108c
SS
8490 {
8491 /* Global Variable.
8492 Don't enter into the minimal symbol tables as there is
8493 a minimal symbol table entry from the ELF symbols already.
8494 Enter into partial symbol table if it has a location
8495 descriptor or a type.
8496 If the location descriptor is missing, new_symbol will create
8497 a LOC_UNRESOLVED symbol, the address of the variable will then
8498 be determined from the minimal symbol table whenever the variable
8499 is referenced.
8500 The address for the partial symbol table entry is not
8501 used by GDB, but it comes in handy for debugging partial symbol
8502 table building. */
8503
95554aad 8504 if (pdi->d.locdesc || pdi->has_type)
31edb802 8505 add_psymbol_to_list (actual_name,
15d034d0 8506 built_actual_name != NULL,
f47fb265 8507 VAR_DOMAIN, LOC_STATIC,
79748972 8508 SECT_OFF_TEXT (objfile),
75aedd27 8509 psymbol_placement::GLOBAL,
79748972 8510 addr, cu->language, objfile);
c906108c
SS
8511 }
8512 else
8513 {
ff908ebf
AW
8514 int has_loc = pdi->d.locdesc != NULL;
8515
8516 /* Static Variable. Skip symbols whose value we cannot know (those
8517 without location descriptors or constant values). */
8518 if (!has_loc && !pdi->has_const_value)
43816ebc 8519 return;
ff908ebf 8520
31edb802 8521 add_psymbol_to_list (actual_name,
15d034d0 8522 built_actual_name != NULL,
f47fb265 8523 VAR_DOMAIN, LOC_STATIC,
79748972 8524 SECT_OFF_TEXT (objfile),
75aedd27 8525 psymbol_placement::STATIC,
79748972 8526 has_loc ? addr : 0,
f47fb265 8527 cu->language, objfile);
c906108c
SS
8528 }
8529 break;
8530 case DW_TAG_typedef:
8531 case DW_TAG_base_type:
a02abb62 8532 case DW_TAG_subrange_type:
31edb802 8533 add_psymbol_to_list (actual_name,
15d034d0 8534 built_actual_name != NULL,
79748972 8535 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8536 psymbol_placement::STATIC,
1762568f 8537 0, cu->language, objfile);
c906108c 8538 break;
74921315 8539 case DW_TAG_imported_declaration:
72bf9492 8540 case DW_TAG_namespace:
31edb802 8541 add_psymbol_to_list (actual_name,
15d034d0 8542 built_actual_name != NULL,
79748972 8543 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8544 psymbol_placement::GLOBAL,
1762568f 8545 0, cu->language, objfile);
72bf9492 8546 break;
530e8392 8547 case DW_TAG_module:
a5fd13a9
BH
8548 /* With Fortran 77 there might be a "BLOCK DATA" module
8549 available without any name. If so, we skip the module as it
8550 doesn't bring any value. */
8551 if (actual_name != nullptr)
31edb802 8552 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8553 built_actual_name != NULL,
8554 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8555 psymbol_placement::GLOBAL,
8556 0, cu->language, objfile);
530e8392 8557 break;
c906108c 8558 case DW_TAG_class_type:
680b30c7 8559 case DW_TAG_interface_type:
c906108c
SS
8560 case DW_TAG_structure_type:
8561 case DW_TAG_union_type:
8562 case DW_TAG_enumeration_type:
fa4028e9
JB
8563 /* Skip external references. The DWARF standard says in the section
8564 about "Structure, Union, and Class Type Entries": "An incomplete
8565 structure, union or class type is represented by a structure,
8566 union or class entry that does not have a byte size attribute
8567 and that has a DW_AT_declaration attribute." */
8568 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8569 return;
fa4028e9 8570
63d06c5c
DC
8571 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8572 static vs. global. */
31edb802 8573 add_psymbol_to_list (actual_name,
15d034d0 8574 built_actual_name != NULL,
79748972 8575 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8576 cu->language == language_cplus
75aedd27
TT
8577 ? psymbol_placement::GLOBAL
8578 : psymbol_placement::STATIC,
1762568f 8579 0, cu->language, objfile);
c906108c 8580
c906108c
SS
8581 break;
8582 case DW_TAG_enumerator:
31edb802 8583 add_psymbol_to_list (actual_name,
15d034d0 8584 built_actual_name != NULL,
79748972 8585 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8586 cu->language == language_cplus
75aedd27
TT
8587 ? psymbol_placement::GLOBAL
8588 : psymbol_placement::STATIC,
1762568f 8589 0, cu->language, objfile);
c906108c
SS
8590 break;
8591 default:
8592 break;
8593 }
8594}
8595
5c4e30ca
DC
8596/* Read a partial die corresponding to a namespace; also, add a symbol
8597 corresponding to that namespace to the symbol table. NAMESPACE is
8598 the name of the enclosing namespace. */
91c24f0a 8599
72bf9492
DJ
8600static void
8601add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8602 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8603 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8604{
72bf9492 8605 /* Add a symbol for the namespace. */
e7c27a73 8606
72bf9492 8607 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8608
8609 /* Now scan partial symbols in that namespace. */
8610
91c24f0a 8611 if (pdi->has_children)
cdc07690 8612 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8613}
8614
5d7cb8df
JK
8615/* Read a partial die corresponding to a Fortran module. */
8616
8617static void
8618add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8619 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8620{
530e8392
KB
8621 /* Add a symbol for the namespace. */
8622
8623 add_partial_symbol (pdi, cu);
8624
f55ee35c 8625 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8626
8627 if (pdi->has_children)
cdc07690 8628 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8629}
8630
b1dc1806
XR
8631/* Read a partial die corresponding to a subprogram or an inlined
8632 subprogram and create a partial symbol for that subprogram.
8633 When the CU language allows it, this routine also defines a partial
8634 symbol for each nested subprogram that this subprogram contains.
8635 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8636 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8637
cdc07690
YQ
8638 PDI may also be a lexical block, in which case we simply search
8639 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8640 Again, this is only performed when the CU language allows this
8641 type of definitions. */
8642
8643static void
8644add_partial_subprogram (struct partial_die_info *pdi,
8645 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8646 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8647{
b1dc1806 8648 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8649 {
8650 if (pdi->has_pc_info)
8651 {
8652 if (pdi->lowpc < *lowpc)
8653 *lowpc = pdi->lowpc;
8654 if (pdi->highpc > *highpc)
8655 *highpc = pdi->highpc;
cdc07690 8656 if (set_addrmap)
5734ee8b 8657 {
518817b3 8658 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8659 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8660 CORE_ADDR baseaddr;
b926417a
TT
8661 CORE_ADDR this_highpc;
8662 CORE_ADDR this_lowpc;
5734ee8b 8663
b3b3bada 8664 baseaddr = objfile->text_section_offset ();
b926417a
TT
8665 this_lowpc
8666 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8667 pdi->lowpc + baseaddr)
8668 - baseaddr);
8669 this_highpc
8670 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8671 pdi->highpc + baseaddr)
8672 - baseaddr);
d320c2b5 8673 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8674 this_lowpc, this_highpc - 1,
9291a0cd 8675 cu->per_cu->v.psymtab);
5734ee8b 8676 }
481860b3
GB
8677 }
8678
8679 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8680 {
bc30ff58 8681 if (!pdi->is_declaration)
e8d05480
JB
8682 /* Ignore subprogram DIEs that do not have a name, they are
8683 illegal. Do not emit a complaint at this point, we will
8684 do so when we convert this psymtab into a symtab. */
8685 if (pdi->name)
8686 add_partial_symbol (pdi, cu);
bc30ff58
JB
8687 }
8688 }
6e70227d 8689
bc30ff58
JB
8690 if (! pdi->has_children)
8691 return;
8692
0a4b0913 8693 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8694 {
8695 pdi = pdi->die_child;
8696 while (pdi != NULL)
8697 {
52356b79 8698 pdi->fixup (cu);
bc30ff58 8699 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8700 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8701 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8702 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8703 pdi = pdi->die_sibling;
8704 }
8705 }
8706}
8707
91c24f0a
DC
8708/* Read a partial die corresponding to an enumeration type. */
8709
72bf9492
DJ
8710static void
8711add_partial_enumeration (struct partial_die_info *enum_pdi,
8712 struct dwarf2_cu *cu)
91c24f0a 8713{
72bf9492 8714 struct partial_die_info *pdi;
91c24f0a
DC
8715
8716 if (enum_pdi->name != NULL)
72bf9492
DJ
8717 add_partial_symbol (enum_pdi, cu);
8718
8719 pdi = enum_pdi->die_child;
8720 while (pdi)
91c24f0a 8721 {
72bf9492 8722 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8723 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8724 else
72bf9492
DJ
8725 add_partial_symbol (pdi, cu);
8726 pdi = pdi->die_sibling;
91c24f0a 8727 }
91c24f0a
DC
8728}
8729
6caca83c
CC
8730/* Return the initial uleb128 in the die at INFO_PTR. */
8731
8732static unsigned int
d521ce57 8733peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8734{
8735 unsigned int bytes_read;
8736
8737 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8738}
8739
685af9cd
TT
8740/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8741 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8742
4bb7a0a7
DJ
8743 Return the corresponding abbrev, or NULL if the number is zero (indicating
8744 an empty DIE). In either case *BYTES_READ will be set to the length of
8745 the initial number. */
8746
8747static struct abbrev_info *
685af9cd
TT
8748peek_die_abbrev (const die_reader_specs &reader,
8749 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8750{
685af9cd 8751 dwarf2_cu *cu = reader.cu;
518817b3 8752 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8753 unsigned int abbrev_number
8754 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8755
8756 if (abbrev_number == 0)
8757 return NULL;
8758
685af9cd 8759 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8760 if (!abbrev)
8761 {
422b9917 8762 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8763 " at offset %s [in module %s]"),
422b9917 8764 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8765 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8766 }
8767
8768 return abbrev;
8769}
8770
93311388
DE
8771/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8772 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8773 DIE. Any children of the skipped DIEs will also be skipped. */
8774
d521ce57
TT
8775static const gdb_byte *
8776skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8777{
4bb7a0a7
DJ
8778 while (1)
8779 {
685af9cd
TT
8780 unsigned int bytes_read;
8781 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8782
4bb7a0a7
DJ
8783 if (abbrev == NULL)
8784 return info_ptr + bytes_read;
8785 else
dee91e82 8786 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8787 }
8788}
8789
93311388
DE
8790/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8791 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8792 abbrev corresponding to that skipped uleb128 should be passed in
8793 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8794 children. */
8795
d521ce57
TT
8796static const gdb_byte *
8797skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8798 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8799{
8800 unsigned int bytes_read;
8801 struct attribute attr;
dee91e82
DE
8802 bfd *abfd = reader->abfd;
8803 struct dwarf2_cu *cu = reader->cu;
d521ce57 8804 const gdb_byte *buffer = reader->buffer;
f664829e 8805 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8806 unsigned int form, i;
8807
8808 for (i = 0; i < abbrev->num_attrs; i++)
8809 {
8810 /* The only abbrev we care about is DW_AT_sibling. */
8811 if (abbrev->attrs[i].name == DW_AT_sibling)
8812 {
18a8505e
AT
8813 bool ignored;
8814 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8815 &ignored);
4bb7a0a7 8816 if (attr.form == DW_FORM_ref_addr)
b98664d3 8817 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8818 else
b9502d3f 8819 {
9c541725
PA
8820 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8821 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8822
8823 if (sibling_ptr < info_ptr)
b98664d3 8824 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
8825 else if (sibling_ptr > reader->buffer_end)
8826 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
8827 else
8828 return sibling_ptr;
8829 }
4bb7a0a7
DJ
8830 }
8831
8832 /* If it isn't DW_AT_sibling, skip this attribute. */
8833 form = abbrev->attrs[i].form;
8834 skip_attribute:
8835 switch (form)
8836 {
4bb7a0a7 8837 case DW_FORM_ref_addr:
ae411497
TT
8838 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8839 and later it is offset sized. */
8840 if (cu->header.version == 2)
8841 info_ptr += cu->header.addr_size;
8842 else
8843 info_ptr += cu->header.offset_size;
8844 break;
36586728
TT
8845 case DW_FORM_GNU_ref_alt:
8846 info_ptr += cu->header.offset_size;
8847 break;
ae411497 8848 case DW_FORM_addr:
4bb7a0a7
DJ
8849 info_ptr += cu->header.addr_size;
8850 break;
8851 case DW_FORM_data1:
8852 case DW_FORM_ref1:
8853 case DW_FORM_flag:
8fe0f950 8854 case DW_FORM_strx1:
4bb7a0a7
DJ
8855 info_ptr += 1;
8856 break;
2dc7f7b3 8857 case DW_FORM_flag_present:
43988095 8858 case DW_FORM_implicit_const:
2dc7f7b3 8859 break;
4bb7a0a7
DJ
8860 case DW_FORM_data2:
8861 case DW_FORM_ref2:
8fe0f950 8862 case DW_FORM_strx2:
4bb7a0a7
DJ
8863 info_ptr += 2;
8864 break;
8fe0f950
AT
8865 case DW_FORM_strx3:
8866 info_ptr += 3;
8867 break;
4bb7a0a7
DJ
8868 case DW_FORM_data4:
8869 case DW_FORM_ref4:
8fe0f950 8870 case DW_FORM_strx4:
4bb7a0a7
DJ
8871 info_ptr += 4;
8872 break;
8873 case DW_FORM_data8:
8874 case DW_FORM_ref8:
55f1336d 8875 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8876 info_ptr += 8;
8877 break;
0224619f
JK
8878 case DW_FORM_data16:
8879 info_ptr += 16;
8880 break;
4bb7a0a7 8881 case DW_FORM_string:
9b1c24c8 8882 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8883 info_ptr += bytes_read;
8884 break;
2dc7f7b3 8885 case DW_FORM_sec_offset:
4bb7a0a7 8886 case DW_FORM_strp:
36586728 8887 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8888 info_ptr += cu->header.offset_size;
8889 break;
2dc7f7b3 8890 case DW_FORM_exprloc:
4bb7a0a7
DJ
8891 case DW_FORM_block:
8892 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8893 info_ptr += bytes_read;
8894 break;
8895 case DW_FORM_block1:
8896 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8897 break;
8898 case DW_FORM_block2:
8899 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8900 break;
8901 case DW_FORM_block4:
8902 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8903 break;
336d760d 8904 case DW_FORM_addrx:
cf532bd1 8905 case DW_FORM_strx:
4bb7a0a7
DJ
8906 case DW_FORM_sdata:
8907 case DW_FORM_udata:
8908 case DW_FORM_ref_udata:
3019eac3
DE
8909 case DW_FORM_GNU_addr_index:
8910 case DW_FORM_GNU_str_index:
18a8505e 8911 case DW_FORM_rnglistx:
d521ce57 8912 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8913 break;
8914 case DW_FORM_indirect:
8915 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8916 info_ptr += bytes_read;
8917 /* We need to continue parsing from here, so just go back to
8918 the top. */
8919 goto skip_attribute;
8920
8921 default:
3e43a32a
MS
8922 error (_("Dwarf Error: Cannot handle %s "
8923 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8924 dwarf_form_name (form),
8925 bfd_get_filename (abfd));
8926 }
8927 }
8928
8929 if (abbrev->has_children)
dee91e82 8930 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8931 else
8932 return info_ptr;
8933}
8934
93311388 8935/* Locate ORIG_PDI's sibling.
dee91e82 8936 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8937
d521ce57 8938static const gdb_byte *
dee91e82
DE
8939locate_pdi_sibling (const struct die_reader_specs *reader,
8940 struct partial_die_info *orig_pdi,
d521ce57 8941 const gdb_byte *info_ptr)
91c24f0a
DC
8942{
8943 /* Do we know the sibling already? */
72bf9492 8944
91c24f0a
DC
8945 if (orig_pdi->sibling)
8946 return orig_pdi->sibling;
8947
8948 /* Are there any children to deal with? */
8949
8950 if (!orig_pdi->has_children)
8951 return info_ptr;
8952
4bb7a0a7 8953 /* Skip the children the long way. */
91c24f0a 8954
dee91e82 8955 return skip_children (reader, info_ptr);
91c24f0a
DC
8956}
8957
257e7a09 8958/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8959 not NULL. */
c906108c 8960
891813be
TT
8961void
8962dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8963{
ed2dc618
SM
8964 struct dwarf2_per_objfile *dwarf2_per_objfile
8965 = get_dwarf2_per_objfile (objfile);
8966
077cbab2
TT
8967 gdb_assert (!readin);
8968 /* If this psymtab is constructed from a debug-only objfile, the
8969 has_section_at_zero flag will not necessarily be correct. We
8970 can get the correct value for this flag by looking at the data
8971 associated with the (presumably stripped) associated objfile. */
8972 if (objfile->separate_debug_objfile_backlink)
c906108c 8973 {
077cbab2
TT
8974 struct dwarf2_per_objfile *dpo_backlink
8975 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8976
077cbab2
TT
8977 dwarf2_per_objfile->has_section_at_zero
8978 = dpo_backlink->has_section_at_zero;
8979 }
98bfdba5 8980
077cbab2 8981 dwarf2_per_objfile->reading_partial_symbols = 0;
c906108c 8982
8566b89b 8983 expand_psymtab (objfile);
95554aad 8984
ed2dc618 8985 process_cu_includes (dwarf2_per_objfile);
c906108c 8986}
9cdd5dbd
DE
8987\f
8988/* Reading in full CUs. */
c906108c 8989
10b3939b
DJ
8990/* Add PER_CU to the queue. */
8991
8992static void
95554aad
TT
8993queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8994 enum language pretend_language)
10b3939b 8995{
10b3939b 8996 per_cu->queued = 1;
39856def 8997 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8998}
8999
89e63ee4
DE
9000/* If PER_CU is not yet queued, add it to the queue.
9001 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9002 dependency.
0907af0c 9003 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9004 meaning either PER_CU is already queued or it is already loaded.
9005
9006 N.B. There is an invariant here that if a CU is queued then it is loaded.
9007 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9008
9009static int
89e63ee4 9010maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9011 struct dwarf2_per_cu_data *per_cu,
9012 enum language pretend_language)
9013{
9014 /* We may arrive here during partial symbol reading, if we need full
9015 DIEs to process an unusual case (e.g. template arguments). Do
9016 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9017 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9018 {
9019 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9020 return 1;
9021 return 0;
9022 }
9023
9024 /* Mark the dependence relation so that we don't flush PER_CU
9025 too early. */
89e63ee4
DE
9026 if (dependent_cu != NULL)
9027 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9028
9029 /* If it's already on the queue, we have nothing to do. */
9030 if (per_cu->queued)
9031 return 0;
9032
9033 /* If the compilation unit is already loaded, just mark it as
9034 used. */
9035 if (per_cu->cu != NULL)
9036 {
9037 per_cu->cu->last_used = 0;
9038 return 0;
9039 }
9040
9041 /* Add it to the queue. */
9042 queue_comp_unit (per_cu, pretend_language);
9043
9044 return 1;
9045}
9046
10b3939b
DJ
9047/* Process the queue. */
9048
9049static void
ed2dc618 9050process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 9051{
b4f54984 9052 if (dwarf_read_debug)
45cfd468
DE
9053 {
9054 fprintf_unfiltered (gdb_stdlog,
9055 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9056 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9057 }
9058
03dd20cc
DJ
9059 /* The queue starts out with one item, but following a DIE reference
9060 may load a new CU, adding it to the end of the queue. */
39856def 9061 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 9062 {
39856def
TT
9063 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
9064
cc12ce38 9065 if ((dwarf2_per_objfile->using_index
39856def
TT
9066 ? !item.per_cu->v.quick->compunit_symtab
9067 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 9068 /* Skip dummy CUs. */
39856def 9069 && item.per_cu->cu != NULL)
f4dc4d17 9070 {
39856def 9071 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9072 unsigned int debug_print_threshold;
247f5c4f 9073 char buf[100];
f4dc4d17 9074
247f5c4f 9075 if (per_cu->is_debug_types)
f4dc4d17 9076 {
247f5c4f
DE
9077 struct signatured_type *sig_type =
9078 (struct signatured_type *) per_cu;
9079
9d8780f0 9080 sprintf (buf, "TU %s at offset %s",
73be47f5 9081 hex_string (sig_type->signature),
9d8780f0 9082 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9083 /* There can be 100s of TUs.
9084 Only print them in verbose mode. */
9085 debug_print_threshold = 2;
f4dc4d17 9086 }
247f5c4f 9087 else
73be47f5 9088 {
9d8780f0
SM
9089 sprintf (buf, "CU at offset %s",
9090 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9091 debug_print_threshold = 1;
9092 }
247f5c4f 9093
b4f54984 9094 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9095 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9096
9097 if (per_cu->is_debug_types)
39856def 9098 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9099 else
39856def 9100 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9101
b4f54984 9102 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9103 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9104 }
10b3939b 9105
39856def
TT
9106 item.per_cu->queued = 0;
9107 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
9108 }
9109
b4f54984 9110 if (dwarf_read_debug)
45cfd468
DE
9111 {
9112 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9113 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9114 }
10b3939b
DJ
9115}
9116
10b3939b
DJ
9117/* Read in full symbols for PST, and anything it depends on. */
9118
8566b89b
TT
9119void
9120dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9121{
10b3939b 9122 struct dwarf2_per_cu_data *per_cu;
aaa75496 9123
8566b89b 9124 if (readin)
95554aad
TT
9125 return;
9126
0494dbec 9127 read_dependencies (objfile);
aaa75496 9128
8566b89b 9129 per_cu = per_cu_data;
10b3939b
DJ
9130
9131 if (per_cu == NULL)
aaa75496
JB
9132 {
9133 /* It's an include file, no symbols to read for it.
9134 Everything is in the parent symtab. */
8566b89b 9135 readin = true;
aaa75496
JB
9136 return;
9137 }
c906108c 9138
58f0c718 9139 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9140}
9141
dee91e82
DE
9142/* Trivial hash function for die_info: the hash value of a DIE
9143 is its offset in .debug_info for this objfile. */
10b3939b 9144
dee91e82
DE
9145static hashval_t
9146die_hash (const void *item)
10b3939b 9147{
9a3c8263 9148 const struct die_info *die = (const struct die_info *) item;
6502dd73 9149
9c541725 9150 return to_underlying (die->sect_off);
dee91e82 9151}
63d06c5c 9152
dee91e82
DE
9153/* Trivial comparison function for die_info structures: two DIEs
9154 are equal if they have the same offset. */
98bfdba5 9155
dee91e82
DE
9156static int
9157die_eq (const void *item_lhs, const void *item_rhs)
9158{
9a3c8263
SM
9159 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9160 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9161
9c541725 9162 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9163}
c906108c 9164
c0ab21c2 9165/* Load the DIEs associated with PER_CU into memory. */
c906108c 9166
dee91e82 9167static void
c0ab21c2
TT
9168load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9169 bool skip_partial,
9170 enum language pretend_language)
dee91e82 9171{
c0ab21c2
TT
9172 gdb_assert (! this_cu->is_debug_types);
9173
9174 cutu_reader reader (this_cu, NULL, 1, 1, skip_partial);
9175 if (reader.dummy_p)
9176 return;
9177
9178 struct dwarf2_cu *cu = reader.cu;
9179 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9180
dee91e82
DE
9181 gdb_assert (cu->die_hash == NULL);
9182 cu->die_hash =
9183 htab_create_alloc_ex (cu->header.length / 12,
9184 die_hash,
9185 die_eq,
9186 NULL,
9187 &cu->comp_unit_obstack,
9188 hashtab_obstack_allocate,
9189 dummy_obstack_deallocate);
e142c38c 9190
3e225074 9191 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9192 reader.comp_unit_die->child
9193 = read_die_and_siblings (&reader, reader.info_ptr,
9194 &info_ptr, reader.comp_unit_die);
9195 cu->dies = reader.comp_unit_die;
dee91e82 9196 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9197
9198 /* We try not to read any attributes in this function, because not
9cdd5dbd 9199 all CUs needed for references have been loaded yet, and symbol
10b3939b 9200 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9201 or we won't be able to build types correctly.
9202 Similarly, if we do not read the producer, we can not apply
9203 producer-specific interpretation. */
c0ab21c2 9204 prepare_one_comp_unit (cu, cu->dies, pretend_language);
10b3939b
DJ
9205}
9206
3da10d80
KS
9207/* Add a DIE to the delayed physname list. */
9208
9209static void
9210add_to_method_list (struct type *type, int fnfield_index, int index,
9211 const char *name, struct die_info *die,
9212 struct dwarf2_cu *cu)
9213{
9214 struct delayed_method_info mi;
9215 mi.type = type;
9216 mi.fnfield_index = fnfield_index;
9217 mi.index = index;
9218 mi.name = name;
9219 mi.die = die;
c89b44cd 9220 cu->method_list.push_back (mi);
3da10d80
KS
9221}
9222
3693fdb3
PA
9223/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9224 "const" / "volatile". If so, decrements LEN by the length of the
9225 modifier and return true. Otherwise return false. */
9226
9227template<size_t N>
9228static bool
9229check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9230{
9231 size_t mod_len = sizeof (mod) - 1;
9232 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9233 {
9234 len -= mod_len;
9235 return true;
9236 }
9237 return false;
9238}
9239
3da10d80
KS
9240/* Compute the physnames of any methods on the CU's method list.
9241
9242 The computation of method physnames is delayed in order to avoid the
9243 (bad) condition that one of the method's formal parameters is of an as yet
9244 incomplete type. */
9245
9246static void
9247compute_delayed_physnames (struct dwarf2_cu *cu)
9248{
3693fdb3 9249 /* Only C++ delays computing physnames. */
c89b44cd 9250 if (cu->method_list.empty ())
3693fdb3
PA
9251 return;
9252 gdb_assert (cu->language == language_cplus);
9253
52941706 9254 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9255 {
1d06ead6 9256 const char *physname;
3da10d80 9257 struct fn_fieldlist *fn_flp
c89b44cd
TT
9258 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9259 physname = dwarf2_physname (mi.name, mi.die, cu);
9260 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9261 = physname ? physname : "";
3693fdb3
PA
9262
9263 /* Since there's no tag to indicate whether a method is a
9264 const/volatile overload, extract that information out of the
9265 demangled name. */
9266 if (physname != NULL)
9267 {
9268 size_t len = strlen (physname);
9269
9270 while (1)
9271 {
9272 if (physname[len] == ')') /* shortcut */
9273 break;
9274 else if (check_modifier (physname, len, " const"))
c89b44cd 9275 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9276 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9277 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9278 else
9279 break;
9280 }
9281 }
3da10d80 9282 }
c89b44cd
TT
9283
9284 /* The list is no longer needed. */
9285 cu->method_list.clear ();
3da10d80
KS
9286}
9287
a766d390
DE
9288/* Go objects should be embedded in a DW_TAG_module DIE,
9289 and it's not clear if/how imported objects will appear.
9290 To keep Go support simple until that's worked out,
9291 go back through what we've read and create something usable.
9292 We could do this while processing each DIE, and feels kinda cleaner,
9293 but that way is more invasive.
9294 This is to, for example, allow the user to type "p var" or "b main"
9295 without having to specify the package name, and allow lookups
9296 of module.object to work in contexts that use the expression
9297 parser. */
9298
9299static void
9300fixup_go_packaging (struct dwarf2_cu *cu)
9301{
421d1616 9302 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9303 struct pending *list;
9304 int i;
9305
c24bdb02 9306 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9307 list != NULL;
9308 list = list->next)
a766d390
DE
9309 {
9310 for (i = 0; i < list->nsyms; ++i)
9311 {
9312 struct symbol *sym = list->symbol[i];
9313
c1b5c1eb 9314 if (sym->language () == language_go
a766d390
DE
9315 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9316 {
421d1616
TT
9317 gdb::unique_xmalloc_ptr<char> this_package_name
9318 (go_symbol_package_name (sym));
a766d390
DE
9319
9320 if (this_package_name == NULL)
9321 continue;
9322 if (package_name == NULL)
421d1616 9323 package_name = std::move (this_package_name);
a766d390
DE
9324 else
9325 {
518817b3
SM
9326 struct objfile *objfile
9327 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9328 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9329 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9330 (symbol_symtab (sym) != NULL
9331 ? symtab_to_filename_for_display
9332 (symbol_symtab (sym))
e3b94546 9333 : objfile_name (objfile)),
421d1616 9334 this_package_name.get (), package_name.get ());
a766d390
DE
9335 }
9336 }
9337 }
9338 }
9339
9340 if (package_name != NULL)
9341 {
518817b3 9342 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9343 const char *saved_package_name
421d1616 9344 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name.get ());
19f392bc
UW
9345 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9346 saved_package_name);
a766d390
DE
9347 struct symbol *sym;
9348
e623cf5d 9349 sym = allocate_symbol (objfile);
d3ecddab 9350 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9351 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9352 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9353 e.g., "main" finds the "main" module and not C's main(). */
9354 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9355 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9356 SYMBOL_TYPE (sym) = type;
9357
c24bdb02 9358 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9359 }
9360}
9361
c9317f21
TT
9362/* Allocate a fully-qualified name consisting of the two parts on the
9363 obstack. */
9364
9365static const char *
9366rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9367{
9368 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9369}
9370
9371/* A helper that allocates a struct discriminant_info to attach to a
9372 union type. */
9373
9374static struct discriminant_info *
9375alloc_discriminant_info (struct type *type, int discriminant_index,
9376 int default_index)
9377{
9378 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9379 gdb_assert (discriminant_index == -1
9380 || (discriminant_index >= 0
9381 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9382 gdb_assert (default_index == -1
c7b15a66 9383 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9384
9385 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9386
9387 struct discriminant_info *disc
9388 = ((struct discriminant_info *)
9389 TYPE_ZALLOC (type,
9390 offsetof (struct discriminant_info, discriminants)
9391 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9392 disc->default_index = default_index;
9393 disc->discriminant_index = discriminant_index;
9394
9395 struct dynamic_prop prop;
9396 prop.kind = PROP_UNDEFINED;
9397 prop.data.baton = disc;
9398
9399 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9400
9401 return disc;
9402}
9403
9404/* Some versions of rustc emitted enums in an unusual way.
9405
9406 Ordinary enums were emitted as unions. The first element of each
9407 structure in the union was named "RUST$ENUM$DISR". This element
9408 held the discriminant.
9409
9410 These versions of Rust also implemented the "non-zero"
9411 optimization. When the enum had two values, and one is empty and
9412 the other holds a pointer that cannot be zero, the pointer is used
9413 as the discriminant, with a zero value meaning the empty variant.
9414 Here, the union's first member is of the form
9415 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9416 where the fieldnos are the indices of the fields that should be
9417 traversed in order to find the field (which may be several fields deep)
9418 and the variantname is the name of the variant of the case when the
9419 field is zero.
9420
9421 This function recognizes whether TYPE is of one of these forms,
9422 and, if so, smashes it to be a variant type. */
9423
9424static void
9425quirk_rust_enum (struct type *type, struct objfile *objfile)
9426{
9427 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9428
9429 /* We don't need to deal with empty enums. */
9430 if (TYPE_NFIELDS (type) == 0)
9431 return;
9432
9433#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9434 if (TYPE_NFIELDS (type) == 1
9435 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9436 {
9437 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9438
9439 /* Decode the field name to find the offset of the
9440 discriminant. */
9441 ULONGEST bit_offset = 0;
9442 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9443 while (name[0] >= '0' && name[0] <= '9')
9444 {
9445 char *tail;
9446 unsigned long index = strtoul (name, &tail, 10);
9447 name = tail;
9448 if (*name != '$'
9449 || index >= TYPE_NFIELDS (field_type)
9450 || (TYPE_FIELD_LOC_KIND (field_type, index)
9451 != FIELD_LOC_KIND_BITPOS))
9452 {
b98664d3 9453 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9454 "[in module %s]"),
9455 TYPE_FIELD_NAME (type, 0),
9456 objfile_name (objfile));
9457 return;
9458 }
9459 ++name;
9460
9461 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9462 field_type = TYPE_FIELD_TYPE (field_type, index);
9463 }
9464
9465 /* Make a union to hold the variants. */
9466 struct type *union_type = alloc_type (objfile);
9467 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9468 TYPE_NFIELDS (union_type) = 3;
9469 TYPE_FIELDS (union_type)
9470 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9471 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9472 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9473
9474 /* Put the discriminant must at index 0. */
9475 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9476 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9477 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9478 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9479
9480 /* The order of fields doesn't really matter, so put the real
9481 field at index 1 and the data-less field at index 2. */
9482 struct discriminant_info *disc
9483 = alloc_discriminant_info (union_type, 0, 1);
9484 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9485 TYPE_FIELD_NAME (union_type, 1)
9486 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9487 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9488 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9489 TYPE_FIELD_NAME (union_type, 1));
9490
9491 const char *dataless_name
9492 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9493 name);
9494 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9495 dataless_name);
9496 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9497 /* NAME points into the original discriminant name, which
9498 already has the correct lifetime. */
9499 TYPE_FIELD_NAME (union_type, 2) = name;
9500 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9501 disc->discriminants[2] = 0;
9502
9503 /* Smash this type to be a structure type. We have to do this
9504 because the type has already been recorded. */
9505 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9506 TYPE_NFIELDS (type) = 1;
9507 TYPE_FIELDS (type)
9508 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9509
9510 /* Install the variant part. */
9511 TYPE_FIELD_TYPE (type, 0) = union_type;
9512 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9513 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9514 }
77c2dba3
TT
9515 /* A union with a single anonymous field is probably an old-style
9516 univariant enum. */
9517 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9518 {
c9317f21
TT
9519 /* Smash this type to be a structure type. We have to do this
9520 because the type has already been recorded. */
9521 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9522
9523 /* Make a union to hold the variants. */
9524 struct type *union_type = alloc_type (objfile);
9525 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9526 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9527 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9528 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9529 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9530
9531 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9532 const char *variant_name
9533 = rust_last_path_segment (TYPE_NAME (field_type));
9534 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9535 TYPE_NAME (field_type)
9536 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9537 TYPE_NAME (type), variant_name);
c9317f21
TT
9538
9539 /* Install the union in the outer struct type. */
9540 TYPE_NFIELDS (type) = 1;
9541 TYPE_FIELDS (type)
9542 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9543 TYPE_FIELD_TYPE (type, 0) = union_type;
9544 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9545 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9546
9547 alloc_discriminant_info (union_type, -1, 0);
9548 }
9549 else
9550 {
9551 struct type *disr_type = nullptr;
9552 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9553 {
9554 disr_type = TYPE_FIELD_TYPE (type, i);
9555
a037790e
TT
9556 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9557 {
9558 /* All fields of a true enum will be structs. */
9559 return;
9560 }
9561 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9562 {
9563 /* Could be data-less variant, so keep going. */
a037790e 9564 disr_type = nullptr;
c9317f21
TT
9565 }
9566 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9567 "RUST$ENUM$DISR") != 0)
9568 {
9569 /* Not a Rust enum. */
9570 return;
9571 }
9572 else
9573 {
9574 /* Found one. */
9575 break;
9576 }
9577 }
9578
9579 /* If we got here without a discriminant, then it's probably
9580 just a union. */
9581 if (disr_type == nullptr)
9582 return;
9583
9584 /* Smash this type to be a structure type. We have to do this
9585 because the type has already been recorded. */
9586 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9587
9588 /* Make a union to hold the variants. */
9589 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9590 struct type *union_type = alloc_type (objfile);
9591 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9592 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9593 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9594 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9595 TYPE_FIELDS (union_type)
9596 = (struct field *) TYPE_ZALLOC (union_type,
9597 (TYPE_NFIELDS (union_type)
9598 * sizeof (struct field)));
9599
9600 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9601 TYPE_NFIELDS (type) * sizeof (struct field));
9602
9603 /* Install the discriminant at index 0 in the union. */
9604 TYPE_FIELD (union_type, 0) = *disr_field;
9605 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9606 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9607
9608 /* Install the union in the outer struct type. */
9609 TYPE_FIELD_TYPE (type, 0) = union_type;
9610 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9611 TYPE_NFIELDS (type) = 1;
9612
9613 /* Set the size and offset of the union type. */
9614 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9615
9616 /* We need a way to find the correct discriminant given a
9617 variant name. For convenience we build a map here. */
9618 struct type *enum_type = FIELD_TYPE (*disr_field);
9619 std::unordered_map<std::string, ULONGEST> discriminant_map;
9620 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9621 {
9622 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9623 {
9624 const char *name
9625 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9626 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9627 }
9628 }
9629
9630 int n_fields = TYPE_NFIELDS (union_type);
9631 struct discriminant_info *disc
9632 = alloc_discriminant_info (union_type, 0, -1);
9633 /* Skip the discriminant here. */
9634 for (int i = 1; i < n_fields; ++i)
9635 {
9636 /* Find the final word in the name of this variant's type.
9637 That name can be used to look up the correct
9638 discriminant. */
9639 const char *variant_name
9640 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9641 i)));
9642
9643 auto iter = discriminant_map.find (variant_name);
9644 if (iter != discriminant_map.end ())
9645 disc->discriminants[i] = iter->second;
9646
bedda9ac 9647 /* Remove the discriminant field, if it exists. */
c9317f21 9648 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9649 if (TYPE_NFIELDS (sub_type) > 0)
9650 {
9651 --TYPE_NFIELDS (sub_type);
9652 ++TYPE_FIELDS (sub_type);
9653 }
c9317f21
TT
9654 TYPE_FIELD_NAME (union_type, i) = variant_name;
9655 TYPE_NAME (sub_type)
9656 = rust_fully_qualify (&objfile->objfile_obstack,
9657 TYPE_NAME (type), variant_name);
9658 }
9659 }
9660}
9661
9662/* Rewrite some Rust unions to be structures with variants parts. */
9663
9664static void
9665rust_union_quirks (struct dwarf2_cu *cu)
9666{
9667 gdb_assert (cu->language == language_rust);
52941706
SM
9668 for (type *type_ : cu->rust_unions)
9669 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9670 /* We don't need this any more. */
9671 cu->rust_unions.clear ();
c9317f21
TT
9672}
9673
95554aad
TT
9674/* Return the symtab for PER_CU. This works properly regardless of
9675 whether we're using the index or psymtabs. */
9676
43f3e411
DE
9677static struct compunit_symtab *
9678get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9679{
ed2dc618 9680 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9681 ? per_cu->v.quick->compunit_symtab
9682 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9683}
9684
9685/* A helper function for computing the list of all symbol tables
9686 included by PER_CU. */
9687
9688static void
4c39bc03 9689recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9690 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9691 struct dwarf2_per_cu_data *per_cu,
43f3e411 9692 struct compunit_symtab *immediate_parent)
95554aad
TT
9693{
9694 void **slot;
43f3e411 9695 struct compunit_symtab *cust;
95554aad
TT
9696
9697 slot = htab_find_slot (all_children, per_cu, INSERT);
9698 if (*slot != NULL)
9699 {
9700 /* This inclusion and its children have been processed. */
9701 return;
9702 }
9703
9704 *slot = per_cu;
9705 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9706 cust = get_compunit_symtab (per_cu);
9707 if (cust != NULL)
ec94af83
DE
9708 {
9709 /* If this is a type unit only add its symbol table if we haven't
9710 seen it yet (type unit per_cu's can share symtabs). */
9711 if (per_cu->is_debug_types)
9712 {
43f3e411 9713 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9714 if (*slot == NULL)
9715 {
43f3e411 9716 *slot = cust;
4c39bc03 9717 result->push_back (cust);
43f3e411
DE
9718 if (cust->user == NULL)
9719 cust->user = immediate_parent;
ec94af83
DE
9720 }
9721 }
9722 else
f9125b6c 9723 {
4c39bc03 9724 result->push_back (cust);
43f3e411
DE
9725 if (cust->user == NULL)
9726 cust->user = immediate_parent;
f9125b6c 9727 }
ec94af83 9728 }
95554aad 9729
ae640021
AB
9730 if (!per_cu->imported_symtabs_empty ())
9731 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9732 {
9733 recursively_compute_inclusions (result, all_children,
9734 all_type_symtabs, ptr, cust);
9735 }
95554aad
TT
9736}
9737
43f3e411 9738/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9739 PER_CU. */
9740
9741static void
43f3e411 9742compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9743{
f4dc4d17
DE
9744 gdb_assert (! per_cu->is_debug_types);
9745
ae640021 9746 if (!per_cu->imported_symtabs_empty ())
95554aad 9747 {
ae640021 9748 int len;
4c39bc03 9749 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9750 htab_t all_children, all_type_symtabs;
43f3e411 9751 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9752
9753 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9754 if (cust == NULL)
95554aad
TT
9755 return;
9756
9757 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9758 NULL, xcalloc, xfree);
ec94af83
DE
9759 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9760 NULL, xcalloc, xfree);
95554aad 9761
ae640021 9762 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9763 {
9764 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9765 all_type_symtabs, ptr, cust);
ec94af83 9766 }
95554aad 9767
ec94af83 9768 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9769 len = result_symtabs.size ();
43f3e411 9770 cust->includes
ed2dc618 9771 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9772 struct compunit_symtab *, len + 1);
4c39bc03
TT
9773 memcpy (cust->includes, result_symtabs.data (),
9774 len * sizeof (compunit_symtab *));
43f3e411 9775 cust->includes[len] = NULL;
95554aad 9776
95554aad 9777 htab_delete (all_children);
ec94af83 9778 htab_delete (all_type_symtabs);
95554aad
TT
9779 }
9780}
9781
9782/* Compute the 'includes' field for the symtabs of all the CUs we just
9783 read. */
9784
9785static void
ed2dc618 9786process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9787{
71b73764 9788 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9789 {
9790 if (! iter->is_debug_types)
43f3e411 9791 compute_compunit_symtab_includes (iter);
f4dc4d17 9792 }
95554aad 9793
c5d0225d 9794 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9795}
9796
9cdd5dbd 9797/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9798 already been loaded into memory. */
9799
9800static void
95554aad
TT
9801process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9802 enum language pretend_language)
10b3939b 9803{
10b3939b 9804 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9805 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9806 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9807 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9808 CORE_ADDR lowpc, highpc;
43f3e411 9809 struct compunit_symtab *cust;
10b3939b 9810 CORE_ADDR baseaddr;
4359dff1 9811 struct block *static_block;
3e29f34a 9812 CORE_ADDR addr;
10b3939b 9813
b3b3bada 9814 baseaddr = objfile->text_section_offset ();
10b3939b 9815
c89b44cd
TT
9816 /* Clear the list here in case something was left over. */
9817 cu->method_list.clear ();
10b3939b 9818
95554aad
TT
9819 cu->language = pretend_language;
9820 cu->language_defn = language_def (cu->language);
9821
c906108c 9822 /* Do line number decoding in read_file_scope () */
10b3939b 9823 process_die (cu->dies, cu);
c906108c 9824
a766d390
DE
9825 /* For now fudge the Go package. */
9826 if (cu->language == language_go)
9827 fixup_go_packaging (cu);
9828
5f48f8f3 9829 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9830 should be complete, and it should now be safe to compute all of the
9831 physnames. */
9832 compute_delayed_physnames (cu);
3da10d80 9833
c9317f21
TT
9834 if (cu->language == language_rust)
9835 rust_union_quirks (cu);
9836
fae299cd
DC
9837 /* Some compilers don't define a DW_AT_high_pc attribute for the
9838 compilation unit. If the DW_AT_high_pc is missing, synthesize
9839 it, by scanning the DIE's below the compilation unit. */
10b3939b 9840 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9841
3e29f34a 9842 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9843 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9844
9845 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9846 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9847 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9848 addrmap to help ensure it has an accurate map of pc values belonging to
9849 this comp unit. */
9850 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9851
c24bdb02 9852 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9853 SECT_OFF_TEXT (objfile),
9854 0);
c906108c 9855
43f3e411 9856 if (cust != NULL)
c906108c 9857 {
df15bd07 9858 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9859
8be455d7
JK
9860 /* Set symtab language to language from DW_AT_language. If the
9861 compilation is from a C file generated by language preprocessors, do
9862 not set the language if it was already deduced by start_subfile. */
43f3e411 9863 if (!(cu->language == language_c
40e3ad0e 9864 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9865 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9866
9867 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9868 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9869 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9870 there were bugs in prologue debug info, fixed later in GCC-4.5
9871 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9872
9873 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9874 needed, it would be wrong due to missing DW_AT_producer there.
9875
9876 Still one can confuse GDB by using non-standard GCC compilation
9877 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9878 */
ab260dad 9879 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9880 cust->locations_valid = 1;
e0d00bc7
JK
9881
9882 if (gcc_4_minor >= 5)
43f3e411 9883 cust->epilogue_unwind_valid = 1;
96408a79 9884
43f3e411 9885 cust->call_site_htab = cu->call_site_htab;
c906108c 9886 }
9291a0cd
TT
9887
9888 if (dwarf2_per_objfile->using_index)
43f3e411 9889 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9890 else
9891 {
891813be 9892 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9893 pst->compunit_symtab = cust;
6d94535f 9894 pst->readin = true;
9291a0cd 9895 }
c906108c 9896
95554aad 9897 /* Push it for inclusion processing later. */
c5d0225d 9898 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9899
9900 /* Not needed any more. */
c24bdb02 9901 cu->reset_builder ();
f4dc4d17 9902}
45cfd468 9903
f4dc4d17
DE
9904/* Generate full symbol information for type unit PER_CU, whose DIEs have
9905 already been loaded into memory. */
9906
9907static void
9908process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9909 enum language pretend_language)
9910{
9911 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9912 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9913 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9914 struct compunit_symtab *cust;
0186c6a7
DE
9915 struct signatured_type *sig_type;
9916
9917 gdb_assert (per_cu->is_debug_types);
9918 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9919
c89b44cd
TT
9920 /* Clear the list here in case something was left over. */
9921 cu->method_list.clear ();
f4dc4d17 9922
f4dc4d17
DE
9923 cu->language = pretend_language;
9924 cu->language_defn = language_def (cu->language);
9925
9926 /* The symbol tables are set up in read_type_unit_scope. */
9927 process_die (cu->dies, cu);
9928
9929 /* For now fudge the Go package. */
9930 if (cu->language == language_go)
9931 fixup_go_packaging (cu);
9932
5f48f8f3 9933 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9934 should be complete, and it should now be safe to compute all of the
9935 physnames. */
9936 compute_delayed_physnames (cu);
f4dc4d17 9937
c9317f21
TT
9938 if (cu->language == language_rust)
9939 rust_union_quirks (cu);
9940
f4dc4d17
DE
9941 /* TUs share symbol tables.
9942 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9943 of it with end_expandable_symtab. Otherwise, complete the addition of
9944 this TU's symbols to the existing symtab. */
43f3e411 9945 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9946 {
c24bdb02
KS
9947 buildsym_compunit *builder = cu->get_builder ();
9948 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9949 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9950
43f3e411 9951 if (cust != NULL)
f4dc4d17
DE
9952 {
9953 /* Set symtab language to language from DW_AT_language. If the
9954 compilation is from a C file generated by language preprocessors,
9955 do not set the language if it was already deduced by
9956 start_subfile. */
43f3e411
DE
9957 if (!(cu->language == language_c
9958 && COMPUNIT_FILETABS (cust)->language != language_c))
9959 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9960 }
9961 }
9962 else
9963 {
c24bdb02 9964 cu->get_builder ()->augment_type_symtab ();
43f3e411 9965 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9966 }
9967
9968 if (dwarf2_per_objfile->using_index)
43f3e411 9969 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9970 else
9971 {
891813be 9972 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9973 pst->compunit_symtab = cust;
6d94535f 9974 pst->readin = true;
45cfd468 9975 }
804d2729
TT
9976
9977 /* Not needed any more. */
c24bdb02 9978 cu->reset_builder ();
c906108c
SS
9979}
9980
95554aad
TT
9981/* Process an imported unit DIE. */
9982
9983static void
9984process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9985{
9986 struct attribute *attr;
9987
f4dc4d17
DE
9988 /* For now we don't handle imported units in type units. */
9989 if (cu->per_cu->is_debug_types)
9990 {
9991 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9992 " supported in type units [in module %s]"),
518817b3 9993 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9994 }
9995
95554aad
TT
9996 attr = dwarf2_attr (die, DW_AT_import, cu);
9997 if (attr != NULL)
9998 {
9c541725
PA
9999 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10000 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10001 dwarf2_per_cu_data *per_cu
e3b94546 10002 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10003 cu->per_cu->dwarf2_per_objfile);
95554aad 10004
69d751e3 10005 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10006 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10007 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10008
ae640021 10009 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10010 }
10011}
10012
4c8aa72d
PA
10013/* RAII object that represents a process_die scope: i.e.,
10014 starts/finishes processing a DIE. */
10015class process_die_scope
adde2bff 10016{
4c8aa72d
PA
10017public:
10018 process_die_scope (die_info *die, dwarf2_cu *cu)
10019 : m_die (die), m_cu (cu)
10020 {
10021 /* We should only be processing DIEs not already in process. */
10022 gdb_assert (!m_die->in_process);
10023 m_die->in_process = true;
10024 }
8c3cb9fa 10025
4c8aa72d
PA
10026 ~process_die_scope ()
10027 {
10028 m_die->in_process = false;
10029
10030 /* If we're done processing the DIE for the CU that owns the line
10031 header, we don't need the line header anymore. */
10032 if (m_cu->line_header_die_owner == m_die)
10033 {
10034 delete m_cu->line_header;
10035 m_cu->line_header = NULL;
10036 m_cu->line_header_die_owner = NULL;
10037 }
10038 }
10039
10040private:
10041 die_info *m_die;
10042 dwarf2_cu *m_cu;
10043};
adde2bff 10044
c906108c
SS
10045/* Process a die and its children. */
10046
10047static void
e7c27a73 10048process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10049{
4c8aa72d 10050 process_die_scope scope (die, cu);
adde2bff 10051
c906108c
SS
10052 switch (die->tag)
10053 {
10054 case DW_TAG_padding:
10055 break;
10056 case DW_TAG_compile_unit:
95554aad 10057 case DW_TAG_partial_unit:
e7c27a73 10058 read_file_scope (die, cu);
c906108c 10059 break;
348e048f
DE
10060 case DW_TAG_type_unit:
10061 read_type_unit_scope (die, cu);
10062 break;
c906108c 10063 case DW_TAG_subprogram:
0a4b0913
AB
10064 /* Nested subprograms in Fortran get a prefix. */
10065 if (cu->language == language_fortran
10066 && die->parent != NULL
10067 && die->parent->tag == DW_TAG_subprogram)
10068 cu->processing_has_namespace_info = true;
10069 /* Fall through. */
c906108c 10070 case DW_TAG_inlined_subroutine:
edb3359d 10071 read_func_scope (die, cu);
c906108c
SS
10072 break;
10073 case DW_TAG_lexical_block:
14898363
L
10074 case DW_TAG_try_block:
10075 case DW_TAG_catch_block:
e7c27a73 10076 read_lexical_block_scope (die, cu);
c906108c 10077 break;
216f72a1 10078 case DW_TAG_call_site:
96408a79
SA
10079 case DW_TAG_GNU_call_site:
10080 read_call_site_scope (die, cu);
10081 break;
c906108c 10082 case DW_TAG_class_type:
680b30c7 10083 case DW_TAG_interface_type:
c906108c
SS
10084 case DW_TAG_structure_type:
10085 case DW_TAG_union_type:
134d01f1 10086 process_structure_scope (die, cu);
c906108c
SS
10087 break;
10088 case DW_TAG_enumeration_type:
134d01f1 10089 process_enumeration_scope (die, cu);
c906108c 10090 break;
134d01f1 10091
f792889a
DJ
10092 /* These dies have a type, but processing them does not create
10093 a symbol or recurse to process the children. Therefore we can
10094 read them on-demand through read_type_die. */
c906108c 10095 case DW_TAG_subroutine_type:
72019c9c 10096 case DW_TAG_set_type:
c906108c 10097 case DW_TAG_array_type:
c906108c 10098 case DW_TAG_pointer_type:
c906108c 10099 case DW_TAG_ptr_to_member_type:
c906108c 10100 case DW_TAG_reference_type:
4297a3f0 10101 case DW_TAG_rvalue_reference_type:
c906108c 10102 case DW_TAG_string_type:
c906108c 10103 break;
134d01f1 10104
c906108c 10105 case DW_TAG_base_type:
a02abb62 10106 case DW_TAG_subrange_type:
cb249c71 10107 case DW_TAG_typedef:
134d01f1
DJ
10108 /* Add a typedef symbol for the type definition, if it has a
10109 DW_AT_name. */
f792889a 10110 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10111 break;
c906108c 10112 case DW_TAG_common_block:
e7c27a73 10113 read_common_block (die, cu);
c906108c
SS
10114 break;
10115 case DW_TAG_common_inclusion:
10116 break;
d9fa45fe 10117 case DW_TAG_namespace:
9068261f 10118 cu->processing_has_namespace_info = true;
e7c27a73 10119 read_namespace (die, cu);
d9fa45fe 10120 break;
5d7cb8df 10121 case DW_TAG_module:
9068261f 10122 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10123 read_module (die, cu);
10124 break;
d9fa45fe 10125 case DW_TAG_imported_declaration:
9068261f 10126 cu->processing_has_namespace_info = true;
74921315
KS
10127 if (read_namespace_alias (die, cu))
10128 break;
86a73007
TT
10129 /* The declaration is not a global namespace alias. */
10130 /* Fall through. */
d9fa45fe 10131 case DW_TAG_imported_module:
9068261f 10132 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10133 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10134 || cu->language != language_fortran))
b98664d3 10135 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10136 dwarf_tag_name (die->tag));
10137 read_import_statement (die, cu);
d9fa45fe 10138 break;
95554aad
TT
10139
10140 case DW_TAG_imported_unit:
10141 process_imported_unit_die (die, cu);
10142 break;
10143
71a3c369
TT
10144 case DW_TAG_variable:
10145 read_variable (die, cu);
10146 break;
10147
c906108c 10148 default:
e7c27a73 10149 new_symbol (die, NULL, cu);
c906108c
SS
10150 break;
10151 }
10152}
ca69b9e6
DE
10153\f
10154/* DWARF name computation. */
c906108c 10155
94af9270
KS
10156/* A helper function for dwarf2_compute_name which determines whether DIE
10157 needs to have the name of the scope prepended to the name listed in the
10158 die. */
10159
10160static int
10161die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10162{
1c809c68
TT
10163 struct attribute *attr;
10164
94af9270
KS
10165 switch (die->tag)
10166 {
10167 case DW_TAG_namespace:
10168 case DW_TAG_typedef:
10169 case DW_TAG_class_type:
10170 case DW_TAG_interface_type:
10171 case DW_TAG_structure_type:
10172 case DW_TAG_union_type:
10173 case DW_TAG_enumeration_type:
10174 case DW_TAG_enumerator:
10175 case DW_TAG_subprogram:
08a76f8a 10176 case DW_TAG_inlined_subroutine:
94af9270 10177 case DW_TAG_member:
74921315 10178 case DW_TAG_imported_declaration:
94af9270
KS
10179 return 1;
10180
10181 case DW_TAG_variable:
c2b0a229 10182 case DW_TAG_constant:
94af9270
KS
10183 /* We only need to prefix "globally" visible variables. These include
10184 any variable marked with DW_AT_external or any variable that
10185 lives in a namespace. [Variables in anonymous namespaces
10186 require prefixing, but they are not DW_AT_external.] */
10187
10188 if (dwarf2_attr (die, DW_AT_specification, cu))
10189 {
10190 struct dwarf2_cu *spec_cu = cu;
9a619af0 10191
94af9270
KS
10192 return die_needs_namespace (die_specification (die, &spec_cu),
10193 spec_cu);
10194 }
10195
1c809c68 10196 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10197 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10198 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10199 return 0;
10200 /* A variable in a lexical block of some kind does not need a
10201 namespace, even though in C++ such variables may be external
10202 and have a mangled name. */
10203 if (die->parent->tag == DW_TAG_lexical_block
10204 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10205 || die->parent->tag == DW_TAG_catch_block
10206 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10207 return 0;
10208 return 1;
94af9270
KS
10209
10210 default:
10211 return 0;
10212 }
10213}
10214
73b9be8b
KS
10215/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10216 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10217 defined for the given DIE. */
10218
10219static struct attribute *
10220dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10221{
10222 struct attribute *attr;
10223
10224 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10225 if (attr == NULL)
10226 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10227
10228 return attr;
10229}
10230
10231/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10232 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10233 defined for the given DIE. */
10234
10235static const char *
10236dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10237{
10238 const char *linkage_name;
10239
10240 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10241 if (linkage_name == NULL)
10242 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10243
10244 return linkage_name;
10245}
10246
94af9270 10247/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10248 compute the physname for the object, which include a method's:
9c37b5ae 10249 - formal parameters (C++),
a766d390 10250 - receiver type (Go),
a766d390
DE
10251
10252 The term "physname" is a bit confusing.
10253 For C++, for example, it is the demangled name.
10254 For Go, for example, it's the mangled name.
94af9270 10255
af6b7be1
JB
10256 For Ada, return the DIE's linkage name rather than the fully qualified
10257 name. PHYSNAME is ignored..
10258
94af9270
KS
10259 The result is allocated on the objfile_obstack and canonicalized. */
10260
10261static const char *
15d034d0
TT
10262dwarf2_compute_name (const char *name,
10263 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10264 int physname)
10265{
518817b3 10266 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10267
94af9270
KS
10268 if (name == NULL)
10269 name = dwarf2_name (die, cu);
10270
2ee7123e
DE
10271 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10272 but otherwise compute it by typename_concat inside GDB.
10273 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10274 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10275 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10276 will set the demangled name to the result of dwarf2_full_name, and it is
10277 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10278 if (cu->language == language_ada
10279 || (cu->language == language_fortran && physname))
10280 {
10281 /* For Ada unit, we prefer the linkage name over the name, as
10282 the former contains the exported name, which the user expects
10283 to be able to reference. Ideally, we want the user to be able
10284 to reference this entity using either natural or linkage name,
10285 but we haven't started looking at this enhancement yet. */
73b9be8b 10286 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10287
2ee7123e
DE
10288 if (linkage_name != NULL)
10289 return linkage_name;
f55ee35c
JK
10290 }
10291
94af9270
KS
10292 /* These are the only languages we know how to qualify names in. */
10293 if (name != NULL
9c37b5ae 10294 && (cu->language == language_cplus
c44af4eb
TT
10295 || cu->language == language_fortran || cu->language == language_d
10296 || cu->language == language_rust))
94af9270
KS
10297 {
10298 if (die_needs_namespace (die, cu))
10299 {
0d5cff50 10300 const char *prefix;
34a68019 10301 const char *canonical_name = NULL;
94af9270 10302
d7e74731
PA
10303 string_file buf;
10304
94af9270 10305 prefix = determine_prefix (die, cu);
94af9270
KS
10306 if (*prefix != '\0')
10307 {
43816ebc
TT
10308 gdb::unique_xmalloc_ptr<char> prefixed_name
10309 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10310
43816ebc 10311 buf.puts (prefixed_name.get ());
94af9270
KS
10312 }
10313 else
d7e74731 10314 buf.puts (name);
94af9270 10315
98bfdba5
PA
10316 /* Template parameters may be specified in the DIE's DW_AT_name, or
10317 as children with DW_TAG_template_type_param or
10318 DW_TAG_value_type_param. If the latter, add them to the name
10319 here. If the name already has template parameters, then
10320 skip this step; some versions of GCC emit both, and
10321 it is more efficient to use the pre-computed name.
10322
10323 Something to keep in mind about this process: it is very
10324 unlikely, or in some cases downright impossible, to produce
10325 something that will match the mangled name of a function.
10326 If the definition of the function has the same debug info,
10327 we should be able to match up with it anyway. But fallbacks
10328 using the minimal symbol, for instance to find a method
10329 implemented in a stripped copy of libstdc++, will not work.
10330 If we do not have debug info for the definition, we will have to
10331 match them up some other way.
10332
10333 When we do name matching there is a related problem with function
10334 templates; two instantiated function templates are allowed to
10335 differ only by their return types, which we do not add here. */
10336
10337 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10338 {
10339 struct attribute *attr;
10340 struct die_info *child;
10341 int first = 1;
10342
10343 die->building_fullname = 1;
10344
10345 for (child = die->child; child != NULL; child = child->sibling)
10346 {
10347 struct type *type;
12df843f 10348 LONGEST value;
d521ce57 10349 const gdb_byte *bytes;
98bfdba5
PA
10350 struct dwarf2_locexpr_baton *baton;
10351 struct value *v;
10352
10353 if (child->tag != DW_TAG_template_type_param
10354 && child->tag != DW_TAG_template_value_param)
10355 continue;
10356
10357 if (first)
10358 {
d7e74731 10359 buf.puts ("<");
98bfdba5
PA
10360 first = 0;
10361 }
10362 else
d7e74731 10363 buf.puts (", ");
98bfdba5
PA
10364
10365 attr = dwarf2_attr (child, DW_AT_type, cu);
10366 if (attr == NULL)
10367 {
b98664d3 10368 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10369 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10370 continue;
10371 }
10372 type = die_type (child, cu);
10373
10374 if (child->tag == DW_TAG_template_type_param)
10375 {
c1ec8cea
TT
10376 c_print_type (type, "", &buf, -1, 0, cu->language,
10377 &type_print_raw_options);
98bfdba5
PA
10378 continue;
10379 }
10380
10381 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10382 if (attr == NULL)
10383 {
b98664d3 10384 complaint (_("template parameter missing "
3e43a32a 10385 "DW_AT_const_value"));
d7e74731 10386 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10387 continue;
10388 }
10389
10390 dwarf2_const_value_attr (attr, type, name,
10391 &cu->comp_unit_obstack, cu,
10392 &value, &bytes, &baton);
10393
10394 if (TYPE_NOSIGN (type))
10395 /* GDB prints characters as NUMBER 'CHAR'. If that's
10396 changed, this can use value_print instead. */
d7e74731 10397 c_printchar (value, type, &buf);
98bfdba5
PA
10398 else
10399 {
10400 struct value_print_options opts;
10401
10402 if (baton != NULL)
10403 v = dwarf2_evaluate_loc_desc (type, NULL,
10404 baton->data,
10405 baton->size,
10406 baton->per_cu);
10407 else if (bytes != NULL)
10408 {
10409 v = allocate_value (type);
10410 memcpy (value_contents_writeable (v), bytes,
10411 TYPE_LENGTH (type));
10412 }
10413 else
10414 v = value_from_longest (type, value);
10415
3e43a32a
MS
10416 /* Specify decimal so that we do not depend on
10417 the radix. */
98bfdba5
PA
10418 get_formatted_print_options (&opts, 'd');
10419 opts.raw = 1;
d7e74731 10420 value_print (v, &buf, &opts);
98bfdba5 10421 release_value (v);
98bfdba5
PA
10422 }
10423 }
10424
10425 die->building_fullname = 0;
10426
10427 if (!first)
10428 {
10429 /* Close the argument list, with a space if necessary
10430 (nested templates). */
d7e74731
PA
10431 if (!buf.empty () && buf.string ().back () == '>')
10432 buf.puts (" >");
98bfdba5 10433 else
d7e74731 10434 buf.puts (">");
98bfdba5
PA
10435 }
10436 }
10437
9c37b5ae 10438 /* For C++ methods, append formal parameter type
94af9270 10439 information, if PHYSNAME. */
6e70227d 10440
94af9270 10441 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10442 && cu->language == language_cplus)
94af9270
KS
10443 {
10444 struct type *type = read_type_die (die, cu);
10445
d7e74731 10446 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10447 &type_print_raw_options);
94af9270 10448
9c37b5ae 10449 if (cu->language == language_cplus)
94af9270 10450 {
60430eff
DJ
10451 /* Assume that an artificial first parameter is
10452 "this", but do not crash if it is not. RealView
10453 marks unnamed (and thus unused) parameters as
10454 artificial; there is no way to differentiate
10455 the two cases. */
94af9270
KS
10456 if (TYPE_NFIELDS (type) > 0
10457 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10458 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10459 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10460 0))))
d7e74731 10461 buf.puts (" const");
94af9270
KS
10462 }
10463 }
10464
d7e74731 10465 const std::string &intermediate_name = buf.string ();
94af9270
KS
10466
10467 if (cu->language == language_cplus)
34a68019 10468 canonical_name
322a8516 10469 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10470 &objfile->per_bfd->storage_obstack);
10471
10472 /* If we only computed INTERMEDIATE_NAME, or if
10473 INTERMEDIATE_NAME is already canonical, then we need to
10474 copy it to the appropriate obstack. */
322a8516 10475 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10476 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10477 intermediate_name);
34a68019
TT
10478 else
10479 name = canonical_name;
94af9270
KS
10480 }
10481 }
10482
10483 return name;
10484}
10485
0114d602
DJ
10486/* Return the fully qualified name of DIE, based on its DW_AT_name.
10487 If scope qualifiers are appropriate they will be added. The result
34a68019 10488 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10489 not have a name. NAME may either be from a previous call to
10490 dwarf2_name or NULL.
10491
9c37b5ae 10492 The output string will be canonicalized (if C++). */
0114d602
DJ
10493
10494static const char *
15d034d0 10495dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10496{
94af9270
KS
10497 return dwarf2_compute_name (name, die, cu, 0);
10498}
0114d602 10499
94af9270
KS
10500/* Construct a physname for the given DIE in CU. NAME may either be
10501 from a previous call to dwarf2_name or NULL. The result will be
10502 allocated on the objfile_objstack or NULL if the DIE does not have a
10503 name.
0114d602 10504
9c37b5ae 10505 The output string will be canonicalized (if C++). */
0114d602 10506
94af9270 10507static const char *
15d034d0 10508dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10509{
518817b3 10510 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10511 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10512 int need_copy = 1;
10513
10514 /* In this case dwarf2_compute_name is just a shortcut not building anything
10515 on its own. */
10516 if (!die_needs_namespace (die, cu))
10517 return dwarf2_compute_name (name, die, cu, 1);
10518
73b9be8b 10519 mangled = dw2_linkage_name (die, cu);
900e11f9 10520
e98c9e7c
TT
10521 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10522 See https://github.com/rust-lang/rust/issues/32925. */
10523 if (cu->language == language_rust && mangled != NULL
10524 && strchr (mangled, '{') != NULL)
10525 mangled = NULL;
10526
900e11f9
JK
10527 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10528 has computed. */
791afaa2 10529 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10530 if (mangled != NULL)
900e11f9 10531 {
900e11f9 10532
59cc4834
JB
10533 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10534 {
10535 /* Do nothing (do not demangle the symbol name). */
10536 }
10537 else if (cu->language == language_go)
a766d390 10538 {
5e2db402
TT
10539 /* This is a lie, but we already lie to the caller new_symbol.
10540 new_symbol assumes we return the mangled name.
a766d390 10541 This just undoes that lie until things are cleaned up. */
a766d390
DE
10542 }
10543 else
10544 {
0eb876f5
JB
10545 /* Use DMGL_RET_DROP for C++ template functions to suppress
10546 their return type. It is easier for GDB users to search
10547 for such functions as `name(params)' than `long name(params)'.
10548 In such case the minimal symbol names do not match the full
10549 symbol names but for template functions there is never a need
10550 to look up their definition from their declaration so
10551 the only disadvantage remains the minimal symbol variant
10552 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10553 demangled.reset (gdb_demangle (mangled,
10554 (DMGL_PARAMS | DMGL_ANSI
10555 | DMGL_RET_DROP)));
a766d390 10556 }
900e11f9 10557 if (demangled)
791afaa2 10558 canon = demangled.get ();
900e11f9
JK
10559 else
10560 {
10561 canon = mangled;
10562 need_copy = 0;
10563 }
10564 }
10565
10566 if (canon == NULL || check_physname)
10567 {
10568 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10569
10570 if (canon != NULL && strcmp (physname, canon) != 0)
10571 {
10572 /* It may not mean a bug in GDB. The compiler could also
10573 compute DW_AT_linkage_name incorrectly. But in such case
10574 GDB would need to be bug-to-bug compatible. */
10575
b98664d3 10576 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10577 "(from linkage <%s>) - DIE at %s [in module %s]"),
10578 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10579 objfile_name (objfile));
900e11f9
JK
10580
10581 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10582 is available here - over computed PHYSNAME. It is safer
10583 against both buggy GDB and buggy compilers. */
10584
10585 retval = canon;
10586 }
10587 else
10588 {
10589 retval = physname;
10590 need_copy = 0;
10591 }
10592 }
10593 else
10594 retval = canon;
10595
10596 if (need_copy)
021887d8 10597 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 10598
900e11f9 10599 return retval;
0114d602
DJ
10600}
10601
74921315
KS
10602/* Inspect DIE in CU for a namespace alias. If one exists, record
10603 a new symbol for it.
10604
10605 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10606
10607static int
10608read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10609{
10610 struct attribute *attr;
10611
10612 /* If the die does not have a name, this is not a namespace
10613 alias. */
10614 attr = dwarf2_attr (die, DW_AT_name, cu);
10615 if (attr != NULL)
10616 {
10617 int num;
10618 struct die_info *d = die;
10619 struct dwarf2_cu *imported_cu = cu;
10620
10621 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10622 keep inspecting DIEs until we hit the underlying import. */
10623#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10624 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10625 {
10626 attr = dwarf2_attr (d, DW_AT_import, cu);
10627 if (attr == NULL)
10628 break;
10629
10630 d = follow_die_ref (d, attr, &imported_cu);
10631 if (d->tag != DW_TAG_imported_declaration)
10632 break;
10633 }
10634
10635 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10636 {
b98664d3 10637 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10638 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10639 return 0;
10640 }
10641
10642 if (attr != NULL)
10643 {
10644 struct type *type;
9c541725 10645 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 10646
9c541725 10647 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10648 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10649 {
10650 /* This declaration is a global namespace alias. Add
10651 a symbol for it whose type is the aliased namespace. */
10652 new_symbol (die, type, cu);
10653 return 1;
10654 }
10655 }
10656 }
10657
10658 return 0;
10659}
10660
22cee43f 10661/* Return the using directives repository (global or local?) to use in the
804d2729 10662 current context for CU.
22cee43f
PMR
10663
10664 For Ada, imported declarations can materialize renamings, which *may* be
10665 global. However it is impossible (for now?) in DWARF to distinguish
10666 "external" imported declarations and "static" ones. As all imported
10667 declarations seem to be static in all other languages, make them all CU-wide
10668 global only in Ada. */
10669
10670static struct using_direct **
804d2729 10671using_directives (struct dwarf2_cu *cu)
22cee43f 10672{
c24bdb02
KS
10673 if (cu->language == language_ada
10674 && cu->get_builder ()->outermost_context_p ())
10675 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10676 else
c24bdb02 10677 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10678}
10679
27aa8d6a
SW
10680/* Read the import statement specified by the given die and record it. */
10681
10682static void
10683read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10684{
518817b3 10685 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10686 struct attribute *import_attr;
32019081 10687 struct die_info *imported_die, *child_die;
de4affc9 10688 struct dwarf2_cu *imported_cu;
27aa8d6a 10689 const char *imported_name;
794684b6 10690 const char *imported_name_prefix;
13387711
SW
10691 const char *canonical_name;
10692 const char *import_alias;
10693 const char *imported_declaration = NULL;
794684b6 10694 const char *import_prefix;
eb1e02fd 10695 std::vector<const char *> excludes;
13387711 10696
27aa8d6a
SW
10697 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10698 if (import_attr == NULL)
10699 {
b98664d3 10700 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10701 dwarf_tag_name (die->tag));
10702 return;
10703 }
10704
de4affc9
CC
10705 imported_cu = cu;
10706 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10707 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10708 if (imported_name == NULL)
10709 {
10710 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10711
10712 The import in the following code:
10713 namespace A
10714 {
10715 typedef int B;
10716 }
10717
10718 int main ()
10719 {
10720 using A::B;
10721 B b;
10722 return b;
10723 }
10724
10725 ...
10726 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10727 <52> DW_AT_decl_file : 1
10728 <53> DW_AT_decl_line : 6
10729 <54> DW_AT_import : <0x75>
10730 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10731 <59> DW_AT_name : B
10732 <5b> DW_AT_decl_file : 1
10733 <5c> DW_AT_decl_line : 2
10734 <5d> DW_AT_type : <0x6e>
10735 ...
10736 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10737 <76> DW_AT_byte_size : 4
10738 <77> DW_AT_encoding : 5 (signed)
10739
10740 imports the wrong die ( 0x75 instead of 0x58 ).
10741 This case will be ignored until the gcc bug is fixed. */
10742 return;
10743 }
10744
82856980
SW
10745 /* Figure out the local name after import. */
10746 import_alias = dwarf2_name (die, cu);
27aa8d6a 10747
794684b6
SW
10748 /* Figure out where the statement is being imported to. */
10749 import_prefix = determine_prefix (die, cu);
10750
10751 /* Figure out what the scope of the imported die is and prepend it
10752 to the name of the imported die. */
de4affc9 10753 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10754
f55ee35c
JK
10755 if (imported_die->tag != DW_TAG_namespace
10756 && imported_die->tag != DW_TAG_module)
794684b6 10757 {
13387711
SW
10758 imported_declaration = imported_name;
10759 canonical_name = imported_name_prefix;
794684b6 10760 }
13387711 10761 else if (strlen (imported_name_prefix) > 0)
12aaed36 10762 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10763 imported_name_prefix,
10764 (cu->language == language_d ? "." : "::"),
10765 imported_name, (char *) NULL);
13387711
SW
10766 else
10767 canonical_name = imported_name;
794684b6 10768
32019081
JK
10769 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10770 for (child_die = die->child; child_die && child_die->tag;
10771 child_die = sibling_die (child_die))
10772 {
10773 /* DWARF-4: A Fortran use statement with a “rename list” may be
10774 represented by an imported module entry with an import attribute
10775 referring to the module and owned entries corresponding to those
10776 entities that are renamed as part of being imported. */
10777
10778 if (child_die->tag != DW_TAG_imported_declaration)
10779 {
b98664d3 10780 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10781 "- DIE at %s [in module %s]"),
10782 sect_offset_str (child_die->sect_off),
10783 objfile_name (objfile));
32019081
JK
10784 continue;
10785 }
10786
10787 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10788 if (import_attr == NULL)
10789 {
b98664d3 10790 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10791 dwarf_tag_name (child_die->tag));
10792 continue;
10793 }
10794
10795 imported_cu = cu;
10796 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10797 &imported_cu);
10798 imported_name = dwarf2_name (imported_die, imported_cu);
10799 if (imported_name == NULL)
10800 {
b98664d3 10801 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10802 "imported name - DIE at %s [in module %s]"),
10803 sect_offset_str (child_die->sect_off),
10804 objfile_name (objfile));
32019081
JK
10805 continue;
10806 }
10807
eb1e02fd 10808 excludes.push_back (imported_name);
32019081
JK
10809
10810 process_die (child_die, cu);
10811 }
10812
804d2729 10813 add_using_directive (using_directives (cu),
22cee43f
PMR
10814 import_prefix,
10815 canonical_name,
10816 import_alias,
10817 imported_declaration,
10818 excludes,
10819 0,
10820 &objfile->objfile_obstack);
27aa8d6a
SW
10821}
10822
5230b05a
WT
10823/* ICC<14 does not output the required DW_AT_declaration on incomplete
10824 types, but gives them a size of zero. Starting with version 14,
10825 ICC is compatible with GCC. */
10826
9068261f 10827static bool
5230b05a
WT
10828producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10829{
10830 if (!cu->checked_producer)
10831 check_producer (cu);
10832
10833 return cu->producer_is_icc_lt_14;
10834}
10835
eb77c9df
AB
10836/* ICC generates a DW_AT_type for C void functions. This was observed on
10837 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10838 which says that void functions should not have a DW_AT_type. */
10839
10840static bool
10841producer_is_icc (struct dwarf2_cu *cu)
10842{
10843 if (!cu->checked_producer)
10844 check_producer (cu);
10845
10846 return cu->producer_is_icc;
10847}
10848
1b80a9fa
JK
10849/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10850 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10851 this, it was first present in GCC release 4.3.0. */
10852
9068261f 10853static bool
1b80a9fa
JK
10854producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10855{
10856 if (!cu->checked_producer)
10857 check_producer (cu);
10858
10859 return cu->producer_is_gcc_lt_4_3;
10860}
10861
d721ba37
PA
10862static file_and_directory
10863find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10864{
d721ba37
PA
10865 file_and_directory res;
10866
9291a0cd
TT
10867 /* Find the filename. Do not use dwarf2_name here, since the filename
10868 is not a source language identifier. */
d721ba37
PA
10869 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10870 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10871
d721ba37
PA
10872 if (res.comp_dir == NULL
10873 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10874 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10875 {
d721ba37
PA
10876 res.comp_dir_storage = ldirname (res.name);
10877 if (!res.comp_dir_storage.empty ())
10878 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10879 }
d721ba37 10880 if (res.comp_dir != NULL)
9291a0cd
TT
10881 {
10882 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10883 directory, get rid of it. */
d721ba37 10884 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10885
d721ba37
PA
10886 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10887 res.comp_dir = cp + 1;
9291a0cd
TT
10888 }
10889
d721ba37
PA
10890 if (res.name == NULL)
10891 res.name = "<unknown>";
10892
10893 return res;
9291a0cd
TT
10894}
10895
f4dc4d17
DE
10896/* Handle DW_AT_stmt_list for a compilation unit.
10897 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10898 COMP_DIR is the compilation directory. LOWPC is passed to
10899 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10900
10901static void
10902handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10903 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10904{
518817b3
SM
10905 struct dwarf2_per_objfile *dwarf2_per_objfile
10906 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10907 struct attribute *attr;
527f3840
JK
10908 struct line_header line_header_local;
10909 hashval_t line_header_local_hash;
527f3840
JK
10910 void **slot;
10911 int decode_mapping;
2ab95328 10912
f4dc4d17
DE
10913 gdb_assert (! cu->per_cu->is_debug_types);
10914
2ab95328 10915 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10916 if (attr == NULL)
10917 return;
10918
9c541725 10919 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10920
10921 /* The line header hash table is only created if needed (it exists to
10922 prevent redundant reading of the line table for partial_units).
10923 If we're given a partial_unit, we'll need it. If we're given a
10924 compile_unit, then use the line header hash table if it's already
10925 created, but don't create one just yet. */
10926
10927 if (dwarf2_per_objfile->line_header_hash == NULL
10928 && die->tag == DW_TAG_partial_unit)
2ab95328 10929 {
527f3840 10930 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10931 .reset (htab_create_alloc (127, line_header_hash_voidp,
10932 line_header_eq_voidp,
10933 free_line_header_voidp,
10934 xcalloc, xfree));
527f3840 10935 }
2ab95328 10936
9c541725 10937 line_header_local.sect_off = line_offset;
527f3840
JK
10938 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10939 line_header_local_hash = line_header_hash (&line_header_local);
10940 if (dwarf2_per_objfile->line_header_hash != NULL)
10941 {
d15acc42 10942 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10943 &line_header_local,
10944 line_header_local_hash, NO_INSERT);
10945
10946 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10947 is not present in *SLOT (since if there is something in *SLOT then
10948 it will be for a partial_unit). */
10949 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10950 {
527f3840 10951 gdb_assert (*slot != NULL);
9a3c8263 10952 cu->line_header = (struct line_header *) *slot;
527f3840 10953 return;
dee91e82 10954 }
2ab95328 10955 }
527f3840
JK
10956
10957 /* dwarf_decode_line_header does not yet provide sufficient information.
10958 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10959 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10960 if (lh == NULL)
527f3840 10961 return;
4c8aa72d
PA
10962
10963 cu->line_header = lh.release ();
10964 cu->line_header_die_owner = die;
527f3840
JK
10965
10966 if (dwarf2_per_objfile->line_header_hash == NULL)
10967 slot = NULL;
10968 else
10969 {
d15acc42 10970 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10971 &line_header_local,
10972 line_header_local_hash, INSERT);
10973 gdb_assert (slot != NULL);
10974 }
10975 if (slot != NULL && *slot == NULL)
10976 {
10977 /* This newly decoded line number information unit will be owned
10978 by line_header_hash hash table. */
10979 *slot = cu->line_header;
4c8aa72d 10980 cu->line_header_die_owner = NULL;
527f3840
JK
10981 }
10982 else
10983 {
10984 /* We cannot free any current entry in (*slot) as that struct line_header
10985 may be already used by multiple CUs. Create only temporary decoded
10986 line_header for this CU - it may happen at most once for each line
10987 number information unit. And if we're not using line_header_hash
10988 then this is what we want as well. */
10989 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10990 }
10991 decode_mapping = (die->tag != DW_TAG_partial_unit);
10992 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10993 decode_mapping);
fff8551c 10994
2ab95328
TT
10995}
10996
95554aad 10997/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10998
c906108c 10999static void
e7c27a73 11000read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11001{
518817b3
SM
11002 struct dwarf2_per_objfile *dwarf2_per_objfile
11003 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11004 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11005 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11006 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11007 CORE_ADDR highpc = ((CORE_ADDR) 0);
11008 struct attribute *attr;
c906108c 11009 struct die_info *child_die;
e142c38c 11010 CORE_ADDR baseaddr;
6e70227d 11011
380618d6 11012 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11013 baseaddr = objfile->text_section_offset ();
c906108c 11014
fae299cd 11015 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11016
11017 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11018 from finish_block. */
2acceee2 11019 if (lowpc == ((CORE_ADDR) -1))
c906108c 11020 lowpc = highpc;
3e29f34a 11021 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11022
d721ba37 11023 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11024
f4b8a18d
KW
11025 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11026 standardised yet. As a workaround for the language detection we fall
11027 back to the DW_AT_producer string. */
11028 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11029 cu->language = language_opencl;
11030
3019eac3
DE
11031 /* Similar hack for Go. */
11032 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11033 set_cu_language (DW_LANG_Go, cu);
11034
c24bdb02 11035 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11036
11037 /* Decode line number information if present. We do this before
11038 processing child DIEs, so that the line header table is available
11039 for DW_AT_decl_file. */
d721ba37 11040 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11041
11042 /* Process all dies in compilation unit. */
11043 if (die->child != NULL)
11044 {
11045 child_die = die->child;
11046 while (child_die && child_die->tag)
11047 {
11048 process_die (child_die, cu);
11049 child_die = sibling_die (child_die);
11050 }
11051 }
11052
11053 /* Decode macro information, if present. Dwarf 2 macro information
11054 refers to information in the line number info statement program
11055 header, so we can only read it if we've read the header
11056 successfully. */
0af92d60
JK
11057 attr = dwarf2_attr (die, DW_AT_macros, cu);
11058 if (attr == NULL)
11059 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11060 if (attr && cu->line_header)
11061 {
11062 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11063 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11064
43f3e411 11065 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11066 }
11067 else
11068 {
11069 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11070 if (attr && cu->line_header)
11071 {
11072 unsigned int macro_offset = DW_UNSND (attr);
11073
43f3e411 11074 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11075 }
11076 }
3019eac3
DE
11077}
11078
c24bdb02
KS
11079void
11080dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11081{
f4dc4d17
DE
11082 struct type_unit_group *tu_group;
11083 int first_time;
3019eac3 11084 struct attribute *attr;
9c541725 11085 unsigned int i;
0186c6a7 11086 struct signatured_type *sig_type;
3019eac3 11087
f4dc4d17 11088 gdb_assert (per_cu->is_debug_types);
0186c6a7 11089 sig_type = (struct signatured_type *) per_cu;
3019eac3 11090
c24bdb02 11091 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11092
f4dc4d17 11093 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11094 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11095 if (sig_type->type_unit_group == NULL)
c24bdb02 11096 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11097 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11098
11099 /* If we've already processed this stmt_list there's no real need to
11100 do it again, we could fake it and just recreate the part we need
11101 (file name,index -> symtab mapping). If data shows this optimization
11102 is useful we can do it then. */
43f3e411 11103 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11104
11105 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11106 debug info. */
fff8551c 11107 line_header_up lh;
f4dc4d17 11108 if (attr != NULL)
3019eac3 11109 {
9c541725 11110 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11111 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11112 }
11113 if (lh == NULL)
11114 {
11115 if (first_time)
c24bdb02 11116 start_symtab ("", NULL, 0);
f4dc4d17
DE
11117 else
11118 {
11119 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11120 gdb_assert (m_builder == nullptr);
804d2729 11121 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11122 m_builder.reset (new struct buildsym_compunit
11123 (COMPUNIT_OBJFILE (cust), "",
11124 COMPUNIT_DIRNAME (cust),
11125 compunit_language (cust),
11126 0, cust));
f4dc4d17 11127 }
f4dc4d17 11128 return;
3019eac3
DE
11129 }
11130
c24bdb02
KS
11131 line_header = lh.release ();
11132 line_header_die_owner = die;
3019eac3 11133
f4dc4d17
DE
11134 if (first_time)
11135 {
c24bdb02 11136 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11137
1fd60fc0
DE
11138 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11139 still initializing it, and our caller (a few levels up)
11140 process_full_type_unit still needs to know if this is the first
11141 time. */
11142
7ba99d21 11143 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11144 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11145 line_header->file_names_size ());
3019eac3 11146
7ba99d21
AT
11147 auto &file_names = line_header->file_names ();
11148 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11149 {
7ba99d21 11150 file_entry &fe = file_names[i];
c24bdb02
KS
11151 dwarf2_start_subfile (this, fe.name,
11152 fe.include_dir (line_header));
11153 buildsym_compunit *b = get_builder ();
11154 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11155 {
4c8aa72d
PA
11156 /* NOTE: start_subfile will recognize when it's been
11157 passed a file it has already seen. So we can't
11158 assume there's a simple mapping from
11159 cu->line_header->file_names to subfiles, plus
11160 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11161 b->get_current_subfile ()->symtab
11162 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11163 }
11164
c24bdb02 11165 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11166 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11167 }
11168 }
11169 else
3019eac3 11170 {
c24bdb02 11171 gdb_assert (m_builder == nullptr);
804d2729 11172 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11173 m_builder.reset (new struct buildsym_compunit
11174 (COMPUNIT_OBJFILE (cust), "",
11175 COMPUNIT_DIRNAME (cust),
11176 compunit_language (cust),
11177 0, cust));
f4dc4d17 11178
7ba99d21
AT
11179 auto &file_names = line_header->file_names ();
11180 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11181 {
7ba99d21 11182 file_entry &fe = file_names[i];
4c8aa72d 11183 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11184 }
3019eac3
DE
11185 }
11186
f4dc4d17
DE
11187 /* The main symtab is allocated last. Type units don't have DW_AT_name
11188 so they don't have a "real" (so to speak) symtab anyway.
11189 There is later code that will assign the main symtab to all symbols
11190 that don't have one. We need to handle the case of a symbol with a
11191 missing symtab (DW_AT_decl_file) anyway. */
11192}
3019eac3 11193
f4dc4d17
DE
11194/* Process DW_TAG_type_unit.
11195 For TUs we want to skip the first top level sibling if it's not the
11196 actual type being defined by this TU. In this case the first top
11197 level sibling is there to provide context only. */
3019eac3 11198
f4dc4d17
DE
11199static void
11200read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11201{
11202 struct die_info *child_die;
3019eac3 11203
f4dc4d17
DE
11204 prepare_one_comp_unit (cu, die, language_minimal);
11205
11206 /* Initialize (or reinitialize) the machinery for building symtabs.
11207 We do this before processing child DIEs, so that the line header table
11208 is available for DW_AT_decl_file. */
c24bdb02 11209 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11210
11211 if (die->child != NULL)
11212 {
11213 child_die = die->child;
11214 while (child_die && child_die->tag)
11215 {
11216 process_die (child_die, cu);
11217 child_die = sibling_die (child_die);
11218 }
11219 }
3019eac3
DE
11220}
11221\f
80626a55
DE
11222/* DWO/DWP files.
11223
11224 http://gcc.gnu.org/wiki/DebugFission
11225 http://gcc.gnu.org/wiki/DebugFissionDWP
11226
11227 To simplify handling of both DWO files ("object" files with the DWARF info)
11228 and DWP files (a file with the DWOs packaged up into one file), we treat
11229 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11230
11231static hashval_t
11232hash_dwo_file (const void *item)
11233{
9a3c8263 11234 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11235 hashval_t hash;
3019eac3 11236
a2ce51a0
DE
11237 hash = htab_hash_string (dwo_file->dwo_name);
11238 if (dwo_file->comp_dir != NULL)
11239 hash += htab_hash_string (dwo_file->comp_dir);
11240 return hash;
3019eac3
DE
11241}
11242
11243static int
11244eq_dwo_file (const void *item_lhs, const void *item_rhs)
11245{
9a3c8263
SM
11246 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11247 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11248
a2ce51a0
DE
11249 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11250 return 0;
11251 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11252 return lhs->comp_dir == rhs->comp_dir;
11253 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11254}
11255
11256/* Allocate a hash table for DWO files. */
11257
51ac9db5 11258static htab_up
ed2dc618 11259allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11260{
51ac9db5
SM
11261 auto delete_dwo_file = [] (void *item)
11262 {
11263 struct dwo_file *dwo_file = (struct dwo_file *) item;
11264
11265 delete dwo_file;
11266 };
11267
bc68fb19
TT
11268 return htab_up (htab_create_alloc (41,
11269 hash_dwo_file,
11270 eq_dwo_file,
11271 delete_dwo_file,
11272 xcalloc, xfree));
3019eac3
DE
11273}
11274
80626a55
DE
11275/* Lookup DWO file DWO_NAME. */
11276
11277static void **
ed2dc618
SM
11278lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11279 const char *dwo_name,
11280 const char *comp_dir)
80626a55
DE
11281{
11282 struct dwo_file find_entry;
11283 void **slot;
11284
11285 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11286 dwarf2_per_objfile->dwo_files
11287 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11288
0ac5b59e
DE
11289 find_entry.dwo_name = dwo_name;
11290 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11291 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11292 INSERT);
80626a55
DE
11293
11294 return slot;
11295}
11296
3019eac3
DE
11297static hashval_t
11298hash_dwo_unit (const void *item)
11299{
9a3c8263 11300 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11301
11302 /* This drops the top 32 bits of the id, but is ok for a hash. */
11303 return dwo_unit->signature;
11304}
11305
11306static int
11307eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11308{
9a3c8263
SM
11309 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11310 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11311
11312 /* The signature is assumed to be unique within the DWO file.
11313 So while object file CU dwo_id's always have the value zero,
11314 that's OK, assuming each object file DWO file has only one CU,
11315 and that's the rule for now. */
11316 return lhs->signature == rhs->signature;
11317}
11318
11319/* Allocate a hash table for DWO CUs,TUs.
11320 There is one of these tables for each of CUs,TUs for each DWO file. */
11321
b0b6a987 11322static htab_up
3019eac3
DE
11323allocate_dwo_unit_table (struct objfile *objfile)
11324{
11325 /* Start out with a pretty small number.
11326 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11327 return htab_up (htab_create_alloc (3,
11328 hash_dwo_unit,
11329 eq_dwo_unit,
11330 NULL, xcalloc, xfree));
3019eac3
DE
11331}
11332
19c3d4c9 11333/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11334
11335static void
19c3d4c9
DE
11336create_dwo_cu_reader (const struct die_reader_specs *reader,
11337 const gdb_byte *info_ptr,
11338 struct die_info *comp_unit_die,
c0ab21c2
TT
11339 struct dwo_file *dwo_file,
11340 struct dwo_unit *dwo_unit)
3019eac3
DE
11341{
11342 struct dwarf2_cu *cu = reader->cu;
9c541725 11343 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11344 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11345
a084a2a6
AT
11346 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11347 if (!signature.has_value ())
3019eac3 11348 {
b98664d3 11349 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11350 " its dwo_id [in module %s]"),
9d8780f0 11351 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11352 return;
11353 }
11354
3019eac3 11355 dwo_unit->dwo_file = dwo_file;
a084a2a6 11356 dwo_unit->signature = *signature;
8a0459fd 11357 dwo_unit->section = section;
9c541725 11358 dwo_unit->sect_off = sect_off;
3019eac3
DE
11359 dwo_unit->length = cu->per_cu->length;
11360
b4f54984 11361 if (dwarf_read_debug)
9d8780f0
SM
11362 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11363 sect_offset_str (sect_off),
9c541725 11364 hex_string (dwo_unit->signature));
3019eac3
DE
11365}
11366
33c5cd75 11367/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11368 Note: This function processes DWO files only, not DWP files. */
3019eac3 11369
33c5cd75 11370static void
ed2dc618 11371create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11372 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11373 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11374{
11375 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11376 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11377
96b79293 11378 section.read (objfile);
33c5cd75 11379 info_ptr = section.buffer;
3019eac3
DE
11380
11381 if (info_ptr == NULL)
33c5cd75 11382 return;
3019eac3 11383
b4f54984 11384 if (dwarf_read_debug)
19c3d4c9
DE
11385 {
11386 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11387 section.get_name (),
11388 section.get_file_name ());
19c3d4c9 11389 }
3019eac3 11390
33c5cd75 11391 end_ptr = info_ptr + section.size;
3019eac3
DE
11392 while (info_ptr < end_ptr)
11393 {
11394 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11395 struct dwo_unit read_unit {};
33c5cd75
DB
11396 struct dwo_unit *dwo_unit;
11397 void **slot;
11398 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11399
11400 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11401 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11402 per_cu.is_debug_types = 0;
33c5cd75
DB
11403 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11404 per_cu.section = &section;
11405
c0ab21c2
TT
11406 cutu_reader reader (&per_cu, cu, &dwo_file);
11407 if (!reader.dummy_p)
11408 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11409 &dwo_file, &read_unit);
33c5cd75
DB
11410 info_ptr += per_cu.length;
11411
11412 // If the unit could not be parsed, skip it.
c0ab21c2 11413 if (read_unit.dwo_file == NULL)
33c5cd75 11414 continue;
3019eac3 11415
33c5cd75
DB
11416 if (cus_htab == NULL)
11417 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11418
33c5cd75 11419 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11420 *dwo_unit = read_unit;
b0b6a987 11421 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11422 gdb_assert (slot != NULL);
11423 if (*slot != NULL)
19c3d4c9 11424 {
33c5cd75
DB
11425 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11426 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11427
b98664d3 11428 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11429 " the entry at offset %s, signature %s"),
11430 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11431 hex_string (dwo_unit->signature));
19c3d4c9 11432 }
33c5cd75 11433 *slot = (void *)dwo_unit;
3019eac3 11434 }
3019eac3
DE
11435}
11436
80626a55
DE
11437/* DWP file .debug_{cu,tu}_index section format:
11438 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11439
d2415c6c
DE
11440 DWP Version 1:
11441
80626a55
DE
11442 Both index sections have the same format, and serve to map a 64-bit
11443 signature to a set of section numbers. Each section begins with a header,
11444 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11445 indexes, and a pool of 32-bit section numbers. The index sections will be
11446 aligned at 8-byte boundaries in the file.
11447
d2415c6c
DE
11448 The index section header consists of:
11449
11450 V, 32 bit version number
11451 -, 32 bits unused
11452 N, 32 bit number of compilation units or type units in the index
11453 M, 32 bit number of slots in the hash table
80626a55 11454
d2415c6c 11455 Numbers are recorded using the byte order of the application binary.
80626a55 11456
d2415c6c
DE
11457 The hash table begins at offset 16 in the section, and consists of an array
11458 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11459 order of the application binary). Unused slots in the hash table are 0.
11460 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11461
d2415c6c
DE
11462 The parallel table begins immediately after the hash table
11463 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11464 array of 32-bit indexes (using the byte order of the application binary),
11465 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11466 table contains a 32-bit index into the pool of section numbers. For unused
11467 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11468
73869dc2
DE
11469 The pool of section numbers begins immediately following the hash table
11470 (at offset 16 + 12 * M from the beginning of the section). The pool of
11471 section numbers consists of an array of 32-bit words (using the byte order
11472 of the application binary). Each item in the array is indexed starting
11473 from 0. The hash table entry provides the index of the first section
11474 number in the set. Additional section numbers in the set follow, and the
11475 set is terminated by a 0 entry (section number 0 is not used in ELF).
11476
11477 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11478 section must be the first entry in the set, and the .debug_abbrev.dwo must
11479 be the second entry. Other members of the set may follow in any order.
11480
11481 ---
11482
11483 DWP Version 2:
11484
11485 DWP Version 2 combines all the .debug_info, etc. sections into one,
11486 and the entries in the index tables are now offsets into these sections.
11487 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11488 section.
11489
11490 Index Section Contents:
11491 Header
11492 Hash Table of Signatures dwp_hash_table.hash_table
11493 Parallel Table of Indices dwp_hash_table.unit_table
11494 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11495 Table of Section Sizes dwp_hash_table.v2.sizes
11496
11497 The index section header consists of:
11498
11499 V, 32 bit version number
11500 L, 32 bit number of columns in the table of section offsets
11501 N, 32 bit number of compilation units or type units in the index
11502 M, 32 bit number of slots in the hash table
11503
11504 Numbers are recorded using the byte order of the application binary.
11505
11506 The hash table has the same format as version 1.
11507 The parallel table of indices has the same format as version 1,
11508 except that the entries are origin-1 indices into the table of sections
11509 offsets and the table of section sizes.
11510
11511 The table of offsets begins immediately following the parallel table
11512 (at offset 16 + 12 * M from the beginning of the section). The table is
11513 a two-dimensional array of 32-bit words (using the byte order of the
11514 application binary), with L columns and N+1 rows, in row-major order.
11515 Each row in the array is indexed starting from 0. The first row provides
11516 a key to the remaining rows: each column in this row provides an identifier
11517 for a debug section, and the offsets in the same column of subsequent rows
11518 refer to that section. The section identifiers are:
11519
11520 DW_SECT_INFO 1 .debug_info.dwo
11521 DW_SECT_TYPES 2 .debug_types.dwo
11522 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11523 DW_SECT_LINE 4 .debug_line.dwo
11524 DW_SECT_LOC 5 .debug_loc.dwo
11525 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11526 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11527 DW_SECT_MACRO 8 .debug_macro.dwo
11528
11529 The offsets provided by the CU and TU index sections are the base offsets
11530 for the contributions made by each CU or TU to the corresponding section
11531 in the package file. Each CU and TU header contains an abbrev_offset
11532 field, used to find the abbreviations table for that CU or TU within the
11533 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11534 be interpreted as relative to the base offset given in the index section.
11535 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11536 should be interpreted as relative to the base offset for .debug_line.dwo,
11537 and offsets into other debug sections obtained from DWARF attributes should
11538 also be interpreted as relative to the corresponding base offset.
11539
11540 The table of sizes begins immediately following the table of offsets.
11541 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11542 with L columns and N rows, in row-major order. Each row in the array is
11543 indexed starting from 1 (row 0 is shared by the two tables).
11544
11545 ---
11546
11547 Hash table lookup is handled the same in version 1 and 2:
11548
11549 We assume that N and M will not exceed 2^32 - 1.
11550 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11551
d2415c6c
DE
11552 Given a 64-bit compilation unit signature or a type signature S, an entry
11553 in the hash table is located as follows:
80626a55 11554
d2415c6c
DE
11555 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11556 the low-order k bits all set to 1.
80626a55 11557
d2415c6c 11558 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11559
d2415c6c
DE
11560 3) If the hash table entry at index H matches the signature, use that
11561 entry. If the hash table entry at index H is unused (all zeroes),
11562 terminate the search: the signature is not present in the table.
80626a55 11563
d2415c6c 11564 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11565
d2415c6c 11566 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11567 to stop at an unused slot or find the match. */
80626a55
DE
11568
11569/* Create a hash table to map DWO IDs to their CU/TU entry in
11570 .debug_{info,types}.dwo in DWP_FILE.
11571 Returns NULL if there isn't one.
11572 Note: This function processes DWP files only, not DWO files. */
11573
11574static struct dwp_hash_table *
ed2dc618
SM
11575create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11576 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11577{
11578 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11579 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11580 const gdb_byte *index_ptr, *index_end;
80626a55 11581 struct dwarf2_section_info *index;
73869dc2 11582 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11583 struct dwp_hash_table *htab;
11584
11585 if (is_debug_types)
11586 index = &dwp_file->sections.tu_index;
11587 else
11588 index = &dwp_file->sections.cu_index;
11589
96b79293 11590 if (index->empty ())
80626a55 11591 return NULL;
96b79293 11592 index->read (objfile);
80626a55
DE
11593
11594 index_ptr = index->buffer;
11595 index_end = index_ptr + index->size;
11596
11597 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11598 index_ptr += 4;
11599 if (version == 2)
11600 nr_columns = read_4_bytes (dbfd, index_ptr);
11601 else
11602 nr_columns = 0;
11603 index_ptr += 4;
80626a55
DE
11604 nr_units = read_4_bytes (dbfd, index_ptr);
11605 index_ptr += 4;
11606 nr_slots = read_4_bytes (dbfd, index_ptr);
11607 index_ptr += 4;
11608
73869dc2 11609 if (version != 1 && version != 2)
80626a55 11610 {
21aa081e 11611 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11612 " [in module %s]"),
21aa081e 11613 pulongest (version), dwp_file->name);
80626a55
DE
11614 }
11615 if (nr_slots != (nr_slots & -nr_slots))
11616 {
21aa081e 11617 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11618 " is not power of 2 [in module %s]"),
21aa081e 11619 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11620 }
11621
11622 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11623 htab->version = version;
11624 htab->nr_columns = nr_columns;
80626a55
DE
11625 htab->nr_units = nr_units;
11626 htab->nr_slots = nr_slots;
11627 htab->hash_table = index_ptr;
11628 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11629
11630 /* Exit early if the table is empty. */
11631 if (nr_slots == 0 || nr_units == 0
11632 || (version == 2 && nr_columns == 0))
11633 {
11634 /* All must be zero. */
11635 if (nr_slots != 0 || nr_units != 0
11636 || (version == 2 && nr_columns != 0))
11637 {
b98664d3 11638 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11639 " all zero [in modules %s]"),
11640 dwp_file->name);
11641 }
11642 return htab;
11643 }
11644
11645 if (version == 1)
11646 {
11647 htab->section_pool.v1.indices =
11648 htab->unit_table + sizeof (uint32_t) * nr_slots;
11649 /* It's harder to decide whether the section is too small in v1.
11650 V1 is deprecated anyway so we punt. */
11651 }
11652 else
11653 {
11654 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11655 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11656 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11657 /* Reverse map for error checking. */
11658 int ids_seen[DW_SECT_MAX + 1];
11659 int i;
11660
11661 if (nr_columns < 2)
11662 {
11663 error (_("Dwarf Error: bad DWP hash table, too few columns"
11664 " in section table [in module %s]"),
11665 dwp_file->name);
11666 }
11667 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11668 {
11669 error (_("Dwarf Error: bad DWP hash table, too many columns"
11670 " in section table [in module %s]"),
11671 dwp_file->name);
11672 }
04fd5eed
GB
11673 memset (ids, 255, sizeof_ids);
11674 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11675 for (i = 0; i < nr_columns; ++i)
11676 {
11677 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11678
11679 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11680 {
11681 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11682 " in section table [in module %s]"),
11683 id, dwp_file->name);
11684 }
11685 if (ids_seen[id] != -1)
11686 {
11687 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11688 " id %d in section table [in module %s]"),
11689 id, dwp_file->name);
11690 }
11691 ids_seen[id] = i;
11692 ids[i] = id;
11693 }
11694 /* Must have exactly one info or types section. */
11695 if (((ids_seen[DW_SECT_INFO] != -1)
11696 + (ids_seen[DW_SECT_TYPES] != -1))
11697 != 1)
11698 {
11699 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11700 " DWO info/types section [in module %s]"),
11701 dwp_file->name);
11702 }
11703 /* Must have an abbrev section. */
11704 if (ids_seen[DW_SECT_ABBREV] == -1)
11705 {
11706 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11707 " section [in module %s]"),
11708 dwp_file->name);
11709 }
11710 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11711 htab->section_pool.v2.sizes =
11712 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11713 * nr_units * nr_columns);
11714 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11715 * nr_units * nr_columns))
11716 > index_end)
11717 {
11718 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11719 " [in module %s]"),
11720 dwp_file->name);
11721 }
11722 }
80626a55
DE
11723
11724 return htab;
11725}
11726
11727/* Update SECTIONS with the data from SECTP.
11728
11729 This function is like the other "locate" section routines that are
11730 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11731 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11732
11733 The result is non-zero for success, or zero if an error was found. */
11734
11735static int
73869dc2
DE
11736locate_v1_virtual_dwo_sections (asection *sectp,
11737 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11738{
11739 const struct dwop_section_names *names = &dwop_section_names;
11740
11741 if (section_is_p (sectp->name, &names->abbrev_dwo))
11742 {
11743 /* There can be only one. */
049412e3 11744 if (sections->abbrev.s.section != NULL)
80626a55 11745 return 0;
049412e3 11746 sections->abbrev.s.section = sectp;
fd361982 11747 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11748 }
11749 else if (section_is_p (sectp->name, &names->info_dwo)
11750 || section_is_p (sectp->name, &names->types_dwo))
11751 {
11752 /* There can be only one. */
049412e3 11753 if (sections->info_or_types.s.section != NULL)
80626a55 11754 return 0;
049412e3 11755 sections->info_or_types.s.section = sectp;
fd361982 11756 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11757 }
11758 else if (section_is_p (sectp->name, &names->line_dwo))
11759 {
11760 /* There can be only one. */
049412e3 11761 if (sections->line.s.section != NULL)
80626a55 11762 return 0;
049412e3 11763 sections->line.s.section = sectp;
fd361982 11764 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11765 }
11766 else if (section_is_p (sectp->name, &names->loc_dwo))
11767 {
11768 /* There can be only one. */
049412e3 11769 if (sections->loc.s.section != NULL)
80626a55 11770 return 0;
049412e3 11771 sections->loc.s.section = sectp;
fd361982 11772 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11773 }
11774 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11775 {
11776 /* There can be only one. */
049412e3 11777 if (sections->macinfo.s.section != NULL)
80626a55 11778 return 0;
049412e3 11779 sections->macinfo.s.section = sectp;
fd361982 11780 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11781 }
11782 else if (section_is_p (sectp->name, &names->macro_dwo))
11783 {
11784 /* There can be only one. */
049412e3 11785 if (sections->macro.s.section != NULL)
80626a55 11786 return 0;
049412e3 11787 sections->macro.s.section = sectp;
fd361982 11788 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11789 }
11790 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11791 {
11792 /* There can be only one. */
049412e3 11793 if (sections->str_offsets.s.section != NULL)
80626a55 11794 return 0;
049412e3 11795 sections->str_offsets.s.section = sectp;
fd361982 11796 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11797 }
11798 else
11799 {
11800 /* No other kind of section is valid. */
11801 return 0;
11802 }
11803
11804 return 1;
11805}
11806
73869dc2
DE
11807/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11808 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11809 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11810 This is for DWP version 1 files. */
80626a55
DE
11811
11812static struct dwo_unit *
ed2dc618
SM
11813create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11814 struct dwp_file *dwp_file,
73869dc2
DE
11815 uint32_t unit_index,
11816 const char *comp_dir,
11817 ULONGEST signature, int is_debug_types)
80626a55
DE
11818{
11819 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11820 const struct dwp_hash_table *dwp_htab =
11821 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11822 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11823 const char *kind = is_debug_types ? "TU" : "CU";
11824 struct dwo_file *dwo_file;
11825 struct dwo_unit *dwo_unit;
73869dc2 11826 struct virtual_v1_dwo_sections sections;
80626a55 11827 void **dwo_file_slot;
80626a55
DE
11828 int i;
11829
73869dc2
DE
11830 gdb_assert (dwp_file->version == 1);
11831
b4f54984 11832 if (dwarf_read_debug)
80626a55 11833 {
73869dc2 11834 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11835 kind,
73869dc2 11836 pulongest (unit_index), hex_string (signature),
80626a55
DE
11837 dwp_file->name);
11838 }
11839
19ac8c2e 11840 /* Fetch the sections of this DWO unit.
80626a55
DE
11841 Put a limit on the number of sections we look for so that bad data
11842 doesn't cause us to loop forever. */
11843
73869dc2 11844#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11845 (1 /* .debug_info or .debug_types */ \
11846 + 1 /* .debug_abbrev */ \
11847 + 1 /* .debug_line */ \
11848 + 1 /* .debug_loc */ \
11849 + 1 /* .debug_str_offsets */ \
19ac8c2e 11850 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11851 + 1 /* trailing zero */)
11852
11853 memset (&sections, 0, sizeof (sections));
80626a55 11854
73869dc2 11855 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11856 {
11857 asection *sectp;
11858 uint32_t section_nr =
11859 read_4_bytes (dbfd,
73869dc2
DE
11860 dwp_htab->section_pool.v1.indices
11861 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11862
11863 if (section_nr == 0)
11864 break;
11865 if (section_nr >= dwp_file->num_sections)
11866 {
11867 error (_("Dwarf Error: bad DWP hash table, section number too large"
11868 " [in module %s]"),
11869 dwp_file->name);
11870 }
11871
11872 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11873 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11874 {
11875 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11876 " [in module %s]"),
11877 dwp_file->name);
11878 }
11879 }
11880
11881 if (i < 2
96b79293
TT
11882 || sections.info_or_types.empty ()
11883 || sections.abbrev.empty ())
80626a55
DE
11884 {
11885 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11886 " [in module %s]"),
11887 dwp_file->name);
11888 }
73869dc2 11889 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11890 {
11891 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11892 " [in module %s]"),
11893 dwp_file->name);
11894 }
11895
11896 /* It's easier for the rest of the code if we fake a struct dwo_file and
11897 have dwo_unit "live" in that. At least for now.
11898
11899 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11900 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11901 file, we can combine them back into a virtual DWO file to save space
11902 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11903 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11904
791afaa2
TT
11905 std::string virtual_dwo_name =
11906 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11907 sections.abbrev.get_id (),
11908 sections.line.get_id (),
11909 sections.loc.get_id (),
11910 sections.str_offsets.get_id ());
80626a55 11911 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11912 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11913 virtual_dwo_name.c_str (),
11914 comp_dir);
80626a55
DE
11915 /* Create one if necessary. */
11916 if (*dwo_file_slot == NULL)
11917 {
b4f54984 11918 if (dwarf_read_debug)
80626a55
DE
11919 {
11920 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11921 virtual_dwo_name.c_str ());
80626a55 11922 }
51ac9db5 11923 dwo_file = new struct dwo_file;
efba19b0
TT
11924 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
11925 virtual_dwo_name);
0ac5b59e 11926 dwo_file->comp_dir = comp_dir;
80626a55
DE
11927 dwo_file->sections.abbrev = sections.abbrev;
11928 dwo_file->sections.line = sections.line;
11929 dwo_file->sections.loc = sections.loc;
11930 dwo_file->sections.macinfo = sections.macinfo;
11931 dwo_file->sections.macro = sections.macro;
11932 dwo_file->sections.str_offsets = sections.str_offsets;
11933 /* The "str" section is global to the entire DWP file. */
11934 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11935 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11936 there's no need to record it in dwo_file.
11937 Also, we can't simply record type sections in dwo_file because
11938 we record a pointer into the vector in dwo_unit. As we collect more
11939 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11940 for it, invalidating all copies of pointers into the previous
11941 contents. */
80626a55
DE
11942 *dwo_file_slot = dwo_file;
11943 }
11944 else
11945 {
b4f54984 11946 if (dwarf_read_debug)
80626a55
DE
11947 {
11948 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11949 virtual_dwo_name.c_str ());
80626a55 11950 }
9a3c8263 11951 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11952 }
80626a55
DE
11953
11954 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11955 dwo_unit->dwo_file = dwo_file;
11956 dwo_unit->signature = signature;
8d749320
SM
11957 dwo_unit->section =
11958 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11959 *dwo_unit->section = sections.info_or_types;
57d63ce2 11960 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11961
11962 return dwo_unit;
11963}
11964
73869dc2
DE
11965/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11966 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11967 piece within that section used by a TU/CU, return a virtual section
11968 of just that piece. */
11969
11970static struct dwarf2_section_info
ed2dc618
SM
11971create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11972 struct dwarf2_section_info *section,
73869dc2
DE
11973 bfd_size_type offset, bfd_size_type size)
11974{
11975 struct dwarf2_section_info result;
11976 asection *sectp;
11977
11978 gdb_assert (section != NULL);
11979 gdb_assert (!section->is_virtual);
11980
11981 memset (&result, 0, sizeof (result));
11982 result.s.containing_section = section;
dc4ccb6f 11983 result.is_virtual = true;
73869dc2
DE
11984
11985 if (size == 0)
11986 return result;
11987
96b79293 11988 sectp = section->get_bfd_section ();
73869dc2
DE
11989
11990 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11991 bounds of the real section. This is a pretty-rare event, so just
11992 flag an error (easier) instead of a warning and trying to cope. */
11993 if (sectp == NULL
fd361982 11994 || offset + size > bfd_section_size (sectp))
73869dc2 11995 {
73869dc2
DE
11996 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11997 " in section %s [in module %s]"),
fd361982 11998 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11999 objfile_name (dwarf2_per_objfile->objfile));
12000 }
12001
12002 result.virtual_offset = offset;
12003 result.size = size;
12004 return result;
12005}
12006
12007/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12008 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12009 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12010 This is for DWP version 2 files. */
12011
12012static struct dwo_unit *
ed2dc618
SM
12013create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12014 struct dwp_file *dwp_file,
73869dc2
DE
12015 uint32_t unit_index,
12016 const char *comp_dir,
12017 ULONGEST signature, int is_debug_types)
12018{
12019 struct objfile *objfile = dwarf2_per_objfile->objfile;
12020 const struct dwp_hash_table *dwp_htab =
12021 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12022 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12023 const char *kind = is_debug_types ? "TU" : "CU";
12024 struct dwo_file *dwo_file;
12025 struct dwo_unit *dwo_unit;
12026 struct virtual_v2_dwo_sections sections;
12027 void **dwo_file_slot;
73869dc2
DE
12028 int i;
12029
12030 gdb_assert (dwp_file->version == 2);
12031
b4f54984 12032 if (dwarf_read_debug)
73869dc2
DE
12033 {
12034 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12035 kind,
12036 pulongest (unit_index), hex_string (signature),
12037 dwp_file->name);
12038 }
12039
12040 /* Fetch the section offsets of this DWO unit. */
12041
12042 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12043
12044 for (i = 0; i < dwp_htab->nr_columns; ++i)
12045 {
12046 uint32_t offset = read_4_bytes (dbfd,
12047 dwp_htab->section_pool.v2.offsets
12048 + (((unit_index - 1) * dwp_htab->nr_columns
12049 + i)
12050 * sizeof (uint32_t)));
12051 uint32_t size = read_4_bytes (dbfd,
12052 dwp_htab->section_pool.v2.sizes
12053 + (((unit_index - 1) * dwp_htab->nr_columns
12054 + i)
12055 * sizeof (uint32_t)));
12056
12057 switch (dwp_htab->section_pool.v2.section_ids[i])
12058 {
12059 case DW_SECT_INFO:
12060 case DW_SECT_TYPES:
12061 sections.info_or_types_offset = offset;
12062 sections.info_or_types_size = size;
12063 break;
12064 case DW_SECT_ABBREV:
12065 sections.abbrev_offset = offset;
12066 sections.abbrev_size = size;
12067 break;
12068 case DW_SECT_LINE:
12069 sections.line_offset = offset;
12070 sections.line_size = size;
12071 break;
12072 case DW_SECT_LOC:
12073 sections.loc_offset = offset;
12074 sections.loc_size = size;
12075 break;
12076 case DW_SECT_STR_OFFSETS:
12077 sections.str_offsets_offset = offset;
12078 sections.str_offsets_size = size;
12079 break;
12080 case DW_SECT_MACINFO:
12081 sections.macinfo_offset = offset;
12082 sections.macinfo_size = size;
12083 break;
12084 case DW_SECT_MACRO:
12085 sections.macro_offset = offset;
12086 sections.macro_size = size;
12087 break;
12088 }
12089 }
12090
12091 /* It's easier for the rest of the code if we fake a struct dwo_file and
12092 have dwo_unit "live" in that. At least for now.
12093
12094 The DWP file can be made up of a random collection of CUs and TUs.
12095 However, for each CU + set of TUs that came from the same original DWO
12096 file, we can combine them back into a virtual DWO file to save space
12097 (fewer struct dwo_file objects to allocate). Remember that for really
12098 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12099
791afaa2
TT
12100 std::string virtual_dwo_name =
12101 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12102 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12103 (long) (sections.line_size ? sections.line_offset : 0),
12104 (long) (sections.loc_size ? sections.loc_offset : 0),
12105 (long) (sections.str_offsets_size
12106 ? sections.str_offsets_offset : 0));
73869dc2 12107 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12108 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12109 virtual_dwo_name.c_str (),
12110 comp_dir);
73869dc2
DE
12111 /* Create one if necessary. */
12112 if (*dwo_file_slot == NULL)
12113 {
b4f54984 12114 if (dwarf_read_debug)
73869dc2
DE
12115 {
12116 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12117 virtual_dwo_name.c_str ());
73869dc2 12118 }
51ac9db5 12119 dwo_file = new struct dwo_file;
efba19b0
TT
12120 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12121 virtual_dwo_name);
73869dc2
DE
12122 dwo_file->comp_dir = comp_dir;
12123 dwo_file->sections.abbrev =
ed2dc618 12124 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12125 sections.abbrev_offset, sections.abbrev_size);
12126 dwo_file->sections.line =
ed2dc618 12127 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12128 sections.line_offset, sections.line_size);
12129 dwo_file->sections.loc =
ed2dc618 12130 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12131 sections.loc_offset, sections.loc_size);
12132 dwo_file->sections.macinfo =
ed2dc618 12133 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12134 sections.macinfo_offset, sections.macinfo_size);
12135 dwo_file->sections.macro =
ed2dc618 12136 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12137 sections.macro_offset, sections.macro_size);
12138 dwo_file->sections.str_offsets =
ed2dc618
SM
12139 create_dwp_v2_section (dwarf2_per_objfile,
12140 &dwp_file->sections.str_offsets,
73869dc2
DE
12141 sections.str_offsets_offset,
12142 sections.str_offsets_size);
12143 /* The "str" section is global to the entire DWP file. */
12144 dwo_file->sections.str = dwp_file->sections.str;
12145 /* The info or types section is assigned below to dwo_unit,
12146 there's no need to record it in dwo_file.
12147 Also, we can't simply record type sections in dwo_file because
12148 we record a pointer into the vector in dwo_unit. As we collect more
12149 types we'll grow the vector and eventually have to reallocate space
12150 for it, invalidating all copies of pointers into the previous
12151 contents. */
12152 *dwo_file_slot = dwo_file;
12153 }
12154 else
12155 {
b4f54984 12156 if (dwarf_read_debug)
73869dc2
DE
12157 {
12158 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12159 virtual_dwo_name.c_str ());
73869dc2 12160 }
9a3c8263 12161 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12162 }
73869dc2
DE
12163
12164 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12165 dwo_unit->dwo_file = dwo_file;
12166 dwo_unit->signature = signature;
8d749320
SM
12167 dwo_unit->section =
12168 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12169 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12170 is_debug_types
73869dc2
DE
12171 ? &dwp_file->sections.types
12172 : &dwp_file->sections.info,
12173 sections.info_or_types_offset,
12174 sections.info_or_types_size);
12175 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12176
12177 return dwo_unit;
12178}
12179
57d63ce2
DE
12180/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12181 Returns NULL if the signature isn't found. */
80626a55
DE
12182
12183static struct dwo_unit *
ed2dc618
SM
12184lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12185 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12186 ULONGEST signature, int is_debug_types)
80626a55 12187{
57d63ce2
DE
12188 const struct dwp_hash_table *dwp_htab =
12189 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12190 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12191 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12192 uint32_t hash = signature & mask;
12193 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12194 unsigned int i;
12195 void **slot;
870f88f7 12196 struct dwo_unit find_dwo_cu;
80626a55
DE
12197
12198 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12199 find_dwo_cu.signature = signature;
19ac8c2e 12200 slot = htab_find_slot (is_debug_types
48b490f2
TT
12201 ? dwp_file->loaded_tus.get ()
12202 : dwp_file->loaded_cus.get (),
19ac8c2e 12203 &find_dwo_cu, INSERT);
80626a55
DE
12204
12205 if (*slot != NULL)
9a3c8263 12206 return (struct dwo_unit *) *slot;
80626a55
DE
12207
12208 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12209 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12210 {
12211 ULONGEST signature_in_table;
12212
12213 signature_in_table =
57d63ce2 12214 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12215 if (signature_in_table == signature)
12216 {
57d63ce2
DE
12217 uint32_t unit_index =
12218 read_4_bytes (dbfd,
12219 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12220
73869dc2
DE
12221 if (dwp_file->version == 1)
12222 {
ed2dc618
SM
12223 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12224 dwp_file, unit_index,
73869dc2
DE
12225 comp_dir, signature,
12226 is_debug_types);
12227 }
12228 else
12229 {
ed2dc618
SM
12230 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12231 dwp_file, unit_index,
73869dc2
DE
12232 comp_dir, signature,
12233 is_debug_types);
12234 }
9a3c8263 12235 return (struct dwo_unit *) *slot;
80626a55
DE
12236 }
12237 if (signature_in_table == 0)
12238 return NULL;
12239 hash = (hash + hash2) & mask;
12240 }
12241
12242 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12243 " [in module %s]"),
12244 dwp_file->name);
12245}
12246
ab5088bf 12247/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12248 Open the file specified by FILE_NAME and hand it off to BFD for
12249 preliminary analysis. Return a newly initialized bfd *, which
12250 includes a canonicalized copy of FILE_NAME.
80626a55 12251 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12252 SEARCH_CWD is true if the current directory is to be searched.
12253 It will be searched before debug-file-directory.
13aaf454
DE
12254 If successful, the file is added to the bfd include table of the
12255 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12256 If unable to find/open the file, return NULL.
3019eac3
DE
12257 NOTE: This function is derived from symfile_bfd_open. */
12258
192b62ce 12259static gdb_bfd_ref_ptr
ed2dc618
SM
12260try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12261 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12262{
24b9144d 12263 int desc;
9c02c129
DE
12264 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12265 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12266 to debug_file_directory. */
e0cc99a6 12267 const char *search_path;
9c02c129
DE
12268 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12269
e0cc99a6 12270 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12271 if (search_cwd)
12272 {
12273 if (*debug_file_directory != '\0')
e0cc99a6
TT
12274 {
12275 search_path_holder.reset (concat (".", dirname_separator_string,
12276 debug_file_directory,
12277 (char *) NULL));
12278 search_path = search_path_holder.get ();
12279 }
6ac97d4c 12280 else
e0cc99a6 12281 search_path = ".";
6ac97d4c 12282 }
9c02c129 12283 else
e0cc99a6 12284 search_path = debug_file_directory;
3019eac3 12285
24b9144d 12286 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12287 if (is_dwp)
12288 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12289
12290 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12291 desc = openp (search_path, flags, file_name,
3019eac3
DE
12292 O_RDONLY | O_BINARY, &absolute_name);
12293 if (desc < 0)
12294 return NULL;
12295
e0cc99a6
TT
12296 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12297 gnutarget, desc));
9c02c129
DE
12298 if (sym_bfd == NULL)
12299 return NULL;
192b62ce 12300 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12301
192b62ce
TT
12302 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12303 return NULL;
3019eac3 12304
13aaf454
DE
12305 /* Success. Record the bfd as having been included by the objfile's bfd.
12306 This is important because things like demangled_names_hash lives in the
12307 objfile's per_bfd space and may have references to things like symbol
12308 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12309 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12310
3019eac3
DE
12311 return sym_bfd;
12312}
12313
ab5088bf 12314/* Try to open DWO file FILE_NAME.
3019eac3
DE
12315 COMP_DIR is the DW_AT_comp_dir attribute.
12316 The result is the bfd handle of the file.
12317 If there is a problem finding or opening the file, return NULL.
12318 Upon success, the canonicalized path of the file is stored in the bfd,
12319 same as symfile_bfd_open. */
12320
192b62ce 12321static gdb_bfd_ref_ptr
ed2dc618
SM
12322open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12323 const char *file_name, const char *comp_dir)
3019eac3 12324{
80626a55 12325 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12326 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12327 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12328
12329 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12330
12331 if (comp_dir != NULL)
12332 {
43816ebc
TT
12333 gdb::unique_xmalloc_ptr<char> path_to_try
12334 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12335
12336 /* NOTE: If comp_dir is a relative path, this will also try the
12337 search path, which seems useful. */
ed2dc618 12338 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12339 path_to_try.get (),
ed2dc618 12340 0 /*is_dwp*/,
192b62ce 12341 1 /*search_cwd*/));
3019eac3
DE
12342 if (abfd != NULL)
12343 return abfd;
12344 }
12345
12346 /* That didn't work, try debug-file-directory, which, despite its name,
12347 is a list of paths. */
12348
12349 if (*debug_file_directory == '\0')
12350 return NULL;
12351
ed2dc618
SM
12352 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12353 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12354}
12355
80626a55
DE
12356/* This function is mapped across the sections and remembers the offset and
12357 size of each of the DWO debugging sections we are interested in. */
12358
12359static void
12360dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12361{
9a3c8263 12362 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12363 const struct dwop_section_names *names = &dwop_section_names;
12364
12365 if (section_is_p (sectp->name, &names->abbrev_dwo))
12366 {
049412e3 12367 dwo_sections->abbrev.s.section = sectp;
fd361982 12368 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12369 }
12370 else if (section_is_p (sectp->name, &names->info_dwo))
12371 {
049412e3 12372 dwo_sections->info.s.section = sectp;
fd361982 12373 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12374 }
12375 else if (section_is_p (sectp->name, &names->line_dwo))
12376 {
049412e3 12377 dwo_sections->line.s.section = sectp;
fd361982 12378 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12379 }
12380 else if (section_is_p (sectp->name, &names->loc_dwo))
12381 {
049412e3 12382 dwo_sections->loc.s.section = sectp;
fd361982 12383 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12384 }
12385 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12386 {
049412e3 12387 dwo_sections->macinfo.s.section = sectp;
fd361982 12388 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12389 }
12390 else if (section_is_p (sectp->name, &names->macro_dwo))
12391 {
049412e3 12392 dwo_sections->macro.s.section = sectp;
fd361982 12393 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->str_dwo))
12396 {
049412e3 12397 dwo_sections->str.s.section = sectp;
fd361982 12398 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12399 }
12400 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12401 {
049412e3 12402 dwo_sections->str_offsets.s.section = sectp;
fd361982 12403 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12404 }
12405 else if (section_is_p (sectp->name, &names->types_dwo))
12406 {
12407 struct dwarf2_section_info type_section;
12408
12409 memset (&type_section, 0, sizeof (type_section));
049412e3 12410 type_section.s.section = sectp;
fd361982 12411 type_section.size = bfd_section_size (sectp);
fd5866f6 12412 dwo_sections->types.push_back (type_section);
80626a55
DE
12413 }
12414}
12415
ab5088bf 12416/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12417 by PER_CU. This is for the non-DWP case.
80626a55 12418 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12419
12420static struct dwo_file *
0ac5b59e
DE
12421open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12422 const char *dwo_name, const char *comp_dir)
3019eac3 12423{
ed2dc618 12424 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12425
fb1eb2f9 12426 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12427 if (dbfd == NULL)
12428 {
b4f54984 12429 if (dwarf_read_debug)
80626a55
DE
12430 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12431 return NULL;
12432 }
263db9a1 12433
51ac9db5 12434 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12435 dwo_file->dwo_name = dwo_name;
12436 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12437 dwo_file->dbfd = std::move (dbfd);
3019eac3 12438
fb1eb2f9 12439 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12440 &dwo_file->sections);
3019eac3 12441
18a8505e
AT
12442 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12443 dwo_file->sections.info, dwo_file->cus);
3019eac3 12444
263db9a1 12445 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12446 dwo_file->sections.types, dwo_file->tus);
3019eac3 12447
b4f54984 12448 if (dwarf_read_debug)
80626a55
DE
12449 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12450
263db9a1 12451 return dwo_file.release ();
3019eac3
DE
12452}
12453
80626a55 12454/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12455 size of each of the DWP debugging sections common to version 1 and 2 that
12456 we are interested in. */
3019eac3 12457
80626a55 12458static void
73869dc2
DE
12459dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12460 void *dwp_file_ptr)
3019eac3 12461{
9a3c8263 12462 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12463 const struct dwop_section_names *names = &dwop_section_names;
12464 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12465
80626a55 12466 /* Record the ELF section number for later lookup: this is what the
73869dc2 12467 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12468 gdb_assert (elf_section_nr < dwp_file->num_sections);
12469 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12470
80626a55
DE
12471 /* Look for specific sections that we need. */
12472 if (section_is_p (sectp->name, &names->str_dwo))
12473 {
049412e3 12474 dwp_file->sections.str.s.section = sectp;
fd361982 12475 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12476 }
12477 else if (section_is_p (sectp->name, &names->cu_index))
12478 {
049412e3 12479 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12480 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12481 }
12482 else if (section_is_p (sectp->name, &names->tu_index))
12483 {
049412e3 12484 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12485 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12486 }
12487}
3019eac3 12488
73869dc2
DE
12489/* This function is mapped across the sections and remembers the offset and
12490 size of each of the DWP version 2 debugging sections that we are interested
12491 in. This is split into a separate function because we don't know if we
12492 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12493
12494static void
12495dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12496{
9a3c8263 12497 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12498 const struct dwop_section_names *names = &dwop_section_names;
12499 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12500
12501 /* Record the ELF section number for later lookup: this is what the
12502 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12503 gdb_assert (elf_section_nr < dwp_file->num_sections);
12504 dwp_file->elf_sections[elf_section_nr] = sectp;
12505
12506 /* Look for specific sections that we need. */
12507 if (section_is_p (sectp->name, &names->abbrev_dwo))
12508 {
049412e3 12509 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12510 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12511 }
12512 else if (section_is_p (sectp->name, &names->info_dwo))
12513 {
049412e3 12514 dwp_file->sections.info.s.section = sectp;
fd361982 12515 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12516 }
12517 else if (section_is_p (sectp->name, &names->line_dwo))
12518 {
049412e3 12519 dwp_file->sections.line.s.section = sectp;
fd361982 12520 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12521 }
12522 else if (section_is_p (sectp->name, &names->loc_dwo))
12523 {
049412e3 12524 dwp_file->sections.loc.s.section = sectp;
fd361982 12525 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12526 }
12527 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12528 {
049412e3 12529 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12530 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12531 }
12532 else if (section_is_p (sectp->name, &names->macro_dwo))
12533 {
049412e3 12534 dwp_file->sections.macro.s.section = sectp;
fd361982 12535 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12536 }
12537 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12538 {
049412e3 12539 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12540 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12541 }
12542 else if (section_is_p (sectp->name, &names->types_dwo))
12543 {
049412e3 12544 dwp_file->sections.types.s.section = sectp;
fd361982 12545 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12546 }
12547}
12548
80626a55 12549/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12550
80626a55
DE
12551static hashval_t
12552hash_dwp_loaded_cutus (const void *item)
12553{
9a3c8263 12554 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12555
80626a55
DE
12556 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12557 return dwo_unit->signature;
3019eac3
DE
12558}
12559
80626a55 12560/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12561
80626a55
DE
12562static int
12563eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12564{
9a3c8263
SM
12565 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12566 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12567
80626a55
DE
12568 return dua->signature == dub->signature;
12569}
3019eac3 12570
80626a55 12571/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12572
48b490f2 12573static htab_up
80626a55
DE
12574allocate_dwp_loaded_cutus_table (struct objfile *objfile)
12575{
48b490f2
TT
12576 return htab_up (htab_create_alloc (3,
12577 hash_dwp_loaded_cutus,
12578 eq_dwp_loaded_cutus,
12579 NULL, xcalloc, xfree));
80626a55 12580}
3019eac3 12581
ab5088bf
DE
12582/* Try to open DWP file FILE_NAME.
12583 The result is the bfd handle of the file.
12584 If there is a problem finding or opening the file, return NULL.
12585 Upon success, the canonicalized path of the file is stored in the bfd,
12586 same as symfile_bfd_open. */
12587
192b62ce 12588static gdb_bfd_ref_ptr
ed2dc618
SM
12589open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12590 const char *file_name)
ab5088bf 12591{
ed2dc618
SM
12592 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12593 1 /*is_dwp*/,
192b62ce 12594 1 /*search_cwd*/));
6ac97d4c
DE
12595 if (abfd != NULL)
12596 return abfd;
12597
12598 /* Work around upstream bug 15652.
12599 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12600 [Whether that's a "bug" is debatable, but it is getting in our way.]
12601 We have no real idea where the dwp file is, because gdb's realpath-ing
12602 of the executable's path may have discarded the needed info.
12603 [IWBN if the dwp file name was recorded in the executable, akin to
12604 .gnu_debuglink, but that doesn't exist yet.]
12605 Strip the directory from FILE_NAME and search again. */
12606 if (*debug_file_directory != '\0')
12607 {
12608 /* Don't implicitly search the current directory here.
12609 If the user wants to search "." to handle this case,
12610 it must be added to debug-file-directory. */
ed2dc618
SM
12611 return try_open_dwop_file (dwarf2_per_objfile,
12612 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12613 0 /*search_cwd*/);
12614 }
12615
12616 return NULL;
ab5088bf
DE
12617}
12618
80626a55
DE
12619/* Initialize the use of the DWP file for the current objfile.
12620 By convention the name of the DWP file is ${objfile}.dwp.
12621 The result is NULL if it can't be found. */
a766d390 12622
400174b1 12623static std::unique_ptr<struct dwp_file>
ed2dc618 12624open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12625{
12626 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12627
82bf32bc
JK
12628 /* Try to find first .dwp for the binary file before any symbolic links
12629 resolving. */
6c447423
DE
12630
12631 /* If the objfile is a debug file, find the name of the real binary
12632 file and get the name of dwp file from there. */
d721ba37 12633 std::string dwp_name;
6c447423
DE
12634 if (objfile->separate_debug_objfile_backlink != NULL)
12635 {
12636 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12637 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12638
d721ba37 12639 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12640 }
12641 else
d721ba37
PA
12642 dwp_name = objfile->original_name;
12643
12644 dwp_name += ".dwp";
80626a55 12645
ed2dc618 12646 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12647 if (dbfd == NULL
12648 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12649 {
12650 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12651 dwp_name = objfile_name (objfile);
12652 dwp_name += ".dwp";
ed2dc618 12653 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12654 }
12655
80626a55
DE
12656 if (dbfd == NULL)
12657 {
b4f54984 12658 if (dwarf_read_debug)
d721ba37 12659 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12660 return std::unique_ptr<dwp_file> ();
3019eac3 12661 }
400174b1
TT
12662
12663 const char *name = bfd_get_filename (dbfd.get ());
12664 std::unique_ptr<struct dwp_file> dwp_file
12665 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12666
0a0f4c01 12667 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12668 dwp_file->elf_sections =
12669 OBSTACK_CALLOC (&objfile->objfile_obstack,
12670 dwp_file->num_sections, asection *);
12671
400174b1
TT
12672 bfd_map_over_sections (dwp_file->dbfd.get (),
12673 dwarf2_locate_common_dwp_sections,
12674 dwp_file.get ());
80626a55 12675
400174b1
TT
12676 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12677 0);
80626a55 12678
400174b1
TT
12679 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12680 1);
80626a55 12681
73869dc2 12682 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12683 if (dwp_file->cus && dwp_file->tus
12684 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12685 {
12686 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12687 pretty bizarre. We use pulongest here because that's the established
4d65956b 12688 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12689 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12690 " TU version %s [in DWP file %s]"),
12691 pulongest (dwp_file->cus->version),
d721ba37 12692 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12693 }
08302ed2
DE
12694
12695 if (dwp_file->cus)
12696 dwp_file->version = dwp_file->cus->version;
12697 else if (dwp_file->tus)
12698 dwp_file->version = dwp_file->tus->version;
12699 else
12700 dwp_file->version = 2;
73869dc2
DE
12701
12702 if (dwp_file->version == 2)
400174b1
TT
12703 bfd_map_over_sections (dwp_file->dbfd.get (),
12704 dwarf2_locate_v2_dwp_sections,
12705 dwp_file.get ());
73869dc2 12706
19ac8c2e
DE
12707 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
12708 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 12709
b4f54984 12710 if (dwarf_read_debug)
80626a55
DE
12711 {
12712 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12713 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12714 " %s CUs, %s TUs\n",
12715 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12716 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12717 }
12718
12719 return dwp_file;
3019eac3 12720}
c906108c 12721
ab5088bf
DE
12722/* Wrapper around open_and_init_dwp_file, only open it once. */
12723
12724static struct dwp_file *
ed2dc618 12725get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12726{
12727 if (! dwarf2_per_objfile->dwp_checked)
12728 {
ed2dc618
SM
12729 dwarf2_per_objfile->dwp_file
12730 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12731 dwarf2_per_objfile->dwp_checked = 1;
12732 }
400174b1 12733 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12734}
12735
80626a55
DE
12736/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12737 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12738 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12739 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12740 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12741
12742 This is called, for example, when wanting to read a variable with a
12743 complex location. Therefore we don't want to do file i/o for every call.
12744 Therefore we don't want to look for a DWO file on every call.
12745 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12746 then we check if we've already seen DWO_NAME, and only THEN do we check
12747 for a DWO file.
12748
1c658ad5 12749 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12750 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12751
3019eac3 12752static struct dwo_unit *
80626a55
DE
12753lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12754 const char *dwo_name, const char *comp_dir,
12755 ULONGEST signature, int is_debug_types)
3019eac3 12756{
ed2dc618 12757 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12758 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12759 const char *kind = is_debug_types ? "TU" : "CU";
12760 void **dwo_file_slot;
3019eac3 12761 struct dwo_file *dwo_file;
80626a55 12762 struct dwp_file *dwp_file;
cb1df416 12763
6a506a2d
DE
12764 /* First see if there's a DWP file.
12765 If we have a DWP file but didn't find the DWO inside it, don't
12766 look for the original DWO file. It makes gdb behave differently
12767 depending on whether one is debugging in the build tree. */
cf2c3c16 12768
ed2dc618 12769 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12770 if (dwp_file != NULL)
cf2c3c16 12771 {
80626a55
DE
12772 const struct dwp_hash_table *dwp_htab =
12773 is_debug_types ? dwp_file->tus : dwp_file->cus;
12774
12775 if (dwp_htab != NULL)
12776 {
12777 struct dwo_unit *dwo_cutu =
ed2dc618 12778 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12779 signature, is_debug_types);
80626a55
DE
12780
12781 if (dwo_cutu != NULL)
12782 {
b4f54984 12783 if (dwarf_read_debug)
80626a55
DE
12784 {
12785 fprintf_unfiltered (gdb_stdlog,
12786 "Virtual DWO %s %s found: @%s\n",
12787 kind, hex_string (signature),
12788 host_address_to_string (dwo_cutu));
12789 }
12790 return dwo_cutu;
12791 }
12792 }
12793 }
6a506a2d 12794 else
80626a55 12795 {
6a506a2d 12796 /* No DWP file, look for the DWO file. */
80626a55 12797
ed2dc618
SM
12798 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12799 dwo_name, comp_dir);
6a506a2d 12800 if (*dwo_file_slot == NULL)
80626a55 12801 {
6a506a2d
DE
12802 /* Read in the file and build a table of the CUs/TUs it contains. */
12803 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12804 }
6a506a2d 12805 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12806 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12807
6a506a2d 12808 if (dwo_file != NULL)
19c3d4c9 12809 {
6a506a2d
DE
12810 struct dwo_unit *dwo_cutu = NULL;
12811
12812 if (is_debug_types && dwo_file->tus)
12813 {
12814 struct dwo_unit find_dwo_cutu;
12815
12816 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12817 find_dwo_cutu.signature = signature;
9a3c8263 12818 dwo_cutu
b0b6a987
TT
12819 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12820 &find_dwo_cutu);
6a506a2d 12821 }
33c5cd75 12822 else if (!is_debug_types && dwo_file->cus)
80626a55 12823 {
33c5cd75
DB
12824 struct dwo_unit find_dwo_cutu;
12825
12826 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12827 find_dwo_cutu.signature = signature;
b0b6a987 12828 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12829 &find_dwo_cutu);
6a506a2d
DE
12830 }
12831
12832 if (dwo_cutu != NULL)
12833 {
b4f54984 12834 if (dwarf_read_debug)
6a506a2d
DE
12835 {
12836 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12837 kind, dwo_name, hex_string (signature),
12838 host_address_to_string (dwo_cutu));
12839 }
12840 return dwo_cutu;
80626a55
DE
12841 }
12842 }
2e276125 12843 }
9cdd5dbd 12844
80626a55
DE
12845 /* We didn't find it. This could mean a dwo_id mismatch, or
12846 someone deleted the DWO/DWP file, or the search path isn't set up
12847 correctly to find the file. */
12848
b4f54984 12849 if (dwarf_read_debug)
80626a55
DE
12850 {
12851 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12852 kind, dwo_name, hex_string (signature));
12853 }
3019eac3 12854
6656a72d
DE
12855 /* This is a warning and not a complaint because it can be caused by
12856 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12857 {
12858 /* Print the name of the DWP file if we looked there, helps the user
12859 better diagnose the problem. */
791afaa2 12860 std::string dwp_text;
43942612
DE
12861
12862 if (dwp_file != NULL)
791afaa2
TT
12863 dwp_text = string_printf (" [in DWP file %s]",
12864 lbasename (dwp_file->name));
43942612 12865
9d8780f0 12866 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12867 " [in module %s]"),
12868 kind, dwo_name, hex_string (signature),
791afaa2 12869 dwp_text.c_str (),
43942612 12870 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12871 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12872 }
3019eac3 12873 return NULL;
5fb290d7
DJ
12874}
12875
80626a55
DE
12876/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12877 See lookup_dwo_cutu_unit for details. */
12878
12879static struct dwo_unit *
12880lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12881 const char *dwo_name, const char *comp_dir,
12882 ULONGEST signature)
12883{
12884 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12885}
12886
12887/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12888 See lookup_dwo_cutu_unit for details. */
12889
12890static struct dwo_unit *
12891lookup_dwo_type_unit (struct signatured_type *this_tu,
12892 const char *dwo_name, const char *comp_dir)
12893{
12894 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12895}
12896
89e63ee4
DE
12897/* Traversal function for queue_and_load_all_dwo_tus. */
12898
12899static int
12900queue_and_load_dwo_tu (void **slot, void *info)
12901{
12902 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12903 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12904 ULONGEST signature = dwo_unit->signature;
12905 struct signatured_type *sig_type =
12906 lookup_dwo_signatured_type (per_cu->cu, signature);
12907
12908 if (sig_type != NULL)
12909 {
12910 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12911
12912 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12913 a real dependency of PER_CU on SIG_TYPE. That is detected later
12914 while processing PER_CU. */
12915 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12916 load_full_type_unit (sig_cu);
ae640021 12917 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12918 }
12919
12920 return 1;
12921}
12922
12923/* Queue all TUs contained in the DWO of PER_CU to be read in.
12924 The DWO may have the only definition of the type, though it may not be
12925 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12926 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12927
12928static void
12929queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12930{
12931 struct dwo_unit *dwo_unit;
12932 struct dwo_file *dwo_file;
12933
12934 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12935 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12936 gdb_assert (per_cu->cu != NULL);
12937
12938 dwo_unit = per_cu->cu->dwo_unit;
12939 gdb_assert (dwo_unit != NULL);
12940
12941 dwo_file = dwo_unit->dwo_file;
12942 if (dwo_file->tus != NULL)
b0b6a987
TT
12943 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12944 per_cu);
89e63ee4
DE
12945}
12946
3019eac3 12947/* Read in various DIEs. */
348e048f 12948
d389af10 12949/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12950 Inherit only the children of the DW_AT_abstract_origin DIE not being
12951 already referenced by DW_AT_abstract_origin from the children of the
12952 current DIE. */
d389af10
JK
12953
12954static void
12955inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12956{
12957 struct die_info *child_die;
791afaa2 12958 sect_offset *offsetp;
d389af10
JK
12959 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12960 struct die_info *origin_die;
12961 /* Iterator of the ORIGIN_DIE children. */
12962 struct die_info *origin_child_die;
d389af10 12963 struct attribute *attr;
cd02d79d
PA
12964 struct dwarf2_cu *origin_cu;
12965 struct pending **origin_previous_list_in_scope;
d389af10
JK
12966
12967 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12968 if (!attr)
12969 return;
12970
cd02d79d
PA
12971 /* Note that following die references may follow to a die in a
12972 different cu. */
12973
12974 origin_cu = cu;
12975 origin_die = follow_die_ref (die, attr, &origin_cu);
12976
12977 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12978 symbols in. */
12979 origin_previous_list_in_scope = origin_cu->list_in_scope;
12980 origin_cu->list_in_scope = cu->list_in_scope;
12981
edb3359d
DJ
12982 if (die->tag != origin_die->tag
12983 && !(die->tag == DW_TAG_inlined_subroutine
12984 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12985 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12986 sect_offset_str (die->sect_off),
12987 sect_offset_str (origin_die->sect_off));
d389af10 12988
791afaa2 12989 std::vector<sect_offset> offsets;
d389af10 12990
3ea89b92
PMR
12991 for (child_die = die->child;
12992 child_die && child_die->tag;
12993 child_die = sibling_die (child_die))
12994 {
12995 struct die_info *child_origin_die;
12996 struct dwarf2_cu *child_origin_cu;
12997
12998 /* We are trying to process concrete instance entries:
216f72a1 12999 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13000 it's not relevant to our analysis here. i.e. detecting DIEs that are
13001 present in the abstract instance but not referenced in the concrete
13002 one. */
216f72a1
JK
13003 if (child_die->tag == DW_TAG_call_site
13004 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13005 continue;
13006
c38f313d
DJ
13007 /* For each CHILD_DIE, find the corresponding child of
13008 ORIGIN_DIE. If there is more than one layer of
13009 DW_AT_abstract_origin, follow them all; there shouldn't be,
13010 but GCC versions at least through 4.4 generate this (GCC PR
13011 40573). */
3ea89b92
PMR
13012 child_origin_die = child_die;
13013 child_origin_cu = cu;
c38f313d
DJ
13014 while (1)
13015 {
cd02d79d
PA
13016 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13017 child_origin_cu);
c38f313d
DJ
13018 if (attr == NULL)
13019 break;
cd02d79d
PA
13020 child_origin_die = follow_die_ref (child_origin_die, attr,
13021 &child_origin_cu);
c38f313d
DJ
13022 }
13023
d389af10
JK
13024 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13025 counterpart may exist. */
c38f313d 13026 if (child_origin_die != child_die)
d389af10 13027 {
edb3359d
DJ
13028 if (child_die->tag != child_origin_die->tag
13029 && !(child_die->tag == DW_TAG_inlined_subroutine
13030 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13031 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13032 "different tags"),
9d8780f0
SM
13033 sect_offset_str (child_die->sect_off),
13034 sect_offset_str (child_origin_die->sect_off));
c38f313d 13035 if (child_origin_die->parent != origin_die)
b98664d3 13036 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13037 "different parents"),
9d8780f0
SM
13038 sect_offset_str (child_die->sect_off),
13039 sect_offset_str (child_origin_die->sect_off));
c38f313d 13040 else
791afaa2 13041 offsets.push_back (child_origin_die->sect_off);
d389af10 13042 }
d389af10 13043 }
791afaa2
TT
13044 std::sort (offsets.begin (), offsets.end ());
13045 sect_offset *offsets_end = offsets.data () + offsets.size ();
13046 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13047 if (offsetp[-1] == *offsetp)
b98664d3 13048 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13049 "to DIE %s as their abstract origin"),
13050 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13051
791afaa2 13052 offsetp = offsets.data ();
d389af10
JK
13053 origin_child_die = origin_die->child;
13054 while (origin_child_die && origin_child_die->tag)
13055 {
13056 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13057 while (offsetp < offsets_end
9c541725 13058 && *offsetp < origin_child_die->sect_off)
d389af10 13059 offsetp++;
b64f50a1 13060 if (offsetp >= offsets_end
9c541725 13061 || *offsetp > origin_child_die->sect_off)
d389af10 13062 {
adde2bff
DE
13063 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13064 Check whether we're already processing ORIGIN_CHILD_DIE.
13065 This can happen with mutually referenced abstract_origins.
13066 PR 16581. */
13067 if (!origin_child_die->in_process)
13068 process_die (origin_child_die, origin_cu);
d389af10
JK
13069 }
13070 origin_child_die = sibling_die (origin_child_die);
13071 }
cd02d79d 13072 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13073
13074 if (cu != origin_cu)
13075 compute_delayed_physnames (origin_cu);
d389af10
JK
13076}
13077
c906108c 13078static void
e7c27a73 13079read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13080{
518817b3 13081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13082 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13083 struct context_stack *newobj;
c906108c
SS
13084 CORE_ADDR lowpc;
13085 CORE_ADDR highpc;
13086 struct die_info *child_die;
edb3359d 13087 struct attribute *attr, *call_line, *call_file;
15d034d0 13088 const char *name;
e142c38c 13089 CORE_ADDR baseaddr;
801e3a5b 13090 struct block *block;
edb3359d 13091 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13092 std::vector<struct symbol *> template_args;
34eaf542 13093 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13094
13095 if (inlined_func)
13096 {
13097 /* If we do not have call site information, we can't show the
13098 caller of this inlined function. That's too confusing, so
13099 only use the scope for local variables. */
13100 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13101 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13102 if (call_line == NULL || call_file == NULL)
13103 {
13104 read_lexical_block_scope (die, cu);
13105 return;
13106 }
13107 }
c906108c 13108
b3b3bada 13109 baseaddr = objfile->text_section_offset ();
e142c38c 13110
94af9270 13111 name = dwarf2_name (die, cu);
c906108c 13112
e8d05480
JB
13113 /* Ignore functions with missing or empty names. These are actually
13114 illegal according to the DWARF standard. */
13115 if (name == NULL)
13116 {
b98664d3 13117 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13118 sect_offset_str (die->sect_off));
e8d05480
JB
13119 return;
13120 }
13121
13122 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13123 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13124 <= PC_BOUNDS_INVALID)
e8d05480 13125 {
ae4d0c03
PM
13126 attr = dwarf2_attr (die, DW_AT_external, cu);
13127 if (!attr || !DW_UNSND (attr))
b98664d3 13128 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13129 "for subprogram DIE at %s"),
13130 sect_offset_str (die->sect_off));
e8d05480
JB
13131 return;
13132 }
c906108c 13133
3e29f34a
MR
13134 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13135 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13136
34eaf542
TT
13137 /* If we have any template arguments, then we must allocate a
13138 different sort of symbol. */
13139 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13140 {
13141 if (child_die->tag == DW_TAG_template_type_param
13142 || child_die->tag == DW_TAG_template_value_param)
13143 {
e623cf5d 13144 templ_func = allocate_template_symbol (objfile);
cf724bc9 13145 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13146 break;
13147 }
13148 }
13149
c24bdb02 13150 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13151 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13152 (struct symbol *) templ_func);
4c2df51b 13153
81873cc8 13154 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13155 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13156 cu->language);
13157
4cecd739
DJ
13158 /* If there is a location expression for DW_AT_frame_base, record
13159 it. */
e142c38c 13160 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13161 if (attr != nullptr)
fe978cb0 13162 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13163
63e43d3a
PMR
13164 /* If there is a location for the static link, record it. */
13165 newobj->static_link = NULL;
13166 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13167 if (attr != nullptr)
63e43d3a 13168 {
224c3ddb
SM
13169 newobj->static_link
13170 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13171 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13172 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13173 }
13174
c24bdb02 13175 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13176
639d11d3 13177 if (die->child != NULL)
c906108c 13178 {
639d11d3 13179 child_die = die->child;
c906108c
SS
13180 while (child_die && child_die->tag)
13181 {
34eaf542
TT
13182 if (child_die->tag == DW_TAG_template_type_param
13183 || child_die->tag == DW_TAG_template_value_param)
13184 {
13185 struct symbol *arg = new_symbol (child_die, NULL, cu);
13186
f1078f66 13187 if (arg != NULL)
2f4732b0 13188 template_args.push_back (arg);
34eaf542
TT
13189 }
13190 else
13191 process_die (child_die, cu);
c906108c
SS
13192 child_die = sibling_die (child_die);
13193 }
13194 }
13195
d389af10
JK
13196 inherit_abstract_dies (die, cu);
13197
4a811a97
UW
13198 /* If we have a DW_AT_specification, we might need to import using
13199 directives from the context of the specification DIE. See the
13200 comment in determine_prefix. */
13201 if (cu->language == language_cplus
13202 && dwarf2_attr (die, DW_AT_specification, cu))
13203 {
13204 struct dwarf2_cu *spec_cu = cu;
13205 struct die_info *spec_die = die_specification (die, &spec_cu);
13206
13207 while (spec_die)
13208 {
13209 child_die = spec_die->child;
13210 while (child_die && child_die->tag)
13211 {
13212 if (child_die->tag == DW_TAG_imported_module)
13213 process_die (child_die, spec_cu);
13214 child_die = sibling_die (child_die);
13215 }
13216
13217 /* In some cases, GCC generates specification DIEs that
13218 themselves contain DW_AT_specification attributes. */
13219 spec_die = die_specification (spec_die, &spec_cu);
13220 }
13221 }
13222
c24bdb02 13223 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13224 /* Make a block for the local symbols within. */
c24bdb02 13225 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13226 cstk.static_link, lowpc, highpc);
801e3a5b 13227
df8a16a1 13228 /* For C++, set the block's scope. */
45280282
IB
13229 if ((cu->language == language_cplus
13230 || cu->language == language_fortran
c44af4eb
TT
13231 || cu->language == language_d
13232 || cu->language == language_rust)
4d4ec4e5 13233 && cu->processing_has_namespace_info)
195a3f6c
TT
13234 block_set_scope (block, determine_prefix (die, cu),
13235 &objfile->objfile_obstack);
df8a16a1 13236
801e3a5b
JB
13237 /* If we have address ranges, record them. */
13238 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13239
a60f3166 13240 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13241
34eaf542 13242 /* Attach template arguments to function. */
2f4732b0 13243 if (!template_args.empty ())
34eaf542
TT
13244 {
13245 gdb_assert (templ_func != NULL);
13246
2f4732b0 13247 templ_func->n_template_arguments = template_args.size ();
34eaf542 13248 templ_func->template_arguments
8d749320
SM
13249 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13250 templ_func->n_template_arguments);
34eaf542 13251 memcpy (templ_func->template_arguments,
2f4732b0 13252 template_args.data (),
34eaf542 13253 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13254
13255 /* Make sure that the symtab is set on the new symbols. Even
13256 though they don't appear in this symtab directly, other parts
13257 of gdb assume that symbols do, and this is reasonably
13258 true. */
8634679f 13259 for (symbol *sym : template_args)
3e1d3d8c 13260 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13261 }
13262
208d8187
JB
13263 /* In C++, we can have functions nested inside functions (e.g., when
13264 a function declares a class that has methods). This means that
13265 when we finish processing a function scope, we may need to go
13266 back to building a containing block's symbol lists. */
c24bdb02
KS
13267 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13268 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13269
921e78cf
JB
13270 /* If we've finished processing a top-level function, subsequent
13271 symbols go in the file symbol list. */
c24bdb02
KS
13272 if (cu->get_builder ()->outermost_context_p ())
13273 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13274}
13275
13276/* Process all the DIES contained within a lexical block scope. Start
13277 a new scope, process the dies, and then close the scope. */
13278
13279static void
e7c27a73 13280read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13281{
518817b3 13282 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13283 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13284 CORE_ADDR lowpc, highpc;
13285 struct die_info *child_die;
e142c38c
DJ
13286 CORE_ADDR baseaddr;
13287
b3b3bada 13288 baseaddr = objfile->text_section_offset ();
c906108c
SS
13289
13290 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13291 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13292 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13293 be nasty. Might be easier to properly extend generic blocks to
af34e669 13294 describe ranges. */
e385593e
JK
13295 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13296 {
13297 case PC_BOUNDS_NOT_PRESENT:
13298 /* DW_TAG_lexical_block has no attributes, process its children as if
13299 there was no wrapping by that DW_TAG_lexical_block.
13300 GCC does no longer produces such DWARF since GCC r224161. */
13301 for (child_die = die->child;
13302 child_die != NULL && child_die->tag;
13303 child_die = sibling_die (child_die))
13304 process_die (child_die, cu);
13305 return;
13306 case PC_BOUNDS_INVALID:
13307 return;
13308 }
3e29f34a
MR
13309 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13310 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13311
c24bdb02 13312 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13313 if (die->child != NULL)
c906108c 13314 {
639d11d3 13315 child_die = die->child;
c906108c
SS
13316 while (child_die && child_die->tag)
13317 {
e7c27a73 13318 process_die (child_die, cu);
c906108c
SS
13319 child_die = sibling_die (child_die);
13320 }
13321 }
3ea89b92 13322 inherit_abstract_dies (die, cu);
c24bdb02 13323 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13324
c24bdb02
KS
13325 if (*cu->get_builder ()->get_local_symbols () != NULL
13326 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13327 {
801e3a5b 13328 struct block *block
c24bdb02 13329 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13330 cstk.start_addr, highpc);
801e3a5b
JB
13331
13332 /* Note that recording ranges after traversing children, as we
13333 do here, means that recording a parent's ranges entails
13334 walking across all its children's ranges as they appear in
13335 the address map, which is quadratic behavior.
13336
13337 It would be nicer to record the parent's ranges before
13338 traversing its children, simply overriding whatever you find
13339 there. But since we don't even decide whether to create a
13340 block until after we've traversed its children, that's hard
13341 to do. */
13342 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13343 }
c24bdb02
KS
13344 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13345 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13346}
13347
216f72a1 13348/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13349
13350static void
13351read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13352{
518817b3 13353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13354 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13355 CORE_ADDR pc, baseaddr;
13356 struct attribute *attr;
13357 struct call_site *call_site, call_site_local;
13358 void **slot;
13359 int nparams;
13360 struct die_info *child_die;
13361
b3b3bada 13362 baseaddr = objfile->text_section_offset ();
96408a79 13363
216f72a1
JK
13364 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13365 if (attr == NULL)
13366 {
13367 /* This was a pre-DWARF-5 GNU extension alias
13368 for DW_AT_call_return_pc. */
13369 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13370 }
96408a79
SA
13371 if (!attr)
13372 {
b98664d3 13373 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13374 "DIE %s [in module %s]"),
13375 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13376 return;
13377 }
cd6c91b4 13378 pc = attr->value_as_address () + baseaddr;
3e29f34a 13379 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13380
13381 if (cu->call_site_htab == NULL)
13382 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13383 NULL, &objfile->objfile_obstack,
13384 hashtab_obstack_allocate, NULL);
13385 call_site_local.pc = pc;
13386 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13387 if (*slot != NULL)
13388 {
b98664d3 13389 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13390 "DIE %s [in module %s]"),
13391 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13392 objfile_name (objfile));
96408a79
SA
13393 return;
13394 }
13395
13396 /* Count parameters at the caller. */
13397
13398 nparams = 0;
13399 for (child_die = die->child; child_die && child_die->tag;
13400 child_die = sibling_die (child_die))
13401 {
216f72a1
JK
13402 if (child_die->tag != DW_TAG_call_site_parameter
13403 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13404 {
b98664d3 13405 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13406 "DW_TAG_call_site child DIE %s [in module %s]"),
13407 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13408 objfile_name (objfile));
96408a79
SA
13409 continue;
13410 }
13411
13412 nparams++;
13413 }
13414
224c3ddb
SM
13415 call_site
13416 = ((struct call_site *)
13417 obstack_alloc (&objfile->objfile_obstack,
13418 sizeof (*call_site)
13419 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13420 *slot = call_site;
13421 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13422 call_site->pc = pc;
13423
216f72a1
JK
13424 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13425 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13426 {
13427 struct die_info *func_die;
13428
13429 /* Skip also over DW_TAG_inlined_subroutine. */
13430 for (func_die = die->parent;
13431 func_die && func_die->tag != DW_TAG_subprogram
13432 && func_die->tag != DW_TAG_subroutine_type;
13433 func_die = func_die->parent);
13434
216f72a1
JK
13435 /* DW_AT_call_all_calls is a superset
13436 of DW_AT_call_all_tail_calls. */
96408a79 13437 if (func_die
216f72a1 13438 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13439 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13440 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13441 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13442 {
13443 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13444 not complete. But keep CALL_SITE for look ups via call_site_htab,
13445 both the initial caller containing the real return address PC and
13446 the final callee containing the current PC of a chain of tail
13447 calls do not need to have the tail call list complete. But any
13448 function candidate for a virtual tail call frame searched via
13449 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13450 determined unambiguously. */
13451 }
13452 else
13453 {
13454 struct type *func_type = NULL;
13455
13456 if (func_die)
13457 func_type = get_die_type (func_die, cu);
13458 if (func_type != NULL)
13459 {
13460 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13461
13462 /* Enlist this call site to the function. */
13463 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13464 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13465 }
13466 else
b98664d3 13467 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13468 "DIE %s [in module %s]"),
13469 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13470 }
13471 }
13472
216f72a1
JK
13473 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13474 if (attr == NULL)
13475 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13476 if (attr == NULL)
13477 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13478 if (attr == NULL)
216f72a1
JK
13479 {
13480 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13481 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 }
96408a79 13483 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13484 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13485 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13486 else if (attr->form_is_block ())
96408a79
SA
13487 {
13488 struct dwarf2_locexpr_baton *dlbaton;
13489
8d749320 13490 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13491 dlbaton->data = DW_BLOCK (attr)->data;
13492 dlbaton->size = DW_BLOCK (attr)->size;
13493 dlbaton->per_cu = cu->per_cu;
13494
13495 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13496 }
cd6c91b4 13497 else if (attr->form_is_ref ())
96408a79 13498 {
96408a79
SA
13499 struct dwarf2_cu *target_cu = cu;
13500 struct die_info *target_die;
13501
ac9ec31b 13502 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13503 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13504 if (die_is_declaration (target_die, target_cu))
13505 {
7d45c7c3 13506 const char *target_physname;
9112db09
JK
13507
13508 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13509 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13510 if (target_physname == NULL)
9112db09 13511 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13512 if (target_physname == NULL)
b98664d3 13513 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13514 "physname, for referencing DIE %s [in module %s]"),
13515 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13516 else
7d455152 13517 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13518 }
13519 else
13520 {
13521 CORE_ADDR lowpc;
13522
13523 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13524 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13525 <= PC_BOUNDS_INVALID)
b98664d3 13526 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13527 "low pc, for referencing DIE %s [in module %s]"),
13528 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13529 else
3e29f34a
MR
13530 {
13531 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13532 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13533 }
96408a79
SA
13534 }
13535 }
13536 else
b98664d3 13537 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13538 "block nor reference, for DIE %s [in module %s]"),
13539 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13540
13541 call_site->per_cu = cu->per_cu;
13542
13543 for (child_die = die->child;
13544 child_die && child_die->tag;
13545 child_die = sibling_die (child_die))
13546 {
96408a79 13547 struct call_site_parameter *parameter;
1788b2d3 13548 struct attribute *loc, *origin;
96408a79 13549
216f72a1
JK
13550 if (child_die->tag != DW_TAG_call_site_parameter
13551 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13552 {
13553 /* Already printed the complaint above. */
13554 continue;
13555 }
13556
13557 gdb_assert (call_site->parameter_count < nparams);
13558 parameter = &call_site->parameter[call_site->parameter_count];
13559
1788b2d3
JK
13560 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13561 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13562 register is contained in DW_AT_call_value. */
96408a79 13563
24c5c679 13564 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13565 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13566 if (origin == NULL)
13567 {
13568 /* This was a pre-DWARF-5 GNU extension alias
13569 for DW_AT_call_parameter. */
13570 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13571 }
cd6c91b4 13572 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13573 {
1788b2d3 13574 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
13575
13576 sect_offset sect_off
13577 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13578 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
13579 {
13580 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13581 binding can be done only inside one CU. Such referenced DIE
13582 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13583 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13584 "DW_TAG_call_site child DIE %s [in module %s]"),
13585 sect_offset_str (child_die->sect_off),
9c541725 13586 objfile_name (objfile));
d76b7dbc
JK
13587 continue;
13588 }
9c541725
PA
13589 parameter->u.param_cu_off
13590 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13591 }
4fc6c0d5 13592 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13593 {
b98664d3 13594 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13595 "DW_TAG_call_site child DIE %s [in module %s]"),
13596 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13597 continue;
13598 }
24c5c679 13599 else
96408a79 13600 {
24c5c679
JK
13601 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13602 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13603 if (parameter->u.dwarf_reg != -1)
13604 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13605 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13606 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13607 &parameter->u.fb_offset))
13608 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13609 else
13610 {
b98664d3 13611 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13612 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13613 "DW_TAG_call_site child DIE %s "
24c5c679 13614 "[in module %s]"),
9d8780f0 13615 sect_offset_str (child_die->sect_off),
9c541725 13616 objfile_name (objfile));
24c5c679
JK
13617 continue;
13618 }
96408a79
SA
13619 }
13620
216f72a1
JK
13621 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13622 if (attr == NULL)
13623 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13624 if (attr == NULL || !attr->form_is_block ())
96408a79 13625 {
b98664d3 13626 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13627 "DW_TAG_call_site child DIE %s [in module %s]"),
13628 sect_offset_str (child_die->sect_off),
9c541725 13629 objfile_name (objfile));
96408a79
SA
13630 continue;
13631 }
13632 parameter->value = DW_BLOCK (attr)->data;
13633 parameter->value_size = DW_BLOCK (attr)->size;
13634
13635 /* Parameters are not pre-cleared by memset above. */
13636 parameter->data_value = NULL;
13637 parameter->data_value_size = 0;
13638 call_site->parameter_count++;
13639
216f72a1
JK
13640 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13641 if (attr == NULL)
13642 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13643 if (attr != nullptr)
96408a79 13644 {
4fc6c0d5 13645 if (!attr->form_is_block ())
b98664d3 13646 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13647 "DW_TAG_call_site child DIE %s [in module %s]"),
13648 sect_offset_str (child_die->sect_off),
9c541725 13649 objfile_name (objfile));
96408a79
SA
13650 else
13651 {
13652 parameter->data_value = DW_BLOCK (attr)->data;
13653 parameter->data_value_size = DW_BLOCK (attr)->size;
13654 }
13655 }
13656 }
13657}
13658
71a3c369
TT
13659/* Helper function for read_variable. If DIE represents a virtual
13660 table, then return the type of the concrete object that is
13661 associated with the virtual table. Otherwise, return NULL. */
13662
13663static struct type *
13664rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13665{
13666 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13667 if (attr == NULL)
13668 return NULL;
13669
13670 /* Find the type DIE. */
13671 struct die_info *type_die = NULL;
13672 struct dwarf2_cu *type_cu = cu;
13673
cd6c91b4 13674 if (attr->form_is_ref ())
71a3c369
TT
13675 type_die = follow_die_ref (die, attr, &type_cu);
13676 if (type_die == NULL)
13677 return NULL;
13678
13679 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13680 return NULL;
13681 return die_containing_type (type_die, type_cu);
13682}
13683
13684/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13685
13686static void
13687read_variable (struct die_info *die, struct dwarf2_cu *cu)
13688{
13689 struct rust_vtable_symbol *storage = NULL;
13690
13691 if (cu->language == language_rust)
13692 {
13693 struct type *containing_type = rust_containing_type (die, cu);
13694
13695 if (containing_type != NULL)
13696 {
518817b3 13697 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13698
468c0cbb 13699 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13700 initialize_objfile_symbol (storage);
13701 storage->concrete_type = containing_type;
cf724bc9 13702 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13703 }
13704 }
13705
e4a62c65
TV
13706 struct symbol *res = new_symbol (die, NULL, cu, storage);
13707 struct attribute *abstract_origin
13708 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13709 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13710 if (res == NULL && loc && abstract_origin)
13711 {
13712 /* We have a variable without a name, but with a location and an abstract
13713 origin. This may be a concrete instance of an abstract variable
13714 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13715 later. */
13716 struct dwarf2_cu *origin_cu = cu;
13717 struct die_info *origin_die
13718 = follow_die_ref (die, abstract_origin, &origin_cu);
13719 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13720 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13721 }
71a3c369
TT
13722}
13723
43988095
JK
13724/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13725 reading .debug_rnglists.
13726 Callback's type should be:
13727 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13728 Return true if the attributes are present and valid, otherwise,
13729 return false. */
13730
13731template <typename Callback>
13732static bool
13733dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13734 Callback &&callback)
13735{
ed2dc618 13736 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13737 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13738 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13739 bfd *obfd = objfile->obfd;
43988095
JK
13740 /* Base address selection entry. */
13741 CORE_ADDR base;
13742 int found_base;
43988095 13743 const gdb_byte *buffer;
43988095
JK
13744 CORE_ADDR baseaddr;
13745 bool overflow = false;
13746
13747 found_base = cu->base_known;
13748 base = cu->base_address;
13749
96b79293 13750 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13751 if (offset >= dwarf2_per_objfile->rnglists.size)
13752 {
b98664d3 13753 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13754 offset);
13755 return false;
13756 }
13757 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13758
b3b3bada 13759 baseaddr = objfile->text_section_offset ();
43988095
JK
13760
13761 while (1)
13762 {
7814882a
JK
13763 /* Initialize it due to a false compiler warning. */
13764 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13765 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13766 + dwarf2_per_objfile->rnglists.size);
13767 unsigned int bytes_read;
13768
13769 if (buffer == buf_end)
13770 {
13771 overflow = true;
13772 break;
13773 }
13774 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13775 switch (rlet)
13776 {
13777 case DW_RLE_end_of_list:
13778 break;
13779 case DW_RLE_base_address:
13780 if (buffer + cu->header.addr_size > buf_end)
13781 {
13782 overflow = true;
13783 break;
13784 }
13785 base = read_address (obfd, buffer, cu, &bytes_read);
13786 found_base = 1;
13787 buffer += bytes_read;
13788 break;
13789 case DW_RLE_start_length:
13790 if (buffer + cu->header.addr_size > buf_end)
13791 {
13792 overflow = true;
13793 break;
13794 }
13795 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13796 buffer += bytes_read;
13797 range_end = (range_beginning
13798 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13799 buffer += bytes_read;
13800 if (buffer > buf_end)
13801 {
13802 overflow = true;
13803 break;
13804 }
13805 break;
13806 case DW_RLE_offset_pair:
13807 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13808 buffer += bytes_read;
13809 if (buffer > buf_end)
13810 {
13811 overflow = true;
13812 break;
13813 }
13814 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13815 buffer += bytes_read;
13816 if (buffer > buf_end)
13817 {
13818 overflow = true;
13819 break;
13820 }
13821 break;
13822 case DW_RLE_start_end:
13823 if (buffer + 2 * cu->header.addr_size > buf_end)
13824 {
13825 overflow = true;
13826 break;
13827 }
13828 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
13829 buffer += bytes_read;
13830 range_end = read_address (obfd, buffer, cu, &bytes_read);
13831 buffer += bytes_read;
13832 break;
13833 default:
b98664d3 13834 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13835 return false;
13836 }
13837 if (rlet == DW_RLE_end_of_list || overflow)
13838 break;
13839 if (rlet == DW_RLE_base_address)
13840 continue;
13841
13842 if (!found_base)
13843 {
13844 /* We have no valid base address for the ranges
13845 data. */
b98664d3 13846 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13847 return false;
13848 }
13849
13850 if (range_beginning > range_end)
13851 {
13852 /* Inverted range entries are invalid. */
b98664d3 13853 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13854 return false;
13855 }
13856
13857 /* Empty range entries have no effect. */
13858 if (range_beginning == range_end)
13859 continue;
13860
13861 range_beginning += base;
13862 range_end += base;
13863
13864 /* A not-uncommon case of bad debug info.
13865 Don't pollute the addrmap with bad data. */
13866 if (range_beginning + baseaddr == 0
13867 && !dwarf2_per_objfile->has_section_at_zero)
13868 {
b98664d3 13869 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13870 " [in module %s]"), objfile_name (objfile));
13871 continue;
13872 }
13873
13874 callback (range_beginning, range_end);
13875 }
13876
13877 if (overflow)
13878 {
b98664d3 13879 complaint (_("Offset %d is not terminated "
43988095
JK
13880 "for DW_AT_ranges attribute"),
13881 offset);
13882 return false;
13883 }
13884
13885 return true;
13886}
13887
13888/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13889 Callback's type should be:
13890 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13891 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13892
43988095 13893template <typename Callback>
43039443 13894static int
5f46c5a5 13895dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13896 Callback &&callback)
43039443 13897{
ed2dc618 13898 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13899 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13900 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13901 struct comp_unit_head *cu_header = &cu->header;
13902 bfd *obfd = objfile->obfd;
13903 unsigned int addr_size = cu_header->addr_size;
13904 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13905 /* Base address selection entry. */
13906 CORE_ADDR base;
13907 int found_base;
13908 unsigned int dummy;
d521ce57 13909 const gdb_byte *buffer;
ff013f42 13910 CORE_ADDR baseaddr;
43039443 13911
43988095
JK
13912 if (cu_header->version >= 5)
13913 return dwarf2_rnglists_process (offset, cu, callback);
13914
d00adf39
DE
13915 found_base = cu->base_known;
13916 base = cu->base_address;
43039443 13917
96b79293 13918 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13919 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13920 {
b98664d3 13921 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13922 offset);
13923 return 0;
13924 }
dce234bc 13925 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13926
b3b3bada 13927 baseaddr = objfile->text_section_offset ();
ff013f42 13928
43039443
JK
13929 while (1)
13930 {
13931 CORE_ADDR range_beginning, range_end;
13932
13933 range_beginning = read_address (obfd, buffer, cu, &dummy);
13934 buffer += addr_size;
13935 range_end = read_address (obfd, buffer, cu, &dummy);
13936 buffer += addr_size;
13937 offset += 2 * addr_size;
13938
13939 /* An end of list marker is a pair of zero addresses. */
13940 if (range_beginning == 0 && range_end == 0)
13941 /* Found the end of list entry. */
13942 break;
13943
13944 /* Each base address selection entry is a pair of 2 values.
13945 The first is the largest possible address, the second is
13946 the base address. Check for a base address here. */
13947 if ((range_beginning & mask) == mask)
13948 {
28d2bfb9
AB
13949 /* If we found the largest possible address, then we already
13950 have the base address in range_end. */
13951 base = range_end;
43039443
JK
13952 found_base = 1;
13953 continue;
13954 }
13955
13956 if (!found_base)
13957 {
13958 /* We have no valid base address for the ranges
13959 data. */
b98664d3 13960 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13961 return 0;
13962 }
13963
9277c30c
UW
13964 if (range_beginning > range_end)
13965 {
13966 /* Inverted range entries are invalid. */
b98664d3 13967 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13968 return 0;
13969 }
13970
13971 /* Empty range entries have no effect. */
13972 if (range_beginning == range_end)
13973 continue;
13974
43039443
JK
13975 range_beginning += base;
13976 range_end += base;
13977
01093045
DE
13978 /* A not-uncommon case of bad debug info.
13979 Don't pollute the addrmap with bad data. */
13980 if (range_beginning + baseaddr == 0
13981 && !dwarf2_per_objfile->has_section_at_zero)
13982 {
b98664d3 13983 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13984 " [in module %s]"), objfile_name (objfile));
01093045
DE
13985 continue;
13986 }
13987
5f46c5a5
JK
13988 callback (range_beginning, range_end);
13989 }
13990
13991 return 1;
13992}
13993
13994/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13995 Return 1 if the attributes are present and valid, otherwise, return 0.
13996 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13997
13998static int
13999dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14000 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 14001 dwarf2_psymtab *ranges_pst)
5f46c5a5 14002{
518817b3 14003 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 14004 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 14005 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14006 int low_set = 0;
14007 CORE_ADDR low = 0;
14008 CORE_ADDR high = 0;
14009 int retval;
14010
14011 retval = dwarf2_ranges_process (offset, cu,
14012 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14013 {
9277c30c 14014 if (ranges_pst != NULL)
3e29f34a
MR
14015 {
14016 CORE_ADDR lowpc;
14017 CORE_ADDR highpc;
14018
79748972
TT
14019 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14020 range_beginning + baseaddr)
14021 - baseaddr);
14022 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14023 range_end + baseaddr)
14024 - baseaddr);
d320c2b5
TT
14025 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14026 lowpc, highpc - 1, ranges_pst);
3e29f34a 14027 }
ff013f42 14028
43039443
JK
14029 /* FIXME: This is recording everything as a low-high
14030 segment of consecutive addresses. We should have a
14031 data structure for discontiguous block ranges
14032 instead. */
14033 if (! low_set)
14034 {
14035 low = range_beginning;
14036 high = range_end;
14037 low_set = 1;
14038 }
14039 else
14040 {
14041 if (range_beginning < low)
14042 low = range_beginning;
14043 if (range_end > high)
14044 high = range_end;
14045 }
5f46c5a5
JK
14046 });
14047 if (!retval)
14048 return 0;
43039443
JK
14049
14050 if (! low_set)
14051 /* If the first entry is an end-of-list marker, the range
14052 describes an empty scope, i.e. no instructions. */
14053 return 0;
14054
14055 if (low_return)
14056 *low_return = low;
14057 if (high_return)
14058 *high_return = high;
14059 return 1;
14060}
14061
3a2b436a
JK
14062/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14063 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14064 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14065
3a2b436a 14066static enum pc_bounds_kind
af34e669 14067dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14068 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14069 dwarf2_psymtab *pst)
c906108c 14070{
518817b3
SM
14071 struct dwarf2_per_objfile *dwarf2_per_objfile
14072 = cu->per_cu->dwarf2_per_objfile;
c906108c 14073 struct attribute *attr;
91da1414 14074 struct attribute *attr_high;
af34e669
DJ
14075 CORE_ADDR low = 0;
14076 CORE_ADDR high = 0;
e385593e 14077 enum pc_bounds_kind ret;
c906108c 14078
91da1414
MW
14079 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14080 if (attr_high)
af34e669 14081 {
e142c38c 14082 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14083 if (attr != nullptr)
91da1414 14084 {
cd6c91b4
TT
14085 low = attr->value_as_address ();
14086 high = attr_high->value_as_address ();
14087 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14088 high += low;
91da1414 14089 }
af34e669
DJ
14090 else
14091 /* Found high w/o low attribute. */
e385593e 14092 return PC_BOUNDS_INVALID;
af34e669
DJ
14093
14094 /* Found consecutive range of addresses. */
3a2b436a 14095 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14096 }
c906108c 14097 else
af34e669 14098 {
e142c38c 14099 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14100 if (attr != NULL)
14101 {
18a8505e 14102 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14103 We take advantage of the fact that DW_AT_ranges does not appear
14104 in DW_TAG_compile_unit of DWO files. */
14105 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14106 unsigned int ranges_offset = (DW_UNSND (attr)
14107 + (need_ranges_base
14108 ? cu->ranges_base
14109 : 0));
2e3cf129 14110
af34e669 14111 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14112 .debug_ranges section. */
2e3cf129 14113 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14114 return PC_BOUNDS_INVALID;
43039443 14115 /* Found discontinuous range of addresses. */
3a2b436a 14116 ret = PC_BOUNDS_RANGES;
af34e669 14117 }
e385593e
JK
14118 else
14119 return PC_BOUNDS_NOT_PRESENT;
af34e669 14120 }
c906108c 14121
48fbe735 14122 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14123 if (high <= low)
e385593e 14124 return PC_BOUNDS_INVALID;
c906108c
SS
14125
14126 /* When using the GNU linker, .gnu.linkonce. sections are used to
14127 eliminate duplicate copies of functions and vtables and such.
14128 The linker will arbitrarily choose one and discard the others.
14129 The AT_*_pc values for such functions refer to local labels in
14130 these sections. If the section from that file was discarded, the
14131 labels are not in the output, so the relocs get a value of 0.
14132 If this is a discarded function, mark the pc bounds as invalid,
14133 so that GDB will ignore it. */
72dca2f5 14134 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14135 return PC_BOUNDS_INVALID;
c906108c
SS
14136
14137 *lowpc = low;
96408a79
SA
14138 if (highpc)
14139 *highpc = high;
af34e669 14140 return ret;
c906108c
SS
14141}
14142
b084d499
JB
14143/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14144 its low and high PC addresses. Do nothing if these addresses could not
14145 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14146 and HIGHPC to the high address if greater than HIGHPC. */
14147
14148static void
14149dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14150 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14151 struct dwarf2_cu *cu)
14152{
14153 CORE_ADDR low, high;
14154 struct die_info *child = die->child;
14155
e385593e 14156 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14157 {
325fac50
PA
14158 *lowpc = std::min (*lowpc, low);
14159 *highpc = std::max (*highpc, high);
b084d499
JB
14160 }
14161
14162 /* If the language does not allow nested subprograms (either inside
14163 subprograms or lexical blocks), we're done. */
14164 if (cu->language != language_ada)
14165 return;
6e70227d 14166
b084d499
JB
14167 /* Check all the children of the given DIE. If it contains nested
14168 subprograms, then check their pc bounds. Likewise, we need to
14169 check lexical blocks as well, as they may also contain subprogram
14170 definitions. */
14171 while (child && child->tag)
14172 {
14173 if (child->tag == DW_TAG_subprogram
14174 || child->tag == DW_TAG_lexical_block)
14175 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14176 child = sibling_die (child);
14177 }
14178}
14179
fae299cd
DC
14180/* Get the low and high pc's represented by the scope DIE, and store
14181 them in *LOWPC and *HIGHPC. If the correct values can't be
14182 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14183
14184static void
14185get_scope_pc_bounds (struct die_info *die,
14186 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14187 struct dwarf2_cu *cu)
14188{
14189 CORE_ADDR best_low = (CORE_ADDR) -1;
14190 CORE_ADDR best_high = (CORE_ADDR) 0;
14191 CORE_ADDR current_low, current_high;
14192
3a2b436a 14193 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14194 >= PC_BOUNDS_RANGES)
fae299cd
DC
14195 {
14196 best_low = current_low;
14197 best_high = current_high;
14198 }
14199 else
14200 {
14201 struct die_info *child = die->child;
14202
14203 while (child && child->tag)
14204 {
14205 switch (child->tag) {
14206 case DW_TAG_subprogram:
b084d499 14207 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14208 break;
14209 case DW_TAG_namespace:
f55ee35c 14210 case DW_TAG_module:
fae299cd
DC
14211 /* FIXME: carlton/2004-01-16: Should we do this for
14212 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14213 that current GCC's always emit the DIEs corresponding
14214 to definitions of methods of classes as children of a
14215 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14216 the DIEs giving the declarations, which could be
14217 anywhere). But I don't see any reason why the
14218 standards says that they have to be there. */
14219 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14220
14221 if (current_low != ((CORE_ADDR) -1))
14222 {
325fac50
PA
14223 best_low = std::min (best_low, current_low);
14224 best_high = std::max (best_high, current_high);
fae299cd
DC
14225 }
14226 break;
14227 default:
0963b4bd 14228 /* Ignore. */
fae299cd
DC
14229 break;
14230 }
14231
14232 child = sibling_die (child);
14233 }
14234 }
14235
14236 *lowpc = best_low;
14237 *highpc = best_high;
14238}
14239
801e3a5b
JB
14240/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14241 in DIE. */
380bca97 14242
801e3a5b
JB
14243static void
14244dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14245 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14246{
518817b3 14247 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14248 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14249 struct attribute *attr;
91da1414 14250 struct attribute *attr_high;
801e3a5b 14251
91da1414
MW
14252 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14253 if (attr_high)
801e3a5b 14254 {
801e3a5b 14255 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14256 if (attr != nullptr)
801e3a5b 14257 {
cd6c91b4
TT
14258 CORE_ADDR low = attr->value_as_address ();
14259 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14260
cd6c91b4 14261 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14262 high += low;
9a619af0 14263
3e29f34a
MR
14264 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14265 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14266 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14267 }
14268 }
14269
14270 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14271 if (attr != nullptr)
801e3a5b 14272 {
18a8505e 14273 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14274 We take advantage of the fact that DW_AT_ranges does not appear
14275 in DW_TAG_compile_unit of DWO files. */
14276 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14277
14278 /* The value of the DW_AT_ranges attribute is the offset of the
14279 address range list in the .debug_ranges section. */
ab435259
DE
14280 unsigned long offset = (DW_UNSND (attr)
14281 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14282
2d5f09ec 14283 std::vector<blockrange> blockvec;
5f46c5a5
JK
14284 dwarf2_ranges_process (offset, cu,
14285 [&] (CORE_ADDR start, CORE_ADDR end)
14286 {
58fdfd2c
JK
14287 start += baseaddr;
14288 end += baseaddr;
5f46c5a5
JK
14289 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14290 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14291 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14292 blockvec.emplace_back (start, end);
5f46c5a5 14293 });
2d5f09ec
KB
14294
14295 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14296 }
14297}
14298
685b1105
JK
14299/* Check whether the producer field indicates either of GCC < 4.6, or the
14300 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14301
685b1105
JK
14302static void
14303check_producer (struct dwarf2_cu *cu)
60d5a603 14304{
38360086 14305 int major, minor;
60d5a603
JK
14306
14307 if (cu->producer == NULL)
14308 {
14309 /* For unknown compilers expect their behavior is DWARF version
14310 compliant.
14311
14312 GCC started to support .debug_types sections by -gdwarf-4 since
14313 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14314 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14315 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14316 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14317 }
b1ffba5a 14318 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14319 {
38360086
MW
14320 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14321 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14322 }
5230b05a 14323 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14324 {
14325 cu->producer_is_icc = true;
14326 cu->producer_is_icc_lt_14 = major < 14;
14327 }
c258c396
JD
14328 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14329 cu->producer_is_codewarrior = true;
685b1105
JK
14330 else
14331 {
14332 /* For other non-GCC compilers, expect their behavior is DWARF version
14333 compliant. */
60d5a603
JK
14334 }
14335
9068261f 14336 cu->checked_producer = true;
685b1105 14337}
ba919b58 14338
685b1105
JK
14339/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14340 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14341 during 4.6.0 experimental. */
14342
9068261f 14343static bool
685b1105
JK
14344producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14345{
14346 if (!cu->checked_producer)
14347 check_producer (cu);
14348
14349 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14350}
14351
c258c396
JD
14352
14353/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14354 with incorrect is_stmt attributes. */
14355
14356static bool
14357producer_is_codewarrior (struct dwarf2_cu *cu)
14358{
14359 if (!cu->checked_producer)
14360 check_producer (cu);
14361
14362 return cu->producer_is_codewarrior;
14363}
14364
405feb71 14365/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14366 DW_AT_accessibility. */
14367
14368static enum dwarf_access_attribute
14369dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14370{
14371 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14372 {
14373 /* The default DWARF 2 accessibility for members is public, the default
14374 accessibility for inheritance is private. */
14375
14376 if (die->tag != DW_TAG_inheritance)
14377 return DW_ACCESS_public;
14378 else
14379 return DW_ACCESS_private;
14380 }
14381 else
14382 {
14383 /* DWARF 3+ defines the default accessibility a different way. The same
14384 rules apply now for DW_TAG_inheritance as for the members and it only
14385 depends on the container kind. */
14386
14387 if (die->parent->tag == DW_TAG_class_type)
14388 return DW_ACCESS_private;
14389 else
14390 return DW_ACCESS_public;
14391 }
14392}
14393
74ac6d43
TT
14394/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14395 offset. If the attribute was not found return 0, otherwise return
14396 1. If it was found but could not properly be handled, set *OFFSET
14397 to 0. */
14398
14399static int
14400handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14401 LONGEST *offset)
14402{
14403 struct attribute *attr;
14404
14405 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14406 if (attr != NULL)
14407 {
14408 *offset = 0;
14409
14410 /* Note that we do not check for a section offset first here.
14411 This is because DW_AT_data_member_location is new in DWARF 4,
14412 so if we see it, we can assume that a constant form is really
14413 a constant and not a section offset. */
cd6c91b4 14414 if (attr->form_is_constant ())
74ac6d43 14415 *offset = dwarf2_get_attr_constant_value (attr, 0);
cd6c91b4 14416 else if (attr->form_is_section_offset ())
74ac6d43 14417 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14418 else if (attr->form_is_block ())
74ac6d43
TT
14419 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14420 else
14421 dwarf2_complex_location_expr_complaint ();
14422
14423 return 1;
14424 }
14425
14426 return 0;
14427}
14428
c906108c
SS
14429/* Add an aggregate field to the field list. */
14430
14431static void
107d2387 14432dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14433 struct dwarf2_cu *cu)
6e70227d 14434{
518817b3 14435 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14436 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14437 struct nextfield *new_field;
14438 struct attribute *attr;
14439 struct field *fp;
15d034d0 14440 const char *fieldname = "";
c906108c 14441
7d0ccb61
DJ
14442 if (die->tag == DW_TAG_inheritance)
14443 {
be2daae6
TT
14444 fip->baseclasses.emplace_back ();
14445 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14446 }
14447 else
14448 {
be2daae6
TT
14449 fip->fields.emplace_back ();
14450 new_field = &fip->fields.back ();
7d0ccb61 14451 }
be2daae6 14452
c906108c
SS
14453 fip->nfields++;
14454
e142c38c 14455 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14456 if (attr != nullptr)
c906108c 14457 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14458 else
14459 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14460 if (new_field->accessibility != DW_ACCESS_public)
14461 fip->non_public_fields = 1;
60d5a603 14462
e142c38c 14463 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14464 if (attr != nullptr)
c906108c 14465 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14466 else
14467 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14468
14469 fp = &new_field->field;
a9a9bd0f 14470
e142c38c 14471 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14472 {
74ac6d43
TT
14473 LONGEST offset;
14474
a9a9bd0f 14475 /* Data member other than a C++ static data member. */
6e70227d 14476
c906108c 14477 /* Get type of field. */
e7c27a73 14478 fp->type = die_type (die, cu);
c906108c 14479
d6a843b5 14480 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14481
c906108c 14482 /* Get bit size of field (zero if none). */
e142c38c 14483 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14484 if (attr != nullptr)
c906108c
SS
14485 {
14486 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14487 }
14488 else
14489 {
14490 FIELD_BITSIZE (*fp) = 0;
14491 }
14492
14493 /* Get bit offset of field. */
74ac6d43
TT
14494 if (handle_data_member_location (die, cu, &offset))
14495 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14496 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14497 if (attr != nullptr)
c906108c 14498 {
d5a22e77 14499 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14500 {
14501 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14502 additional bit offset from the MSB of the containing
14503 anonymous object to the MSB of the field. We don't
14504 have to do anything special since we don't need to
14505 know the size of the anonymous object. */
f41f5e61 14506 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14507 }
14508 else
14509 {
14510 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14511 MSB of the anonymous object, subtract off the number of
14512 bits from the MSB of the field to the MSB of the
14513 object, and then subtract off the number of bits of
14514 the field itself. The result is the bit offset of
14515 the LSB of the field. */
c906108c
SS
14516 int anonymous_size;
14517 int bit_offset = DW_UNSND (attr);
14518
e142c38c 14519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14520 if (attr != nullptr)
c906108c
SS
14521 {
14522 /* The size of the anonymous object containing
14523 the bit field is explicit, so use the
14524 indicated size (in bytes). */
14525 anonymous_size = DW_UNSND (attr);
14526 }
14527 else
14528 {
14529 /* The size of the anonymous object containing
14530 the bit field must be inferred from the type
14531 attribute of the data member containing the
14532 bit field. */
14533 anonymous_size = TYPE_LENGTH (fp->type);
14534 }
f41f5e61
PA
14535 SET_FIELD_BITPOS (*fp,
14536 (FIELD_BITPOS (*fp)
14537 + anonymous_size * bits_per_byte
14538 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14539 }
14540 }
da5b30da
AA
14541 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14542 if (attr != NULL)
14543 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14544 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14545
14546 /* Get name of field. */
39cbfefa
DJ
14547 fieldname = dwarf2_name (die, cu);
14548 if (fieldname == NULL)
14549 fieldname = "";
d8151005
DJ
14550
14551 /* The name is already allocated along with this objfile, so we don't
14552 need to duplicate it for the type. */
14553 fp->name = fieldname;
c906108c
SS
14554
14555 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14556 pointer or virtual base class pointer) to private. */
e142c38c 14557 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14558 {
d48cc9dd 14559 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14560 new_field->accessibility = DW_ACCESS_private;
14561 fip->non_public_fields = 1;
14562 }
14563 }
a9a9bd0f 14564 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14565 {
a9a9bd0f
DC
14566 /* C++ static member. */
14567
14568 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14569 is a declaration, but all versions of G++ as of this writing
14570 (so through at least 3.2.1) incorrectly generate
14571 DW_TAG_variable tags. */
6e70227d 14572
ff355380 14573 const char *physname;
c906108c 14574
a9a9bd0f 14575 /* Get name of field. */
39cbfefa
DJ
14576 fieldname = dwarf2_name (die, cu);
14577 if (fieldname == NULL)
c906108c
SS
14578 return;
14579
254e6b9e 14580 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14581 if (attr
14582 /* Only create a symbol if this is an external value.
14583 new_symbol checks this and puts the value in the global symbol
14584 table, which we want. If it is not external, new_symbol
14585 will try to put the value in cu->list_in_scope which is wrong. */
14586 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14587 {
14588 /* A static const member, not much different than an enum as far as
14589 we're concerned, except that we can support more types. */
14590 new_symbol (die, NULL, cu);
14591 }
14592
2df3850c 14593 /* Get physical name. */
ff355380 14594 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14595
d8151005
DJ
14596 /* The name is already allocated along with this objfile, so we don't
14597 need to duplicate it for the type. */
14598 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14599 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14600 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14601 }
14602 else if (die->tag == DW_TAG_inheritance)
14603 {
74ac6d43 14604 LONGEST offset;
d4b96c9a 14605
74ac6d43
TT
14606 /* C++ base class field. */
14607 if (handle_data_member_location (die, cu, &offset))
14608 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14609 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14610 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14611 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14612 }
2ddeaf8a
TT
14613 else if (die->tag == DW_TAG_variant_part)
14614 {
14615 /* process_structure_scope will treat this DIE as a union. */
14616 process_structure_scope (die, cu);
14617
14618 /* The variant part is relative to the start of the enclosing
14619 structure. */
14620 SET_FIELD_BITPOS (*fp, 0);
14621 fp->type = get_die_type (die, cu);
14622 fp->artificial = 1;
14623 fp->name = "<<variant>>";
c8c81635
TT
14624
14625 /* Normally a DW_TAG_variant_part won't have a size, but our
14626 representation requires one, so set it to the maximum of the
489dbda6
TT
14627 child sizes, being sure to account for the offset at which
14628 each child is seen. */
c8c81635
TT
14629 if (TYPE_LENGTH (fp->type) == 0)
14630 {
14631 unsigned max = 0;
14632 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14633 {
14634 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14635 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14636 if (len > max)
14637 max = len;
14638 }
c8c81635
TT
14639 TYPE_LENGTH (fp->type) = max;
14640 }
2ddeaf8a
TT
14641 }
14642 else
14643 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14644}
14645
883fd55a
KS
14646/* Can the type given by DIE define another type? */
14647
14648static bool
14649type_can_define_types (const struct die_info *die)
14650{
14651 switch (die->tag)
14652 {
14653 case DW_TAG_typedef:
14654 case DW_TAG_class_type:
14655 case DW_TAG_structure_type:
14656 case DW_TAG_union_type:
14657 case DW_TAG_enumeration_type:
14658 return true;
14659
14660 default:
14661 return false;
14662 }
14663}
14664
14665/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14666
14667static void
883fd55a
KS
14668dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14669 struct dwarf2_cu *cu)
6e70227d 14670{
be2daae6
TT
14671 struct decl_field fp;
14672 memset (&fp, 0, sizeof (fp));
98751a41 14673
883fd55a 14674 gdb_assert (type_can_define_types (die));
98751a41 14675
883fd55a 14676 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14677 fp.name = dwarf2_name (die, cu);
14678 fp.type = read_type_die (die, cu);
98751a41 14679
c191a687
KS
14680 /* Save accessibility. */
14681 enum dwarf_access_attribute accessibility;
14682 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14683 if (attr != NULL)
14684 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14685 else
14686 accessibility = dwarf2_default_access_attribute (die, cu);
14687 switch (accessibility)
14688 {
14689 case DW_ACCESS_public:
14690 /* The assumed value if neither private nor protected. */
14691 break;
14692 case DW_ACCESS_private:
be2daae6 14693 fp.is_private = 1;
c191a687
KS
14694 break;
14695 case DW_ACCESS_protected:
be2daae6 14696 fp.is_protected = 1;
c191a687
KS
14697 break;
14698 default:
b98664d3 14699 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14700 }
14701
883fd55a 14702 if (die->tag == DW_TAG_typedef)
be2daae6 14703 fip->typedef_field_list.push_back (fp);
883fd55a 14704 else
be2daae6 14705 fip->nested_types_list.push_back (fp);
98751a41
JK
14706}
14707
c906108c
SS
14708/* Create the vector of fields, and attach it to the type. */
14709
14710static void
fba45db2 14711dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14712 struct dwarf2_cu *cu)
c906108c
SS
14713{
14714 int nfields = fip->nfields;
14715
14716 /* Record the field count, allocate space for the array of fields,
14717 and create blank accessibility bitfields if necessary. */
14718 TYPE_NFIELDS (type) = nfields;
14719 TYPE_FIELDS (type) = (struct field *)
be2daae6 14720 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14721
b4ba55a1 14722 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14723 {
14724 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14725
14726 TYPE_FIELD_PRIVATE_BITS (type) =
14727 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14728 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14729
14730 TYPE_FIELD_PROTECTED_BITS (type) =
14731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14732 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14733
774b6a14
TT
14734 TYPE_FIELD_IGNORE_BITS (type) =
14735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14736 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14737 }
14738
14739 /* If the type has baseclasses, allocate and clear a bit vector for
14740 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14741 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14742 {
be2daae6 14743 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14744 unsigned char *pointer;
c906108c
SS
14745
14746 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14747 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14748 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14749 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14750 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14751 }
14752
2ddeaf8a
TT
14753 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14754 {
14755 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14756
be2daae6 14757 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14758 {
be2daae6
TT
14759 struct nextfield &field = fip->fields[index];
14760
14761 if (field.variant.is_discriminant)
2ddeaf8a 14762 di->discriminant_index = index;
be2daae6 14763 else if (field.variant.default_branch)
2ddeaf8a
TT
14764 di->default_index = index;
14765 else
be2daae6 14766 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14767 }
14768 }
14769
be2daae6
TT
14770 /* Copy the saved-up fields into the field vector. */
14771 for (int i = 0; i < nfields; ++i)
c906108c 14772 {
be2daae6
TT
14773 struct nextfield &field
14774 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14775 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14776
be2daae6
TT
14777 TYPE_FIELD (type, i) = field.field;
14778 switch (field.accessibility)
c906108c 14779 {
c5aa993b 14780 case DW_ACCESS_private:
b4ba55a1 14781 if (cu->language != language_ada)
be2daae6 14782 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14783 break;
c906108c 14784
c5aa993b 14785 case DW_ACCESS_protected:
b4ba55a1 14786 if (cu->language != language_ada)
be2daae6 14787 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14788 break;
c906108c 14789
c5aa993b
JM
14790 case DW_ACCESS_public:
14791 break;
c906108c 14792
c5aa993b
JM
14793 default:
14794 /* Unknown accessibility. Complain and treat it as public. */
14795 {
b98664d3 14796 complaint (_("unsupported accessibility %d"),
be2daae6 14797 field.accessibility);
c5aa993b
JM
14798 }
14799 break;
c906108c 14800 }
be2daae6 14801 if (i < fip->baseclasses.size ())
c906108c 14802 {
be2daae6 14803 switch (field.virtuality)
c906108c 14804 {
c5aa993b
JM
14805 case DW_VIRTUALITY_virtual:
14806 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14807 if (cu->language == language_ada)
a73c6dcd 14808 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14809 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14810 break;
c906108c
SS
14811 }
14812 }
c906108c
SS
14813 }
14814}
14815
7d27a96d
TT
14816/* Return true if this member function is a constructor, false
14817 otherwise. */
14818
14819static int
14820dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14821{
14822 const char *fieldname;
fe978cb0 14823 const char *type_name;
7d27a96d
TT
14824 int len;
14825
14826 if (die->parent == NULL)
14827 return 0;
14828
14829 if (die->parent->tag != DW_TAG_structure_type
14830 && die->parent->tag != DW_TAG_union_type
14831 && die->parent->tag != DW_TAG_class_type)
14832 return 0;
14833
14834 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14835 type_name = dwarf2_name (die->parent, cu);
14836 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14837 return 0;
14838
14839 len = strlen (fieldname);
fe978cb0
PA
14840 return (strncmp (fieldname, type_name, len) == 0
14841 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14842}
14843
e35000a7
TBA
14844/* Check if the given VALUE is a recognized enum
14845 dwarf_defaulted_attribute constant according to DWARF5 spec,
14846 Table 7.24. */
14847
14848static bool
14849is_valid_DW_AT_defaulted (ULONGEST value)
14850{
14851 switch (value)
14852 {
14853 case DW_DEFAULTED_no:
14854 case DW_DEFAULTED_in_class:
14855 case DW_DEFAULTED_out_of_class:
14856 return true;
14857 }
14858
3142e908 14859 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14860 return false;
14861}
14862
c906108c
SS
14863/* Add a member function to the proper fieldlist. */
14864
14865static void
107d2387 14866dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14867 struct type *type, struct dwarf2_cu *cu)
c906108c 14868{
518817b3 14869 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14870 struct attribute *attr;
c906108c 14871 int i;
be2daae6 14872 struct fnfieldlist *flp = nullptr;
c906108c 14873 struct fn_field *fnp;
15d034d0 14874 const char *fieldname;
f792889a 14875 struct type *this_type;
60d5a603 14876 enum dwarf_access_attribute accessibility;
c906108c 14877
b4ba55a1 14878 if (cu->language == language_ada)
a73c6dcd 14879 error (_("unexpected member function in Ada type"));
b4ba55a1 14880
2df3850c 14881 /* Get name of member function. */
39cbfefa
DJ
14882 fieldname = dwarf2_name (die, cu);
14883 if (fieldname == NULL)
2df3850c 14884 return;
c906108c 14885
c906108c 14886 /* Look up member function name in fieldlist. */
be2daae6 14887 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14888 {
27bfe10e 14889 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14890 {
14891 flp = &fip->fnfieldlists[i];
14892 break;
14893 }
c906108c
SS
14894 }
14895
be2daae6
TT
14896 /* Create a new fnfieldlist if necessary. */
14897 if (flp == nullptr)
c906108c 14898 {
be2daae6
TT
14899 fip->fnfieldlists.emplace_back ();
14900 flp = &fip->fnfieldlists.back ();
c906108c 14901 flp->name = fieldname;
be2daae6 14902 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14903 }
14904
be2daae6
TT
14905 /* Create a new member function field and add it to the vector of
14906 fnfieldlists. */
14907 flp->fnfields.emplace_back ();
14908 fnp = &flp->fnfields.back ();
3da10d80
KS
14909
14910 /* Delay processing of the physname until later. */
9c37b5ae 14911 if (cu->language == language_cplus)
be2daae6
TT
14912 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14913 die, cu);
3da10d80
KS
14914 else
14915 {
1d06ead6 14916 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14917 fnp->physname = physname ? physname : "";
14918 }
14919
c906108c 14920 fnp->type = alloc_type (objfile);
f792889a
DJ
14921 this_type = read_type_die (die, cu);
14922 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14923 {
f792889a 14924 int nparams = TYPE_NFIELDS (this_type);
c906108c 14925
f792889a 14926 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14927 of the method itself (TYPE_CODE_METHOD). */
14928 smash_to_method_type (fnp->type, type,
f792889a
DJ
14929 TYPE_TARGET_TYPE (this_type),
14930 TYPE_FIELDS (this_type),
14931 TYPE_NFIELDS (this_type),
14932 TYPE_VARARGS (this_type));
c906108c
SS
14933
14934 /* Handle static member functions.
c5aa993b 14935 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14936 member functions. G++ helps GDB by marking the first
14937 parameter for non-static member functions (which is the this
14938 pointer) as artificial. We obtain this information from
14939 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14940 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14941 fnp->voffset = VOFFSET_STATIC;
14942 }
14943 else
b98664d3 14944 complaint (_("member function type missing for '%s'"),
3da10d80 14945 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14946
14947 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14948 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14949 fnp->fcontext = die_containing_type (die, cu);
c906108c 14950
3e43a32a
MS
14951 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14952 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14953
14954 /* Get accessibility. */
e142c38c 14955 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14956 if (attr != nullptr)
aead7601 14957 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14958 else
14959 accessibility = dwarf2_default_access_attribute (die, cu);
14960 switch (accessibility)
c906108c 14961 {
60d5a603
JK
14962 case DW_ACCESS_private:
14963 fnp->is_private = 1;
14964 break;
14965 case DW_ACCESS_protected:
14966 fnp->is_protected = 1;
14967 break;
c906108c
SS
14968 }
14969
b02dede2 14970 /* Check for artificial methods. */
e142c38c 14971 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14972 if (attr && DW_UNSND (attr) != 0)
14973 fnp->is_artificial = 1;
14974
e35000a7
TBA
14975 /* Check for defaulted methods. */
14976 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14977 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14978 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14979
14980 /* Check for deleted methods. */
14981 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14982 if (attr != nullptr && DW_UNSND (attr) != 0)
14983 fnp->is_deleted = 1;
14984
7d27a96d
TT
14985 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14986
0d564a31 14987 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14988 function. For older versions of GCC, this is an offset in the
14989 appropriate virtual table, as specified by DW_AT_containing_type.
14990 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14991 to the object address. */
14992
e142c38c 14993 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14994 if (attr != nullptr)
8e19ed76 14995 {
4fc6c0d5 14996 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14997 {
aec5aa8b
TT
14998 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14999 {
15000 /* Old-style GCC. */
15001 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15002 }
15003 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15004 || (DW_BLOCK (attr)->size > 1
15005 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15006 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15007 {
aec5aa8b
TT
15008 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15009 if ((fnp->voffset % cu->header.addr_size) != 0)
15010 dwarf2_complex_location_expr_complaint ();
15011 else
15012 fnp->voffset /= cu->header.addr_size;
15013 fnp->voffset += 2;
15014 }
15015 else
15016 dwarf2_complex_location_expr_complaint ();
15017
15018 if (!fnp->fcontext)
7e993ebf
KS
15019 {
15020 /* If there is no `this' field and no DW_AT_containing_type,
15021 we cannot actually find a base class context for the
15022 vtable! */
15023 if (TYPE_NFIELDS (this_type) == 0
15024 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15025 {
b98664d3 15026 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15027 "function \"%s\" (offset %s)"),
15028 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15029 }
15030 else
15031 {
15032 fnp->fcontext
15033 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15034 }
15035 }
aec5aa8b 15036 }
cd6c91b4 15037 else if (attr->form_is_section_offset ())
8e19ed76 15038 {
4d3c2250 15039 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15040 }
15041 else
15042 {
4d3c2250
KB
15043 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15044 fieldname);
8e19ed76 15045 }
0d564a31 15046 }
d48cc9dd
DJ
15047 else
15048 {
15049 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15050 if (attr && DW_UNSND (attr))
15051 {
15052 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15053 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15054 "but the vtable offset is not specified"),
9d8780f0 15055 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15056 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15057 TYPE_CPLUS_DYNAMIC (type) = 1;
15058 }
15059 }
c906108c
SS
15060}
15061
15062/* Create the vector of member function fields, and attach it to the type. */
15063
15064static void
fba45db2 15065dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15066 struct dwarf2_cu *cu)
c906108c 15067{
b4ba55a1 15068 if (cu->language == language_ada)
a73c6dcd 15069 error (_("unexpected member functions in Ada type"));
b4ba55a1 15070
c906108c
SS
15071 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15072 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15073 TYPE_ALLOC (type,
15074 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15075
be2daae6 15076 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15077 {
be2daae6 15078 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15079 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15080
be2daae6
TT
15081 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15082 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15083 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15084 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15085
15086 for (int k = 0; k < nf.fnfields.size (); ++k)
15087 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15088 }
15089
be2daae6 15090 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15091}
15092
1168df01
JB
15093/* Returns non-zero if NAME is the name of a vtable member in CU's
15094 language, zero otherwise. */
15095static int
15096is_vtable_name (const char *name, struct dwarf2_cu *cu)
15097{
15098 static const char vptr[] = "_vptr";
15099
9c37b5ae
TT
15100 /* Look for the C++ form of the vtable. */
15101 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15102 return 1;
15103
15104 return 0;
15105}
15106
c0dd20ea 15107/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15108 functions, with the ABI-specified layout. If TYPE describes
15109 such a structure, smash it into a member function type.
61049d3b
DJ
15110
15111 GCC shouldn't do this; it should just output pointer to member DIEs.
15112 This is GCC PR debug/28767. */
c0dd20ea 15113
0b92b5bb
TT
15114static void
15115quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15116{
09e2d7c7 15117 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15118
15119 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15120 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15121 return;
c0dd20ea
DJ
15122
15123 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15124 if (TYPE_FIELD_NAME (type, 0) == NULL
15125 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15126 || TYPE_FIELD_NAME (type, 1) == NULL
15127 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15128 return;
c0dd20ea
DJ
15129
15130 /* Find the type of the method. */
0b92b5bb 15131 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15132 if (pfn_type == NULL
15133 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15134 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15135 return;
c0dd20ea
DJ
15136
15137 /* Look for the "this" argument. */
15138 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15139 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15140 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15141 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15142 return;
c0dd20ea 15143
09e2d7c7 15144 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15145 new_type = alloc_type (objfile);
09e2d7c7 15146 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15147 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15148 TYPE_VARARGS (pfn_type));
0b92b5bb 15149 smash_to_methodptr_type (type, new_type);
c0dd20ea 15150}
1168df01 15151
2b4424c3
TT
15152/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15153 appropriate error checking and issuing complaints if there is a
15154 problem. */
15155
15156static ULONGEST
15157get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15158{
15159 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15160
15161 if (attr == nullptr)
15162 return 0;
15163
cd6c91b4 15164 if (!attr->form_is_constant ())
2b4424c3 15165 {
b98664d3 15166 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15167 " - DIE at %s [in module %s]"),
15168 sect_offset_str (die->sect_off),
15169 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15170 return 0;
15171 }
15172
15173 ULONGEST align;
15174 if (attr->form == DW_FORM_sdata)
15175 {
15176 LONGEST val = DW_SND (attr);
15177 if (val < 0)
15178 {
b98664d3 15179 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15180 " - DIE at %s [in module %s]"),
15181 sect_offset_str (die->sect_off),
15182 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15183 return 0;
15184 }
15185 align = val;
15186 }
15187 else
15188 align = DW_UNSND (attr);
15189
15190 if (align == 0)
15191 {
b98664d3 15192 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15193 " - DIE at %s [in module %s]"),
15194 sect_offset_str (die->sect_off),
15195 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15196 return 0;
15197 }
15198 if ((align & (align - 1)) != 0)
15199 {
b98664d3 15200 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15201 " - DIE at %s [in module %s]"),
15202 sect_offset_str (die->sect_off),
15203 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15204 return 0;
15205 }
15206
15207 return align;
15208}
15209
15210/* If the DIE has a DW_AT_alignment attribute, use its value to set
15211 the alignment for TYPE. */
15212
15213static void
15214maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15215 struct type *type)
15216{
15217 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15218 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15219 " - DIE at %s [in module %s]"),
15220 sect_offset_str (die->sect_off),
15221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15222}
685b1105 15223
e35000a7
TBA
15224/* Check if the given VALUE is a valid enum dwarf_calling_convention
15225 constant for a type, according to DWARF5 spec, Table 5.5. */
15226
15227static bool
15228is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15229{
15230 switch (value)
15231 {
15232 case DW_CC_normal:
15233 case DW_CC_pass_by_reference:
15234 case DW_CC_pass_by_value:
15235 return true;
15236
15237 default:
15238 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15239 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15240 return false;
15241 }
15242}
15243
d0922fcf
TBA
15244/* Check if the given VALUE is a valid enum dwarf_calling_convention
15245 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15246 also according to GNU-specific values (see include/dwarf2.h). */
15247
15248static bool
15249is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15250{
15251 switch (value)
15252 {
15253 case DW_CC_normal:
15254 case DW_CC_program:
15255 case DW_CC_nocall:
15256 return true;
15257
15258 case DW_CC_GNU_renesas_sh:
15259 case DW_CC_GNU_borland_fastcall_i386:
15260 case DW_CC_GDB_IBM_OpenCL:
15261 return true;
15262
15263 default:
15264 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15265 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15266 return false;
15267 }
15268}
15269
c906108c 15270/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15271 (definition) to create a type for the structure or union. Fill in
15272 the type's name and general properties; the members will not be
83655187
DE
15273 processed until process_structure_scope. A symbol table entry for
15274 the type will also not be done until process_structure_scope (assuming
15275 the type has a name).
c906108c 15276
c767944b
DJ
15277 NOTE: we need to call these functions regardless of whether or not the
15278 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15279 structure or union. This gets the type entered into our set of
83655187 15280 user defined types. */
c906108c 15281
f792889a 15282static struct type *
134d01f1 15283read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15284{
518817b3 15285 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15286 struct type *type;
15287 struct attribute *attr;
15d034d0 15288 const char *name;
c906108c 15289
348e048f
DE
15290 /* If the definition of this type lives in .debug_types, read that type.
15291 Don't follow DW_AT_specification though, that will take us back up
15292 the chain and we want to go down. */
45e58e77 15293 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15294 if (attr != nullptr)
348e048f 15295 {
ac9ec31b 15296 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15297
ac9ec31b 15298 /* The type's CU may not be the same as CU.
02142a6c 15299 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15300 return set_die_type (die, type, cu);
15301 }
15302
c0dd20ea 15303 type = alloc_type (objfile);
c906108c 15304 INIT_CPLUS_SPECIFIC (type);
93311388 15305
39cbfefa
DJ
15306 name = dwarf2_name (die, cu);
15307 if (name != NULL)
c906108c 15308 {
987504bb 15309 if (cu->language == language_cplus
c44af4eb
TT
15310 || cu->language == language_d
15311 || cu->language == language_rust)
63d06c5c 15312 {
15d034d0 15313 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15314
15315 /* dwarf2_full_name might have already finished building the DIE's
15316 type. If so, there is no need to continue. */
15317 if (get_die_type (die, cu) != NULL)
15318 return get_die_type (die, cu);
15319
e86ca25f 15320 TYPE_NAME (type) = full_name;
63d06c5c
DC
15321 }
15322 else
15323 {
d8151005
DJ
15324 /* The name is already allocated along with this objfile, so
15325 we don't need to duplicate it for the type. */
e86ca25f 15326 TYPE_NAME (type) = name;
63d06c5c 15327 }
c906108c
SS
15328 }
15329
15330 if (die->tag == DW_TAG_structure_type)
15331 {
15332 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15333 }
15334 else if (die->tag == DW_TAG_union_type)
15335 {
15336 TYPE_CODE (type) = TYPE_CODE_UNION;
15337 }
2ddeaf8a
TT
15338 else if (die->tag == DW_TAG_variant_part)
15339 {
15340 TYPE_CODE (type) = TYPE_CODE_UNION;
15341 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15342 }
c906108c
SS
15343 else
15344 {
4753d33b 15345 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15346 }
15347
0cc2414c
TT
15348 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15349 TYPE_DECLARED_CLASS (type) = 1;
15350
e35000a7
TBA
15351 /* Store the calling convention in the type if it's available in
15352 the die. Otherwise the calling convention remains set to
15353 the default value DW_CC_normal. */
15354 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15355 if (attr != nullptr
15356 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15357 {
15358 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15359 TYPE_CPLUS_CALLING_CONVENTION (type)
15360 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15361 }
15362
e142c38c 15363 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15364 if (attr != nullptr)
c906108c 15365 {
cd6c91b4 15366 if (attr->form_is_constant ())
155bfbd3
JB
15367 TYPE_LENGTH (type) = DW_UNSND (attr);
15368 else
15369 {
15370 /* For the moment, dynamic type sizes are not supported
15371 by GDB's struct type. The actual size is determined
15372 on-demand when resolving the type of a given object,
15373 so set the type's length to zero for now. Otherwise,
15374 we record an expression as the length, and that expression
15375 could lead to a very large value, which could eventually
15376 lead to us trying to allocate that much memory when creating
15377 a value of that type. */
15378 TYPE_LENGTH (type) = 0;
15379 }
c906108c
SS
15380 }
15381 else
15382 {
15383 TYPE_LENGTH (type) = 0;
15384 }
15385
2b4424c3
TT
15386 maybe_set_alignment (cu, die, type);
15387
5230b05a 15388 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15389 {
5230b05a
WT
15390 /* ICC<14 does not output the required DW_AT_declaration on
15391 incomplete types, but gives them a size of zero. */
422b1cb0 15392 TYPE_STUB (type) = 1;
685b1105
JK
15393 }
15394 else
15395 TYPE_STUB_SUPPORTED (type) = 1;
15396
dc718098 15397 if (die_is_declaration (die, cu))
876cecd0 15398 TYPE_STUB (type) = 1;
a6c727b2
DJ
15399 else if (attr == NULL && die->child == NULL
15400 && producer_is_realview (cu->producer))
15401 /* RealView does not output the required DW_AT_declaration
15402 on incomplete types. */
15403 TYPE_STUB (type) = 1;
dc718098 15404
c906108c
SS
15405 /* We need to add the type field to the die immediately so we don't
15406 infinitely recurse when dealing with pointers to the structure
0963b4bd 15407 type within the structure itself. */
1c379e20 15408 set_die_type (die, type, cu);
c906108c 15409
7e314c57
JK
15410 /* set_die_type should be already done. */
15411 set_descriptive_type (type, die, cu);
15412
c767944b
DJ
15413 return type;
15414}
15415
2ddeaf8a
TT
15416/* A helper for process_structure_scope that handles a single member
15417 DIE. */
15418
15419static void
15420handle_struct_member_die (struct die_info *child_die, struct type *type,
15421 struct field_info *fi,
15422 std::vector<struct symbol *> *template_args,
15423 struct dwarf2_cu *cu)
15424{
15425 if (child_die->tag == DW_TAG_member
15426 || child_die->tag == DW_TAG_variable
15427 || child_die->tag == DW_TAG_variant_part)
15428 {
15429 /* NOTE: carlton/2002-11-05: A C++ static data member
15430 should be a DW_TAG_member that is a declaration, but
15431 all versions of G++ as of this writing (so through at
15432 least 3.2.1) incorrectly generate DW_TAG_variable
15433 tags for them instead. */
15434 dwarf2_add_field (fi, child_die, cu);
15435 }
15436 else if (child_die->tag == DW_TAG_subprogram)
15437 {
15438 /* Rust doesn't have member functions in the C++ sense.
15439 However, it does emit ordinary functions as children
15440 of a struct DIE. */
15441 if (cu->language == language_rust)
15442 read_func_scope (child_die, cu);
15443 else
15444 {
15445 /* C++ member function. */
15446 dwarf2_add_member_fn (fi, child_die, type, cu);
15447 }
15448 }
15449 else if (child_die->tag == DW_TAG_inheritance)
15450 {
15451 /* C++ base class field. */
15452 dwarf2_add_field (fi, child_die, cu);
15453 }
15454 else if (type_can_define_types (child_die))
15455 dwarf2_add_type_defn (fi, child_die, cu);
15456 else if (child_die->tag == DW_TAG_template_type_param
15457 || child_die->tag == DW_TAG_template_value_param)
15458 {
15459 struct symbol *arg = new_symbol (child_die, NULL, cu);
15460
15461 if (arg != NULL)
15462 template_args->push_back (arg);
15463 }
15464 else if (child_die->tag == DW_TAG_variant)
15465 {
15466 /* In a variant we want to get the discriminant and also add a
15467 field for our sole member child. */
15468 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15469
bde09ab7 15470 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15471 variant_child != NULL;
15472 variant_child = sibling_die (variant_child))
15473 {
15474 if (variant_child->tag == DW_TAG_member)
15475 {
15476 handle_struct_member_die (variant_child, type, fi,
15477 template_args, cu);
15478 /* Only handle the one. */
15479 break;
15480 }
15481 }
15482
15483 /* We don't handle this but we might as well report it if we see
15484 it. */
15485 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15486 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15487 " - DIE at %s [in module %s]"),
15488 sect_offset_str (child_die->sect_off),
15489 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15490
15491 /* The first field was just added, so we can stash the
15492 discriminant there. */
be2daae6 15493 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15494 if (discr == NULL)
be2daae6 15495 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15496 else
be2daae6 15497 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15498 }
15499}
15500
c767944b
DJ
15501/* Finish creating a structure or union type, including filling in
15502 its members and creating a symbol for it. */
15503
15504static void
15505process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15506{
518817b3 15507 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15508 struct die_info *child_die;
c767944b
DJ
15509 struct type *type;
15510
15511 type = get_die_type (die, cu);
15512 if (type == NULL)
15513 type = read_structure_type (die, cu);
15514
2ddeaf8a
TT
15515 /* When reading a DW_TAG_variant_part, we need to notice when we
15516 read the discriminant member, so we can record it later in the
15517 discriminant_info. */
15518 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15519 sect_offset discr_offset {};
3e1d3d8c 15520 bool has_template_parameters = false;
2ddeaf8a
TT
15521
15522 if (is_variant_part)
15523 {
15524 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15525 if (discr == NULL)
15526 {
15527 /* Maybe it's a univariant form, an extension we support.
15528 In this case arrange not to check the offset. */
15529 is_variant_part = false;
15530 }
cd6c91b4 15531 else if (discr->form_is_ref ())
2ddeaf8a
TT
15532 {
15533 struct dwarf2_cu *target_cu = cu;
15534 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15535
15536 discr_offset = target_die->sect_off;
15537 }
15538 else
15539 {
b98664d3 15540 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15541 " - DIE at %s [in module %s]"),
15542 sect_offset_str (die->sect_off),
15543 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15544 is_variant_part = false;
15545 }
15546 }
15547
e142c38c 15548 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15549 {
15550 struct field_info fi;
2f4732b0 15551 std::vector<struct symbol *> template_args;
c906108c 15552
639d11d3 15553 child_die = die->child;
c906108c
SS
15554
15555 while (child_die && child_die->tag)
15556 {
2ddeaf8a 15557 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15558
2ddeaf8a 15559 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15560 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15561
c906108c
SS
15562 child_die = sibling_die (child_die);
15563 }
15564
34eaf542 15565 /* Attach template arguments to type. */
2f4732b0 15566 if (!template_args.empty ())
34eaf542 15567 {
3e1d3d8c 15568 has_template_parameters = true;
34eaf542 15569 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15570 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15571 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15572 = XOBNEWVEC (&objfile->objfile_obstack,
15573 struct symbol *,
15574 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15575 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15576 template_args.data (),
34eaf542
TT
15577 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15578 * sizeof (struct symbol *)));
34eaf542
TT
15579 }
15580
c906108c
SS
15581 /* Attach fields and member functions to the type. */
15582 if (fi.nfields)
e7c27a73 15583 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15584 if (!fi.fnfieldlists.empty ())
c906108c 15585 {
e7c27a73 15586 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15587
c5aa993b 15588 /* Get the type which refers to the base class (possibly this
c906108c 15589 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15590 class from the DW_AT_containing_type attribute. This use of
15591 DW_AT_containing_type is a GNU extension. */
c906108c 15592
e142c38c 15593 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15594 {
e7c27a73 15595 struct type *t = die_containing_type (die, cu);
c906108c 15596
ae6ae975 15597 set_type_vptr_basetype (type, t);
c906108c
SS
15598 if (type == t)
15599 {
c906108c
SS
15600 int i;
15601
15602 /* Our own class provides vtbl ptr. */
15603 for (i = TYPE_NFIELDS (t) - 1;
15604 i >= TYPE_N_BASECLASSES (t);
15605 --i)
15606 {
0d5cff50 15607 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15608
1168df01 15609 if (is_vtable_name (fieldname, cu))
c906108c 15610 {
ae6ae975 15611 set_type_vptr_fieldno (type, i);
c906108c
SS
15612 break;
15613 }
15614 }
15615
15616 /* Complain if virtual function table field not found. */
15617 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15618 complaint (_("virtual function table pointer "
3e43a32a 15619 "not found when defining class '%s'"),
e86ca25f 15620 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15621 }
15622 else
15623 {
ae6ae975 15624 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15625 }
15626 }
f6235d4c 15627 else if (cu->producer
61012eef 15628 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15629 {
15630 /* The IBM XLC compiler does not provide direct indication
15631 of the containing type, but the vtable pointer is
15632 always named __vfp. */
15633
15634 int i;
15635
15636 for (i = TYPE_NFIELDS (type) - 1;
15637 i >= TYPE_N_BASECLASSES (type);
15638 --i)
15639 {
15640 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15641 {
ae6ae975
DE
15642 set_type_vptr_fieldno (type, i);
15643 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15644 break;
15645 }
15646 }
15647 }
c906108c 15648 }
98751a41
JK
15649
15650 /* Copy fi.typedef_field_list linked list elements content into the
15651 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15652 if (!fi.typedef_field_list.empty ())
98751a41 15653 {
be2daae6 15654 int count = fi.typedef_field_list.size ();
98751a41 15655
a0d7a4ff 15656 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15657 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15658 = ((struct decl_field *)
be2daae6
TT
15659 TYPE_ALLOC (type,
15660 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15661 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15662
be2daae6
TT
15663 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15664 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15665 }
c767944b 15666
883fd55a
KS
15667 /* Copy fi.nested_types_list linked list elements content into the
15668 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15669 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15670 {
be2daae6 15671 int count = fi.nested_types_list.size ();
883fd55a
KS
15672
15673 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15674 TYPE_NESTED_TYPES_ARRAY (type)
15675 = ((struct decl_field *)
be2daae6
TT
15676 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15677 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15678
be2daae6
TT
15679 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15680 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15681 }
c906108c 15682 }
63d06c5c 15683
bb5ed363 15684 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15685 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15686 cu->rust_unions.push_back (type);
0b92b5bb 15687
90aeadfc
DC
15688 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15689 snapshots) has been known to create a die giving a declaration
15690 for a class that has, as a child, a die giving a definition for a
15691 nested class. So we have to process our children even if the
15692 current die is a declaration. Normally, of course, a declaration
15693 won't have any children at all. */
134d01f1 15694
ca040673
DE
15695 child_die = die->child;
15696
90aeadfc
DC
15697 while (child_die != NULL && child_die->tag)
15698 {
15699 if (child_die->tag == DW_TAG_member
15700 || child_die->tag == DW_TAG_variable
34eaf542
TT
15701 || child_die->tag == DW_TAG_inheritance
15702 || child_die->tag == DW_TAG_template_value_param
15703 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15704 {
90aeadfc 15705 /* Do nothing. */
134d01f1 15706 }
90aeadfc
DC
15707 else
15708 process_die (child_die, cu);
134d01f1 15709
90aeadfc 15710 child_die = sibling_die (child_die);
134d01f1
DJ
15711 }
15712
fa4028e9
JB
15713 /* Do not consider external references. According to the DWARF standard,
15714 these DIEs are identified by the fact that they have no byte_size
15715 attribute, and a declaration attribute. */
15716 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15717 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15718 {
15719 struct symbol *sym = new_symbol (die, type, cu);
15720
15721 if (has_template_parameters)
15722 {
a776957c
TT
15723 struct symtab *symtab;
15724 if (sym != nullptr)
15725 symtab = symbol_symtab (sym);
15726 else if (cu->line_header != nullptr)
15727 {
15728 /* Any related symtab will do. */
15729 symtab
7ba99d21 15730 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15731 }
15732 else
15733 {
15734 symtab = nullptr;
15735 complaint (_("could not find suitable "
15736 "symtab for template parameter"
15737 " - DIE at %s [in module %s]"),
15738 sect_offset_str (die->sect_off),
15739 objfile_name (objfile));
15740 }
15741
15742 if (symtab != nullptr)
15743 {
15744 /* Make sure that the symtab is set on the new symbols.
15745 Even though they don't appear in this symtab directly,
15746 other parts of gdb assume that symbols do, and this is
15747 reasonably true. */
15748 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15749 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15750 }
3e1d3d8c
TT
15751 }
15752 }
134d01f1
DJ
15753}
15754
55426c9d
JB
15755/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15756 update TYPE using some information only available in DIE's children. */
15757
15758static void
15759update_enumeration_type_from_children (struct die_info *die,
15760 struct type *type,
15761 struct dwarf2_cu *cu)
15762{
60f7655a 15763 struct die_info *child_die;
55426c9d
JB
15764 int unsigned_enum = 1;
15765 int flag_enum = 1;
15766 ULONGEST mask = 0;
55426c9d 15767
8268c778 15768 auto_obstack obstack;
55426c9d 15769
60f7655a
DE
15770 for (child_die = die->child;
15771 child_die != NULL && child_die->tag;
15772 child_die = sibling_die (child_die))
55426c9d
JB
15773 {
15774 struct attribute *attr;
15775 LONGEST value;
15776 const gdb_byte *bytes;
15777 struct dwarf2_locexpr_baton *baton;
15778 const char *name;
60f7655a 15779
55426c9d
JB
15780 if (child_die->tag != DW_TAG_enumerator)
15781 continue;
15782
15783 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15784 if (attr == NULL)
15785 continue;
15786
15787 name = dwarf2_name (child_die, cu);
15788 if (name == NULL)
15789 name = "<anonymous enumerator>";
15790
15791 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15792 &value, &bytes, &baton);
15793 if (value < 0)
15794 {
15795 unsigned_enum = 0;
15796 flag_enum = 0;
15797 }
15798 else if ((mask & value) != 0)
15799 flag_enum = 0;
15800 else
15801 mask |= value;
15802
15803 /* If we already know that the enum type is neither unsigned, nor
15804 a flag type, no need to look at the rest of the enumerates. */
15805 if (!unsigned_enum && !flag_enum)
15806 break;
55426c9d
JB
15807 }
15808
15809 if (unsigned_enum)
15810 TYPE_UNSIGNED (type) = 1;
15811 if (flag_enum)
15812 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15813}
15814
134d01f1
DJ
15815/* Given a DW_AT_enumeration_type die, set its type. We do not
15816 complete the type's fields yet, or create any symbols. */
c906108c 15817
f792889a 15818static struct type *
134d01f1 15819read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15820{
518817b3 15821 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15822 struct type *type;
c906108c 15823 struct attribute *attr;
0114d602 15824 const char *name;
134d01f1 15825
348e048f
DE
15826 /* If the definition of this type lives in .debug_types, read that type.
15827 Don't follow DW_AT_specification though, that will take us back up
15828 the chain and we want to go down. */
45e58e77 15829 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
435d3d88 15830 if (attr != nullptr)
348e048f 15831 {
ac9ec31b 15832 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15833
ac9ec31b 15834 /* The type's CU may not be the same as CU.
02142a6c 15835 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15836 return set_die_type (die, type, cu);
15837 }
15838
c906108c
SS
15839 type = alloc_type (objfile);
15840
15841 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15842 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15843 if (name != NULL)
e86ca25f 15844 TYPE_NAME (type) = name;
c906108c 15845
0626fc76
TT
15846 attr = dwarf2_attr (die, DW_AT_type, cu);
15847 if (attr != NULL)
15848 {
15849 struct type *underlying_type = die_type (die, cu);
15850
15851 TYPE_TARGET_TYPE (type) = underlying_type;
15852 }
15853
e142c38c 15854 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15855 if (attr != nullptr)
c906108c
SS
15856 {
15857 TYPE_LENGTH (type) = DW_UNSND (attr);
15858 }
15859 else
15860 {
15861 TYPE_LENGTH (type) = 0;
15862 }
15863
2b4424c3
TT
15864 maybe_set_alignment (cu, die, type);
15865
137033e9
JB
15866 /* The enumeration DIE can be incomplete. In Ada, any type can be
15867 declared as private in the package spec, and then defined only
15868 inside the package body. Such types are known as Taft Amendment
15869 Types. When another package uses such a type, an incomplete DIE
15870 may be generated by the compiler. */
02eb380e 15871 if (die_is_declaration (die, cu))
876cecd0 15872 TYPE_STUB (type) = 1;
02eb380e 15873
0626fc76
TT
15874 /* Finish the creation of this type by using the enum's children.
15875 We must call this even when the underlying type has been provided
15876 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15877 update_enumeration_type_from_children (die, type, cu);
15878
0626fc76
TT
15879 /* If this type has an underlying type that is not a stub, then we
15880 may use its attributes. We always use the "unsigned" attribute
15881 in this situation, because ordinarily we guess whether the type
15882 is unsigned -- but the guess can be wrong and the underlying type
15883 can tell us the reality. However, we defer to a local size
15884 attribute if one exists, because this lets the compiler override
15885 the underlying type if needed. */
15886 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15887 {
15888 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15889 if (TYPE_LENGTH (type) == 0)
15890 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
15891 if (TYPE_RAW_ALIGN (type) == 0
15892 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15893 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
15894 }
15895
3d567982
TT
15896 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15897
f792889a 15898 return set_die_type (die, type, cu);
134d01f1
DJ
15899}
15900
15901/* Given a pointer to a die which begins an enumeration, process all
15902 the dies that define the members of the enumeration, and create the
15903 symbol for the enumeration type.
15904
15905 NOTE: We reverse the order of the element list. */
15906
15907static void
15908process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15909{
f792889a 15910 struct type *this_type;
134d01f1 15911
f792889a
DJ
15912 this_type = get_die_type (die, cu);
15913 if (this_type == NULL)
15914 this_type = read_enumeration_type (die, cu);
9dc481d3 15915
639d11d3 15916 if (die->child != NULL)
c906108c 15917 {
9dc481d3
DE
15918 struct die_info *child_die;
15919 struct symbol *sym;
43816ebc 15920 std::vector<struct field> fields;
15d034d0 15921 const char *name;
9dc481d3 15922
639d11d3 15923 child_die = die->child;
c906108c
SS
15924 while (child_die && child_die->tag)
15925 {
15926 if (child_die->tag != DW_TAG_enumerator)
15927 {
e7c27a73 15928 process_die (child_die, cu);
c906108c
SS
15929 }
15930 else
15931 {
39cbfefa
DJ
15932 name = dwarf2_name (child_die, cu);
15933 if (name)
c906108c 15934 {
f792889a 15935 sym = new_symbol (child_die, this_type, cu);
c906108c 15936
43816ebc
TT
15937 fields.emplace_back ();
15938 struct field &field = fields.back ();
c906108c 15939
43816ebc
TT
15940 FIELD_NAME (field) = sym->linkage_name ();
15941 FIELD_TYPE (field) = NULL;
15942 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15943 FIELD_BITSIZE (field) = 0;
c906108c
SS
15944 }
15945 }
15946
15947 child_die = sibling_die (child_die);
15948 }
15949
43816ebc 15950 if (!fields.empty ())
c906108c 15951 {
43816ebc 15952 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15953 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15954 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15955 memcpy (TYPE_FIELDS (this_type), fields.data (),
15956 sizeof (struct field) * fields.size ());
c906108c 15957 }
c906108c 15958 }
134d01f1 15959
6c83ed52
TT
15960 /* If we are reading an enum from a .debug_types unit, and the enum
15961 is a declaration, and the enum is not the signatured type in the
15962 unit, then we do not want to add a symbol for it. Adding a
15963 symbol would in some cases obscure the true definition of the
15964 enum, giving users an incomplete type when the definition is
15965 actually available. Note that we do not want to do this for all
15966 enums which are just declarations, because C++0x allows forward
15967 enum declarations. */
3019eac3 15968 if (cu->per_cu->is_debug_types
6c83ed52
TT
15969 && die_is_declaration (die, cu))
15970 {
52dc124a 15971 struct signatured_type *sig_type;
6c83ed52 15972
c0f78cd4 15973 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15974 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15975 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15976 return;
15977 }
15978
f792889a 15979 new_symbol (die, this_type, cu);
c906108c
SS
15980}
15981
15982/* Extract all information from a DW_TAG_array_type DIE and put it in
15983 the DIE's type field. For now, this only handles one dimensional
15984 arrays. */
15985
f792889a 15986static struct type *
e7c27a73 15987read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15988{
518817b3 15989 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15990 struct die_info *child_die;
7e314c57 15991 struct type *type;
c906108c 15992 struct type *element_type, *range_type, *index_type;
c906108c 15993 struct attribute *attr;
15d034d0 15994 const char *name;
a405673c 15995 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15996 unsigned int bit_stride = 0;
c906108c 15997
e7c27a73 15998 element_type = die_type (die, cu);
c906108c 15999
7e314c57
JK
16000 /* The die_type call above may have already set the type for this DIE. */
16001 type = get_die_type (die, cu);
16002 if (type)
16003 return type;
16004
dc53a7ad
JB
16005 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16006 if (attr != NULL)
a405673c
JB
16007 {
16008 int stride_ok;
9a49df9d
AB
16009 struct type *prop_type
16010 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16011
16012 byte_stride_prop
16013 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16014 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16015 prop_type);
a405673c
JB
16016 if (!stride_ok)
16017 {
b98664d3 16018 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16019 " - DIE at %s [in module %s]"),
16020 sect_offset_str (die->sect_off),
518817b3 16021 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16022 /* Ignore this attribute. We will likely not be able to print
16023 arrays of this type correctly, but there is little we can do
16024 to help if we cannot read the attribute's value. */
16025 byte_stride_prop = NULL;
16026 }
16027 }
dc53a7ad
JB
16028
16029 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16030 if (attr != NULL)
16031 bit_stride = DW_UNSND (attr);
16032
c906108c
SS
16033 /* Irix 6.2 native cc creates array types without children for
16034 arrays with unspecified length. */
639d11d3 16035 if (die->child == NULL)
c906108c 16036 {
46bf5051 16037 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16038 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16039 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16040 byte_stride_prop, bit_stride);
f792889a 16041 return set_die_type (die, type, cu);
c906108c
SS
16042 }
16043
791afaa2 16044 std::vector<struct type *> range_types;
639d11d3 16045 child_die = die->child;
c906108c
SS
16046 while (child_die && child_die->tag)
16047 {
16048 if (child_die->tag == DW_TAG_subrange_type)
16049 {
f792889a 16050 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16051
f792889a 16052 if (child_type != NULL)
a02abb62 16053 {
0963b4bd
MS
16054 /* The range type was succesfully read. Save it for the
16055 array type creation. */
791afaa2 16056 range_types.push_back (child_type);
a02abb62 16057 }
c906108c
SS
16058 }
16059 child_die = sibling_die (child_die);
16060 }
16061
16062 /* Dwarf2 dimensions are output from left to right, create the
16063 necessary array types in backwards order. */
7ca2d3a3 16064
c906108c 16065 type = element_type;
7ca2d3a3
DL
16066
16067 if (read_array_order (die, cu) == DW_ORD_col_major)
16068 {
16069 int i = 0;
9a619af0 16070
791afaa2 16071 while (i < range_types.size ())
dc53a7ad 16072 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16073 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16074 }
16075 else
16076 {
791afaa2 16077 size_t ndim = range_types.size ();
7ca2d3a3 16078 while (ndim-- > 0)
dc53a7ad 16079 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16080 byte_stride_prop, bit_stride);
7ca2d3a3 16081 }
c906108c 16082
f5f8a009
EZ
16083 /* Understand Dwarf2 support for vector types (like they occur on
16084 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16085 array type. This is not part of the Dwarf2/3 standard yet, but a
16086 custom vendor extension. The main difference between a regular
16087 array and the vector variant is that vectors are passed by value
16088 to functions. */
e142c38c 16089 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16090 if (attr != nullptr)
ea37ba09 16091 make_vector_type (type);
f5f8a009 16092
dbc98a8b
KW
16093 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16094 implementation may choose to implement triple vectors using this
16095 attribute. */
16096 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16097 if (attr != nullptr)
dbc98a8b
KW
16098 {
16099 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16100 TYPE_LENGTH (type) = DW_UNSND (attr);
16101 else
b98664d3 16102 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16103 "than the total size of elements"));
dbc98a8b
KW
16104 }
16105
39cbfefa
DJ
16106 name = dwarf2_name (die, cu);
16107 if (name)
16108 TYPE_NAME (type) = name;
6e70227d 16109
2b4424c3
TT
16110 maybe_set_alignment (cu, die, type);
16111
0963b4bd 16112 /* Install the type in the die. */
7e314c57
JK
16113 set_die_type (die, type, cu);
16114
16115 /* set_die_type should be already done. */
b4ba55a1
JB
16116 set_descriptive_type (type, die, cu);
16117
7e314c57 16118 return type;
c906108c
SS
16119}
16120
7ca2d3a3 16121static enum dwarf_array_dim_ordering
6e70227d 16122read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16123{
16124 struct attribute *attr;
16125
16126 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16127
435d3d88 16128 if (attr != nullptr)
aead7601 16129 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16130
0963b4bd
MS
16131 /* GNU F77 is a special case, as at 08/2004 array type info is the
16132 opposite order to the dwarf2 specification, but data is still
16133 laid out as per normal fortran.
7ca2d3a3 16134
0963b4bd
MS
16135 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16136 version checking. */
7ca2d3a3 16137
905e0470
PM
16138 if (cu->language == language_fortran
16139 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16140 {
16141 return DW_ORD_row_major;
16142 }
16143
6e70227d 16144 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16145 {
16146 case array_column_major:
16147 return DW_ORD_col_major;
16148 case array_row_major:
16149 default:
16150 return DW_ORD_row_major;
16151 };
16152}
16153
72019c9c 16154/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16155 the DIE's type field. */
72019c9c 16156
f792889a 16157static struct type *
72019c9c
GM
16158read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16159{
7e314c57
JK
16160 struct type *domain_type, *set_type;
16161 struct attribute *attr;
f792889a 16162
7e314c57
JK
16163 domain_type = die_type (die, cu);
16164
16165 /* The die_type call above may have already set the type for this DIE. */
16166 set_type = get_die_type (die, cu);
16167 if (set_type)
16168 return set_type;
16169
16170 set_type = create_set_type (NULL, domain_type);
16171
16172 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16173 if (attr != nullptr)
d09039dd 16174 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16175
2b4424c3
TT
16176 maybe_set_alignment (cu, die, set_type);
16177
f792889a 16178 return set_die_type (die, set_type, cu);
72019c9c 16179}
7ca2d3a3 16180
0971de02
TT
16181/* A helper for read_common_block that creates a locexpr baton.
16182 SYM is the symbol which we are marking as computed.
16183 COMMON_DIE is the DIE for the common block.
16184 COMMON_LOC is the location expression attribute for the common
16185 block itself.
16186 MEMBER_LOC is the location expression attribute for the particular
16187 member of the common block that we are processing.
16188 CU is the CU from which the above come. */
16189
16190static void
16191mark_common_block_symbol_computed (struct symbol *sym,
16192 struct die_info *common_die,
16193 struct attribute *common_loc,
16194 struct attribute *member_loc,
16195 struct dwarf2_cu *cu)
16196{
518817b3
SM
16197 struct dwarf2_per_objfile *dwarf2_per_objfile
16198 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16199 struct objfile *objfile = dwarf2_per_objfile->objfile;
16200 struct dwarf2_locexpr_baton *baton;
16201 gdb_byte *ptr;
16202 unsigned int cu_off;
16203 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16204 LONGEST offset = 0;
16205
16206 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16207 gdb_assert (common_loc->form_is_block ());
16208 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16209 || member_loc->form_is_constant ());
0971de02 16210
8d749320 16211 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16212 baton->per_cu = cu->per_cu;
16213 gdb_assert (baton->per_cu);
16214
16215 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16216
cd6c91b4 16217 if (member_loc->form_is_constant ())
0971de02
TT
16218 {
16219 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16220 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16221 }
16222 else
16223 baton->size += DW_BLOCK (member_loc)->size;
16224
224c3ddb 16225 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16226 baton->data = ptr;
16227
16228 *ptr++ = DW_OP_call4;
9c541725 16229 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16230 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16231 ptr += 4;
16232
cd6c91b4 16233 if (member_loc->form_is_constant ())
0971de02
TT
16234 {
16235 *ptr++ = DW_OP_addr;
16236 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16237 ptr += cu->header.addr_size;
16238 }
16239 else
16240 {
16241 /* We have to copy the data here, because DW_OP_call4 will only
16242 use a DW_AT_location attribute. */
16243 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16244 ptr += DW_BLOCK (member_loc)->size;
16245 }
16246
16247 *ptr++ = DW_OP_plus;
16248 gdb_assert (ptr - baton->data == baton->size);
16249
0971de02 16250 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16251 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16252}
16253
4357ac6c
TT
16254/* Create appropriate locally-scoped variables for all the
16255 DW_TAG_common_block entries. Also create a struct common_block
16256 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16257 is used to separate the common blocks name namespace from regular
4357ac6c 16258 variable names. */
c906108c
SS
16259
16260static void
e7c27a73 16261read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16262{
0971de02
TT
16263 struct attribute *attr;
16264
16265 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16266 if (attr != nullptr)
0971de02
TT
16267 {
16268 /* Support the .debug_loc offsets. */
4fc6c0d5 16269 if (attr->form_is_block ())
0971de02
TT
16270 {
16271 /* Ok. */
16272 }
cd6c91b4 16273 else if (attr->form_is_section_offset ())
0971de02
TT
16274 {
16275 dwarf2_complex_location_expr_complaint ();
16276 attr = NULL;
16277 }
16278 else
16279 {
16280 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16281 "common block member");
16282 attr = NULL;
16283 }
16284 }
16285
639d11d3 16286 if (die->child != NULL)
c906108c 16287 {
518817b3 16288 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16289 struct die_info *child_die;
16290 size_t n_entries = 0, size;
16291 struct common_block *common_block;
16292 struct symbol *sym;
74ac6d43 16293
4357ac6c
TT
16294 for (child_die = die->child;
16295 child_die && child_die->tag;
16296 child_die = sibling_die (child_die))
16297 ++n_entries;
16298
16299 size = (sizeof (struct common_block)
16300 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16301 common_block
16302 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16303 size);
4357ac6c
TT
16304 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16305 common_block->n_entries = 0;
16306
16307 for (child_die = die->child;
16308 child_die && child_die->tag;
16309 child_die = sibling_die (child_die))
16310 {
16311 /* Create the symbol in the DW_TAG_common_block block in the current
16312 symbol scope. */
e7c27a73 16313 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16314 if (sym != NULL)
16315 {
16316 struct attribute *member_loc;
16317
16318 common_block->contents[common_block->n_entries++] = sym;
16319
16320 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16321 cu);
16322 if (member_loc)
16323 {
16324 /* GDB has handled this for a long time, but it is
16325 not specified by DWARF. It seems to have been
16326 emitted by gfortran at least as recently as:
16327 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16328 complaint (_("Variable in common block has "
0971de02 16329 "DW_AT_data_member_location "
9d8780f0
SM
16330 "- DIE at %s [in module %s]"),
16331 sect_offset_str (child_die->sect_off),
518817b3 16332 objfile_name (objfile));
0971de02 16333
cd6c91b4 16334 if (member_loc->form_is_section_offset ())
0971de02 16335 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16336 else if (member_loc->form_is_constant ()
4fc6c0d5 16337 || member_loc->form_is_block ())
0971de02 16338 {
435d3d88 16339 if (attr != nullptr)
0971de02
TT
16340 mark_common_block_symbol_computed (sym, die, attr,
16341 member_loc, cu);
16342 }
16343 else
16344 dwarf2_complex_location_expr_complaint ();
16345 }
16346 }
c906108c 16347 }
4357ac6c
TT
16348
16349 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16350 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16351 }
16352}
16353
0114d602 16354/* Create a type for a C++ namespace. */
d9fa45fe 16355
0114d602
DJ
16356static struct type *
16357read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16358{
518817b3 16359 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16360 const char *previous_prefix, *name;
9219021c 16361 int is_anonymous;
0114d602
DJ
16362 struct type *type;
16363
16364 /* For extensions, reuse the type of the original namespace. */
16365 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16366 {
16367 struct die_info *ext_die;
16368 struct dwarf2_cu *ext_cu = cu;
9a619af0 16369
0114d602
DJ
16370 ext_die = dwarf2_extension (die, &ext_cu);
16371 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16372
16373 /* EXT_CU may not be the same as CU.
02142a6c 16374 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16375 return set_die_type (die, type, cu);
16376 }
9219021c 16377
e142c38c 16378 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16379
16380 /* Now build the name of the current namespace. */
16381
0114d602
DJ
16382 previous_prefix = determine_prefix (die, cu);
16383 if (previous_prefix[0] != '\0')
16384 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16385 previous_prefix, name, 0, cu);
0114d602
DJ
16386
16387 /* Create the type. */
19f392bc 16388 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16389
60531b24 16390 return set_die_type (die, type, cu);
0114d602
DJ
16391}
16392
22cee43f 16393/* Read a namespace scope. */
0114d602
DJ
16394
16395static void
16396read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16397{
518817b3 16398 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16399 int is_anonymous;
9219021c 16400
5c4e30ca
DC
16401 /* Add a symbol associated to this if we haven't seen the namespace
16402 before. Also, add a using directive if it's an anonymous
16403 namespace. */
9219021c 16404
f2f0e013 16405 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16406 {
16407 struct type *type;
16408
0114d602 16409 type = read_type_die (die, cu);
e7c27a73 16410 new_symbol (die, type, cu);
5c4e30ca 16411
e8e80198 16412 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16413 if (is_anonymous)
0114d602
DJ
16414 {
16415 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16416
eb1e02fd 16417 std::vector<const char *> excludes;
804d2729 16418 add_using_directive (using_directives (cu),
22cee43f 16419 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16420 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16421 }
5c4e30ca 16422 }
9219021c 16423
639d11d3 16424 if (die->child != NULL)
d9fa45fe 16425 {
639d11d3 16426 struct die_info *child_die = die->child;
6e70227d 16427
d9fa45fe
DC
16428 while (child_die && child_die->tag)
16429 {
e7c27a73 16430 process_die (child_die, cu);
d9fa45fe
DC
16431 child_die = sibling_die (child_die);
16432 }
16433 }
38d518c9
EZ
16434}
16435
f55ee35c
JK
16436/* Read a Fortran module as type. This DIE can be only a declaration used for
16437 imported module. Still we need that type as local Fortran "use ... only"
16438 declaration imports depend on the created type in determine_prefix. */
16439
16440static struct type *
16441read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16442{
518817b3 16443 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16444 const char *module_name;
f55ee35c
JK
16445 struct type *type;
16446
16447 module_name = dwarf2_name (die, cu);
19f392bc 16448 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16449
f55ee35c
JK
16450 return set_die_type (die, type, cu);
16451}
16452
5d7cb8df
JK
16453/* Read a Fortran module. */
16454
16455static void
16456read_module (struct die_info *die, struct dwarf2_cu *cu)
16457{
16458 struct die_info *child_die = die->child;
530e8392
KB
16459 struct type *type;
16460
16461 type = read_type_die (die, cu);
16462 new_symbol (die, type, cu);
5d7cb8df 16463
5d7cb8df
JK
16464 while (child_die && child_die->tag)
16465 {
16466 process_die (child_die, cu);
16467 child_die = sibling_die (child_die);
16468 }
16469}
16470
38d518c9
EZ
16471/* Return the name of the namespace represented by DIE. Set
16472 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16473 namespace. */
16474
16475static const char *
e142c38c 16476namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16477{
16478 struct die_info *current_die;
16479 const char *name = NULL;
16480
16481 /* Loop through the extensions until we find a name. */
16482
16483 for (current_die = die;
16484 current_die != NULL;
f2f0e013 16485 current_die = dwarf2_extension (die, &cu))
38d518c9 16486 {
96553a0c
DE
16487 /* We don't use dwarf2_name here so that we can detect the absence
16488 of a name -> anonymous namespace. */
7d45c7c3 16489 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16490
38d518c9
EZ
16491 if (name != NULL)
16492 break;
16493 }
16494
16495 /* Is it an anonymous namespace? */
16496
16497 *is_anonymous = (name == NULL);
16498 if (*is_anonymous)
2b1dbab0 16499 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16500
16501 return name;
d9fa45fe
DC
16502}
16503
c906108c
SS
16504/* Extract all information from a DW_TAG_pointer_type DIE and add to
16505 the user defined type vector. */
16506
f792889a 16507static struct type *
e7c27a73 16508read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16509{
518817b3
SM
16510 struct gdbarch *gdbarch
16511 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16512 struct comp_unit_head *cu_header = &cu->header;
c906108c 16513 struct type *type;
8b2dbe47
KB
16514 struct attribute *attr_byte_size;
16515 struct attribute *attr_address_class;
16516 int byte_size, addr_class;
7e314c57
JK
16517 struct type *target_type;
16518
16519 target_type = die_type (die, cu);
c906108c 16520
7e314c57
JK
16521 /* The die_type call above may have already set the type for this DIE. */
16522 type = get_die_type (die, cu);
16523 if (type)
16524 return type;
16525
16526 type = lookup_pointer_type (target_type);
8b2dbe47 16527
e142c38c 16528 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16529 if (attr_byte_size)
16530 byte_size = DW_UNSND (attr_byte_size);
c906108c 16531 else
8b2dbe47
KB
16532 byte_size = cu_header->addr_size;
16533
e142c38c 16534 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16535 if (attr_address_class)
16536 addr_class = DW_UNSND (attr_address_class);
16537 else
16538 addr_class = DW_ADDR_none;
16539
2b4424c3
TT
16540 ULONGEST alignment = get_alignment (cu, die);
16541
16542 /* If the pointer size, alignment, or address class is different
16543 than the default, create a type variant marked as such and set
16544 the length accordingly. */
16545 if (TYPE_LENGTH (type) != byte_size
16546 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16547 && alignment != TYPE_RAW_ALIGN (type))
16548 || addr_class != DW_ADDR_none)
c906108c 16549 {
5e2b427d 16550 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16551 {
16552 int type_flags;
16553
849957d9 16554 type_flags = gdbarch_address_class_type_flags
5e2b427d 16555 (gdbarch, byte_size, addr_class);
876cecd0
TT
16556 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16557 == 0);
8b2dbe47
KB
16558 type = make_type_with_address_space (type, type_flags);
16559 }
16560 else if (TYPE_LENGTH (type) != byte_size)
16561 {
b98664d3 16562 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16563 }
2b4424c3
TT
16564 else if (TYPE_RAW_ALIGN (type) != alignment)
16565 {
b98664d3 16566 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16567 " - DIE at %s [in module %s]"),
16568 sect_offset_str (die->sect_off),
16569 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16570 }
6e70227d 16571 else
9a619af0
MS
16572 {
16573 /* Should we also complain about unhandled address classes? */
16574 }
c906108c 16575 }
8b2dbe47
KB
16576
16577 TYPE_LENGTH (type) = byte_size;
2b4424c3 16578 set_type_align (type, alignment);
f792889a 16579 return set_die_type (die, type, cu);
c906108c
SS
16580}
16581
16582/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16583 the user defined type vector. */
16584
f792889a 16585static struct type *
e7c27a73 16586read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16587{
16588 struct type *type;
16589 struct type *to_type;
16590 struct type *domain;
16591
e7c27a73
DJ
16592 to_type = die_type (die, cu);
16593 domain = die_containing_type (die, cu);
0d5de010 16594
7e314c57
JK
16595 /* The calls above may have already set the type for this DIE. */
16596 type = get_die_type (die, cu);
16597 if (type)
16598 return type;
16599
0d5de010
DJ
16600 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16601 type = lookup_methodptr_type (to_type);
7078baeb
TT
16602 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16603 {
518817b3
SM
16604 struct type *new_type
16605 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16606
16607 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16608 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16609 TYPE_VARARGS (to_type));
16610 type = lookup_methodptr_type (new_type);
16611 }
0d5de010
DJ
16612 else
16613 type = lookup_memberptr_type (to_type, domain);
c906108c 16614
f792889a 16615 return set_die_type (die, type, cu);
c906108c
SS
16616}
16617
4297a3f0 16618/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16619 the user defined type vector. */
16620
f792889a 16621static struct type *
4297a3f0
AV
16622read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16623 enum type_code refcode)
c906108c 16624{
e7c27a73 16625 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16626 struct type *type, *target_type;
c906108c
SS
16627 struct attribute *attr;
16628
4297a3f0
AV
16629 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16630
7e314c57
JK
16631 target_type = die_type (die, cu);
16632
16633 /* The die_type call above may have already set the type for this DIE. */
16634 type = get_die_type (die, cu);
16635 if (type)
16636 return type;
16637
4297a3f0 16638 type = lookup_reference_type (target_type, refcode);
e142c38c 16639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16640 if (attr != nullptr)
c906108c
SS
16641 {
16642 TYPE_LENGTH (type) = DW_UNSND (attr);
16643 }
16644 else
16645 {
107d2387 16646 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16647 }
2b4424c3 16648 maybe_set_alignment (cu, die, type);
f792889a 16649 return set_die_type (die, type, cu);
c906108c
SS
16650}
16651
cf363f18
MW
16652/* Add the given cv-qualifiers to the element type of the array. GCC
16653 outputs DWARF type qualifiers that apply to an array, not the
16654 element type. But GDB relies on the array element type to carry
16655 the cv-qualifiers. This mimics section 6.7.3 of the C99
16656 specification. */
16657
16658static struct type *
16659add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16660 struct type *base_type, int cnst, int voltl)
16661{
16662 struct type *el_type, *inner_array;
16663
16664 base_type = copy_type (base_type);
16665 inner_array = base_type;
16666
16667 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16668 {
16669 TYPE_TARGET_TYPE (inner_array) =
16670 copy_type (TYPE_TARGET_TYPE (inner_array));
16671 inner_array = TYPE_TARGET_TYPE (inner_array);
16672 }
16673
16674 el_type = TYPE_TARGET_TYPE (inner_array);
16675 cnst |= TYPE_CONST (el_type);
16676 voltl |= TYPE_VOLATILE (el_type);
16677 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16678
16679 return set_die_type (die, base_type, cu);
16680}
16681
f792889a 16682static struct type *
e7c27a73 16683read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16684{
f792889a 16685 struct type *base_type, *cv_type;
c906108c 16686
e7c27a73 16687 base_type = die_type (die, cu);
7e314c57
JK
16688
16689 /* The die_type call above may have already set the type for this DIE. */
16690 cv_type = get_die_type (die, cu);
16691 if (cv_type)
16692 return cv_type;
16693
2f608a3a
KW
16694 /* In case the const qualifier is applied to an array type, the element type
16695 is so qualified, not the array type (section 6.7.3 of C99). */
16696 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16697 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16698
f792889a
DJ
16699 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16700 return set_die_type (die, cv_type, cu);
c906108c
SS
16701}
16702
f792889a 16703static struct type *
e7c27a73 16704read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16705{
f792889a 16706 struct type *base_type, *cv_type;
c906108c 16707
e7c27a73 16708 base_type = die_type (die, cu);
7e314c57
JK
16709
16710 /* The die_type call above may have already set the type for this DIE. */
16711 cv_type = get_die_type (die, cu);
16712 if (cv_type)
16713 return cv_type;
16714
cf363f18
MW
16715 /* In case the volatile qualifier is applied to an array type, the
16716 element type is so qualified, not the array type (section 6.7.3
16717 of C99). */
16718 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16719 return add_array_cv_type (die, cu, base_type, 0, 1);
16720
f792889a
DJ
16721 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16722 return set_die_type (die, cv_type, cu);
c906108c
SS
16723}
16724
06d66ee9
TT
16725/* Handle DW_TAG_restrict_type. */
16726
16727static struct type *
16728read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16729{
16730 struct type *base_type, *cv_type;
16731
16732 base_type = die_type (die, cu);
16733
16734 /* The die_type call above may have already set the type for this DIE. */
16735 cv_type = get_die_type (die, cu);
16736 if (cv_type)
16737 return cv_type;
16738
16739 cv_type = make_restrict_type (base_type);
16740 return set_die_type (die, cv_type, cu);
16741}
16742
a2c2acaf
MW
16743/* Handle DW_TAG_atomic_type. */
16744
16745static struct type *
16746read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16747{
16748 struct type *base_type, *cv_type;
16749
16750 base_type = die_type (die, cu);
16751
16752 /* The die_type call above may have already set the type for this DIE. */
16753 cv_type = get_die_type (die, cu);
16754 if (cv_type)
16755 return cv_type;
16756
16757 cv_type = make_atomic_type (base_type);
16758 return set_die_type (die, cv_type, cu);
16759}
16760
c906108c
SS
16761/* Extract all information from a DW_TAG_string_type DIE and add to
16762 the user defined type vector. It isn't really a user defined type,
16763 but it behaves like one, with other DIE's using an AT_user_def_type
16764 attribute to reference it. */
16765
f792889a 16766static struct type *
e7c27a73 16767read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16768{
518817b3 16769 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16771 struct type *type, *range_type, *index_type, *char_type;
16772 struct attribute *attr;
216a7e6b
AB
16773 struct dynamic_prop prop;
16774 bool length_is_constant = true;
16775 LONGEST length;
16776
16777 /* There are a couple of places where bit sizes might be made use of
16778 when parsing a DW_TAG_string_type, however, no producer that we know
16779 of make use of these. Handling bit sizes that are a multiple of the
16780 byte size is easy enough, but what about other bit sizes? Lets deal
16781 with that problem when we have to. Warn about these attributes being
16782 unsupported, then parse the type and ignore them like we always
16783 have. */
16784 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16785 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16786 {
16787 static bool warning_printed = false;
16788 if (!warning_printed)
16789 {
16790 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16791 "currently supported on DW_TAG_string_type."));
16792 warning_printed = true;
16793 }
16794 }
c906108c 16795
e142c38c 16796 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16797 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16798 {
16799 /* The string length describes the location at which the length of
16800 the string can be found. The size of the length field can be
16801 specified with one of the attributes below. */
16802 struct type *prop_type;
16803 struct attribute *len
16804 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16805 if (len == nullptr)
16806 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16807 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16808 {
16809 /* Pass 0 as the default as we know this attribute is constant
16810 and the default value will not be returned. */
16811 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16812 prop_type = dwarf2_per_cu_int_type (cu->per_cu, sz, true);
16813 }
16814 else
16815 {
16816 /* If the size is not specified then we assume it is the size of
16817 an address on this target. */
16818 prop_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, true);
16819 }
16820
16821 /* Convert the attribute into a dynamic property. */
16822 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16823 length = 1;
16824 else
16825 length_is_constant = false;
16826 }
16827 else if (attr != nullptr)
16828 {
16829 /* This DW_AT_string_length just contains the length with no
16830 indirection. There's no need to create a dynamic property in this
16831 case. Pass 0 for the default value as we know it will not be
16832 returned in this case. */
16833 length = dwarf2_get_attr_constant_value (attr, 0);
16834 }
16835 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16836 {
216a7e6b
AB
16837 /* We don't currently support non-constant byte sizes for strings. */
16838 length = dwarf2_get_attr_constant_value (attr, 1);
c906108c
SS
16839 }
16840 else
16841 {
216a7e6b
AB
16842 /* Use 1 as a fallback length if we have nothing else. */
16843 length = 1;
c906108c 16844 }
6ccb9162 16845
46bf5051 16846 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16847 if (length_is_constant)
16848 range_type = create_static_range_type (NULL, index_type, 1, length);
16849 else
16850 {
16851 struct dynamic_prop low_bound;
16852
16853 low_bound.kind = PROP_CONST;
16854 low_bound.data.const_val = 1;
16855 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16856 }
3b7538c0
UW
16857 char_type = language_string_char_type (cu->language_defn, gdbarch);
16858 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16859
f792889a 16860 return set_die_type (die, type, cu);
c906108c
SS
16861}
16862
4d804846
JB
16863/* Assuming that DIE corresponds to a function, returns nonzero
16864 if the function is prototyped. */
16865
16866static int
16867prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16868{
16869 struct attribute *attr;
16870
16871 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16872 if (attr && (DW_UNSND (attr) != 0))
16873 return 1;
16874
16875 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16876 is only meaningful for C, but the concept also extends to other
4d804846
JB
16877 languages that allow unprototyped functions (Eg: Objective C).
16878 For all other languages, assume that functions are always
16879 prototyped. */
16880 if (cu->language != language_c
16881 && cu->language != language_objc
16882 && cu->language != language_opencl)
16883 return 1;
16884
16885 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16886 prototyped and unprototyped functions; default to prototyped,
16887 since that is more common in modern code (and RealView warns
16888 about unprototyped functions). */
16889 if (producer_is_realview (cu->producer))
16890 return 1;
16891
16892 return 0;
16893}
16894
c906108c
SS
16895/* Handle DIES due to C code like:
16896
16897 struct foo
c5aa993b
JM
16898 {
16899 int (*funcp)(int a, long l);
16900 int b;
16901 };
c906108c 16902
0963b4bd 16903 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16904
f792889a 16905static struct type *
e7c27a73 16906read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16907{
518817b3 16908 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16909 struct type *type; /* Type that this function returns. */
16910 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16911 struct attribute *attr;
16912
e7c27a73 16913 type = die_type (die, cu);
7e314c57
JK
16914
16915 /* The die_type call above may have already set the type for this DIE. */
16916 ftype = get_die_type (die, cu);
16917 if (ftype)
16918 return ftype;
16919
0c8b41f1 16920 ftype = lookup_function_type (type);
c906108c 16921
4d804846 16922 if (prototyped_function_p (die, cu))
a6c727b2 16923 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16924
c055b101
CV
16925 /* Store the calling convention in the type if it's available in
16926 the subroutine die. Otherwise set the calling convention to
16927 the default value DW_CC_normal. */
16928 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16929 if (attr != nullptr
16930 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16931 TYPE_CALLING_CONVENTION (ftype)
16932 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16933 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16934 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16935 else
16936 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16937
743649fd
MW
16938 /* Record whether the function returns normally to its caller or not
16939 if the DWARF producer set that information. */
16940 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16941 if (attr && (DW_UNSND (attr) != 0))
16942 TYPE_NO_RETURN (ftype) = 1;
16943
76c10ea2
GM
16944 /* We need to add the subroutine type to the die immediately so
16945 we don't infinitely recurse when dealing with parameters
0963b4bd 16946 declared as the same subroutine type. */
76c10ea2 16947 set_die_type (die, ftype, cu);
6e70227d 16948
639d11d3 16949 if (die->child != NULL)
c906108c 16950 {
bb5ed363 16951 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16952 struct die_info *child_die;
8072405b 16953 int nparams, iparams;
c906108c
SS
16954
16955 /* Count the number of parameters.
16956 FIXME: GDB currently ignores vararg functions, but knows about
16957 vararg member functions. */
8072405b 16958 nparams = 0;
639d11d3 16959 child_die = die->child;
c906108c
SS
16960 while (child_die && child_die->tag)
16961 {
16962 if (child_die->tag == DW_TAG_formal_parameter)
16963 nparams++;
16964 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16965 TYPE_VARARGS (ftype) = 1;
c906108c
SS
16966 child_die = sibling_die (child_die);
16967 }
16968
16969 /* Allocate storage for parameters and fill them in. */
16970 TYPE_NFIELDS (ftype) = nparams;
16971 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16972 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16973
8072405b
JK
16974 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16975 even if we error out during the parameters reading below. */
16976 for (iparams = 0; iparams < nparams; iparams++)
16977 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16978
16979 iparams = 0;
639d11d3 16980 child_die = die->child;
c906108c
SS
16981 while (child_die && child_die->tag)
16982 {
16983 if (child_die->tag == DW_TAG_formal_parameter)
16984 {
3ce3b1ba
PA
16985 struct type *arg_type;
16986
16987 /* DWARF version 2 has no clean way to discern C++
16988 static and non-static member functions. G++ helps
16989 GDB by marking the first parameter for non-static
16990 member functions (which is the this pointer) as
16991 artificial. We pass this information to
16992 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16993
16994 DWARF version 3 added DW_AT_object_pointer, which GCC
16995 4.5 does not yet generate. */
e142c38c 16996 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16997 if (attr != nullptr)
c906108c
SS
16998 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16999 else
9c37b5ae 17000 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17001 arg_type = die_type (child_die, cu);
17002
17003 /* RealView does not mark THIS as const, which the testsuite
17004 expects. GCC marks THIS as const in method definitions,
17005 but not in the class specifications (GCC PR 43053). */
17006 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17007 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17008 {
17009 int is_this = 0;
17010 struct dwarf2_cu *arg_cu = cu;
17011 const char *name = dwarf2_name (child_die, cu);
17012
17013 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17014 if (attr != nullptr)
3ce3b1ba
PA
17015 {
17016 /* If the compiler emits this, use it. */
17017 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17018 is_this = 1;
17019 }
17020 else if (name && strcmp (name, "this") == 0)
17021 /* Function definitions will have the argument names. */
17022 is_this = 1;
17023 else if (name == NULL && iparams == 0)
17024 /* Declarations may not have the names, so like
17025 elsewhere in GDB, assume an artificial first
17026 argument is "this". */
17027 is_this = 1;
17028
17029 if (is_this)
17030 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17031 arg_type, 0);
17032 }
17033
17034 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17035 iparams++;
17036 }
17037 child_die = sibling_die (child_die);
17038 }
17039 }
17040
76c10ea2 17041 return ftype;
c906108c
SS
17042}
17043
f792889a 17044static struct type *
e7c27a73 17045read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17046{
518817b3 17047 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17048 const char *name = NULL;
3c8e0968 17049 struct type *this_type, *target_type;
c906108c 17050
94af9270 17051 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17052 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17053 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17054 set_die_type (die, this_type, cu);
3c8e0968
DE
17055 target_type = die_type (die, cu);
17056 if (target_type != this_type)
17057 TYPE_TARGET_TYPE (this_type) = target_type;
17058 else
17059 {
17060 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17061 spec and cause infinite loops in GDB. */
b98664d3 17062 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17063 "- DIE at %s [in module %s]"),
17064 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17065 TYPE_TARGET_TYPE (this_type) = NULL;
17066 }
f792889a 17067 return this_type;
c906108c
SS
17068}
17069
9b790ce7
UW
17070/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17071 (which may be different from NAME) to the architecture back-end to allow
17072 it to guess the correct format if necessary. */
17073
17074static struct type *
17075dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17076 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
17077{
17078 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17079 const struct floatformat **format;
17080 struct type *type;
17081
17082 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17083 if (format)
103a685e 17084 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17085 else
77b7c781 17086 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17087
17088 return type;
17089}
17090
eb77c9df
AB
17091/* Allocate an integer type of size BITS and name NAME. */
17092
17093static struct type *
17094dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17095 int bits, int unsigned_p, const char *name)
17096{
17097 struct type *type;
17098
17099 /* Versions of Intel's C Compiler generate an integer type called "void"
17100 instead of using DW_TAG_unspecified_type. This has been seen on
17101 at least versions 14, 17, and 18. */
35ee2dc2
AB
17102 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17103 && strcmp (name, "void") == 0)
eb77c9df
AB
17104 type = objfile_type (objfile)->builtin_void;
17105 else
17106 type = init_integer_type (objfile, bits, unsigned_p, name);
17107
17108 return type;
17109}
17110
8bdc1658
AB
17111/* Initialise and return a floating point type of size BITS suitable for
17112 use as a component of a complex number. The NAME_HINT is passed through
17113 when initialising the floating point type and is the name of the complex
17114 type.
17115
17116 As DWARF doesn't currently provide an explicit name for the components
17117 of a complex number, but it can be helpful to have these components
17118 named, we try to select a suitable name based on the size of the
17119 component. */
17120static struct type *
17121dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17122 struct objfile *objfile,
103a685e
TT
17123 int bits, const char *name_hint,
17124 enum bfd_endian byte_order)
8bdc1658
AB
17125{
17126 gdbarch *gdbarch = get_objfile_arch (objfile);
17127 struct type *tt = nullptr;
17128
35add35e
AB
17129 /* Try to find a suitable floating point builtin type of size BITS.
17130 We're going to use the name of this type as the name for the complex
17131 target type that we are about to create. */
1db455a7 17132 switch (cu->language)
8bdc1658 17133 {
1db455a7
AB
17134 case language_fortran:
17135 switch (bits)
17136 {
17137 case 32:
17138 tt = builtin_f_type (gdbarch)->builtin_real;
17139 break;
17140 case 64:
17141 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17142 break;
17143 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17144 case 128:
17145 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17146 break;
17147 }
8bdc1658 17148 break;
1db455a7
AB
17149 default:
17150 switch (bits)
17151 {
17152 case 32:
17153 tt = builtin_type (gdbarch)->builtin_float;
17154 break;
17155 case 64:
17156 tt = builtin_type (gdbarch)->builtin_double;
17157 break;
17158 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17159 case 128:
17160 tt = builtin_type (gdbarch)->builtin_long_double;
17161 break;
17162 }
8bdc1658
AB
17163 break;
17164 }
17165
35add35e
AB
17166 /* If the type we found doesn't match the size we were looking for, then
17167 pretend we didn't find a type at all, the complex target type we
17168 create will then be nameless. */
a12e5744 17169 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17170 tt = nullptr;
17171
8bdc1658 17172 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17173 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17174}
17175
c906108c
SS
17176/* Find a representation of a given base type and install
17177 it in the TYPE field of the die. */
17178
f792889a 17179static struct type *
e7c27a73 17180read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17181{
518817b3 17182 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17183 struct type *type;
17184 struct attribute *attr;
19f392bc 17185 int encoding = 0, bits = 0;
15d034d0 17186 const char *name;
34877895 17187 gdbarch *arch;
c906108c 17188
e142c38c 17189 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17190 if (attr != nullptr)
34877895 17191 encoding = DW_UNSND (attr);
e142c38c 17192 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17193 if (attr != nullptr)
34877895 17194 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17195 name = dwarf2_name (die, cu);
6ccb9162 17196 if (!name)
34877895 17197 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
17198
17199 arch = get_objfile_arch (objfile);
17200 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17201
34877895
PJ
17202 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17203 if (attr)
103a685e
TT
17204 {
17205 int endianity = DW_UNSND (attr);
17206
17207 switch (endianity)
17208 {
17209 case DW_END_big:
17210 byte_order = BFD_ENDIAN_BIG;
17211 break;
17212 case DW_END_little:
17213 byte_order = BFD_ENDIAN_LITTLE;
17214 break;
17215 default:
17216 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17217 break;
17218 }
17219 }
6ccb9162
UW
17220
17221 switch (encoding)
c906108c 17222 {
6ccb9162
UW
17223 case DW_ATE_address:
17224 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17225 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17226 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17227 break;
17228 case DW_ATE_boolean:
19f392bc 17229 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17230 break;
17231 case DW_ATE_complex_float:
103a685e
TT
17232 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17233 byte_order);
19f392bc 17234 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17235 break;
17236 case DW_ATE_decimal_float:
19f392bc 17237 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17238 break;
17239 case DW_ATE_float:
103a685e 17240 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17241 break;
17242 case DW_ATE_signed:
eb77c9df 17243 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17244 break;
17245 case DW_ATE_unsigned:
3b2b8fea
TT
17246 if (cu->language == language_fortran
17247 && name
61012eef 17248 && startswith (name, "character("))
19f392bc
UW
17249 type = init_character_type (objfile, bits, 1, name);
17250 else
eb77c9df 17251 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17252 break;
17253 case DW_ATE_signed_char:
6e70227d 17254 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17255 || cu->language == language_pascal
17256 || cu->language == language_fortran)
19f392bc
UW
17257 type = init_character_type (objfile, bits, 0, name);
17258 else
eb77c9df 17259 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17260 break;
17261 case DW_ATE_unsigned_char:
868a0084 17262 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17263 || cu->language == language_pascal
c44af4eb
TT
17264 || cu->language == language_fortran
17265 || cu->language == language_rust)
19f392bc
UW
17266 type = init_character_type (objfile, bits, 1, name);
17267 else
eb77c9df 17268 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17269 break;
75079b2b 17270 case DW_ATE_UTF:
53e710ac 17271 {
53e710ac
PA
17272 if (bits == 16)
17273 type = builtin_type (arch)->builtin_char16;
17274 else if (bits == 32)
17275 type = builtin_type (arch)->builtin_char32;
17276 else
17277 {
b98664d3 17278 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17279 bits);
eb77c9df 17280 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17281 }
17282 return set_die_type (die, type, cu);
17283 }
75079b2b
TT
17284 break;
17285
6ccb9162 17286 default:
b98664d3 17287 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17288 dwarf_type_encoding_name (encoding));
77b7c781 17289 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17290 break;
c906108c 17291 }
6ccb9162 17292
0114d602 17293 if (name && strcmp (name, "char") == 0)
876cecd0 17294 TYPE_NOSIGN (type) = 1;
0114d602 17295
2b4424c3
TT
17296 maybe_set_alignment (cu, die, type);
17297
103a685e 17298 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17299
f792889a 17300 return set_die_type (die, type, cu);
c906108c
SS
17301}
17302
80180f79
SA
17303/* Parse dwarf attribute if it's a block, reference or constant and put the
17304 resulting value of the attribute into struct bound_prop.
17305 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17306
17307static int
17308attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17309 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17310 struct type *default_type)
80180f79
SA
17311{
17312 struct dwarf2_property_baton *baton;
518817b3
SM
17313 struct obstack *obstack
17314 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17315
9a49df9d
AB
17316 gdb_assert (default_type != NULL);
17317
80180f79
SA
17318 if (attr == NULL || prop == NULL)
17319 return 0;
17320
4fc6c0d5 17321 if (attr->form_is_block ())
80180f79 17322 {
8d749320 17323 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17324 baton->property_type = default_type;
80180f79
SA
17325 baton->locexpr.per_cu = cu->per_cu;
17326 baton->locexpr.size = DW_BLOCK (attr)->size;
17327 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17328 switch (attr->name)
17329 {
17330 case DW_AT_string_length:
17331 baton->locexpr.is_reference = true;
17332 break;
17333 default:
17334 baton->locexpr.is_reference = false;
17335 break;
17336 }
80180f79
SA
17337 prop->data.baton = baton;
17338 prop->kind = PROP_LOCEXPR;
17339 gdb_assert (prop->data.baton != NULL);
17340 }
cd6c91b4 17341 else if (attr->form_is_ref ())
80180f79
SA
17342 {
17343 struct dwarf2_cu *target_cu = cu;
17344 struct die_info *target_die;
17345 struct attribute *target_attr;
17346
17347 target_die = follow_die_ref (die, attr, &target_cu);
17348 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17349 if (target_attr == NULL)
17350 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17351 target_cu);
80180f79
SA
17352 if (target_attr == NULL)
17353 return 0;
17354
df25ebbd 17355 switch (target_attr->name)
80180f79 17356 {
df25ebbd 17357 case DW_AT_location:
cd6c91b4 17358 if (target_attr->form_is_section_offset ())
df25ebbd 17359 {
8d749320 17360 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17361 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17362 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17363 prop->data.baton = baton;
17364 prop->kind = PROP_LOCLIST;
17365 gdb_assert (prop->data.baton != NULL);
17366 }
4fc6c0d5 17367 else if (target_attr->form_is_block ())
df25ebbd 17368 {
8d749320 17369 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17370 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17371 baton->locexpr.per_cu = cu->per_cu;
17372 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17373 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17374 baton->locexpr.is_reference = true;
df25ebbd
JB
17375 prop->data.baton = baton;
17376 prop->kind = PROP_LOCEXPR;
17377 gdb_assert (prop->data.baton != NULL);
17378 }
17379 else
17380 {
17381 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17382 "dynamic property");
17383 return 0;
17384 }
17385 break;
17386 case DW_AT_data_member_location:
17387 {
17388 LONGEST offset;
17389
17390 if (!handle_data_member_location (target_die, target_cu,
17391 &offset))
17392 return 0;
17393
8d749320 17394 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17395 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17396 target_cu);
df25ebbd
JB
17397 baton->offset_info.offset = offset;
17398 baton->offset_info.type = die_type (target_die, target_cu);
17399 prop->data.baton = baton;
17400 prop->kind = PROP_ADDR_OFFSET;
17401 break;
17402 }
80180f79
SA
17403 }
17404 }
cd6c91b4 17405 else if (attr->form_is_constant ())
80180f79
SA
17406 {
17407 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17408 prop->kind = PROP_CONST;
17409 }
17410 else
17411 {
17412 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17413 dwarf2_name (die, cu));
17414 return 0;
17415 }
17416
17417 return 1;
17418}
17419
11a8b164
AB
17420/* Find an integer type SIZE_IN_BYTES bytes in size and return it.
17421 UNSIGNED_P controls if the integer is unsigned or not. */
9a49df9d
AB
17422
17423static struct type *
11a8b164
AB
17424dwarf2_per_cu_int_type (struct dwarf2_per_cu_data *per_cu,
17425 int size_in_bytes, bool unsigned_p)
9a49df9d
AB
17426{
17427 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9a49df9d
AB
17428 struct type *int_type;
17429
17430 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17431#define TRY_TYPE(F) \
17432 int_type = (unsigned_p \
17433 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17434 : objfile_type (objfile)->builtin_ ## F); \
17435 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17436 return int_type
17437
17438 TRY_TYPE (char);
17439 TRY_TYPE (short);
17440 TRY_TYPE (int);
17441 TRY_TYPE (long);
17442 TRY_TYPE (long_long);
17443
17444#undef TRY_TYPE
17445
17446 gdb_assert_not_reached ("unable to find suitable integer type");
17447}
17448
11a8b164
AB
17449/* Find an integer type the same size as the address size given in the
17450 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17451 is unsigned or not. */
17452
17453static struct type *
17454dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17455 bool unsigned_p)
17456{
17457 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17458 return dwarf2_per_cu_int_type (per_cu, addr_size, unsigned_p);
17459}
17460
b86352cf
AB
17461/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17462 present (which is valid) then compute the default type based on the
17463 compilation units address size. */
17464
17465static struct type *
17466read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17467{
17468 struct type *index_type = die_type (die, cu);
17469
17470 /* Dwarf-2 specifications explicitly allows to create subrange types
17471 without specifying a base type.
17472 In that case, the base type must be set to the type of
17473 the lower bound, upper bound or count, in that order, if any of these
17474 three attributes references an object that has a type.
17475 If no base type is found, the Dwarf-2 specifications say that
17476 a signed integer type of size equal to the size of an address should
17477 be used.
17478 For the following C code: `extern char gdb_int [];'
17479 GCC produces an empty range DIE.
17480 FIXME: muller/2010-05-28: Possible references to object for low bound,
17481 high bound or count are not yet handled by this code. */
17482 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17483 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17484
17485 return index_type;
17486}
17487
a02abb62
JB
17488/* Read the given DW_AT_subrange DIE. */
17489
f792889a 17490static struct type *
a02abb62
JB
17491read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17492{
4c9ad8c2 17493 struct type *base_type, *orig_base_type;
a02abb62
JB
17494 struct type *range_type;
17495 struct attribute *attr;
729efb13 17496 struct dynamic_prop low, high;
4fae6e18 17497 int low_default_is_valid;
c451ebe5 17498 int high_bound_is_count = 0;
15d034d0 17499 const char *name;
d359392f 17500 ULONGEST negative_mask;
e77813c8 17501
b86352cf
AB
17502 orig_base_type = read_subrange_index_type (die, cu);
17503
4c9ad8c2
TT
17504 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17505 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17506 creating the range type, but we use the result of check_typedef
17507 when examining properties of the type. */
17508 base_type = check_typedef (orig_base_type);
a02abb62 17509
7e314c57
JK
17510 /* The die_type call above may have already set the type for this DIE. */
17511 range_type = get_die_type (die, cu);
17512 if (range_type)
17513 return range_type;
17514
729efb13
SA
17515 low.kind = PROP_CONST;
17516 high.kind = PROP_CONST;
17517 high.data.const_val = 0;
17518
4fae6e18
JK
17519 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17520 omitting DW_AT_lower_bound. */
17521 switch (cu->language)
6e70227d 17522 {
4fae6e18
JK
17523 case language_c:
17524 case language_cplus:
729efb13 17525 low.data.const_val = 0;
4fae6e18
JK
17526 low_default_is_valid = 1;
17527 break;
17528 case language_fortran:
729efb13 17529 low.data.const_val = 1;
4fae6e18
JK
17530 low_default_is_valid = 1;
17531 break;
17532 case language_d:
4fae6e18 17533 case language_objc:
c44af4eb 17534 case language_rust:
729efb13 17535 low.data.const_val = 0;
4fae6e18
JK
17536 low_default_is_valid = (cu->header.version >= 4);
17537 break;
17538 case language_ada:
17539 case language_m2:
17540 case language_pascal:
729efb13 17541 low.data.const_val = 1;
4fae6e18
JK
17542 low_default_is_valid = (cu->header.version >= 4);
17543 break;
17544 default:
729efb13 17545 low.data.const_val = 0;
4fae6e18
JK
17546 low_default_is_valid = 0;
17547 break;
a02abb62
JB
17548 }
17549
e142c38c 17550 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17551 if (attr != nullptr)
9a49df9d 17552 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17553 else if (!low_default_is_valid)
b98664d3 17554 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17555 "- DIE at %s [in module %s]"),
17556 sect_offset_str (die->sect_off),
518817b3 17557 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17558
506f5c41
TV
17559 struct attribute *attr_ub, *attr_count;
17560 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17561 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17562 {
506f5c41 17563 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17564 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17565 {
c451ebe5
SA
17566 /* If bounds are constant do the final calculation here. */
17567 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17568 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17569 else
17570 high_bound_is_count = 1;
c2ff108b 17571 }
506f5c41
TV
17572 else
17573 {
17574 if (attr_ub != NULL)
17575 complaint (_("Unresolved DW_AT_upper_bound "
17576 "- DIE at %s [in module %s]"),
17577 sect_offset_str (die->sect_off),
17578 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17579 if (attr_count != NULL)
17580 complaint (_("Unresolved DW_AT_count "
17581 "- DIE at %s [in module %s]"),
17582 sect_offset_str (die->sect_off),
17583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17584 }
e77813c8 17585 }
a02abb62 17586
4e962e74
TT
17587 LONGEST bias = 0;
17588 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17589 if (bias_attr != nullptr && bias_attr->form_is_constant ())
4e962e74
TT
17590 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17591
dbb9c2b1
JB
17592 /* Normally, the DWARF producers are expected to use a signed
17593 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17594 But this is unfortunately not always the case, as witnessed
17595 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17596 is used instead. To work around that ambiguity, we treat
17597 the bounds as signed, and thus sign-extend their values, when
17598 the base type is signed. */
6e70227d 17599 negative_mask =
d359392f 17600 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17601 if (low.kind == PROP_CONST
17602 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17603 low.data.const_val |= negative_mask;
17604 if (high.kind == PROP_CONST
17605 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17606 high.data.const_val |= negative_mask;
43bbcdc2 17607
5bbd8269
AB
17608 /* Check for bit and byte strides. */
17609 struct dynamic_prop byte_stride_prop;
17610 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17611 if (attr_byte_stride != nullptr)
17612 {
17613 struct type *prop_type
17614 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17615 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17616 prop_type);
17617 }
17618
17619 struct dynamic_prop bit_stride_prop;
17620 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17621 if (attr_bit_stride != nullptr)
17622 {
17623 /* It only makes sense to have either a bit or byte stride. */
17624 if (attr_byte_stride != nullptr)
17625 {
17626 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17627 "- DIE at %s [in module %s]"),
17628 sect_offset_str (die->sect_off),
17629 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17630 attr_bit_stride = nullptr;
17631 }
17632 else
17633 {
17634 struct type *prop_type
17635 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
17636 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17637 prop_type);
17638 }
17639 }
17640
17641 if (attr_byte_stride != nullptr
17642 || attr_bit_stride != nullptr)
17643 {
17644 bool byte_stride_p = (attr_byte_stride != nullptr);
17645 struct dynamic_prop *stride
17646 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17647
17648 range_type
17649 = create_range_type_with_stride (NULL, orig_base_type, &low,
17650 &high, bias, stride, byte_stride_p);
17651 }
17652 else
17653 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17654
c451ebe5
SA
17655 if (high_bound_is_count)
17656 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17657
c2ff108b
JK
17658 /* Ada expects an empty array on no boundary attributes. */
17659 if (attr == NULL && cu->language != language_ada)
729efb13 17660 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17661
39cbfefa
DJ
17662 name = dwarf2_name (die, cu);
17663 if (name)
17664 TYPE_NAME (range_type) = name;
6e70227d 17665
e142c38c 17666 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17667 if (attr != nullptr)
a02abb62
JB
17668 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17669
2b4424c3
TT
17670 maybe_set_alignment (cu, die, range_type);
17671
7e314c57
JK
17672 set_die_type (die, range_type, cu);
17673
17674 /* set_die_type should be already done. */
b4ba55a1
JB
17675 set_descriptive_type (range_type, die, cu);
17676
7e314c57 17677 return range_type;
a02abb62 17678}
6e70227d 17679
f792889a 17680static struct type *
81a17f79
JB
17681read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17682{
17683 struct type *type;
81a17f79 17684
518817b3
SM
17685 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17686 NULL);
0114d602 17687 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17688
74a2f8ff 17689 /* In Ada, an unspecified type is typically used when the description
85102364 17690 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17691 such a type, we treat it as a stub, and try to resolve it later on,
17692 when needed. */
17693 if (cu->language == language_ada)
17694 TYPE_STUB (type) = 1;
17695
f792889a 17696 return set_die_type (die, type, cu);
81a17f79 17697}
a02abb62 17698
639d11d3
DC
17699/* Read a single die and all its descendents. Set the die's sibling
17700 field to NULL; set other fields in the die correctly, and set all
17701 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17702 location of the info_ptr after reading all of those dies. PARENT
17703 is the parent of the die in question. */
17704
17705static struct die_info *
dee91e82 17706read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17707 const gdb_byte *info_ptr,
17708 const gdb_byte **new_info_ptr,
dee91e82 17709 struct die_info *parent)
639d11d3
DC
17710{
17711 struct die_info *die;
d521ce57 17712 const gdb_byte *cur_ptr;
639d11d3 17713
3e225074 17714 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17715 if (die == NULL)
17716 {
17717 *new_info_ptr = cur_ptr;
17718 return NULL;
17719 }
93311388 17720 store_in_ref_table (die, reader->cu);
639d11d3 17721
3e225074 17722 if (die->has_children)
bf6af496 17723 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17724 else
17725 {
17726 die->child = NULL;
17727 *new_info_ptr = cur_ptr;
17728 }
17729
17730 die->sibling = NULL;
17731 die->parent = parent;
17732 return die;
17733}
17734
17735/* Read a die, all of its descendents, and all of its siblings; set
17736 all of the fields of all of the dies correctly. Arguments are as
17737 in read_die_and_children. */
17738
17739static struct die_info *
bf6af496 17740read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17741 const gdb_byte *info_ptr,
17742 const gdb_byte **new_info_ptr,
bf6af496 17743 struct die_info *parent)
639d11d3
DC
17744{
17745 struct die_info *first_die, *last_sibling;
d521ce57 17746 const gdb_byte *cur_ptr;
639d11d3 17747
c906108c 17748 cur_ptr = info_ptr;
639d11d3
DC
17749 first_die = last_sibling = NULL;
17750
17751 while (1)
c906108c 17752 {
639d11d3 17753 struct die_info *die
dee91e82 17754 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17755
1d325ec1 17756 if (die == NULL)
c906108c 17757 {
639d11d3
DC
17758 *new_info_ptr = cur_ptr;
17759 return first_die;
c906108c 17760 }
1d325ec1
DJ
17761
17762 if (!first_die)
17763 first_die = die;
c906108c 17764 else
1d325ec1
DJ
17765 last_sibling->sibling = die;
17766
17767 last_sibling = die;
c906108c 17768 }
c906108c
SS
17769}
17770
bf6af496
DE
17771/* Read a die, all of its descendents, and all of its siblings; set
17772 all of the fields of all of the dies correctly. Arguments are as
17773 in read_die_and_children.
17774 This the main entry point for reading a DIE and all its children. */
17775
17776static struct die_info *
17777read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17778 const gdb_byte *info_ptr,
17779 const gdb_byte **new_info_ptr,
bf6af496
DE
17780 struct die_info *parent)
17781{
17782 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17783 new_info_ptr, parent);
17784
b4f54984 17785 if (dwarf_die_debug)
bf6af496
DE
17786 {
17787 fprintf_unfiltered (gdb_stdlog,
17788 "Read die from %s@0x%x of %s:\n",
96b79293 17789 reader->die_section->get_name (),
bf6af496
DE
17790 (unsigned) (info_ptr - reader->die_section->buffer),
17791 bfd_get_filename (reader->abfd));
b4f54984 17792 dump_die (die, dwarf_die_debug);
bf6af496
DE
17793 }
17794
17795 return die;
17796}
17797
3019eac3
DE
17798/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17799 attributes.
17800 The caller is responsible for filling in the extra attributes
17801 and updating (*DIEP)->num_attrs.
17802 Set DIEP to point to a newly allocated die with its information,
3e225074 17803 except for its child, sibling, and parent fields. */
93311388 17804
d521ce57 17805static const gdb_byte *
3019eac3 17806read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17807 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17808 int num_extra_attrs)
93311388 17809{
b64f50a1 17810 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17811 struct abbrev_info *abbrev;
17812 struct die_info *die;
17813 struct dwarf2_cu *cu = reader->cu;
17814 bfd *abfd = reader->abfd;
17815
9c541725 17816 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17817 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17818 info_ptr += bytes_read;
17819 if (!abbrev_number)
17820 {
17821 *diep = NULL;
93311388
DE
17822 return info_ptr;
17823 }
17824
685af9cd 17825 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17826 if (!abbrev)
348e048f
DE
17827 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17828 abbrev_number,
17829 bfd_get_filename (abfd));
17830
3019eac3 17831 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17832 die->sect_off = sect_off;
93311388
DE
17833 die->tag = abbrev->tag;
17834 die->abbrev = abbrev_number;
3e225074 17835 die->has_children = abbrev->has_children;
93311388 17836
3019eac3
DE
17837 /* Make the result usable.
17838 The caller needs to update num_attrs after adding the extra
17839 attributes. */
93311388
DE
17840 die->num_attrs = abbrev->num_attrs;
17841
18a8505e 17842 std::vector<int> indexes_that_need_reprocess;
93311388 17843 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17844 {
17845 bool need_reprocess;
17846 info_ptr =
17847 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17848 info_ptr, &need_reprocess);
17849 if (need_reprocess)
17850 indexes_that_need_reprocess.push_back (i);
17851 }
17852
17853 struct attribute *attr = dwarf2_attr_no_follow (die, DW_AT_str_offsets_base);
17854 if (attr != nullptr)
17855 cu->str_offsets_base = DW_UNSND (attr);
93311388 17856
18a8505e
AT
17857 auto maybe_addr_base = lookup_addr_base(die);
17858 if (maybe_addr_base.has_value ())
17859 cu->addr_base = *maybe_addr_base;
17860 for (int index : indexes_that_need_reprocess)
17861 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17862 *diep = die;
93311388
DE
17863 return info_ptr;
17864}
17865
3019eac3
DE
17866/* Read a die and all its attributes.
17867 Set DIEP to point to a newly allocated die with its information,
3e225074 17868 except for its child, sibling, and parent fields. */
3019eac3 17869
d521ce57 17870static const gdb_byte *
3019eac3 17871read_full_die (const struct die_reader_specs *reader,
3e225074 17872 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17873{
d521ce57 17874 const gdb_byte *result;
bf6af496 17875
3e225074 17876 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17877
b4f54984 17878 if (dwarf_die_debug)
bf6af496
DE
17879 {
17880 fprintf_unfiltered (gdb_stdlog,
17881 "Read die from %s@0x%x of %s:\n",
96b79293 17882 reader->die_section->get_name (),
bf6af496
DE
17883 (unsigned) (info_ptr - reader->die_section->buffer),
17884 bfd_get_filename (reader->abfd));
b4f54984 17885 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17886 }
17887
17888 return result;
3019eac3 17889}
433df2d4 17890\f
c906108c 17891
72bf9492
DJ
17892/* Returns nonzero if TAG represents a type that we might generate a partial
17893 symbol for. */
17894
17895static int
17896is_type_tag_for_partial (int tag)
17897{
17898 switch (tag)
17899 {
17900#if 0
17901 /* Some types that would be reasonable to generate partial symbols for,
17902 that we don't at present. */
17903 case DW_TAG_array_type:
17904 case DW_TAG_file_type:
17905 case DW_TAG_ptr_to_member_type:
17906 case DW_TAG_set_type:
17907 case DW_TAG_string_type:
17908 case DW_TAG_subroutine_type:
17909#endif
17910 case DW_TAG_base_type:
17911 case DW_TAG_class_type:
680b30c7 17912 case DW_TAG_interface_type:
72bf9492
DJ
17913 case DW_TAG_enumeration_type:
17914 case DW_TAG_structure_type:
17915 case DW_TAG_subrange_type:
17916 case DW_TAG_typedef:
17917 case DW_TAG_union_type:
17918 return 1;
17919 default:
17920 return 0;
17921 }
17922}
17923
17924/* Load all DIEs that are interesting for partial symbols into memory. */
17925
17926static struct partial_die_info *
dee91e82 17927load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17928 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17929{
dee91e82 17930 struct dwarf2_cu *cu = reader->cu;
518817b3 17931 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17932 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17933 unsigned int bytes_read;
5afb4e99 17934 unsigned int load_all = 0;
72bf9492
DJ
17935 int nesting_level = 1;
17936
17937 parent_die = NULL;
17938 last_die = NULL;
17939
7adf1e79
DE
17940 gdb_assert (cu->per_cu != NULL);
17941 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17942 load_all = 1;
17943
72bf9492
DJ
17944 cu->partial_dies
17945 = htab_create_alloc_ex (cu->header.length / 12,
17946 partial_die_hash,
17947 partial_die_eq,
17948 NULL,
17949 &cu->comp_unit_obstack,
17950 hashtab_obstack_allocate,
17951 dummy_obstack_deallocate);
17952
72bf9492
DJ
17953 while (1)
17954 {
685af9cd 17955 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17956
17957 /* A NULL abbrev means the end of a series of children. */
17958 if (abbrev == NULL)
17959 {
17960 if (--nesting_level == 0)
cd9983dd
YQ
17961 return first_die;
17962
72bf9492
DJ
17963 info_ptr += bytes_read;
17964 last_die = parent_die;
17965 parent_die = parent_die->die_parent;
17966 continue;
17967 }
17968
98bfdba5
PA
17969 /* Check for template arguments. We never save these; if
17970 they're seen, we just mark the parent, and go on our way. */
17971 if (parent_die != NULL
17972 && cu->language == language_cplus
17973 && (abbrev->tag == DW_TAG_template_type_param
17974 || abbrev->tag == DW_TAG_template_value_param))
17975 {
17976 parent_die->has_template_arguments = 1;
17977
17978 if (!load_all)
17979 {
17980 /* We don't need a partial DIE for the template argument. */
dee91e82 17981 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17982 continue;
17983 }
17984 }
17985
0d99eb77 17986 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17987 Skip their other children. */
17988 if (!load_all
17989 && cu->language == language_cplus
17990 && parent_die != NULL
17991 && parent_die->tag == DW_TAG_subprogram)
17992 {
dee91e82 17993 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17994 continue;
17995 }
17996
5afb4e99
DJ
17997 /* Check whether this DIE is interesting enough to save. Normally
17998 we would not be interested in members here, but there may be
17999 later variables referencing them via DW_AT_specification (for
18000 static members). */
18001 if (!load_all
18002 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18003 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18004 && abbrev->tag != DW_TAG_enumerator
18005 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18006 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18007 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18008 && abbrev->tag != DW_TAG_variable
5afb4e99 18009 && abbrev->tag != DW_TAG_namespace
f55ee35c 18010 && abbrev->tag != DW_TAG_module
95554aad 18011 && abbrev->tag != DW_TAG_member
74921315
KS
18012 && abbrev->tag != DW_TAG_imported_unit
18013 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18014 {
18015 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18016 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18017 continue;
18018 }
18019
6f06d47b
YQ
18020 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18021 abbrev);
cd9983dd 18022
48fbe735 18023 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18024
18025 /* This two-pass algorithm for processing partial symbols has a
18026 high cost in cache pressure. Thus, handle some simple cases
18027 here which cover the majority of C partial symbols. DIEs
18028 which neither have specification tags in them, nor could have
18029 specification tags elsewhere pointing at them, can simply be
18030 processed and discarded.
18031
18032 This segment is also optional; scan_partial_symbols and
18033 add_partial_symbol will handle these DIEs if we chain
18034 them in normally. When compilers which do not emit large
18035 quantities of duplicate debug information are more common,
18036 this code can probably be removed. */
18037
18038 /* Any complete simple types at the top level (pretty much all
18039 of them, for a language without namespaces), can be processed
18040 directly. */
18041 if (parent_die == NULL
cd9983dd
YQ
18042 && pdi.has_specification == 0
18043 && pdi.is_declaration == 0
18044 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18045 || pdi.tag == DW_TAG_base_type
18046 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18047 {
cd9983dd 18048 if (building_psymtab && pdi.name != NULL)
31edb802 18049 add_psymbol_to_list (pdi.name, false,
79748972 18050 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18051 psymbol_placement::STATIC,
1762568f 18052 0, cu->language, objfile);
cd9983dd 18053 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18054 continue;
18055 }
18056
d8228535
JK
18057 /* The exception for DW_TAG_typedef with has_children above is
18058 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18059 type_name_or_error will error on such types later.
d8228535
JK
18060
18061 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18062 it could not find the child DIEs referenced later, this is checked
18063 above. In correct DWARF DW_TAG_typedef should have no children. */
18064
cd9983dd 18065 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18066 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18067 "- DIE at %s [in module %s]"),
cd9983dd 18068 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18069
72bf9492
DJ
18070 /* If we're at the second level, and we're an enumerator, and
18071 our parent has no specification (meaning possibly lives in a
18072 namespace elsewhere), then we can add the partial symbol now
18073 instead of queueing it. */
cd9983dd 18074 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18075 && parent_die != NULL
18076 && parent_die->die_parent == NULL
18077 && parent_die->tag == DW_TAG_enumeration_type
18078 && parent_die->has_specification == 0)
18079 {
cd9983dd 18080 if (pdi.name == NULL)
b98664d3 18081 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18082 else if (building_psymtab)
31edb802 18083 add_psymbol_to_list (pdi.name, false,
79748972 18084 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18085 cu->language == language_cplus
75aedd27
TT
18086 ? psymbol_placement::GLOBAL
18087 : psymbol_placement::STATIC,
1762568f 18088 0, cu->language, objfile);
72bf9492 18089
cd9983dd 18090 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18091 continue;
18092 }
18093
cd9983dd 18094 struct partial_die_info *part_die
6f06d47b 18095 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18096
72bf9492
DJ
18097 /* We'll save this DIE so link it in. */
18098 part_die->die_parent = parent_die;
18099 part_die->die_sibling = NULL;
18100 part_die->die_child = NULL;
18101
18102 if (last_die && last_die == parent_die)
18103 last_die->die_child = part_die;
18104 else if (last_die)
18105 last_die->die_sibling = part_die;
18106
18107 last_die = part_die;
18108
18109 if (first_die == NULL)
18110 first_die = part_die;
18111
18112 /* Maybe add the DIE to the hash table. Not all DIEs that we
18113 find interesting need to be in the hash table, because we
18114 also have the parent/sibling/child chains; only those that we
18115 might refer to by offset later during partial symbol reading.
18116
18117 For now this means things that might have be the target of a
18118 DW_AT_specification, DW_AT_abstract_origin, or
18119 DW_AT_extension. DW_AT_extension will refer only to
18120 namespaces; DW_AT_abstract_origin refers to functions (and
18121 many things under the function DIE, but we do not recurse
18122 into function DIEs during partial symbol reading) and
18123 possibly variables as well; DW_AT_specification refers to
18124 declarations. Declarations ought to have the DW_AT_declaration
18125 flag. It happens that GCC forgets to put it in sometimes, but
18126 only for functions, not for types.
18127
18128 Adding more things than necessary to the hash table is harmless
18129 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18130 wasted time in find_partial_die, when we reread the compilation
18131 unit with load_all_dies set. */
72bf9492 18132
5afb4e99 18133 if (load_all
72929c62 18134 || abbrev->tag == DW_TAG_constant
5afb4e99 18135 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18136 || abbrev->tag == DW_TAG_variable
18137 || abbrev->tag == DW_TAG_namespace
18138 || part_die->is_declaration)
18139 {
18140 void **slot;
18141
18142 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18143 to_underlying (part_die->sect_off),
18144 INSERT);
72bf9492
DJ
18145 *slot = part_die;
18146 }
18147
72bf9492 18148 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18149 we have no reason to follow the children of structures; for other
98bfdba5
PA
18150 languages we have to, so that we can get at method physnames
18151 to infer fully qualified class names, for DW_AT_specification,
18152 and for C++ template arguments. For C++, we also look one level
18153 inside functions to find template arguments (if the name of the
18154 function does not already contain the template arguments).
bc30ff58 18155
0a4b0913
AB
18156 For Ada and Fortran, we need to scan the children of subprograms
18157 and lexical blocks as well because these languages allow the
18158 definition of nested entities that could be interesting for the
18159 debugger, such as nested subprograms for instance. */
72bf9492 18160 if (last_die->has_children
5afb4e99
DJ
18161 && (load_all
18162 || last_die->tag == DW_TAG_namespace
f55ee35c 18163 || last_die->tag == DW_TAG_module
72bf9492 18164 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18165 || (cu->language == language_cplus
18166 && last_die->tag == DW_TAG_subprogram
18167 && (last_die->name == NULL
18168 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18169 || (cu->language != language_c
18170 && (last_die->tag == DW_TAG_class_type
680b30c7 18171 || last_die->tag == DW_TAG_interface_type
72bf9492 18172 || last_die->tag == DW_TAG_structure_type
bc30ff58 18173 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18174 || ((cu->language == language_ada
18175 || cu->language == language_fortran)
bc30ff58
JB
18176 && (last_die->tag == DW_TAG_subprogram
18177 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18178 {
18179 nesting_level++;
18180 parent_die = last_die;
18181 continue;
18182 }
18183
18184 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18185 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18186
18187 /* Back to the top, do it again. */
18188 }
18189}
18190
6f06d47b
YQ
18191partial_die_info::partial_die_info (sect_offset sect_off_,
18192 struct abbrev_info *abbrev)
18193 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18194{
18195}
18196
35cc7ed7
YQ
18197/* Read a minimal amount of information into the minimal die structure.
18198 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18199
48fbe735
YQ
18200const gdb_byte *
18201partial_die_info::read (const struct die_reader_specs *reader,
18202 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18203{
dee91e82 18204 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18205 struct dwarf2_per_objfile *dwarf2_per_objfile
18206 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18207 unsigned int i;
c5aa993b 18208 int has_low_pc_attr = 0;
c906108c 18209 int has_high_pc_attr = 0;
91da1414 18210 int high_pc_relative = 0;
c906108c 18211
18a8505e 18212 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
fd0a254f 18213 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18214 {
18a8505e
AT
18215 bool need_reprocess;
18216 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
18217 info_ptr, &need_reprocess);
18218 /* String and address offsets that need to do the reprocessing have
18219 already been read at this point, so there is no need to wait until
18220 the loop terminates to do the reprocessing. */
18221 if (need_reprocess)
18222 read_attribute_reprocess (reader, &attr_vec[i]);
18223 attribute &attr = attr_vec[i];
c906108c 18224 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18225 partial symbol table. */
c906108c
SS
18226 switch (attr.name)
18227 {
18228 case DW_AT_name:
48fbe735 18229 switch (tag)
71c25dea
TT
18230 {
18231 case DW_TAG_compile_unit:
95554aad 18232 case DW_TAG_partial_unit:
348e048f 18233 case DW_TAG_type_unit:
71c25dea
TT
18234 /* Compilation units have a DW_AT_name that is a filename, not
18235 a source language identifier. */
18236 case DW_TAG_enumeration_type:
18237 case DW_TAG_enumerator:
18238 /* These tags always have simple identifiers already; no need
18239 to canonicalize them. */
48fbe735 18240 name = DW_STRING (&attr);
71c25dea
TT
18241 break;
18242 default:
48fbe735
YQ
18243 {
18244 struct objfile *objfile = dwarf2_per_objfile->objfile;
18245
18246 name
18247 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18248 &objfile->per_bfd->storage_obstack);
18249 }
71c25dea
TT
18250 break;
18251 }
c906108c 18252 break;
31ef98ae 18253 case DW_AT_linkage_name:
c906108c 18254 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18255 /* Note that both forms of linkage name might appear. We
18256 assume they will be the same, and we only store the last
18257 one we see. */
48fbe735 18258 linkage_name = DW_STRING (&attr);
c906108c
SS
18259 break;
18260 case DW_AT_low_pc:
18261 has_low_pc_attr = 1;
cd6c91b4 18262 lowpc = attr.value_as_address ();
c906108c
SS
18263 break;
18264 case DW_AT_high_pc:
18265 has_high_pc_attr = 1;
cd6c91b4
TT
18266 highpc = attr.value_as_address ();
18267 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18268 high_pc_relative = 1;
c906108c
SS
18269 break;
18270 case DW_AT_location:
0963b4bd 18271 /* Support the .debug_loc offsets. */
4fc6c0d5 18272 if (attr.form_is_block ())
8e19ed76 18273 {
48fbe735 18274 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18275 }
cd6c91b4 18276 else if (attr.form_is_section_offset ())
8e19ed76 18277 {
4d3c2250 18278 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18279 }
18280 else
18281 {
4d3c2250
KB
18282 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18283 "partial symbol information");
8e19ed76 18284 }
c906108c 18285 break;
c906108c 18286 case DW_AT_external:
48fbe735 18287 is_external = DW_UNSND (&attr);
c906108c
SS
18288 break;
18289 case DW_AT_declaration:
48fbe735 18290 is_declaration = DW_UNSND (&attr);
c906108c
SS
18291 break;
18292 case DW_AT_type:
48fbe735 18293 has_type = 1;
c906108c
SS
18294 break;
18295 case DW_AT_abstract_origin:
18296 case DW_AT_specification:
72bf9492 18297 case DW_AT_extension:
48fbe735
YQ
18298 has_specification = 1;
18299 spec_offset = dwarf2_get_ref_die_offset (&attr);
18300 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18301 || cu->per_cu->is_dwz);
c906108c
SS
18302 break;
18303 case DW_AT_sibling:
18304 /* Ignore absolute siblings, they might point outside of
18305 the current compile unit. */
18306 if (attr.form == DW_FORM_ref_addr)
b98664d3 18307 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18308 else
b9502d3f 18309 {
48fbe735 18310 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18311 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18312 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18313
18314 if (sibling_ptr < info_ptr)
b98664d3 18315 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18316 else if (sibling_ptr > reader->buffer_end)
18317 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18318 else
48fbe735 18319 sibling = sibling_ptr;
b9502d3f 18320 }
c906108c 18321 break;
fa4028e9 18322 case DW_AT_byte_size:
48fbe735 18323 has_byte_size = 1;
fa4028e9 18324 break;
ff908ebf 18325 case DW_AT_const_value:
48fbe735 18326 has_const_value = 1;
ff908ebf 18327 break;
68511cec
CES
18328 case DW_AT_calling_convention:
18329 /* DWARF doesn't provide a way to identify a program's source-level
18330 entry point. DW_AT_calling_convention attributes are only meant
18331 to describe functions' calling conventions.
18332
18333 However, because it's a necessary piece of information in
0c1b455e
TT
18334 Fortran, and before DWARF 4 DW_CC_program was the only
18335 piece of debugging information whose definition refers to
18336 a 'main program' at all, several compilers marked Fortran
18337 main programs with DW_CC_program --- even when those
18338 functions use the standard calling conventions.
18339
18340 Although DWARF now specifies a way to provide this
18341 information, we support this practice for backward
18342 compatibility. */
68511cec 18343 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18344 && cu->language == language_fortran)
48fbe735 18345 main_subprogram = 1;
68511cec 18346 break;
481860b3
GB
18347 case DW_AT_inline:
18348 if (DW_UNSND (&attr) == DW_INL_inlined
18349 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18350 may_be_inlined = 1;
481860b3 18351 break;
95554aad
TT
18352
18353 case DW_AT_import:
48fbe735 18354 if (tag == DW_TAG_imported_unit)
36586728 18355 {
48fbe735
YQ
18356 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18357 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18358 || cu->per_cu->is_dwz);
18359 }
95554aad
TT
18360 break;
18361
0c1b455e 18362 case DW_AT_main_subprogram:
48fbe735 18363 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18364 break;
18365
05caa1d2
TT
18366 case DW_AT_ranges:
18367 {
18368 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18369 but that requires a full DIE, so instead we just
18370 reimplement it. */
18371 int need_ranges_base = tag != DW_TAG_compile_unit;
18372 unsigned int ranges_offset = (DW_UNSND (&attr)
18373 + (need_ranges_base
18374 ? cu->ranges_base
18375 : 0));
18376
18377 /* Value of the DW_AT_ranges attribute is the offset in the
18378 .debug_ranges section. */
18379 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18380 nullptr))
18381 has_pc_info = 1;
18382 }
18383 break;
18384
c906108c
SS
18385 default:
18386 break;
18387 }
18388 }
18389
10d06d82
TT
18390 /* For Ada, if both the name and the linkage name appear, we prefer
18391 the latter. This lets "catch exception" work better, regardless
18392 of the order in which the name and linkage name were emitted.
18393 Really, though, this is just a workaround for the fact that gdb
18394 doesn't store both the name and the linkage name. */
18395 if (cu->language == language_ada && linkage_name != nullptr)
18396 name = linkage_name;
18397
91da1414 18398 if (high_pc_relative)
48fbe735 18399 highpc += lowpc;
91da1414 18400
9373cf26
JK
18401 if (has_low_pc_attr && has_high_pc_attr)
18402 {
18403 /* When using the GNU linker, .gnu.linkonce. sections are used to
18404 eliminate duplicate copies of functions and vtables and such.
18405 The linker will arbitrarily choose one and discard the others.
18406 The AT_*_pc values for such functions refer to local labels in
18407 these sections. If the section from that file was discarded, the
18408 labels are not in the output, so the relocs get a value of 0.
18409 If this is a discarded function, mark the pc bounds as invalid,
18410 so that GDB will ignore it. */
48fbe735 18411 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18412 {
48fbe735 18413 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18414 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18415
b98664d3 18416 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18417 "for DIE at %s [in module %s]"),
48fbe735
YQ
18418 paddress (gdbarch, lowpc),
18419 sect_offset_str (sect_off),
9d8780f0 18420 objfile_name (objfile));
9373cf26
JK
18421 }
18422 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18423 else if (lowpc >= highpc)
9373cf26 18424 {
48fbe735 18425 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18426 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18427
b98664d3 18428 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18429 "for DIE at %s [in module %s]"),
48fbe735
YQ
18430 paddress (gdbarch, lowpc),
18431 paddress (gdbarch, highpc),
18432 sect_offset_str (sect_off),
9c541725 18433 objfile_name (objfile));
9373cf26
JK
18434 }
18435 else
48fbe735 18436 has_pc_info = 1;
9373cf26 18437 }
85cbf3d3 18438
c906108c
SS
18439 return info_ptr;
18440}
18441
72bf9492
DJ
18442/* Find a cached partial DIE at OFFSET in CU. */
18443
d590ff25
YQ
18444struct partial_die_info *
18445dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18446{
18447 struct partial_die_info *lookup_die = NULL;
6f06d47b 18448 struct partial_die_info part_die (sect_off);
72bf9492 18449
9a3c8263 18450 lookup_die = ((struct partial_die_info *)
d590ff25 18451 htab_find_with_hash (partial_dies, &part_die,
9c541725 18452 to_underlying (sect_off)));
72bf9492 18453
72bf9492
DJ
18454 return lookup_die;
18455}
18456
348e048f
DE
18457/* Find a partial DIE at OFFSET, which may or may not be in CU,
18458 except in the case of .debug_types DIEs which do not reference
18459 outside their CU (they do however referencing other types via
55f1336d 18460 DW_FORM_ref_sig8). */
72bf9492 18461
122cf0f2 18462static const struct cu_partial_die_info
9c541725 18463find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18464{
518817b3
SM
18465 struct dwarf2_per_objfile *dwarf2_per_objfile
18466 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18467 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18468 struct dwarf2_per_cu_data *per_cu = NULL;
18469 struct partial_die_info *pd = NULL;
72bf9492 18470
36586728 18471 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18472 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18473 {
d590ff25 18474 pd = cu->find_partial_die (sect_off);
5afb4e99 18475 if (pd != NULL)
fb816e8b 18476 return { cu, pd };
0d99eb77
DE
18477 /* We missed recording what we needed.
18478 Load all dies and try again. */
18479 per_cu = cu->per_cu;
5afb4e99 18480 }
0d99eb77
DE
18481 else
18482 {
18483 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18484 if (cu->per_cu->is_debug_types)
0d99eb77 18485 {
9d8780f0
SM
18486 error (_("Dwarf Error: Type Unit at offset %s contains"
18487 " external reference to offset %s [in module %s].\n"),
18488 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18489 bfd_get_filename (objfile->obfd));
18490 }
9c541725 18491 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18492 dwarf2_per_objfile);
72bf9492 18493
0d99eb77
DE
18494 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18495 load_partial_comp_unit (per_cu);
ae038cb0 18496
0d99eb77 18497 per_cu->cu->last_used = 0;
d590ff25 18498 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18499 }
5afb4e99 18500
dee91e82
DE
18501 /* If we didn't find it, and not all dies have been loaded,
18502 load them all and try again. */
18503
5afb4e99
DJ
18504 if (pd == NULL && per_cu->load_all_dies == 0)
18505 {
5afb4e99 18506 per_cu->load_all_dies = 1;
fd820528
DE
18507
18508 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18509 THIS_CU->cu may already be in use. So we can't just free it and
18510 replace its DIEs with the ones we read in. Instead, we leave those
18511 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18512 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18513 set. */
dee91e82 18514 load_partial_comp_unit (per_cu);
5afb4e99 18515
d590ff25 18516 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18517 }
18518
18519 if (pd == NULL)
18520 internal_error (__FILE__, __LINE__,
9d8780f0 18521 _("could not find partial DIE %s "
3e43a32a 18522 "in cache [from module %s]\n"),
9d8780f0 18523 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18524 return { per_cu->cu, pd };
72bf9492
DJ
18525}
18526
abc72ce4
DE
18527/* See if we can figure out if the class lives in a namespace. We do
18528 this by looking for a member function; its demangled name will
18529 contain namespace info, if there is any. */
18530
18531static void
18532guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18533 struct dwarf2_cu *cu)
18534{
18535 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18536 what template types look like, because the demangler
18537 frequently doesn't give the same name as the debug info. We
18538 could fix this by only using the demangled name to get the
18539 prefix (but see comment in read_structure_type). */
18540
18541 struct partial_die_info *real_pdi;
18542 struct partial_die_info *child_pdi;
18543
18544 /* If this DIE (this DIE's specification, if any) has a parent, then
18545 we should not do this. We'll prepend the parent's fully qualified
18546 name when we create the partial symbol. */
18547
18548 real_pdi = struct_pdi;
18549 while (real_pdi->has_specification)
fb816e8b 18550 {
122cf0f2
AB
18551 auto res = find_partial_die (real_pdi->spec_offset,
18552 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18553 real_pdi = res.pdi;
18554 cu = res.cu;
18555 }
abc72ce4
DE
18556
18557 if (real_pdi->die_parent != NULL)
18558 return;
18559
18560 for (child_pdi = struct_pdi->die_child;
18561 child_pdi != NULL;
18562 child_pdi = child_pdi->die_sibling)
18563 {
18564 if (child_pdi->tag == DW_TAG_subprogram
18565 && child_pdi->linkage_name != NULL)
18566 {
43816ebc
TT
18567 gdb::unique_xmalloc_ptr<char> actual_class_name
18568 (language_class_name_from_physname (cu->language_defn,
18569 child_pdi->linkage_name));
abc72ce4
DE
18570 if (actual_class_name != NULL)
18571 {
518817b3 18572 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18573 struct_pdi->name
021887d8 18574 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 18575 actual_class_name.get ());
abc72ce4
DE
18576 }
18577 break;
18578 }
18579 }
18580}
18581
52356b79
YQ
18582void
18583partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18584{
abc72ce4
DE
18585 /* Once we've fixed up a die, there's no point in doing so again.
18586 This also avoids a memory leak if we were to call
18587 guess_partial_die_structure_name multiple times. */
52356b79 18588 if (fixup_called)
abc72ce4
DE
18589 return;
18590
72bf9492
DJ
18591 /* If we found a reference attribute and the DIE has no name, try
18592 to find a name in the referred to DIE. */
18593
52356b79 18594 if (name == NULL && has_specification)
72bf9492
DJ
18595 {
18596 struct partial_die_info *spec_die;
72bf9492 18597
122cf0f2 18598 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18599 spec_die = res.pdi;
18600 cu = res.cu;
72bf9492 18601
52356b79 18602 spec_die->fixup (cu);
72bf9492
DJ
18603
18604 if (spec_die->name)
18605 {
52356b79 18606 name = spec_die->name;
72bf9492
DJ
18607
18608 /* Copy DW_AT_external attribute if it is set. */
18609 if (spec_die->is_external)
52356b79 18610 is_external = spec_die->is_external;
72bf9492
DJ
18611 }
18612 }
18613
18614 /* Set default names for some unnamed DIEs. */
72bf9492 18615
52356b79
YQ
18616 if (name == NULL && tag == DW_TAG_namespace)
18617 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18618
abc72ce4
DE
18619 /* If there is no parent die to provide a namespace, and there are
18620 children, see if we can determine the namespace from their linkage
122d1940 18621 name. */
abc72ce4 18622 if (cu->language == language_cplus
fd5866f6 18623 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18624 && die_parent == NULL
18625 && has_children
18626 && (tag == DW_TAG_class_type
18627 || tag == DW_TAG_structure_type
18628 || tag == DW_TAG_union_type))
18629 guess_partial_die_structure_name (this, cu);
abc72ce4 18630
53832f31
TT
18631 /* GCC might emit a nameless struct or union that has a linkage
18632 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18633 if (name == NULL
18634 && (tag == DW_TAG_class_type
18635 || tag == DW_TAG_interface_type
18636 || tag == DW_TAG_structure_type
18637 || tag == DW_TAG_union_type)
18638 && linkage_name != NULL)
53832f31 18639 {
43816ebc
TT
18640 gdb::unique_xmalloc_ptr<char> demangled
18641 (gdb_demangle (linkage_name, DMGL_TYPES));
18642 if (demangled != nullptr)
53832f31 18643 {
96408a79
SA
18644 const char *base;
18645
18646 /* Strip any leading namespaces/classes, keep only the base name.
18647 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18648 base = strrchr (demangled.get (), ':');
18649 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18650 base++;
18651 else
43816ebc 18652 base = demangled.get ();
96408a79 18653
518817b3 18654 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 18655 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
18656 }
18657 }
18658
52356b79 18659 fixup_called = 1;
72bf9492
DJ
18660}
18661
18a8505e
AT
18662/* Process the attributes that had to be skipped in the first round. These
18663 attributes are the ones that need str_offsets_base or addr_base attributes.
18664 They could not have been processed in the first round, because at the time
18665 the values of str_offsets_base or addr_base may not have been known. */
18666void read_attribute_reprocess (const struct die_reader_specs *reader,
18667 struct attribute *attr)
18668{
18669 struct dwarf2_cu *cu = reader->cu;
18670 switch (attr->form)
18671 {
18672 case DW_FORM_addrx:
18673 case DW_FORM_GNU_addr_index:
18674 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18675 break;
18676 case DW_FORM_strx:
18677 case DW_FORM_strx1:
18678 case DW_FORM_strx2:
18679 case DW_FORM_strx3:
18680 case DW_FORM_strx4:
18681 case DW_FORM_GNU_str_index:
18682 {
18683 unsigned int str_index = DW_UNSND (attr);
18684 if (reader->dwo_file != NULL)
18685 {
18686 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18687 DW_STRING_IS_CANONICAL (attr) = 0;
18688 }
18689 else
18690 {
18691 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18692 DW_STRING_IS_CANONICAL (attr) = 0;
18693 }
18694 break;
18695 }
18696 default:
18697 gdb_assert_not_reached (_("Unexpected DWARF form."));
18698 }
18699}
18700
a8329558 18701/* Read an attribute value described by an attribute form. */
c906108c 18702
d521ce57 18703static const gdb_byte *
dee91e82
DE
18704read_attribute_value (const struct die_reader_specs *reader,
18705 struct attribute *attr, unsigned form,
18a8505e
AT
18706 LONGEST implicit_const, const gdb_byte *info_ptr,
18707 bool *need_reprocess)
c906108c 18708{
dee91e82 18709 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18710 struct dwarf2_per_objfile *dwarf2_per_objfile
18711 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18712 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18713 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18714 bfd *abfd = reader->abfd;
e7c27a73 18715 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18716 unsigned int bytes_read;
18717 struct dwarf_block *blk;
18a8505e 18718 *need_reprocess = false;
c906108c 18719
aead7601 18720 attr->form = (enum dwarf_form) form;
a8329558 18721 switch (form)
c906108c 18722 {
c906108c 18723 case DW_FORM_ref_addr:
ae411497 18724 if (cu->header.version == 2)
4568ecf9 18725 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18726 else
4568ecf9
DE
18727 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18728 &cu->header, &bytes_read);
ae411497
TT
18729 info_ptr += bytes_read;
18730 break;
36586728
TT
18731 case DW_FORM_GNU_ref_alt:
18732 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18733 info_ptr += bytes_read;
18734 break;
ae411497 18735 case DW_FORM_addr:
e7c27a73 18736 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18737 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18738 info_ptr += bytes_read;
c906108c
SS
18739 break;
18740 case DW_FORM_block2:
7b5a2f43 18741 blk = dwarf_alloc_block (cu);
c906108c
SS
18742 blk->size = read_2_bytes (abfd, info_ptr);
18743 info_ptr += 2;
18744 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18745 info_ptr += blk->size;
18746 DW_BLOCK (attr) = blk;
18747 break;
18748 case DW_FORM_block4:
7b5a2f43 18749 blk = dwarf_alloc_block (cu);
c906108c
SS
18750 blk->size = read_4_bytes (abfd, info_ptr);
18751 info_ptr += 4;
18752 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18753 info_ptr += blk->size;
18754 DW_BLOCK (attr) = blk;
18755 break;
18756 case DW_FORM_data2:
18757 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18758 info_ptr += 2;
18759 break;
18760 case DW_FORM_data4:
18761 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18762 info_ptr += 4;
18763 break;
18764 case DW_FORM_data8:
18765 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18766 info_ptr += 8;
18767 break;
0224619f
JK
18768 case DW_FORM_data16:
18769 blk = dwarf_alloc_block (cu);
18770 blk->size = 16;
18771 blk->data = read_n_bytes (abfd, info_ptr, 16);
18772 info_ptr += 16;
18773 DW_BLOCK (attr) = blk;
18774 break;
2dc7f7b3
TT
18775 case DW_FORM_sec_offset:
18776 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18777 info_ptr += bytes_read;
18778 break;
c906108c 18779 case DW_FORM_string:
9b1c24c8 18780 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18781 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18782 info_ptr += bytes_read;
18783 break;
4bdf3d34 18784 case DW_FORM_strp:
36586728
TT
18785 if (!cu->per_cu->is_dwz)
18786 {
ed2dc618
SM
18787 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18788 abfd, info_ptr, cu_header,
36586728
TT
18789 &bytes_read);
18790 DW_STRING_IS_CANONICAL (attr) = 0;
18791 info_ptr += bytes_read;
18792 break;
18793 }
18794 /* FALLTHROUGH */
43988095
JK
18795 case DW_FORM_line_strp:
18796 if (!cu->per_cu->is_dwz)
18797 {
ed2dc618
SM
18798 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18799 abfd, info_ptr,
43988095
JK
18800 cu_header, &bytes_read);
18801 DW_STRING_IS_CANONICAL (attr) = 0;
18802 info_ptr += bytes_read;
18803 break;
18804 }
18805 /* FALLTHROUGH */
36586728
TT
18806 case DW_FORM_GNU_strp_alt:
18807 {
ed2dc618 18808 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18809 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18810 &bytes_read);
18811
ed2dc618
SM
18812 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18813 dwz, str_offset);
36586728
TT
18814 DW_STRING_IS_CANONICAL (attr) = 0;
18815 info_ptr += bytes_read;
18816 }
4bdf3d34 18817 break;
2dc7f7b3 18818 case DW_FORM_exprloc:
c906108c 18819 case DW_FORM_block:
7b5a2f43 18820 blk = dwarf_alloc_block (cu);
c906108c
SS
18821 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18822 info_ptr += bytes_read;
18823 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18824 info_ptr += blk->size;
18825 DW_BLOCK (attr) = blk;
18826 break;
18827 case DW_FORM_block1:
7b5a2f43 18828 blk = dwarf_alloc_block (cu);
c906108c
SS
18829 blk->size = read_1_byte (abfd, info_ptr);
18830 info_ptr += 1;
18831 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18832 info_ptr += blk->size;
18833 DW_BLOCK (attr) = blk;
18834 break;
18835 case DW_FORM_data1:
18836 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18837 info_ptr += 1;
18838 break;
18839 case DW_FORM_flag:
18840 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18841 info_ptr += 1;
18842 break;
2dc7f7b3
TT
18843 case DW_FORM_flag_present:
18844 DW_UNSND (attr) = 1;
18845 break;
c906108c
SS
18846 case DW_FORM_sdata:
18847 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18848 info_ptr += bytes_read;
18849 break;
18850 case DW_FORM_udata:
18a8505e 18851 case DW_FORM_rnglistx:
c906108c
SS
18852 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18853 info_ptr += bytes_read;
18854 break;
18855 case DW_FORM_ref1:
9c541725 18856 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18857 + read_1_byte (abfd, info_ptr));
c906108c
SS
18858 info_ptr += 1;
18859 break;
18860 case DW_FORM_ref2:
9c541725 18861 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18862 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18863 info_ptr += 2;
18864 break;
18865 case DW_FORM_ref4:
9c541725 18866 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18867 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18868 info_ptr += 4;
18869 break;
613e1657 18870 case DW_FORM_ref8:
9c541725 18871 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18872 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18873 info_ptr += 8;
18874 break;
55f1336d 18875 case DW_FORM_ref_sig8:
ac9ec31b 18876 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18877 info_ptr += 8;
18878 break;
c906108c 18879 case DW_FORM_ref_udata:
9c541725 18880 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18881 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18882 info_ptr += bytes_read;
18883 break;
c906108c 18884 case DW_FORM_indirect:
a8329558
KW
18885 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18886 info_ptr += bytes_read;
43988095
JK
18887 if (form == DW_FORM_implicit_const)
18888 {
18889 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18890 info_ptr += bytes_read;
18891 }
18892 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18893 info_ptr, need_reprocess);
43988095
JK
18894 break;
18895 case DW_FORM_implicit_const:
18896 DW_SND (attr) = implicit_const;
a8329558 18897 break;
336d760d 18898 case DW_FORM_addrx:
3019eac3 18899 case DW_FORM_GNU_addr_index:
18a8505e
AT
18900 *need_reprocess = true;
18901 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18902 info_ptr += bytes_read;
18903 break;
cf532bd1 18904 case DW_FORM_strx:
15f18d14
AT
18905 case DW_FORM_strx1:
18906 case DW_FORM_strx2:
18907 case DW_FORM_strx3:
18908 case DW_FORM_strx4:
3019eac3 18909 case DW_FORM_GNU_str_index:
3019eac3 18910 {
15f18d14
AT
18911 ULONGEST str_index;
18912 if (form == DW_FORM_strx1)
18913 {
18914 str_index = read_1_byte (abfd, info_ptr);
18915 info_ptr += 1;
18916 }
18917 else if (form == DW_FORM_strx2)
18918 {
18919 str_index = read_2_bytes (abfd, info_ptr);
18920 info_ptr += 2;
18921 }
18922 else if (form == DW_FORM_strx3)
18923 {
18924 str_index = read_3_bytes (abfd, info_ptr);
18925 info_ptr += 3;
18926 }
18927 else if (form == DW_FORM_strx4)
18928 {
18929 str_index = read_4_bytes (abfd, info_ptr);
18930 info_ptr += 4;
18931 }
18932 else
18933 {
18934 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18935 info_ptr += bytes_read;
18936 }
18a8505e
AT
18937 *need_reprocess = true;
18938 DW_UNSND (attr) = str_index;
18939 }
3019eac3 18940 break;
c906108c 18941 default:
8a3fe4f8 18942 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18943 dwarf_form_name (form),
18944 bfd_get_filename (abfd));
c906108c 18945 }
28e94949 18946
36586728 18947 /* Super hack. */
cd6c91b4 18948 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18949 attr->form = DW_FORM_GNU_ref_alt;
18950
28e94949
JB
18951 /* We have seen instances where the compiler tried to emit a byte
18952 size attribute of -1 which ended up being encoded as an unsigned
18953 0xffffffff. Although 0xffffffff is technically a valid size value,
18954 an object of this size seems pretty unlikely so we can relatively
18955 safely treat these cases as if the size attribute was invalid and
18956 treat them as zero by default. */
18957 if (attr->name == DW_AT_byte_size
18958 && form == DW_FORM_data4
18959 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18960 {
18961 complaint
b98664d3 18962 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18963 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18964 DW_UNSND (attr) = 0;
18965 }
28e94949 18966
c906108c
SS
18967 return info_ptr;
18968}
18969
a8329558
KW
18970/* Read an attribute described by an abbreviated attribute. */
18971
d521ce57 18972static const gdb_byte *
dee91e82
DE
18973read_attribute (const struct die_reader_specs *reader,
18974 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18975 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18976{
18977 attr->name = abbrev->name;
43988095 18978 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18979 abbrev->implicit_const, info_ptr,
18980 need_reprocess);
a8329558
KW
18981}
18982
c906108c 18983static CORE_ADDR
d521ce57 18984read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 18985 unsigned int *bytes_read)
c906108c 18986{
e7c27a73 18987 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18988 CORE_ADDR retval = 0;
18989
107d2387 18990 if (cu_header->signed_addr_p)
c906108c 18991 {
107d2387
AC
18992 switch (cu_header->addr_size)
18993 {
18994 case 2:
fe1b8b76 18995 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
18996 break;
18997 case 4:
fe1b8b76 18998 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
18999 break;
19000 case 8:
fe1b8b76 19001 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19002 break;
19003 default:
8e65ff28 19004 internal_error (__FILE__, __LINE__,
e2e0b3e5 19005 _("read_address: bad switch, signed [in module %s]"),
659b0389 19006 bfd_get_filename (abfd));
107d2387
AC
19007 }
19008 }
19009 else
19010 {
19011 switch (cu_header->addr_size)
19012 {
19013 case 2:
fe1b8b76 19014 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19015 break;
19016 case 4:
fe1b8b76 19017 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19018 break;
19019 case 8:
fe1b8b76 19020 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19021 break;
19022 default:
8e65ff28 19023 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19024 _("read_address: bad switch, "
19025 "unsigned [in module %s]"),
659b0389 19026 bfd_get_filename (abfd));
107d2387 19027 }
c906108c 19028 }
64367e0a 19029
107d2387
AC
19030 *bytes_read = cu_header->addr_size;
19031 return retval;
c906108c
SS
19032}
19033
f7ef9339 19034/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19035 specification allows the initial length to take up either 4 bytes
19036 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19037 bytes describe the length and all offsets will be 8 bytes in length
19038 instead of 4.
19039
f7ef9339
KB
19040 An older, non-standard 64-bit format is also handled by this
19041 function. The older format in question stores the initial length
19042 as an 8-byte quantity without an escape value. Lengths greater
19043 than 2^32 aren't very common which means that the initial 4 bytes
19044 is almost always zero. Since a length value of zero doesn't make
19045 sense for the 32-bit format, this initial zero can be considered to
19046 be an escape value which indicates the presence of the older 64-bit
19047 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19048 greater than 4GB. If it becomes necessary to handle lengths
19049 somewhat larger than 4GB, we could allow other small values (such
19050 as the non-sensical values of 1, 2, and 3) to also be used as
19051 escape values indicating the presence of the old format.
f7ef9339 19052
917c78fc
MK
19053 The value returned via bytes_read should be used to increment the
19054 relevant pointer after calling read_initial_length().
c764a876 19055
613e1657
KB
19056 [ Note: read_initial_length() and read_offset() are based on the
19057 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19058 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19059 from:
19060
f7ef9339 19061 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19062
613e1657
KB
19063 This document is only a draft and is subject to change. (So beware.)
19064
f7ef9339 19065 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19066 determined empirically by examining 64-bit ELF files produced by
19067 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19068
19069 - Kevin, July 16, 2002
613e1657
KB
19070 ] */
19071
19072static LONGEST
d521ce57 19073read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19074{
fe1b8b76 19075 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19076
dd373385 19077 if (length == 0xffffffff)
613e1657 19078 {
fe1b8b76 19079 length = bfd_get_64 (abfd, buf + 4);
613e1657 19080 *bytes_read = 12;
613e1657 19081 }
dd373385 19082 else if (length == 0)
f7ef9339 19083 {
dd373385 19084 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19085 length = bfd_get_64 (abfd, buf);
f7ef9339 19086 *bytes_read = 8;
f7ef9339 19087 }
613e1657
KB
19088 else
19089 {
19090 *bytes_read = 4;
613e1657
KB
19091 }
19092
c764a876
DE
19093 return length;
19094}
dd373385 19095
c764a876
DE
19096/* Cover function for read_initial_length.
19097 Returns the length of the object at BUF, and stores the size of the
19098 initial length in *BYTES_READ and stores the size that offsets will be in
19099 *OFFSET_SIZE.
19100 If the initial length size is not equivalent to that specified in
19101 CU_HEADER then issue a complaint.
19102 This is useful when reading non-comp-unit headers. */
dd373385 19103
c764a876 19104static LONGEST
d521ce57 19105read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19106 const struct comp_unit_head *cu_header,
19107 unsigned int *bytes_read,
19108 unsigned int *offset_size)
19109{
19110 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19111
19112 gdb_assert (cu_header->initial_length_size == 4
19113 || cu_header->initial_length_size == 8
19114 || cu_header->initial_length_size == 12);
19115
19116 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19117 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19118
c764a876 19119 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19120 return length;
613e1657
KB
19121}
19122
19123/* Read an offset from the data stream. The size of the offset is
917c78fc 19124 given by cu_header->offset_size. */
613e1657
KB
19125
19126static LONGEST
d521ce57
TT
19127read_offset (bfd *abfd, const gdb_byte *buf,
19128 const struct comp_unit_head *cu_header,
891d2f0b 19129 unsigned int *bytes_read)
c764a876
DE
19130{
19131 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19132
c764a876
DE
19133 *bytes_read = cu_header->offset_size;
19134 return offset;
19135}
19136
19137/* Read an offset from the data stream. */
19138
19139static LONGEST
d521ce57 19140read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19141{
19142 LONGEST retval = 0;
19143
c764a876 19144 switch (offset_size)
613e1657
KB
19145 {
19146 case 4:
fe1b8b76 19147 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19148 break;
19149 case 8:
fe1b8b76 19150 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19151 break;
19152 default:
8e65ff28 19153 internal_error (__FILE__, __LINE__,
c764a876 19154 _("read_offset_1: bad switch [in module %s]"),
659b0389 19155 bfd_get_filename (abfd));
613e1657
KB
19156 }
19157
917c78fc 19158 return retval;
613e1657
KB
19159}
19160
d521ce57
TT
19161static const gdb_byte *
19162read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19163{
19164 /* If the size of a host char is 8 bits, we can return a pointer
19165 to the buffer, otherwise we have to copy the data to a buffer
19166 allocated on the temporary obstack. */
4bdf3d34 19167 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19168 return buf;
c906108c
SS
19169}
19170
d521ce57
TT
19171static const char *
19172read_direct_string (bfd *abfd, const gdb_byte *buf,
19173 unsigned int *bytes_read_ptr)
c906108c
SS
19174{
19175 /* If the size of a host char is 8 bits, we can return a pointer
19176 to the string, otherwise we have to copy the string to a buffer
19177 allocated on the temporary obstack. */
4bdf3d34 19178 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19179 if (*buf == '\0')
19180 {
19181 *bytes_read_ptr = 1;
19182 return NULL;
19183 }
d521ce57
TT
19184 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19185 return (const char *) buf;
4bdf3d34
JJ
19186}
19187
43988095
JK
19188/* Return pointer to string at section SECT offset STR_OFFSET with error
19189 reporting strings FORM_NAME and SECT_NAME. */
19190
d521ce57 19191static const char *
ed2dc618
SM
19192read_indirect_string_at_offset_from (struct objfile *objfile,
19193 bfd *abfd, LONGEST str_offset,
43988095
JK
19194 struct dwarf2_section_info *sect,
19195 const char *form_name,
19196 const char *sect_name)
19197{
96b79293 19198 sect->read (objfile);
43988095
JK
19199 if (sect->buffer == NULL)
19200 error (_("%s used without %s section [in module %s]"),
19201 form_name, sect_name, bfd_get_filename (abfd));
19202 if (str_offset >= sect->size)
19203 error (_("%s pointing outside of %s section [in module %s]"),
19204 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19205 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19206 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19207 return NULL;
43988095
JK
19208 return (const char *) (sect->buffer + str_offset);
19209}
19210
19211/* Return pointer to string at .debug_str offset STR_OFFSET. */
19212
19213static const char *
ed2dc618
SM
19214read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19215 bfd *abfd, LONGEST str_offset)
43988095 19216{
ed2dc618
SM
19217 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19218 abfd, str_offset,
43988095
JK
19219 &dwarf2_per_objfile->str,
19220 "DW_FORM_strp", ".debug_str");
19221}
19222
19223/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19224
19225static const char *
ed2dc618
SM
19226read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19227 bfd *abfd, LONGEST str_offset)
43988095 19228{
ed2dc618
SM
19229 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19230 abfd, str_offset,
43988095
JK
19231 &dwarf2_per_objfile->line_str,
19232 "DW_FORM_line_strp",
19233 ".debug_line_str");
c906108c
SS
19234}
19235
36586728
TT
19236/* Read a string at offset STR_OFFSET in the .debug_str section from
19237 the .dwz file DWZ. Throw an error if the offset is too large. If
19238 the string consists of a single NUL byte, return NULL; otherwise
19239 return a pointer to the string. */
19240
d521ce57 19241static const char *
ed2dc618
SM
19242read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19243 LONGEST str_offset)
36586728 19244{
96b79293 19245 dwz->str.read (objfile);
36586728
TT
19246
19247 if (dwz->str.buffer == NULL)
19248 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19249 "section [in module %s]"),
00f93c44 19250 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19251 if (str_offset >= dwz->str.size)
19252 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19253 ".debug_str section [in module %s]"),
00f93c44 19254 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19255 gdb_assert (HOST_CHAR_BIT == 8);
19256 if (dwz->str.buffer[str_offset] == '\0')
19257 return NULL;
d521ce57 19258 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19259}
19260
43988095
JK
19261/* Return pointer to string at .debug_str offset as read from BUF.
19262 BUF is assumed to be in a compilation unit described by CU_HEADER.
19263 Return *BYTES_READ_PTR count of bytes read from BUF. */
19264
d521ce57 19265static const char *
ed2dc618
SM
19266read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19267 const gdb_byte *buf,
cf2c3c16
TT
19268 const struct comp_unit_head *cu_header,
19269 unsigned int *bytes_read_ptr)
19270{
19271 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19272
ed2dc618 19273 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19274}
19275
43988095
JK
19276/* Return pointer to string at .debug_line_str offset as read from BUF.
19277 BUF is assumed to be in a compilation unit described by CU_HEADER.
19278 Return *BYTES_READ_PTR count of bytes read from BUF. */
19279
19280static const char *
ed2dc618
SM
19281read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19282 bfd *abfd, const gdb_byte *buf,
43988095
JK
19283 const struct comp_unit_head *cu_header,
19284 unsigned int *bytes_read_ptr)
19285{
19286 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19287
ed2dc618
SM
19288 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19289 str_offset);
43988095
JK
19290}
19291
3019eac3 19292/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19293 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19294 ADDR_SIZE is the size of addresses from the CU header. */
19295
19296static CORE_ADDR
ed2dc618 19297read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19298 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19299 int addr_size)
3019eac3
DE
19300{
19301 struct objfile *objfile = dwarf2_per_objfile->objfile;
19302 bfd *abfd = objfile->obfd;
19303 const gdb_byte *info_ptr;
18a8505e 19304 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19305
96b79293 19306 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19307 if (dwarf2_per_objfile->addr.buffer == NULL)
19308 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19309 objfile_name (objfile));
18a8505e
AT
19310 if (addr_base_or_zero + addr_index * addr_size
19311 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19312 error (_("DW_FORM_addr_index pointing outside of "
19313 ".debug_addr section [in module %s]"),
4262abfb 19314 objfile_name (objfile));
3019eac3 19315 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19316 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19317 if (addr_size == 4)
19318 return bfd_get_32 (abfd, info_ptr);
19319 else
19320 return bfd_get_64 (abfd, info_ptr);
19321}
19322
19323/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19324
19325static CORE_ADDR
19326read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19327{
518817b3
SM
19328 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19329 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19330}
19331
19332/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19333
19334static CORE_ADDR
d521ce57 19335read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19336 unsigned int *bytes_read)
19337{
518817b3 19338 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19339 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19340
19341 return read_addr_index (cu, addr_index);
19342}
19343
3019eac3
DE
19344/* Given an index in .debug_addr, fetch the value.
19345 NOTE: This can be called during dwarf expression evaluation,
19346 long after the debug information has been read, and thus per_cu->cu
19347 may no longer exist. */
19348
19349CORE_ADDR
19350dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19351 unsigned int addr_index)
19352{
ed2dc618 19353 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19354 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19355 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19356 int addr_size;
19357
3019eac3
DE
19358 /* We need addr_base and addr_size.
19359 If we don't have PER_CU->cu, we have to get it.
19360 Nasty, but the alternative is storing the needed info in PER_CU,
19361 which at this point doesn't seem justified: it's not clear how frequently
19362 it would get used and it would increase the size of every PER_CU.
19363 Entry points like dwarf2_per_cu_addr_size do a similar thing
19364 so we're not in uncharted territory here.
19365 Alas we need to be a bit more complicated as addr_base is contained
19366 in the DIE.
19367
19368 We don't need to read the entire CU(/TU).
19369 We just need the header and top level die.
a1b64ce1 19370
3019eac3 19371 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19372 For now we skip this optimization. */
3019eac3
DE
19373
19374 if (cu != NULL)
19375 {
19376 addr_base = cu->addr_base;
19377 addr_size = cu->header.addr_size;
19378 }
19379 else
19380 {
c0ab21c2
TT
19381 cutu_reader reader (per_cu, NULL, 0, 0, false);
19382 addr_base = reader.cu->addr_base;
19383 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19384 }
19385
ed2dc618
SM
19386 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19387 addr_size);
3019eac3
DE
19388}
19389
18a8505e
AT
19390/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19391 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19392 DWO file. */
3019eac3 19393
d521ce57 19394static const char *
18a8505e
AT
19395read_str_index (struct dwarf2_cu *cu,
19396 struct dwarf2_section_info *str_section,
19397 struct dwarf2_section_info *str_offsets_section,
19398 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19399{
518817b3
SM
19400 struct dwarf2_per_objfile *dwarf2_per_objfile
19401 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19402 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19403 const char *objf_name = objfile_name (objfile);
3019eac3 19404 bfd *abfd = objfile->obfd;
d521ce57 19405 const gdb_byte *info_ptr;
3019eac3 19406 ULONGEST str_offset;
cf532bd1 19407 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19408
96b79293
TT
19409 str_section->read (objfile);
19410 str_offsets_section->read (objfile);
73869dc2 19411 if (str_section->buffer == NULL)
18a8505e 19412 error (_("%s used without %s section"
9d8780f0 19413 " in CU at offset %s [in module %s]"),
96b79293 19414 form_name, str_section->get_name (),
18a8505e 19415 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19416 if (str_offsets_section->buffer == NULL)
18a8505e 19417 error (_("%s used without %s section"
9d8780f0 19418 " in CU at offset %s [in module %s]"),
96b79293 19419 form_name, str_section->get_name (),
18a8505e 19420 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19421 info_ptr = (str_offsets_section->buffer
18a8505e 19422 + str_offsets_base
3019eac3
DE
19423 + str_index * cu->header.offset_size);
19424 if (cu->header.offset_size == 4)
19425 str_offset = bfd_get_32 (abfd, info_ptr);
19426 else
19427 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19428 if (str_offset >= str_section->size)
57d63ce2 19429 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19430 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19431 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19432 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19433}
19434
18a8505e
AT
19435/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19436
19437static const char *
19438read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19439{
19440 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19441 ? reader->cu->header.addr_size : 0;
19442 return read_str_index (reader->cu,
19443 &reader->dwo_file->sections.str,
19444 &reader->dwo_file->sections.str_offsets,
19445 str_offsets_base, str_index);
19446}
19447
19448/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19449
19450static const char *
19451read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19452{
19453 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19454 const char *objf_name = objfile_name (objfile);
19455 static const char form_name[] = "DW_FORM_GNU_str_index";
19456 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19457
19458 if (!cu->str_offsets_base.has_value ())
19459 error (_("%s used in Fission stub without %s"
19460 " in CU at offset 0x%lx [in module %s]"),
19461 form_name, str_offsets_attr_name,
19462 (long) cu->header.offset_size, objf_name);
19463
19464 return read_str_index (cu,
19465 &cu->per_cu->dwarf2_per_objfile->str,
19466 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19467 *cu->str_offsets_base, str_index);
19468}
19469
3019eac3
DE
19470/* Return the length of an LEB128 number in BUF. */
19471
19472static int
19473leb128_size (const gdb_byte *buf)
19474{
19475 const gdb_byte *begin = buf;
19476 gdb_byte byte;
19477
19478 while (1)
19479 {
19480 byte = *buf++;
19481 if ((byte & 128) == 0)
19482 return buf - begin;
19483 }
19484}
19485
c906108c 19486static void
e142c38c 19487set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19488{
19489 switch (lang)
19490 {
19491 case DW_LANG_C89:
76bee0cc 19492 case DW_LANG_C99:
0cfd832f 19493 case DW_LANG_C11:
c906108c 19494 case DW_LANG_C:
d1be3247 19495 case DW_LANG_UPC:
e142c38c 19496 cu->language = language_c;
c906108c 19497 break;
9c37b5ae 19498 case DW_LANG_Java:
c906108c 19499 case DW_LANG_C_plus_plus:
0cfd832f
MW
19500 case DW_LANG_C_plus_plus_11:
19501 case DW_LANG_C_plus_plus_14:
e142c38c 19502 cu->language = language_cplus;
c906108c 19503 break;
6aecb9c2
JB
19504 case DW_LANG_D:
19505 cu->language = language_d;
19506 break;
c906108c
SS
19507 case DW_LANG_Fortran77:
19508 case DW_LANG_Fortran90:
b21b22e0 19509 case DW_LANG_Fortran95:
f7de9aab
MW
19510 case DW_LANG_Fortran03:
19511 case DW_LANG_Fortran08:
e142c38c 19512 cu->language = language_fortran;
c906108c 19513 break;
a766d390
DE
19514 case DW_LANG_Go:
19515 cu->language = language_go;
19516 break;
c906108c 19517 case DW_LANG_Mips_Assembler:
e142c38c 19518 cu->language = language_asm;
c906108c
SS
19519 break;
19520 case DW_LANG_Ada83:
8aaf0b47 19521 case DW_LANG_Ada95:
bc5f45f8
JB
19522 cu->language = language_ada;
19523 break;
72019c9c
GM
19524 case DW_LANG_Modula2:
19525 cu->language = language_m2;
19526 break;
fe8e67fd
PM
19527 case DW_LANG_Pascal83:
19528 cu->language = language_pascal;
19529 break;
22566fbd
DJ
19530 case DW_LANG_ObjC:
19531 cu->language = language_objc;
19532 break;
c44af4eb
TT
19533 case DW_LANG_Rust:
19534 case DW_LANG_Rust_old:
19535 cu->language = language_rust;
19536 break;
c906108c
SS
19537 case DW_LANG_Cobol74:
19538 case DW_LANG_Cobol85:
c906108c 19539 default:
e142c38c 19540 cu->language = language_minimal;
c906108c
SS
19541 break;
19542 }
e142c38c 19543 cu->language_defn = language_def (cu->language);
c906108c
SS
19544}
19545
19546/* Return the named attribute or NULL if not there. */
19547
19548static struct attribute *
e142c38c 19549dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19550{
a48e046c 19551 for (;;)
c906108c 19552 {
a48e046c
TT
19553 unsigned int i;
19554 struct attribute *spec = NULL;
19555
19556 for (i = 0; i < die->num_attrs; ++i)
19557 {
19558 if (die->attrs[i].name == name)
19559 return &die->attrs[i];
19560 if (die->attrs[i].name == DW_AT_specification
19561 || die->attrs[i].name == DW_AT_abstract_origin)
19562 spec = &die->attrs[i];
19563 }
19564
19565 if (!spec)
19566 break;
c906108c 19567
f2f0e013 19568 die = follow_die_ref (die, spec, &cu);
f2f0e013 19569 }
c5aa993b 19570
c906108c
SS
19571 return NULL;
19572}
19573
348e048f
DE
19574/* Return the named attribute or NULL if not there,
19575 but do not follow DW_AT_specification, etc.
19576 This is for use in contexts where we're reading .debug_types dies.
19577 Following DW_AT_specification, DW_AT_abstract_origin will take us
19578 back up the chain, and we want to go down. */
19579
19580static struct attribute *
45e58e77 19581dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19582{
19583 unsigned int i;
19584
19585 for (i = 0; i < die->num_attrs; ++i)
19586 if (die->attrs[i].name == name)
19587 return &die->attrs[i];
19588
19589 return NULL;
19590}
19591
7d45c7c3
KB
19592/* Return the string associated with a string-typed attribute, or NULL if it
19593 is either not found or is of an incorrect type. */
19594
19595static const char *
19596dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19597{
19598 struct attribute *attr;
19599 const char *str = NULL;
19600
19601 attr = dwarf2_attr (die, name, cu);
19602
19603 if (attr != NULL)
19604 {
43988095 19605 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19606 || attr->form == DW_FORM_string
cf532bd1 19607 || attr->form == DW_FORM_strx
8fe0f950
AT
19608 || attr->form == DW_FORM_strx1
19609 || attr->form == DW_FORM_strx2
19610 || attr->form == DW_FORM_strx3
19611 || attr->form == DW_FORM_strx4
b3340438 19612 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19613 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19614 str = DW_STRING (attr);
19615 else
b98664d3 19616 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19617 "DIE at %s in module %s"),
19618 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19620 }
19621
19622 return str;
19623}
19624
a084a2a6 19625/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19626 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19627static const char *
19628dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19629{
19630 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19631 if (dwo_name == nullptr)
19632 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19633 return dwo_name;
19634}
19635
05cf31d1
JB
19636/* Return non-zero iff the attribute NAME is defined for the given DIE,
19637 and holds a non-zero value. This function should only be used for
2dc7f7b3 19638 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19639
19640static int
19641dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19642{
19643 struct attribute *attr = dwarf2_attr (die, name, cu);
19644
19645 return (attr && DW_UNSND (attr));
19646}
19647
3ca72b44 19648static int
e142c38c 19649die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19650{
05cf31d1
JB
19651 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19652 which value is non-zero. However, we have to be careful with
19653 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19654 (via dwarf2_flag_true_p) follows this attribute. So we may
19655 end up accidently finding a declaration attribute that belongs
19656 to a different DIE referenced by the specification attribute,
19657 even though the given DIE does not have a declaration attribute. */
19658 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19659 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19660}
19661
63d06c5c 19662/* Return the die giving the specification for DIE, if there is
f2f0e013 19663 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19664 containing the return value on output. If there is no
19665 specification, but there is an abstract origin, that is
19666 returned. */
63d06c5c
DC
19667
19668static struct die_info *
f2f0e013 19669die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19670{
f2f0e013
DJ
19671 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19672 *spec_cu);
63d06c5c 19673
edb3359d
DJ
19674 if (spec_attr == NULL)
19675 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19676
63d06c5c
DC
19677 if (spec_attr == NULL)
19678 return NULL;
19679 else
f2f0e013 19680 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19681}
c906108c 19682
527f3840
JK
19683/* Stub for free_line_header to match void * callback types. */
19684
19685static void
19686free_line_header_voidp (void *arg)
19687{
9a3c8263 19688 struct line_header *lh = (struct line_header *) arg;
527f3840 19689
fff8551c 19690 delete lh;
527f3840
JK
19691}
19692
83769d0b 19693/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19694
19695static struct dwarf2_section_info *
19696get_debug_line_section (struct dwarf2_cu *cu)
19697{
19698 struct dwarf2_section_info *section;
518817b3
SM
19699 struct dwarf2_per_objfile *dwarf2_per_objfile
19700 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19701
19702 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19703 DWO file. */
19704 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19705 section = &cu->dwo_unit->dwo_file->sections.line;
19706 else if (cu->per_cu->is_dwz)
19707 {
ed2dc618 19708 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19709
19710 section = &dwz->line;
19711 }
19712 else
19713 section = &dwarf2_per_objfile->line;
19714
19715 return section;
19716}
19717
43988095
JK
19718/* Read directory or file name entry format, starting with byte of
19719 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19720 entries count and the entries themselves in the described entry
19721 format. */
19722
19723static void
ed2dc618
SM
19724read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19725 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19726 struct line_header *lh,
19727 const struct comp_unit_head *cu_header,
19728 void (*callback) (struct line_header *lh,
19729 const char *name,
ecfb656c 19730 dir_index d_index,
43988095
JK
19731 unsigned int mod_time,
19732 unsigned int length))
19733{
19734 gdb_byte format_count, formati;
19735 ULONGEST data_count, datai;
19736 const gdb_byte *buf = *bufp;
19737 const gdb_byte *format_header_data;
43988095
JK
19738 unsigned int bytes_read;
19739
19740 format_count = read_1_byte (abfd, buf);
19741 buf += 1;
19742 format_header_data = buf;
19743 for (formati = 0; formati < format_count; formati++)
19744 {
19745 read_unsigned_leb128 (abfd, buf, &bytes_read);
19746 buf += bytes_read;
19747 read_unsigned_leb128 (abfd, buf, &bytes_read);
19748 buf += bytes_read;
19749 }
19750
19751 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19752 buf += bytes_read;
19753 for (datai = 0; datai < data_count; datai++)
19754 {
19755 const gdb_byte *format = format_header_data;
19756 struct file_entry fe;
19757
43988095
JK
19758 for (formati = 0; formati < format_count; formati++)
19759 {
ecfb656c 19760 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19761 format += bytes_read;
43988095 19762
ecfb656c 19763 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19764 format += bytes_read;
ecfb656c
PA
19765
19766 gdb::optional<const char *> string;
19767 gdb::optional<unsigned int> uint;
19768
43988095
JK
19769 switch (form)
19770 {
19771 case DW_FORM_string:
ecfb656c 19772 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19773 buf += bytes_read;
19774 break;
19775
19776 case DW_FORM_line_strp:
ed2dc618
SM
19777 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19778 abfd, buf,
ecfb656c
PA
19779 cu_header,
19780 &bytes_read));
43988095
JK
19781 buf += bytes_read;
19782 break;
19783
19784 case DW_FORM_data1:
ecfb656c 19785 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19786 buf += 1;
19787 break;
19788
19789 case DW_FORM_data2:
ecfb656c 19790 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19791 buf += 2;
19792 break;
19793
19794 case DW_FORM_data4:
ecfb656c 19795 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19796 buf += 4;
19797 break;
19798
19799 case DW_FORM_data8:
ecfb656c 19800 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19801 buf += 8;
19802 break;
19803
7ba99d21
AT
19804 case DW_FORM_data16:
19805 /* This is used for MD5, but file_entry does not record MD5s. */
19806 buf += 16;
19807 break;
19808
43988095 19809 case DW_FORM_udata:
ecfb656c 19810 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19811 buf += bytes_read;
19812 break;
19813
19814 case DW_FORM_block:
19815 /* It is valid only for DW_LNCT_timestamp which is ignored by
19816 current GDB. */
19817 break;
19818 }
ecfb656c
PA
19819
19820 switch (content_type)
19821 {
19822 case DW_LNCT_path:
19823 if (string.has_value ())
19824 fe.name = *string;
19825 break;
19826 case DW_LNCT_directory_index:
19827 if (uint.has_value ())
19828 fe.d_index = (dir_index) *uint;
19829 break;
19830 case DW_LNCT_timestamp:
19831 if (uint.has_value ())
19832 fe.mod_time = *uint;
19833 break;
19834 case DW_LNCT_size:
19835 if (uint.has_value ())
19836 fe.length = *uint;
19837 break;
19838 case DW_LNCT_MD5:
19839 break;
19840 default:
b98664d3 19841 complaint (_("Unknown format content type %s"),
ecfb656c
PA
19842 pulongest (content_type));
19843 }
43988095
JK
19844 }
19845
ecfb656c 19846 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
19847 }
19848
19849 *bufp = buf;
19850}
19851
debd256d 19852/* Read the statement program header starting at OFFSET in
3019eac3 19853 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19854 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19855 Returns NULL if there is a problem reading the header, e.g., if it
19856 has a version we don't understand.
debd256d
JB
19857
19858 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19859 the returned object point into the dwarf line section buffer,
19860 and must not be freed. */
ae2de4f8 19861
fff8551c 19862static line_header_up
9c541725 19863dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19864{
d521ce57 19865 const gdb_byte *line_ptr;
c764a876 19866 unsigned int bytes_read, offset_size;
debd256d 19867 int i;
d521ce57 19868 const char *cur_dir, *cur_file;
3019eac3
DE
19869 struct dwarf2_section_info *section;
19870 bfd *abfd;
518817b3
SM
19871 struct dwarf2_per_objfile *dwarf2_per_objfile
19872 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19873
36586728 19874 section = get_debug_line_section (cu);
96b79293 19875 section->read (dwarf2_per_objfile->objfile);
3019eac3 19876 if (section->buffer == NULL)
debd256d 19877 {
3019eac3 19878 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19879 complaint (_("missing .debug_line.dwo section"));
3019eac3 19880 else
b98664d3 19881 complaint (_("missing .debug_line section"));
debd256d
JB
19882 return 0;
19883 }
19884
fceca515
DE
19885 /* We can't do this until we know the section is non-empty.
19886 Only then do we know we have such a section. */
96b79293 19887 abfd = section->get_bfd_owner ();
fceca515 19888
a738430d
MK
19889 /* Make sure that at least there's room for the total_length field.
19890 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 19891 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 19892 {
4d3c2250 19893 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19894 return 0;
19895 }
19896
fff8551c 19897 line_header_up lh (new line_header ());
debd256d 19898
9c541725 19899 lh->sect_off = sect_off;
527f3840
JK
19900 lh->offset_in_dwz = cu->per_cu->is_dwz;
19901
9c541725 19902 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 19903
a738430d 19904 /* Read in the header. */
6e70227d 19905 lh->total_length =
c764a876
DE
19906 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
19907 &bytes_read, &offset_size);
debd256d 19908 line_ptr += bytes_read;
7ba99d21
AT
19909
19910 const gdb_byte *start_here = line_ptr;
19911
3019eac3 19912 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 19913 {
4d3c2250 19914 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
19915 return 0;
19916 }
7ba99d21 19917 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
19918 lh->version = read_2_bytes (abfd, line_ptr);
19919 line_ptr += 2;
43988095 19920 if (lh->version > 5)
cd366ee8
DE
19921 {
19922 /* This is a version we don't understand. The format could have
19923 changed in ways we don't handle properly so just punt. */
b98664d3 19924 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
19925 return NULL;
19926 }
43988095
JK
19927 if (lh->version >= 5)
19928 {
19929 gdb_byte segment_selector_size;
19930
19931 /* Skip address size. */
19932 read_1_byte (abfd, line_ptr);
19933 line_ptr += 1;
19934
19935 segment_selector_size = read_1_byte (abfd, line_ptr);
19936 line_ptr += 1;
19937 if (segment_selector_size != 0)
19938 {
b98664d3 19939 complaint (_("unsupported segment selector size %u "
43988095
JK
19940 "in .debug_line section"),
19941 segment_selector_size);
19942 return NULL;
19943 }
19944 }
c764a876
DE
19945 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
19946 line_ptr += offset_size;
7ba99d21 19947 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
19948 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
19949 line_ptr += 1;
2dc7f7b3
TT
19950 if (lh->version >= 4)
19951 {
19952 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
19953 line_ptr += 1;
19954 }
19955 else
19956 lh->maximum_ops_per_instruction = 1;
19957
19958 if (lh->maximum_ops_per_instruction == 0)
19959 {
19960 lh->maximum_ops_per_instruction = 1;
b98664d3 19961 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 19962 "in `.debug_line' section"));
2dc7f7b3
TT
19963 }
19964
debd256d
JB
19965 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
19966 line_ptr += 1;
19967 lh->line_base = read_1_signed_byte (abfd, line_ptr);
19968 line_ptr += 1;
19969 lh->line_range = read_1_byte (abfd, line_ptr);
19970 line_ptr += 1;
19971 lh->opcode_base = read_1_byte (abfd, line_ptr);
19972 line_ptr += 1;
fff8551c 19973 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
19974
19975 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
19976 for (i = 1; i < lh->opcode_base; ++i)
19977 {
19978 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
19979 line_ptr += 1;
19980 }
19981
43988095 19982 if (lh->version >= 5)
debd256d 19983 {
43988095 19984 /* Read directory table. */
ed2dc618
SM
19985 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19986 &cu->header,
b926417a 19987 [] (struct line_header *header, const char *name,
ecfb656c 19988 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19989 unsigned int length)
19990 {
b926417a 19991 header->add_include_dir (name);
fff8551c 19992 });
debd256d 19993
43988095 19994 /* Read file name table. */
ed2dc618
SM
19995 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
19996 &cu->header,
b926417a 19997 [] (struct line_header *header, const char *name,
ecfb656c 19998 dir_index d_index, unsigned int mod_time,
fff8551c
PA
19999 unsigned int length)
20000 {
b926417a 20001 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20002 });
43988095
JK
20003 }
20004 else
debd256d 20005 {
43988095
JK
20006 /* Read directory table. */
20007 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20008 {
20009 line_ptr += bytes_read;
fff8551c 20010 lh->add_include_dir (cur_dir);
43988095 20011 }
debd256d
JB
20012 line_ptr += bytes_read;
20013
43988095
JK
20014 /* Read file name table. */
20015 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20016 {
ecfb656c
PA
20017 unsigned int mod_time, length;
20018 dir_index d_index;
43988095
JK
20019
20020 line_ptr += bytes_read;
ecfb656c 20021 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20022 line_ptr += bytes_read;
20023 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20024 line_ptr += bytes_read;
20025 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20026 line_ptr += bytes_read;
20027
ecfb656c 20028 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20029 }
20030 line_ptr += bytes_read;
debd256d 20031 }
debd256d 20032
3019eac3 20033 if (line_ptr > (section->buffer + section->size))
b98664d3 20034 complaint (_("line number info header doesn't "
3e43a32a 20035 "fit in `.debug_line' section"));
debd256d 20036
debd256d
JB
20037 return lh;
20038}
c906108c 20039
c6da4cef 20040/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20041 Return the file name of the psymtab for the given file_entry.
c6da4cef 20042 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20043 If space for the result is malloc'd, *NAME_HOLDER will be set.
20044 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20045
d521ce57 20046static const char *
7ba99d21 20047psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20048 const dwarf2_psymtab *pst,
c89b44cd
TT
20049 const char *comp_dir,
20050 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20051{
d521ce57
TT
20052 const char *include_name = fe.name;
20053 const char *include_name_to_compare = include_name;
72b9f47f 20054 const char *pst_filename;
c6da4cef
DE
20055 int file_is_pst;
20056
8c43009f 20057 const char *dir_name = fe.include_dir (lh);
c6da4cef 20058
c89b44cd 20059 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20060 if (!IS_ABSOLUTE_PATH (include_name)
20061 && (dir_name != NULL || comp_dir != NULL))
20062 {
20063 /* Avoid creating a duplicate psymtab for PST.
20064 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20065 Before we do the comparison, however, we need to account
20066 for DIR_NAME and COMP_DIR.
20067 First prepend dir_name (if non-NULL). If we still don't
20068 have an absolute path prepend comp_dir (if non-NULL).
20069 However, the directory we record in the include-file's
20070 psymtab does not contain COMP_DIR (to match the
20071 corresponding symtab(s)).
20072
20073 Example:
20074
20075 bash$ cd /tmp
20076 bash$ gcc -g ./hello.c
20077 include_name = "hello.c"
20078 dir_name = "."
20079 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20080 DW_AT_name = "./hello.c"
20081
20082 */
c6da4cef
DE
20083
20084 if (dir_name != NULL)
20085 {
c89b44cd
TT
20086 name_holder->reset (concat (dir_name, SLASH_STRING,
20087 include_name, (char *) NULL));
20088 include_name = name_holder->get ();
c6da4cef 20089 include_name_to_compare = include_name;
c6da4cef
DE
20090 }
20091 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20092 {
c89b44cd
TT
20093 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20094 include_name, (char *) NULL));
20095 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20096 }
20097 }
20098
20099 pst_filename = pst->filename;
c89b44cd 20100 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20101 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20102 {
c89b44cd
TT
20103 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20104 pst_filename, (char *) NULL));
20105 pst_filename = copied_name.get ();
c6da4cef
DE
20106 }
20107
1e3fad37 20108 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20109
c6da4cef
DE
20110 if (file_is_pst)
20111 return NULL;
20112 return include_name;
20113}
20114
d9b3de22
DE
20115/* State machine to track the state of the line number program. */
20116
6f77053d 20117class lnp_state_machine
d9b3de22 20118{
6f77053d
PA
20119public:
20120 /* Initialize a machine state for the start of a line number
20121 program. */
804d2729
TT
20122 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20123 bool record_lines_p);
6f77053d 20124
8c43009f
PA
20125 file_entry *current_file ()
20126 {
20127 /* lh->file_names is 0-based, but the file name numbers in the
20128 statement program are 1-based. */
6f77053d
PA
20129 return m_line_header->file_name_at (m_file);
20130 }
20131
20132 /* Record the line in the state machine. END_SEQUENCE is true if
20133 we're processing the end of a sequence. */
20134 void record_line (bool end_sequence);
20135
7ab6656f
OJ
20136 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20137 nop-out rest of the lines in this sequence. */
6f77053d
PA
20138 void check_line_address (struct dwarf2_cu *cu,
20139 const gdb_byte *line_ptr,
7ab6656f 20140 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20141
20142 void handle_set_discriminator (unsigned int discriminator)
20143 {
20144 m_discriminator = discriminator;
20145 m_line_has_non_zero_discriminator |= discriminator != 0;
20146 }
20147
20148 /* Handle DW_LNE_set_address. */
20149 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20150 {
20151 m_op_index = 0;
20152 address += baseaddr;
20153 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20154 }
20155
20156 /* Handle DW_LNS_advance_pc. */
20157 void handle_advance_pc (CORE_ADDR adjust);
20158
20159 /* Handle a special opcode. */
20160 void handle_special_opcode (unsigned char op_code);
20161
20162 /* Handle DW_LNS_advance_line. */
20163 void handle_advance_line (int line_delta)
20164 {
20165 advance_line (line_delta);
20166 }
20167
20168 /* Handle DW_LNS_set_file. */
20169 void handle_set_file (file_name_index file);
20170
20171 /* Handle DW_LNS_negate_stmt. */
20172 void handle_negate_stmt ()
20173 {
20174 m_is_stmt = !m_is_stmt;
20175 }
20176
20177 /* Handle DW_LNS_const_add_pc. */
20178 void handle_const_add_pc ();
20179
20180 /* Handle DW_LNS_fixed_advance_pc. */
20181 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20182 {
20183 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20184 m_op_index = 0;
20185 }
20186
20187 /* Handle DW_LNS_copy. */
20188 void handle_copy ()
20189 {
20190 record_line (false);
20191 m_discriminator = 0;
20192 }
20193
20194 /* Handle DW_LNE_end_sequence. */
20195 void handle_end_sequence ()
20196 {
804d2729 20197 m_currently_recording_lines = true;
6f77053d
PA
20198 }
20199
20200private:
20201 /* Advance the line by LINE_DELTA. */
20202 void advance_line (int line_delta)
20203 {
20204 m_line += line_delta;
20205
20206 if (line_delta != 0)
20207 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20208 }
20209
804d2729
TT
20210 struct dwarf2_cu *m_cu;
20211
6f77053d
PA
20212 gdbarch *m_gdbarch;
20213
20214 /* True if we're recording lines.
20215 Otherwise we're building partial symtabs and are just interested in
20216 finding include files mentioned by the line number program. */
20217 bool m_record_lines_p;
20218
8c43009f 20219 /* The line number header. */
6f77053d 20220 line_header *m_line_header;
8c43009f 20221
6f77053d
PA
20222 /* These are part of the standard DWARF line number state machine,
20223 and initialized according to the DWARF spec. */
d9b3de22 20224
6f77053d 20225 unsigned char m_op_index = 0;
7ba99d21
AT
20226 /* The line table index of the current file. */
20227 file_name_index m_file = 1;
6f77053d
PA
20228 unsigned int m_line = 1;
20229
20230 /* These are initialized in the constructor. */
20231
20232 CORE_ADDR m_address;
20233 bool m_is_stmt;
20234 unsigned int m_discriminator;
d9b3de22
DE
20235
20236 /* Additional bits of state we need to track. */
20237
20238 /* The last file that we called dwarf2_start_subfile for.
20239 This is only used for TLLs. */
6f77053d 20240 unsigned int m_last_file = 0;
d9b3de22 20241 /* The last file a line number was recorded for. */
6f77053d 20242 struct subfile *m_last_subfile = NULL;
d9b3de22 20243
804d2729
TT
20244 /* When true, record the lines we decode. */
20245 bool m_currently_recording_lines = false;
d9b3de22
DE
20246
20247 /* The last line number that was recorded, used to coalesce
20248 consecutive entries for the same line. This can happen, for
20249 example, when discriminators are present. PR 17276. */
6f77053d
PA
20250 unsigned int m_last_line = 0;
20251 bool m_line_has_non_zero_discriminator = false;
8c43009f 20252};
d9b3de22 20253
6f77053d
PA
20254void
20255lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20256{
20257 CORE_ADDR addr_adj = (((m_op_index + adjust)
20258 / m_line_header->maximum_ops_per_instruction)
20259 * m_line_header->minimum_instruction_length);
20260 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20261 m_op_index = ((m_op_index + adjust)
20262 % m_line_header->maximum_ops_per_instruction);
20263}
d9b3de22 20264
6f77053d
PA
20265void
20266lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20267{
6f77053d
PA
20268 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20269 CORE_ADDR addr_adj = (((m_op_index
20270 + (adj_opcode / m_line_header->line_range))
20271 / m_line_header->maximum_ops_per_instruction)
20272 * m_line_header->minimum_instruction_length);
20273 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20274 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20275 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20276
6f77053d
PA
20277 int line_delta = (m_line_header->line_base
20278 + (adj_opcode % m_line_header->line_range));
20279 advance_line (line_delta);
20280 record_line (false);
20281 m_discriminator = 0;
20282}
d9b3de22 20283
6f77053d
PA
20284void
20285lnp_state_machine::handle_set_file (file_name_index file)
20286{
20287 m_file = file;
20288
20289 const file_entry *fe = current_file ();
20290 if (fe == NULL)
20291 dwarf2_debug_line_missing_file_complaint ();
20292 else if (m_record_lines_p)
20293 {
20294 const char *dir = fe->include_dir (m_line_header);
20295
c24bdb02 20296 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20297 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20298 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20299 }
20300}
20301
20302void
20303lnp_state_machine::handle_const_add_pc ()
20304{
20305 CORE_ADDR adjust
20306 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20307
20308 CORE_ADDR addr_adj
20309 = (((m_op_index + adjust)
20310 / m_line_header->maximum_ops_per_instruction)
20311 * m_line_header->minimum_instruction_length);
20312
20313 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20314 m_op_index = ((m_op_index + adjust)
20315 % m_line_header->maximum_ops_per_instruction);
20316}
d9b3de22 20317
a05a36a5
DE
20318/* Return non-zero if we should add LINE to the line number table.
20319 LINE is the line to add, LAST_LINE is the last line that was added,
20320 LAST_SUBFILE is the subfile for LAST_LINE.
20321 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20322 had a non-zero discriminator.
20323
20324 We have to be careful in the presence of discriminators.
20325 E.g., for this line:
20326
20327 for (i = 0; i < 100000; i++);
20328
20329 clang can emit four line number entries for that one line,
20330 each with a different discriminator.
20331 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20332
20333 However, we want gdb to coalesce all four entries into one.
20334 Otherwise the user could stepi into the middle of the line and
20335 gdb would get confused about whether the pc really was in the
20336 middle of the line.
20337
20338 Things are further complicated by the fact that two consecutive
20339 line number entries for the same line is a heuristic used by gcc
20340 to denote the end of the prologue. So we can't just discard duplicate
20341 entries, we have to be selective about it. The heuristic we use is
20342 that we only collapse consecutive entries for the same line if at least
20343 one of those entries has a non-zero discriminator. PR 17276.
20344
20345 Note: Addresses in the line number state machine can never go backwards
20346 within one sequence, thus this coalescing is ok. */
20347
20348static int
804d2729
TT
20349dwarf_record_line_p (struct dwarf2_cu *cu,
20350 unsigned int line, unsigned int last_line,
a05a36a5
DE
20351 int line_has_non_zero_discriminator,
20352 struct subfile *last_subfile)
20353{
c24bdb02 20354 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20355 return 1;
20356 if (line != last_line)
20357 return 1;
20358 /* Same line for the same file that we've seen already.
20359 As a last check, for pr 17276, only record the line if the line
20360 has never had a non-zero discriminator. */
20361 if (!line_has_non_zero_discriminator)
20362 return 1;
20363 return 0;
20364}
20365
804d2729
TT
20366/* Use the CU's builder to record line number LINE beginning at
20367 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20368
20369static void
d9b3de22
DE
20370dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20371 unsigned int line, CORE_ADDR address,
804d2729 20372 struct dwarf2_cu *cu)
252a6764
DE
20373{
20374 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20375
27e0867f
DE
20376 if (dwarf_line_debug)
20377 {
20378 fprintf_unfiltered (gdb_stdlog,
20379 "Recording line %u, file %s, address %s\n",
20380 line, lbasename (subfile->name),
20381 paddress (gdbarch, address));
20382 }
20383
804d2729 20384 if (cu != nullptr)
c24bdb02 20385 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20386}
20387
20388/* Subroutine of dwarf_decode_lines_1 to simplify it.
20389 Mark the end of a set of line number records.
d9b3de22 20390 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20391 If SUBFILE is NULL the request is ignored. */
20392
20393static void
20394dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20395 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20396{
27e0867f
DE
20397 if (subfile == NULL)
20398 return;
20399
20400 if (dwarf_line_debug)
20401 {
20402 fprintf_unfiltered (gdb_stdlog,
20403 "Finishing current line, file %s, address %s\n",
20404 lbasename (subfile->name),
20405 paddress (gdbarch, address));
20406 }
20407
804d2729 20408 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20409}
20410
6f77053d
PA
20411void
20412lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20413{
d9b3de22
DE
20414 if (dwarf_line_debug)
20415 {
20416 fprintf_unfiltered (gdb_stdlog,
20417 "Processing actual line %u: file %u,"
94a72be7 20418 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20419 m_line, m_file,
6f77053d 20420 paddress (m_gdbarch, m_address),
94a72be7
AB
20421 m_is_stmt, m_discriminator,
20422 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20423 }
20424
6f77053d 20425 file_entry *fe = current_file ();
8c43009f
PA
20426
20427 if (fe == NULL)
d9b3de22
DE
20428 dwarf2_debug_line_missing_file_complaint ();
20429 /* For now we ignore lines not starting on an instruction boundary.
20430 But not when processing end_sequence for compatibility with the
20431 previous version of the code. */
6f77053d 20432 else if (m_op_index == 0 || end_sequence)
d9b3de22 20433 {
8c43009f 20434 fe->included_p = 1;
94a72be7
AB
20435 if (m_record_lines_p
20436 && (producer_is_codewarrior (m_cu) || m_is_stmt || end_sequence))
d9b3de22 20437 {
c24bdb02 20438 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20439 || end_sequence)
d9b3de22 20440 {
804d2729
TT
20441 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20442 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20443 }
20444
20445 if (!end_sequence)
20446 {
804d2729 20447 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20448 m_line_has_non_zero_discriminator,
20449 m_last_subfile))
d9b3de22 20450 {
c24bdb02 20451 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20452 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20453 builder->get_current_subfile (),
6f77053d 20454 m_line, m_address,
804d2729 20455 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20456 }
c24bdb02 20457 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20458 m_last_line = m_line;
d9b3de22
DE
20459 }
20460 }
20461 }
20462}
20463
804d2729
TT
20464lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20465 line_header *lh, bool record_lines_p)
d9b3de22 20466{
804d2729 20467 m_cu = cu;
6f77053d
PA
20468 m_gdbarch = arch;
20469 m_record_lines_p = record_lines_p;
20470 m_line_header = lh;
d9b3de22 20471
804d2729 20472 m_currently_recording_lines = true;
d9b3de22 20473
d9b3de22
DE
20474 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20475 was a line entry for it so that the backend has a chance to adjust it
20476 and also record it in case it needs it. This is currently used by MIPS
20477 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20478 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20479 m_is_stmt = lh->default_is_stmt;
20480 m_discriminator = 0;
252a6764
DE
20481}
20482
6f77053d
PA
20483void
20484lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20485 const gdb_byte *line_ptr,
7ab6656f 20486 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20487{
7ab6656f
OJ
20488 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20489 the pc range of the CU. However, we restrict the test to only ADDRESS
20490 values of zero to preserve GDB's previous behaviour which is to handle
20491 the specific case of a function being GC'd by the linker. */
924c2928 20492
7ab6656f 20493 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20494 {
20495 /* This line table is for a function which has been
20496 GCd by the linker. Ignore it. PR gdb/12528 */
20497
518817b3 20498 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20499 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20500
b98664d3 20501 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20502 line_offset, objfile_name (objfile));
804d2729
TT
20503 m_currently_recording_lines = false;
20504 /* Note: m_currently_recording_lines is left as false until we see
20505 DW_LNE_end_sequence. */
924c2928
DE
20506 }
20507}
20508
f3f5162e 20509/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20510 Process the line number information in LH.
20511 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20512 program in order to set included_p for every referenced header. */
debd256d 20513
c906108c 20514static void
43f3e411
DE
20515dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20516 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20517{
d521ce57
TT
20518 const gdb_byte *line_ptr, *extended_end;
20519 const gdb_byte *line_end;
a8c50c1f 20520 unsigned int bytes_read, extended_len;
699ca60a 20521 unsigned char op_code, extended_op;
e142c38c 20522 CORE_ADDR baseaddr;
518817b3 20523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20524 bfd *abfd = objfile->obfd;
fbf65064 20525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20526 /* True if we're recording line info (as opposed to building partial
20527 symtabs and just interested in finding include files mentioned by
20528 the line number program). */
20529 bool record_lines_p = !decode_for_pst_p;
e142c38c 20530
b3b3bada 20531 baseaddr = objfile->text_section_offset ();
c906108c 20532
debd256d
JB
20533 line_ptr = lh->statement_program_start;
20534 line_end = lh->statement_program_end;
c906108c
SS
20535
20536 /* Read the statement sequences until there's nothing left. */
20537 while (line_ptr < line_end)
20538 {
6f77053d
PA
20539 /* The DWARF line number program state machine. Reset the state
20540 machine at the start of each sequence. */
804d2729 20541 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20542 bool end_sequence = false;
d9b3de22 20543
8c43009f 20544 if (record_lines_p)
c906108c 20545 {
8c43009f
PA
20546 /* Start a subfile for the current file of the state
20547 machine. */
20548 const file_entry *fe = state_machine.current_file ();
20549
20550 if (fe != NULL)
804d2729 20551 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20552 }
20553
a738430d 20554 /* Decode the table. */
d9b3de22 20555 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20556 {
20557 op_code = read_1_byte (abfd, line_ptr);
20558 line_ptr += 1;
9aa1fe7e 20559
debd256d 20560 if (op_code >= lh->opcode_base)
6e70227d 20561 {
8e07a239 20562 /* Special opcode. */
6f77053d 20563 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20564 }
20565 else switch (op_code)
c906108c
SS
20566 {
20567 case DW_LNS_extended_op:
3e43a32a
MS
20568 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20569 &bytes_read);
473b7be6 20570 line_ptr += bytes_read;
a8c50c1f 20571 extended_end = line_ptr + extended_len;
c906108c
SS
20572 extended_op = read_1_byte (abfd, line_ptr);
20573 line_ptr += 1;
20574 switch (extended_op)
20575 {
20576 case DW_LNE_end_sequence:
6f77053d
PA
20577 state_machine.handle_end_sequence ();
20578 end_sequence = true;
c906108c
SS
20579 break;
20580 case DW_LNE_set_address:
d9b3de22
DE
20581 {
20582 CORE_ADDR address
20583 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20584 line_ptr += bytes_read;
6f77053d
PA
20585
20586 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20587 lowpc - baseaddr, address);
6f77053d 20588 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20589 }
c906108c
SS
20590 break;
20591 case DW_LNE_define_file:
debd256d 20592 {
d521ce57 20593 const char *cur_file;
ecfb656c
PA
20594 unsigned int mod_time, length;
20595 dir_index dindex;
6e70227d 20596
3e43a32a
MS
20597 cur_file = read_direct_string (abfd, line_ptr,
20598 &bytes_read);
debd256d 20599 line_ptr += bytes_read;
ecfb656c 20600 dindex = (dir_index)
debd256d
JB
20601 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20602 line_ptr += bytes_read;
20603 mod_time =
20604 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20605 line_ptr += bytes_read;
20606 length =
20607 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20608 line_ptr += bytes_read;
ecfb656c 20609 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20610 }
c906108c 20611 break;
d0c6ba3d 20612 case DW_LNE_set_discriminator:
6f77053d
PA
20613 {
20614 /* The discriminator is not interesting to the
20615 debugger; just ignore it. We still need to
20616 check its value though:
20617 if there are consecutive entries for the same
20618 (non-prologue) line we want to coalesce them.
20619 PR 17276. */
20620 unsigned int discr
20621 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20622 line_ptr += bytes_read;
20623
20624 state_machine.handle_set_discriminator (discr);
20625 }
d0c6ba3d 20626 break;
c906108c 20627 default:
b98664d3 20628 complaint (_("mangled .debug_line section"));
debd256d 20629 return;
c906108c 20630 }
a8c50c1f
DJ
20631 /* Make sure that we parsed the extended op correctly. If e.g.
20632 we expected a different address size than the producer used,
20633 we may have read the wrong number of bytes. */
20634 if (line_ptr != extended_end)
20635 {
b98664d3 20636 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20637 return;
20638 }
c906108c
SS
20639 break;
20640 case DW_LNS_copy:
6f77053d 20641 state_machine.handle_copy ();
c906108c
SS
20642 break;
20643 case DW_LNS_advance_pc:
2dc7f7b3
TT
20644 {
20645 CORE_ADDR adjust
20646 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20647 line_ptr += bytes_read;
6f77053d
PA
20648
20649 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20650 }
c906108c
SS
20651 break;
20652 case DW_LNS_advance_line:
a05a36a5
DE
20653 {
20654 int line_delta
20655 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20656 line_ptr += bytes_read;
6f77053d
PA
20657
20658 state_machine.handle_advance_line (line_delta);
a05a36a5 20659 }
c906108c
SS
20660 break;
20661 case DW_LNS_set_file:
d9b3de22 20662 {
6f77053d 20663 file_name_index file
ecfb656c
PA
20664 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20665 &bytes_read);
d9b3de22 20666 line_ptr += bytes_read;
8c43009f 20667
6f77053d 20668 state_machine.handle_set_file (file);
d9b3de22 20669 }
c906108c
SS
20670 break;
20671 case DW_LNS_set_column:
0ad93d4f 20672 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20673 line_ptr += bytes_read;
20674 break;
20675 case DW_LNS_negate_stmt:
6f77053d 20676 state_machine.handle_negate_stmt ();
c906108c
SS
20677 break;
20678 case DW_LNS_set_basic_block:
c906108c 20679 break;
c2c6d25f
JM
20680 /* Add to the address register of the state machine the
20681 address increment value corresponding to special opcode
a738430d
MK
20682 255. I.e., this value is scaled by the minimum
20683 instruction length since special opcode 255 would have
b021a221 20684 scaled the increment. */
c906108c 20685 case DW_LNS_const_add_pc:
6f77053d 20686 state_machine.handle_const_add_pc ();
c906108c
SS
20687 break;
20688 case DW_LNS_fixed_advance_pc:
3e29f34a 20689 {
6f77053d 20690 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20691 line_ptr += 2;
6f77053d
PA
20692
20693 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20694 }
c906108c 20695 break;
9aa1fe7e 20696 default:
a738430d
MK
20697 {
20698 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20699 int i;
a738430d 20700
debd256d 20701 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20702 {
20703 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20704 line_ptr += bytes_read;
20705 }
20706 }
c906108c
SS
20707 }
20708 }
d9b3de22
DE
20709
20710 if (!end_sequence)
20711 dwarf2_debug_line_missing_end_sequence_complaint ();
20712
20713 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20714 in which case we still finish recording the last line). */
6f77053d 20715 state_machine.record_line (true);
c906108c 20716 }
f3f5162e
DE
20717}
20718
20719/* Decode the Line Number Program (LNP) for the given line_header
20720 structure and CU. The actual information extracted and the type
20721 of structures created from the LNP depends on the value of PST.
20722
20723 1. If PST is NULL, then this procedure uses the data from the program
20724 to create all necessary symbol tables, and their linetables.
20725
20726 2. If PST is not NULL, this procedure reads the program to determine
20727 the list of files included by the unit represented by PST, and
20728 builds all the associated partial symbol tables.
20729
20730 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20731 It is used for relative paths in the line table.
20732 NOTE: When processing partial symtabs (pst != NULL),
20733 comp_dir == pst->dirname.
20734
20735 NOTE: It is important that psymtabs have the same file name (via strcmp)
20736 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20737 symtab we don't use it in the name of the psymtabs we create.
20738 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20739 A good testcase for this is mb-inline.exp.
20740
527f3840
JK
20741 LOWPC is the lowest address in CU (or 0 if not known).
20742
20743 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20744 for its PC<->lines mapping information. Otherwise only the filename
20745 table is read in. */
f3f5162e
DE
20746
20747static void
20748dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20749 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20750 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20751{
518817b3 20752 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20753 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20754
527f3840
JK
20755 if (decode_mapping)
20756 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20757
20758 if (decode_for_pst_p)
20759 {
aaa75496
JB
20760 /* Now that we're done scanning the Line Header Program, we can
20761 create the psymtab of each included file. */
7ba99d21
AT
20762 for (auto &file_entry : lh->file_names ())
20763 if (file_entry.included_p == 1)
aaa75496 20764 {
c89b44cd 20765 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20766 const char *include_name =
7ba99d21
AT
20767 psymtab_include_file_name (lh, file_entry, pst,
20768 comp_dir, &name_holder);
c6da4cef 20769 if (include_name != NULL)
aaa75496
JB
20770 dwarf2_create_include_psymtab (include_name, pst, objfile);
20771 }
20772 }
cb1df416
DJ
20773 else
20774 {
20775 /* Make sure a symtab is created for every file, even files
20776 which contain only variables (i.e. no code with associated
20777 line numbers). */
c24bdb02
KS
20778 buildsym_compunit *builder = cu->get_builder ();
20779 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20780
7ba99d21 20781 for (auto &fe : lh->file_names ())
cb1df416 20782 {
804d2729 20783 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20784 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20785 {
c24bdb02 20786 builder->get_current_subfile ()->symtab
804d2729 20787 = allocate_symtab (cust,
c24bdb02 20788 builder->get_current_subfile ()->name);
43f3e411 20789 }
c24bdb02 20790 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20791 }
20792 }
c906108c
SS
20793}
20794
20795/* Start a subfile for DWARF. FILENAME is the name of the file and
20796 DIRNAME the name of the source directory which contains FILENAME
4d663531 20797 or NULL if not known.
c906108c
SS
20798 This routine tries to keep line numbers from identical absolute and
20799 relative file names in a common subfile.
20800
20801 Using the `list' example from the GDB testsuite, which resides in
20802 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20803 of /srcdir/list0.c yields the following debugging information for list0.c:
20804
c5aa993b 20805 DW_AT_name: /srcdir/list0.c
4d663531 20806 DW_AT_comp_dir: /compdir
357e46e7 20807 files.files[0].name: list0.h
c5aa993b 20808 files.files[0].dir: /srcdir
357e46e7 20809 files.files[1].name: list0.c
c5aa993b 20810 files.files[1].dir: /srcdir
c906108c
SS
20811
20812 The line number information for list0.c has to end up in a single
4f1520fb
FR
20813 subfile, so that `break /srcdir/list0.c:1' works as expected.
20814 start_subfile will ensure that this happens provided that we pass the
20815 concatenation of files.files[1].dir and files.files[1].name as the
20816 subfile's name. */
c906108c
SS
20817
20818static void
804d2729
TT
20819dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20820 const char *dirname)
c906108c 20821{
43816ebc 20822 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20823
4d663531 20824 /* In order not to lose the line information directory,
4f1520fb
FR
20825 we concatenate it to the filename when it makes sense.
20826 Note that the Dwarf3 standard says (speaking of filenames in line
20827 information): ``The directory index is ignored for file names
20828 that represent full path names''. Thus ignoring dirname in the
20829 `else' branch below isn't an issue. */
c906108c 20830
d5166ae1 20831 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20832 {
43816ebc
TT
20833 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20834 filename = copy.get ();
d521ce57 20835 }
c906108c 20836
c24bdb02 20837 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20838}
20839
804d2729
TT
20840/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20841 buildsym_compunit constructor. */
f4dc4d17 20842
c24bdb02
KS
20843struct compunit_symtab *
20844dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20845 CORE_ADDR low_pc)
f4dc4d17 20846{
c24bdb02 20847 gdb_assert (m_builder == nullptr);
43f3e411 20848
c24bdb02
KS
20849 m_builder.reset (new struct buildsym_compunit
20850 (per_cu->dwarf2_per_objfile->objfile,
20851 name, comp_dir, language, low_pc));
93b8bea4 20852
c24bdb02 20853 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20854
c24bdb02
KS
20855 get_builder ()->record_debugformat ("DWARF 2");
20856 get_builder ()->record_producer (producer);
f4dc4d17 20857
c24bdb02 20858 processing_has_namespace_info = false;
43f3e411 20859
c24bdb02 20860 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20861}
20862
4c2df51b
DJ
20863static void
20864var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20865 struct dwarf2_cu *cu)
4c2df51b 20866{
518817b3 20867 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20868 struct comp_unit_head *cu_header = &cu->header;
20869
4c2df51b
DJ
20870 /* NOTE drow/2003-01-30: There used to be a comment and some special
20871 code here to turn a symbol with DW_AT_external and a
20872 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20873 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20874 with some versions of binutils) where shared libraries could have
20875 relocations against symbols in their debug information - the
20876 minimal symbol would have the right address, but the debug info
20877 would not. It's no longer necessary, because we will explicitly
20878 apply relocations when we read in the debug information now. */
20879
20880 /* A DW_AT_location attribute with no contents indicates that a
20881 variable has been optimized away. */
4fc6c0d5 20882 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20883 {
f1e6e072 20884 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20885 return;
20886 }
20887
20888 /* Handle one degenerate form of location expression specially, to
20889 preserve GDB's previous behavior when section offsets are
336d760d
AT
20890 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20891 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20892
4fc6c0d5 20893 if (attr->form_is_block ()
3019eac3
DE
20894 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20895 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20896 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20897 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20898 && (DW_BLOCK (attr)->size
20899 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20900 {
891d2f0b 20901 unsigned int dummy;
4c2df51b 20902
3019eac3 20903 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
20904 SET_SYMBOL_VALUE_ADDRESS (sym,
20905 read_address (objfile->obfd,
20906 DW_BLOCK (attr)->data + 1,
20907 cu, &dummy));
3019eac3 20908 else
38583298
TT
20909 SET_SYMBOL_VALUE_ADDRESS
20910 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20911 &dummy));
f1e6e072 20912 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20913 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20914 SET_SYMBOL_VALUE_ADDRESS
20915 (sym,
20916 SYMBOL_VALUE_ADDRESS (sym)
20917 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20918 return;
20919 }
20920
20921 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20922 expression evaluator, and use LOC_COMPUTED only when necessary
20923 (i.e. when the value of a register or memory location is
20924 referenced, or a thread-local block, etc.). Then again, it might
20925 not be worthwhile. I'm assuming that it isn't unless performance
20926 or memory numbers show me otherwise. */
20927
f1e6e072 20928 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20929
f1e6e072 20930 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20931 cu->has_loclist = true;
4c2df51b
DJ
20932}
20933
c906108c
SS
20934/* Given a pointer to a DWARF information entry, figure out if we need
20935 to make a symbol table entry for it, and if so, create a new entry
20936 and return a pointer to it.
20937 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20938 used the passed type.
20939 If SPACE is not NULL, use it to hold the new symbol. If it is
20940 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20941
20942static struct symbol *
5e2db402
TT
20943new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20944 struct symbol *space)
c906108c 20945{
518817b3
SM
20946 struct dwarf2_per_objfile *dwarf2_per_objfile
20947 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20948 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20949 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20950 struct symbol *sym = NULL;
15d034d0 20951 const char *name;
c906108c
SS
20952 struct attribute *attr = NULL;
20953 struct attribute *attr2 = NULL;
e142c38c 20954 CORE_ADDR baseaddr;
e37fd15a
SW
20955 struct pending **list_to_add = NULL;
20956
edb3359d 20957 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20958
b3b3bada 20959 baseaddr = objfile->text_section_offset ();
c906108c 20960
94af9270 20961 name = dwarf2_name (die, cu);
c906108c
SS
20962 if (name)
20963 {
94af9270 20964 const char *linkagename;
34eaf542 20965 int suppress_add = 0;
94af9270 20966
34eaf542
TT
20967 if (space)
20968 sym = space;
20969 else
e623cf5d 20970 sym = allocate_symbol (objfile);
c906108c 20971 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20972
20973 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20974 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20975 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20976 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20977
f55ee35c
JK
20978 /* Fortran does not have mangling standard and the mangling does differ
20979 between gfortran, iFort etc. */
20980 if (cu->language == language_fortran
468c0cbb
CB
20981 && symbol_get_demangled_name (sym) == NULL)
20982 symbol_set_demangled_name (sym,
cfc594ee 20983 dwarf2_full_name (name, die, cu),
29df156d 20984 NULL);
f55ee35c 20985
c906108c 20986 /* Default assumptions.
c5aa993b 20987 Use the passed type or decode it from the die. */
176620f1 20988 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20989 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20990 if (type != NULL)
20991 SYMBOL_TYPE (sym) = type;
20992 else
e7c27a73 20993 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20994 attr = dwarf2_attr (die,
20995 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20996 cu);
435d3d88 20997 if (attr != nullptr)
c906108c
SS
20998 {
20999 SYMBOL_LINE (sym) = DW_UNSND (attr);
21000 }
cb1df416 21001
edb3359d
DJ
21002 attr = dwarf2_attr (die,
21003 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21004 cu);
435d3d88 21005 if (attr != nullptr)
cb1df416 21006 {
ecfb656c 21007 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21008 struct file_entry *fe;
9a619af0 21009
ecfb656c
PA
21010 if (cu->line_header != NULL)
21011 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21012 else
21013 fe = NULL;
21014
21015 if (fe == NULL)
b98664d3 21016 complaint (_("file index out of range"));
8c43009f
PA
21017 else
21018 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21019 }
21020
c906108c
SS
21021 switch (die->tag)
21022 {
21023 case DW_TAG_label:
e142c38c 21024 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21025 if (attr != nullptr)
3e29f34a
MR
21026 {
21027 CORE_ADDR addr;
21028
cd6c91b4 21029 addr = attr->value_as_address ();
3e29f34a 21030 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21031 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21032 }
0f5238ed
TT
21033 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21034 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21035 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21036 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21037 break;
21038 case DW_TAG_subprogram:
21039 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21040 finish_block. */
f1e6e072 21041 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21042 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21043 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21044 || cu->language == language_ada
21045 || cu->language == language_fortran)
c906108c 21046 {
2cfa0c8d 21047 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21048 Ada and Fortran subprograms, whether marked external or
21049 not, are always stored as a global symbol, because we want
21050 to be able to access them globally. For instance, we want
21051 to be able to break on a nested subprogram without having
21052 to specify the context. */
c24bdb02 21053 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21054 }
21055 else
21056 {
e37fd15a 21057 list_to_add = cu->list_in_scope;
c906108c
SS
21058 }
21059 break;
edb3359d
DJ
21060 case DW_TAG_inlined_subroutine:
21061 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21062 finish_block. */
f1e6e072 21063 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21064 SYMBOL_INLINED (sym) = 1;
481860b3 21065 list_to_add = cu->list_in_scope;
edb3359d 21066 break;
34eaf542
TT
21067 case DW_TAG_template_value_param:
21068 suppress_add = 1;
21069 /* Fall through. */
72929c62 21070 case DW_TAG_constant:
c906108c 21071 case DW_TAG_variable:
254e6b9e 21072 case DW_TAG_member:
0963b4bd
MS
21073 /* Compilation with minimal debug info may result in
21074 variables with missing type entries. Change the
21075 misleading `void' type to something sensible. */
c906108c 21076 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21077 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21078
e142c38c 21079 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21080 /* In the case of DW_TAG_member, we should only be called for
21081 static const members. */
21082 if (die->tag == DW_TAG_member)
21083 {
3863f96c
DE
21084 /* dwarf2_add_field uses die_is_declaration,
21085 so we do the same. */
254e6b9e
DE
21086 gdb_assert (die_is_declaration (die, cu));
21087 gdb_assert (attr);
21088 }
435d3d88 21089 if (attr != nullptr)
c906108c 21090 {
e7c27a73 21091 dwarf2_const_value (attr, sym, cu);
e142c38c 21092 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21093 if (!suppress_add)
34eaf542
TT
21094 {
21095 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21096 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21097 else
e37fd15a 21098 list_to_add = cu->list_in_scope;
34eaf542 21099 }
c906108c
SS
21100 break;
21101 }
e142c38c 21102 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21103 if (attr != nullptr)
c906108c 21104 {
e7c27a73 21105 var_decode_location (attr, sym, cu);
e142c38c 21106 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21107
21108 /* Fortran explicitly imports any global symbols to the local
21109 scope by DW_TAG_common_block. */
21110 if (cu->language == language_fortran && die->parent
21111 && die->parent->tag == DW_TAG_common_block)
21112 attr2 = NULL;
21113
caac4577
JG
21114 if (SYMBOL_CLASS (sym) == LOC_STATIC
21115 && SYMBOL_VALUE_ADDRESS (sym) == 0
21116 && !dwarf2_per_objfile->has_section_at_zero)
21117 {
21118 /* When a static variable is eliminated by the linker,
21119 the corresponding debug information is not stripped
21120 out, but the variable address is set to null;
21121 do not add such variables into symbol table. */
21122 }
21123 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21124 {
4b610737
TT
21125 if (SYMBOL_CLASS (sym) == LOC_STATIC
21126 && (objfile->flags & OBJF_MAINLINE) == 0
21127 && dwarf2_per_objfile->can_copy)
21128 {
21129 /* A global static variable might be subject to
21130 copy relocation. We first check for a local
21131 minsym, though, because maybe the symbol was
21132 marked hidden, in which case this would not
21133 apply. */
21134 bound_minimal_symbol found
21135 = (lookup_minimal_symbol_linkage
987012b8 21136 (sym->linkage_name (), objfile));
4b610737
TT
21137 if (found.minsym != nullptr)
21138 sym->maybe_copied = 1;
21139 }
f55ee35c 21140
1c809c68
TT
21141 /* A variable with DW_AT_external is never static,
21142 but it may be block-scoped. */
804d2729 21143 list_to_add
c24bdb02
KS
21144 = ((cu->list_in_scope
21145 == cu->get_builder ()->get_file_symbols ())
21146 ? cu->get_builder ()->get_global_symbols ()
804d2729 21147 : cu->list_in_scope);
1c809c68 21148 }
c906108c 21149 else
e37fd15a 21150 list_to_add = cu->list_in_scope;
c906108c
SS
21151 }
21152 else
21153 {
21154 /* We do not know the address of this symbol.
c5aa993b
JM
21155 If it is an external symbol and we have type information
21156 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21157 The address of the variable will then be determined from
21158 the minimal symbol table whenever the variable is
21159 referenced. */
e142c38c 21160 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21161
21162 /* Fortran explicitly imports any global symbols to the local
21163 scope by DW_TAG_common_block. */
21164 if (cu->language == language_fortran && die->parent
21165 && die->parent->tag == DW_TAG_common_block)
21166 {
21167 /* SYMBOL_CLASS doesn't matter here because
21168 read_common_block is going to reset it. */
21169 if (!suppress_add)
21170 list_to_add = cu->list_in_scope;
21171 }
21172 else if (attr2 && (DW_UNSND (attr2) != 0)
21173 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21174 {
0fe7935b
DJ
21175 /* A variable with DW_AT_external is never static, but it
21176 may be block-scoped. */
804d2729 21177 list_to_add
c24bdb02
KS
21178 = ((cu->list_in_scope
21179 == cu->get_builder ()->get_file_symbols ())
21180 ? cu->get_builder ()->get_global_symbols ()
804d2729 21181 : cu->list_in_scope);
0fe7935b 21182
f1e6e072 21183 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21184 }
442ddf59
JK
21185 else if (!die_is_declaration (die, cu))
21186 {
21187 /* Use the default LOC_OPTIMIZED_OUT class. */
21188 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21189 if (!suppress_add)
21190 list_to_add = cu->list_in_scope;
442ddf59 21191 }
c906108c
SS
21192 }
21193 break;
21194 case DW_TAG_formal_parameter:
a60f3166
TT
21195 {
21196 /* If we are inside a function, mark this as an argument. If
21197 not, we might be looking at an argument to an inlined function
21198 when we do not have enough information to show inlined frames;
21199 pretend it's a local variable in that case so that the user can
21200 still see it. */
804d2729 21201 struct context_stack *curr
c24bdb02 21202 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21203 if (curr != nullptr && curr->name != nullptr)
21204 SYMBOL_IS_ARGUMENT (sym) = 1;
21205 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21206 if (attr != nullptr)
a60f3166
TT
21207 {
21208 var_decode_location (attr, sym, cu);
21209 }
21210 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21211 if (attr != nullptr)
a60f3166
TT
21212 {
21213 dwarf2_const_value (attr, sym, cu);
21214 }
f346a30d 21215
a60f3166
TT
21216 list_to_add = cu->list_in_scope;
21217 }
c906108c
SS
21218 break;
21219 case DW_TAG_unspecified_parameters:
21220 /* From varargs functions; gdb doesn't seem to have any
21221 interest in this information, so just ignore it for now.
21222 (FIXME?) */
21223 break;
34eaf542
TT
21224 case DW_TAG_template_type_param:
21225 suppress_add = 1;
21226 /* Fall through. */
c906108c 21227 case DW_TAG_class_type:
680b30c7 21228 case DW_TAG_interface_type:
c906108c
SS
21229 case DW_TAG_structure_type:
21230 case DW_TAG_union_type:
72019c9c 21231 case DW_TAG_set_type:
c906108c 21232 case DW_TAG_enumeration_type:
f1e6e072 21233 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21234 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21235
63d06c5c 21236 {
9c37b5ae 21237 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21238 really ever be static objects: otherwise, if you try
21239 to, say, break of a class's method and you're in a file
21240 which doesn't mention that class, it won't work unless
21241 the check for all static symbols in lookup_symbol_aux
21242 saves you. See the OtherFileClass tests in
21243 gdb.c++/namespace.exp. */
21244
e37fd15a 21245 if (!suppress_add)
34eaf542 21246 {
c24bdb02 21247 buildsym_compunit *builder = cu->get_builder ();
804d2729 21248 list_to_add
c24bdb02 21249 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21250 && cu->language == language_cplus
c24bdb02 21251 ? builder->get_global_symbols ()
804d2729 21252 : cu->list_in_scope);
63d06c5c 21253
64382290 21254 /* The semantics of C++ state that "struct foo {
9c37b5ae 21255 ... }" also defines a typedef for "foo". */
64382290 21256 if (cu->language == language_cplus
45280282 21257 || cu->language == language_ada
c44af4eb
TT
21258 || cu->language == language_d
21259 || cu->language == language_rust)
64382290
TT
21260 {
21261 /* The symbol's name is already allocated along
21262 with this objfile, so we don't need to
21263 duplicate it for the type. */
21264 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 21265 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 21266 }
63d06c5c
DC
21267 }
21268 }
c906108c
SS
21269 break;
21270 case DW_TAG_typedef:
f1e6e072 21271 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21272 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21273 list_to_add = cu->list_in_scope;
63d06c5c 21274 break;
c906108c 21275 case DW_TAG_base_type:
a02abb62 21276 case DW_TAG_subrange_type:
f1e6e072 21277 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21278 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21279 list_to_add = cu->list_in_scope;
c906108c
SS
21280 break;
21281 case DW_TAG_enumerator:
e142c38c 21282 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21283 if (attr != nullptr)
c906108c 21284 {
e7c27a73 21285 dwarf2_const_value (attr, sym, cu);
c906108c 21286 }
63d06c5c
DC
21287 {
21288 /* NOTE: carlton/2003-11-10: See comment above in the
21289 DW_TAG_class_type, etc. block. */
21290
804d2729 21291 list_to_add
c24bdb02 21292 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21293 && cu->language == language_cplus
c24bdb02 21294 ? cu->get_builder ()->get_global_symbols ()
804d2729 21295 : cu->list_in_scope);
63d06c5c 21296 }
c906108c 21297 break;
74921315 21298 case DW_TAG_imported_declaration:
5c4e30ca 21299 case DW_TAG_namespace:
f1e6e072 21300 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21301 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21302 break;
530e8392
KB
21303 case DW_TAG_module:
21304 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21305 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21306 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21307 break;
4357ac6c 21308 case DW_TAG_common_block:
f1e6e072 21309 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21310 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21311 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21312 break;
c906108c
SS
21313 default:
21314 /* Not a tag we recognize. Hopefully we aren't processing
21315 trash data, but since we must specifically ignore things
21316 we don't recognize, there is nothing else we should do at
0963b4bd 21317 this point. */
b98664d3 21318 complaint (_("unsupported tag: '%s'"),
4d3c2250 21319 dwarf_tag_name (die->tag));
c906108c
SS
21320 break;
21321 }
df8a16a1 21322
e37fd15a
SW
21323 if (suppress_add)
21324 {
21325 sym->hash_next = objfile->template_symbols;
21326 objfile->template_symbols = sym;
21327 list_to_add = NULL;
21328 }
21329
21330 if (list_to_add != NULL)
d3cb6808 21331 add_symbol_to_list (sym, list_to_add);
e37fd15a 21332
df8a16a1
DJ
21333 /* For the benefit of old versions of GCC, check for anonymous
21334 namespaces based on the demangled name. */
4d4ec4e5 21335 if (!cu->processing_has_namespace_info
94af9270 21336 && cu->language == language_cplus)
c24bdb02 21337 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21338 }
21339 return (sym);
21340}
21341
98bfdba5
PA
21342/* Given an attr with a DW_FORM_dataN value in host byte order,
21343 zero-extend it as appropriate for the symbol's type. The DWARF
21344 standard (v4) is not entirely clear about the meaning of using
21345 DW_FORM_dataN for a constant with a signed type, where the type is
21346 wider than the data. The conclusion of a discussion on the DWARF
21347 list was that this is unspecified. We choose to always zero-extend
21348 because that is the interpretation long in use by GCC. */
c906108c 21349
98bfdba5 21350static gdb_byte *
ff39bb5e 21351dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21352 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21353{
518817b3 21354 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21355 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21356 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21357 LONGEST l = DW_UNSND (attr);
21358
21359 if (bits < sizeof (*value) * 8)
21360 {
21361 l &= ((LONGEST) 1 << bits) - 1;
21362 *value = l;
21363 }
21364 else if (bits == sizeof (*value) * 8)
21365 *value = l;
21366 else
21367 {
224c3ddb 21368 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21369 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21370 return bytes;
21371 }
21372
21373 return NULL;
21374}
21375
21376/* Read a constant value from an attribute. Either set *VALUE, or if
21377 the value does not fit in *VALUE, set *BYTES - either already
21378 allocated on the objfile obstack, or newly allocated on OBSTACK,
21379 or, set *BATON, if we translated the constant to a location
21380 expression. */
21381
21382static void
ff39bb5e 21383dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21384 const char *name, struct obstack *obstack,
21385 struct dwarf2_cu *cu,
d521ce57 21386 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21387 struct dwarf2_locexpr_baton **baton)
21388{
518817b3 21389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21390 struct comp_unit_head *cu_header = &cu->header;
c906108c 21391 struct dwarf_block *blk;
98bfdba5
PA
21392 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21393 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21394
21395 *value = 0;
21396 *bytes = NULL;
21397 *baton = NULL;
c906108c
SS
21398
21399 switch (attr->form)
21400 {
21401 case DW_FORM_addr:
336d760d 21402 case DW_FORM_addrx:
3019eac3 21403 case DW_FORM_GNU_addr_index:
ac56253d 21404 {
ac56253d
TT
21405 gdb_byte *data;
21406
98bfdba5
PA
21407 if (TYPE_LENGTH (type) != cu_header->addr_size)
21408 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21409 cu_header->addr_size,
98bfdba5 21410 TYPE_LENGTH (type));
ac56253d
TT
21411 /* Symbols of this form are reasonably rare, so we just
21412 piggyback on the existing location code rather than writing
21413 a new implementation of symbol_computed_ops. */
8d749320 21414 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21415 (*baton)->per_cu = cu->per_cu;
21416 gdb_assert ((*baton)->per_cu);
ac56253d 21417
98bfdba5 21418 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21419 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21420 (*baton)->data = data;
ac56253d
TT
21421
21422 data[0] = DW_OP_addr;
21423 store_unsigned_integer (&data[1], cu_header->addr_size,
21424 byte_order, DW_ADDR (attr));
21425 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21426 }
c906108c 21427 break;
4ac36638 21428 case DW_FORM_string:
93b5768b 21429 case DW_FORM_strp:
cf532bd1 21430 case DW_FORM_strx:
3019eac3 21431 case DW_FORM_GNU_str_index:
36586728 21432 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21433 /* DW_STRING is already allocated on the objfile obstack, point
21434 directly to it. */
d521ce57 21435 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21436 break;
c906108c
SS
21437 case DW_FORM_block1:
21438 case DW_FORM_block2:
21439 case DW_FORM_block4:
21440 case DW_FORM_block:
2dc7f7b3 21441 case DW_FORM_exprloc:
0224619f 21442 case DW_FORM_data16:
c906108c 21443 blk = DW_BLOCK (attr);
98bfdba5
PA
21444 if (TYPE_LENGTH (type) != blk->size)
21445 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21446 TYPE_LENGTH (type));
21447 *bytes = blk->data;
c906108c 21448 break;
2df3850c
JM
21449
21450 /* The DW_AT_const_value attributes are supposed to carry the
21451 symbol's value "represented as it would be on the target
21452 architecture." By the time we get here, it's already been
21453 converted to host endianness, so we just need to sign- or
21454 zero-extend it as appropriate. */
21455 case DW_FORM_data1:
3aef2284 21456 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21457 break;
c906108c 21458 case DW_FORM_data2:
3aef2284 21459 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21460 break;
c906108c 21461 case DW_FORM_data4:
3aef2284 21462 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21463 break;
c906108c 21464 case DW_FORM_data8:
3aef2284 21465 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21466 break;
21467
c906108c 21468 case DW_FORM_sdata:
663c44ac 21469 case DW_FORM_implicit_const:
98bfdba5 21470 *value = DW_SND (attr);
2df3850c
JM
21471 break;
21472
c906108c 21473 case DW_FORM_udata:
98bfdba5 21474 *value = DW_UNSND (attr);
c906108c 21475 break;
2df3850c 21476
c906108c 21477 default:
b98664d3 21478 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21479 dwarf_form_name (attr->form));
98bfdba5 21480 *value = 0;
c906108c
SS
21481 break;
21482 }
21483}
21484
2df3850c 21485
98bfdba5
PA
21486/* Copy constant value from an attribute to a symbol. */
21487
2df3850c 21488static void
ff39bb5e 21489dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21490 struct dwarf2_cu *cu)
2df3850c 21491{
518817b3 21492 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21493 LONGEST value;
d521ce57 21494 const gdb_byte *bytes;
98bfdba5 21495 struct dwarf2_locexpr_baton *baton;
2df3850c 21496
98bfdba5 21497 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21498 sym->print_name (),
98bfdba5
PA
21499 &objfile->objfile_obstack, cu,
21500 &value, &bytes, &baton);
2df3850c 21501
98bfdba5
PA
21502 if (baton != NULL)
21503 {
98bfdba5 21504 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21505 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21506 }
21507 else if (bytes != NULL)
21508 {
21509 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21510 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21511 }
21512 else
21513 {
21514 SYMBOL_VALUE (sym) = value;
f1e6e072 21515 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21516 }
2df3850c
JM
21517}
21518
c906108c
SS
21519/* Return the type of the die in question using its DW_AT_type attribute. */
21520
21521static struct type *
e7c27a73 21522die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21523{
c906108c 21524 struct attribute *type_attr;
c906108c 21525
e142c38c 21526 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21527 if (!type_attr)
21528 {
518817b3 21529 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21530 /* A missing DW_AT_type represents a void type. */
518817b3 21531 return objfile_type (objfile)->builtin_void;
c906108c 21532 }
348e048f 21533
673bfd45 21534 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21535}
21536
b4ba55a1
JB
21537/* True iff CU's producer generates GNAT Ada auxiliary information
21538 that allows to find parallel types through that information instead
21539 of having to do expensive parallel lookups by type name. */
21540
21541static int
21542need_gnat_info (struct dwarf2_cu *cu)
21543{
de4cb04a
JB
21544 /* Assume that the Ada compiler was GNAT, which always produces
21545 the auxiliary information. */
21546 return (cu->language == language_ada);
b4ba55a1
JB
21547}
21548
b4ba55a1
JB
21549/* Return the auxiliary type of the die in question using its
21550 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21551 attribute is not present. */
21552
21553static struct type *
21554die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21555{
b4ba55a1 21556 struct attribute *type_attr;
b4ba55a1
JB
21557
21558 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21559 if (!type_attr)
21560 return NULL;
21561
673bfd45 21562 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21563}
21564
21565/* If DIE has a descriptive_type attribute, then set the TYPE's
21566 descriptive type accordingly. */
21567
21568static void
21569set_descriptive_type (struct type *type, struct die_info *die,
21570 struct dwarf2_cu *cu)
21571{
21572 struct type *descriptive_type = die_descriptive_type (die, cu);
21573
21574 if (descriptive_type)
21575 {
21576 ALLOCATE_GNAT_AUX_TYPE (type);
21577 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21578 }
21579}
21580
c906108c
SS
21581/* Return the containing type of the die in question using its
21582 DW_AT_containing_type attribute. */
21583
21584static struct type *
e7c27a73 21585die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21586{
c906108c 21587 struct attribute *type_attr;
518817b3 21588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21589
e142c38c 21590 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21591 if (!type_attr)
21592 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21593 "[in module %s]"), objfile_name (objfile));
33ac96f0 21594
673bfd45 21595 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21596}
21597
ac9ec31b
DE
21598/* Return an error marker type to use for the ill formed type in DIE/CU. */
21599
21600static struct type *
21601build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21602{
518817b3
SM
21603 struct dwarf2_per_objfile *dwarf2_per_objfile
21604 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21605 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21606 char *saved;
ac9ec31b 21607
528e1572
SM
21608 std::string message
21609 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21610 objfile_name (objfile),
21611 sect_offset_str (cu->header.sect_off),
21612 sect_offset_str (die->sect_off));
efba19b0 21613 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21614
19f392bc 21615 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21616}
21617
673bfd45 21618/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21619 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21620 DW_AT_containing_type.
673bfd45
DE
21621 If there is no type substitute an error marker. */
21622
c906108c 21623static struct type *
ff39bb5e 21624lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21625 struct dwarf2_cu *cu)
c906108c 21626{
518817b3
SM
21627 struct dwarf2_per_objfile *dwarf2_per_objfile
21628 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21629 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21630 struct type *this_type;
21631
ac9ec31b
DE
21632 gdb_assert (attr->name == DW_AT_type
21633 || attr->name == DW_AT_GNAT_descriptive_type
21634 || attr->name == DW_AT_containing_type);
21635
673bfd45
DE
21636 /* First see if we have it cached. */
21637
36586728
TT
21638 if (attr->form == DW_FORM_GNU_ref_alt)
21639 {
21640 struct dwarf2_per_cu_data *per_cu;
9c541725 21641 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21642
ed2dc618
SM
21643 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21644 dwarf2_per_objfile);
9c541725 21645 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21646 }
cd6c91b4 21647 else if (attr->form_is_ref ())
673bfd45 21648 {
9c541725 21649 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21650
9c541725 21651 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21652 }
55f1336d 21653 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21654 {
ac9ec31b 21655 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21656
ac9ec31b 21657 return get_signatured_type (die, signature, cu);
673bfd45
DE
21658 }
21659 else
21660 {
b98664d3 21661 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21662 " at %s [in module %s]"),
21663 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21664 objfile_name (objfile));
ac9ec31b 21665 return build_error_marker_type (cu, die);
673bfd45
DE
21666 }
21667
21668 /* If not cached we need to read it in. */
21669
21670 if (this_type == NULL)
21671 {
ac9ec31b 21672 struct die_info *type_die = NULL;
673bfd45
DE
21673 struct dwarf2_cu *type_cu = cu;
21674
cd6c91b4 21675 if (attr->form_is_ref ())
ac9ec31b
DE
21676 type_die = follow_die_ref (die, attr, &type_cu);
21677 if (type_die == NULL)
21678 return build_error_marker_type (cu, die);
21679 /* If we find the type now, it's probably because the type came
3019eac3
DE
21680 from an inter-CU reference and the type's CU got expanded before
21681 ours. */
ac9ec31b 21682 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21683 }
21684
21685 /* If we still don't have a type use an error marker. */
21686
21687 if (this_type == NULL)
ac9ec31b 21688 return build_error_marker_type (cu, die);
673bfd45 21689
f792889a 21690 return this_type;
c906108c
SS
21691}
21692
673bfd45
DE
21693/* Return the type in DIE, CU.
21694 Returns NULL for invalid types.
21695
02142a6c 21696 This first does a lookup in die_type_hash,
673bfd45
DE
21697 and only reads the die in if necessary.
21698
21699 NOTE: This can be called when reading in partial or full symbols. */
21700
f792889a 21701static struct type *
e7c27a73 21702read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21703{
f792889a
DJ
21704 struct type *this_type;
21705
21706 this_type = get_die_type (die, cu);
21707 if (this_type)
21708 return this_type;
21709
673bfd45
DE
21710 return read_type_die_1 (die, cu);
21711}
21712
21713/* Read the type in DIE, CU.
21714 Returns NULL for invalid types. */
21715
21716static struct type *
21717read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21718{
21719 struct type *this_type = NULL;
21720
c906108c
SS
21721 switch (die->tag)
21722 {
21723 case DW_TAG_class_type:
680b30c7 21724 case DW_TAG_interface_type:
c906108c
SS
21725 case DW_TAG_structure_type:
21726 case DW_TAG_union_type:
f792889a 21727 this_type = read_structure_type (die, cu);
c906108c
SS
21728 break;
21729 case DW_TAG_enumeration_type:
f792889a 21730 this_type = read_enumeration_type (die, cu);
c906108c
SS
21731 break;
21732 case DW_TAG_subprogram:
21733 case DW_TAG_subroutine_type:
edb3359d 21734 case DW_TAG_inlined_subroutine:
f792889a 21735 this_type = read_subroutine_type (die, cu);
c906108c
SS
21736 break;
21737 case DW_TAG_array_type:
f792889a 21738 this_type = read_array_type (die, cu);
c906108c 21739 break;
72019c9c 21740 case DW_TAG_set_type:
f792889a 21741 this_type = read_set_type (die, cu);
72019c9c 21742 break;
c906108c 21743 case DW_TAG_pointer_type:
f792889a 21744 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21745 break;
21746 case DW_TAG_ptr_to_member_type:
f792889a 21747 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21748 break;
21749 case DW_TAG_reference_type:
4297a3f0
AV
21750 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21751 break;
21752 case DW_TAG_rvalue_reference_type:
21753 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21754 break;
21755 case DW_TAG_const_type:
f792889a 21756 this_type = read_tag_const_type (die, cu);
c906108c
SS
21757 break;
21758 case DW_TAG_volatile_type:
f792889a 21759 this_type = read_tag_volatile_type (die, cu);
c906108c 21760 break;
06d66ee9
TT
21761 case DW_TAG_restrict_type:
21762 this_type = read_tag_restrict_type (die, cu);
21763 break;
c906108c 21764 case DW_TAG_string_type:
f792889a 21765 this_type = read_tag_string_type (die, cu);
c906108c
SS
21766 break;
21767 case DW_TAG_typedef:
f792889a 21768 this_type = read_typedef (die, cu);
c906108c 21769 break;
a02abb62 21770 case DW_TAG_subrange_type:
f792889a 21771 this_type = read_subrange_type (die, cu);
a02abb62 21772 break;
c906108c 21773 case DW_TAG_base_type:
f792889a 21774 this_type = read_base_type (die, cu);
c906108c 21775 break;
81a17f79 21776 case DW_TAG_unspecified_type:
f792889a 21777 this_type = read_unspecified_type (die, cu);
81a17f79 21778 break;
0114d602
DJ
21779 case DW_TAG_namespace:
21780 this_type = read_namespace_type (die, cu);
21781 break;
f55ee35c
JK
21782 case DW_TAG_module:
21783 this_type = read_module_type (die, cu);
21784 break;
a2c2acaf
MW
21785 case DW_TAG_atomic_type:
21786 this_type = read_tag_atomic_type (die, cu);
21787 break;
c906108c 21788 default:
b98664d3 21789 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21790 dwarf_tag_name (die->tag));
c906108c
SS
21791 break;
21792 }
63d06c5c 21793
f792889a 21794 return this_type;
63d06c5c
DC
21795}
21796
abc72ce4
DE
21797/* See if we can figure out if the class lives in a namespace. We do
21798 this by looking for a member function; its demangled name will
21799 contain namespace info, if there is any.
21800 Return the computed name or NULL.
21801 Space for the result is allocated on the objfile's obstack.
21802 This is the full-die version of guess_partial_die_structure_name.
21803 In this case we know DIE has no useful parent. */
21804
43816ebc 21805static const char *
abc72ce4
DE
21806guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21807{
21808 struct die_info *spec_die;
21809 struct dwarf2_cu *spec_cu;
21810 struct die_info *child;
518817b3 21811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21812
21813 spec_cu = cu;
21814 spec_die = die_specification (die, &spec_cu);
21815 if (spec_die != NULL)
21816 {
21817 die = spec_die;
21818 cu = spec_cu;
21819 }
21820
21821 for (child = die->child;
21822 child != NULL;
21823 child = child->sibling)
21824 {
21825 if (child->tag == DW_TAG_subprogram)
21826 {
73b9be8b 21827 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21828
7d45c7c3 21829 if (linkage_name != NULL)
abc72ce4 21830 {
43816ebc
TT
21831 gdb::unique_xmalloc_ptr<char> actual_name
21832 (language_class_name_from_physname (cu->language_defn,
21833 linkage_name));
21834 const char *name = NULL;
abc72ce4
DE
21835
21836 if (actual_name != NULL)
21837 {
15d034d0 21838 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21839
21840 if (die_name != NULL
43816ebc 21841 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21842 {
21843 /* Strip off the class name from the full name.
21844 We want the prefix. */
21845 int die_name_len = strlen (die_name);
43816ebc
TT
21846 int actual_name_len = strlen (actual_name.get ());
21847 const char *ptr = actual_name.get ();
abc72ce4
DE
21848
21849 /* Test for '::' as a sanity check. */
21850 if (actual_name_len > die_name_len + 2
43816ebc 21851 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21852 name = obstack_strndup (
e3b94546 21853 &objfile->per_bfd->storage_obstack,
43816ebc 21854 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21855 }
21856 }
abc72ce4
DE
21857 return name;
21858 }
21859 }
21860 }
21861
21862 return NULL;
21863}
21864
96408a79
SA
21865/* GCC might emit a nameless typedef that has a linkage name. Determine the
21866 prefix part in such case. See
21867 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21868
a121b7c1 21869static const char *
96408a79
SA
21870anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21871{
21872 struct attribute *attr;
e6a959d6 21873 const char *base;
96408a79
SA
21874
21875 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21876 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21877 return NULL;
21878
7d45c7c3 21879 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21880 return NULL;
21881
73b9be8b 21882 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21883 if (attr == NULL || DW_STRING (attr) == NULL)
21884 return NULL;
21885
21886 /* dwarf2_name had to be already called. */
21887 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21888
21889 /* Strip the base name, keep any leading namespaces/classes. */
21890 base = strrchr (DW_STRING (attr), ':');
21891 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21892 return "";
21893
518817b3 21894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21895 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21896 DW_STRING (attr),
21897 &base[-1] - DW_STRING (attr));
96408a79
SA
21898}
21899
fdde2d81 21900/* Return the name of the namespace/class that DIE is defined within,
0114d602 21901 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21902
0114d602
DJ
21903 For example, if we're within the method foo() in the following
21904 code:
21905
21906 namespace N {
21907 class C {
21908 void foo () {
21909 }
21910 };
21911 }
21912
21913 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21914
0d5cff50 21915static const char *
e142c38c 21916determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21917{
518817b3
SM
21918 struct dwarf2_per_objfile *dwarf2_per_objfile
21919 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21920 struct die_info *parent, *spec_die;
21921 struct dwarf2_cu *spec_cu;
21922 struct type *parent_type;
a121b7c1 21923 const char *retval;
63d06c5c 21924
9c37b5ae 21925 if (cu->language != language_cplus
c44af4eb
TT
21926 && cu->language != language_fortran && cu->language != language_d
21927 && cu->language != language_rust)
0114d602
DJ
21928 return "";
21929
96408a79
SA
21930 retval = anonymous_struct_prefix (die, cu);
21931 if (retval)
21932 return retval;
21933
0114d602
DJ
21934 /* We have to be careful in the presence of DW_AT_specification.
21935 For example, with GCC 3.4, given the code
21936
21937 namespace N {
21938 void foo() {
21939 // Definition of N::foo.
21940 }
21941 }
21942
21943 then we'll have a tree of DIEs like this:
21944
21945 1: DW_TAG_compile_unit
21946 2: DW_TAG_namespace // N
21947 3: DW_TAG_subprogram // declaration of N::foo
21948 4: DW_TAG_subprogram // definition of N::foo
21949 DW_AT_specification // refers to die #3
21950
21951 Thus, when processing die #4, we have to pretend that we're in
21952 the context of its DW_AT_specification, namely the contex of die
21953 #3. */
21954 spec_cu = cu;
21955 spec_die = die_specification (die, &spec_cu);
21956 if (spec_die == NULL)
21957 parent = die->parent;
21958 else
63d06c5c 21959 {
0114d602
DJ
21960 parent = spec_die->parent;
21961 cu = spec_cu;
63d06c5c 21962 }
0114d602
DJ
21963
21964 if (parent == NULL)
21965 return "";
98bfdba5
PA
21966 else if (parent->building_fullname)
21967 {
21968 const char *name;
21969 const char *parent_name;
21970
21971 /* It has been seen on RealView 2.2 built binaries,
21972 DW_TAG_template_type_param types actually _defined_ as
21973 children of the parent class:
21974
21975 enum E {};
21976 template class <class Enum> Class{};
21977 Class<enum E> class_e;
21978
21979 1: DW_TAG_class_type (Class)
21980 2: DW_TAG_enumeration_type (E)
21981 3: DW_TAG_enumerator (enum1:0)
21982 3: DW_TAG_enumerator (enum2:1)
21983 ...
21984 2: DW_TAG_template_type_param
21985 DW_AT_type DW_FORM_ref_udata (E)
21986
21987 Besides being broken debug info, it can put GDB into an
21988 infinite loop. Consider:
21989
21990 When we're building the full name for Class<E>, we'll start
21991 at Class, and go look over its template type parameters,
21992 finding E. We'll then try to build the full name of E, and
21993 reach here. We're now trying to build the full name of E,
21994 and look over the parent DIE for containing scope. In the
21995 broken case, if we followed the parent DIE of E, we'd again
21996 find Class, and once again go look at its template type
21997 arguments, etc., etc. Simply don't consider such parent die
21998 as source-level parent of this die (it can't be, the language
21999 doesn't allow it), and break the loop here. */
22000 name = dwarf2_name (die, cu);
22001 parent_name = dwarf2_name (parent, cu);
b98664d3 22002 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22003 name ? name : "<unknown>",
22004 parent_name ? parent_name : "<unknown>");
22005 return "";
22006 }
63d06c5c 22007 else
0114d602
DJ
22008 switch (parent->tag)
22009 {
63d06c5c 22010 case DW_TAG_namespace:
0114d602 22011 parent_type = read_type_die (parent, cu);
acebe513
UW
22012 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22013 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22014 Work around this problem here. */
22015 if (cu->language == language_cplus
e86ca25f 22016 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22017 return "";
0114d602 22018 /* We give a name to even anonymous namespaces. */
e86ca25f 22019 return TYPE_NAME (parent_type);
63d06c5c 22020 case DW_TAG_class_type:
680b30c7 22021 case DW_TAG_interface_type:
63d06c5c 22022 case DW_TAG_structure_type:
0114d602 22023 case DW_TAG_union_type:
f55ee35c 22024 case DW_TAG_module:
0114d602 22025 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22026 if (TYPE_NAME (parent_type) != NULL)
22027 return TYPE_NAME (parent_type);
0114d602
DJ
22028 else
22029 /* An anonymous structure is only allowed non-static data
22030 members; no typedefs, no member functions, et cetera.
22031 So it does not need a prefix. */
22032 return "";
abc72ce4 22033 case DW_TAG_compile_unit:
95554aad 22034 case DW_TAG_partial_unit:
abc72ce4
DE
22035 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22036 if (cu->language == language_cplus
fd5866f6 22037 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22038 && die->child != NULL
22039 && (die->tag == DW_TAG_class_type
22040 || die->tag == DW_TAG_structure_type
22041 || die->tag == DW_TAG_union_type))
22042 {
43816ebc 22043 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22044 if (name != NULL)
22045 return name;
22046 }
22047 return "";
0a4b0913
AB
22048 case DW_TAG_subprogram:
22049 /* Nested subroutines in Fortran get a prefix with the name
22050 of the parent's subroutine. */
22051 if (cu->language == language_fortran)
22052 {
22053 if ((die->tag == DW_TAG_subprogram)
22054 && (dwarf2_name (parent, cu) != NULL))
22055 return dwarf2_name (parent, cu);
22056 }
22057 return determine_prefix (parent, cu);
3d567982
TT
22058 case DW_TAG_enumeration_type:
22059 parent_type = read_type_die (parent, cu);
22060 if (TYPE_DECLARED_CLASS (parent_type))
22061 {
e86ca25f
TT
22062 if (TYPE_NAME (parent_type) != NULL)
22063 return TYPE_NAME (parent_type);
3d567982
TT
22064 return "";
22065 }
22066 /* Fall through. */
63d06c5c 22067 default:
8176b9b8 22068 return determine_prefix (parent, cu);
63d06c5c 22069 }
63d06c5c
DC
22070}
22071
3e43a32a
MS
22072/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22073 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22074 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22075 an obconcat, otherwise allocate storage for the result. The CU argument is
22076 used to determine the language and hence, the appropriate separator. */
987504bb 22077
f55ee35c 22078#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22079
22080static char *
f55ee35c
JK
22081typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22082 int physname, struct dwarf2_cu *cu)
63d06c5c 22083{
f55ee35c 22084 const char *lead = "";
5c315b68 22085 const char *sep;
63d06c5c 22086
3e43a32a
MS
22087 if (suffix == NULL || suffix[0] == '\0'
22088 || prefix == NULL || prefix[0] == '\0')
987504bb 22089 sep = "";
45280282
IB
22090 else if (cu->language == language_d)
22091 {
22092 /* For D, the 'main' function could be defined in any module, but it
22093 should never be prefixed. */
22094 if (strcmp (suffix, "D main") == 0)
22095 {
22096 prefix = "";
22097 sep = "";
22098 }
22099 else
22100 sep = ".";
22101 }
f55ee35c
JK
22102 else if (cu->language == language_fortran && physname)
22103 {
22104 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22105 DW_AT_MIPS_linkage_name is preferred and used instead. */
22106
22107 lead = "__";
22108 sep = "_MOD_";
22109 }
987504bb
JJ
22110 else
22111 sep = "::";
63d06c5c 22112
6dd47d34
DE
22113 if (prefix == NULL)
22114 prefix = "";
22115 if (suffix == NULL)
22116 suffix = "";
22117
987504bb
JJ
22118 if (obs == NULL)
22119 {
3e43a32a 22120 char *retval
224c3ddb
SM
22121 = ((char *)
22122 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22123
f55ee35c
JK
22124 strcpy (retval, lead);
22125 strcat (retval, prefix);
6dd47d34
DE
22126 strcat (retval, sep);
22127 strcat (retval, suffix);
63d06c5c
DC
22128 return retval;
22129 }
987504bb
JJ
22130 else
22131 {
22132 /* We have an obstack. */
f55ee35c 22133 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22134 }
63d06c5c
DC
22135}
22136
c906108c
SS
22137/* Return sibling of die, NULL if no sibling. */
22138
f9aca02d 22139static struct die_info *
fba45db2 22140sibling_die (struct die_info *die)
c906108c 22141{
639d11d3 22142 return die->sibling;
c906108c
SS
22143}
22144
71c25dea
TT
22145/* Get name of a die, return NULL if not found. */
22146
15d034d0
TT
22147static const char *
22148dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22149 struct obstack *obstack)
22150{
22151 if (name && cu->language == language_cplus)
22152 {
2f408ecb 22153 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22154
2f408ecb 22155 if (!canon_name.empty ())
71c25dea 22156 {
2f408ecb 22157 if (canon_name != name)
efba19b0 22158 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22159 }
22160 }
22161
22162 return name;
c906108c
SS
22163}
22164
96553a0c
DE
22165/* Get name of a die, return NULL if not found.
22166 Anonymous namespaces are converted to their magic string. */
9219021c 22167
15d034d0 22168static const char *
e142c38c 22169dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22170{
22171 struct attribute *attr;
518817b3 22172 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22173
e142c38c 22174 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22175 if ((!attr || !DW_STRING (attr))
96553a0c 22176 && die->tag != DW_TAG_namespace
53832f31
TT
22177 && die->tag != DW_TAG_class_type
22178 && die->tag != DW_TAG_interface_type
22179 && die->tag != DW_TAG_structure_type
22180 && die->tag != DW_TAG_union_type)
71c25dea
TT
22181 return NULL;
22182
22183 switch (die->tag)
22184 {
22185 case DW_TAG_compile_unit:
95554aad 22186 case DW_TAG_partial_unit:
71c25dea
TT
22187 /* Compilation units have a DW_AT_name that is a filename, not
22188 a source language identifier. */
22189 case DW_TAG_enumeration_type:
22190 case DW_TAG_enumerator:
22191 /* These tags always have simple identifiers already; no need
22192 to canonicalize them. */
22193 return DW_STRING (attr);
907af001 22194
96553a0c
DE
22195 case DW_TAG_namespace:
22196 if (attr != NULL && DW_STRING (attr) != NULL)
22197 return DW_STRING (attr);
22198 return CP_ANONYMOUS_NAMESPACE_STR;
22199
907af001
UW
22200 case DW_TAG_class_type:
22201 case DW_TAG_interface_type:
22202 case DW_TAG_structure_type:
22203 case DW_TAG_union_type:
22204 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22205 structures or unions. These were of the form "._%d" in GCC 4.1,
22206 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22207 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22208 if (attr && DW_STRING (attr)
61012eef
GB
22209 && (startswith (DW_STRING (attr), "._")
22210 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22211 return NULL;
53832f31
TT
22212
22213 /* GCC might emit a nameless typedef that has a linkage name. See
22214 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22215 if (!attr || DW_STRING (attr) == NULL)
22216 {
73b9be8b 22217 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22218 if (attr == NULL || DW_STRING (attr) == NULL)
22219 return NULL;
22220
df5c6c50
JK
22221 /* Avoid demangling DW_STRING (attr) the second time on a second
22222 call for the same DIE. */
22223 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 22224 {
43816ebc
TT
22225 gdb::unique_xmalloc_ptr<char> demangled
22226 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
22227
e6a959d6 22228 const char *base;
96408a79 22229
53832f31 22230 /* FIXME: we already did this for the partial symbol... */
34a68019 22231 DW_STRING (attr)
021887d8 22232 = obstack_strdup (&objfile->per_bfd->storage_obstack,
43816ebc 22233 demangled.get ());
53832f31 22234 DW_STRING_IS_CANONICAL (attr) = 1;
96408a79
SA
22235
22236 /* Strip any leading namespaces/classes, keep only the base name.
22237 DW_AT_name for named DIEs does not contain the prefixes. */
22238 base = strrchr (DW_STRING (attr), ':');
22239 if (base && base > DW_STRING (attr) && base[-1] == ':')
22240 return &base[1];
22241 else
22242 return DW_STRING (attr);
53832f31
TT
22243 }
22244 }
907af001
UW
22245 break;
22246
71c25dea 22247 default:
907af001
UW
22248 break;
22249 }
22250
22251 if (!DW_STRING_IS_CANONICAL (attr))
22252 {
22253 DW_STRING (attr)
22254 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22255 &objfile->per_bfd->storage_obstack);
907af001 22256 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22257 }
907af001 22258 return DW_STRING (attr);
9219021c
DC
22259}
22260
22261/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22262 is none. *EXT_CU is the CU containing DIE on input, and the CU
22263 containing the return value on output. */
9219021c
DC
22264
22265static struct die_info *
f2f0e013 22266dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22267{
22268 struct attribute *attr;
9219021c 22269
f2f0e013 22270 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22271 if (attr == NULL)
22272 return NULL;
22273
f2f0e013 22274 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22275}
22276
fa9c3fa0
TT
22277/* A convenience function that returns an "unknown" DWARF name,
22278 including the value of V. STR is the name of the entity being
22279 printed, e.g., "TAG". */
22280
22281static const char *
22282dwarf_unknown (const char *str, unsigned v)
22283{
22284 char *cell = get_print_cell ();
22285 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22286 return cell;
22287}
22288
c906108c
SS
22289/* Convert a DIE tag into its string name. */
22290
f39c6ffd 22291static const char *
aa1ee363 22292dwarf_tag_name (unsigned tag)
c906108c 22293{
f39c6ffd
TT
22294 const char *name = get_DW_TAG_name (tag);
22295
22296 if (name == NULL)
fa9c3fa0 22297 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22298
22299 return name;
c906108c
SS
22300}
22301
22302/* Convert a DWARF attribute code into its string name. */
22303
f39c6ffd 22304static const char *
aa1ee363 22305dwarf_attr_name (unsigned attr)
c906108c 22306{
f39c6ffd
TT
22307 const char *name;
22308
c764a876 22309#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22310 if (attr == DW_AT_MIPS_fde)
22311 return "DW_AT_MIPS_fde";
22312#else
22313 if (attr == DW_AT_HP_block_index)
22314 return "DW_AT_HP_block_index";
c764a876 22315#endif
f39c6ffd
TT
22316
22317 name = get_DW_AT_name (attr);
22318
22319 if (name == NULL)
fa9c3fa0 22320 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22321
22322 return name;
c906108c
SS
22323}
22324
a084a2a6
AT
22325/* Convert a unit type to corresponding DW_UT name. */
22326
22327static const char *
22328dwarf_unit_type_name (int unit_type) {
22329 switch (unit_type)
22330 {
22331 case 0x01:
22332 return "DW_UT_compile (0x01)";
22333 case 0x02:
22334 return "DW_UT_type (0x02)";
22335 case 0x03:
22336 return "DW_UT_partial (0x03)";
22337 case 0x04:
22338 return "DW_UT_skeleton (0x04)";
22339 case 0x05:
22340 return "DW_UT_split_compile (0x05)";
22341 case 0x06:
22342 return "DW_UT_split_type (0x06)";
22343 case 0x80:
22344 return "DW_UT_lo_user (0x80)";
22345 case 0xff:
22346 return "DW_UT_hi_user (0xff)";
22347 default:
22348 return nullptr;
22349 }
22350}
22351
c906108c
SS
22352/* Convert a DWARF value form code into its string name. */
22353
f39c6ffd 22354static const char *
aa1ee363 22355dwarf_form_name (unsigned form)
c906108c 22356{
f39c6ffd
TT
22357 const char *name = get_DW_FORM_name (form);
22358
22359 if (name == NULL)
fa9c3fa0 22360 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22361
22362 return name;
c906108c
SS
22363}
22364
a121b7c1 22365static const char *
fba45db2 22366dwarf_bool_name (unsigned mybool)
c906108c
SS
22367{
22368 if (mybool)
22369 return "TRUE";
22370 else
22371 return "FALSE";
22372}
22373
22374/* Convert a DWARF type code into its string name. */
22375
f39c6ffd 22376static const char *
aa1ee363 22377dwarf_type_encoding_name (unsigned enc)
c906108c 22378{
f39c6ffd 22379 const char *name = get_DW_ATE_name (enc);
c906108c 22380
f39c6ffd 22381 if (name == NULL)
fa9c3fa0 22382 return dwarf_unknown ("ATE", enc);
c906108c 22383
f39c6ffd 22384 return name;
c906108c 22385}
c906108c 22386
f9aca02d 22387static void
d97bc12b 22388dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22389{
22390 unsigned int i;
22391
d97bc12b 22392 print_spaces (indent, f);
9d8780f0 22393 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22394 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22395 sect_offset_str (die->sect_off));
d97bc12b
DE
22396
22397 if (die->parent != NULL)
22398 {
22399 print_spaces (indent, f);
9d8780f0
SM
22400 fprintf_unfiltered (f, " parent at offset: %s\n",
22401 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22402 }
22403
22404 print_spaces (indent, f);
22405 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22406 dwarf_bool_name (die->child != NULL));
c906108c 22407
d97bc12b
DE
22408 print_spaces (indent, f);
22409 fprintf_unfiltered (f, " attributes:\n");
22410
c906108c
SS
22411 for (i = 0; i < die->num_attrs; ++i)
22412 {
d97bc12b
DE
22413 print_spaces (indent, f);
22414 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22415 dwarf_attr_name (die->attrs[i].name),
22416 dwarf_form_name (die->attrs[i].form));
d97bc12b 22417
c906108c
SS
22418 switch (die->attrs[i].form)
22419 {
c906108c 22420 case DW_FORM_addr:
336d760d 22421 case DW_FORM_addrx:
3019eac3 22422 case DW_FORM_GNU_addr_index:
d97bc12b 22423 fprintf_unfiltered (f, "address: ");
5af949e3 22424 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22425 break;
22426 case DW_FORM_block2:
22427 case DW_FORM_block4:
22428 case DW_FORM_block:
22429 case DW_FORM_block1:
56eb65bd
SP
22430 fprintf_unfiltered (f, "block: size %s",
22431 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22432 break;
2dc7f7b3 22433 case DW_FORM_exprloc:
56eb65bd
SP
22434 fprintf_unfiltered (f, "expression: size %s",
22435 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22436 break;
0224619f
JK
22437 case DW_FORM_data16:
22438 fprintf_unfiltered (f, "constant of 16 bytes");
22439 break;
4568ecf9
DE
22440 case DW_FORM_ref_addr:
22441 fprintf_unfiltered (f, "ref address: ");
22442 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22443 break;
36586728
TT
22444 case DW_FORM_GNU_ref_alt:
22445 fprintf_unfiltered (f, "alt ref address: ");
22446 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22447 break;
10b3939b
DJ
22448 case DW_FORM_ref1:
22449 case DW_FORM_ref2:
22450 case DW_FORM_ref4:
4568ecf9
DE
22451 case DW_FORM_ref8:
22452 case DW_FORM_ref_udata:
d97bc12b 22453 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22454 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22455 break;
c906108c
SS
22456 case DW_FORM_data1:
22457 case DW_FORM_data2:
22458 case DW_FORM_data4:
ce5d95e1 22459 case DW_FORM_data8:
c906108c
SS
22460 case DW_FORM_udata:
22461 case DW_FORM_sdata:
43bbcdc2
PH
22462 fprintf_unfiltered (f, "constant: %s",
22463 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22464 break;
2dc7f7b3
TT
22465 case DW_FORM_sec_offset:
22466 fprintf_unfiltered (f, "section offset: %s",
22467 pulongest (DW_UNSND (&die->attrs[i])));
22468 break;
55f1336d 22469 case DW_FORM_ref_sig8:
ac9ec31b
DE
22470 fprintf_unfiltered (f, "signature: %s",
22471 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22472 break;
c906108c 22473 case DW_FORM_string:
4bdf3d34 22474 case DW_FORM_strp:
43988095 22475 case DW_FORM_line_strp:
cf532bd1 22476 case DW_FORM_strx:
3019eac3 22477 case DW_FORM_GNU_str_index:
36586728 22478 case DW_FORM_GNU_strp_alt:
8285870a 22479 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22480 DW_STRING (&die->attrs[i])
8285870a
JK
22481 ? DW_STRING (&die->attrs[i]) : "",
22482 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22483 break;
22484 case DW_FORM_flag:
22485 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22486 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22487 else
d97bc12b 22488 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22489 break;
2dc7f7b3
TT
22490 case DW_FORM_flag_present:
22491 fprintf_unfiltered (f, "flag: TRUE");
22492 break;
a8329558 22493 case DW_FORM_indirect:
0963b4bd
MS
22494 /* The reader will have reduced the indirect form to
22495 the "base form" so this form should not occur. */
5f48f8f3 22496 fprintf_unfiltered (f,
3e43a32a 22497 "unexpected attribute form: DW_FORM_indirect");
a8329558 22498 break;
663c44ac
JK
22499 case DW_FORM_implicit_const:
22500 fprintf_unfiltered (f, "constant: %s",
22501 plongest (DW_SND (&die->attrs[i])));
22502 break;
c906108c 22503 default:
d97bc12b 22504 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22505 die->attrs[i].form);
d97bc12b 22506 break;
c906108c 22507 }
d97bc12b 22508 fprintf_unfiltered (f, "\n");
c906108c
SS
22509 }
22510}
22511
f9aca02d 22512static void
d97bc12b 22513dump_die_for_error (struct die_info *die)
c906108c 22514{
d97bc12b
DE
22515 dump_die_shallow (gdb_stderr, 0, die);
22516}
22517
22518static void
22519dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22520{
22521 int indent = level * 4;
22522
22523 gdb_assert (die != NULL);
22524
22525 if (level >= max_level)
22526 return;
22527
22528 dump_die_shallow (f, indent, die);
22529
22530 if (die->child != NULL)
c906108c 22531 {
d97bc12b
DE
22532 print_spaces (indent, f);
22533 fprintf_unfiltered (f, " Children:");
22534 if (level + 1 < max_level)
22535 {
22536 fprintf_unfiltered (f, "\n");
22537 dump_die_1 (f, level + 1, max_level, die->child);
22538 }
22539 else
22540 {
3e43a32a
MS
22541 fprintf_unfiltered (f,
22542 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22543 }
22544 }
22545
22546 if (die->sibling != NULL && level > 0)
22547 {
22548 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22549 }
22550}
22551
d97bc12b
DE
22552/* This is called from the pdie macro in gdbinit.in.
22553 It's not static so gcc will keep a copy callable from gdb. */
22554
22555void
22556dump_die (struct die_info *die, int max_level)
22557{
22558 dump_die_1 (gdb_stdlog, 0, max_level, die);
22559}
22560
f9aca02d 22561static void
51545339 22562store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22563{
51545339 22564 void **slot;
c906108c 22565
9c541725
PA
22566 slot = htab_find_slot_with_hash (cu->die_hash, die,
22567 to_underlying (die->sect_off),
b64f50a1 22568 INSERT);
51545339
DJ
22569
22570 *slot = die;
c906108c
SS
22571}
22572
b64f50a1
JK
22573/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22574 required kind. */
22575
22576static sect_offset
ff39bb5e 22577dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22578{
cd6c91b4 22579 if (attr->form_is_ref ())
9c541725 22580 return (sect_offset) DW_UNSND (attr);
93311388 22581
b98664d3 22582 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22583 dwarf_form_name (attr->form));
9c541725 22584 return {};
c906108c
SS
22585}
22586
43bbcdc2
PH
22587/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22588 * the value held by the attribute is not constant. */
a02abb62 22589
43bbcdc2 22590static LONGEST
ff39bb5e 22591dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22592{
663c44ac 22593 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22594 return DW_SND (attr);
22595 else if (attr->form == DW_FORM_udata
22596 || attr->form == DW_FORM_data1
22597 || attr->form == DW_FORM_data2
22598 || attr->form == DW_FORM_data4
22599 || attr->form == DW_FORM_data8)
22600 return DW_UNSND (attr);
22601 else
22602 {
cd6c91b4 22603 /* For DW_FORM_data16 see attribute::form_is_constant. */
b98664d3 22604 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22605 dwarf_form_name (attr->form));
22606 return default_value;
22607 }
22608}
22609
348e048f
DE
22610/* Follow reference or signature attribute ATTR of SRC_DIE.
22611 On entry *REF_CU is the CU of SRC_DIE.
22612 On exit *REF_CU is the CU of the result. */
22613
22614static struct die_info *
ff39bb5e 22615follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22616 struct dwarf2_cu **ref_cu)
22617{
22618 struct die_info *die;
22619
cd6c91b4 22620 if (attr->form_is_ref ())
348e048f 22621 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22622 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22623 die = follow_die_sig (src_die, attr, ref_cu);
22624 else
22625 {
22626 dump_die_for_error (src_die);
22627 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22628 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22629 }
22630
22631 return die;
03dd20cc
DJ
22632}
22633
5c631832 22634/* Follow reference OFFSET.
673bfd45
DE
22635 On entry *REF_CU is the CU of the source die referencing OFFSET.
22636 On exit *REF_CU is the CU of the result.
22637 Returns NULL if OFFSET is invalid. */
f504f079 22638
f9aca02d 22639static struct die_info *
9c541725 22640follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22641 struct dwarf2_cu **ref_cu)
c906108c 22642{
10b3939b 22643 struct die_info temp_die;
f2f0e013 22644 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22645 struct dwarf2_per_objfile *dwarf2_per_objfile
22646 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22647
348e048f
DE
22648 gdb_assert (cu->per_cu != NULL);
22649
98bfdba5
PA
22650 target_cu = cu;
22651
3019eac3 22652 if (cu->per_cu->is_debug_types)
348e048f
DE
22653 {
22654 /* .debug_types CUs cannot reference anything outside their CU.
22655 If they need to, they have to reference a signatured type via
55f1336d 22656 DW_FORM_ref_sig8. */
9c541725 22657 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22658 return NULL;
348e048f 22659 }
36586728 22660 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22661 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22662 {
22663 struct dwarf2_per_cu_data *per_cu;
9a619af0 22664
9c541725 22665 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22666 dwarf2_per_objfile);
03dd20cc
DJ
22667
22668 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22669 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22670 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22671
10b3939b
DJ
22672 target_cu = per_cu->cu;
22673 }
98bfdba5
PA
22674 else if (cu->dies == NULL)
22675 {
22676 /* We're loading full DIEs during partial symbol reading. */
22677 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22678 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22679 }
c906108c 22680
f2f0e013 22681 *ref_cu = target_cu;
9c541725 22682 temp_die.sect_off = sect_off;
c24bdb02
KS
22683
22684 if (target_cu != cu)
22685 target_cu->ancestor = cu;
22686
9a3c8263 22687 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22688 &temp_die,
22689 to_underlying (sect_off));
5c631832 22690}
10b3939b 22691
5c631832
JK
22692/* Follow reference attribute ATTR of SRC_DIE.
22693 On entry *REF_CU is the CU of SRC_DIE.
22694 On exit *REF_CU is the CU of the result. */
22695
22696static struct die_info *
ff39bb5e 22697follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22698 struct dwarf2_cu **ref_cu)
22699{
9c541725 22700 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22701 struct dwarf2_cu *cu = *ref_cu;
22702 struct die_info *die;
22703
9c541725 22704 die = follow_die_offset (sect_off,
36586728
TT
22705 (attr->form == DW_FORM_GNU_ref_alt
22706 || cu->per_cu->is_dwz),
22707 ref_cu);
5c631832 22708 if (!die)
9d8780f0
SM
22709 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22710 "at %s [in module %s]"),
22711 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22712 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22713
5c631832
JK
22714 return die;
22715}
22716
9c541725 22717/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22718 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22719 dwarf2_locexpr_baton->data has lifetime of
22720 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22721
22722struct dwarf2_locexpr_baton
9c541725 22723dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22724 struct dwarf2_per_cu_data *per_cu,
22725 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22726 void *baton, bool resolve_abstract_p)
5c631832 22727{
918dd910 22728 struct dwarf2_cu *cu;
5c631832
JK
22729 struct die_info *die;
22730 struct attribute *attr;
22731 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22732 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22733 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22734
918dd910 22735 if (per_cu->cu == NULL)
58f0c718 22736 load_cu (per_cu, false);
918dd910 22737 cu = per_cu->cu;
cc12ce38
DE
22738 if (cu == NULL)
22739 {
22740 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22741 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22742 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22743 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22744 }
918dd910 22745
9c541725 22746 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22747 if (!die)
9d8780f0
SM
22748 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22749 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22750
22751 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22752 if (!attr && resolve_abstract_p
3360b6e7 22753 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22754 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22755 {
22756 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22757 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 22758 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 22759
3360b6e7
TV
22760 for (const auto &cand_off
22761 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22762 {
3360b6e7
TV
22763 struct dwarf2_cu *cand_cu = cu;
22764 struct die_info *cand
22765 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22766 if (!cand
22767 || !cand->parent
e4a62c65
TV
22768 || cand->parent->tag != DW_TAG_subprogram)
22769 continue;
22770
22771 CORE_ADDR pc_low, pc_high;
22772 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22773 if (pc_low == ((CORE_ADDR) -1))
22774 continue;
22775 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22776 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22777 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22778 continue;
22779
22780 die = cand;
22781 attr = dwarf2_attr (die, DW_AT_location, cu);
22782 break;
22783 }
22784 }
22785
5c631832
JK
22786 if (!attr)
22787 {
e103e986
JK
22788 /* DWARF: "If there is no such attribute, then there is no effect.".
22789 DATA is ignored if SIZE is 0. */
5c631832 22790
e103e986 22791 retval.data = NULL;
5c631832
JK
22792 retval.size = 0;
22793 }
cd6c91b4 22794 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22795 {
22796 struct dwarf2_loclist_baton loclist_baton;
22797 CORE_ADDR pc = (*get_frame_pc) (baton);
22798 size_t size;
22799
22800 fill_in_loclist_baton (cu, &loclist_baton, attr);
22801
22802 retval.data = dwarf2_find_location_expression (&loclist_baton,
22803 &size, pc);
22804 retval.size = size;
22805 }
5c631832
JK
22806 else
22807 {
4fc6c0d5 22808 if (!attr->form_is_block ())
9d8780f0 22809 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22810 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22811 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22812
22813 retval.data = DW_BLOCK (attr)->data;
22814 retval.size = DW_BLOCK (attr)->size;
22815 }
22816 retval.per_cu = cu->per_cu;
918dd910 22817
ed2dc618 22818 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22819
5c631832 22820 return retval;
348e048f
DE
22821}
22822
8b9737bf
TT
22823/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22824 offset. */
22825
22826struct dwarf2_locexpr_baton
22827dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22828 struct dwarf2_per_cu_data *per_cu,
22829 CORE_ADDR (*get_frame_pc) (void *baton),
22830 void *baton)
22831{
9c541725 22832 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22833
9c541725 22834 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22835}
22836
b6807d98
TT
22837/* Write a constant of a given type as target-ordered bytes into
22838 OBSTACK. */
22839
22840static const gdb_byte *
22841write_constant_as_bytes (struct obstack *obstack,
22842 enum bfd_endian byte_order,
22843 struct type *type,
22844 ULONGEST value,
22845 LONGEST *len)
22846{
22847 gdb_byte *result;
22848
22849 *len = TYPE_LENGTH (type);
224c3ddb 22850 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22851 store_unsigned_integer (result, *len, byte_order, value);
22852
22853 return result;
22854}
22855
22856/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22857 pointer to the constant bytes and set LEN to the length of the
22858 data. If memory is needed, allocate it on OBSTACK. If the DIE
22859 does not have a DW_AT_const_value, return NULL. */
22860
22861const gdb_byte *
9c541725 22862dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22863 struct dwarf2_per_cu_data *per_cu,
22864 struct obstack *obstack,
22865 LONGEST *len)
22866{
22867 struct dwarf2_cu *cu;
22868 struct die_info *die;
22869 struct attribute *attr;
22870 const gdb_byte *result = NULL;
22871 struct type *type;
22872 LONGEST value;
22873 enum bfd_endian byte_order;
e3b94546 22874 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22875
b6807d98 22876 if (per_cu->cu == NULL)
58f0c718 22877 load_cu (per_cu, false);
b6807d98 22878 cu = per_cu->cu;
cc12ce38
DE
22879 if (cu == NULL)
22880 {
22881 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22882 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22883 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22884 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22885 }
b6807d98 22886
9c541725 22887 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22888 if (!die)
9d8780f0
SM
22889 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22890 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22891
22892 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22893 if (attr == NULL)
22894 return NULL;
22895
e3b94546 22896 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22897 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22898
22899 switch (attr->form)
22900 {
22901 case DW_FORM_addr:
336d760d 22902 case DW_FORM_addrx:
b6807d98
TT
22903 case DW_FORM_GNU_addr_index:
22904 {
22905 gdb_byte *tem;
22906
22907 *len = cu->header.addr_size;
224c3ddb 22908 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22909 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22910 result = tem;
22911 }
22912 break;
22913 case DW_FORM_string:
22914 case DW_FORM_strp:
cf532bd1 22915 case DW_FORM_strx:
b6807d98
TT
22916 case DW_FORM_GNU_str_index:
22917 case DW_FORM_GNU_strp_alt:
22918 /* DW_STRING is already allocated on the objfile obstack, point
22919 directly to it. */
22920 result = (const gdb_byte *) DW_STRING (attr);
22921 *len = strlen (DW_STRING (attr));
22922 break;
22923 case DW_FORM_block1:
22924 case DW_FORM_block2:
22925 case DW_FORM_block4:
22926 case DW_FORM_block:
22927 case DW_FORM_exprloc:
0224619f 22928 case DW_FORM_data16:
b6807d98
TT
22929 result = DW_BLOCK (attr)->data;
22930 *len = DW_BLOCK (attr)->size;
22931 break;
22932
22933 /* The DW_AT_const_value attributes are supposed to carry the
22934 symbol's value "represented as it would be on the target
22935 architecture." By the time we get here, it's already been
22936 converted to host endianness, so we just need to sign- or
22937 zero-extend it as appropriate. */
22938 case DW_FORM_data1:
22939 type = die_type (die, cu);
22940 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22941 if (result == NULL)
22942 result = write_constant_as_bytes (obstack, byte_order,
22943 type, value, len);
22944 break;
22945 case DW_FORM_data2:
22946 type = die_type (die, cu);
22947 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22948 if (result == NULL)
22949 result = write_constant_as_bytes (obstack, byte_order,
22950 type, value, len);
22951 break;
22952 case DW_FORM_data4:
22953 type = die_type (die, cu);
22954 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22955 if (result == NULL)
22956 result = write_constant_as_bytes (obstack, byte_order,
22957 type, value, len);
22958 break;
22959 case DW_FORM_data8:
22960 type = die_type (die, cu);
22961 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22962 if (result == NULL)
22963 result = write_constant_as_bytes (obstack, byte_order,
22964 type, value, len);
22965 break;
22966
22967 case DW_FORM_sdata:
663c44ac 22968 case DW_FORM_implicit_const:
b6807d98
TT
22969 type = die_type (die, cu);
22970 result = write_constant_as_bytes (obstack, byte_order,
22971 type, DW_SND (attr), len);
22972 break;
22973
22974 case DW_FORM_udata:
22975 type = die_type (die, cu);
22976 result = write_constant_as_bytes (obstack, byte_order,
22977 type, DW_UNSND (attr), len);
22978 break;
22979
22980 default:
b98664d3 22981 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22982 dwarf_form_name (attr->form));
22983 break;
22984 }
22985
22986 return result;
22987}
22988
7942e96e
AA
22989/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
22990 valid type for this die is found. */
22991
22992struct type *
9c541725 22993dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
22994 struct dwarf2_per_cu_data *per_cu)
22995{
22996 struct dwarf2_cu *cu;
22997 struct die_info *die;
22998
7942e96e 22999 if (per_cu->cu == NULL)
58f0c718 23000 load_cu (per_cu, false);
7942e96e
AA
23001 cu = per_cu->cu;
23002 if (!cu)
23003 return NULL;
23004
9c541725 23005 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23006 if (!die)
23007 return NULL;
23008
23009 return die_type (die, cu);
23010}
23011
8a9b8146
TT
23012/* Return the type of the DIE at DIE_OFFSET in the CU named by
23013 PER_CU. */
23014
23015struct type *
b64f50a1 23016dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23017 struct dwarf2_per_cu_data *per_cu)
23018{
9c541725 23019 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23020 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23021}
23022
ac9ec31b 23023/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23024 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23025 On exit *REF_CU is the CU of the result.
23026 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23027
23028static struct die_info *
ac9ec31b
DE
23029follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23030 struct dwarf2_cu **ref_cu)
348e048f 23031{
348e048f 23032 struct die_info temp_die;
c24bdb02 23033 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23034 struct die_info *die;
23035
ac9ec31b
DE
23036 /* While it might be nice to assert sig_type->type == NULL here,
23037 we can get here for DW_AT_imported_declaration where we need
23038 the DIE not the type. */
348e048f
DE
23039
23040 /* If necessary, add it to the queue and load its DIEs. */
23041
95554aad 23042 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23043 read_signatured_type (sig_type);
348e048f 23044
348e048f 23045 sig_cu = sig_type->per_cu.cu;
69d751e3 23046 gdb_assert (sig_cu != NULL);
9c541725
PA
23047 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23048 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23049 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23050 to_underlying (temp_die.sect_off));
348e048f
DE
23051 if (die)
23052 {
ed2dc618 23053 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23054 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23055
796a7ff8
DE
23056 /* For .gdb_index version 7 keep track of included TUs.
23057 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23058 if (dwarf2_per_objfile->index_table != NULL
23059 && dwarf2_per_objfile->index_table->version <= 7)
23060 {
ae640021 23061 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23062 }
23063
348e048f 23064 *ref_cu = sig_cu;
c24bdb02
KS
23065 if (sig_cu != cu)
23066 sig_cu->ancestor = cu;
23067
348e048f
DE
23068 return die;
23069 }
23070
ac9ec31b
DE
23071 return NULL;
23072}
23073
23074/* Follow signatured type referenced by ATTR in SRC_DIE.
23075 On entry *REF_CU is the CU of SRC_DIE.
23076 On exit *REF_CU is the CU of the result.
23077 The result is the DIE of the type.
23078 If the referenced type cannot be found an error is thrown. */
23079
23080static struct die_info *
ff39bb5e 23081follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23082 struct dwarf2_cu **ref_cu)
23083{
23084 ULONGEST signature = DW_SIGNATURE (attr);
23085 struct signatured_type *sig_type;
23086 struct die_info *die;
23087
23088 gdb_assert (attr->form == DW_FORM_ref_sig8);
23089
a2ce51a0 23090 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23091 /* sig_type will be NULL if the signatured type is missing from
23092 the debug info. */
23093 if (sig_type == NULL)
23094 {
23095 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23096 " from DIE at %s [in module %s]"),
23097 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23098 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23099 }
23100
23101 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23102 if (die == NULL)
23103 {
23104 dump_die_for_error (src_die);
23105 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23106 " from DIE at %s [in module %s]"),
23107 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23108 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23109 }
23110
23111 return die;
23112}
23113
23114/* Get the type specified by SIGNATURE referenced in DIE/CU,
23115 reading in and processing the type unit if necessary. */
23116
23117static struct type *
23118get_signatured_type (struct die_info *die, ULONGEST signature,
23119 struct dwarf2_cu *cu)
23120{
518817b3
SM
23121 struct dwarf2_per_objfile *dwarf2_per_objfile
23122 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23123 struct signatured_type *sig_type;
23124 struct dwarf2_cu *type_cu;
23125 struct die_info *type_die;
23126 struct type *type;
23127
a2ce51a0 23128 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23129 /* sig_type will be NULL if the signatured type is missing from
23130 the debug info. */
23131 if (sig_type == NULL)
23132 {
b98664d3 23133 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23134 " from DIE at %s [in module %s]"),
23135 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23136 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23137 return build_error_marker_type (cu, die);
23138 }
23139
23140 /* If we already know the type we're done. */
23141 if (sig_type->type != NULL)
23142 return sig_type->type;
23143
23144 type_cu = cu;
23145 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23146 if (type_die != NULL)
23147 {
23148 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23149 is created. This is important, for example, because for c++ classes
23150 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23151 type = read_type_die (type_die, type_cu);
23152 if (type == NULL)
23153 {
b98664d3 23154 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23155 " referenced from DIE at %s [in module %s]"),
23156 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23157 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23158 type = build_error_marker_type (cu, die);
23159 }
23160 }
23161 else
23162 {
b98664d3 23163 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23164 " from DIE at %s [in module %s]"),
23165 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23166 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23167 type = build_error_marker_type (cu, die);
23168 }
23169 sig_type->type = type;
23170
23171 return type;
23172}
23173
23174/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23175 reading in and processing the type unit if necessary. */
23176
23177static struct type *
ff39bb5e 23178get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23179 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23180{
23181 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23182 if (attr->form_is_ref ())
ac9ec31b
DE
23183 {
23184 struct dwarf2_cu *type_cu = cu;
23185 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23186
23187 return read_type_die (type_die, type_cu);
23188 }
23189 else if (attr->form == DW_FORM_ref_sig8)
23190 {
23191 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23192 }
23193 else
23194 {
518817b3
SM
23195 struct dwarf2_per_objfile *dwarf2_per_objfile
23196 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23197
b98664d3 23198 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23199 " at %s [in module %s]"),
23200 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23201 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23202 return build_error_marker_type (cu, die);
23203 }
348e048f
DE
23204}
23205
e5fe5e75 23206/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23207
23208static void
e5fe5e75 23209load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23210{
52dc124a 23211 struct signatured_type *sig_type;
348e048f 23212
f4dc4d17
DE
23213 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23214 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23215
6721b2ec
DE
23216 /* We have the per_cu, but we need the signatured_type.
23217 Fortunately this is an easy translation. */
23218 gdb_assert (per_cu->is_debug_types);
23219 sig_type = (struct signatured_type *) per_cu;
348e048f 23220
6721b2ec 23221 gdb_assert (per_cu->cu == NULL);
348e048f 23222
52dc124a 23223 read_signatured_type (sig_type);
348e048f 23224
6721b2ec 23225 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23226}
23227
3019eac3
DE
23228/* Read in a signatured type and build its CU and DIEs.
23229 If the type is a stub for the real type in a DWO file,
23230 read in the real type from the DWO file as well. */
dee91e82
DE
23231
23232static void
23233read_signatured_type (struct signatured_type *sig_type)
23234{
23235 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23236
3019eac3 23237 gdb_assert (per_cu->is_debug_types);
dee91e82 23238 gdb_assert (per_cu->cu == NULL);
348e048f 23239
c0ab21c2
TT
23240 cutu_reader reader (per_cu, NULL, 0, 1, false);
23241
23242 if (!reader.dummy_p)
23243 {
23244 struct dwarf2_cu *cu = reader.cu;
23245 const gdb_byte *info_ptr = reader.info_ptr;
23246
23247 gdb_assert (cu->die_hash == NULL);
23248 cu->die_hash =
23249 htab_create_alloc_ex (cu->header.length / 12,
23250 die_hash,
23251 die_eq,
23252 NULL,
23253 &cu->comp_unit_obstack,
23254 hashtab_obstack_allocate,
23255 dummy_obstack_deallocate);
23256
3e225074 23257 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23258 reader.comp_unit_die->child
23259 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23260 reader.comp_unit_die);
23261 cu->dies = reader.comp_unit_die;
23262 /* comp_unit_die is not stored in die_hash, no need. */
23263
23264 /* We try not to read any attributes in this function, because
23265 not all CUs needed for references have been loaded yet, and
23266 symbol table processing isn't initialized. But we have to
23267 set the CU language, or we won't be able to build types
23268 correctly. Similarly, if we do not read the producer, we can
23269 not apply producer-specific interpretation. */
23270 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23271 }
23272
7ee85ab1 23273 sig_type->per_cu.tu_read = 1;
c906108c
SS
23274}
23275
c906108c
SS
23276/* Decode simple location descriptions.
23277 Given a pointer to a dwarf block that defines a location, compute
23278 the location and return the value.
23279
4cecd739
DJ
23280 NOTE drow/2003-11-18: This function is called in two situations
23281 now: for the address of static or global variables (partial symbols
23282 only) and for offsets into structures which are expected to be
23283 (more or less) constant. The partial symbol case should go away,
23284 and only the constant case should remain. That will let this
23285 function complain more accurately. A few special modes are allowed
23286 without complaint for global variables (for instance, global
23287 register values and thread-local values).
c906108c
SS
23288
23289 A location description containing no operations indicates that the
4cecd739 23290 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23291 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23292 callers will only want a very basic result and this can become a
21ae7a4d
JK
23293 complaint.
23294
23295 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23296
23297static CORE_ADDR
e7c27a73 23298decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23299{
518817b3 23300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23301 size_t i;
23302 size_t size = blk->size;
d521ce57 23303 const gdb_byte *data = blk->data;
21ae7a4d
JK
23304 CORE_ADDR stack[64];
23305 int stacki;
23306 unsigned int bytes_read, unsnd;
23307 gdb_byte op;
c906108c 23308
21ae7a4d
JK
23309 i = 0;
23310 stacki = 0;
23311 stack[stacki] = 0;
23312 stack[++stacki] = 0;
23313
23314 while (i < size)
23315 {
23316 op = data[i++];
23317 switch (op)
23318 {
23319 case DW_OP_lit0:
23320 case DW_OP_lit1:
23321 case DW_OP_lit2:
23322 case DW_OP_lit3:
23323 case DW_OP_lit4:
23324 case DW_OP_lit5:
23325 case DW_OP_lit6:
23326 case DW_OP_lit7:
23327 case DW_OP_lit8:
23328 case DW_OP_lit9:
23329 case DW_OP_lit10:
23330 case DW_OP_lit11:
23331 case DW_OP_lit12:
23332 case DW_OP_lit13:
23333 case DW_OP_lit14:
23334 case DW_OP_lit15:
23335 case DW_OP_lit16:
23336 case DW_OP_lit17:
23337 case DW_OP_lit18:
23338 case DW_OP_lit19:
23339 case DW_OP_lit20:
23340 case DW_OP_lit21:
23341 case DW_OP_lit22:
23342 case DW_OP_lit23:
23343 case DW_OP_lit24:
23344 case DW_OP_lit25:
23345 case DW_OP_lit26:
23346 case DW_OP_lit27:
23347 case DW_OP_lit28:
23348 case DW_OP_lit29:
23349 case DW_OP_lit30:
23350 case DW_OP_lit31:
23351 stack[++stacki] = op - DW_OP_lit0;
23352 break;
f1bea926 23353
21ae7a4d
JK
23354 case DW_OP_reg0:
23355 case DW_OP_reg1:
23356 case DW_OP_reg2:
23357 case DW_OP_reg3:
23358 case DW_OP_reg4:
23359 case DW_OP_reg5:
23360 case DW_OP_reg6:
23361 case DW_OP_reg7:
23362 case DW_OP_reg8:
23363 case DW_OP_reg9:
23364 case DW_OP_reg10:
23365 case DW_OP_reg11:
23366 case DW_OP_reg12:
23367 case DW_OP_reg13:
23368 case DW_OP_reg14:
23369 case DW_OP_reg15:
23370 case DW_OP_reg16:
23371 case DW_OP_reg17:
23372 case DW_OP_reg18:
23373 case DW_OP_reg19:
23374 case DW_OP_reg20:
23375 case DW_OP_reg21:
23376 case DW_OP_reg22:
23377 case DW_OP_reg23:
23378 case DW_OP_reg24:
23379 case DW_OP_reg25:
23380 case DW_OP_reg26:
23381 case DW_OP_reg27:
23382 case DW_OP_reg28:
23383 case DW_OP_reg29:
23384 case DW_OP_reg30:
23385 case DW_OP_reg31:
23386 stack[++stacki] = op - DW_OP_reg0;
23387 if (i < size)
23388 dwarf2_complex_location_expr_complaint ();
23389 break;
c906108c 23390
21ae7a4d
JK
23391 case DW_OP_regx:
23392 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23393 i += bytes_read;
23394 stack[++stacki] = unsnd;
23395 if (i < size)
23396 dwarf2_complex_location_expr_complaint ();
23397 break;
c906108c 23398
21ae7a4d
JK
23399 case DW_OP_addr:
23400 stack[++stacki] = read_address (objfile->obfd, &data[i],
23401 cu, &bytes_read);
23402 i += bytes_read;
23403 break;
d53d4ac5 23404
21ae7a4d
JK
23405 case DW_OP_const1u:
23406 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23407 i += 1;
23408 break;
23409
23410 case DW_OP_const1s:
23411 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23412 i += 1;
23413 break;
23414
23415 case DW_OP_const2u:
23416 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23417 i += 2;
23418 break;
23419
23420 case DW_OP_const2s:
23421 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23422 i += 2;
23423 break;
d53d4ac5 23424
21ae7a4d
JK
23425 case DW_OP_const4u:
23426 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23427 i += 4;
23428 break;
23429
23430 case DW_OP_const4s:
23431 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23432 i += 4;
23433 break;
23434
585861ea
JK
23435 case DW_OP_const8u:
23436 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23437 i += 8;
23438 break;
23439
21ae7a4d
JK
23440 case DW_OP_constu:
23441 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23442 &bytes_read);
23443 i += bytes_read;
23444 break;
23445
23446 case DW_OP_consts:
23447 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23448 i += bytes_read;
23449 break;
23450
23451 case DW_OP_dup:
23452 stack[stacki + 1] = stack[stacki];
23453 stacki++;
23454 break;
23455
23456 case DW_OP_plus:
23457 stack[stacki - 1] += stack[stacki];
23458 stacki--;
23459 break;
23460
23461 case DW_OP_plus_uconst:
23462 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23463 &bytes_read);
23464 i += bytes_read;
23465 break;
23466
23467 case DW_OP_minus:
23468 stack[stacki - 1] -= stack[stacki];
23469 stacki--;
23470 break;
23471
23472 case DW_OP_deref:
23473 /* If we're not the last op, then we definitely can't encode
23474 this using GDB's address_class enum. This is valid for partial
23475 global symbols, although the variable's address will be bogus
23476 in the psymtab. */
23477 if (i < size)
23478 dwarf2_complex_location_expr_complaint ();
23479 break;
23480
23481 case DW_OP_GNU_push_tls_address:
4aa4e28b 23482 case DW_OP_form_tls_address:
21ae7a4d
JK
23483 /* The top of the stack has the offset from the beginning
23484 of the thread control block at which the variable is located. */
23485 /* Nothing should follow this operator, so the top of stack would
23486 be returned. */
23487 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23488 address will be bogus in the psymtab. Make it always at least
23489 non-zero to not look as a variable garbage collected by linker
23490 which have DW_OP_addr 0. */
21ae7a4d
JK
23491 if (i < size)
23492 dwarf2_complex_location_expr_complaint ();
585861ea 23493 stack[stacki]++;
21ae7a4d
JK
23494 break;
23495
23496 case DW_OP_GNU_uninit:
23497 break;
23498
336d760d 23499 case DW_OP_addrx:
3019eac3 23500 case DW_OP_GNU_addr_index:
49f6c839 23501 case DW_OP_GNU_const_index:
3019eac3
DE
23502 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23503 &bytes_read);
23504 i += bytes_read;
23505 break;
23506
21ae7a4d
JK
23507 default:
23508 {
f39c6ffd 23509 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23510
23511 if (name)
b98664d3 23512 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23513 name);
23514 else
b98664d3 23515 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23516 op);
23517 }
23518
23519 return (stack[stacki]);
d53d4ac5 23520 }
3c6e0cb3 23521
21ae7a4d
JK
23522 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23523 outside of the allocated space. Also enforce minimum>0. */
23524 if (stacki >= ARRAY_SIZE (stack) - 1)
23525 {
b98664d3 23526 complaint (_("location description stack overflow"));
21ae7a4d
JK
23527 return 0;
23528 }
23529
23530 if (stacki <= 0)
23531 {
b98664d3 23532 complaint (_("location description stack underflow"));
21ae7a4d
JK
23533 return 0;
23534 }
23535 }
23536 return (stack[stacki]);
c906108c
SS
23537}
23538
23539/* memory allocation interface */
23540
c906108c 23541static struct dwarf_block *
7b5a2f43 23542dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23543{
8d749320 23544 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23545}
23546
c906108c 23547static struct die_info *
b60c80d6 23548dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23549{
23550 struct die_info *die;
b60c80d6
DJ
23551 size_t size = sizeof (struct die_info);
23552
23553 if (num_attrs > 1)
23554 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23555
b60c80d6 23556 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23557 memset (die, 0, sizeof (struct die_info));
23558 return (die);
23559}
2e276125
JB
23560
23561\f
23562/* Macro support. */
23563
2e276125 23564static struct macro_source_file *
804d2729
TT
23565macro_start_file (struct dwarf2_cu *cu,
23566 int file, int line,
2e276125 23567 struct macro_source_file *current_file,
43f3e411 23568 struct line_header *lh)
2e276125 23569{
233d95b5 23570 /* File name relative to the compilation directory of this source file. */
03075812 23571 gdb::unique_xmalloc_ptr<char> file_name = lh->file_file_name (file);
2e276125 23572
2e276125 23573 if (! current_file)
abc9d0dc 23574 {
fc474241
DE
23575 /* Note: We don't create a macro table for this compilation unit
23576 at all until we actually get a filename. */
c24bdb02 23577 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 23578
abc9d0dc
TT
23579 /* If we have no current file, then this must be the start_file
23580 directive for the compilation unit's main source file. */
03075812 23581 current_file = macro_set_main (macro_table, file_name.get ());
fc474241 23582 macro_define_special (macro_table);
abc9d0dc 23583 }
2e276125 23584 else
03075812 23585 current_file = macro_include (current_file, line, file_name.get ());
6e70227d 23586
2e276125
JB
23587 return current_file;
23588}
23589
2e276125
JB
23590static const char *
23591consume_improper_spaces (const char *p, const char *body)
23592{
23593 if (*p == ' ')
23594 {
b98664d3 23595 complaint (_("macro definition contains spaces "
3e43a32a 23596 "in formal argument list:\n`%s'"),
4d3c2250 23597 body);
2e276125
JB
23598
23599 while (*p == ' ')
23600 p++;
23601 }
23602
23603 return p;
23604}
23605
23606
23607static void
23608parse_macro_definition (struct macro_source_file *file, int line,
23609 const char *body)
23610{
23611 const char *p;
23612
23613 /* The body string takes one of two forms. For object-like macro
23614 definitions, it should be:
23615
23616 <macro name> " " <definition>
23617
23618 For function-like macro definitions, it should be:
23619
23620 <macro name> "() " <definition>
23621 or
23622 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23623
23624 Spaces may appear only where explicitly indicated, and in the
23625 <definition>.
23626
23627 The Dwarf 2 spec says that an object-like macro's name is always
23628 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23629 the space when the macro's definition is the empty string.
2e276125
JB
23630
23631 The Dwarf 2 spec says that there should be no spaces between the
23632 formal arguments in a function-like macro's formal argument list,
23633 but versions of GCC around March 2002 include spaces after the
23634 commas. */
23635
23636
23637 /* Find the extent of the macro name. The macro name is terminated
23638 by either a space or null character (for an object-like macro) or
23639 an opening paren (for a function-like macro). */
23640 for (p = body; *p; p++)
23641 if (*p == ' ' || *p == '(')
23642 break;
23643
23644 if (*p == ' ' || *p == '\0')
23645 {
23646 /* It's an object-like macro. */
23647 int name_len = p - body;
456e800a 23648 std::string name (body, name_len);
2e276125
JB
23649 const char *replacement;
23650
23651 if (*p == ' ')
23652 replacement = body + name_len + 1;
23653 else
23654 {
4d3c2250 23655 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23656 replacement = body + name_len;
23657 }
6e70227d 23658
456e800a 23659 macro_define_object (file, line, name.c_str (), replacement);
2e276125
JB
23660 }
23661 else if (*p == '(')
23662 {
23663 /* It's a function-like macro. */
456e800a 23664 std::string name (body, p - body);
2e276125
JB
23665 int argc = 0;
23666 int argv_size = 1;
8d749320 23667 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23668
23669 p++;
23670
23671 p = consume_improper_spaces (p, body);
23672
23673 /* Parse the formal argument list. */
23674 while (*p && *p != ')')
23675 {
23676 /* Find the extent of the current argument name. */
23677 const char *arg_start = p;
23678
23679 while (*p && *p != ',' && *p != ')' && *p != ' ')
23680 p++;
23681
23682 if (! *p || p == arg_start)
4d3c2250 23683 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23684 else
23685 {
23686 /* Make sure argv has room for the new argument. */
23687 if (argc >= argv_size)
23688 {
23689 argv_size *= 2;
224c3ddb 23690 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23691 }
23692
3f8a7804 23693 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23694 }
23695
23696 p = consume_improper_spaces (p, body);
23697
23698 /* Consume the comma, if present. */
23699 if (*p == ',')
23700 {
23701 p++;
23702
23703 p = consume_improper_spaces (p, body);
23704 }
23705 }
23706
23707 if (*p == ')')
23708 {
23709 p++;
23710
23711 if (*p == ' ')
23712 /* Perfectly formed definition, no complaints. */
456e800a 23713 macro_define_function (file, line, name.c_str (),
6e70227d 23714 argc, (const char **) argv,
2e276125
JB
23715 p + 1);
23716 else if (*p == '\0')
23717 {
23718 /* Complain, but do define it. */
4d3c2250 23719 dwarf2_macro_malformed_definition_complaint (body);
456e800a 23720 macro_define_function (file, line, name.c_str (),
6e70227d 23721 argc, (const char **) argv,
2e276125
JB
23722 p);
23723 }
23724 else
23725 /* Just complain. */
4d3c2250 23726 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23727 }
23728 else
23729 /* Just complain. */
4d3c2250 23730 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23731
2e276125
JB
23732 {
23733 int i;
23734
23735 for (i = 0; i < argc; i++)
23736 xfree (argv[i]);
23737 }
23738 xfree (argv);
23739 }
23740 else
4d3c2250 23741 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23742}
23743
cf2c3c16
TT
23744/* Skip some bytes from BYTES according to the form given in FORM.
23745 Returns the new pointer. */
2e276125 23746
d521ce57
TT
23747static const gdb_byte *
23748skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23749 enum dwarf_form form,
23750 unsigned int offset_size,
23751 struct dwarf2_section_info *section)
2e276125 23752{
cf2c3c16 23753 unsigned int bytes_read;
2e276125 23754
cf2c3c16 23755 switch (form)
2e276125 23756 {
cf2c3c16
TT
23757 case DW_FORM_data1:
23758 case DW_FORM_flag:
23759 ++bytes;
23760 break;
23761
23762 case DW_FORM_data2:
23763 bytes += 2;
23764 break;
23765
23766 case DW_FORM_data4:
23767 bytes += 4;
23768 break;
23769
23770 case DW_FORM_data8:
23771 bytes += 8;
23772 break;
23773
0224619f
JK
23774 case DW_FORM_data16:
23775 bytes += 16;
23776 break;
23777
cf2c3c16
TT
23778 case DW_FORM_string:
23779 read_direct_string (abfd, bytes, &bytes_read);
23780 bytes += bytes_read;
23781 break;
23782
23783 case DW_FORM_sec_offset:
23784 case DW_FORM_strp:
36586728 23785 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23786 bytes += offset_size;
23787 break;
23788
23789 case DW_FORM_block:
23790 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23791 bytes += bytes_read;
23792 break;
23793
23794 case DW_FORM_block1:
23795 bytes += 1 + read_1_byte (abfd, bytes);
23796 break;
23797 case DW_FORM_block2:
23798 bytes += 2 + read_2_bytes (abfd, bytes);
23799 break;
23800 case DW_FORM_block4:
23801 bytes += 4 + read_4_bytes (abfd, bytes);
23802 break;
23803
336d760d 23804 case DW_FORM_addrx:
cf2c3c16 23805 case DW_FORM_sdata:
cf532bd1 23806 case DW_FORM_strx:
cf2c3c16 23807 case DW_FORM_udata:
3019eac3
DE
23808 case DW_FORM_GNU_addr_index:
23809 case DW_FORM_GNU_str_index:
d521ce57 23810 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23811 if (bytes == NULL)
23812 {
23813 dwarf2_section_buffer_overflow_complaint (section);
23814 return NULL;
23815 }
cf2c3c16
TT
23816 break;
23817
663c44ac
JK
23818 case DW_FORM_implicit_const:
23819 break;
23820
cf2c3c16
TT
23821 default:
23822 {
b98664d3 23823 complaint (_("invalid form 0x%x in `%s'"),
96b79293 23824 form, section->get_name ());
cf2c3c16
TT
23825 return NULL;
23826 }
2e276125
JB
23827 }
23828
cf2c3c16
TT
23829 return bytes;
23830}
757a13d0 23831
cf2c3c16
TT
23832/* A helper for dwarf_decode_macros that handles skipping an unknown
23833 opcode. Returns an updated pointer to the macro data buffer; or,
23834 on error, issues a complaint and returns NULL. */
757a13d0 23835
d521ce57 23836static const gdb_byte *
cf2c3c16 23837skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23838 const gdb_byte **opcode_definitions,
23839 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23840 bfd *abfd,
23841 unsigned int offset_size,
23842 struct dwarf2_section_info *section)
23843{
23844 unsigned int bytes_read, i;
23845 unsigned long arg;
d521ce57 23846 const gdb_byte *defn;
2e276125 23847
cf2c3c16 23848 if (opcode_definitions[opcode] == NULL)
2e276125 23849 {
b98664d3 23850 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23851 opcode);
23852 return NULL;
23853 }
2e276125 23854
cf2c3c16
TT
23855 defn = opcode_definitions[opcode];
23856 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23857 defn += bytes_read;
2e276125 23858
cf2c3c16
TT
23859 for (i = 0; i < arg; ++i)
23860 {
aead7601
SM
23861 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
23862 (enum dwarf_form) defn[i], offset_size,
f664829e 23863 section);
cf2c3c16
TT
23864 if (mac_ptr == NULL)
23865 {
23866 /* skip_form_bytes already issued the complaint. */
23867 return NULL;
23868 }
23869 }
757a13d0 23870
cf2c3c16
TT
23871 return mac_ptr;
23872}
757a13d0 23873
cf2c3c16
TT
23874/* A helper function which parses the header of a macro section.
23875 If the macro section is the extended (for now called "GNU") type,
23876 then this updates *OFFSET_SIZE. Returns a pointer to just after
23877 the header, or issues a complaint and returns NULL on error. */
757a13d0 23878
d521ce57
TT
23879static const gdb_byte *
23880dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 23881 bfd *abfd,
d521ce57 23882 const gdb_byte *mac_ptr,
cf2c3c16
TT
23883 unsigned int *offset_size,
23884 int section_is_gnu)
23885{
23886 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 23887
cf2c3c16
TT
23888 if (section_is_gnu)
23889 {
23890 unsigned int version, flags;
757a13d0 23891
cf2c3c16 23892 version = read_2_bytes (abfd, mac_ptr);
0af92d60 23893 if (version != 4 && version != 5)
cf2c3c16 23894 {
b98664d3 23895 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
23896 version);
23897 return NULL;
23898 }
23899 mac_ptr += 2;
757a13d0 23900
cf2c3c16
TT
23901 flags = read_1_byte (abfd, mac_ptr);
23902 ++mac_ptr;
23903 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 23904
cf2c3c16
TT
23905 if ((flags & 2) != 0)
23906 /* We don't need the line table offset. */
23907 mac_ptr += *offset_size;
757a13d0 23908
cf2c3c16
TT
23909 /* Vendor opcode descriptions. */
23910 if ((flags & 4) != 0)
23911 {
23912 unsigned int i, count;
757a13d0 23913
cf2c3c16
TT
23914 count = read_1_byte (abfd, mac_ptr);
23915 ++mac_ptr;
23916 for (i = 0; i < count; ++i)
23917 {
23918 unsigned int opcode, bytes_read;
23919 unsigned long arg;
23920
23921 opcode = read_1_byte (abfd, mac_ptr);
23922 ++mac_ptr;
23923 opcode_definitions[opcode] = mac_ptr;
23924 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
23925 mac_ptr += bytes_read;
23926 mac_ptr += arg;
23927 }
757a13d0 23928 }
cf2c3c16 23929 }
757a13d0 23930
cf2c3c16
TT
23931 return mac_ptr;
23932}
757a13d0 23933
cf2c3c16 23934/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 23935 including DW_MACRO_import. */
cf2c3c16
TT
23936
23937static void
804d2729 23938dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 23939 bfd *abfd,
d521ce57 23940 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 23941 struct macro_source_file *current_file,
43f3e411 23942 struct line_header *lh,
cf2c3c16 23943 struct dwarf2_section_info *section,
36586728 23944 int section_is_gnu, int section_is_dwz,
cf2c3c16 23945 unsigned int offset_size,
8fc3fc34 23946 htab_t include_hash)
cf2c3c16 23947{
804d2729
TT
23948 struct dwarf2_per_objfile *dwarf2_per_objfile
23949 = cu->per_cu->dwarf2_per_objfile;
4d663531 23950 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
23951 enum dwarf_macro_record_type macinfo_type;
23952 int at_commandline;
d521ce57 23953 const gdb_byte *opcode_definitions[256];
757a13d0 23954
cf2c3c16
TT
23955 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
23956 &offset_size, section_is_gnu);
23957 if (mac_ptr == NULL)
23958 {
23959 /* We already issued a complaint. */
23960 return;
23961 }
757a13d0
JK
23962
23963 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
23964 GDB is still reading the definitions from command line. First
23965 DW_MACINFO_start_file will need to be ignored as it was already executed
23966 to create CURRENT_FILE for the main source holding also the command line
23967 definitions. On first met DW_MACINFO_start_file this flag is reset to
23968 normally execute all the remaining DW_MACINFO_start_file macinfos. */
23969
23970 at_commandline = 1;
23971
23972 do
23973 {
23974 /* Do we at least have room for a macinfo type byte? */
23975 if (mac_ptr >= mac_end)
23976 {
f664829e 23977 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
23978 break;
23979 }
23980
aead7601 23981 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
23982 mac_ptr++;
23983
cf2c3c16
TT
23984 /* Note that we rely on the fact that the corresponding GNU and
23985 DWARF constants are the same. */
132448f8
SM
23986 DIAGNOSTIC_PUSH
23987 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
23988 switch (macinfo_type)
23989 {
23990 /* A zero macinfo type indicates the end of the macro
23991 information. */
23992 case 0:
23993 break;
2e276125 23994
0af92d60
JK
23995 case DW_MACRO_define:
23996 case DW_MACRO_undef:
23997 case DW_MACRO_define_strp:
23998 case DW_MACRO_undef_strp:
23999 case DW_MACRO_define_sup:
24000 case DW_MACRO_undef_sup:
2e276125 24001 {
891d2f0b 24002 unsigned int bytes_read;
2e276125 24003 int line;
d521ce57 24004 const char *body;
cf2c3c16 24005 int is_define;
2e276125 24006
cf2c3c16
TT
24007 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24008 mac_ptr += bytes_read;
24009
0af92d60
JK
24010 if (macinfo_type == DW_MACRO_define
24011 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24012 {
24013 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24014 mac_ptr += bytes_read;
24015 }
24016 else
24017 {
24018 LONGEST str_offset;
24019
24020 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24021 mac_ptr += offset_size;
2e276125 24022
0af92d60
JK
24023 if (macinfo_type == DW_MACRO_define_sup
24024 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24025 || section_is_dwz)
36586728 24026 {
ed2dc618
SM
24027 struct dwz_file *dwz
24028 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24029
ed2dc618
SM
24030 body = read_indirect_string_from_dwz (objfile,
24031 dwz, str_offset);
36586728
TT
24032 }
24033 else
ed2dc618
SM
24034 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24035 abfd, str_offset);
cf2c3c16
TT
24036 }
24037
0af92d60
JK
24038 is_define = (macinfo_type == DW_MACRO_define
24039 || macinfo_type == DW_MACRO_define_strp
24040 || macinfo_type == DW_MACRO_define_sup);
2e276125 24041 if (! current_file)
757a13d0
JK
24042 {
24043 /* DWARF violation as no main source is present. */
b98664d3 24044 complaint (_("debug info with no main source gives macro %s "
757a13d0 24045 "on line %d: %s"),
cf2c3c16
TT
24046 is_define ? _("definition") : _("undefinition"),
24047 line, body);
757a13d0
JK
24048 break;
24049 }
3e43a32a
MS
24050 if ((line == 0 && !at_commandline)
24051 || (line != 0 && at_commandline))
b98664d3 24052 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24053 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24054 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24055 line == 0 ? _("zero") : _("non-zero"), line, body);
24056
955b06fa 24057 if (body == NULL)
7bede828 24058 {
955b06fa
SDJ
24059 /* Fedora's rpm-build's "debugedit" binary
24060 corrupted .debug_macro sections.
24061
24062 For more info, see
24063 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24064 complaint (_("debug info gives %s invalid macro %s "
24065 "without body (corrupted?) at line %d "
24066 "on file %s"),
24067 at_commandline ? _("command-line") : _("in-file"),
24068 is_define ? _("definition") : _("undefinition"),
24069 line, current_file->filename);
7bede828 24070 }
955b06fa
SDJ
24071 else if (is_define)
24072 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24073 else
24074 {
0af92d60
JK
24075 gdb_assert (macinfo_type == DW_MACRO_undef
24076 || macinfo_type == DW_MACRO_undef_strp
24077 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24078 macro_undef (current_file, line, body);
24079 }
2e276125
JB
24080 }
24081 break;
24082
0af92d60 24083 case DW_MACRO_start_file:
2e276125 24084 {
891d2f0b 24085 unsigned int bytes_read;
2e276125
JB
24086 int line, file;
24087
24088 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24089 mac_ptr += bytes_read;
24090 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24091 mac_ptr += bytes_read;
24092
3e43a32a
MS
24093 if ((line == 0 && !at_commandline)
24094 || (line != 0 && at_commandline))
b98664d3 24095 complaint (_("debug info gives source %d included "
757a13d0
JK
24096 "from %s at %s line %d"),
24097 file, at_commandline ? _("command-line") : _("file"),
24098 line == 0 ? _("zero") : _("non-zero"), line);
24099
24100 if (at_commandline)
24101 {
0af92d60 24102 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24103 pass one. */
757a13d0
JK
24104 at_commandline = 0;
24105 }
24106 else
804d2729
TT
24107 current_file = macro_start_file (cu, file, line, current_file,
24108 lh);
2e276125
JB
24109 }
24110 break;
24111
0af92d60 24112 case DW_MACRO_end_file:
2e276125 24113 if (! current_file)
b98664d3 24114 complaint (_("macro debug info has an unmatched "
3e43a32a 24115 "`close_file' directive"));
2e276125
JB
24116 else
24117 {
24118 current_file = current_file->included_by;
24119 if (! current_file)
24120 {
cf2c3c16 24121 enum dwarf_macro_record_type next_type;
2e276125
JB
24122
24123 /* GCC circa March 2002 doesn't produce the zero
24124 type byte marking the end of the compilation
24125 unit. Complain if it's not there, but exit no
24126 matter what. */
24127
24128 /* Do we at least have room for a macinfo type byte? */
24129 if (mac_ptr >= mac_end)
24130 {
f664829e 24131 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24132 return;
24133 }
24134
24135 /* We don't increment mac_ptr here, so this is just
24136 a look-ahead. */
aead7601
SM
24137 next_type
24138 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24139 mac_ptr);
2e276125 24140 if (next_type != 0)
b98664d3 24141 complaint (_("no terminating 0-type entry for "
3e43a32a 24142 "macros in `.debug_macinfo' section"));
2e276125
JB
24143
24144 return;
24145 }
24146 }
24147 break;
24148
0af92d60
JK
24149 case DW_MACRO_import:
24150 case DW_MACRO_import_sup:
cf2c3c16
TT
24151 {
24152 LONGEST offset;
8fc3fc34 24153 void **slot;
a036ba48
TT
24154 bfd *include_bfd = abfd;
24155 struct dwarf2_section_info *include_section = section;
d521ce57 24156 const gdb_byte *include_mac_end = mac_end;
a036ba48 24157 int is_dwz = section_is_dwz;
d521ce57 24158 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24159
24160 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24161 mac_ptr += offset_size;
24162
0af92d60 24163 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24164 {
ed2dc618 24165 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24166
96b79293 24167 dwz->macro.read (objfile);
a036ba48 24168
a036ba48 24169 include_section = &dwz->macro;
96b79293 24170 include_bfd = include_section->get_bfd_owner ();
a036ba48
TT
24171 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24172 is_dwz = 1;
24173 }
24174
24175 new_mac_ptr = include_section->buffer + offset;
24176 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24177
8fc3fc34
TT
24178 if (*slot != NULL)
24179 {
24180 /* This has actually happened; see
24181 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24182 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24183 ".debug_macro section"));
24184 }
24185 else
24186 {
d521ce57 24187 *slot = (void *) new_mac_ptr;
36586728 24188
804d2729 24189 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24190 include_mac_end, current_file, lh,
36586728 24191 section, section_is_gnu, is_dwz,
4d663531 24192 offset_size, include_hash);
8fc3fc34 24193
d521ce57 24194 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24195 }
cf2c3c16
TT
24196 }
24197 break;
24198
2e276125 24199 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24200 if (!section_is_gnu)
24201 {
24202 unsigned int bytes_read;
2e276125 24203
ac298888
TT
24204 /* This reads the constant, but since we don't recognize
24205 any vendor extensions, we ignore it. */
24206 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24207 mac_ptr += bytes_read;
24208 read_direct_string (abfd, mac_ptr, &bytes_read);
24209 mac_ptr += bytes_read;
2e276125 24210
cf2c3c16
TT
24211 /* We don't recognize any vendor extensions. */
24212 break;
24213 }
24214 /* FALLTHROUGH */
24215
24216 default:
24217 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24218 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24219 section);
24220 if (mac_ptr == NULL)
24221 return;
24222 break;
2e276125 24223 }
132448f8 24224 DIAGNOSTIC_POP
757a13d0 24225 } while (macinfo_type != 0);
2e276125 24226}
8e19ed76 24227
cf2c3c16 24228static void
09262596 24229dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24230 int section_is_gnu)
cf2c3c16 24231{
518817b3
SM
24232 struct dwarf2_per_objfile *dwarf2_per_objfile
24233 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24234 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24235 struct line_header *lh = cu->line_header;
24236 bfd *abfd;
d521ce57 24237 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24238 struct macro_source_file *current_file = 0;
24239 enum dwarf_macro_record_type macinfo_type;
24240 unsigned int offset_size = cu->header.offset_size;
d521ce57 24241 const gdb_byte *opcode_definitions[256];
8fc3fc34 24242 void **slot;
09262596
DE
24243 struct dwarf2_section_info *section;
24244 const char *section_name;
24245
24246 if (cu->dwo_unit != NULL)
24247 {
24248 if (section_is_gnu)
24249 {
24250 section = &cu->dwo_unit->dwo_file->sections.macro;
24251 section_name = ".debug_macro.dwo";
24252 }
24253 else
24254 {
24255 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24256 section_name = ".debug_macinfo.dwo";
24257 }
24258 }
24259 else
24260 {
24261 if (section_is_gnu)
24262 {
24263 section = &dwarf2_per_objfile->macro;
24264 section_name = ".debug_macro";
24265 }
24266 else
24267 {
24268 section = &dwarf2_per_objfile->macinfo;
24269 section_name = ".debug_macinfo";
24270 }
24271 }
cf2c3c16 24272
96b79293 24273 section->read (objfile);
cf2c3c16
TT
24274 if (section->buffer == NULL)
24275 {
b98664d3 24276 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24277 return;
24278 }
96b79293 24279 abfd = section->get_bfd_owner ();
cf2c3c16
TT
24280
24281 /* First pass: Find the name of the base filename.
24282 This filename is needed in order to process all macros whose definition
24283 (or undefinition) comes from the command line. These macros are defined
24284 before the first DW_MACINFO_start_file entry, and yet still need to be
24285 associated to the base file.
24286
24287 To determine the base file name, we scan the macro definitions until we
24288 reach the first DW_MACINFO_start_file entry. We then initialize
24289 CURRENT_FILE accordingly so that any macro definition found before the
24290 first DW_MACINFO_start_file can still be associated to the base file. */
24291
24292 mac_ptr = section->buffer + offset;
24293 mac_end = section->buffer + section->size;
24294
24295 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24296 &offset_size, section_is_gnu);
24297 if (mac_ptr == NULL)
24298 {
24299 /* We already issued a complaint. */
24300 return;
24301 }
24302
24303 do
24304 {
24305 /* Do we at least have room for a macinfo type byte? */
24306 if (mac_ptr >= mac_end)
24307 {
24308 /* Complaint is printed during the second pass as GDB will probably
24309 stop the first pass earlier upon finding
24310 DW_MACINFO_start_file. */
24311 break;
24312 }
24313
aead7601 24314 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24315 mac_ptr++;
24316
24317 /* Note that we rely on the fact that the corresponding GNU and
24318 DWARF constants are the same. */
132448f8
SM
24319 DIAGNOSTIC_PUSH
24320 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24321 switch (macinfo_type)
24322 {
24323 /* A zero macinfo type indicates the end of the macro
24324 information. */
24325 case 0:
24326 break;
24327
0af92d60
JK
24328 case DW_MACRO_define:
24329 case DW_MACRO_undef:
cf2c3c16
TT
24330 /* Only skip the data by MAC_PTR. */
24331 {
24332 unsigned int bytes_read;
24333
24334 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24335 mac_ptr += bytes_read;
24336 read_direct_string (abfd, mac_ptr, &bytes_read);
24337 mac_ptr += bytes_read;
24338 }
24339 break;
24340
0af92d60 24341 case DW_MACRO_start_file:
cf2c3c16
TT
24342 {
24343 unsigned int bytes_read;
24344 int line, file;
24345
24346 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24347 mac_ptr += bytes_read;
24348 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24349 mac_ptr += bytes_read;
24350
804d2729 24351 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24352 }
24353 break;
24354
0af92d60 24355 case DW_MACRO_end_file:
cf2c3c16
TT
24356 /* No data to skip by MAC_PTR. */
24357 break;
24358
0af92d60
JK
24359 case DW_MACRO_define_strp:
24360 case DW_MACRO_undef_strp:
24361 case DW_MACRO_define_sup:
24362 case DW_MACRO_undef_sup:
cf2c3c16
TT
24363 {
24364 unsigned int bytes_read;
24365
24366 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24367 mac_ptr += bytes_read;
24368 mac_ptr += offset_size;
24369 }
24370 break;
24371
0af92d60
JK
24372 case DW_MACRO_import:
24373 case DW_MACRO_import_sup:
cf2c3c16 24374 /* Note that, according to the spec, a transparent include
0af92d60 24375 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24376 skip this opcode. */
24377 mac_ptr += offset_size;
24378 break;
24379
24380 case DW_MACINFO_vendor_ext:
24381 /* Only skip the data by MAC_PTR. */
24382 if (!section_is_gnu)
24383 {
24384 unsigned int bytes_read;
24385
24386 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24387 mac_ptr += bytes_read;
24388 read_direct_string (abfd, mac_ptr, &bytes_read);
24389 mac_ptr += bytes_read;
24390 }
24391 /* FALLTHROUGH */
24392
24393 default:
24394 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24395 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24396 section);
24397 if (mac_ptr == NULL)
24398 return;
24399 break;
24400 }
132448f8 24401 DIAGNOSTIC_POP
cf2c3c16
TT
24402 } while (macinfo_type != 0 && current_file == NULL);
24403
24404 /* Second pass: Process all entries.
24405
24406 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24407 command-line macro definitions/undefinitions. This flag is unset when we
24408 reach the first DW_MACINFO_start_file entry. */
24409
fc4007c9
TT
24410 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24411 htab_eq_pointer,
24412 NULL, xcalloc, xfree));
8fc3fc34 24413 mac_ptr = section->buffer + offset;
fc4007c9 24414 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24415 *slot = (void *) mac_ptr;
804d2729 24416 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24417 current_file, lh, section,
fc4007c9
TT
24418 section_is_gnu, 0, offset_size,
24419 include_hash.get ());
cf2c3c16
TT
24420}
24421
3019eac3
DE
24422/* Return the .debug_loc section to use for CU.
24423 For DWO files use .debug_loc.dwo. */
24424
24425static struct dwarf2_section_info *
24426cu_debug_loc_section (struct dwarf2_cu *cu)
24427{
518817b3
SM
24428 struct dwarf2_per_objfile *dwarf2_per_objfile
24429 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24430
3019eac3 24431 if (cu->dwo_unit)
43988095
JK
24432 {
24433 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24434
43988095
JK
24435 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24436 }
24437 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24438 : &dwarf2_per_objfile->loc);
3019eac3
DE
24439}
24440
8cf6f0b1
TT
24441/* A helper function that fills in a dwarf2_loclist_baton. */
24442
24443static void
24444fill_in_loclist_baton (struct dwarf2_cu *cu,
24445 struct dwarf2_loclist_baton *baton,
ff39bb5e 24446 const struct attribute *attr)
8cf6f0b1 24447{
518817b3
SM
24448 struct dwarf2_per_objfile *dwarf2_per_objfile
24449 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24450 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24451
96b79293 24452 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
24453
24454 baton->per_cu = cu->per_cu;
24455 gdb_assert (baton->per_cu);
24456 /* We don't know how long the location list is, but make sure we
24457 don't run off the edge of the section. */
3019eac3
DE
24458 baton->size = section->size - DW_UNSND (attr);
24459 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24460 baton->base_address = cu->base_address;
f664829e 24461 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24462}
24463
4c2df51b 24464static void
ff39bb5e 24465dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24466 struct dwarf2_cu *cu, int is_block)
4c2df51b 24467{
518817b3
SM
24468 struct dwarf2_per_objfile *dwarf2_per_objfile
24469 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24470 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24471 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24472
cd6c91b4 24473 if (attr->form_is_section_offset ()
3019eac3 24474 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24475 the section. If so, fall through to the complaint in the
24476 other branch. */
3019eac3 24477 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24478 {
0d53c4c4 24479 struct dwarf2_loclist_baton *baton;
4c2df51b 24480
8d749320 24481 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24482
8cf6f0b1 24483 fill_in_loclist_baton (cu, baton, attr);
be391dca 24484
d00adf39 24485 if (cu->base_known == 0)
b98664d3 24486 complaint (_("Location list used without "
3e43a32a 24487 "specifying the CU base address."));
4c2df51b 24488
f1e6e072
TT
24489 SYMBOL_ACLASS_INDEX (sym) = (is_block
24490 ? dwarf2_loclist_block_index
24491 : dwarf2_loclist_index);
0d53c4c4
DJ
24492 SYMBOL_LOCATION_BATON (sym) = baton;
24493 }
24494 else
24495 {
24496 struct dwarf2_locexpr_baton *baton;
24497
8d749320 24498 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24499 baton->per_cu = cu->per_cu;
24500 gdb_assert (baton->per_cu);
0d53c4c4 24501
4fc6c0d5 24502 if (attr->form_is_block ())
0d53c4c4
DJ
24503 {
24504 /* Note that we're just copying the block's data pointer
24505 here, not the actual data. We're still pointing into the
6502dd73
DJ
24506 info_buffer for SYM's objfile; right now we never release
24507 that buffer, but when we do clean up properly this may
24508 need to change. */
0d53c4c4
DJ
24509 baton->size = DW_BLOCK (attr)->size;
24510 baton->data = DW_BLOCK (attr)->data;
24511 }
24512 else
24513 {
24514 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24515 sym->natural_name ());
0d53c4c4 24516 baton->size = 0;
0d53c4c4 24517 }
6e70227d 24518
f1e6e072
TT
24519 SYMBOL_ACLASS_INDEX (sym) = (is_block
24520 ? dwarf2_locexpr_block_index
24521 : dwarf2_locexpr_index);
0d53c4c4
DJ
24522 SYMBOL_LOCATION_BATON (sym) = baton;
24523 }
4c2df51b 24524}
6502dd73 24525
9aa1f1e3
TT
24526/* Return the OBJFILE associated with the compilation unit CU. If CU
24527 came from a separate debuginfo file, then the master objfile is
24528 returned. */
ae0d2f24
UW
24529
24530struct objfile *
24531dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24532{
e3b94546 24533 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24534
24535 /* Return the master objfile, so that we can report and look up the
24536 correct file containing this variable. */
24537 if (objfile->separate_debug_objfile_backlink)
24538 objfile = objfile->separate_debug_objfile_backlink;
24539
24540 return objfile;
24541}
24542
96408a79
SA
24543/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24544 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24545 CU_HEADERP first. */
24546
24547static const struct comp_unit_head *
24548per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24549 struct dwarf2_per_cu_data *per_cu)
24550{
d521ce57 24551 const gdb_byte *info_ptr;
96408a79
SA
24552
24553 if (per_cu->cu)
24554 return &per_cu->cu->header;
24555
9c541725 24556 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24557
24558 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24559 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24560 rcuh_kind::COMPILE);
96408a79
SA
24561
24562 return cu_headerp;
24563}
24564
ae0d2f24
UW
24565/* Return the address size given in the compilation unit header for CU. */
24566
98714339 24567int
ae0d2f24
UW
24568dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24569{
96408a79
SA
24570 struct comp_unit_head cu_header_local;
24571 const struct comp_unit_head *cu_headerp;
c471e790 24572
96408a79
SA
24573 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24574
24575 return cu_headerp->addr_size;
ae0d2f24
UW
24576}
24577
9eae7c52
TT
24578/* Return the offset size given in the compilation unit header for CU. */
24579
24580int
24581dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24582{
96408a79
SA
24583 struct comp_unit_head cu_header_local;
24584 const struct comp_unit_head *cu_headerp;
9c6c53f7 24585
96408a79
SA
24586 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24587
24588 return cu_headerp->offset_size;
24589}
24590
24591/* See its dwarf2loc.h declaration. */
24592
24593int
24594dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24595{
24596 struct comp_unit_head cu_header_local;
24597 const struct comp_unit_head *cu_headerp;
24598
24599 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24600
24601 if (cu_headerp->version == 2)
24602 return cu_headerp->addr_size;
24603 else
24604 return cu_headerp->offset_size;
181cebd4
JK
24605}
24606
9aa1f1e3
TT
24607/* Return the text offset of the CU. The returned offset comes from
24608 this CU's objfile. If this objfile came from a separate debuginfo
24609 file, then the offset may be different from the corresponding
24610 offset in the parent objfile. */
24611
24612CORE_ADDR
24613dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24614{
b3b3bada 24615 return per_cu->dwarf2_per_objfile->objfile->text_section_offset ();
9aa1f1e3
TT
24616}
24617
9a49df9d
AB
24618/* Return a type that is a generic pointer type, the size of which matches
24619 the address size given in the compilation unit header for PER_CU. */
24620static struct type *
24621dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
24622{
24623 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
24624 struct type *void_type = objfile_type (objfile)->builtin_void;
24625 struct type *addr_type = lookup_pointer_type (void_type);
24626 int addr_size = dwarf2_per_cu_addr_size (per_cu);
24627
24628 if (TYPE_LENGTH (addr_type) == addr_size)
24629 return addr_type;
24630
24631 addr_type
24632 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
24633 return addr_type;
24634}
24635
43988095
JK
24636/* Return DWARF version number of PER_CU. */
24637
24638short
24639dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24640{
24641 return per_cu->dwarf_version;
24642}
24643
348e048f
DE
24644/* Locate the .debug_info compilation unit from CU's objfile which contains
24645 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24646
24647static struct dwarf2_per_cu_data *
9c541725 24648dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24649 unsigned int offset_in_dwz,
ed2dc618 24650 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24651{
24652 struct dwarf2_per_cu_data *this_cu;
24653 int low, high;
24654
ae038cb0 24655 low = 0;
b76e467d 24656 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24657 while (high > low)
24658 {
36586728 24659 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24660 int mid = low + (high - low) / 2;
9a619af0 24661
36586728 24662 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 24663 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24664 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 24665 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
24666 high = mid;
24667 else
24668 low = mid + 1;
24669 }
24670 gdb_assert (low == high);
36586728 24671 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 24672 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24673 {
36586728 24674 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24675 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24676 "offset %s [in module %s]"),
24677 sect_offset_str (sect_off),
ed2dc618 24678 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24679
9c541725
PA
24680 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24681 <= sect_off);
ae038cb0
DJ
24682 return dwarf2_per_objfile->all_comp_units[low-1];
24683 }
24684 else
24685 {
b76e467d 24686 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24687 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24688 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24689 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24690 return this_cu;
24691 }
24692}
24693
23745b47 24694/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24695
fcd3b13d
SM
24696dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24697 : per_cu (per_cu_),
9068261f
AB
24698 mark (false),
24699 has_loclist (false),
24700 checked_producer (false),
24701 producer_is_gxx_lt_4_6 (false),
24702 producer_is_gcc_lt_4_3 (false),
eb77c9df 24703 producer_is_icc (false),
9068261f 24704 producer_is_icc_lt_14 (false),
c258c396 24705 producer_is_codewarrior (false),
9068261f 24706 processing_has_namespace_info (false)
93311388 24707{
fcd3b13d
SM
24708 per_cu->cu = this;
24709}
24710
24711/* Destroy a dwarf2_cu. */
24712
24713dwarf2_cu::~dwarf2_cu ()
24714{
24715 per_cu->cu = NULL;
9816fde3
JK
24716}
24717
24718/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24719
24720static void
95554aad
TT
24721prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24722 enum language pretend_language)
9816fde3
JK
24723{
24724 struct attribute *attr;
24725
24726 /* Set the language we're debugging. */
24727 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24728 if (attr != nullptr)
9816fde3
JK
24729 set_cu_language (DW_UNSND (attr), cu);
24730 else
9cded63f 24731 {
95554aad 24732 cu->language = pretend_language;
9cded63f
TT
24733 cu->language_defn = language_def (cu->language);
24734 }
dee91e82 24735
7d45c7c3 24736 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24737}
24738
ae038cb0
DJ
24739/* Increase the age counter on each cached compilation unit, and free
24740 any that are too old. */
24741
24742static void
ed2dc618 24743age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24744{
24745 struct dwarf2_per_cu_data *per_cu, **last_chain;
24746
24747 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24748 per_cu = dwarf2_per_objfile->read_in_chain;
24749 while (per_cu != NULL)
24750 {
24751 per_cu->cu->last_used ++;
b4f54984 24752 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24753 dwarf2_mark (per_cu->cu);
24754 per_cu = per_cu->cu->read_in_chain;
24755 }
24756
24757 per_cu = dwarf2_per_objfile->read_in_chain;
24758 last_chain = &dwarf2_per_objfile->read_in_chain;
24759 while (per_cu != NULL)
24760 {
24761 struct dwarf2_per_cu_data *next_cu;
24762
24763 next_cu = per_cu->cu->read_in_chain;
24764
24765 if (!per_cu->cu->mark)
24766 {
fcd3b13d 24767 delete per_cu->cu;
ae038cb0
DJ
24768 *last_chain = next_cu;
24769 }
24770 else
24771 last_chain = &per_cu->cu->read_in_chain;
24772
24773 per_cu = next_cu;
24774 }
24775}
24776
24777/* Remove a single compilation unit from the cache. */
24778
24779static void
dee91e82 24780free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24781{
24782 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24783 struct dwarf2_per_objfile *dwarf2_per_objfile
24784 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24785
24786 per_cu = dwarf2_per_objfile->read_in_chain;
24787 last_chain = &dwarf2_per_objfile->read_in_chain;
24788 while (per_cu != NULL)
24789 {
24790 struct dwarf2_per_cu_data *next_cu;
24791
24792 next_cu = per_cu->cu->read_in_chain;
24793
dee91e82 24794 if (per_cu == target_per_cu)
ae038cb0 24795 {
fcd3b13d 24796 delete per_cu->cu;
dee91e82 24797 per_cu->cu = NULL;
ae038cb0
DJ
24798 *last_chain = next_cu;
24799 break;
24800 }
24801 else
24802 last_chain = &per_cu->cu->read_in_chain;
24803
24804 per_cu = next_cu;
24805 }
24806}
24807
dee91e82
DE
24808/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24809 We store these in a hash table separate from the DIEs, and preserve them
24810 when the DIEs are flushed out of cache.
24811
24812 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24813 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24814 or the type may come from a DWO file. Furthermore, while it's more logical
24815 to use per_cu->section+offset, with Fission the section with the data is in
24816 the DWO file but we don't know that section at the point we need it.
24817 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24818 because we can enter the lookup routine, get_die_type_at_offset, from
24819 outside this file, and thus won't necessarily have PER_CU->cu.
24820 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24821
dee91e82 24822struct dwarf2_per_cu_offset_and_type
1c379e20 24823{
dee91e82 24824 const struct dwarf2_per_cu_data *per_cu;
9c541725 24825 sect_offset sect_off;
1c379e20
DJ
24826 struct type *type;
24827};
24828
dee91e82 24829/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24830
24831static hashval_t
dee91e82 24832per_cu_offset_and_type_hash (const void *item)
1c379e20 24833{
9a3c8263
SM
24834 const struct dwarf2_per_cu_offset_and_type *ofs
24835 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24836
9c541725 24837 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24838}
24839
dee91e82 24840/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24841
24842static int
dee91e82 24843per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24844{
9a3c8263
SM
24845 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24846 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24847 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24848 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24849
dee91e82 24850 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24851 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24852}
24853
24854/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24855 table if necessary. For convenience, return TYPE.
24856
24857 The DIEs reading must have careful ordering to:
85102364 24858 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24859 reading current DIE.
24860 * Not trying to dereference contents of still incompletely read in types
24861 while reading in other DIEs.
24862 * Enable referencing still incompletely read in types just by a pointer to
24863 the type without accessing its fields.
24864
24865 Therefore caller should follow these rules:
24866 * Try to fetch any prerequisite types we may need to build this DIE type
24867 before building the type and calling set_die_type.
e71ec853 24868 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24869 possible before fetching more types to complete the current type.
24870 * Make the type as complete as possible before fetching more types. */
1c379e20 24871
f792889a 24872static struct type *
1c379e20
DJ
24873set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24874{
518817b3
SM
24875 struct dwarf2_per_objfile *dwarf2_per_objfile
24876 = cu->per_cu->dwarf2_per_objfile;
dee91e82 24877 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 24878 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
24879 struct attribute *attr;
24880 struct dynamic_prop prop;
1c379e20 24881
b4ba55a1
JB
24882 /* For Ada types, make sure that the gnat-specific data is always
24883 initialized (if not already set). There are a few types where
24884 we should not be doing so, because the type-specific area is
24885 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24886 where the type-specific area is used to store the floatformat).
24887 But this is not a problem, because the gnat-specific information
24888 is actually not needed for these types. */
24889 if (need_gnat_info (cu)
24890 && TYPE_CODE (type) != TYPE_CODE_FUNC
24891 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
24892 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
24893 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
24894 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
24895 && !HAVE_GNAT_AUX_INFO (type))
24896 INIT_GNAT_SPECIFIC (type);
24897
3f2f83dd
KB
24898 /* Read DW_AT_allocated and set in type. */
24899 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 24900 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24901 {
9a49df9d
AB
24902 struct type *prop_type
24903 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
24904 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24905 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
24906 }
24907 else if (attr != NULL)
24908 {
b98664d3 24909 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 24910 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24911 sect_offset_str (die->sect_off));
3f2f83dd
KB
24912 }
24913
24914 /* Read DW_AT_associated and set in type. */
24915 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 24916 if (attr != NULL && attr->form_is_block ())
3f2f83dd 24917 {
9a49df9d
AB
24918 struct type *prop_type
24919 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
24920 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 24921 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
24922 }
24923 else if (attr != NULL)
24924 {
b98664d3 24925 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 24926 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 24927 sect_offset_str (die->sect_off));
3f2f83dd
KB
24928 }
24929
3cdcd0ce
JB
24930 /* Read DW_AT_data_location and set in type. */
24931 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
24932 if (attr_to_dynamic_prop (attr, die, cu, &prop,
24933 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 24934 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 24935
dee91e82 24936 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
24937 dwarf2_per_objfile->die_type_hash
24938 = htab_up (htab_create_alloc (127,
24939 per_cu_offset_and_type_hash,
24940 per_cu_offset_and_type_eq,
24941 NULL, xcalloc, xfree));
1c379e20 24942
dee91e82 24943 ofs.per_cu = cu->per_cu;
9c541725 24944 ofs.sect_off = die->sect_off;
1c379e20 24945 ofs.type = type;
dee91e82 24946 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 24947 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24948 if (*slot)
b98664d3 24949 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24950 sect_offset_str (die->sect_off));
8d749320
SM
24951 *slot = XOBNEW (&objfile->objfile_obstack,
24952 struct dwarf2_per_cu_offset_and_type);
1c379e20 24953 **slot = ofs;
f792889a 24954 return type;
1c379e20
DJ
24955}
24956
9c541725 24957/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24958 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24959
24960static struct type *
9c541725 24961get_die_type_at_offset (sect_offset sect_off,
673bfd45 24962 struct dwarf2_per_cu_data *per_cu)
1c379e20 24963{
dee91e82 24964 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 24965 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 24966
dee91e82 24967 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 24968 return NULL;
1c379e20 24969
dee91e82 24970 ofs.per_cu = per_cu;
9c541725 24971 ofs.sect_off = sect_off;
9a3c8263 24972 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 24973 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24974 if (slot)
24975 return slot->type;
24976 else
24977 return NULL;
24978}
24979
02142a6c 24980/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24981 or return NULL if DIE does not have a saved type. */
24982
24983static struct type *
24984get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24985{
9c541725 24986 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
24987}
24988
10b3939b
DJ
24989/* Add a dependence relationship from CU to REF_PER_CU. */
24990
24991static void
24992dwarf2_add_dependence (struct dwarf2_cu *cu,
24993 struct dwarf2_per_cu_data *ref_per_cu)
24994{
24995 void **slot;
24996
24997 if (cu->dependencies == NULL)
24998 cu->dependencies
24999 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25000 NULL, &cu->comp_unit_obstack,
25001 hashtab_obstack_allocate,
25002 dummy_obstack_deallocate);
25003
25004 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25005 if (*slot == NULL)
25006 *slot = ref_per_cu;
25007}
1c379e20 25008
f504f079
DE
25009/* Subroutine of dwarf2_mark to pass to htab_traverse.
25010 Set the mark field in every compilation unit in the
ae038cb0
DJ
25011 cache that we must keep because we are keeping CU. */
25012
10b3939b
DJ
25013static int
25014dwarf2_mark_helper (void **slot, void *data)
25015{
25016 struct dwarf2_per_cu_data *per_cu;
25017
25018 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25019
25020 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25021 reading of the chain. As such dependencies remain valid it is not much
25022 useful to track and undo them during QUIT cleanups. */
25023 if (per_cu->cu == NULL)
25024 return 1;
25025
10b3939b
DJ
25026 if (per_cu->cu->mark)
25027 return 1;
9068261f 25028 per_cu->cu->mark = true;
10b3939b
DJ
25029
25030 if (per_cu->cu->dependencies != NULL)
25031 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25032
25033 return 1;
25034}
25035
f504f079
DE
25036/* Set the mark field in CU and in every other compilation unit in the
25037 cache that we must keep because we are keeping CU. */
25038
ae038cb0
DJ
25039static void
25040dwarf2_mark (struct dwarf2_cu *cu)
25041{
25042 if (cu->mark)
25043 return;
9068261f 25044 cu->mark = true;
10b3939b
DJ
25045 if (cu->dependencies != NULL)
25046 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25047}
25048
25049static void
25050dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25051{
25052 while (per_cu)
25053 {
9068261f 25054 per_cu->cu->mark = false;
ae038cb0
DJ
25055 per_cu = per_cu->cu->read_in_chain;
25056 }
72bf9492
DJ
25057}
25058
72bf9492
DJ
25059/* Trivial hash function for partial_die_info: the hash value of a DIE
25060 is its offset in .debug_info for this objfile. */
25061
25062static hashval_t
25063partial_die_hash (const void *item)
25064{
9a3c8263
SM
25065 const struct partial_die_info *part_die
25066 = (const struct partial_die_info *) item;
9a619af0 25067
9c541725 25068 return to_underlying (part_die->sect_off);
72bf9492
DJ
25069}
25070
25071/* Trivial comparison function for partial_die_info structures: two DIEs
25072 are equal if they have the same offset. */
25073
25074static int
25075partial_die_eq (const void *item_lhs, const void *item_rhs)
25076{
9a3c8263
SM
25077 const struct partial_die_info *part_die_lhs
25078 = (const struct partial_die_info *) item_lhs;
25079 const struct partial_die_info *part_die_rhs
25080 = (const struct partial_die_info *) item_rhs;
9a619af0 25081
9c541725 25082 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25083}
25084
3c3bb058
AB
25085struct cmd_list_element *set_dwarf_cmdlist;
25086struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25087
25088static void
981a3fb3 25089set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25090{
b4f54984 25091 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25092 gdb_stdout);
ae038cb0
DJ
25093}
25094
25095static void
981a3fb3 25096show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25097{
b4f54984 25098 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25099}
25100
9291a0cd 25101static void
cd4fb1b2
SM
25102show_check_physname (struct ui_file *file, int from_tty,
25103 struct cmd_list_element *c, const char *value)
9291a0cd 25104{
cd4fb1b2
SM
25105 fprintf_filtered (file,
25106 _("Whether to check \"physname\" is %s.\n"),
25107 value);
9291a0cd
TT
25108}
25109
6c265988 25110void _initialize_dwarf2_read ();
cd4fb1b2 25111void
6c265988 25112_initialize_dwarf2_read ()
9291a0cd 25113{
cd4fb1b2
SM
25114 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25115Set DWARF specific variables.\n\
590042fc 25116Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25117 &set_dwarf_cmdlist, "maintenance set dwarf ",
25118 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25119
cd4fb1b2 25120 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25121Show DWARF specific variables.\n\
25122Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25123 &show_dwarf_cmdlist, "maintenance show dwarf ",
25124 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25125
cd4fb1b2
SM
25126 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25127 &dwarf_max_cache_age, _("\
25128Set the upper bound on the age of cached DWARF compilation units."), _("\
25129Show the upper bound on the age of cached DWARF compilation units."), _("\
25130A higher limit means that cached compilation units will be stored\n\
25131in memory longer, and more total memory will be used. Zero disables\n\
25132caching, which can slow down startup."),
25133 NULL,
25134 show_dwarf_max_cache_age,
25135 &set_dwarf_cmdlist,
25136 &show_dwarf_cmdlist);
156942c7 25137
cd4fb1b2
SM
25138 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25139Set debugging of the DWARF reader."), _("\
25140Show debugging of the DWARF reader."), _("\
25141When enabled (non-zero), debugging messages are printed during DWARF\n\
25142reading and symtab expansion. A value of 1 (one) provides basic\n\
25143information. A value greater than 1 provides more verbose information."),
25144 NULL,
25145 NULL,
25146 &setdebuglist, &showdebuglist);
9291a0cd 25147
cd4fb1b2
SM
25148 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25149Set debugging of the DWARF DIE reader."), _("\
25150Show debugging of the DWARF DIE reader."), _("\
25151When enabled (non-zero), DIEs are dumped after they are read in.\n\
25152The value is the maximum depth to print."),
25153 NULL,
25154 NULL,
25155 &setdebuglist, &showdebuglist);
9291a0cd 25156
cd4fb1b2
SM
25157 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25158Set debugging of the dwarf line reader."), _("\
25159Show debugging of the dwarf line reader."), _("\
25160When enabled (non-zero), line number entries are dumped as they are read in.\n\
25161A value of 1 (one) provides basic information.\n\
25162A value greater than 1 provides more verbose information."),
25163 NULL,
25164 NULL,
25165 &setdebuglist, &showdebuglist);
437afbb8 25166
cd4fb1b2
SM
25167 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25168Set cross-checking of \"physname\" code against demangler."), _("\
25169Show cross-checking of \"physname\" code against demangler."), _("\
25170When enabled, GDB's internal \"physname\" code is checked against\n\
25171the demangler."),
25172 NULL, show_check_physname,
25173 &setdebuglist, &showdebuglist);
900e11f9 25174
e615022a
DE
25175 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25176 no_class, &use_deprecated_index_sections, _("\
25177Set whether to use deprecated gdb_index sections."), _("\
25178Show whether to use deprecated gdb_index sections."), _("\
25179When enabled, deprecated .gdb_index sections are used anyway.\n\
25180Normally they are ignored either because of a missing feature or\n\
25181performance issue.\n\
25182Warning: This option must be enabled before gdb reads the file."),
25183 NULL,
25184 NULL,
25185 &setlist, &showlist);
25186
f1e6e072
TT
25187 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25188 &dwarf2_locexpr_funcs);
25189 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25190 &dwarf2_loclist_funcs);
25191
25192 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25193 &dwarf2_block_frame_base_locexpr_funcs);
25194 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25195 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25196
25197#if GDB_SELF_TEST
25198 selftests::register_test ("dw2_expand_symtabs_matching",
25199 selftests::dw2_expand_symtabs_matching::run_test);
25200#endif
6502dd73 25201}
This page took 6.74192 seconds and 4 git commands to generate.