gdb: add type::num_fields / type::set_num_fields
[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"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
9c6a1327
TT
1085struct variant_part_builder;
1086
1087/* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
2ddeaf8a
TT
1089
1090struct variant_field
1091{
9c6a1327
TT
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
2ddeaf8a
TT
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
9c6a1327
TT
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107};
1108
1109/* This represents a DW_TAG_variant_part. */
1110
1111struct variant_part_builder
1112{
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
2ddeaf8a
TT
1121};
1122
52059ffd
TT
1123struct nextfield
1124{
be2daae6
TT
1125 int accessibility = 0;
1126 int virtuality = 0;
9c6a1327
TT
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
be2daae6 1131 struct field field {};
52059ffd
TT
1132};
1133
1134struct fnfieldlist
1135{
be2daae6
TT
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
52059ffd
TT
1138};
1139
c906108c
SS
1140/* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143struct field_info
c5aa993b 1144 {
0963b4bd 1145 /* List of data member and baseclasses fields. */
be2daae6
TT
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
c906108c 1148
85102364 1149 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1150 int non_public_fields = 0;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
be2daae6 1155 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1159 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
be2daae6 1163 std::vector<struct decl_field> nested_types_list;
317f7127 1164
9c6a1327
TT
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
317f7127
TT
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
c5aa993b 1177 };
c906108c 1178
ae038cb0
DJ
1179/* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1184static int dwarf_max_cache_age = 5;
920d2a44 1185static void
b4f54984
DE
1186show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
920d2a44 1188{
3e43a32a 1189 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1190 "DWARF compilation units is %s.\n"),
920d2a44
AC
1191 value);
1192}
4390d890 1193\f
c906108c
SS
1194/* local function prototypes */
1195
918dd910
JK
1196static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
891813be 1199static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
f1902523
JK
1202static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
3e225074 1204 struct die_info *type_unit_die);
f1902523 1205
ed2dc618
SM
1206static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1208
72bf9492
DJ
1209static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1211 int, struct dwarf2_cu *);
c906108c 1212
72bf9492
DJ
1213static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
63d06c5c 1215
72bf9492
DJ
1216static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1218 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1219
5d7cb8df 1220static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1221 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1222 struct dwarf2_cu *cu);
1223
72bf9492
DJ
1224static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
91c24f0a 1226
bc30ff58
JB
1227static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1229 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1230
d521ce57 1231static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1232
dee91e82 1233static struct partial_die_info *load_partial_dies
d521ce57 1234 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1235
fb816e8b
TV
1236/* A pair of partial_die_info and compilation unit. */
1237struct cu_partial_die_info
1238{
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
122cf0f2
AB
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
405feb71 1247 { /* Nothing. */ }
122cf0f2
AB
1248
1249private:
1250 cu_partial_die_info () = delete;
fb816e8b
TV
1251};
1252
122cf0f2
AB
1253static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
72bf9492 1255
d521ce57
TT
1256static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1258 const gdb_byte *, bool *need_reprocess);
1259
1260static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1264
ed2dc618
SM
1265static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1268
ed2dc618
SM
1269static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1272
ed2dc618 1273static const char *read_indirect_string_at_offset
4f44ae6c 1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1275
d521ce57
TT
1276static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
3019eac3
DE
1278 unsigned int *);
1279
18a8505e
AT
1280static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
3019eac3 1285
e142c38c 1286static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1287
e142c38c
DJ
1288static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
c906108c 1290
7d45c7c3
KB
1291static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
a084a2a6
AT
1294static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
05cf31d1
JB
1296static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
e142c38c 1299static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1300
e142c38c 1301static struct die_info *die_specification (struct die_info *die,
f2f0e013 1302 struct dwarf2_cu **);
63d06c5c 1303
9c541725 1304static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1305 struct dwarf2_cu *cu);
debd256d 1306
f3f5162e 1307static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1308 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1309 CORE_ADDR, int decode_mapping);
c906108c 1310
804d2729
TT
1311static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
c906108c 1313
a14ed312 1314static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1315 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1316
ff39bb5e 1317static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1318 struct dwarf2_cu *);
c906108c 1319
ff39bb5e 1320static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
12df843f 1324 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1325 const gdb_byte **bytes,
98bfdba5 1326 struct dwarf2_locexpr_baton **baton);
2df3850c 1327
e7c27a73 1328static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1329
b4ba55a1
JB
1330static int need_gnat_info (struct dwarf2_cu *);
1331
3e43a32a
MS
1332static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
b4ba55a1
JB
1334
1335static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
e7c27a73
DJ
1338static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
c906108c 1340
ff39bb5e 1341static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1342 struct dwarf2_cu *);
c906108c 1343
f792889a 1344static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1345
673bfd45
DE
1346static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
0d5cff50 1348static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1349
6e70227d 1350static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
63d06c5c 1353
e7c27a73 1354static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1355
348e048f
DE
1356static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
e7c27a73 1358static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1359
e7c27a73 1360static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1361
96408a79
SA
1362static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
71a3c369
TT
1364static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
ff013f42 1366static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1367 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1368
41144253 1369/* Return the .debug_loclists section to use for cu. */
1370static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
3a2b436a 1372/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1373 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1374enum pc_bounds_kind
1375{
e385593e 1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1377 PC_BOUNDS_NOT_PRESENT,
1378
e385593e
JK
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
3a2b436a
JK
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388};
1389
1390static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
891813be 1393 dwarf2_psymtab *);
c906108c 1394
fae299cd
DC
1395static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
801e3a5b
JB
1399static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
a14ed312 1402static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
a14ed312 1405static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1406 struct type *, struct dwarf2_cu *);
c906108c 1407
a14ed312 1408static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1409 struct die_info *, struct type *,
e7c27a73 1410 struct dwarf2_cu *);
c906108c 1411
a14ed312 1412static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1413 struct type *,
1414 struct dwarf2_cu *);
c906108c 1415
134d01f1 1416static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1419
e7c27a73 1420static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1421
5d7cb8df
JK
1422static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
804d2729 1424static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1425
27aa8d6a
SW
1426static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
74921315
KS
1428static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
f55ee35c
JK
1430static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
38d518c9 1433static const char *namespace_name (struct die_info *die,
e142c38c 1434 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1435
134d01f1 1436static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
7d79de9a
TT
1438static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
c906108c 1440
6e70227d 1441static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1442 struct dwarf2_cu *);
1443
bf6af496 1444static struct die_info *read_die_and_siblings_1
d521ce57 1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1446 struct die_info *);
639d11d3 1447
dee91e82 1448static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
639d11d3
DC
1451 struct die_info *parent);
1452
d521ce57
TT
1453static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
3e225074 1455 int);
3019eac3 1456
d521ce57 1457static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1458 struct die_info **, const gdb_byte *);
93311388 1459
e7c27a73 1460static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1461
15d034d0 1462static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1463 struct objfile *);
71c25dea 1464
15d034d0 1465static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1466
15d034d0 1467static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
ca69b9e6
DE
1471static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
e142c38c 1474static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1475 struct dwarf2_cu **);
9219021c 1476
d97bc12b
DE
1477static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479static void dump_die_for_error (struct die_info *);
1480
1481static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
c906108c 1483
d97bc12b 1484/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1485
51545339 1486static void store_in_ref_table (struct die_info *,
10b3939b 1487 struct dwarf2_cu *);
c906108c 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
4390d890
DE
1680static void
1681dwarf2_debug_line_missing_file_complaint (void)
1682{
b98664d3 1683 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_end_sequence_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line "
4390d890
DE
1690 "program sequence without an end"));
1691}
1692
1693static void
1694dwarf2_complex_location_expr_complaint (void)
1695{
b98664d3 1696 complaint (_("location expression too complex"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702{
b98664d3 1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1704 arg1, arg2, arg3);
1705}
1706
4390d890
DE
1707static void
1708dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709{
b98664d3 1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1711 arg1, arg2);
1712}
527f3840
JK
1713
1714/* Hash function for line_header_hash. */
1715
1716static hashval_t
1717line_header_hash (const struct line_header *ofs)
1718{
9c541725 1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1720}
1721
1722/* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724static hashval_t
1725line_header_hash_voidp (const void *item)
1726{
9a3c8263 1727 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1728
1729 return line_header_hash (ofs);
1730}
1731
1732/* Equality function for line_header_hash. */
1733
1734static int
1735line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736{
9a3c8263
SM
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1739
9c541725 1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742}
1743
4390d890 1744\f
9291a0cd 1745
330cdd98
PA
1746/* See declaration. */
1747
1748dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
330cdd98
PA
1753{
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761}
1762
1763dwarf2_per_objfile::~dwarf2_per_objfile ()
1764{
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
b76e467d 1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1769 per_cu->imported_symtabs_free ();
fc8e7e75 1770
b2bdb8cf 1771 for (signatured_type *sig_type : all_type_units)
ae640021 1772 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1773
330cdd98
PA
1774 /* Everything else should be on the objfile obstack. */
1775}
1776
1777/* See declaration. */
1778
1779void
1780dwarf2_per_objfile::free_cached_comp_units ()
1781{
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
fcd3b13d 1788 delete per_cu->cu;
330cdd98
PA
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792}
1793
11ed8cad
TT
1794/* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797class free_cached_comp_units
1798{
1799public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816};
1817
c906108c 1818/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
c906108c
SS
1824
1825int
251d32d9 1826dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
c906108c 1829{
97cbe998
SDJ
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
ed2dc618
SM
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
5bfd760d 1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1838 names,
1839 can_copy);
5bfd760d 1840
73869dc2 1841 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1842 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1843 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1845}
1846
251d32d9
TG
1847/* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
233a11ab
CS
1849
1850static int
251d32d9
TG
1851section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
233a11ab 1853{
251d32d9
TG
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
233a11ab
CS
1861}
1862
330cdd98 1863/* See declaration. */
c906108c 1864
330cdd98
PA
1865void
1866dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
c906108c 1868{
fd361982 1869 flagword aflag = bfd_section_flags (sectp);
251d32d9 1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
950b7495
KS
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
330cdd98 1883 else if (section_is_p (sectp->name, &names.info))
c906108c 1884 {
330cdd98 1885 this->info.s.section = sectp;
fd361982 1886 this->info.size = bfd_section_size (sectp);
c906108c 1887 }
330cdd98 1888 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1889 {
330cdd98 1890 this->abbrev.s.section = sectp;
fd361982 1891 this->abbrev.size = bfd_section_size (sectp);
c906108c 1892 }
330cdd98 1893 else if (section_is_p (sectp->name, &names.line))
c906108c 1894 {
330cdd98 1895 this->line.s.section = sectp;
fd361982 1896 this->line.size = bfd_section_size (sectp);
c906108c 1897 }
330cdd98 1898 else if (section_is_p (sectp->name, &names.loc))
c906108c 1899 {
330cdd98 1900 this->loc.s.section = sectp;
fd361982 1901 this->loc.size = bfd_section_size (sectp);
c906108c 1902 }
330cdd98 1903 else if (section_is_p (sectp->name, &names.loclists))
43988095 1904 {
330cdd98 1905 this->loclists.s.section = sectp;
fd361982 1906 this->loclists.size = bfd_section_size (sectp);
43988095 1907 }
330cdd98 1908 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1909 {
330cdd98 1910 this->macinfo.s.section = sectp;
fd361982 1911 this->macinfo.size = bfd_section_size (sectp);
c906108c 1912 }
330cdd98 1913 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1914 {
330cdd98 1915 this->macro.s.section = sectp;
fd361982 1916 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1917 }
330cdd98 1918 else if (section_is_p (sectp->name, &names.str))
c906108c 1919 {
330cdd98 1920 this->str.s.section = sectp;
fd361982 1921 this->str.size = bfd_section_size (sectp);
c906108c 1922 }
18a8505e
AT
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
330cdd98 1928 else if (section_is_p (sectp->name, &names.line_str))
43988095 1929 {
330cdd98 1930 this->line_str.s.section = sectp;
fd361982 1931 this->line_str.size = bfd_section_size (sectp);
43988095 1932 }
330cdd98 1933 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1934 {
330cdd98 1935 this->addr.s.section = sectp;
fd361982 1936 this->addr.size = bfd_section_size (sectp);
3019eac3 1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1939 {
330cdd98 1940 this->frame.s.section = sectp;
fd361982 1941 this->frame.size = bfd_section_size (sectp);
b6af0555 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1944 {
330cdd98 1945 this->eh_frame.s.section = sectp;
fd361982 1946 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1949 {
330cdd98 1950 this->ranges.s.section = sectp;
fd361982 1951 this->ranges.size = bfd_section_size (sectp);
af34e669 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1954 {
330cdd98 1955 this->rnglists.s.section = sectp;
fd361982 1956 this->rnglists.size = bfd_section_size (sectp);
43988095 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.types))
348e048f 1959 {
8b70b953
TT
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
049412e3 1963 type_section.s.section = sectp;
fd361982 1964 type_section.size = bfd_section_size (sectp);
8b70b953 1965
fd5866f6 1966 this->types.push_back (type_section);
348e048f 1967 }
330cdd98 1968 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1969 {
330cdd98 1970 this->gdb_index.s.section = sectp;
fd361982 1971 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1972 }
927aa2e7
JK
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
fd361982 1976 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
fd361982 1981 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1982 }
dce234bc 1983
fd361982
AM
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
330cdd98 1986 this->has_section_at_zero = true;
c906108c
SS
1987}
1988
dce234bc 1989/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1990 SECTION_NAME. */
af34e669 1991
dce234bc 1992void
3017a003
TG
1993dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
d521ce57 1995 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1996 bfd_size_type *sizep)
1997{
5bfd760d 1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1999 struct dwarf2_section_info *info;
a3b2a86b
TT
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
3017a003
TG
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
dce234bc 2021
96b79293 2022 info->read (objfile);
dce234bc 2023
96b79293 2024 *sectp = info->get_bfd_section ();
dce234bc
PP
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027}
2028
36586728
TT
2029/* A helper function to find the sections for a .dwz file. */
2030
2031static void
2032locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033{
9a3c8263 2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
049412e3 2040 dwz_file->abbrev.s.section = sectp;
fd361982 2041 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
049412e3 2045 dwz_file->info.s.section = sectp;
fd361982 2046 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
049412e3 2050 dwz_file->str.s.section = sectp;
fd361982 2051 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
049412e3 2055 dwz_file->line.s.section = sectp;
fd361982 2056 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
049412e3 2060 dwz_file->macro.s.section = sectp;
fd361982 2061 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2062 }
2ec9a5e0
TT
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
049412e3 2065 dwz_file->gdb_index.s.section = sectp;
fd361982 2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2067 }
927aa2e7
JK
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
fd361982 2071 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2072 }
36586728
TT
2073}
2074
c4973306 2075/* See dwarf2read.h. */
36586728 2076
c4973306 2077struct dwz_file *
ed2dc618 2078dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2079{
36586728 2080 const char *filename;
acd13123 2081 bfd_size_type buildid_len_arg;
dc294be5
TT
2082 size_t buildid_len;
2083 bfd_byte *buildid;
36586728
TT
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2086 return dwarf2_per_objfile->dwz_file.get ();
36586728 2087
4db1a1dc 2088 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
4db1a1dc
TT
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
791afaa2
TT
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2101
acd13123
TT
2102 buildid_len = (size_t) buildid_len_arg;
2103
791afaa2 2104 filename = data.get ();
d721ba37
PA
2105
2106 std::string abs_storage;
36586728
TT
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
14278e1f
TT
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2111
14278e1f 2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2113 filename = abs_storage.c_str ();
36586728
TT
2114 }
2115
dc294be5
TT
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
ad80db5b 2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2119 if (dwz_bfd != NULL)
36586728 2120 {
192b62ce 2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2122 dwz_bfd.reset (nullptr);
36586728
TT
2123 }
2124
dc294be5
TT
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
0d79cdc4
AM
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
ad80db5b 2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
dc294be5
TT
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
7ff8cb8c
TT
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2157
7ff8cb8c
TT
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
36586728 2160
7ff8cb8c
TT
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
36586728 2165}
9291a0cd 2166\f
7b9f3c50
DE
2167/* DWARF quick_symbols_functions support. */
2168
2169/* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174struct quick_file_names
2175{
094b34ac
DE
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
7b9f3c50
DE
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189};
2190
2191/* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194struct dwarf2_per_cu_quick_data
2195{
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
43f3e411 2203 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212};
2213
094b34ac
DE
2214/* Utility hash function for a stmt_list_hash. */
2215
2216static hashval_t
2217hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218{
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2223 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2224 return v;
2225}
2226
2227/* Utility equality function for a stmt_list_hash. */
2228
2229static int
2230eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232{
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
9c541725 2239 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2240}
2241
7b9f3c50
DE
2242/* Hash function for a quick_file_names. */
2243
2244static hashval_t
2245hash_file_name_entry (const void *e)
2246{
9a3c8263
SM
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
7b9f3c50 2249
094b34ac 2250 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2251}
2252
2253/* Equality function for a quick_file_names. */
2254
2255static int
2256eq_file_name_entry (const void *a, const void *b)
2257{
9a3c8263
SM
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2260
094b34ac 2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2262}
2263
2264/* Delete function for a quick_file_names. */
2265
2266static void
2267delete_file_name_entry (void *e)
2268{
9a3c8263 2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281}
2282
2283/* Create a quick_file_names hash table. */
2284
5895093f 2285static htab_up
7b9f3c50
DE
2286create_quick_file_names_table (unsigned int nr_initial_entries)
2287{
5895093f
TT
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2291}
9291a0cd 2292
918dd910
JK
2293/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297static void
58f0c718 2298load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2299{
3019eac3 2300 if (per_cu->is_debug_types)
e5fe5e75 2301 load_full_type_unit (per_cu);
918dd910 2302 else
58f0c718 2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2304
cc12ce38
DE
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2dc860c0
DE
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2309}
2310
a0f42c21 2311/* Read in the symbols for PER_CU. */
2fdf6df6 2312
9291a0cd 2313static void
58f0c718 2314dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2315{
ed2dc618 2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2317
f4dc4d17
DE
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
197400e8 2320 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2321 return;
2322
b303c6f6
AB
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
39856def 2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2327
95554aad 2328 if (dwarf2_per_objfile->using_index
43f3e411 2329 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
58f0c718 2333 load_cu (per_cu, skip_partial);
89e63ee4
DE
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
cc12ce38 2339 && per_cu->cu != NULL
89e63ee4
DE
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
ed2dc618 2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2345 queue_and_load_all_dwo_tus (per_cu);
95554aad 2346 }
9291a0cd 2347
ed2dc618 2348 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
ed2dc618 2352 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
43f3e411 2359static struct compunit_symtab *
58f0c718 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2361{
ed2dc618
SM
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
95554aad 2364 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2365 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2366 {
11ed8cad 2367 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2368 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2370 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2371 }
f194fefb 2372
43f3e411 2373 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2374}
2375
ff4c9fec 2376/* See declaration. */
f4dc4d17 2377
ff4c9fec
SM
2378dwarf2_per_cu_data *
2379dwarf2_per_objfile::get_cutu (int index)
2380{
b76e467d 2381 if (index >= this->all_comp_units.size ())
ff4c9fec 2382 {
b76e467d 2383 index -= this->all_comp_units.size ();
b2bdb8cf 2384 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2385 return &this->all_type_units[index]->per_cu;
2386 }
f4dc4d17 2387
ff4c9fec
SM
2388 return this->all_comp_units[index];
2389}
f4dc4d17 2390
ff4c9fec 2391/* See declaration. */
2fdf6df6 2392
ff4c9fec
SM
2393dwarf2_per_cu_data *
2394dwarf2_per_objfile::get_cu (int index)
1fd400ff 2395{
b76e467d 2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2397
ff4c9fec 2398 return this->all_comp_units[index];
f4dc4d17
DE
2399}
2400
ff4c9fec 2401/* See declaration. */
f4dc4d17 2402
ff4c9fec
SM
2403signatured_type *
2404dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2405{
b2bdb8cf 2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2407
ff4c9fec 2408 return this->all_type_units[index];
1fd400ff
TT
2409}
2410
4b514bc8
JK
2411/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2412 objfile_obstack, and constructed with the specified field
2413 values. */
2414
2415static dwarf2_per_cu_data *
ed2dc618 2416create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2417 struct dwarf2_section_info *section,
2418 int is_dwz,
2419 sect_offset sect_off, ULONGEST length)
2420{
ed2dc618 2421 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2422 dwarf2_per_cu_data *the_cu
2423 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2424 struct dwarf2_per_cu_data);
2425 the_cu->sect_off = sect_off;
2426 the_cu->length = length;
e3b94546 2427 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2428 the_cu->section = section;
2429 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_quick_data);
2431 the_cu->is_dwz = is_dwz;
2432 return the_cu;
2433}
2434
2ec9a5e0
TT
2435/* A helper for create_cus_from_index that handles a given list of
2436 CUs. */
2fdf6df6 2437
74a0d9f6 2438static void
12359b5e 2439create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2440 const gdb_byte *cu_list, offset_type n_elements,
2441 struct dwarf2_section_info *section,
b76e467d 2442 int is_dwz)
9291a0cd 2443{
12359b5e 2444 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2445 {
74a0d9f6 2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2447
2448 sect_offset sect_off
2449 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2450 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2451 cu_list += 2 * 8;
2452
b76e467d 2453 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2454 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2455 sect_off, length);
b76e467d 2456 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2457 }
9291a0cd
TT
2458}
2459
2ec9a5e0 2460/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2461 the CU objects for this objfile. */
2ec9a5e0 2462
74a0d9f6 2463static void
12359b5e 2464create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467{
b76e467d
SM
2468 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2469 dwarf2_per_objfile->all_comp_units.reserve
2470 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2471
12359b5e 2472 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2473 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2474
2475 if (dwz_elements == 0)
74a0d9f6 2476 return;
2ec9a5e0 2477
12359b5e
SM
2478 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2479 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2480 &dwz->info, 1);
2ec9a5e0
TT
2481}
2482
1fd400ff 2483/* Create the signatured type hash table from the index. */
673bfd45 2484
74a0d9f6 2485static void
12359b5e
SM
2486create_signatured_type_table_from_index
2487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2488 struct dwarf2_section_info *section,
2489 const gdb_byte *bytes,
2490 offset_type elements)
1fd400ff 2491{
12359b5e 2492 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2493
b2bdb8cf
SM
2494 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2495 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2496
298e9637 2497 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2498
12359b5e 2499 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2500 {
52dc124a 2501 struct signatured_type *sig_type;
9c541725 2502 ULONGEST signature;
1fd400ff 2503 void **slot;
9c541725 2504 cu_offset type_offset_in_tu;
1fd400ff 2505
74a0d9f6 2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
52dc124a 2515 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2516 struct signatured_type);
52dc124a 2517 sig_type->signature = signature;
9c541725 2518 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2519 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2520 sig_type->per_cu.section = section;
9c541725 2521 sig_type->per_cu.sect_off = sect_off;
e3b94546 2522 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2523 sig_type->per_cu.v.quick
1fd400ff
TT
2524 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2525 struct dwarf2_per_cu_quick_data);
2526
b0b6a987 2527 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2528 *slot = sig_type;
1fd400ff 2529
b2bdb8cf 2530 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2531 }
2532
b0b6a987 2533 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2534}
2535
927aa2e7
JK
2536/* Create the signatured type hash table from .debug_names. */
2537
2538static void
2539create_signatured_type_table_from_debug_names
ed2dc618 2540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2541 const mapped_debug_names &map,
2542 struct dwarf2_section_info *section,
2543 struct dwarf2_section_info *abbrev_section)
2544{
ed2dc618
SM
2545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2546
96b79293
TT
2547 section->read (objfile);
2548 abbrev_section->read (objfile);
927aa2e7 2549
b2bdb8cf
SM
2550 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2551 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2552
298e9637 2553 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2554
2555 for (uint32_t i = 0; i < map.tu_count; ++i)
2556 {
2557 struct signatured_type *sig_type;
927aa2e7 2558 void **slot;
927aa2e7
JK
2559
2560 sect_offset sect_off
2561 = (sect_offset) (extract_unsigned_integer
2562 (map.tu_table_reordered + i * map.offset_size,
2563 map.offset_size,
2564 map.dwarf5_byte_order));
2565
2566 comp_unit_head cu_header;
ed2dc618
SM
2567 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2568 abbrev_section,
927aa2e7
JK
2569 section->buffer + to_underlying (sect_off),
2570 rcuh_kind::TYPE);
2571
2572 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct signatured_type);
2574 sig_type->signature = cu_header.signature;
2575 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2576 sig_type->per_cu.is_debug_types = 1;
2577 sig_type->per_cu.section = section;
2578 sig_type->per_cu.sect_off = sect_off;
e3b94546 2579 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
b0b6a987 2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2585 *slot = sig_type;
2586
b2bdb8cf 2587 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2588 }
2589
b0b6a987 2590 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2591}
2592
9291a0cd
TT
2593/* Read the address map data from the mapped index, and use it to
2594 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2595
9291a0cd 2596static void
ed2dc618
SM
2597create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 struct mapped_index *index)
9291a0cd 2599{
ed2dc618 2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2601 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2602 const gdb_byte *iter, *end;
9291a0cd 2603 struct addrmap *mutable_map;
9291a0cd
TT
2604 CORE_ADDR baseaddr;
2605
8268c778
PA
2606 auto_obstack temp_obstack;
2607
9291a0cd
TT
2608 mutable_map = addrmap_create_mutable (&temp_obstack);
2609
f00a2de2
PA
2610 iter = index->address_table.data ();
2611 end = iter + index->address_table.size ();
9291a0cd 2612
b3b3bada 2613 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2614
2615 while (iter < end)
2616 {
2617 ULONGEST hi, lo, cu_index;
2618 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2619 iter += 8;
2620 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2623 iter += 4;
f652bce2 2624
24a55014 2625 if (lo > hi)
f652bce2 2626 {
b98664d3 2627 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2628 hex_string (lo), hex_string (hi));
24a55014 2629 continue;
f652bce2 2630 }
24a55014 2631
b76e467d 2632 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2633 {
b98664d3 2634 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2635 (unsigned) cu_index);
24a55014 2636 continue;
f652bce2 2637 }
24a55014 2638
79748972
TT
2639 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2640 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2641 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2642 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2643 }
2644
d320c2b5 2645 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2646 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2647}
2648
927aa2e7
JK
2649/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2650 populate the objfile's psymtabs_addrmap. */
2651
2652static void
ed2dc618 2653create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2654 struct dwarf2_section_info *section)
2655{
ed2dc618 2656 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2657 bfd *abfd = objfile->obfd;
08feed99 2658 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2659 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2660
2661 auto_obstack temp_obstack;
2662 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 std::unordered_map<sect_offset,
2665 dwarf2_per_cu_data *,
2666 gdb::hash_enum<sect_offset>>
2667 debug_info_offset_to_per_cu;
b76e467d 2668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2669 {
927aa2e7
JK
2670 const auto insertpair
2671 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2672 if (!insertpair.second)
2673 {
2674 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2675 "debug_info_offset %s, ignoring .debug_aranges."),
2676 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2677 return;
2678 }
2679 }
2680
96b79293 2681 section->read (objfile);
927aa2e7
JK
2682
2683 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2684
2685 const gdb_byte *addr = section->buffer;
2686
2687 while (addr < section->buffer + section->size)
2688 {
2689 const gdb_byte *const entry_addr = addr;
2690 unsigned int bytes_read;
2691
2692 const LONGEST entry_length = read_initial_length (abfd, addr,
2693 &bytes_read);
2694 addr += bytes_read;
2695
2696 const gdb_byte *const entry_end = addr + entry_length;
2697 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2698 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2699 if (addr + entry_length > section->buffer + section->size)
2700 {
47e3f474 2701 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2702 "length %s exceeds section length %s, "
2703 "ignoring .debug_aranges."),
47e3f474
TV
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
927aa2e7
JK
2706 plongest (bytes_read + entry_length),
2707 pulongest (section->size));
2708 return;
2709 }
2710
2711 /* The version number. */
2712 const uint16_t version = read_2_bytes (abfd, addr);
2713 addr += 2;
2714 if (version != 2)
2715 {
47e3f474 2716 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2717 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2720 return;
2721 }
2722
2723 const uint64_t debug_info_offset
2724 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2725 addr += offset_size;
2726 const auto per_cu_it
2727 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2728 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2729 {
47e3f474 2730 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2731 "debug_info_offset %s does not exists, "
2732 "ignoring .debug_aranges."),
47e3f474
TV
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
927aa2e7
JK
2735 pulongest (debug_info_offset));
2736 return;
2737 }
2738 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2739
2740 const uint8_t address_size = *addr++;
2741 if (address_size < 1 || address_size > 8)
2742 {
47e3f474 2743 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2744 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2747 return;
2748 }
2749
2750 const uint8_t segment_selector_size = *addr++;
2751 if (segment_selector_size != 0)
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "segment_selector_size %u is not supported, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 segment_selector_size);
2759 return;
2760 }
2761
2762 /* Must pad to an alignment boundary that is twice the address
2763 size. It is undocumented by the DWARF standard but GCC does
2764 use it. */
2765 for (size_t padding = ((-(addr - section->buffer))
2766 & (2 * address_size - 1));
2767 padding > 0; padding--)
2768 if (*addr++ != 0)
2769 {
47e3f474 2770 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2771 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer));
927aa2e7
JK
2774 return;
2775 }
2776
2777 for (;;)
2778 {
2779 if (addr + 2 * address_size > entry_end)
2780 {
47e3f474 2781 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2782 "address list is not properly terminated, "
2783 "ignoring .debug_aranges."),
47e3f474
TV
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
927aa2e7
JK
2786 return;
2787 }
2788 ULONGEST start = extract_unsigned_integer (addr, address_size,
2789 dwarf5_byte_order);
2790 addr += address_size;
2791 ULONGEST length = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 if (start == 0 && length == 0)
2795 break;
2796 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2797 {
2798 /* Symbol was eliminated due to a COMDAT group. */
2799 continue;
2800 }
2801 ULONGEST end = start + length;
79748972
TT
2802 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2803 - baseaddr);
2804 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2805 - baseaddr);
927aa2e7
JK
2806 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2807 }
2808 }
2809
d320c2b5 2810 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2811 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2812}
2813
9291a0cd
TT
2814/* Find a slot in the mapped index INDEX for the object named NAME.
2815 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2816 constant pool and return true. If NAME cannot be found, return
2817 false. */
2fdf6df6 2818
109483d9 2819static bool
9291a0cd
TT
2820find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2821 offset_type **vec_out)
2822{
0cf03b49 2823 offset_type hash;
9291a0cd 2824 offset_type slot, step;
559a7a62 2825 int (*cmp) (const char *, const char *);
9291a0cd 2826
791afaa2 2827 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2828 if (current_language->la_language == language_cplus
45280282
IB
2829 || current_language->la_language == language_fortran
2830 || current_language->la_language == language_d)
0cf03b49
JK
2831 {
2832 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2833 not contain any. */
a8719064 2834
72998fb3 2835 if (strchr (name, '(') != NULL)
0cf03b49 2836 {
109483d9 2837 without_params = cp_remove_params (name);
0cf03b49 2838
72998fb3 2839 if (without_params != NULL)
791afaa2 2840 name = without_params.get ();
0cf03b49
JK
2841 }
2842 }
2843
559a7a62 2844 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2845 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2846 simulate our NAME being searched is also lowercased. */
2847 hash = mapped_index_string_hash ((index->version == 4
2848 && case_sensitivity == case_sensitive_off
2849 ? 5 : index->version),
2850 name);
2851
f00a2de2
PA
2852 slot = hash & (index->symbol_table.size () - 1);
2853 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2854 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2855
2856 for (;;)
2857 {
9291a0cd 2858 const char *str;
f00a2de2
PA
2859
2860 const auto &bucket = index->symbol_table[slot];
2861 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2862 return false;
9291a0cd 2863
f00a2de2 2864 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2865 if (!cmp (name, str))
9291a0cd
TT
2866 {
2867 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2868 + MAYBE_SWAP (bucket.vec));
109483d9 2869 return true;
9291a0cd
TT
2870 }
2871
f00a2de2 2872 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2873 }
2874}
2875
4485a1c1
SM
2876/* A helper function that reads the .gdb_index from BUFFER and fills
2877 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2878 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2879 ok to use deprecated sections.
2880
2881 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2882 out parameters that are filled in with information about the CU and
2883 TU lists in the section.
2884
4485a1c1 2885 Returns true if all went well, false otherwise. */
2fdf6df6 2886
d33bc52e 2887static bool
3810f182 2888read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896{
2897 const gdb_byte *addr = &buffer[0];
82430852 2898
9291a0cd 2899 /* Version check. */
4485a1c1 2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2902 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2903 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2904 indices. */
831adc1f 2905 if (version < 4)
481860b3
GB
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2911 filename);
481860b3
GB
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2ec9a5e0 2924 if (version < 6 && !deprecated_ok)
481860b3
GB
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
e615022a
DE
2929 warning (_("\
2930Skipping deprecated .gdb_index section in %s.\n\
2931Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932to use the section anyway."),
2ec9a5e0 2933 filename);
481860b3
GB
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
796a7ff8 2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2945
481860b3 2946 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2947 longer backward compatible. */
796a7ff8 2948 if (version > 8)
594e8718 2949 return 0;
9291a0cd 2950
559a7a62 2951 map->version = version;
9291a0cd 2952
4485a1c1 2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2954
4485a1c1 2955 int i = 0;
2ec9a5e0
TT
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
1fd400ff
TT
2959 ++i;
2960
2ec9a5e0
TT
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
987d643c 2965 ++i;
1fd400ff 2966
f00a2de2
PA
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2971 ++i;
2972
f00a2de2
PA
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2979
f00a2de2 2980 ++i;
f9d83a0b 2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2982
2ec9a5e0
TT
2983 return 1;
2984}
2985
4485a1c1
SM
2986/* Callback types for dwarf2_read_gdb_index. */
2987
2988typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
927aa2e7 2995/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998static int
4485a1c1
SM
2999dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3003{
2ec9a5e0
TT
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3006 struct dwz_file *dwz;
12359b5e 3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3008
4485a1c1
SM
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3063847f 3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3016 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
2ec9a5e0
TT
3021 return 0;
3022
0fefef59 3023 /* Don't use the index if it's empty. */
3063847f 3024 if (map->symbol_table.empty ())
0fefef59
DE
3025 return 0;
3026
2ec9a5e0
TT
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
ed2dc618 3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3030 if (dwz != NULL)
2ec9a5e0 3031 {
2ec9a5e0
TT
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
4485a1c1
SM
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3810f182 3042 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3043 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3044 &dwz_list, &dwz_list_elements,
3045 &dwz_types_ignore,
3046 &dwz_types_elements_ignore))
2ec9a5e0
TT
3047 {
3048 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3049 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3050 return 0;
3051 }
3052 }
3053
12359b5e
SM
3054 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3055 dwz_list, dwz_list_elements);
1fd400ff 3056
8b70b953
TT
3057 if (types_list_elements)
3058 {
8b70b953
TT
3059 /* We can only handle a single .debug_types when we have an
3060 index. */
fd5866f6 3061 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3062 return 0;
3063
fd5866f6 3064 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3065
12359b5e
SM
3066 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3067 types_list, types_list_elements);
8b70b953 3068 }
9291a0cd 3069
3063847f 3070 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3071
3063847f 3072 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3073 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3074 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3075 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3076
3077 return 1;
3078}
3079
dee91e82 3080/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3081
dee91e82
DE
3082static void
3083dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3084 const gdb_byte *info_ptr,
3e225074 3085 struct die_info *comp_unit_die)
9291a0cd 3086{
dee91e82 3087 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3088 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3089 struct dwarf2_per_objfile *dwarf2_per_objfile
3090 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3091 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3092 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3093 struct attribute *attr;
7b9f3c50
DE
3094 void **slot;
3095 struct quick_file_names *qfn;
9291a0cd 3096
0186c6a7
DE
3097 gdb_assert (! this_cu->is_debug_types);
3098
07261596
TT
3099 /* Our callers never want to match partial units -- instead they
3100 will match the enclosing full CU. */
3101 if (comp_unit_die->tag == DW_TAG_partial_unit)
3102 {
3103 this_cu->v.quick->no_file_data = 1;
3104 return;
3105 }
3106
0186c6a7 3107 lh_cu = this_cu;
7b9f3c50 3108 slot = NULL;
dee91e82 3109
fff8551c 3110 line_header_up lh;
9c541725 3111 sect_offset line_offset {};
fff8551c 3112
dee91e82 3113 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3114 if (attr != nullptr)
9291a0cd 3115 {
7b9f3c50
DE
3116 struct quick_file_names find_entry;
3117
9c541725 3118 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3119
3120 /* We may have already read in this line header (TU line header sharing).
3121 If we have we're done. */
094b34ac 3122 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3123 find_entry.hash.line_sect_off = line_offset;
5895093f 3124 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3125 &find_entry, INSERT);
3126 if (*slot != NULL)
3127 {
9a3c8263 3128 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3129 return;
7b9f3c50
DE
3130 }
3131
3019eac3 3132 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3133 }
3134 if (lh == NULL)
3135 {
094b34ac 3136 lh_cu->v.quick->no_file_data = 1;
dee91e82 3137 return;
9291a0cd
TT
3138 }
3139
8d749320 3140 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3141 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3142 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3143 gdb_assert (slot != NULL);
3144 *slot = qfn;
9291a0cd 3145
d721ba37 3146 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3147
aa391654
TT
3148 int offset = 0;
3149 if (strcmp (fnd.name, "<unknown>") != 0)
3150 ++offset;
3151
7ba99d21 3152 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3153 qfn->file_names =
aa391654
TT
3154 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3157 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
7b9f3c50 3160 qfn->real_names = NULL;
9291a0cd 3161
094b34ac 3162 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3163}
3164
3165/* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168static struct quick_file_names *
e4a48d9d 3169dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3170{
0186c6a7
DE
3171 /* This should never be called for TUs. */
3172 gdb_assert (! this_cu->is_debug_types);
3173 /* Nor type unit groups. */
197400e8 3174 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3175
dee91e82
DE
3176 if (this_cu->v.quick->file_names != NULL)
3177 return this_cu->v.quick->file_names;
3178 /* If we know there is no line data, no point in looking again. */
3179 if (this_cu->v.quick->no_file_data)
3180 return NULL;
3181
c0ab21c2
TT
3182 cutu_reader reader (this_cu);
3183 if (!reader.dummy_p)
3e225074 3184 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3185
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188 return this_cu->v.quick->file_names;
9291a0cd
TT
3189}
3190
3191/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3192 real path for a given file name from the line table. */
2fdf6df6 3193
9291a0cd 3194static const char *
7b9f3c50
DE
3195dw2_get_real_path (struct objfile *objfile,
3196 struct quick_file_names *qfn, int index)
9291a0cd 3197{
7b9f3c50
DE
3198 if (qfn->real_names == NULL)
3199 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3200 qfn->num_file_names, const char *);
9291a0cd 3201
7b9f3c50 3202 if (qfn->real_names[index] == NULL)
14278e1f 3203 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3204
7b9f3c50 3205 return qfn->real_names[index];
9291a0cd
TT
3206}
3207
3208static struct symtab *
3209dw2_find_last_source_symtab (struct objfile *objfile)
3210{
ed2dc618
SM
3211 struct dwarf2_per_objfile *dwarf2_per_objfile
3212 = get_dwarf2_per_objfile (objfile);
b76e467d 3213 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3215
43f3e411
DE
3216 if (cust == NULL)
3217 return NULL;
ed2dc618 3218
43f3e411 3219 return compunit_primary_filetab (cust);
9291a0cd
TT
3220}
3221
7b9f3c50
DE
3222/* Traversal function for dw2_forget_cached_source_info. */
3223
3224static int
3225dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3226{
7b9f3c50 3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3228
7b9f3c50 3229 if (file_data->real_names)
9291a0cd 3230 {
7b9f3c50 3231 int i;
9291a0cd 3232
7b9f3c50 3233 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3234 {
7b9f3c50
DE
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
9291a0cd
TT
3237 }
3238 }
7b9f3c50
DE
3239
3240 return 1;
3241}
3242
3243static void
3244dw2_forget_cached_source_info (struct objfile *objfile)
3245{
ed2dc618
SM
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3248
5895093f 3249 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3250 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3251}
3252
f8eba3c6
TT
3253/* Helper function for dw2_map_symtabs_matching_filename that expands
3254 the symtabs and calls the iterator. */
3255
3256static int
3257dw2_map_expand_apply (struct objfile *objfile,
3258 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3259 const char *name, const char *real_path,
14bc53a8 3260 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3261{
43f3e411 3262 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3263
3264 /* Don't visit already-expanded CUs. */
43f3e411 3265 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
58f0c718 3270 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3271
14bc53a8
PA
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
f8eba3c6
TT
3274}
3275
3276/* Implementation of the map_symtabs_matching_filename method. */
3277
14bc53a8
PA
3278static bool
3279dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3282{
c011a4f4 3283 const char *name_basename = lbasename (name);
ed2dc618
SM
3284 struct dwarf2_per_objfile *dwarf2_per_objfile
3285 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3286
848e3e78
DE
3287 /* The rule is CUs specify all the files, including those used by
3288 any TU, so there's no need to scan TUs here. */
f4dc4d17 3289
b76e467d 3290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3291 {
3d7bb9d9 3292 /* We only need to look at symtabs not already expanded. */
43f3e411 3293 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3294 continue;
3295
b76e467d 3296 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3297 if (file_data == NULL)
9291a0cd
TT
3298 continue;
3299
b76e467d 3300 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3301 {
7b9f3c50 3302 const char *this_name = file_data->file_names[j];
da235a7c 3303 const char *this_real_name;
9291a0cd 3304
af529f8f 3305 if (compare_filenames_for_search (this_name, name))
9291a0cd 3306 {
f5b95b50 3307 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3308 callback))
3309 return true;
288e77a7 3310 continue;
4aac40c8 3311 }
9291a0cd 3312
c011a4f4
DE
3313 /* Before we invoke realpath, which can get expensive when many
3314 files are involved, do a quick comparison of the basenames. */
3315 if (! basenames_may_differ
3316 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3317 continue;
3318
da235a7c
JK
3319 this_real_name = dw2_get_real_path (objfile, file_data, j);
3320 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3321 {
da235a7c 3322 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3323 callback))
3324 return true;
288e77a7 3325 continue;
da235a7c 3326 }
9291a0cd 3327
da235a7c
JK
3328 if (real_path != NULL)
3329 {
af529f8f
JK
3330 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3331 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3332 if (this_real_name != NULL
af529f8f 3333 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3334 {
f5b95b50 3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3336 callback))
3337 return true;
288e77a7 3338 continue;
9291a0cd
TT
3339 }
3340 }
3341 }
3342 }
3343
14bc53a8 3344 return false;
9291a0cd
TT
3345}
3346
da51c347
DE
3347/* Struct used to manage iterating over all CUs looking for a symbol. */
3348
3349struct dw2_symtab_iterator
9291a0cd 3350{
ed2dc618
SM
3351 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3352 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3353 /* If set, only look for symbols that match that block. Valid values are
3354 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3355 gdb::optional<block_enum> block_index;
da51c347
DE
3356 /* The kind of symbol we're looking for. */
3357 domain_enum domain;
3358 /* The list of CUs from the index entry of the symbol,
3359 or NULL if not found. */
3360 offset_type *vec;
3361 /* The next element in VEC to look at. */
3362 int next;
3363 /* The number of elements in VEC, or zero if there is no match. */
3364 int length;
8943b874
DE
3365 /* Have we seen a global version of the symbol?
3366 If so we can ignore all further global instances.
3367 This is to work around gold/15646, inefficient gold-generated
3368 indices. */
3369 int global_seen;
da51c347 3370};
9291a0cd 3371
2b79f376 3372/* Initialize the index symtab iterator ITER. */
2fdf6df6 3373
9291a0cd 3374static void
da51c347 3375dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3376 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3377 gdb::optional<block_enum> block_index,
da51c347
DE
3378 domain_enum domain,
3379 const char *name)
3380{
ed2dc618 3381 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3382 iter->block_index = block_index;
3383 iter->domain = domain;
3384 iter->next = 0;
8943b874 3385 iter->global_seen = 0;
da51c347 3386
3063847f 3387 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3388
3389 /* index is NULL if OBJF_READNOW. */
3390 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3391 iter->length = MAYBE_SWAP (*iter->vec);
3392 else
3393 {
3394 iter->vec = NULL;
3395 iter->length = 0;
3396 }
3397}
3398
3399/* Return the next matching CU or NULL if there are no more. */
3400
3401static struct dwarf2_per_cu_data *
3402dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3403{
ed2dc618
SM
3404 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3405
da51c347
DE
3406 for ( ; iter->next < iter->length; ++iter->next)
3407 {
3408 offset_type cu_index_and_attrs =
3409 MAYBE_SWAP (iter->vec[iter->next + 1]);
3410 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3411 gdb_index_symbol_kind symbol_kind =
3412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3413 /* Only check the symbol attributes if they're present.
3414 Indices prior to version 7 don't record them,
3415 and indices >= 7 may elide them for certain symbols
3416 (gold does this). */
3417 int attrs_valid =
ed2dc618 3418 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3420
3190f0c6 3421 /* Don't crash on bad data. */
b76e467d 3422 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3423 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3424 {
b98664d3 3425 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3426 " [in module %s]"),
3427 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3428 continue;
3429 }
3430
ff4c9fec 3431 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3432
da51c347 3433 /* Skip if already read in. */
43f3e411 3434 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3435 continue;
3436
8943b874
DE
3437 /* Check static vs global. */
3438 if (attrs_valid)
3439 {
2b79f376
SM
3440 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3441
3442 if (iter->block_index.has_value ())
3443 {
3444 bool want_static = *iter->block_index == STATIC_BLOCK;
3445
3446 if (is_static != want_static)
3447 continue;
3448 }
3449
8943b874
DE
3450 /* Work around gold/15646. */
3451 if (!is_static && iter->global_seen)
3452 continue;
3453 if (!is_static)
3454 iter->global_seen = 1;
3455 }
da51c347
DE
3456
3457 /* Only check the symbol's kind if it has one. */
3458 if (attrs_valid)
3459 {
3460 switch (iter->domain)
3461 {
3462 case VAR_DOMAIN:
3463 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3464 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3465 /* Some types are also in VAR_DOMAIN. */
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3467 continue;
3468 break;
3469 case STRUCT_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case LABEL_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3475 continue;
3476 break;
59c35742
AB
3477 case MODULE_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
da51c347
DE
3481 default:
3482 break;
3483 }
3484 }
3485
3486 ++iter->next;
3487 return per_cu;
3488 }
3489
3490 return NULL;
3491}
3492
43f3e411 3493static struct compunit_symtab *
c7f839cb 3494dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3495 const char *name, domain_enum domain)
9291a0cd 3496{
43f3e411 3497 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3498 struct dwarf2_per_objfile *dwarf2_per_objfile
3499 = get_dwarf2_per_objfile (objfile);
9291a0cd 3500
b5ec771e
PA
3501 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3502
ed2dc618
SM
3503 struct dw2_symtab_iterator iter;
3504 struct dwarf2_per_cu_data *per_cu;
da51c347 3505
2b79f376 3506 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3507
ed2dc618
SM
3508 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3509 {
3510 struct symbol *sym, *with_opaque = NULL;
58f0c718 3511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3514
ed2dc618
SM
3515 sym = block_find_symbol (block, name, domain,
3516 block_find_non_opaque_type_preferred,
3517 &with_opaque);
b2e2f908 3518
ed2dc618
SM
3519 /* Some caution must be observed with overloaded functions
3520 and methods, since the index will not contain any overload
3521 information (but NAME might contain it). */
da51c347 3522
ed2dc618
SM
3523 if (sym != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3525 return stab;
3526 if (with_opaque != NULL
3527 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3528 stab_best = stab;
da51c347 3529
ed2dc618 3530 /* Keep looking through other CUs. */
9291a0cd 3531 }
9291a0cd 3532
da51c347 3533 return stab_best;
9291a0cd
TT
3534}
3535
3536static void
3537dw2_print_stats (struct objfile *objfile)
3538{
ed2dc618
SM
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
b76e467d 3541 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3542 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3543 int count = 0;
9291a0cd 3544
ed2dc618 3545 for (int i = 0; i < total; ++i)
9291a0cd 3546 {
ff4c9fec 3547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3548
43f3e411 3549 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3550 ++count;
3551 }
e4a48d9d 3552 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3553 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3554}
3555
779bd270
DE
3556/* This dumps minimal information about the index.
3557 It is called via "mt print objfiles".
3558 One use is to verify .gdb_index has been loaded by the
3559 gdb.dwarf2/gdb-index.exp testcase. */
3560
9291a0cd
TT
3561static void
3562dw2_dump (struct objfile *objfile)
3563{
ed2dc618
SM
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
3566
779bd270
DE
3567 gdb_assert (dwarf2_per_objfile->using_index);
3568 printf_filtered (".gdb_index:");
3569 if (dwarf2_per_objfile->index_table != NULL)
3570 {
3571 printf_filtered (" version %d\n",
3572 dwarf2_per_objfile->index_table->version);
3573 }
3574 else
3575 printf_filtered (" faked for \"readnow\"\n");
3576 printf_filtered ("\n");
9291a0cd
TT
3577}
3578
9291a0cd
TT
3579static void
3580dw2_expand_symtabs_for_function (struct objfile *objfile,
3581 const char *func_name)
3582{
ed2dc618
SM
3583 struct dwarf2_per_objfile *dwarf2_per_objfile
3584 = get_dwarf2_per_objfile (objfile);
da51c347 3585
ed2dc618
SM
3586 struct dw2_symtab_iterator iter;
3587 struct dwarf2_per_cu_data *per_cu;
da51c347 3588
2b79f376 3589 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3590
ed2dc618 3591 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3592 dw2_instantiate_symtab (per_cu, false);
da51c347 3593
9291a0cd
TT
3594}
3595
3596static void
3597dw2_expand_all_symtabs (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
b76e467d 3601 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3602 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3603
ed2dc618 3604 for (int i = 0; i < total_units; ++i)
9291a0cd 3605 {
ff4c9fec 3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3607
58f0c718
TT
3608 /* We don't want to directly expand a partial CU, because if we
3609 read it with the wrong language, then assertion failures can
3610 be triggered later on. See PR symtab/23010. So, tell
3611 dw2_instantiate_symtab to skip partial CUs -- any important
3612 partial CU will be read via DW_TAG_imported_unit anyway. */
3613 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3614 }
3615}
3616
3617static void
652a8996
JK
3618dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3619 const char *fullname)
9291a0cd 3620{
ed2dc618
SM
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3623
3624 /* We don't need to consider type units here.
3625 This is only called for examining code, e.g. expand_line_sal.
3626 There can be an order of magnitude (or more) more type units
3627 than comp units, and we avoid them if we can. */
3628
b76e467d 3629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3630 {
3d7bb9d9 3631 /* We only need to look at symtabs not already expanded. */
43f3e411 3632 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3633 continue;
3634
b76e467d 3635 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3636 if (file_data == NULL)
9291a0cd
TT
3637 continue;
3638
b76e467d 3639 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3640 {
652a8996
JK
3641 const char *this_fullname = file_data->file_names[j];
3642
3643 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3644 {
58f0c718 3645 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3646 break;
3647 }
3648 }
3649 }
3650}
3651
9a0bacfb
TV
3652static void
3653dw2_expand_symtabs_matching_symbol
3654 (mapped_index_base &index,
3655 const lookup_name_info &lookup_name_in,
3656 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3657 enum search_domain kind,
3658 gdb::function_view<bool (offset_type)> match_callback);
3659
3660static void
3661dw2_expand_symtabs_matching_one
3662 (struct dwarf2_per_cu_data *per_cu,
3663 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3664 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3665
9291a0cd 3666static void
199b4314
TT
3667dw2_map_matching_symbols
3668 (struct objfile *objfile,
b054970d 3669 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3670 int global,
3671 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3672 symbol_compare_ftype *ordered_compare)
9291a0cd 3673{
1aa98955
TV
3674 /* Used for Ada. */
3675 struct dwarf2_per_objfile *dwarf2_per_objfile
3676 = get_dwarf2_per_objfile (objfile);
3677
9a0bacfb
TV
3678 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3679
1aa98955
TV
3680 if (dwarf2_per_objfile->index_table != nullptr)
3681 {
3682 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3683 here though if the current language is Ada for a non-Ada objfile
9a0bacfb
TV
3684 using GNU index. */
3685 mapped_index &index = *dwarf2_per_objfile->index_table;
1aa98955 3686
9a0bacfb
TV
3687 const char *match_name = name.ada ().lookup_name ().c_str ();
3688 auto matcher = [&] (const char *symname)
3689 {
3690 if (ordered_compare == nullptr)
3691 return true;
3692 return ordered_compare (symname, match_name) == 0;
3693 };
3694
3695 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3696 [&] (offset_type namei)
3697 {
3698 struct dw2_symtab_iterator iter;
3699 struct dwarf2_per_cu_data *per_cu;
3700
3701 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3702 match_name);
3703 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3704 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
3705 return true;
3706 });
3707 }
3708 else
3709 {
3710 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3711 proceed assuming all symtabs have been read in. */
3712 }
1aa98955
TV
3713
3714 for (compunit_symtab *cust : objfile->compunits ())
3715 {
3716 const struct block *block;
3717
3718 if (cust == NULL)
3719 continue;
3720 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3721 if (!iterate_over_symbols_terminated (block, name,
3722 domain, callback))
3723 return;
3724 }
9291a0cd
TT
3725}
3726
e1ef7d7a
PA
3727/* Starting from a search name, return the string that finds the upper
3728 bound of all strings that start with SEARCH_NAME in a sorted name
3729 list. Returns the empty string to indicate that the upper bound is
3730 the end of the list. */
3731
3732static std::string
3733make_sort_after_prefix_name (const char *search_name)
3734{
3735 /* When looking to complete "func", we find the upper bound of all
3736 symbols that start with "func" by looking for where we'd insert
3737 the closest string that would follow "func" in lexicographical
3738 order. Usually, that's "func"-with-last-character-incremented,
3739 i.e. "fund". Mind non-ASCII characters, though. Usually those
3740 will be UTF-8 multi-byte sequences, but we can't be certain.
3741 Especially mind the 0xff character, which is a valid character in
3742 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3743 rule out compilers allowing it in identifiers. Note that
3744 conveniently, strcmp/strcasecmp are specified to compare
3745 characters interpreted as unsigned char. So what we do is treat
3746 the whole string as a base 256 number composed of a sequence of
3747 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3748 to 0, and carries 1 to the following more-significant position.
3749 If the very first character in SEARCH_NAME ends up incremented
3750 and carries/overflows, then the upper bound is the end of the
3751 list. The string after the empty string is also the empty
3752 string.
3753
3754 Some examples of this operation:
3755
3756 SEARCH_NAME => "+1" RESULT
3757
3758 "abc" => "abd"
3759 "ab\xff" => "ac"
3760 "\xff" "a" "\xff" => "\xff" "b"
3761 "\xff" => ""
3762 "\xff\xff" => ""
3763 "" => ""
3764
3765 Then, with these symbols for example:
3766
3767 func
3768 func1
3769 fund
3770
3771 completing "func" looks for symbols between "func" and
3772 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3773 which finds "func" and "func1", but not "fund".
3774
3775 And with:
3776
3777 funcÿ (Latin1 'ÿ' [0xff])
3778 funcÿ1
3779 fund
3780
3781 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3782 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3783
3784 And with:
3785
3786 ÿÿ (Latin1 'ÿ' [0xff])
3787 ÿÿ1
3788
3789 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3790 the end of the list.
3791 */
3792 std::string after = search_name;
3793 while (!after.empty () && (unsigned char) after.back () == 0xff)
3794 after.pop_back ();
3795 if (!after.empty ())
3796 after.back () = (unsigned char) after.back () + 1;
3797 return after;
3798}
3799
5c58de74 3800/* See declaration. */
61d96d7e 3801
5c58de74
PA
3802std::pair<std::vector<name_component>::const_iterator,
3803 std::vector<name_component>::const_iterator>
44ed8f3e 3804mapped_index_base::find_name_components_bounds
3b00ef10 3805 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3806{
5c58de74
PA
3807 auto *name_cmp
3808 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3809
3b00ef10 3810 const char *lang_name
e0802d59 3811 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3812
3f563c84
PA
3813 /* Comparison function object for lower_bound that matches against a
3814 given symbol name. */
3815 auto lookup_compare_lower = [&] (const name_component &elem,
3816 const char *name)
3817 {
5c58de74 3818 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3819 const char *elem_name = elem_qualified + elem.name_offset;
3820 return name_cmp (elem_name, name) < 0;
3821 };
3822
3823 /* Comparison function object for upper_bound that matches against a
3824 given symbol name. */
3825 auto lookup_compare_upper = [&] (const char *name,
3826 const name_component &elem)
3827 {
5c58de74 3828 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3829 const char *elem_name = elem_qualified + elem.name_offset;
3830 return name_cmp (name, elem_name) < 0;
3831 };
3832
5c58de74
PA
3833 auto begin = this->name_components.begin ();
3834 auto end = this->name_components.end ();
3f563c84
PA
3835
3836 /* Find the lower bound. */
3837 auto lower = [&] ()
3838 {
3b00ef10 3839 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3840 return begin;
3841 else
3b00ef10 3842 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3843 } ();
3844
3845 /* Find the upper bound. */
3846 auto upper = [&] ()
3847 {
5c58de74 3848 if (lookup_name_without_params.completion_mode ())
3f563c84 3849 {
e1ef7d7a
PA
3850 /* In completion mode, we want UPPER to point past all
3851 symbols names that have the same prefix. I.e., with
3852 these symbols, and completing "func":
3853
3854 function << lower bound
3855 function1
3856 other_function << upper bound
3857
3858 We find the upper bound by looking for the insertion
3859 point of "func"-with-last-character-incremented,
3860 i.e. "fund". */
3b00ef10 3861 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3862 if (after.empty ())
3f563c84 3863 return end;
e6b2f5ef
PA
3864 return std::lower_bound (lower, end, after.c_str (),
3865 lookup_compare_lower);
3f563c84
PA
3866 }
3867 else
3b00ef10 3868 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3869 } ();
3870
5c58de74
PA
3871 return {lower, upper};
3872}
3873
3874/* See declaration. */
3875
3876void
44ed8f3e 3877mapped_index_base::build_name_components ()
5c58de74
PA
3878{
3879 if (!this->name_components.empty ())
3880 return;
3881
3882 this->name_components_casing = case_sensitivity;
3883 auto *name_cmp
3884 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3885
3886 /* The code below only knows how to break apart components of C++
3887 symbol names (and other languages that use '::' as
3b00ef10 3888 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3889 auto count = this->symbol_name_count ();
3890 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3891 {
44ed8f3e 3892 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3893 continue;
3894
3895 const char *name = this->symbol_name_at (idx);
3896
3897 /* Add each name component to the name component table. */
3898 unsigned int previous_len = 0;
3b00ef10
TT
3899
3900 if (strstr (name, "::") != nullptr)
3901 {
3902 for (unsigned int current_len = cp_find_first_component (name);
3903 name[current_len] != '\0';
3904 current_len += cp_find_first_component (name + current_len))
3905 {
3906 gdb_assert (name[current_len] == ':');
3907 this->name_components.push_back ({previous_len, idx});
3908 /* Skip the '::'. */
3909 current_len += 2;
3910 previous_len = current_len;
3911 }
3912 }
3913 else
5c58de74 3914 {
3b00ef10
TT
3915 /* Handle the Ada encoded (aka mangled) form here. */
3916 for (const char *iter = strstr (name, "__");
3917 iter != nullptr;
3918 iter = strstr (iter, "__"))
3919 {
3920 this->name_components.push_back ({previous_len, idx});
3921 iter += 2;
3922 previous_len = iter - name;
3923 }
5c58de74 3924 }
3b00ef10 3925
5c58de74
PA
3926 this->name_components.push_back ({previous_len, idx});
3927 }
3928
3929 /* Sort name_components elements by name. */
3930 auto name_comp_compare = [&] (const name_component &left,
3931 const name_component &right)
3932 {
3933 const char *left_qualified = this->symbol_name_at (left.idx);
3934 const char *right_qualified = this->symbol_name_at (right.idx);
3935
3936 const char *left_name = left_qualified + left.name_offset;
3937 const char *right_name = right_qualified + right.name_offset;
3938
3939 return name_cmp (left_name, right_name) < 0;
3940 };
3941
3942 std::sort (this->name_components.begin (),
3943 this->name_components.end (),
3944 name_comp_compare);
3945}
3946
3947/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3948 mapped_index_base instead of the containing objfile. This is split
3949 to a separate function in order to be able to unit test the
3950 name_components matching using a mock mapped_index_base. For each
5c58de74 3951 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3952 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3953
3954static void
3955dw2_expand_symtabs_matching_symbol
44ed8f3e 3956 (mapped_index_base &index,
5c58de74
PA
3957 const lookup_name_info &lookup_name_in,
3958 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3959 enum search_domain kind,
3b00ef10 3960 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3961{
3962 lookup_name_info lookup_name_without_params
3963 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3964
3965 /* Build the symbol name component sorted vector, if we haven't
3966 yet. */
3967 index.build_name_components ();
3968
3f563c84
PA
3969 /* The same symbol may appear more than once in the range though.
3970 E.g., if we're looking for symbols that complete "w", and we have
3971 a symbol named "w1::w2", we'll find the two name components for
3972 that same symbol in the range. To be sure we only call the
3973 callback once per symbol, we first collect the symbol name
3974 indexes that matched in a temporary vector and ignore
3975 duplicates. */
3976 std::vector<offset_type> matches;
3f563c84 3977
3b00ef10
TT
3978 struct name_and_matcher
3979 {
3980 symbol_name_matcher_ftype *matcher;
ecc6c606 3981 const char *name;
3b00ef10
TT
3982
3983 bool operator== (const name_and_matcher &other) const
3f563c84 3984 {
ecc6c606 3985 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3986 }
3987 };
3988
3989 /* A vector holding all the different symbol name matchers, for all
3990 languages. */
3991 std::vector<name_and_matcher> matchers;
3992
3993 for (int i = 0; i < nr_languages; i++)
3994 {
3995 enum language lang_e = (enum language) i;
3996
3997 const language_defn *lang = language_def (lang_e);
3998 symbol_name_matcher_ftype *name_matcher
3999 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4000
3b00ef10
TT
4001 name_and_matcher key {
4002 name_matcher,
4003 lookup_name_without_params.language_lookup_name (lang_e)
4004 };
4005
4006 /* Don't insert the same comparison routine more than once.
4007 Note that we do this linear walk. This is not a problem in
4008 practice because the number of supported languages is
4009 low. */
4010 if (std::find (matchers.begin (), matchers.end (), key)
4011 != matchers.end ())
9291a0cd 4012 continue;
3b00ef10
TT
4013 matchers.push_back (std::move (key));
4014
4015 auto bounds
4016 = index.find_name_components_bounds (lookup_name_without_params,
4017 lang_e);
4018
4019 /* Now for each symbol name in range, check to see if we have a name
4020 match, and if so, call the MATCH_CALLBACK callback. */
4021
4022 for (; bounds.first != bounds.second; ++bounds.first)
4023 {
4024 const char *qualified = index.symbol_name_at (bounds.first->idx);
4025
4026 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4027 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4028 continue;
9291a0cd 4029
3b00ef10
TT
4030 matches.push_back (bounds.first->idx);
4031 }
3f563c84
PA
4032 }
4033
4034 std::sort (matches.begin (), matches.end ());
4035
4036 /* Finally call the callback, once per match. */
4037 ULONGEST prev = -1;
4038 for (offset_type idx : matches)
4039 {
4040 if (prev != idx)
4041 {
3b00ef10
TT
4042 if (!match_callback (idx))
4043 break;
3f563c84
PA
4044 prev = idx;
4045 }
4046 }
4047
4048 /* Above we use a type wider than idx's for 'prev', since 0 and
4049 (offset_type)-1 are both possible values. */
4050 static_assert (sizeof (prev) > sizeof (offset_type), "");
4051}
4052
c62446b1
PA
4053#if GDB_SELF_TEST
4054
4055namespace selftests { namespace dw2_expand_symtabs_matching {
4056
a3c5fafd
PA
4057/* A mock .gdb_index/.debug_names-like name index table, enough to
4058 exercise dw2_expand_symtabs_matching_symbol, which works with the
4059 mapped_index_base interface. Builds an index from the symbol list
4060 passed as parameter to the constructor. */
4061class mock_mapped_index : public mapped_index_base
c62446b1
PA
4062{
4063public:
a3c5fafd
PA
4064 mock_mapped_index (gdb::array_view<const char *> symbols)
4065 : m_symbol_table (symbols)
c62446b1
PA
4066 {}
4067
a3c5fafd 4068 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4069
a3c5fafd 4070 /* Return the number of names in the symbol table. */
632e107b 4071 size_t symbol_name_count () const override
c62446b1 4072 {
a3c5fafd 4073 return m_symbol_table.size ();
c62446b1
PA
4074 }
4075
a3c5fafd 4076 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4077 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4078 {
4079 return m_symbol_table[idx];
4080 }
c62446b1 4081
a3c5fafd
PA
4082private:
4083 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4084};
4085
4086/* Convenience function that converts a NULL pointer to a "<null>"
4087 string, to pass to print routines. */
4088
4089static const char *
4090string_or_null (const char *str)
4091{
4092 return str != NULL ? str : "<null>";
4093}
4094
4095/* Check if a lookup_name_info built from
4096 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4097 index. EXPECTED_LIST is the list of expected matches, in expected
4098 matching order. If no match expected, then an empty list is
4099 specified. Returns true on success. On failure prints a warning
4100 indicating the file:line that failed, and returns false. */
4101
4102static bool
4103check_match (const char *file, int line,
4104 mock_mapped_index &mock_index,
4105 const char *name, symbol_name_match_type match_type,
4106 bool completion_mode,
4107 std::initializer_list<const char *> expected_list)
4108{
4109 lookup_name_info lookup_name (name, match_type, completion_mode);
4110
4111 bool matched = true;
4112
4113 auto mismatch = [&] (const char *expected_str,
4114 const char *got)
4115 {
4116 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4117 "expected=\"%s\", got=\"%s\"\n"),
4118 file, line,
4119 (match_type == symbol_name_match_type::FULL
4120 ? "FULL" : "WILD"),
4121 name, string_or_null (expected_str), string_or_null (got));
4122 matched = false;
4123 };
4124
4125 auto expected_it = expected_list.begin ();
4126 auto expected_end = expected_list.end ();
4127
a3c5fafd 4128 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4129 NULL, ALL_DOMAIN,
4130 [&] (offset_type idx)
4131 {
a3c5fafd 4132 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4133 const char *expected_str
4134 = expected_it == expected_end ? NULL : *expected_it++;
4135
4136 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4137 mismatch (expected_str, matched_name);
3b00ef10 4138 return true;
c62446b1
PA
4139 });
4140
4141 const char *expected_str
4142 = expected_it == expected_end ? NULL : *expected_it++;
4143 if (expected_str != NULL)
4144 mismatch (expected_str, NULL);
4145
4146 return matched;
4147}
4148
4149/* The symbols added to the mock mapped_index for testing (in
4150 canonical form). */
4151static const char *test_symbols[] = {
4152 "function",
4153 "std::bar",
4154 "std::zfunction",
4155 "std::zfunction2",
4156 "w1::w2",
4157 "ns::foo<char*>",
4158 "ns::foo<int>",
4159 "ns::foo<long>",
a20714ff
PA
4160 "ns2::tmpl<int>::foo2",
4161 "(anonymous namespace)::A::B::C",
c62446b1 4162
e1ef7d7a
PA
4163 /* These are used to check that the increment-last-char in the
4164 matching algorithm for completion doesn't match "t1_fund" when
4165 completing "t1_func". */
4166 "t1_func",
4167 "t1_func1",
4168 "t1_fund",
4169 "t1_fund1",
4170
4171 /* A UTF-8 name with multi-byte sequences to make sure that
4172 cp-name-parser understands this as a single identifier ("função"
4173 is "function" in PT). */
4174 u8"u8função",
4175
4176 /* \377 (0xff) is Latin1 'ÿ'. */
4177 "yfunc\377",
4178
4179 /* \377 (0xff) is Latin1 'ÿ'. */
4180 "\377",
4181 "\377\377123",
4182
c62446b1
PA
4183 /* A name with all sorts of complications. Starts with "z" to make
4184 it easier for the completion tests below. */
4185#define Z_SYM_NAME \
4186 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4187 "::tuple<(anonymous namespace)::ui*, " \
4188 "std::default_delete<(anonymous namespace)::ui>, void>"
4189
4190 Z_SYM_NAME
4191};
4192
a3c5fafd
PA
4193/* Returns true if the mapped_index_base::find_name_component_bounds
4194 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4195 in completion mode. */
5c58de74
PA
4196
4197static bool
a3c5fafd 4198check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4199 const char *search_name,
4200 gdb::array_view<const char *> expected_syms)
4201{
4202 lookup_name_info lookup_name (search_name,
4203 symbol_name_match_type::FULL, true);
4204
3b00ef10
TT
4205 auto bounds = index.find_name_components_bounds (lookup_name,
4206 language_cplus);
5c58de74
PA
4207
4208 size_t distance = std::distance (bounds.first, bounds.second);
4209 if (distance != expected_syms.size ())
4210 return false;
4211
4212 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4213 {
4214 auto nc_elem = bounds.first + exp_elem;
4215 const char *qualified = index.symbol_name_at (nc_elem->idx);
4216 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4217 return false;
4218 }
4219
4220 return true;
4221}
4222
4223/* Test the lower-level mapped_index::find_name_component_bounds
4224 method. */
4225
c62446b1 4226static void
5c58de74
PA
4227test_mapped_index_find_name_component_bounds ()
4228{
4229 mock_mapped_index mock_index (test_symbols);
4230
a3c5fafd 4231 mock_index.build_name_components ();
5c58de74
PA
4232
4233 /* Test the lower-level mapped_index::find_name_component_bounds
4234 method in completion mode. */
4235 {
4236 static const char *expected_syms[] = {
4237 "t1_func",
4238 "t1_func1",
5c58de74
PA
4239 };
4240
a3c5fafd 4241 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4242 "t1_func", expected_syms));
4243 }
4244
4245 /* Check that the increment-last-char in the name matching algorithm
4246 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4247 {
4248 static const char *expected_syms1[] = {
4249 "\377",
4250 "\377\377123",
4251 };
a3c5fafd 4252 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4253 "\377", expected_syms1));
4254
4255 static const char *expected_syms2[] = {
4256 "\377\377123",
4257 };
a3c5fafd 4258 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4259 "\377\377", expected_syms2));
4260 }
4261}
4262
4263/* Test dw2_expand_symtabs_matching_symbol. */
4264
4265static void
4266test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4267{
4268 mock_mapped_index mock_index (test_symbols);
4269
4270 /* We let all tests run until the end even if some fails, for debug
4271 convenience. */
4272 bool any_mismatch = false;
4273
4274 /* Create the expected symbols list (an initializer_list). Needed
4275 because lists have commas, and we need to pass them to CHECK,
4276 which is a macro. */
4277#define EXPECT(...) { __VA_ARGS__ }
4278
4279 /* Wrapper for check_match that passes down the current
4280 __FILE__/__LINE__. */
4281#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4282 any_mismatch |= !check_match (__FILE__, __LINE__, \
4283 mock_index, \
4284 NAME, MATCH_TYPE, COMPLETION_MODE, \
4285 EXPECTED_LIST)
4286
4287 /* Identity checks. */
4288 for (const char *sym : test_symbols)
4289 {
4290 /* Should be able to match all existing symbols. */
4291 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4292 EXPECT (sym));
4293
4294 /* Should be able to match all existing symbols with
4295 parameters. */
4296 std::string with_params = std::string (sym) + "(int)";
4297 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4298 EXPECT (sym));
4299
4300 /* Should be able to match all existing symbols with
4301 parameters and qualifiers. */
4302 with_params = std::string (sym) + " ( int ) const";
4303 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4304 EXPECT (sym));
4305
4306 /* This should really find sym, but cp-name-parser.y doesn't
4307 know about lvalue/rvalue qualifiers yet. */
4308 with_params = std::string (sym) + " ( int ) &&";
4309 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4310 {});
4311 }
4312
e1ef7d7a
PA
4313 /* Check that the name matching algorithm for completion doesn't get
4314 confused with Latin1 'ÿ' / 0xff. */
4315 {
4316 static const char str[] = "\377";
4317 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4318 EXPECT ("\377", "\377\377123"));
4319 }
4320
4321 /* Check that the increment-last-char in the matching algorithm for
4322 completion doesn't match "t1_fund" when completing "t1_func". */
4323 {
4324 static const char str[] = "t1_func";
4325 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4326 EXPECT ("t1_func", "t1_func1"));
4327 }
4328
c62446b1
PA
4329 /* Check that completion mode works at each prefix of the expected
4330 symbol name. */
4331 {
4332 static const char str[] = "function(int)";
4333 size_t len = strlen (str);
4334 std::string lookup;
4335
4336 for (size_t i = 1; i < len; i++)
4337 {
4338 lookup.assign (str, i);
4339 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4340 EXPECT ("function"));
4341 }
4342 }
4343
4344 /* While "w" is a prefix of both components, the match function
4345 should still only be called once. */
4346 {
4347 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4348 EXPECT ("w1::w2"));
a20714ff
PA
4349 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4350 EXPECT ("w1::w2"));
c62446b1
PA
4351 }
4352
4353 /* Same, with a "complicated" symbol. */
4354 {
4355 static const char str[] = Z_SYM_NAME;
4356 size_t len = strlen (str);
4357 std::string lookup;
4358
4359 for (size_t i = 1; i < len; i++)
4360 {
4361 lookup.assign (str, i);
4362 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4363 EXPECT (Z_SYM_NAME));
4364 }
4365 }
4366
4367 /* In FULL mode, an incomplete symbol doesn't match. */
4368 {
4369 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4370 {});
4371 }
4372
4373 /* A complete symbol with parameters matches any overload, since the
4374 index has no overload info. */
4375 {
4376 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4377 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4378 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4379 EXPECT ("std::zfunction", "std::zfunction2"));
4380 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4381 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4382 }
4383
4384 /* Check that whitespace is ignored appropriately. A symbol with a
4385 template argument list. */
4386 {
4387 static const char expected[] = "ns::foo<int>";
4388 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4389 EXPECT (expected));
a20714ff
PA
4390 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4391 EXPECT (expected));
c62446b1
PA
4392 }
4393
4394 /* Check that whitespace is ignored appropriately. A symbol with a
4395 template argument list that includes a pointer. */
4396 {
4397 static const char expected[] = "ns::foo<char*>";
4398 /* Try both completion and non-completion modes. */
4399 static const bool completion_mode[2] = {false, true};
4400 for (size_t i = 0; i < 2; i++)
4401 {
4402 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4403 completion_mode[i], EXPECT (expected));
a20714ff
PA
4404 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4405 completion_mode[i], EXPECT (expected));
c62446b1
PA
4406
4407 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4408 completion_mode[i], EXPECT (expected));
a20714ff
PA
4409 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4410 completion_mode[i], EXPECT (expected));
c62446b1
PA
4411 }
4412 }
4413
4414 {
4415 /* Check method qualifiers are ignored. */
4416 static const char expected[] = "ns::foo<char*>";
4417 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4418 symbol_name_match_type::FULL, true, EXPECT (expected));
4419 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4420 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4421 CHECK_MATCH ("foo < char * > ( int ) const",
4422 symbol_name_match_type::WILD, true, EXPECT (expected));
4423 CHECK_MATCH ("foo < char * > ( int ) &&",
4424 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4425 }
4426
4427 /* Test lookup names that don't match anything. */
4428 {
a20714ff
PA
4429 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4430 {});
4431
c62446b1
PA
4432 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
a20714ff
PA
4436 /* Some wild matching tests, exercising "(anonymous namespace)",
4437 which should not be confused with a parameter list. */
4438 {
4439 static const char *syms[] = {
4440 "A::B::C",
4441 "B::C",
4442 "C",
4443 "A :: B :: C ( int )",
4444 "B :: C ( int )",
4445 "C ( int )",
4446 };
4447
4448 for (const char *s : syms)
4449 {
4450 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4451 EXPECT ("(anonymous namespace)::A::B::C"));
4452 }
4453 }
4454
4455 {
4456 static const char expected[] = "ns2::tmpl<int>::foo2";
4457 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4458 EXPECT (expected));
4459 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4460 EXPECT (expected));
4461 }
4462
c62446b1
PA
4463 SELF_CHECK (!any_mismatch);
4464
4465#undef EXPECT
4466#undef CHECK_MATCH
4467}
4468
5c58de74
PA
4469static void
4470run_test ()
4471{
4472 test_mapped_index_find_name_component_bounds ();
4473 test_dw2_expand_symtabs_matching_symbol ();
4474}
4475
c62446b1
PA
4476}} // namespace selftests::dw2_expand_symtabs_matching
4477
4478#endif /* GDB_SELF_TEST */
4479
4b514bc8
JK
4480/* If FILE_MATCHER is NULL or if PER_CU has
4481 dwarf2_per_cu_quick_data::MARK set (see
4482 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4483 EXPANSION_NOTIFY on it. */
4484
4485static void
4486dw2_expand_symtabs_matching_one
4487 (struct dwarf2_per_cu_data *per_cu,
4488 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4489 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4490{
4491 if (file_matcher == NULL || per_cu->v.quick->mark)
4492 {
4493 bool symtab_was_null
4494 = (per_cu->v.quick->compunit_symtab == NULL);
4495
58f0c718 4496 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4497
4498 if (expansion_notify != NULL
4499 && symtab_was_null
4500 && per_cu->v.quick->compunit_symtab != NULL)
4501 expansion_notify (per_cu->v.quick->compunit_symtab);
4502 }
4503}
4504
3f563c84
PA
4505/* Helper for dw2_expand_matching symtabs. Called on each symbol
4506 matched, to expand corresponding CUs that were marked. IDX is the
4507 index of the symbol name that matched. */
4508
4509static void
4510dw2_expand_marked_cus
ed2dc618 4511 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4512 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4513 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4514 search_domain kind)
4515{
3f563c84
PA
4516 offset_type *vec, vec_len, vec_idx;
4517 bool global_seen = false;
ed2dc618 4518 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4519
61920122 4520 vec = (offset_type *) (index.constant_pool
f00a2de2 4521 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4522 vec_len = MAYBE_SWAP (vec[0]);
4523 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4524 {
61920122
PA
4525 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4526 /* This value is only valid for index versions >= 7. */
4527 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4528 gdb_index_symbol_kind symbol_kind =
4529 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4530 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4531 /* Only check the symbol attributes if they're present.
4532 Indices prior to version 7 don't record them,
4533 and indices >= 7 may elide them for certain symbols
4534 (gold does this). */
4535 int attrs_valid =
4536 (index.version >= 7
4537 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4538
4539 /* Work around gold/15646. */
4540 if (attrs_valid)
9291a0cd 4541 {
61920122
PA
4542 if (!is_static && global_seen)
4543 continue;
4544 if (!is_static)
4545 global_seen = true;
4546 }
3190f0c6 4547
61920122
PA
4548 /* Only check the symbol's kind if it has one. */
4549 if (attrs_valid)
4550 {
4551 switch (kind)
8943b874 4552 {
61920122
PA
4553 case VARIABLES_DOMAIN:
4554 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4555 continue;
4556 break;
4557 case FUNCTIONS_DOMAIN:
4558 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4559 continue;
61920122
PA
4560 break;
4561 case TYPES_DOMAIN:
4562 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4563 continue;
4564 break;
59c35742
AB
4565 case MODULES_DOMAIN:
4566 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4567 continue;
4568 break;
61920122
PA
4569 default:
4570 break;
8943b874 4571 }
61920122 4572 }
8943b874 4573
61920122 4574 /* Don't crash on bad data. */
b76e467d 4575 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4576 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4577 {
b98664d3 4578 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4579 " [in module %s]"),
4580 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4581 continue;
4582 }
4583
ff4c9fec 4584 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4585 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4586 expansion_notify);
61920122
PA
4587 }
4588}
4589
4b514bc8
JK
4590/* If FILE_MATCHER is non-NULL, set all the
4591 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4592 that match FILE_MATCHER. */
4593
61920122 4594static void
4b514bc8 4595dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4596 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4597 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4598{
4b514bc8 4599 if (file_matcher == NULL)
61920122
PA
4600 return;
4601
4b514bc8
JK
4602 objfile *const objfile = dwarf2_per_objfile->objfile;
4603
4604 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4605 htab_eq_pointer,
4606 NULL, xcalloc, xfree));
4607 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4608 htab_eq_pointer,
4609 NULL, xcalloc, xfree));
61920122 4610
4b514bc8
JK
4611 /* The rule is CUs specify all the files, including those used by
4612 any TU, so there's no need to scan TUs here. */
61920122 4613
b76e467d 4614 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4615 {
927aa2e7
JK
4616 QUIT;
4617
4618 per_cu->v.quick->mark = 0;
4619
4620 /* We only need to look at symtabs not already expanded. */
4621 if (per_cu->v.quick->compunit_symtab)
4622 continue;
4623
b76e467d 4624 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4625 if (file_data == NULL)
4626 continue;
4627
4628 if (htab_find (visited_not_found.get (), file_data) != NULL)
4629 continue;
4630 else if (htab_find (visited_found.get (), file_data) != NULL)
4631 {
4632 per_cu->v.quick->mark = 1;
4633 continue;
4634 }
4635
b76e467d 4636 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4637 {
4638 const char *this_real_name;
4639
4640 if (file_matcher (file_data->file_names[j], false))
4641 {
4642 per_cu->v.quick->mark = 1;
4643 break;
4644 }
4645
4646 /* Before we invoke realpath, which can get expensive when many
4647 files are involved, do a quick comparison of the basenames. */
4648 if (!basenames_may_differ
4649 && !file_matcher (lbasename (file_data->file_names[j]),
4650 true))
4651 continue;
4652
4653 this_real_name = dw2_get_real_path (objfile, file_data, j);
4654 if (file_matcher (this_real_name, false))
4655 {
4656 per_cu->v.quick->mark = 1;
4657 break;
4658 }
4659 }
4660
b76e467d
SM
4661 void **slot = htab_find_slot (per_cu->v.quick->mark
4662 ? visited_found.get ()
4663 : visited_not_found.get (),
4664 file_data, INSERT);
927aa2e7
JK
4665 *slot = file_data;
4666 }
4667}
4668
4669static void
4670dw2_expand_symtabs_matching
4671 (struct objfile *objfile,
4672 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4673 const lookup_name_info *lookup_name,
927aa2e7
JK
4674 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4675 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4676 enum search_domain kind)
4677{
ed2dc618
SM
4678 struct dwarf2_per_objfile *dwarf2_per_objfile
4679 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4680
4681 /* index_table is NULL if OBJF_READNOW. */
4682 if (!dwarf2_per_objfile->index_table)
4683 return;
4684
ed2dc618 4685 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4686
c1a66c06
TV
4687 if (symbol_matcher == NULL && lookup_name == NULL)
4688 {
4689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4690 {
4691 QUIT;
4692
4693 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4694 expansion_notify);
4695 }
4696 return;
4697 }
4698
927aa2e7
JK
4699 mapped_index &index = *dwarf2_per_objfile->index_table;
4700
c1a66c06 4701 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4702 symbol_matcher,
4703 kind, [&] (offset_type idx)
4704 {
ed2dc618 4705 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4706 expansion_notify, kind);
3b00ef10 4707 return true;
927aa2e7
JK
4708 });
4709}
4710
4711/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4712 symtab. */
4713
4714static struct compunit_symtab *
4715recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4716 CORE_ADDR pc)
4717{
4718 int i;
4719
4720 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4721 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4722 return cust;
4723
4724 if (cust->includes == NULL)
4725 return NULL;
4726
4727 for (i = 0; cust->includes[i]; ++i)
4728 {
4729 struct compunit_symtab *s = cust->includes[i];
4730
4731 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4732 if (s != NULL)
4733 return s;
4734 }
4735
4736 return NULL;
4737}
4738
4739static struct compunit_symtab *
4740dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4741 struct bound_minimal_symbol msymbol,
4742 CORE_ADDR pc,
4743 struct obj_section *section,
4744 int warn_if_readin)
4745{
4746 struct dwarf2_per_cu_data *data;
4747 struct compunit_symtab *result;
4748
d320c2b5 4749 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4750 return NULL;
4751
b3b3bada 4752 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4753 data = (struct dwarf2_per_cu_data *) addrmap_find
4754 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4755 if (!data)
4756 return NULL;
4757
4758 if (warn_if_readin && data->v.quick->compunit_symtab)
4759 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4760 paddress (objfile->arch (), pc));
927aa2e7
JK
4761
4762 result
58f0c718
TT
4763 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4764 false),
927aa2e7
JK
4765 pc);
4766 gdb_assert (result != NULL);
4767 return result;
4768}
4769
4770static void
4771dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4772 void *data, int need_fullname)
4773{
ed2dc618
SM
4774 struct dwarf2_per_objfile *dwarf2_per_objfile
4775 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4776
4777 if (!dwarf2_per_objfile->filenames_cache)
4778 {
4779 dwarf2_per_objfile->filenames_cache.emplace ();
4780
4781 htab_up visited (htab_create_alloc (10,
4782 htab_hash_pointer, htab_eq_pointer,
4783 NULL, xcalloc, xfree));
4784
4785 /* The rule is CUs specify all the files, including those used
4786 by any TU, so there's no need to scan TUs here. We can
4787 ignore file names coming from already-expanded CUs. */
4788
b76e467d 4789 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4790 {
927aa2e7
JK
4791 if (per_cu->v.quick->compunit_symtab)
4792 {
4793 void **slot = htab_find_slot (visited.get (),
4794 per_cu->v.quick->file_names,
4795 INSERT);
4796
4797 *slot = per_cu->v.quick->file_names;
4798 }
4799 }
4800
b76e467d 4801 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4802 {
927aa2e7
JK
4803 /* We only need to look at symtabs not already expanded. */
4804 if (per_cu->v.quick->compunit_symtab)
4805 continue;
4806
b76e467d 4807 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4808 if (file_data == NULL)
4809 continue;
4810
b76e467d 4811 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4812 if (*slot)
4813 {
4814 /* Already visited. */
4815 continue;
4816 }
4817 *slot = file_data;
4818
4819 for (int j = 0; j < file_data->num_file_names; ++j)
4820 {
4821 const char *filename = file_data->file_names[j];
4822 dwarf2_per_objfile->filenames_cache->seen (filename);
4823 }
4824 }
4825 }
4826
4827 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4828 {
4829 gdb::unique_xmalloc_ptr<char> this_real_name;
4830
4831 if (need_fullname)
4832 this_real_name = gdb_realpath (filename);
4833 (*fun) (filename, this_real_name.get (), data);
4834 });
4835}
4836
4837static int
4838dw2_has_symbols (struct objfile *objfile)
4839{
4840 return 1;
4841}
4842
4843const struct quick_symbol_functions dwarf2_gdb_index_functions =
4844{
4845 dw2_has_symbols,
4846 dw2_find_last_source_symtab,
4847 dw2_forget_cached_source_info,
4848 dw2_map_symtabs_matching_filename,
4849 dw2_lookup_symbol,
d3214198 4850 NULL,
927aa2e7
JK
4851 dw2_print_stats,
4852 dw2_dump,
927aa2e7
JK
4853 dw2_expand_symtabs_for_function,
4854 dw2_expand_all_symtabs,
4855 dw2_expand_symtabs_with_fullname,
4856 dw2_map_matching_symbols,
4857 dw2_expand_symtabs_matching,
4858 dw2_find_pc_sect_compunit_symtab,
4859 NULL,
4860 dw2_map_symbol_filenames
4861};
4862
4863/* DWARF-5 debug_names reader. */
4864
4865/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4866static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4867
4868/* A helper function that reads the .debug_names section in SECTION
4869 and fills in MAP. FILENAME is the name of the file containing the
4870 section; it is used for error reporting.
4871
4872 Returns true if all went well, false otherwise. */
4873
4874static bool
4875read_debug_names_from_section (struct objfile *objfile,
4876 const char *filename,
4877 struct dwarf2_section_info *section,
4878 mapped_debug_names &map)
4879{
96b79293 4880 if (section->empty ())
927aa2e7
JK
4881 return false;
4882
4883 /* Older elfutils strip versions could keep the section in the main
4884 executable while splitting it for the separate debug info file. */
96b79293 4885 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4886 return false;
4887
96b79293 4888 section->read (objfile);
927aa2e7 4889
08feed99 4890 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4891
4892 const gdb_byte *addr = section->buffer;
4893
96b79293 4894 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4895
4896 unsigned int bytes_read;
4897 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4898 addr += bytes_read;
4899
4900 map.dwarf5_is_dwarf64 = bytes_read != 4;
4901 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4902 if (bytes_read + length != section->size)
4903 {
4904 /* There may be multiple per-CU indices. */
4905 warning (_("Section .debug_names in %s length %s does not match "
4906 "section length %s, ignoring .debug_names."),
4907 filename, plongest (bytes_read + length),
4908 pulongest (section->size));
4909 return false;
4910 }
4911
4912 /* The version number. */
4913 uint16_t version = read_2_bytes (abfd, addr);
4914 addr += 2;
4915 if (version != 5)
4916 {
4917 warning (_("Section .debug_names in %s has unsupported version %d, "
4918 "ignoring .debug_names."),
4919 filename, version);
4920 return false;
4921 }
4922
4923 /* Padding. */
4924 uint16_t padding = read_2_bytes (abfd, addr);
4925 addr += 2;
4926 if (padding != 0)
4927 {
4928 warning (_("Section .debug_names in %s has unsupported padding %d, "
4929 "ignoring .debug_names."),
4930 filename, padding);
4931 return false;
4932 }
4933
4934 /* comp_unit_count - The number of CUs in the CU list. */
4935 map.cu_count = read_4_bytes (abfd, addr);
4936 addr += 4;
4937
4938 /* local_type_unit_count - The number of TUs in the local TU
4939 list. */
4940 map.tu_count = read_4_bytes (abfd, addr);
4941 addr += 4;
4942
4943 /* foreign_type_unit_count - The number of TUs in the foreign TU
4944 list. */
4945 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4946 addr += 4;
4947 if (foreign_tu_count != 0)
4948 {
4949 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4950 "ignoring .debug_names."),
4951 filename, static_cast<unsigned long> (foreign_tu_count));
4952 return false;
4953 }
4954
4955 /* bucket_count - The number of hash buckets in the hash lookup
4956 table. */
4957 map.bucket_count = read_4_bytes (abfd, addr);
4958 addr += 4;
4959
4960 /* name_count - The number of unique names in the index. */
4961 map.name_count = read_4_bytes (abfd, addr);
4962 addr += 4;
4963
4964 /* abbrev_table_size - The size in bytes of the abbreviations
4965 table. */
4966 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4967 addr += 4;
4968
4969 /* augmentation_string_size - The size in bytes of the augmentation
4970 string. This value is rounded up to a multiple of 4. */
4971 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4972 addr += 4;
4973 map.augmentation_is_gdb = ((augmentation_string_size
4974 == sizeof (dwarf5_augmentation))
4975 && memcmp (addr, dwarf5_augmentation,
4976 sizeof (dwarf5_augmentation)) == 0);
4977 augmentation_string_size += (-augmentation_string_size) & 3;
4978 addr += augmentation_string_size;
4979
4980 /* List of CUs */
4981 map.cu_table_reordered = addr;
4982 addr += map.cu_count * map.offset_size;
4983
4984 /* List of Local TUs */
4985 map.tu_table_reordered = addr;
4986 addr += map.tu_count * map.offset_size;
4987
4988 /* Hash Lookup Table */
4989 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4990 addr += map.bucket_count * 4;
4991 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4992 addr += map.name_count * 4;
4993
4994 /* Name Table */
4995 map.name_table_string_offs_reordered = addr;
4996 addr += map.name_count * map.offset_size;
4997 map.name_table_entry_offs_reordered = addr;
4998 addr += map.name_count * map.offset_size;
4999
5000 const gdb_byte *abbrev_table_start = addr;
5001 for (;;)
5002 {
927aa2e7
JK
5003 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5004 addr += bytes_read;
5005 if (index_num == 0)
5006 break;
5007
5008 const auto insertpair
5009 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5010 if (!insertpair.second)
5011 {
5012 warning (_("Section .debug_names in %s has duplicate index %s, "
5013 "ignoring .debug_names."),
5014 filename, pulongest (index_num));
5015 return false;
5016 }
5017 mapped_debug_names::index_val &indexval = insertpair.first->second;
5018 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5019 addr += bytes_read;
5020
5021 for (;;)
5022 {
5023 mapped_debug_names::index_val::attr attr;
5024 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5025 addr += bytes_read;
5026 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5027 addr += bytes_read;
5028 if (attr.form == DW_FORM_implicit_const)
5029 {
5030 attr.implicit_const = read_signed_leb128 (abfd, addr,
5031 &bytes_read);
5032 addr += bytes_read;
5033 }
5034 if (attr.dw_idx == 0 && attr.form == 0)
5035 break;
5036 indexval.attr_vec.push_back (std::move (attr));
5037 }
5038 }
5039 if (addr != abbrev_table_start + abbrev_table_size)
5040 {
5041 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5042 "of size %s vs. written as %u, ignoring .debug_names."),
5043 filename, plongest (addr - abbrev_table_start),
5044 abbrev_table_size);
927aa2e7
JK
5045 return false;
5046 }
5047 map.entry_pool = addr;
5048
5049 return true;
5050}
5051
5052/* A helper for create_cus_from_debug_names that handles the MAP's CU
5053 list. */
5054
5055static void
ed2dc618 5056create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5057 const mapped_debug_names &map,
5058 dwarf2_section_info &section,
b76e467d 5059 bool is_dwz)
927aa2e7 5060{
3ee6bb11
TV
5061 if (!map.augmentation_is_gdb)
5062 {
5063 for (uint32_t i = 0; i < map.cu_count; ++i)
5064 {
5065 sect_offset sect_off
5066 = (sect_offset) (extract_unsigned_integer
5067 (map.cu_table_reordered + i * map.offset_size,
5068 map.offset_size,
5069 map.dwarf5_byte_order));
5070 /* We don't know the length of the CU, because the CU list in a
5071 .debug_names index can be incomplete, so we can't use the start of
5072 the next CU as end of this CU. We create the CUs here with length 0,
5073 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5074 dwarf2_per_cu_data *per_cu
5075 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5076 sect_off, 0);
5077 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5078 }
5079 }
5080
927aa2e7
JK
5081 sect_offset sect_off_prev;
5082 for (uint32_t i = 0; i <= map.cu_count; ++i)
5083 {
5084 sect_offset sect_off_next;
5085 if (i < map.cu_count)
5086 {
5087 sect_off_next
5088 = (sect_offset) (extract_unsigned_integer
5089 (map.cu_table_reordered + i * map.offset_size,
5090 map.offset_size,
5091 map.dwarf5_byte_order));
5092 }
5093 else
5094 sect_off_next = (sect_offset) section.size;
5095 if (i >= 1)
5096 {
5097 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5098 dwarf2_per_cu_data *per_cu
ed2dc618 5099 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5100 sect_off_prev, length);
b76e467d 5101 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5102 }
5103 sect_off_prev = sect_off_next;
5104 }
5105}
5106
5107/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5108 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5109
5110static void
ed2dc618 5111create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5112 const mapped_debug_names &map,
5113 const mapped_debug_names &dwz_map)
5114{
b76e467d
SM
5115 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5116 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5117
ed2dc618
SM
5118 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5119 dwarf2_per_objfile->info,
b76e467d 5120 false /* is_dwz */);
927aa2e7
JK
5121
5122 if (dwz_map.cu_count == 0)
5123 return;
5124
ed2dc618
SM
5125 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5126 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5127 true /* is_dwz */);
927aa2e7
JK
5128}
5129
5130/* Read .debug_names. If everything went ok, initialize the "quick"
5131 elements of all the CUs and return true. Otherwise, return false. */
5132
5133static bool
ed2dc618 5134dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5135{
22ca247e
TT
5136 std::unique_ptr<mapped_debug_names> map
5137 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5138 mapped_debug_names dwz_map (dwarf2_per_objfile);
5139 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5140
5141 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5142 &dwarf2_per_objfile->debug_names,
22ca247e 5143 *map))
927aa2e7
JK
5144 return false;
5145
5146 /* Don't use the index if it's empty. */
22ca247e 5147 if (map->name_count == 0)
927aa2e7
JK
5148 return false;
5149
5150 /* If there is a .dwz file, read it so we can get its CU list as
5151 well. */
ed2dc618 5152 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5153 if (dwz != NULL)
5154 {
5155 if (!read_debug_names_from_section (objfile,
00f93c44 5156 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5157 &dwz->debug_names, dwz_map))
5158 {
5159 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5160 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5161 return false;
5162 }
5163 }
5164
22ca247e 5165 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5166
22ca247e 5167 if (map->tu_count != 0)
927aa2e7
JK
5168 {
5169 /* We can only handle a single .debug_types when we have an
5170 index. */
fd5866f6 5171 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5172 return false;
5173
fd5866f6 5174 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5175
5176 create_signatured_type_table_from_debug_names
22ca247e 5177 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5178 }
5179
ed2dc618
SM
5180 create_addrmap_from_aranges (dwarf2_per_objfile,
5181 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5182
22ca247e 5183 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5184 dwarf2_per_objfile->using_index = 1;
5185 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5186 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5187
5188 return true;
5189}
5190
927aa2e7
JK
5191/* Type used to manage iterating over all CUs looking for a symbol for
5192 .debug_names. */
5193
5194class dw2_debug_names_iterator
5195{
5196public:
927aa2e7 5197 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5198 gdb::optional<block_enum> block_index,
5199 domain_enum domain,
927aa2e7 5200 const char *name)
2b79f376 5201 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5202 m_addr (find_vec_in_debug_names (map, name))
5203 {}
5204
5205 dw2_debug_names_iterator (const mapped_debug_names &map,
5206 search_domain search, uint32_t namei)
5207 : m_map (map),
5208 m_search (search),
5209 m_addr (find_vec_in_debug_names (map, namei))
5210 {}
5211
3b00ef10
TT
5212 dw2_debug_names_iterator (const mapped_debug_names &map,
5213 block_enum block_index, domain_enum domain,
5214 uint32_t namei)
5215 : m_map (map), m_block_index (block_index), m_domain (domain),
5216 m_addr (find_vec_in_debug_names (map, namei))
5217 {}
5218
927aa2e7
JK
5219 /* Return the next matching CU or NULL if there are no more. */
5220 dwarf2_per_cu_data *next ();
5221
5222private:
5223 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5224 const char *name);
5225 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5226 uint32_t namei);
5227
5228 /* The internalized form of .debug_names. */
5229 const mapped_debug_names &m_map;
5230
2b79f376
SM
5231 /* If set, only look for symbols that match that block. Valid values are
5232 GLOBAL_BLOCK and STATIC_BLOCK. */
5233 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5234
5235 /* The kind of symbol we're looking for. */
5236 const domain_enum m_domain = UNDEF_DOMAIN;
5237 const search_domain m_search = ALL_DOMAIN;
5238
5239 /* The list of CUs from the index entry of the symbol, or NULL if
5240 not found. */
5241 const gdb_byte *m_addr;
5242};
5243
5244const char *
5245mapped_debug_names::namei_to_name (uint32_t namei) const
5246{
5247 const ULONGEST namei_string_offs
5248 = extract_unsigned_integer ((name_table_string_offs_reordered
5249 + namei * offset_size),
5250 offset_size,
5251 dwarf5_byte_order);
4f44ae6c
TT
5252 return read_indirect_string_at_offset (dwarf2_per_objfile,
5253 namei_string_offs);
927aa2e7
JK
5254}
5255
5256/* Find a slot in .debug_names for the object named NAME. If NAME is
5257 found, return pointer to its pool data. If NAME cannot be found,
5258 return NULL. */
5259
5260const gdb_byte *
5261dw2_debug_names_iterator::find_vec_in_debug_names
5262 (const mapped_debug_names &map, const char *name)
5263{
5264 int (*cmp) (const char *, const char *);
5265
54ee4252 5266 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5267 if (current_language->la_language == language_cplus
5268 || current_language->la_language == language_fortran
5269 || current_language->la_language == language_d)
5270 {
5271 /* NAME is already canonical. Drop any qualifiers as
5272 .debug_names does not contain any. */
5273
5274 if (strchr (name, '(') != NULL)
5275 {
54ee4252 5276 without_params = cp_remove_params (name);
927aa2e7 5277 if (without_params != NULL)
54ee4252 5278 name = without_params.get ();
927aa2e7
JK
5279 }
5280 }
5281
5282 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5283
5284 const uint32_t full_hash = dwarf5_djb_hash (name);
5285 uint32_t namei
5286 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5287 (map.bucket_table_reordered
5288 + (full_hash % map.bucket_count)), 4,
5289 map.dwarf5_byte_order);
5290 if (namei == 0)
5291 return NULL;
5292 --namei;
5293 if (namei >= map.name_count)
5294 {
b98664d3 5295 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5296 "[in module %s]"),
5297 namei, map.name_count,
ed2dc618 5298 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5299 return NULL;
5300 }
5301
5302 for (;;)
5303 {
5304 const uint32_t namei_full_hash
5305 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5306 (map.hash_table_reordered + namei), 4,
5307 map.dwarf5_byte_order);
5308 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5309 return NULL;
5310
5311 if (full_hash == namei_full_hash)
5312 {
5313 const char *const namei_string = map.namei_to_name (namei);
5314
5315#if 0 /* An expensive sanity check. */
5316 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5317 {
b98664d3 5318 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5319 "[in module %s]"),
5320 namei, objfile_name (dwarf2_per_objfile->objfile));
5321 return NULL;
5322 }
5323#endif
5324
5325 if (cmp (namei_string, name) == 0)
5326 {
5327 const ULONGEST namei_entry_offs
5328 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5329 + namei * map.offset_size),
5330 map.offset_size, map.dwarf5_byte_order);
5331 return map.entry_pool + namei_entry_offs;
5332 }
5333 }
5334
5335 ++namei;
5336 if (namei >= map.name_count)
5337 return NULL;
5338 }
5339}
5340
5341const gdb_byte *
5342dw2_debug_names_iterator::find_vec_in_debug_names
5343 (const mapped_debug_names &map, uint32_t namei)
5344{
5345 if (namei >= map.name_count)
5346 {
b98664d3 5347 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5348 "[in module %s]"),
5349 namei, map.name_count,
ed2dc618 5350 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5351 return NULL;
5352 }
5353
5354 const ULONGEST namei_entry_offs
5355 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5356 + namei * map.offset_size),
5357 map.offset_size, map.dwarf5_byte_order);
5358 return map.entry_pool + namei_entry_offs;
5359}
5360
5361/* See dw2_debug_names_iterator. */
5362
5363dwarf2_per_cu_data *
5364dw2_debug_names_iterator::next ()
5365{
5366 if (m_addr == NULL)
5367 return NULL;
5368
ed2dc618
SM
5369 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5370 struct objfile *objfile = dwarf2_per_objfile->objfile;
5371 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5372
5373 again:
5374
5375 unsigned int bytes_read;
5376 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5377 m_addr += bytes_read;
5378 if (abbrev == 0)
5379 return NULL;
5380
5381 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5382 if (indexval_it == m_map.abbrev_map.cend ())
5383 {
b98664d3 5384 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5385 "[in module %s]"),
ed2dc618 5386 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5387 return NULL;
5388 }
5389 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5390 enum class symbol_linkage {
5391 unknown,
5392 static_,
5393 extern_,
23c13d42 5394 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5395 dwarf2_per_cu_data *per_cu = NULL;
5396 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5397 {
5398 ULONGEST ull;
5399 switch (attr.form)
5400 {
5401 case DW_FORM_implicit_const:
5402 ull = attr.implicit_const;
5403 break;
5404 case DW_FORM_flag_present:
5405 ull = 1;
5406 break;
5407 case DW_FORM_udata:
5408 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5409 m_addr += bytes_read;
5410 break;
6dc55ce9 5411 case DW_FORM_ref4:
5412 ull = read_4_bytes (abfd, m_addr);
5413 m_addr += 4;
5414 break;
5415 case DW_FORM_ref8:
5416 ull = read_8_bytes (abfd, m_addr);
5417 m_addr += 8;
5418 break;
5419 case DW_FORM_ref_sig8:
5420 ull = read_8_bytes (abfd, m_addr);
5421 m_addr += 8;
5422 break;
927aa2e7 5423 default:
b98664d3 5424 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5425 dwarf_form_name (attr.form),
ed2dc618 5426 objfile_name (objfile));
927aa2e7
JK
5427 return NULL;
5428 }
5429 switch (attr.dw_idx)
5430 {
5431 case DW_IDX_compile_unit:
5432 /* Don't crash on bad data. */
b76e467d 5433 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5434 {
b98664d3 5435 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5436 " [in module %s]"),
5437 pulongest (ull),
5438 objfile_name (dwarf2_per_objfile->objfile));
5439 continue;
5440 }
ff4c9fec 5441 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5442 break;
8af5c486
JK
5443 case DW_IDX_type_unit:
5444 /* Don't crash on bad data. */
b2bdb8cf 5445 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5446 {
b98664d3 5447 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5448 " [in module %s]"),
5449 pulongest (ull),
5450 objfile_name (dwarf2_per_objfile->objfile));
5451 continue;
5452 }
ff4c9fec 5453 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5454 break;
6dc55ce9 5455 case DW_IDX_die_offset:
5456 /* In a per-CU index (as opposed to a per-module index), index
5457 entries without CU attribute implicitly refer to the single CU. */
5458 if (per_cu == NULL)
5459 per_cu = dwarf2_per_objfile->get_cu (0);
5460 break;
927aa2e7
JK
5461 case DW_IDX_GNU_internal:
5462 if (!m_map.augmentation_is_gdb)
5463 break;
23c13d42 5464 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5465 break;
5466 case DW_IDX_GNU_external:
5467 if (!m_map.augmentation_is_gdb)
5468 break;
23c13d42 5469 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5470 break;
5471 }
5472 }
5473
5474 /* Skip if already read in. */
5475 if (per_cu->v.quick->compunit_symtab)
5476 goto again;
5477
5478 /* Check static vs global. */
23c13d42 5479 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5480 {
2b79f376 5481 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5482 const bool symbol_is_static =
5483 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5484 if (want_static != symbol_is_static)
2b79f376 5485 goto again;
927aa2e7
JK
5486 }
5487
5488 /* Match dw2_symtab_iter_next, symbol_kind
5489 and debug_names::psymbol_tag. */
5490 switch (m_domain)
5491 {
5492 case VAR_DOMAIN:
5493 switch (indexval.dwarf_tag)
5494 {
5495 case DW_TAG_variable:
5496 case DW_TAG_subprogram:
5497 /* Some types are also in VAR_DOMAIN. */
5498 case DW_TAG_typedef:
5499 case DW_TAG_structure_type:
5500 break;
5501 default:
5502 goto again;
5503 }
5504 break;
5505 case STRUCT_DOMAIN:
5506 switch (indexval.dwarf_tag)
5507 {
5508 case DW_TAG_typedef:
5509 case DW_TAG_structure_type:
5510 break;
5511 default:
5512 goto again;
5513 }
5514 break;
5515 case LABEL_DOMAIN:
5516 switch (indexval.dwarf_tag)
5517 {
5518 case 0:
5519 case DW_TAG_variable:
5520 break;
5521 default:
5522 goto again;
5523 }
5524 break;
59c35742
AB
5525 case MODULE_DOMAIN:
5526 switch (indexval.dwarf_tag)
5527 {
5528 case DW_TAG_module:
5529 break;
5530 default:
5531 goto again;
5532 }
5533 break;
927aa2e7
JK
5534 default:
5535 break;
5536 }
5537
5538 /* Match dw2_expand_symtabs_matching, symbol_kind and
5539 debug_names::psymbol_tag. */
5540 switch (m_search)
4b514bc8 5541 {
927aa2e7
JK
5542 case VARIABLES_DOMAIN:
5543 switch (indexval.dwarf_tag)
4b514bc8 5544 {
927aa2e7
JK
5545 case DW_TAG_variable:
5546 break;
5547 default:
5548 goto again;
4b514bc8 5549 }
927aa2e7
JK
5550 break;
5551 case FUNCTIONS_DOMAIN:
5552 switch (indexval.dwarf_tag)
4b514bc8 5553 {
927aa2e7
JK
5554 case DW_TAG_subprogram:
5555 break;
5556 default:
5557 goto again;
4b514bc8 5558 }
927aa2e7
JK
5559 break;
5560 case TYPES_DOMAIN:
5561 switch (indexval.dwarf_tag)
5562 {
5563 case DW_TAG_typedef:
5564 case DW_TAG_structure_type:
5565 break;
5566 default:
5567 goto again;
5568 }
5569 break;
59c35742
AB
5570 case MODULES_DOMAIN:
5571 switch (indexval.dwarf_tag)
5572 {
5573 case DW_TAG_module:
5574 break;
5575 default:
5576 goto again;
5577 }
927aa2e7
JK
5578 default:
5579 break;
4b514bc8 5580 }
927aa2e7
JK
5581
5582 return per_cu;
4b514bc8 5583}
61920122 5584
927aa2e7 5585static struct compunit_symtab *
c7f839cb 5586dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5587 const char *name, domain_enum domain)
4b514bc8 5588{
ed2dc618
SM
5589 struct dwarf2_per_objfile *dwarf2_per_objfile
5590 = get_dwarf2_per_objfile (objfile);
61920122 5591
927aa2e7
JK
5592 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5593 if (!mapp)
61920122 5594 {
927aa2e7
JK
5595 /* index is NULL if OBJF_READNOW. */
5596 return NULL;
5597 }
5598 const auto &map = *mapp;
9291a0cd 5599
2b79f376 5600 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5601
927aa2e7
JK
5602 struct compunit_symtab *stab_best = NULL;
5603 struct dwarf2_per_cu_data *per_cu;
5604 while ((per_cu = iter.next ()) != NULL)
5605 {
5606 struct symbol *sym, *with_opaque = NULL;
58f0c718 5607 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5608 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5609 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5610
927aa2e7
JK
5611 sym = block_find_symbol (block, name, domain,
5612 block_find_non_opaque_type_preferred,
5613 &with_opaque);
9703b513 5614
927aa2e7
JK
5615 /* Some caution must be observed with overloaded functions and
5616 methods, since the index will not contain any overload
5617 information (but NAME might contain it). */
a3ec0bb1 5618
927aa2e7 5619 if (sym != NULL
987012b8 5620 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5621 return stab;
5622 if (with_opaque != NULL
987012b8 5623 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5624 stab_best = stab;
9703b513 5625
927aa2e7 5626 /* Keep looking through other CUs. */
9703b513
TT
5627 }
5628
927aa2e7 5629 return stab_best;
9703b513
TT
5630}
5631
927aa2e7
JK
5632/* This dumps minimal information about .debug_names. It is called
5633 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5634 uses this to verify that .debug_names has been loaded. */
9291a0cd 5635
927aa2e7
JK
5636static void
5637dw2_debug_names_dump (struct objfile *objfile)
5638{
ed2dc618
SM
5639 struct dwarf2_per_objfile *dwarf2_per_objfile
5640 = get_dwarf2_per_objfile (objfile);
5641
927aa2e7
JK
5642 gdb_assert (dwarf2_per_objfile->using_index);
5643 printf_filtered (".debug_names:");
5644 if (dwarf2_per_objfile->debug_names_table)
5645 printf_filtered (" exists\n");
5646 else
5647 printf_filtered (" faked for \"readnow\"\n");
5648 printf_filtered ("\n");
9291a0cd
TT
5649}
5650
9291a0cd 5651static void
927aa2e7
JK
5652dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5653 const char *func_name)
9291a0cd 5654{
ed2dc618
SM
5655 struct dwarf2_per_objfile *dwarf2_per_objfile
5656 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5657
927aa2e7
JK
5658 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5659 if (dwarf2_per_objfile->debug_names_table)
24c79950 5660 {
927aa2e7 5661 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5662
2b79f376 5663 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5664
927aa2e7
JK
5665 struct dwarf2_per_cu_data *per_cu;
5666 while ((per_cu = iter.next ()) != NULL)
58f0c718 5667 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5668 }
5669}
24c79950 5670
3b00ef10
TT
5671static void
5672dw2_debug_names_map_matching_symbols
5673 (struct objfile *objfile,
5674 const lookup_name_info &name, domain_enum domain,
5675 int global,
5676 gdb::function_view<symbol_found_callback_ftype> callback,
5677 symbol_compare_ftype *ordered_compare)
5678{
5679 struct dwarf2_per_objfile *dwarf2_per_objfile
5680 = get_dwarf2_per_objfile (objfile);
5681
5682 /* debug_names_table is NULL if OBJF_READNOW. */
5683 if (!dwarf2_per_objfile->debug_names_table)
5684 return;
5685
5686 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5687 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5688
5689 const char *match_name = name.ada ().lookup_name ().c_str ();
5690 auto matcher = [&] (const char *symname)
5691 {
5692 if (ordered_compare == nullptr)
5693 return true;
5694 return ordered_compare (symname, match_name) == 0;
5695 };
5696
5697 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5698 [&] (offset_type namei)
5699 {
5700 /* The name was matched, now expand corresponding CUs that were
5701 marked. */
5702 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5703
5704 struct dwarf2_per_cu_data *per_cu;
5705 while ((per_cu = iter.next ()) != NULL)
5706 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5707 return true;
5708 });
5709
5710 /* It's a shame we couldn't do this inside the
5711 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5712 that have already been expanded. Instead, this loop matches what
5713 the psymtab code does. */
5714 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5715 {
5716 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5717 if (cust != nullptr)
5718 {
5719 const struct block *block
5720 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5721 if (!iterate_over_symbols_terminated (block, name,
5722 domain, callback))
5723 break;
5724 }
5725 }
5726}
5727
927aa2e7
JK
5728static void
5729dw2_debug_names_expand_symtabs_matching
5730 (struct objfile *objfile,
5731 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5732 const lookup_name_info *lookup_name,
927aa2e7
JK
5733 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5734 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5735 enum search_domain kind)
5736{
ed2dc618
SM
5737 struct dwarf2_per_objfile *dwarf2_per_objfile
5738 = get_dwarf2_per_objfile (objfile);
9291a0cd 5739
927aa2e7
JK
5740 /* debug_names_table is NULL if OBJF_READNOW. */
5741 if (!dwarf2_per_objfile->debug_names_table)
5742 return;
9291a0cd 5743
ed2dc618 5744 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5745
c1a66c06
TV
5746 if (symbol_matcher == NULL && lookup_name == NULL)
5747 {
5748 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5749 {
5750 QUIT;
5751
5752 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5753 expansion_notify);
5754 }
5755 return;
5756 }
5757
44ed8f3e 5758 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5759
c1a66c06 5760 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5761 symbol_matcher,
5762 kind, [&] (offset_type namei)
927aa2e7 5763 {
927aa2e7
JK
5764 /* The name was matched, now expand corresponding CUs that were
5765 marked. */
5766 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5767
927aa2e7
JK
5768 struct dwarf2_per_cu_data *per_cu;
5769 while ((per_cu = iter.next ()) != NULL)
5770 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5771 expansion_notify);
3b00ef10 5772 return true;
44ed8f3e 5773 });
9291a0cd
TT
5774}
5775
927aa2e7 5776const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5777{
5778 dw2_has_symbols,
5779 dw2_find_last_source_symtab,
5780 dw2_forget_cached_source_info,
f8eba3c6 5781 dw2_map_symtabs_matching_filename,
927aa2e7 5782 dw2_debug_names_lookup_symbol,
d3214198 5783 NULL,
9291a0cd 5784 dw2_print_stats,
927aa2e7 5785 dw2_debug_names_dump,
927aa2e7 5786 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5787 dw2_expand_all_symtabs,
652a8996 5788 dw2_expand_symtabs_with_fullname,
3b00ef10 5789 dw2_debug_names_map_matching_symbols,
927aa2e7 5790 dw2_debug_names_expand_symtabs_matching,
43f3e411 5791 dw2_find_pc_sect_compunit_symtab,
71a3c369 5792 NULL,
9291a0cd
TT
5793 dw2_map_symbol_filenames
5794};
5795
4485a1c1
SM
5796/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5797 to either a dwarf2_per_objfile or dwz_file object. */
5798
5799template <typename T>
5800static gdb::array_view<const gdb_byte>
5801get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5802{
5803 dwarf2_section_info *section = &section_owner->gdb_index;
5804
96b79293 5805 if (section->empty ())
4485a1c1
SM
5806 return {};
5807
5808 /* Older elfutils strip versions could keep the section in the main
5809 executable while splitting it for the separate debug info file. */
96b79293 5810 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5811 return {};
5812
96b79293 5813 section->read (obj);
4485a1c1 5814
8bebfcda
PA
5815 /* dwarf2_section_info::size is a bfd_size_type, while
5816 gdb::array_view works with size_t. On 32-bit hosts, with
5817 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5818 is 32-bit. So we need an explicit narrowing conversion here.
5819 This is fine, because it's impossible to allocate or mmap an
5820 array/buffer larger than what size_t can represent. */
5821 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5822}
5823
87d6a7aa
SM
5824/* Lookup the index cache for the contents of the index associated to
5825 DWARF2_OBJ. */
5826
5827static gdb::array_view<const gdb_byte>
5828get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5829{
5830 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5831 if (build_id == nullptr)
5832 return {};
5833
5834 return global_index_cache.lookup_gdb_index (build_id,
5835 &dwarf2_obj->index_cache_res);
5836}
5837
5838/* Same as the above, but for DWZ. */
5839
5840static gdb::array_view<const gdb_byte>
5841get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5842{
5843 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5844 if (build_id == nullptr)
5845 return {};
5846
5847 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5848}
5849
3c0aa29a 5850/* See symfile.h. */
9291a0cd 5851
3c0aa29a
PA
5852bool
5853dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5854{
ed2dc618
SM
5855 struct dwarf2_per_objfile *dwarf2_per_objfile
5856 = get_dwarf2_per_objfile (objfile);
5857
9291a0cd
TT
5858 /* If we're about to read full symbols, don't bother with the
5859 indices. In this case we also don't care if some other debug
5860 format is making psymtabs, because they are all about to be
5861 expanded anyway. */
5862 if ((objfile->flags & OBJF_READNOW))
5863 {
9291a0cd 5864 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5865 create_all_comp_units (dwarf2_per_objfile);
5866 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5867 dwarf2_per_objfile->quick_file_names_table
5868 = create_quick_file_names_table
5869 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5870
b76e467d 5871 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5872 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5873 {
ff4c9fec 5874 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5875
e254ef6a
DE
5876 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5877 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5878 }
5879
5880 /* Return 1 so that gdb sees the "quick" functions. However,
5881 these functions will be no-ops because we will have expanded
5882 all symtabs. */
3c0aa29a
PA
5883 *index_kind = dw_index_kind::GDB_INDEX;
5884 return true;
9291a0cd
TT
5885 }
5886
ed2dc618 5887 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5888 {
5889 *index_kind = dw_index_kind::DEBUG_NAMES;
5890 return true;
5891 }
927aa2e7 5892
4485a1c1
SM
5893 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5894 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5895 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5896 {
5897 *index_kind = dw_index_kind::GDB_INDEX;
5898 return true;
5899 }
9291a0cd 5900
87d6a7aa
SM
5901 /* ... otherwise, try to find the index in the index cache. */
5902 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5903 get_gdb_index_contents_from_cache,
5904 get_gdb_index_contents_from_cache_dwz))
5905 {
5906 global_index_cache.hit ();
5907 *index_kind = dw_index_kind::GDB_INDEX;
5908 return true;
5909 }
5910
5911 global_index_cache.miss ();
3c0aa29a 5912 return false;
9291a0cd
TT
5913}
5914
5915\f
5916
dce234bc
PP
5917/* Build a partial symbol table. */
5918
5919void
f29dff0a 5920dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5921{
ed2dc618
SM
5922 struct dwarf2_per_objfile *dwarf2_per_objfile
5923 = get_dwarf2_per_objfile (objfile);
c9bf0622 5924
6eee24ce 5925 init_psymbol_list (objfile, 1024);
c906108c 5926
a70b8144 5927 try
c9bf0622
TT
5928 {
5929 /* This isn't really ideal: all the data we allocate on the
5930 objfile's obstack is still uselessly kept around. However,
5931 freeing it seems unsafe. */
906768f9 5932 psymtab_discarder psymtabs (objfile);
ed2dc618 5933 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5934 psymtabs.keep ();
87d6a7aa
SM
5935
5936 /* (maybe) store an index in the cache. */
5937 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5938 }
230d2906 5939 catch (const gdb_exception_error &except)
492d29ea
PA
5940 {
5941 exception_print (gdb_stderr, except);
5942 }
c906108c 5943}
c906108c 5944
3b80fe9b
DE
5945/* Find the base address of the compilation unit for range lists and
5946 location lists. It will normally be specified by DW_AT_low_pc.
5947 In DWARF-3 draft 4, the base address could be overridden by
5948 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5949 compilation units with discontinuous ranges. */
5950
5951static void
5952dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5953{
5954 struct attribute *attr;
5955
2b24b6e4 5956 cu->base_address.reset ();
3b80fe9b
DE
5957
5958 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5959 if (attr != nullptr)
2b24b6e4 5960 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5961 else
5962 {
5963 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5964 if (attr != nullptr)
2b24b6e4 5965 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5966 }
5967}
5968
36586728
TT
5969/* Helper function that returns the proper abbrev section for
5970 THIS_CU. */
5971
5972static struct dwarf2_section_info *
5973get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5974{
5975 struct dwarf2_section_info *abbrev;
ed2dc618 5976 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5977
5978 if (this_cu->is_dwz)
ed2dc618 5979 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5980 else
5981 abbrev = &dwarf2_per_objfile->abbrev;
5982
5983 return abbrev;
5984}
5985
f4dc4d17
DE
5986/* Fetch the abbreviation table offset from a comp or type unit header. */
5987
5988static sect_offset
ed2dc618
SM
5989read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5990 struct dwarf2_section_info *section,
9c541725 5991 sect_offset sect_off)
f4dc4d17 5992{
96b79293 5993 bfd *abfd = section->get_bfd_owner ();
d521ce57 5994 const gdb_byte *info_ptr;
ac298888 5995 unsigned int initial_length_size, offset_size;
43988095 5996 uint16_t version;
f4dc4d17 5997
96b79293 5998 section->read (dwarf2_per_objfile->objfile);
9c541725 5999 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6000 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6001 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6002 info_ptr += initial_length_size;
6003
6004 version = read_2_bytes (abfd, info_ptr);
6005 info_ptr += 2;
6006 if (version >= 5)
6007 {
6008 /* Skip unit type and address size. */
6009 info_ptr += 2;
6010 }
6011
24aa364d 6012 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6013}
6014
b83470bf
TT
6015/* A partial symtab that is used only for include files. */
6016struct dwarf2_include_psymtab : public partial_symtab
6017{
6018 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6019 : partial_symtab (filename, objfile)
6020 {
6021 }
6022
6023 void read_symtab (struct objfile *objfile) override
6024 {
194d088f
TV
6025 /* It's an include file, no symbols to read for it.
6026 Everything is in the includer symtab. */
6027
6028 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6029 expansion of the includer psymtab. We use the dependencies[0] field to
6030 model the includer. But if we go the regular route of calling
6031 expand_psymtab here, and having expand_psymtab call expand_dependencies
6032 to expand the includer, we'll only use expand_psymtab on the includer
6033 (making it a non-toplevel psymtab), while if we expand the includer via
6034 another path, we'll use read_symtab (making it a toplevel psymtab).
6035 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6036 psymtab, and trigger read_symtab on the includer here directly. */
6037 includer ()->read_symtab (objfile);
b83470bf
TT
6038 }
6039
6040 void expand_psymtab (struct objfile *objfile) override
6041 {
194d088f
TV
6042 /* This is not called by read_symtab, and should not be called by any
6043 expand_dependencies. */
6044 gdb_assert (false);
b83470bf
TT
6045 }
6046
6047 bool readin_p () const override
6048 {
194d088f 6049 return includer ()->readin_p ();
b83470bf
TT
6050 }
6051
6052 struct compunit_symtab *get_compunit_symtab () const override
6053 {
6054 return nullptr;
6055 }
6056
6057private:
194d088f
TV
6058 partial_symtab *includer () const
6059 {
6060 /* An include psymtab has exactly one dependency: the psymtab that
6061 includes it. */
6062 gdb_assert (this->number_of_dependencies == 1);
6063 return this->dependencies[0];
6064 }
b83470bf
TT
6065};
6066
aaa75496
JB
6067/* Allocate a new partial symtab for file named NAME and mark this new
6068 partial symtab as being an include of PST. */
6069
6070static void
891813be 6071dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6072 struct objfile *objfile)
6073{
b83470bf 6074 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6075
fbd9ab74
JK
6076 if (!IS_ABSOLUTE_PATH (subpst->filename))
6077 {
6078 /* It shares objfile->objfile_obstack. */
6079 subpst->dirname = pst->dirname;
6080 }
6081
a9342b62 6082 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6083 subpst->dependencies[0] = pst;
6084 subpst->number_of_dependencies = 1;
aaa75496
JB
6085}
6086
6087/* Read the Line Number Program data and extract the list of files
6088 included by the source file represented by PST. Build an include
d85a05f0 6089 partial symtab for each of these included files. */
aaa75496
JB
6090
6091static void
6092dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6093 struct die_info *die,
891813be 6094 dwarf2_psymtab *pst)
aaa75496 6095{
fff8551c 6096 line_header_up lh;
d85a05f0 6097 struct attribute *attr;
aaa75496 6098
d85a05f0 6099 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6100 if (attr != nullptr)
9c541725 6101 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6102 if (lh == NULL)
6103 return; /* No linetable, so no includes. */
6104
79748972
TT
6105 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6106 that we pass in the raw text_low here; that is ok because we're
6107 only decoding the line table to make include partial symtabs, and
6108 so the addresses aren't really used. */
4ae976d1 6109 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6110 pst->raw_text_low (), 1);
aaa75496
JB
6111}
6112
348e048f 6113static hashval_t
52dc124a 6114hash_signatured_type (const void *item)
348e048f 6115{
9a3c8263
SM
6116 const struct signatured_type *sig_type
6117 = (const struct signatured_type *) item;
9a619af0 6118
348e048f 6119 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6120 return sig_type->signature;
348e048f
DE
6121}
6122
6123static int
52dc124a 6124eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6125{
9a3c8263
SM
6126 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6127 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6128
348e048f
DE
6129 return lhs->signature == rhs->signature;
6130}
6131
1fd400ff
TT
6132/* Allocate a hash table for signatured types. */
6133
b0b6a987 6134static htab_up
298e9637 6135allocate_signatured_type_table ()
1fd400ff 6136{
b0b6a987
TT
6137 return htab_up (htab_create_alloc (41,
6138 hash_signatured_type,
6139 eq_signatured_type,
6140 NULL, xcalloc, xfree));
1fd400ff
TT
6141}
6142
d467dd73 6143/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6144
6145static int
d467dd73 6146add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6147{
9a3c8263 6148 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6149 std::vector<signatured_type *> *all_type_units
6150 = (std::vector<signatured_type *> *) datum;
1fd400ff 6151
b2bdb8cf 6152 all_type_units->push_back (sigt);
1fd400ff
TT
6153
6154 return 1;
6155}
6156
78d4d2c5 6157/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6158 and fill them into TYPES_HTAB. It will process only type units,
6159 therefore DW_UT_type. */
c88ee1f0 6160
78d4d2c5 6161static void
ed2dc618
SM
6162create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6163 struct dwo_file *dwo_file,
b0b6a987 6164 dwarf2_section_info *section, htab_up &types_htab,
43988095 6165 rcuh_kind section_kind)
348e048f 6166{
3019eac3 6167 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6168 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6169 bfd *abfd;
6170 const gdb_byte *info_ptr, *end_ptr;
348e048f 6171
4bdcc0c1
DE
6172 abbrev_section = (dwo_file != NULL
6173 ? &dwo_file->sections.abbrev
6174 : &dwarf2_per_objfile->abbrev);
6175
b4f54984 6176 if (dwarf_read_debug)
43988095 6177 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6178 section->get_name (),
6179 abbrev_section->get_file_name ());
09406207 6180
96b79293 6181 section->read (objfile);
78d4d2c5 6182 info_ptr = section->buffer;
348e048f 6183
78d4d2c5
JK
6184 if (info_ptr == NULL)
6185 return;
348e048f 6186
78d4d2c5
JK
6187 /* We can't set abfd until now because the section may be empty or
6188 not present, in which case the bfd is unknown. */
96b79293 6189 abfd = section->get_bfd_owner ();
348e048f 6190
c0ab21c2
TT
6191 /* We don't use cutu_reader here because we don't need to read
6192 any dies: the signature is in the header. */
3019eac3 6193
78d4d2c5
JK
6194 end_ptr = info_ptr + section->size;
6195 while (info_ptr < end_ptr)
6196 {
78d4d2c5
JK
6197 struct signatured_type *sig_type;
6198 struct dwo_unit *dwo_tu;
6199 void **slot;
6200 const gdb_byte *ptr = info_ptr;
6201 struct comp_unit_head header;
6202 unsigned int length;
8b70b953 6203
9c541725 6204 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6205
a49dd8dd
JK
6206 /* Initialize it due to a false compiler warning. */
6207 header.signature = -1;
9c541725 6208 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6209
78d4d2c5
JK
6210 /* We need to read the type's signature in order to build the hash
6211 table, but we don't need anything else just yet. */
348e048f 6212
ed2dc618 6213 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6214 abbrev_section, ptr, section_kind);
348e048f 6215
4057dfde 6216 length = header.get_length ();
6caca83c 6217
78d4d2c5
JK
6218 /* Skip dummy type units. */
6219 if (ptr >= info_ptr + length
43988095
JK
6220 || peek_abbrev_code (abfd, ptr) == 0
6221 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6222 {
6223 info_ptr += length;
6224 continue;
6225 }
dee91e82 6226
78d4d2c5
JK
6227 if (types_htab == NULL)
6228 {
6229 if (dwo_file)
298e9637 6230 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6231 else
298e9637 6232 types_htab = allocate_signatured_type_table ();
78d4d2c5 6233 }
8b70b953 6234
78d4d2c5
JK
6235 if (dwo_file)
6236 {
6237 sig_type = NULL;
6238 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6239 struct dwo_unit);
6240 dwo_tu->dwo_file = dwo_file;
43988095 6241 dwo_tu->signature = header.signature;
9c541725 6242 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6243 dwo_tu->section = section;
9c541725 6244 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6245 dwo_tu->length = length;
6246 }
6247 else
6248 {
6249 /* N.B.: type_offset is not usable if this type uses a DWO file.
6250 The real type_offset is in the DWO file. */
6251 dwo_tu = NULL;
6252 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6253 struct signatured_type);
43988095 6254 sig_type->signature = header.signature;
9c541725 6255 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6256 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6257 sig_type->per_cu.is_debug_types = 1;
6258 sig_type->per_cu.section = section;
9c541725 6259 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6260 sig_type->per_cu.length = length;
6261 }
6262
b0b6a987 6263 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6264 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6265 INSERT);
6266 gdb_assert (slot != NULL);
6267 if (*slot != NULL)
6268 {
9c541725 6269 sect_offset dup_sect_off;
0349ea22 6270
3019eac3
DE
6271 if (dwo_file)
6272 {
78d4d2c5
JK
6273 const struct dwo_unit *dup_tu
6274 = (const struct dwo_unit *) *slot;
6275
9c541725 6276 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6277 }
6278 else
6279 {
78d4d2c5
JK
6280 const struct signatured_type *dup_tu
6281 = (const struct signatured_type *) *slot;
6282
9c541725 6283 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6284 }
8b70b953 6285
b98664d3 6286 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6287 " the entry at offset %s, signature %s"),
6288 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6289 hex_string (header.signature));
78d4d2c5
JK
6290 }
6291 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6292
78d4d2c5 6293 if (dwarf_read_debug > 1)
9d8780f0
SM
6294 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6295 sect_offset_str (sect_off),
43988095 6296 hex_string (header.signature));
3019eac3 6297
78d4d2c5
JK
6298 info_ptr += length;
6299 }
6300}
3019eac3 6301
78d4d2c5
JK
6302/* Create the hash table of all entries in the .debug_types
6303 (or .debug_types.dwo) section(s).
6304 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6305 otherwise it is NULL.
b3c8eb43 6306
78d4d2c5 6307 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6308
78d4d2c5 6309 Note: This function processes DWO files only, not DWP files. */
348e048f 6310
78d4d2c5 6311static void
ed2dc618
SM
6312create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6313 struct dwo_file *dwo_file,
fd5866f6 6314 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6315 htab_up &types_htab)
78d4d2c5 6316{
fd5866f6
SM
6317 for (dwarf2_section_info &section : type_sections)
6318 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6319 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6320}
6321
6322/* Create the hash table of all entries in the .debug_types section,
6323 and initialize all_type_units.
6324 The result is zero if there is an error (e.g. missing .debug_types section),
6325 otherwise non-zero. */
6326
6327static int
ed2dc618 6328create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6329{
b0b6a987 6330 htab_up types_htab;
3019eac3 6331
ed2dc618
SM
6332 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6333 &dwarf2_per_objfile->info, types_htab,
43988095 6334 rcuh_kind::COMPILE);
ed2dc618
SM
6335 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6336 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6337 if (types_htab == NULL)
6338 {
6339 dwarf2_per_objfile->signatured_types = NULL;
6340 return 0;
6341 }
6342
b0b6a987 6343 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6344
b2bdb8cf 6345 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6346 dwarf2_per_objfile->all_type_units.reserve
6347 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6348
b0b6a987
TT
6349 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6350 add_signatured_type_cu_to_table,
b2bdb8cf 6351 &dwarf2_per_objfile->all_type_units);
1fd400ff 6352
348e048f
DE
6353 return 1;
6354}
6355
6aa5f3a6
DE
6356/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6357 If SLOT is non-NULL, it is the entry to use in the hash table.
6358 Otherwise we find one. */
6359
6360static struct signatured_type *
ed2dc618
SM
6361add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6362 void **slot)
6aa5f3a6
DE
6363{
6364 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6365
b2bdb8cf
SM
6366 if (dwarf2_per_objfile->all_type_units.size ()
6367 == dwarf2_per_objfile->all_type_units.capacity ())
6368 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6369
b2bdb8cf
SM
6370 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6371 struct signatured_type);
6372
6373 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6374 sig_type->signature = sig;
6375 sig_type->per_cu.is_debug_types = 1;
6376 if (dwarf2_per_objfile->using_index)
6377 {
6378 sig_type->per_cu.v.quick =
6379 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6380 struct dwarf2_per_cu_quick_data);
6381 }
6382
6383 if (slot == NULL)
6384 {
b0b6a987 6385 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6386 sig_type, INSERT);
6387 }
6388 gdb_assert (*slot == NULL);
6389 *slot = sig_type;
6390 /* The rest of sig_type must be filled in by the caller. */
6391 return sig_type;
6392}
6393
a2ce51a0
DE
6394/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6395 Fill in SIG_ENTRY with DWO_ENTRY. */
6396
6397static void
ed2dc618 6398fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6399 struct signatured_type *sig_entry,
6400 struct dwo_unit *dwo_entry)
6401{
7ee85ab1 6402 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6403 gdb_assert (! sig_entry->per_cu.queued);
6404 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6405 if (dwarf2_per_objfile->using_index)
6406 {
6407 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6408 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6409 }
6410 else
6411 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6412 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6413 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6414 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6415 gdb_assert (sig_entry->dwo_unit == NULL);
6416
6417 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6418 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6419 sig_entry->per_cu.length = dwo_entry->length;
6420 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6421 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6422 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6423 sig_entry->dwo_unit = dwo_entry;
6424}
6425
6426/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6427 If we haven't read the TU yet, create the signatured_type data structure
6428 for a TU to be read in directly from a DWO file, bypassing the stub.
6429 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6430 using .gdb_index, then when reading a CU we want to stay in the DWO file
6431 containing that CU. Otherwise we could end up reading several other DWO
6432 files (due to comdat folding) to process the transitive closure of all the
6433 mentioned TUs, and that can be slow. The current DWO file will have every
6434 type signature that it needs.
a2ce51a0
DE
6435 We only do this for .gdb_index because in the psymtab case we already have
6436 to read all the DWOs to build the type unit groups. */
6437
6438static struct signatured_type *
6439lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6440{
518817b3
SM
6441 struct dwarf2_per_objfile *dwarf2_per_objfile
6442 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6443 struct dwo_file *dwo_file;
6444 struct dwo_unit find_dwo_entry, *dwo_entry;
6445 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6446 void **slot;
a2ce51a0
DE
6447
6448 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6449
6aa5f3a6
DE
6450 /* If TU skeletons have been removed then we may not have read in any
6451 TUs yet. */
6452 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6453 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6454
6455 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6456 Use the global signatured_types array to do our own comdat-folding
6457 of types. If this is the first time we're reading this TU, and
6458 the TU has an entry in .gdb_index, replace the recorded data from
6459 .gdb_index with this TU. */
a2ce51a0 6460
a2ce51a0 6461 find_sig_entry.signature = sig;
b0b6a987 6462 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6463 &find_sig_entry, INSERT);
9a3c8263 6464 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6465
6466 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6467 read. Don't reassign the global entry to point to this DWO if that's
6468 the case. Also note that if the TU is already being read, it may not
6469 have come from a DWO, the program may be a mix of Fission-compiled
6470 code and non-Fission-compiled code. */
6471
6472 /* Have we already tried to read this TU?
6473 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6474 needn't exist in the global table yet). */
6475 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6476 return sig_entry;
6477
6aa5f3a6
DE
6478 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6479 dwo_unit of the TU itself. */
6480 dwo_file = cu->dwo_unit->dwo_file;
6481
a2ce51a0
DE
6482 /* Ok, this is the first time we're reading this TU. */
6483 if (dwo_file->tus == NULL)
6484 return NULL;
6485 find_dwo_entry.signature = sig;
b0b6a987
TT
6486 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6487 &find_dwo_entry);
a2ce51a0
DE
6488 if (dwo_entry == NULL)
6489 return NULL;
6490
6aa5f3a6
DE
6491 /* If the global table doesn't have an entry for this TU, add one. */
6492 if (sig_entry == NULL)
ed2dc618 6493 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6494
ed2dc618 6495 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6496 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6497 return sig_entry;
6498}
6499
a2ce51a0
DE
6500/* Subroutine of lookup_signatured_type.
6501 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6502 then try the DWP file. If the TU stub (skeleton) has been removed then
6503 it won't be in .gdb_index. */
a2ce51a0
DE
6504
6505static struct signatured_type *
6506lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6507{
518817b3
SM
6508 struct dwarf2_per_objfile *dwarf2_per_objfile
6509 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6510 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6511 struct dwo_unit *dwo_entry;
6512 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6513 void **slot;
a2ce51a0
DE
6514
6515 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6516 gdb_assert (dwp_file != NULL);
6517
6aa5f3a6
DE
6518 /* If TU skeletons have been removed then we may not have read in any
6519 TUs yet. */
6520 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6521 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6522
6aa5f3a6 6523 find_sig_entry.signature = sig;
b0b6a987 6524 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6525 &find_sig_entry, INSERT);
9a3c8263 6526 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6527
6528 /* Have we already tried to read this TU?
6529 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6530 needn't exist in the global table yet). */
6531 if (sig_entry != NULL)
6532 return sig_entry;
6533
a2ce51a0
DE
6534 if (dwp_file->tus == NULL)
6535 return NULL;
ed2dc618 6536 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6537 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6538 if (dwo_entry == NULL)
6539 return NULL;
6540
ed2dc618
SM
6541 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6542 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6543
a2ce51a0
DE
6544 return sig_entry;
6545}
6546
380bca97 6547/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6548 Returns NULL if signature SIG is not present in the table.
6549 It is up to the caller to complain about this. */
348e048f
DE
6550
6551static struct signatured_type *
a2ce51a0 6552lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6553{
518817b3
SM
6554 struct dwarf2_per_objfile *dwarf2_per_objfile
6555 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6556
a2ce51a0
DE
6557 if (cu->dwo_unit
6558 && dwarf2_per_objfile->using_index)
6559 {
6560 /* We're in a DWO/DWP file, and we're using .gdb_index.
6561 These cases require special processing. */
ed2dc618 6562 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6563 return lookup_dwo_signatured_type (cu, sig);
6564 else
6565 return lookup_dwp_signatured_type (cu, sig);
6566 }
6567 else
6568 {
6569 struct signatured_type find_entry, *entry;
348e048f 6570
a2ce51a0
DE
6571 if (dwarf2_per_objfile->signatured_types == NULL)
6572 return NULL;
6573 find_entry.signature = sig;
9a3c8263 6574 entry = ((struct signatured_type *)
b0b6a987
TT
6575 htab_find (dwarf2_per_objfile->signatured_types.get (),
6576 &find_entry));
a2ce51a0
DE
6577 return entry;
6578 }
348e048f 6579}
18a8505e 6580
42e7ad6c 6581/* Low level DIE reading support. */
348e048f 6582
d85a05f0
DJ
6583/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6584
6585static void
6586init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6587 struct dwarf2_cu *cu,
3019eac3 6588 struct dwarf2_section_info *section,
685af9cd
TT
6589 struct dwo_file *dwo_file,
6590 struct abbrev_table *abbrev_table)
d85a05f0 6591{
fceca515 6592 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6593 reader->abfd = section->get_bfd_owner ();
d85a05f0 6594 reader->cu = cu;
3019eac3 6595 reader->dwo_file = dwo_file;
dee91e82
DE
6596 reader->die_section = section;
6597 reader->buffer = section->buffer;
f664829e 6598 reader->buffer_end = section->buffer + section->size;
685af9cd 6599 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6600}
6601
c0ab21c2 6602/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6603 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6604 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6605 already.
6606
6607 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6608 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6609 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6610 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6611 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6612 STUB_COMP_DIR may be non-NULL.
3e225074 6613 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6614 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6615 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6616 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6617 kept around for at least as long as *RESULT_READER.
6618
b0c7bfa9
DE
6619 The result is non-zero if a valid (non-dummy) DIE was found. */
6620
6621static int
6622read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6623 struct dwo_unit *dwo_unit,
b0c7bfa9 6624 struct die_info *stub_comp_unit_die,
a2ce51a0 6625 const char *stub_comp_dir,
b0c7bfa9 6626 struct die_reader_specs *result_reader,
d521ce57 6627 const gdb_byte **result_info_ptr,
b0c7bfa9 6628 struct die_info **result_comp_unit_die,
685af9cd 6629 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6630{
ed2dc618 6631 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6632 struct objfile *objfile = dwarf2_per_objfile->objfile;
6633 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6634 bfd *abfd;
d521ce57 6635 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6636 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6637 int i,num_extra_attrs;
6638 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6639 struct die_info *comp_unit_die;
6640
b0aeadb3
DE
6641 /* At most one of these may be provided. */
6642 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6643
b0c7bfa9
DE
6644 /* These attributes aren't processed until later:
6645 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6646 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6647 referenced later. However, these attributes are found in the stub
6648 which we won't have later. In order to not impose this complication
6649 on the rest of the code, we read them here and copy them to the
6650 DWO CU/TU die. */
b0c7bfa9
DE
6651
6652 stmt_list = NULL;
6653 low_pc = NULL;
6654 high_pc = NULL;
6655 ranges = NULL;
6656 comp_dir = NULL;
6657
6658 if (stub_comp_unit_die != NULL)
6659 {
6660 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6661 DWO file. */
6662 if (! this_cu->is_debug_types)
6663 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6664 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6665 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6666 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6667 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6668
a39fdb41 6669 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6670
18a8505e
AT
6671 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6672 here (if needed). We need the value before we can process
6673 DW_AT_ranges. */
a39fdb41 6674 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6675 }
a2ce51a0
DE
6676 else if (stub_comp_dir != NULL)
6677 {
6678 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6679 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6680 comp_dir->name = DW_AT_comp_dir;
6681 comp_dir->form = DW_FORM_string;
6682 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6683 DW_STRING (comp_dir) = stub_comp_dir;
6684 }
b0c7bfa9
DE
6685
6686 /* Set up for reading the DWO CU/TU. */
6687 cu->dwo_unit = dwo_unit;
685af9cd 6688 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6689 section->read (objfile);
6690 abfd = section->get_bfd_owner ();
9c541725
PA
6691 begin_info_ptr = info_ptr = (section->buffer
6692 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6693 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6694
6695 if (this_cu->is_debug_types)
6696 {
b0c7bfa9
DE
6697 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6698
ed2dc618
SM
6699 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6700 &cu->header, section,
b0c7bfa9 6701 dwo_abbrev_section,
43988095 6702 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6703 /* This is not an assert because it can be caused by bad debug info. */
43988095 6704 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6705 {
6706 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6707 " TU at offset %s [in module %s]"),
a2ce51a0 6708 hex_string (sig_type->signature),
43988095 6709 hex_string (cu->header.signature),
9d8780f0 6710 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6711 bfd_get_filename (abfd));
6712 }
9c541725 6713 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6714 /* For DWOs coming from DWP files, we don't know the CU length
6715 nor the type's offset in the TU until now. */
4057dfde 6716 dwo_unit->length = cu->header.get_length ();
9c541725 6717 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6718
6719 /* Establish the type offset that can be used to lookup the type.
6720 For DWO files, we don't know it until now. */
9c541725
PA
6721 sig_type->type_offset_in_section
6722 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6723 }
6724 else
6725 {
ed2dc618
SM
6726 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6727 &cu->header, section,
b0c7bfa9 6728 dwo_abbrev_section,
43988095 6729 info_ptr, rcuh_kind::COMPILE);
9c541725 6730 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6731 /* For DWOs coming from DWP files, we don't know the CU length
6732 until now. */
4057dfde 6733 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6734 }
6735
685af9cd 6736 *result_dwo_abbrev_table
86de1d91
TT
6737 = abbrev_table::read (objfile, dwo_abbrev_section,
6738 cu->header.abbrev_sect_off);
685af9cd
TT
6739 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6740 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6741
6742 /* Read in the die, but leave space to copy over the attributes
6743 from the stub. This has the benefit of simplifying the rest of
6744 the code - all the work to maintain the illusion of a single
6745 DW_TAG_{compile,type}_unit DIE is done here. */
6746 num_extra_attrs = ((stmt_list != NULL)
6747 + (low_pc != NULL)
6748 + (high_pc != NULL)
6749 + (ranges != NULL)
6750 + (comp_dir != NULL));
6751 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6752 num_extra_attrs);
b0c7bfa9
DE
6753
6754 /* Copy over the attributes from the stub to the DIE we just read in. */
6755 comp_unit_die = *result_comp_unit_die;
6756 i = comp_unit_die->num_attrs;
6757 if (stmt_list != NULL)
6758 comp_unit_die->attrs[i++] = *stmt_list;
6759 if (low_pc != NULL)
6760 comp_unit_die->attrs[i++] = *low_pc;
6761 if (high_pc != NULL)
6762 comp_unit_die->attrs[i++] = *high_pc;
6763 if (ranges != NULL)
6764 comp_unit_die->attrs[i++] = *ranges;
6765 if (comp_dir != NULL)
6766 comp_unit_die->attrs[i++] = *comp_dir;
6767 comp_unit_die->num_attrs += num_extra_attrs;
6768
b4f54984 6769 if (dwarf_die_debug)
bf6af496
DE
6770 {
6771 fprintf_unfiltered (gdb_stdlog,
6772 "Read die from %s@0x%x of %s:\n",
96b79293 6773 section->get_name (),
bf6af496
DE
6774 (unsigned) (begin_info_ptr - section->buffer),
6775 bfd_get_filename (abfd));
b4f54984 6776 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6777 }
6778
b0c7bfa9
DE
6779 /* Skip dummy compilation units. */
6780 if (info_ptr >= begin_info_ptr + dwo_unit->length
6781 || peek_abbrev_code (abfd, info_ptr) == 0)
6782 return 0;
6783
6784 *result_info_ptr = info_ptr;
6785 return 1;
6786}
6787
a084a2a6
AT
6788/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6789 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6790 signature is part of the header. */
6791static gdb::optional<ULONGEST>
6792lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6793{
6794 if (cu->header.version >= 5)
6795 return cu->header.signature;
6796 struct attribute *attr;
6797 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6798 if (attr == nullptr)
6799 return gdb::optional<ULONGEST> ();
6800 return DW_UNSND (attr);
6801}
6802
c0ab21c2 6803/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6804 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6805 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6806
6807static struct dwo_unit *
6808lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6809 struct die_info *comp_unit_die,
6810 const char *dwo_name)
b0c7bfa9
DE
6811{
6812 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6813 struct dwo_unit *dwo_unit;
c0ab21c2 6814 const char *comp_dir;
b0c7bfa9 6815
a2ce51a0
DE
6816 gdb_assert (cu != NULL);
6817
b0c7bfa9 6818 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6819 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6820 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6821
6822 if (this_cu->is_debug_types)
6823 {
6824 struct signatured_type *sig_type;
6825
6826 /* Since this_cu is the first member of struct signatured_type,
6827 we can go from a pointer to one to a pointer to the other. */
6828 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6829 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6830 }
6831 else
6832 {
a084a2a6
AT
6833 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6834 if (!signature.has_value ())
b0c7bfa9
DE
6835 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6836 " [in module %s]"),
e3b94546 6837 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6838 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6839 *signature);
b0c7bfa9
DE
6840 }
6841
b0c7bfa9
DE
6842 return dwo_unit;
6843}
6844
c0ab21c2 6845/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6846 See it for a description of the parameters.
fcd3b13d 6847 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6848
c0ab21c2
TT
6849void
6850cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6851 int use_existing_cu)
a2ce51a0 6852{
a2ce51a0 6853 struct signatured_type *sig_type;
a2ce51a0
DE
6854
6855 /* Verify we can do the following downcast, and that we have the
6856 data we need. */
6857 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6858 sig_type = (struct signatured_type *) this_cu;
6859 gdb_assert (sig_type->dwo_unit != NULL);
6860
6aa5f3a6
DE
6861 if (use_existing_cu && this_cu->cu != NULL)
6862 {
6863 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6864 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6865 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6866 }
6867 else
6868 {
6869 /* If !use_existing_cu, this_cu->cu must be NULL. */
6870 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6871 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6872 }
6873
6874 /* A future optimization, if needed, would be to use an existing
6875 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6876 could share abbrev tables. */
a2ce51a0
DE
6877
6878 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6879 NULL /* stub_comp_unit_die */,
6880 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6881 this, &info_ptr,
3e225074 6882 &comp_unit_die,
c0ab21c2 6883 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6884 {
6885 /* Dummy die. */
c0ab21c2 6886 dummy_p = true;
a2ce51a0 6887 }
a2ce51a0
DE
6888}
6889
fd820528 6890/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6891 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6892
f4dc4d17
DE
6893 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6894 Otherwise the table specified in the comp unit header is read in and used.
6895 This is an optimization for when we already have the abbrev table.
6896
dee91e82 6897 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6898 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6899
c0ab21c2
TT
6900cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6901 struct abbrev_table *abbrev_table,
6751ebae 6902 int use_existing_cu,
c0ab21c2
TT
6903 bool skip_partial)
6904 : die_reader_specs {},
6751ebae 6905 m_this_cu (this_cu)
c906108c 6906{
ed2dc618 6907 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6908 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6909 struct dwarf2_section_info *section = this_cu->section;
96b79293 6910 bfd *abfd = section->get_bfd_owner ();
dee91e82 6911 struct dwarf2_cu *cu;
c0ab21c2 6912 const gdb_byte *begin_info_ptr;
dee91e82 6913 struct signatured_type *sig_type = NULL;
4bdcc0c1 6914 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6915 /* Non-zero if CU currently points to a DWO file and we need to
6916 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6917 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6918 int rereading_dwo_cu = 0;
c906108c 6919
b4f54984 6920 if (dwarf_die_debug)
9d8780f0 6921 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6922 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6923 sect_offset_str (this_cu->sect_off));
09406207 6924
a2ce51a0
DE
6925 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6926 file (instead of going through the stub), short-circuit all of this. */
6927 if (this_cu->reading_dwo_directly)
6928 {
6929 /* Narrow down the scope of possibilities to have to understand. */
6930 gdb_assert (this_cu->is_debug_types);
6931 gdb_assert (abbrev_table == NULL);
6751ebae 6932 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6933 return;
6934 }
6935
dee91e82 6936 /* This is cheap if the section is already read in. */
96b79293 6937 section->read (objfile);
dee91e82 6938
9c541725 6939 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6940
6941 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6942
6943 if (use_existing_cu && this_cu->cu != NULL)
6944 {
6945 cu = this_cu->cu;
42e7ad6c
DE
6946 /* If this CU is from a DWO file we need to start over, we need to
6947 refetch the attributes from the skeleton CU.
6948 This could be optimized by retrieving those attributes from when we
6949 were here the first time: the previous comp_unit_die was stored in
6950 comp_unit_obstack. But there's no data yet that we need this
6951 optimization. */
6952 if (cu->dwo_unit != NULL)
6953 rereading_dwo_cu = 1;
dee91e82
DE
6954 }
6955 else
6956 {
6957 /* If !use_existing_cu, this_cu->cu must be NULL. */
6958 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6959 m_new_cu.reset (new dwarf2_cu (this_cu));
6960 cu = m_new_cu.get ();
42e7ad6c 6961 }
dee91e82 6962
b0c7bfa9 6963 /* Get the header. */
9c541725 6964 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6965 {
6966 /* We already have the header, there's no need to read it in again. */
9c541725 6967 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6968 }
6969 else
6970 {
3019eac3 6971 if (this_cu->is_debug_types)
dee91e82 6972 {
ed2dc618
SM
6973 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6974 &cu->header, section,
4bdcc0c1 6975 abbrev_section, info_ptr,
43988095 6976 rcuh_kind::TYPE);
dee91e82 6977
42e7ad6c
DE
6978 /* Since per_cu is the first member of struct signatured_type,
6979 we can go from a pointer to one to a pointer to the other. */
6980 sig_type = (struct signatured_type *) this_cu;
43988095 6981 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6982 gdb_assert (sig_type->type_offset_in_tu
6983 == cu->header.type_cu_offset_in_tu);
6984 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6985
42e7ad6c
DE
6986 /* LENGTH has not been set yet for type units if we're
6987 using .gdb_index. */
4057dfde 6988 this_cu->length = cu->header.get_length ();
3019eac3
DE
6989
6990 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6991 sig_type->type_offset_in_section =
6992 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6993
6994 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6995 }
6996 else
6997 {
ed2dc618
SM
6998 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6999 &cu->header, section,
4bdcc0c1 7000 abbrev_section,
43988095
JK
7001 info_ptr,
7002 rcuh_kind::COMPILE);
dee91e82 7003
9c541725 7004 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7005 if (this_cu->length == 0)
7006 this_cu->length = cu->header.get_length ();
7007 else
7008 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7009 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7010 }
7011 }
10b3939b 7012
6caca83c 7013 /* Skip dummy compilation units. */
dee91e82 7014 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7015 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7016 {
7017 dummy_p = true;
7018 return;
7019 }
6caca83c 7020
433df2d4
DE
7021 /* If we don't have them yet, read the abbrevs for this compilation unit.
7022 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7023 done. */
f4dc4d17 7024 if (abbrev_table != NULL)
685af9cd
TT
7025 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7026 else
f4dc4d17 7027 {
c0ab21c2 7028 m_abbrev_table_holder
86de1d91
TT
7029 = abbrev_table::read (objfile, abbrev_section,
7030 cu->header.abbrev_sect_off);
c0ab21c2 7031 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7032 }
af703f96 7033
dee91e82 7034 /* Read the top level CU/TU die. */
c0ab21c2 7035 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7036 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7037
58f0c718 7038 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7039 {
7040 dummy_p = true;
7041 return;
7042 }
58f0c718 7043
b0c7bfa9 7044 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7045 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7046 table from the DWO file and pass the ownership over to us. It will be
7047 referenced from READER, so we must make sure to free it after we're done
7048 with READER.
7049
b0c7bfa9
DE
7050 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7051 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7052 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7053 if (dwo_name != nullptr)
3019eac3 7054 {
3019eac3 7055 struct dwo_unit *dwo_unit;
b0c7bfa9 7056 struct die_info *dwo_comp_unit_die;
3019eac3 7057
3e225074 7058 if (comp_unit_die->has_children)
6a506a2d 7059 {
b98664d3 7060 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7061 " has children (offset %s) [in module %s]"),
7062 sect_offset_str (this_cu->sect_off),
7063 bfd_get_filename (abfd));
6a506a2d 7064 }
c0ab21c2 7065 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7066 if (dwo_unit != NULL)
3019eac3 7067 {
6a506a2d 7068 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7069 comp_unit_die, NULL,
c0ab21c2 7070 this, &info_ptr,
3e225074 7071 &dwo_comp_unit_die,
c0ab21c2 7072 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7073 {
7074 /* Dummy die. */
c0ab21c2 7075 dummy_p = true;
6a506a2d
DE
7076 return;
7077 }
7078 comp_unit_die = dwo_comp_unit_die;
7079 }
7080 else
7081 {
7082 /* Yikes, we couldn't find the rest of the DIE, we only have
7083 the stub. A complaint has already been logged. There's
7084 not much more we can do except pass on the stub DIE to
7085 die_reader_func. We don't want to throw an error on bad
7086 debug info. */
3019eac3
DE
7087 }
7088 }
c0ab21c2 7089}
3019eac3 7090
6751ebae
TT
7091void
7092cutu_reader::keep ()
c0ab21c2 7093{
b0c7bfa9 7094 /* Done, clean up. */
6751ebae
TT
7095 gdb_assert (!dummy_p);
7096 if (m_new_cu != NULL)
348e048f 7097 {
c0ab21c2
TT
7098 struct dwarf2_per_objfile *dwarf2_per_objfile
7099 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7100 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7101 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7102 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7103 /* The chain owns it now. */
c0ab21c2 7104 m_new_cu.release ();
348e048f 7105 }
dee91e82
DE
7106}
7107
18a8505e
AT
7108/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7109 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7110 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7111
7112 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7113 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7114
7115 We fill in THIS_CU->length.
7116
dee91e82 7117 THIS_CU->cu is always freed when done.
3019eac3 7118 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7119 to care whether it refers to the "main" CU or the DWO CU.
7120
7121 When parent_cu is passed, it is used to provide a default value for
7122 str_offsets_base and addr_base from the parent. */
dee91e82 7123
c0ab21c2
TT
7124cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7125 struct dwarf2_cu *parent_cu,
7126 struct dwo_file *dwo_file)
7127 : die_reader_specs {},
7128 m_this_cu (this_cu)
dee91e82 7129{
ed2dc618 7130 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7131 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7132 struct dwarf2_section_info *section = this_cu->section;
96b79293 7133 bfd *abfd = section->get_bfd_owner ();
33e80786 7134 struct dwarf2_section_info *abbrev_section;
d521ce57 7135 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7136
b4f54984 7137 if (dwarf_die_debug)
9d8780f0 7138 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7139 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7140 sect_offset_str (this_cu->sect_off));
09406207 7141
dee91e82
DE
7142 gdb_assert (this_cu->cu == NULL);
7143
33e80786
DE
7144 abbrev_section = (dwo_file != NULL
7145 ? &dwo_file->sections.abbrev
7146 : get_abbrev_section_for_cu (this_cu));
7147
dee91e82 7148 /* This is cheap if the section is already read in. */
96b79293 7149 section->read (objfile);
dee91e82 7150
c0ab21c2 7151 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7152
9c541725 7153 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7154 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7155 &m_new_cu->header, section,
4bdcc0c1 7156 abbrev_section, info_ptr,
43988095
JK
7157 (this_cu->is_debug_types
7158 ? rcuh_kind::TYPE
7159 : rcuh_kind::COMPILE));
dee91e82 7160
18a8505e
AT
7161 if (parent_cu != nullptr)
7162 {
c0ab21c2
TT
7163 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7164 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7165 }
4057dfde 7166 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7167
7168 /* Skip dummy compilation units. */
7169 if (info_ptr >= begin_info_ptr + this_cu->length
7170 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7171 {
7172 dummy_p = true;
7173 return;
7174 }
72bf9492 7175
c0ab21c2 7176 m_abbrev_table_holder
86de1d91
TT
7177 = abbrev_table::read (objfile, abbrev_section,
7178 m_new_cu->header.abbrev_sect_off);
dee91e82 7179
c0ab21c2
TT
7180 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7181 m_abbrev_table_holder.get ());
3e225074 7182 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7183}
7184
0018ea6f
DE
7185\f
7186/* Type Unit Groups.
dee91e82 7187
0018ea6f
DE
7188 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7189 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7190 so that all types coming from the same compilation (.o file) are grouped
7191 together. A future step could be to put the types in the same symtab as
7192 the CU the types ultimately came from. */
ff013f42 7193
f4dc4d17
DE
7194static hashval_t
7195hash_type_unit_group (const void *item)
7196{
9a3c8263
SM
7197 const struct type_unit_group *tu_group
7198 = (const struct type_unit_group *) item;
f4dc4d17 7199
094b34ac 7200 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7201}
348e048f
DE
7202
7203static int
f4dc4d17 7204eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7205{
9a3c8263
SM
7206 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7207 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7208
094b34ac 7209 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7210}
348e048f 7211
f4dc4d17
DE
7212/* Allocate a hash table for type unit groups. */
7213
eaa5fa8b 7214static htab_up
298e9637 7215allocate_type_unit_groups_table ()
f4dc4d17 7216{
eaa5fa8b
TT
7217 return htab_up (htab_create_alloc (3,
7218 hash_type_unit_group,
7219 eq_type_unit_group,
7220 NULL, xcalloc, xfree));
f4dc4d17 7221}
dee91e82 7222
f4dc4d17
DE
7223/* Type units that don't have DW_AT_stmt_list are grouped into their own
7224 partial symtabs. We combine several TUs per psymtab to not let the size
7225 of any one psymtab grow too big. */
7226#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7227#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7228
094b34ac 7229/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7230 Create the type_unit_group object used to hold one or more TUs. */
7231
7232static struct type_unit_group *
094b34ac 7233create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7234{
518817b3
SM
7235 struct dwarf2_per_objfile *dwarf2_per_objfile
7236 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7237 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7238 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7239 struct type_unit_group *tu_group;
f4dc4d17
DE
7240
7241 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7242 struct type_unit_group);
094b34ac 7243 per_cu = &tu_group->per_cu;
518817b3 7244 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7245
094b34ac
DE
7246 if (dwarf2_per_objfile->using_index)
7247 {
7248 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7249 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7250 }
7251 else
7252 {
9c541725 7253 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7254 dwarf2_psymtab *pst;
528e1572 7255 std::string name;
094b34ac
DE
7256
7257 /* Give the symtab a useful name for debug purposes. */
7258 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7259 name = string_printf ("<type_units_%d>",
7260 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7261 else
528e1572 7262 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7263
528e1572 7264 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7265 pst->anonymous = true;
094b34ac 7266 }
f4dc4d17 7267
094b34ac 7268 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7269 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7270
7271 return tu_group;
7272}
7273
094b34ac
DE
7274/* Look up the type_unit_group for type unit CU, and create it if necessary.
7275 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7276
7277static struct type_unit_group *
ff39bb5e 7278get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7279{
518817b3
SM
7280 struct dwarf2_per_objfile *dwarf2_per_objfile
7281 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7282 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7283 struct type_unit_group *tu_group;
7284 void **slot;
7285 unsigned int line_offset;
7286 struct type_unit_group type_unit_group_for_lookup;
7287
7288 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7289 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7290
7291 /* Do we need to create a new group, or can we use an existing one? */
7292
7293 if (stmt_list)
7294 {
7295 line_offset = DW_UNSND (stmt_list);
7296 ++tu_stats->nr_symtab_sharers;
7297 }
7298 else
7299 {
7300 /* Ugh, no stmt_list. Rare, but we have to handle it.
7301 We can do various things here like create one group per TU or
7302 spread them over multiple groups to split up the expansion work.
7303 To avoid worst case scenarios (too many groups or too large groups)
7304 we, umm, group them in bunches. */
7305 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7306 | (tu_stats->nr_stmt_less_type_units
7307 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7308 ++tu_stats->nr_stmt_less_type_units;
7309 }
7310
094b34ac 7311 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7312 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7313 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7314 &type_unit_group_for_lookup, INSERT);
7315 if (*slot != NULL)
7316 {
9a3c8263 7317 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7318 gdb_assert (tu_group != NULL);
7319 }
7320 else
7321 {
9c541725 7322 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7323 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7324 *slot = tu_group;
7325 ++tu_stats->nr_symtabs;
7326 }
7327
7328 return tu_group;
7329}
0018ea6f
DE
7330\f
7331/* Partial symbol tables. */
7332
7333/* Create a psymtab named NAME and assign it to PER_CU.
7334
7335 The caller must fill in the following details:
7336 dirname, textlow, texthigh. */
7337
891813be 7338static dwarf2_psymtab *
0018ea6f
DE
7339create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7340{
e3b94546 7341 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7342 dwarf2_psymtab *pst;
0018ea6f 7343
9f4e76a4 7344 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7345
6d94535f 7346 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7347
7348 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7349 per_cu->v.psymtab = pst;
7350
7351 return pst;
7352}
7353
c0ab21c2 7354/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7355
7356static void
7357process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7358 const gdb_byte *info_ptr,
0018ea6f 7359 struct die_info *comp_unit_die,
c0ab21c2 7360 enum language pretend_language)
0018ea6f
DE
7361{
7362 struct dwarf2_cu *cu = reader->cu;
518817b3 7363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7364 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7365 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7366 CORE_ADDR baseaddr;
7367 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7368 dwarf2_psymtab *pst;
3a2b436a 7369 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7370 const char *filename;
0018ea6f 7371
0018ea6f
DE
7372 gdb_assert (! per_cu->is_debug_types);
7373
c0ab21c2 7374 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7375
0018ea6f 7376 /* Allocate a new partial symbol table structure. */
2e927613
TV
7377 gdb::unique_xmalloc_ptr<char> debug_filename;
7378 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7379 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7380 if (filename == NULL)
0018ea6f 7381 filename = "";
2e927613
TV
7382 else if (strcmp (filename, artificial) == 0)
7383 {
7384 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7385 sect_offset_str (per_cu->sect_off),
7386 (char *) NULL));
2e927613
TV
7387 filename = debug_filename.get ();
7388 }
0018ea6f
DE
7389
7390 pst = create_partial_symtab (per_cu, filename);
7391
7392 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7393 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7394
b3b3bada 7395 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7396
7397 dwarf2_find_base_address (comp_unit_die, cu);
7398
7399 /* Possibly set the default values of LOWPC and HIGHPC from
7400 `DW_AT_ranges'. */
3a2b436a
JK
7401 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7402 &best_highpc, cu, pst);
7403 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7404 {
7405 CORE_ADDR low
7406 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7407 - baseaddr);
7408 CORE_ADDR high
7409 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7410 - baseaddr - 1);
7411 /* Store the contiguous range if it is not empty; it can be
7412 empty for CUs with no code. */
d320c2b5
TT
7413 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7414 low, high, pst);
79748972 7415 }
0018ea6f
DE
7416
7417 /* Check if comp unit has_children.
7418 If so, read the rest of the partial symbols from this comp unit.
7419 If not, there's no more debug_info for this comp unit. */
3e225074 7420 if (comp_unit_die->has_children)
0018ea6f
DE
7421 {
7422 struct partial_die_info *first_die;
7423 CORE_ADDR lowpc, highpc;
7424
7425 lowpc = ((CORE_ADDR) -1);
7426 highpc = ((CORE_ADDR) 0);
7427
7428 first_die = load_partial_dies (reader, info_ptr, 1);
7429
7430 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7431 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7432
7433 /* If we didn't find a lowpc, set it to highpc to avoid
7434 complaints from `maint check'. */
7435 if (lowpc == ((CORE_ADDR) -1))
7436 lowpc = highpc;
7437
7438 /* If the compilation unit didn't have an explicit address range,
7439 then use the information extracted from its child dies. */
e385593e 7440 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7441 {
7442 best_lowpc = lowpc;
7443 best_highpc = highpc;
7444 }
7445 }
4ae976d1 7446 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7447 best_lowpc + baseaddr)
7448 - baseaddr);
4ae976d1 7449 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7450 best_highpc + baseaddr)
7451 - baseaddr);
0018ea6f 7452
8763cede 7453 end_psymtab_common (objfile, pst);
0018ea6f 7454
ae640021 7455 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7456 {
7457 int i;
ae640021 7458 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7459
7460 /* Fill in 'dependencies' here; we fill in 'users' in a
7461 post-pass. */
7462 pst->number_of_dependencies = len;
a9342b62
TT
7463 pst->dependencies
7464 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7465 for (i = 0; i < len; ++i)
7466 {
7467 pst->dependencies[i]
7468 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7469 }
0018ea6f 7470
ae640021 7471 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7472 }
7473
7474 /* Get the list of files included in the current compilation unit,
7475 and build a psymtab for each of them. */
7476 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7477
b4f54984 7478 if (dwarf_read_debug)
b926417a
TT
7479 fprintf_unfiltered (gdb_stdlog,
7480 "Psymtab for %s unit @%s: %s - %s"
7481 ", %d global, %d static syms\n",
7482 per_cu->is_debug_types ? "type" : "comp",
7483 sect_offset_str (per_cu->sect_off),
7484 paddress (gdbarch, pst->text_low (objfile)),
7485 paddress (gdbarch, pst->text_high (objfile)),
7486 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7487}
7488
7489/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7490 Process compilation unit THIS_CU for a psymtab. */
7491
7492static void
7493process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7494 bool want_partial_unit,
b93601f3 7495 enum language pretend_language)
0018ea6f
DE
7496{
7497 /* If this compilation unit was already read in, free the
7498 cached copy in order to read it in again. This is
7499 necessary because we skipped some symbols when we first
7500 read in the compilation unit (see load_partial_dies).
7501 This problem could be avoided, but the benefit is unclear. */
7502 if (this_cu->cu != NULL)
7503 free_one_cached_comp_unit (this_cu);
7504
6751ebae 7505 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7506
58990295
TV
7507 switch (reader.comp_unit_die->tag)
7508 {
7509 case DW_TAG_compile_unit:
7510 this_cu->unit_type = DW_UT_compile;
7511 break;
7512 case DW_TAG_partial_unit:
7513 this_cu->unit_type = DW_UT_partial;
7514 break;
7515 default:
7516 abort ();
7517 }
7518
c0ab21c2 7519 if (reader.dummy_p)
f1902523 7520 {
c0ab21c2 7521 /* Nothing. */
f1902523 7522 }
c0ab21c2 7523 else if (this_cu->is_debug_types)
3e225074
TT
7524 build_type_psymtabs_reader (&reader, reader.info_ptr,
7525 reader.comp_unit_die);
135f5437
TT
7526 else if (want_partial_unit
7527 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7528 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7529 reader.comp_unit_die,
c0ab21c2 7530 pretend_language);
0018ea6f 7531
58990295
TV
7532 this_cu->lang = this_cu->cu->language;
7533
0018ea6f 7534 /* Age out any secondary CUs. */
ed2dc618 7535 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7536}
f4dc4d17
DE
7537
7538/* Reader function for build_type_psymtabs. */
7539
7540static void
7541build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7542 const gdb_byte *info_ptr,
3e225074 7543 struct die_info *type_unit_die)
f4dc4d17 7544{
ed2dc618 7545 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7546 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7547 struct objfile *objfile = dwarf2_per_objfile->objfile;
7548 struct dwarf2_cu *cu = reader->cu;
7549 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7550 struct signatured_type *sig_type;
f4dc4d17
DE
7551 struct type_unit_group *tu_group;
7552 struct attribute *attr;
7553 struct partial_die_info *first_die;
7554 CORE_ADDR lowpc, highpc;
891813be 7555 dwarf2_psymtab *pst;
f4dc4d17 7556
0186c6a7
DE
7557 gdb_assert (per_cu->is_debug_types);
7558 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7559
3e225074 7560 if (! type_unit_die->has_children)
f4dc4d17
DE
7561 return;
7562
052c8bb8 7563 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7564 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7565
df07e2c7 7566 if (tu_group->tus == nullptr)
a8b3b8e9 7567 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7568 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7569
7570 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7571 pst = create_partial_symtab (per_cu, "");
6d94535f 7572 pst->anonymous = true;
f4dc4d17
DE
7573
7574 first_die = load_partial_dies (reader, info_ptr, 1);
7575
7576 lowpc = (CORE_ADDR) -1;
7577 highpc = (CORE_ADDR) 0;
7578 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7579
8763cede 7580 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7581}
7582
73051182
DE
7583/* Struct used to sort TUs by their abbreviation table offset. */
7584
7585struct tu_abbrev_offset
7586{
b2bdb8cf
SM
7587 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7588 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7589 {}
7590
7591 signatured_type *sig_type;
73051182
DE
7592 sect_offset abbrev_offset;
7593};
7594
484cf504 7595/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7596
484cf504
TT
7597static bool
7598sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7599 const struct tu_abbrev_offset &b)
73051182 7600{
484cf504 7601 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7602}
7603
7604/* Efficiently read all the type units.
7605 This does the bulk of the work for build_type_psymtabs.
7606
7607 The efficiency is because we sort TUs by the abbrev table they use and
7608 only read each abbrev table once. In one program there are 200K TUs
7609 sharing 8K abbrev tables.
7610
7611 The main purpose of this function is to support building the
7612 dwarf2_per_objfile->type_unit_groups table.
7613 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7614 can collapse the search space by grouping them by stmt_list.
7615 The savings can be significant, in the same program from above the 200K TUs
7616 share 8K stmt_list tables.
7617
7618 FUNC is expected to call get_type_unit_group, which will create the
7619 struct type_unit_group if necessary and add it to
7620 dwarf2_per_objfile->type_unit_groups. */
7621
7622static void
ed2dc618 7623build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7624{
73051182 7625 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7626 abbrev_table_up abbrev_table;
73051182 7627 sect_offset abbrev_offset;
73051182
DE
7628
7629 /* It's up to the caller to not call us multiple times. */
7630 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7631
b2bdb8cf 7632 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7633 return;
7634
7635 /* TUs typically share abbrev tables, and there can be way more TUs than
7636 abbrev tables. Sort by abbrev table to reduce the number of times we
7637 read each abbrev table in.
7638 Alternatives are to punt or to maintain a cache of abbrev tables.
7639 This is simpler and efficient enough for now.
7640
7641 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7642 symtab to use). Typically TUs with the same abbrev offset have the same
7643 stmt_list value too so in practice this should work well.
7644
7645 The basic algorithm here is:
7646
7647 sort TUs by abbrev table
7648 for each TU with same abbrev table:
7649 read abbrev table if first user
7650 read TU top level DIE
7651 [IWBN if DWO skeletons had DW_AT_stmt_list]
7652 call FUNC */
7653
b4f54984 7654 if (dwarf_read_debug)
73051182
DE
7655 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7656
7657 /* Sort in a separate table to maintain the order of all_type_units
7658 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7659 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7660 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7661
7662 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7663 sorted_by_abbrev.emplace_back
7664 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7665 sig_type->per_cu.section,
7666 sig_type->per_cu.sect_off));
73051182 7667
484cf504
TT
7668 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7669 sort_tu_by_abbrev_offset);
73051182 7670
9c541725 7671 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7672
b2bdb8cf 7673 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7674 {
73051182
DE
7675 /* Switch to the next abbrev table if necessary. */
7676 if (abbrev_table == NULL
b2bdb8cf 7677 || tu.abbrev_offset != abbrev_offset)
73051182 7678 {
b2bdb8cf 7679 abbrev_offset = tu.abbrev_offset;
73051182 7680 abbrev_table =
86de1d91
TT
7681 abbrev_table::read (dwarf2_per_objfile->objfile,
7682 &dwarf2_per_objfile->abbrev,
7683 abbrev_offset);
73051182
DE
7684 ++tu_stats->nr_uniq_abbrev_tables;
7685 }
7686
c0ab21c2 7687 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7688 0, false);
c0ab21c2
TT
7689 if (!reader.dummy_p)
7690 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7691 reader.comp_unit_die);
73051182 7692 }
6aa5f3a6 7693}
73051182 7694
6aa5f3a6
DE
7695/* Print collected type unit statistics. */
7696
7697static void
ed2dc618 7698print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7699{
7700 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7701
7702 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7703 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7704 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7705 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7706 tu_stats->nr_uniq_abbrev_tables);
7707 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7708 tu_stats->nr_symtabs);
7709 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7710 tu_stats->nr_symtab_sharers);
7711 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7712 tu_stats->nr_stmt_less_type_units);
7713 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7714 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7715}
7716
f4dc4d17
DE
7717/* Traversal function for build_type_psymtabs. */
7718
7719static int
7720build_type_psymtab_dependencies (void **slot, void *info)
7721{
ed2dc618
SM
7722 struct dwarf2_per_objfile *dwarf2_per_objfile
7723 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7724 struct objfile *objfile = dwarf2_per_objfile->objfile;
7725 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7726 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7727 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7728 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7729 int i;
7730
7731 gdb_assert (len > 0);
197400e8 7732 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7733
7734 pst->number_of_dependencies = len;
a9342b62 7735 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7736 for (i = 0; i < len; ++i)
f4dc4d17 7737 {
df07e2c7 7738 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7739 gdb_assert (iter->per_cu.is_debug_types);
7740 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7741 iter->type_unit_group = tu_group;
f4dc4d17
DE
7742 }
7743
df07e2c7
AB
7744 delete tu_group->tus;
7745 tu_group->tus = nullptr;
348e048f
DE
7746
7747 return 1;
7748}
7749
7750/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7751 Build partial symbol tables for the .debug_types comp-units. */
7752
7753static void
ed2dc618 7754build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7755{
ed2dc618 7756 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7757 return;
7758
ed2dc618 7759 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7760}
f4dc4d17 7761
6aa5f3a6
DE
7762/* Traversal function for process_skeletonless_type_unit.
7763 Read a TU in a DWO file and build partial symbols for it. */
7764
7765static int
7766process_skeletonless_type_unit (void **slot, void *info)
7767{
7768 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7769 struct dwarf2_per_objfile *dwarf2_per_objfile
7770 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7771 struct signatured_type find_entry, *entry;
7772
7773 /* If this TU doesn't exist in the global table, add it and read it in. */
7774
7775 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7776 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7777
7778 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7779 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7780 &find_entry, INSERT);
6aa5f3a6
DE
7781 /* If we've already seen this type there's nothing to do. What's happening
7782 is we're doing our own version of comdat-folding here. */
7783 if (*slot != NULL)
7784 return 1;
7785
7786 /* This does the job that create_all_type_units would have done for
7787 this TU. */
ed2dc618
SM
7788 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7789 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7790 *slot = entry;
7791
7792 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7793 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7794 if (!reader.dummy_p)
7795 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7796 reader.comp_unit_die);
6aa5f3a6
DE
7797
7798 return 1;
7799}
7800
7801/* Traversal function for process_skeletonless_type_units. */
7802
7803static int
7804process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7805{
7806 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7807
7808 if (dwo_file->tus != NULL)
b0b6a987
TT
7809 htab_traverse_noresize (dwo_file->tus.get (),
7810 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7811
7812 return 1;
7813}
7814
7815/* Scan all TUs of DWO files, verifying we've processed them.
7816 This is needed in case a TU was emitted without its skeleton.
7817 Note: This can't be done until we know what all the DWO files are. */
7818
7819static void
ed2dc618 7820process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7821{
7822 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7823 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7824 && dwarf2_per_objfile->dwo_files != NULL)
7825 {
51ac9db5 7826 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7827 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7828 dwarf2_per_objfile);
6aa5f3a6 7829 }
348e048f
DE
7830}
7831
ed2dc618 7832/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7833
7834static void
ed2dc618 7835set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7836{
b76e467d 7837 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7838 {
891813be 7839 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7840
36586728
TT
7841 if (pst == NULL)
7842 continue;
7843
b76e467d 7844 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7845 {
7846 /* Set the 'user' field only if it is not already set. */
7847 if (pst->dependencies[j]->user == NULL)
7848 pst->dependencies[j]->user = pst;
7849 }
7850 }
7851}
7852
93311388
DE
7853/* Build the partial symbol table by doing a quick pass through the
7854 .debug_info and .debug_abbrev sections. */
72bf9492 7855
93311388 7856static void
ed2dc618 7857dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7858{
ed2dc618 7859 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7860
b4f54984 7861 if (dwarf_read_debug)
45cfd468
DE
7862 {
7863 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7864 objfile_name (objfile));
45cfd468
DE
7865 }
7866
76935768
TT
7867 scoped_restore restore_reading_psyms
7868 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7869 true);
98bfdba5 7870
96b79293 7871 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7872
93311388
DE
7873 /* Any cached compilation units will be linked by the per-objfile
7874 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7875 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7876
ed2dc618 7877 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7878
ed2dc618 7879 create_all_comp_units (dwarf2_per_objfile);
c906108c 7880
60606b2c
TT
7881 /* Create a temporary address map on a temporary obstack. We later
7882 copy this to the final obstack. */
8268c778 7883 auto_obstack temp_obstack;
791afaa2
TT
7884
7885 scoped_restore save_psymtabs_addrmap
d320c2b5 7886 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7887 addrmap_create_mutable (&temp_obstack));
72bf9492 7888
b76e467d 7889 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3d5afab3
TV
7890 {
7891 if (per_cu->v.psymtab != NULL)
7892 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7893 continue;
7894 process_psymtab_comp_unit (per_cu, false, language_minimal);
7895 }
ff013f42 7896
6aa5f3a6 7897 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7898 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7899
7900 /* Now that all TUs have been processed we can fill in the dependencies. */
7901 if (dwarf2_per_objfile->type_unit_groups != NULL)
7902 {
eaa5fa8b 7903 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7904 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7905 }
7906
b4f54984 7907 if (dwarf_read_debug)
ed2dc618 7908 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7909
ed2dc618 7910 set_partial_user (dwarf2_per_objfile);
95554aad 7911
d320c2b5
TT
7912 objfile->partial_symtabs->psymtabs_addrmap
7913 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7914 objfile->partial_symtabs->obstack ());
791afaa2
TT
7915 /* At this point we want to keep the address map. */
7916 save_psymtabs_addrmap.release ();
ff013f42 7917
b4f54984 7918 if (dwarf_read_debug)
45cfd468 7919 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7920 objfile_name (objfile));
ae038cb0
DJ
7921}
7922
dee91e82
DE
7923/* Load the partial DIEs for a secondary CU into memory.
7924 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7925
dee91e82
DE
7926static void
7927load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7928{
6751ebae 7929 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7930
7931 if (!reader.dummy_p)
7932 {
7933 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7934 language_minimal);
7935
7936 /* Check if comp unit has_children.
7937 If so, read the rest of the partial symbols from this comp unit.
7938 If not, there's no more debug_info for this comp unit. */
3e225074 7939 if (reader.comp_unit_die->has_children)
c0ab21c2 7940 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7941
7942 reader.keep ();
c0ab21c2 7943 }
ae038cb0
DJ
7944}
7945
ae038cb0 7946static void
ed2dc618 7947read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7948 struct dwarf2_section_info *section,
f1902523 7949 struct dwarf2_section_info *abbrev_section,
b76e467d 7950 unsigned int is_dwz)
ae038cb0 7951{
d521ce57 7952 const gdb_byte *info_ptr;
ed2dc618 7953 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7954
b4f54984 7955 if (dwarf_read_debug)
bf6af496 7956 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7957 section->get_name (),
7958 section->get_file_name ());
bf6af496 7959
96b79293 7960 section->read (objfile);
ae038cb0 7961
36586728 7962 info_ptr = section->buffer;
6e70227d 7963
36586728 7964 while (info_ptr < section->buffer + section->size)
ae038cb0 7965 {
ae038cb0 7966 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7967
9c541725 7968 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7969
f1902523 7970 comp_unit_head cu_header;
ed2dc618
SM
7971 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7972 abbrev_section, info_ptr,
7973 rcuh_kind::COMPILE);
ae038cb0
DJ
7974
7975 /* Save the compilation unit for later lookup. */
f1902523
JK
7976 if (cu_header.unit_type != DW_UT_type)
7977 {
7978 this_cu = XOBNEW (&objfile->objfile_obstack,
7979 struct dwarf2_per_cu_data);
7980 memset (this_cu, 0, sizeof (*this_cu));
7981 }
7982 else
7983 {
7984 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7985 struct signatured_type);
7986 memset (sig_type, 0, sizeof (*sig_type));
7987 sig_type->signature = cu_header.signature;
7988 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7989 this_cu = &sig_type->per_cu;
7990 }
7991 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7992 this_cu->sect_off = sect_off;
f1902523 7993 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7994 this_cu->is_dwz = is_dwz;
e3b94546 7995 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7996 this_cu->section = section;
ae038cb0 7997
b76e467d 7998 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7999
8000 info_ptr = info_ptr + this_cu->length;
8001 }
36586728
TT
8002}
8003
8004/* Create a list of all compilation units in OBJFILE.
8005 This is only done for -readnow and building partial symtabs. */
8006
8007static void
ed2dc618 8008create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8009{
b76e467d 8010 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8011 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8012 &dwarf2_per_objfile->abbrev, 0);
36586728 8013
b76e467d 8014 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8015 if (dwz != NULL)
ed2dc618 8016 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8017 1);
c906108c
SS
8018}
8019
5734ee8b 8020/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8021 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8022 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8023 DW_AT_ranges). See the comments of add_partial_subprogram on how
8024 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8025
72bf9492
DJ
8026static void
8027scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8028 CORE_ADDR *highpc, int set_addrmap,
8029 struct dwarf2_cu *cu)
c906108c 8030{
72bf9492 8031 struct partial_die_info *pdi;
c906108c 8032
91c24f0a
DC
8033 /* Now, march along the PDI's, descending into ones which have
8034 interesting children but skipping the children of the other ones,
8035 until we reach the end of the compilation unit. */
c906108c 8036
72bf9492 8037 pdi = first_die;
91c24f0a 8038
72bf9492
DJ
8039 while (pdi != NULL)
8040 {
52356b79 8041 pdi->fixup (cu);
c906108c 8042
f55ee35c 8043 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8044 children, so we need to look at them. Ditto for anonymous
8045 enums. */
933c6fe4 8046
72bf9492 8047 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8048 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8049 || pdi->tag == DW_TAG_imported_unit
8050 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8051 {
72bf9492 8052 switch (pdi->tag)
c906108c
SS
8053 {
8054 case DW_TAG_subprogram:
b1dc1806 8055 case DW_TAG_inlined_subroutine:
cdc07690 8056 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8057 break;
72929c62 8058 case DW_TAG_constant:
c906108c
SS
8059 case DW_TAG_variable:
8060 case DW_TAG_typedef:
91c24f0a 8061 case DW_TAG_union_type:
317d2668
TV
8062 if (!pdi->is_declaration
8063 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8064 {
72bf9492 8065 add_partial_symbol (pdi, cu);
63d06c5c
DC
8066 }
8067 break;
c906108c 8068 case DW_TAG_class_type:
680b30c7 8069 case DW_TAG_interface_type:
c906108c 8070 case DW_TAG_structure_type:
72bf9492 8071 if (!pdi->is_declaration)
c906108c 8072 {
72bf9492 8073 add_partial_symbol (pdi, cu);
c906108c 8074 }
b7fee5a3
KS
8075 if ((cu->language == language_rust
8076 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8077 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8078 set_addrmap, cu);
c906108c 8079 break;
91c24f0a 8080 case DW_TAG_enumeration_type:
72bf9492
DJ
8081 if (!pdi->is_declaration)
8082 add_partial_enumeration (pdi, cu);
c906108c
SS
8083 break;
8084 case DW_TAG_base_type:
a02abb62 8085 case DW_TAG_subrange_type:
c906108c 8086 /* File scope base type definitions are added to the partial
c5aa993b 8087 symbol table. */
72bf9492 8088 add_partial_symbol (pdi, cu);
c906108c 8089 break;
d9fa45fe 8090 case DW_TAG_namespace:
cdc07690 8091 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8092 break;
5d7cb8df 8093 case DW_TAG_module:
59c35742
AB
8094 if (!pdi->is_declaration)
8095 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8096 break;
95554aad
TT
8097 case DW_TAG_imported_unit:
8098 {
8099 struct dwarf2_per_cu_data *per_cu;
8100
f4dc4d17
DE
8101 /* For now we don't handle imported units in type units. */
8102 if (cu->per_cu->is_debug_types)
8103 {
8104 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8105 " supported in type units [in module %s]"),
518817b3 8106 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8107 }
8108
e3b94546
SM
8109 per_cu = dwarf2_find_containing_comp_unit
8110 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8111 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8112
8113 /* Go read the partial unit, if needed. */
8114 if (per_cu->v.psymtab == NULL)
135f5437 8115 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8116
ae640021 8117 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8118 }
8119 break;
74921315
KS
8120 case DW_TAG_imported_declaration:
8121 add_partial_symbol (pdi, cu);
8122 break;
c906108c
SS
8123 default:
8124 break;
8125 }
8126 }
8127
72bf9492
DJ
8128 /* If the die has a sibling, skip to the sibling. */
8129
8130 pdi = pdi->die_sibling;
8131 }
8132}
8133
8134/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8135
72bf9492 8136 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8137 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8138 Enumerators are an exception; they use the scope of their parent
8139 enumeration type, i.e. the name of the enumeration type is not
8140 prepended to the enumerator.
91c24f0a 8141
72bf9492
DJ
8142 There are two complexities. One is DW_AT_specification; in this
8143 case "parent" means the parent of the target of the specification,
8144 instead of the direct parent of the DIE. The other is compilers
8145 which do not emit DW_TAG_namespace; in this case we try to guess
8146 the fully qualified name of structure types from their members'
8147 linkage names. This must be done using the DIE's children rather
8148 than the children of any DW_AT_specification target. We only need
8149 to do this for structures at the top level, i.e. if the target of
8150 any DW_AT_specification (if any; otherwise the DIE itself) does not
8151 have a parent. */
8152
8153/* Compute the scope prefix associated with PDI's parent, in
8154 compilation unit CU. The result will be allocated on CU's
8155 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8156 field. NULL is returned if no prefix is necessary. */
15d034d0 8157static const char *
72bf9492
DJ
8158partial_die_parent_scope (struct partial_die_info *pdi,
8159 struct dwarf2_cu *cu)
8160{
15d034d0 8161 const char *grandparent_scope;
72bf9492 8162 struct partial_die_info *parent, *real_pdi;
91c24f0a 8163
72bf9492
DJ
8164 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8165 then this means the parent of the specification DIE. */
8166
8167 real_pdi = pdi;
72bf9492 8168 while (real_pdi->has_specification)
fb816e8b 8169 {
122cf0f2
AB
8170 auto res = find_partial_die (real_pdi->spec_offset,
8171 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8172 real_pdi = res.pdi;
8173 cu = res.cu;
8174 }
72bf9492
DJ
8175
8176 parent = real_pdi->die_parent;
8177 if (parent == NULL)
8178 return NULL;
8179
8180 if (parent->scope_set)
8181 return parent->scope;
8182
52356b79 8183 parent->fixup (cu);
72bf9492 8184
10b3939b 8185 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8186
acebe513
UW
8187 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8188 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8189 Work around this problem here. */
8190 if (cu->language == language_cplus
6e70227d 8191 && parent->tag == DW_TAG_namespace
acebe513
UW
8192 && strcmp (parent->name, "::") == 0
8193 && grandparent_scope == NULL)
8194 {
8195 parent->scope = NULL;
8196 parent->scope_set = 1;
8197 return NULL;
8198 }
8199
0a4b0913 8200 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8201 if (pdi->tag == DW_TAG_enumerator)
8202 /* Enumerators should not get the name of the enumeration as a prefix. */
8203 parent->scope = grandparent_scope;
8204 else if (parent->tag == DW_TAG_namespace
f55ee35c 8205 || parent->tag == DW_TAG_module
72bf9492
DJ
8206 || parent->tag == DW_TAG_structure_type
8207 || parent->tag == DW_TAG_class_type
680b30c7 8208 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8209 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8210 || parent->tag == DW_TAG_enumeration_type
8211 || (cu->language == language_fortran
8212 && parent->tag == DW_TAG_subprogram
8213 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8214 {
8215 if (grandparent_scope == NULL)
8216 parent->scope = parent->name;
8217 else
3e43a32a
MS
8218 parent->scope = typename_concat (&cu->comp_unit_obstack,
8219 grandparent_scope,
f55ee35c 8220 parent->name, 0, cu);
72bf9492 8221 }
72bf9492
DJ
8222 else
8223 {
8224 /* FIXME drow/2004-04-01: What should we be doing with
8225 function-local names? For partial symbols, we should probably be
8226 ignoring them. */
fa9c3fa0
TT
8227 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8228 dwarf_tag_name (parent->tag),
8229 sect_offset_str (pdi->sect_off));
72bf9492 8230 parent->scope = grandparent_scope;
c906108c
SS
8231 }
8232
72bf9492
DJ
8233 parent->scope_set = 1;
8234 return parent->scope;
8235}
8236
8237/* Return the fully scoped name associated with PDI, from compilation unit
8238 CU. The result will be allocated with malloc. */
4568ecf9 8239
43816ebc 8240static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8241partial_die_full_name (struct partial_die_info *pdi,
8242 struct dwarf2_cu *cu)
8243{
15d034d0 8244 const char *parent_scope;
72bf9492 8245
98bfdba5
PA
8246 /* If this is a template instantiation, we can not work out the
8247 template arguments from partial DIEs. So, unfortunately, we have
8248 to go through the full DIEs. At least any work we do building
8249 types here will be reused if full symbols are loaded later. */
8250 if (pdi->has_template_arguments)
8251 {
52356b79 8252 pdi->fixup (cu);
98bfdba5
PA
8253
8254 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8255 {
8256 struct die_info *die;
8257 struct attribute attr;
8258 struct dwarf2_cu *ref_cu = cu;
8259
b64f50a1 8260 /* DW_FORM_ref_addr is using section offset. */
b4069958 8261 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8262 attr.form = DW_FORM_ref_addr;
9c541725 8263 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8264 die = follow_die_ref (NULL, &attr, &ref_cu);
8265
43816ebc 8266 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8267 }
8268 }
8269
72bf9492
DJ
8270 parent_scope = partial_die_parent_scope (pdi, cu);
8271 if (parent_scope == NULL)
8272 return NULL;
8273 else
43816ebc
TT
8274 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8275 pdi->name, 0, cu));
c906108c
SS
8276}
8277
8278static void
72bf9492 8279add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8280{
518817b3
SM
8281 struct dwarf2_per_objfile *dwarf2_per_objfile
8282 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8283 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8284 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8285 CORE_ADDR addr = 0;
15d034d0 8286 const char *actual_name = NULL;
e142c38c
DJ
8287 CORE_ADDR baseaddr;
8288
b3b3bada 8289 baseaddr = objfile->text_section_offset ();
c906108c 8290
43816ebc
TT
8291 gdb::unique_xmalloc_ptr<char> built_actual_name
8292 = partial_die_full_name (pdi, cu);
15d034d0 8293 if (built_actual_name != NULL)
43816ebc 8294 actual_name = built_actual_name.get ();
63d06c5c 8295
72bf9492
DJ
8296 if (actual_name == NULL)
8297 actual_name = pdi->name;
8298
76e288d1
TT
8299 partial_symbol psymbol;
8300 memset (&psymbol, 0, sizeof (psymbol));
8301 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8302 psymbol.ginfo.section = -1;
8303
8304 /* The code below indicates that the psymbol should be installed by
8305 setting this. */
8306 gdb::optional<psymbol_placement> where;
8307
c906108c
SS
8308 switch (pdi->tag)
8309 {
b1dc1806 8310 case DW_TAG_inlined_subroutine:
c906108c 8311 case DW_TAG_subprogram:
79748972
TT
8312 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8313 - baseaddr);
0a4b0913
AB
8314 if (pdi->is_external
8315 || cu->language == language_ada
8316 || (cu->language == language_fortran
8317 && pdi->die_parent != NULL
8318 && pdi->die_parent->tag == DW_TAG_subprogram))
8319 {
8320 /* Normally, only "external" DIEs are part of the global scope.
8321 But in Ada and Fortran, we want to be able to access nested
8322 procedures globally. So all Ada and Fortran subprograms are
8323 stored in the global scope. */
76e288d1 8324 where = psymbol_placement::GLOBAL;
c906108c
SS
8325 }
8326 else
76e288d1
TT
8327 where = psymbol_placement::STATIC;
8328
8329 psymbol.domain = VAR_DOMAIN;
8330 psymbol.aclass = LOC_BLOCK;
8331 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8332 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8333
8334 if (pdi->main_subprogram && actual_name != NULL)
8335 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8336 break;
72929c62 8337 case DW_TAG_constant:
76e288d1
TT
8338 psymbol.domain = VAR_DOMAIN;
8339 psymbol.aclass = LOC_STATIC;
8340 where = (pdi->is_external
8341 ? psymbol_placement::GLOBAL
8342 : psymbol_placement::STATIC);
72929c62 8343 break;
c906108c 8344 case DW_TAG_variable:
95554aad
TT
8345 if (pdi->d.locdesc)
8346 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8347
95554aad 8348 if (pdi->d.locdesc
caac4577
JG
8349 && addr == 0
8350 && !dwarf2_per_objfile->has_section_at_zero)
8351 {
8352 /* A global or static variable may also have been stripped
8353 out by the linker if unused, in which case its address
8354 will be nullified; do not add such variables into partial
8355 symbol table then. */
8356 }
8357 else if (pdi->is_external)
c906108c
SS
8358 {
8359 /* Global Variable.
8360 Don't enter into the minimal symbol tables as there is
8361 a minimal symbol table entry from the ELF symbols already.
8362 Enter into partial symbol table if it has a location
8363 descriptor or a type.
8364 If the location descriptor is missing, new_symbol will create
8365 a LOC_UNRESOLVED symbol, the address of the variable will then
8366 be determined from the minimal symbol table whenever the variable
8367 is referenced.
8368 The address for the partial symbol table entry is not
8369 used by GDB, but it comes in handy for debugging partial symbol
8370 table building. */
8371
95554aad 8372 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8373 {
8374 psymbol.domain = VAR_DOMAIN;
8375 psymbol.aclass = LOC_STATIC;
8376 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8377 psymbol.ginfo.value.address = addr;
8378 where = psymbol_placement::GLOBAL;
8379 }
c906108c
SS
8380 }
8381 else
8382 {
ff908ebf
AW
8383 int has_loc = pdi->d.locdesc != NULL;
8384
8385 /* Static Variable. Skip symbols whose value we cannot know (those
8386 without location descriptors or constant values). */
8387 if (!has_loc && !pdi->has_const_value)
43816ebc 8388 return;
ff908ebf 8389
76e288d1
TT
8390 psymbol.domain = VAR_DOMAIN;
8391 psymbol.aclass = LOC_STATIC;
8392 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8393 if (has_loc)
8394 psymbol.ginfo.value.address = addr;
8395 where = psymbol_placement::STATIC;
c906108c
SS
8396 }
8397 break;
8398 case DW_TAG_typedef:
8399 case DW_TAG_base_type:
a02abb62 8400 case DW_TAG_subrange_type:
76e288d1
TT
8401 psymbol.domain = VAR_DOMAIN;
8402 psymbol.aclass = LOC_TYPEDEF;
8403 where = psymbol_placement::STATIC;
c906108c 8404 break;
74921315 8405 case DW_TAG_imported_declaration:
72bf9492 8406 case DW_TAG_namespace:
76e288d1
TT
8407 psymbol.domain = VAR_DOMAIN;
8408 psymbol.aclass = LOC_TYPEDEF;
8409 where = psymbol_placement::GLOBAL;
72bf9492 8410 break;
530e8392 8411 case DW_TAG_module:
a5fd13a9
BH
8412 /* With Fortran 77 there might be a "BLOCK DATA" module
8413 available without any name. If so, we skip the module as it
8414 doesn't bring any value. */
8415 if (actual_name != nullptr)
76e288d1
TT
8416 {
8417 psymbol.domain = MODULE_DOMAIN;
8418 psymbol.aclass = LOC_TYPEDEF;
8419 where = psymbol_placement::GLOBAL;
8420 }
530e8392 8421 break;
c906108c 8422 case DW_TAG_class_type:
680b30c7 8423 case DW_TAG_interface_type:
c906108c
SS
8424 case DW_TAG_structure_type:
8425 case DW_TAG_union_type:
8426 case DW_TAG_enumeration_type:
fa4028e9
JB
8427 /* Skip external references. The DWARF standard says in the section
8428 about "Structure, Union, and Class Type Entries": "An incomplete
8429 structure, union or class type is represented by a structure,
8430 union or class entry that does not have a byte size attribute
8431 and that has a DW_AT_declaration attribute." */
8432 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8433 return;
fa4028e9 8434
63d06c5c
DC
8435 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8436 static vs. global. */
76e288d1
TT
8437 psymbol.domain = STRUCT_DOMAIN;
8438 psymbol.aclass = LOC_TYPEDEF;
8439 where = (cu->language == language_cplus
8440 ? psymbol_placement::GLOBAL
8441 : psymbol_placement::STATIC);
c906108c
SS
8442 break;
8443 case DW_TAG_enumerator:
76e288d1
TT
8444 psymbol.domain = VAR_DOMAIN;
8445 psymbol.aclass = LOC_CONST;
8446 where = (cu->language == language_cplus
8447 ? psymbol_placement::GLOBAL
8448 : psymbol_placement::STATIC);
c906108c
SS
8449 break;
8450 default:
8451 break;
8452 }
76e288d1
TT
8453
8454 if (where.has_value ())
8455 {
f049a313
TT
8456 if (built_actual_name != nullptr)
8457 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8458 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8459 psymbol.ginfo.set_linkage_name (actual_name);
8460 else
8461 {
8462 psymbol.ginfo.set_demangled_name (actual_name,
8463 &objfile->objfile_obstack);
8464 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8465 }
76e288d1
TT
8466 add_psymbol_to_list (psymbol, *where, objfile);
8467 }
c906108c
SS
8468}
8469
5c4e30ca
DC
8470/* Read a partial die corresponding to a namespace; also, add a symbol
8471 corresponding to that namespace to the symbol table. NAMESPACE is
8472 the name of the enclosing namespace. */
91c24f0a 8473
72bf9492
DJ
8474static void
8475add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8476 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8477 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8478{
72bf9492 8479 /* Add a symbol for the namespace. */
e7c27a73 8480
72bf9492 8481 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8482
8483 /* Now scan partial symbols in that namespace. */
8484
91c24f0a 8485 if (pdi->has_children)
cdc07690 8486 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8487}
8488
5d7cb8df
JK
8489/* Read a partial die corresponding to a Fortran module. */
8490
8491static void
8492add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8493 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8494{
530e8392
KB
8495 /* Add a symbol for the namespace. */
8496
8497 add_partial_symbol (pdi, cu);
8498
f55ee35c 8499 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8500
8501 if (pdi->has_children)
cdc07690 8502 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8503}
8504
b1dc1806
XR
8505/* Read a partial die corresponding to a subprogram or an inlined
8506 subprogram and create a partial symbol for that subprogram.
8507 When the CU language allows it, this routine also defines a partial
8508 symbol for each nested subprogram that this subprogram contains.
8509 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8510 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8511
cdc07690
YQ
8512 PDI may also be a lexical block, in which case we simply search
8513 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8514 Again, this is only performed when the CU language allows this
8515 type of definitions. */
8516
8517static void
8518add_partial_subprogram (struct partial_die_info *pdi,
8519 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8520 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8521{
b1dc1806 8522 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8523 {
8524 if (pdi->has_pc_info)
8525 {
8526 if (pdi->lowpc < *lowpc)
8527 *lowpc = pdi->lowpc;
8528 if (pdi->highpc > *highpc)
8529 *highpc = pdi->highpc;
cdc07690 8530 if (set_addrmap)
5734ee8b 8531 {
518817b3 8532 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8533 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8534 CORE_ADDR baseaddr;
b926417a
TT
8535 CORE_ADDR this_highpc;
8536 CORE_ADDR this_lowpc;
5734ee8b 8537
b3b3bada 8538 baseaddr = objfile->text_section_offset ();
b926417a
TT
8539 this_lowpc
8540 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8541 pdi->lowpc + baseaddr)
8542 - baseaddr);
8543 this_highpc
8544 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8545 pdi->highpc + baseaddr)
8546 - baseaddr);
d320c2b5 8547 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8548 this_lowpc, this_highpc - 1,
9291a0cd 8549 cu->per_cu->v.psymtab);
5734ee8b 8550 }
481860b3
GB
8551 }
8552
8553 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8554 {
bc30ff58 8555 if (!pdi->is_declaration)
e8d05480
JB
8556 /* Ignore subprogram DIEs that do not have a name, they are
8557 illegal. Do not emit a complaint at this point, we will
8558 do so when we convert this psymtab into a symtab. */
8559 if (pdi->name)
8560 add_partial_symbol (pdi, cu);
bc30ff58
JB
8561 }
8562 }
6e70227d 8563
bc30ff58
JB
8564 if (! pdi->has_children)
8565 return;
8566
0a4b0913 8567 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8568 {
8569 pdi = pdi->die_child;
8570 while (pdi != NULL)
8571 {
52356b79 8572 pdi->fixup (cu);
bc30ff58 8573 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8574 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8575 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8576 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8577 pdi = pdi->die_sibling;
8578 }
8579 }
8580}
8581
91c24f0a
DC
8582/* Read a partial die corresponding to an enumeration type. */
8583
72bf9492
DJ
8584static void
8585add_partial_enumeration (struct partial_die_info *enum_pdi,
8586 struct dwarf2_cu *cu)
91c24f0a 8587{
72bf9492 8588 struct partial_die_info *pdi;
91c24f0a
DC
8589
8590 if (enum_pdi->name != NULL)
72bf9492
DJ
8591 add_partial_symbol (enum_pdi, cu);
8592
8593 pdi = enum_pdi->die_child;
8594 while (pdi)
91c24f0a 8595 {
72bf9492 8596 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8597 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8598 else
72bf9492
DJ
8599 add_partial_symbol (pdi, cu);
8600 pdi = pdi->die_sibling;
91c24f0a 8601 }
91c24f0a
DC
8602}
8603
6caca83c
CC
8604/* Return the initial uleb128 in the die at INFO_PTR. */
8605
8606static unsigned int
d521ce57 8607peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8608{
8609 unsigned int bytes_read;
8610
8611 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8612}
8613
685af9cd
TT
8614/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8615 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8616
4bb7a0a7
DJ
8617 Return the corresponding abbrev, or NULL if the number is zero (indicating
8618 an empty DIE). In either case *BYTES_READ will be set to the length of
8619 the initial number. */
8620
8621static struct abbrev_info *
685af9cd
TT
8622peek_die_abbrev (const die_reader_specs &reader,
8623 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8624{
685af9cd 8625 dwarf2_cu *cu = reader.cu;
518817b3 8626 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8627 unsigned int abbrev_number
8628 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8629
8630 if (abbrev_number == 0)
8631 return NULL;
8632
685af9cd 8633 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8634 if (!abbrev)
8635 {
422b9917 8636 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8637 " at offset %s [in module %s]"),
422b9917 8638 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8639 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8640 }
8641
8642 return abbrev;
8643}
8644
93311388
DE
8645/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8646 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8647 DIE. Any children of the skipped DIEs will also be skipped. */
8648
d521ce57
TT
8649static const gdb_byte *
8650skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8651{
4bb7a0a7
DJ
8652 while (1)
8653 {
685af9cd
TT
8654 unsigned int bytes_read;
8655 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8656
4bb7a0a7
DJ
8657 if (abbrev == NULL)
8658 return info_ptr + bytes_read;
8659 else
dee91e82 8660 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8661 }
8662}
8663
93311388
DE
8664/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8665 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8666 abbrev corresponding to that skipped uleb128 should be passed in
8667 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8668 children. */
8669
d521ce57
TT
8670static const gdb_byte *
8671skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8672 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8673{
8674 unsigned int bytes_read;
8675 struct attribute attr;
dee91e82
DE
8676 bfd *abfd = reader->abfd;
8677 struct dwarf2_cu *cu = reader->cu;
d521ce57 8678 const gdb_byte *buffer = reader->buffer;
f664829e 8679 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8680 unsigned int form, i;
8681
8682 for (i = 0; i < abbrev->num_attrs; i++)
8683 {
8684 /* The only abbrev we care about is DW_AT_sibling. */
8685 if (abbrev->attrs[i].name == DW_AT_sibling)
8686 {
18a8505e
AT
8687 bool ignored;
8688 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8689 &ignored);
4bb7a0a7 8690 if (attr.form == DW_FORM_ref_addr)
b98664d3 8691 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8692 else
b9502d3f 8693 {
0826b30a 8694 sect_offset off = attr.get_ref_die_offset ();
9c541725 8695 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8696
8697 if (sibling_ptr < info_ptr)
b98664d3 8698 complaint (_("DW_AT_sibling points backwards"));
22869d73 8699 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8700 reader->die_section->overflow_complaint ();
b9502d3f
WN
8701 else
8702 return sibling_ptr;
8703 }
4bb7a0a7
DJ
8704 }
8705
8706 /* If it isn't DW_AT_sibling, skip this attribute. */
8707 form = abbrev->attrs[i].form;
8708 skip_attribute:
8709 switch (form)
8710 {
4bb7a0a7 8711 case DW_FORM_ref_addr:
ae411497
TT
8712 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8713 and later it is offset sized. */
8714 if (cu->header.version == 2)
8715 info_ptr += cu->header.addr_size;
8716 else
8717 info_ptr += cu->header.offset_size;
8718 break;
36586728
TT
8719 case DW_FORM_GNU_ref_alt:
8720 info_ptr += cu->header.offset_size;
8721 break;
ae411497 8722 case DW_FORM_addr:
4bb7a0a7
DJ
8723 info_ptr += cu->header.addr_size;
8724 break;
8725 case DW_FORM_data1:
8726 case DW_FORM_ref1:
8727 case DW_FORM_flag:
8fe0f950 8728 case DW_FORM_strx1:
4bb7a0a7
DJ
8729 info_ptr += 1;
8730 break;
2dc7f7b3 8731 case DW_FORM_flag_present:
43988095 8732 case DW_FORM_implicit_const:
2dc7f7b3 8733 break;
4bb7a0a7
DJ
8734 case DW_FORM_data2:
8735 case DW_FORM_ref2:
8fe0f950 8736 case DW_FORM_strx2:
4bb7a0a7
DJ
8737 info_ptr += 2;
8738 break;
8fe0f950
AT
8739 case DW_FORM_strx3:
8740 info_ptr += 3;
8741 break;
4bb7a0a7
DJ
8742 case DW_FORM_data4:
8743 case DW_FORM_ref4:
8fe0f950 8744 case DW_FORM_strx4:
4bb7a0a7
DJ
8745 info_ptr += 4;
8746 break;
8747 case DW_FORM_data8:
8748 case DW_FORM_ref8:
55f1336d 8749 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8750 info_ptr += 8;
8751 break;
0224619f
JK
8752 case DW_FORM_data16:
8753 info_ptr += 16;
8754 break;
4bb7a0a7 8755 case DW_FORM_string:
9b1c24c8 8756 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8757 info_ptr += bytes_read;
8758 break;
2dc7f7b3 8759 case DW_FORM_sec_offset:
4bb7a0a7 8760 case DW_FORM_strp:
36586728 8761 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8762 info_ptr += cu->header.offset_size;
8763 break;
2dc7f7b3 8764 case DW_FORM_exprloc:
4bb7a0a7
DJ
8765 case DW_FORM_block:
8766 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8767 info_ptr += bytes_read;
8768 break;
8769 case DW_FORM_block1:
8770 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8771 break;
8772 case DW_FORM_block2:
8773 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8774 break;
8775 case DW_FORM_block4:
8776 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8777 break;
336d760d 8778 case DW_FORM_addrx:
cf532bd1 8779 case DW_FORM_strx:
4bb7a0a7
DJ
8780 case DW_FORM_sdata:
8781 case DW_FORM_udata:
8782 case DW_FORM_ref_udata:
3019eac3
DE
8783 case DW_FORM_GNU_addr_index:
8784 case DW_FORM_GNU_str_index:
18a8505e 8785 case DW_FORM_rnglistx:
41144253 8786 case DW_FORM_loclistx:
d521ce57 8787 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8788 break;
8789 case DW_FORM_indirect:
8790 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8791 info_ptr += bytes_read;
8792 /* We need to continue parsing from here, so just go back to
8793 the top. */
8794 goto skip_attribute;
8795
8796 default:
3e43a32a
MS
8797 error (_("Dwarf Error: Cannot handle %s "
8798 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8799 dwarf_form_name (form),
8800 bfd_get_filename (abfd));
8801 }
8802 }
8803
8804 if (abbrev->has_children)
dee91e82 8805 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8806 else
8807 return info_ptr;
8808}
8809
93311388 8810/* Locate ORIG_PDI's sibling.
dee91e82 8811 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8812
d521ce57 8813static const gdb_byte *
dee91e82
DE
8814locate_pdi_sibling (const struct die_reader_specs *reader,
8815 struct partial_die_info *orig_pdi,
d521ce57 8816 const gdb_byte *info_ptr)
91c24f0a
DC
8817{
8818 /* Do we know the sibling already? */
72bf9492 8819
91c24f0a
DC
8820 if (orig_pdi->sibling)
8821 return orig_pdi->sibling;
8822
8823 /* Are there any children to deal with? */
8824
8825 if (!orig_pdi->has_children)
8826 return info_ptr;
8827
4bb7a0a7 8828 /* Skip the children the long way. */
91c24f0a 8829
dee91e82 8830 return skip_children (reader, info_ptr);
91c24f0a
DC
8831}
8832
257e7a09 8833/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8834 not NULL. */
c906108c 8835
891813be
TT
8836void
8837dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8838{
ed2dc618
SM
8839 struct dwarf2_per_objfile *dwarf2_per_objfile
8840 = get_dwarf2_per_objfile (objfile);
8841
077cbab2
TT
8842 gdb_assert (!readin);
8843 /* If this psymtab is constructed from a debug-only objfile, the
8844 has_section_at_zero flag will not necessarily be correct. We
8845 can get the correct value for this flag by looking at the data
8846 associated with the (presumably stripped) associated objfile. */
8847 if (objfile->separate_debug_objfile_backlink)
c906108c 8848 {
077cbab2
TT
8849 struct dwarf2_per_objfile *dpo_backlink
8850 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8851
077cbab2
TT
8852 dwarf2_per_objfile->has_section_at_zero
8853 = dpo_backlink->has_section_at_zero;
8854 }
98bfdba5 8855
8566b89b 8856 expand_psymtab (objfile);
95554aad 8857
ed2dc618 8858 process_cu_includes (dwarf2_per_objfile);
c906108c 8859}
9cdd5dbd
DE
8860\f
8861/* Reading in full CUs. */
c906108c 8862
10b3939b
DJ
8863/* Add PER_CU to the queue. */
8864
8865static void
95554aad
TT
8866queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8867 enum language pretend_language)
10b3939b 8868{
10b3939b 8869 per_cu->queued = 1;
39856def 8870 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8871}
8872
89e63ee4
DE
8873/* If PER_CU is not yet queued, add it to the queue.
8874 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8875 dependency.
0907af0c 8876 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8877 meaning either PER_CU is already queued or it is already loaded.
8878
8879 N.B. There is an invariant here that if a CU is queued then it is loaded.
8880 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8881
8882static int
89e63ee4 8883maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8884 struct dwarf2_per_cu_data *per_cu,
8885 enum language pretend_language)
8886{
8887 /* We may arrive here during partial symbol reading, if we need full
8888 DIEs to process an unusual case (e.g. template arguments). Do
8889 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8890 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8891 {
8892 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8893 return 1;
8894 return 0;
8895 }
8896
8897 /* Mark the dependence relation so that we don't flush PER_CU
8898 too early. */
89e63ee4
DE
8899 if (dependent_cu != NULL)
8900 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8901
8902 /* If it's already on the queue, we have nothing to do. */
8903 if (per_cu->queued)
8904 return 0;
8905
8906 /* If the compilation unit is already loaded, just mark it as
8907 used. */
8908 if (per_cu->cu != NULL)
8909 {
8910 per_cu->cu->last_used = 0;
8911 return 0;
8912 }
8913
8914 /* Add it to the queue. */
8915 queue_comp_unit (per_cu, pretend_language);
8916
8917 return 1;
8918}
8919
10b3939b
DJ
8920/* Process the queue. */
8921
8922static void
ed2dc618 8923process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8924{
b4f54984 8925 if (dwarf_read_debug)
45cfd468
DE
8926 {
8927 fprintf_unfiltered (gdb_stdlog,
8928 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8929 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8930 }
8931
03dd20cc
DJ
8932 /* The queue starts out with one item, but following a DIE reference
8933 may load a new CU, adding it to the end of the queue. */
39856def 8934 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8935 {
39856def
TT
8936 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8937
cc12ce38 8938 if ((dwarf2_per_objfile->using_index
39856def
TT
8939 ? !item.per_cu->v.quick->compunit_symtab
8940 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8941 /* Skip dummy CUs. */
39856def 8942 && item.per_cu->cu != NULL)
f4dc4d17 8943 {
39856def 8944 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8945 unsigned int debug_print_threshold;
247f5c4f 8946 char buf[100];
f4dc4d17 8947
247f5c4f 8948 if (per_cu->is_debug_types)
f4dc4d17 8949 {
247f5c4f
DE
8950 struct signatured_type *sig_type =
8951 (struct signatured_type *) per_cu;
8952
9d8780f0 8953 sprintf (buf, "TU %s at offset %s",
73be47f5 8954 hex_string (sig_type->signature),
9d8780f0 8955 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8956 /* There can be 100s of TUs.
8957 Only print them in verbose mode. */
8958 debug_print_threshold = 2;
f4dc4d17 8959 }
247f5c4f 8960 else
73be47f5 8961 {
9d8780f0
SM
8962 sprintf (buf, "CU at offset %s",
8963 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8964 debug_print_threshold = 1;
8965 }
247f5c4f 8966
b4f54984 8967 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8968 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8969
8970 if (per_cu->is_debug_types)
39856def 8971 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8972 else
39856def 8973 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8974
b4f54984 8975 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8976 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8977 }
10b3939b 8978
39856def
TT
8979 item.per_cu->queued = 0;
8980 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8981 }
8982
b4f54984 8983 if (dwarf_read_debug)
45cfd468
DE
8984 {
8985 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8986 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8987 }
10b3939b
DJ
8988}
8989
10b3939b
DJ
8990/* Read in full symbols for PST, and anything it depends on. */
8991
8566b89b
TT
8992void
8993dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8994{
194d088f 8995 gdb_assert (!readin);
95554aad 8996
48993951 8997 expand_dependencies (objfile);
aaa75496 8998
b83470bf
TT
8999 dw2_do_instantiate_symtab (per_cu_data, false);
9000 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
9001}
9002
dee91e82
DE
9003/* Trivial hash function for die_info: the hash value of a DIE
9004 is its offset in .debug_info for this objfile. */
10b3939b 9005
dee91e82
DE
9006static hashval_t
9007die_hash (const void *item)
10b3939b 9008{
9a3c8263 9009 const struct die_info *die = (const struct die_info *) item;
6502dd73 9010
9c541725 9011 return to_underlying (die->sect_off);
dee91e82 9012}
63d06c5c 9013
dee91e82
DE
9014/* Trivial comparison function for die_info structures: two DIEs
9015 are equal if they have the same offset. */
98bfdba5 9016
dee91e82
DE
9017static int
9018die_eq (const void *item_lhs, const void *item_rhs)
9019{
9a3c8263
SM
9020 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9021 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9022
9c541725 9023 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9024}
c906108c 9025
c0ab21c2 9026/* Load the DIEs associated with PER_CU into memory. */
c906108c 9027
dee91e82 9028static void
c0ab21c2
TT
9029load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9030 bool skip_partial,
9031 enum language pretend_language)
dee91e82 9032{
c0ab21c2
TT
9033 gdb_assert (! this_cu->is_debug_types);
9034
6751ebae 9035 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
9036 if (reader.dummy_p)
9037 return;
9038
9039 struct dwarf2_cu *cu = reader.cu;
9040 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9041
dee91e82
DE
9042 gdb_assert (cu->die_hash == NULL);
9043 cu->die_hash =
9044 htab_create_alloc_ex (cu->header.length / 12,
9045 die_hash,
9046 die_eq,
9047 NULL,
9048 &cu->comp_unit_obstack,
9049 hashtab_obstack_allocate,
9050 dummy_obstack_deallocate);
e142c38c 9051
3e225074 9052 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9053 reader.comp_unit_die->child
9054 = read_die_and_siblings (&reader, reader.info_ptr,
9055 &info_ptr, reader.comp_unit_die);
9056 cu->dies = reader.comp_unit_die;
dee91e82 9057 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9058
9059 /* We try not to read any attributes in this function, because not
9cdd5dbd 9060 all CUs needed for references have been loaded yet, and symbol
10b3939b 9061 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9062 or we won't be able to build types correctly.
9063 Similarly, if we do not read the producer, we can not apply
9064 producer-specific interpretation. */
c0ab21c2 9065 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9066
9067 reader.keep ();
10b3939b
DJ
9068}
9069
3da10d80
KS
9070/* Add a DIE to the delayed physname list. */
9071
9072static void
9073add_to_method_list (struct type *type, int fnfield_index, int index,
9074 const char *name, struct die_info *die,
9075 struct dwarf2_cu *cu)
9076{
9077 struct delayed_method_info mi;
9078 mi.type = type;
9079 mi.fnfield_index = fnfield_index;
9080 mi.index = index;
9081 mi.name = name;
9082 mi.die = die;
c89b44cd 9083 cu->method_list.push_back (mi);
3da10d80
KS
9084}
9085
3693fdb3
PA
9086/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9087 "const" / "volatile". If so, decrements LEN by the length of the
9088 modifier and return true. Otherwise return false. */
9089
9090template<size_t N>
9091static bool
9092check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9093{
9094 size_t mod_len = sizeof (mod) - 1;
9095 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9096 {
9097 len -= mod_len;
9098 return true;
9099 }
9100 return false;
9101}
9102
3da10d80
KS
9103/* Compute the physnames of any methods on the CU's method list.
9104
9105 The computation of method physnames is delayed in order to avoid the
9106 (bad) condition that one of the method's formal parameters is of an as yet
9107 incomplete type. */
9108
9109static void
9110compute_delayed_physnames (struct dwarf2_cu *cu)
9111{
3693fdb3 9112 /* Only C++ delays computing physnames. */
c89b44cd 9113 if (cu->method_list.empty ())
3693fdb3
PA
9114 return;
9115 gdb_assert (cu->language == language_cplus);
9116
52941706 9117 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9118 {
1d06ead6 9119 const char *physname;
3da10d80 9120 struct fn_fieldlist *fn_flp
c89b44cd
TT
9121 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9122 physname = dwarf2_physname (mi.name, mi.die, cu);
9123 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9124 = physname ? physname : "";
3693fdb3
PA
9125
9126 /* Since there's no tag to indicate whether a method is a
9127 const/volatile overload, extract that information out of the
9128 demangled name. */
9129 if (physname != NULL)
9130 {
9131 size_t len = strlen (physname);
9132
9133 while (1)
9134 {
9135 if (physname[len] == ')') /* shortcut */
9136 break;
9137 else if (check_modifier (physname, len, " const"))
c89b44cd 9138 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9139 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9140 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9141 else
9142 break;
9143 }
9144 }
3da10d80 9145 }
c89b44cd
TT
9146
9147 /* The list is no longer needed. */
9148 cu->method_list.clear ();
3da10d80
KS
9149}
9150
a766d390
DE
9151/* Go objects should be embedded in a DW_TAG_module DIE,
9152 and it's not clear if/how imported objects will appear.
9153 To keep Go support simple until that's worked out,
9154 go back through what we've read and create something usable.
9155 We could do this while processing each DIE, and feels kinda cleaner,
9156 but that way is more invasive.
9157 This is to, for example, allow the user to type "p var" or "b main"
9158 without having to specify the package name, and allow lookups
9159 of module.object to work in contexts that use the expression
9160 parser. */
9161
9162static void
9163fixup_go_packaging (struct dwarf2_cu *cu)
9164{
421d1616 9165 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9166 struct pending *list;
9167 int i;
9168
c24bdb02 9169 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9170 list != NULL;
9171 list = list->next)
a766d390
DE
9172 {
9173 for (i = 0; i < list->nsyms; ++i)
9174 {
9175 struct symbol *sym = list->symbol[i];
9176
c1b5c1eb 9177 if (sym->language () == language_go
a766d390
DE
9178 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9179 {
421d1616
TT
9180 gdb::unique_xmalloc_ptr<char> this_package_name
9181 (go_symbol_package_name (sym));
a766d390
DE
9182
9183 if (this_package_name == NULL)
9184 continue;
9185 if (package_name == NULL)
421d1616 9186 package_name = std::move (this_package_name);
a766d390
DE
9187 else
9188 {
518817b3
SM
9189 struct objfile *objfile
9190 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9191 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9192 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9193 (symbol_symtab (sym) != NULL
9194 ? symtab_to_filename_for_display
9195 (symbol_symtab (sym))
e3b94546 9196 : objfile_name (objfile)),
421d1616 9197 this_package_name.get (), package_name.get ());
a766d390
DE
9198 }
9199 }
9200 }
9201 }
9202
9203 if (package_name != NULL)
9204 {
518817b3 9205 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9206 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9207 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9208 saved_package_name);
a766d390
DE
9209 struct symbol *sym;
9210
8c14c3a3 9211 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9212 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9213 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9214 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9215 e.g., "main" finds the "main" module and not C's main(). */
9216 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9218 SYMBOL_TYPE (sym) = type;
9219
c24bdb02 9220 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9221 }
9222}
9223
c9317f21
TT
9224/* Allocate a fully-qualified name consisting of the two parts on the
9225 obstack. */
9226
9227static const char *
9228rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9229{
9230 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9231}
9232
9c6a1327
TT
9233/* A helper that allocates a variant part to attach to a Rust enum
9234 type. OBSTACK is where the results should be allocated. TYPE is
9235 the type we're processing. DISCRIMINANT_INDEX is the index of the
9236 discriminant. It must be the index of one of the fields of TYPE.
9237 DEFAULT_INDEX is the index of the default field; or -1 if there is
9238 no default. RANGES is indexed by "effective" field number (the
9239 field index, but omitting the discriminant and default fields) and
9240 must hold the discriminant values used by the variants. Note that
9241 RANGES must have a lifetime at least as long as OBSTACK -- either
9242 already allocated on it, or static. */
c9317f21 9243
9c6a1327
TT
9244static void
9245alloc_rust_variant (struct obstack *obstack, struct type *type,
9246 int discriminant_index, int default_index,
9247 gdb::array_view<discriminant_range> ranges)
9248{
9249 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9250 must be handled by the caller. */
9251 gdb_assert (discriminant_index >= 0
9252 && discriminant_index < TYPE_NFIELDS (type));
c9317f21 9253 gdb_assert (default_index == -1
c7b15a66 9254 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21 9255
9c6a1327
TT
9256 /* We have one variant for each non-discriminant field. */
9257 int n_variants = TYPE_NFIELDS (type) - 1;
c9317f21 9258
9c6a1327
TT
9259 variant *variants = new (obstack) variant[n_variants];
9260 int var_idx = 0;
9261 int range_idx = 0;
9262 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9263 {
9264 if (i == discriminant_index)
9265 continue;
c9317f21 9266
9c6a1327
TT
9267 variants[var_idx].first_field = i;
9268 variants[var_idx].last_field = i + 1;
9269
9270 /* The default field does not need a range, but other fields do.
9271 We skipped the discriminant above. */
9272 if (i != default_index)
9273 {
9274 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9275 ++range_idx;
9276 }
c9317f21 9277
9c6a1327
TT
9278 ++var_idx;
9279 }
9280
9281 gdb_assert (range_idx == ranges.size ());
9282 gdb_assert (var_idx == n_variants);
9283
9284 variant_part *part = new (obstack) variant_part;
9285 part->discriminant_index = discriminant_index;
9286 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9287 discriminant_index));
9288 part->variants = gdb::array_view<variant> (variants, n_variants);
9289
9290 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9291 gdb::array_view<variant_part> *prop_value
9292 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9293
9c6a1327
TT
9294 struct dynamic_prop prop;
9295 prop.kind = PROP_VARIANT_PARTS;
9296 prop.data.variant_parts = prop_value;
9297
5c54719c 9298 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9299}
9300
9301/* Some versions of rustc emitted enums in an unusual way.
9302
9303 Ordinary enums were emitted as unions. The first element of each
9304 structure in the union was named "RUST$ENUM$DISR". This element
9305 held the discriminant.
9306
9307 These versions of Rust also implemented the "non-zero"
9308 optimization. When the enum had two values, and one is empty and
9309 the other holds a pointer that cannot be zero, the pointer is used
9310 as the discriminant, with a zero value meaning the empty variant.
9311 Here, the union's first member is of the form
9312 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9313 where the fieldnos are the indices of the fields that should be
9314 traversed in order to find the field (which may be several fields deep)
9315 and the variantname is the name of the variant of the case when the
9316 field is zero.
9317
9318 This function recognizes whether TYPE is of one of these forms,
9319 and, if so, smashes it to be a variant type. */
9320
9321static void
9322quirk_rust_enum (struct type *type, struct objfile *objfile)
9323{
78134374 9324 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9325
9326 /* We don't need to deal with empty enums. */
9327 if (TYPE_NFIELDS (type) == 0)
9328 return;
9329
9330#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9331 if (TYPE_NFIELDS (type) == 1
9332 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9333 {
9334 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9335
9336 /* Decode the field name to find the offset of the
9337 discriminant. */
9338 ULONGEST bit_offset = 0;
9339 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9340 while (name[0] >= '0' && name[0] <= '9')
9341 {
9342 char *tail;
9343 unsigned long index = strtoul (name, &tail, 10);
9344 name = tail;
9345 if (*name != '$'
9346 || index >= TYPE_NFIELDS (field_type)
9347 || (TYPE_FIELD_LOC_KIND (field_type, index)
9348 != FIELD_LOC_KIND_BITPOS))
9349 {
b98664d3 9350 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9351 "[in module %s]"),
9352 TYPE_FIELD_NAME (type, 0),
9353 objfile_name (objfile));
9354 return;
9355 }
9356 ++name;
9357
9358 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9359 field_type = TYPE_FIELD_TYPE (field_type, index);
9360 }
9361
9c6a1327
TT
9362 /* Smash this type to be a structure type. We have to do this
9363 because the type has already been recorded. */
67607e24 9364 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9365 type->set_num_fields (3);
9c6a1327
TT
9366 /* Save the field we care about. */
9367 struct field saved_field = TYPE_FIELD (type, 0);
9368 TYPE_FIELDS (type)
c9317f21 9369 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
c9317f21 9370
9c6a1327
TT
9371 /* Put the discriminant at index 0. */
9372 TYPE_FIELD_TYPE (type, 0) = field_type;
9373 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9374 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9375 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
c9317f21
TT
9376
9377 /* The order of fields doesn't really matter, so put the real
9378 field at index 1 and the data-less field at index 2. */
9c6a1327
TT
9379 TYPE_FIELD (type, 1) = saved_field;
9380 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9381 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9382 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9383 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9384 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9385
9386 const char *dataless_name
7d93a1e0 9387 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9388 name);
9389 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9390 dataless_name);
9c6a1327 9391 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9392 /* NAME points into the original discriminant name, which
9393 already has the correct lifetime. */
9c6a1327
TT
9394 TYPE_FIELD_NAME (type, 2) = name;
9395 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
c9317f21 9396
9c6a1327
TT
9397 /* Indicate that this is a variant type. */
9398 static discriminant_range ranges[1] = { { 0, 0 } };
9399 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9400 }
77c2dba3
TT
9401 /* A union with a single anonymous field is probably an old-style
9402 univariant enum. */
9403 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9404 {
c9317f21
TT
9405 /* Smash this type to be a structure type. We have to do this
9406 because the type has already been recorded. */
67607e24 9407 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9408
9c6a1327 9409 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9410 const char *variant_name
7d93a1e0 9411 = rust_last_path_segment (field_type->name ());
9c6a1327 9412 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9413 field_type->set_name
9414 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9415 type->name (), variant_name));
c9317f21
TT
9416 }
9417 else
9418 {
9419 struct type *disr_type = nullptr;
9420 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9421 {
9422 disr_type = TYPE_FIELD_TYPE (type, i);
9423
78134374 9424 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9425 {
9426 /* All fields of a true enum will be structs. */
9427 return;
9428 }
9429 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9430 {
9431 /* Could be data-less variant, so keep going. */
a037790e 9432 disr_type = nullptr;
c9317f21
TT
9433 }
9434 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9435 "RUST$ENUM$DISR") != 0)
9436 {
9437 /* Not a Rust enum. */
9438 return;
9439 }
9440 else
9441 {
9442 /* Found one. */
9443 break;
9444 }
9445 }
9446
9447 /* If we got here without a discriminant, then it's probably
9448 just a union. */
9449 if (disr_type == nullptr)
9450 return;
9451
9452 /* Smash this type to be a structure type. We have to do this
9453 because the type has already been recorded. */
67607e24 9454 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9455
9c6a1327 9456 /* Make space for the discriminant field. */
c9317f21 9457 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9c6a1327 9458 field *new_fields
f408d82c 9459 = (struct field *) TYPE_ZALLOC (type, ((TYPE_NFIELDS (type) + 1)
9c6a1327
TT
9460 * sizeof (struct field)));
9461 memcpy (new_fields + 1, TYPE_FIELDS (type),
c9317f21 9462 TYPE_NFIELDS (type) * sizeof (struct field));
9c6a1327 9463 TYPE_FIELDS (type) = new_fields;
5e33d5f4 9464 type->set_num_fields (TYPE_NFIELDS (type) + 1);
c9317f21
TT
9465
9466 /* Install the discriminant at index 0 in the union. */
9c6a1327
TT
9467 TYPE_FIELD (type, 0) = *disr_field;
9468 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9469 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9470
9471 /* We need a way to find the correct discriminant given a
9472 variant name. For convenience we build a map here. */
9473 struct type *enum_type = FIELD_TYPE (*disr_field);
9474 std::unordered_map<std::string, ULONGEST> discriminant_map;
9475 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9476 {
9477 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9478 {
9479 const char *name
9480 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9481 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9482 }
9483 }
9484
9c6a1327
TT
9485 int n_fields = TYPE_NFIELDS (type);
9486 /* We don't need a range entry for the discriminant, but we do
9487 need one for every other field, as there is no default
9488 variant. */
9489 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9490 discriminant_range,
9491 n_fields - 1);
c9317f21
TT
9492 /* Skip the discriminant here. */
9493 for (int i = 1; i < n_fields; ++i)
9494 {
9495 /* Find the final word in the name of this variant's type.
9496 That name can be used to look up the correct
9497 discriminant. */
9498 const char *variant_name
7d93a1e0 9499 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9500
9501 auto iter = discriminant_map.find (variant_name);
9502 if (iter != discriminant_map.end ())
9c6a1327
TT
9503 {
9504 ranges[i].low = iter->second;
9505 ranges[i].high = iter->second;
9506 }
c9317f21 9507
bedda9ac 9508 /* Remove the discriminant field, if it exists. */
9c6a1327 9509 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
bedda9ac
TT
9510 if (TYPE_NFIELDS (sub_type) > 0)
9511 {
5e33d5f4 9512 sub_type->set_num_fields (sub_type->num_fields () - 1);
bedda9ac
TT
9513 ++TYPE_FIELDS (sub_type);
9514 }
9c6a1327 9515 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9516 sub_type->set_name
9517 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9518 type->name (), variant_name));
c9317f21 9519 }
9c6a1327
TT
9520
9521 /* Indicate that this is a variant type. */
9522 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9523 gdb::array_view<discriminant_range> (ranges,
9524 n_fields - 1));
c9317f21
TT
9525 }
9526}
9527
9528/* Rewrite some Rust unions to be structures with variants parts. */
9529
9530static void
9531rust_union_quirks (struct dwarf2_cu *cu)
9532{
9533 gdb_assert (cu->language == language_rust);
52941706
SM
9534 for (type *type_ : cu->rust_unions)
9535 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9536 /* We don't need this any more. */
9537 cu->rust_unions.clear ();
c9317f21
TT
9538}
9539
95554aad
TT
9540/* Return the symtab for PER_CU. This works properly regardless of
9541 whether we're using the index or psymtabs. */
9542
43f3e411
DE
9543static struct compunit_symtab *
9544get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9545{
ed2dc618 9546 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9547 ? per_cu->v.quick->compunit_symtab
9548 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9549}
9550
9551/* A helper function for computing the list of all symbol tables
9552 included by PER_CU. */
9553
9554static void
4c39bc03 9555recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9556 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9557 struct dwarf2_per_cu_data *per_cu,
43f3e411 9558 struct compunit_symtab *immediate_parent)
95554aad
TT
9559{
9560 void **slot;
43f3e411 9561 struct compunit_symtab *cust;
95554aad
TT
9562
9563 slot = htab_find_slot (all_children, per_cu, INSERT);
9564 if (*slot != NULL)
9565 {
9566 /* This inclusion and its children have been processed. */
9567 return;
9568 }
9569
9570 *slot = per_cu;
9571 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9572 cust = get_compunit_symtab (per_cu);
9573 if (cust != NULL)
ec94af83
DE
9574 {
9575 /* If this is a type unit only add its symbol table if we haven't
9576 seen it yet (type unit per_cu's can share symtabs). */
9577 if (per_cu->is_debug_types)
9578 {
43f3e411 9579 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9580 if (*slot == NULL)
9581 {
43f3e411 9582 *slot = cust;
4c39bc03 9583 result->push_back (cust);
43f3e411
DE
9584 if (cust->user == NULL)
9585 cust->user = immediate_parent;
ec94af83
DE
9586 }
9587 }
9588 else
f9125b6c 9589 {
4c39bc03 9590 result->push_back (cust);
43f3e411
DE
9591 if (cust->user == NULL)
9592 cust->user = immediate_parent;
f9125b6c 9593 }
ec94af83 9594 }
95554aad 9595
ae640021
AB
9596 if (!per_cu->imported_symtabs_empty ())
9597 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9598 {
9599 recursively_compute_inclusions (result, all_children,
9600 all_type_symtabs, ptr, cust);
9601 }
95554aad
TT
9602}
9603
43f3e411 9604/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9605 PER_CU. */
9606
9607static void
43f3e411 9608compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9609{
f4dc4d17
DE
9610 gdb_assert (! per_cu->is_debug_types);
9611
ae640021 9612 if (!per_cu->imported_symtabs_empty ())
95554aad 9613 {
ae640021 9614 int len;
4c39bc03 9615 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9616 htab_t all_children, all_type_symtabs;
43f3e411 9617 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9618
9619 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9620 if (cust == NULL)
95554aad
TT
9621 return;
9622
9623 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9624 NULL, xcalloc, xfree);
ec94af83
DE
9625 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9626 NULL, xcalloc, xfree);
95554aad 9627
ae640021 9628 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9629 {
9630 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9631 all_type_symtabs, ptr, cust);
ec94af83 9632 }
95554aad 9633
ec94af83 9634 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9635 len = result_symtabs.size ();
43f3e411 9636 cust->includes
ed2dc618 9637 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9638 struct compunit_symtab *, len + 1);
4c39bc03
TT
9639 memcpy (cust->includes, result_symtabs.data (),
9640 len * sizeof (compunit_symtab *));
43f3e411 9641 cust->includes[len] = NULL;
95554aad 9642
95554aad 9643 htab_delete (all_children);
ec94af83 9644 htab_delete (all_type_symtabs);
95554aad
TT
9645 }
9646}
9647
9648/* Compute the 'includes' field for the symtabs of all the CUs we just
9649 read. */
9650
9651static void
ed2dc618 9652process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9653{
71b73764 9654 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9655 {
9656 if (! iter->is_debug_types)
43f3e411 9657 compute_compunit_symtab_includes (iter);
f4dc4d17 9658 }
95554aad 9659
c5d0225d 9660 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9661}
9662
9cdd5dbd 9663/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9664 already been loaded into memory. */
9665
9666static void
95554aad
TT
9667process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9668 enum language pretend_language)
10b3939b 9669{
10b3939b 9670 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9671 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9672 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9673 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9674 CORE_ADDR lowpc, highpc;
43f3e411 9675 struct compunit_symtab *cust;
10b3939b 9676 CORE_ADDR baseaddr;
4359dff1 9677 struct block *static_block;
3e29f34a 9678 CORE_ADDR addr;
10b3939b 9679
b3b3bada 9680 baseaddr = objfile->text_section_offset ();
10b3939b 9681
c89b44cd
TT
9682 /* Clear the list here in case something was left over. */
9683 cu->method_list.clear ();
10b3939b 9684
95554aad
TT
9685 cu->language = pretend_language;
9686 cu->language_defn = language_def (cu->language);
9687
c906108c 9688 /* Do line number decoding in read_file_scope () */
10b3939b 9689 process_die (cu->dies, cu);
c906108c 9690
a766d390
DE
9691 /* For now fudge the Go package. */
9692 if (cu->language == language_go)
9693 fixup_go_packaging (cu);
9694
5f48f8f3 9695 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9696 should be complete, and it should now be safe to compute all of the
9697 physnames. */
9698 compute_delayed_physnames (cu);
3da10d80 9699
c9317f21
TT
9700 if (cu->language == language_rust)
9701 rust_union_quirks (cu);
9702
fae299cd
DC
9703 /* Some compilers don't define a DW_AT_high_pc attribute for the
9704 compilation unit. If the DW_AT_high_pc is missing, synthesize
9705 it, by scanning the DIE's below the compilation unit. */
10b3939b 9706 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9707
3e29f34a 9708 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9709 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9710
9711 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9712 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9713 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9714 addrmap to help ensure it has an accurate map of pc values belonging to
9715 this comp unit. */
9716 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9717
c24bdb02 9718 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9719 SECT_OFF_TEXT (objfile),
9720 0);
c906108c 9721
43f3e411 9722 if (cust != NULL)
c906108c 9723 {
df15bd07 9724 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9725
8be455d7
JK
9726 /* Set symtab language to language from DW_AT_language. If the
9727 compilation is from a C file generated by language preprocessors, do
9728 not set the language if it was already deduced by start_subfile. */
43f3e411 9729 if (!(cu->language == language_c
40e3ad0e 9730 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9731 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9732
9733 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9734 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9735 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9736 there were bugs in prologue debug info, fixed later in GCC-4.5
9737 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9738
9739 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9740 needed, it would be wrong due to missing DW_AT_producer there.
9741
9742 Still one can confuse GDB by using non-standard GCC compilation
9743 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9744 */
ab260dad 9745 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9746 cust->locations_valid = 1;
e0d00bc7
JK
9747
9748 if (gcc_4_minor >= 5)
43f3e411 9749 cust->epilogue_unwind_valid = 1;
96408a79 9750
43f3e411 9751 cust->call_site_htab = cu->call_site_htab;
c906108c 9752 }
9291a0cd
TT
9753
9754 if (dwarf2_per_objfile->using_index)
43f3e411 9755 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9756 else
9757 {
891813be 9758 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9759 pst->compunit_symtab = cust;
6d94535f 9760 pst->readin = true;
9291a0cd 9761 }
c906108c 9762
95554aad 9763 /* Push it for inclusion processing later. */
c5d0225d 9764 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9765
9766 /* Not needed any more. */
c24bdb02 9767 cu->reset_builder ();
f4dc4d17 9768}
45cfd468 9769
f4dc4d17
DE
9770/* Generate full symbol information for type unit PER_CU, whose DIEs have
9771 already been loaded into memory. */
9772
9773static void
9774process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9775 enum language pretend_language)
9776{
9777 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9778 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9779 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9780 struct compunit_symtab *cust;
0186c6a7
DE
9781 struct signatured_type *sig_type;
9782
9783 gdb_assert (per_cu->is_debug_types);
9784 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9785
c89b44cd
TT
9786 /* Clear the list here in case something was left over. */
9787 cu->method_list.clear ();
f4dc4d17 9788
f4dc4d17
DE
9789 cu->language = pretend_language;
9790 cu->language_defn = language_def (cu->language);
9791
9792 /* The symbol tables are set up in read_type_unit_scope. */
9793 process_die (cu->dies, cu);
9794
9795 /* For now fudge the Go package. */
9796 if (cu->language == language_go)
9797 fixup_go_packaging (cu);
9798
5f48f8f3 9799 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9800 should be complete, and it should now be safe to compute all of the
9801 physnames. */
9802 compute_delayed_physnames (cu);
f4dc4d17 9803
c9317f21
TT
9804 if (cu->language == language_rust)
9805 rust_union_quirks (cu);
9806
f4dc4d17
DE
9807 /* TUs share symbol tables.
9808 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9809 of it with end_expandable_symtab. Otherwise, complete the addition of
9810 this TU's symbols to the existing symtab. */
43f3e411 9811 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9812 {
c24bdb02
KS
9813 buildsym_compunit *builder = cu->get_builder ();
9814 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9815 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9816
43f3e411 9817 if (cust != NULL)
f4dc4d17
DE
9818 {
9819 /* Set symtab language to language from DW_AT_language. If the
9820 compilation is from a C file generated by language preprocessors,
9821 do not set the language if it was already deduced by
9822 start_subfile. */
43f3e411
DE
9823 if (!(cu->language == language_c
9824 && COMPUNIT_FILETABS (cust)->language != language_c))
9825 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9826 }
9827 }
9828 else
9829 {
c24bdb02 9830 cu->get_builder ()->augment_type_symtab ();
43f3e411 9831 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9832 }
9833
9834 if (dwarf2_per_objfile->using_index)
43f3e411 9835 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9836 else
9837 {
891813be 9838 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9839 pst->compunit_symtab = cust;
6d94535f 9840 pst->readin = true;
45cfd468 9841 }
804d2729
TT
9842
9843 /* Not needed any more. */
c24bdb02 9844 cu->reset_builder ();
c906108c
SS
9845}
9846
95554aad
TT
9847/* Process an imported unit DIE. */
9848
9849static void
9850process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9851{
9852 struct attribute *attr;
9853
f4dc4d17
DE
9854 /* For now we don't handle imported units in type units. */
9855 if (cu->per_cu->is_debug_types)
9856 {
9857 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9858 " supported in type units [in module %s]"),
518817b3 9859 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9860 }
9861
95554aad
TT
9862 attr = dwarf2_attr (die, DW_AT_import, cu);
9863 if (attr != NULL)
9864 {
0826b30a 9865 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9866 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9867 dwarf2_per_cu_data *per_cu
e3b94546 9868 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9869 cu->per_cu->dwarf2_per_objfile);
95554aad 9870
58990295
TV
9871 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9872 into another compilation unit, at root level. Regard this as a hint,
9873 and ignore it. */
9874 if (die->parent && die->parent->parent == NULL
9875 && per_cu->unit_type == DW_UT_compile
9876 && per_cu->lang == language_cplus)
9877 return;
9878
69d751e3 9879 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9880 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9881 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9882
ae640021 9883 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9884 }
9885}
9886
4c8aa72d
PA
9887/* RAII object that represents a process_die scope: i.e.,
9888 starts/finishes processing a DIE. */
9889class process_die_scope
adde2bff 9890{
4c8aa72d
PA
9891public:
9892 process_die_scope (die_info *die, dwarf2_cu *cu)
9893 : m_die (die), m_cu (cu)
9894 {
9895 /* We should only be processing DIEs not already in process. */
9896 gdb_assert (!m_die->in_process);
9897 m_die->in_process = true;
9898 }
8c3cb9fa 9899
4c8aa72d
PA
9900 ~process_die_scope ()
9901 {
9902 m_die->in_process = false;
9903
9904 /* If we're done processing the DIE for the CU that owns the line
9905 header, we don't need the line header anymore. */
9906 if (m_cu->line_header_die_owner == m_die)
9907 {
9908 delete m_cu->line_header;
9909 m_cu->line_header = NULL;
9910 m_cu->line_header_die_owner = NULL;
9911 }
9912 }
9913
9914private:
9915 die_info *m_die;
9916 dwarf2_cu *m_cu;
9917};
adde2bff 9918
c906108c
SS
9919/* Process a die and its children. */
9920
9921static void
e7c27a73 9922process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9923{
4c8aa72d 9924 process_die_scope scope (die, cu);
adde2bff 9925
c906108c
SS
9926 switch (die->tag)
9927 {
9928 case DW_TAG_padding:
9929 break;
9930 case DW_TAG_compile_unit:
95554aad 9931 case DW_TAG_partial_unit:
e7c27a73 9932 read_file_scope (die, cu);
c906108c 9933 break;
348e048f
DE
9934 case DW_TAG_type_unit:
9935 read_type_unit_scope (die, cu);
9936 break;
c906108c 9937 case DW_TAG_subprogram:
0a4b0913
AB
9938 /* Nested subprograms in Fortran get a prefix. */
9939 if (cu->language == language_fortran
9940 && die->parent != NULL
9941 && die->parent->tag == DW_TAG_subprogram)
9942 cu->processing_has_namespace_info = true;
9943 /* Fall through. */
c906108c 9944 case DW_TAG_inlined_subroutine:
edb3359d 9945 read_func_scope (die, cu);
c906108c
SS
9946 break;
9947 case DW_TAG_lexical_block:
14898363
L
9948 case DW_TAG_try_block:
9949 case DW_TAG_catch_block:
e7c27a73 9950 read_lexical_block_scope (die, cu);
c906108c 9951 break;
216f72a1 9952 case DW_TAG_call_site:
96408a79
SA
9953 case DW_TAG_GNU_call_site:
9954 read_call_site_scope (die, cu);
9955 break;
c906108c 9956 case DW_TAG_class_type:
680b30c7 9957 case DW_TAG_interface_type:
c906108c
SS
9958 case DW_TAG_structure_type:
9959 case DW_TAG_union_type:
134d01f1 9960 process_structure_scope (die, cu);
c906108c
SS
9961 break;
9962 case DW_TAG_enumeration_type:
134d01f1 9963 process_enumeration_scope (die, cu);
c906108c 9964 break;
134d01f1 9965
f792889a
DJ
9966 /* These dies have a type, but processing them does not create
9967 a symbol or recurse to process the children. Therefore we can
9968 read them on-demand through read_type_die. */
c906108c 9969 case DW_TAG_subroutine_type:
72019c9c 9970 case DW_TAG_set_type:
c906108c 9971 case DW_TAG_array_type:
c906108c 9972 case DW_TAG_pointer_type:
c906108c 9973 case DW_TAG_ptr_to_member_type:
c906108c 9974 case DW_TAG_reference_type:
4297a3f0 9975 case DW_TAG_rvalue_reference_type:
c906108c 9976 case DW_TAG_string_type:
c906108c 9977 break;
134d01f1 9978
c906108c 9979 case DW_TAG_base_type:
a02abb62 9980 case DW_TAG_subrange_type:
cb249c71 9981 case DW_TAG_typedef:
134d01f1
DJ
9982 /* Add a typedef symbol for the type definition, if it has a
9983 DW_AT_name. */
f792889a 9984 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9985 break;
c906108c 9986 case DW_TAG_common_block:
e7c27a73 9987 read_common_block (die, cu);
c906108c
SS
9988 break;
9989 case DW_TAG_common_inclusion:
9990 break;
d9fa45fe 9991 case DW_TAG_namespace:
9068261f 9992 cu->processing_has_namespace_info = true;
e7c27a73 9993 read_namespace (die, cu);
d9fa45fe 9994 break;
5d7cb8df 9995 case DW_TAG_module:
9068261f 9996 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9997 read_module (die, cu);
9998 break;
d9fa45fe 9999 case DW_TAG_imported_declaration:
9068261f 10000 cu->processing_has_namespace_info = true;
74921315
KS
10001 if (read_namespace_alias (die, cu))
10002 break;
86a73007
TT
10003 /* The declaration is not a global namespace alias. */
10004 /* Fall through. */
d9fa45fe 10005 case DW_TAG_imported_module:
9068261f 10006 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10007 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10008 || cu->language != language_fortran))
b98664d3 10009 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10010 dwarf_tag_name (die->tag));
10011 read_import_statement (die, cu);
d9fa45fe 10012 break;
95554aad
TT
10013
10014 case DW_TAG_imported_unit:
10015 process_imported_unit_die (die, cu);
10016 break;
10017
71a3c369
TT
10018 case DW_TAG_variable:
10019 read_variable (die, cu);
10020 break;
10021
c906108c 10022 default:
e7c27a73 10023 new_symbol (die, NULL, cu);
c906108c
SS
10024 break;
10025 }
10026}
ca69b9e6
DE
10027\f
10028/* DWARF name computation. */
c906108c 10029
94af9270
KS
10030/* A helper function for dwarf2_compute_name which determines whether DIE
10031 needs to have the name of the scope prepended to the name listed in the
10032 die. */
10033
10034static int
10035die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10036{
1c809c68
TT
10037 struct attribute *attr;
10038
94af9270
KS
10039 switch (die->tag)
10040 {
10041 case DW_TAG_namespace:
10042 case DW_TAG_typedef:
10043 case DW_TAG_class_type:
10044 case DW_TAG_interface_type:
10045 case DW_TAG_structure_type:
10046 case DW_TAG_union_type:
10047 case DW_TAG_enumeration_type:
10048 case DW_TAG_enumerator:
10049 case DW_TAG_subprogram:
08a76f8a 10050 case DW_TAG_inlined_subroutine:
94af9270 10051 case DW_TAG_member:
74921315 10052 case DW_TAG_imported_declaration:
94af9270
KS
10053 return 1;
10054
10055 case DW_TAG_variable:
c2b0a229 10056 case DW_TAG_constant:
94af9270
KS
10057 /* We only need to prefix "globally" visible variables. These include
10058 any variable marked with DW_AT_external or any variable that
10059 lives in a namespace. [Variables in anonymous namespaces
10060 require prefixing, but they are not DW_AT_external.] */
10061
10062 if (dwarf2_attr (die, DW_AT_specification, cu))
10063 {
10064 struct dwarf2_cu *spec_cu = cu;
9a619af0 10065
94af9270
KS
10066 return die_needs_namespace (die_specification (die, &spec_cu),
10067 spec_cu);
10068 }
10069
1c809c68 10070 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10071 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10072 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10073 return 0;
10074 /* A variable in a lexical block of some kind does not need a
10075 namespace, even though in C++ such variables may be external
10076 and have a mangled name. */
10077 if (die->parent->tag == DW_TAG_lexical_block
10078 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10079 || die->parent->tag == DW_TAG_catch_block
10080 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10081 return 0;
10082 return 1;
94af9270
KS
10083
10084 default:
10085 return 0;
10086 }
10087}
10088
73b9be8b
KS
10089/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10090 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10091 defined for the given DIE. */
10092
10093static struct attribute *
10094dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10095{
10096 struct attribute *attr;
10097
10098 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10099 if (attr == NULL)
10100 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10101
10102 return attr;
10103}
10104
10105/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10106 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10107 defined for the given DIE. */
10108
10109static const char *
10110dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10111{
10112 const char *linkage_name;
10113
10114 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10115 if (linkage_name == NULL)
10116 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10117
787de330
TT
10118 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10119 See https://github.com/rust-lang/rust/issues/32925. */
10120 if (cu->language == language_rust && linkage_name != NULL
10121 && strchr (linkage_name, '{') != NULL)
10122 linkage_name = NULL;
10123
73b9be8b
KS
10124 return linkage_name;
10125}
10126
94af9270 10127/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10128 compute the physname for the object, which include a method's:
9c37b5ae 10129 - formal parameters (C++),
a766d390 10130 - receiver type (Go),
a766d390
DE
10131
10132 The term "physname" is a bit confusing.
10133 For C++, for example, it is the demangled name.
10134 For Go, for example, it's the mangled name.
94af9270 10135
af6b7be1
JB
10136 For Ada, return the DIE's linkage name rather than the fully qualified
10137 name. PHYSNAME is ignored..
10138
94af9270
KS
10139 The result is allocated on the objfile_obstack and canonicalized. */
10140
10141static const char *
15d034d0
TT
10142dwarf2_compute_name (const char *name,
10143 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10144 int physname)
10145{
518817b3 10146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10147
94af9270
KS
10148 if (name == NULL)
10149 name = dwarf2_name (die, cu);
10150
2ee7123e
DE
10151 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10152 but otherwise compute it by typename_concat inside GDB.
10153 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10154 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10155 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10156 will set the demangled name to the result of dwarf2_full_name, and it is
10157 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10158 if (cu->language == language_ada
10159 || (cu->language == language_fortran && physname))
10160 {
10161 /* For Ada unit, we prefer the linkage name over the name, as
10162 the former contains the exported name, which the user expects
10163 to be able to reference. Ideally, we want the user to be able
10164 to reference this entity using either natural or linkage name,
10165 but we haven't started looking at this enhancement yet. */
73b9be8b 10166 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10167
2ee7123e
DE
10168 if (linkage_name != NULL)
10169 return linkage_name;
f55ee35c
JK
10170 }
10171
94af9270
KS
10172 /* These are the only languages we know how to qualify names in. */
10173 if (name != NULL
9c37b5ae 10174 && (cu->language == language_cplus
c44af4eb
TT
10175 || cu->language == language_fortran || cu->language == language_d
10176 || cu->language == language_rust))
94af9270
KS
10177 {
10178 if (die_needs_namespace (die, cu))
10179 {
0d5cff50 10180 const char *prefix;
34a68019 10181 const char *canonical_name = NULL;
94af9270 10182
d7e74731
PA
10183 string_file buf;
10184
94af9270 10185 prefix = determine_prefix (die, cu);
94af9270
KS
10186 if (*prefix != '\0')
10187 {
43816ebc
TT
10188 gdb::unique_xmalloc_ptr<char> prefixed_name
10189 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10190
43816ebc 10191 buf.puts (prefixed_name.get ());
94af9270
KS
10192 }
10193 else
d7e74731 10194 buf.puts (name);
94af9270 10195
98bfdba5
PA
10196 /* Template parameters may be specified in the DIE's DW_AT_name, or
10197 as children with DW_TAG_template_type_param or
10198 DW_TAG_value_type_param. If the latter, add them to the name
10199 here. If the name already has template parameters, then
10200 skip this step; some versions of GCC emit both, and
10201 it is more efficient to use the pre-computed name.
10202
10203 Something to keep in mind about this process: it is very
10204 unlikely, or in some cases downright impossible, to produce
10205 something that will match the mangled name of a function.
10206 If the definition of the function has the same debug info,
10207 we should be able to match up with it anyway. But fallbacks
10208 using the minimal symbol, for instance to find a method
10209 implemented in a stripped copy of libstdc++, will not work.
10210 If we do not have debug info for the definition, we will have to
10211 match them up some other way.
10212
10213 When we do name matching there is a related problem with function
10214 templates; two instantiated function templates are allowed to
10215 differ only by their return types, which we do not add here. */
10216
10217 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10218 {
10219 struct attribute *attr;
10220 struct die_info *child;
10221 int first = 1;
10222
10223 die->building_fullname = 1;
10224
10225 for (child = die->child; child != NULL; child = child->sibling)
10226 {
10227 struct type *type;
12df843f 10228 LONGEST value;
d521ce57 10229 const gdb_byte *bytes;
98bfdba5
PA
10230 struct dwarf2_locexpr_baton *baton;
10231 struct value *v;
10232
10233 if (child->tag != DW_TAG_template_type_param
10234 && child->tag != DW_TAG_template_value_param)
10235 continue;
10236
10237 if (first)
10238 {
d7e74731 10239 buf.puts ("<");
98bfdba5
PA
10240 first = 0;
10241 }
10242 else
d7e74731 10243 buf.puts (", ");
98bfdba5
PA
10244
10245 attr = dwarf2_attr (child, DW_AT_type, cu);
10246 if (attr == NULL)
10247 {
b98664d3 10248 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10249 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10250 continue;
10251 }
10252 type = die_type (child, cu);
10253
10254 if (child->tag == DW_TAG_template_type_param)
10255 {
c1ec8cea
TT
10256 c_print_type (type, "", &buf, -1, 0, cu->language,
10257 &type_print_raw_options);
98bfdba5
PA
10258 continue;
10259 }
10260
10261 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10262 if (attr == NULL)
10263 {
b98664d3 10264 complaint (_("template parameter missing "
3e43a32a 10265 "DW_AT_const_value"));
d7e74731 10266 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10267 continue;
10268 }
10269
10270 dwarf2_const_value_attr (attr, type, name,
10271 &cu->comp_unit_obstack, cu,
10272 &value, &bytes, &baton);
10273
10274 if (TYPE_NOSIGN (type))
10275 /* GDB prints characters as NUMBER 'CHAR'. If that's
10276 changed, this can use value_print instead. */
d7e74731 10277 c_printchar (value, type, &buf);
98bfdba5
PA
10278 else
10279 {
10280 struct value_print_options opts;
10281
10282 if (baton != NULL)
10283 v = dwarf2_evaluate_loc_desc (type, NULL,
10284 baton->data,
10285 baton->size,
10286 baton->per_cu);
10287 else if (bytes != NULL)
10288 {
10289 v = allocate_value (type);
10290 memcpy (value_contents_writeable (v), bytes,
10291 TYPE_LENGTH (type));
10292 }
10293 else
10294 v = value_from_longest (type, value);
10295
3e43a32a
MS
10296 /* Specify decimal so that we do not depend on
10297 the radix. */
98bfdba5
PA
10298 get_formatted_print_options (&opts, 'd');
10299 opts.raw = 1;
d7e74731 10300 value_print (v, &buf, &opts);
98bfdba5 10301 release_value (v);
98bfdba5
PA
10302 }
10303 }
10304
10305 die->building_fullname = 0;
10306
10307 if (!first)
10308 {
10309 /* Close the argument list, with a space if necessary
10310 (nested templates). */
d7e74731
PA
10311 if (!buf.empty () && buf.string ().back () == '>')
10312 buf.puts (" >");
98bfdba5 10313 else
d7e74731 10314 buf.puts (">");
98bfdba5
PA
10315 }
10316 }
10317
9c37b5ae 10318 /* For C++ methods, append formal parameter type
94af9270 10319 information, if PHYSNAME. */
6e70227d 10320
94af9270 10321 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10322 && cu->language == language_cplus)
94af9270
KS
10323 {
10324 struct type *type = read_type_die (die, cu);
10325
d7e74731 10326 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10327 &type_print_raw_options);
94af9270 10328
9c37b5ae 10329 if (cu->language == language_cplus)
94af9270 10330 {
60430eff
DJ
10331 /* Assume that an artificial first parameter is
10332 "this", but do not crash if it is not. RealView
10333 marks unnamed (and thus unused) parameters as
10334 artificial; there is no way to differentiate
10335 the two cases. */
94af9270
KS
10336 if (TYPE_NFIELDS (type) > 0
10337 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10338 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10339 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10340 0))))
d7e74731 10341 buf.puts (" const");
94af9270
KS
10342 }
10343 }
10344
d7e74731 10345 const std::string &intermediate_name = buf.string ();
94af9270
KS
10346
10347 if (cu->language == language_cplus)
34a68019 10348 canonical_name
322a8516 10349 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10350 objfile);
34a68019
TT
10351
10352 /* If we only computed INTERMEDIATE_NAME, or if
10353 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10354 intern it. */
322a8516 10355 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10356 name = objfile->intern (intermediate_name);
34a68019
TT
10357 else
10358 name = canonical_name;
94af9270
KS
10359 }
10360 }
10361
10362 return name;
10363}
10364
0114d602
DJ
10365/* Return the fully qualified name of DIE, based on its DW_AT_name.
10366 If scope qualifiers are appropriate they will be added. The result
34a68019 10367 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10368 not have a name. NAME may either be from a previous call to
10369 dwarf2_name or NULL.
10370
9c37b5ae 10371 The output string will be canonicalized (if C++). */
0114d602
DJ
10372
10373static const char *
15d034d0 10374dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10375{
94af9270
KS
10376 return dwarf2_compute_name (name, die, cu, 0);
10377}
0114d602 10378
94af9270
KS
10379/* Construct a physname for the given DIE in CU. NAME may either be
10380 from a previous call to dwarf2_name or NULL. The result will be
10381 allocated on the objfile_objstack or NULL if the DIE does not have a
10382 name.
0114d602 10383
9c37b5ae 10384 The output string will be canonicalized (if C++). */
0114d602 10385
94af9270 10386static const char *
15d034d0 10387dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10388{
518817b3 10389 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10390 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10391 int need_copy = 1;
10392
10393 /* In this case dwarf2_compute_name is just a shortcut not building anything
10394 on its own. */
10395 if (!die_needs_namespace (die, cu))
10396 return dwarf2_compute_name (name, die, cu, 1);
10397
906bb4c5
TT
10398 if (cu->language != language_rust)
10399 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10400
10401 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10402 has computed. */
791afaa2 10403 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10404 if (mangled != NULL)
900e11f9 10405 {
900e11f9 10406
59cc4834
JB
10407 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10408 {
10409 /* Do nothing (do not demangle the symbol name). */
10410 }
10411 else if (cu->language == language_go)
a766d390 10412 {
5e2db402
TT
10413 /* This is a lie, but we already lie to the caller new_symbol.
10414 new_symbol assumes we return the mangled name.
a766d390 10415 This just undoes that lie until things are cleaned up. */
a766d390
DE
10416 }
10417 else
10418 {
0eb876f5
JB
10419 /* Use DMGL_RET_DROP for C++ template functions to suppress
10420 their return type. It is easier for GDB users to search
10421 for such functions as `name(params)' than `long name(params)'.
10422 In such case the minimal symbol names do not match the full
10423 symbol names but for template functions there is never a need
10424 to look up their definition from their declaration so
10425 the only disadvantage remains the minimal symbol variant
10426 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10427 demangled.reset (gdb_demangle (mangled,
10428 (DMGL_PARAMS | DMGL_ANSI
10429 | DMGL_RET_DROP)));
a766d390 10430 }
900e11f9 10431 if (demangled)
791afaa2 10432 canon = demangled.get ();
900e11f9
JK
10433 else
10434 {
10435 canon = mangled;
10436 need_copy = 0;
10437 }
10438 }
10439
10440 if (canon == NULL || check_physname)
10441 {
10442 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10443
10444 if (canon != NULL && strcmp (physname, canon) != 0)
10445 {
10446 /* It may not mean a bug in GDB. The compiler could also
10447 compute DW_AT_linkage_name incorrectly. But in such case
10448 GDB would need to be bug-to-bug compatible. */
10449
b98664d3 10450 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10451 "(from linkage <%s>) - DIE at %s [in module %s]"),
10452 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10453 objfile_name (objfile));
900e11f9
JK
10454
10455 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10456 is available here - over computed PHYSNAME. It is safer
10457 against both buggy GDB and buggy compilers. */
10458
10459 retval = canon;
10460 }
10461 else
10462 {
10463 retval = physname;
10464 need_copy = 0;
10465 }
10466 }
10467 else
10468 retval = canon;
10469
10470 if (need_copy)
be1e3d3e 10471 retval = objfile->intern (retval);
900e11f9 10472
900e11f9 10473 return retval;
0114d602
DJ
10474}
10475
74921315
KS
10476/* Inspect DIE in CU for a namespace alias. If one exists, record
10477 a new symbol for it.
10478
10479 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10480
10481static int
10482read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10483{
10484 struct attribute *attr;
10485
10486 /* If the die does not have a name, this is not a namespace
10487 alias. */
10488 attr = dwarf2_attr (die, DW_AT_name, cu);
10489 if (attr != NULL)
10490 {
10491 int num;
10492 struct die_info *d = die;
10493 struct dwarf2_cu *imported_cu = cu;
10494
10495 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10496 keep inspecting DIEs until we hit the underlying import. */
10497#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10498 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10499 {
10500 attr = dwarf2_attr (d, DW_AT_import, cu);
10501 if (attr == NULL)
10502 break;
10503
10504 d = follow_die_ref (d, attr, &imported_cu);
10505 if (d->tag != DW_TAG_imported_declaration)
10506 break;
10507 }
10508
10509 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10510 {
b98664d3 10511 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10512 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10513 return 0;
10514 }
10515
10516 if (attr != NULL)
10517 {
10518 struct type *type;
0826b30a 10519 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10520
9c541725 10521 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10522 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10523 {
10524 /* This declaration is a global namespace alias. Add
10525 a symbol for it whose type is the aliased namespace. */
10526 new_symbol (die, type, cu);
10527 return 1;
10528 }
10529 }
10530 }
10531
10532 return 0;
10533}
10534
22cee43f 10535/* Return the using directives repository (global or local?) to use in the
804d2729 10536 current context for CU.
22cee43f
PMR
10537
10538 For Ada, imported declarations can materialize renamings, which *may* be
10539 global. However it is impossible (for now?) in DWARF to distinguish
10540 "external" imported declarations and "static" ones. As all imported
10541 declarations seem to be static in all other languages, make them all CU-wide
10542 global only in Ada. */
10543
10544static struct using_direct **
804d2729 10545using_directives (struct dwarf2_cu *cu)
22cee43f 10546{
c24bdb02
KS
10547 if (cu->language == language_ada
10548 && cu->get_builder ()->outermost_context_p ())
10549 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10550 else
c24bdb02 10551 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10552}
10553
27aa8d6a
SW
10554/* Read the import statement specified by the given die and record it. */
10555
10556static void
10557read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10558{
518817b3 10559 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10560 struct attribute *import_attr;
32019081 10561 struct die_info *imported_die, *child_die;
de4affc9 10562 struct dwarf2_cu *imported_cu;
27aa8d6a 10563 const char *imported_name;
794684b6 10564 const char *imported_name_prefix;
13387711
SW
10565 const char *canonical_name;
10566 const char *import_alias;
10567 const char *imported_declaration = NULL;
794684b6 10568 const char *import_prefix;
eb1e02fd 10569 std::vector<const char *> excludes;
13387711 10570
27aa8d6a
SW
10571 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10572 if (import_attr == NULL)
10573 {
b98664d3 10574 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10575 dwarf_tag_name (die->tag));
10576 return;
10577 }
10578
de4affc9
CC
10579 imported_cu = cu;
10580 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10581 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10582 if (imported_name == NULL)
10583 {
10584 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10585
10586 The import in the following code:
10587 namespace A
10588 {
10589 typedef int B;
10590 }
10591
10592 int main ()
10593 {
10594 using A::B;
10595 B b;
10596 return b;
10597 }
10598
10599 ...
10600 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10601 <52> DW_AT_decl_file : 1
10602 <53> DW_AT_decl_line : 6
10603 <54> DW_AT_import : <0x75>
10604 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10605 <59> DW_AT_name : B
10606 <5b> DW_AT_decl_file : 1
10607 <5c> DW_AT_decl_line : 2
10608 <5d> DW_AT_type : <0x6e>
10609 ...
10610 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10611 <76> DW_AT_byte_size : 4
10612 <77> DW_AT_encoding : 5 (signed)
10613
10614 imports the wrong die ( 0x75 instead of 0x58 ).
10615 This case will be ignored until the gcc bug is fixed. */
10616 return;
10617 }
10618
82856980
SW
10619 /* Figure out the local name after import. */
10620 import_alias = dwarf2_name (die, cu);
27aa8d6a 10621
794684b6
SW
10622 /* Figure out where the statement is being imported to. */
10623 import_prefix = determine_prefix (die, cu);
10624
10625 /* Figure out what the scope of the imported die is and prepend it
10626 to the name of the imported die. */
de4affc9 10627 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10628
f55ee35c
JK
10629 if (imported_die->tag != DW_TAG_namespace
10630 && imported_die->tag != DW_TAG_module)
794684b6 10631 {
13387711
SW
10632 imported_declaration = imported_name;
10633 canonical_name = imported_name_prefix;
794684b6 10634 }
13387711 10635 else if (strlen (imported_name_prefix) > 0)
12aaed36 10636 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10637 imported_name_prefix,
10638 (cu->language == language_d ? "." : "::"),
10639 imported_name, (char *) NULL);
13387711
SW
10640 else
10641 canonical_name = imported_name;
794684b6 10642
32019081
JK
10643 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10644 for (child_die = die->child; child_die && child_die->tag;
436c571c 10645 child_die = child_die->sibling)
32019081
JK
10646 {
10647 /* DWARF-4: A Fortran use statement with a “rename list” may be
10648 represented by an imported module entry with an import attribute
10649 referring to the module and owned entries corresponding to those
10650 entities that are renamed as part of being imported. */
10651
10652 if (child_die->tag != DW_TAG_imported_declaration)
10653 {
b98664d3 10654 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10655 "- DIE at %s [in module %s]"),
10656 sect_offset_str (child_die->sect_off),
10657 objfile_name (objfile));
32019081
JK
10658 continue;
10659 }
10660
10661 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10662 if (import_attr == NULL)
10663 {
b98664d3 10664 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10665 dwarf_tag_name (child_die->tag));
10666 continue;
10667 }
10668
10669 imported_cu = cu;
10670 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10671 &imported_cu);
10672 imported_name = dwarf2_name (imported_die, imported_cu);
10673 if (imported_name == NULL)
10674 {
b98664d3 10675 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10676 "imported name - DIE at %s [in module %s]"),
10677 sect_offset_str (child_die->sect_off),
10678 objfile_name (objfile));
32019081
JK
10679 continue;
10680 }
10681
eb1e02fd 10682 excludes.push_back (imported_name);
32019081
JK
10683
10684 process_die (child_die, cu);
10685 }
10686
804d2729 10687 add_using_directive (using_directives (cu),
22cee43f
PMR
10688 import_prefix,
10689 canonical_name,
10690 import_alias,
10691 imported_declaration,
10692 excludes,
10693 0,
10694 &objfile->objfile_obstack);
27aa8d6a
SW
10695}
10696
5230b05a
WT
10697/* ICC<14 does not output the required DW_AT_declaration on incomplete
10698 types, but gives them a size of zero. Starting with version 14,
10699 ICC is compatible with GCC. */
10700
9068261f 10701static bool
5230b05a
WT
10702producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10703{
10704 if (!cu->checked_producer)
10705 check_producer (cu);
10706
10707 return cu->producer_is_icc_lt_14;
10708}
10709
eb77c9df
AB
10710/* ICC generates a DW_AT_type for C void functions. This was observed on
10711 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10712 which says that void functions should not have a DW_AT_type. */
10713
10714static bool
10715producer_is_icc (struct dwarf2_cu *cu)
10716{
10717 if (!cu->checked_producer)
10718 check_producer (cu);
10719
10720 return cu->producer_is_icc;
10721}
10722
1b80a9fa
JK
10723/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10724 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10725 this, it was first present in GCC release 4.3.0. */
10726
9068261f 10727static bool
1b80a9fa
JK
10728producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10729{
10730 if (!cu->checked_producer)
10731 check_producer (cu);
10732
10733 return cu->producer_is_gcc_lt_4_3;
10734}
10735
d721ba37
PA
10736static file_and_directory
10737find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10738{
d721ba37
PA
10739 file_and_directory res;
10740
9291a0cd
TT
10741 /* Find the filename. Do not use dwarf2_name here, since the filename
10742 is not a source language identifier. */
d721ba37
PA
10743 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10744 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10745
d721ba37
PA
10746 if (res.comp_dir == NULL
10747 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10748 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10749 {
d721ba37
PA
10750 res.comp_dir_storage = ldirname (res.name);
10751 if (!res.comp_dir_storage.empty ())
10752 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10753 }
d721ba37 10754 if (res.comp_dir != NULL)
9291a0cd
TT
10755 {
10756 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10757 directory, get rid of it. */
d721ba37 10758 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10759
d721ba37
PA
10760 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10761 res.comp_dir = cp + 1;
9291a0cd
TT
10762 }
10763
d721ba37
PA
10764 if (res.name == NULL)
10765 res.name = "<unknown>";
10766
10767 return res;
9291a0cd
TT
10768}
10769
f4dc4d17
DE
10770/* Handle DW_AT_stmt_list for a compilation unit.
10771 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10772 COMP_DIR is the compilation directory. LOWPC is passed to
10773 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10774
10775static void
10776handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10777 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10778{
518817b3
SM
10779 struct dwarf2_per_objfile *dwarf2_per_objfile
10780 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10781 struct attribute *attr;
527f3840
JK
10782 struct line_header line_header_local;
10783 hashval_t line_header_local_hash;
527f3840
JK
10784 void **slot;
10785 int decode_mapping;
2ab95328 10786
f4dc4d17
DE
10787 gdb_assert (! cu->per_cu->is_debug_types);
10788
2ab95328 10789 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10790 if (attr == NULL)
10791 return;
10792
9c541725 10793 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10794
10795 /* The line header hash table is only created if needed (it exists to
10796 prevent redundant reading of the line table for partial_units).
10797 If we're given a partial_unit, we'll need it. If we're given a
10798 compile_unit, then use the line header hash table if it's already
10799 created, but don't create one just yet. */
10800
10801 if (dwarf2_per_objfile->line_header_hash == NULL
10802 && die->tag == DW_TAG_partial_unit)
2ab95328 10803 {
527f3840 10804 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10805 .reset (htab_create_alloc (127, line_header_hash_voidp,
10806 line_header_eq_voidp,
10807 free_line_header_voidp,
10808 xcalloc, xfree));
527f3840 10809 }
2ab95328 10810
9c541725 10811 line_header_local.sect_off = line_offset;
527f3840
JK
10812 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10813 line_header_local_hash = line_header_hash (&line_header_local);
10814 if (dwarf2_per_objfile->line_header_hash != NULL)
10815 {
d15acc42 10816 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10817 &line_header_local,
10818 line_header_local_hash, NO_INSERT);
10819
10820 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10821 is not present in *SLOT (since if there is something in *SLOT then
10822 it will be for a partial_unit). */
10823 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10824 {
527f3840 10825 gdb_assert (*slot != NULL);
9a3c8263 10826 cu->line_header = (struct line_header *) *slot;
527f3840 10827 return;
dee91e82 10828 }
2ab95328 10829 }
527f3840
JK
10830
10831 /* dwarf_decode_line_header does not yet provide sufficient information.
10832 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10833 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10834 if (lh == NULL)
527f3840 10835 return;
4c8aa72d
PA
10836
10837 cu->line_header = lh.release ();
10838 cu->line_header_die_owner = die;
527f3840
JK
10839
10840 if (dwarf2_per_objfile->line_header_hash == NULL)
10841 slot = NULL;
10842 else
10843 {
d15acc42 10844 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10845 &line_header_local,
10846 line_header_local_hash, INSERT);
10847 gdb_assert (slot != NULL);
10848 }
10849 if (slot != NULL && *slot == NULL)
10850 {
10851 /* This newly decoded line number information unit will be owned
10852 by line_header_hash hash table. */
10853 *slot = cu->line_header;
4c8aa72d 10854 cu->line_header_die_owner = NULL;
527f3840
JK
10855 }
10856 else
10857 {
10858 /* We cannot free any current entry in (*slot) as that struct line_header
10859 may be already used by multiple CUs. Create only temporary decoded
10860 line_header for this CU - it may happen at most once for each line
10861 number information unit. And if we're not using line_header_hash
10862 then this is what we want as well. */
10863 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10864 }
10865 decode_mapping = (die->tag != DW_TAG_partial_unit);
10866 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10867 decode_mapping);
fff8551c 10868
2ab95328
TT
10869}
10870
95554aad 10871/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10872
c906108c 10873static void
e7c27a73 10874read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10875{
518817b3
SM
10876 struct dwarf2_per_objfile *dwarf2_per_objfile
10877 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10878 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10879 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10880 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10881 CORE_ADDR highpc = ((CORE_ADDR) 0);
10882 struct attribute *attr;
c906108c 10883 struct die_info *child_die;
e142c38c 10884 CORE_ADDR baseaddr;
6e70227d 10885
380618d6 10886 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10887 baseaddr = objfile->text_section_offset ();
c906108c 10888
fae299cd 10889 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10890
10891 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10892 from finish_block. */
2acceee2 10893 if (lowpc == ((CORE_ADDR) -1))
c906108c 10894 lowpc = highpc;
3e29f34a 10895 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10896
d721ba37 10897 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10898
f4b8a18d
KW
10899 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10900 standardised yet. As a workaround for the language detection we fall
10901 back to the DW_AT_producer string. */
10902 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10903 cu->language = language_opencl;
10904
3019eac3
DE
10905 /* Similar hack for Go. */
10906 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10907 set_cu_language (DW_LANG_Go, cu);
10908
c24bdb02 10909 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10910
10911 /* Decode line number information if present. We do this before
10912 processing child DIEs, so that the line header table is available
10913 for DW_AT_decl_file. */
d721ba37 10914 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10915
10916 /* Process all dies in compilation unit. */
10917 if (die->child != NULL)
10918 {
10919 child_die = die->child;
10920 while (child_die && child_die->tag)
10921 {
10922 process_die (child_die, cu);
436c571c 10923 child_die = child_die->sibling;
3019eac3
DE
10924 }
10925 }
10926
10927 /* Decode macro information, if present. Dwarf 2 macro information
10928 refers to information in the line number info statement program
10929 header, so we can only read it if we've read the header
10930 successfully. */
0af92d60
JK
10931 attr = dwarf2_attr (die, DW_AT_macros, cu);
10932 if (attr == NULL)
10933 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10934 if (attr && cu->line_header)
10935 {
10936 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10937 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10938
43f3e411 10939 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10940 }
10941 else
10942 {
10943 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10944 if (attr && cu->line_header)
10945 {
10946 unsigned int macro_offset = DW_UNSND (attr);
10947
43f3e411 10948 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10949 }
10950 }
3019eac3
DE
10951}
10952
c24bdb02
KS
10953void
10954dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10955{
f4dc4d17
DE
10956 struct type_unit_group *tu_group;
10957 int first_time;
3019eac3 10958 struct attribute *attr;
9c541725 10959 unsigned int i;
0186c6a7 10960 struct signatured_type *sig_type;
3019eac3 10961
f4dc4d17 10962 gdb_assert (per_cu->is_debug_types);
0186c6a7 10963 sig_type = (struct signatured_type *) per_cu;
3019eac3 10964
c24bdb02 10965 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10966
f4dc4d17 10967 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10968 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10969 if (sig_type->type_unit_group == NULL)
c24bdb02 10970 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10971 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10972
10973 /* If we've already processed this stmt_list there's no real need to
10974 do it again, we could fake it and just recreate the part we need
10975 (file name,index -> symtab mapping). If data shows this optimization
10976 is useful we can do it then. */
43f3e411 10977 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10978
10979 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10980 debug info. */
fff8551c 10981 line_header_up lh;
f4dc4d17 10982 if (attr != NULL)
3019eac3 10983 {
9c541725 10984 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10985 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10986 }
10987 if (lh == NULL)
10988 {
10989 if (first_time)
c24bdb02 10990 start_symtab ("", NULL, 0);
f4dc4d17
DE
10991 else
10992 {
10993 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10994 gdb_assert (m_builder == nullptr);
804d2729 10995 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10996 m_builder.reset (new struct buildsym_compunit
10997 (COMPUNIT_OBJFILE (cust), "",
10998 COMPUNIT_DIRNAME (cust),
10999 compunit_language (cust),
11000 0, cust));
770479f2 11001 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11002 }
f4dc4d17 11003 return;
3019eac3
DE
11004 }
11005
c24bdb02
KS
11006 line_header = lh.release ();
11007 line_header_die_owner = die;
3019eac3 11008
f4dc4d17
DE
11009 if (first_time)
11010 {
c24bdb02 11011 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11012
1fd60fc0
DE
11013 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11014 still initializing it, and our caller (a few levels up)
11015 process_full_type_unit still needs to know if this is the first
11016 time. */
11017
4ac93832
TT
11018 tu_group->symtabs
11019 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11020 struct symtab *, line_header->file_names_size ());
3019eac3 11021
7ba99d21
AT
11022 auto &file_names = line_header->file_names ();
11023 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11024 {
7ba99d21 11025 file_entry &fe = file_names[i];
c24bdb02
KS
11026 dwarf2_start_subfile (this, fe.name,
11027 fe.include_dir (line_header));
11028 buildsym_compunit *b = get_builder ();
11029 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11030 {
4c8aa72d
PA
11031 /* NOTE: start_subfile will recognize when it's been
11032 passed a file it has already seen. So we can't
11033 assume there's a simple mapping from
11034 cu->line_header->file_names to subfiles, plus
11035 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11036 b->get_current_subfile ()->symtab
11037 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11038 }
11039
c24bdb02 11040 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11041 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11042 }
11043 }
11044 else
3019eac3 11045 {
c24bdb02 11046 gdb_assert (m_builder == nullptr);
804d2729 11047 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11048 m_builder.reset (new struct buildsym_compunit
11049 (COMPUNIT_OBJFILE (cust), "",
11050 COMPUNIT_DIRNAME (cust),
11051 compunit_language (cust),
11052 0, cust));
770479f2 11053 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11054
7ba99d21
AT
11055 auto &file_names = line_header->file_names ();
11056 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11057 {
7ba99d21 11058 file_entry &fe = file_names[i];
4c8aa72d 11059 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11060 }
3019eac3
DE
11061 }
11062
f4dc4d17
DE
11063 /* The main symtab is allocated last. Type units don't have DW_AT_name
11064 so they don't have a "real" (so to speak) symtab anyway.
11065 There is later code that will assign the main symtab to all symbols
11066 that don't have one. We need to handle the case of a symbol with a
11067 missing symtab (DW_AT_decl_file) anyway. */
11068}
3019eac3 11069
f4dc4d17
DE
11070/* Process DW_TAG_type_unit.
11071 For TUs we want to skip the first top level sibling if it's not the
11072 actual type being defined by this TU. In this case the first top
11073 level sibling is there to provide context only. */
3019eac3 11074
f4dc4d17
DE
11075static void
11076read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11077{
11078 struct die_info *child_die;
3019eac3 11079
f4dc4d17
DE
11080 prepare_one_comp_unit (cu, die, language_minimal);
11081
11082 /* Initialize (or reinitialize) the machinery for building symtabs.
11083 We do this before processing child DIEs, so that the line header table
11084 is available for DW_AT_decl_file. */
c24bdb02 11085 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11086
11087 if (die->child != NULL)
11088 {
11089 child_die = die->child;
11090 while (child_die && child_die->tag)
11091 {
11092 process_die (child_die, cu);
436c571c 11093 child_die = child_die->sibling;
f4dc4d17
DE
11094 }
11095 }
3019eac3
DE
11096}
11097\f
80626a55
DE
11098/* DWO/DWP files.
11099
11100 http://gcc.gnu.org/wiki/DebugFission
11101 http://gcc.gnu.org/wiki/DebugFissionDWP
11102
11103 To simplify handling of both DWO files ("object" files with the DWARF info)
11104 and DWP files (a file with the DWOs packaged up into one file), we treat
11105 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11106
11107static hashval_t
11108hash_dwo_file (const void *item)
11109{
9a3c8263 11110 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11111 hashval_t hash;
3019eac3 11112
a2ce51a0
DE
11113 hash = htab_hash_string (dwo_file->dwo_name);
11114 if (dwo_file->comp_dir != NULL)
11115 hash += htab_hash_string (dwo_file->comp_dir);
11116 return hash;
3019eac3
DE
11117}
11118
11119static int
11120eq_dwo_file (const void *item_lhs, const void *item_rhs)
11121{
9a3c8263
SM
11122 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11123 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11124
a2ce51a0
DE
11125 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11126 return 0;
11127 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11128 return lhs->comp_dir == rhs->comp_dir;
11129 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11130}
11131
11132/* Allocate a hash table for DWO files. */
11133
51ac9db5 11134static htab_up
298e9637 11135allocate_dwo_file_hash_table ()
3019eac3 11136{
51ac9db5
SM
11137 auto delete_dwo_file = [] (void *item)
11138 {
11139 struct dwo_file *dwo_file = (struct dwo_file *) item;
11140
11141 delete dwo_file;
11142 };
11143
bc68fb19
TT
11144 return htab_up (htab_create_alloc (41,
11145 hash_dwo_file,
11146 eq_dwo_file,
11147 delete_dwo_file,
11148 xcalloc, xfree));
3019eac3
DE
11149}
11150
80626a55
DE
11151/* Lookup DWO file DWO_NAME. */
11152
11153static void **
ed2dc618
SM
11154lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11155 const char *dwo_name,
11156 const char *comp_dir)
80626a55
DE
11157{
11158 struct dwo_file find_entry;
11159 void **slot;
11160
11161 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11162 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11163
0ac5b59e
DE
11164 find_entry.dwo_name = dwo_name;
11165 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11166 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11167 INSERT);
80626a55
DE
11168
11169 return slot;
11170}
11171
3019eac3
DE
11172static hashval_t
11173hash_dwo_unit (const void *item)
11174{
9a3c8263 11175 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11176
11177 /* This drops the top 32 bits of the id, but is ok for a hash. */
11178 return dwo_unit->signature;
11179}
11180
11181static int
11182eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11183{
9a3c8263
SM
11184 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11185 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11186
11187 /* The signature is assumed to be unique within the DWO file.
11188 So while object file CU dwo_id's always have the value zero,
11189 that's OK, assuming each object file DWO file has only one CU,
11190 and that's the rule for now. */
11191 return lhs->signature == rhs->signature;
11192}
11193
11194/* Allocate a hash table for DWO CUs,TUs.
11195 There is one of these tables for each of CUs,TUs for each DWO file. */
11196
b0b6a987 11197static htab_up
298e9637 11198allocate_dwo_unit_table ()
3019eac3
DE
11199{
11200 /* Start out with a pretty small number.
11201 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11202 return htab_up (htab_create_alloc (3,
11203 hash_dwo_unit,
11204 eq_dwo_unit,
11205 NULL, xcalloc, xfree));
3019eac3
DE
11206}
11207
19c3d4c9 11208/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11209
11210static void
19c3d4c9
DE
11211create_dwo_cu_reader (const struct die_reader_specs *reader,
11212 const gdb_byte *info_ptr,
11213 struct die_info *comp_unit_die,
c0ab21c2
TT
11214 struct dwo_file *dwo_file,
11215 struct dwo_unit *dwo_unit)
3019eac3
DE
11216{
11217 struct dwarf2_cu *cu = reader->cu;
9c541725 11218 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11219 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11220
a084a2a6
AT
11221 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11222 if (!signature.has_value ())
3019eac3 11223 {
b98664d3 11224 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11225 " its dwo_id [in module %s]"),
9d8780f0 11226 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11227 return;
11228 }
11229
3019eac3 11230 dwo_unit->dwo_file = dwo_file;
a084a2a6 11231 dwo_unit->signature = *signature;
8a0459fd 11232 dwo_unit->section = section;
9c541725 11233 dwo_unit->sect_off = sect_off;
3019eac3
DE
11234 dwo_unit->length = cu->per_cu->length;
11235
b4f54984 11236 if (dwarf_read_debug)
9d8780f0
SM
11237 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11238 sect_offset_str (sect_off),
9c541725 11239 hex_string (dwo_unit->signature));
3019eac3
DE
11240}
11241
33c5cd75 11242/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11243 Note: This function processes DWO files only, not DWP files. */
3019eac3 11244
33c5cd75 11245static void
ed2dc618 11246create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11247 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11248 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11249{
11250 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11251 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11252
96b79293 11253 section.read (objfile);
33c5cd75 11254 info_ptr = section.buffer;
3019eac3
DE
11255
11256 if (info_ptr == NULL)
33c5cd75 11257 return;
3019eac3 11258
b4f54984 11259 if (dwarf_read_debug)
19c3d4c9
DE
11260 {
11261 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11262 section.get_name (),
11263 section.get_file_name ());
19c3d4c9 11264 }
3019eac3 11265
33c5cd75 11266 end_ptr = info_ptr + section.size;
3019eac3
DE
11267 while (info_ptr < end_ptr)
11268 {
11269 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11270 struct dwo_unit read_unit {};
33c5cd75
DB
11271 struct dwo_unit *dwo_unit;
11272 void **slot;
11273 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11274
11275 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11276 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11277 per_cu.is_debug_types = 0;
33c5cd75
DB
11278 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11279 per_cu.section = &section;
11280
c0ab21c2
TT
11281 cutu_reader reader (&per_cu, cu, &dwo_file);
11282 if (!reader.dummy_p)
11283 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11284 &dwo_file, &read_unit);
33c5cd75
DB
11285 info_ptr += per_cu.length;
11286
11287 // If the unit could not be parsed, skip it.
c0ab21c2 11288 if (read_unit.dwo_file == NULL)
33c5cd75 11289 continue;
3019eac3 11290
33c5cd75 11291 if (cus_htab == NULL)
298e9637 11292 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11293
33c5cd75 11294 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11295 *dwo_unit = read_unit;
b0b6a987 11296 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11297 gdb_assert (slot != NULL);
11298 if (*slot != NULL)
19c3d4c9 11299 {
33c5cd75
DB
11300 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11301 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11302
b98664d3 11303 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11304 " the entry at offset %s, signature %s"),
11305 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11306 hex_string (dwo_unit->signature));
19c3d4c9 11307 }
33c5cd75 11308 *slot = (void *)dwo_unit;
3019eac3 11309 }
3019eac3
DE
11310}
11311
80626a55
DE
11312/* DWP file .debug_{cu,tu}_index section format:
11313 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11314
d2415c6c
DE
11315 DWP Version 1:
11316
80626a55
DE
11317 Both index sections have the same format, and serve to map a 64-bit
11318 signature to a set of section numbers. Each section begins with a header,
11319 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11320 indexes, and a pool of 32-bit section numbers. The index sections will be
11321 aligned at 8-byte boundaries in the file.
11322
d2415c6c
DE
11323 The index section header consists of:
11324
11325 V, 32 bit version number
11326 -, 32 bits unused
11327 N, 32 bit number of compilation units or type units in the index
11328 M, 32 bit number of slots in the hash table
80626a55 11329
d2415c6c 11330 Numbers are recorded using the byte order of the application binary.
80626a55 11331
d2415c6c
DE
11332 The hash table begins at offset 16 in the section, and consists of an array
11333 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11334 order of the application binary). Unused slots in the hash table are 0.
11335 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11336
d2415c6c
DE
11337 The parallel table begins immediately after the hash table
11338 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11339 array of 32-bit indexes (using the byte order of the application binary),
11340 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11341 table contains a 32-bit index into the pool of section numbers. For unused
11342 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11343
73869dc2
DE
11344 The pool of section numbers begins immediately following the hash table
11345 (at offset 16 + 12 * M from the beginning of the section). The pool of
11346 section numbers consists of an array of 32-bit words (using the byte order
11347 of the application binary). Each item in the array is indexed starting
11348 from 0. The hash table entry provides the index of the first section
11349 number in the set. Additional section numbers in the set follow, and the
11350 set is terminated by a 0 entry (section number 0 is not used in ELF).
11351
11352 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11353 section must be the first entry in the set, and the .debug_abbrev.dwo must
11354 be the second entry. Other members of the set may follow in any order.
11355
11356 ---
11357
11358 DWP Version 2:
11359
11360 DWP Version 2 combines all the .debug_info, etc. sections into one,
11361 and the entries in the index tables are now offsets into these sections.
11362 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11363 section.
11364
11365 Index Section Contents:
11366 Header
11367 Hash Table of Signatures dwp_hash_table.hash_table
11368 Parallel Table of Indices dwp_hash_table.unit_table
11369 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11370 Table of Section Sizes dwp_hash_table.v2.sizes
11371
11372 The index section header consists of:
11373
11374 V, 32 bit version number
11375 L, 32 bit number of columns in the table of section offsets
11376 N, 32 bit number of compilation units or type units in the index
11377 M, 32 bit number of slots in the hash table
11378
11379 Numbers are recorded using the byte order of the application binary.
11380
11381 The hash table has the same format as version 1.
11382 The parallel table of indices has the same format as version 1,
11383 except that the entries are origin-1 indices into the table of sections
11384 offsets and the table of section sizes.
11385
11386 The table of offsets begins immediately following the parallel table
11387 (at offset 16 + 12 * M from the beginning of the section). The table is
11388 a two-dimensional array of 32-bit words (using the byte order of the
11389 application binary), with L columns and N+1 rows, in row-major order.
11390 Each row in the array is indexed starting from 0. The first row provides
11391 a key to the remaining rows: each column in this row provides an identifier
11392 for a debug section, and the offsets in the same column of subsequent rows
11393 refer to that section. The section identifiers are:
11394
11395 DW_SECT_INFO 1 .debug_info.dwo
11396 DW_SECT_TYPES 2 .debug_types.dwo
11397 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11398 DW_SECT_LINE 4 .debug_line.dwo
11399 DW_SECT_LOC 5 .debug_loc.dwo
11400 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11401 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11402 DW_SECT_MACRO 8 .debug_macro.dwo
11403
11404 The offsets provided by the CU and TU index sections are the base offsets
11405 for the contributions made by each CU or TU to the corresponding section
11406 in the package file. Each CU and TU header contains an abbrev_offset
11407 field, used to find the abbreviations table for that CU or TU within the
11408 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11409 be interpreted as relative to the base offset given in the index section.
11410 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11411 should be interpreted as relative to the base offset for .debug_line.dwo,
11412 and offsets into other debug sections obtained from DWARF attributes should
11413 also be interpreted as relative to the corresponding base offset.
11414
11415 The table of sizes begins immediately following the table of offsets.
11416 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11417 with L columns and N rows, in row-major order. Each row in the array is
11418 indexed starting from 1 (row 0 is shared by the two tables).
11419
11420 ---
11421
11422 Hash table lookup is handled the same in version 1 and 2:
11423
11424 We assume that N and M will not exceed 2^32 - 1.
11425 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11426
d2415c6c
DE
11427 Given a 64-bit compilation unit signature or a type signature S, an entry
11428 in the hash table is located as follows:
80626a55 11429
d2415c6c
DE
11430 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11431 the low-order k bits all set to 1.
80626a55 11432
d2415c6c 11433 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11434
d2415c6c
DE
11435 3) If the hash table entry at index H matches the signature, use that
11436 entry. If the hash table entry at index H is unused (all zeroes),
11437 terminate the search: the signature is not present in the table.
80626a55 11438
d2415c6c 11439 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11440
d2415c6c 11441 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11442 to stop at an unused slot or find the match. */
80626a55
DE
11443
11444/* Create a hash table to map DWO IDs to their CU/TU entry in
11445 .debug_{info,types}.dwo in DWP_FILE.
11446 Returns NULL if there isn't one.
11447 Note: This function processes DWP files only, not DWO files. */
11448
11449static struct dwp_hash_table *
ed2dc618
SM
11450create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11451 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11452{
11453 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11454 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11455 const gdb_byte *index_ptr, *index_end;
80626a55 11456 struct dwarf2_section_info *index;
73869dc2 11457 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11458 struct dwp_hash_table *htab;
11459
11460 if (is_debug_types)
11461 index = &dwp_file->sections.tu_index;
11462 else
11463 index = &dwp_file->sections.cu_index;
11464
96b79293 11465 if (index->empty ())
80626a55 11466 return NULL;
96b79293 11467 index->read (objfile);
80626a55
DE
11468
11469 index_ptr = index->buffer;
11470 index_end = index_ptr + index->size;
11471
11472 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11473 index_ptr += 4;
11474 if (version == 2)
11475 nr_columns = read_4_bytes (dbfd, index_ptr);
11476 else
11477 nr_columns = 0;
11478 index_ptr += 4;
80626a55
DE
11479 nr_units = read_4_bytes (dbfd, index_ptr);
11480 index_ptr += 4;
11481 nr_slots = read_4_bytes (dbfd, index_ptr);
11482 index_ptr += 4;
11483
73869dc2 11484 if (version != 1 && version != 2)
80626a55 11485 {
21aa081e 11486 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11487 " [in module %s]"),
21aa081e 11488 pulongest (version), dwp_file->name);
80626a55
DE
11489 }
11490 if (nr_slots != (nr_slots & -nr_slots))
11491 {
21aa081e 11492 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11493 " is not power of 2 [in module %s]"),
21aa081e 11494 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11495 }
11496
11497 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11498 htab->version = version;
11499 htab->nr_columns = nr_columns;
80626a55
DE
11500 htab->nr_units = nr_units;
11501 htab->nr_slots = nr_slots;
11502 htab->hash_table = index_ptr;
11503 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11504
11505 /* Exit early if the table is empty. */
11506 if (nr_slots == 0 || nr_units == 0
11507 || (version == 2 && nr_columns == 0))
11508 {
11509 /* All must be zero. */
11510 if (nr_slots != 0 || nr_units != 0
11511 || (version == 2 && nr_columns != 0))
11512 {
b98664d3 11513 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11514 " all zero [in modules %s]"),
11515 dwp_file->name);
11516 }
11517 return htab;
11518 }
11519
11520 if (version == 1)
11521 {
11522 htab->section_pool.v1.indices =
11523 htab->unit_table + sizeof (uint32_t) * nr_slots;
11524 /* It's harder to decide whether the section is too small in v1.
11525 V1 is deprecated anyway so we punt. */
11526 }
11527 else
11528 {
11529 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11530 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11531 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11532 /* Reverse map for error checking. */
11533 int ids_seen[DW_SECT_MAX + 1];
11534 int i;
11535
11536 if (nr_columns < 2)
11537 {
11538 error (_("Dwarf Error: bad DWP hash table, too few columns"
11539 " in section table [in module %s]"),
11540 dwp_file->name);
11541 }
11542 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, too many columns"
11545 " in section table [in module %s]"),
11546 dwp_file->name);
11547 }
04fd5eed
GB
11548 memset (ids, 255, sizeof_ids);
11549 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11550 for (i = 0; i < nr_columns; ++i)
11551 {
11552 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11553
11554 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11555 {
11556 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11557 " in section table [in module %s]"),
11558 id, dwp_file->name);
11559 }
11560 if (ids_seen[id] != -1)
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11563 " id %d in section table [in module %s]"),
11564 id, dwp_file->name);
11565 }
11566 ids_seen[id] = i;
11567 ids[i] = id;
11568 }
11569 /* Must have exactly one info or types section. */
11570 if (((ids_seen[DW_SECT_INFO] != -1)
11571 + (ids_seen[DW_SECT_TYPES] != -1))
11572 != 1)
11573 {
11574 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11575 " DWO info/types section [in module %s]"),
11576 dwp_file->name);
11577 }
11578 /* Must have an abbrev section. */
11579 if (ids_seen[DW_SECT_ABBREV] == -1)
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11582 " section [in module %s]"),
11583 dwp_file->name);
11584 }
11585 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11586 htab->section_pool.v2.sizes =
11587 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11588 * nr_units * nr_columns);
11589 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11590 * nr_units * nr_columns))
11591 > index_end)
11592 {
11593 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11594 " [in module %s]"),
11595 dwp_file->name);
11596 }
11597 }
80626a55
DE
11598
11599 return htab;
11600}
11601
11602/* Update SECTIONS with the data from SECTP.
11603
11604 This function is like the other "locate" section routines that are
11605 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11606 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11607
11608 The result is non-zero for success, or zero if an error was found. */
11609
11610static int
73869dc2
DE
11611locate_v1_virtual_dwo_sections (asection *sectp,
11612 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11613{
11614 const struct dwop_section_names *names = &dwop_section_names;
11615
11616 if (section_is_p (sectp->name, &names->abbrev_dwo))
11617 {
11618 /* There can be only one. */
049412e3 11619 if (sections->abbrev.s.section != NULL)
80626a55 11620 return 0;
049412e3 11621 sections->abbrev.s.section = sectp;
fd361982 11622 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11623 }
11624 else if (section_is_p (sectp->name, &names->info_dwo)
11625 || section_is_p (sectp->name, &names->types_dwo))
11626 {
11627 /* There can be only one. */
049412e3 11628 if (sections->info_or_types.s.section != NULL)
80626a55 11629 return 0;
049412e3 11630 sections->info_or_types.s.section = sectp;
fd361982 11631 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11632 }
11633 else if (section_is_p (sectp->name, &names->line_dwo))
11634 {
11635 /* There can be only one. */
049412e3 11636 if (sections->line.s.section != NULL)
80626a55 11637 return 0;
049412e3 11638 sections->line.s.section = sectp;
fd361982 11639 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11640 }
11641 else if (section_is_p (sectp->name, &names->loc_dwo))
11642 {
11643 /* There can be only one. */
049412e3 11644 if (sections->loc.s.section != NULL)
80626a55 11645 return 0;
049412e3 11646 sections->loc.s.section = sectp;
fd361982 11647 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11648 }
11649 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11650 {
11651 /* There can be only one. */
049412e3 11652 if (sections->macinfo.s.section != NULL)
80626a55 11653 return 0;
049412e3 11654 sections->macinfo.s.section = sectp;
fd361982 11655 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11656 }
11657 else if (section_is_p (sectp->name, &names->macro_dwo))
11658 {
11659 /* There can be only one. */
049412e3 11660 if (sections->macro.s.section != NULL)
80626a55 11661 return 0;
049412e3 11662 sections->macro.s.section = sectp;
fd361982 11663 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11664 }
11665 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11666 {
11667 /* There can be only one. */
049412e3 11668 if (sections->str_offsets.s.section != NULL)
80626a55 11669 return 0;
049412e3 11670 sections->str_offsets.s.section = sectp;
fd361982 11671 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11672 }
11673 else
11674 {
11675 /* No other kind of section is valid. */
11676 return 0;
11677 }
11678
11679 return 1;
11680}
11681
73869dc2
DE
11682/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11683 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11684 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11685 This is for DWP version 1 files. */
80626a55
DE
11686
11687static struct dwo_unit *
ed2dc618
SM
11688create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11689 struct dwp_file *dwp_file,
73869dc2
DE
11690 uint32_t unit_index,
11691 const char *comp_dir,
11692 ULONGEST signature, int is_debug_types)
80626a55
DE
11693{
11694 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11695 const struct dwp_hash_table *dwp_htab =
11696 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11697 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11698 const char *kind = is_debug_types ? "TU" : "CU";
11699 struct dwo_file *dwo_file;
11700 struct dwo_unit *dwo_unit;
73869dc2 11701 struct virtual_v1_dwo_sections sections;
80626a55 11702 void **dwo_file_slot;
80626a55
DE
11703 int i;
11704
73869dc2
DE
11705 gdb_assert (dwp_file->version == 1);
11706
b4f54984 11707 if (dwarf_read_debug)
80626a55 11708 {
73869dc2 11709 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11710 kind,
73869dc2 11711 pulongest (unit_index), hex_string (signature),
80626a55
DE
11712 dwp_file->name);
11713 }
11714
19ac8c2e 11715 /* Fetch the sections of this DWO unit.
80626a55
DE
11716 Put a limit on the number of sections we look for so that bad data
11717 doesn't cause us to loop forever. */
11718
73869dc2 11719#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11720 (1 /* .debug_info or .debug_types */ \
11721 + 1 /* .debug_abbrev */ \
11722 + 1 /* .debug_line */ \
11723 + 1 /* .debug_loc */ \
11724 + 1 /* .debug_str_offsets */ \
19ac8c2e 11725 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11726 + 1 /* trailing zero */)
11727
11728 memset (&sections, 0, sizeof (sections));
80626a55 11729
73869dc2 11730 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11731 {
11732 asection *sectp;
11733 uint32_t section_nr =
11734 read_4_bytes (dbfd,
73869dc2
DE
11735 dwp_htab->section_pool.v1.indices
11736 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11737
11738 if (section_nr == 0)
11739 break;
11740 if (section_nr >= dwp_file->num_sections)
11741 {
11742 error (_("Dwarf Error: bad DWP hash table, section number too large"
11743 " [in module %s]"),
11744 dwp_file->name);
11745 }
11746
11747 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11748 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11749 {
11750 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11751 " [in module %s]"),
11752 dwp_file->name);
11753 }
11754 }
11755
11756 if (i < 2
96b79293
TT
11757 || sections.info_or_types.empty ()
11758 || sections.abbrev.empty ())
80626a55
DE
11759 {
11760 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11761 " [in module %s]"),
11762 dwp_file->name);
11763 }
73869dc2 11764 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11765 {
11766 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11767 " [in module %s]"),
11768 dwp_file->name);
11769 }
11770
11771 /* It's easier for the rest of the code if we fake a struct dwo_file and
11772 have dwo_unit "live" in that. At least for now.
11773
11774 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11775 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11776 file, we can combine them back into a virtual DWO file to save space
11777 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11778 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11779
791afaa2
TT
11780 std::string virtual_dwo_name =
11781 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11782 sections.abbrev.get_id (),
11783 sections.line.get_id (),
11784 sections.loc.get_id (),
11785 sections.str_offsets.get_id ());
80626a55 11786 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11787 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11788 virtual_dwo_name.c_str (),
11789 comp_dir);
80626a55
DE
11790 /* Create one if necessary. */
11791 if (*dwo_file_slot == NULL)
11792 {
b4f54984 11793 if (dwarf_read_debug)
80626a55
DE
11794 {
11795 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11796 virtual_dwo_name.c_str ());
80626a55 11797 }
51ac9db5 11798 dwo_file = new struct dwo_file;
be1e3d3e 11799 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11800 dwo_file->comp_dir = comp_dir;
80626a55
DE
11801 dwo_file->sections.abbrev = sections.abbrev;
11802 dwo_file->sections.line = sections.line;
11803 dwo_file->sections.loc = sections.loc;
11804 dwo_file->sections.macinfo = sections.macinfo;
11805 dwo_file->sections.macro = sections.macro;
11806 dwo_file->sections.str_offsets = sections.str_offsets;
11807 /* The "str" section is global to the entire DWP file. */
11808 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11809 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11810 there's no need to record it in dwo_file.
11811 Also, we can't simply record type sections in dwo_file because
11812 we record a pointer into the vector in dwo_unit. As we collect more
11813 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11814 for it, invalidating all copies of pointers into the previous
11815 contents. */
80626a55
DE
11816 *dwo_file_slot = dwo_file;
11817 }
11818 else
11819 {
b4f54984 11820 if (dwarf_read_debug)
80626a55
DE
11821 {
11822 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11823 virtual_dwo_name.c_str ());
80626a55 11824 }
9a3c8263 11825 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11826 }
80626a55
DE
11827
11828 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11829 dwo_unit->dwo_file = dwo_file;
11830 dwo_unit->signature = signature;
8d749320
SM
11831 dwo_unit->section =
11832 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11833 *dwo_unit->section = sections.info_or_types;
57d63ce2 11834 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11835
11836 return dwo_unit;
11837}
11838
73869dc2
DE
11839/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11840 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11841 piece within that section used by a TU/CU, return a virtual section
11842 of just that piece. */
11843
11844static struct dwarf2_section_info
ed2dc618
SM
11845create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11846 struct dwarf2_section_info *section,
73869dc2
DE
11847 bfd_size_type offset, bfd_size_type size)
11848{
11849 struct dwarf2_section_info result;
11850 asection *sectp;
11851
11852 gdb_assert (section != NULL);
11853 gdb_assert (!section->is_virtual);
11854
11855 memset (&result, 0, sizeof (result));
11856 result.s.containing_section = section;
dc4ccb6f 11857 result.is_virtual = true;
73869dc2
DE
11858
11859 if (size == 0)
11860 return result;
11861
96b79293 11862 sectp = section->get_bfd_section ();
73869dc2
DE
11863
11864 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11865 bounds of the real section. This is a pretty-rare event, so just
11866 flag an error (easier) instead of a warning and trying to cope. */
11867 if (sectp == NULL
fd361982 11868 || offset + size > bfd_section_size (sectp))
73869dc2 11869 {
73869dc2
DE
11870 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11871 " in section %s [in module %s]"),
fd361982 11872 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11873 objfile_name (dwarf2_per_objfile->objfile));
11874 }
11875
11876 result.virtual_offset = offset;
11877 result.size = size;
11878 return result;
11879}
11880
11881/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11882 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11883 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11884 This is for DWP version 2 files. */
11885
11886static struct dwo_unit *
ed2dc618
SM
11887create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11888 struct dwp_file *dwp_file,
73869dc2
DE
11889 uint32_t unit_index,
11890 const char *comp_dir,
11891 ULONGEST signature, int is_debug_types)
11892{
11893 struct objfile *objfile = dwarf2_per_objfile->objfile;
11894 const struct dwp_hash_table *dwp_htab =
11895 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11896 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11897 const char *kind = is_debug_types ? "TU" : "CU";
11898 struct dwo_file *dwo_file;
11899 struct dwo_unit *dwo_unit;
11900 struct virtual_v2_dwo_sections sections;
11901 void **dwo_file_slot;
73869dc2
DE
11902 int i;
11903
11904 gdb_assert (dwp_file->version == 2);
11905
b4f54984 11906 if (dwarf_read_debug)
73869dc2
DE
11907 {
11908 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11909 kind,
11910 pulongest (unit_index), hex_string (signature),
11911 dwp_file->name);
11912 }
11913
11914 /* Fetch the section offsets of this DWO unit. */
11915
11916 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11917
11918 for (i = 0; i < dwp_htab->nr_columns; ++i)
11919 {
11920 uint32_t offset = read_4_bytes (dbfd,
11921 dwp_htab->section_pool.v2.offsets
11922 + (((unit_index - 1) * dwp_htab->nr_columns
11923 + i)
11924 * sizeof (uint32_t)));
11925 uint32_t size = read_4_bytes (dbfd,
11926 dwp_htab->section_pool.v2.sizes
11927 + (((unit_index - 1) * dwp_htab->nr_columns
11928 + i)
11929 * sizeof (uint32_t)));
11930
11931 switch (dwp_htab->section_pool.v2.section_ids[i])
11932 {
11933 case DW_SECT_INFO:
11934 case DW_SECT_TYPES:
11935 sections.info_or_types_offset = offset;
11936 sections.info_or_types_size = size;
11937 break;
11938 case DW_SECT_ABBREV:
11939 sections.abbrev_offset = offset;
11940 sections.abbrev_size = size;
11941 break;
11942 case DW_SECT_LINE:
11943 sections.line_offset = offset;
11944 sections.line_size = size;
11945 break;
11946 case DW_SECT_LOC:
11947 sections.loc_offset = offset;
11948 sections.loc_size = size;
11949 break;
11950 case DW_SECT_STR_OFFSETS:
11951 sections.str_offsets_offset = offset;
11952 sections.str_offsets_size = size;
11953 break;
11954 case DW_SECT_MACINFO:
11955 sections.macinfo_offset = offset;
11956 sections.macinfo_size = size;
11957 break;
11958 case DW_SECT_MACRO:
11959 sections.macro_offset = offset;
11960 sections.macro_size = size;
11961 break;
11962 }
11963 }
11964
11965 /* It's easier for the rest of the code if we fake a struct dwo_file and
11966 have dwo_unit "live" in that. At least for now.
11967
11968 The DWP file can be made up of a random collection of CUs and TUs.
11969 However, for each CU + set of TUs that came from the same original DWO
11970 file, we can combine them back into a virtual DWO file to save space
11971 (fewer struct dwo_file objects to allocate). Remember that for really
11972 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11973
791afaa2
TT
11974 std::string virtual_dwo_name =
11975 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11976 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11977 (long) (sections.line_size ? sections.line_offset : 0),
11978 (long) (sections.loc_size ? sections.loc_offset : 0),
11979 (long) (sections.str_offsets_size
11980 ? sections.str_offsets_offset : 0));
73869dc2 11981 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11982 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11983 virtual_dwo_name.c_str (),
11984 comp_dir);
73869dc2
DE
11985 /* Create one if necessary. */
11986 if (*dwo_file_slot == NULL)
11987 {
b4f54984 11988 if (dwarf_read_debug)
73869dc2
DE
11989 {
11990 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11991 virtual_dwo_name.c_str ());
73869dc2 11992 }
51ac9db5 11993 dwo_file = new struct dwo_file;
be1e3d3e 11994 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11995 dwo_file->comp_dir = comp_dir;
11996 dwo_file->sections.abbrev =
ed2dc618 11997 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11998 sections.abbrev_offset, sections.abbrev_size);
11999 dwo_file->sections.line =
ed2dc618 12000 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12001 sections.line_offset, sections.line_size);
12002 dwo_file->sections.loc =
ed2dc618 12003 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12004 sections.loc_offset, sections.loc_size);
12005 dwo_file->sections.macinfo =
ed2dc618 12006 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12007 sections.macinfo_offset, sections.macinfo_size);
12008 dwo_file->sections.macro =
ed2dc618 12009 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12010 sections.macro_offset, sections.macro_size);
12011 dwo_file->sections.str_offsets =
ed2dc618
SM
12012 create_dwp_v2_section (dwarf2_per_objfile,
12013 &dwp_file->sections.str_offsets,
73869dc2
DE
12014 sections.str_offsets_offset,
12015 sections.str_offsets_size);
12016 /* The "str" section is global to the entire DWP file. */
12017 dwo_file->sections.str = dwp_file->sections.str;
12018 /* The info or types section is assigned below to dwo_unit,
12019 there's no need to record it in dwo_file.
12020 Also, we can't simply record type sections in dwo_file because
12021 we record a pointer into the vector in dwo_unit. As we collect more
12022 types we'll grow the vector and eventually have to reallocate space
12023 for it, invalidating all copies of pointers into the previous
12024 contents. */
12025 *dwo_file_slot = dwo_file;
12026 }
12027 else
12028 {
b4f54984 12029 if (dwarf_read_debug)
73869dc2
DE
12030 {
12031 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12032 virtual_dwo_name.c_str ());
73869dc2 12033 }
9a3c8263 12034 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12035 }
73869dc2
DE
12036
12037 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12038 dwo_unit->dwo_file = dwo_file;
12039 dwo_unit->signature = signature;
8d749320
SM
12040 dwo_unit->section =
12041 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12042 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12043 is_debug_types
73869dc2
DE
12044 ? &dwp_file->sections.types
12045 : &dwp_file->sections.info,
12046 sections.info_or_types_offset,
12047 sections.info_or_types_size);
12048 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12049
12050 return dwo_unit;
12051}
12052
57d63ce2
DE
12053/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12054 Returns NULL if the signature isn't found. */
80626a55
DE
12055
12056static struct dwo_unit *
ed2dc618
SM
12057lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12058 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12059 ULONGEST signature, int is_debug_types)
80626a55 12060{
57d63ce2
DE
12061 const struct dwp_hash_table *dwp_htab =
12062 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12063 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12064 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12065 uint32_t hash = signature & mask;
12066 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12067 unsigned int i;
12068 void **slot;
870f88f7 12069 struct dwo_unit find_dwo_cu;
80626a55
DE
12070
12071 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12072 find_dwo_cu.signature = signature;
19ac8c2e 12073 slot = htab_find_slot (is_debug_types
48b490f2
TT
12074 ? dwp_file->loaded_tus.get ()
12075 : dwp_file->loaded_cus.get (),
19ac8c2e 12076 &find_dwo_cu, INSERT);
80626a55
DE
12077
12078 if (*slot != NULL)
9a3c8263 12079 return (struct dwo_unit *) *slot;
80626a55
DE
12080
12081 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12082 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12083 {
12084 ULONGEST signature_in_table;
12085
12086 signature_in_table =
57d63ce2 12087 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12088 if (signature_in_table == signature)
12089 {
57d63ce2
DE
12090 uint32_t unit_index =
12091 read_4_bytes (dbfd,
12092 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12093
73869dc2
DE
12094 if (dwp_file->version == 1)
12095 {
ed2dc618
SM
12096 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12097 dwp_file, unit_index,
73869dc2
DE
12098 comp_dir, signature,
12099 is_debug_types);
12100 }
12101 else
12102 {
ed2dc618
SM
12103 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12104 dwp_file, unit_index,
73869dc2
DE
12105 comp_dir, signature,
12106 is_debug_types);
12107 }
9a3c8263 12108 return (struct dwo_unit *) *slot;
80626a55
DE
12109 }
12110 if (signature_in_table == 0)
12111 return NULL;
12112 hash = (hash + hash2) & mask;
12113 }
12114
12115 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12116 " [in module %s]"),
12117 dwp_file->name);
12118}
12119
ab5088bf 12120/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12121 Open the file specified by FILE_NAME and hand it off to BFD for
12122 preliminary analysis. Return a newly initialized bfd *, which
12123 includes a canonicalized copy of FILE_NAME.
80626a55 12124 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12125 SEARCH_CWD is true if the current directory is to be searched.
12126 It will be searched before debug-file-directory.
13aaf454
DE
12127 If successful, the file is added to the bfd include table of the
12128 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12129 If unable to find/open the file, return NULL.
3019eac3
DE
12130 NOTE: This function is derived from symfile_bfd_open. */
12131
192b62ce 12132static gdb_bfd_ref_ptr
ed2dc618
SM
12133try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12134 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12135{
24b9144d 12136 int desc;
9c02c129
DE
12137 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12138 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12139 to debug_file_directory. */
e0cc99a6 12140 const char *search_path;
9c02c129
DE
12141 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12142
e0cc99a6 12143 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12144 if (search_cwd)
12145 {
12146 if (*debug_file_directory != '\0')
e0cc99a6
TT
12147 {
12148 search_path_holder.reset (concat (".", dirname_separator_string,
12149 debug_file_directory,
12150 (char *) NULL));
12151 search_path = search_path_holder.get ();
12152 }
6ac97d4c 12153 else
e0cc99a6 12154 search_path = ".";
6ac97d4c 12155 }
9c02c129 12156 else
e0cc99a6 12157 search_path = debug_file_directory;
3019eac3 12158
24b9144d 12159 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12160 if (is_dwp)
12161 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12162
12163 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12164 desc = openp (search_path, flags, file_name,
3019eac3
DE
12165 O_RDONLY | O_BINARY, &absolute_name);
12166 if (desc < 0)
12167 return NULL;
12168
e0cc99a6
TT
12169 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12170 gnutarget, desc));
9c02c129
DE
12171 if (sym_bfd == NULL)
12172 return NULL;
192b62ce 12173 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12174
192b62ce
TT
12175 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12176 return NULL;
3019eac3 12177
13aaf454
DE
12178 /* Success. Record the bfd as having been included by the objfile's bfd.
12179 This is important because things like demangled_names_hash lives in the
12180 objfile's per_bfd space and may have references to things like symbol
12181 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12182 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12183
3019eac3
DE
12184 return sym_bfd;
12185}
12186
ab5088bf 12187/* Try to open DWO file FILE_NAME.
3019eac3
DE
12188 COMP_DIR is the DW_AT_comp_dir attribute.
12189 The result is the bfd handle of the file.
12190 If there is a problem finding or opening the file, return NULL.
12191 Upon success, the canonicalized path of the file is stored in the bfd,
12192 same as symfile_bfd_open. */
12193
192b62ce 12194static gdb_bfd_ref_ptr
ed2dc618
SM
12195open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12196 const char *file_name, const char *comp_dir)
3019eac3 12197{
80626a55 12198 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12199 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12200 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12201
12202 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12203
12204 if (comp_dir != NULL)
12205 {
43816ebc
TT
12206 gdb::unique_xmalloc_ptr<char> path_to_try
12207 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12208
12209 /* NOTE: If comp_dir is a relative path, this will also try the
12210 search path, which seems useful. */
ed2dc618 12211 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12212 path_to_try.get (),
ed2dc618 12213 0 /*is_dwp*/,
192b62ce 12214 1 /*search_cwd*/));
3019eac3
DE
12215 if (abfd != NULL)
12216 return abfd;
12217 }
12218
12219 /* That didn't work, try debug-file-directory, which, despite its name,
12220 is a list of paths. */
12221
12222 if (*debug_file_directory == '\0')
12223 return NULL;
12224
ed2dc618
SM
12225 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12226 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12227}
12228
80626a55
DE
12229/* This function is mapped across the sections and remembers the offset and
12230 size of each of the DWO debugging sections we are interested in. */
12231
12232static void
12233dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12234{
9a3c8263 12235 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12236 const struct dwop_section_names *names = &dwop_section_names;
12237
12238 if (section_is_p (sectp->name, &names->abbrev_dwo))
12239 {
049412e3 12240 dwo_sections->abbrev.s.section = sectp;
fd361982 12241 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12242 }
12243 else if (section_is_p (sectp->name, &names->info_dwo))
12244 {
049412e3 12245 dwo_sections->info.s.section = sectp;
fd361982 12246 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12247 }
12248 else if (section_is_p (sectp->name, &names->line_dwo))
12249 {
049412e3 12250 dwo_sections->line.s.section = sectp;
fd361982 12251 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12252 }
12253 else if (section_is_p (sectp->name, &names->loc_dwo))
12254 {
049412e3 12255 dwo_sections->loc.s.section = sectp;
fd361982 12256 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12257 }
41144253 12258 else if (section_is_p (sectp->name, &names->loclists_dwo))
12259 {
12260 dwo_sections->loclists.s.section = sectp;
12261 dwo_sections->loclists.size = bfd_section_size (sectp);
12262 }
80626a55
DE
12263 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12264 {
049412e3 12265 dwo_sections->macinfo.s.section = sectp;
fd361982 12266 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12267 }
12268 else if (section_is_p (sectp->name, &names->macro_dwo))
12269 {
049412e3 12270 dwo_sections->macro.s.section = sectp;
fd361982 12271 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12272 }
12273 else if (section_is_p (sectp->name, &names->str_dwo))
12274 {
049412e3 12275 dwo_sections->str.s.section = sectp;
fd361982 12276 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12277 }
12278 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12279 {
049412e3 12280 dwo_sections->str_offsets.s.section = sectp;
fd361982 12281 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12282 }
12283 else if (section_is_p (sectp->name, &names->types_dwo))
12284 {
12285 struct dwarf2_section_info type_section;
12286
12287 memset (&type_section, 0, sizeof (type_section));
049412e3 12288 type_section.s.section = sectp;
fd361982 12289 type_section.size = bfd_section_size (sectp);
fd5866f6 12290 dwo_sections->types.push_back (type_section);
80626a55
DE
12291 }
12292}
12293
ab5088bf 12294/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12295 by PER_CU. This is for the non-DWP case.
80626a55 12296 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12297
12298static struct dwo_file *
0ac5b59e
DE
12299open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12300 const char *dwo_name, const char *comp_dir)
3019eac3 12301{
ed2dc618 12302 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12303
fb1eb2f9 12304 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12305 if (dbfd == NULL)
12306 {
b4f54984 12307 if (dwarf_read_debug)
80626a55
DE
12308 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12309 return NULL;
12310 }
263db9a1 12311
51ac9db5 12312 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12313 dwo_file->dwo_name = dwo_name;
12314 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12315 dwo_file->dbfd = std::move (dbfd);
3019eac3 12316
fb1eb2f9 12317 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12318 &dwo_file->sections);
3019eac3 12319
18a8505e
AT
12320 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12321 dwo_file->sections.info, dwo_file->cus);
3019eac3 12322
263db9a1 12323 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12324 dwo_file->sections.types, dwo_file->tus);
3019eac3 12325
b4f54984 12326 if (dwarf_read_debug)
80626a55
DE
12327 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12328
263db9a1 12329 return dwo_file.release ();
3019eac3
DE
12330}
12331
80626a55 12332/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12333 size of each of the DWP debugging sections common to version 1 and 2 that
12334 we are interested in. */
3019eac3 12335
80626a55 12336static void
73869dc2
DE
12337dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12338 void *dwp_file_ptr)
3019eac3 12339{
9a3c8263 12340 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12341 const struct dwop_section_names *names = &dwop_section_names;
12342 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12343
80626a55 12344 /* Record the ELF section number for later lookup: this is what the
73869dc2 12345 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12346 gdb_assert (elf_section_nr < dwp_file->num_sections);
12347 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12348
80626a55
DE
12349 /* Look for specific sections that we need. */
12350 if (section_is_p (sectp->name, &names->str_dwo))
12351 {
049412e3 12352 dwp_file->sections.str.s.section = sectp;
fd361982 12353 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12354 }
12355 else if (section_is_p (sectp->name, &names->cu_index))
12356 {
049412e3 12357 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12358 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12359 }
12360 else if (section_is_p (sectp->name, &names->tu_index))
12361 {
049412e3 12362 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12363 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12364 }
12365}
3019eac3 12366
73869dc2
DE
12367/* This function is mapped across the sections and remembers the offset and
12368 size of each of the DWP version 2 debugging sections that we are interested
12369 in. This is split into a separate function because we don't know if we
12370 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12371
12372static void
12373dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12374{
9a3c8263 12375 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12376 const struct dwop_section_names *names = &dwop_section_names;
12377 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12378
12379 /* Record the ELF section number for later lookup: this is what the
12380 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12381 gdb_assert (elf_section_nr < dwp_file->num_sections);
12382 dwp_file->elf_sections[elf_section_nr] = sectp;
12383
12384 /* Look for specific sections that we need. */
12385 if (section_is_p (sectp->name, &names->abbrev_dwo))
12386 {
049412e3 12387 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12388 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12389 }
12390 else if (section_is_p (sectp->name, &names->info_dwo))
12391 {
049412e3 12392 dwp_file->sections.info.s.section = sectp;
fd361982 12393 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->line_dwo))
12396 {
049412e3 12397 dwp_file->sections.line.s.section = sectp;
fd361982 12398 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12399 }
12400 else if (section_is_p (sectp->name, &names->loc_dwo))
12401 {
049412e3 12402 dwp_file->sections.loc.s.section = sectp;
fd361982 12403 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12404 }
12405 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12406 {
049412e3 12407 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12408 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12409 }
12410 else if (section_is_p (sectp->name, &names->macro_dwo))
12411 {
049412e3 12412 dwp_file->sections.macro.s.section = sectp;
fd361982 12413 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12414 }
12415 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12416 {
049412e3 12417 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12418 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12419 }
12420 else if (section_is_p (sectp->name, &names->types_dwo))
12421 {
049412e3 12422 dwp_file->sections.types.s.section = sectp;
fd361982 12423 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12424 }
12425}
12426
80626a55 12427/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12428
80626a55
DE
12429static hashval_t
12430hash_dwp_loaded_cutus (const void *item)
12431{
9a3c8263 12432 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12433
80626a55
DE
12434 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12435 return dwo_unit->signature;
3019eac3
DE
12436}
12437
80626a55 12438/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12439
80626a55
DE
12440static int
12441eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12442{
9a3c8263
SM
12443 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12444 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12445
80626a55
DE
12446 return dua->signature == dub->signature;
12447}
3019eac3 12448
80626a55 12449/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12450
48b490f2 12451static htab_up
298e9637 12452allocate_dwp_loaded_cutus_table ()
80626a55 12453{
48b490f2
TT
12454 return htab_up (htab_create_alloc (3,
12455 hash_dwp_loaded_cutus,
12456 eq_dwp_loaded_cutus,
12457 NULL, xcalloc, xfree));
80626a55 12458}
3019eac3 12459
ab5088bf
DE
12460/* Try to open DWP file FILE_NAME.
12461 The result is the bfd handle of the file.
12462 If there is a problem finding or opening the file, return NULL.
12463 Upon success, the canonicalized path of the file is stored in the bfd,
12464 same as symfile_bfd_open. */
12465
192b62ce 12466static gdb_bfd_ref_ptr
ed2dc618
SM
12467open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12468 const char *file_name)
ab5088bf 12469{
ed2dc618
SM
12470 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12471 1 /*is_dwp*/,
192b62ce 12472 1 /*search_cwd*/));
6ac97d4c
DE
12473 if (abfd != NULL)
12474 return abfd;
12475
12476 /* Work around upstream bug 15652.
12477 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12478 [Whether that's a "bug" is debatable, but it is getting in our way.]
12479 We have no real idea where the dwp file is, because gdb's realpath-ing
12480 of the executable's path may have discarded the needed info.
12481 [IWBN if the dwp file name was recorded in the executable, akin to
12482 .gnu_debuglink, but that doesn't exist yet.]
12483 Strip the directory from FILE_NAME and search again. */
12484 if (*debug_file_directory != '\0')
12485 {
12486 /* Don't implicitly search the current directory here.
12487 If the user wants to search "." to handle this case,
12488 it must be added to debug-file-directory. */
ed2dc618
SM
12489 return try_open_dwop_file (dwarf2_per_objfile,
12490 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12491 0 /*search_cwd*/);
12492 }
12493
12494 return NULL;
ab5088bf
DE
12495}
12496
80626a55
DE
12497/* Initialize the use of the DWP file for the current objfile.
12498 By convention the name of the DWP file is ${objfile}.dwp.
12499 The result is NULL if it can't be found. */
a766d390 12500
400174b1 12501static std::unique_ptr<struct dwp_file>
ed2dc618 12502open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12503{
12504 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12505
82bf32bc
JK
12506 /* Try to find first .dwp for the binary file before any symbolic links
12507 resolving. */
6c447423
DE
12508
12509 /* If the objfile is a debug file, find the name of the real binary
12510 file and get the name of dwp file from there. */
d721ba37 12511 std::string dwp_name;
6c447423
DE
12512 if (objfile->separate_debug_objfile_backlink != NULL)
12513 {
12514 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12515 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12516
d721ba37 12517 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12518 }
12519 else
d721ba37
PA
12520 dwp_name = objfile->original_name;
12521
12522 dwp_name += ".dwp";
80626a55 12523
ed2dc618 12524 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12525 if (dbfd == NULL
12526 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12527 {
12528 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12529 dwp_name = objfile_name (objfile);
12530 dwp_name += ".dwp";
ed2dc618 12531 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12532 }
12533
80626a55
DE
12534 if (dbfd == NULL)
12535 {
b4f54984 12536 if (dwarf_read_debug)
d721ba37 12537 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12538 return std::unique_ptr<dwp_file> ();
3019eac3 12539 }
400174b1
TT
12540
12541 const char *name = bfd_get_filename (dbfd.get ());
12542 std::unique_ptr<struct dwp_file> dwp_file
12543 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12544
0a0f4c01 12545 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12546 dwp_file->elf_sections =
12547 OBSTACK_CALLOC (&objfile->objfile_obstack,
12548 dwp_file->num_sections, asection *);
12549
400174b1
TT
12550 bfd_map_over_sections (dwp_file->dbfd.get (),
12551 dwarf2_locate_common_dwp_sections,
12552 dwp_file.get ());
80626a55 12553
400174b1
TT
12554 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12555 0);
80626a55 12556
400174b1
TT
12557 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12558 1);
80626a55 12559
73869dc2 12560 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12561 if (dwp_file->cus && dwp_file->tus
12562 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12563 {
12564 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12565 pretty bizarre. We use pulongest here because that's the established
4d65956b 12566 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12567 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12568 " TU version %s [in DWP file %s]"),
12569 pulongest (dwp_file->cus->version),
d721ba37 12570 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12571 }
08302ed2
DE
12572
12573 if (dwp_file->cus)
12574 dwp_file->version = dwp_file->cus->version;
12575 else if (dwp_file->tus)
12576 dwp_file->version = dwp_file->tus->version;
12577 else
12578 dwp_file->version = 2;
73869dc2
DE
12579
12580 if (dwp_file->version == 2)
400174b1
TT
12581 bfd_map_over_sections (dwp_file->dbfd.get (),
12582 dwarf2_locate_v2_dwp_sections,
12583 dwp_file.get ());
73869dc2 12584
298e9637
SM
12585 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12586 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12587
b4f54984 12588 if (dwarf_read_debug)
80626a55
DE
12589 {
12590 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12591 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12592 " %s CUs, %s TUs\n",
12593 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12594 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12595 }
12596
12597 return dwp_file;
3019eac3 12598}
c906108c 12599
ab5088bf
DE
12600/* Wrapper around open_and_init_dwp_file, only open it once. */
12601
12602static struct dwp_file *
ed2dc618 12603get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12604{
12605 if (! dwarf2_per_objfile->dwp_checked)
12606 {
ed2dc618
SM
12607 dwarf2_per_objfile->dwp_file
12608 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12609 dwarf2_per_objfile->dwp_checked = 1;
12610 }
400174b1 12611 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12612}
12613
80626a55
DE
12614/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12615 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12616 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12617 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12618 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12619
12620 This is called, for example, when wanting to read a variable with a
12621 complex location. Therefore we don't want to do file i/o for every call.
12622 Therefore we don't want to look for a DWO file on every call.
12623 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12624 then we check if we've already seen DWO_NAME, and only THEN do we check
12625 for a DWO file.
12626
1c658ad5 12627 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12628 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12629
3019eac3 12630static struct dwo_unit *
80626a55
DE
12631lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12632 const char *dwo_name, const char *comp_dir,
12633 ULONGEST signature, int is_debug_types)
3019eac3 12634{
ed2dc618 12635 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12636 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12637 const char *kind = is_debug_types ? "TU" : "CU";
12638 void **dwo_file_slot;
3019eac3 12639 struct dwo_file *dwo_file;
80626a55 12640 struct dwp_file *dwp_file;
cb1df416 12641
6a506a2d
DE
12642 /* First see if there's a DWP file.
12643 If we have a DWP file but didn't find the DWO inside it, don't
12644 look for the original DWO file. It makes gdb behave differently
12645 depending on whether one is debugging in the build tree. */
cf2c3c16 12646
ed2dc618 12647 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12648 if (dwp_file != NULL)
cf2c3c16 12649 {
80626a55
DE
12650 const struct dwp_hash_table *dwp_htab =
12651 is_debug_types ? dwp_file->tus : dwp_file->cus;
12652
12653 if (dwp_htab != NULL)
12654 {
12655 struct dwo_unit *dwo_cutu =
ed2dc618 12656 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12657 signature, is_debug_types);
80626a55
DE
12658
12659 if (dwo_cutu != NULL)
12660 {
b4f54984 12661 if (dwarf_read_debug)
80626a55
DE
12662 {
12663 fprintf_unfiltered (gdb_stdlog,
12664 "Virtual DWO %s %s found: @%s\n",
12665 kind, hex_string (signature),
12666 host_address_to_string (dwo_cutu));
12667 }
12668 return dwo_cutu;
12669 }
12670 }
12671 }
6a506a2d 12672 else
80626a55 12673 {
6a506a2d 12674 /* No DWP file, look for the DWO file. */
80626a55 12675
ed2dc618
SM
12676 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12677 dwo_name, comp_dir);
6a506a2d 12678 if (*dwo_file_slot == NULL)
80626a55 12679 {
6a506a2d
DE
12680 /* Read in the file and build a table of the CUs/TUs it contains. */
12681 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12682 }
6a506a2d 12683 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12684 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12685
6a506a2d 12686 if (dwo_file != NULL)
19c3d4c9 12687 {
6a506a2d
DE
12688 struct dwo_unit *dwo_cutu = NULL;
12689
12690 if (is_debug_types && dwo_file->tus)
12691 {
12692 struct dwo_unit find_dwo_cutu;
12693
12694 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12695 find_dwo_cutu.signature = signature;
9a3c8263 12696 dwo_cutu
b0b6a987
TT
12697 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12698 &find_dwo_cutu);
6a506a2d 12699 }
33c5cd75 12700 else if (!is_debug_types && dwo_file->cus)
80626a55 12701 {
33c5cd75
DB
12702 struct dwo_unit find_dwo_cutu;
12703
12704 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12705 find_dwo_cutu.signature = signature;
b0b6a987 12706 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12707 &find_dwo_cutu);
6a506a2d
DE
12708 }
12709
12710 if (dwo_cutu != NULL)
12711 {
b4f54984 12712 if (dwarf_read_debug)
6a506a2d
DE
12713 {
12714 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12715 kind, dwo_name, hex_string (signature),
12716 host_address_to_string (dwo_cutu));
12717 }
12718 return dwo_cutu;
80626a55
DE
12719 }
12720 }
2e276125 12721 }
9cdd5dbd 12722
80626a55
DE
12723 /* We didn't find it. This could mean a dwo_id mismatch, or
12724 someone deleted the DWO/DWP file, or the search path isn't set up
12725 correctly to find the file. */
12726
b4f54984 12727 if (dwarf_read_debug)
80626a55
DE
12728 {
12729 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12730 kind, dwo_name, hex_string (signature));
12731 }
3019eac3 12732
6656a72d
DE
12733 /* This is a warning and not a complaint because it can be caused by
12734 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12735 {
12736 /* Print the name of the DWP file if we looked there, helps the user
12737 better diagnose the problem. */
791afaa2 12738 std::string dwp_text;
43942612
DE
12739
12740 if (dwp_file != NULL)
791afaa2
TT
12741 dwp_text = string_printf (" [in DWP file %s]",
12742 lbasename (dwp_file->name));
43942612 12743
9d8780f0 12744 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12745 " [in module %s]"),
12746 kind, dwo_name, hex_string (signature),
791afaa2 12747 dwp_text.c_str (),
43942612 12748 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12749 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12750 }
3019eac3 12751 return NULL;
5fb290d7
DJ
12752}
12753
80626a55
DE
12754/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12755 See lookup_dwo_cutu_unit for details. */
12756
12757static struct dwo_unit *
12758lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12759 const char *dwo_name, const char *comp_dir,
12760 ULONGEST signature)
12761{
12762 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12763}
12764
12765/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12766 See lookup_dwo_cutu_unit for details. */
12767
12768static struct dwo_unit *
12769lookup_dwo_type_unit (struct signatured_type *this_tu,
12770 const char *dwo_name, const char *comp_dir)
12771{
12772 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12773}
12774
89e63ee4
DE
12775/* Traversal function for queue_and_load_all_dwo_tus. */
12776
12777static int
12778queue_and_load_dwo_tu (void **slot, void *info)
12779{
12780 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12781 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12782 ULONGEST signature = dwo_unit->signature;
12783 struct signatured_type *sig_type =
12784 lookup_dwo_signatured_type (per_cu->cu, signature);
12785
12786 if (sig_type != NULL)
12787 {
12788 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12789
12790 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12791 a real dependency of PER_CU on SIG_TYPE. That is detected later
12792 while processing PER_CU. */
12793 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12794 load_full_type_unit (sig_cu);
ae640021 12795 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12796 }
12797
12798 return 1;
12799}
12800
12801/* Queue all TUs contained in the DWO of PER_CU to be read in.
12802 The DWO may have the only definition of the type, though it may not be
12803 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12804 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12805
12806static void
12807queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12808{
12809 struct dwo_unit *dwo_unit;
12810 struct dwo_file *dwo_file;
12811
12812 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12813 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12814 gdb_assert (per_cu->cu != NULL);
12815
12816 dwo_unit = per_cu->cu->dwo_unit;
12817 gdb_assert (dwo_unit != NULL);
12818
12819 dwo_file = dwo_unit->dwo_file;
12820 if (dwo_file->tus != NULL)
b0b6a987
TT
12821 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12822 per_cu);
89e63ee4
DE
12823}
12824
3019eac3 12825/* Read in various DIEs. */
348e048f 12826
d389af10 12827/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12828 Inherit only the children of the DW_AT_abstract_origin DIE not being
12829 already referenced by DW_AT_abstract_origin from the children of the
12830 current DIE. */
d389af10
JK
12831
12832static void
12833inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12834{
12835 struct die_info *child_die;
791afaa2 12836 sect_offset *offsetp;
d389af10
JK
12837 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12838 struct die_info *origin_die;
12839 /* Iterator of the ORIGIN_DIE children. */
12840 struct die_info *origin_child_die;
d389af10 12841 struct attribute *attr;
cd02d79d
PA
12842 struct dwarf2_cu *origin_cu;
12843 struct pending **origin_previous_list_in_scope;
d389af10
JK
12844
12845 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12846 if (!attr)
12847 return;
12848
cd02d79d
PA
12849 /* Note that following die references may follow to a die in a
12850 different cu. */
12851
12852 origin_cu = cu;
12853 origin_die = follow_die_ref (die, attr, &origin_cu);
12854
12855 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12856 symbols in. */
12857 origin_previous_list_in_scope = origin_cu->list_in_scope;
12858 origin_cu->list_in_scope = cu->list_in_scope;
12859
edb3359d
DJ
12860 if (die->tag != origin_die->tag
12861 && !(die->tag == DW_TAG_inlined_subroutine
12862 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12863 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12864 sect_offset_str (die->sect_off),
12865 sect_offset_str (origin_die->sect_off));
d389af10 12866
791afaa2 12867 std::vector<sect_offset> offsets;
d389af10 12868
3ea89b92
PMR
12869 for (child_die = die->child;
12870 child_die && child_die->tag;
436c571c 12871 child_die = child_die->sibling)
3ea89b92
PMR
12872 {
12873 struct die_info *child_origin_die;
12874 struct dwarf2_cu *child_origin_cu;
12875
12876 /* We are trying to process concrete instance entries:
216f72a1 12877 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12878 it's not relevant to our analysis here. i.e. detecting DIEs that are
12879 present in the abstract instance but not referenced in the concrete
12880 one. */
216f72a1
JK
12881 if (child_die->tag == DW_TAG_call_site
12882 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12883 continue;
12884
c38f313d
DJ
12885 /* For each CHILD_DIE, find the corresponding child of
12886 ORIGIN_DIE. If there is more than one layer of
12887 DW_AT_abstract_origin, follow them all; there shouldn't be,
12888 but GCC versions at least through 4.4 generate this (GCC PR
12889 40573). */
3ea89b92
PMR
12890 child_origin_die = child_die;
12891 child_origin_cu = cu;
c38f313d
DJ
12892 while (1)
12893 {
cd02d79d
PA
12894 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12895 child_origin_cu);
c38f313d
DJ
12896 if (attr == NULL)
12897 break;
cd02d79d
PA
12898 child_origin_die = follow_die_ref (child_origin_die, attr,
12899 &child_origin_cu);
c38f313d
DJ
12900 }
12901
d389af10
JK
12902 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12903 counterpart may exist. */
c38f313d 12904 if (child_origin_die != child_die)
d389af10 12905 {
edb3359d
DJ
12906 if (child_die->tag != child_origin_die->tag
12907 && !(child_die->tag == DW_TAG_inlined_subroutine
12908 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12909 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12910 "different tags"),
9d8780f0
SM
12911 sect_offset_str (child_die->sect_off),
12912 sect_offset_str (child_origin_die->sect_off));
c38f313d 12913 if (child_origin_die->parent != origin_die)
b98664d3 12914 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12915 "different parents"),
9d8780f0
SM
12916 sect_offset_str (child_die->sect_off),
12917 sect_offset_str (child_origin_die->sect_off));
c38f313d 12918 else
791afaa2 12919 offsets.push_back (child_origin_die->sect_off);
d389af10 12920 }
d389af10 12921 }
791afaa2
TT
12922 std::sort (offsets.begin (), offsets.end ());
12923 sect_offset *offsets_end = offsets.data () + offsets.size ();
12924 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12925 if (offsetp[-1] == *offsetp)
b98664d3 12926 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12927 "to DIE %s as their abstract origin"),
12928 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12929
791afaa2 12930 offsetp = offsets.data ();
d389af10
JK
12931 origin_child_die = origin_die->child;
12932 while (origin_child_die && origin_child_die->tag)
12933 {
12934 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12935 while (offsetp < offsets_end
9c541725 12936 && *offsetp < origin_child_die->sect_off)
d389af10 12937 offsetp++;
b64f50a1 12938 if (offsetp >= offsets_end
9c541725 12939 || *offsetp > origin_child_die->sect_off)
d389af10 12940 {
adde2bff
DE
12941 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12942 Check whether we're already processing ORIGIN_CHILD_DIE.
12943 This can happen with mutually referenced abstract_origins.
12944 PR 16581. */
12945 if (!origin_child_die->in_process)
12946 process_die (origin_child_die, origin_cu);
d389af10 12947 }
436c571c 12948 origin_child_die = origin_child_die->sibling;
d389af10 12949 }
cd02d79d 12950 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12951
12952 if (cu != origin_cu)
12953 compute_delayed_physnames (origin_cu);
d389af10
JK
12954}
12955
c906108c 12956static void
e7c27a73 12957read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12958{
518817b3 12959 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12960 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12961 struct context_stack *newobj;
c906108c
SS
12962 CORE_ADDR lowpc;
12963 CORE_ADDR highpc;
12964 struct die_info *child_die;
edb3359d 12965 struct attribute *attr, *call_line, *call_file;
15d034d0 12966 const char *name;
e142c38c 12967 CORE_ADDR baseaddr;
801e3a5b 12968 struct block *block;
edb3359d 12969 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12970 std::vector<struct symbol *> template_args;
34eaf542 12971 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12972
12973 if (inlined_func)
12974 {
12975 /* If we do not have call site information, we can't show the
12976 caller of this inlined function. That's too confusing, so
12977 only use the scope for local variables. */
12978 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12979 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12980 if (call_line == NULL || call_file == NULL)
12981 {
12982 read_lexical_block_scope (die, cu);
12983 return;
12984 }
12985 }
c906108c 12986
b3b3bada 12987 baseaddr = objfile->text_section_offset ();
e142c38c 12988
94af9270 12989 name = dwarf2_name (die, cu);
c906108c 12990
e8d05480
JB
12991 /* Ignore functions with missing or empty names. These are actually
12992 illegal according to the DWARF standard. */
12993 if (name == NULL)
12994 {
b98664d3 12995 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12996 sect_offset_str (die->sect_off));
e8d05480
JB
12997 return;
12998 }
12999
13000 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13001 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13002 <= PC_BOUNDS_INVALID)
e8d05480 13003 {
ae4d0c03
PM
13004 attr = dwarf2_attr (die, DW_AT_external, cu);
13005 if (!attr || !DW_UNSND (attr))
b98664d3 13006 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13007 "for subprogram DIE at %s"),
13008 sect_offset_str (die->sect_off));
e8d05480
JB
13009 return;
13010 }
c906108c 13011
3e29f34a
MR
13012 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13013 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13014
34eaf542
TT
13015 /* If we have any template arguments, then we must allocate a
13016 different sort of symbol. */
436c571c 13017 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13018 {
13019 if (child_die->tag == DW_TAG_template_type_param
13020 || child_die->tag == DW_TAG_template_value_param)
13021 {
8c14c3a3 13022 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13023 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13024 break;
13025 }
13026 }
13027
c24bdb02 13028 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13029 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13030 (struct symbol *) templ_func);
4c2df51b 13031
81873cc8 13032 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13033 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13034 cu->language);
13035
4cecd739
DJ
13036 /* If there is a location expression for DW_AT_frame_base, record
13037 it. */
e142c38c 13038 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13039 if (attr != nullptr)
fe978cb0 13040 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13041
63e43d3a
PMR
13042 /* If there is a location for the static link, record it. */
13043 newobj->static_link = NULL;
13044 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13045 if (attr != nullptr)
63e43d3a 13046 {
224c3ddb
SM
13047 newobj->static_link
13048 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13049 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13050 cu->per_cu->addr_type ());
63e43d3a
PMR
13051 }
13052
c24bdb02 13053 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13054
639d11d3 13055 if (die->child != NULL)
c906108c 13056 {
639d11d3 13057 child_die = die->child;
c906108c
SS
13058 while (child_die && child_die->tag)
13059 {
34eaf542
TT
13060 if (child_die->tag == DW_TAG_template_type_param
13061 || child_die->tag == DW_TAG_template_value_param)
13062 {
13063 struct symbol *arg = new_symbol (child_die, NULL, cu);
13064
f1078f66 13065 if (arg != NULL)
2f4732b0 13066 template_args.push_back (arg);
34eaf542
TT
13067 }
13068 else
13069 process_die (child_die, cu);
436c571c 13070 child_die = child_die->sibling;
c906108c
SS
13071 }
13072 }
13073
d389af10
JK
13074 inherit_abstract_dies (die, cu);
13075
4a811a97
UW
13076 /* If we have a DW_AT_specification, we might need to import using
13077 directives from the context of the specification DIE. See the
13078 comment in determine_prefix. */
13079 if (cu->language == language_cplus
13080 && dwarf2_attr (die, DW_AT_specification, cu))
13081 {
13082 struct dwarf2_cu *spec_cu = cu;
13083 struct die_info *spec_die = die_specification (die, &spec_cu);
13084
13085 while (spec_die)
13086 {
13087 child_die = spec_die->child;
13088 while (child_die && child_die->tag)
13089 {
13090 if (child_die->tag == DW_TAG_imported_module)
13091 process_die (child_die, spec_cu);
436c571c 13092 child_die = child_die->sibling;
4a811a97
UW
13093 }
13094
13095 /* In some cases, GCC generates specification DIEs that
13096 themselves contain DW_AT_specification attributes. */
13097 spec_die = die_specification (spec_die, &spec_cu);
13098 }
13099 }
13100
c24bdb02 13101 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13102 /* Make a block for the local symbols within. */
c24bdb02 13103 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13104 cstk.static_link, lowpc, highpc);
801e3a5b 13105
df8a16a1 13106 /* For C++, set the block's scope. */
45280282
IB
13107 if ((cu->language == language_cplus
13108 || cu->language == language_fortran
c44af4eb
TT
13109 || cu->language == language_d
13110 || cu->language == language_rust)
4d4ec4e5 13111 && cu->processing_has_namespace_info)
195a3f6c
TT
13112 block_set_scope (block, determine_prefix (die, cu),
13113 &objfile->objfile_obstack);
df8a16a1 13114
801e3a5b
JB
13115 /* If we have address ranges, record them. */
13116 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13117
a60f3166 13118 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13119
34eaf542 13120 /* Attach template arguments to function. */
2f4732b0 13121 if (!template_args.empty ())
34eaf542
TT
13122 {
13123 gdb_assert (templ_func != NULL);
13124
2f4732b0 13125 templ_func->n_template_arguments = template_args.size ();
34eaf542 13126 templ_func->template_arguments
8d749320
SM
13127 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13128 templ_func->n_template_arguments);
34eaf542 13129 memcpy (templ_func->template_arguments,
2f4732b0 13130 template_args.data (),
34eaf542 13131 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13132
13133 /* Make sure that the symtab is set on the new symbols. Even
13134 though they don't appear in this symtab directly, other parts
13135 of gdb assume that symbols do, and this is reasonably
13136 true. */
8634679f 13137 for (symbol *sym : template_args)
3e1d3d8c 13138 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13139 }
13140
208d8187
JB
13141 /* In C++, we can have functions nested inside functions (e.g., when
13142 a function declares a class that has methods). This means that
13143 when we finish processing a function scope, we may need to go
13144 back to building a containing block's symbol lists. */
c24bdb02
KS
13145 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13146 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13147
921e78cf
JB
13148 /* If we've finished processing a top-level function, subsequent
13149 symbols go in the file symbol list. */
c24bdb02
KS
13150 if (cu->get_builder ()->outermost_context_p ())
13151 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13152}
13153
13154/* Process all the DIES contained within a lexical block scope. Start
13155 a new scope, process the dies, and then close the scope. */
13156
13157static void
e7c27a73 13158read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13159{
518817b3 13160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13161 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13162 CORE_ADDR lowpc, highpc;
13163 struct die_info *child_die;
e142c38c
DJ
13164 CORE_ADDR baseaddr;
13165
b3b3bada 13166 baseaddr = objfile->text_section_offset ();
c906108c
SS
13167
13168 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13169 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13170 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13171 be nasty. Might be easier to properly extend generic blocks to
af34e669 13172 describe ranges. */
e385593e
JK
13173 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13174 {
13175 case PC_BOUNDS_NOT_PRESENT:
13176 /* DW_TAG_lexical_block has no attributes, process its children as if
13177 there was no wrapping by that DW_TAG_lexical_block.
13178 GCC does no longer produces such DWARF since GCC r224161. */
13179 for (child_die = die->child;
13180 child_die != NULL && child_die->tag;
436c571c 13181 child_die = child_die->sibling)
4f7bc5ed
TT
13182 {
13183 /* We might already be processing this DIE. This can happen
13184 in an unusual circumstance -- where a subroutine A
13185 appears lexically in another subroutine B, but A actually
13186 inlines B. The recursion is broken here, rather than in
13187 inherit_abstract_dies, because it seems better to simply
13188 drop concrete children here. */
13189 if (!child_die->in_process)
13190 process_die (child_die, cu);
13191 }
e385593e
JK
13192 return;
13193 case PC_BOUNDS_INVALID:
13194 return;
13195 }
3e29f34a
MR
13196 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13197 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13198
c24bdb02 13199 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13200 if (die->child != NULL)
c906108c 13201 {
639d11d3 13202 child_die = die->child;
c906108c
SS
13203 while (child_die && child_die->tag)
13204 {
e7c27a73 13205 process_die (child_die, cu);
436c571c 13206 child_die = child_die->sibling;
c906108c
SS
13207 }
13208 }
3ea89b92 13209 inherit_abstract_dies (die, cu);
c24bdb02 13210 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13211
c24bdb02
KS
13212 if (*cu->get_builder ()->get_local_symbols () != NULL
13213 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13214 {
801e3a5b 13215 struct block *block
c24bdb02 13216 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13217 cstk.start_addr, highpc);
801e3a5b
JB
13218
13219 /* Note that recording ranges after traversing children, as we
13220 do here, means that recording a parent's ranges entails
13221 walking across all its children's ranges as they appear in
13222 the address map, which is quadratic behavior.
13223
13224 It would be nicer to record the parent's ranges before
13225 traversing its children, simply overriding whatever you find
13226 there. But since we don't even decide whether to create a
13227 block until after we've traversed its children, that's hard
13228 to do. */
13229 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13230 }
c24bdb02
KS
13231 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13232 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13233}
13234
216f72a1 13235/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13236
13237static void
13238read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13239{
518817b3 13240 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13241 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13242 CORE_ADDR pc, baseaddr;
13243 struct attribute *attr;
13244 struct call_site *call_site, call_site_local;
13245 void **slot;
13246 int nparams;
13247 struct die_info *child_die;
13248
b3b3bada 13249 baseaddr = objfile->text_section_offset ();
96408a79 13250
216f72a1
JK
13251 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13252 if (attr == NULL)
13253 {
13254 /* This was a pre-DWARF-5 GNU extension alias
13255 for DW_AT_call_return_pc. */
13256 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13257 }
96408a79
SA
13258 if (!attr)
13259 {
b98664d3 13260 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13261 "DIE %s [in module %s]"),
13262 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13263 return;
13264 }
cd6c91b4 13265 pc = attr->value_as_address () + baseaddr;
3e29f34a 13266 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13267
13268 if (cu->call_site_htab == NULL)
13269 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13270 NULL, &objfile->objfile_obstack,
13271 hashtab_obstack_allocate, NULL);
13272 call_site_local.pc = pc;
13273 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13274 if (*slot != NULL)
13275 {
b98664d3 13276 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13277 "DIE %s [in module %s]"),
13278 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13279 objfile_name (objfile));
96408a79
SA
13280 return;
13281 }
13282
13283 /* Count parameters at the caller. */
13284
13285 nparams = 0;
13286 for (child_die = die->child; child_die && child_die->tag;
436c571c 13287 child_die = child_die->sibling)
96408a79 13288 {
216f72a1
JK
13289 if (child_die->tag != DW_TAG_call_site_parameter
13290 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13291 {
b98664d3 13292 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13293 "DW_TAG_call_site child DIE %s [in module %s]"),
13294 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13295 objfile_name (objfile));
96408a79
SA
13296 continue;
13297 }
13298
13299 nparams++;
13300 }
13301
224c3ddb
SM
13302 call_site
13303 = ((struct call_site *)
13304 obstack_alloc (&objfile->objfile_obstack,
13305 sizeof (*call_site)
13306 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13307 *slot = call_site;
13308 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13309 call_site->pc = pc;
13310
216f72a1
JK
13311 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13312 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13313 {
13314 struct die_info *func_die;
13315
13316 /* Skip also over DW_TAG_inlined_subroutine. */
13317 for (func_die = die->parent;
13318 func_die && func_die->tag != DW_TAG_subprogram
13319 && func_die->tag != DW_TAG_subroutine_type;
13320 func_die = func_die->parent);
13321
216f72a1
JK
13322 /* DW_AT_call_all_calls is a superset
13323 of DW_AT_call_all_tail_calls. */
96408a79 13324 if (func_die
216f72a1 13325 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13326 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13327 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13328 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13329 {
13330 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13331 not complete. But keep CALL_SITE for look ups via call_site_htab,
13332 both the initial caller containing the real return address PC and
13333 the final callee containing the current PC of a chain of tail
13334 calls do not need to have the tail call list complete. But any
13335 function candidate for a virtual tail call frame searched via
13336 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13337 determined unambiguously. */
13338 }
13339 else
13340 {
13341 struct type *func_type = NULL;
13342
13343 if (func_die)
13344 func_type = get_die_type (func_die, cu);
13345 if (func_type != NULL)
13346 {
78134374 13347 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13348
13349 /* Enlist this call site to the function. */
13350 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13351 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13352 }
13353 else
b98664d3 13354 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13355 "DIE %s [in module %s]"),
13356 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13357 }
13358 }
13359
216f72a1
JK
13360 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13361 if (attr == NULL)
13362 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13363 if (attr == NULL)
13364 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13365 if (attr == NULL)
216f72a1
JK
13366 {
13367 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13368 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13369 }
96408a79 13370 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13371 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13372 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13373 else if (attr->form_is_block ())
96408a79
SA
13374 {
13375 struct dwarf2_locexpr_baton *dlbaton;
13376
8d749320 13377 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13378 dlbaton->data = DW_BLOCK (attr)->data;
13379 dlbaton->size = DW_BLOCK (attr)->size;
13380 dlbaton->per_cu = cu->per_cu;
13381
13382 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13383 }
cd6c91b4 13384 else if (attr->form_is_ref ())
96408a79 13385 {
96408a79
SA
13386 struct dwarf2_cu *target_cu = cu;
13387 struct die_info *target_die;
13388
ac9ec31b 13389 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13390 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13391 if (die_is_declaration (target_die, target_cu))
13392 {
7d45c7c3 13393 const char *target_physname;
9112db09
JK
13394
13395 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13396 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13397 if (target_physname == NULL)
9112db09 13398 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13399 if (target_physname == NULL)
b98664d3 13400 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13401 "physname, for referencing DIE %s [in module %s]"),
13402 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13403 else
7d455152 13404 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13405 }
13406 else
13407 {
13408 CORE_ADDR lowpc;
13409
13410 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13411 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13412 <= PC_BOUNDS_INVALID)
b98664d3 13413 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13414 "low pc, for referencing DIE %s [in module %s]"),
13415 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13416 else
3e29f34a
MR
13417 {
13418 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13419 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13420 }
96408a79
SA
13421 }
13422 }
13423 else
b98664d3 13424 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13425 "block nor reference, for DIE %s [in module %s]"),
13426 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13427
13428 call_site->per_cu = cu->per_cu;
13429
13430 for (child_die = die->child;
13431 child_die && child_die->tag;
436c571c 13432 child_die = child_die->sibling)
96408a79 13433 {
96408a79 13434 struct call_site_parameter *parameter;
1788b2d3 13435 struct attribute *loc, *origin;
96408a79 13436
216f72a1
JK
13437 if (child_die->tag != DW_TAG_call_site_parameter
13438 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13439 {
13440 /* Already printed the complaint above. */
13441 continue;
13442 }
13443
13444 gdb_assert (call_site->parameter_count < nparams);
13445 parameter = &call_site->parameter[call_site->parameter_count];
13446
1788b2d3
JK
13447 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13448 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13449 register is contained in DW_AT_call_value. */
96408a79 13450
24c5c679 13451 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13452 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13453 if (origin == NULL)
13454 {
13455 /* This was a pre-DWARF-5 GNU extension alias
13456 for DW_AT_call_parameter. */
13457 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13458 }
cd6c91b4 13459 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13460 {
1788b2d3 13461 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13462
0826b30a 13463 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13464 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13465 {
13466 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13467 binding can be done only inside one CU. Such referenced DIE
13468 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13469 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13470 "DW_TAG_call_site child DIE %s [in module %s]"),
13471 sect_offset_str (child_die->sect_off),
9c541725 13472 objfile_name (objfile));
d76b7dbc
JK
13473 continue;
13474 }
9c541725
PA
13475 parameter->u.param_cu_off
13476 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13477 }
4fc6c0d5 13478 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13479 {
b98664d3 13480 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13481 "DW_TAG_call_site child DIE %s [in module %s]"),
13482 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13483 continue;
13484 }
24c5c679 13485 else
96408a79 13486 {
24c5c679
JK
13487 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13488 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13489 if (parameter->u.dwarf_reg != -1)
13490 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13491 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13492 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13493 &parameter->u.fb_offset))
13494 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13495 else
13496 {
b98664d3 13497 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13498 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13499 "DW_TAG_call_site child DIE %s "
24c5c679 13500 "[in module %s]"),
9d8780f0 13501 sect_offset_str (child_die->sect_off),
9c541725 13502 objfile_name (objfile));
24c5c679
JK
13503 continue;
13504 }
96408a79
SA
13505 }
13506
216f72a1
JK
13507 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13508 if (attr == NULL)
13509 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13510 if (attr == NULL || !attr->form_is_block ())
96408a79 13511 {
b98664d3 13512 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13513 "DW_TAG_call_site child DIE %s [in module %s]"),
13514 sect_offset_str (child_die->sect_off),
9c541725 13515 objfile_name (objfile));
96408a79
SA
13516 continue;
13517 }
13518 parameter->value = DW_BLOCK (attr)->data;
13519 parameter->value_size = DW_BLOCK (attr)->size;
13520
13521 /* Parameters are not pre-cleared by memset above. */
13522 parameter->data_value = NULL;
13523 parameter->data_value_size = 0;
13524 call_site->parameter_count++;
13525
216f72a1
JK
13526 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13527 if (attr == NULL)
13528 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13529 if (attr != nullptr)
96408a79 13530 {
4fc6c0d5 13531 if (!attr->form_is_block ())
b98664d3 13532 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13533 "DW_TAG_call_site child DIE %s [in module %s]"),
13534 sect_offset_str (child_die->sect_off),
9c541725 13535 objfile_name (objfile));
96408a79
SA
13536 else
13537 {
13538 parameter->data_value = DW_BLOCK (attr)->data;
13539 parameter->data_value_size = DW_BLOCK (attr)->size;
13540 }
13541 }
13542 }
13543}
13544
71a3c369
TT
13545/* Helper function for read_variable. If DIE represents a virtual
13546 table, then return the type of the concrete object that is
13547 associated with the virtual table. Otherwise, return NULL. */
13548
13549static struct type *
13550rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13551{
13552 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13553 if (attr == NULL)
13554 return NULL;
13555
13556 /* Find the type DIE. */
13557 struct die_info *type_die = NULL;
13558 struct dwarf2_cu *type_cu = cu;
13559
cd6c91b4 13560 if (attr->form_is_ref ())
71a3c369
TT
13561 type_die = follow_die_ref (die, attr, &type_cu);
13562 if (type_die == NULL)
13563 return NULL;
13564
13565 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13566 return NULL;
13567 return die_containing_type (type_die, type_cu);
13568}
13569
13570/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13571
13572static void
13573read_variable (struct die_info *die, struct dwarf2_cu *cu)
13574{
13575 struct rust_vtable_symbol *storage = NULL;
13576
13577 if (cu->language == language_rust)
13578 {
13579 struct type *containing_type = rust_containing_type (die, cu);
13580
13581 if (containing_type != NULL)
13582 {
518817b3 13583 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13584
8c14c3a3 13585 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13586 storage->concrete_type = containing_type;
cf724bc9 13587 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13588 }
13589 }
13590
e4a62c65
TV
13591 struct symbol *res = new_symbol (die, NULL, cu, storage);
13592 struct attribute *abstract_origin
13593 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13594 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13595 if (res == NULL && loc && abstract_origin)
13596 {
13597 /* We have a variable without a name, but with a location and an abstract
13598 origin. This may be a concrete instance of an abstract variable
13599 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13600 later. */
13601 struct dwarf2_cu *origin_cu = cu;
13602 struct die_info *origin_die
13603 = follow_die_ref (die, abstract_origin, &origin_cu);
13604 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13605 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13606 }
71a3c369
TT
13607}
13608
43988095
JK
13609/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13610 reading .debug_rnglists.
13611 Callback's type should be:
13612 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13613 Return true if the attributes are present and valid, otherwise,
13614 return false. */
13615
13616template <typename Callback>
13617static bool
13618dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13619 Callback &&callback)
13620{
ed2dc618 13621 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13622 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13623 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13624 bfd *obfd = objfile->obfd;
43988095 13625 /* Base address selection entry. */
2b24b6e4 13626 gdb::optional<CORE_ADDR> base;
43988095 13627 const gdb_byte *buffer;
43988095
JK
13628 CORE_ADDR baseaddr;
13629 bool overflow = false;
13630
43988095
JK
13631 base = cu->base_address;
13632
96b79293 13633 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13634 if (offset >= dwarf2_per_objfile->rnglists.size)
13635 {
b98664d3 13636 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13637 offset);
13638 return false;
13639 }
13640 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13641
b3b3bada 13642 baseaddr = objfile->text_section_offset ();
43988095
JK
13643
13644 while (1)
13645 {
7814882a
JK
13646 /* Initialize it due to a false compiler warning. */
13647 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13648 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13649 + dwarf2_per_objfile->rnglists.size);
13650 unsigned int bytes_read;
13651
13652 if (buffer == buf_end)
13653 {
13654 overflow = true;
13655 break;
13656 }
13657 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13658 switch (rlet)
13659 {
13660 case DW_RLE_end_of_list:
13661 break;
13662 case DW_RLE_base_address:
13663 if (buffer + cu->header.addr_size > buf_end)
13664 {
13665 overflow = true;
13666 break;
13667 }
c8a7a66f 13668 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13669 buffer += bytes_read;
13670 break;
13671 case DW_RLE_start_length:
13672 if (buffer + cu->header.addr_size > buf_end)
13673 {
13674 overflow = true;
13675 break;
13676 }
c8a7a66f
TT
13677 range_beginning = cu->header.read_address (obfd, buffer,
13678 &bytes_read);
43988095
JK
13679 buffer += bytes_read;
13680 range_end = (range_beginning
13681 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13682 buffer += bytes_read;
13683 if (buffer > buf_end)
13684 {
13685 overflow = true;
13686 break;
13687 }
13688 break;
13689 case DW_RLE_offset_pair:
13690 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13691 buffer += bytes_read;
13692 if (buffer > buf_end)
13693 {
13694 overflow = true;
13695 break;
13696 }
13697 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13698 buffer += bytes_read;
13699 if (buffer > buf_end)
13700 {
13701 overflow = true;
13702 break;
13703 }
13704 break;
13705 case DW_RLE_start_end:
13706 if (buffer + 2 * cu->header.addr_size > buf_end)
13707 {
13708 overflow = true;
13709 break;
13710 }
c8a7a66f
TT
13711 range_beginning = cu->header.read_address (obfd, buffer,
13712 &bytes_read);
43988095 13713 buffer += bytes_read;
c8a7a66f 13714 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13715 buffer += bytes_read;
13716 break;
13717 default:
b98664d3 13718 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13719 return false;
13720 }
13721 if (rlet == DW_RLE_end_of_list || overflow)
13722 break;
13723 if (rlet == DW_RLE_base_address)
13724 continue;
13725
2b24b6e4 13726 if (!base.has_value ())
43988095
JK
13727 {
13728 /* We have no valid base address for the ranges
13729 data. */
b98664d3 13730 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13731 return false;
13732 }
13733
13734 if (range_beginning > range_end)
13735 {
13736 /* Inverted range entries are invalid. */
b98664d3 13737 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13738 return false;
13739 }
13740
13741 /* Empty range entries have no effect. */
13742 if (range_beginning == range_end)
13743 continue;
13744
2b24b6e4
TT
13745 range_beginning += *base;
13746 range_end += *base;
43988095
JK
13747
13748 /* A not-uncommon case of bad debug info.
13749 Don't pollute the addrmap with bad data. */
13750 if (range_beginning + baseaddr == 0
13751 && !dwarf2_per_objfile->has_section_at_zero)
13752 {
b98664d3 13753 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13754 " [in module %s]"), objfile_name (objfile));
13755 continue;
13756 }
13757
13758 callback (range_beginning, range_end);
13759 }
13760
13761 if (overflow)
13762 {
b98664d3 13763 complaint (_("Offset %d is not terminated "
43988095
JK
13764 "for DW_AT_ranges attribute"),
13765 offset);
13766 return false;
13767 }
13768
13769 return true;
13770}
13771
13772/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13773 Callback's type should be:
13774 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13775 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13776
43988095 13777template <typename Callback>
43039443 13778static int
5f46c5a5 13779dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13780 Callback &&callback)
43039443 13781{
ed2dc618 13782 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13783 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13784 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13785 struct comp_unit_head *cu_header = &cu->header;
13786 bfd *obfd = objfile->obfd;
13787 unsigned int addr_size = cu_header->addr_size;
13788 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13789 /* Base address selection entry. */
2b24b6e4 13790 gdb::optional<CORE_ADDR> base;
43039443 13791 unsigned int dummy;
d521ce57 13792 const gdb_byte *buffer;
ff013f42 13793 CORE_ADDR baseaddr;
43039443 13794
43988095
JK
13795 if (cu_header->version >= 5)
13796 return dwarf2_rnglists_process (offset, cu, callback);
13797
d00adf39 13798 base = cu->base_address;
43039443 13799
96b79293 13800 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13801 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13802 {
b98664d3 13803 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13804 offset);
13805 return 0;
13806 }
dce234bc 13807 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13808
b3b3bada 13809 baseaddr = objfile->text_section_offset ();
ff013f42 13810
43039443
JK
13811 while (1)
13812 {
13813 CORE_ADDR range_beginning, range_end;
13814
c8a7a66f 13815 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13816 buffer += addr_size;
c8a7a66f 13817 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13818 buffer += addr_size;
13819 offset += 2 * addr_size;
13820
13821 /* An end of list marker is a pair of zero addresses. */
13822 if (range_beginning == 0 && range_end == 0)
13823 /* Found the end of list entry. */
13824 break;
13825
13826 /* Each base address selection entry is a pair of 2 values.
13827 The first is the largest possible address, the second is
13828 the base address. Check for a base address here. */
13829 if ((range_beginning & mask) == mask)
13830 {
28d2bfb9
AB
13831 /* If we found the largest possible address, then we already
13832 have the base address in range_end. */
13833 base = range_end;
43039443
JK
13834 continue;
13835 }
13836
2b24b6e4 13837 if (!base.has_value ())
43039443
JK
13838 {
13839 /* We have no valid base address for the ranges
13840 data. */
b98664d3 13841 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13842 return 0;
13843 }
13844
9277c30c
UW
13845 if (range_beginning > range_end)
13846 {
13847 /* Inverted range entries are invalid. */
b98664d3 13848 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13849 return 0;
13850 }
13851
13852 /* Empty range entries have no effect. */
13853 if (range_beginning == range_end)
13854 continue;
13855
2b24b6e4
TT
13856 range_beginning += *base;
13857 range_end += *base;
43039443 13858
01093045
DE
13859 /* A not-uncommon case of bad debug info.
13860 Don't pollute the addrmap with bad data. */
13861 if (range_beginning + baseaddr == 0
13862 && !dwarf2_per_objfile->has_section_at_zero)
13863 {
b98664d3 13864 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13865 " [in module %s]"), objfile_name (objfile));
01093045
DE
13866 continue;
13867 }
13868
5f46c5a5
JK
13869 callback (range_beginning, range_end);
13870 }
13871
13872 return 1;
13873}
13874
13875/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13876 Return 1 if the attributes are present and valid, otherwise, return 0.
13877 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13878
13879static int
13880dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13881 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13882 dwarf2_psymtab *ranges_pst)
5f46c5a5 13883{
518817b3 13884 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13885 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13886 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13887 int low_set = 0;
13888 CORE_ADDR low = 0;
13889 CORE_ADDR high = 0;
13890 int retval;
13891
13892 retval = dwarf2_ranges_process (offset, cu,
13893 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13894 {
9277c30c 13895 if (ranges_pst != NULL)
3e29f34a
MR
13896 {
13897 CORE_ADDR lowpc;
13898 CORE_ADDR highpc;
13899
79748972
TT
13900 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13901 range_beginning + baseaddr)
13902 - baseaddr);
13903 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13904 range_end + baseaddr)
13905 - baseaddr);
d320c2b5
TT
13906 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13907 lowpc, highpc - 1, ranges_pst);
3e29f34a 13908 }
ff013f42 13909
43039443
JK
13910 /* FIXME: This is recording everything as a low-high
13911 segment of consecutive addresses. We should have a
13912 data structure for discontiguous block ranges
13913 instead. */
13914 if (! low_set)
13915 {
13916 low = range_beginning;
13917 high = range_end;
13918 low_set = 1;
13919 }
13920 else
13921 {
13922 if (range_beginning < low)
13923 low = range_beginning;
13924 if (range_end > high)
13925 high = range_end;
13926 }
5f46c5a5
JK
13927 });
13928 if (!retval)
13929 return 0;
43039443
JK
13930
13931 if (! low_set)
13932 /* If the first entry is an end-of-list marker, the range
13933 describes an empty scope, i.e. no instructions. */
13934 return 0;
13935
13936 if (low_return)
13937 *low_return = low;
13938 if (high_return)
13939 *high_return = high;
13940 return 1;
13941}
13942
3a2b436a
JK
13943/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13944 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13945 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13946
3a2b436a 13947static enum pc_bounds_kind
af34e669 13948dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13949 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13950 dwarf2_psymtab *pst)
c906108c 13951{
518817b3
SM
13952 struct dwarf2_per_objfile *dwarf2_per_objfile
13953 = cu->per_cu->dwarf2_per_objfile;
c906108c 13954 struct attribute *attr;
91da1414 13955 struct attribute *attr_high;
af34e669
DJ
13956 CORE_ADDR low = 0;
13957 CORE_ADDR high = 0;
e385593e 13958 enum pc_bounds_kind ret;
c906108c 13959
91da1414
MW
13960 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13961 if (attr_high)
af34e669 13962 {
e142c38c 13963 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13964 if (attr != nullptr)
91da1414 13965 {
cd6c91b4
TT
13966 low = attr->value_as_address ();
13967 high = attr_high->value_as_address ();
13968 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13969 high += low;
91da1414 13970 }
af34e669
DJ
13971 else
13972 /* Found high w/o low attribute. */
e385593e 13973 return PC_BOUNDS_INVALID;
af34e669
DJ
13974
13975 /* Found consecutive range of addresses. */
3a2b436a 13976 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13977 }
c906108c 13978 else
af34e669 13979 {
e142c38c 13980 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13981 if (attr != NULL)
13982 {
18a8505e 13983 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13984 We take advantage of the fact that DW_AT_ranges does not appear
13985 in DW_TAG_compile_unit of DWO files. */
13986 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13987 unsigned int ranges_offset = (DW_UNSND (attr)
13988 + (need_ranges_base
13989 ? cu->ranges_base
13990 : 0));
2e3cf129 13991
af34e669 13992 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13993 .debug_ranges section. */
2e3cf129 13994 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13995 return PC_BOUNDS_INVALID;
43039443 13996 /* Found discontinuous range of addresses. */
3a2b436a 13997 ret = PC_BOUNDS_RANGES;
af34e669 13998 }
e385593e
JK
13999 else
14000 return PC_BOUNDS_NOT_PRESENT;
af34e669 14001 }
c906108c 14002
48fbe735 14003 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14004 if (high <= low)
e385593e 14005 return PC_BOUNDS_INVALID;
c906108c
SS
14006
14007 /* When using the GNU linker, .gnu.linkonce. sections are used to
14008 eliminate duplicate copies of functions and vtables and such.
14009 The linker will arbitrarily choose one and discard the others.
14010 The AT_*_pc values for such functions refer to local labels in
14011 these sections. If the section from that file was discarded, the
14012 labels are not in the output, so the relocs get a value of 0.
14013 If this is a discarded function, mark the pc bounds as invalid,
14014 so that GDB will ignore it. */
72dca2f5 14015 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14016 return PC_BOUNDS_INVALID;
c906108c
SS
14017
14018 *lowpc = low;
96408a79
SA
14019 if (highpc)
14020 *highpc = high;
af34e669 14021 return ret;
c906108c
SS
14022}
14023
b084d499
JB
14024/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14025 its low and high PC addresses. Do nothing if these addresses could not
14026 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14027 and HIGHPC to the high address if greater than HIGHPC. */
14028
14029static void
14030dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14032 struct dwarf2_cu *cu)
14033{
14034 CORE_ADDR low, high;
14035 struct die_info *child = die->child;
14036
e385593e 14037 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14038 {
325fac50
PA
14039 *lowpc = std::min (*lowpc, low);
14040 *highpc = std::max (*highpc, high);
b084d499
JB
14041 }
14042
14043 /* If the language does not allow nested subprograms (either inside
14044 subprograms or lexical blocks), we're done. */
14045 if (cu->language != language_ada)
14046 return;
6e70227d 14047
b084d499
JB
14048 /* Check all the children of the given DIE. If it contains nested
14049 subprograms, then check their pc bounds. Likewise, we need to
14050 check lexical blocks as well, as they may also contain subprogram
14051 definitions. */
14052 while (child && child->tag)
14053 {
14054 if (child->tag == DW_TAG_subprogram
14055 || child->tag == DW_TAG_lexical_block)
14056 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14057 child = child->sibling;
b084d499
JB
14058 }
14059}
14060
fae299cd
DC
14061/* Get the low and high pc's represented by the scope DIE, and store
14062 them in *LOWPC and *HIGHPC. If the correct values can't be
14063 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14064
14065static void
14066get_scope_pc_bounds (struct die_info *die,
14067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14068 struct dwarf2_cu *cu)
14069{
14070 CORE_ADDR best_low = (CORE_ADDR) -1;
14071 CORE_ADDR best_high = (CORE_ADDR) 0;
14072 CORE_ADDR current_low, current_high;
14073
3a2b436a 14074 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14075 >= PC_BOUNDS_RANGES)
fae299cd
DC
14076 {
14077 best_low = current_low;
14078 best_high = current_high;
14079 }
14080 else
14081 {
14082 struct die_info *child = die->child;
14083
14084 while (child && child->tag)
14085 {
14086 switch (child->tag) {
14087 case DW_TAG_subprogram:
b084d499 14088 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14089 break;
14090 case DW_TAG_namespace:
f55ee35c 14091 case DW_TAG_module:
fae299cd
DC
14092 /* FIXME: carlton/2004-01-16: Should we do this for
14093 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14094 that current GCC's always emit the DIEs corresponding
14095 to definitions of methods of classes as children of a
14096 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14097 the DIEs giving the declarations, which could be
14098 anywhere). But I don't see any reason why the
14099 standards says that they have to be there. */
14100 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14101
14102 if (current_low != ((CORE_ADDR) -1))
14103 {
325fac50
PA
14104 best_low = std::min (best_low, current_low);
14105 best_high = std::max (best_high, current_high);
fae299cd
DC
14106 }
14107 break;
14108 default:
0963b4bd 14109 /* Ignore. */
fae299cd
DC
14110 break;
14111 }
14112
436c571c 14113 child = child->sibling;
fae299cd
DC
14114 }
14115 }
14116
14117 *lowpc = best_low;
14118 *highpc = best_high;
14119}
14120
801e3a5b
JB
14121/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14122 in DIE. */
380bca97 14123
801e3a5b
JB
14124static void
14125dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14126 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14127{
518817b3 14128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14129 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14130 struct attribute *attr;
91da1414 14131 struct attribute *attr_high;
801e3a5b 14132
91da1414
MW
14133 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14134 if (attr_high)
801e3a5b 14135 {
801e3a5b 14136 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14137 if (attr != nullptr)
801e3a5b 14138 {
cd6c91b4
TT
14139 CORE_ADDR low = attr->value_as_address ();
14140 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14141
cd6c91b4 14142 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14143 high += low;
9a619af0 14144
3e29f34a
MR
14145 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14146 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14147 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14148 }
14149 }
14150
14151 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14152 if (attr != nullptr)
801e3a5b 14153 {
18a8505e 14154 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14155 We take advantage of the fact that DW_AT_ranges does not appear
14156 in DW_TAG_compile_unit of DWO files. */
14157 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14158
14159 /* The value of the DW_AT_ranges attribute is the offset of the
14160 address range list in the .debug_ranges section. */
ab435259
DE
14161 unsigned long offset = (DW_UNSND (attr)
14162 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14163
2d5f09ec 14164 std::vector<blockrange> blockvec;
5f46c5a5
JK
14165 dwarf2_ranges_process (offset, cu,
14166 [&] (CORE_ADDR start, CORE_ADDR end)
14167 {
58fdfd2c
JK
14168 start += baseaddr;
14169 end += baseaddr;
5f46c5a5
JK
14170 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14171 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14172 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14173 blockvec.emplace_back (start, end);
5f46c5a5 14174 });
2d5f09ec
KB
14175
14176 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14177 }
14178}
14179
685b1105
JK
14180/* Check whether the producer field indicates either of GCC < 4.6, or the
14181 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14182
685b1105
JK
14183static void
14184check_producer (struct dwarf2_cu *cu)
60d5a603 14185{
38360086 14186 int major, minor;
60d5a603
JK
14187
14188 if (cu->producer == NULL)
14189 {
14190 /* For unknown compilers expect their behavior is DWARF version
14191 compliant.
14192
14193 GCC started to support .debug_types sections by -gdwarf-4 since
14194 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14195 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14196 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14197 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14198 }
b1ffba5a 14199 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14200 {
38360086
MW
14201 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14202 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14203 }
5230b05a 14204 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14205 {
14206 cu->producer_is_icc = true;
14207 cu->producer_is_icc_lt_14 = major < 14;
14208 }
c258c396
JD
14209 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14210 cu->producer_is_codewarrior = true;
685b1105
JK
14211 else
14212 {
14213 /* For other non-GCC compilers, expect their behavior is DWARF version
14214 compliant. */
60d5a603
JK
14215 }
14216
9068261f 14217 cu->checked_producer = true;
685b1105 14218}
ba919b58 14219
685b1105
JK
14220/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14221 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14222 during 4.6.0 experimental. */
14223
9068261f 14224static bool
685b1105
JK
14225producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14226{
14227 if (!cu->checked_producer)
14228 check_producer (cu);
14229
14230 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14231}
14232
c258c396
JD
14233
14234/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14235 with incorrect is_stmt attributes. */
14236
14237static bool
14238producer_is_codewarrior (struct dwarf2_cu *cu)
14239{
14240 if (!cu->checked_producer)
14241 check_producer (cu);
14242
14243 return cu->producer_is_codewarrior;
14244}
14245
405feb71 14246/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14247 DW_AT_accessibility. */
14248
14249static enum dwarf_access_attribute
14250dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14251{
14252 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14253 {
14254 /* The default DWARF 2 accessibility for members is public, the default
14255 accessibility for inheritance is private. */
14256
14257 if (die->tag != DW_TAG_inheritance)
14258 return DW_ACCESS_public;
14259 else
14260 return DW_ACCESS_private;
14261 }
14262 else
14263 {
14264 /* DWARF 3+ defines the default accessibility a different way. The same
14265 rules apply now for DW_TAG_inheritance as for the members and it only
14266 depends on the container kind. */
14267
14268 if (die->parent->tag == DW_TAG_class_type)
14269 return DW_ACCESS_private;
14270 else
14271 return DW_ACCESS_public;
14272 }
14273}
14274
74ac6d43
TT
14275/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14276 offset. If the attribute was not found return 0, otherwise return
14277 1. If it was found but could not properly be handled, set *OFFSET
14278 to 0. */
14279
14280static int
14281handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14282 LONGEST *offset)
14283{
14284 struct attribute *attr;
14285
14286 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14287 if (attr != NULL)
14288 {
14289 *offset = 0;
14290
14291 /* Note that we do not check for a section offset first here.
14292 This is because DW_AT_data_member_location is new in DWARF 4,
14293 so if we see it, we can assume that a constant form is really
14294 a constant and not a section offset. */
cd6c91b4 14295 if (attr->form_is_constant ())
0826b30a 14296 *offset = attr->constant_value (0);
cd6c91b4 14297 else if (attr->form_is_section_offset ())
74ac6d43 14298 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14299 else if (attr->form_is_block ())
74ac6d43
TT
14300 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14301 else
14302 dwarf2_complex_location_expr_complaint ();
14303
14304 return 1;
14305 }
14306
14307 return 0;
14308}
14309
7d79de9a
TT
14310/* Look for DW_AT_data_member_location and store the results in FIELD. */
14311
14312static void
14313handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14314 struct field *field)
14315{
14316 struct attribute *attr;
14317
14318 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14319 if (attr != NULL)
14320 {
14321 if (attr->form_is_constant ())
14322 {
14323 LONGEST offset = attr->constant_value (0);
14324 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14325 }
14326 else if (attr->form_is_section_offset ())
14327 dwarf2_complex_location_expr_complaint ();
14328 else if (attr->form_is_block ())
14329 {
14330 bool handled;
14331 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14332 if (handled)
14333 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14334 else
14335 {
14336 struct objfile *objfile
14337 = cu->per_cu->dwarf2_per_objfile->objfile;
14338 struct dwarf2_locexpr_baton *dlbaton
14339 = XOBNEW (&objfile->objfile_obstack,
14340 struct dwarf2_locexpr_baton);
14341 dlbaton->data = DW_BLOCK (attr)->data;
14342 dlbaton->size = DW_BLOCK (attr)->size;
14343 /* When using this baton, we want to compute the address
14344 of the field, not the value. This is why
14345 is_reference is set to false here. */
14346 dlbaton->is_reference = false;
14347 dlbaton->per_cu = cu->per_cu;
14348
14349 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14350 }
14351 }
14352 else
14353 dwarf2_complex_location_expr_complaint ();
14354 }
14355}
14356
c906108c
SS
14357/* Add an aggregate field to the field list. */
14358
14359static void
107d2387 14360dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14361 struct dwarf2_cu *cu)
6e70227d 14362{
518817b3 14363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14364 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14365 struct nextfield *new_field;
14366 struct attribute *attr;
14367 struct field *fp;
15d034d0 14368 const char *fieldname = "";
c906108c 14369
7d0ccb61
DJ
14370 if (die->tag == DW_TAG_inheritance)
14371 {
be2daae6
TT
14372 fip->baseclasses.emplace_back ();
14373 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14374 }
14375 else
14376 {
be2daae6
TT
14377 fip->fields.emplace_back ();
14378 new_field = &fip->fields.back ();
7d0ccb61 14379 }
be2daae6 14380
9c6a1327
TT
14381 new_field->offset = die->sect_off;
14382
e142c38c 14383 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14384 if (attr != nullptr)
c906108c 14385 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14386 else
14387 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14388 if (new_field->accessibility != DW_ACCESS_public)
14389 fip->non_public_fields = 1;
60d5a603 14390
e142c38c 14391 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14392 if (attr != nullptr)
c906108c 14393 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14394 else
14395 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14396
14397 fp = &new_field->field;
a9a9bd0f 14398
e142c38c 14399 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14400 {
a9a9bd0f 14401 /* Data member other than a C++ static data member. */
6e70227d 14402
c906108c 14403 /* Get type of field. */
e7c27a73 14404 fp->type = die_type (die, cu);
c906108c 14405
d6a843b5 14406 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14407
c906108c 14408 /* Get bit size of field (zero if none). */
e142c38c 14409 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14410 if (attr != nullptr)
c906108c
SS
14411 {
14412 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14413 }
14414 else
14415 {
14416 FIELD_BITSIZE (*fp) = 0;
14417 }
14418
14419 /* Get bit offset of field. */
7d79de9a 14420 handle_data_member_location (die, cu, fp);
e142c38c 14421 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14422 if (attr != nullptr)
c906108c 14423 {
d5a22e77 14424 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14425 {
14426 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14427 additional bit offset from the MSB of the containing
14428 anonymous object to the MSB of the field. We don't
14429 have to do anything special since we don't need to
14430 know the size of the anonymous object. */
f41f5e61 14431 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14432 }
14433 else
14434 {
14435 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14436 MSB of the anonymous object, subtract off the number of
14437 bits from the MSB of the field to the MSB of the
14438 object, and then subtract off the number of bits of
14439 the field itself. The result is the bit offset of
14440 the LSB of the field. */
c906108c
SS
14441 int anonymous_size;
14442 int bit_offset = DW_UNSND (attr);
14443
e142c38c 14444 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14445 if (attr != nullptr)
c906108c
SS
14446 {
14447 /* The size of the anonymous object containing
14448 the bit field is explicit, so use the
14449 indicated size (in bytes). */
14450 anonymous_size = DW_UNSND (attr);
14451 }
14452 else
14453 {
14454 /* The size of the anonymous object containing
14455 the bit field must be inferred from the type
14456 attribute of the data member containing the
14457 bit field. */
14458 anonymous_size = TYPE_LENGTH (fp->type);
14459 }
f41f5e61
PA
14460 SET_FIELD_BITPOS (*fp,
14461 (FIELD_BITPOS (*fp)
14462 + anonymous_size * bits_per_byte
14463 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14464 }
14465 }
da5b30da
AA
14466 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14467 if (attr != NULL)
14468 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14469 + attr->constant_value (0)));
c906108c
SS
14470
14471 /* Get name of field. */
39cbfefa
DJ
14472 fieldname = dwarf2_name (die, cu);
14473 if (fieldname == NULL)
14474 fieldname = "";
d8151005
DJ
14475
14476 /* The name is already allocated along with this objfile, so we don't
14477 need to duplicate it for the type. */
14478 fp->name = fieldname;
c906108c
SS
14479
14480 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14481 pointer or virtual base class pointer) to private. */
e142c38c 14482 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14483 {
d48cc9dd 14484 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14485 new_field->accessibility = DW_ACCESS_private;
14486 fip->non_public_fields = 1;
14487 }
14488 }
a9a9bd0f 14489 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14490 {
a9a9bd0f
DC
14491 /* C++ static member. */
14492
14493 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14494 is a declaration, but all versions of G++ as of this writing
14495 (so through at least 3.2.1) incorrectly generate
14496 DW_TAG_variable tags. */
6e70227d 14497
ff355380 14498 const char *physname;
c906108c 14499
a9a9bd0f 14500 /* Get name of field. */
39cbfefa
DJ
14501 fieldname = dwarf2_name (die, cu);
14502 if (fieldname == NULL)
c906108c
SS
14503 return;
14504
254e6b9e 14505 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14506 if (attr
14507 /* Only create a symbol if this is an external value.
14508 new_symbol checks this and puts the value in the global symbol
14509 table, which we want. If it is not external, new_symbol
14510 will try to put the value in cu->list_in_scope which is wrong. */
14511 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14512 {
14513 /* A static const member, not much different than an enum as far as
14514 we're concerned, except that we can support more types. */
14515 new_symbol (die, NULL, cu);
14516 }
14517
2df3850c 14518 /* Get physical name. */
ff355380 14519 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14520
d8151005
DJ
14521 /* The name is already allocated along with this objfile, so we don't
14522 need to duplicate it for the type. */
14523 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14524 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14525 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14526 }
14527 else if (die->tag == DW_TAG_inheritance)
14528 {
74ac6d43 14529 /* C++ base class field. */
7d79de9a 14530 handle_data_member_location (die, cu, fp);
c906108c 14531 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14532 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14533 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14534 }
2ddeaf8a
TT
14535 else
14536 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14537}
14538
883fd55a
KS
14539/* Can the type given by DIE define another type? */
14540
14541static bool
14542type_can_define_types (const struct die_info *die)
14543{
14544 switch (die->tag)
14545 {
14546 case DW_TAG_typedef:
14547 case DW_TAG_class_type:
14548 case DW_TAG_structure_type:
14549 case DW_TAG_union_type:
14550 case DW_TAG_enumeration_type:
14551 return true;
14552
14553 default:
14554 return false;
14555 }
14556}
14557
14558/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14559
14560static void
883fd55a
KS
14561dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14562 struct dwarf2_cu *cu)
6e70227d 14563{
be2daae6
TT
14564 struct decl_field fp;
14565 memset (&fp, 0, sizeof (fp));
98751a41 14566
883fd55a 14567 gdb_assert (type_can_define_types (die));
98751a41 14568
883fd55a 14569 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14570 fp.name = dwarf2_name (die, cu);
14571 fp.type = read_type_die (die, cu);
98751a41 14572
c191a687
KS
14573 /* Save accessibility. */
14574 enum dwarf_access_attribute accessibility;
14575 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14576 if (attr != NULL)
14577 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14578 else
14579 accessibility = dwarf2_default_access_attribute (die, cu);
14580 switch (accessibility)
14581 {
14582 case DW_ACCESS_public:
14583 /* The assumed value if neither private nor protected. */
14584 break;
14585 case DW_ACCESS_private:
be2daae6 14586 fp.is_private = 1;
c191a687
KS
14587 break;
14588 case DW_ACCESS_protected:
be2daae6 14589 fp.is_protected = 1;
c191a687
KS
14590 break;
14591 default:
b98664d3 14592 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14593 }
14594
883fd55a 14595 if (die->tag == DW_TAG_typedef)
be2daae6 14596 fip->typedef_field_list.push_back (fp);
883fd55a 14597 else
be2daae6 14598 fip->nested_types_list.push_back (fp);
98751a41
JK
14599}
14600
9c6a1327
TT
14601/* A convenience typedef that's used when finding the discriminant
14602 field for a variant part. */
1b95cdb7
SM
14603typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14604 offset_map_type;
9c6a1327
TT
14605
14606/* Compute the discriminant range for a given variant. OBSTACK is
14607 where the results will be stored. VARIANT is the variant to
14608 process. IS_UNSIGNED indicates whether the discriminant is signed
14609 or unsigned. */
14610
14611static const gdb::array_view<discriminant_range>
14612convert_variant_range (struct obstack *obstack, const variant_field &variant,
14613 bool is_unsigned)
14614{
14615 std::vector<discriminant_range> ranges;
14616
14617 if (variant.default_branch)
14618 return {};
14619
14620 if (variant.discr_list_data == nullptr)
14621 {
14622 discriminant_range r
14623 = {variant.discriminant_value, variant.discriminant_value};
14624 ranges.push_back (r);
14625 }
14626 else
14627 {
14628 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14629 variant.discr_list_data->size);
14630 while (!data.empty ())
14631 {
14632 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14633 {
14634 complaint (_("invalid discriminant marker: %d"), data[0]);
14635 break;
14636 }
14637 bool is_range = data[0] == DW_DSC_range;
14638 data = data.slice (1);
14639
14640 ULONGEST low, high;
14641 unsigned int bytes_read;
14642
14643 if (data.empty ())
14644 {
14645 complaint (_("DW_AT_discr_list missing low value"));
14646 break;
14647 }
14648 if (is_unsigned)
14649 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14650 else
14651 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14652 &bytes_read);
14653 data = data.slice (bytes_read);
14654
14655 if (is_range)
14656 {
14657 if (data.empty ())
14658 {
14659 complaint (_("DW_AT_discr_list missing high value"));
14660 break;
14661 }
14662 if (is_unsigned)
14663 high = read_unsigned_leb128 (nullptr, data.data (),
14664 &bytes_read);
14665 else
14666 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14667 &bytes_read);
14668 data = data.slice (bytes_read);
14669 }
14670 else
14671 high = low;
14672
14673 ranges.push_back ({ low, high });
14674 }
14675 }
14676
14677 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14678 ranges.size ());
14679 std::copy (ranges.begin (), ranges.end (), result);
14680 return gdb::array_view<discriminant_range> (result, ranges.size ());
14681}
14682
14683static const gdb::array_view<variant_part> create_variant_parts
14684 (struct obstack *obstack,
14685 const offset_map_type &offset_map,
14686 struct field_info *fi,
14687 const std::vector<variant_part_builder> &variant_parts);
14688
14689/* Fill in a "struct variant" for a given variant field. RESULT is
14690 the variant to fill in. OBSTACK is where any needed allocations
14691 will be done. OFFSET_MAP holds the mapping from section offsets to
14692 fields for the type. FI describes the fields of the type we're
14693 processing. FIELD is the variant field we're converting. */
14694
14695static void
14696create_one_variant (variant &result, struct obstack *obstack,
14697 const offset_map_type &offset_map,
14698 struct field_info *fi, const variant_field &field)
14699{
14700 result.discriminants = convert_variant_range (obstack, field, false);
14701 result.first_field = field.first_field + fi->baseclasses.size ();
14702 result.last_field = field.last_field + fi->baseclasses.size ();
14703 result.parts = create_variant_parts (obstack, offset_map, fi,
14704 field.variant_parts);
14705}
14706
14707/* Fill in a "struct variant_part" for a given variant part. RESULT
14708 is the variant part to fill in. OBSTACK is where any needed
14709 allocations will be done. OFFSET_MAP holds the mapping from
14710 section offsets to fields for the type. FI describes the fields of
14711 the type we're processing. BUILDER is the variant part to be
14712 converted. */
14713
14714static void
14715create_one_variant_part (variant_part &result,
14716 struct obstack *obstack,
14717 const offset_map_type &offset_map,
14718 struct field_info *fi,
14719 const variant_part_builder &builder)
14720{
14721 auto iter = offset_map.find (builder.discriminant_offset);
14722 if (iter == offset_map.end ())
14723 {
14724 result.discriminant_index = -1;
14725 /* Doesn't matter. */
14726 result.is_unsigned = false;
14727 }
14728 else
14729 {
14730 result.discriminant_index = iter->second;
14731 result.is_unsigned
14732 = TYPE_UNSIGNED (FIELD_TYPE
14733 (fi->fields[result.discriminant_index].field));
14734 }
14735
14736 size_t n = builder.variants.size ();
14737 variant *output = new (obstack) variant[n];
14738 for (size_t i = 0; i < n; ++i)
14739 create_one_variant (output[i], obstack, offset_map, fi,
14740 builder.variants[i]);
14741
14742 result.variants = gdb::array_view<variant> (output, n);
14743}
14744
14745/* Create a vector of variant parts that can be attached to a type.
14746 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14747 holds the mapping from section offsets to fields for the type. FI
14748 describes the fields of the type we're processing. VARIANT_PARTS
14749 is the vector to convert. */
14750
14751static const gdb::array_view<variant_part>
14752create_variant_parts (struct obstack *obstack,
14753 const offset_map_type &offset_map,
14754 struct field_info *fi,
14755 const std::vector<variant_part_builder> &variant_parts)
14756{
14757 if (variant_parts.empty ())
14758 return {};
14759
14760 size_t n = variant_parts.size ();
14761 variant_part *result = new (obstack) variant_part[n];
14762 for (size_t i = 0; i < n; ++i)
14763 create_one_variant_part (result[i], obstack, offset_map, fi,
14764 variant_parts[i]);
14765
14766 return gdb::array_view<variant_part> (result, n);
14767}
14768
14769/* Compute the variant part vector for FIP, attaching it to TYPE when
14770 done. */
14771
14772static void
14773add_variant_property (struct field_info *fip, struct type *type,
14774 struct dwarf2_cu *cu)
14775{
14776 /* Map section offsets of fields to their field index. Note the
14777 field index here does not take the number of baseclasses into
14778 account. */
14779 offset_map_type offset_map;
14780 for (int i = 0; i < fip->fields.size (); ++i)
14781 offset_map[fip->fields[i].offset] = i;
14782
14783 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14784 gdb::array_view<variant_part> parts
14785 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14786 fip->variant_parts);
14787
14788 struct dynamic_prop prop;
14789 prop.kind = PROP_VARIANT_PARTS;
14790 prop.data.variant_parts
14791 = ((gdb::array_view<variant_part> *)
14792 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14793
5c54719c 14794 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14795}
14796
c906108c
SS
14797/* Create the vector of fields, and attach it to the type. */
14798
14799static void
fba45db2 14800dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14801 struct dwarf2_cu *cu)
c906108c 14802{
317f7127 14803 int nfields = fip->nfields ();
c906108c
SS
14804
14805 /* Record the field count, allocate space for the array of fields,
14806 and create blank accessibility bitfields if necessary. */
5e33d5f4 14807 type->set_num_fields (nfields);
c906108c 14808 TYPE_FIELDS (type) = (struct field *)
be2daae6 14809 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14810
b4ba55a1 14811 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14812 {
14813 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14814
14815 TYPE_FIELD_PRIVATE_BITS (type) =
14816 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14817 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14818
14819 TYPE_FIELD_PROTECTED_BITS (type) =
14820 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14821 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14822
774b6a14
TT
14823 TYPE_FIELD_IGNORE_BITS (type) =
14824 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14825 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14826 }
14827
14828 /* If the type has baseclasses, allocate and clear a bit vector for
14829 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14830 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14831 {
be2daae6 14832 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14833 unsigned char *pointer;
c906108c
SS
14834
14835 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14836 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14837 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14838 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14839 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14840 }
14841
9c6a1327
TT
14842 if (!fip->variant_parts.empty ())
14843 add_variant_property (fip, type, cu);
2ddeaf8a 14844
be2daae6
TT
14845 /* Copy the saved-up fields into the field vector. */
14846 for (int i = 0; i < nfields; ++i)
c906108c 14847 {
be2daae6
TT
14848 struct nextfield &field
14849 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14850 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14851
be2daae6
TT
14852 TYPE_FIELD (type, i) = field.field;
14853 switch (field.accessibility)
c906108c 14854 {
c5aa993b 14855 case DW_ACCESS_private:
b4ba55a1 14856 if (cu->language != language_ada)
be2daae6 14857 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14858 break;
c906108c 14859
c5aa993b 14860 case DW_ACCESS_protected:
b4ba55a1 14861 if (cu->language != language_ada)
be2daae6 14862 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14863 break;
c906108c 14864
c5aa993b
JM
14865 case DW_ACCESS_public:
14866 break;
c906108c 14867
c5aa993b
JM
14868 default:
14869 /* Unknown accessibility. Complain and treat it as public. */
14870 {
b98664d3 14871 complaint (_("unsupported accessibility %d"),
be2daae6 14872 field.accessibility);
c5aa993b
JM
14873 }
14874 break;
c906108c 14875 }
be2daae6 14876 if (i < fip->baseclasses.size ())
c906108c 14877 {
be2daae6 14878 switch (field.virtuality)
c906108c 14879 {
c5aa993b
JM
14880 case DW_VIRTUALITY_virtual:
14881 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14882 if (cu->language == language_ada)
a73c6dcd 14883 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14884 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14885 break;
c906108c
SS
14886 }
14887 }
c906108c
SS
14888 }
14889}
14890
7d27a96d
TT
14891/* Return true if this member function is a constructor, false
14892 otherwise. */
14893
14894static int
14895dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14896{
14897 const char *fieldname;
fe978cb0 14898 const char *type_name;
7d27a96d
TT
14899 int len;
14900
14901 if (die->parent == NULL)
14902 return 0;
14903
14904 if (die->parent->tag != DW_TAG_structure_type
14905 && die->parent->tag != DW_TAG_union_type
14906 && die->parent->tag != DW_TAG_class_type)
14907 return 0;
14908
14909 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14910 type_name = dwarf2_name (die->parent, cu);
14911 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14912 return 0;
14913
14914 len = strlen (fieldname);
fe978cb0
PA
14915 return (strncmp (fieldname, type_name, len) == 0
14916 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14917}
14918
e35000a7
TBA
14919/* Check if the given VALUE is a recognized enum
14920 dwarf_defaulted_attribute constant according to DWARF5 spec,
14921 Table 7.24. */
14922
14923static bool
14924is_valid_DW_AT_defaulted (ULONGEST value)
14925{
14926 switch (value)
14927 {
14928 case DW_DEFAULTED_no:
14929 case DW_DEFAULTED_in_class:
14930 case DW_DEFAULTED_out_of_class:
14931 return true;
14932 }
14933
3142e908 14934 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14935 return false;
14936}
14937
c906108c
SS
14938/* Add a member function to the proper fieldlist. */
14939
14940static void
107d2387 14941dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14942 struct type *type, struct dwarf2_cu *cu)
c906108c 14943{
518817b3 14944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14945 struct attribute *attr;
c906108c 14946 int i;
be2daae6 14947 struct fnfieldlist *flp = nullptr;
c906108c 14948 struct fn_field *fnp;
15d034d0 14949 const char *fieldname;
f792889a 14950 struct type *this_type;
60d5a603 14951 enum dwarf_access_attribute accessibility;
c906108c 14952
b4ba55a1 14953 if (cu->language == language_ada)
a73c6dcd 14954 error (_("unexpected member function in Ada type"));
b4ba55a1 14955
2df3850c 14956 /* Get name of member function. */
39cbfefa
DJ
14957 fieldname = dwarf2_name (die, cu);
14958 if (fieldname == NULL)
2df3850c 14959 return;
c906108c 14960
c906108c 14961 /* Look up member function name in fieldlist. */
be2daae6 14962 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14963 {
27bfe10e 14964 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14965 {
14966 flp = &fip->fnfieldlists[i];
14967 break;
14968 }
c906108c
SS
14969 }
14970
be2daae6
TT
14971 /* Create a new fnfieldlist if necessary. */
14972 if (flp == nullptr)
c906108c 14973 {
be2daae6
TT
14974 fip->fnfieldlists.emplace_back ();
14975 flp = &fip->fnfieldlists.back ();
c906108c 14976 flp->name = fieldname;
be2daae6 14977 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14978 }
14979
be2daae6
TT
14980 /* Create a new member function field and add it to the vector of
14981 fnfieldlists. */
14982 flp->fnfields.emplace_back ();
14983 fnp = &flp->fnfields.back ();
3da10d80
KS
14984
14985 /* Delay processing of the physname until later. */
9c37b5ae 14986 if (cu->language == language_cplus)
be2daae6
TT
14987 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14988 die, cu);
3da10d80
KS
14989 else
14990 {
1d06ead6 14991 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14992 fnp->physname = physname ? physname : "";
14993 }
14994
c906108c 14995 fnp->type = alloc_type (objfile);
f792889a 14996 this_type = read_type_die (die, cu);
78134374 14997 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14998 {
f792889a 14999 int nparams = TYPE_NFIELDS (this_type);
c906108c 15000
f792889a 15001 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15002 of the method itself (TYPE_CODE_METHOD). */
15003 smash_to_method_type (fnp->type, type,
f792889a
DJ
15004 TYPE_TARGET_TYPE (this_type),
15005 TYPE_FIELDS (this_type),
15006 TYPE_NFIELDS (this_type),
15007 TYPE_VARARGS (this_type));
c906108c
SS
15008
15009 /* Handle static member functions.
c5aa993b 15010 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15011 member functions. G++ helps GDB by marking the first
15012 parameter for non-static member functions (which is the this
15013 pointer) as artificial. We obtain this information from
15014 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15015 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15016 fnp->voffset = VOFFSET_STATIC;
15017 }
15018 else
b98664d3 15019 complaint (_("member function type missing for '%s'"),
3da10d80 15020 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15021
15022 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15023 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15024 fnp->fcontext = die_containing_type (die, cu);
c906108c 15025
3e43a32a
MS
15026 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15027 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15028
15029 /* Get accessibility. */
e142c38c 15030 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15031 if (attr != nullptr)
aead7601 15032 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15033 else
15034 accessibility = dwarf2_default_access_attribute (die, cu);
15035 switch (accessibility)
c906108c 15036 {
60d5a603
JK
15037 case DW_ACCESS_private:
15038 fnp->is_private = 1;
15039 break;
15040 case DW_ACCESS_protected:
15041 fnp->is_protected = 1;
15042 break;
c906108c
SS
15043 }
15044
b02dede2 15045 /* Check for artificial methods. */
e142c38c 15046 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15047 if (attr && DW_UNSND (attr) != 0)
15048 fnp->is_artificial = 1;
15049
e35000a7
TBA
15050 /* Check for defaulted methods. */
15051 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15052 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15053 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15054
15055 /* Check for deleted methods. */
15056 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15057 if (attr != nullptr && DW_UNSND (attr) != 0)
15058 fnp->is_deleted = 1;
15059
7d27a96d
TT
15060 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15061
0d564a31 15062 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15063 function. For older versions of GCC, this is an offset in the
15064 appropriate virtual table, as specified by DW_AT_containing_type.
15065 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15066 to the object address. */
15067
e142c38c 15068 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15069 if (attr != nullptr)
8e19ed76 15070 {
4fc6c0d5 15071 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15072 {
aec5aa8b
TT
15073 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15074 {
15075 /* Old-style GCC. */
15076 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15077 }
15078 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15079 || (DW_BLOCK (attr)->size > 1
15080 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15081 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15082 {
aec5aa8b
TT
15083 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15084 if ((fnp->voffset % cu->header.addr_size) != 0)
15085 dwarf2_complex_location_expr_complaint ();
15086 else
15087 fnp->voffset /= cu->header.addr_size;
15088 fnp->voffset += 2;
15089 }
15090 else
15091 dwarf2_complex_location_expr_complaint ();
15092
15093 if (!fnp->fcontext)
7e993ebf
KS
15094 {
15095 /* If there is no `this' field and no DW_AT_containing_type,
15096 we cannot actually find a base class context for the
15097 vtable! */
15098 if (TYPE_NFIELDS (this_type) == 0
15099 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15100 {
b98664d3 15101 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15102 "function \"%s\" (offset %s)"),
15103 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15104 }
15105 else
15106 {
15107 fnp->fcontext
15108 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15109 }
15110 }
aec5aa8b 15111 }
cd6c91b4 15112 else if (attr->form_is_section_offset ())
8e19ed76 15113 {
4d3c2250 15114 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15115 }
15116 else
15117 {
4d3c2250
KB
15118 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15119 fieldname);
8e19ed76 15120 }
0d564a31 15121 }
d48cc9dd
DJ
15122 else
15123 {
15124 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15125 if (attr && DW_UNSND (attr))
15126 {
15127 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15128 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15129 "but the vtable offset is not specified"),
9d8780f0 15130 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15131 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15132 TYPE_CPLUS_DYNAMIC (type) = 1;
15133 }
15134 }
c906108c
SS
15135}
15136
15137/* Create the vector of member function fields, and attach it to the type. */
15138
15139static void
fba45db2 15140dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15141 struct dwarf2_cu *cu)
c906108c 15142{
b4ba55a1 15143 if (cu->language == language_ada)
a73c6dcd 15144 error (_("unexpected member functions in Ada type"));
b4ba55a1 15145
c906108c
SS
15146 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15147 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15148 TYPE_ALLOC (type,
15149 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15150
be2daae6 15151 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15152 {
be2daae6 15153 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15154 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15155
be2daae6
TT
15156 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15157 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15158 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15159 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15160
15161 for (int k = 0; k < nf.fnfields.size (); ++k)
15162 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15163 }
15164
be2daae6 15165 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15166}
15167
1168df01
JB
15168/* Returns non-zero if NAME is the name of a vtable member in CU's
15169 language, zero otherwise. */
15170static int
15171is_vtable_name (const char *name, struct dwarf2_cu *cu)
15172{
15173 static const char vptr[] = "_vptr";
15174
9c37b5ae
TT
15175 /* Look for the C++ form of the vtable. */
15176 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15177 return 1;
15178
15179 return 0;
15180}
15181
c0dd20ea 15182/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15183 functions, with the ABI-specified layout. If TYPE describes
15184 such a structure, smash it into a member function type.
61049d3b
DJ
15185
15186 GCC shouldn't do this; it should just output pointer to member DIEs.
15187 This is GCC PR debug/28767. */
c0dd20ea 15188
0b92b5bb
TT
15189static void
15190quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15191{
09e2d7c7 15192 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15193
15194 /* Check for a structure with no name and two children. */
78134374 15195 if (type->code () != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
0b92b5bb 15196 return;
c0dd20ea
DJ
15197
15198 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15199 if (TYPE_FIELD_NAME (type, 0) == NULL
15200 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15201 || TYPE_FIELD_NAME (type, 1) == NULL
15202 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15203 return;
c0dd20ea
DJ
15204
15205 /* Find the type of the method. */
0b92b5bb 15206 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15207 if (pfn_type == NULL
78134374
SM
15208 || pfn_type->code () != TYPE_CODE_PTR
15209 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15210 return;
c0dd20ea
DJ
15211
15212 /* Look for the "this" argument. */
15213 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15214 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15215 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15216 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15217 return;
c0dd20ea 15218
09e2d7c7 15219 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15220 new_type = alloc_type (objfile);
09e2d7c7 15221 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15222 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15223 TYPE_VARARGS (pfn_type));
0b92b5bb 15224 smash_to_methodptr_type (type, new_type);
c0dd20ea 15225}
1168df01 15226
2b4424c3
TT
15227/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15228 appropriate error checking and issuing complaints if there is a
15229 problem. */
15230
15231static ULONGEST
15232get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15233{
15234 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15235
15236 if (attr == nullptr)
15237 return 0;
15238
cd6c91b4 15239 if (!attr->form_is_constant ())
2b4424c3 15240 {
b98664d3 15241 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15242 " - DIE at %s [in module %s]"),
15243 sect_offset_str (die->sect_off),
15244 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15245 return 0;
15246 }
15247
15248 ULONGEST align;
15249 if (attr->form == DW_FORM_sdata)
15250 {
15251 LONGEST val = DW_SND (attr);
15252 if (val < 0)
15253 {
b98664d3 15254 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15255 " - DIE at %s [in module %s]"),
15256 sect_offset_str (die->sect_off),
15257 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15258 return 0;
15259 }
15260 align = val;
15261 }
15262 else
15263 align = DW_UNSND (attr);
15264
15265 if (align == 0)
15266 {
b98664d3 15267 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15268 " - DIE at %s [in module %s]"),
15269 sect_offset_str (die->sect_off),
15270 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15271 return 0;
15272 }
15273 if ((align & (align - 1)) != 0)
15274 {
b98664d3 15275 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15276 " - DIE at %s [in module %s]"),
15277 sect_offset_str (die->sect_off),
15278 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15279 return 0;
15280 }
15281
15282 return align;
15283}
15284
15285/* If the DIE has a DW_AT_alignment attribute, use its value to set
15286 the alignment for TYPE. */
15287
15288static void
15289maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15290 struct type *type)
15291{
15292 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15293 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15294 " - DIE at %s [in module %s]"),
15295 sect_offset_str (die->sect_off),
15296 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15297}
685b1105 15298
e35000a7
TBA
15299/* Check if the given VALUE is a valid enum dwarf_calling_convention
15300 constant for a type, according to DWARF5 spec, Table 5.5. */
15301
15302static bool
15303is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15304{
15305 switch (value)
15306 {
15307 case DW_CC_normal:
15308 case DW_CC_pass_by_reference:
15309 case DW_CC_pass_by_value:
15310 return true;
15311
15312 default:
15313 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15314 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15315 return false;
15316 }
15317}
15318
d0922fcf
TBA
15319/* Check if the given VALUE is a valid enum dwarf_calling_convention
15320 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15321 also according to GNU-specific values (see include/dwarf2.h). */
15322
15323static bool
15324is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15325{
15326 switch (value)
15327 {
15328 case DW_CC_normal:
15329 case DW_CC_program:
15330 case DW_CC_nocall:
15331 return true;
15332
15333 case DW_CC_GNU_renesas_sh:
15334 case DW_CC_GNU_borland_fastcall_i386:
15335 case DW_CC_GDB_IBM_OpenCL:
15336 return true;
15337
15338 default:
15339 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15340 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15341 return false;
15342 }
15343}
15344
c906108c 15345/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15346 (definition) to create a type for the structure or union. Fill in
15347 the type's name and general properties; the members will not be
83655187
DE
15348 processed until process_structure_scope. A symbol table entry for
15349 the type will also not be done until process_structure_scope (assuming
15350 the type has a name).
c906108c 15351
c767944b
DJ
15352 NOTE: we need to call these functions regardless of whether or not the
15353 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15354 structure or union. This gets the type entered into our set of
83655187 15355 user defined types. */
c906108c 15356
f792889a 15357static struct type *
134d01f1 15358read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15359{
518817b3 15360 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15361 struct type *type;
15362 struct attribute *attr;
15d034d0 15363 const char *name;
c906108c 15364
348e048f
DE
15365 /* If the definition of this type lives in .debug_types, read that type.
15366 Don't follow DW_AT_specification though, that will take us back up
15367 the chain and we want to go down. */
052c8bb8 15368 attr = die->attr (DW_AT_signature);
435d3d88 15369 if (attr != nullptr)
348e048f 15370 {
ac9ec31b 15371 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15372
ac9ec31b 15373 /* The type's CU may not be the same as CU.
02142a6c 15374 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15375 return set_die_type (die, type, cu);
15376 }
15377
c0dd20ea 15378 type = alloc_type (objfile);
c906108c 15379 INIT_CPLUS_SPECIFIC (type);
93311388 15380
39cbfefa
DJ
15381 name = dwarf2_name (die, cu);
15382 if (name != NULL)
c906108c 15383 {
987504bb 15384 if (cu->language == language_cplus
c44af4eb
TT
15385 || cu->language == language_d
15386 || cu->language == language_rust)
63d06c5c 15387 {
15d034d0 15388 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15389
15390 /* dwarf2_full_name might have already finished building the DIE's
15391 type. If so, there is no need to continue. */
15392 if (get_die_type (die, cu) != NULL)
15393 return get_die_type (die, cu);
15394
d0e39ea2 15395 type->set_name (full_name);
63d06c5c
DC
15396 }
15397 else
15398 {
d8151005
DJ
15399 /* The name is already allocated along with this objfile, so
15400 we don't need to duplicate it for the type. */
d0e39ea2 15401 type->set_name (name);
63d06c5c 15402 }
c906108c
SS
15403 }
15404
15405 if (die->tag == DW_TAG_structure_type)
15406 {
67607e24 15407 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15408 }
15409 else if (die->tag == DW_TAG_union_type)
15410 {
67607e24 15411 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15412 }
15413 else
15414 {
67607e24 15415 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15416 }
15417
0cc2414c
TT
15418 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15419 TYPE_DECLARED_CLASS (type) = 1;
15420
e35000a7
TBA
15421 /* Store the calling convention in the type if it's available in
15422 the die. Otherwise the calling convention remains set to
15423 the default value DW_CC_normal. */
15424 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15425 if (attr != nullptr
15426 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15427 {
15428 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15429 TYPE_CPLUS_CALLING_CONVENTION (type)
15430 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15431 }
15432
e142c38c 15433 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15434 if (attr != nullptr)
c906108c 15435 {
cd6c91b4 15436 if (attr->form_is_constant ())
155bfbd3
JB
15437 TYPE_LENGTH (type) = DW_UNSND (attr);
15438 else
15439 {
f8e89861
TT
15440 struct dynamic_prop prop;
15441 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15442 cu->per_cu->addr_type ()))
5c54719c 15443 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15444 TYPE_LENGTH (type) = 0;
15445 }
c906108c
SS
15446 }
15447 else
15448 {
15449 TYPE_LENGTH (type) = 0;
15450 }
15451
2b4424c3
TT
15452 maybe_set_alignment (cu, die, type);
15453
5230b05a 15454 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15455 {
5230b05a
WT
15456 /* ICC<14 does not output the required DW_AT_declaration on
15457 incomplete types, but gives them a size of zero. */
422b1cb0 15458 TYPE_STUB (type) = 1;
685b1105
JK
15459 }
15460 else
15461 TYPE_STUB_SUPPORTED (type) = 1;
15462
dc718098 15463 if (die_is_declaration (die, cu))
876cecd0 15464 TYPE_STUB (type) = 1;
a6c727b2
DJ
15465 else if (attr == NULL && die->child == NULL
15466 && producer_is_realview (cu->producer))
15467 /* RealView does not output the required DW_AT_declaration
15468 on incomplete types. */
15469 TYPE_STUB (type) = 1;
dc718098 15470
c906108c
SS
15471 /* We need to add the type field to the die immediately so we don't
15472 infinitely recurse when dealing with pointers to the structure
0963b4bd 15473 type within the structure itself. */
1c379e20 15474 set_die_type (die, type, cu);
c906108c 15475
7e314c57
JK
15476 /* set_die_type should be already done. */
15477 set_descriptive_type (type, die, cu);
15478
c767944b
DJ
15479 return type;
15480}
15481
9c6a1327
TT
15482static void handle_struct_member_die
15483 (struct die_info *child_die,
15484 struct type *type,
15485 struct field_info *fi,
15486 std::vector<struct symbol *> *template_args,
15487 struct dwarf2_cu *cu);
15488
15489/* A helper for handle_struct_member_die that handles
15490 DW_TAG_variant_part. */
15491
15492static void
15493handle_variant_part (struct die_info *die, struct type *type,
15494 struct field_info *fi,
15495 std::vector<struct symbol *> *template_args,
15496 struct dwarf2_cu *cu)
15497{
15498 variant_part_builder *new_part;
15499 if (fi->current_variant_part == nullptr)
15500 {
15501 fi->variant_parts.emplace_back ();
15502 new_part = &fi->variant_parts.back ();
15503 }
15504 else if (!fi->current_variant_part->processing_variant)
15505 {
15506 complaint (_("nested DW_TAG_variant_part seen "
15507 "- DIE at %s [in module %s]"),
15508 sect_offset_str (die->sect_off),
15509 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15510 return;
15511 }
15512 else
15513 {
15514 variant_field &current = fi->current_variant_part->variants.back ();
15515 current.variant_parts.emplace_back ();
15516 new_part = &current.variant_parts.back ();
15517 }
15518
15519 /* When we recurse, we want callees to add to this new variant
15520 part. */
15521 scoped_restore save_current_variant_part
15522 = make_scoped_restore (&fi->current_variant_part, new_part);
15523
15524 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15525 if (discr == NULL)
15526 {
15527 /* It's a univariant form, an extension we support. */
15528 }
15529 else if (discr->form_is_ref ())
15530 {
15531 struct dwarf2_cu *target_cu = cu;
15532 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15533
15534 new_part->discriminant_offset = target_die->sect_off;
15535 }
15536 else
15537 {
15538 complaint (_("DW_AT_discr does not have DIE reference form"
15539 " - DIE at %s [in module %s]"),
15540 sect_offset_str (die->sect_off),
15541 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15542 }
15543
15544 for (die_info *child_die = die->child;
15545 child_die != NULL;
15546 child_die = child_die->sibling)
15547 handle_struct_member_die (child_die, type, fi, template_args, cu);
15548}
15549
15550/* A helper for handle_struct_member_die that handles
15551 DW_TAG_variant. */
15552
15553static void
15554handle_variant (struct die_info *die, struct type *type,
15555 struct field_info *fi,
15556 std::vector<struct symbol *> *template_args,
15557 struct dwarf2_cu *cu)
15558{
15559 if (fi->current_variant_part == nullptr)
15560 {
15561 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15562 "- DIE at %s [in module %s]"),
15563 sect_offset_str (die->sect_off),
15564 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15565 return;
15566 }
15567 if (fi->current_variant_part->processing_variant)
15568 {
15569 complaint (_("nested DW_TAG_variant seen "
15570 "- DIE at %s [in module %s]"),
15571 sect_offset_str (die->sect_off),
15572 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15573 return;
15574 }
15575
15576 scoped_restore save_processing_variant
15577 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15578 true);
15579
15580 fi->current_variant_part->variants.emplace_back ();
15581 variant_field &variant = fi->current_variant_part->variants.back ();
15582 variant.first_field = fi->fields.size ();
15583
15584 /* In a variant we want to get the discriminant and also add a
15585 field for our sole member child. */
15586 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15587 if (discr == nullptr)
15588 {
15589 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15590 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15591 variant.default_branch = true;
15592 else
15593 variant.discr_list_data = DW_BLOCK (discr);
15594 }
15595 else
15596 variant.discriminant_value = DW_UNSND (discr);
15597
15598 for (die_info *variant_child = die->child;
15599 variant_child != NULL;
15600 variant_child = variant_child->sibling)
15601 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15602
15603 variant.last_field = fi->fields.size ();
15604}
15605
2ddeaf8a
TT
15606/* A helper for process_structure_scope that handles a single member
15607 DIE. */
15608
15609static void
15610handle_struct_member_die (struct die_info *child_die, struct type *type,
15611 struct field_info *fi,
15612 std::vector<struct symbol *> *template_args,
15613 struct dwarf2_cu *cu)
15614{
15615 if (child_die->tag == DW_TAG_member
9c6a1327 15616 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15617 {
15618 /* NOTE: carlton/2002-11-05: A C++ static data member
15619 should be a DW_TAG_member that is a declaration, but
15620 all versions of G++ as of this writing (so through at
15621 least 3.2.1) incorrectly generate DW_TAG_variable
15622 tags for them instead. */
15623 dwarf2_add_field (fi, child_die, cu);
15624 }
15625 else if (child_die->tag == DW_TAG_subprogram)
15626 {
15627 /* Rust doesn't have member functions in the C++ sense.
15628 However, it does emit ordinary functions as children
15629 of a struct DIE. */
15630 if (cu->language == language_rust)
15631 read_func_scope (child_die, cu);
15632 else
15633 {
15634 /* C++ member function. */
15635 dwarf2_add_member_fn (fi, child_die, type, cu);
15636 }
15637 }
15638 else if (child_die->tag == DW_TAG_inheritance)
15639 {
15640 /* C++ base class field. */
15641 dwarf2_add_field (fi, child_die, cu);
15642 }
15643 else if (type_can_define_types (child_die))
15644 dwarf2_add_type_defn (fi, child_die, cu);
15645 else if (child_die->tag == DW_TAG_template_type_param
15646 || child_die->tag == DW_TAG_template_value_param)
15647 {
15648 struct symbol *arg = new_symbol (child_die, NULL, cu);
15649
15650 if (arg != NULL)
15651 template_args->push_back (arg);
15652 }
9c6a1327
TT
15653 else if (child_die->tag == DW_TAG_variant_part)
15654 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15655 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15656 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15657}
15658
c767944b
DJ
15659/* Finish creating a structure or union type, including filling in
15660 its members and creating a symbol for it. */
15661
15662static void
15663process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15664{
518817b3 15665 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15666 struct die_info *child_die;
c767944b
DJ
15667 struct type *type;
15668
15669 type = get_die_type (die, cu);
15670 if (type == NULL)
15671 type = read_structure_type (die, cu);
15672
3e1d3d8c 15673 bool has_template_parameters = false;
e142c38c 15674 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15675 {
15676 struct field_info fi;
2f4732b0 15677 std::vector<struct symbol *> template_args;
c906108c 15678
639d11d3 15679 child_die = die->child;
c906108c
SS
15680
15681 while (child_die && child_die->tag)
15682 {
2ddeaf8a 15683 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15684 child_die = child_die->sibling;
c906108c
SS
15685 }
15686
34eaf542 15687 /* Attach template arguments to type. */
2f4732b0 15688 if (!template_args.empty ())
34eaf542 15689 {
3e1d3d8c 15690 has_template_parameters = true;
34eaf542 15691 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15692 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15693 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15694 = XOBNEWVEC (&objfile->objfile_obstack,
15695 struct symbol *,
15696 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15697 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15698 template_args.data (),
34eaf542
TT
15699 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15700 * sizeof (struct symbol *)));
34eaf542
TT
15701 }
15702
c906108c 15703 /* Attach fields and member functions to the type. */
317f7127 15704 if (fi.nfields () > 0)
e7c27a73 15705 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15706 if (!fi.fnfieldlists.empty ())
c906108c 15707 {
e7c27a73 15708 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15709
c5aa993b 15710 /* Get the type which refers to the base class (possibly this
c906108c 15711 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15712 class from the DW_AT_containing_type attribute. This use of
15713 DW_AT_containing_type is a GNU extension. */
c906108c 15714
e142c38c 15715 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15716 {
e7c27a73 15717 struct type *t = die_containing_type (die, cu);
c906108c 15718
ae6ae975 15719 set_type_vptr_basetype (type, t);
c906108c
SS
15720 if (type == t)
15721 {
c906108c
SS
15722 int i;
15723
15724 /* Our own class provides vtbl ptr. */
15725 for (i = TYPE_NFIELDS (t) - 1;
15726 i >= TYPE_N_BASECLASSES (t);
15727 --i)
15728 {
0d5cff50 15729 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15730
1168df01 15731 if (is_vtable_name (fieldname, cu))
c906108c 15732 {
ae6ae975 15733 set_type_vptr_fieldno (type, i);
c906108c
SS
15734 break;
15735 }
15736 }
15737
15738 /* Complain if virtual function table field not found. */
15739 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15740 complaint (_("virtual function table pointer "
3e43a32a 15741 "not found when defining class '%s'"),
7d93a1e0 15742 type->name () ? type->name () : "");
c906108c
SS
15743 }
15744 else
15745 {
ae6ae975 15746 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15747 }
15748 }
f6235d4c 15749 else if (cu->producer
61012eef 15750 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15751 {
15752 /* The IBM XLC compiler does not provide direct indication
15753 of the containing type, but the vtable pointer is
15754 always named __vfp. */
15755
15756 int i;
15757
15758 for (i = TYPE_NFIELDS (type) - 1;
15759 i >= TYPE_N_BASECLASSES (type);
15760 --i)
15761 {
15762 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15763 {
ae6ae975
DE
15764 set_type_vptr_fieldno (type, i);
15765 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15766 break;
15767 }
15768 }
15769 }
c906108c 15770 }
98751a41
JK
15771
15772 /* Copy fi.typedef_field_list linked list elements content into the
15773 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15774 if (!fi.typedef_field_list.empty ())
98751a41 15775 {
be2daae6 15776 int count = fi.typedef_field_list.size ();
98751a41 15777
a0d7a4ff 15778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15779 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15780 = ((struct decl_field *)
be2daae6
TT
15781 TYPE_ALLOC (type,
15782 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15783 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15784
be2daae6
TT
15785 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15786 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15787 }
c767944b 15788
883fd55a
KS
15789 /* Copy fi.nested_types_list linked list elements content into the
15790 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15791 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15792 {
be2daae6 15793 int count = fi.nested_types_list.size ();
883fd55a
KS
15794
15795 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15796 TYPE_NESTED_TYPES_ARRAY (type)
15797 = ((struct decl_field *)
be2daae6
TT
15798 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15799 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15800
be2daae6
TT
15801 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15802 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15803 }
c906108c 15804 }
63d06c5c 15805
bb5ed363 15806 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15807 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15808 cu->rust_unions.push_back (type);
0b92b5bb 15809
90aeadfc
DC
15810 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15811 snapshots) has been known to create a die giving a declaration
15812 for a class that has, as a child, a die giving a definition for a
15813 nested class. So we have to process our children even if the
15814 current die is a declaration. Normally, of course, a declaration
15815 won't have any children at all. */
134d01f1 15816
ca040673
DE
15817 child_die = die->child;
15818
90aeadfc
DC
15819 while (child_die != NULL && child_die->tag)
15820 {
15821 if (child_die->tag == DW_TAG_member
15822 || child_die->tag == DW_TAG_variable
34eaf542
TT
15823 || child_die->tag == DW_TAG_inheritance
15824 || child_die->tag == DW_TAG_template_value_param
15825 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15826 {
90aeadfc 15827 /* Do nothing. */
134d01f1 15828 }
90aeadfc
DC
15829 else
15830 process_die (child_die, cu);
134d01f1 15831
436c571c 15832 child_die = child_die->sibling;
134d01f1
DJ
15833 }
15834
fa4028e9
JB
15835 /* Do not consider external references. According to the DWARF standard,
15836 these DIEs are identified by the fact that they have no byte_size
15837 attribute, and a declaration attribute. */
15838 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15839 || !die_is_declaration (die, cu)
15840 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15841 {
15842 struct symbol *sym = new_symbol (die, type, cu);
15843
15844 if (has_template_parameters)
15845 {
a776957c
TT
15846 struct symtab *symtab;
15847 if (sym != nullptr)
15848 symtab = symbol_symtab (sym);
15849 else if (cu->line_header != nullptr)
15850 {
15851 /* Any related symtab will do. */
15852 symtab
7ba99d21 15853 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15854 }
15855 else
15856 {
15857 symtab = nullptr;
15858 complaint (_("could not find suitable "
15859 "symtab for template parameter"
15860 " - DIE at %s [in module %s]"),
15861 sect_offset_str (die->sect_off),
15862 objfile_name (objfile));
15863 }
15864
15865 if (symtab != nullptr)
15866 {
15867 /* Make sure that the symtab is set on the new symbols.
15868 Even though they don't appear in this symtab directly,
15869 other parts of gdb assume that symbols do, and this is
15870 reasonably true. */
15871 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15872 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15873 }
3e1d3d8c
TT
15874 }
15875 }
134d01f1
DJ
15876}
15877
ed6acedd
TT
15878/* Assuming DIE is an enumeration type, and TYPE is its associated
15879 type, update TYPE using some information only available in DIE's
15880 children. In particular, the fields are computed. */
55426c9d
JB
15881
15882static void
15883update_enumeration_type_from_children (struct die_info *die,
15884 struct type *type,
15885 struct dwarf2_cu *cu)
15886{
60f7655a 15887 struct die_info *child_die;
55426c9d
JB
15888 int unsigned_enum = 1;
15889 int flag_enum = 1;
55426c9d 15890
8268c778 15891 auto_obstack obstack;
ed6acedd 15892 std::vector<struct field> fields;
55426c9d 15893
60f7655a
DE
15894 for (child_die = die->child;
15895 child_die != NULL && child_die->tag;
436c571c 15896 child_die = child_die->sibling)
55426c9d
JB
15897 {
15898 struct attribute *attr;
15899 LONGEST value;
15900 const gdb_byte *bytes;
15901 struct dwarf2_locexpr_baton *baton;
15902 const char *name;
60f7655a 15903
55426c9d
JB
15904 if (child_die->tag != DW_TAG_enumerator)
15905 continue;
15906
15907 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15908 if (attr == NULL)
15909 continue;
15910
15911 name = dwarf2_name (child_die, cu);
15912 if (name == NULL)
15913 name = "<anonymous enumerator>";
15914
15915 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15916 &value, &bytes, &baton);
15917 if (value < 0)
15918 {
15919 unsigned_enum = 0;
15920 flag_enum = 0;
15921 }
55426c9d 15922 else
edd45eb0
SM
15923 {
15924 if (count_one_bits_ll (value) >= 2)
15925 flag_enum = 0;
edd45eb0 15926 }
55426c9d 15927
ed6acedd
TT
15928 fields.emplace_back ();
15929 struct field &field = fields.back ();
15930 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15931 SET_FIELD_ENUMVAL (field, value);
15932 }
15933
15934 if (!fields.empty ())
15935 {
5e33d5f4 15936 type->set_num_fields (fields.size ());
ed6acedd
TT
15937 TYPE_FIELDS (type) = (struct field *)
15938 TYPE_ALLOC (type, sizeof (struct field) * fields.size ());
15939 memcpy (TYPE_FIELDS (type), fields.data (),
15940 sizeof (struct field) * fields.size ());
55426c9d
JB
15941 }
15942
15943 if (unsigned_enum)
15944 TYPE_UNSIGNED (type) = 1;
15945 if (flag_enum)
15946 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15947}
15948
134d01f1
DJ
15949/* Given a DW_AT_enumeration_type die, set its type. We do not
15950 complete the type's fields yet, or create any symbols. */
c906108c 15951
f792889a 15952static struct type *
134d01f1 15953read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15954{
518817b3 15955 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15956 struct type *type;
c906108c 15957 struct attribute *attr;
0114d602 15958 const char *name;
134d01f1 15959
348e048f
DE
15960 /* If the definition of this type lives in .debug_types, read that type.
15961 Don't follow DW_AT_specification though, that will take us back up
15962 the chain and we want to go down. */
052c8bb8 15963 attr = die->attr (DW_AT_signature);
435d3d88 15964 if (attr != nullptr)
348e048f 15965 {
ac9ec31b 15966 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15967
ac9ec31b 15968 /* The type's CU may not be the same as CU.
02142a6c 15969 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15970 return set_die_type (die, type, cu);
15971 }
15972
c906108c
SS
15973 type = alloc_type (objfile);
15974
67607e24 15975 type->set_code (TYPE_CODE_ENUM);
94af9270 15976 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15977 if (name != NULL)
d0e39ea2 15978 type->set_name (name);
c906108c 15979
0626fc76
TT
15980 attr = dwarf2_attr (die, DW_AT_type, cu);
15981 if (attr != NULL)
15982 {
15983 struct type *underlying_type = die_type (die, cu);
15984
15985 TYPE_TARGET_TYPE (type) = underlying_type;
15986 }
15987
e142c38c 15988 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15989 if (attr != nullptr)
c906108c
SS
15990 {
15991 TYPE_LENGTH (type) = DW_UNSND (attr);
15992 }
15993 else
15994 {
15995 TYPE_LENGTH (type) = 0;
15996 }
15997
2b4424c3
TT
15998 maybe_set_alignment (cu, die, type);
15999
137033e9
JB
16000 /* The enumeration DIE can be incomplete. In Ada, any type can be
16001 declared as private in the package spec, and then defined only
16002 inside the package body. Such types are known as Taft Amendment
16003 Types. When another package uses such a type, an incomplete DIE
16004 may be generated by the compiler. */
02eb380e 16005 if (die_is_declaration (die, cu))
876cecd0 16006 TYPE_STUB (type) = 1;
02eb380e 16007
0626fc76
TT
16008 /* If this type has an underlying type that is not a stub, then we
16009 may use its attributes. We always use the "unsigned" attribute
16010 in this situation, because ordinarily we guess whether the type
16011 is unsigned -- but the guess can be wrong and the underlying type
16012 can tell us the reality. However, we defer to a local size
16013 attribute if one exists, because this lets the compiler override
16014 the underlying type if needed. */
16015 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16016 {
9e7c9a03
HD
16017 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16018 underlying_type = check_typedef (underlying_type);
16019 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16020 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16021 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16022 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16023 && TYPE_RAW_ALIGN (underlying_type) != 0)
16024 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16025 }
16026
3d567982
TT
16027 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16028
ed6acedd
TT
16029 set_die_type (die, type, cu);
16030
16031 /* Finish the creation of this type by using the enum's children.
16032 Note that, as usual, this must come after set_die_type to avoid
16033 infinite recursion when trying to compute the names of the
16034 enumerators. */
16035 update_enumeration_type_from_children (die, type, cu);
16036
16037 return type;
134d01f1
DJ
16038}
16039
16040/* Given a pointer to a die which begins an enumeration, process all
16041 the dies that define the members of the enumeration, and create the
16042 symbol for the enumeration type.
16043
16044 NOTE: We reverse the order of the element list. */
16045
16046static void
16047process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16048{
f792889a 16049 struct type *this_type;
134d01f1 16050
f792889a
DJ
16051 this_type = get_die_type (die, cu);
16052 if (this_type == NULL)
16053 this_type = read_enumeration_type (die, cu);
9dc481d3 16054
639d11d3 16055 if (die->child != NULL)
c906108c 16056 {
9dc481d3 16057 struct die_info *child_die;
15d034d0 16058 const char *name;
9dc481d3 16059
639d11d3 16060 child_die = die->child;
c906108c
SS
16061 while (child_die && child_die->tag)
16062 {
16063 if (child_die->tag != DW_TAG_enumerator)
16064 {
e7c27a73 16065 process_die (child_die, cu);
c906108c
SS
16066 }
16067 else
16068 {
39cbfefa
DJ
16069 name = dwarf2_name (child_die, cu);
16070 if (name)
ed6acedd 16071 new_symbol (child_die, this_type, cu);
c906108c
SS
16072 }
16073
436c571c 16074 child_die = child_die->sibling;
c906108c 16075 }
c906108c 16076 }
134d01f1 16077
6c83ed52
TT
16078 /* If we are reading an enum from a .debug_types unit, and the enum
16079 is a declaration, and the enum is not the signatured type in the
16080 unit, then we do not want to add a symbol for it. Adding a
16081 symbol would in some cases obscure the true definition of the
16082 enum, giving users an incomplete type when the definition is
16083 actually available. Note that we do not want to do this for all
16084 enums which are just declarations, because C++0x allows forward
16085 enum declarations. */
3019eac3 16086 if (cu->per_cu->is_debug_types
6c83ed52
TT
16087 && die_is_declaration (die, cu))
16088 {
52dc124a 16089 struct signatured_type *sig_type;
6c83ed52 16090
c0f78cd4 16091 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16092 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16093 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16094 return;
16095 }
16096
f792889a 16097 new_symbol (die, this_type, cu);
c906108c
SS
16098}
16099
16100/* Extract all information from a DW_TAG_array_type DIE and put it in
16101 the DIE's type field. For now, this only handles one dimensional
16102 arrays. */
16103
f792889a 16104static struct type *
e7c27a73 16105read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16106{
518817b3 16107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16108 struct die_info *child_die;
7e314c57 16109 struct type *type;
c906108c 16110 struct type *element_type, *range_type, *index_type;
c906108c 16111 struct attribute *attr;
15d034d0 16112 const char *name;
a405673c 16113 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16114 unsigned int bit_stride = 0;
c906108c 16115
e7c27a73 16116 element_type = die_type (die, cu);
c906108c 16117
7e314c57
JK
16118 /* The die_type call above may have already set the type for this DIE. */
16119 type = get_die_type (die, cu);
16120 if (type)
16121 return type;
16122
dc53a7ad
JB
16123 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16124 if (attr != NULL)
a405673c
JB
16125 {
16126 int stride_ok;
09ba997f 16127 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16128
16129 byte_stride_prop
16130 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16131 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16132 prop_type);
a405673c
JB
16133 if (!stride_ok)
16134 {
b98664d3 16135 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16136 " - DIE at %s [in module %s]"),
16137 sect_offset_str (die->sect_off),
518817b3 16138 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16139 /* Ignore this attribute. We will likely not be able to print
16140 arrays of this type correctly, but there is little we can do
16141 to help if we cannot read the attribute's value. */
16142 byte_stride_prop = NULL;
16143 }
16144 }
dc53a7ad
JB
16145
16146 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16147 if (attr != NULL)
16148 bit_stride = DW_UNSND (attr);
16149
c906108c
SS
16150 /* Irix 6.2 native cc creates array types without children for
16151 arrays with unspecified length. */
639d11d3 16152 if (die->child == NULL)
c906108c 16153 {
46bf5051 16154 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16155 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16156 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16157 byte_stride_prop, bit_stride);
f792889a 16158 return set_die_type (die, type, cu);
c906108c
SS
16159 }
16160
791afaa2 16161 std::vector<struct type *> range_types;
639d11d3 16162 child_die = die->child;
c906108c
SS
16163 while (child_die && child_die->tag)
16164 {
16165 if (child_die->tag == DW_TAG_subrange_type)
16166 {
f792889a 16167 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16168
f792889a 16169 if (child_type != NULL)
a02abb62 16170 {
0963b4bd
MS
16171 /* The range type was succesfully read. Save it for the
16172 array type creation. */
791afaa2 16173 range_types.push_back (child_type);
a02abb62 16174 }
c906108c 16175 }
436c571c 16176 child_die = child_die->sibling;
c906108c
SS
16177 }
16178
16179 /* Dwarf2 dimensions are output from left to right, create the
16180 necessary array types in backwards order. */
7ca2d3a3 16181
c906108c 16182 type = element_type;
7ca2d3a3
DL
16183
16184 if (read_array_order (die, cu) == DW_ORD_col_major)
16185 {
16186 int i = 0;
9a619af0 16187
791afaa2 16188 while (i < range_types.size ())
dc53a7ad 16189 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16190 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16191 }
16192 else
16193 {
791afaa2 16194 size_t ndim = range_types.size ();
7ca2d3a3 16195 while (ndim-- > 0)
dc53a7ad 16196 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16197 byte_stride_prop, bit_stride);
7ca2d3a3 16198 }
c906108c 16199
f5f8a009
EZ
16200 /* Understand Dwarf2 support for vector types (like they occur on
16201 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16202 array type. This is not part of the Dwarf2/3 standard yet, but a
16203 custom vendor extension. The main difference between a regular
16204 array and the vector variant is that vectors are passed by value
16205 to functions. */
e142c38c 16206 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16207 if (attr != nullptr)
ea37ba09 16208 make_vector_type (type);
f5f8a009 16209
dbc98a8b
KW
16210 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16211 implementation may choose to implement triple vectors using this
16212 attribute. */
16213 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16214 if (attr != nullptr)
dbc98a8b
KW
16215 {
16216 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16217 TYPE_LENGTH (type) = DW_UNSND (attr);
16218 else
b98664d3 16219 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16220 "than the total size of elements"));
dbc98a8b
KW
16221 }
16222
39cbfefa
DJ
16223 name = dwarf2_name (die, cu);
16224 if (name)
d0e39ea2 16225 type->set_name (name);
6e70227d 16226
2b4424c3
TT
16227 maybe_set_alignment (cu, die, type);
16228
0963b4bd 16229 /* Install the type in the die. */
7e314c57
JK
16230 set_die_type (die, type, cu);
16231
16232 /* set_die_type should be already done. */
b4ba55a1
JB
16233 set_descriptive_type (type, die, cu);
16234
7e314c57 16235 return type;
c906108c
SS
16236}
16237
7ca2d3a3 16238static enum dwarf_array_dim_ordering
6e70227d 16239read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16240{
16241 struct attribute *attr;
16242
16243 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16244
435d3d88 16245 if (attr != nullptr)
aead7601 16246 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16247
0963b4bd
MS
16248 /* GNU F77 is a special case, as at 08/2004 array type info is the
16249 opposite order to the dwarf2 specification, but data is still
16250 laid out as per normal fortran.
7ca2d3a3 16251
0963b4bd
MS
16252 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16253 version checking. */
7ca2d3a3 16254
905e0470
PM
16255 if (cu->language == language_fortran
16256 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16257 {
16258 return DW_ORD_row_major;
16259 }
16260
6e70227d 16261 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16262 {
16263 case array_column_major:
16264 return DW_ORD_col_major;
16265 case array_row_major:
16266 default:
16267 return DW_ORD_row_major;
16268 };
16269}
16270
72019c9c 16271/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16272 the DIE's type field. */
72019c9c 16273
f792889a 16274static struct type *
72019c9c
GM
16275read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16276{
7e314c57
JK
16277 struct type *domain_type, *set_type;
16278 struct attribute *attr;
f792889a 16279
7e314c57
JK
16280 domain_type = die_type (die, cu);
16281
16282 /* The die_type call above may have already set the type for this DIE. */
16283 set_type = get_die_type (die, cu);
16284 if (set_type)
16285 return set_type;
16286
16287 set_type = create_set_type (NULL, domain_type);
16288
16289 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16290 if (attr != nullptr)
d09039dd 16291 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16292
2b4424c3
TT
16293 maybe_set_alignment (cu, die, set_type);
16294
f792889a 16295 return set_die_type (die, set_type, cu);
72019c9c 16296}
7ca2d3a3 16297
0971de02
TT
16298/* A helper for read_common_block that creates a locexpr baton.
16299 SYM is the symbol which we are marking as computed.
16300 COMMON_DIE is the DIE for the common block.
16301 COMMON_LOC is the location expression attribute for the common
16302 block itself.
16303 MEMBER_LOC is the location expression attribute for the particular
16304 member of the common block that we are processing.
16305 CU is the CU from which the above come. */
16306
16307static void
16308mark_common_block_symbol_computed (struct symbol *sym,
16309 struct die_info *common_die,
16310 struct attribute *common_loc,
16311 struct attribute *member_loc,
16312 struct dwarf2_cu *cu)
16313{
518817b3
SM
16314 struct dwarf2_per_objfile *dwarf2_per_objfile
16315 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16316 struct objfile *objfile = dwarf2_per_objfile->objfile;
16317 struct dwarf2_locexpr_baton *baton;
16318 gdb_byte *ptr;
16319 unsigned int cu_off;
08feed99 16320 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16321 LONGEST offset = 0;
16322
16323 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16324 gdb_assert (common_loc->form_is_block ());
16325 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16326 || member_loc->form_is_constant ());
0971de02 16327
8d749320 16328 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16329 baton->per_cu = cu->per_cu;
16330 gdb_assert (baton->per_cu);
16331
16332 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16333
cd6c91b4 16334 if (member_loc->form_is_constant ())
0971de02 16335 {
0826b30a 16336 offset = member_loc->constant_value (0);
0971de02
TT
16337 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16338 }
16339 else
16340 baton->size += DW_BLOCK (member_loc)->size;
16341
224c3ddb 16342 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16343 baton->data = ptr;
16344
16345 *ptr++ = DW_OP_call4;
9c541725 16346 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16347 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16348 ptr += 4;
16349
cd6c91b4 16350 if (member_loc->form_is_constant ())
0971de02
TT
16351 {
16352 *ptr++ = DW_OP_addr;
16353 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16354 ptr += cu->header.addr_size;
16355 }
16356 else
16357 {
16358 /* We have to copy the data here, because DW_OP_call4 will only
16359 use a DW_AT_location attribute. */
16360 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16361 ptr += DW_BLOCK (member_loc)->size;
16362 }
16363
16364 *ptr++ = DW_OP_plus;
16365 gdb_assert (ptr - baton->data == baton->size);
16366
0971de02 16367 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16368 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16369}
16370
4357ac6c
TT
16371/* Create appropriate locally-scoped variables for all the
16372 DW_TAG_common_block entries. Also create a struct common_block
16373 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16374 is used to separate the common blocks name namespace from regular
4357ac6c 16375 variable names. */
c906108c
SS
16376
16377static void
e7c27a73 16378read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16379{
0971de02
TT
16380 struct attribute *attr;
16381
16382 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16383 if (attr != nullptr)
0971de02
TT
16384 {
16385 /* Support the .debug_loc offsets. */
4fc6c0d5 16386 if (attr->form_is_block ())
0971de02
TT
16387 {
16388 /* Ok. */
16389 }
cd6c91b4 16390 else if (attr->form_is_section_offset ())
0971de02
TT
16391 {
16392 dwarf2_complex_location_expr_complaint ();
16393 attr = NULL;
16394 }
16395 else
16396 {
16397 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16398 "common block member");
16399 attr = NULL;
16400 }
16401 }
16402
639d11d3 16403 if (die->child != NULL)
c906108c 16404 {
518817b3 16405 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16406 struct die_info *child_die;
16407 size_t n_entries = 0, size;
16408 struct common_block *common_block;
16409 struct symbol *sym;
74ac6d43 16410
4357ac6c
TT
16411 for (child_die = die->child;
16412 child_die && child_die->tag;
436c571c 16413 child_die = child_die->sibling)
4357ac6c
TT
16414 ++n_entries;
16415
16416 size = (sizeof (struct common_block)
16417 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16418 common_block
16419 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16420 size);
4357ac6c
TT
16421 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16422 common_block->n_entries = 0;
16423
16424 for (child_die = die->child;
16425 child_die && child_die->tag;
436c571c 16426 child_die = child_die->sibling)
4357ac6c
TT
16427 {
16428 /* Create the symbol in the DW_TAG_common_block block in the current
16429 symbol scope. */
e7c27a73 16430 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16431 if (sym != NULL)
16432 {
16433 struct attribute *member_loc;
16434
16435 common_block->contents[common_block->n_entries++] = sym;
16436
16437 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16438 cu);
16439 if (member_loc)
16440 {
16441 /* GDB has handled this for a long time, but it is
16442 not specified by DWARF. It seems to have been
16443 emitted by gfortran at least as recently as:
16444 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16445 complaint (_("Variable in common block has "
0971de02 16446 "DW_AT_data_member_location "
9d8780f0
SM
16447 "- DIE at %s [in module %s]"),
16448 sect_offset_str (child_die->sect_off),
518817b3 16449 objfile_name (objfile));
0971de02 16450
cd6c91b4 16451 if (member_loc->form_is_section_offset ())
0971de02 16452 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16453 else if (member_loc->form_is_constant ()
4fc6c0d5 16454 || member_loc->form_is_block ())
0971de02 16455 {
435d3d88 16456 if (attr != nullptr)
0971de02
TT
16457 mark_common_block_symbol_computed (sym, die, attr,
16458 member_loc, cu);
16459 }
16460 else
16461 dwarf2_complex_location_expr_complaint ();
16462 }
16463 }
c906108c 16464 }
4357ac6c
TT
16465
16466 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16467 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16468 }
16469}
16470
0114d602 16471/* Create a type for a C++ namespace. */
d9fa45fe 16472
0114d602
DJ
16473static struct type *
16474read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16475{
518817b3 16476 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16477 const char *previous_prefix, *name;
9219021c 16478 int is_anonymous;
0114d602
DJ
16479 struct type *type;
16480
16481 /* For extensions, reuse the type of the original namespace. */
16482 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16483 {
16484 struct die_info *ext_die;
16485 struct dwarf2_cu *ext_cu = cu;
9a619af0 16486
0114d602
DJ
16487 ext_die = dwarf2_extension (die, &ext_cu);
16488 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16489
16490 /* EXT_CU may not be the same as CU.
02142a6c 16491 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16492 return set_die_type (die, type, cu);
16493 }
9219021c 16494
e142c38c 16495 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16496
16497 /* Now build the name of the current namespace. */
16498
0114d602
DJ
16499 previous_prefix = determine_prefix (die, cu);
16500 if (previous_prefix[0] != '\0')
16501 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16502 previous_prefix, name, 0, cu);
0114d602
DJ
16503
16504 /* Create the type. */
19f392bc 16505 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16506
60531b24 16507 return set_die_type (die, type, cu);
0114d602
DJ
16508}
16509
22cee43f 16510/* Read a namespace scope. */
0114d602
DJ
16511
16512static void
16513read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16514{
518817b3 16515 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16516 int is_anonymous;
9219021c 16517
5c4e30ca
DC
16518 /* Add a symbol associated to this if we haven't seen the namespace
16519 before. Also, add a using directive if it's an anonymous
16520 namespace. */
9219021c 16521
f2f0e013 16522 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16523 {
16524 struct type *type;
16525
0114d602 16526 type = read_type_die (die, cu);
e7c27a73 16527 new_symbol (die, type, cu);
5c4e30ca 16528
e8e80198 16529 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16530 if (is_anonymous)
0114d602
DJ
16531 {
16532 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16533
eb1e02fd 16534 std::vector<const char *> excludes;
804d2729 16535 add_using_directive (using_directives (cu),
7d93a1e0 16536 previous_prefix, type->name (), NULL,
eb1e02fd 16537 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16538 }
5c4e30ca 16539 }
9219021c 16540
639d11d3 16541 if (die->child != NULL)
d9fa45fe 16542 {
639d11d3 16543 struct die_info *child_die = die->child;
6e70227d 16544
d9fa45fe
DC
16545 while (child_die && child_die->tag)
16546 {
e7c27a73 16547 process_die (child_die, cu);
436c571c 16548 child_die = child_die->sibling;
d9fa45fe
DC
16549 }
16550 }
38d518c9
EZ
16551}
16552
f55ee35c
JK
16553/* Read a Fortran module as type. This DIE can be only a declaration used for
16554 imported module. Still we need that type as local Fortran "use ... only"
16555 declaration imports depend on the created type in determine_prefix. */
16556
16557static struct type *
16558read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16559{
518817b3 16560 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16561 const char *module_name;
f55ee35c
JK
16562 struct type *type;
16563
16564 module_name = dwarf2_name (die, cu);
19f392bc 16565 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16566
f55ee35c
JK
16567 return set_die_type (die, type, cu);
16568}
16569
5d7cb8df
JK
16570/* Read a Fortran module. */
16571
16572static void
16573read_module (struct die_info *die, struct dwarf2_cu *cu)
16574{
16575 struct die_info *child_die = die->child;
530e8392
KB
16576 struct type *type;
16577
16578 type = read_type_die (die, cu);
16579 new_symbol (die, type, cu);
5d7cb8df 16580
5d7cb8df
JK
16581 while (child_die && child_die->tag)
16582 {
16583 process_die (child_die, cu);
436c571c 16584 child_die = child_die->sibling;
5d7cb8df
JK
16585 }
16586}
16587
38d518c9
EZ
16588/* Return the name of the namespace represented by DIE. Set
16589 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16590 namespace. */
16591
16592static const char *
e142c38c 16593namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16594{
16595 struct die_info *current_die;
16596 const char *name = NULL;
16597
16598 /* Loop through the extensions until we find a name. */
16599
16600 for (current_die = die;
16601 current_die != NULL;
f2f0e013 16602 current_die = dwarf2_extension (die, &cu))
38d518c9 16603 {
96553a0c
DE
16604 /* We don't use dwarf2_name here so that we can detect the absence
16605 of a name -> anonymous namespace. */
7d45c7c3 16606 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16607
38d518c9
EZ
16608 if (name != NULL)
16609 break;
16610 }
16611
16612 /* Is it an anonymous namespace? */
16613
16614 *is_anonymous = (name == NULL);
16615 if (*is_anonymous)
2b1dbab0 16616 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16617
16618 return name;
d9fa45fe
DC
16619}
16620
c906108c
SS
16621/* Extract all information from a DW_TAG_pointer_type DIE and add to
16622 the user defined type vector. */
16623
f792889a 16624static struct type *
e7c27a73 16625read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16626{
518817b3 16627 struct gdbarch *gdbarch
08feed99 16628 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16629 struct comp_unit_head *cu_header = &cu->header;
c906108c 16630 struct type *type;
8b2dbe47
KB
16631 struct attribute *attr_byte_size;
16632 struct attribute *attr_address_class;
16633 int byte_size, addr_class;
7e314c57
JK
16634 struct type *target_type;
16635
16636 target_type = die_type (die, cu);
c906108c 16637
7e314c57
JK
16638 /* The die_type call above may have already set the type for this DIE. */
16639 type = get_die_type (die, cu);
16640 if (type)
16641 return type;
16642
16643 type = lookup_pointer_type (target_type);
8b2dbe47 16644
e142c38c 16645 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16646 if (attr_byte_size)
16647 byte_size = DW_UNSND (attr_byte_size);
c906108c 16648 else
8b2dbe47
KB
16649 byte_size = cu_header->addr_size;
16650
e142c38c 16651 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16652 if (attr_address_class)
16653 addr_class = DW_UNSND (attr_address_class);
16654 else
16655 addr_class = DW_ADDR_none;
16656
2b4424c3
TT
16657 ULONGEST alignment = get_alignment (cu, die);
16658
16659 /* If the pointer size, alignment, or address class is different
16660 than the default, create a type variant marked as such and set
16661 the length accordingly. */
16662 if (TYPE_LENGTH (type) != byte_size
16663 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16664 && alignment != TYPE_RAW_ALIGN (type))
16665 || addr_class != DW_ADDR_none)
c906108c 16666 {
5e2b427d 16667 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16668 {
16669 int type_flags;
16670
849957d9 16671 type_flags = gdbarch_address_class_type_flags
5e2b427d 16672 (gdbarch, byte_size, addr_class);
876cecd0
TT
16673 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16674 == 0);
8b2dbe47
KB
16675 type = make_type_with_address_space (type, type_flags);
16676 }
16677 else if (TYPE_LENGTH (type) != byte_size)
16678 {
b98664d3 16679 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16680 }
2b4424c3
TT
16681 else if (TYPE_RAW_ALIGN (type) != alignment)
16682 {
b98664d3 16683 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16684 " - DIE at %s [in module %s]"),
16685 sect_offset_str (die->sect_off),
16686 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16687 }
6e70227d 16688 else
9a619af0
MS
16689 {
16690 /* Should we also complain about unhandled address classes? */
16691 }
c906108c 16692 }
8b2dbe47
KB
16693
16694 TYPE_LENGTH (type) = byte_size;
2b4424c3 16695 set_type_align (type, alignment);
f792889a 16696 return set_die_type (die, type, cu);
c906108c
SS
16697}
16698
16699/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16700 the user defined type vector. */
16701
f792889a 16702static struct type *
e7c27a73 16703read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16704{
16705 struct type *type;
16706 struct type *to_type;
16707 struct type *domain;
16708
e7c27a73
DJ
16709 to_type = die_type (die, cu);
16710 domain = die_containing_type (die, cu);
0d5de010 16711
7e314c57
JK
16712 /* The calls above may have already set the type for this DIE. */
16713 type = get_die_type (die, cu);
16714 if (type)
16715 return type;
16716
78134374 16717 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16718 type = lookup_methodptr_type (to_type);
78134374 16719 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16720 {
518817b3
SM
16721 struct type *new_type
16722 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16723
16724 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16725 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16726 TYPE_VARARGS (to_type));
16727 type = lookup_methodptr_type (new_type);
16728 }
0d5de010
DJ
16729 else
16730 type = lookup_memberptr_type (to_type, domain);
c906108c 16731
f792889a 16732 return set_die_type (die, type, cu);
c906108c
SS
16733}
16734
4297a3f0 16735/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16736 the user defined type vector. */
16737
f792889a 16738static struct type *
4297a3f0
AV
16739read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16740 enum type_code refcode)
c906108c 16741{
e7c27a73 16742 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16743 struct type *type, *target_type;
c906108c
SS
16744 struct attribute *attr;
16745
4297a3f0
AV
16746 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16747
7e314c57
JK
16748 target_type = die_type (die, cu);
16749
16750 /* The die_type call above may have already set the type for this DIE. */
16751 type = get_die_type (die, cu);
16752 if (type)
16753 return type;
16754
4297a3f0 16755 type = lookup_reference_type (target_type, refcode);
e142c38c 16756 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16757 if (attr != nullptr)
c906108c
SS
16758 {
16759 TYPE_LENGTH (type) = DW_UNSND (attr);
16760 }
16761 else
16762 {
107d2387 16763 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16764 }
2b4424c3 16765 maybe_set_alignment (cu, die, type);
f792889a 16766 return set_die_type (die, type, cu);
c906108c
SS
16767}
16768
cf363f18
MW
16769/* Add the given cv-qualifiers to the element type of the array. GCC
16770 outputs DWARF type qualifiers that apply to an array, not the
16771 element type. But GDB relies on the array element type to carry
16772 the cv-qualifiers. This mimics section 6.7.3 of the C99
16773 specification. */
16774
16775static struct type *
16776add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16777 struct type *base_type, int cnst, int voltl)
16778{
16779 struct type *el_type, *inner_array;
16780
16781 base_type = copy_type (base_type);
16782 inner_array = base_type;
16783
78134374 16784 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16785 {
16786 TYPE_TARGET_TYPE (inner_array) =
16787 copy_type (TYPE_TARGET_TYPE (inner_array));
16788 inner_array = TYPE_TARGET_TYPE (inner_array);
16789 }
16790
16791 el_type = TYPE_TARGET_TYPE (inner_array);
16792 cnst |= TYPE_CONST (el_type);
16793 voltl |= TYPE_VOLATILE (el_type);
16794 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16795
16796 return set_die_type (die, base_type, cu);
16797}
16798
f792889a 16799static struct type *
e7c27a73 16800read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16801{
f792889a 16802 struct type *base_type, *cv_type;
c906108c 16803
e7c27a73 16804 base_type = die_type (die, cu);
7e314c57
JK
16805
16806 /* The die_type call above may have already set the type for this DIE. */
16807 cv_type = get_die_type (die, cu);
16808 if (cv_type)
16809 return cv_type;
16810
2f608a3a
KW
16811 /* In case the const qualifier is applied to an array type, the element type
16812 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16813 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16814 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16815
f792889a
DJ
16816 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16817 return set_die_type (die, cv_type, cu);
c906108c
SS
16818}
16819
f792889a 16820static struct type *
e7c27a73 16821read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16822{
f792889a 16823 struct type *base_type, *cv_type;
c906108c 16824
e7c27a73 16825 base_type = die_type (die, cu);
7e314c57
JK
16826
16827 /* The die_type call above may have already set the type for this DIE. */
16828 cv_type = get_die_type (die, cu);
16829 if (cv_type)
16830 return cv_type;
16831
cf363f18
MW
16832 /* In case the volatile qualifier is applied to an array type, the
16833 element type is so qualified, not the array type (section 6.7.3
16834 of C99). */
78134374 16835 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16836 return add_array_cv_type (die, cu, base_type, 0, 1);
16837
f792889a
DJ
16838 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16839 return set_die_type (die, cv_type, cu);
c906108c
SS
16840}
16841
06d66ee9
TT
16842/* Handle DW_TAG_restrict_type. */
16843
16844static struct type *
16845read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16846{
16847 struct type *base_type, *cv_type;
16848
16849 base_type = die_type (die, cu);
16850
16851 /* The die_type call above may have already set the type for this DIE. */
16852 cv_type = get_die_type (die, cu);
16853 if (cv_type)
16854 return cv_type;
16855
16856 cv_type = make_restrict_type (base_type);
16857 return set_die_type (die, cv_type, cu);
16858}
16859
a2c2acaf
MW
16860/* Handle DW_TAG_atomic_type. */
16861
16862static struct type *
16863read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16864{
16865 struct type *base_type, *cv_type;
16866
16867 base_type = die_type (die, cu);
16868
16869 /* The die_type call above may have already set the type for this DIE. */
16870 cv_type = get_die_type (die, cu);
16871 if (cv_type)
16872 return cv_type;
16873
16874 cv_type = make_atomic_type (base_type);
16875 return set_die_type (die, cv_type, cu);
16876}
16877
c906108c
SS
16878/* Extract all information from a DW_TAG_string_type DIE and add to
16879 the user defined type vector. It isn't really a user defined type,
16880 but it behaves like one, with other DIE's using an AT_user_def_type
16881 attribute to reference it. */
16882
f792889a 16883static struct type *
e7c27a73 16884read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16885{
518817b3 16886 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16887 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16888 struct type *type, *range_type, *index_type, *char_type;
16889 struct attribute *attr;
216a7e6b
AB
16890 struct dynamic_prop prop;
16891 bool length_is_constant = true;
16892 LONGEST length;
16893
16894 /* There are a couple of places where bit sizes might be made use of
16895 when parsing a DW_TAG_string_type, however, no producer that we know
16896 of make use of these. Handling bit sizes that are a multiple of the
16897 byte size is easy enough, but what about other bit sizes? Lets deal
16898 with that problem when we have to. Warn about these attributes being
16899 unsupported, then parse the type and ignore them like we always
16900 have. */
16901 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16902 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16903 {
16904 static bool warning_printed = false;
16905 if (!warning_printed)
16906 {
16907 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16908 "currently supported on DW_TAG_string_type."));
16909 warning_printed = true;
16910 }
16911 }
c906108c 16912
e142c38c 16913 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16914 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16915 {
16916 /* The string length describes the location at which the length of
16917 the string can be found. The size of the length field can be
16918 specified with one of the attributes below. */
16919 struct type *prop_type;
16920 struct attribute *len
16921 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16922 if (len == nullptr)
16923 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16924 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16925 {
16926 /* Pass 0 as the default as we know this attribute is constant
16927 and the default value will not be returned. */
0826b30a 16928 LONGEST sz = len->constant_value (0);
09ba997f 16929 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16930 }
16931 else
16932 {
16933 /* If the size is not specified then we assume it is the size of
16934 an address on this target. */
09ba997f 16935 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16936 }
16937
16938 /* Convert the attribute into a dynamic property. */
16939 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16940 length = 1;
16941 else
16942 length_is_constant = false;
16943 }
16944 else if (attr != nullptr)
16945 {
16946 /* This DW_AT_string_length just contains the length with no
16947 indirection. There's no need to create a dynamic property in this
16948 case. Pass 0 for the default value as we know it will not be
16949 returned in this case. */
0826b30a 16950 length = attr->constant_value (0);
216a7e6b
AB
16951 }
16952 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16953 {
216a7e6b 16954 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16955 length = attr->constant_value (1);
c906108c
SS
16956 }
16957 else
16958 {
216a7e6b
AB
16959 /* Use 1 as a fallback length if we have nothing else. */
16960 length = 1;
c906108c 16961 }
6ccb9162 16962
46bf5051 16963 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16964 if (length_is_constant)
16965 range_type = create_static_range_type (NULL, index_type, 1, length);
16966 else
16967 {
16968 struct dynamic_prop low_bound;
16969
16970 low_bound.kind = PROP_CONST;
16971 low_bound.data.const_val = 1;
16972 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16973 }
3b7538c0
UW
16974 char_type = language_string_char_type (cu->language_defn, gdbarch);
16975 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16976
f792889a 16977 return set_die_type (die, type, cu);
c906108c
SS
16978}
16979
4d804846
JB
16980/* Assuming that DIE corresponds to a function, returns nonzero
16981 if the function is prototyped. */
16982
16983static int
16984prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16985{
16986 struct attribute *attr;
16987
16988 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16989 if (attr && (DW_UNSND (attr) != 0))
16990 return 1;
16991
16992 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16993 is only meaningful for C, but the concept also extends to other
4d804846
JB
16994 languages that allow unprototyped functions (Eg: Objective C).
16995 For all other languages, assume that functions are always
16996 prototyped. */
16997 if (cu->language != language_c
16998 && cu->language != language_objc
16999 && cu->language != language_opencl)
17000 return 1;
17001
17002 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17003 prototyped and unprototyped functions; default to prototyped,
17004 since that is more common in modern code (and RealView warns
17005 about unprototyped functions). */
17006 if (producer_is_realview (cu->producer))
17007 return 1;
17008
17009 return 0;
17010}
17011
c906108c
SS
17012/* Handle DIES due to C code like:
17013
17014 struct foo
c5aa993b
JM
17015 {
17016 int (*funcp)(int a, long l);
17017 int b;
17018 };
c906108c 17019
0963b4bd 17020 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17021
f792889a 17022static struct type *
e7c27a73 17023read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17024{
518817b3 17025 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17026 struct type *type; /* Type that this function returns. */
17027 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17028 struct attribute *attr;
17029
e7c27a73 17030 type = die_type (die, cu);
7e314c57
JK
17031
17032 /* The die_type call above may have already set the type for this DIE. */
17033 ftype = get_die_type (die, cu);
17034 if (ftype)
17035 return ftype;
17036
0c8b41f1 17037 ftype = lookup_function_type (type);
c906108c 17038
4d804846 17039 if (prototyped_function_p (die, cu))
a6c727b2 17040 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17041
c055b101
CV
17042 /* Store the calling convention in the type if it's available in
17043 the subroutine die. Otherwise set the calling convention to
17044 the default value DW_CC_normal. */
17045 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17046 if (attr != nullptr
17047 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17048 TYPE_CALLING_CONVENTION (ftype)
17049 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17050 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17051 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17052 else
17053 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17054
743649fd
MW
17055 /* Record whether the function returns normally to its caller or not
17056 if the DWARF producer set that information. */
17057 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17058 if (attr && (DW_UNSND (attr) != 0))
17059 TYPE_NO_RETURN (ftype) = 1;
17060
76c10ea2
GM
17061 /* We need to add the subroutine type to the die immediately so
17062 we don't infinitely recurse when dealing with parameters
0963b4bd 17063 declared as the same subroutine type. */
76c10ea2 17064 set_die_type (die, ftype, cu);
6e70227d 17065
639d11d3 17066 if (die->child != NULL)
c906108c 17067 {
bb5ed363 17068 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17069 struct die_info *child_die;
8072405b 17070 int nparams, iparams;
c906108c
SS
17071
17072 /* Count the number of parameters.
17073 FIXME: GDB currently ignores vararg functions, but knows about
17074 vararg member functions. */
8072405b 17075 nparams = 0;
639d11d3 17076 child_die = die->child;
c906108c
SS
17077 while (child_die && child_die->tag)
17078 {
17079 if (child_die->tag == DW_TAG_formal_parameter)
17080 nparams++;
17081 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17082 TYPE_VARARGS (ftype) = 1;
436c571c 17083 child_die = child_die->sibling;
c906108c
SS
17084 }
17085
17086 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17087 ftype->set_num_fields (nparams);
c906108c 17088 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17089 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17090
8072405b
JK
17091 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17092 even if we error out during the parameters reading below. */
17093 for (iparams = 0; iparams < nparams; iparams++)
17094 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17095
17096 iparams = 0;
639d11d3 17097 child_die = die->child;
c906108c
SS
17098 while (child_die && child_die->tag)
17099 {
17100 if (child_die->tag == DW_TAG_formal_parameter)
17101 {
3ce3b1ba
PA
17102 struct type *arg_type;
17103
17104 /* DWARF version 2 has no clean way to discern C++
17105 static and non-static member functions. G++ helps
17106 GDB by marking the first parameter for non-static
17107 member functions (which is the this pointer) as
17108 artificial. We pass this information to
17109 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17110
17111 DWARF version 3 added DW_AT_object_pointer, which GCC
17112 4.5 does not yet generate. */
e142c38c 17113 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17114 if (attr != nullptr)
c906108c
SS
17115 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17116 else
9c37b5ae 17117 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17118 arg_type = die_type (child_die, cu);
17119
17120 /* RealView does not mark THIS as const, which the testsuite
17121 expects. GCC marks THIS as const in method definitions,
17122 but not in the class specifications (GCC PR 43053). */
17123 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17124 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17125 {
17126 int is_this = 0;
17127 struct dwarf2_cu *arg_cu = cu;
17128 const char *name = dwarf2_name (child_die, cu);
17129
17130 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17131 if (attr != nullptr)
3ce3b1ba
PA
17132 {
17133 /* If the compiler emits this, use it. */
17134 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17135 is_this = 1;
17136 }
17137 else if (name && strcmp (name, "this") == 0)
17138 /* Function definitions will have the argument names. */
17139 is_this = 1;
17140 else if (name == NULL && iparams == 0)
17141 /* Declarations may not have the names, so like
17142 elsewhere in GDB, assume an artificial first
17143 argument is "this". */
17144 is_this = 1;
17145
17146 if (is_this)
17147 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17148 arg_type, 0);
17149 }
17150
17151 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17152 iparams++;
17153 }
436c571c 17154 child_die = child_die->sibling;
c906108c
SS
17155 }
17156 }
17157
76c10ea2 17158 return ftype;
c906108c
SS
17159}
17160
f792889a 17161static struct type *
e7c27a73 17162read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17163{
518817b3 17164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17165 const char *name = NULL;
3c8e0968 17166 struct type *this_type, *target_type;
c906108c 17167
94af9270 17168 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17169 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17170 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17171 set_die_type (die, this_type, cu);
3c8e0968
DE
17172 target_type = die_type (die, cu);
17173 if (target_type != this_type)
17174 TYPE_TARGET_TYPE (this_type) = target_type;
17175 else
17176 {
17177 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17178 spec and cause infinite loops in GDB. */
b98664d3 17179 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17180 "- DIE at %s [in module %s]"),
17181 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17182 TYPE_TARGET_TYPE (this_type) = NULL;
17183 }
e4003a34
TV
17184 if (name == NULL)
17185 {
17186 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17187 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17188 Handle these by just returning the target type, rather than
17189 constructing an anonymous typedef type and trying to handle this
17190 elsewhere. */
17191 set_die_type (die, target_type, cu);
17192 return target_type;
17193 }
f792889a 17194 return this_type;
c906108c
SS
17195}
17196
9b790ce7
UW
17197/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17198 (which may be different from NAME) to the architecture back-end to allow
17199 it to guess the correct format if necessary. */
17200
17201static struct type *
17202dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17203 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17204{
08feed99 17205 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17206 const struct floatformat **format;
17207 struct type *type;
17208
17209 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17210 if (format)
103a685e 17211 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17212 else
77b7c781 17213 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17214
17215 return type;
17216}
17217
eb77c9df
AB
17218/* Allocate an integer type of size BITS and name NAME. */
17219
17220static struct type *
17221dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17222 int bits, int unsigned_p, const char *name)
17223{
17224 struct type *type;
17225
17226 /* Versions of Intel's C Compiler generate an integer type called "void"
17227 instead of using DW_TAG_unspecified_type. This has been seen on
17228 at least versions 14, 17, and 18. */
35ee2dc2
AB
17229 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17230 && strcmp (name, "void") == 0)
eb77c9df
AB
17231 type = objfile_type (objfile)->builtin_void;
17232 else
17233 type = init_integer_type (objfile, bits, unsigned_p, name);
17234
17235 return type;
17236}
17237
8bdc1658
AB
17238/* Initialise and return a floating point type of size BITS suitable for
17239 use as a component of a complex number. The NAME_HINT is passed through
17240 when initialising the floating point type and is the name of the complex
17241 type.
17242
17243 As DWARF doesn't currently provide an explicit name for the components
17244 of a complex number, but it can be helpful to have these components
17245 named, we try to select a suitable name based on the size of the
17246 component. */
17247static struct type *
17248dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17249 struct objfile *objfile,
103a685e
TT
17250 int bits, const char *name_hint,
17251 enum bfd_endian byte_order)
8bdc1658 17252{
08feed99 17253 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17254 struct type *tt = nullptr;
17255
35add35e
AB
17256 /* Try to find a suitable floating point builtin type of size BITS.
17257 We're going to use the name of this type as the name for the complex
17258 target type that we are about to create. */
1db455a7 17259 switch (cu->language)
8bdc1658 17260 {
1db455a7
AB
17261 case language_fortran:
17262 switch (bits)
17263 {
17264 case 32:
17265 tt = builtin_f_type (gdbarch)->builtin_real;
17266 break;
17267 case 64:
17268 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17269 break;
17270 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17271 case 128:
17272 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17273 break;
17274 }
8bdc1658 17275 break;
1db455a7
AB
17276 default:
17277 switch (bits)
17278 {
17279 case 32:
17280 tt = builtin_type (gdbarch)->builtin_float;
17281 break;
17282 case 64:
17283 tt = builtin_type (gdbarch)->builtin_double;
17284 break;
17285 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17286 case 128:
17287 tt = builtin_type (gdbarch)->builtin_long_double;
17288 break;
17289 }
8bdc1658
AB
17290 break;
17291 }
17292
35add35e
AB
17293 /* If the type we found doesn't match the size we were looking for, then
17294 pretend we didn't find a type at all, the complex target type we
17295 create will then be nameless. */
a12e5744 17296 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17297 tt = nullptr;
17298
7d93a1e0 17299 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17300 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17301}
17302
c906108c
SS
17303/* Find a representation of a given base type and install
17304 it in the TYPE field of the die. */
17305
f792889a 17306static struct type *
e7c27a73 17307read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17308{
518817b3 17309 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17310 struct type *type;
17311 struct attribute *attr;
19f392bc 17312 int encoding = 0, bits = 0;
15d034d0 17313 const char *name;
34877895 17314 gdbarch *arch;
c906108c 17315
e142c38c 17316 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17317 if (attr != nullptr)
34877895 17318 encoding = DW_UNSND (attr);
e142c38c 17319 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17320 if (attr != nullptr)
34877895 17321 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17322 name = dwarf2_name (die, cu);
6ccb9162 17323 if (!name)
34877895 17324 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17325
08feed99 17326 arch = objfile->arch ();
103a685e
TT
17327 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17328
34877895
PJ
17329 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17330 if (attr)
103a685e
TT
17331 {
17332 int endianity = DW_UNSND (attr);
17333
17334 switch (endianity)
17335 {
17336 case DW_END_big:
17337 byte_order = BFD_ENDIAN_BIG;
17338 break;
17339 case DW_END_little:
17340 byte_order = BFD_ENDIAN_LITTLE;
17341 break;
17342 default:
17343 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17344 break;
17345 }
17346 }
6ccb9162
UW
17347
17348 switch (encoding)
c906108c 17349 {
6ccb9162
UW
17350 case DW_ATE_address:
17351 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17352 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17353 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17354 break;
17355 case DW_ATE_boolean:
19f392bc 17356 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17357 break;
17358 case DW_ATE_complex_float:
103a685e
TT
17359 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17360 byte_order);
78134374 17361 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17362 {
17363 if (name == nullptr)
17364 {
17365 struct obstack *obstack
17366 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
7d93a1e0 17367 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17368 nullptr);
17369 }
17370 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17371 }
17372 else
17373 type = init_complex_type (name, type);
6ccb9162
UW
17374 break;
17375 case DW_ATE_decimal_float:
19f392bc 17376 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17377 break;
17378 case DW_ATE_float:
103a685e 17379 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17380 break;
17381 case DW_ATE_signed:
eb77c9df 17382 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17383 break;
17384 case DW_ATE_unsigned:
3b2b8fea
TT
17385 if (cu->language == language_fortran
17386 && name
61012eef 17387 && startswith (name, "character("))
19f392bc
UW
17388 type = init_character_type (objfile, bits, 1, name);
17389 else
eb77c9df 17390 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17391 break;
17392 case DW_ATE_signed_char:
6e70227d 17393 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17394 || cu->language == language_pascal
17395 || cu->language == language_fortran)
19f392bc
UW
17396 type = init_character_type (objfile, bits, 0, name);
17397 else
eb77c9df 17398 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17399 break;
17400 case DW_ATE_unsigned_char:
868a0084 17401 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17402 || cu->language == language_pascal
c44af4eb
TT
17403 || cu->language == language_fortran
17404 || cu->language == language_rust)
19f392bc
UW
17405 type = init_character_type (objfile, bits, 1, name);
17406 else
eb77c9df 17407 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17408 break;
75079b2b 17409 case DW_ATE_UTF:
53e710ac 17410 {
53e710ac
PA
17411 if (bits == 16)
17412 type = builtin_type (arch)->builtin_char16;
17413 else if (bits == 32)
17414 type = builtin_type (arch)->builtin_char32;
17415 else
17416 {
b98664d3 17417 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17418 bits);
eb77c9df 17419 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17420 }
17421 return set_die_type (die, type, cu);
17422 }
75079b2b
TT
17423 break;
17424
6ccb9162 17425 default:
b98664d3 17426 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17427 dwarf_type_encoding_name (encoding));
77b7c781 17428 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17429 break;
c906108c 17430 }
6ccb9162 17431
0114d602 17432 if (name && strcmp (name, "char") == 0)
876cecd0 17433 TYPE_NOSIGN (type) = 1;
0114d602 17434
2b4424c3
TT
17435 maybe_set_alignment (cu, die, type);
17436
103a685e 17437 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17438
f792889a 17439 return set_die_type (die, type, cu);
c906108c
SS
17440}
17441
80180f79
SA
17442/* Parse dwarf attribute if it's a block, reference or constant and put the
17443 resulting value of the attribute into struct bound_prop.
17444 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17445
17446static int
17447attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17448 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17449 struct type *default_type)
80180f79
SA
17450{
17451 struct dwarf2_property_baton *baton;
518817b3
SM
17452 struct obstack *obstack
17453 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17454
9a49df9d
AB
17455 gdb_assert (default_type != NULL);
17456
80180f79
SA
17457 if (attr == NULL || prop == NULL)
17458 return 0;
17459
4fc6c0d5 17460 if (attr->form_is_block ())
80180f79 17461 {
8d749320 17462 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17463 baton->property_type = default_type;
80180f79
SA
17464 baton->locexpr.per_cu = cu->per_cu;
17465 baton->locexpr.size = DW_BLOCK (attr)->size;
17466 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17467 switch (attr->name)
17468 {
17469 case DW_AT_string_length:
17470 baton->locexpr.is_reference = true;
17471 break;
17472 default:
17473 baton->locexpr.is_reference = false;
17474 break;
17475 }
80180f79
SA
17476 prop->data.baton = baton;
17477 prop->kind = PROP_LOCEXPR;
17478 gdb_assert (prop->data.baton != NULL);
17479 }
cd6c91b4 17480 else if (attr->form_is_ref ())
80180f79
SA
17481 {
17482 struct dwarf2_cu *target_cu = cu;
17483 struct die_info *target_die;
17484 struct attribute *target_attr;
17485
17486 target_die = follow_die_ref (die, attr, &target_cu);
17487 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17488 if (target_attr == NULL)
17489 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17490 target_cu);
80180f79
SA
17491 if (target_attr == NULL)
17492 return 0;
17493
df25ebbd 17494 switch (target_attr->name)
80180f79 17495 {
df25ebbd 17496 case DW_AT_location:
cd6c91b4 17497 if (target_attr->form_is_section_offset ())
df25ebbd 17498 {
8d749320 17499 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17500 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17501 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17502 prop->data.baton = baton;
17503 prop->kind = PROP_LOCLIST;
17504 gdb_assert (prop->data.baton != NULL);
17505 }
4fc6c0d5 17506 else if (target_attr->form_is_block ())
df25ebbd 17507 {
8d749320 17508 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17509 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17510 baton->locexpr.per_cu = cu->per_cu;
17511 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17512 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17513 baton->locexpr.is_reference = true;
df25ebbd
JB
17514 prop->data.baton = baton;
17515 prop->kind = PROP_LOCEXPR;
17516 gdb_assert (prop->data.baton != NULL);
17517 }
17518 else
17519 {
17520 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17521 "dynamic property");
17522 return 0;
17523 }
17524 break;
17525 case DW_AT_data_member_location:
17526 {
17527 LONGEST offset;
17528
17529 if (!handle_data_member_location (target_die, target_cu,
17530 &offset))
17531 return 0;
17532
8d749320 17533 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17534 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17535 target_cu);
df25ebbd
JB
17536 baton->offset_info.offset = offset;
17537 baton->offset_info.type = die_type (target_die, target_cu);
17538 prop->data.baton = baton;
17539 prop->kind = PROP_ADDR_OFFSET;
17540 break;
17541 }
80180f79
SA
17542 }
17543 }
cd6c91b4 17544 else if (attr->form_is_constant ())
80180f79 17545 {
0826b30a 17546 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17547 prop->kind = PROP_CONST;
17548 }
17549 else
17550 {
17551 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17552 dwarf2_name (die, cu));
17553 return 0;
17554 }
17555
17556 return 1;
17557}
17558
09ba997f 17559/* See read.h. */
9a49df9d 17560
09ba997f
TT
17561struct type *
17562dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17563{
09ba997f 17564 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17565 struct type *int_type;
17566
17567 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17568#define TRY_TYPE(F) \
17569 int_type = (unsigned_p \
17570 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17571 : objfile_type (objfile)->builtin_ ## F); \
17572 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17573 return int_type
17574
17575 TRY_TYPE (char);
17576 TRY_TYPE (short);
17577 TRY_TYPE (int);
17578 TRY_TYPE (long);
17579 TRY_TYPE (long_long);
17580
17581#undef TRY_TYPE
17582
17583 gdb_assert_not_reached ("unable to find suitable integer type");
17584}
17585
09ba997f 17586/* See read.h. */
11a8b164 17587
09ba997f
TT
17588struct type *
17589dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17590{
09ba997f
TT
17591 int addr_size = this->addr_size ();
17592 return int_type (addr_size, unsigned_p);
11a8b164
AB
17593}
17594
b86352cf
AB
17595/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17596 present (which is valid) then compute the default type based on the
17597 compilation units address size. */
17598
17599static struct type *
17600read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17601{
17602 struct type *index_type = die_type (die, cu);
17603
17604 /* Dwarf-2 specifications explicitly allows to create subrange types
17605 without specifying a base type.
17606 In that case, the base type must be set to the type of
17607 the lower bound, upper bound or count, in that order, if any of these
17608 three attributes references an object that has a type.
17609 If no base type is found, the Dwarf-2 specifications say that
17610 a signed integer type of size equal to the size of an address should
17611 be used.
17612 For the following C code: `extern char gdb_int [];'
17613 GCC produces an empty range DIE.
17614 FIXME: muller/2010-05-28: Possible references to object for low bound,
17615 high bound or count are not yet handled by this code. */
78134374 17616 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17617 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17618
17619 return index_type;
17620}
17621
a02abb62
JB
17622/* Read the given DW_AT_subrange DIE. */
17623
f792889a 17624static struct type *
a02abb62
JB
17625read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17626{
4c9ad8c2 17627 struct type *base_type, *orig_base_type;
a02abb62
JB
17628 struct type *range_type;
17629 struct attribute *attr;
729efb13 17630 struct dynamic_prop low, high;
4fae6e18 17631 int low_default_is_valid;
c451ebe5 17632 int high_bound_is_count = 0;
15d034d0 17633 const char *name;
d359392f 17634 ULONGEST negative_mask;
e77813c8 17635
b86352cf
AB
17636 orig_base_type = read_subrange_index_type (die, cu);
17637
4c9ad8c2
TT
17638 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17639 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17640 creating the range type, but we use the result of check_typedef
17641 when examining properties of the type. */
17642 base_type = check_typedef (orig_base_type);
a02abb62 17643
7e314c57
JK
17644 /* The die_type call above may have already set the type for this DIE. */
17645 range_type = get_die_type (die, cu);
17646 if (range_type)
17647 return range_type;
17648
729efb13
SA
17649 low.kind = PROP_CONST;
17650 high.kind = PROP_CONST;
17651 high.data.const_val = 0;
17652
4fae6e18
JK
17653 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17654 omitting DW_AT_lower_bound. */
17655 switch (cu->language)
6e70227d 17656 {
4fae6e18
JK
17657 case language_c:
17658 case language_cplus:
729efb13 17659 low.data.const_val = 0;
4fae6e18
JK
17660 low_default_is_valid = 1;
17661 break;
17662 case language_fortran:
729efb13 17663 low.data.const_val = 1;
4fae6e18
JK
17664 low_default_is_valid = 1;
17665 break;
17666 case language_d:
4fae6e18 17667 case language_objc:
c44af4eb 17668 case language_rust:
729efb13 17669 low.data.const_val = 0;
4fae6e18
JK
17670 low_default_is_valid = (cu->header.version >= 4);
17671 break;
17672 case language_ada:
17673 case language_m2:
17674 case language_pascal:
729efb13 17675 low.data.const_val = 1;
4fae6e18
JK
17676 low_default_is_valid = (cu->header.version >= 4);
17677 break;
17678 default:
729efb13 17679 low.data.const_val = 0;
4fae6e18
JK
17680 low_default_is_valid = 0;
17681 break;
a02abb62
JB
17682 }
17683
e142c38c 17684 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17685 if (attr != nullptr)
9a49df9d 17686 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17687 else if (!low_default_is_valid)
b98664d3 17688 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17689 "- DIE at %s [in module %s]"),
17690 sect_offset_str (die->sect_off),
518817b3 17691 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17692
506f5c41
TV
17693 struct attribute *attr_ub, *attr_count;
17694 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17695 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17696 {
506f5c41 17697 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17698 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17699 {
c451ebe5
SA
17700 /* If bounds are constant do the final calculation here. */
17701 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17702 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17703 else
17704 high_bound_is_count = 1;
c2ff108b 17705 }
506f5c41
TV
17706 else
17707 {
17708 if (attr_ub != NULL)
17709 complaint (_("Unresolved DW_AT_upper_bound "
17710 "- DIE at %s [in module %s]"),
17711 sect_offset_str (die->sect_off),
17712 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17713 if (attr_count != NULL)
17714 complaint (_("Unresolved DW_AT_count "
17715 "- DIE at %s [in module %s]"),
17716 sect_offset_str (die->sect_off),
17717 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17718 }
e77813c8 17719 }
a02abb62 17720
4e962e74
TT
17721 LONGEST bias = 0;
17722 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17723 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17724 bias = bias_attr->constant_value (0);
4e962e74 17725
dbb9c2b1
JB
17726 /* Normally, the DWARF producers are expected to use a signed
17727 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17728 But this is unfortunately not always the case, as witnessed
17729 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17730 is used instead. To work around that ambiguity, we treat
17731 the bounds as signed, and thus sign-extend their values, when
17732 the base type is signed. */
6e70227d 17733 negative_mask =
d359392f 17734 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17735 if (low.kind == PROP_CONST
17736 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17737 low.data.const_val |= negative_mask;
17738 if (high.kind == PROP_CONST
17739 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17740 high.data.const_val |= negative_mask;
43bbcdc2 17741
5bbd8269
AB
17742 /* Check for bit and byte strides. */
17743 struct dynamic_prop byte_stride_prop;
17744 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17745 if (attr_byte_stride != nullptr)
17746 {
09ba997f 17747 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17748 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17749 prop_type);
17750 }
17751
17752 struct dynamic_prop bit_stride_prop;
17753 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17754 if (attr_bit_stride != nullptr)
17755 {
17756 /* It only makes sense to have either a bit or byte stride. */
17757 if (attr_byte_stride != nullptr)
17758 {
17759 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17760 "- DIE at %s [in module %s]"),
17761 sect_offset_str (die->sect_off),
17762 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17763 attr_bit_stride = nullptr;
17764 }
17765 else
17766 {
09ba997f 17767 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17768 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17769 prop_type);
17770 }
17771 }
17772
17773 if (attr_byte_stride != nullptr
17774 || attr_bit_stride != nullptr)
17775 {
17776 bool byte_stride_p = (attr_byte_stride != nullptr);
17777 struct dynamic_prop *stride
17778 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17779
17780 range_type
17781 = create_range_type_with_stride (NULL, orig_base_type, &low,
17782 &high, bias, stride, byte_stride_p);
17783 }
17784 else
17785 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17786
c451ebe5
SA
17787 if (high_bound_is_count)
17788 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17789
c2ff108b
JK
17790 /* Ada expects an empty array on no boundary attributes. */
17791 if (attr == NULL && cu->language != language_ada)
729efb13 17792 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17793
39cbfefa
DJ
17794 name = dwarf2_name (die, cu);
17795 if (name)
d0e39ea2 17796 range_type->set_name (name);
6e70227d 17797
e142c38c 17798 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17799 if (attr != nullptr)
a02abb62
JB
17800 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17801
2b4424c3
TT
17802 maybe_set_alignment (cu, die, range_type);
17803
7e314c57
JK
17804 set_die_type (die, range_type, cu);
17805
17806 /* set_die_type should be already done. */
b4ba55a1
JB
17807 set_descriptive_type (range_type, die, cu);
17808
7e314c57 17809 return range_type;
a02abb62 17810}
6e70227d 17811
f792889a 17812static struct type *
81a17f79
JB
17813read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17814{
17815 struct type *type;
81a17f79 17816
518817b3
SM
17817 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17818 NULL);
d0e39ea2 17819 type->set_name (dwarf2_name (die, cu));
81a17f79 17820
74a2f8ff 17821 /* In Ada, an unspecified type is typically used when the description
85102364 17822 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17823 such a type, we treat it as a stub, and try to resolve it later on,
17824 when needed. */
17825 if (cu->language == language_ada)
17826 TYPE_STUB (type) = 1;
17827
f792889a 17828 return set_die_type (die, type, cu);
81a17f79 17829}
a02abb62 17830
639d11d3
DC
17831/* Read a single die and all its descendents. Set the die's sibling
17832 field to NULL; set other fields in the die correctly, and set all
17833 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17834 location of the info_ptr after reading all of those dies. PARENT
17835 is the parent of the die in question. */
17836
17837static struct die_info *
dee91e82 17838read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17839 const gdb_byte *info_ptr,
17840 const gdb_byte **new_info_ptr,
dee91e82 17841 struct die_info *parent)
639d11d3
DC
17842{
17843 struct die_info *die;
d521ce57 17844 const gdb_byte *cur_ptr;
639d11d3 17845
3e225074 17846 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17847 if (die == NULL)
17848 {
17849 *new_info_ptr = cur_ptr;
17850 return NULL;
17851 }
93311388 17852 store_in_ref_table (die, reader->cu);
639d11d3 17853
3e225074 17854 if (die->has_children)
bf6af496 17855 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17856 else
17857 {
17858 die->child = NULL;
17859 *new_info_ptr = cur_ptr;
17860 }
17861
17862 die->sibling = NULL;
17863 die->parent = parent;
17864 return die;
17865}
17866
17867/* Read a die, all of its descendents, and all of its siblings; set
17868 all of the fields of all of the dies correctly. Arguments are as
17869 in read_die_and_children. */
17870
17871static struct die_info *
bf6af496 17872read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17873 const gdb_byte *info_ptr,
17874 const gdb_byte **new_info_ptr,
bf6af496 17875 struct die_info *parent)
639d11d3
DC
17876{
17877 struct die_info *first_die, *last_sibling;
d521ce57 17878 const gdb_byte *cur_ptr;
639d11d3 17879
c906108c 17880 cur_ptr = info_ptr;
639d11d3
DC
17881 first_die = last_sibling = NULL;
17882
17883 while (1)
c906108c 17884 {
639d11d3 17885 struct die_info *die
dee91e82 17886 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17887
1d325ec1 17888 if (die == NULL)
c906108c 17889 {
639d11d3
DC
17890 *new_info_ptr = cur_ptr;
17891 return first_die;
c906108c 17892 }
1d325ec1
DJ
17893
17894 if (!first_die)
17895 first_die = die;
c906108c 17896 else
1d325ec1
DJ
17897 last_sibling->sibling = die;
17898
17899 last_sibling = die;
c906108c 17900 }
c906108c
SS
17901}
17902
bf6af496
DE
17903/* Read a die, all of its descendents, and all of its siblings; set
17904 all of the fields of all of the dies correctly. Arguments are as
17905 in read_die_and_children.
17906 This the main entry point for reading a DIE and all its children. */
17907
17908static struct die_info *
17909read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17910 const gdb_byte *info_ptr,
17911 const gdb_byte **new_info_ptr,
bf6af496
DE
17912 struct die_info *parent)
17913{
17914 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17915 new_info_ptr, parent);
17916
b4f54984 17917 if (dwarf_die_debug)
bf6af496
DE
17918 {
17919 fprintf_unfiltered (gdb_stdlog,
17920 "Read die from %s@0x%x of %s:\n",
96b79293 17921 reader->die_section->get_name (),
bf6af496
DE
17922 (unsigned) (info_ptr - reader->die_section->buffer),
17923 bfd_get_filename (reader->abfd));
b4f54984 17924 dump_die (die, dwarf_die_debug);
bf6af496
DE
17925 }
17926
17927 return die;
17928}
17929
3019eac3
DE
17930/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17931 attributes.
17932 The caller is responsible for filling in the extra attributes
17933 and updating (*DIEP)->num_attrs.
17934 Set DIEP to point to a newly allocated die with its information,
3e225074 17935 except for its child, sibling, and parent fields. */
93311388 17936
d521ce57 17937static const gdb_byte *
3019eac3 17938read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17939 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17940 int num_extra_attrs)
93311388 17941{
b64f50a1 17942 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17943 struct abbrev_info *abbrev;
17944 struct die_info *die;
17945 struct dwarf2_cu *cu = reader->cu;
17946 bfd *abfd = reader->abfd;
17947
9c541725 17948 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17949 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17950 info_ptr += bytes_read;
17951 if (!abbrev_number)
17952 {
17953 *diep = NULL;
93311388
DE
17954 return info_ptr;
17955 }
17956
685af9cd 17957 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17958 if (!abbrev)
348e048f
DE
17959 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17960 abbrev_number,
17961 bfd_get_filename (abfd));
17962
3019eac3 17963 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17964 die->sect_off = sect_off;
93311388
DE
17965 die->tag = abbrev->tag;
17966 die->abbrev = abbrev_number;
3e225074 17967 die->has_children = abbrev->has_children;
93311388 17968
3019eac3
DE
17969 /* Make the result usable.
17970 The caller needs to update num_attrs after adding the extra
17971 attributes. */
93311388
DE
17972 die->num_attrs = abbrev->num_attrs;
17973
18a8505e 17974 std::vector<int> indexes_that_need_reprocess;
93311388 17975 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17976 {
17977 bool need_reprocess;
17978 info_ptr =
17979 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17980 info_ptr, &need_reprocess);
17981 if (need_reprocess)
17982 indexes_that_need_reprocess.push_back (i);
17983 }
17984
052c8bb8 17985 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17986 if (attr != nullptr)
17987 cu->str_offsets_base = DW_UNSND (attr);
93311388 17988
41144253 17989 attr = die->attr (DW_AT_loclists_base);
17990 if (attr != nullptr)
17991 cu->loclist_base = DW_UNSND (attr);
17992
a39fdb41 17993 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17994 if (maybe_addr_base.has_value ())
17995 cu->addr_base = *maybe_addr_base;
17996 for (int index : indexes_that_need_reprocess)
17997 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17998 *diep = die;
93311388
DE
17999 return info_ptr;
18000}
18001
3019eac3
DE
18002/* Read a die and all its attributes.
18003 Set DIEP to point to a newly allocated die with its information,
3e225074 18004 except for its child, sibling, and parent fields. */
3019eac3 18005
d521ce57 18006static const gdb_byte *
3019eac3 18007read_full_die (const struct die_reader_specs *reader,
3e225074 18008 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18009{
d521ce57 18010 const gdb_byte *result;
bf6af496 18011
3e225074 18012 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18013
b4f54984 18014 if (dwarf_die_debug)
bf6af496
DE
18015 {
18016 fprintf_unfiltered (gdb_stdlog,
18017 "Read die from %s@0x%x of %s:\n",
96b79293 18018 reader->die_section->get_name (),
bf6af496
DE
18019 (unsigned) (info_ptr - reader->die_section->buffer),
18020 bfd_get_filename (reader->abfd));
b4f54984 18021 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18022 }
18023
18024 return result;
3019eac3 18025}
433df2d4 18026\f
c906108c 18027
72bf9492
DJ
18028/* Returns nonzero if TAG represents a type that we might generate a partial
18029 symbol for. */
18030
18031static int
18032is_type_tag_for_partial (int tag)
18033{
18034 switch (tag)
18035 {
18036#if 0
18037 /* Some types that would be reasonable to generate partial symbols for,
18038 that we don't at present. */
18039 case DW_TAG_array_type:
18040 case DW_TAG_file_type:
18041 case DW_TAG_ptr_to_member_type:
18042 case DW_TAG_set_type:
18043 case DW_TAG_string_type:
18044 case DW_TAG_subroutine_type:
18045#endif
18046 case DW_TAG_base_type:
18047 case DW_TAG_class_type:
680b30c7 18048 case DW_TAG_interface_type:
72bf9492
DJ
18049 case DW_TAG_enumeration_type:
18050 case DW_TAG_structure_type:
18051 case DW_TAG_subrange_type:
18052 case DW_TAG_typedef:
18053 case DW_TAG_union_type:
18054 return 1;
18055 default:
18056 return 0;
18057 }
18058}
18059
18060/* Load all DIEs that are interesting for partial symbols into memory. */
18061
18062static struct partial_die_info *
dee91e82 18063load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18064 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18065{
dee91e82 18066 struct dwarf2_cu *cu = reader->cu;
518817b3 18067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18068 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18069 unsigned int bytes_read;
5afb4e99 18070 unsigned int load_all = 0;
72bf9492
DJ
18071 int nesting_level = 1;
18072
18073 parent_die = NULL;
18074 last_die = NULL;
18075
7adf1e79
DE
18076 gdb_assert (cu->per_cu != NULL);
18077 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18078 load_all = 1;
18079
72bf9492
DJ
18080 cu->partial_dies
18081 = htab_create_alloc_ex (cu->header.length / 12,
18082 partial_die_hash,
18083 partial_die_eq,
18084 NULL,
18085 &cu->comp_unit_obstack,
18086 hashtab_obstack_allocate,
18087 dummy_obstack_deallocate);
18088
72bf9492
DJ
18089 while (1)
18090 {
685af9cd 18091 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18092
18093 /* A NULL abbrev means the end of a series of children. */
18094 if (abbrev == NULL)
18095 {
18096 if (--nesting_level == 0)
cd9983dd
YQ
18097 return first_die;
18098
72bf9492
DJ
18099 info_ptr += bytes_read;
18100 last_die = parent_die;
18101 parent_die = parent_die->die_parent;
18102 continue;
18103 }
18104
98bfdba5
PA
18105 /* Check for template arguments. We never save these; if
18106 they're seen, we just mark the parent, and go on our way. */
18107 if (parent_die != NULL
18108 && cu->language == language_cplus
18109 && (abbrev->tag == DW_TAG_template_type_param
18110 || abbrev->tag == DW_TAG_template_value_param))
18111 {
18112 parent_die->has_template_arguments = 1;
18113
18114 if (!load_all)
18115 {
18116 /* We don't need a partial DIE for the template argument. */
dee91e82 18117 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18118 continue;
18119 }
18120 }
18121
0d99eb77 18122 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18123 Skip their other children. */
18124 if (!load_all
18125 && cu->language == language_cplus
18126 && parent_die != NULL
18127 && parent_die->tag == DW_TAG_subprogram)
18128 {
dee91e82 18129 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18130 continue;
18131 }
18132
5afb4e99
DJ
18133 /* Check whether this DIE is interesting enough to save. Normally
18134 we would not be interested in members here, but there may be
18135 later variables referencing them via DW_AT_specification (for
18136 static members). */
18137 if (!load_all
18138 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18139 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18140 && abbrev->tag != DW_TAG_enumerator
18141 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18142 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18143 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18144 && abbrev->tag != DW_TAG_variable
5afb4e99 18145 && abbrev->tag != DW_TAG_namespace
f55ee35c 18146 && abbrev->tag != DW_TAG_module
95554aad 18147 && abbrev->tag != DW_TAG_member
74921315
KS
18148 && abbrev->tag != DW_TAG_imported_unit
18149 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18150 {
18151 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18152 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18153 continue;
18154 }
18155
6f06d47b
YQ
18156 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18157 abbrev);
cd9983dd 18158
48fbe735 18159 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18160
18161 /* This two-pass algorithm for processing partial symbols has a
18162 high cost in cache pressure. Thus, handle some simple cases
18163 here which cover the majority of C partial symbols. DIEs
18164 which neither have specification tags in them, nor could have
18165 specification tags elsewhere pointing at them, can simply be
18166 processed and discarded.
18167
18168 This segment is also optional; scan_partial_symbols and
18169 add_partial_symbol will handle these DIEs if we chain
18170 them in normally. When compilers which do not emit large
18171 quantities of duplicate debug information are more common,
18172 this code can probably be removed. */
18173
18174 /* Any complete simple types at the top level (pretty much all
18175 of them, for a language without namespaces), can be processed
18176 directly. */
18177 if (parent_die == NULL
cd9983dd
YQ
18178 && pdi.has_specification == 0
18179 && pdi.is_declaration == 0
18180 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18181 || pdi.tag == DW_TAG_base_type
18182 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18183 {
cd9983dd 18184 if (building_psymtab && pdi.name != NULL)
31edb802 18185 add_psymbol_to_list (pdi.name, false,
79748972 18186 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18187 psymbol_placement::STATIC,
1762568f 18188 0, cu->language, objfile);
cd9983dd 18189 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18190 continue;
18191 }
18192
d8228535
JK
18193 /* The exception for DW_TAG_typedef with has_children above is
18194 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18195 type_name_or_error will error on such types later.
d8228535
JK
18196
18197 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18198 it could not find the child DIEs referenced later, this is checked
18199 above. In correct DWARF DW_TAG_typedef should have no children. */
18200
cd9983dd 18201 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18202 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18203 "- DIE at %s [in module %s]"),
cd9983dd 18204 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18205
72bf9492
DJ
18206 /* If we're at the second level, and we're an enumerator, and
18207 our parent has no specification (meaning possibly lives in a
18208 namespace elsewhere), then we can add the partial symbol now
18209 instead of queueing it. */
cd9983dd 18210 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18211 && parent_die != NULL
18212 && parent_die->die_parent == NULL
18213 && parent_die->tag == DW_TAG_enumeration_type
18214 && parent_die->has_specification == 0)
18215 {
cd9983dd 18216 if (pdi.name == NULL)
b98664d3 18217 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18218 else if (building_psymtab)
31edb802 18219 add_psymbol_to_list (pdi.name, false,
79748972 18220 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18221 cu->language == language_cplus
75aedd27
TT
18222 ? psymbol_placement::GLOBAL
18223 : psymbol_placement::STATIC,
1762568f 18224 0, cu->language, objfile);
72bf9492 18225
cd9983dd 18226 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18227 continue;
18228 }
18229
cd9983dd 18230 struct partial_die_info *part_die
6f06d47b 18231 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18232
72bf9492
DJ
18233 /* We'll save this DIE so link it in. */
18234 part_die->die_parent = parent_die;
18235 part_die->die_sibling = NULL;
18236 part_die->die_child = NULL;
18237
18238 if (last_die && last_die == parent_die)
18239 last_die->die_child = part_die;
18240 else if (last_die)
18241 last_die->die_sibling = part_die;
18242
18243 last_die = part_die;
18244
18245 if (first_die == NULL)
18246 first_die = part_die;
18247
18248 /* Maybe add the DIE to the hash table. Not all DIEs that we
18249 find interesting need to be in the hash table, because we
18250 also have the parent/sibling/child chains; only those that we
18251 might refer to by offset later during partial symbol reading.
18252
18253 For now this means things that might have be the target of a
18254 DW_AT_specification, DW_AT_abstract_origin, or
18255 DW_AT_extension. DW_AT_extension will refer only to
18256 namespaces; DW_AT_abstract_origin refers to functions (and
18257 many things under the function DIE, but we do not recurse
18258 into function DIEs during partial symbol reading) and
18259 possibly variables as well; DW_AT_specification refers to
18260 declarations. Declarations ought to have the DW_AT_declaration
18261 flag. It happens that GCC forgets to put it in sometimes, but
18262 only for functions, not for types.
18263
18264 Adding more things than necessary to the hash table is harmless
18265 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18266 wasted time in find_partial_die, when we reread the compilation
18267 unit with load_all_dies set. */
72bf9492 18268
5afb4e99 18269 if (load_all
72929c62 18270 || abbrev->tag == DW_TAG_constant
5afb4e99 18271 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18272 || abbrev->tag == DW_TAG_variable
18273 || abbrev->tag == DW_TAG_namespace
18274 || part_die->is_declaration)
18275 {
18276 void **slot;
18277
18278 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18279 to_underlying (part_die->sect_off),
18280 INSERT);
72bf9492
DJ
18281 *slot = part_die;
18282 }
18283
72bf9492 18284 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18285 we have no reason to follow the children of structures; for other
98bfdba5
PA
18286 languages we have to, so that we can get at method physnames
18287 to infer fully qualified class names, for DW_AT_specification,
18288 and for C++ template arguments. For C++, we also look one level
18289 inside functions to find template arguments (if the name of the
18290 function does not already contain the template arguments).
bc30ff58 18291
0a4b0913
AB
18292 For Ada and Fortran, we need to scan the children of subprograms
18293 and lexical blocks as well because these languages allow the
18294 definition of nested entities that could be interesting for the
18295 debugger, such as nested subprograms for instance. */
72bf9492 18296 if (last_die->has_children
5afb4e99
DJ
18297 && (load_all
18298 || last_die->tag == DW_TAG_namespace
f55ee35c 18299 || last_die->tag == DW_TAG_module
72bf9492 18300 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18301 || (cu->language == language_cplus
18302 && last_die->tag == DW_TAG_subprogram
18303 && (last_die->name == NULL
18304 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18305 || (cu->language != language_c
18306 && (last_die->tag == DW_TAG_class_type
680b30c7 18307 || last_die->tag == DW_TAG_interface_type
72bf9492 18308 || last_die->tag == DW_TAG_structure_type
bc30ff58 18309 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18310 || ((cu->language == language_ada
18311 || cu->language == language_fortran)
bc30ff58
JB
18312 && (last_die->tag == DW_TAG_subprogram
18313 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18314 {
18315 nesting_level++;
18316 parent_die = last_die;
18317 continue;
18318 }
18319
18320 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18321 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18322
18323 /* Back to the top, do it again. */
18324 }
18325}
18326
6f06d47b
YQ
18327partial_die_info::partial_die_info (sect_offset sect_off_,
18328 struct abbrev_info *abbrev)
18329 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18330{
18331}
18332
35cc7ed7
YQ
18333/* Read a minimal amount of information into the minimal die structure.
18334 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18335
48fbe735
YQ
18336const gdb_byte *
18337partial_die_info::read (const struct die_reader_specs *reader,
18338 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18339{
dee91e82 18340 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18341 struct dwarf2_per_objfile *dwarf2_per_objfile
18342 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18343 unsigned int i;
c5aa993b 18344 int has_low_pc_attr = 0;
c906108c 18345 int has_high_pc_attr = 0;
91da1414 18346 int high_pc_relative = 0;
c906108c 18347
fd0a254f 18348 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18349 {
e7da7f8f 18350 attribute attr;
18a8505e 18351 bool need_reprocess;
e7da7f8f 18352 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18353 info_ptr, &need_reprocess);
18354 /* String and address offsets that need to do the reprocessing have
18355 already been read at this point, so there is no need to wait until
18356 the loop terminates to do the reprocessing. */
18357 if (need_reprocess)
e7da7f8f 18358 read_attribute_reprocess (reader, &attr);
c906108c 18359 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18360 partial symbol table. */
c906108c
SS
18361 switch (attr.name)
18362 {
18363 case DW_AT_name:
48fbe735 18364 switch (tag)
71c25dea
TT
18365 {
18366 case DW_TAG_compile_unit:
95554aad 18367 case DW_TAG_partial_unit:
348e048f 18368 case DW_TAG_type_unit:
71c25dea
TT
18369 /* Compilation units have a DW_AT_name that is a filename, not
18370 a source language identifier. */
18371 case DW_TAG_enumeration_type:
18372 case DW_TAG_enumerator:
18373 /* These tags always have simple identifiers already; no need
18374 to canonicalize them. */
48fbe735 18375 name = DW_STRING (&attr);
71c25dea
TT
18376 break;
18377 default:
48fbe735
YQ
18378 {
18379 struct objfile *objfile = dwarf2_per_objfile->objfile;
18380
18381 name
be1e3d3e 18382 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18383 }
71c25dea
TT
18384 break;
18385 }
c906108c 18386 break;
31ef98ae 18387 case DW_AT_linkage_name:
c906108c 18388 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18389 /* Note that both forms of linkage name might appear. We
18390 assume they will be the same, and we only store the last
18391 one we see. */
e61108c9 18392 linkage_name = attr.value_as_string ();
787de330
TT
18393 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18394 See https://github.com/rust-lang/rust/issues/32925. */
18395 if (cu->language == language_rust && linkage_name != NULL
18396 && strchr (linkage_name, '{') != NULL)
18397 linkage_name = NULL;
c906108c
SS
18398 break;
18399 case DW_AT_low_pc:
18400 has_low_pc_attr = 1;
cd6c91b4 18401 lowpc = attr.value_as_address ();
c906108c
SS
18402 break;
18403 case DW_AT_high_pc:
18404 has_high_pc_attr = 1;
cd6c91b4
TT
18405 highpc = attr.value_as_address ();
18406 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18407 high_pc_relative = 1;
c906108c
SS
18408 break;
18409 case DW_AT_location:
0963b4bd 18410 /* Support the .debug_loc offsets. */
4fc6c0d5 18411 if (attr.form_is_block ())
8e19ed76 18412 {
48fbe735 18413 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18414 }
cd6c91b4 18415 else if (attr.form_is_section_offset ())
8e19ed76 18416 {
4d3c2250 18417 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18418 }
18419 else
18420 {
4d3c2250
KB
18421 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18422 "partial symbol information");
8e19ed76 18423 }
c906108c 18424 break;
c906108c 18425 case DW_AT_external:
48fbe735 18426 is_external = DW_UNSND (&attr);
c906108c
SS
18427 break;
18428 case DW_AT_declaration:
48fbe735 18429 is_declaration = DW_UNSND (&attr);
c906108c
SS
18430 break;
18431 case DW_AT_type:
48fbe735 18432 has_type = 1;
c906108c
SS
18433 break;
18434 case DW_AT_abstract_origin:
18435 case DW_AT_specification:
72bf9492 18436 case DW_AT_extension:
48fbe735 18437 has_specification = 1;
0826b30a 18438 spec_offset = attr.get_ref_die_offset ();
48fbe735 18439 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18440 || cu->per_cu->is_dwz);
c906108c
SS
18441 break;
18442 case DW_AT_sibling:
18443 /* Ignore absolute siblings, they might point outside of
18444 the current compile unit. */
18445 if (attr.form == DW_FORM_ref_addr)
b98664d3 18446 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18447 else
b9502d3f 18448 {
48fbe735 18449 const gdb_byte *buffer = reader->buffer;
0826b30a 18450 sect_offset off = attr.get_ref_die_offset ();
9c541725 18451 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18452
18453 if (sibling_ptr < info_ptr)
b98664d3 18454 complaint (_("DW_AT_sibling points backwards"));
22869d73 18455 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18456 reader->die_section->overflow_complaint ();
b9502d3f 18457 else
48fbe735 18458 sibling = sibling_ptr;
b9502d3f 18459 }
c906108c 18460 break;
fa4028e9 18461 case DW_AT_byte_size:
48fbe735 18462 has_byte_size = 1;
fa4028e9 18463 break;
ff908ebf 18464 case DW_AT_const_value:
48fbe735 18465 has_const_value = 1;
ff908ebf 18466 break;
68511cec
CES
18467 case DW_AT_calling_convention:
18468 /* DWARF doesn't provide a way to identify a program's source-level
18469 entry point. DW_AT_calling_convention attributes are only meant
18470 to describe functions' calling conventions.
18471
18472 However, because it's a necessary piece of information in
0c1b455e
TT
18473 Fortran, and before DWARF 4 DW_CC_program was the only
18474 piece of debugging information whose definition refers to
18475 a 'main program' at all, several compilers marked Fortran
18476 main programs with DW_CC_program --- even when those
18477 functions use the standard calling conventions.
18478
18479 Although DWARF now specifies a way to provide this
18480 information, we support this practice for backward
18481 compatibility. */
68511cec 18482 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18483 && cu->language == language_fortran)
48fbe735 18484 main_subprogram = 1;
68511cec 18485 break;
481860b3
GB
18486 case DW_AT_inline:
18487 if (DW_UNSND (&attr) == DW_INL_inlined
18488 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18489 may_be_inlined = 1;
481860b3 18490 break;
95554aad
TT
18491
18492 case DW_AT_import:
48fbe735 18493 if (tag == DW_TAG_imported_unit)
36586728 18494 {
0826b30a 18495 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18496 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18497 || cu->per_cu->is_dwz);
18498 }
95554aad
TT
18499 break;
18500
0c1b455e 18501 case DW_AT_main_subprogram:
48fbe735 18502 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18503 break;
18504
05caa1d2
TT
18505 case DW_AT_ranges:
18506 {
18507 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18508 but that requires a full DIE, so instead we just
18509 reimplement it. */
18510 int need_ranges_base = tag != DW_TAG_compile_unit;
18511 unsigned int ranges_offset = (DW_UNSND (&attr)
18512 + (need_ranges_base
18513 ? cu->ranges_base
18514 : 0));
18515
18516 /* Value of the DW_AT_ranges attribute is the offset in the
18517 .debug_ranges section. */
18518 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18519 nullptr))
18520 has_pc_info = 1;
18521 }
18522 break;
18523
c906108c
SS
18524 default:
18525 break;
18526 }
18527 }
18528
10d06d82
TT
18529 /* For Ada, if both the name and the linkage name appear, we prefer
18530 the latter. This lets "catch exception" work better, regardless
18531 of the order in which the name and linkage name were emitted.
18532 Really, though, this is just a workaround for the fact that gdb
18533 doesn't store both the name and the linkage name. */
18534 if (cu->language == language_ada && linkage_name != nullptr)
18535 name = linkage_name;
18536
91da1414 18537 if (high_pc_relative)
48fbe735 18538 highpc += lowpc;
91da1414 18539
9373cf26
JK
18540 if (has_low_pc_attr && has_high_pc_attr)
18541 {
18542 /* When using the GNU linker, .gnu.linkonce. sections are used to
18543 eliminate duplicate copies of functions and vtables and such.
18544 The linker will arbitrarily choose one and discard the others.
18545 The AT_*_pc values for such functions refer to local labels in
18546 these sections. If the section from that file was discarded, the
18547 labels are not in the output, so the relocs get a value of 0.
18548 If this is a discarded function, mark the pc bounds as invalid,
18549 so that GDB will ignore it. */
48fbe735 18550 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18551 {
48fbe735 18552 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18553 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18554
b98664d3 18555 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18556 "for DIE at %s [in module %s]"),
48fbe735
YQ
18557 paddress (gdbarch, lowpc),
18558 sect_offset_str (sect_off),
9d8780f0 18559 objfile_name (objfile));
9373cf26
JK
18560 }
18561 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18562 else if (lowpc >= highpc)
9373cf26 18563 {
48fbe735 18564 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18565 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18566
b98664d3 18567 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18568 "for DIE at %s [in module %s]"),
48fbe735
YQ
18569 paddress (gdbarch, lowpc),
18570 paddress (gdbarch, highpc),
18571 sect_offset_str (sect_off),
9c541725 18572 objfile_name (objfile));
9373cf26
JK
18573 }
18574 else
48fbe735 18575 has_pc_info = 1;
9373cf26 18576 }
85cbf3d3 18577
c906108c
SS
18578 return info_ptr;
18579}
18580
72bf9492
DJ
18581/* Find a cached partial DIE at OFFSET in CU. */
18582
d590ff25
YQ
18583struct partial_die_info *
18584dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18585{
18586 struct partial_die_info *lookup_die = NULL;
6f06d47b 18587 struct partial_die_info part_die (sect_off);
72bf9492 18588
9a3c8263 18589 lookup_die = ((struct partial_die_info *)
d590ff25 18590 htab_find_with_hash (partial_dies, &part_die,
9c541725 18591 to_underlying (sect_off)));
72bf9492 18592
72bf9492
DJ
18593 return lookup_die;
18594}
18595
348e048f
DE
18596/* Find a partial DIE at OFFSET, which may or may not be in CU,
18597 except in the case of .debug_types DIEs which do not reference
18598 outside their CU (they do however referencing other types via
55f1336d 18599 DW_FORM_ref_sig8). */
72bf9492 18600
122cf0f2 18601static const struct cu_partial_die_info
9c541725 18602find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18603{
518817b3
SM
18604 struct dwarf2_per_objfile *dwarf2_per_objfile
18605 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18606 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18607 struct dwarf2_per_cu_data *per_cu = NULL;
18608 struct partial_die_info *pd = NULL;
72bf9492 18609
36586728 18610 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18611 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18612 {
d590ff25 18613 pd = cu->find_partial_die (sect_off);
5afb4e99 18614 if (pd != NULL)
fb816e8b 18615 return { cu, pd };
0d99eb77
DE
18616 /* We missed recording what we needed.
18617 Load all dies and try again. */
18618 per_cu = cu->per_cu;
5afb4e99 18619 }
0d99eb77
DE
18620 else
18621 {
18622 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18623 if (cu->per_cu->is_debug_types)
0d99eb77 18624 {
9d8780f0
SM
18625 error (_("Dwarf Error: Type Unit at offset %s contains"
18626 " external reference to offset %s [in module %s].\n"),
18627 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18628 bfd_get_filename (objfile->obfd));
18629 }
9c541725 18630 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18631 dwarf2_per_objfile);
72bf9492 18632
0d99eb77
DE
18633 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18634 load_partial_comp_unit (per_cu);
ae038cb0 18635
0d99eb77 18636 per_cu->cu->last_used = 0;
d590ff25 18637 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18638 }
5afb4e99 18639
dee91e82
DE
18640 /* If we didn't find it, and not all dies have been loaded,
18641 load them all and try again. */
18642
5afb4e99
DJ
18643 if (pd == NULL && per_cu->load_all_dies == 0)
18644 {
5afb4e99 18645 per_cu->load_all_dies = 1;
fd820528
DE
18646
18647 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18648 THIS_CU->cu may already be in use. So we can't just free it and
18649 replace its DIEs with the ones we read in. Instead, we leave those
18650 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18651 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18652 set. */
dee91e82 18653 load_partial_comp_unit (per_cu);
5afb4e99 18654
d590ff25 18655 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18656 }
18657
18658 if (pd == NULL)
18659 internal_error (__FILE__, __LINE__,
9d8780f0 18660 _("could not find partial DIE %s "
3e43a32a 18661 "in cache [from module %s]\n"),
9d8780f0 18662 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18663 return { per_cu->cu, pd };
72bf9492
DJ
18664}
18665
abc72ce4
DE
18666/* See if we can figure out if the class lives in a namespace. We do
18667 this by looking for a member function; its demangled name will
18668 contain namespace info, if there is any. */
18669
18670static void
18671guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18672 struct dwarf2_cu *cu)
18673{
18674 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18675 what template types look like, because the demangler
18676 frequently doesn't give the same name as the debug info. We
18677 could fix this by only using the demangled name to get the
18678 prefix (but see comment in read_structure_type). */
18679
18680 struct partial_die_info *real_pdi;
18681 struct partial_die_info *child_pdi;
18682
18683 /* If this DIE (this DIE's specification, if any) has a parent, then
18684 we should not do this. We'll prepend the parent's fully qualified
18685 name when we create the partial symbol. */
18686
18687 real_pdi = struct_pdi;
18688 while (real_pdi->has_specification)
fb816e8b 18689 {
122cf0f2
AB
18690 auto res = find_partial_die (real_pdi->spec_offset,
18691 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18692 real_pdi = res.pdi;
18693 cu = res.cu;
18694 }
abc72ce4
DE
18695
18696 if (real_pdi->die_parent != NULL)
18697 return;
18698
18699 for (child_pdi = struct_pdi->die_child;
18700 child_pdi != NULL;
18701 child_pdi = child_pdi->die_sibling)
18702 {
18703 if (child_pdi->tag == DW_TAG_subprogram
18704 && child_pdi->linkage_name != NULL)
18705 {
43816ebc
TT
18706 gdb::unique_xmalloc_ptr<char> actual_class_name
18707 (language_class_name_from_physname (cu->language_defn,
18708 child_pdi->linkage_name));
abc72ce4
DE
18709 if (actual_class_name != NULL)
18710 {
518817b3 18711 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18712 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18713 }
18714 break;
18715 }
18716 }
18717}
18718
25c11aca
TV
18719/* Return true if a DIE with TAG may have the DW_AT_const_value
18720 attribute. */
18721
18722static bool
18723can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18724{
18725 switch (tag)
18726 {
18727 case DW_TAG_constant:
18728 case DW_TAG_enumerator:
18729 case DW_TAG_formal_parameter:
18730 case DW_TAG_template_value_param:
18731 case DW_TAG_variable:
18732 return true;
18733 }
18734
18735 return false;
18736}
18737
52356b79
YQ
18738void
18739partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18740{
abc72ce4
DE
18741 /* Once we've fixed up a die, there's no point in doing so again.
18742 This also avoids a memory leak if we were to call
18743 guess_partial_die_structure_name multiple times. */
52356b79 18744 if (fixup_called)
abc72ce4
DE
18745 return;
18746
72bf9492
DJ
18747 /* If we found a reference attribute and the DIE has no name, try
18748 to find a name in the referred to DIE. */
18749
52356b79 18750 if (name == NULL && has_specification)
72bf9492
DJ
18751 {
18752 struct partial_die_info *spec_die;
72bf9492 18753
122cf0f2 18754 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18755 spec_die = res.pdi;
18756 cu = res.cu;
72bf9492 18757
52356b79 18758 spec_die->fixup (cu);
72bf9492
DJ
18759
18760 if (spec_die->name)
18761 {
52356b79 18762 name = spec_die->name;
72bf9492
DJ
18763
18764 /* Copy DW_AT_external attribute if it is set. */
18765 if (spec_die->is_external)
52356b79 18766 is_external = spec_die->is_external;
72bf9492
DJ
18767 }
18768 }
18769
25c11aca
TV
18770 if (!has_const_value && has_specification
18771 && can_have_DW_AT_const_value_p (tag))
18772 {
18773 struct partial_die_info *spec_die;
18774
18775 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18776 spec_die = res.pdi;
18777 cu = res.cu;
18778
18779 spec_die->fixup (cu);
18780
18781 if (spec_die->has_const_value)
18782 {
18783 /* Copy DW_AT_const_value attribute if it is set. */
18784 has_const_value = spec_die->has_const_value;
18785 }
18786 }
18787
72bf9492 18788 /* Set default names for some unnamed DIEs. */
72bf9492 18789
52356b79
YQ
18790 if (name == NULL && tag == DW_TAG_namespace)
18791 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18792
abc72ce4
DE
18793 /* If there is no parent die to provide a namespace, and there are
18794 children, see if we can determine the namespace from their linkage
122d1940 18795 name. */
abc72ce4 18796 if (cu->language == language_cplus
fd5866f6 18797 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18798 && die_parent == NULL
18799 && has_children
18800 && (tag == DW_TAG_class_type
18801 || tag == DW_TAG_structure_type
18802 || tag == DW_TAG_union_type))
18803 guess_partial_die_structure_name (this, cu);
abc72ce4 18804
53832f31
TT
18805 /* GCC might emit a nameless struct or union that has a linkage
18806 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18807 if (name == NULL
18808 && (tag == DW_TAG_class_type
18809 || tag == DW_TAG_interface_type
18810 || tag == DW_TAG_structure_type
18811 || tag == DW_TAG_union_type)
18812 && linkage_name != NULL)
53832f31 18813 {
43816ebc
TT
18814 gdb::unique_xmalloc_ptr<char> demangled
18815 (gdb_demangle (linkage_name, DMGL_TYPES));
18816 if (demangled != nullptr)
53832f31 18817 {
96408a79
SA
18818 const char *base;
18819
18820 /* Strip any leading namespaces/classes, keep only the base name.
18821 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18822 base = strrchr (demangled.get (), ':');
18823 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18824 base++;
18825 else
43816ebc 18826 base = demangled.get ();
96408a79 18827
518817b3 18828 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18829 name = objfile->intern (base);
53832f31
TT
18830 }
18831 }
18832
52356b79 18833 fixup_called = 1;
72bf9492
DJ
18834}
18835
41144253 18836/* Read the .debug_loclists header contents from the given SECTION in the
18837 HEADER. */
18838static void
18839read_loclist_header (struct loclist_header *header,
18840 struct dwarf2_section_info *section)
18841{
18842 unsigned int bytes_read;
18843 bfd *abfd = section->get_bfd_owner ();
18844 const gdb_byte *info_ptr = section->buffer;
18845 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18846 info_ptr += bytes_read;
18847 header->version = read_2_bytes (abfd, info_ptr);
18848 info_ptr += 2;
18849 header->addr_size = read_1_byte (abfd, info_ptr);
18850 info_ptr += 1;
18851 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18852 info_ptr += 1;
18853 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18854}
18855
18856/* Return the DW_AT_loclists_base value for the CU. */
18857static ULONGEST
18858lookup_loclist_base (struct dwarf2_cu *cu)
18859{
18860 /* For the .dwo unit, the loclist_base points to the first offset following
18861 the header. The header consists of the following entities-
18862 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18863 bit format)
18864 2. version (2 bytes)
18865 3. address size (1 byte)
18866 4. segment selector size (1 byte)
18867 5. offset entry count (4 bytes)
18868 These sizes are derived as per the DWARFv5 standard. */
18869 if (cu->dwo_unit != nullptr)
18870 {
18871 if (cu->header.initial_length_size == 4)
18872 return LOCLIST_HEADER_SIZE32;
18873 return LOCLIST_HEADER_SIZE64;
18874 }
18875 return cu->loclist_base;
18876}
18877
18878/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18879 array of offsets in the .debug_loclists section. */
18880static CORE_ADDR
18881read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18882{
18883 struct dwarf2_per_objfile *dwarf2_per_objfile
18884 = cu->per_cu->dwarf2_per_objfile;
18885 struct objfile *objfile = dwarf2_per_objfile->objfile;
18886 bfd *abfd = objfile->obfd;
18887 ULONGEST loclist_base = lookup_loclist_base (cu);
18888 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18889
18890 section->read (objfile);
18891 if (section->buffer == NULL)
18892 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18893 "section [in module %s]"), objfile_name (objfile));
18894 struct loclist_header header;
18895 read_loclist_header (&header, section);
18896 if (loclist_index >= header.offset_entry_count)
18897 complaint (_("DW_FORM_loclistx pointing outside of "
18898 ".debug_loclists offset array [in module %s]"),
18899 objfile_name (objfile));
18900 if (loclist_base + loclist_index * cu->header.offset_size
18901 >= section->size)
18902 complaint (_("DW_FORM_loclistx pointing outside of "
18903 ".debug_loclists section [in module %s]"),
18904 objfile_name (objfile));
18905 const gdb_byte *info_ptr
18906 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18907
18908 if (cu->header.offset_size == 4)
18909 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18910 else
18911 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18912}
18913
18a8505e
AT
18914/* Process the attributes that had to be skipped in the first round. These
18915 attributes are the ones that need str_offsets_base or addr_base attributes.
18916 They could not have been processed in the first round, because at the time
18917 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18918static void
18919read_attribute_reprocess (const struct die_reader_specs *reader,
18920 struct attribute *attr)
18a8505e
AT
18921{
18922 struct dwarf2_cu *cu = reader->cu;
18923 switch (attr->form)
18924 {
18925 case DW_FORM_addrx:
18926 case DW_FORM_GNU_addr_index:
18927 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18928 break;
41144253 18929 case DW_FORM_loclistx:
18930 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18931 break;
18a8505e
AT
18932 case DW_FORM_strx:
18933 case DW_FORM_strx1:
18934 case DW_FORM_strx2:
18935 case DW_FORM_strx3:
18936 case DW_FORM_strx4:
18937 case DW_FORM_GNU_str_index:
18938 {
18939 unsigned int str_index = DW_UNSND (attr);
18940 if (reader->dwo_file != NULL)
18941 {
18942 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18943 DW_STRING_IS_CANONICAL (attr) = 0;
18944 }
18945 else
18946 {
18947 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18948 DW_STRING_IS_CANONICAL (attr) = 0;
18949 }
18950 break;
18951 }
18952 default:
18953 gdb_assert_not_reached (_("Unexpected DWARF form."));
18954 }
18955}
18956
a8329558 18957/* Read an attribute value described by an attribute form. */
c906108c 18958
d521ce57 18959static const gdb_byte *
dee91e82
DE
18960read_attribute_value (const struct die_reader_specs *reader,
18961 struct attribute *attr, unsigned form,
18a8505e
AT
18962 LONGEST implicit_const, const gdb_byte *info_ptr,
18963 bool *need_reprocess)
c906108c 18964{
dee91e82 18965 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18966 struct dwarf2_per_objfile *dwarf2_per_objfile
18967 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18968 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 18969 bfd *abfd = reader->abfd;
e7c27a73 18970 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18971 unsigned int bytes_read;
18972 struct dwarf_block *blk;
18a8505e 18973 *need_reprocess = false;
c906108c 18974
aead7601 18975 attr->form = (enum dwarf_form) form;
a8329558 18976 switch (form)
c906108c 18977 {
c906108c 18978 case DW_FORM_ref_addr:
ae411497 18979 if (cu->header.version == 2)
c8a7a66f
TT
18980 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18981 &bytes_read);
ae411497 18982 else
8266302d
TT
18983 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18984 &bytes_read);
ae411497
TT
18985 info_ptr += bytes_read;
18986 break;
36586728 18987 case DW_FORM_GNU_ref_alt:
8266302d 18988 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18989 info_ptr += bytes_read;
18990 break;
ae411497 18991 case DW_FORM_addr:
08feed99
TT
18992 {
18993 struct gdbarch *gdbarch = objfile->arch ();
18994 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18995 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18996 info_ptr += bytes_read;
18997 }
c906108c
SS
18998 break;
18999 case DW_FORM_block2:
7b5a2f43 19000 blk = dwarf_alloc_block (cu);
c906108c
SS
19001 blk->size = read_2_bytes (abfd, info_ptr);
19002 info_ptr += 2;
19003 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19004 info_ptr += blk->size;
19005 DW_BLOCK (attr) = blk;
19006 break;
19007 case DW_FORM_block4:
7b5a2f43 19008 blk = dwarf_alloc_block (cu);
c906108c
SS
19009 blk->size = read_4_bytes (abfd, info_ptr);
19010 info_ptr += 4;
19011 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19012 info_ptr += blk->size;
19013 DW_BLOCK (attr) = blk;
19014 break;
19015 case DW_FORM_data2:
19016 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19017 info_ptr += 2;
19018 break;
19019 case DW_FORM_data4:
19020 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19021 info_ptr += 4;
19022 break;
19023 case DW_FORM_data8:
19024 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19025 info_ptr += 8;
19026 break;
0224619f
JK
19027 case DW_FORM_data16:
19028 blk = dwarf_alloc_block (cu);
19029 blk->size = 16;
19030 blk->data = read_n_bytes (abfd, info_ptr, 16);
19031 info_ptr += 16;
19032 DW_BLOCK (attr) = blk;
19033 break;
2dc7f7b3 19034 case DW_FORM_sec_offset:
8266302d 19035 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19036 info_ptr += bytes_read;
19037 break;
41144253 19038 case DW_FORM_loclistx:
19039 {
19040 *need_reprocess = true;
19041 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19042 info_ptr += bytes_read;
19043 }
19044 break;
c906108c 19045 case DW_FORM_string:
9b1c24c8 19046 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19047 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19048 info_ptr += bytes_read;
19049 break;
4bdf3d34 19050 case DW_FORM_strp:
36586728
TT
19051 if (!cu->per_cu->is_dwz)
19052 {
ed2dc618
SM
19053 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19054 abfd, info_ptr, cu_header,
36586728
TT
19055 &bytes_read);
19056 DW_STRING_IS_CANONICAL (attr) = 0;
19057 info_ptr += bytes_read;
19058 break;
19059 }
19060 /* FALLTHROUGH */
43988095
JK
19061 case DW_FORM_line_strp:
19062 if (!cu->per_cu->is_dwz)
19063 {
86c0bb4c
TT
19064 DW_STRING (attr)
19065 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19066 &bytes_read);
43988095
JK
19067 DW_STRING_IS_CANONICAL (attr) = 0;
19068 info_ptr += bytes_read;
19069 break;
19070 }
19071 /* FALLTHROUGH */
36586728
TT
19072 case DW_FORM_GNU_strp_alt:
19073 {
ed2dc618 19074 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
19075 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19076 &bytes_read);
36586728 19077
0314b390 19078 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19079 DW_STRING_IS_CANONICAL (attr) = 0;
19080 info_ptr += bytes_read;
19081 }
4bdf3d34 19082 break;
2dc7f7b3 19083 case DW_FORM_exprloc:
c906108c 19084 case DW_FORM_block:
7b5a2f43 19085 blk = dwarf_alloc_block (cu);
c906108c
SS
19086 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19087 info_ptr += bytes_read;
19088 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19089 info_ptr += blk->size;
19090 DW_BLOCK (attr) = blk;
19091 break;
19092 case DW_FORM_block1:
7b5a2f43 19093 blk = dwarf_alloc_block (cu);
c906108c
SS
19094 blk->size = read_1_byte (abfd, info_ptr);
19095 info_ptr += 1;
19096 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19097 info_ptr += blk->size;
19098 DW_BLOCK (attr) = blk;
19099 break;
19100 case DW_FORM_data1:
19101 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19102 info_ptr += 1;
19103 break;
19104 case DW_FORM_flag:
19105 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19106 info_ptr += 1;
19107 break;
2dc7f7b3
TT
19108 case DW_FORM_flag_present:
19109 DW_UNSND (attr) = 1;
19110 break;
c906108c
SS
19111 case DW_FORM_sdata:
19112 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19113 info_ptr += bytes_read;
19114 break;
19115 case DW_FORM_udata:
18a8505e 19116 case DW_FORM_rnglistx:
c906108c
SS
19117 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19118 info_ptr += bytes_read;
19119 break;
19120 case DW_FORM_ref1:
9c541725 19121 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19122 + read_1_byte (abfd, info_ptr));
c906108c
SS
19123 info_ptr += 1;
19124 break;
19125 case DW_FORM_ref2:
9c541725 19126 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19127 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19128 info_ptr += 2;
19129 break;
19130 case DW_FORM_ref4:
9c541725 19131 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19132 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19133 info_ptr += 4;
19134 break;
613e1657 19135 case DW_FORM_ref8:
9c541725 19136 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19137 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19138 info_ptr += 8;
19139 break;
55f1336d 19140 case DW_FORM_ref_sig8:
ac9ec31b 19141 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19142 info_ptr += 8;
19143 break;
c906108c 19144 case DW_FORM_ref_udata:
9c541725 19145 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19146 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19147 info_ptr += bytes_read;
19148 break;
c906108c 19149 case DW_FORM_indirect:
a8329558
KW
19150 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19151 info_ptr += bytes_read;
43988095
JK
19152 if (form == DW_FORM_implicit_const)
19153 {
19154 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19155 info_ptr += bytes_read;
19156 }
19157 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19158 info_ptr, need_reprocess);
43988095
JK
19159 break;
19160 case DW_FORM_implicit_const:
19161 DW_SND (attr) = implicit_const;
a8329558 19162 break;
336d760d 19163 case DW_FORM_addrx:
3019eac3 19164 case DW_FORM_GNU_addr_index:
18a8505e
AT
19165 *need_reprocess = true;
19166 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19167 info_ptr += bytes_read;
19168 break;
cf532bd1 19169 case DW_FORM_strx:
15f18d14
AT
19170 case DW_FORM_strx1:
19171 case DW_FORM_strx2:
19172 case DW_FORM_strx3:
19173 case DW_FORM_strx4:
3019eac3 19174 case DW_FORM_GNU_str_index:
3019eac3 19175 {
15f18d14
AT
19176 ULONGEST str_index;
19177 if (form == DW_FORM_strx1)
19178 {
19179 str_index = read_1_byte (abfd, info_ptr);
19180 info_ptr += 1;
19181 }
19182 else if (form == DW_FORM_strx2)
19183 {
19184 str_index = read_2_bytes (abfd, info_ptr);
19185 info_ptr += 2;
19186 }
19187 else if (form == DW_FORM_strx3)
19188 {
19189 str_index = read_3_bytes (abfd, info_ptr);
19190 info_ptr += 3;
19191 }
19192 else if (form == DW_FORM_strx4)
19193 {
19194 str_index = read_4_bytes (abfd, info_ptr);
19195 info_ptr += 4;
19196 }
19197 else
19198 {
19199 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19200 info_ptr += bytes_read;
19201 }
18a8505e
AT
19202 *need_reprocess = true;
19203 DW_UNSND (attr) = str_index;
19204 }
3019eac3 19205 break;
c906108c 19206 default:
8a3fe4f8 19207 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19208 dwarf_form_name (form),
19209 bfd_get_filename (abfd));
c906108c 19210 }
28e94949 19211
36586728 19212 /* Super hack. */
cd6c91b4 19213 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19214 attr->form = DW_FORM_GNU_ref_alt;
19215
28e94949
JB
19216 /* We have seen instances where the compiler tried to emit a byte
19217 size attribute of -1 which ended up being encoded as an unsigned
19218 0xffffffff. Although 0xffffffff is technically a valid size value,
19219 an object of this size seems pretty unlikely so we can relatively
19220 safely treat these cases as if the size attribute was invalid and
19221 treat them as zero by default. */
19222 if (attr->name == DW_AT_byte_size
19223 && form == DW_FORM_data4
19224 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19225 {
19226 complaint
b98664d3 19227 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19228 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19229 DW_UNSND (attr) = 0;
19230 }
28e94949 19231
c906108c
SS
19232 return info_ptr;
19233}
19234
a8329558
KW
19235/* Read an attribute described by an abbreviated attribute. */
19236
d521ce57 19237static const gdb_byte *
dee91e82
DE
19238read_attribute (const struct die_reader_specs *reader,
19239 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19240 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19241{
19242 attr->name = abbrev->name;
43988095 19243 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19244 abbrev->implicit_const, info_ptr,
19245 need_reprocess);
a8329558
KW
19246}
19247
43988095
JK
19248/* Return pointer to string at .debug_str offset STR_OFFSET. */
19249
19250static const char *
ed2dc618 19251read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19252 LONGEST str_offset)
43988095 19253{
4f44ae6c
TT
19254 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19255 str_offset, "DW_FORM_strp");
c906108c
SS
19256}
19257
43988095
JK
19258/* Return pointer to string at .debug_str offset as read from BUF.
19259 BUF is assumed to be in a compilation unit described by CU_HEADER.
19260 Return *BYTES_READ_PTR count of bytes read from BUF. */
19261
d521ce57 19262static const char *
ed2dc618
SM
19263read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19264 const gdb_byte *buf,
cf2c3c16
TT
19265 const struct comp_unit_head *cu_header,
19266 unsigned int *bytes_read_ptr)
19267{
8266302d 19268 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19269
4f44ae6c 19270 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19271}
19272
86c0bb4c 19273/* See read.h. */
43988095 19274
86c0bb4c
TT
19275const char *
19276dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
19277 const struct comp_unit_head *cu_header,
19278 unsigned int *bytes_read_ptr)
19279{
86c0bb4c 19280 bfd *abfd = objfile->obfd;
8266302d 19281 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19282
86c0bb4c 19283 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19284}
19285
3019eac3 19286/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19287 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19288 ADDR_SIZE is the size of addresses from the CU header. */
19289
19290static CORE_ADDR
ed2dc618 19291read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19292 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19293 int addr_size)
3019eac3
DE
19294{
19295 struct objfile *objfile = dwarf2_per_objfile->objfile;
19296 bfd *abfd = objfile->obfd;
19297 const gdb_byte *info_ptr;
18a8505e 19298 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19299
96b79293 19300 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19301 if (dwarf2_per_objfile->addr.buffer == NULL)
19302 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19303 objfile_name (objfile));
18a8505e
AT
19304 if (addr_base_or_zero + addr_index * addr_size
19305 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19306 error (_("DW_FORM_addr_index pointing outside of "
19307 ".debug_addr section [in module %s]"),
4262abfb 19308 objfile_name (objfile));
3019eac3 19309 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19310 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19311 if (addr_size == 4)
19312 return bfd_get_32 (abfd, info_ptr);
19313 else
19314 return bfd_get_64 (abfd, info_ptr);
19315}
19316
19317/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19318
19319static CORE_ADDR
19320read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19321{
518817b3
SM
19322 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19323 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19324}
19325
19326/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19327
19328static CORE_ADDR
d521ce57 19329read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19330 unsigned int *bytes_read)
19331{
518817b3 19332 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19333 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19334
19335 return read_addr_index (cu, addr_index);
19336}
19337
450a1bfc 19338/* See read.h. */
3019eac3
DE
19339
19340CORE_ADDR
450a1bfc 19341dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19342{
ed2dc618 19343 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19344 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19345 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19346 int addr_size;
19347
3019eac3
DE
19348 /* We need addr_base and addr_size.
19349 If we don't have PER_CU->cu, we have to get it.
19350 Nasty, but the alternative is storing the needed info in PER_CU,
19351 which at this point doesn't seem justified: it's not clear how frequently
19352 it would get used and it would increase the size of every PER_CU.
19353 Entry points like dwarf2_per_cu_addr_size do a similar thing
19354 so we're not in uncharted territory here.
19355 Alas we need to be a bit more complicated as addr_base is contained
19356 in the DIE.
19357
19358 We don't need to read the entire CU(/TU).
19359 We just need the header and top level die.
a1b64ce1 19360
3019eac3 19361 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19362 For now we skip this optimization. */
3019eac3
DE
19363
19364 if (cu != NULL)
19365 {
19366 addr_base = cu->addr_base;
19367 addr_size = cu->header.addr_size;
19368 }
19369 else
19370 {
6751ebae 19371 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19372 addr_base = reader.cu->addr_base;
19373 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19374 }
19375
ed2dc618
SM
19376 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19377 addr_size);
3019eac3
DE
19378}
19379
18a8505e
AT
19380/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19381 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19382 DWO file. */
3019eac3 19383
d521ce57 19384static const char *
18a8505e
AT
19385read_str_index (struct dwarf2_cu *cu,
19386 struct dwarf2_section_info *str_section,
19387 struct dwarf2_section_info *str_offsets_section,
19388 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19389{
518817b3
SM
19390 struct dwarf2_per_objfile *dwarf2_per_objfile
19391 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19392 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19393 const char *objf_name = objfile_name (objfile);
3019eac3 19394 bfd *abfd = objfile->obfd;
d521ce57 19395 const gdb_byte *info_ptr;
3019eac3 19396 ULONGEST str_offset;
cf532bd1 19397 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19398
96b79293
TT
19399 str_section->read (objfile);
19400 str_offsets_section->read (objfile);
73869dc2 19401 if (str_section->buffer == NULL)
18a8505e 19402 error (_("%s used without %s section"
9d8780f0 19403 " in CU at offset %s [in module %s]"),
96b79293 19404 form_name, str_section->get_name (),
18a8505e 19405 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19406 if (str_offsets_section->buffer == NULL)
18a8505e 19407 error (_("%s used without %s section"
9d8780f0 19408 " in CU at offset %s [in module %s]"),
96b79293 19409 form_name, str_section->get_name (),
18a8505e 19410 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19411 info_ptr = (str_offsets_section->buffer
18a8505e 19412 + str_offsets_base
3019eac3
DE
19413 + str_index * cu->header.offset_size);
19414 if (cu->header.offset_size == 4)
19415 str_offset = bfd_get_32 (abfd, info_ptr);
19416 else
19417 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19418 if (str_offset >= str_section->size)
57d63ce2 19419 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19420 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19421 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19422 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19423}
19424
18a8505e
AT
19425/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19426
19427static const char *
19428read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19429{
19430 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19431 ? reader->cu->header.addr_size : 0;
19432 return read_str_index (reader->cu,
19433 &reader->dwo_file->sections.str,
19434 &reader->dwo_file->sections.str_offsets,
19435 str_offsets_base, str_index);
19436}
19437
19438/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19439
19440static const char *
19441read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19442{
19443 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19444 const char *objf_name = objfile_name (objfile);
19445 static const char form_name[] = "DW_FORM_GNU_str_index";
19446 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19447
19448 if (!cu->str_offsets_base.has_value ())
19449 error (_("%s used in Fission stub without %s"
19450 " in CU at offset 0x%lx [in module %s]"),
19451 form_name, str_offsets_attr_name,
19452 (long) cu->header.offset_size, objf_name);
19453
19454 return read_str_index (cu,
19455 &cu->per_cu->dwarf2_per_objfile->str,
19456 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19457 *cu->str_offsets_base, str_index);
19458}
19459
3019eac3
DE
19460/* Return the length of an LEB128 number in BUF. */
19461
19462static int
19463leb128_size (const gdb_byte *buf)
19464{
19465 const gdb_byte *begin = buf;
19466 gdb_byte byte;
19467
19468 while (1)
19469 {
19470 byte = *buf++;
19471 if ((byte & 128) == 0)
19472 return buf - begin;
19473 }
19474}
19475
c906108c 19476static void
e142c38c 19477set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19478{
19479 switch (lang)
19480 {
19481 case DW_LANG_C89:
76bee0cc 19482 case DW_LANG_C99:
0cfd832f 19483 case DW_LANG_C11:
c906108c 19484 case DW_LANG_C:
d1be3247 19485 case DW_LANG_UPC:
e142c38c 19486 cu->language = language_c;
c906108c 19487 break;
9c37b5ae 19488 case DW_LANG_Java:
c906108c 19489 case DW_LANG_C_plus_plus:
0cfd832f
MW
19490 case DW_LANG_C_plus_plus_11:
19491 case DW_LANG_C_plus_plus_14:
e142c38c 19492 cu->language = language_cplus;
c906108c 19493 break;
6aecb9c2
JB
19494 case DW_LANG_D:
19495 cu->language = language_d;
19496 break;
c906108c
SS
19497 case DW_LANG_Fortran77:
19498 case DW_LANG_Fortran90:
b21b22e0 19499 case DW_LANG_Fortran95:
f7de9aab
MW
19500 case DW_LANG_Fortran03:
19501 case DW_LANG_Fortran08:
e142c38c 19502 cu->language = language_fortran;
c906108c 19503 break;
a766d390
DE
19504 case DW_LANG_Go:
19505 cu->language = language_go;
19506 break;
c906108c 19507 case DW_LANG_Mips_Assembler:
e142c38c 19508 cu->language = language_asm;
c906108c
SS
19509 break;
19510 case DW_LANG_Ada83:
8aaf0b47 19511 case DW_LANG_Ada95:
bc5f45f8
JB
19512 cu->language = language_ada;
19513 break;
72019c9c
GM
19514 case DW_LANG_Modula2:
19515 cu->language = language_m2;
19516 break;
fe8e67fd
PM
19517 case DW_LANG_Pascal83:
19518 cu->language = language_pascal;
19519 break;
22566fbd
DJ
19520 case DW_LANG_ObjC:
19521 cu->language = language_objc;
19522 break;
c44af4eb
TT
19523 case DW_LANG_Rust:
19524 case DW_LANG_Rust_old:
19525 cu->language = language_rust;
19526 break;
c906108c
SS
19527 case DW_LANG_Cobol74:
19528 case DW_LANG_Cobol85:
c906108c 19529 default:
e142c38c 19530 cu->language = language_minimal;
c906108c
SS
19531 break;
19532 }
e142c38c 19533 cu->language_defn = language_def (cu->language);
c906108c
SS
19534}
19535
19536/* Return the named attribute or NULL if not there. */
19537
19538static struct attribute *
e142c38c 19539dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19540{
a48e046c 19541 for (;;)
c906108c 19542 {
a48e046c
TT
19543 unsigned int i;
19544 struct attribute *spec = NULL;
19545
19546 for (i = 0; i < die->num_attrs; ++i)
19547 {
19548 if (die->attrs[i].name == name)
19549 return &die->attrs[i];
19550 if (die->attrs[i].name == DW_AT_specification
19551 || die->attrs[i].name == DW_AT_abstract_origin)
19552 spec = &die->attrs[i];
19553 }
19554
19555 if (!spec)
19556 break;
c906108c 19557
f2f0e013 19558 die = follow_die_ref (die, spec, &cu);
f2f0e013 19559 }
c5aa993b 19560
c906108c
SS
19561 return NULL;
19562}
19563
7d45c7c3
KB
19564/* Return the string associated with a string-typed attribute, or NULL if it
19565 is either not found or is of an incorrect type. */
19566
19567static const char *
19568dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19569{
19570 struct attribute *attr;
19571 const char *str = NULL;
19572
19573 attr = dwarf2_attr (die, name, cu);
19574
19575 if (attr != NULL)
19576 {
e61108c9
TT
19577 str = attr->value_as_string ();
19578 if (str == nullptr)
b98664d3 19579 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19580 "DIE at %s in module %s"),
19581 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19582 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19583 }
19584
19585 return str;
19586}
19587
a084a2a6 19588/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19589 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19590static const char *
19591dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19592{
19593 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19594 if (dwo_name == nullptr)
19595 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19596 return dwo_name;
19597}
19598
05cf31d1
JB
19599/* Return non-zero iff the attribute NAME is defined for the given DIE,
19600 and holds a non-zero value. This function should only be used for
2dc7f7b3 19601 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19602
19603static int
19604dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19605{
19606 struct attribute *attr = dwarf2_attr (die, name, cu);
19607
19608 return (attr && DW_UNSND (attr));
19609}
19610
3ca72b44 19611static int
e142c38c 19612die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19613{
05cf31d1
JB
19614 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19615 which value is non-zero. However, we have to be careful with
19616 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19617 (via dwarf2_flag_true_p) follows this attribute. So we may
19618 end up accidently finding a declaration attribute that belongs
19619 to a different DIE referenced by the specification attribute,
19620 even though the given DIE does not have a declaration attribute. */
19621 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19622 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19623}
19624
63d06c5c 19625/* Return the die giving the specification for DIE, if there is
f2f0e013 19626 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19627 containing the return value on output. If there is no
19628 specification, but there is an abstract origin, that is
19629 returned. */
63d06c5c
DC
19630
19631static struct die_info *
f2f0e013 19632die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19633{
f2f0e013
DJ
19634 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19635 *spec_cu);
63d06c5c 19636
edb3359d
DJ
19637 if (spec_attr == NULL)
19638 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19639
63d06c5c
DC
19640 if (spec_attr == NULL)
19641 return NULL;
19642 else
f2f0e013 19643 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19644}
c906108c 19645
527f3840
JK
19646/* Stub for free_line_header to match void * callback types. */
19647
19648static void
19649free_line_header_voidp (void *arg)
19650{
9a3c8263 19651 struct line_header *lh = (struct line_header *) arg;
527f3840 19652
fff8551c 19653 delete lh;
527f3840
JK
19654}
19655
83769d0b 19656/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19657
19658static struct dwarf2_section_info *
19659get_debug_line_section (struct dwarf2_cu *cu)
19660{
19661 struct dwarf2_section_info *section;
518817b3
SM
19662 struct dwarf2_per_objfile *dwarf2_per_objfile
19663 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19664
19665 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19666 DWO file. */
19667 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19668 section = &cu->dwo_unit->dwo_file->sections.line;
19669 else if (cu->per_cu->is_dwz)
19670 {
ed2dc618 19671 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19672
19673 section = &dwz->line;
19674 }
19675 else
19676 section = &dwarf2_per_objfile->line;
19677
19678 return section;
19679}
19680
debd256d 19681/* Read the statement program header starting at OFFSET in
3019eac3 19682 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19683 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19684 Returns NULL if there is a problem reading the header, e.g., if it
19685 has a version we don't understand.
debd256d
JB
19686
19687 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19688 the returned object point into the dwarf line section buffer,
19689 and must not be freed. */
ae2de4f8 19690
fff8551c 19691static line_header_up
9c541725 19692dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19693{
3019eac3 19694 struct dwarf2_section_info *section;
518817b3
SM
19695 struct dwarf2_per_objfile *dwarf2_per_objfile
19696 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19697
36586728 19698 section = get_debug_line_section (cu);
96b79293 19699 section->read (dwarf2_per_objfile->objfile);
3019eac3 19700 if (section->buffer == NULL)
debd256d 19701 {
3019eac3 19702 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19703 complaint (_("missing .debug_line.dwo section"));
3019eac3 19704 else
b98664d3 19705 complaint (_("missing .debug_line section"));
debd256d
JB
19706 return 0;
19707 }
19708
0df7ad3a
TT
19709 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19710 dwarf2_per_objfile, section,
19711 &cu->header);
debd256d 19712}
c906108c 19713
c6da4cef 19714/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19715 Return the file name of the psymtab for the given file_entry.
c6da4cef 19716 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19717 If space for the result is malloc'd, *NAME_HOLDER will be set.
19718 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19719
d521ce57 19720static const char *
7ba99d21 19721psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19722 const dwarf2_psymtab *pst,
c89b44cd
TT
19723 const char *comp_dir,
19724 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19725{
d521ce57
TT
19726 const char *include_name = fe.name;
19727 const char *include_name_to_compare = include_name;
72b9f47f 19728 const char *pst_filename;
c6da4cef
DE
19729 int file_is_pst;
19730
8c43009f 19731 const char *dir_name = fe.include_dir (lh);
c6da4cef 19732
c89b44cd 19733 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19734 if (!IS_ABSOLUTE_PATH (include_name)
19735 && (dir_name != NULL || comp_dir != NULL))
19736 {
19737 /* Avoid creating a duplicate psymtab for PST.
19738 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19739 Before we do the comparison, however, we need to account
19740 for DIR_NAME and COMP_DIR.
19741 First prepend dir_name (if non-NULL). If we still don't
19742 have an absolute path prepend comp_dir (if non-NULL).
19743 However, the directory we record in the include-file's
19744 psymtab does not contain COMP_DIR (to match the
19745 corresponding symtab(s)).
19746
19747 Example:
19748
19749 bash$ cd /tmp
19750 bash$ gcc -g ./hello.c
19751 include_name = "hello.c"
19752 dir_name = "."
19753 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19754 DW_AT_name = "./hello.c"
19755
19756 */
c6da4cef
DE
19757
19758 if (dir_name != NULL)
19759 {
c89b44cd
TT
19760 name_holder->reset (concat (dir_name, SLASH_STRING,
19761 include_name, (char *) NULL));
19762 include_name = name_holder->get ();
c6da4cef 19763 include_name_to_compare = include_name;
c6da4cef
DE
19764 }
19765 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19766 {
c89b44cd
TT
19767 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19768 include_name, (char *) NULL));
19769 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19770 }
19771 }
19772
19773 pst_filename = pst->filename;
c89b44cd 19774 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19775 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19776 {
c89b44cd
TT
19777 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19778 pst_filename, (char *) NULL));
19779 pst_filename = copied_name.get ();
c6da4cef
DE
19780 }
19781
1e3fad37 19782 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19783
c6da4cef
DE
19784 if (file_is_pst)
19785 return NULL;
19786 return include_name;
19787}
19788
d9b3de22
DE
19789/* State machine to track the state of the line number program. */
19790
6f77053d 19791class lnp_state_machine
d9b3de22 19792{
6f77053d
PA
19793public:
19794 /* Initialize a machine state for the start of a line number
19795 program. */
804d2729
TT
19796 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19797 bool record_lines_p);
6f77053d 19798
8c43009f
PA
19799 file_entry *current_file ()
19800 {
19801 /* lh->file_names is 0-based, but the file name numbers in the
19802 statement program are 1-based. */
6f77053d
PA
19803 return m_line_header->file_name_at (m_file);
19804 }
19805
19806 /* Record the line in the state machine. END_SEQUENCE is true if
19807 we're processing the end of a sequence. */
19808 void record_line (bool end_sequence);
19809
7ab6656f
OJ
19810 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19811 nop-out rest of the lines in this sequence. */
6f77053d
PA
19812 void check_line_address (struct dwarf2_cu *cu,
19813 const gdb_byte *line_ptr,
7ab6656f 19814 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19815
19816 void handle_set_discriminator (unsigned int discriminator)
19817 {
19818 m_discriminator = discriminator;
19819 m_line_has_non_zero_discriminator |= discriminator != 0;
19820 }
19821
19822 /* Handle DW_LNE_set_address. */
19823 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19824 {
19825 m_op_index = 0;
19826 address += baseaddr;
19827 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19828 }
19829
19830 /* Handle DW_LNS_advance_pc. */
19831 void handle_advance_pc (CORE_ADDR adjust);
19832
19833 /* Handle a special opcode. */
19834 void handle_special_opcode (unsigned char op_code);
19835
19836 /* Handle DW_LNS_advance_line. */
19837 void handle_advance_line (int line_delta)
19838 {
19839 advance_line (line_delta);
19840 }
19841
19842 /* Handle DW_LNS_set_file. */
19843 void handle_set_file (file_name_index file);
19844
19845 /* Handle DW_LNS_negate_stmt. */
19846 void handle_negate_stmt ()
19847 {
19848 m_is_stmt = !m_is_stmt;
19849 }
19850
19851 /* Handle DW_LNS_const_add_pc. */
19852 void handle_const_add_pc ();
19853
19854 /* Handle DW_LNS_fixed_advance_pc. */
19855 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19856 {
19857 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19858 m_op_index = 0;
19859 }
19860
19861 /* Handle DW_LNS_copy. */
19862 void handle_copy ()
19863 {
19864 record_line (false);
19865 m_discriminator = 0;
19866 }
19867
19868 /* Handle DW_LNE_end_sequence. */
19869 void handle_end_sequence ()
19870 {
804d2729 19871 m_currently_recording_lines = true;
6f77053d
PA
19872 }
19873
19874private:
19875 /* Advance the line by LINE_DELTA. */
19876 void advance_line (int line_delta)
19877 {
19878 m_line += line_delta;
19879
19880 if (line_delta != 0)
19881 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19882 }
19883
804d2729
TT
19884 struct dwarf2_cu *m_cu;
19885
6f77053d
PA
19886 gdbarch *m_gdbarch;
19887
19888 /* True if we're recording lines.
19889 Otherwise we're building partial symtabs and are just interested in
19890 finding include files mentioned by the line number program. */
19891 bool m_record_lines_p;
19892
8c43009f 19893 /* The line number header. */
6f77053d 19894 line_header *m_line_header;
8c43009f 19895
6f77053d
PA
19896 /* These are part of the standard DWARF line number state machine,
19897 and initialized according to the DWARF spec. */
d9b3de22 19898
6f77053d 19899 unsigned char m_op_index = 0;
7ba99d21
AT
19900 /* The line table index of the current file. */
19901 file_name_index m_file = 1;
6f77053d
PA
19902 unsigned int m_line = 1;
19903
19904 /* These are initialized in the constructor. */
19905
19906 CORE_ADDR m_address;
19907 bool m_is_stmt;
19908 unsigned int m_discriminator;
d9b3de22
DE
19909
19910 /* Additional bits of state we need to track. */
19911
19912 /* The last file that we called dwarf2_start_subfile for.
19913 This is only used for TLLs. */
6f77053d 19914 unsigned int m_last_file = 0;
d9b3de22 19915 /* The last file a line number was recorded for. */
6f77053d 19916 struct subfile *m_last_subfile = NULL;
d9b3de22 19917
804d2729
TT
19918 /* When true, record the lines we decode. */
19919 bool m_currently_recording_lines = false;
d9b3de22
DE
19920
19921 /* The last line number that was recorded, used to coalesce
19922 consecutive entries for the same line. This can happen, for
19923 example, when discriminators are present. PR 17276. */
6f77053d
PA
19924 unsigned int m_last_line = 0;
19925 bool m_line_has_non_zero_discriminator = false;
8c43009f 19926};
d9b3de22 19927
6f77053d
PA
19928void
19929lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19930{
19931 CORE_ADDR addr_adj = (((m_op_index + adjust)
19932 / m_line_header->maximum_ops_per_instruction)
19933 * m_line_header->minimum_instruction_length);
19934 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19935 m_op_index = ((m_op_index + adjust)
19936 % m_line_header->maximum_ops_per_instruction);
19937}
d9b3de22 19938
6f77053d
PA
19939void
19940lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19941{
6f77053d 19942 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19943 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19944 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19945 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19946 / m_line_header->maximum_ops_per_instruction)
19947 * m_line_header->minimum_instruction_length);
19948 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19949 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19950 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19951
258bf0ee 19952 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19953 advance_line (line_delta);
19954 record_line (false);
19955 m_discriminator = 0;
19956}
d9b3de22 19957
6f77053d
PA
19958void
19959lnp_state_machine::handle_set_file (file_name_index file)
19960{
19961 m_file = file;
19962
19963 const file_entry *fe = current_file ();
19964 if (fe == NULL)
19965 dwarf2_debug_line_missing_file_complaint ();
19966 else if (m_record_lines_p)
19967 {
19968 const char *dir = fe->include_dir (m_line_header);
19969
c24bdb02 19970 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19971 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19972 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19973 }
19974}
19975
19976void
19977lnp_state_machine::handle_const_add_pc ()
19978{
19979 CORE_ADDR adjust
19980 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19981
19982 CORE_ADDR addr_adj
19983 = (((m_op_index + adjust)
19984 / m_line_header->maximum_ops_per_instruction)
19985 * m_line_header->minimum_instruction_length);
19986
19987 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19988 m_op_index = ((m_op_index + adjust)
19989 % m_line_header->maximum_ops_per_instruction);
19990}
d9b3de22 19991
a05a36a5
DE
19992/* Return non-zero if we should add LINE to the line number table.
19993 LINE is the line to add, LAST_LINE is the last line that was added,
19994 LAST_SUBFILE is the subfile for LAST_LINE.
19995 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19996 had a non-zero discriminator.
19997
19998 We have to be careful in the presence of discriminators.
19999 E.g., for this line:
20000
20001 for (i = 0; i < 100000; i++);
20002
20003 clang can emit four line number entries for that one line,
20004 each with a different discriminator.
20005 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20006
20007 However, we want gdb to coalesce all four entries into one.
20008 Otherwise the user could stepi into the middle of the line and
20009 gdb would get confused about whether the pc really was in the
20010 middle of the line.
20011
20012 Things are further complicated by the fact that two consecutive
20013 line number entries for the same line is a heuristic used by gcc
20014 to denote the end of the prologue. So we can't just discard duplicate
20015 entries, we have to be selective about it. The heuristic we use is
20016 that we only collapse consecutive entries for the same line if at least
20017 one of those entries has a non-zero discriminator. PR 17276.
20018
20019 Note: Addresses in the line number state machine can never go backwards
20020 within one sequence, thus this coalescing is ok. */
20021
20022static int
804d2729
TT
20023dwarf_record_line_p (struct dwarf2_cu *cu,
20024 unsigned int line, unsigned int last_line,
a05a36a5
DE
20025 int line_has_non_zero_discriminator,
20026 struct subfile *last_subfile)
20027{
c24bdb02 20028 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20029 return 1;
20030 if (line != last_line)
20031 return 1;
20032 /* Same line for the same file that we've seen already.
20033 As a last check, for pr 17276, only record the line if the line
20034 has never had a non-zero discriminator. */
20035 if (!line_has_non_zero_discriminator)
20036 return 1;
20037 return 0;
20038}
20039
804d2729
TT
20040/* Use the CU's builder to record line number LINE beginning at
20041 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20042
20043static void
d9b3de22 20044dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20045 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20046 struct dwarf2_cu *cu)
252a6764
DE
20047{
20048 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20049
27e0867f
DE
20050 if (dwarf_line_debug)
20051 {
20052 fprintf_unfiltered (gdb_stdlog,
20053 "Recording line %u, file %s, address %s\n",
20054 line, lbasename (subfile->name),
20055 paddress (gdbarch, address));
20056 }
20057
804d2729 20058 if (cu != nullptr)
8c95582d 20059 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20060}
20061
20062/* Subroutine of dwarf_decode_lines_1 to simplify it.
20063 Mark the end of a set of line number records.
d9b3de22 20064 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20065 If SUBFILE is NULL the request is ignored. */
20066
20067static void
20068dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20069 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20070{
27e0867f
DE
20071 if (subfile == NULL)
20072 return;
20073
20074 if (dwarf_line_debug)
20075 {
20076 fprintf_unfiltered (gdb_stdlog,
20077 "Finishing current line, file %s, address %s\n",
20078 lbasename (subfile->name),
20079 paddress (gdbarch, address));
20080 }
20081
8c95582d 20082 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20083}
20084
6f77053d
PA
20085void
20086lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20087{
d9b3de22
DE
20088 if (dwarf_line_debug)
20089 {
20090 fprintf_unfiltered (gdb_stdlog,
20091 "Processing actual line %u: file %u,"
94a72be7 20092 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20093 m_line, m_file,
6f77053d 20094 paddress (m_gdbarch, m_address),
94a72be7
AB
20095 m_is_stmt, m_discriminator,
20096 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20097 }
20098
6f77053d 20099 file_entry *fe = current_file ();
8c43009f
PA
20100
20101 if (fe == NULL)
d9b3de22
DE
20102 dwarf2_debug_line_missing_file_complaint ();
20103 /* For now we ignore lines not starting on an instruction boundary.
20104 But not when processing end_sequence for compatibility with the
20105 previous version of the code. */
6f77053d 20106 else if (m_op_index == 0 || end_sequence)
d9b3de22 20107 {
8c43009f 20108 fe->included_p = 1;
8c95582d 20109 if (m_record_lines_p)
d9b3de22 20110 {
c24bdb02 20111 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20112 || end_sequence)
d9b3de22 20113 {
804d2729
TT
20114 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20115 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20116 }
20117
20118 if (!end_sequence)
20119 {
8c95582d
AB
20120 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20121
804d2729 20122 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20123 m_line_has_non_zero_discriminator,
20124 m_last_subfile))
d9b3de22 20125 {
c24bdb02 20126 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20127 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20128 builder->get_current_subfile (),
8c95582d 20129 m_line, m_address, is_stmt,
804d2729 20130 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20131 }
c24bdb02 20132 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20133 m_last_line = m_line;
d9b3de22
DE
20134 }
20135 }
20136 }
20137}
20138
804d2729
TT
20139lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20140 line_header *lh, bool record_lines_p)
d9b3de22 20141{
804d2729 20142 m_cu = cu;
6f77053d
PA
20143 m_gdbarch = arch;
20144 m_record_lines_p = record_lines_p;
20145 m_line_header = lh;
d9b3de22 20146
804d2729 20147 m_currently_recording_lines = true;
d9b3de22 20148
d9b3de22
DE
20149 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20150 was a line entry for it so that the backend has a chance to adjust it
20151 and also record it in case it needs it. This is currently used by MIPS
20152 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20153 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20154 m_is_stmt = lh->default_is_stmt;
20155 m_discriminator = 0;
252a6764
DE
20156}
20157
6f77053d
PA
20158void
20159lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20160 const gdb_byte *line_ptr,
7ab6656f 20161 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20162{
7ab6656f
OJ
20163 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20164 the pc range of the CU. However, we restrict the test to only ADDRESS
20165 values of zero to preserve GDB's previous behaviour which is to handle
20166 the specific case of a function being GC'd by the linker. */
924c2928 20167
7ab6656f 20168 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20169 {
20170 /* This line table is for a function which has been
20171 GCd by the linker. Ignore it. PR gdb/12528 */
20172
518817b3 20173 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20174 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20175
b98664d3 20176 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20177 line_offset, objfile_name (objfile));
804d2729
TT
20178 m_currently_recording_lines = false;
20179 /* Note: m_currently_recording_lines is left as false until we see
20180 DW_LNE_end_sequence. */
924c2928
DE
20181 }
20182}
20183
f3f5162e 20184/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20185 Process the line number information in LH.
20186 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20187 program in order to set included_p for every referenced header. */
debd256d 20188
c906108c 20189static void
43f3e411
DE
20190dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20191 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20192{
d521ce57
TT
20193 const gdb_byte *line_ptr, *extended_end;
20194 const gdb_byte *line_end;
a8c50c1f 20195 unsigned int bytes_read, extended_len;
699ca60a 20196 unsigned char op_code, extended_op;
e142c38c 20197 CORE_ADDR baseaddr;
518817b3 20198 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20199 bfd *abfd = objfile->obfd;
08feed99 20200 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20201 /* True if we're recording line info (as opposed to building partial
20202 symtabs and just interested in finding include files mentioned by
20203 the line number program). */
20204 bool record_lines_p = !decode_for_pst_p;
e142c38c 20205
b3b3bada 20206 baseaddr = objfile->text_section_offset ();
c906108c 20207
debd256d
JB
20208 line_ptr = lh->statement_program_start;
20209 line_end = lh->statement_program_end;
c906108c
SS
20210
20211 /* Read the statement sequences until there's nothing left. */
20212 while (line_ptr < line_end)
20213 {
6f77053d
PA
20214 /* The DWARF line number program state machine. Reset the state
20215 machine at the start of each sequence. */
804d2729 20216 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20217 bool end_sequence = false;
d9b3de22 20218
8c43009f 20219 if (record_lines_p)
c906108c 20220 {
8c43009f
PA
20221 /* Start a subfile for the current file of the state
20222 machine. */
20223 const file_entry *fe = state_machine.current_file ();
20224
20225 if (fe != NULL)
804d2729 20226 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20227 }
20228
a738430d 20229 /* Decode the table. */
d9b3de22 20230 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20231 {
20232 op_code = read_1_byte (abfd, line_ptr);
20233 line_ptr += 1;
9aa1fe7e 20234
debd256d 20235 if (op_code >= lh->opcode_base)
6e70227d 20236 {
8e07a239 20237 /* Special opcode. */
6f77053d 20238 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20239 }
20240 else switch (op_code)
c906108c
SS
20241 {
20242 case DW_LNS_extended_op:
3e43a32a
MS
20243 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20244 &bytes_read);
473b7be6 20245 line_ptr += bytes_read;
a8c50c1f 20246 extended_end = line_ptr + extended_len;
c906108c
SS
20247 extended_op = read_1_byte (abfd, line_ptr);
20248 line_ptr += 1;
20249 switch (extended_op)
20250 {
20251 case DW_LNE_end_sequence:
6f77053d
PA
20252 state_machine.handle_end_sequence ();
20253 end_sequence = true;
c906108c
SS
20254 break;
20255 case DW_LNE_set_address:
d9b3de22
DE
20256 {
20257 CORE_ADDR address
c8a7a66f 20258 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20259 line_ptr += bytes_read;
6f77053d
PA
20260
20261 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20262 lowpc - baseaddr, address);
6f77053d 20263 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20264 }
c906108c
SS
20265 break;
20266 case DW_LNE_define_file:
debd256d 20267 {
d521ce57 20268 const char *cur_file;
ecfb656c
PA
20269 unsigned int mod_time, length;
20270 dir_index dindex;
6e70227d 20271
3e43a32a
MS
20272 cur_file = read_direct_string (abfd, line_ptr,
20273 &bytes_read);
debd256d 20274 line_ptr += bytes_read;
ecfb656c 20275 dindex = (dir_index)
debd256d
JB
20276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20277 line_ptr += bytes_read;
20278 mod_time =
20279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20280 line_ptr += bytes_read;
20281 length =
20282 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20283 line_ptr += bytes_read;
ecfb656c 20284 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20285 }
c906108c 20286 break;
d0c6ba3d 20287 case DW_LNE_set_discriminator:
6f77053d
PA
20288 {
20289 /* The discriminator is not interesting to the
20290 debugger; just ignore it. We still need to
20291 check its value though:
20292 if there are consecutive entries for the same
20293 (non-prologue) line we want to coalesce them.
20294 PR 17276. */
20295 unsigned int discr
20296 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20297 line_ptr += bytes_read;
20298
20299 state_machine.handle_set_discriminator (discr);
20300 }
d0c6ba3d 20301 break;
c906108c 20302 default:
b98664d3 20303 complaint (_("mangled .debug_line section"));
debd256d 20304 return;
c906108c 20305 }
a8c50c1f
DJ
20306 /* Make sure that we parsed the extended op correctly. If e.g.
20307 we expected a different address size than the producer used,
20308 we may have read the wrong number of bytes. */
20309 if (line_ptr != extended_end)
20310 {
b98664d3 20311 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20312 return;
20313 }
c906108c
SS
20314 break;
20315 case DW_LNS_copy:
6f77053d 20316 state_machine.handle_copy ();
c906108c
SS
20317 break;
20318 case DW_LNS_advance_pc:
2dc7f7b3
TT
20319 {
20320 CORE_ADDR adjust
20321 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20322 line_ptr += bytes_read;
6f77053d
PA
20323
20324 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20325 }
c906108c
SS
20326 break;
20327 case DW_LNS_advance_line:
a05a36a5
DE
20328 {
20329 int line_delta
20330 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20331 line_ptr += bytes_read;
6f77053d
PA
20332
20333 state_machine.handle_advance_line (line_delta);
a05a36a5 20334 }
c906108c
SS
20335 break;
20336 case DW_LNS_set_file:
d9b3de22 20337 {
6f77053d 20338 file_name_index file
ecfb656c
PA
20339 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20340 &bytes_read);
d9b3de22 20341 line_ptr += bytes_read;
8c43009f 20342
6f77053d 20343 state_machine.handle_set_file (file);
d9b3de22 20344 }
c906108c
SS
20345 break;
20346 case DW_LNS_set_column:
0ad93d4f 20347 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20348 line_ptr += bytes_read;
20349 break;
20350 case DW_LNS_negate_stmt:
6f77053d 20351 state_machine.handle_negate_stmt ();
c906108c
SS
20352 break;
20353 case DW_LNS_set_basic_block:
c906108c 20354 break;
c2c6d25f
JM
20355 /* Add to the address register of the state machine the
20356 address increment value corresponding to special opcode
a738430d
MK
20357 255. I.e., this value is scaled by the minimum
20358 instruction length since special opcode 255 would have
b021a221 20359 scaled the increment. */
c906108c 20360 case DW_LNS_const_add_pc:
6f77053d 20361 state_machine.handle_const_add_pc ();
c906108c
SS
20362 break;
20363 case DW_LNS_fixed_advance_pc:
3e29f34a 20364 {
6f77053d 20365 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20366 line_ptr += 2;
6f77053d
PA
20367
20368 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20369 }
c906108c 20370 break;
9aa1fe7e 20371 default:
a738430d
MK
20372 {
20373 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20374 int i;
a738430d 20375
debd256d 20376 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20377 {
20378 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20379 line_ptr += bytes_read;
20380 }
20381 }
c906108c
SS
20382 }
20383 }
d9b3de22
DE
20384
20385 if (!end_sequence)
20386 dwarf2_debug_line_missing_end_sequence_complaint ();
20387
20388 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20389 in which case we still finish recording the last line). */
6f77053d 20390 state_machine.record_line (true);
c906108c 20391 }
f3f5162e
DE
20392}
20393
20394/* Decode the Line Number Program (LNP) for the given line_header
20395 structure and CU. The actual information extracted and the type
20396 of structures created from the LNP depends on the value of PST.
20397
20398 1. If PST is NULL, then this procedure uses the data from the program
20399 to create all necessary symbol tables, and their linetables.
20400
20401 2. If PST is not NULL, this procedure reads the program to determine
20402 the list of files included by the unit represented by PST, and
20403 builds all the associated partial symbol tables.
20404
20405 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20406 It is used for relative paths in the line table.
20407 NOTE: When processing partial symtabs (pst != NULL),
20408 comp_dir == pst->dirname.
20409
20410 NOTE: It is important that psymtabs have the same file name (via strcmp)
20411 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20412 symtab we don't use it in the name of the psymtabs we create.
20413 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20414 A good testcase for this is mb-inline.exp.
20415
527f3840
JK
20416 LOWPC is the lowest address in CU (or 0 if not known).
20417
20418 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20419 for its PC<->lines mapping information. Otherwise only the filename
20420 table is read in. */
f3f5162e
DE
20421
20422static void
20423dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20424 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20425 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20426{
518817b3 20427 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20428 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20429
527f3840
JK
20430 if (decode_mapping)
20431 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20432
20433 if (decode_for_pst_p)
20434 {
aaa75496
JB
20435 /* Now that we're done scanning the Line Header Program, we can
20436 create the psymtab of each included file. */
7ba99d21
AT
20437 for (auto &file_entry : lh->file_names ())
20438 if (file_entry.included_p == 1)
aaa75496 20439 {
c89b44cd 20440 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20441 const char *include_name =
7ba99d21
AT
20442 psymtab_include_file_name (lh, file_entry, pst,
20443 comp_dir, &name_holder);
c6da4cef 20444 if (include_name != NULL)
aaa75496
JB
20445 dwarf2_create_include_psymtab (include_name, pst, objfile);
20446 }
20447 }
cb1df416
DJ
20448 else
20449 {
20450 /* Make sure a symtab is created for every file, even files
20451 which contain only variables (i.e. no code with associated
20452 line numbers). */
c24bdb02
KS
20453 buildsym_compunit *builder = cu->get_builder ();
20454 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20455
7ba99d21 20456 for (auto &fe : lh->file_names ())
cb1df416 20457 {
804d2729 20458 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20459 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20460 {
c24bdb02 20461 builder->get_current_subfile ()->symtab
804d2729 20462 = allocate_symtab (cust,
c24bdb02 20463 builder->get_current_subfile ()->name);
43f3e411 20464 }
c24bdb02 20465 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20466 }
20467 }
c906108c
SS
20468}
20469
20470/* Start a subfile for DWARF. FILENAME is the name of the file and
20471 DIRNAME the name of the source directory which contains FILENAME
4d663531 20472 or NULL if not known.
c906108c
SS
20473 This routine tries to keep line numbers from identical absolute and
20474 relative file names in a common subfile.
20475
20476 Using the `list' example from the GDB testsuite, which resides in
20477 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20478 of /srcdir/list0.c yields the following debugging information for list0.c:
20479
c5aa993b 20480 DW_AT_name: /srcdir/list0.c
4d663531 20481 DW_AT_comp_dir: /compdir
357e46e7 20482 files.files[0].name: list0.h
c5aa993b 20483 files.files[0].dir: /srcdir
357e46e7 20484 files.files[1].name: list0.c
c5aa993b 20485 files.files[1].dir: /srcdir
c906108c
SS
20486
20487 The line number information for list0.c has to end up in a single
4f1520fb
FR
20488 subfile, so that `break /srcdir/list0.c:1' works as expected.
20489 start_subfile will ensure that this happens provided that we pass the
20490 concatenation of files.files[1].dir and files.files[1].name as the
20491 subfile's name. */
c906108c
SS
20492
20493static void
804d2729
TT
20494dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20495 const char *dirname)
c906108c 20496{
43816ebc 20497 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20498
4d663531 20499 /* In order not to lose the line information directory,
4f1520fb
FR
20500 we concatenate it to the filename when it makes sense.
20501 Note that the Dwarf3 standard says (speaking of filenames in line
20502 information): ``The directory index is ignored for file names
20503 that represent full path names''. Thus ignoring dirname in the
20504 `else' branch below isn't an issue. */
c906108c 20505
d5166ae1 20506 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20507 {
43816ebc
TT
20508 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20509 filename = copy.get ();
d521ce57 20510 }
c906108c 20511
c24bdb02 20512 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20513}
20514
804d2729
TT
20515/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20516 buildsym_compunit constructor. */
f4dc4d17 20517
c24bdb02
KS
20518struct compunit_symtab *
20519dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20520 CORE_ADDR low_pc)
f4dc4d17 20521{
c24bdb02 20522 gdb_assert (m_builder == nullptr);
43f3e411 20523
c24bdb02
KS
20524 m_builder.reset (new struct buildsym_compunit
20525 (per_cu->dwarf2_per_objfile->objfile,
20526 name, comp_dir, language, low_pc));
93b8bea4 20527
c24bdb02 20528 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20529
c24bdb02
KS
20530 get_builder ()->record_debugformat ("DWARF 2");
20531 get_builder ()->record_producer (producer);
f4dc4d17 20532
c24bdb02 20533 processing_has_namespace_info = false;
43f3e411 20534
c24bdb02 20535 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20536}
20537
4c2df51b
DJ
20538static void
20539var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20540 struct dwarf2_cu *cu)
4c2df51b 20541{
518817b3 20542 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20543 struct comp_unit_head *cu_header = &cu->header;
20544
4c2df51b
DJ
20545 /* NOTE drow/2003-01-30: There used to be a comment and some special
20546 code here to turn a symbol with DW_AT_external and a
20547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20549 with some versions of binutils) where shared libraries could have
20550 relocations against symbols in their debug information - the
20551 minimal symbol would have the right address, but the debug info
20552 would not. It's no longer necessary, because we will explicitly
20553 apply relocations when we read in the debug information now. */
20554
20555 /* A DW_AT_location attribute with no contents indicates that a
20556 variable has been optimized away. */
4fc6c0d5 20557 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20558 {
f1e6e072 20559 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20560 return;
20561 }
20562
20563 /* Handle one degenerate form of location expression specially, to
20564 preserve GDB's previous behavior when section offsets are
336d760d
AT
20565 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20566 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20567
4fc6c0d5 20568 if (attr->form_is_block ()
3019eac3
DE
20569 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20570 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20571 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20572 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20573 && (DW_BLOCK (attr)->size
20574 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20575 {
891d2f0b 20576 unsigned int dummy;
4c2df51b 20577
3019eac3 20578 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20579 SET_SYMBOL_VALUE_ADDRESS
20580 (sym, cu->header.read_address (objfile->obfd,
20581 DW_BLOCK (attr)->data + 1,
20582 &dummy));
3019eac3 20583 else
38583298
TT
20584 SET_SYMBOL_VALUE_ADDRESS
20585 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20586 &dummy));
f1e6e072 20587 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20588 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20589 SET_SYMBOL_VALUE_ADDRESS
20590 (sym,
20591 SYMBOL_VALUE_ADDRESS (sym)
20592 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20593 return;
20594 }
20595
20596 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20597 expression evaluator, and use LOC_COMPUTED only when necessary
20598 (i.e. when the value of a register or memory location is
20599 referenced, or a thread-local block, etc.). Then again, it might
20600 not be worthwhile. I'm assuming that it isn't unless performance
20601 or memory numbers show me otherwise. */
20602
f1e6e072 20603 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20604
f1e6e072 20605 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20606 cu->has_loclist = true;
4c2df51b
DJ
20607}
20608
c906108c
SS
20609/* Given a pointer to a DWARF information entry, figure out if we need
20610 to make a symbol table entry for it, and if so, create a new entry
20611 and return a pointer to it.
20612 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20613 used the passed type.
20614 If SPACE is not NULL, use it to hold the new symbol. If it is
20615 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20616
20617static struct symbol *
5e2db402
TT
20618new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20619 struct symbol *space)
c906108c 20620{
518817b3
SM
20621 struct dwarf2_per_objfile *dwarf2_per_objfile
20622 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20623 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20624 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20625 struct symbol *sym = NULL;
15d034d0 20626 const char *name;
c906108c
SS
20627 struct attribute *attr = NULL;
20628 struct attribute *attr2 = NULL;
e142c38c 20629 CORE_ADDR baseaddr;
e37fd15a
SW
20630 struct pending **list_to_add = NULL;
20631
edb3359d 20632 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20633
b3b3bada 20634 baseaddr = objfile->text_section_offset ();
c906108c 20635
94af9270 20636 name = dwarf2_name (die, cu);
c906108c
SS
20637 if (name)
20638 {
34eaf542 20639 int suppress_add = 0;
94af9270 20640
34eaf542
TT
20641 if (space)
20642 sym = space;
20643 else
8c14c3a3 20644 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20645 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20646
20647 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20648 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20649 /* Fortran does not have mangling standard and the mangling does differ
20650 between gfortran, iFort etc. */
bcfe6157
TT
20651 const char *physname
20652 = (cu->language == language_fortran
20653 ? dwarf2_full_name (name, die, cu)
20654 : dwarf2_physname (name, die, cu));
20655 const char *linkagename = dw2_linkage_name (die, cu);
20656
20657 if (linkagename == nullptr || cu->language == language_ada)
20658 sym->set_linkage_name (physname);
20659 else
20660 {
20661 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20662 sym->set_linkage_name (linkagename);
20663 }
f55ee35c 20664
c906108c 20665 /* Default assumptions.
c5aa993b 20666 Use the passed type or decode it from the die. */
176620f1 20667 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20668 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20669 if (type != NULL)
20670 SYMBOL_TYPE (sym) = type;
20671 else
e7c27a73 20672 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20673 attr = dwarf2_attr (die,
20674 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20675 cu);
435d3d88 20676 if (attr != nullptr)
c906108c
SS
20677 {
20678 SYMBOL_LINE (sym) = DW_UNSND (attr);
20679 }
cb1df416 20680
edb3359d
DJ
20681 attr = dwarf2_attr (die,
20682 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20683 cu);
435d3d88 20684 if (attr != nullptr)
cb1df416 20685 {
ecfb656c 20686 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20687 struct file_entry *fe;
9a619af0 20688
ecfb656c
PA
20689 if (cu->line_header != NULL)
20690 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20691 else
20692 fe = NULL;
20693
20694 if (fe == NULL)
b98664d3 20695 complaint (_("file index out of range"));
8c43009f
PA
20696 else
20697 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20698 }
20699
c906108c
SS
20700 switch (die->tag)
20701 {
20702 case DW_TAG_label:
e142c38c 20703 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20704 if (attr != nullptr)
3e29f34a
MR
20705 {
20706 CORE_ADDR addr;
20707
cd6c91b4 20708 addr = attr->value_as_address ();
3e29f34a 20709 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20710 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20711 }
0f5238ed
TT
20712 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20713 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20714 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20715 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20716 break;
20717 case DW_TAG_subprogram:
20718 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20719 finish_block. */
f1e6e072 20720 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20721 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20722 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20723 || cu->language == language_ada
20724 || cu->language == language_fortran)
c906108c 20725 {
2cfa0c8d 20726 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20727 Ada and Fortran subprograms, whether marked external or
20728 not, are always stored as a global symbol, because we want
20729 to be able to access them globally. For instance, we want
20730 to be able to break on a nested subprogram without having
20731 to specify the context. */
c24bdb02 20732 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20733 }
20734 else
20735 {
e37fd15a 20736 list_to_add = cu->list_in_scope;
c906108c
SS
20737 }
20738 break;
edb3359d
DJ
20739 case DW_TAG_inlined_subroutine:
20740 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20741 finish_block. */
f1e6e072 20742 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20743 SYMBOL_INLINED (sym) = 1;
481860b3 20744 list_to_add = cu->list_in_scope;
edb3359d 20745 break;
34eaf542
TT
20746 case DW_TAG_template_value_param:
20747 suppress_add = 1;
20748 /* Fall through. */
72929c62 20749 case DW_TAG_constant:
c906108c 20750 case DW_TAG_variable:
254e6b9e 20751 case DW_TAG_member:
0963b4bd
MS
20752 /* Compilation with minimal debug info may result in
20753 variables with missing type entries. Change the
20754 misleading `void' type to something sensible. */
78134374 20755 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20756 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20757
e142c38c 20758 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20759 /* In the case of DW_TAG_member, we should only be called for
20760 static const members. */
20761 if (die->tag == DW_TAG_member)
20762 {
3863f96c
DE
20763 /* dwarf2_add_field uses die_is_declaration,
20764 so we do the same. */
254e6b9e
DE
20765 gdb_assert (die_is_declaration (die, cu));
20766 gdb_assert (attr);
20767 }
435d3d88 20768 if (attr != nullptr)
c906108c 20769 {
e7c27a73 20770 dwarf2_const_value (attr, sym, cu);
e142c38c 20771 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20772 if (!suppress_add)
34eaf542
TT
20773 {
20774 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20775 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20776 else
e37fd15a 20777 list_to_add = cu->list_in_scope;
34eaf542 20778 }
c906108c
SS
20779 break;
20780 }
e142c38c 20781 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20782 if (attr != nullptr)
c906108c 20783 {
e7c27a73 20784 var_decode_location (attr, sym, cu);
e142c38c 20785 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20786
20787 /* Fortran explicitly imports any global symbols to the local
20788 scope by DW_TAG_common_block. */
20789 if (cu->language == language_fortran && die->parent
20790 && die->parent->tag == DW_TAG_common_block)
20791 attr2 = NULL;
20792
caac4577
JG
20793 if (SYMBOL_CLASS (sym) == LOC_STATIC
20794 && SYMBOL_VALUE_ADDRESS (sym) == 0
20795 && !dwarf2_per_objfile->has_section_at_zero)
20796 {
20797 /* When a static variable is eliminated by the linker,
20798 the corresponding debug information is not stripped
20799 out, but the variable address is set to null;
20800 do not add such variables into symbol table. */
20801 }
20802 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20803 {
4b610737
TT
20804 if (SYMBOL_CLASS (sym) == LOC_STATIC
20805 && (objfile->flags & OBJF_MAINLINE) == 0
20806 && dwarf2_per_objfile->can_copy)
20807 {
20808 /* A global static variable might be subject to
20809 copy relocation. We first check for a local
20810 minsym, though, because maybe the symbol was
20811 marked hidden, in which case this would not
20812 apply. */
20813 bound_minimal_symbol found
20814 = (lookup_minimal_symbol_linkage
987012b8 20815 (sym->linkage_name (), objfile));
4b610737
TT
20816 if (found.minsym != nullptr)
20817 sym->maybe_copied = 1;
20818 }
f55ee35c 20819
1c809c68
TT
20820 /* A variable with DW_AT_external is never static,
20821 but it may be block-scoped. */
804d2729 20822 list_to_add
c24bdb02
KS
20823 = ((cu->list_in_scope
20824 == cu->get_builder ()->get_file_symbols ())
20825 ? cu->get_builder ()->get_global_symbols ()
804d2729 20826 : cu->list_in_scope);
1c809c68 20827 }
c906108c 20828 else
e37fd15a 20829 list_to_add = cu->list_in_scope;
c906108c
SS
20830 }
20831 else
20832 {
20833 /* We do not know the address of this symbol.
c5aa993b
JM
20834 If it is an external symbol and we have type information
20835 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20836 The address of the variable will then be determined from
20837 the minimal symbol table whenever the variable is
20838 referenced. */
e142c38c 20839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20840
20841 /* Fortran explicitly imports any global symbols to the local
20842 scope by DW_TAG_common_block. */
20843 if (cu->language == language_fortran && die->parent
20844 && die->parent->tag == DW_TAG_common_block)
20845 {
20846 /* SYMBOL_CLASS doesn't matter here because
20847 read_common_block is going to reset it. */
20848 if (!suppress_add)
20849 list_to_add = cu->list_in_scope;
20850 }
20851 else if (attr2 && (DW_UNSND (attr2) != 0)
20852 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20853 {
0fe7935b
DJ
20854 /* A variable with DW_AT_external is never static, but it
20855 may be block-scoped. */
804d2729 20856 list_to_add
c24bdb02
KS
20857 = ((cu->list_in_scope
20858 == cu->get_builder ()->get_file_symbols ())
20859 ? cu->get_builder ()->get_global_symbols ()
804d2729 20860 : cu->list_in_scope);
0fe7935b 20861
f1e6e072 20862 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20863 }
442ddf59
JK
20864 else if (!die_is_declaration (die, cu))
20865 {
20866 /* Use the default LOC_OPTIMIZED_OUT class. */
20867 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20868 if (!suppress_add)
20869 list_to_add = cu->list_in_scope;
442ddf59 20870 }
c906108c
SS
20871 }
20872 break;
20873 case DW_TAG_formal_parameter:
a60f3166
TT
20874 {
20875 /* If we are inside a function, mark this as an argument. If
20876 not, we might be looking at an argument to an inlined function
20877 when we do not have enough information to show inlined frames;
20878 pretend it's a local variable in that case so that the user can
20879 still see it. */
804d2729 20880 struct context_stack *curr
c24bdb02 20881 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20882 if (curr != nullptr && curr->name != nullptr)
20883 SYMBOL_IS_ARGUMENT (sym) = 1;
20884 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20885 if (attr != nullptr)
a60f3166
TT
20886 {
20887 var_decode_location (attr, sym, cu);
20888 }
20889 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20890 if (attr != nullptr)
a60f3166
TT
20891 {
20892 dwarf2_const_value (attr, sym, cu);
20893 }
f346a30d 20894
a60f3166
TT
20895 list_to_add = cu->list_in_scope;
20896 }
c906108c
SS
20897 break;
20898 case DW_TAG_unspecified_parameters:
20899 /* From varargs functions; gdb doesn't seem to have any
20900 interest in this information, so just ignore it for now.
20901 (FIXME?) */
20902 break;
34eaf542
TT
20903 case DW_TAG_template_type_param:
20904 suppress_add = 1;
20905 /* Fall through. */
c906108c 20906 case DW_TAG_class_type:
680b30c7 20907 case DW_TAG_interface_type:
c906108c
SS
20908 case DW_TAG_structure_type:
20909 case DW_TAG_union_type:
72019c9c 20910 case DW_TAG_set_type:
c906108c 20911 case DW_TAG_enumeration_type:
f1e6e072 20912 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20913 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20914
63d06c5c 20915 {
9c37b5ae 20916 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20917 really ever be static objects: otherwise, if you try
20918 to, say, break of a class's method and you're in a file
20919 which doesn't mention that class, it won't work unless
20920 the check for all static symbols in lookup_symbol_aux
20921 saves you. See the OtherFileClass tests in
20922 gdb.c++/namespace.exp. */
20923
e37fd15a 20924 if (!suppress_add)
34eaf542 20925 {
c24bdb02 20926 buildsym_compunit *builder = cu->get_builder ();
804d2729 20927 list_to_add
c24bdb02 20928 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20929 && cu->language == language_cplus
c24bdb02 20930 ? builder->get_global_symbols ()
804d2729 20931 : cu->list_in_scope);
63d06c5c 20932
64382290 20933 /* The semantics of C++ state that "struct foo {
9c37b5ae 20934 ... }" also defines a typedef for "foo". */
64382290 20935 if (cu->language == language_cplus
45280282 20936 || cu->language == language_ada
c44af4eb
TT
20937 || cu->language == language_d
20938 || cu->language == language_rust)
64382290
TT
20939 {
20940 /* The symbol's name is already allocated along
20941 with this objfile, so we don't need to
20942 duplicate it for the type. */
7d93a1e0 20943 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20944 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20945 }
63d06c5c
DC
20946 }
20947 }
c906108c
SS
20948 break;
20949 case DW_TAG_typedef:
f1e6e072 20950 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20951 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20952 list_to_add = cu->list_in_scope;
63d06c5c 20953 break;
c906108c 20954 case DW_TAG_base_type:
a02abb62 20955 case DW_TAG_subrange_type:
f1e6e072 20956 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20957 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20958 list_to_add = cu->list_in_scope;
c906108c
SS
20959 break;
20960 case DW_TAG_enumerator:
e142c38c 20961 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20962 if (attr != nullptr)
c906108c 20963 {
e7c27a73 20964 dwarf2_const_value (attr, sym, cu);
c906108c 20965 }
63d06c5c
DC
20966 {
20967 /* NOTE: carlton/2003-11-10: See comment above in the
20968 DW_TAG_class_type, etc. block. */
20969
804d2729 20970 list_to_add
c24bdb02 20971 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20972 && cu->language == language_cplus
c24bdb02 20973 ? cu->get_builder ()->get_global_symbols ()
804d2729 20974 : cu->list_in_scope);
63d06c5c 20975 }
c906108c 20976 break;
74921315 20977 case DW_TAG_imported_declaration:
5c4e30ca 20978 case DW_TAG_namespace:
f1e6e072 20979 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20980 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20981 break;
530e8392
KB
20982 case DW_TAG_module:
20983 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20984 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20985 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20986 break;
4357ac6c 20987 case DW_TAG_common_block:
f1e6e072 20988 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20989 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20990 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20991 break;
c906108c
SS
20992 default:
20993 /* Not a tag we recognize. Hopefully we aren't processing
20994 trash data, but since we must specifically ignore things
20995 we don't recognize, there is nothing else we should do at
0963b4bd 20996 this point. */
b98664d3 20997 complaint (_("unsupported tag: '%s'"),
4d3c2250 20998 dwarf_tag_name (die->tag));
c906108c
SS
20999 break;
21000 }
df8a16a1 21001
e37fd15a
SW
21002 if (suppress_add)
21003 {
21004 sym->hash_next = objfile->template_symbols;
21005 objfile->template_symbols = sym;
21006 list_to_add = NULL;
21007 }
21008
21009 if (list_to_add != NULL)
d3cb6808 21010 add_symbol_to_list (sym, list_to_add);
e37fd15a 21011
df8a16a1
DJ
21012 /* For the benefit of old versions of GCC, check for anonymous
21013 namespaces based on the demangled name. */
4d4ec4e5 21014 if (!cu->processing_has_namespace_info
94af9270 21015 && cu->language == language_cplus)
c24bdb02 21016 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21017 }
21018 return (sym);
21019}
21020
98bfdba5
PA
21021/* Given an attr with a DW_FORM_dataN value in host byte order,
21022 zero-extend it as appropriate for the symbol's type. The DWARF
21023 standard (v4) is not entirely clear about the meaning of using
21024 DW_FORM_dataN for a constant with a signed type, where the type is
21025 wider than the data. The conclusion of a discussion on the DWARF
21026 list was that this is unspecified. We choose to always zero-extend
21027 because that is the interpretation long in use by GCC. */
c906108c 21028
98bfdba5 21029static gdb_byte *
ff39bb5e 21030dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21031 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21032{
518817b3 21033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21034 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21035 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21036 LONGEST l = DW_UNSND (attr);
21037
21038 if (bits < sizeof (*value) * 8)
21039 {
21040 l &= ((LONGEST) 1 << bits) - 1;
21041 *value = l;
21042 }
21043 else if (bits == sizeof (*value) * 8)
21044 *value = l;
21045 else
21046 {
224c3ddb 21047 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21048 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21049 return bytes;
21050 }
21051
21052 return NULL;
21053}
21054
21055/* Read a constant value from an attribute. Either set *VALUE, or if
21056 the value does not fit in *VALUE, set *BYTES - either already
21057 allocated on the objfile obstack, or newly allocated on OBSTACK,
21058 or, set *BATON, if we translated the constant to a location
21059 expression. */
21060
21061static void
ff39bb5e 21062dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21063 const char *name, struct obstack *obstack,
21064 struct dwarf2_cu *cu,
d521ce57 21065 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21066 struct dwarf2_locexpr_baton **baton)
21067{
518817b3 21068 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21069 struct comp_unit_head *cu_header = &cu->header;
c906108c 21070 struct dwarf_block *blk;
98bfdba5
PA
21071 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21072 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21073
21074 *value = 0;
21075 *bytes = NULL;
21076 *baton = NULL;
c906108c
SS
21077
21078 switch (attr->form)
21079 {
21080 case DW_FORM_addr:
336d760d 21081 case DW_FORM_addrx:
3019eac3 21082 case DW_FORM_GNU_addr_index:
ac56253d 21083 {
ac56253d
TT
21084 gdb_byte *data;
21085
98bfdba5
PA
21086 if (TYPE_LENGTH (type) != cu_header->addr_size)
21087 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21088 cu_header->addr_size,
98bfdba5 21089 TYPE_LENGTH (type));
ac56253d
TT
21090 /* Symbols of this form are reasonably rare, so we just
21091 piggyback on the existing location code rather than writing
21092 a new implementation of symbol_computed_ops. */
8d749320 21093 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21094 (*baton)->per_cu = cu->per_cu;
21095 gdb_assert ((*baton)->per_cu);
ac56253d 21096
98bfdba5 21097 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21098 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21099 (*baton)->data = data;
ac56253d
TT
21100
21101 data[0] = DW_OP_addr;
21102 store_unsigned_integer (&data[1], cu_header->addr_size,
21103 byte_order, DW_ADDR (attr));
21104 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21105 }
c906108c 21106 break;
4ac36638 21107 case DW_FORM_string:
93b5768b 21108 case DW_FORM_strp:
cf532bd1 21109 case DW_FORM_strx:
3019eac3 21110 case DW_FORM_GNU_str_index:
36586728 21111 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21112 /* DW_STRING is already allocated on the objfile obstack, point
21113 directly to it. */
d521ce57 21114 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21115 break;
c906108c
SS
21116 case DW_FORM_block1:
21117 case DW_FORM_block2:
21118 case DW_FORM_block4:
21119 case DW_FORM_block:
2dc7f7b3 21120 case DW_FORM_exprloc:
0224619f 21121 case DW_FORM_data16:
c906108c 21122 blk = DW_BLOCK (attr);
98bfdba5
PA
21123 if (TYPE_LENGTH (type) != blk->size)
21124 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21125 TYPE_LENGTH (type));
21126 *bytes = blk->data;
c906108c 21127 break;
2df3850c
JM
21128
21129 /* The DW_AT_const_value attributes are supposed to carry the
21130 symbol's value "represented as it would be on the target
21131 architecture." By the time we get here, it's already been
21132 converted to host endianness, so we just need to sign- or
21133 zero-extend it as appropriate. */
21134 case DW_FORM_data1:
3aef2284 21135 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21136 break;
c906108c 21137 case DW_FORM_data2:
3aef2284 21138 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21139 break;
c906108c 21140 case DW_FORM_data4:
3aef2284 21141 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21142 break;
c906108c 21143 case DW_FORM_data8:
3aef2284 21144 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21145 break;
21146
c906108c 21147 case DW_FORM_sdata:
663c44ac 21148 case DW_FORM_implicit_const:
98bfdba5 21149 *value = DW_SND (attr);
2df3850c
JM
21150 break;
21151
c906108c 21152 case DW_FORM_udata:
98bfdba5 21153 *value = DW_UNSND (attr);
c906108c 21154 break;
2df3850c 21155
c906108c 21156 default:
b98664d3 21157 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21158 dwarf_form_name (attr->form));
98bfdba5 21159 *value = 0;
c906108c
SS
21160 break;
21161 }
21162}
21163
2df3850c 21164
98bfdba5
PA
21165/* Copy constant value from an attribute to a symbol. */
21166
2df3850c 21167static void
ff39bb5e 21168dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21169 struct dwarf2_cu *cu)
2df3850c 21170{
518817b3 21171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21172 LONGEST value;
d521ce57 21173 const gdb_byte *bytes;
98bfdba5 21174 struct dwarf2_locexpr_baton *baton;
2df3850c 21175
98bfdba5 21176 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21177 sym->print_name (),
98bfdba5
PA
21178 &objfile->objfile_obstack, cu,
21179 &value, &bytes, &baton);
2df3850c 21180
98bfdba5
PA
21181 if (baton != NULL)
21182 {
98bfdba5 21183 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21184 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21185 }
21186 else if (bytes != NULL)
21187 {
21188 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21189 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21190 }
21191 else
21192 {
21193 SYMBOL_VALUE (sym) = value;
f1e6e072 21194 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21195 }
2df3850c
JM
21196}
21197
c906108c
SS
21198/* Return the type of the die in question using its DW_AT_type attribute. */
21199
21200static struct type *
e7c27a73 21201die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21202{
c906108c 21203 struct attribute *type_attr;
c906108c 21204
e142c38c 21205 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21206 if (!type_attr)
21207 {
518817b3 21208 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21209 /* A missing DW_AT_type represents a void type. */
518817b3 21210 return objfile_type (objfile)->builtin_void;
c906108c 21211 }
348e048f 21212
673bfd45 21213 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21214}
21215
b4ba55a1
JB
21216/* True iff CU's producer generates GNAT Ada auxiliary information
21217 that allows to find parallel types through that information instead
21218 of having to do expensive parallel lookups by type name. */
21219
21220static int
21221need_gnat_info (struct dwarf2_cu *cu)
21222{
de4cb04a
JB
21223 /* Assume that the Ada compiler was GNAT, which always produces
21224 the auxiliary information. */
21225 return (cu->language == language_ada);
b4ba55a1
JB
21226}
21227
b4ba55a1
JB
21228/* Return the auxiliary type of the die in question using its
21229 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21230 attribute is not present. */
21231
21232static struct type *
21233die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21234{
b4ba55a1 21235 struct attribute *type_attr;
b4ba55a1
JB
21236
21237 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21238 if (!type_attr)
21239 return NULL;
21240
673bfd45 21241 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21242}
21243
21244/* If DIE has a descriptive_type attribute, then set the TYPE's
21245 descriptive type accordingly. */
21246
21247static void
21248set_descriptive_type (struct type *type, struct die_info *die,
21249 struct dwarf2_cu *cu)
21250{
21251 struct type *descriptive_type = die_descriptive_type (die, cu);
21252
21253 if (descriptive_type)
21254 {
21255 ALLOCATE_GNAT_AUX_TYPE (type);
21256 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21257 }
21258}
21259
c906108c
SS
21260/* Return the containing type of the die in question using its
21261 DW_AT_containing_type attribute. */
21262
21263static struct type *
e7c27a73 21264die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21265{
c906108c 21266 struct attribute *type_attr;
518817b3 21267 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21268
e142c38c 21269 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21270 if (!type_attr)
21271 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21272 "[in module %s]"), objfile_name (objfile));
33ac96f0 21273
673bfd45 21274 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21275}
21276
ac9ec31b
DE
21277/* Return an error marker type to use for the ill formed type in DIE/CU. */
21278
21279static struct type *
21280build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21281{
518817b3
SM
21282 struct dwarf2_per_objfile *dwarf2_per_objfile
21283 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21284 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21285 char *saved;
ac9ec31b 21286
528e1572
SM
21287 std::string message
21288 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21289 objfile_name (objfile),
21290 sect_offset_str (cu->header.sect_off),
21291 sect_offset_str (die->sect_off));
efba19b0 21292 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21293
19f392bc 21294 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21295}
21296
673bfd45 21297/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21298 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21299 DW_AT_containing_type.
673bfd45
DE
21300 If there is no type substitute an error marker. */
21301
c906108c 21302static struct type *
ff39bb5e 21303lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21304 struct dwarf2_cu *cu)
c906108c 21305{
518817b3
SM
21306 struct dwarf2_per_objfile *dwarf2_per_objfile
21307 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21308 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21309 struct type *this_type;
21310
ac9ec31b
DE
21311 gdb_assert (attr->name == DW_AT_type
21312 || attr->name == DW_AT_GNAT_descriptive_type
21313 || attr->name == DW_AT_containing_type);
21314
673bfd45
DE
21315 /* First see if we have it cached. */
21316
36586728
TT
21317 if (attr->form == DW_FORM_GNU_ref_alt)
21318 {
21319 struct dwarf2_per_cu_data *per_cu;
0826b30a 21320 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21321
ed2dc618
SM
21322 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21323 dwarf2_per_objfile);
9c541725 21324 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21325 }
cd6c91b4 21326 else if (attr->form_is_ref ())
673bfd45 21327 {
0826b30a 21328 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21329
9c541725 21330 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21331 }
55f1336d 21332 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21333 {
ac9ec31b 21334 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21335
ac9ec31b 21336 return get_signatured_type (die, signature, cu);
673bfd45
DE
21337 }
21338 else
21339 {
b98664d3 21340 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21341 " at %s [in module %s]"),
21342 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21343 objfile_name (objfile));
ac9ec31b 21344 return build_error_marker_type (cu, die);
673bfd45
DE
21345 }
21346
21347 /* If not cached we need to read it in. */
21348
21349 if (this_type == NULL)
21350 {
ac9ec31b 21351 struct die_info *type_die = NULL;
673bfd45
DE
21352 struct dwarf2_cu *type_cu = cu;
21353
cd6c91b4 21354 if (attr->form_is_ref ())
ac9ec31b
DE
21355 type_die = follow_die_ref (die, attr, &type_cu);
21356 if (type_die == NULL)
21357 return build_error_marker_type (cu, die);
21358 /* If we find the type now, it's probably because the type came
3019eac3
DE
21359 from an inter-CU reference and the type's CU got expanded before
21360 ours. */
ac9ec31b 21361 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21362 }
21363
21364 /* If we still don't have a type use an error marker. */
21365
21366 if (this_type == NULL)
ac9ec31b 21367 return build_error_marker_type (cu, die);
673bfd45 21368
f792889a 21369 return this_type;
c906108c
SS
21370}
21371
673bfd45
DE
21372/* Return the type in DIE, CU.
21373 Returns NULL for invalid types.
21374
02142a6c 21375 This first does a lookup in die_type_hash,
673bfd45
DE
21376 and only reads the die in if necessary.
21377
21378 NOTE: This can be called when reading in partial or full symbols. */
21379
f792889a 21380static struct type *
e7c27a73 21381read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21382{
f792889a
DJ
21383 struct type *this_type;
21384
21385 this_type = get_die_type (die, cu);
21386 if (this_type)
21387 return this_type;
21388
673bfd45
DE
21389 return read_type_die_1 (die, cu);
21390}
21391
21392/* Read the type in DIE, CU.
21393 Returns NULL for invalid types. */
21394
21395static struct type *
21396read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21397{
21398 struct type *this_type = NULL;
21399
c906108c
SS
21400 switch (die->tag)
21401 {
21402 case DW_TAG_class_type:
680b30c7 21403 case DW_TAG_interface_type:
c906108c
SS
21404 case DW_TAG_structure_type:
21405 case DW_TAG_union_type:
f792889a 21406 this_type = read_structure_type (die, cu);
c906108c
SS
21407 break;
21408 case DW_TAG_enumeration_type:
f792889a 21409 this_type = read_enumeration_type (die, cu);
c906108c
SS
21410 break;
21411 case DW_TAG_subprogram:
21412 case DW_TAG_subroutine_type:
edb3359d 21413 case DW_TAG_inlined_subroutine:
f792889a 21414 this_type = read_subroutine_type (die, cu);
c906108c
SS
21415 break;
21416 case DW_TAG_array_type:
f792889a 21417 this_type = read_array_type (die, cu);
c906108c 21418 break;
72019c9c 21419 case DW_TAG_set_type:
f792889a 21420 this_type = read_set_type (die, cu);
72019c9c 21421 break;
c906108c 21422 case DW_TAG_pointer_type:
f792889a 21423 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21424 break;
21425 case DW_TAG_ptr_to_member_type:
f792889a 21426 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21427 break;
21428 case DW_TAG_reference_type:
4297a3f0
AV
21429 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21430 break;
21431 case DW_TAG_rvalue_reference_type:
21432 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21433 break;
21434 case DW_TAG_const_type:
f792889a 21435 this_type = read_tag_const_type (die, cu);
c906108c
SS
21436 break;
21437 case DW_TAG_volatile_type:
f792889a 21438 this_type = read_tag_volatile_type (die, cu);
c906108c 21439 break;
06d66ee9
TT
21440 case DW_TAG_restrict_type:
21441 this_type = read_tag_restrict_type (die, cu);
21442 break;
c906108c 21443 case DW_TAG_string_type:
f792889a 21444 this_type = read_tag_string_type (die, cu);
c906108c
SS
21445 break;
21446 case DW_TAG_typedef:
f792889a 21447 this_type = read_typedef (die, cu);
c906108c 21448 break;
a02abb62 21449 case DW_TAG_subrange_type:
f792889a 21450 this_type = read_subrange_type (die, cu);
a02abb62 21451 break;
c906108c 21452 case DW_TAG_base_type:
f792889a 21453 this_type = read_base_type (die, cu);
c906108c 21454 break;
81a17f79 21455 case DW_TAG_unspecified_type:
f792889a 21456 this_type = read_unspecified_type (die, cu);
81a17f79 21457 break;
0114d602
DJ
21458 case DW_TAG_namespace:
21459 this_type = read_namespace_type (die, cu);
21460 break;
f55ee35c
JK
21461 case DW_TAG_module:
21462 this_type = read_module_type (die, cu);
21463 break;
a2c2acaf
MW
21464 case DW_TAG_atomic_type:
21465 this_type = read_tag_atomic_type (die, cu);
21466 break;
c906108c 21467 default:
b98664d3 21468 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21469 dwarf_tag_name (die->tag));
c906108c
SS
21470 break;
21471 }
63d06c5c 21472
f792889a 21473 return this_type;
63d06c5c
DC
21474}
21475
abc72ce4
DE
21476/* See if we can figure out if the class lives in a namespace. We do
21477 this by looking for a member function; its demangled name will
21478 contain namespace info, if there is any.
21479 Return the computed name or NULL.
21480 Space for the result is allocated on the objfile's obstack.
21481 This is the full-die version of guess_partial_die_structure_name.
21482 In this case we know DIE has no useful parent. */
21483
43816ebc 21484static const char *
abc72ce4
DE
21485guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21486{
21487 struct die_info *spec_die;
21488 struct dwarf2_cu *spec_cu;
21489 struct die_info *child;
518817b3 21490 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21491
21492 spec_cu = cu;
21493 spec_die = die_specification (die, &spec_cu);
21494 if (spec_die != NULL)
21495 {
21496 die = spec_die;
21497 cu = spec_cu;
21498 }
21499
21500 for (child = die->child;
21501 child != NULL;
21502 child = child->sibling)
21503 {
21504 if (child->tag == DW_TAG_subprogram)
21505 {
73b9be8b 21506 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21507
7d45c7c3 21508 if (linkage_name != NULL)
abc72ce4 21509 {
43816ebc
TT
21510 gdb::unique_xmalloc_ptr<char> actual_name
21511 (language_class_name_from_physname (cu->language_defn,
21512 linkage_name));
21513 const char *name = NULL;
abc72ce4
DE
21514
21515 if (actual_name != NULL)
21516 {
15d034d0 21517 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21518
21519 if (die_name != NULL
43816ebc 21520 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21521 {
21522 /* Strip off the class name from the full name.
21523 We want the prefix. */
21524 int die_name_len = strlen (die_name);
43816ebc
TT
21525 int actual_name_len = strlen (actual_name.get ());
21526 const char *ptr = actual_name.get ();
abc72ce4
DE
21527
21528 /* Test for '::' as a sanity check. */
21529 if (actual_name_len > die_name_len + 2
43816ebc 21530 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21531 name = obstack_strndup (
e3b94546 21532 &objfile->per_bfd->storage_obstack,
43816ebc 21533 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21534 }
21535 }
abc72ce4
DE
21536 return name;
21537 }
21538 }
21539 }
21540
21541 return NULL;
21542}
21543
96408a79
SA
21544/* GCC might emit a nameless typedef that has a linkage name. Determine the
21545 prefix part in such case. See
21546 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21547
a121b7c1 21548static const char *
96408a79
SA
21549anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21550{
21551 struct attribute *attr;
e6a959d6 21552 const char *base;
96408a79
SA
21553
21554 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21555 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21556 return NULL;
21557
7d45c7c3 21558 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21559 return NULL;
21560
73b9be8b 21561 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21562 if (attr == NULL || DW_STRING (attr) == NULL)
21563 return NULL;
21564
21565 /* dwarf2_name had to be already called. */
21566 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21567
21568 /* Strip the base name, keep any leading namespaces/classes. */
21569 base = strrchr (DW_STRING (attr), ':');
21570 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21571 return "";
21572
518817b3 21573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21574 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21575 DW_STRING (attr),
21576 &base[-1] - DW_STRING (attr));
96408a79
SA
21577}
21578
fdde2d81 21579/* Return the name of the namespace/class that DIE is defined within,
0114d602 21580 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21581
0114d602
DJ
21582 For example, if we're within the method foo() in the following
21583 code:
21584
21585 namespace N {
21586 class C {
21587 void foo () {
21588 }
21589 };
21590 }
21591
21592 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21593
0d5cff50 21594static const char *
e142c38c 21595determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21596{
518817b3
SM
21597 struct dwarf2_per_objfile *dwarf2_per_objfile
21598 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21599 struct die_info *parent, *spec_die;
21600 struct dwarf2_cu *spec_cu;
21601 struct type *parent_type;
a121b7c1 21602 const char *retval;
63d06c5c 21603
9c37b5ae 21604 if (cu->language != language_cplus
c44af4eb
TT
21605 && cu->language != language_fortran && cu->language != language_d
21606 && cu->language != language_rust)
0114d602
DJ
21607 return "";
21608
96408a79
SA
21609 retval = anonymous_struct_prefix (die, cu);
21610 if (retval)
21611 return retval;
21612
0114d602
DJ
21613 /* We have to be careful in the presence of DW_AT_specification.
21614 For example, with GCC 3.4, given the code
21615
21616 namespace N {
21617 void foo() {
21618 // Definition of N::foo.
21619 }
21620 }
21621
21622 then we'll have a tree of DIEs like this:
21623
21624 1: DW_TAG_compile_unit
21625 2: DW_TAG_namespace // N
21626 3: DW_TAG_subprogram // declaration of N::foo
21627 4: DW_TAG_subprogram // definition of N::foo
21628 DW_AT_specification // refers to die #3
21629
21630 Thus, when processing die #4, we have to pretend that we're in
21631 the context of its DW_AT_specification, namely the contex of die
21632 #3. */
21633 spec_cu = cu;
21634 spec_die = die_specification (die, &spec_cu);
21635 if (spec_die == NULL)
21636 parent = die->parent;
21637 else
63d06c5c 21638 {
0114d602
DJ
21639 parent = spec_die->parent;
21640 cu = spec_cu;
63d06c5c 21641 }
0114d602
DJ
21642
21643 if (parent == NULL)
21644 return "";
98bfdba5
PA
21645 else if (parent->building_fullname)
21646 {
21647 const char *name;
21648 const char *parent_name;
21649
21650 /* It has been seen on RealView 2.2 built binaries,
21651 DW_TAG_template_type_param types actually _defined_ as
21652 children of the parent class:
21653
21654 enum E {};
21655 template class <class Enum> Class{};
21656 Class<enum E> class_e;
21657
21658 1: DW_TAG_class_type (Class)
21659 2: DW_TAG_enumeration_type (E)
21660 3: DW_TAG_enumerator (enum1:0)
21661 3: DW_TAG_enumerator (enum2:1)
21662 ...
21663 2: DW_TAG_template_type_param
21664 DW_AT_type DW_FORM_ref_udata (E)
21665
21666 Besides being broken debug info, it can put GDB into an
21667 infinite loop. Consider:
21668
21669 When we're building the full name for Class<E>, we'll start
21670 at Class, and go look over its template type parameters,
21671 finding E. We'll then try to build the full name of E, and
21672 reach here. We're now trying to build the full name of E,
21673 and look over the parent DIE for containing scope. In the
21674 broken case, if we followed the parent DIE of E, we'd again
21675 find Class, and once again go look at its template type
21676 arguments, etc., etc. Simply don't consider such parent die
21677 as source-level parent of this die (it can't be, the language
21678 doesn't allow it), and break the loop here. */
21679 name = dwarf2_name (die, cu);
21680 parent_name = dwarf2_name (parent, cu);
b98664d3 21681 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21682 name ? name : "<unknown>",
21683 parent_name ? parent_name : "<unknown>");
21684 return "";
21685 }
63d06c5c 21686 else
0114d602
DJ
21687 switch (parent->tag)
21688 {
63d06c5c 21689 case DW_TAG_namespace:
0114d602 21690 parent_type = read_type_die (parent, cu);
acebe513
UW
21691 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21692 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21693 Work around this problem here. */
21694 if (cu->language == language_cplus
7d93a1e0 21695 && strcmp (parent_type->name (), "::") == 0)
acebe513 21696 return "";
0114d602 21697 /* We give a name to even anonymous namespaces. */
7d93a1e0 21698 return parent_type->name ();
63d06c5c 21699 case DW_TAG_class_type:
680b30c7 21700 case DW_TAG_interface_type:
63d06c5c 21701 case DW_TAG_structure_type:
0114d602 21702 case DW_TAG_union_type:
f55ee35c 21703 case DW_TAG_module:
0114d602 21704 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21705 if (parent_type->name () != NULL)
21706 return parent_type->name ();
0114d602
DJ
21707 else
21708 /* An anonymous structure is only allowed non-static data
21709 members; no typedefs, no member functions, et cetera.
21710 So it does not need a prefix. */
21711 return "";
abc72ce4 21712 case DW_TAG_compile_unit:
95554aad 21713 case DW_TAG_partial_unit:
abc72ce4
DE
21714 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21715 if (cu->language == language_cplus
fd5866f6 21716 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21717 && die->child != NULL
21718 && (die->tag == DW_TAG_class_type
21719 || die->tag == DW_TAG_structure_type
21720 || die->tag == DW_TAG_union_type))
21721 {
43816ebc 21722 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21723 if (name != NULL)
21724 return name;
21725 }
21726 return "";
0a4b0913
AB
21727 case DW_TAG_subprogram:
21728 /* Nested subroutines in Fortran get a prefix with the name
21729 of the parent's subroutine. */
21730 if (cu->language == language_fortran)
21731 {
21732 if ((die->tag == DW_TAG_subprogram)
21733 && (dwarf2_name (parent, cu) != NULL))
21734 return dwarf2_name (parent, cu);
21735 }
21736 return determine_prefix (parent, cu);
3d567982
TT
21737 case DW_TAG_enumeration_type:
21738 parent_type = read_type_die (parent, cu);
21739 if (TYPE_DECLARED_CLASS (parent_type))
21740 {
7d93a1e0
SM
21741 if (parent_type->name () != NULL)
21742 return parent_type->name ();
3d567982
TT
21743 return "";
21744 }
21745 /* Fall through. */
63d06c5c 21746 default:
8176b9b8 21747 return determine_prefix (parent, cu);
63d06c5c 21748 }
63d06c5c
DC
21749}
21750
3e43a32a
MS
21751/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21752 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21753 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21754 an obconcat, otherwise allocate storage for the result. The CU argument is
21755 used to determine the language and hence, the appropriate separator. */
987504bb 21756
f55ee35c 21757#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21758
21759static char *
f55ee35c
JK
21760typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21761 int physname, struct dwarf2_cu *cu)
63d06c5c 21762{
f55ee35c 21763 const char *lead = "";
5c315b68 21764 const char *sep;
63d06c5c 21765
3e43a32a
MS
21766 if (suffix == NULL || suffix[0] == '\0'
21767 || prefix == NULL || prefix[0] == '\0')
987504bb 21768 sep = "";
45280282
IB
21769 else if (cu->language == language_d)
21770 {
21771 /* For D, the 'main' function could be defined in any module, but it
21772 should never be prefixed. */
21773 if (strcmp (suffix, "D main") == 0)
21774 {
21775 prefix = "";
21776 sep = "";
21777 }
21778 else
21779 sep = ".";
21780 }
f55ee35c
JK
21781 else if (cu->language == language_fortran && physname)
21782 {
21783 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21784 DW_AT_MIPS_linkage_name is preferred and used instead. */
21785
21786 lead = "__";
21787 sep = "_MOD_";
21788 }
987504bb
JJ
21789 else
21790 sep = "::";
63d06c5c 21791
6dd47d34
DE
21792 if (prefix == NULL)
21793 prefix = "";
21794 if (suffix == NULL)
21795 suffix = "";
21796
987504bb
JJ
21797 if (obs == NULL)
21798 {
3e43a32a 21799 char *retval
224c3ddb
SM
21800 = ((char *)
21801 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21802
f55ee35c
JK
21803 strcpy (retval, lead);
21804 strcat (retval, prefix);
6dd47d34
DE
21805 strcat (retval, sep);
21806 strcat (retval, suffix);
63d06c5c
DC
21807 return retval;
21808 }
987504bb
JJ
21809 else
21810 {
21811 /* We have an obstack. */
f55ee35c 21812 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21813 }
63d06c5c
DC
21814}
21815
71c25dea
TT
21816/* Get name of a die, return NULL if not found. */
21817
15d034d0
TT
21818static const char *
21819dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21820 struct objfile *objfile)
71c25dea
TT
21821{
21822 if (name && cu->language == language_cplus)
21823 {
596dc4ad
TT
21824 gdb::unique_xmalloc_ptr<char> canon_name
21825 = cp_canonicalize_string (name);
71c25dea 21826
596dc4ad
TT
21827 if (canon_name != nullptr)
21828 name = objfile->intern (canon_name.get ());
71c25dea
TT
21829 }
21830
21831 return name;
c906108c
SS
21832}
21833
96553a0c
DE
21834/* Get name of a die, return NULL if not found.
21835 Anonymous namespaces are converted to their magic string. */
9219021c 21836
15d034d0 21837static const char *
e142c38c 21838dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21839{
21840 struct attribute *attr;
518817b3 21841 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21842
e142c38c 21843 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21844 if ((!attr || !DW_STRING (attr))
96553a0c 21845 && die->tag != DW_TAG_namespace
53832f31
TT
21846 && die->tag != DW_TAG_class_type
21847 && die->tag != DW_TAG_interface_type
21848 && die->tag != DW_TAG_structure_type
21849 && die->tag != DW_TAG_union_type)
71c25dea
TT
21850 return NULL;
21851
21852 switch (die->tag)
21853 {
21854 case DW_TAG_compile_unit:
95554aad 21855 case DW_TAG_partial_unit:
71c25dea
TT
21856 /* Compilation units have a DW_AT_name that is a filename, not
21857 a source language identifier. */
21858 case DW_TAG_enumeration_type:
21859 case DW_TAG_enumerator:
21860 /* These tags always have simple identifiers already; no need
21861 to canonicalize them. */
21862 return DW_STRING (attr);
907af001 21863
96553a0c
DE
21864 case DW_TAG_namespace:
21865 if (attr != NULL && DW_STRING (attr) != NULL)
21866 return DW_STRING (attr);
21867 return CP_ANONYMOUS_NAMESPACE_STR;
21868
907af001
UW
21869 case DW_TAG_class_type:
21870 case DW_TAG_interface_type:
21871 case DW_TAG_structure_type:
21872 case DW_TAG_union_type:
21873 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21874 structures or unions. These were of the form "._%d" in GCC 4.1,
21875 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21876 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21877 if (attr && DW_STRING (attr)
61012eef
GB
21878 && (startswith (DW_STRING (attr), "._")
21879 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21880 return NULL;
53832f31
TT
21881
21882 /* GCC might emit a nameless typedef that has a linkage name. See
21883 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21884 if (!attr || DW_STRING (attr) == NULL)
21885 {
73b9be8b 21886 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21887 if (attr == NULL || DW_STRING (attr) == NULL)
21888 return NULL;
21889
df5c6c50
JK
21890 /* Avoid demangling DW_STRING (attr) the second time on a second
21891 call for the same DIE. */
21892 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21893 {
43816ebc
TT
21894 gdb::unique_xmalloc_ptr<char> demangled
21895 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21896 if (demangled == nullptr)
21897 return nullptr;
43816ebc 21898
be1e3d3e 21899 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21900 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21901 }
67430cd0
TT
21902
21903 /* Strip any leading namespaces/classes, keep only the base name.
21904 DW_AT_name for named DIEs does not contain the prefixes. */
21905 const char *base = strrchr (DW_STRING (attr), ':');
21906 if (base && base > DW_STRING (attr) && base[-1] == ':')
21907 return &base[1];
21908 else
21909 return DW_STRING (attr);
53832f31 21910 }
907af001
UW
21911 break;
21912
71c25dea 21913 default:
907af001
UW
21914 break;
21915 }
21916
21917 if (!DW_STRING_IS_CANONICAL (attr))
21918 {
be1e3d3e
TT
21919 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21920 objfile);
907af001 21921 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21922 }
907af001 21923 return DW_STRING (attr);
9219021c
DC
21924}
21925
21926/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21927 is none. *EXT_CU is the CU containing DIE on input, and the CU
21928 containing the return value on output. */
9219021c
DC
21929
21930static struct die_info *
f2f0e013 21931dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21932{
21933 struct attribute *attr;
9219021c 21934
f2f0e013 21935 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21936 if (attr == NULL)
21937 return NULL;
21938
f2f0e013 21939 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21940}
21941
f9aca02d 21942static void
d97bc12b 21943dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21944{
21945 unsigned int i;
21946
d97bc12b 21947 print_spaces (indent, f);
9d8780f0 21948 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21949 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21950 sect_offset_str (die->sect_off));
d97bc12b
DE
21951
21952 if (die->parent != NULL)
21953 {
21954 print_spaces (indent, f);
9d8780f0
SM
21955 fprintf_unfiltered (f, " parent at offset: %s\n",
21956 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21957 }
21958
21959 print_spaces (indent, f);
21960 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21961 dwarf_bool_name (die->child != NULL));
c906108c 21962
d97bc12b
DE
21963 print_spaces (indent, f);
21964 fprintf_unfiltered (f, " attributes:\n");
21965
c906108c
SS
21966 for (i = 0; i < die->num_attrs; ++i)
21967 {
d97bc12b
DE
21968 print_spaces (indent, f);
21969 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21970 dwarf_attr_name (die->attrs[i].name),
21971 dwarf_form_name (die->attrs[i].form));
d97bc12b 21972
c906108c
SS
21973 switch (die->attrs[i].form)
21974 {
c906108c 21975 case DW_FORM_addr:
336d760d 21976 case DW_FORM_addrx:
3019eac3 21977 case DW_FORM_GNU_addr_index:
d97bc12b 21978 fprintf_unfiltered (f, "address: ");
5af949e3 21979 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21980 break;
21981 case DW_FORM_block2:
21982 case DW_FORM_block4:
21983 case DW_FORM_block:
21984 case DW_FORM_block1:
56eb65bd
SP
21985 fprintf_unfiltered (f, "block: size %s",
21986 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21987 break;
2dc7f7b3 21988 case DW_FORM_exprloc:
56eb65bd
SP
21989 fprintf_unfiltered (f, "expression: size %s",
21990 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21991 break;
0224619f
JK
21992 case DW_FORM_data16:
21993 fprintf_unfiltered (f, "constant of 16 bytes");
21994 break;
4568ecf9
DE
21995 case DW_FORM_ref_addr:
21996 fprintf_unfiltered (f, "ref address: ");
21997 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21998 break;
36586728
TT
21999 case DW_FORM_GNU_ref_alt:
22000 fprintf_unfiltered (f, "alt ref address: ");
22001 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22002 break;
10b3939b
DJ
22003 case DW_FORM_ref1:
22004 case DW_FORM_ref2:
22005 case DW_FORM_ref4:
4568ecf9
DE
22006 case DW_FORM_ref8:
22007 case DW_FORM_ref_udata:
d97bc12b 22008 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22009 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22010 break;
c906108c
SS
22011 case DW_FORM_data1:
22012 case DW_FORM_data2:
22013 case DW_FORM_data4:
ce5d95e1 22014 case DW_FORM_data8:
c906108c
SS
22015 case DW_FORM_udata:
22016 case DW_FORM_sdata:
43bbcdc2
PH
22017 fprintf_unfiltered (f, "constant: %s",
22018 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22019 break;
2dc7f7b3
TT
22020 case DW_FORM_sec_offset:
22021 fprintf_unfiltered (f, "section offset: %s",
22022 pulongest (DW_UNSND (&die->attrs[i])));
22023 break;
55f1336d 22024 case DW_FORM_ref_sig8:
ac9ec31b
DE
22025 fprintf_unfiltered (f, "signature: %s",
22026 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22027 break;
c906108c 22028 case DW_FORM_string:
4bdf3d34 22029 case DW_FORM_strp:
43988095 22030 case DW_FORM_line_strp:
cf532bd1 22031 case DW_FORM_strx:
3019eac3 22032 case DW_FORM_GNU_str_index:
36586728 22033 case DW_FORM_GNU_strp_alt:
8285870a 22034 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22035 DW_STRING (&die->attrs[i])
8285870a
JK
22036 ? DW_STRING (&die->attrs[i]) : "",
22037 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22038 break;
22039 case DW_FORM_flag:
22040 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22041 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22042 else
d97bc12b 22043 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22044 break;
2dc7f7b3
TT
22045 case DW_FORM_flag_present:
22046 fprintf_unfiltered (f, "flag: TRUE");
22047 break;
a8329558 22048 case DW_FORM_indirect:
0963b4bd
MS
22049 /* The reader will have reduced the indirect form to
22050 the "base form" so this form should not occur. */
5f48f8f3 22051 fprintf_unfiltered (f,
3e43a32a 22052 "unexpected attribute form: DW_FORM_indirect");
a8329558 22053 break;
663c44ac
JK
22054 case DW_FORM_implicit_const:
22055 fprintf_unfiltered (f, "constant: %s",
22056 plongest (DW_SND (&die->attrs[i])));
22057 break;
c906108c 22058 default:
d97bc12b 22059 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22060 die->attrs[i].form);
d97bc12b 22061 break;
c906108c 22062 }
d97bc12b 22063 fprintf_unfiltered (f, "\n");
c906108c
SS
22064 }
22065}
22066
f9aca02d 22067static void
d97bc12b 22068dump_die_for_error (struct die_info *die)
c906108c 22069{
d97bc12b
DE
22070 dump_die_shallow (gdb_stderr, 0, die);
22071}
22072
22073static void
22074dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22075{
22076 int indent = level * 4;
22077
22078 gdb_assert (die != NULL);
22079
22080 if (level >= max_level)
22081 return;
22082
22083 dump_die_shallow (f, indent, die);
22084
22085 if (die->child != NULL)
c906108c 22086 {
d97bc12b
DE
22087 print_spaces (indent, f);
22088 fprintf_unfiltered (f, " Children:");
22089 if (level + 1 < max_level)
22090 {
22091 fprintf_unfiltered (f, "\n");
22092 dump_die_1 (f, level + 1, max_level, die->child);
22093 }
22094 else
22095 {
3e43a32a
MS
22096 fprintf_unfiltered (f,
22097 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22098 }
22099 }
22100
22101 if (die->sibling != NULL && level > 0)
22102 {
22103 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22104 }
22105}
22106
d97bc12b
DE
22107/* This is called from the pdie macro in gdbinit.in.
22108 It's not static so gcc will keep a copy callable from gdb. */
22109
22110void
22111dump_die (struct die_info *die, int max_level)
22112{
22113 dump_die_1 (gdb_stdlog, 0, max_level, die);
22114}
22115
f9aca02d 22116static void
51545339 22117store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22118{
51545339 22119 void **slot;
c906108c 22120
9c541725
PA
22121 slot = htab_find_slot_with_hash (cu->die_hash, die,
22122 to_underlying (die->sect_off),
b64f50a1 22123 INSERT);
51545339
DJ
22124
22125 *slot = die;
c906108c
SS
22126}
22127
348e048f
DE
22128/* Follow reference or signature attribute ATTR of SRC_DIE.
22129 On entry *REF_CU is the CU of SRC_DIE.
22130 On exit *REF_CU is the CU of the result. */
22131
22132static struct die_info *
ff39bb5e 22133follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22134 struct dwarf2_cu **ref_cu)
22135{
22136 struct die_info *die;
22137
cd6c91b4 22138 if (attr->form_is_ref ())
348e048f 22139 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22140 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22141 die = follow_die_sig (src_die, attr, ref_cu);
22142 else
22143 {
22144 dump_die_for_error (src_die);
22145 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22146 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22147 }
22148
22149 return die;
03dd20cc
DJ
22150}
22151
5c631832 22152/* Follow reference OFFSET.
673bfd45
DE
22153 On entry *REF_CU is the CU of the source die referencing OFFSET.
22154 On exit *REF_CU is the CU of the result.
22155 Returns NULL if OFFSET is invalid. */
f504f079 22156
f9aca02d 22157static struct die_info *
9c541725 22158follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22159 struct dwarf2_cu **ref_cu)
c906108c 22160{
10b3939b 22161 struct die_info temp_die;
f2f0e013 22162 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22163 struct dwarf2_per_objfile *dwarf2_per_objfile
22164 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22165
348e048f
DE
22166 gdb_assert (cu->per_cu != NULL);
22167
98bfdba5
PA
22168 target_cu = cu;
22169
3019eac3 22170 if (cu->per_cu->is_debug_types)
348e048f
DE
22171 {
22172 /* .debug_types CUs cannot reference anything outside their CU.
22173 If they need to, they have to reference a signatured type via
55f1336d 22174 DW_FORM_ref_sig8. */
4057dfde 22175 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22176 return NULL;
348e048f 22177 }
36586728 22178 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22179 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22180 {
22181 struct dwarf2_per_cu_data *per_cu;
9a619af0 22182
9c541725 22183 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22184 dwarf2_per_objfile);
03dd20cc
DJ
22185
22186 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22187 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22188 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22189
10b3939b
DJ
22190 target_cu = per_cu->cu;
22191 }
98bfdba5
PA
22192 else if (cu->dies == NULL)
22193 {
22194 /* We're loading full DIEs during partial symbol reading. */
22195 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22196 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22197 }
c906108c 22198
f2f0e013 22199 *ref_cu = target_cu;
9c541725 22200 temp_die.sect_off = sect_off;
c24bdb02
KS
22201
22202 if (target_cu != cu)
22203 target_cu->ancestor = cu;
22204
9a3c8263 22205 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22206 &temp_die,
22207 to_underlying (sect_off));
5c631832 22208}
10b3939b 22209
5c631832
JK
22210/* Follow reference attribute ATTR of SRC_DIE.
22211 On entry *REF_CU is the CU of SRC_DIE.
22212 On exit *REF_CU is the CU of the result. */
22213
22214static struct die_info *
ff39bb5e 22215follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22216 struct dwarf2_cu **ref_cu)
22217{
0826b30a 22218 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22219 struct dwarf2_cu *cu = *ref_cu;
22220 struct die_info *die;
22221
9c541725 22222 die = follow_die_offset (sect_off,
36586728
TT
22223 (attr->form == DW_FORM_GNU_ref_alt
22224 || cu->per_cu->is_dwz),
22225 ref_cu);
5c631832 22226 if (!die)
9d8780f0
SM
22227 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22228 "at %s [in module %s]"),
22229 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22230 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22231
5c631832
JK
22232 return die;
22233}
22234
d4c9a4f8 22235/* See read.h. */
5c631832
JK
22236
22237struct dwarf2_locexpr_baton
9c541725 22238dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22239 dwarf2_per_cu_data *per_cu,
8b9737bf 22240 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22241 void *baton, bool resolve_abstract_p)
5c631832 22242{
918dd910 22243 struct dwarf2_cu *cu;
5c631832
JK
22244 struct die_info *die;
22245 struct attribute *attr;
22246 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22247 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22248 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22249
918dd910 22250 if (per_cu->cu == NULL)
58f0c718 22251 load_cu (per_cu, false);
918dd910 22252 cu = per_cu->cu;
cc12ce38
DE
22253 if (cu == NULL)
22254 {
22255 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22256 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22257 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22258 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22259 }
918dd910 22260
9c541725 22261 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22262 if (!die)
9d8780f0
SM
22263 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22264 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22265
22266 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22267 if (!attr && resolve_abstract_p
3360b6e7 22268 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22269 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22270 {
22271 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22272 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22273 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22274
3360b6e7
TV
22275 for (const auto &cand_off
22276 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22277 {
3360b6e7
TV
22278 struct dwarf2_cu *cand_cu = cu;
22279 struct die_info *cand
22280 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22281 if (!cand
22282 || !cand->parent
e4a62c65
TV
22283 || cand->parent->tag != DW_TAG_subprogram)
22284 continue;
22285
22286 CORE_ADDR pc_low, pc_high;
22287 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22288 if (pc_low == ((CORE_ADDR) -1))
22289 continue;
22290 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22291 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22292 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22293 continue;
22294
22295 die = cand;
22296 attr = dwarf2_attr (die, DW_AT_location, cu);
22297 break;
22298 }
22299 }
22300
5c631832
JK
22301 if (!attr)
22302 {
e103e986
JK
22303 /* DWARF: "If there is no such attribute, then there is no effect.".
22304 DATA is ignored if SIZE is 0. */
5c631832 22305
e103e986 22306 retval.data = NULL;
5c631832
JK
22307 retval.size = 0;
22308 }
cd6c91b4 22309 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22310 {
22311 struct dwarf2_loclist_baton loclist_baton;
22312 CORE_ADDR pc = (*get_frame_pc) (baton);
22313 size_t size;
22314
22315 fill_in_loclist_baton (cu, &loclist_baton, attr);
22316
22317 retval.data = dwarf2_find_location_expression (&loclist_baton,
22318 &size, pc);
22319 retval.size = size;
22320 }
5c631832
JK
22321 else
22322 {
4fc6c0d5 22323 if (!attr->form_is_block ())
9d8780f0 22324 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22325 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22326 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22327
22328 retval.data = DW_BLOCK (attr)->data;
22329 retval.size = DW_BLOCK (attr)->size;
22330 }
22331 retval.per_cu = cu->per_cu;
918dd910 22332
ed2dc618 22333 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22334
5c631832 22335 return retval;
348e048f
DE
22336}
22337
d4c9a4f8 22338/* See read.h. */
8b9737bf
TT
22339
22340struct dwarf2_locexpr_baton
22341dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22342 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22343 CORE_ADDR (*get_frame_pc) (void *baton),
22344 void *baton)
22345{
9c541725 22346 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22347
9c541725 22348 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22349}
22350
b6807d98
TT
22351/* Write a constant of a given type as target-ordered bytes into
22352 OBSTACK. */
22353
22354static const gdb_byte *
22355write_constant_as_bytes (struct obstack *obstack,
22356 enum bfd_endian byte_order,
22357 struct type *type,
22358 ULONGEST value,
22359 LONGEST *len)
22360{
22361 gdb_byte *result;
22362
22363 *len = TYPE_LENGTH (type);
224c3ddb 22364 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22365 store_unsigned_integer (result, *len, byte_order, value);
22366
22367 return result;
22368}
22369
d4c9a4f8 22370/* See read.h. */
b6807d98
TT
22371
22372const gdb_byte *
9c541725 22373dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22374 dwarf2_per_cu_data *per_cu,
22375 obstack *obstack,
b6807d98
TT
22376 LONGEST *len)
22377{
22378 struct dwarf2_cu *cu;
22379 struct die_info *die;
22380 struct attribute *attr;
22381 const gdb_byte *result = NULL;
22382 struct type *type;
22383 LONGEST value;
22384 enum bfd_endian byte_order;
e3b94546 22385 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22386
b6807d98 22387 if (per_cu->cu == NULL)
58f0c718 22388 load_cu (per_cu, false);
b6807d98 22389 cu = per_cu->cu;
cc12ce38
DE
22390 if (cu == NULL)
22391 {
22392 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22393 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22394 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22395 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22396 }
b6807d98 22397
9c541725 22398 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22399 if (!die)
9d8780f0
SM
22400 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22401 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22402
22403 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22404 if (attr == NULL)
22405 return NULL;
22406
e3b94546 22407 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22408 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22409
22410 switch (attr->form)
22411 {
22412 case DW_FORM_addr:
336d760d 22413 case DW_FORM_addrx:
b6807d98
TT
22414 case DW_FORM_GNU_addr_index:
22415 {
22416 gdb_byte *tem;
22417
22418 *len = cu->header.addr_size;
224c3ddb 22419 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22420 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22421 result = tem;
22422 }
22423 break;
22424 case DW_FORM_string:
22425 case DW_FORM_strp:
cf532bd1 22426 case DW_FORM_strx:
b6807d98
TT
22427 case DW_FORM_GNU_str_index:
22428 case DW_FORM_GNU_strp_alt:
22429 /* DW_STRING is already allocated on the objfile obstack, point
22430 directly to it. */
22431 result = (const gdb_byte *) DW_STRING (attr);
22432 *len = strlen (DW_STRING (attr));
22433 break;
22434 case DW_FORM_block1:
22435 case DW_FORM_block2:
22436 case DW_FORM_block4:
22437 case DW_FORM_block:
22438 case DW_FORM_exprloc:
0224619f 22439 case DW_FORM_data16:
b6807d98
TT
22440 result = DW_BLOCK (attr)->data;
22441 *len = DW_BLOCK (attr)->size;
22442 break;
22443
22444 /* The DW_AT_const_value attributes are supposed to carry the
22445 symbol's value "represented as it would be on the target
22446 architecture." By the time we get here, it's already been
22447 converted to host endianness, so we just need to sign- or
22448 zero-extend it as appropriate. */
22449 case DW_FORM_data1:
22450 type = die_type (die, cu);
22451 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22452 if (result == NULL)
22453 result = write_constant_as_bytes (obstack, byte_order,
22454 type, value, len);
22455 break;
22456 case DW_FORM_data2:
22457 type = die_type (die, cu);
22458 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22459 if (result == NULL)
22460 result = write_constant_as_bytes (obstack, byte_order,
22461 type, value, len);
22462 break;
22463 case DW_FORM_data4:
22464 type = die_type (die, cu);
22465 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22466 if (result == NULL)
22467 result = write_constant_as_bytes (obstack, byte_order,
22468 type, value, len);
22469 break;
22470 case DW_FORM_data8:
22471 type = die_type (die, cu);
22472 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22473 if (result == NULL)
22474 result = write_constant_as_bytes (obstack, byte_order,
22475 type, value, len);
22476 break;
22477
22478 case DW_FORM_sdata:
663c44ac 22479 case DW_FORM_implicit_const:
b6807d98
TT
22480 type = die_type (die, cu);
22481 result = write_constant_as_bytes (obstack, byte_order,
22482 type, DW_SND (attr), len);
22483 break;
22484
22485 case DW_FORM_udata:
22486 type = die_type (die, cu);
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, DW_UNSND (attr), len);
22489 break;
22490
22491 default:
b98664d3 22492 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22493 dwarf_form_name (attr->form));
22494 break;
22495 }
22496
22497 return result;
22498}
22499
d4c9a4f8 22500/* See read.h. */
7942e96e
AA
22501
22502struct type *
9c541725 22503dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22504 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22505{
22506 struct dwarf2_cu *cu;
22507 struct die_info *die;
22508
7942e96e 22509 if (per_cu->cu == NULL)
58f0c718 22510 load_cu (per_cu, false);
7942e96e
AA
22511 cu = per_cu->cu;
22512 if (!cu)
22513 return NULL;
22514
9c541725 22515 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22516 if (!die)
22517 return NULL;
22518
22519 return die_type (die, cu);
22520}
22521
8cb5117c 22522/* See read.h. */
8a9b8146
TT
22523
22524struct type *
b64f50a1 22525dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22526 struct dwarf2_per_cu_data *per_cu)
22527{
9c541725 22528 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22529 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22530}
22531
ac9ec31b 22532/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22533 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22534 On exit *REF_CU is the CU of the result.
22535 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22536
22537static struct die_info *
ac9ec31b
DE
22538follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22539 struct dwarf2_cu **ref_cu)
348e048f 22540{
348e048f 22541 struct die_info temp_die;
c24bdb02 22542 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22543 struct die_info *die;
22544
ac9ec31b
DE
22545 /* While it might be nice to assert sig_type->type == NULL here,
22546 we can get here for DW_AT_imported_declaration where we need
22547 the DIE not the type. */
348e048f
DE
22548
22549 /* If necessary, add it to the queue and load its DIEs. */
22550
95554aad 22551 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22552 read_signatured_type (sig_type);
348e048f 22553
348e048f 22554 sig_cu = sig_type->per_cu.cu;
69d751e3 22555 gdb_assert (sig_cu != NULL);
9c541725
PA
22556 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22557 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22558 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22559 to_underlying (temp_die.sect_off));
348e048f
DE
22560 if (die)
22561 {
ed2dc618 22562 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22563 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22564
796a7ff8
DE
22565 /* For .gdb_index version 7 keep track of included TUs.
22566 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22567 if (dwarf2_per_objfile->index_table != NULL
22568 && dwarf2_per_objfile->index_table->version <= 7)
22569 {
ae640021 22570 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22571 }
22572
348e048f 22573 *ref_cu = sig_cu;
c24bdb02
KS
22574 if (sig_cu != cu)
22575 sig_cu->ancestor = cu;
22576
348e048f
DE
22577 return die;
22578 }
22579
ac9ec31b
DE
22580 return NULL;
22581}
22582
22583/* Follow signatured type referenced by ATTR in SRC_DIE.
22584 On entry *REF_CU is the CU of SRC_DIE.
22585 On exit *REF_CU is the CU of the result.
22586 The result is the DIE of the type.
22587 If the referenced type cannot be found an error is thrown. */
22588
22589static struct die_info *
ff39bb5e 22590follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22591 struct dwarf2_cu **ref_cu)
22592{
22593 ULONGEST signature = DW_SIGNATURE (attr);
22594 struct signatured_type *sig_type;
22595 struct die_info *die;
22596
22597 gdb_assert (attr->form == DW_FORM_ref_sig8);
22598
a2ce51a0 22599 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22600 /* sig_type will be NULL if the signatured type is missing from
22601 the debug info. */
22602 if (sig_type == NULL)
22603 {
22604 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22605 " from DIE at %s [in module %s]"),
22606 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22607 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22608 }
22609
22610 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22611 if (die == NULL)
22612 {
22613 dump_die_for_error (src_die);
22614 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22615 " from DIE at %s [in module %s]"),
22616 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22617 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22618 }
22619
22620 return die;
22621}
22622
22623/* Get the type specified by SIGNATURE referenced in DIE/CU,
22624 reading in and processing the type unit if necessary. */
22625
22626static struct type *
22627get_signatured_type (struct die_info *die, ULONGEST signature,
22628 struct dwarf2_cu *cu)
22629{
518817b3
SM
22630 struct dwarf2_per_objfile *dwarf2_per_objfile
22631 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22632 struct signatured_type *sig_type;
22633 struct dwarf2_cu *type_cu;
22634 struct die_info *type_die;
22635 struct type *type;
22636
a2ce51a0 22637 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22638 /* sig_type will be NULL if the signatured type is missing from
22639 the debug info. */
22640 if (sig_type == NULL)
22641 {
b98664d3 22642 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22643 " from DIE at %s [in module %s]"),
22644 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22645 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22646 return build_error_marker_type (cu, die);
22647 }
22648
22649 /* If we already know the type we're done. */
22650 if (sig_type->type != NULL)
22651 return sig_type->type;
22652
22653 type_cu = cu;
22654 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22655 if (type_die != NULL)
22656 {
22657 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22658 is created. This is important, for example, because for c++ classes
22659 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22660 type = read_type_die (type_die, type_cu);
22661 if (type == NULL)
22662 {
b98664d3 22663 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22664 " referenced from DIE at %s [in module %s]"),
22665 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22666 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22667 type = build_error_marker_type (cu, die);
22668 }
22669 }
22670 else
22671 {
b98664d3 22672 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22673 " from DIE at %s [in module %s]"),
22674 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22675 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22676 type = build_error_marker_type (cu, die);
22677 }
22678 sig_type->type = type;
22679
22680 return type;
22681}
22682
22683/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22684 reading in and processing the type unit if necessary. */
22685
22686static struct type *
ff39bb5e 22687get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22688 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22689{
22690 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22691 if (attr->form_is_ref ())
ac9ec31b
DE
22692 {
22693 struct dwarf2_cu *type_cu = cu;
22694 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22695
22696 return read_type_die (type_die, type_cu);
22697 }
22698 else if (attr->form == DW_FORM_ref_sig8)
22699 {
22700 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22701 }
22702 else
22703 {
518817b3
SM
22704 struct dwarf2_per_objfile *dwarf2_per_objfile
22705 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22706
b98664d3 22707 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22708 " at %s [in module %s]"),
22709 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22710 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22711 return build_error_marker_type (cu, die);
22712 }
348e048f
DE
22713}
22714
e5fe5e75 22715/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22716
22717static void
e5fe5e75 22718load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22719{
52dc124a 22720 struct signatured_type *sig_type;
348e048f 22721
f4dc4d17 22722 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22723 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22724
6721b2ec
DE
22725 /* We have the per_cu, but we need the signatured_type.
22726 Fortunately this is an easy translation. */
22727 gdb_assert (per_cu->is_debug_types);
22728 sig_type = (struct signatured_type *) per_cu;
348e048f 22729
6721b2ec 22730 gdb_assert (per_cu->cu == NULL);
348e048f 22731
52dc124a 22732 read_signatured_type (sig_type);
348e048f 22733
6721b2ec 22734 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22735}
22736
3019eac3
DE
22737/* Read in a signatured type and build its CU and DIEs.
22738 If the type is a stub for the real type in a DWO file,
22739 read in the real type from the DWO file as well. */
dee91e82
DE
22740
22741static void
22742read_signatured_type (struct signatured_type *sig_type)
22743{
22744 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22745
3019eac3 22746 gdb_assert (per_cu->is_debug_types);
dee91e82 22747 gdb_assert (per_cu->cu == NULL);
348e048f 22748
6751ebae 22749 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22750
22751 if (!reader.dummy_p)
22752 {
22753 struct dwarf2_cu *cu = reader.cu;
22754 const gdb_byte *info_ptr = reader.info_ptr;
22755
22756 gdb_assert (cu->die_hash == NULL);
22757 cu->die_hash =
22758 htab_create_alloc_ex (cu->header.length / 12,
22759 die_hash,
22760 die_eq,
22761 NULL,
22762 &cu->comp_unit_obstack,
22763 hashtab_obstack_allocate,
22764 dummy_obstack_deallocate);
22765
3e225074 22766 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22767 reader.comp_unit_die->child
22768 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22769 reader.comp_unit_die);
22770 cu->dies = reader.comp_unit_die;
22771 /* comp_unit_die is not stored in die_hash, no need. */
22772
22773 /* We try not to read any attributes in this function, because
22774 not all CUs needed for references have been loaded yet, and
22775 symbol table processing isn't initialized. But we have to
22776 set the CU language, or we won't be able to build types
22777 correctly. Similarly, if we do not read the producer, we can
22778 not apply producer-specific interpretation. */
22779 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22780
22781 reader.keep ();
c0ab21c2
TT
22782 }
22783
7ee85ab1 22784 sig_type->per_cu.tu_read = 1;
c906108c
SS
22785}
22786
c906108c
SS
22787/* Decode simple location descriptions.
22788 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22789 the location and return the value. If COMPUTED is non-null, it is
22790 set to true to indicate that decoding was successful, and false
22791 otherwise. If COMPUTED is null, then this function may emit a
22792 complaint. */
c906108c
SS
22793
22794static CORE_ADDR
7d79de9a 22795decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22796{
518817b3 22797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22798 size_t i;
22799 size_t size = blk->size;
d521ce57 22800 const gdb_byte *data = blk->data;
21ae7a4d
JK
22801 CORE_ADDR stack[64];
22802 int stacki;
22803 unsigned int bytes_read, unsnd;
22804 gdb_byte op;
c906108c 22805
7d79de9a
TT
22806 if (computed != nullptr)
22807 *computed = false;
22808
21ae7a4d
JK
22809 i = 0;
22810 stacki = 0;
22811 stack[stacki] = 0;
22812 stack[++stacki] = 0;
22813
22814 while (i < size)
22815 {
22816 op = data[i++];
22817 switch (op)
22818 {
22819 case DW_OP_lit0:
22820 case DW_OP_lit1:
22821 case DW_OP_lit2:
22822 case DW_OP_lit3:
22823 case DW_OP_lit4:
22824 case DW_OP_lit5:
22825 case DW_OP_lit6:
22826 case DW_OP_lit7:
22827 case DW_OP_lit8:
22828 case DW_OP_lit9:
22829 case DW_OP_lit10:
22830 case DW_OP_lit11:
22831 case DW_OP_lit12:
22832 case DW_OP_lit13:
22833 case DW_OP_lit14:
22834 case DW_OP_lit15:
22835 case DW_OP_lit16:
22836 case DW_OP_lit17:
22837 case DW_OP_lit18:
22838 case DW_OP_lit19:
22839 case DW_OP_lit20:
22840 case DW_OP_lit21:
22841 case DW_OP_lit22:
22842 case DW_OP_lit23:
22843 case DW_OP_lit24:
22844 case DW_OP_lit25:
22845 case DW_OP_lit26:
22846 case DW_OP_lit27:
22847 case DW_OP_lit28:
22848 case DW_OP_lit29:
22849 case DW_OP_lit30:
22850 case DW_OP_lit31:
22851 stack[++stacki] = op - DW_OP_lit0;
22852 break;
f1bea926 22853
21ae7a4d
JK
22854 case DW_OP_reg0:
22855 case DW_OP_reg1:
22856 case DW_OP_reg2:
22857 case DW_OP_reg3:
22858 case DW_OP_reg4:
22859 case DW_OP_reg5:
22860 case DW_OP_reg6:
22861 case DW_OP_reg7:
22862 case DW_OP_reg8:
22863 case DW_OP_reg9:
22864 case DW_OP_reg10:
22865 case DW_OP_reg11:
22866 case DW_OP_reg12:
22867 case DW_OP_reg13:
22868 case DW_OP_reg14:
22869 case DW_OP_reg15:
22870 case DW_OP_reg16:
22871 case DW_OP_reg17:
22872 case DW_OP_reg18:
22873 case DW_OP_reg19:
22874 case DW_OP_reg20:
22875 case DW_OP_reg21:
22876 case DW_OP_reg22:
22877 case DW_OP_reg23:
22878 case DW_OP_reg24:
22879 case DW_OP_reg25:
22880 case DW_OP_reg26:
22881 case DW_OP_reg27:
22882 case DW_OP_reg28:
22883 case DW_OP_reg29:
22884 case DW_OP_reg30:
22885 case DW_OP_reg31:
22886 stack[++stacki] = op - DW_OP_reg0;
22887 if (i < size)
7d79de9a
TT
22888 {
22889 if (computed == nullptr)
22890 dwarf2_complex_location_expr_complaint ();
22891 else
22892 return 0;
22893 }
21ae7a4d 22894 break;
c906108c 22895
21ae7a4d
JK
22896 case DW_OP_regx:
22897 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22898 i += bytes_read;
22899 stack[++stacki] = unsnd;
22900 if (i < size)
7d79de9a
TT
22901 {
22902 if (computed == nullptr)
22903 dwarf2_complex_location_expr_complaint ();
22904 else
22905 return 0;
22906 }
21ae7a4d 22907 break;
c906108c 22908
21ae7a4d 22909 case DW_OP_addr:
c8a7a66f
TT
22910 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22911 &bytes_read);
21ae7a4d
JK
22912 i += bytes_read;
22913 break;
d53d4ac5 22914
21ae7a4d
JK
22915 case DW_OP_const1u:
22916 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22917 i += 1;
22918 break;
22919
22920 case DW_OP_const1s:
22921 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22922 i += 1;
22923 break;
22924
22925 case DW_OP_const2u:
22926 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22927 i += 2;
22928 break;
22929
22930 case DW_OP_const2s:
22931 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22932 i += 2;
22933 break;
d53d4ac5 22934
21ae7a4d
JK
22935 case DW_OP_const4u:
22936 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22937 i += 4;
22938 break;
22939
22940 case DW_OP_const4s:
22941 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22942 i += 4;
22943 break;
22944
585861ea
JK
22945 case DW_OP_const8u:
22946 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22947 i += 8;
22948 break;
22949
21ae7a4d
JK
22950 case DW_OP_constu:
22951 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22952 &bytes_read);
22953 i += bytes_read;
22954 break;
22955
22956 case DW_OP_consts:
22957 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22958 i += bytes_read;
22959 break;
22960
22961 case DW_OP_dup:
22962 stack[stacki + 1] = stack[stacki];
22963 stacki++;
22964 break;
22965
22966 case DW_OP_plus:
22967 stack[stacki - 1] += stack[stacki];
22968 stacki--;
22969 break;
22970
22971 case DW_OP_plus_uconst:
22972 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22973 &bytes_read);
22974 i += bytes_read;
22975 break;
22976
22977 case DW_OP_minus:
22978 stack[stacki - 1] -= stack[stacki];
22979 stacki--;
22980 break;
22981
22982 case DW_OP_deref:
22983 /* If we're not the last op, then we definitely can't encode
22984 this using GDB's address_class enum. This is valid for partial
22985 global symbols, although the variable's address will be bogus
22986 in the psymtab. */
22987 if (i < size)
7d79de9a
TT
22988 {
22989 if (computed == nullptr)
22990 dwarf2_complex_location_expr_complaint ();
22991 else
22992 return 0;
22993 }
21ae7a4d
JK
22994 break;
22995
22996 case DW_OP_GNU_push_tls_address:
4aa4e28b 22997 case DW_OP_form_tls_address:
21ae7a4d
JK
22998 /* The top of the stack has the offset from the beginning
22999 of the thread control block at which the variable is located. */
23000 /* Nothing should follow this operator, so the top of stack would
23001 be returned. */
23002 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23003 address will be bogus in the psymtab. Make it always at least
23004 non-zero to not look as a variable garbage collected by linker
23005 which have DW_OP_addr 0. */
21ae7a4d 23006 if (i < size)
7d79de9a
TT
23007 {
23008 if (computed == nullptr)
23009 dwarf2_complex_location_expr_complaint ();
23010 else
23011 return 0;
23012 }
585861ea 23013 stack[stacki]++;
21ae7a4d
JK
23014 break;
23015
23016 case DW_OP_GNU_uninit:
7d79de9a
TT
23017 if (computed != nullptr)
23018 return 0;
21ae7a4d
JK
23019 break;
23020
336d760d 23021 case DW_OP_addrx:
3019eac3 23022 case DW_OP_GNU_addr_index:
49f6c839 23023 case DW_OP_GNU_const_index:
3019eac3
DE
23024 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23025 &bytes_read);
23026 i += bytes_read;
23027 break;
23028
21ae7a4d 23029 default:
7d79de9a
TT
23030 if (computed == nullptr)
23031 {
23032 const char *name = get_DW_OP_name (op);
21ae7a4d 23033
7d79de9a
TT
23034 if (name)
23035 complaint (_("unsupported stack op: '%s'"),
23036 name);
23037 else
23038 complaint (_("unsupported stack op: '%02x'"),
23039 op);
23040 }
21ae7a4d
JK
23041
23042 return (stack[stacki]);
d53d4ac5 23043 }
3c6e0cb3 23044
21ae7a4d
JK
23045 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23046 outside of the allocated space. Also enforce minimum>0. */
23047 if (stacki >= ARRAY_SIZE (stack) - 1)
23048 {
7d79de9a
TT
23049 if (computed == nullptr)
23050 complaint (_("location description stack overflow"));
21ae7a4d
JK
23051 return 0;
23052 }
23053
23054 if (stacki <= 0)
23055 {
7d79de9a
TT
23056 if (computed == nullptr)
23057 complaint (_("location description stack underflow"));
21ae7a4d
JK
23058 return 0;
23059 }
23060 }
7d79de9a
TT
23061
23062 if (computed != nullptr)
23063 *computed = true;
21ae7a4d 23064 return (stack[stacki]);
c906108c
SS
23065}
23066
23067/* memory allocation interface */
23068
c906108c 23069static struct dwarf_block *
7b5a2f43 23070dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23071{
8d749320 23072 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23073}
23074
c906108c 23075static struct die_info *
b60c80d6 23076dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23077{
23078 struct die_info *die;
b60c80d6
DJ
23079 size_t size = sizeof (struct die_info);
23080
23081 if (num_attrs > 1)
23082 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23083
b60c80d6 23084 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23085 memset (die, 0, sizeof (struct die_info));
23086 return (die);
23087}
2e276125
JB
23088
23089\f
a036ba48 23090
c90ec28a 23091/* Macro support. */
cf2c3c16 23092
9eac9650
TT
23093/* An overload of dwarf_decode_macros that finds the correct section
23094 and ensures it is read in before calling the other overload. */
23095
23096static void
23097dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23098 int section_is_gnu)
23099{
23100 struct dwarf2_per_objfile *dwarf2_per_objfile
23101 = cu->per_cu->dwarf2_per_objfile;
23102 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23103 const struct line_header *lh = cu->line_header;
9eac9650
TT
23104 unsigned int offset_size = cu->header.offset_size;
23105 struct dwarf2_section_info *section;
23106 const char *section_name;
23107
23108 if (cu->dwo_unit != nullptr)
23109 {
23110 if (section_is_gnu)
23111 {
23112 section = &cu->dwo_unit->dwo_file->sections.macro;
23113 section_name = ".debug_macro.dwo";
23114 }
23115 else
23116 {
23117 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23118 section_name = ".debug_macinfo.dwo";
23119 }
23120 }
23121 else
23122 {
23123 if (section_is_gnu)
23124 {
23125 section = &dwarf2_per_objfile->macro;
23126 section_name = ".debug_macro";
23127 }
23128 else
23129 {
23130 section = &dwarf2_per_objfile->macinfo;
23131 section_name = ".debug_macinfo";
23132 }
23133 }
23134
23135 section->read (objfile);
23136 if (section->buffer == nullptr)
23137 {
23138 complaint (_("missing %s section"), section_name);
23139 return;
23140 }
23141
23142 buildsym_compunit *builder = cu->get_builder ();
23143
23144 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23145 offset_size, offset, section_is_gnu);
23146}
23147
3019eac3
DE
23148/* Return the .debug_loc section to use for CU.
23149 For DWO files use .debug_loc.dwo. */
23150
23151static struct dwarf2_section_info *
23152cu_debug_loc_section (struct dwarf2_cu *cu)
23153{
518817b3
SM
23154 struct dwarf2_per_objfile *dwarf2_per_objfile
23155 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23156
3019eac3 23157 if (cu->dwo_unit)
43988095
JK
23158 {
23159 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23160
43988095
JK
23161 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23162 }
23163 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23164 : &dwarf2_per_objfile->loc);
3019eac3
DE
23165}
23166
8cf6f0b1
TT
23167/* A helper function that fills in a dwarf2_loclist_baton. */
23168
23169static void
23170fill_in_loclist_baton (struct dwarf2_cu *cu,
23171 struct dwarf2_loclist_baton *baton,
ff39bb5e 23172 const struct attribute *attr)
8cf6f0b1 23173{
518817b3
SM
23174 struct dwarf2_per_objfile *dwarf2_per_objfile
23175 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23176 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23177
96b79293 23178 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23179
23180 baton->per_cu = cu->per_cu;
23181 gdb_assert (baton->per_cu);
23182 /* We don't know how long the location list is, but make sure we
23183 don't run off the edge of the section. */
3019eac3
DE
23184 baton->size = section->size - DW_UNSND (attr);
23185 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23186 if (cu->base_address.has_value ())
23187 baton->base_address = *cu->base_address;
23188 else
23189 baton->base_address = 0;
f664829e 23190 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23191}
23192
4c2df51b 23193static void
ff39bb5e 23194dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23195 struct dwarf2_cu *cu, int is_block)
4c2df51b 23196{
518817b3
SM
23197 struct dwarf2_per_objfile *dwarf2_per_objfile
23198 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23199 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23200 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23201
cd6c91b4 23202 if (attr->form_is_section_offset ()
3019eac3 23203 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23204 the section. If so, fall through to the complaint in the
23205 other branch. */
2c7d5afc 23206 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23207 {
0d53c4c4 23208 struct dwarf2_loclist_baton *baton;
4c2df51b 23209
8d749320 23210 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23211
8cf6f0b1 23212 fill_in_loclist_baton (cu, baton, attr);
be391dca 23213
2b24b6e4 23214 if (!cu->base_address.has_value ())
b98664d3 23215 complaint (_("Location list used without "
3e43a32a 23216 "specifying the CU base address."));
4c2df51b 23217
f1e6e072
TT
23218 SYMBOL_ACLASS_INDEX (sym) = (is_block
23219 ? dwarf2_loclist_block_index
23220 : dwarf2_loclist_index);
0d53c4c4
DJ
23221 SYMBOL_LOCATION_BATON (sym) = baton;
23222 }
23223 else
23224 {
23225 struct dwarf2_locexpr_baton *baton;
23226
8d749320 23227 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23228 baton->per_cu = cu->per_cu;
23229 gdb_assert (baton->per_cu);
0d53c4c4 23230
4fc6c0d5 23231 if (attr->form_is_block ())
0d53c4c4
DJ
23232 {
23233 /* Note that we're just copying the block's data pointer
23234 here, not the actual data. We're still pointing into the
6502dd73
DJ
23235 info_buffer for SYM's objfile; right now we never release
23236 that buffer, but when we do clean up properly this may
23237 need to change. */
0d53c4c4
DJ
23238 baton->size = DW_BLOCK (attr)->size;
23239 baton->data = DW_BLOCK (attr)->data;
23240 }
23241 else
23242 {
23243 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23244 sym->natural_name ());
0d53c4c4 23245 baton->size = 0;
0d53c4c4 23246 }
6e70227d 23247
f1e6e072
TT
23248 SYMBOL_ACLASS_INDEX (sym) = (is_block
23249 ? dwarf2_locexpr_block_index
23250 : dwarf2_locexpr_index);
0d53c4c4
DJ
23251 SYMBOL_LOCATION_BATON (sym) = baton;
23252 }
4c2df51b 23253}
6502dd73 23254
09ba997f 23255/* See read.h. */
ae0d2f24
UW
23256
23257struct objfile *
09ba997f 23258dwarf2_per_cu_data::objfile () const
ae0d2f24 23259{
09ba997f 23260 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23261
23262 /* Return the master objfile, so that we can report and look up the
23263 correct file containing this variable. */
23264 if (objfile->separate_debug_objfile_backlink)
23265 objfile = objfile->separate_debug_objfile_backlink;
23266
23267 return objfile;
23268}
23269
96408a79
SA
23270/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23271 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23272 CU_HEADERP first. */
23273
23274static const struct comp_unit_head *
23275per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23276 const struct dwarf2_per_cu_data *per_cu)
96408a79 23277{
d521ce57 23278 const gdb_byte *info_ptr;
96408a79
SA
23279
23280 if (per_cu->cu)
23281 return &per_cu->cu->header;
23282
9c541725 23283 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23284
23285 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23286 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23287 rcuh_kind::COMPILE);
96408a79
SA
23288
23289 return cu_headerp;
23290}
23291
09ba997f 23292/* See read.h. */
ae0d2f24 23293
98714339 23294int
09ba997f 23295dwarf2_per_cu_data::addr_size () const
ae0d2f24 23296{
96408a79
SA
23297 struct comp_unit_head cu_header_local;
23298 const struct comp_unit_head *cu_headerp;
c471e790 23299
09ba997f 23300 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23301
23302 return cu_headerp->addr_size;
ae0d2f24
UW
23303}
23304
09ba997f 23305/* See read.h. */
9eae7c52
TT
23306
23307int
09ba997f 23308dwarf2_per_cu_data::offset_size () const
9eae7c52 23309{
96408a79
SA
23310 struct comp_unit_head cu_header_local;
23311 const struct comp_unit_head *cu_headerp;
9c6c53f7 23312
09ba997f 23313 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23314
23315 return cu_headerp->offset_size;
23316}
23317
09ba997f 23318/* See read.h. */
96408a79
SA
23319
23320int
09ba997f 23321dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23322{
23323 struct comp_unit_head cu_header_local;
23324 const struct comp_unit_head *cu_headerp;
23325
09ba997f 23326 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23327
23328 if (cu_headerp->version == 2)
23329 return cu_headerp->addr_size;
23330 else
23331 return cu_headerp->offset_size;
181cebd4
JK
23332}
23333
09ba997f 23334/* See read.h. */
9aa1f1e3
TT
23335
23336CORE_ADDR
09ba997f 23337dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23338{
09ba997f
TT
23339 struct objfile *objfile = dwarf2_per_objfile->objfile;
23340
23341 return objfile->text_section_offset ();
9aa1f1e3
TT
23342}
23343
09ba997f
TT
23344/* See read.h. */
23345
23346struct type *
23347dwarf2_per_cu_data::addr_type () const
9a49df9d 23348{
09ba997f 23349 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23350 struct type *void_type = objfile_type (objfile)->builtin_void;
23351 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23352 int addr_size = this->addr_size ();
9a49df9d
AB
23353
23354 if (TYPE_LENGTH (addr_type) == addr_size)
23355 return addr_type;
23356
09ba997f 23357 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23358 return addr_type;
23359}
23360
22b6cd70
TT
23361/* A helper function for dwarf2_find_containing_comp_unit that returns
23362 the index of the result, and that searches a vector. It will
23363 return a result even if the offset in question does not actually
23364 occur in any CU. This is separate so that it can be unit
23365 tested. */
ae038cb0 23366
22b6cd70
TT
23367static int
23368dwarf2_find_containing_comp_unit
23369 (sect_offset sect_off,
23370 unsigned int offset_in_dwz,
23371 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23372{
ae038cb0
DJ
23373 int low, high;
23374
ae038cb0 23375 low = 0;
22b6cd70 23376 high = all_comp_units.size () - 1;
ae038cb0
DJ
23377 while (high > low)
23378 {
36586728 23379 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23380 int mid = low + (high - low) / 2;
9a619af0 23381
22b6cd70 23382 mid_cu = all_comp_units[mid];
36586728 23383 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23384 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23385 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23386 high = mid;
23387 else
23388 low = mid + 1;
23389 }
23390 gdb_assert (low == high);
22b6cd70
TT
23391 return low;
23392}
23393
23394/* Locate the .debug_info compilation unit from CU's objfile which contains
23395 the DIE at OFFSET. Raises an error on failure. */
23396
23397static struct dwarf2_per_cu_data *
23398dwarf2_find_containing_comp_unit (sect_offset sect_off,
23399 unsigned int offset_in_dwz,
23400 struct dwarf2_per_objfile *dwarf2_per_objfile)
23401{
23402 int low
23403 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23404 dwarf2_per_objfile->all_comp_units);
23405 struct dwarf2_per_cu_data *this_cu
23406 = dwarf2_per_objfile->all_comp_units[low];
23407
45b8ae0c 23408 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23409 {
36586728 23410 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23411 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23412 "offset %s [in module %s]"),
23413 sect_offset_str (sect_off),
ed2dc618 23414 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23415
9c541725
PA
23416 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23417 <= sect_off);
ae038cb0
DJ
23418 return dwarf2_per_objfile->all_comp_units[low-1];
23419 }
23420 else
23421 {
b76e467d 23422 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23423 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23424 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23425 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23426 return this_cu;
23427 }
23428}
23429
22b6cd70
TT
23430#if GDB_SELF_TEST
23431
23432namespace selftests {
23433namespace find_containing_comp_unit {
23434
23435static void
23436run_test ()
23437{
23438 struct dwarf2_per_cu_data one {};
23439 struct dwarf2_per_cu_data two {};
23440 struct dwarf2_per_cu_data three {};
23441 struct dwarf2_per_cu_data four {};
23442
23443 one.length = 5;
23444 two.sect_off = sect_offset (one.length);
23445 two.length = 7;
23446
23447 three.length = 5;
23448 three.is_dwz = 1;
23449 four.sect_off = sect_offset (three.length);
23450 four.length = 7;
23451 four.is_dwz = 1;
23452
23453 std::vector<dwarf2_per_cu_data *> units;
23454 units.push_back (&one);
23455 units.push_back (&two);
23456 units.push_back (&three);
23457 units.push_back (&four);
23458
23459 int result;
23460
23461 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23462 SELF_CHECK (units[result] == &one);
23463 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23464 SELF_CHECK (units[result] == &one);
23465 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23466 SELF_CHECK (units[result] == &two);
23467
23468 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23469 SELF_CHECK (units[result] == &three);
23470 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23471 SELF_CHECK (units[result] == &three);
23472 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23473 SELF_CHECK (units[result] == &four);
23474}
23475
23476}
23477}
23478
23479#endif /* GDB_SELF_TEST */
23480
23745b47 23481/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23482
fcd3b13d
SM
23483dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23484 : per_cu (per_cu_),
9068261f
AB
23485 mark (false),
23486 has_loclist (false),
23487 checked_producer (false),
23488 producer_is_gxx_lt_4_6 (false),
23489 producer_is_gcc_lt_4_3 (false),
eb77c9df 23490 producer_is_icc (false),
9068261f 23491 producer_is_icc_lt_14 (false),
c258c396 23492 producer_is_codewarrior (false),
9068261f 23493 processing_has_namespace_info (false)
93311388 23494{
fcd3b13d
SM
23495 per_cu->cu = this;
23496}
23497
23498/* Destroy a dwarf2_cu. */
23499
23500dwarf2_cu::~dwarf2_cu ()
23501{
23502 per_cu->cu = NULL;
9816fde3
JK
23503}
23504
23505/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23506
23507static void
95554aad
TT
23508prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23509 enum language pretend_language)
9816fde3
JK
23510{
23511 struct attribute *attr;
23512
23513 /* Set the language we're debugging. */
23514 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23515 if (attr != nullptr)
9816fde3
JK
23516 set_cu_language (DW_UNSND (attr), cu);
23517 else
9cded63f 23518 {
95554aad 23519 cu->language = pretend_language;
9cded63f
TT
23520 cu->language_defn = language_def (cu->language);
23521 }
dee91e82 23522
7d45c7c3 23523 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23524}
23525
ae038cb0
DJ
23526/* Increase the age counter on each cached compilation unit, and free
23527 any that are too old. */
23528
23529static void
ed2dc618 23530age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23531{
23532 struct dwarf2_per_cu_data *per_cu, **last_chain;
23533
23534 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23535 per_cu = dwarf2_per_objfile->read_in_chain;
23536 while (per_cu != NULL)
23537 {
23538 per_cu->cu->last_used ++;
b4f54984 23539 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23540 dwarf2_mark (per_cu->cu);
23541 per_cu = per_cu->cu->read_in_chain;
23542 }
23543
23544 per_cu = dwarf2_per_objfile->read_in_chain;
23545 last_chain = &dwarf2_per_objfile->read_in_chain;
23546 while (per_cu != NULL)
23547 {
23548 struct dwarf2_per_cu_data *next_cu;
23549
23550 next_cu = per_cu->cu->read_in_chain;
23551
23552 if (!per_cu->cu->mark)
23553 {
fcd3b13d 23554 delete per_cu->cu;
ae038cb0
DJ
23555 *last_chain = next_cu;
23556 }
23557 else
23558 last_chain = &per_cu->cu->read_in_chain;
23559
23560 per_cu = next_cu;
23561 }
23562}
23563
23564/* Remove a single compilation unit from the cache. */
23565
23566static void
dee91e82 23567free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23568{
23569 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23570 struct dwarf2_per_objfile *dwarf2_per_objfile
23571 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23572
23573 per_cu = dwarf2_per_objfile->read_in_chain;
23574 last_chain = &dwarf2_per_objfile->read_in_chain;
23575 while (per_cu != NULL)
23576 {
23577 struct dwarf2_per_cu_data *next_cu;
23578
23579 next_cu = per_cu->cu->read_in_chain;
23580
dee91e82 23581 if (per_cu == target_per_cu)
ae038cb0 23582 {
fcd3b13d 23583 delete per_cu->cu;
dee91e82 23584 per_cu->cu = NULL;
ae038cb0
DJ
23585 *last_chain = next_cu;
23586 break;
23587 }
23588 else
23589 last_chain = &per_cu->cu->read_in_chain;
23590
23591 per_cu = next_cu;
23592 }
23593}
23594
dee91e82
DE
23595/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23596 We store these in a hash table separate from the DIEs, and preserve them
23597 when the DIEs are flushed out of cache.
23598
23599 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23600 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23601 or the type may come from a DWO file. Furthermore, while it's more logical
23602 to use per_cu->section+offset, with Fission the section with the data is in
23603 the DWO file but we don't know that section at the point we need it.
23604 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23605 because we can enter the lookup routine, get_die_type_at_offset, from
23606 outside this file, and thus won't necessarily have PER_CU->cu.
23607 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23608
dee91e82 23609struct dwarf2_per_cu_offset_and_type
1c379e20 23610{
dee91e82 23611 const struct dwarf2_per_cu_data *per_cu;
9c541725 23612 sect_offset sect_off;
1c379e20
DJ
23613 struct type *type;
23614};
23615
dee91e82 23616/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23617
23618static hashval_t
dee91e82 23619per_cu_offset_and_type_hash (const void *item)
1c379e20 23620{
9a3c8263
SM
23621 const struct dwarf2_per_cu_offset_and_type *ofs
23622 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23623
9c541725 23624 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23625}
23626
dee91e82 23627/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23628
23629static int
dee91e82 23630per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23631{
9a3c8263
SM
23632 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23633 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23634 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23635 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23636
dee91e82 23637 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23638 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23639}
23640
23641/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23642 table if necessary. For convenience, return TYPE.
23643
23644 The DIEs reading must have careful ordering to:
85102364 23645 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23646 reading current DIE.
23647 * Not trying to dereference contents of still incompletely read in types
23648 while reading in other DIEs.
23649 * Enable referencing still incompletely read in types just by a pointer to
23650 the type without accessing its fields.
23651
23652 Therefore caller should follow these rules:
23653 * Try to fetch any prerequisite types we may need to build this DIE type
23654 before building the type and calling set_die_type.
e71ec853 23655 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23656 possible before fetching more types to complete the current type.
23657 * Make the type as complete as possible before fetching more types. */
1c379e20 23658
f792889a 23659static struct type *
1c379e20
DJ
23660set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23661{
518817b3
SM
23662 struct dwarf2_per_objfile *dwarf2_per_objfile
23663 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23664 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23665 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23666 struct attribute *attr;
23667 struct dynamic_prop prop;
1c379e20 23668
b4ba55a1
JB
23669 /* For Ada types, make sure that the gnat-specific data is always
23670 initialized (if not already set). There are a few types where
23671 we should not be doing so, because the type-specific area is
23672 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23673 where the type-specific area is used to store the floatformat).
23674 But this is not a problem, because the gnat-specific information
23675 is actually not needed for these types. */
23676 if (need_gnat_info (cu)
78134374
SM
23677 && type->code () != TYPE_CODE_FUNC
23678 && type->code () != TYPE_CODE_FLT
23679 && type->code () != TYPE_CODE_METHODPTR
23680 && type->code () != TYPE_CODE_MEMBERPTR
23681 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23682 && !HAVE_GNAT_AUX_INFO (type))
23683 INIT_GNAT_SPECIFIC (type);
23684
3f2f83dd
KB
23685 /* Read DW_AT_allocated and set in type. */
23686 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23687 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23688 {
09ba997f 23689 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23690 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23691 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23692 }
23693 else if (attr != NULL)
23694 {
b98664d3 23695 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23696 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23697 sect_offset_str (die->sect_off));
3f2f83dd
KB
23698 }
23699
23700 /* Read DW_AT_associated and set in type. */
23701 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23702 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23703 {
09ba997f 23704 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23705 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23706 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23707 }
23708 else if (attr != NULL)
23709 {
b98664d3 23710 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23711 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23712 sect_offset_str (die->sect_off));
3f2f83dd
KB
23713 }
23714
3cdcd0ce
JB
23715 /* Read DW_AT_data_location and set in type. */
23716 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23717 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23718 cu->per_cu->addr_type ()))
5c54719c 23719 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23720
dee91e82 23721 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23722 dwarf2_per_objfile->die_type_hash
23723 = htab_up (htab_create_alloc (127,
23724 per_cu_offset_and_type_hash,
23725 per_cu_offset_and_type_eq,
23726 NULL, xcalloc, xfree));
1c379e20 23727
dee91e82 23728 ofs.per_cu = cu->per_cu;
9c541725 23729 ofs.sect_off = die->sect_off;
1c379e20 23730 ofs.type = type;
dee91e82 23731 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23732 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23733 if (*slot)
b98664d3 23734 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23735 sect_offset_str (die->sect_off));
8d749320
SM
23736 *slot = XOBNEW (&objfile->objfile_obstack,
23737 struct dwarf2_per_cu_offset_and_type);
1c379e20 23738 **slot = ofs;
f792889a 23739 return type;
1c379e20
DJ
23740}
23741
9c541725 23742/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23743 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23744
23745static struct type *
9c541725 23746get_die_type_at_offset (sect_offset sect_off,
673bfd45 23747 struct dwarf2_per_cu_data *per_cu)
1c379e20 23748{
dee91e82 23749 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23750 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23751
dee91e82 23752 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23753 return NULL;
1c379e20 23754
dee91e82 23755 ofs.per_cu = per_cu;
9c541725 23756 ofs.sect_off = sect_off;
9a3c8263 23757 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23758 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23759 if (slot)
23760 return slot->type;
23761 else
23762 return NULL;
23763}
23764
02142a6c 23765/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23766 or return NULL if DIE does not have a saved type. */
23767
23768static struct type *
23769get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23770{
9c541725 23771 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23772}
23773
10b3939b
DJ
23774/* Add a dependence relationship from CU to REF_PER_CU. */
23775
23776static void
23777dwarf2_add_dependence (struct dwarf2_cu *cu,
23778 struct dwarf2_per_cu_data *ref_per_cu)
23779{
23780 void **slot;
23781
23782 if (cu->dependencies == NULL)
23783 cu->dependencies
23784 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23785 NULL, &cu->comp_unit_obstack,
23786 hashtab_obstack_allocate,
23787 dummy_obstack_deallocate);
23788
23789 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23790 if (*slot == NULL)
23791 *slot = ref_per_cu;
23792}
1c379e20 23793
f504f079
DE
23794/* Subroutine of dwarf2_mark to pass to htab_traverse.
23795 Set the mark field in every compilation unit in the
ae038cb0
DJ
23796 cache that we must keep because we are keeping CU. */
23797
10b3939b
DJ
23798static int
23799dwarf2_mark_helper (void **slot, void *data)
23800{
23801 struct dwarf2_per_cu_data *per_cu;
23802
23803 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23804
23805 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23806 reading of the chain. As such dependencies remain valid it is not much
23807 useful to track and undo them during QUIT cleanups. */
23808 if (per_cu->cu == NULL)
23809 return 1;
23810
10b3939b
DJ
23811 if (per_cu->cu->mark)
23812 return 1;
9068261f 23813 per_cu->cu->mark = true;
10b3939b
DJ
23814
23815 if (per_cu->cu->dependencies != NULL)
23816 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23817
23818 return 1;
23819}
23820
f504f079
DE
23821/* Set the mark field in CU and in every other compilation unit in the
23822 cache that we must keep because we are keeping CU. */
23823
ae038cb0
DJ
23824static void
23825dwarf2_mark (struct dwarf2_cu *cu)
23826{
23827 if (cu->mark)
23828 return;
9068261f 23829 cu->mark = true;
10b3939b
DJ
23830 if (cu->dependencies != NULL)
23831 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23832}
23833
23834static void
23835dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23836{
23837 while (per_cu)
23838 {
9068261f 23839 per_cu->cu->mark = false;
ae038cb0
DJ
23840 per_cu = per_cu->cu->read_in_chain;
23841 }
72bf9492
DJ
23842}
23843
72bf9492
DJ
23844/* Trivial hash function for partial_die_info: the hash value of a DIE
23845 is its offset in .debug_info for this objfile. */
23846
23847static hashval_t
23848partial_die_hash (const void *item)
23849{
9a3c8263
SM
23850 const struct partial_die_info *part_die
23851 = (const struct partial_die_info *) item;
9a619af0 23852
9c541725 23853 return to_underlying (part_die->sect_off);
72bf9492
DJ
23854}
23855
23856/* Trivial comparison function for partial_die_info structures: two DIEs
23857 are equal if they have the same offset. */
23858
23859static int
23860partial_die_eq (const void *item_lhs, const void *item_rhs)
23861{
9a3c8263
SM
23862 const struct partial_die_info *part_die_lhs
23863 = (const struct partial_die_info *) item_lhs;
23864 const struct partial_die_info *part_die_rhs
23865 = (const struct partial_die_info *) item_rhs;
9a619af0 23866
9c541725 23867 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23868}
23869
3c3bb058
AB
23870struct cmd_list_element *set_dwarf_cmdlist;
23871struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23872
9291a0cd 23873static void
cd4fb1b2
SM
23874show_check_physname (struct ui_file *file, int from_tty,
23875 struct cmd_list_element *c, const char *value)
9291a0cd 23876{
cd4fb1b2
SM
23877 fprintf_filtered (file,
23878 _("Whether to check \"physname\" is %s.\n"),
23879 value);
9291a0cd
TT
23880}
23881
6c265988 23882void _initialize_dwarf2_read ();
cd4fb1b2 23883void
6c265988 23884_initialize_dwarf2_read ()
9291a0cd 23885{
0743fc83 23886 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23887Set DWARF specific variables.\n\
590042fc 23888Configure DWARF variables such as the cache size."),
0743fc83
TT
23889 &set_dwarf_cmdlist, "maintenance set dwarf ",
23890 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23891
0743fc83 23892 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23893Show DWARF specific variables.\n\
23894Show DWARF variables such as the cache size."),
0743fc83
TT
23895 &show_dwarf_cmdlist, "maintenance show dwarf ",
23896 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23897
cd4fb1b2
SM
23898 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23899 &dwarf_max_cache_age, _("\
23900Set the upper bound on the age of cached DWARF compilation units."), _("\
23901Show the upper bound on the age of cached DWARF compilation units."), _("\
23902A higher limit means that cached compilation units will be stored\n\
23903in memory longer, and more total memory will be used. Zero disables\n\
23904caching, which can slow down startup."),
23905 NULL,
23906 show_dwarf_max_cache_age,
23907 &set_dwarf_cmdlist,
23908 &show_dwarf_cmdlist);
156942c7 23909
cd4fb1b2
SM
23910 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23911Set debugging of the DWARF reader."), _("\
23912Show debugging of the DWARF reader."), _("\
23913When enabled (non-zero), debugging messages are printed during DWARF\n\
23914reading and symtab expansion. A value of 1 (one) provides basic\n\
23915information. A value greater than 1 provides more verbose information."),
23916 NULL,
23917 NULL,
23918 &setdebuglist, &showdebuglist);
9291a0cd 23919
cd4fb1b2
SM
23920 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23921Set debugging of the DWARF DIE reader."), _("\
23922Show debugging of the DWARF DIE reader."), _("\
23923When enabled (non-zero), DIEs are dumped after they are read in.\n\
23924The value is the maximum depth to print."),
23925 NULL,
23926 NULL,
23927 &setdebuglist, &showdebuglist);
9291a0cd 23928
cd4fb1b2
SM
23929 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23930Set debugging of the dwarf line reader."), _("\
23931Show debugging of the dwarf line reader."), _("\
23932When enabled (non-zero), line number entries are dumped as they are read in.\n\
23933A value of 1 (one) provides basic information.\n\
23934A value greater than 1 provides more verbose information."),
23935 NULL,
23936 NULL,
23937 &setdebuglist, &showdebuglist);
437afbb8 23938
cd4fb1b2
SM
23939 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23940Set cross-checking of \"physname\" code against demangler."), _("\
23941Show cross-checking of \"physname\" code against demangler."), _("\
23942When enabled, GDB's internal \"physname\" code is checked against\n\
23943the demangler."),
23944 NULL, show_check_physname,
23945 &setdebuglist, &showdebuglist);
900e11f9 23946
e615022a
DE
23947 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23948 no_class, &use_deprecated_index_sections, _("\
23949Set whether to use deprecated gdb_index sections."), _("\
23950Show whether to use deprecated gdb_index sections."), _("\
23951When enabled, deprecated .gdb_index sections are used anyway.\n\
23952Normally they are ignored either because of a missing feature or\n\
23953performance issue.\n\
23954Warning: This option must be enabled before gdb reads the file."),
23955 NULL,
23956 NULL,
23957 &setlist, &showlist);
23958
f1e6e072
TT
23959 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23960 &dwarf2_locexpr_funcs);
23961 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23962 &dwarf2_loclist_funcs);
23963
23964 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23965 &dwarf2_block_frame_base_locexpr_funcs);
23966 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23967 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23968
23969#if GDB_SELF_TEST
23970 selftests::register_test ("dw2_expand_symtabs_matching",
23971 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23972 selftests::register_test ("dwarf2_find_containing_comp_unit",
23973 selftests::find_containing_comp_unit::run_test);
c62446b1 23974#endif
6502dd73 23975}
This page took 5.453414 seconds and 4 git commands to generate.