Move die_type_hash to dwarf2_per_objfile
[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 486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 487 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
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
5989a64e 602/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
f4dc4d17
DE
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;
5989a64e 1605 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 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
5989a64e
SM
1748dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1749 bool can_copy_)
1750 : can_copy (can_copy_)
330cdd98
PA
1751{
1752 if (names == NULL)
1753 names = &dwarf2_elf_names;
1754
330cdd98
PA
1755 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1756 locate_sections (obfd, sec, *names);
1757}
1758
5989a64e 1759dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1760{
1761 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1762 free_cached_comp_units ();
1763
b76e467d 1764 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1765 per_cu->imported_symtabs_free ();
fc8e7e75 1766
b2bdb8cf 1767 for (signatured_type *sig_type : all_type_units)
ae640021 1768 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1769
5989a64e 1770 /* Everything else should be on this->obstack. */
330cdd98
PA
1771}
1772
1773/* See declaration. */
1774
1775void
5989a64e 1776dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1777{
1778 dwarf2_per_cu_data *per_cu = read_in_chain;
1779 dwarf2_per_cu_data **last_chain = &read_in_chain;
1780 while (per_cu != NULL)
1781 {
1782 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1783
fcd3b13d 1784 delete per_cu->cu;
330cdd98
PA
1785 *last_chain = next_cu;
1786 per_cu = next_cu;
1787 }
1788}
1789
11ed8cad
TT
1790/* A helper class that calls free_cached_comp_units on
1791 destruction. */
1792
1793class free_cached_comp_units
1794{
1795public:
1796
1797 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1798 : m_per_objfile (per_objfile)
1799 {
1800 }
1801
1802 ~free_cached_comp_units ()
1803 {
5989a64e 1804 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1805 }
1806
1807 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1808
1809private:
1810
1811 dwarf2_per_objfile *m_per_objfile;
1812};
1813
af758d11
SM
1814/* See read.h. */
1815
1816bool
1817dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1818{
1819 gdb_assert (per_cu->index < this->m_symtabs.size ());
1820
1821 return this->m_symtabs[per_cu->index] != nullptr;
1822}
1823
1824/* See read.h. */
1825
1826compunit_symtab *
1827dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1828{
1829 gdb_assert (per_cu->index < this->m_symtabs.size ());
1830
1831 return this->m_symtabs[per_cu->index];
1832}
1833
1834/* See read.h. */
1835
1836void
1837dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1838 compunit_symtab *symtab)
1839{
1840 gdb_assert (per_cu->index < this->m_symtabs.size ());
1841 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1842
1843 this->m_symtabs[per_cu->index] = symtab;
1844}
1845
c906108c 1846/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1847 information and return true if we have enough to do something.
1848 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1849 ELF names are used. CAN_COPY is true for formats where symbol
1850 interposition is possible and so symbol values must follow copy
1851 relocation rules. */
c906108c
SS
1852
1853int
251d32d9 1854dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1855 const struct dwarf2_debug_sections *names,
1856 bool can_copy)
c906108c 1857{
97cbe998
SDJ
1858 if (objfile->flags & OBJF_READNEVER)
1859 return 0;
1860
ed2dc618
SM
1861 struct dwarf2_per_objfile *dwarf2_per_objfile
1862 = get_dwarf2_per_objfile (objfile);
1863
1864 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1865 {
1866 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1867 sharing yet). */
1868 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1869
1870 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1871 }
5bfd760d 1872
5989a64e
SM
1873 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1874 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1875 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1876 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1877}
1878
251d32d9
TG
1879/* When loading sections, we look either for uncompressed section or for
1880 compressed section names. */
233a11ab
CS
1881
1882static int
251d32d9
TG
1883section_is_p (const char *section_name,
1884 const struct dwarf2_section_names *names)
233a11ab 1885{
251d32d9
TG
1886 if (names->normal != NULL
1887 && strcmp (section_name, names->normal) == 0)
1888 return 1;
1889 if (names->compressed != NULL
1890 && strcmp (section_name, names->compressed) == 0)
1891 return 1;
1892 return 0;
233a11ab
CS
1893}
1894
330cdd98 1895/* See declaration. */
c906108c 1896
330cdd98 1897void
5989a64e
SM
1898dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1899 const dwarf2_debug_sections &names)
c906108c 1900{
fd361982 1901 flagword aflag = bfd_section_flags (sectp);
251d32d9 1902
dc7650b8
JK
1903 if ((aflag & SEC_HAS_CONTENTS) == 0)
1904 {
1905 }
950b7495
KS
1906 else if (elf_section_data (sectp)->this_hdr.sh_size
1907 > bfd_get_file_size (abfd))
1908 {
1909 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1910 warning (_("Discarding section %s which has a section size (%s"
1911 ") larger than the file size [in module %s]"),
1912 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1913 bfd_get_filename (abfd));
1914 }
330cdd98 1915 else if (section_is_p (sectp->name, &names.info))
c906108c 1916 {
330cdd98 1917 this->info.s.section = sectp;
fd361982 1918 this->info.size = bfd_section_size (sectp);
c906108c 1919 }
330cdd98 1920 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1921 {
330cdd98 1922 this->abbrev.s.section = sectp;
fd361982 1923 this->abbrev.size = bfd_section_size (sectp);
c906108c 1924 }
330cdd98 1925 else if (section_is_p (sectp->name, &names.line))
c906108c 1926 {
330cdd98 1927 this->line.s.section = sectp;
fd361982 1928 this->line.size = bfd_section_size (sectp);
c906108c 1929 }
330cdd98 1930 else if (section_is_p (sectp->name, &names.loc))
c906108c 1931 {
330cdd98 1932 this->loc.s.section = sectp;
fd361982 1933 this->loc.size = bfd_section_size (sectp);
c906108c 1934 }
330cdd98 1935 else if (section_is_p (sectp->name, &names.loclists))
43988095 1936 {
330cdd98 1937 this->loclists.s.section = sectp;
fd361982 1938 this->loclists.size = bfd_section_size (sectp);
43988095 1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1941 {
330cdd98 1942 this->macinfo.s.section = sectp;
fd361982 1943 this->macinfo.size = bfd_section_size (sectp);
c906108c 1944 }
330cdd98 1945 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1946 {
330cdd98 1947 this->macro.s.section = sectp;
fd361982 1948 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.str))
c906108c 1951 {
330cdd98 1952 this->str.s.section = sectp;
fd361982 1953 this->str.size = bfd_section_size (sectp);
c906108c 1954 }
18a8505e
AT
1955 else if (section_is_p (sectp->name, &names.str_offsets))
1956 {
1957 this->str_offsets.s.section = sectp;
1958 this->str_offsets.size = bfd_section_size (sectp);
1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.line_str))
43988095 1961 {
330cdd98 1962 this->line_str.s.section = sectp;
fd361982 1963 this->line_str.size = bfd_section_size (sectp);
43988095 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1966 {
330cdd98 1967 this->addr.s.section = sectp;
fd361982 1968 this->addr.size = bfd_section_size (sectp);
3019eac3 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1971 {
330cdd98 1972 this->frame.s.section = sectp;
fd361982 1973 this->frame.size = bfd_section_size (sectp);
b6af0555 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1976 {
330cdd98 1977 this->eh_frame.s.section = sectp;
fd361982 1978 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1979 }
330cdd98 1980 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1981 {
330cdd98 1982 this->ranges.s.section = sectp;
fd361982 1983 this->ranges.size = bfd_section_size (sectp);
af34e669 1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1986 {
330cdd98 1987 this->rnglists.s.section = sectp;
fd361982 1988 this->rnglists.size = bfd_section_size (sectp);
43988095 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.types))
348e048f 1991 {
8b70b953
TT
1992 struct dwarf2_section_info type_section;
1993
1994 memset (&type_section, 0, sizeof (type_section));
049412e3 1995 type_section.s.section = sectp;
fd361982 1996 type_section.size = bfd_section_size (sectp);
8b70b953 1997
fd5866f6 1998 this->types.push_back (type_section);
348e048f 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2001 {
330cdd98 2002 this->gdb_index.s.section = sectp;
fd361982 2003 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2004 }
927aa2e7
JK
2005 else if (section_is_p (sectp->name, &names.debug_names))
2006 {
2007 this->debug_names.s.section = sectp;
fd361982 2008 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2009 }
2010 else if (section_is_p (sectp->name, &names.debug_aranges))
2011 {
2012 this->debug_aranges.s.section = sectp;
fd361982 2013 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2014 }
dce234bc 2015
fd361982
AM
2016 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2017 && bfd_section_vma (sectp) == 0)
330cdd98 2018 this->has_section_at_zero = true;
c906108c
SS
2019}
2020
dce234bc 2021/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2022 SECTION_NAME. */
af34e669 2023
dce234bc 2024void
3017a003
TG
2025dwarf2_get_section_info (struct objfile *objfile,
2026 enum dwarf2_section_enum sect,
d521ce57 2027 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2028 bfd_size_type *sizep)
2029{
5bfd760d 2030 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2031 struct dwarf2_section_info *info;
a3b2a86b
TT
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
3017a003
TG
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
5989a64e 2045 info = &data->per_bfd->frame;
3017a003
TG
2046 break;
2047 case DWARF2_EH_FRAME:
5989a64e 2048 info = &data->per_bfd->eh_frame;
3017a003
TG
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
dce234bc 2053
96b79293 2054 info->read (objfile);
dce234bc 2055
96b79293 2056 *sectp = info->get_bfd_section ();
dce234bc
PP
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059}
2060
36586728
TT
2061/* A helper function to find the sections for a .dwz file. */
2062
2063static void
2064locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065{
9a3c8263 2066 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
049412e3 2072 dwz_file->abbrev.s.section = sectp;
fd361982 2073 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
049412e3 2077 dwz_file->info.s.section = sectp;
fd361982 2078 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
049412e3 2082 dwz_file->str.s.section = sectp;
fd361982 2083 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
049412e3 2087 dwz_file->line.s.section = sectp;
fd361982 2088 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
049412e3 2092 dwz_file->macro.s.section = sectp;
fd361982 2093 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2094 }
2ec9a5e0
TT
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
049412e3 2097 dwz_file->gdb_index.s.section = sectp;
fd361982 2098 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2099 }
927aa2e7
JK
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2101 {
2102 dwz_file->debug_names.s.section = sectp;
fd361982 2103 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2104 }
36586728
TT
2105}
2106
c4973306 2107/* See dwarf2read.h. */
36586728 2108
c4973306 2109struct dwz_file *
ed2dc618 2110dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2111{
36586728 2112 const char *filename;
acd13123 2113 bfd_size_type buildid_len_arg;
dc294be5
TT
2114 size_t buildid_len;
2115 bfd_byte *buildid;
36586728 2116
5989a64e
SM
2117 if (dwarf2_per_objfile->per_bfd->dwz_file != NULL)
2118 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
36586728 2119
4db1a1dc 2120 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2121 gdb::unique_xmalloc_ptr<char> data
2122 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2123 &buildid_len_arg, &buildid));
4db1a1dc
TT
2124 if (data == NULL)
2125 {
2126 if (bfd_get_error () == bfd_error_no_error)
2127 return NULL;
2128 error (_("could not read '.gnu_debugaltlink' section: %s"),
2129 bfd_errmsg (bfd_get_error ()));
2130 }
791afaa2
TT
2131
2132 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2133
acd13123
TT
2134 buildid_len = (size_t) buildid_len_arg;
2135
791afaa2 2136 filename = data.get ();
d721ba37
PA
2137
2138 std::string abs_storage;
36586728
TT
2139 if (!IS_ABSOLUTE_PATH (filename))
2140 {
14278e1f
TT
2141 gdb::unique_xmalloc_ptr<char> abs
2142 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2143
14278e1f 2144 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2145 filename = abs_storage.c_str ();
36586728
TT
2146 }
2147
dc294be5
TT
2148 /* First try the file name given in the section. If that doesn't
2149 work, try to use the build-id instead. */
ad80db5b 2150 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2151 if (dwz_bfd != NULL)
36586728 2152 {
192b62ce 2153 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2154 dwz_bfd.reset (nullptr);
36586728
TT
2155 }
2156
dc294be5
TT
2157 if (dwz_bfd == NULL)
2158 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2159
0d79cdc4
AM
2160 if (dwz_bfd == nullptr)
2161 {
2162 gdb::unique_xmalloc_ptr<char> alt_filename;
2163 const char *origname = dwarf2_per_objfile->objfile->original_name;
2164
2165 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2166 buildid_len,
2167 origname,
2168 &alt_filename));
2169
2170 if (fd.get () >= 0)
2171 {
2172 /* File successfully retrieved from server. */
ad80db5b 2173 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2174
2175 if (dwz_bfd == nullptr)
2176 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2177 alt_filename.get ());
2178 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2179 dwz_bfd.reset (nullptr);
2180 }
2181 }
2182
dc294be5
TT
2183 if (dwz_bfd == NULL)
2184 error (_("could not find '.gnu_debugaltlink' file for %s"),
2185 objfile_name (dwarf2_per_objfile->objfile));
2186
7ff8cb8c
TT
2187 std::unique_ptr<struct dwz_file> result
2188 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2189
7ff8cb8c
TT
2190 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2191 result.get ());
36586728 2192
7ff8cb8c
TT
2193 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2194 result->dwz_bfd.get ());
5989a64e
SM
2195 dwarf2_per_objfile->per_bfd->dwz_file = std::move (result);
2196 return dwarf2_per_objfile->per_bfd->dwz_file.get ();
36586728 2197}
9291a0cd 2198\f
7b9f3c50
DE
2199/* DWARF quick_symbols_functions support. */
2200
2201/* TUs can share .debug_line entries, and there can be a lot more TUs than
2202 unique line tables, so we maintain a separate table of all .debug_line
2203 derived entries to support the sharing.
2204 All the quick functions need is the list of file names. We discard the
2205 line_header when we're done and don't need to record it here. */
2206struct quick_file_names
2207{
094b34ac
DE
2208 /* The data used to construct the hash key. */
2209 struct stmt_list_hash hash;
7b9f3c50
DE
2210
2211 /* The number of entries in file_names, real_names. */
2212 unsigned int num_file_names;
2213
2214 /* The file names from the line table, after being run through
2215 file_full_name. */
2216 const char **file_names;
2217
2218 /* The file names from the line table after being run through
2219 gdb_realpath. These are computed lazily. */
2220 const char **real_names;
2221};
2222
2223/* When using the index (and thus not using psymtabs), each CU has an
2224 object of this type. This is used to hold information needed by
2225 the various "quick" methods. */
2226struct dwarf2_per_cu_quick_data
2227{
2228 /* The file table. This can be NULL if there was no file table
2229 or it's currently not read in.
5989a64e 2230 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2231 struct quick_file_names *file_names;
2232
7b9f3c50
DE
2233 /* A temporary mark bit used when iterating over all CUs in
2234 expand_symtabs_matching. */
2235 unsigned int mark : 1;
2236
2237 /* True if we've tried to read the file table and found there isn't one.
2238 There will be no point in trying to read it again next time. */
2239 unsigned int no_file_data : 1;
2240};
2241
094b34ac
DE
2242/* Utility hash function for a stmt_list_hash. */
2243
2244static hashval_t
2245hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2246{
2247 hashval_t v = 0;
2248
2249 if (stmt_list_hash->dwo_unit != NULL)
2250 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2251 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2252 return v;
2253}
2254
2255/* Utility equality function for a stmt_list_hash. */
2256
2257static int
2258eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2259 const struct stmt_list_hash *rhs)
2260{
2261 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2262 return 0;
2263 if (lhs->dwo_unit != NULL
2264 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2265 return 0;
2266
9c541725 2267 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2268}
2269
7b9f3c50
DE
2270/* Hash function for a quick_file_names. */
2271
2272static hashval_t
2273hash_file_name_entry (const void *e)
2274{
9a3c8263
SM
2275 const struct quick_file_names *file_data
2276 = (const struct quick_file_names *) e;
7b9f3c50 2277
094b34ac 2278 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2279}
2280
2281/* Equality function for a quick_file_names. */
2282
2283static int
2284eq_file_name_entry (const void *a, const void *b)
2285{
9a3c8263
SM
2286 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2287 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2288
094b34ac 2289 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2290}
2291
2292/* Delete function for a quick_file_names. */
2293
2294static void
2295delete_file_name_entry (void *e)
2296{
9a3c8263 2297 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2298 int i;
2299
2300 for (i = 0; i < file_data->num_file_names; ++i)
2301 {
2302 xfree ((void*) file_data->file_names[i]);
2303 if (file_data->real_names)
2304 xfree ((void*) file_data->real_names[i]);
2305 }
2306
45940949
TT
2307 /* The space for the struct itself lives on the obstack, so we don't
2308 free it here. */
7b9f3c50
DE
2309}
2310
2311/* Create a quick_file_names hash table. */
2312
5895093f 2313static htab_up
7b9f3c50
DE
2314create_quick_file_names_table (unsigned int nr_initial_entries)
2315{
5895093f
TT
2316 return htab_up (htab_create_alloc (nr_initial_entries,
2317 hash_file_name_entry, eq_file_name_entry,
2318 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2319}
9291a0cd 2320
918dd910
JK
2321/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2322 have to be created afterwards. You should call age_cached_comp_units after
2323 processing PER_CU->CU. dw2_setup must have been already called. */
2324
2325static void
58f0c718 2326load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2327{
3019eac3 2328 if (per_cu->is_debug_types)
e5fe5e75 2329 load_full_type_unit (per_cu);
918dd910 2330 else
58f0c718 2331 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2332
cc12ce38
DE
2333 if (per_cu->cu == NULL)
2334 return; /* Dummy CU. */
2dc860c0
DE
2335
2336 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2337}
2338
a0f42c21 2339/* Read in the symbols for PER_CU. */
2fdf6df6 2340
9291a0cd 2341static void
58f0c718 2342dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2343{
ed2dc618 2344 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2345
f4dc4d17
DE
2346 /* Skip type_unit_groups, reading the type units they contain
2347 is handled elsewhere. */
197400e8 2348 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2349 return;
2350
b303c6f6
AB
2351 /* The destructor of dwarf2_queue_guard frees any entries left on
2352 the queue. After this point we're guaranteed to leave this function
2353 with the dwarf queue empty. */
39856def 2354 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2355
af758d11 2356 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2357 {
2358 queue_comp_unit (per_cu, language_minimal);
58f0c718 2359 load_cu (per_cu, skip_partial);
89e63ee4
DE
2360
2361 /* If we just loaded a CU from a DWO, and we're working with an index
2362 that may badly handle TUs, load all the TUs in that DWO as well.
2363 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2364 if (!per_cu->is_debug_types
cc12ce38 2365 && per_cu->cu != NULL
89e63ee4 2366 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2367 && dwarf2_per_objfile->per_bfd->index_table != NULL
2368 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2369 /* DWP files aren't supported yet. */
ed2dc618 2370 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2371 queue_and_load_all_dwo_tus (per_cu);
95554aad 2372 }
9291a0cd 2373
ed2dc618 2374 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2375
2376 /* Age the cache, releasing compilation units that have not
2377 been used recently. */
ed2dc618 2378 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2379}
2380
2381/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2382 the objfile from which this CU came. Returns the resulting symbol
2383 table. */
2fdf6df6 2384
43f3e411 2385static struct compunit_symtab *
58f0c718 2386dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2387{
ed2dc618
SM
2388 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2389
5989a64e 2390 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2391
2392 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2393 {
11ed8cad 2394 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2395 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2396 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2397 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2398 }
f194fefb 2399
af758d11 2400 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2401}
2402
ff4c9fec 2403/* See declaration. */
f4dc4d17 2404
ff4c9fec 2405dwarf2_per_cu_data *
5989a64e 2406dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2407{
b76e467d 2408 if (index >= this->all_comp_units.size ())
ff4c9fec 2409 {
b76e467d 2410 index -= this->all_comp_units.size ();
b2bdb8cf 2411 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2412 return &this->all_type_units[index]->per_cu;
2413 }
f4dc4d17 2414
ff4c9fec
SM
2415 return this->all_comp_units[index];
2416}
f4dc4d17 2417
ff4c9fec 2418/* See declaration. */
2fdf6df6 2419
ff4c9fec 2420dwarf2_per_cu_data *
5989a64e 2421dwarf2_per_bfd::get_cu (int index)
1fd400ff 2422{
b76e467d 2423 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2424
ff4c9fec 2425 return this->all_comp_units[index];
f4dc4d17
DE
2426}
2427
ff4c9fec 2428/* See declaration. */
f4dc4d17 2429
ff4c9fec 2430signatured_type *
5989a64e 2431dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2432{
b2bdb8cf 2433 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2434
ff4c9fec 2435 return this->all_type_units[index];
1fd400ff
TT
2436}
2437
d3473f0c
TT
2438/* See read.h. */
2439
2440dwarf2_per_cu_data *
5989a64e 2441dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2442{
2443 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2444 result->index = m_num_psymtabs++;
2445 return result;
2446}
2447
2448/* See read.h. */
2449
2450signatured_type *
5989a64e 2451dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2452{
2453 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2454 result->per_cu.index = m_num_psymtabs++;
2455 return result;
2456}
2457
45940949
TT
2458/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2459 obstack, and constructed with the specified field values. */
4b514bc8
JK
2460
2461static dwarf2_per_cu_data *
ed2dc618 2462create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2463 struct dwarf2_section_info *section,
2464 int is_dwz,
2465 sect_offset sect_off, ULONGEST length)
2466{
5989a64e 2467 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2468 the_cu->sect_off = sect_off;
2469 the_cu->length = length;
e3b94546 2470 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2471 the_cu->section = section;
5989a64e 2472 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2473 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2474 the_cu->is_dwz = is_dwz;
2475 return the_cu;
2476}
2477
2ec9a5e0
TT
2478/* A helper for create_cus_from_index that handles a given list of
2479 CUs. */
2fdf6df6 2480
74a0d9f6 2481static void
12359b5e 2482create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2483 const gdb_byte *cu_list, offset_type n_elements,
2484 struct dwarf2_section_info *section,
b76e467d 2485 int is_dwz)
9291a0cd 2486{
12359b5e 2487 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2488 {
74a0d9f6 2489 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2490
2491 sect_offset sect_off
2492 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2493 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2494 cu_list += 2 * 8;
2495
b76e467d 2496 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2497 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2498 sect_off, length);
5989a64e 2499 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2500 }
9291a0cd
TT
2501}
2502
2ec9a5e0 2503/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2504 the CU objects for this objfile. */
2ec9a5e0 2505
74a0d9f6 2506static void
12359b5e 2507create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2508 const gdb_byte *cu_list, offset_type cu_list_elements,
2509 const gdb_byte *dwz_list, offset_type dwz_elements)
2510{
5989a64e
SM
2511 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2512 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2513 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2514
12359b5e 2515 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2516 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2517
2518 if (dwz_elements == 0)
74a0d9f6 2519 return;
2ec9a5e0 2520
12359b5e
SM
2521 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2522 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2523 &dwz->info, 1);
2ec9a5e0
TT
2524}
2525
1fd400ff 2526/* Create the signatured type hash table from the index. */
673bfd45 2527
74a0d9f6 2528static void
12359b5e
SM
2529create_signatured_type_table_from_index
2530 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct dwarf2_section_info *section,
2532 const gdb_byte *bytes,
2533 offset_type elements)
1fd400ff 2534{
5989a64e
SM
2535 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2536 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2537
298e9637 2538 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2539
12359b5e 2540 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2541 {
52dc124a 2542 struct signatured_type *sig_type;
9c541725 2543 ULONGEST signature;
1fd400ff 2544 void **slot;
9c541725 2545 cu_offset type_offset_in_tu;
1fd400ff 2546
74a0d9f6 2547 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2548 sect_offset sect_off
2549 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2550 type_offset_in_tu
2551 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2552 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2553 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2554 bytes += 3 * 8;
2555
5989a64e 2556 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2557 sig_type->signature = signature;
9c541725 2558 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2559 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2560 sig_type->per_cu.section = section;
9c541725 2561 sig_type->per_cu.sect_off = sect_off;
e3b94546 2562 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2563 sig_type->per_cu.v.quick
5989a64e 2564 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2565 struct dwarf2_per_cu_quick_data);
2566
b0b6a987 2567 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2568 *slot = sig_type;
1fd400ff 2569
5989a64e 2570 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2571 }
2572
5989a64e 2573 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2574}
2575
927aa2e7
JK
2576/* Create the signatured type hash table from .debug_names. */
2577
2578static void
2579create_signatured_type_table_from_debug_names
ed2dc618 2580 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2581 const mapped_debug_names &map,
2582 struct dwarf2_section_info *section,
2583 struct dwarf2_section_info *abbrev_section)
2584{
ed2dc618
SM
2585 struct objfile *objfile = dwarf2_per_objfile->objfile;
2586
96b79293
TT
2587 section->read (objfile);
2588 abbrev_section->read (objfile);
927aa2e7 2589
5989a64e
SM
2590 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2591 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2592
298e9637 2593 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2594
2595 for (uint32_t i = 0; i < map.tu_count; ++i)
2596 {
2597 struct signatured_type *sig_type;
927aa2e7 2598 void **slot;
927aa2e7
JK
2599
2600 sect_offset sect_off
2601 = (sect_offset) (extract_unsigned_integer
2602 (map.tu_table_reordered + i * map.offset_size,
2603 map.offset_size,
2604 map.dwarf5_byte_order));
2605
2606 comp_unit_head cu_header;
ed2dc618
SM
2607 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2608 abbrev_section,
927aa2e7
JK
2609 section->buffer + to_underlying (sect_off),
2610 rcuh_kind::TYPE);
2611
5989a64e 2612 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2613 sig_type->signature = cu_header.signature;
2614 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2615 sig_type->per_cu.is_debug_types = 1;
2616 sig_type->per_cu.section = section;
2617 sig_type->per_cu.sect_off = sect_off;
e3b94546 2618 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2619 sig_type->per_cu.v.quick
5989a64e 2620 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2621 struct dwarf2_per_cu_quick_data);
2622
b0b6a987 2623 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2624 *slot = sig_type;
2625
5989a64e 2626 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2627 }
2628
5989a64e 2629 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2630}
2631
9291a0cd
TT
2632/* Read the address map data from the mapped index, and use it to
2633 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2634
9291a0cd 2635static void
ed2dc618
SM
2636create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2637 struct mapped_index *index)
9291a0cd 2638{
ed2dc618 2639 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2640 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2641 const gdb_byte *iter, *end;
9291a0cd 2642 struct addrmap *mutable_map;
9291a0cd
TT
2643 CORE_ADDR baseaddr;
2644
8268c778
PA
2645 auto_obstack temp_obstack;
2646
9291a0cd
TT
2647 mutable_map = addrmap_create_mutable (&temp_obstack);
2648
f00a2de2
PA
2649 iter = index->address_table.data ();
2650 end = iter + index->address_table.size ();
9291a0cd 2651
b3b3bada 2652 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2653
2654 while (iter < end)
2655 {
2656 ULONGEST hi, lo, cu_index;
2657 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2658 iter += 8;
2659 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2660 iter += 8;
2661 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2662 iter += 4;
f652bce2 2663
24a55014 2664 if (lo > hi)
f652bce2 2665 {
b98664d3 2666 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2667 hex_string (lo), hex_string (hi));
24a55014 2668 continue;
f652bce2 2669 }
24a55014 2670
5989a64e 2671 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2672 {
b98664d3 2673 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2674 (unsigned) cu_index);
24a55014 2675 continue;
f652bce2 2676 }
24a55014 2677
79748972
TT
2678 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2679 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2680 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2681 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2682 }
2683
d320c2b5 2684 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2685 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2686}
2687
927aa2e7
JK
2688/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2689 populate the objfile's psymtabs_addrmap. */
2690
2691static void
ed2dc618 2692create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2693 struct dwarf2_section_info *section)
2694{
ed2dc618 2695 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2696 bfd *abfd = objfile->obfd;
08feed99 2697 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2698 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2699
2700 auto_obstack temp_obstack;
2701 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2702
2703 std::unordered_map<sect_offset,
2704 dwarf2_per_cu_data *,
2705 gdb::hash_enum<sect_offset>>
2706 debug_info_offset_to_per_cu;
5989a64e 2707 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2708 {
927aa2e7
JK
2709 const auto insertpair
2710 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2711 if (!insertpair.second)
2712 {
2713 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2714 "debug_info_offset %s, ignoring .debug_aranges."),
2715 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2716 return;
2717 }
2718 }
2719
96b79293 2720 section->read (objfile);
927aa2e7
JK
2721
2722 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2723
2724 const gdb_byte *addr = section->buffer;
2725
2726 while (addr < section->buffer + section->size)
2727 {
2728 const gdb_byte *const entry_addr = addr;
2729 unsigned int bytes_read;
2730
2731 const LONGEST entry_length = read_initial_length (abfd, addr,
2732 &bytes_read);
2733 addr += bytes_read;
2734
2735 const gdb_byte *const entry_end = addr + entry_length;
2736 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2737 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2738 if (addr + entry_length > section->buffer + section->size)
2739 {
47e3f474 2740 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2741 "length %s exceeds section length %s, "
2742 "ignoring .debug_aranges."),
47e3f474
TV
2743 objfile_name (objfile),
2744 plongest (entry_addr - section->buffer),
927aa2e7
JK
2745 plongest (bytes_read + entry_length),
2746 pulongest (section->size));
2747 return;
2748 }
2749
2750 /* The version number. */
2751 const uint16_t version = read_2_bytes (abfd, addr);
2752 addr += 2;
2753 if (version != 2)
2754 {
47e3f474 2755 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2756 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2757 objfile_name (objfile),
2758 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2759 return;
2760 }
2761
2762 const uint64_t debug_info_offset
2763 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2764 addr += offset_size;
2765 const auto per_cu_it
2766 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2767 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2768 {
47e3f474 2769 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2770 "debug_info_offset %s does not exists, "
2771 "ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer),
927aa2e7
JK
2774 pulongest (debug_info_offset));
2775 return;
2776 }
2777 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2778
2779 const uint8_t address_size = *addr++;
2780 if (address_size < 1 || address_size > 8)
2781 {
47e3f474 2782 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2783 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2786 return;
2787 }
2788
2789 const uint8_t segment_selector_size = *addr++;
2790 if (segment_selector_size != 0)
2791 {
47e3f474 2792 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2793 "segment_selector_size %u is not supported, "
2794 "ignoring .debug_aranges."),
47e3f474
TV
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer),
927aa2e7
JK
2797 segment_selector_size);
2798 return;
2799 }
2800
2801 /* Must pad to an alignment boundary that is twice the address
2802 size. It is undocumented by the DWARF standard but GCC does
2803 use it. */
2804 for (size_t padding = ((-(addr - section->buffer))
2805 & (2 * address_size - 1));
2806 padding > 0; padding--)
2807 if (*addr++ != 0)
2808 {
47e3f474 2809 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2810 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer));
927aa2e7
JK
2813 return;
2814 }
2815
2816 for (;;)
2817 {
2818 if (addr + 2 * address_size > entry_end)
2819 {
47e3f474 2820 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2821 "address list is not properly terminated, "
2822 "ignoring .debug_aranges."),
47e3f474
TV
2823 objfile_name (objfile),
2824 plongest (entry_addr - section->buffer));
927aa2e7
JK
2825 return;
2826 }
2827 ULONGEST start = extract_unsigned_integer (addr, address_size,
2828 dwarf5_byte_order);
2829 addr += address_size;
2830 ULONGEST length = extract_unsigned_integer (addr, address_size,
2831 dwarf5_byte_order);
2832 addr += address_size;
2833 if (start == 0 && length == 0)
2834 break;
5989a64e 2835 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2836 {
2837 /* Symbol was eliminated due to a COMDAT group. */
2838 continue;
2839 }
2840 ULONGEST end = start + length;
79748972
TT
2841 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2842 - baseaddr);
2843 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2844 - baseaddr);
927aa2e7
JK
2845 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2846 }
2847 }
2848
d320c2b5 2849 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2850 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2851}
2852
9291a0cd
TT
2853/* Find a slot in the mapped index INDEX for the object named NAME.
2854 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2855 constant pool and return true. If NAME cannot be found, return
2856 false. */
2fdf6df6 2857
109483d9 2858static bool
9291a0cd
TT
2859find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2860 offset_type **vec_out)
2861{
0cf03b49 2862 offset_type hash;
9291a0cd 2863 offset_type slot, step;
559a7a62 2864 int (*cmp) (const char *, const char *);
9291a0cd 2865
791afaa2 2866 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2867 if (current_language->la_language == language_cplus
45280282
IB
2868 || current_language->la_language == language_fortran
2869 || current_language->la_language == language_d)
0cf03b49
JK
2870 {
2871 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2872 not contain any. */
a8719064 2873
72998fb3 2874 if (strchr (name, '(') != NULL)
0cf03b49 2875 {
109483d9 2876 without_params = cp_remove_params (name);
0cf03b49 2877
72998fb3 2878 if (without_params != NULL)
791afaa2 2879 name = without_params.get ();
0cf03b49
JK
2880 }
2881 }
2882
559a7a62 2883 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2884 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2885 simulate our NAME being searched is also lowercased. */
2886 hash = mapped_index_string_hash ((index->version == 4
2887 && case_sensitivity == case_sensitive_off
2888 ? 5 : index->version),
2889 name);
2890
f00a2de2
PA
2891 slot = hash & (index->symbol_table.size () - 1);
2892 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2893 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2894
2895 for (;;)
2896 {
9291a0cd 2897 const char *str;
f00a2de2
PA
2898
2899 const auto &bucket = index->symbol_table[slot];
2900 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2901 return false;
9291a0cd 2902
f00a2de2 2903 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2904 if (!cmp (name, str))
9291a0cd
TT
2905 {
2906 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2907 + MAYBE_SWAP (bucket.vec));
109483d9 2908 return true;
9291a0cd
TT
2909 }
2910
f00a2de2 2911 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2912 }
2913}
2914
4485a1c1
SM
2915/* A helper function that reads the .gdb_index from BUFFER and fills
2916 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2917 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2918 ok to use deprecated sections.
2919
2920 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2921 out parameters that are filled in with information about the CU and
2922 TU lists in the section.
2923
4485a1c1 2924 Returns true if all went well, false otherwise. */
2fdf6df6 2925
d33bc52e 2926static bool
3810f182 2927read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2928 bool deprecated_ok,
2929 gdb::array_view<const gdb_byte> buffer,
2930 struct mapped_index *map,
2931 const gdb_byte **cu_list,
2932 offset_type *cu_list_elements,
2933 const gdb_byte **types_list,
2934 offset_type *types_list_elements)
2935{
2936 const gdb_byte *addr = &buffer[0];
82430852 2937
9291a0cd 2938 /* Version check. */
4485a1c1 2939 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2940 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2941 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2942 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2943 indices. */
831adc1f 2944 if (version < 4)
481860b3
GB
2945 {
2946 static int warning_printed = 0;
2947 if (!warning_printed)
2948 {
2949 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2950 filename);
481860b3
GB
2951 warning_printed = 1;
2952 }
2953 return 0;
2954 }
2955 /* Index version 4 uses a different hash function than index version
2956 5 and later.
2957
2958 Versions earlier than 6 did not emit psymbols for inlined
2959 functions. Using these files will cause GDB not to be able to
2960 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2961 indices unless the user has done
2962 "set use-deprecated-index-sections on". */
2ec9a5e0 2963 if (version < 6 && !deprecated_ok)
481860b3
GB
2964 {
2965 static int warning_printed = 0;
2966 if (!warning_printed)
2967 {
e615022a
DE
2968 warning (_("\
2969Skipping deprecated .gdb_index section in %s.\n\
2970Do \"set use-deprecated-index-sections on\" before the file is read\n\
2971to use the section anyway."),
2ec9a5e0 2972 filename);
481860b3
GB
2973 warning_printed = 1;
2974 }
2975 return 0;
2976 }
796a7ff8 2977 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2978 of the TU (for symbols coming from TUs),
2979 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2980 Plus gold-generated indices can have duplicate entries for global symbols,
2981 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2982 These are just performance bugs, and we can't distinguish gdb-generated
2983 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2984
481860b3 2985 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2986 longer backward compatible. */
796a7ff8 2987 if (version > 8)
594e8718 2988 return 0;
9291a0cd 2989
559a7a62 2990 map->version = version;
9291a0cd 2991
4485a1c1 2992 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2993
4485a1c1 2994 int i = 0;
2ec9a5e0
TT
2995 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2996 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2997 / 8);
1fd400ff
TT
2998 ++i;
2999
2ec9a5e0
TT
3000 *types_list = addr + MAYBE_SWAP (metadata[i]);
3001 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3002 - MAYBE_SWAP (metadata[i]))
3003 / 8);
987d643c 3004 ++i;
1fd400ff 3005
f00a2de2
PA
3006 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3007 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3008 map->address_table
3009 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3010 ++i;
3011
f00a2de2
PA
3012 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3013 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3014 map->symbol_table
3015 = gdb::array_view<mapped_index::symbol_table_slot>
3016 ((mapped_index::symbol_table_slot *) symbol_table,
3017 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3018
f00a2de2 3019 ++i;
f9d83a0b 3020 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3021
2ec9a5e0
TT
3022 return 1;
3023}
3024
4485a1c1
SM
3025/* Callback types for dwarf2_read_gdb_index. */
3026
3027typedef gdb::function_view
5989a64e 3028 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3029 get_gdb_index_contents_ftype;
3030typedef gdb::function_view
3031 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3032 get_gdb_index_contents_dwz_ftype;
3033
927aa2e7 3034/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3035 elements of all the CUs and return 1. Otherwise, return 0. */
3036
3037static int
4485a1c1
SM
3038dwarf2_read_gdb_index
3039 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3040 get_gdb_index_contents_ftype get_gdb_index_contents,
3041 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3042{
2ec9a5e0
TT
3043 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3044 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3045 struct dwz_file *dwz;
12359b5e 3046 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3047
4485a1c1 3048 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3049 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3050
3051 if (main_index_contents.empty ())
3052 return 0;
3053
3063847f 3054 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3055 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3056 use_deprecated_index_sections,
3057 main_index_contents, map.get (), &cu_list,
3058 &cu_list_elements, &types_list,
3059 &types_list_elements))
2ec9a5e0
TT
3060 return 0;
3061
0fefef59 3062 /* Don't use the index if it's empty. */
3063847f 3063 if (map->symbol_table.empty ())
0fefef59
DE
3064 return 0;
3065
2ec9a5e0
TT
3066 /* If there is a .dwz file, read it so we can get its CU list as
3067 well. */
ed2dc618 3068 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3069 if (dwz != NULL)
2ec9a5e0 3070 {
2ec9a5e0
TT
3071 struct mapped_index dwz_map;
3072 const gdb_byte *dwz_types_ignore;
3073 offset_type dwz_types_elements_ignore;
3074
4485a1c1
SM
3075 gdb::array_view<const gdb_byte> dwz_index_content
3076 = get_gdb_index_contents_dwz (objfile, dwz);
3077
3078 if (dwz_index_content.empty ())
3079 return 0;
3080
3810f182 3081 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3082 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3083 &dwz_list, &dwz_list_elements,
3084 &dwz_types_ignore,
3085 &dwz_types_elements_ignore))
2ec9a5e0
TT
3086 {
3087 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3088 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3089 return 0;
3090 }
3091 }
3092
12359b5e
SM
3093 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3094 dwz_list, dwz_list_elements);
1fd400ff 3095
8b70b953
TT
3096 if (types_list_elements)
3097 {
8b70b953
TT
3098 /* We can only handle a single .debug_types when we have an
3099 index. */
5989a64e 3100 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3101 return 0;
3102
5989a64e 3103 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3104
12359b5e
SM
3105 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3106 types_list, types_list_elements);
8b70b953 3107 }
9291a0cd 3108
3063847f 3109 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3110
5989a64e
SM
3111 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3112 dwarf2_per_objfile->per_bfd->using_index = 1;
3113 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3114 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3115
3116 return 1;
3117}
3118
dee91e82 3119/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3120
dee91e82
DE
3121static void
3122dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3123 const gdb_byte *info_ptr,
3e225074 3124 struct die_info *comp_unit_die)
9291a0cd 3125{
dee91e82 3126 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3127 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3128 struct dwarf2_per_objfile *dwarf2_per_objfile
3129 = cu->per_cu->dwarf2_per_objfile;
094b34ac 3130 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3131 struct attribute *attr;
7b9f3c50
DE
3132 void **slot;
3133 struct quick_file_names *qfn;
9291a0cd 3134
0186c6a7
DE
3135 gdb_assert (! this_cu->is_debug_types);
3136
07261596
TT
3137 /* Our callers never want to match partial units -- instead they
3138 will match the enclosing full CU. */
3139 if (comp_unit_die->tag == DW_TAG_partial_unit)
3140 {
3141 this_cu->v.quick->no_file_data = 1;
3142 return;
3143 }
3144
0186c6a7 3145 lh_cu = this_cu;
7b9f3c50 3146 slot = NULL;
dee91e82 3147
fff8551c 3148 line_header_up lh;
9c541725 3149 sect_offset line_offset {};
fff8551c 3150
dee91e82 3151 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3152 if (attr != nullptr)
9291a0cd 3153 {
7b9f3c50
DE
3154 struct quick_file_names find_entry;
3155
9c541725 3156 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3157
3158 /* We may have already read in this line header (TU line header sharing).
3159 If we have we're done. */
094b34ac 3160 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3161 find_entry.hash.line_sect_off = line_offset;
5989a64e 3162 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3163 &find_entry, INSERT);
3164 if (*slot != NULL)
3165 {
9a3c8263 3166 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3167 return;
7b9f3c50
DE
3168 }
3169
3019eac3 3170 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3171 }
3172 if (lh == NULL)
3173 {
094b34ac 3174 lh_cu->v.quick->no_file_data = 1;
dee91e82 3175 return;
9291a0cd
TT
3176 }
3177
5989a64e 3178 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3179 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3180 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3181 gdb_assert (slot != NULL);
3182 *slot = qfn;
9291a0cd 3183
d721ba37 3184 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3185
aa391654
TT
3186 int offset = 0;
3187 if (strcmp (fnd.name, "<unknown>") != 0)
3188 ++offset;
3189
7ba99d21 3190 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3191 qfn->file_names =
5989a64e 3192 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3193 qfn->num_file_names);
aa391654
TT
3194 if (offset != 0)
3195 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3196 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3197 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3198 fnd.comp_dir).release ();
7b9f3c50 3199 qfn->real_names = NULL;
9291a0cd 3200
094b34ac 3201 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3202}
3203
3204/* A helper for the "quick" functions which attempts to read the line
3205 table for THIS_CU. */
3206
3207static struct quick_file_names *
e4a48d9d 3208dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3209{
0186c6a7
DE
3210 /* This should never be called for TUs. */
3211 gdb_assert (! this_cu->is_debug_types);
3212 /* Nor type unit groups. */
197400e8 3213 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3214
dee91e82
DE
3215 if (this_cu->v.quick->file_names != NULL)
3216 return this_cu->v.quick->file_names;
3217 /* If we know there is no line data, no point in looking again. */
3218 if (this_cu->v.quick->no_file_data)
3219 return NULL;
3220
c0ab21c2
TT
3221 cutu_reader reader (this_cu);
3222 if (!reader.dummy_p)
3e225074 3223 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3224
3225 if (this_cu->v.quick->no_file_data)
3226 return NULL;
3227 return this_cu->v.quick->file_names;
9291a0cd
TT
3228}
3229
3230/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3231 real path for a given file name from the line table. */
2fdf6df6 3232
9291a0cd 3233static const char *
45940949 3234dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3235 struct quick_file_names *qfn, int index)
9291a0cd 3236{
7b9f3c50 3237 if (qfn->real_names == NULL)
5989a64e 3238 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3239 qfn->num_file_names, const char *);
9291a0cd 3240
7b9f3c50 3241 if (qfn->real_names[index] == NULL)
14278e1f 3242 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3243
7b9f3c50 3244 return qfn->real_names[index];
9291a0cd
TT
3245}
3246
3247static struct symtab *
3248dw2_find_last_source_symtab (struct objfile *objfile)
3249{
ed2dc618
SM
3250 struct dwarf2_per_objfile *dwarf2_per_objfile
3251 = get_dwarf2_per_objfile (objfile);
5989a64e 3252 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
58f0c718 3253 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3254
43f3e411
DE
3255 if (cust == NULL)
3256 return NULL;
ed2dc618 3257
43f3e411 3258 return compunit_primary_filetab (cust);
9291a0cd
TT
3259}
3260
7b9f3c50
DE
3261/* Traversal function for dw2_forget_cached_source_info. */
3262
3263static int
3264dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3265{
7b9f3c50 3266 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3267
7b9f3c50 3268 if (file_data->real_names)
9291a0cd 3269 {
7b9f3c50 3270 int i;
9291a0cd 3271
7b9f3c50 3272 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3273 {
7b9f3c50
DE
3274 xfree ((void*) file_data->real_names[i]);
3275 file_data->real_names[i] = NULL;
9291a0cd
TT
3276 }
3277 }
7b9f3c50
DE
3278
3279 return 1;
3280}
3281
3282static void
3283dw2_forget_cached_source_info (struct objfile *objfile)
3284{
ed2dc618
SM
3285 struct dwarf2_per_objfile *dwarf2_per_objfile
3286 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3287
5989a64e 3288 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3289 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3290}
3291
f8eba3c6
TT
3292/* Helper function for dw2_map_symtabs_matching_filename that expands
3293 the symtabs and calls the iterator. */
3294
3295static int
3296dw2_map_expand_apply (struct objfile *objfile,
3297 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3298 const char *name, const char *real_path,
14bc53a8 3299 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3300{
43f3e411 3301 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3302
3303 /* Don't visit already-expanded CUs. */
af758d11
SM
3304 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3305 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3306 return 0;
3307
3308 /* This may expand more than one symtab, and we want to iterate over
3309 all of them. */
58f0c718 3310 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3311
14bc53a8
PA
3312 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3313 last_made, callback);
f8eba3c6
TT
3314}
3315
3316/* Implementation of the map_symtabs_matching_filename method. */
3317
14bc53a8
PA
3318static bool
3319dw2_map_symtabs_matching_filename
3320 (struct objfile *objfile, const char *name, const char *real_path,
3321 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3322{
c011a4f4 3323 const char *name_basename = lbasename (name);
ed2dc618
SM
3324 struct dwarf2_per_objfile *dwarf2_per_objfile
3325 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3326
848e3e78
DE
3327 /* The rule is CUs specify all the files, including those used by
3328 any TU, so there's no need to scan TUs here. */
f4dc4d17 3329
5989a64e 3330 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3331 {
3d7bb9d9 3332 /* We only need to look at symtabs not already expanded. */
af758d11 3333 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3334 continue;
3335
b76e467d 3336 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3337 if (file_data == NULL)
9291a0cd
TT
3338 continue;
3339
b76e467d 3340 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3341 {
7b9f3c50 3342 const char *this_name = file_data->file_names[j];
da235a7c 3343 const char *this_real_name;
9291a0cd 3344
af529f8f 3345 if (compare_filenames_for_search (this_name, name))
9291a0cd 3346 {
f5b95b50 3347 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3348 callback))
3349 return true;
288e77a7 3350 continue;
4aac40c8 3351 }
9291a0cd 3352
c011a4f4
DE
3353 /* Before we invoke realpath, which can get expensive when many
3354 files are involved, do a quick comparison of the basenames. */
3355 if (! basenames_may_differ
3356 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3357 continue;
3358
45940949
TT
3359 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3360 file_data, j);
da235a7c 3361 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3362 {
da235a7c 3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3364 callback))
3365 return true;
288e77a7 3366 continue;
da235a7c 3367 }
9291a0cd 3368
da235a7c
JK
3369 if (real_path != NULL)
3370 {
af529f8f
JK
3371 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3372 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3373 if (this_real_name != NULL
af529f8f 3374 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3375 {
f5b95b50 3376 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3377 callback))
3378 return true;
288e77a7 3379 continue;
9291a0cd
TT
3380 }
3381 }
3382 }
3383 }
3384
14bc53a8 3385 return false;
9291a0cd
TT
3386}
3387
da51c347
DE
3388/* Struct used to manage iterating over all CUs looking for a symbol. */
3389
3390struct dw2_symtab_iterator
9291a0cd 3391{
ed2dc618
SM
3392 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3393 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3394 /* If set, only look for symbols that match that block. Valid values are
3395 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3396 gdb::optional<block_enum> block_index;
da51c347
DE
3397 /* The kind of symbol we're looking for. */
3398 domain_enum domain;
3399 /* The list of CUs from the index entry of the symbol,
3400 or NULL if not found. */
3401 offset_type *vec;
3402 /* The next element in VEC to look at. */
3403 int next;
3404 /* The number of elements in VEC, or zero if there is no match. */
3405 int length;
8943b874
DE
3406 /* Have we seen a global version of the symbol?
3407 If so we can ignore all further global instances.
3408 This is to work around gold/15646, inefficient gold-generated
3409 indices. */
3410 int global_seen;
da51c347 3411};
9291a0cd 3412
2b79f376 3413/* Initialize the index symtab iterator ITER. */
2fdf6df6 3414
9291a0cd 3415static void
da51c347 3416dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3417 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3418 gdb::optional<block_enum> block_index,
da51c347
DE
3419 domain_enum domain,
3420 const char *name)
3421{
ed2dc618 3422 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3423 iter->block_index = block_index;
3424 iter->domain = domain;
3425 iter->next = 0;
8943b874 3426 iter->global_seen = 0;
da51c347 3427
5989a64e 3428 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3429
3430 /* index is NULL if OBJF_READNOW. */
3431 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3432 iter->length = MAYBE_SWAP (*iter->vec);
3433 else
3434 {
3435 iter->vec = NULL;
3436 iter->length = 0;
3437 }
3438}
3439
3440/* Return the next matching CU or NULL if there are no more. */
3441
3442static struct dwarf2_per_cu_data *
3443dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3444{
ed2dc618
SM
3445 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3446
da51c347
DE
3447 for ( ; iter->next < iter->length; ++iter->next)
3448 {
3449 offset_type cu_index_and_attrs =
3450 MAYBE_SWAP (iter->vec[iter->next + 1]);
3451 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3452 gdb_index_symbol_kind symbol_kind =
3453 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3454 /* Only check the symbol attributes if they're present.
3455 Indices prior to version 7 don't record them,
3456 and indices >= 7 may elide them for certain symbols
3457 (gold does this). */
3458 int attrs_valid =
5989a64e 3459 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3460 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3461
3190f0c6 3462 /* Don't crash on bad data. */
5989a64e
SM
3463 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3464 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3465 {
b98664d3 3466 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3467 " [in module %s]"),
3468 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3469 continue;
3470 }
3471
5989a64e 3472 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3473
da51c347 3474 /* Skip if already read in. */
af758d11 3475 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3476 continue;
3477
8943b874
DE
3478 /* Check static vs global. */
3479 if (attrs_valid)
3480 {
2b79f376
SM
3481 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3482
3483 if (iter->block_index.has_value ())
3484 {
3485 bool want_static = *iter->block_index == STATIC_BLOCK;
3486
3487 if (is_static != want_static)
3488 continue;
3489 }
3490
8943b874
DE
3491 /* Work around gold/15646. */
3492 if (!is_static && iter->global_seen)
3493 continue;
3494 if (!is_static)
3495 iter->global_seen = 1;
3496 }
da51c347
DE
3497
3498 /* Only check the symbol's kind if it has one. */
3499 if (attrs_valid)
3500 {
3501 switch (iter->domain)
3502 {
3503 case VAR_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3505 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3506 /* Some types are also in VAR_DOMAIN. */
3507 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3508 continue;
3509 break;
3510 case STRUCT_DOMAIN:
3511 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3512 continue;
3513 break;
3514 case LABEL_DOMAIN:
3515 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3516 continue;
3517 break;
59c35742
AB
3518 case MODULE_DOMAIN:
3519 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3520 continue;
3521 break;
da51c347
DE
3522 default:
3523 break;
3524 }
3525 }
3526
3527 ++iter->next;
3528 return per_cu;
3529 }
3530
3531 return NULL;
3532}
3533
43f3e411 3534static struct compunit_symtab *
c7f839cb 3535dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3536 const char *name, domain_enum domain)
9291a0cd 3537{
43f3e411 3538 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
9291a0cd 3541
b5ec771e
PA
3542 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3543
ed2dc618
SM
3544 struct dw2_symtab_iterator iter;
3545 struct dwarf2_per_cu_data *per_cu;
da51c347 3546
2b79f376 3547 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3548
ed2dc618
SM
3549 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3550 {
3551 struct symbol *sym, *with_opaque = NULL;
58f0c718 3552 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3553 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3554 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3555
ed2dc618
SM
3556 sym = block_find_symbol (block, name, domain,
3557 block_find_non_opaque_type_preferred,
3558 &with_opaque);
b2e2f908 3559
ed2dc618
SM
3560 /* Some caution must be observed with overloaded functions
3561 and methods, since the index will not contain any overload
3562 information (but NAME might contain it). */
da51c347 3563
ed2dc618
SM
3564 if (sym != NULL
3565 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3566 return stab;
3567 if (with_opaque != NULL
3568 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3569 stab_best = stab;
da51c347 3570
ed2dc618 3571 /* Keep looking through other CUs. */
9291a0cd 3572 }
9291a0cd 3573
da51c347 3574 return stab_best;
9291a0cd
TT
3575}
3576
3577static void
3578dw2_print_stats (struct objfile *objfile)
3579{
ed2dc618
SM
3580 struct dwarf2_per_objfile *dwarf2_per_objfile
3581 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3582 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3583 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3584 int count = 0;
9291a0cd 3585
ed2dc618 3586 for (int i = 0; i < total; ++i)
9291a0cd 3587 {
5989a64e 3588 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3589
af758d11 3590 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3591 ++count;
3592 }
e4a48d9d 3593 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3594 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3595}
3596
779bd270
DE
3597/* This dumps minimal information about the index.
3598 It is called via "mt print objfiles".
3599 One use is to verify .gdb_index has been loaded by the
3600 gdb.dwarf2/gdb-index.exp testcase. */
3601
9291a0cd
TT
3602static void
3603dw2_dump (struct objfile *objfile)
3604{
ed2dc618
SM
3605 struct dwarf2_per_objfile *dwarf2_per_objfile
3606 = get_dwarf2_per_objfile (objfile);
3607
5989a64e 3608 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3609 printf_filtered (".gdb_index:");
5989a64e 3610 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3611 {
3612 printf_filtered (" version %d\n",
5989a64e 3613 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3614 }
3615 else
3616 printf_filtered (" faked for \"readnow\"\n");
3617 printf_filtered ("\n");
9291a0cd
TT
3618}
3619
9291a0cd
TT
3620static void
3621dw2_expand_symtabs_for_function (struct objfile *objfile,
3622 const char *func_name)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
da51c347 3626
ed2dc618
SM
3627 struct dw2_symtab_iterator iter;
3628 struct dwarf2_per_cu_data *per_cu;
da51c347 3629
2b79f376 3630 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3631
ed2dc618 3632 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3633 dw2_instantiate_symtab (per_cu, false);
da51c347 3634
9291a0cd
TT
3635}
3636
3637static void
3638dw2_expand_all_symtabs (struct objfile *objfile)
3639{
ed2dc618
SM
3640 struct dwarf2_per_objfile *dwarf2_per_objfile
3641 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3642 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3643 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3644
ed2dc618 3645 for (int i = 0; i < total_units; ++i)
9291a0cd 3646 {
5989a64e 3647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3648
58f0c718
TT
3649 /* We don't want to directly expand a partial CU, because if we
3650 read it with the wrong language, then assertion failures can
3651 be triggered later on. See PR symtab/23010. So, tell
3652 dw2_instantiate_symtab to skip partial CUs -- any important
3653 partial CU will be read via DW_TAG_imported_unit anyway. */
3654 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3655 }
3656}
3657
3658static void
652a8996
JK
3659dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3660 const char *fullname)
9291a0cd 3661{
ed2dc618
SM
3662 struct dwarf2_per_objfile *dwarf2_per_objfile
3663 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3664
3665 /* We don't need to consider type units here.
3666 This is only called for examining code, e.g. expand_line_sal.
3667 There can be an order of magnitude (or more) more type units
3668 than comp units, and we avoid them if we can. */
3669
5989a64e 3670 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3671 {
3d7bb9d9 3672 /* We only need to look at symtabs not already expanded. */
af758d11 3673 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3674 continue;
3675
b76e467d 3676 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3677 if (file_data == NULL)
9291a0cd
TT
3678 continue;
3679
b76e467d 3680 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3681 {
652a8996
JK
3682 const char *this_fullname = file_data->file_names[j];
3683
3684 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3685 {
58f0c718 3686 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3687 break;
3688 }
3689 }
3690 }
3691}
3692
9a0bacfb
TV
3693static void
3694dw2_expand_symtabs_matching_symbol
3695 (mapped_index_base &index,
3696 const lookup_name_info &lookup_name_in,
3697 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3698 enum search_domain kind,
3699 gdb::function_view<bool (offset_type)> match_callback);
3700
3701static void
3702dw2_expand_symtabs_matching_one
3703 (struct dwarf2_per_cu_data *per_cu,
3704 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3705 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3706
9291a0cd 3707static void
199b4314
TT
3708dw2_map_matching_symbols
3709 (struct objfile *objfile,
b054970d 3710 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3711 int global,
3712 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3713 symbol_compare_ftype *ordered_compare)
9291a0cd 3714{
1aa98955
TV
3715 /* Used for Ada. */
3716 struct dwarf2_per_objfile *dwarf2_per_objfile
3717 = get_dwarf2_per_objfile (objfile);
3718
9a0bacfb
TV
3719 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3720
5989a64e 3721 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3722 {
3723 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3724 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3725 using GNU index. */
5989a64e 3726 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3727
9a0bacfb
TV
3728 const char *match_name = name.ada ().lookup_name ().c_str ();
3729 auto matcher = [&] (const char *symname)
3730 {
3731 if (ordered_compare == nullptr)
3732 return true;
3733 return ordered_compare (symname, match_name) == 0;
3734 };
3735
3736 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3737 [&] (offset_type namei)
3738 {
3739 struct dw2_symtab_iterator iter;
3740 struct dwarf2_per_cu_data *per_cu;
3741
3742 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3743 match_name);
3744 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3745 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
3746 return true;
3747 });
3748 }
3749 else
3750 {
3751 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3752 proceed assuming all symtabs have been read in. */
3753 }
1aa98955
TV
3754
3755 for (compunit_symtab *cust : objfile->compunits ())
3756 {
3757 const struct block *block;
3758
3759 if (cust == NULL)
3760 continue;
3761 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3762 if (!iterate_over_symbols_terminated (block, name,
3763 domain, callback))
3764 return;
3765 }
9291a0cd
TT
3766}
3767
e1ef7d7a
PA
3768/* Starting from a search name, return the string that finds the upper
3769 bound of all strings that start with SEARCH_NAME in a sorted name
3770 list. Returns the empty string to indicate that the upper bound is
3771 the end of the list. */
3772
3773static std::string
3774make_sort_after_prefix_name (const char *search_name)
3775{
3776 /* When looking to complete "func", we find the upper bound of all
3777 symbols that start with "func" by looking for where we'd insert
3778 the closest string that would follow "func" in lexicographical
3779 order. Usually, that's "func"-with-last-character-incremented,
3780 i.e. "fund". Mind non-ASCII characters, though. Usually those
3781 will be UTF-8 multi-byte sequences, but we can't be certain.
3782 Especially mind the 0xff character, which is a valid character in
3783 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3784 rule out compilers allowing it in identifiers. Note that
3785 conveniently, strcmp/strcasecmp are specified to compare
3786 characters interpreted as unsigned char. So what we do is treat
3787 the whole string as a base 256 number composed of a sequence of
3788 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3789 to 0, and carries 1 to the following more-significant position.
3790 If the very first character in SEARCH_NAME ends up incremented
3791 and carries/overflows, then the upper bound is the end of the
3792 list. The string after the empty string is also the empty
3793 string.
3794
3795 Some examples of this operation:
3796
3797 SEARCH_NAME => "+1" RESULT
3798
3799 "abc" => "abd"
3800 "ab\xff" => "ac"
3801 "\xff" "a" "\xff" => "\xff" "b"
3802 "\xff" => ""
3803 "\xff\xff" => ""
3804 "" => ""
3805
3806 Then, with these symbols for example:
3807
3808 func
3809 func1
3810 fund
3811
3812 completing "func" looks for symbols between "func" and
3813 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3814 which finds "func" and "func1", but not "fund".
3815
3816 And with:
3817
3818 funcÿ (Latin1 'ÿ' [0xff])
3819 funcÿ1
3820 fund
3821
3822 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3823 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3824
3825 And with:
3826
3827 ÿÿ (Latin1 'ÿ' [0xff])
3828 ÿÿ1
3829
3830 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3831 the end of the list.
3832 */
3833 std::string after = search_name;
3834 while (!after.empty () && (unsigned char) after.back () == 0xff)
3835 after.pop_back ();
3836 if (!after.empty ())
3837 after.back () = (unsigned char) after.back () + 1;
3838 return after;
3839}
3840
5c58de74 3841/* See declaration. */
61d96d7e 3842
5c58de74
PA
3843std::pair<std::vector<name_component>::const_iterator,
3844 std::vector<name_component>::const_iterator>
44ed8f3e 3845mapped_index_base::find_name_components_bounds
3b00ef10 3846 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3847{
5c58de74
PA
3848 auto *name_cmp
3849 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3850
3b00ef10 3851 const char *lang_name
e0802d59 3852 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3853
3f563c84
PA
3854 /* Comparison function object for lower_bound that matches against a
3855 given symbol name. */
3856 auto lookup_compare_lower = [&] (const name_component &elem,
3857 const char *name)
3858 {
5c58de74 3859 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3860 const char *elem_name = elem_qualified + elem.name_offset;
3861 return name_cmp (elem_name, name) < 0;
3862 };
3863
3864 /* Comparison function object for upper_bound that matches against a
3865 given symbol name. */
3866 auto lookup_compare_upper = [&] (const char *name,
3867 const name_component &elem)
3868 {
5c58de74 3869 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3870 const char *elem_name = elem_qualified + elem.name_offset;
3871 return name_cmp (name, elem_name) < 0;
3872 };
3873
5c58de74
PA
3874 auto begin = this->name_components.begin ();
3875 auto end = this->name_components.end ();
3f563c84
PA
3876
3877 /* Find the lower bound. */
3878 auto lower = [&] ()
3879 {
3b00ef10 3880 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3881 return begin;
3882 else
3b00ef10 3883 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3884 } ();
3885
3886 /* Find the upper bound. */
3887 auto upper = [&] ()
3888 {
5c58de74 3889 if (lookup_name_without_params.completion_mode ())
3f563c84 3890 {
e1ef7d7a
PA
3891 /* In completion mode, we want UPPER to point past all
3892 symbols names that have the same prefix. I.e., with
3893 these symbols, and completing "func":
3894
3895 function << lower bound
3896 function1
3897 other_function << upper bound
3898
3899 We find the upper bound by looking for the insertion
3900 point of "func"-with-last-character-incremented,
3901 i.e. "fund". */
3b00ef10 3902 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3903 if (after.empty ())
3f563c84 3904 return end;
e6b2f5ef
PA
3905 return std::lower_bound (lower, end, after.c_str (),
3906 lookup_compare_lower);
3f563c84
PA
3907 }
3908 else
3b00ef10 3909 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3910 } ();
3911
5c58de74
PA
3912 return {lower, upper};
3913}
3914
3915/* See declaration. */
3916
3917void
44ed8f3e 3918mapped_index_base::build_name_components ()
5c58de74
PA
3919{
3920 if (!this->name_components.empty ())
3921 return;
3922
3923 this->name_components_casing = case_sensitivity;
3924 auto *name_cmp
3925 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3926
3927 /* The code below only knows how to break apart components of C++
3928 symbol names (and other languages that use '::' as
3b00ef10 3929 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3930 auto count = this->symbol_name_count ();
3931 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3932 {
44ed8f3e 3933 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3934 continue;
3935
3936 const char *name = this->symbol_name_at (idx);
3937
3938 /* Add each name component to the name component table. */
3939 unsigned int previous_len = 0;
3b00ef10
TT
3940
3941 if (strstr (name, "::") != nullptr)
3942 {
3943 for (unsigned int current_len = cp_find_first_component (name);
3944 name[current_len] != '\0';
3945 current_len += cp_find_first_component (name + current_len))
3946 {
3947 gdb_assert (name[current_len] == ':');
3948 this->name_components.push_back ({previous_len, idx});
3949 /* Skip the '::'. */
3950 current_len += 2;
3951 previous_len = current_len;
3952 }
3953 }
3954 else
5c58de74 3955 {
3b00ef10
TT
3956 /* Handle the Ada encoded (aka mangled) form here. */
3957 for (const char *iter = strstr (name, "__");
3958 iter != nullptr;
3959 iter = strstr (iter, "__"))
3960 {
3961 this->name_components.push_back ({previous_len, idx});
3962 iter += 2;
3963 previous_len = iter - name;
3964 }
5c58de74 3965 }
3b00ef10 3966
5c58de74
PA
3967 this->name_components.push_back ({previous_len, idx});
3968 }
3969
3970 /* Sort name_components elements by name. */
3971 auto name_comp_compare = [&] (const name_component &left,
3972 const name_component &right)
3973 {
3974 const char *left_qualified = this->symbol_name_at (left.idx);
3975 const char *right_qualified = this->symbol_name_at (right.idx);
3976
3977 const char *left_name = left_qualified + left.name_offset;
3978 const char *right_name = right_qualified + right.name_offset;
3979
3980 return name_cmp (left_name, right_name) < 0;
3981 };
3982
3983 std::sort (this->name_components.begin (),
3984 this->name_components.end (),
3985 name_comp_compare);
3986}
3987
3988/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3989 mapped_index_base instead of the containing objfile. This is split
3990 to a separate function in order to be able to unit test the
3991 name_components matching using a mock mapped_index_base. For each
5c58de74 3992 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3993 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3994
3995static void
3996dw2_expand_symtabs_matching_symbol
44ed8f3e 3997 (mapped_index_base &index,
5c58de74
PA
3998 const lookup_name_info &lookup_name_in,
3999 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4000 enum search_domain kind,
3b00ef10 4001 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4002{
4003 lookup_name_info lookup_name_without_params
4004 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4005
4006 /* Build the symbol name component sorted vector, if we haven't
4007 yet. */
4008 index.build_name_components ();
4009
3f563c84
PA
4010 /* The same symbol may appear more than once in the range though.
4011 E.g., if we're looking for symbols that complete "w", and we have
4012 a symbol named "w1::w2", we'll find the two name components for
4013 that same symbol in the range. To be sure we only call the
4014 callback once per symbol, we first collect the symbol name
4015 indexes that matched in a temporary vector and ignore
4016 duplicates. */
4017 std::vector<offset_type> matches;
3f563c84 4018
3b00ef10
TT
4019 struct name_and_matcher
4020 {
4021 symbol_name_matcher_ftype *matcher;
ecc6c606 4022 const char *name;
3b00ef10
TT
4023
4024 bool operator== (const name_and_matcher &other) const
3f563c84 4025 {
ecc6c606 4026 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4027 }
4028 };
4029
4030 /* A vector holding all the different symbol name matchers, for all
4031 languages. */
4032 std::vector<name_and_matcher> matchers;
4033
4034 for (int i = 0; i < nr_languages; i++)
4035 {
4036 enum language lang_e = (enum language) i;
4037
4038 const language_defn *lang = language_def (lang_e);
4039 symbol_name_matcher_ftype *name_matcher
4040 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4041
3b00ef10
TT
4042 name_and_matcher key {
4043 name_matcher,
4044 lookup_name_without_params.language_lookup_name (lang_e)
4045 };
4046
4047 /* Don't insert the same comparison routine more than once.
4048 Note that we do this linear walk. This is not a problem in
4049 practice because the number of supported languages is
4050 low. */
4051 if (std::find (matchers.begin (), matchers.end (), key)
4052 != matchers.end ())
9291a0cd 4053 continue;
3b00ef10
TT
4054 matchers.push_back (std::move (key));
4055
4056 auto bounds
4057 = index.find_name_components_bounds (lookup_name_without_params,
4058 lang_e);
4059
4060 /* Now for each symbol name in range, check to see if we have a name
4061 match, and if so, call the MATCH_CALLBACK callback. */
4062
4063 for (; bounds.first != bounds.second; ++bounds.first)
4064 {
4065 const char *qualified = index.symbol_name_at (bounds.first->idx);
4066
4067 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4068 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4069 continue;
9291a0cd 4070
3b00ef10
TT
4071 matches.push_back (bounds.first->idx);
4072 }
3f563c84
PA
4073 }
4074
4075 std::sort (matches.begin (), matches.end ());
4076
4077 /* Finally call the callback, once per match. */
4078 ULONGEST prev = -1;
4079 for (offset_type idx : matches)
4080 {
4081 if (prev != idx)
4082 {
3b00ef10
TT
4083 if (!match_callback (idx))
4084 break;
3f563c84
PA
4085 prev = idx;
4086 }
4087 }
4088
4089 /* Above we use a type wider than idx's for 'prev', since 0 and
4090 (offset_type)-1 are both possible values. */
4091 static_assert (sizeof (prev) > sizeof (offset_type), "");
4092}
4093
c62446b1
PA
4094#if GDB_SELF_TEST
4095
4096namespace selftests { namespace dw2_expand_symtabs_matching {
4097
a3c5fafd
PA
4098/* A mock .gdb_index/.debug_names-like name index table, enough to
4099 exercise dw2_expand_symtabs_matching_symbol, which works with the
4100 mapped_index_base interface. Builds an index from the symbol list
4101 passed as parameter to the constructor. */
4102class mock_mapped_index : public mapped_index_base
c62446b1
PA
4103{
4104public:
a3c5fafd
PA
4105 mock_mapped_index (gdb::array_view<const char *> symbols)
4106 : m_symbol_table (symbols)
c62446b1
PA
4107 {}
4108
a3c5fafd 4109 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4110
a3c5fafd 4111 /* Return the number of names in the symbol table. */
632e107b 4112 size_t symbol_name_count () const override
c62446b1 4113 {
a3c5fafd 4114 return m_symbol_table.size ();
c62446b1
PA
4115 }
4116
a3c5fafd 4117 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4118 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4119 {
4120 return m_symbol_table[idx];
4121 }
c62446b1 4122
a3c5fafd
PA
4123private:
4124 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4125};
4126
4127/* Convenience function that converts a NULL pointer to a "<null>"
4128 string, to pass to print routines. */
4129
4130static const char *
4131string_or_null (const char *str)
4132{
4133 return str != NULL ? str : "<null>";
4134}
4135
4136/* Check if a lookup_name_info built from
4137 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4138 index. EXPECTED_LIST is the list of expected matches, in expected
4139 matching order. If no match expected, then an empty list is
4140 specified. Returns true on success. On failure prints a warning
4141 indicating the file:line that failed, and returns false. */
4142
4143static bool
4144check_match (const char *file, int line,
4145 mock_mapped_index &mock_index,
4146 const char *name, symbol_name_match_type match_type,
4147 bool completion_mode,
4148 std::initializer_list<const char *> expected_list)
4149{
4150 lookup_name_info lookup_name (name, match_type, completion_mode);
4151
4152 bool matched = true;
4153
4154 auto mismatch = [&] (const char *expected_str,
4155 const char *got)
4156 {
4157 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4158 "expected=\"%s\", got=\"%s\"\n"),
4159 file, line,
4160 (match_type == symbol_name_match_type::FULL
4161 ? "FULL" : "WILD"),
4162 name, string_or_null (expected_str), string_or_null (got));
4163 matched = false;
4164 };
4165
4166 auto expected_it = expected_list.begin ();
4167 auto expected_end = expected_list.end ();
4168
a3c5fafd 4169 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4170 NULL, ALL_DOMAIN,
4171 [&] (offset_type idx)
4172 {
a3c5fafd 4173 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4174 const char *expected_str
4175 = expected_it == expected_end ? NULL : *expected_it++;
4176
4177 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4178 mismatch (expected_str, matched_name);
3b00ef10 4179 return true;
c62446b1
PA
4180 });
4181
4182 const char *expected_str
4183 = expected_it == expected_end ? NULL : *expected_it++;
4184 if (expected_str != NULL)
4185 mismatch (expected_str, NULL);
4186
4187 return matched;
4188}
4189
4190/* The symbols added to the mock mapped_index for testing (in
4191 canonical form). */
4192static const char *test_symbols[] = {
4193 "function",
4194 "std::bar",
4195 "std::zfunction",
4196 "std::zfunction2",
4197 "w1::w2",
4198 "ns::foo<char*>",
4199 "ns::foo<int>",
4200 "ns::foo<long>",
a20714ff
PA
4201 "ns2::tmpl<int>::foo2",
4202 "(anonymous namespace)::A::B::C",
c62446b1 4203
e1ef7d7a
PA
4204 /* These are used to check that the increment-last-char in the
4205 matching algorithm for completion doesn't match "t1_fund" when
4206 completing "t1_func". */
4207 "t1_func",
4208 "t1_func1",
4209 "t1_fund",
4210 "t1_fund1",
4211
4212 /* A UTF-8 name with multi-byte sequences to make sure that
4213 cp-name-parser understands this as a single identifier ("função"
4214 is "function" in PT). */
4215 u8"u8função",
4216
4217 /* \377 (0xff) is Latin1 'ÿ'. */
4218 "yfunc\377",
4219
4220 /* \377 (0xff) is Latin1 'ÿ'. */
4221 "\377",
4222 "\377\377123",
4223
c62446b1
PA
4224 /* A name with all sorts of complications. Starts with "z" to make
4225 it easier for the completion tests below. */
4226#define Z_SYM_NAME \
4227 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4228 "::tuple<(anonymous namespace)::ui*, " \
4229 "std::default_delete<(anonymous namespace)::ui>, void>"
4230
4231 Z_SYM_NAME
4232};
4233
a3c5fafd
PA
4234/* Returns true if the mapped_index_base::find_name_component_bounds
4235 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4236 in completion mode. */
5c58de74
PA
4237
4238static bool
a3c5fafd 4239check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4240 const char *search_name,
4241 gdb::array_view<const char *> expected_syms)
4242{
4243 lookup_name_info lookup_name (search_name,
4244 symbol_name_match_type::FULL, true);
4245
3b00ef10
TT
4246 auto bounds = index.find_name_components_bounds (lookup_name,
4247 language_cplus);
5c58de74
PA
4248
4249 size_t distance = std::distance (bounds.first, bounds.second);
4250 if (distance != expected_syms.size ())
4251 return false;
4252
4253 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4254 {
4255 auto nc_elem = bounds.first + exp_elem;
4256 const char *qualified = index.symbol_name_at (nc_elem->idx);
4257 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4258 return false;
4259 }
4260
4261 return true;
4262}
4263
4264/* Test the lower-level mapped_index::find_name_component_bounds
4265 method. */
4266
c62446b1 4267static void
5c58de74
PA
4268test_mapped_index_find_name_component_bounds ()
4269{
4270 mock_mapped_index mock_index (test_symbols);
4271
a3c5fafd 4272 mock_index.build_name_components ();
5c58de74
PA
4273
4274 /* Test the lower-level mapped_index::find_name_component_bounds
4275 method in completion mode. */
4276 {
4277 static const char *expected_syms[] = {
4278 "t1_func",
4279 "t1_func1",
5c58de74
PA
4280 };
4281
a3c5fafd 4282 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4283 "t1_func", expected_syms));
4284 }
4285
4286 /* Check that the increment-last-char in the name matching algorithm
4287 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4288 {
4289 static const char *expected_syms1[] = {
4290 "\377",
4291 "\377\377123",
4292 };
a3c5fafd 4293 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4294 "\377", expected_syms1));
4295
4296 static const char *expected_syms2[] = {
4297 "\377\377123",
4298 };
a3c5fafd 4299 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4300 "\377\377", expected_syms2));
4301 }
4302}
4303
4304/* Test dw2_expand_symtabs_matching_symbol. */
4305
4306static void
4307test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4308{
4309 mock_mapped_index mock_index (test_symbols);
4310
4311 /* We let all tests run until the end even if some fails, for debug
4312 convenience. */
4313 bool any_mismatch = false;
4314
4315 /* Create the expected symbols list (an initializer_list). Needed
4316 because lists have commas, and we need to pass them to CHECK,
4317 which is a macro. */
4318#define EXPECT(...) { __VA_ARGS__ }
4319
4320 /* Wrapper for check_match that passes down the current
4321 __FILE__/__LINE__. */
4322#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4323 any_mismatch |= !check_match (__FILE__, __LINE__, \
4324 mock_index, \
4325 NAME, MATCH_TYPE, COMPLETION_MODE, \
4326 EXPECTED_LIST)
4327
4328 /* Identity checks. */
4329 for (const char *sym : test_symbols)
4330 {
4331 /* Should be able to match all existing symbols. */
4332 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4333 EXPECT (sym));
4334
4335 /* Should be able to match all existing symbols with
4336 parameters. */
4337 std::string with_params = std::string (sym) + "(int)";
4338 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4339 EXPECT (sym));
4340
4341 /* Should be able to match all existing symbols with
4342 parameters and qualifiers. */
4343 with_params = std::string (sym) + " ( int ) const";
4344 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4345 EXPECT (sym));
4346
4347 /* This should really find sym, but cp-name-parser.y doesn't
4348 know about lvalue/rvalue qualifiers yet. */
4349 with_params = std::string (sym) + " ( int ) &&";
4350 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4351 {});
4352 }
4353
e1ef7d7a
PA
4354 /* Check that the name matching algorithm for completion doesn't get
4355 confused with Latin1 'ÿ' / 0xff. */
4356 {
4357 static const char str[] = "\377";
4358 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4359 EXPECT ("\377", "\377\377123"));
4360 }
4361
4362 /* Check that the increment-last-char in the matching algorithm for
4363 completion doesn't match "t1_fund" when completing "t1_func". */
4364 {
4365 static const char str[] = "t1_func";
4366 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4367 EXPECT ("t1_func", "t1_func1"));
4368 }
4369
c62446b1
PA
4370 /* Check that completion mode works at each prefix of the expected
4371 symbol name. */
4372 {
4373 static const char str[] = "function(int)";
4374 size_t len = strlen (str);
4375 std::string lookup;
4376
4377 for (size_t i = 1; i < len; i++)
4378 {
4379 lookup.assign (str, i);
4380 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4381 EXPECT ("function"));
4382 }
4383 }
4384
4385 /* While "w" is a prefix of both components, the match function
4386 should still only be called once. */
4387 {
4388 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4389 EXPECT ("w1::w2"));
a20714ff
PA
4390 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4391 EXPECT ("w1::w2"));
c62446b1
PA
4392 }
4393
4394 /* Same, with a "complicated" symbol. */
4395 {
4396 static const char str[] = Z_SYM_NAME;
4397 size_t len = strlen (str);
4398 std::string lookup;
4399
4400 for (size_t i = 1; i < len; i++)
4401 {
4402 lookup.assign (str, i);
4403 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4404 EXPECT (Z_SYM_NAME));
4405 }
4406 }
4407
4408 /* In FULL mode, an incomplete symbol doesn't match. */
4409 {
4410 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4411 {});
4412 }
4413
4414 /* A complete symbol with parameters matches any overload, since the
4415 index has no overload info. */
4416 {
4417 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4418 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4419 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4420 EXPECT ("std::zfunction", "std::zfunction2"));
4421 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4422 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4423 }
4424
4425 /* Check that whitespace is ignored appropriately. A symbol with a
4426 template argument list. */
4427 {
4428 static const char expected[] = "ns::foo<int>";
4429 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4430 EXPECT (expected));
a20714ff
PA
4431 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4432 EXPECT (expected));
c62446b1
PA
4433 }
4434
4435 /* Check that whitespace is ignored appropriately. A symbol with a
4436 template argument list that includes a pointer. */
4437 {
4438 static const char expected[] = "ns::foo<char*>";
4439 /* Try both completion and non-completion modes. */
4440 static const bool completion_mode[2] = {false, true};
4441 for (size_t i = 0; i < 2; i++)
4442 {
4443 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4444 completion_mode[i], EXPECT (expected));
a20714ff
PA
4445 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4446 completion_mode[i], EXPECT (expected));
c62446b1
PA
4447
4448 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4449 completion_mode[i], EXPECT (expected));
a20714ff
PA
4450 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4451 completion_mode[i], EXPECT (expected));
c62446b1
PA
4452 }
4453 }
4454
4455 {
4456 /* Check method qualifiers are ignored. */
4457 static const char expected[] = "ns::foo<char*>";
4458 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4459 symbol_name_match_type::FULL, true, EXPECT (expected));
4460 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4461 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4462 CHECK_MATCH ("foo < char * > ( int ) const",
4463 symbol_name_match_type::WILD, true, EXPECT (expected));
4464 CHECK_MATCH ("foo < char * > ( int ) &&",
4465 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4466 }
4467
4468 /* Test lookup names that don't match anything. */
4469 {
a20714ff
PA
4470 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4471 {});
4472
c62446b1
PA
4473 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4474 {});
4475 }
4476
a20714ff
PA
4477 /* Some wild matching tests, exercising "(anonymous namespace)",
4478 which should not be confused with a parameter list. */
4479 {
4480 static const char *syms[] = {
4481 "A::B::C",
4482 "B::C",
4483 "C",
4484 "A :: B :: C ( int )",
4485 "B :: C ( int )",
4486 "C ( int )",
4487 };
4488
4489 for (const char *s : syms)
4490 {
4491 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4492 EXPECT ("(anonymous namespace)::A::B::C"));
4493 }
4494 }
4495
4496 {
4497 static const char expected[] = "ns2::tmpl<int>::foo2";
4498 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4499 EXPECT (expected));
4500 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4501 EXPECT (expected));
4502 }
4503
c62446b1
PA
4504 SELF_CHECK (!any_mismatch);
4505
4506#undef EXPECT
4507#undef CHECK_MATCH
4508}
4509
5c58de74
PA
4510static void
4511run_test ()
4512{
4513 test_mapped_index_find_name_component_bounds ();
4514 test_dw2_expand_symtabs_matching_symbol ();
4515}
4516
c62446b1
PA
4517}} // namespace selftests::dw2_expand_symtabs_matching
4518
4519#endif /* GDB_SELF_TEST */
4520
4b514bc8
JK
4521/* If FILE_MATCHER is NULL or if PER_CU has
4522 dwarf2_per_cu_quick_data::MARK set (see
4523 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4524 EXPANSION_NOTIFY on it. */
4525
4526static void
4527dw2_expand_symtabs_matching_one
4528 (struct dwarf2_per_cu_data *per_cu,
4529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4530 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4531{
4532 if (file_matcher == NULL || per_cu->v.quick->mark)
4533 {
af758d11
SM
4534 dwarf2_per_objfile *per_objfile = per_cu->dwarf2_per_objfile;
4535 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4536
af758d11
SM
4537 compunit_symtab *symtab = dw2_instantiate_symtab (per_cu, false);
4538 gdb_assert (symtab != nullptr);
4b514bc8 4539
af758d11
SM
4540 if (expansion_notify != NULL && symtab_was_null)
4541 expansion_notify (symtab);
4b514bc8
JK
4542 }
4543}
4544
3f563c84
PA
4545/* Helper for dw2_expand_matching symtabs. Called on each symbol
4546 matched, to expand corresponding CUs that were marked. IDX is the
4547 index of the symbol name that matched. */
4548
4549static void
4550dw2_expand_marked_cus
ed2dc618 4551 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4554 search_domain kind)
4555{
3f563c84
PA
4556 offset_type *vec, vec_len, vec_idx;
4557 bool global_seen = false;
5989a64e 4558 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4559
61920122 4560 vec = (offset_type *) (index.constant_pool
f00a2de2 4561 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4562 vec_len = MAYBE_SWAP (vec[0]);
4563 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4564 {
61920122
PA
4565 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4566 /* This value is only valid for index versions >= 7. */
4567 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4568 gdb_index_symbol_kind symbol_kind =
4569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4570 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4571 /* Only check the symbol attributes if they're present.
4572 Indices prior to version 7 don't record them,
4573 and indices >= 7 may elide them for certain symbols
4574 (gold does this). */
4575 int attrs_valid =
4576 (index.version >= 7
4577 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4578
4579 /* Work around gold/15646. */
4580 if (attrs_valid)
9291a0cd 4581 {
61920122
PA
4582 if (!is_static && global_seen)
4583 continue;
4584 if (!is_static)
4585 global_seen = true;
4586 }
3190f0c6 4587
61920122
PA
4588 /* Only check the symbol's kind if it has one. */
4589 if (attrs_valid)
4590 {
4591 switch (kind)
8943b874 4592 {
61920122
PA
4593 case VARIABLES_DOMAIN:
4594 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4595 continue;
4596 break;
4597 case FUNCTIONS_DOMAIN:
4598 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4599 continue;
61920122
PA
4600 break;
4601 case TYPES_DOMAIN:
4602 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4603 continue;
4604 break;
59c35742
AB
4605 case MODULES_DOMAIN:
4606 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4607 continue;
4608 break;
61920122
PA
4609 default:
4610 break;
8943b874 4611 }
61920122 4612 }
8943b874 4613
61920122 4614 /* Don't crash on bad data. */
5989a64e
SM
4615 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4616 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4617 {
b98664d3 4618 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4619 " [in module %s]"),
4620 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4621 continue;
4622 }
4623
5989a64e 4624 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4b514bc8
JK
4625 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4626 expansion_notify);
61920122
PA
4627 }
4628}
4629
4b514bc8
JK
4630/* If FILE_MATCHER is non-NULL, set all the
4631 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4632 that match FILE_MATCHER. */
4633
61920122 4634static void
4b514bc8 4635dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4636 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4637 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4638{
4b514bc8 4639 if (file_matcher == NULL)
61920122
PA
4640 return;
4641
4b514bc8
JK
4642 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4643 htab_eq_pointer,
4644 NULL, xcalloc, xfree));
4645 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4646 htab_eq_pointer,
4647 NULL, xcalloc, xfree));
61920122 4648
4b514bc8
JK
4649 /* The rule is CUs specify all the files, including those used by
4650 any TU, so there's no need to scan TUs here. */
61920122 4651
5989a64e 4652 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4653 {
927aa2e7
JK
4654 QUIT;
4655
4656 per_cu->v.quick->mark = 0;
4657
4658 /* We only need to look at symtabs not already expanded. */
af758d11 4659 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4660 continue;
4661
b76e467d 4662 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4663 if (file_data == NULL)
4664 continue;
4665
4666 if (htab_find (visited_not_found.get (), file_data) != NULL)
4667 continue;
4668 else if (htab_find (visited_found.get (), file_data) != NULL)
4669 {
4670 per_cu->v.quick->mark = 1;
4671 continue;
4672 }
4673
b76e467d 4674 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4675 {
4676 const char *this_real_name;
4677
4678 if (file_matcher (file_data->file_names[j], false))
4679 {
4680 per_cu->v.quick->mark = 1;
4681 break;
4682 }
4683
4684 /* Before we invoke realpath, which can get expensive when many
4685 files are involved, do a quick comparison of the basenames. */
4686 if (!basenames_may_differ
4687 && !file_matcher (lbasename (file_data->file_names[j]),
4688 true))
4689 continue;
4690
45940949
TT
4691 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4692 file_data, j);
927aa2e7
JK
4693 if (file_matcher (this_real_name, false))
4694 {
4695 per_cu->v.quick->mark = 1;
4696 break;
4697 }
4698 }
4699
b76e467d
SM
4700 void **slot = htab_find_slot (per_cu->v.quick->mark
4701 ? visited_found.get ()
4702 : visited_not_found.get (),
4703 file_data, INSERT);
927aa2e7
JK
4704 *slot = file_data;
4705 }
4706}
4707
4708static void
4709dw2_expand_symtabs_matching
4710 (struct objfile *objfile,
4711 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4712 const lookup_name_info *lookup_name,
927aa2e7
JK
4713 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4714 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4715 enum search_domain kind)
4716{
ed2dc618
SM
4717 struct dwarf2_per_objfile *dwarf2_per_objfile
4718 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4719
4720 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4721 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4722 return;
4723
ed2dc618 4724 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4725
c1a66c06
TV
4726 if (symbol_matcher == NULL && lookup_name == NULL)
4727 {
5989a64e 4728 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4729 {
4730 QUIT;
4731
4732 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4733 expansion_notify);
4734 }
4735 return;
4736 }
4737
5989a64e 4738 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4739
c1a66c06 4740 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4741 symbol_matcher,
4742 kind, [&] (offset_type idx)
4743 {
ed2dc618 4744 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4745 expansion_notify, kind);
3b00ef10 4746 return true;
927aa2e7
JK
4747 });
4748}
4749
4750/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4751 symtab. */
4752
4753static struct compunit_symtab *
4754recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4755 CORE_ADDR pc)
4756{
4757 int i;
4758
4759 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4760 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4761 return cust;
4762
4763 if (cust->includes == NULL)
4764 return NULL;
4765
4766 for (i = 0; cust->includes[i]; ++i)
4767 {
4768 struct compunit_symtab *s = cust->includes[i];
4769
4770 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4771 if (s != NULL)
4772 return s;
4773 }
4774
4775 return NULL;
4776}
4777
4778static struct compunit_symtab *
4779dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4780 struct bound_minimal_symbol msymbol,
4781 CORE_ADDR pc,
4782 struct obj_section *section,
4783 int warn_if_readin)
4784{
4785 struct dwarf2_per_cu_data *data;
4786 struct compunit_symtab *result;
4787
d320c2b5 4788 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4789 return NULL;
4790
b3b3bada 4791 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4792 data = (struct dwarf2_per_cu_data *) addrmap_find
4793 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4794 if (!data)
4795 return NULL;
4796
af758d11
SM
4797 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4798 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4799 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4800 paddress (objfile->arch (), pc));
927aa2e7
JK
4801
4802 result
58f0c718
TT
4803 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4804 false),
927aa2e7
JK
4805 pc);
4806 gdb_assert (result != NULL);
4807 return result;
4808}
4809
4810static void
4811dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4812 void *data, int need_fullname)
4813{
ed2dc618
SM
4814 struct dwarf2_per_objfile *dwarf2_per_objfile
4815 = get_dwarf2_per_objfile (objfile);
927aa2e7 4816
5989a64e 4817 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4818 {
5989a64e 4819 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4820
4821 htab_up visited (htab_create_alloc (10,
4822 htab_hash_pointer, htab_eq_pointer,
4823 NULL, xcalloc, xfree));
4824
4825 /* The rule is CUs specify all the files, including those used
4826 by any TU, so there's no need to scan TUs here. We can
4827 ignore file names coming from already-expanded CUs. */
4828
5989a64e 4829 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4830 {
af758d11 4831 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4832 {
4833 void **slot = htab_find_slot (visited.get (),
4834 per_cu->v.quick->file_names,
4835 INSERT);
4836
4837 *slot = per_cu->v.quick->file_names;
4838 }
4839 }
4840
5989a64e 4841 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4842 {
927aa2e7 4843 /* We only need to look at symtabs not already expanded. */
af758d11 4844 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4845 continue;
4846
b76e467d 4847 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4848 if (file_data == NULL)
4849 continue;
4850
b76e467d 4851 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4852 if (*slot)
4853 {
4854 /* Already visited. */
4855 continue;
4856 }
4857 *slot = file_data;
4858
4859 for (int j = 0; j < file_data->num_file_names; ++j)
4860 {
4861 const char *filename = file_data->file_names[j];
5989a64e 4862 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4863 }
4864 }
4865 }
4866
5989a64e 4867 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4868 {
4869 gdb::unique_xmalloc_ptr<char> this_real_name;
4870
4871 if (need_fullname)
4872 this_real_name = gdb_realpath (filename);
4873 (*fun) (filename, this_real_name.get (), data);
4874 });
4875}
4876
4877static int
4878dw2_has_symbols (struct objfile *objfile)
4879{
4880 return 1;
4881}
4882
4883const struct quick_symbol_functions dwarf2_gdb_index_functions =
4884{
4885 dw2_has_symbols,
4886 dw2_find_last_source_symtab,
4887 dw2_forget_cached_source_info,
4888 dw2_map_symtabs_matching_filename,
4889 dw2_lookup_symbol,
d3214198 4890 NULL,
927aa2e7
JK
4891 dw2_print_stats,
4892 dw2_dump,
927aa2e7
JK
4893 dw2_expand_symtabs_for_function,
4894 dw2_expand_all_symtabs,
4895 dw2_expand_symtabs_with_fullname,
4896 dw2_map_matching_symbols,
4897 dw2_expand_symtabs_matching,
4898 dw2_find_pc_sect_compunit_symtab,
4899 NULL,
4900 dw2_map_symbol_filenames
4901};
4902
4903/* DWARF-5 debug_names reader. */
4904
4905/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4906static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4907
4908/* A helper function that reads the .debug_names section in SECTION
4909 and fills in MAP. FILENAME is the name of the file containing the
4910 section; it is used for error reporting.
4911
4912 Returns true if all went well, false otherwise. */
4913
4914static bool
4915read_debug_names_from_section (struct objfile *objfile,
4916 const char *filename,
4917 struct dwarf2_section_info *section,
4918 mapped_debug_names &map)
4919{
96b79293 4920 if (section->empty ())
927aa2e7
JK
4921 return false;
4922
4923 /* Older elfutils strip versions could keep the section in the main
4924 executable while splitting it for the separate debug info file. */
96b79293 4925 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4926 return false;
4927
96b79293 4928 section->read (objfile);
927aa2e7 4929
08feed99 4930 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4931
4932 const gdb_byte *addr = section->buffer;
4933
96b79293 4934 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4935
4936 unsigned int bytes_read;
4937 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4938 addr += bytes_read;
4939
4940 map.dwarf5_is_dwarf64 = bytes_read != 4;
4941 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4942 if (bytes_read + length != section->size)
4943 {
4944 /* There may be multiple per-CU indices. */
4945 warning (_("Section .debug_names in %s length %s does not match "
4946 "section length %s, ignoring .debug_names."),
4947 filename, plongest (bytes_read + length),
4948 pulongest (section->size));
4949 return false;
4950 }
4951
4952 /* The version number. */
4953 uint16_t version = read_2_bytes (abfd, addr);
4954 addr += 2;
4955 if (version != 5)
4956 {
4957 warning (_("Section .debug_names in %s has unsupported version %d, "
4958 "ignoring .debug_names."),
4959 filename, version);
4960 return false;
4961 }
4962
4963 /* Padding. */
4964 uint16_t padding = read_2_bytes (abfd, addr);
4965 addr += 2;
4966 if (padding != 0)
4967 {
4968 warning (_("Section .debug_names in %s has unsupported padding %d, "
4969 "ignoring .debug_names."),
4970 filename, padding);
4971 return false;
4972 }
4973
4974 /* comp_unit_count - The number of CUs in the CU list. */
4975 map.cu_count = read_4_bytes (abfd, addr);
4976 addr += 4;
4977
4978 /* local_type_unit_count - The number of TUs in the local TU
4979 list. */
4980 map.tu_count = read_4_bytes (abfd, addr);
4981 addr += 4;
4982
4983 /* foreign_type_unit_count - The number of TUs in the foreign TU
4984 list. */
4985 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4986 addr += 4;
4987 if (foreign_tu_count != 0)
4988 {
4989 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4990 "ignoring .debug_names."),
4991 filename, static_cast<unsigned long> (foreign_tu_count));
4992 return false;
4993 }
4994
4995 /* bucket_count - The number of hash buckets in the hash lookup
4996 table. */
4997 map.bucket_count = read_4_bytes (abfd, addr);
4998 addr += 4;
4999
5000 /* name_count - The number of unique names in the index. */
5001 map.name_count = read_4_bytes (abfd, addr);
5002 addr += 4;
5003
5004 /* abbrev_table_size - The size in bytes of the abbreviations
5005 table. */
5006 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* augmentation_string_size - The size in bytes of the augmentation
5010 string. This value is rounded up to a multiple of 4. */
5011 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5012 addr += 4;
5013 map.augmentation_is_gdb = ((augmentation_string_size
5014 == sizeof (dwarf5_augmentation))
5015 && memcmp (addr, dwarf5_augmentation,
5016 sizeof (dwarf5_augmentation)) == 0);
5017 augmentation_string_size += (-augmentation_string_size) & 3;
5018 addr += augmentation_string_size;
5019
5020 /* List of CUs */
5021 map.cu_table_reordered = addr;
5022 addr += map.cu_count * map.offset_size;
5023
5024 /* List of Local TUs */
5025 map.tu_table_reordered = addr;
5026 addr += map.tu_count * map.offset_size;
5027
5028 /* Hash Lookup Table */
5029 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5030 addr += map.bucket_count * 4;
5031 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5032 addr += map.name_count * 4;
5033
5034 /* Name Table */
5035 map.name_table_string_offs_reordered = addr;
5036 addr += map.name_count * map.offset_size;
5037 map.name_table_entry_offs_reordered = addr;
5038 addr += map.name_count * map.offset_size;
5039
5040 const gdb_byte *abbrev_table_start = addr;
5041 for (;;)
5042 {
927aa2e7
JK
5043 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5044 addr += bytes_read;
5045 if (index_num == 0)
5046 break;
5047
5048 const auto insertpair
5049 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5050 if (!insertpair.second)
5051 {
5052 warning (_("Section .debug_names in %s has duplicate index %s, "
5053 "ignoring .debug_names."),
5054 filename, pulongest (index_num));
5055 return false;
5056 }
5057 mapped_debug_names::index_val &indexval = insertpair.first->second;
5058 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5059 addr += bytes_read;
5060
5061 for (;;)
5062 {
5063 mapped_debug_names::index_val::attr attr;
5064 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5065 addr += bytes_read;
5066 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5067 addr += bytes_read;
5068 if (attr.form == DW_FORM_implicit_const)
5069 {
5070 attr.implicit_const = read_signed_leb128 (abfd, addr,
5071 &bytes_read);
5072 addr += bytes_read;
5073 }
5074 if (attr.dw_idx == 0 && attr.form == 0)
5075 break;
5076 indexval.attr_vec.push_back (std::move (attr));
5077 }
5078 }
5079 if (addr != abbrev_table_start + abbrev_table_size)
5080 {
5081 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5082 "of size %s vs. written as %u, ignoring .debug_names."),
5083 filename, plongest (addr - abbrev_table_start),
5084 abbrev_table_size);
927aa2e7
JK
5085 return false;
5086 }
5087 map.entry_pool = addr;
5088
5089 return true;
5090}
5091
5092/* A helper for create_cus_from_debug_names that handles the MAP's CU
5093 list. */
5094
5095static void
ed2dc618 5096create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5097 const mapped_debug_names &map,
5098 dwarf2_section_info &section,
b76e467d 5099 bool is_dwz)
927aa2e7 5100{
3ee6bb11
TV
5101 if (!map.augmentation_is_gdb)
5102 {
5103 for (uint32_t i = 0; i < map.cu_count; ++i)
5104 {
5105 sect_offset sect_off
5106 = (sect_offset) (extract_unsigned_integer
5107 (map.cu_table_reordered + i * map.offset_size,
5108 map.offset_size,
5109 map.dwarf5_byte_order));
5110 /* We don't know the length of the CU, because the CU list in a
5111 .debug_names index can be incomplete, so we can't use the start of
5112 the next CU as end of this CU. We create the CUs here with length 0,
5113 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5114 dwarf2_per_cu_data *per_cu
5115 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5116 sect_off, 0);
5989a64e 5117 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5118 }
5119 }
5120
927aa2e7
JK
5121 sect_offset sect_off_prev;
5122 for (uint32_t i = 0; i <= map.cu_count; ++i)
5123 {
5124 sect_offset sect_off_next;
5125 if (i < map.cu_count)
5126 {
5127 sect_off_next
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 }
5133 else
5134 sect_off_next = (sect_offset) section.size;
5135 if (i >= 1)
5136 {
5137 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5138 dwarf2_per_cu_data *per_cu
ed2dc618 5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5140 sect_off_prev, length);
5989a64e 5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5142 }
5143 sect_off_prev = sect_off_next;
5144 }
5145}
5146
5147/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5148 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5149
5150static void
ed2dc618 5151create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5152 const mapped_debug_names &map,
5153 const mapped_debug_names &dwz_map)
5154{
5989a64e
SM
5155 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5156 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5157
ed2dc618 5158 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5159 dwarf2_per_objfile->per_bfd->info,
b76e467d 5160 false /* is_dwz */);
927aa2e7
JK
5161
5162 if (dwz_map.cu_count == 0)
5163 return;
5164
ed2dc618
SM
5165 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5166 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5167 true /* is_dwz */);
927aa2e7
JK
5168}
5169
5170/* Read .debug_names. If everything went ok, initialize the "quick"
5171 elements of all the CUs and return true. Otherwise, return false. */
5172
5173static bool
ed2dc618 5174dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5175{
22ca247e
TT
5176 std::unique_ptr<mapped_debug_names> map
5177 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5178 mapped_debug_names dwz_map (dwarf2_per_objfile);
5179 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5180
5181 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5182 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5183 *map))
927aa2e7
JK
5184 return false;
5185
5186 /* Don't use the index if it's empty. */
22ca247e 5187 if (map->name_count == 0)
927aa2e7
JK
5188 return false;
5189
5190 /* If there is a .dwz file, read it so we can get its CU list as
5191 well. */
ed2dc618 5192 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5193 if (dwz != NULL)
5194 {
5195 if (!read_debug_names_from_section (objfile,
00f93c44 5196 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5197 &dwz->debug_names, dwz_map))
5198 {
5199 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5200 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5201 return false;
5202 }
5203 }
5204
22ca247e 5205 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5206
22ca247e 5207 if (map->tu_count != 0)
927aa2e7
JK
5208 {
5209 /* We can only handle a single .debug_types when we have an
5210 index. */
5989a64e 5211 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5212 return false;
5213
5989a64e 5214 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5215
5216 create_signatured_type_table_from_debug_names
5989a64e 5217 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5218 }
5219
ed2dc618 5220 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5221 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5222
5989a64e
SM
5223 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5224 dwarf2_per_objfile->per_bfd->using_index = 1;
5225 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5226 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5227
5228 return true;
5229}
5230
927aa2e7
JK
5231/* Type used to manage iterating over all CUs looking for a symbol for
5232 .debug_names. */
5233
5234class dw2_debug_names_iterator
5235{
5236public:
927aa2e7 5237 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5238 gdb::optional<block_enum> block_index,
5239 domain_enum domain,
927aa2e7 5240 const char *name)
2b79f376 5241 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5242 m_addr (find_vec_in_debug_names (map, name))
5243 {}
5244
5245 dw2_debug_names_iterator (const mapped_debug_names &map,
5246 search_domain search, uint32_t namei)
5247 : m_map (map),
5248 m_search (search),
5249 m_addr (find_vec_in_debug_names (map, namei))
5250 {}
5251
3b00ef10
TT
5252 dw2_debug_names_iterator (const mapped_debug_names &map,
5253 block_enum block_index, domain_enum domain,
5254 uint32_t namei)
5255 : m_map (map), m_block_index (block_index), m_domain (domain),
5256 m_addr (find_vec_in_debug_names (map, namei))
5257 {}
5258
927aa2e7
JK
5259 /* Return the next matching CU or NULL if there are no more. */
5260 dwarf2_per_cu_data *next ();
5261
5262private:
5263 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5264 const char *name);
5265 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5266 uint32_t namei);
5267
5268 /* The internalized form of .debug_names. */
5269 const mapped_debug_names &m_map;
5270
2b79f376
SM
5271 /* If set, only look for symbols that match that block. Valid values are
5272 GLOBAL_BLOCK and STATIC_BLOCK. */
5273 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5274
5275 /* The kind of symbol we're looking for. */
5276 const domain_enum m_domain = UNDEF_DOMAIN;
5277 const search_domain m_search = ALL_DOMAIN;
5278
5279 /* The list of CUs from the index entry of the symbol, or NULL if
5280 not found. */
5281 const gdb_byte *m_addr;
5282};
5283
5284const char *
5285mapped_debug_names::namei_to_name (uint32_t namei) const
5286{
5287 const ULONGEST namei_string_offs
5288 = extract_unsigned_integer ((name_table_string_offs_reordered
5289 + namei * offset_size),
5290 offset_size,
5291 dwarf5_byte_order);
4f44ae6c
TT
5292 return read_indirect_string_at_offset (dwarf2_per_objfile,
5293 namei_string_offs);
927aa2e7
JK
5294}
5295
5296/* Find a slot in .debug_names for the object named NAME. If NAME is
5297 found, return pointer to its pool data. If NAME cannot be found,
5298 return NULL. */
5299
5300const gdb_byte *
5301dw2_debug_names_iterator::find_vec_in_debug_names
5302 (const mapped_debug_names &map, const char *name)
5303{
5304 int (*cmp) (const char *, const char *);
5305
54ee4252 5306 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5307 if (current_language->la_language == language_cplus
5308 || current_language->la_language == language_fortran
5309 || current_language->la_language == language_d)
5310 {
5311 /* NAME is already canonical. Drop any qualifiers as
5312 .debug_names does not contain any. */
5313
5314 if (strchr (name, '(') != NULL)
5315 {
54ee4252 5316 without_params = cp_remove_params (name);
927aa2e7 5317 if (without_params != NULL)
54ee4252 5318 name = without_params.get ();
927aa2e7
JK
5319 }
5320 }
5321
5322 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5323
5324 const uint32_t full_hash = dwarf5_djb_hash (name);
5325 uint32_t namei
5326 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5327 (map.bucket_table_reordered
5328 + (full_hash % map.bucket_count)), 4,
5329 map.dwarf5_byte_order);
5330 if (namei == 0)
5331 return NULL;
5332 --namei;
5333 if (namei >= map.name_count)
5334 {
b98664d3 5335 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5336 "[in module %s]"),
5337 namei, map.name_count,
ed2dc618 5338 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5339 return NULL;
5340 }
5341
5342 for (;;)
5343 {
5344 const uint32_t namei_full_hash
5345 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5346 (map.hash_table_reordered + namei), 4,
5347 map.dwarf5_byte_order);
5348 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5349 return NULL;
5350
5351 if (full_hash == namei_full_hash)
5352 {
5353 const char *const namei_string = map.namei_to_name (namei);
5354
5355#if 0 /* An expensive sanity check. */
5356 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5357 {
b98664d3 5358 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5359 "[in module %s]"),
5360 namei, objfile_name (dwarf2_per_objfile->objfile));
5361 return NULL;
5362 }
5363#endif
5364
5365 if (cmp (namei_string, name) == 0)
5366 {
5367 const ULONGEST namei_entry_offs
5368 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5369 + namei * map.offset_size),
5370 map.offset_size, map.dwarf5_byte_order);
5371 return map.entry_pool + namei_entry_offs;
5372 }
5373 }
5374
5375 ++namei;
5376 if (namei >= map.name_count)
5377 return NULL;
5378 }
5379}
5380
5381const gdb_byte *
5382dw2_debug_names_iterator::find_vec_in_debug_names
5383 (const mapped_debug_names &map, uint32_t namei)
5384{
5385 if (namei >= map.name_count)
5386 {
b98664d3 5387 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5388 "[in module %s]"),
5389 namei, map.name_count,
ed2dc618 5390 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5391 return NULL;
5392 }
5393
5394 const ULONGEST namei_entry_offs
5395 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5396 + namei * map.offset_size),
5397 map.offset_size, map.dwarf5_byte_order);
5398 return map.entry_pool + namei_entry_offs;
5399}
5400
5401/* See dw2_debug_names_iterator. */
5402
5403dwarf2_per_cu_data *
5404dw2_debug_names_iterator::next ()
5405{
5406 if (m_addr == NULL)
5407 return NULL;
5408
ed2dc618
SM
5409 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5410 struct objfile *objfile = dwarf2_per_objfile->objfile;
5411 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5412
5413 again:
5414
5415 unsigned int bytes_read;
5416 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5417 m_addr += bytes_read;
5418 if (abbrev == 0)
5419 return NULL;
5420
5421 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5422 if (indexval_it == m_map.abbrev_map.cend ())
5423 {
b98664d3 5424 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5425 "[in module %s]"),
ed2dc618 5426 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5427 return NULL;
5428 }
5429 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5430 enum class symbol_linkage {
5431 unknown,
5432 static_,
5433 extern_,
23c13d42 5434 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5435 dwarf2_per_cu_data *per_cu = NULL;
5436 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5437 {
5438 ULONGEST ull;
5439 switch (attr.form)
5440 {
5441 case DW_FORM_implicit_const:
5442 ull = attr.implicit_const;
5443 break;
5444 case DW_FORM_flag_present:
5445 ull = 1;
5446 break;
5447 case DW_FORM_udata:
5448 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5449 m_addr += bytes_read;
5450 break;
6dc55ce9 5451 case DW_FORM_ref4:
5452 ull = read_4_bytes (abfd, m_addr);
5453 m_addr += 4;
5454 break;
5455 case DW_FORM_ref8:
5456 ull = read_8_bytes (abfd, m_addr);
5457 m_addr += 8;
5458 break;
5459 case DW_FORM_ref_sig8:
5460 ull = read_8_bytes (abfd, m_addr);
5461 m_addr += 8;
5462 break;
927aa2e7 5463 default:
b98664d3 5464 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5465 dwarf_form_name (attr.form),
ed2dc618 5466 objfile_name (objfile));
927aa2e7
JK
5467 return NULL;
5468 }
5469 switch (attr.dw_idx)
5470 {
5471 case DW_IDX_compile_unit:
5472 /* Don't crash on bad data. */
5989a64e 5473 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5474 {
b98664d3 5475 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5476 " [in module %s]"),
5477 pulongest (ull),
5478 objfile_name (dwarf2_per_objfile->objfile));
5479 continue;
5480 }
5989a64e 5481 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5482 break;
8af5c486
JK
5483 case DW_IDX_type_unit:
5484 /* Don't crash on bad data. */
5989a64e 5485 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5486 {
b98664d3 5487 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5488 " [in module %s]"),
5489 pulongest (ull),
5490 objfile_name (dwarf2_per_objfile->objfile));
5491 continue;
5492 }
5989a64e 5493 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5494 break;
6dc55ce9 5495 case DW_IDX_die_offset:
5496 /* In a per-CU index (as opposed to a per-module index), index
5497 entries without CU attribute implicitly refer to the single CU. */
5498 if (per_cu == NULL)
5989a64e 5499 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5500 break;
927aa2e7
JK
5501 case DW_IDX_GNU_internal:
5502 if (!m_map.augmentation_is_gdb)
5503 break;
23c13d42 5504 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5505 break;
5506 case DW_IDX_GNU_external:
5507 if (!m_map.augmentation_is_gdb)
5508 break;
23c13d42 5509 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5510 break;
5511 }
5512 }
5513
5514 /* Skip if already read in. */
af758d11 5515 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5516 goto again;
5517
5518 /* Check static vs global. */
23c13d42 5519 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5520 {
2b79f376 5521 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5522 const bool symbol_is_static =
5523 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5524 if (want_static != symbol_is_static)
2b79f376 5525 goto again;
927aa2e7
JK
5526 }
5527
5528 /* Match dw2_symtab_iter_next, symbol_kind
5529 and debug_names::psymbol_tag. */
5530 switch (m_domain)
5531 {
5532 case VAR_DOMAIN:
5533 switch (indexval.dwarf_tag)
5534 {
5535 case DW_TAG_variable:
5536 case DW_TAG_subprogram:
5537 /* Some types are also in VAR_DOMAIN. */
5538 case DW_TAG_typedef:
5539 case DW_TAG_structure_type:
5540 break;
5541 default:
5542 goto again;
5543 }
5544 break;
5545 case STRUCT_DOMAIN:
5546 switch (indexval.dwarf_tag)
5547 {
5548 case DW_TAG_typedef:
5549 case DW_TAG_structure_type:
5550 break;
5551 default:
5552 goto again;
5553 }
5554 break;
5555 case LABEL_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case 0:
5559 case DW_TAG_variable:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
59c35742
AB
5565 case MODULE_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case DW_TAG_module:
5569 break;
5570 default:
5571 goto again;
5572 }
5573 break;
927aa2e7
JK
5574 default:
5575 break;
5576 }
5577
5578 /* Match dw2_expand_symtabs_matching, symbol_kind and
5579 debug_names::psymbol_tag. */
5580 switch (m_search)
4b514bc8 5581 {
927aa2e7
JK
5582 case VARIABLES_DOMAIN:
5583 switch (indexval.dwarf_tag)
4b514bc8 5584 {
927aa2e7
JK
5585 case DW_TAG_variable:
5586 break;
5587 default:
5588 goto again;
4b514bc8 5589 }
927aa2e7
JK
5590 break;
5591 case FUNCTIONS_DOMAIN:
5592 switch (indexval.dwarf_tag)
4b514bc8 5593 {
927aa2e7
JK
5594 case DW_TAG_subprogram:
5595 break;
5596 default:
5597 goto again;
4b514bc8 5598 }
927aa2e7
JK
5599 break;
5600 case TYPES_DOMAIN:
5601 switch (indexval.dwarf_tag)
5602 {
5603 case DW_TAG_typedef:
5604 case DW_TAG_structure_type:
5605 break;
5606 default:
5607 goto again;
5608 }
5609 break;
59c35742
AB
5610 case MODULES_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_module:
5614 break;
5615 default:
5616 goto again;
5617 }
927aa2e7
JK
5618 default:
5619 break;
4b514bc8 5620 }
927aa2e7
JK
5621
5622 return per_cu;
4b514bc8 5623}
61920122 5624
927aa2e7 5625static struct compunit_symtab *
c7f839cb 5626dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5627 const char *name, domain_enum domain)
4b514bc8 5628{
ed2dc618
SM
5629 struct dwarf2_per_objfile *dwarf2_per_objfile
5630 = get_dwarf2_per_objfile (objfile);
61920122 5631
5989a64e 5632 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5633 if (!mapp)
61920122 5634 {
927aa2e7
JK
5635 /* index is NULL if OBJF_READNOW. */
5636 return NULL;
5637 }
5638 const auto &map = *mapp;
9291a0cd 5639
2b79f376 5640 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5641
927aa2e7
JK
5642 struct compunit_symtab *stab_best = NULL;
5643 struct dwarf2_per_cu_data *per_cu;
5644 while ((per_cu = iter.next ()) != NULL)
5645 {
5646 struct symbol *sym, *with_opaque = NULL;
58f0c718 5647 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5648 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5649 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5650
927aa2e7
JK
5651 sym = block_find_symbol (block, name, domain,
5652 block_find_non_opaque_type_preferred,
5653 &with_opaque);
9703b513 5654
927aa2e7
JK
5655 /* Some caution must be observed with overloaded functions and
5656 methods, since the index will not contain any overload
5657 information (but NAME might contain it). */
a3ec0bb1 5658
927aa2e7 5659 if (sym != NULL
987012b8 5660 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5661 return stab;
5662 if (with_opaque != NULL
987012b8 5663 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5664 stab_best = stab;
9703b513 5665
927aa2e7 5666 /* Keep looking through other CUs. */
9703b513
TT
5667 }
5668
927aa2e7 5669 return stab_best;
9703b513
TT
5670}
5671
927aa2e7
JK
5672/* This dumps minimal information about .debug_names. It is called
5673 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5674 uses this to verify that .debug_names has been loaded. */
9291a0cd 5675
927aa2e7
JK
5676static void
5677dw2_debug_names_dump (struct objfile *objfile)
5678{
ed2dc618
SM
5679 struct dwarf2_per_objfile *dwarf2_per_objfile
5680 = get_dwarf2_per_objfile (objfile);
5681
5989a64e 5682 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5683 printf_filtered (".debug_names:");
5989a64e 5684 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5685 printf_filtered (" exists\n");
5686 else
5687 printf_filtered (" faked for \"readnow\"\n");
5688 printf_filtered ("\n");
9291a0cd
TT
5689}
5690
9291a0cd 5691static void
927aa2e7
JK
5692dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5693 const char *func_name)
9291a0cd 5694{
ed2dc618
SM
5695 struct dwarf2_per_objfile *dwarf2_per_objfile
5696 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5697
5989a64e
SM
5698 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5699 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5700 {
5989a64e 5701 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5702
2b79f376 5703 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5704
927aa2e7
JK
5705 struct dwarf2_per_cu_data *per_cu;
5706 while ((per_cu = iter.next ()) != NULL)
58f0c718 5707 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5708 }
5709}
24c79950 5710
3b00ef10
TT
5711static void
5712dw2_debug_names_map_matching_symbols
5713 (struct objfile *objfile,
5714 const lookup_name_info &name, domain_enum domain,
5715 int global,
5716 gdb::function_view<symbol_found_callback_ftype> callback,
5717 symbol_compare_ftype *ordered_compare)
5718{
5719 struct dwarf2_per_objfile *dwarf2_per_objfile
5720 = get_dwarf2_per_objfile (objfile);
5721
5722 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5723 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5724 return;
5725
5989a64e 5726 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5727 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5728
5729 const char *match_name = name.ada ().lookup_name ().c_str ();
5730 auto matcher = [&] (const char *symname)
5731 {
5732 if (ordered_compare == nullptr)
5733 return true;
5734 return ordered_compare (symname, match_name) == 0;
5735 };
5736
5737 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5738 [&] (offset_type namei)
5739 {
5740 /* The name was matched, now expand corresponding CUs that were
5741 marked. */
5742 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5743
5744 struct dwarf2_per_cu_data *per_cu;
5745 while ((per_cu = iter.next ()) != NULL)
5746 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5747 return true;
5748 });
5749
5750 /* It's a shame we couldn't do this inside the
5751 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5752 that have already been expanded. Instead, this loop matches what
5753 the psymtab code does. */
5989a64e 5754 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5755 {
af758d11
SM
5756 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5757 if (symtab != nullptr)
3b00ef10
TT
5758 {
5759 const struct block *block
af758d11 5760 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5761 if (!iterate_over_symbols_terminated (block, name,
5762 domain, callback))
5763 break;
5764 }
5765 }
5766}
5767
927aa2e7
JK
5768static void
5769dw2_debug_names_expand_symtabs_matching
5770 (struct objfile *objfile,
5771 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5772 const lookup_name_info *lookup_name,
927aa2e7
JK
5773 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5774 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5775 enum search_domain kind)
5776{
ed2dc618
SM
5777 struct dwarf2_per_objfile *dwarf2_per_objfile
5778 = get_dwarf2_per_objfile (objfile);
9291a0cd 5779
927aa2e7 5780 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5781 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5782 return;
9291a0cd 5783
ed2dc618 5784 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5785
c1a66c06
TV
5786 if (symbol_matcher == NULL && lookup_name == NULL)
5787 {
5989a64e 5788 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5789 {
5790 QUIT;
5791
5792 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5793 expansion_notify);
5794 }
5795 return;
5796 }
5797
5989a64e 5798 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5799
c1a66c06 5800 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5801 symbol_matcher,
5802 kind, [&] (offset_type namei)
927aa2e7 5803 {
927aa2e7
JK
5804 /* The name was matched, now expand corresponding CUs that were
5805 marked. */
5806 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5807
927aa2e7
JK
5808 struct dwarf2_per_cu_data *per_cu;
5809 while ((per_cu = iter.next ()) != NULL)
5810 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5811 expansion_notify);
3b00ef10 5812 return true;
44ed8f3e 5813 });
9291a0cd
TT
5814}
5815
927aa2e7 5816const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5817{
5818 dw2_has_symbols,
5819 dw2_find_last_source_symtab,
5820 dw2_forget_cached_source_info,
f8eba3c6 5821 dw2_map_symtabs_matching_filename,
927aa2e7 5822 dw2_debug_names_lookup_symbol,
d3214198 5823 NULL,
9291a0cd 5824 dw2_print_stats,
927aa2e7 5825 dw2_debug_names_dump,
927aa2e7 5826 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5827 dw2_expand_all_symtabs,
652a8996 5828 dw2_expand_symtabs_with_fullname,
3b00ef10 5829 dw2_debug_names_map_matching_symbols,
927aa2e7 5830 dw2_debug_names_expand_symtabs_matching,
43f3e411 5831 dw2_find_pc_sect_compunit_symtab,
71a3c369 5832 NULL,
9291a0cd
TT
5833 dw2_map_symbol_filenames
5834};
5835
4485a1c1 5836/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5837 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5838
5839template <typename T>
5840static gdb::array_view<const gdb_byte>
5841get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5842{
5843 dwarf2_section_info *section = &section_owner->gdb_index;
5844
96b79293 5845 if (section->empty ())
4485a1c1
SM
5846 return {};
5847
5848 /* Older elfutils strip versions could keep the section in the main
5849 executable while splitting it for the separate debug info file. */
96b79293 5850 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5851 return {};
5852
96b79293 5853 section->read (obj);
4485a1c1 5854
8bebfcda
PA
5855 /* dwarf2_section_info::size is a bfd_size_type, while
5856 gdb::array_view works with size_t. On 32-bit hosts, with
5857 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5858 is 32-bit. So we need an explicit narrowing conversion here.
5859 This is fine, because it's impossible to allocate or mmap an
5860 array/buffer larger than what size_t can represent. */
5861 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5862}
5863
87d6a7aa
SM
5864/* Lookup the index cache for the contents of the index associated to
5865 DWARF2_OBJ. */
5866
5867static gdb::array_view<const gdb_byte>
5989a64e 5868get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5869{
5870 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5871 if (build_id == nullptr)
5872 return {};
5873
5874 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5875 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5876}
5877
5878/* Same as the above, but for DWZ. */
5879
5880static gdb::array_view<const gdb_byte>
5881get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5882{
5883 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5884 if (build_id == nullptr)
5885 return {};
5886
5887 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5888}
5889
3c0aa29a 5890/* See symfile.h. */
9291a0cd 5891
3c0aa29a
PA
5892bool
5893dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5894{
ed2dc618
SM
5895 struct dwarf2_per_objfile *dwarf2_per_objfile
5896 = get_dwarf2_per_objfile (objfile);
5897
9291a0cd
TT
5898 /* If we're about to read full symbols, don't bother with the
5899 indices. In this case we also don't care if some other debug
5900 format is making psymtabs, because they are all about to be
5901 expanded anyway. */
5902 if ((objfile->flags & OBJF_READNOW))
5903 {
5989a64e 5904 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5905 create_all_comp_units (dwarf2_per_objfile);
5906 create_all_type_units (dwarf2_per_objfile);
5989a64e 5907 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5908 = create_quick_file_names_table
5989a64e 5909 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5910 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5911
5989a64e
SM
5912 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5913 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5914 {
5989a64e 5915 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5916
5989a64e 5917 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5918 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5919 }
5920
5921 /* Return 1 so that gdb sees the "quick" functions. However,
5922 these functions will be no-ops because we will have expanded
5923 all symtabs. */
3c0aa29a
PA
5924 *index_kind = dw_index_kind::GDB_INDEX;
5925 return true;
9291a0cd
TT
5926 }
5927
ed2dc618 5928 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5929 {
5930 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5931 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5932 return true;
5933 }
927aa2e7 5934
4485a1c1 5935 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5936 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5937 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5938 {
5939 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5940 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5941 return true;
5942 }
9291a0cd 5943
87d6a7aa
SM
5944 /* ... otherwise, try to find the index in the index cache. */
5945 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5946 get_gdb_index_contents_from_cache,
5947 get_gdb_index_contents_from_cache_dwz))
5948 {
5949 global_index_cache.hit ();
5950 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5951 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5952 return true;
5953 }
5954
5955 global_index_cache.miss ();
3c0aa29a 5956 return false;
9291a0cd
TT
5957}
5958
5959\f
5960
dce234bc
PP
5961/* Build a partial symbol table. */
5962
5963void
f29dff0a 5964dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5965{
ed2dc618
SM
5966 struct dwarf2_per_objfile *dwarf2_per_objfile
5967 = get_dwarf2_per_objfile (objfile);
c9bf0622 5968
6eee24ce 5969 init_psymbol_list (objfile, 1024);
c906108c 5970
a70b8144 5971 try
c9bf0622
TT
5972 {
5973 /* This isn't really ideal: all the data we allocate on the
5974 objfile's obstack is still uselessly kept around. However,
5975 freeing it seems unsafe. */
906768f9 5976 psymtab_discarder psymtabs (objfile);
ed2dc618 5977 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5978 psymtabs.keep ();
87d6a7aa 5979
af758d11
SM
5980 dwarf2_per_objfile->resize_symtabs ();
5981
87d6a7aa
SM
5982 /* (maybe) store an index in the cache. */
5983 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5984 }
230d2906 5985 catch (const gdb_exception_error &except)
492d29ea
PA
5986 {
5987 exception_print (gdb_stderr, except);
5988 }
c906108c 5989}
c906108c 5990
3b80fe9b
DE
5991/* Find the base address of the compilation unit for range lists and
5992 location lists. It will normally be specified by DW_AT_low_pc.
5993 In DWARF-3 draft 4, the base address could be overridden by
5994 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5995 compilation units with discontinuous ranges. */
5996
5997static void
5998dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5999{
6000 struct attribute *attr;
6001
2b24b6e4 6002 cu->base_address.reset ();
3b80fe9b
DE
6003
6004 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6005 if (attr != nullptr)
2b24b6e4 6006 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6007 else
6008 {
6009 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6010 if (attr != nullptr)
2b24b6e4 6011 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6012 }
6013}
6014
36586728
TT
6015/* Helper function that returns the proper abbrev section for
6016 THIS_CU. */
6017
6018static struct dwarf2_section_info *
6019get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6020{
6021 struct dwarf2_section_info *abbrev;
ed2dc618 6022 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6023
6024 if (this_cu->is_dwz)
ed2dc618 6025 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728 6026 else
5989a64e 6027 abbrev = &dwarf2_per_objfile->per_bfd->abbrev;
36586728
TT
6028
6029 return abbrev;
6030}
6031
f4dc4d17
DE
6032/* Fetch the abbreviation table offset from a comp or type unit header. */
6033
6034static sect_offset
ed2dc618
SM
6035read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6036 struct dwarf2_section_info *section,
9c541725 6037 sect_offset sect_off)
f4dc4d17 6038{
96b79293 6039 bfd *abfd = section->get_bfd_owner ();
d521ce57 6040 const gdb_byte *info_ptr;
ac298888 6041 unsigned int initial_length_size, offset_size;
43988095 6042 uint16_t version;
f4dc4d17 6043
96b79293 6044 section->read (dwarf2_per_objfile->objfile);
9c541725 6045 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6046 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6047 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6048 info_ptr += initial_length_size;
6049
6050 version = read_2_bytes (abfd, info_ptr);
6051 info_ptr += 2;
6052 if (version >= 5)
6053 {
6054 /* Skip unit type and address size. */
6055 info_ptr += 2;
6056 }
6057
24aa364d 6058 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6059}
6060
b83470bf
TT
6061/* A partial symtab that is used only for include files. */
6062struct dwarf2_include_psymtab : public partial_symtab
6063{
6064 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6065 : partial_symtab (filename, objfile)
6066 {
6067 }
6068
6069 void read_symtab (struct objfile *objfile) override
6070 {
194d088f
TV
6071 /* It's an include file, no symbols to read for it.
6072 Everything is in the includer symtab. */
6073
6074 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6075 expansion of the includer psymtab. We use the dependencies[0] field to
6076 model the includer. But if we go the regular route of calling
6077 expand_psymtab here, and having expand_psymtab call expand_dependencies
6078 to expand the includer, we'll only use expand_psymtab on the includer
6079 (making it a non-toplevel psymtab), while if we expand the includer via
6080 another path, we'll use read_symtab (making it a toplevel psymtab).
6081 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6082 psymtab, and trigger read_symtab on the includer here directly. */
6083 includer ()->read_symtab (objfile);
b83470bf
TT
6084 }
6085
6086 void expand_psymtab (struct objfile *objfile) override
6087 {
194d088f
TV
6088 /* This is not called by read_symtab, and should not be called by any
6089 expand_dependencies. */
6090 gdb_assert (false);
b83470bf
TT
6091 }
6092
5717c425 6093 bool readin_p (struct objfile *objfile) const override
b83470bf 6094 {
5717c425 6095 return includer ()->readin_p (objfile);
b83470bf
TT
6096 }
6097
5717c425 6098 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6099 {
6100 return nullptr;
6101 }
6102
6103private:
194d088f
TV
6104 partial_symtab *includer () const
6105 {
6106 /* An include psymtab has exactly one dependency: the psymtab that
6107 includes it. */
6108 gdb_assert (this->number_of_dependencies == 1);
6109 return this->dependencies[0];
6110 }
b83470bf
TT
6111};
6112
aaa75496
JB
6113/* Allocate a new partial symtab for file named NAME and mark this new
6114 partial symtab as being an include of PST. */
6115
6116static void
891813be 6117dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6118 struct objfile *objfile)
6119{
b83470bf 6120 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6121
fbd9ab74 6122 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6123 subpst->dirname = pst->dirname;
fbd9ab74 6124
a9342b62 6125 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6126 subpst->dependencies[0] = pst;
6127 subpst->number_of_dependencies = 1;
aaa75496
JB
6128}
6129
6130/* Read the Line Number Program data and extract the list of files
6131 included by the source file represented by PST. Build an include
d85a05f0 6132 partial symtab for each of these included files. */
aaa75496
JB
6133
6134static void
6135dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6136 struct die_info *die,
891813be 6137 dwarf2_psymtab *pst)
aaa75496 6138{
fff8551c 6139 line_header_up lh;
d85a05f0 6140 struct attribute *attr;
aaa75496 6141
d85a05f0 6142 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6143 if (attr != nullptr)
9c541725 6144 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6145 if (lh == NULL)
6146 return; /* No linetable, so no includes. */
6147
79748972
TT
6148 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6149 that we pass in the raw text_low here; that is ok because we're
6150 only decoding the line table to make include partial symtabs, and
6151 so the addresses aren't really used. */
4ae976d1 6152 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6153 pst->raw_text_low (), 1);
aaa75496
JB
6154}
6155
348e048f 6156static hashval_t
52dc124a 6157hash_signatured_type (const void *item)
348e048f 6158{
9a3c8263
SM
6159 const struct signatured_type *sig_type
6160 = (const struct signatured_type *) item;
9a619af0 6161
348e048f 6162 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6163 return sig_type->signature;
348e048f
DE
6164}
6165
6166static int
52dc124a 6167eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6168{
9a3c8263
SM
6169 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6170 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6171
348e048f
DE
6172 return lhs->signature == rhs->signature;
6173}
6174
1fd400ff
TT
6175/* Allocate a hash table for signatured types. */
6176
b0b6a987 6177static htab_up
298e9637 6178allocate_signatured_type_table ()
1fd400ff 6179{
b0b6a987
TT
6180 return htab_up (htab_create_alloc (41,
6181 hash_signatured_type,
6182 eq_signatured_type,
6183 NULL, xcalloc, xfree));
1fd400ff
TT
6184}
6185
d467dd73 6186/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6187
6188static int
d467dd73 6189add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6190{
9a3c8263 6191 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6192 std::vector<signatured_type *> *all_type_units
6193 = (std::vector<signatured_type *> *) datum;
1fd400ff 6194
b2bdb8cf 6195 all_type_units->push_back (sigt);
1fd400ff
TT
6196
6197 return 1;
6198}
6199
78d4d2c5 6200/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6201 and fill them into TYPES_HTAB. It will process only type units,
6202 therefore DW_UT_type. */
c88ee1f0 6203
78d4d2c5 6204static void
ed2dc618
SM
6205create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6206 struct dwo_file *dwo_file,
b0b6a987 6207 dwarf2_section_info *section, htab_up &types_htab,
43988095 6208 rcuh_kind section_kind)
348e048f 6209{
3019eac3 6210 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6211 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6212 bfd *abfd;
6213 const gdb_byte *info_ptr, *end_ptr;
348e048f 6214
4bdcc0c1
DE
6215 abbrev_section = (dwo_file != NULL
6216 ? &dwo_file->sections.abbrev
5989a64e 6217 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6218
b4f54984 6219 if (dwarf_read_debug)
43988095 6220 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6221 section->get_name (),
6222 abbrev_section->get_file_name ());
09406207 6223
96b79293 6224 section->read (objfile);
78d4d2c5 6225 info_ptr = section->buffer;
348e048f 6226
78d4d2c5
JK
6227 if (info_ptr == NULL)
6228 return;
348e048f 6229
78d4d2c5
JK
6230 /* We can't set abfd until now because the section may be empty or
6231 not present, in which case the bfd is unknown. */
96b79293 6232 abfd = section->get_bfd_owner ();
348e048f 6233
c0ab21c2
TT
6234 /* We don't use cutu_reader here because we don't need to read
6235 any dies: the signature is in the header. */
3019eac3 6236
78d4d2c5
JK
6237 end_ptr = info_ptr + section->size;
6238 while (info_ptr < end_ptr)
6239 {
78d4d2c5
JK
6240 struct signatured_type *sig_type;
6241 struct dwo_unit *dwo_tu;
6242 void **slot;
6243 const gdb_byte *ptr = info_ptr;
6244 struct comp_unit_head header;
6245 unsigned int length;
8b70b953 6246
9c541725 6247 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6248
a49dd8dd
JK
6249 /* Initialize it due to a false compiler warning. */
6250 header.signature = -1;
9c541725 6251 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6252
78d4d2c5
JK
6253 /* We need to read the type's signature in order to build the hash
6254 table, but we don't need anything else just yet. */
348e048f 6255
ed2dc618 6256 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6257 abbrev_section, ptr, section_kind);
348e048f 6258
4057dfde 6259 length = header.get_length ();
6caca83c 6260
78d4d2c5
JK
6261 /* Skip dummy type units. */
6262 if (ptr >= info_ptr + length
43988095
JK
6263 || peek_abbrev_code (abfd, ptr) == 0
6264 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6265 {
6266 info_ptr += length;
6267 continue;
6268 }
dee91e82 6269
78d4d2c5
JK
6270 if (types_htab == NULL)
6271 {
6272 if (dwo_file)
298e9637 6273 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6274 else
298e9637 6275 types_htab = allocate_signatured_type_table ();
78d4d2c5 6276 }
8b70b953 6277
78d4d2c5
JK
6278 if (dwo_file)
6279 {
6280 sig_type = NULL;
5989a64e 6281 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6282 struct dwo_unit);
6283 dwo_tu->dwo_file = dwo_file;
43988095 6284 dwo_tu->signature = header.signature;
9c541725 6285 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6286 dwo_tu->section = section;
9c541725 6287 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6288 dwo_tu->length = length;
6289 }
6290 else
6291 {
6292 /* N.B.: type_offset is not usable if this type uses a DWO file.
6293 The real type_offset is in the DWO file. */
6294 dwo_tu = NULL;
5989a64e 6295 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6296 sig_type->signature = header.signature;
9c541725 6297 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6298 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6299 sig_type->per_cu.is_debug_types = 1;
6300 sig_type->per_cu.section = section;
9c541725 6301 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6302 sig_type->per_cu.length = length;
6303 }
6304
b0b6a987 6305 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6306 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6307 INSERT);
6308 gdb_assert (slot != NULL);
6309 if (*slot != NULL)
6310 {
9c541725 6311 sect_offset dup_sect_off;
0349ea22 6312
3019eac3
DE
6313 if (dwo_file)
6314 {
78d4d2c5
JK
6315 const struct dwo_unit *dup_tu
6316 = (const struct dwo_unit *) *slot;
6317
9c541725 6318 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6319 }
6320 else
6321 {
78d4d2c5
JK
6322 const struct signatured_type *dup_tu
6323 = (const struct signatured_type *) *slot;
6324
9c541725 6325 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6326 }
8b70b953 6327
b98664d3 6328 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6329 " the entry at offset %s, signature %s"),
6330 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6331 hex_string (header.signature));
78d4d2c5
JK
6332 }
6333 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6334
78d4d2c5 6335 if (dwarf_read_debug > 1)
9d8780f0
SM
6336 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6337 sect_offset_str (sect_off),
43988095 6338 hex_string (header.signature));
3019eac3 6339
78d4d2c5
JK
6340 info_ptr += length;
6341 }
6342}
3019eac3 6343
78d4d2c5
JK
6344/* Create the hash table of all entries in the .debug_types
6345 (or .debug_types.dwo) section(s).
6346 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6347 otherwise it is NULL.
b3c8eb43 6348
78d4d2c5 6349 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6350
78d4d2c5 6351 Note: This function processes DWO files only, not DWP files. */
348e048f 6352
78d4d2c5 6353static void
ed2dc618
SM
6354create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6355 struct dwo_file *dwo_file,
fd5866f6 6356 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6357 htab_up &types_htab)
78d4d2c5 6358{
fd5866f6
SM
6359 for (dwarf2_section_info &section : type_sections)
6360 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6361 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6362}
6363
6364/* Create the hash table of all entries in the .debug_types section,
6365 and initialize all_type_units.
6366 The result is zero if there is an error (e.g. missing .debug_types section),
6367 otherwise non-zero. */
6368
6369static int
ed2dc618 6370create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6371{
b0b6a987 6372 htab_up types_htab;
3019eac3 6373
ed2dc618 6374 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6375 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6376 rcuh_kind::COMPILE);
ed2dc618 6377 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6378 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6379 if (types_htab == NULL)
6380 {
5989a64e 6381 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6382 return 0;
6383 }
6384
5989a64e 6385 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6386
5989a64e
SM
6387 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6388 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6389 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6390
5989a64e 6391 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6392 add_signatured_type_cu_to_table,
5989a64e 6393 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6394
348e048f
DE
6395 return 1;
6396}
6397
5989a64e 6398/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6399 If SLOT is non-NULL, it is the entry to use in the hash table.
6400 Otherwise we find one. */
6401
6402static struct signatured_type *
ed2dc618
SM
6403add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6404 void **slot)
6aa5f3a6 6405{
5989a64e
SM
6406 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6407 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6408 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6409
5989a64e 6410 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6411
af758d11
SM
6412 dwarf2_per_objfile->resize_symtabs ();
6413
5989a64e 6414 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6415 sig_type->signature = sig;
6416 sig_type->per_cu.is_debug_types = 1;
5989a64e 6417 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6418 {
6419 sig_type->per_cu.v.quick =
5989a64e 6420 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6421 struct dwarf2_per_cu_quick_data);
6422 }
6423
6424 if (slot == NULL)
6425 {
5989a64e 6426 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6427 sig_type, INSERT);
6428 }
6429 gdb_assert (*slot == NULL);
6430 *slot = sig_type;
6431 /* The rest of sig_type must be filled in by the caller. */
6432 return sig_type;
6433}
6434
a2ce51a0
DE
6435/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6436 Fill in SIG_ENTRY with DWO_ENTRY. */
6437
6438static void
ed2dc618 6439fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6440 struct signatured_type *sig_entry,
6441 struct dwo_unit *dwo_entry)
6442{
7ee85ab1 6443 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6444 gdb_assert (! sig_entry->per_cu.queued);
6445 gdb_assert (sig_entry->per_cu.cu == NULL);
5989a64e 6446 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6447 {
6448 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6449 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6450 }
6451 else
6452 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6453 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6454 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6455 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6456 gdb_assert (sig_entry->dwo_unit == NULL);
6457
6458 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6459 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6460 sig_entry->per_cu.length = dwo_entry->length;
6461 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6462 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6463 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6464 sig_entry->dwo_unit = dwo_entry;
6465}
6466
6467/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6468 If we haven't read the TU yet, create the signatured_type data structure
6469 for a TU to be read in directly from a DWO file, bypassing the stub.
6470 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6471 using .gdb_index, then when reading a CU we want to stay in the DWO file
6472 containing that CU. Otherwise we could end up reading several other DWO
6473 files (due to comdat folding) to process the transitive closure of all the
6474 mentioned TUs, and that can be slow. The current DWO file will have every
6475 type signature that it needs.
a2ce51a0
DE
6476 We only do this for .gdb_index because in the psymtab case we already have
6477 to read all the DWOs to build the type unit groups. */
6478
6479static struct signatured_type *
6480lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6481{
518817b3
SM
6482 struct dwarf2_per_objfile *dwarf2_per_objfile
6483 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6484 struct dwo_file *dwo_file;
6485 struct dwo_unit find_dwo_entry, *dwo_entry;
6486 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6487 void **slot;
a2ce51a0 6488
5989a64e 6489 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6490
6aa5f3a6
DE
6491 /* If TU skeletons have been removed then we may not have read in any
6492 TUs yet. */
5989a64e
SM
6493 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6494 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6495
6496 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6497 Use the global signatured_types array to do our own comdat-folding
6498 of types. If this is the first time we're reading this TU, and
6499 the TU has an entry in .gdb_index, replace the recorded data from
6500 .gdb_index with this TU. */
a2ce51a0 6501
a2ce51a0 6502 find_sig_entry.signature = sig;
5989a64e 6503 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6504 &find_sig_entry, INSERT);
9a3c8263 6505 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6506
6507 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6508 read. Don't reassign the global entry to point to this DWO if that's
6509 the case. Also note that if the TU is already being read, it may not
6510 have come from a DWO, the program may be a mix of Fission-compiled
6511 code and non-Fission-compiled code. */
6512
6513 /* Have we already tried to read this TU?
6514 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6515 needn't exist in the global table yet). */
6516 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6517 return sig_entry;
6518
6aa5f3a6
DE
6519 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6520 dwo_unit of the TU itself. */
6521 dwo_file = cu->dwo_unit->dwo_file;
6522
a2ce51a0
DE
6523 /* Ok, this is the first time we're reading this TU. */
6524 if (dwo_file->tus == NULL)
6525 return NULL;
6526 find_dwo_entry.signature = sig;
b0b6a987
TT
6527 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6528 &find_dwo_entry);
a2ce51a0
DE
6529 if (dwo_entry == NULL)
6530 return NULL;
6531
6aa5f3a6
DE
6532 /* If the global table doesn't have an entry for this TU, add one. */
6533 if (sig_entry == NULL)
ed2dc618 6534 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6535
ed2dc618 6536 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6537 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6538 return sig_entry;
6539}
6540
a2ce51a0
DE
6541/* Subroutine of lookup_signatured_type.
6542 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6543 then try the DWP file. If the TU stub (skeleton) has been removed then
6544 it won't be in .gdb_index. */
a2ce51a0
DE
6545
6546static struct signatured_type *
6547lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6548{
518817b3
SM
6549 struct dwarf2_per_objfile *dwarf2_per_objfile
6550 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6551 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6552 struct dwo_unit *dwo_entry;
6553 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6554 void **slot;
a2ce51a0 6555
5989a64e 6556 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6557 gdb_assert (dwp_file != NULL);
6558
6aa5f3a6
DE
6559 /* If TU skeletons have been removed then we may not have read in any
6560 TUs yet. */
5989a64e
SM
6561 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6562 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6563
6aa5f3a6 6564 find_sig_entry.signature = sig;
5989a64e 6565 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6566 &find_sig_entry, INSERT);
9a3c8263 6567 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6568
6569 /* Have we already tried to read this TU?
6570 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6571 needn't exist in the global table yet). */
6572 if (sig_entry != NULL)
6573 return sig_entry;
6574
a2ce51a0
DE
6575 if (dwp_file->tus == NULL)
6576 return NULL;
ed2dc618 6577 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6578 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6579 if (dwo_entry == NULL)
6580 return NULL;
6581
ed2dc618
SM
6582 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6583 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6584
a2ce51a0
DE
6585 return sig_entry;
6586}
6587
380bca97 6588/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6589 Returns NULL if signature SIG is not present in the table.
6590 It is up to the caller to complain about this. */
348e048f
DE
6591
6592static struct signatured_type *
a2ce51a0 6593lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6594{
518817b3
SM
6595 struct dwarf2_per_objfile *dwarf2_per_objfile
6596 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6597
a2ce51a0 6598 if (cu->dwo_unit
5989a64e 6599 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6600 {
6601 /* We're in a DWO/DWP file, and we're using .gdb_index.
6602 These cases require special processing. */
ed2dc618 6603 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6604 return lookup_dwo_signatured_type (cu, sig);
6605 else
6606 return lookup_dwp_signatured_type (cu, sig);
6607 }
6608 else
6609 {
6610 struct signatured_type find_entry, *entry;
348e048f 6611
5989a64e 6612 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6613 return NULL;
6614 find_entry.signature = sig;
9a3c8263 6615 entry = ((struct signatured_type *)
5989a64e 6616 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6617 &find_entry));
a2ce51a0
DE
6618 return entry;
6619 }
348e048f 6620}
18a8505e 6621
42e7ad6c 6622/* Low level DIE reading support. */
348e048f 6623
d85a05f0
DJ
6624/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6625
6626static void
6627init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6628 struct dwarf2_cu *cu,
3019eac3 6629 struct dwarf2_section_info *section,
685af9cd
TT
6630 struct dwo_file *dwo_file,
6631 struct abbrev_table *abbrev_table)
d85a05f0 6632{
fceca515 6633 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6634 reader->abfd = section->get_bfd_owner ();
d85a05f0 6635 reader->cu = cu;
3019eac3 6636 reader->dwo_file = dwo_file;
dee91e82
DE
6637 reader->die_section = section;
6638 reader->buffer = section->buffer;
f664829e 6639 reader->buffer_end = section->buffer + section->size;
685af9cd 6640 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6641}
6642
c0ab21c2 6643/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6644 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6645 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6646 already.
6647
6648 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6649 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6650 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6651 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6652 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6653 STUB_COMP_DIR may be non-NULL.
3e225074 6654 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6655 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6656 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6657 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6658 kept around for at least as long as *RESULT_READER.
6659
b0c7bfa9
DE
6660 The result is non-zero if a valid (non-dummy) DIE was found. */
6661
6662static int
6663read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6664 struct dwo_unit *dwo_unit,
b0c7bfa9 6665 struct die_info *stub_comp_unit_die,
a2ce51a0 6666 const char *stub_comp_dir,
b0c7bfa9 6667 struct die_reader_specs *result_reader,
d521ce57 6668 const gdb_byte **result_info_ptr,
b0c7bfa9 6669 struct die_info **result_comp_unit_die,
685af9cd 6670 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6671{
ed2dc618 6672 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6673 struct objfile *objfile = dwarf2_per_objfile->objfile;
6674 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6675 bfd *abfd;
d521ce57 6676 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6677 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6678 int i,num_extra_attrs;
6679 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6680 struct die_info *comp_unit_die;
6681
b0aeadb3
DE
6682 /* At most one of these may be provided. */
6683 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6684
b0c7bfa9
DE
6685 /* These attributes aren't processed until later:
6686 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6687 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6688 referenced later. However, these attributes are found in the stub
6689 which we won't have later. In order to not impose this complication
6690 on the rest of the code, we read them here and copy them to the
6691 DWO CU/TU die. */
b0c7bfa9
DE
6692
6693 stmt_list = NULL;
6694 low_pc = NULL;
6695 high_pc = NULL;
6696 ranges = NULL;
6697 comp_dir = NULL;
6698
6699 if (stub_comp_unit_die != NULL)
6700 {
6701 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6702 DWO file. */
6703 if (! this_cu->is_debug_types)
6704 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6705 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6706 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6707 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6708 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6709
a39fdb41 6710 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6711
18a8505e
AT
6712 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6713 here (if needed). We need the value before we can process
6714 DW_AT_ranges. */
a39fdb41 6715 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6716 }
a2ce51a0
DE
6717 else if (stub_comp_dir != NULL)
6718 {
6719 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6720 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6721 comp_dir->name = DW_AT_comp_dir;
6722 comp_dir->form = DW_FORM_string;
6723 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6724 DW_STRING (comp_dir) = stub_comp_dir;
6725 }
b0c7bfa9
DE
6726
6727 /* Set up for reading the DWO CU/TU. */
6728 cu->dwo_unit = dwo_unit;
685af9cd 6729 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6730 section->read (objfile);
6731 abfd = section->get_bfd_owner ();
9c541725
PA
6732 begin_info_ptr = info_ptr = (section->buffer
6733 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6734 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6735
6736 if (this_cu->is_debug_types)
6737 {
b0c7bfa9
DE
6738 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6739
ed2dc618
SM
6740 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6741 &cu->header, section,
b0c7bfa9 6742 dwo_abbrev_section,
43988095 6743 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6744 /* This is not an assert because it can be caused by bad debug info. */
43988095 6745 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6746 {
6747 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6748 " TU at offset %s [in module %s]"),
a2ce51a0 6749 hex_string (sig_type->signature),
43988095 6750 hex_string (cu->header.signature),
9d8780f0 6751 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6752 bfd_get_filename (abfd));
6753 }
9c541725 6754 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6755 /* For DWOs coming from DWP files, we don't know the CU length
6756 nor the type's offset in the TU until now. */
4057dfde 6757 dwo_unit->length = cu->header.get_length ();
9c541725 6758 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6759
6760 /* Establish the type offset that can be used to lookup the type.
6761 For DWO files, we don't know it until now. */
9c541725
PA
6762 sig_type->type_offset_in_section
6763 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6764 }
6765 else
6766 {
ed2dc618
SM
6767 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6768 &cu->header, section,
b0c7bfa9 6769 dwo_abbrev_section,
43988095 6770 info_ptr, rcuh_kind::COMPILE);
9c541725 6771 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6772 /* For DWOs coming from DWP files, we don't know the CU length
6773 until now. */
4057dfde 6774 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6775 }
6776
685af9cd 6777 *result_dwo_abbrev_table
86de1d91
TT
6778 = abbrev_table::read (objfile, dwo_abbrev_section,
6779 cu->header.abbrev_sect_off);
685af9cd
TT
6780 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6781 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6782
6783 /* Read in the die, but leave space to copy over the attributes
6784 from the stub. This has the benefit of simplifying the rest of
6785 the code - all the work to maintain the illusion of a single
6786 DW_TAG_{compile,type}_unit DIE is done here. */
6787 num_extra_attrs = ((stmt_list != NULL)
6788 + (low_pc != NULL)
6789 + (high_pc != NULL)
6790 + (ranges != NULL)
6791 + (comp_dir != NULL));
6792 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6793 num_extra_attrs);
b0c7bfa9
DE
6794
6795 /* Copy over the attributes from the stub to the DIE we just read in. */
6796 comp_unit_die = *result_comp_unit_die;
6797 i = comp_unit_die->num_attrs;
6798 if (stmt_list != NULL)
6799 comp_unit_die->attrs[i++] = *stmt_list;
6800 if (low_pc != NULL)
6801 comp_unit_die->attrs[i++] = *low_pc;
6802 if (high_pc != NULL)
6803 comp_unit_die->attrs[i++] = *high_pc;
6804 if (ranges != NULL)
6805 comp_unit_die->attrs[i++] = *ranges;
6806 if (comp_dir != NULL)
6807 comp_unit_die->attrs[i++] = *comp_dir;
6808 comp_unit_die->num_attrs += num_extra_attrs;
6809
b4f54984 6810 if (dwarf_die_debug)
bf6af496
DE
6811 {
6812 fprintf_unfiltered (gdb_stdlog,
6813 "Read die from %s@0x%x of %s:\n",
96b79293 6814 section->get_name (),
bf6af496
DE
6815 (unsigned) (begin_info_ptr - section->buffer),
6816 bfd_get_filename (abfd));
b4f54984 6817 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6818 }
6819
b0c7bfa9
DE
6820 /* Skip dummy compilation units. */
6821 if (info_ptr >= begin_info_ptr + dwo_unit->length
6822 || peek_abbrev_code (abfd, info_ptr) == 0)
6823 return 0;
6824
6825 *result_info_ptr = info_ptr;
6826 return 1;
6827}
6828
a084a2a6
AT
6829/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6830 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6831 signature is part of the header. */
6832static gdb::optional<ULONGEST>
6833lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6834{
6835 if (cu->header.version >= 5)
6836 return cu->header.signature;
6837 struct attribute *attr;
6838 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6839 if (attr == nullptr)
6840 return gdb::optional<ULONGEST> ();
6841 return DW_UNSND (attr);
6842}
6843
c0ab21c2 6844/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6845 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6846 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6847
6848static struct dwo_unit *
6849lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6850 struct die_info *comp_unit_die,
6851 const char *dwo_name)
b0c7bfa9
DE
6852{
6853 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6854 struct dwo_unit *dwo_unit;
c0ab21c2 6855 const char *comp_dir;
b0c7bfa9 6856
a2ce51a0
DE
6857 gdb_assert (cu != NULL);
6858
b0c7bfa9 6859 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6860 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6861 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6862
6863 if (this_cu->is_debug_types)
6864 {
6865 struct signatured_type *sig_type;
6866
6867 /* Since this_cu is the first member of struct signatured_type,
6868 we can go from a pointer to one to a pointer to the other. */
6869 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6870 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6871 }
6872 else
6873 {
a084a2a6
AT
6874 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6875 if (!signature.has_value ())
b0c7bfa9
DE
6876 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6877 " [in module %s]"),
e3b94546 6878 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6879 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6880 *signature);
b0c7bfa9
DE
6881 }
6882
b0c7bfa9
DE
6883 return dwo_unit;
6884}
6885
c0ab21c2 6886/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6887 See it for a description of the parameters.
fcd3b13d 6888 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6889
c0ab21c2
TT
6890void
6891cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6892 int use_existing_cu)
a2ce51a0 6893{
a2ce51a0 6894 struct signatured_type *sig_type;
a2ce51a0
DE
6895
6896 /* Verify we can do the following downcast, and that we have the
6897 data we need. */
6898 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6899 sig_type = (struct signatured_type *) this_cu;
6900 gdb_assert (sig_type->dwo_unit != NULL);
6901
6aa5f3a6
DE
6902 if (use_existing_cu && this_cu->cu != NULL)
6903 {
6904 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6905 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6906 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6907 }
6908 else
6909 {
6910 /* If !use_existing_cu, this_cu->cu must be NULL. */
6911 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6912 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6913 }
6914
6915 /* A future optimization, if needed, would be to use an existing
6916 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6917 could share abbrev tables. */
a2ce51a0
DE
6918
6919 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6920 NULL /* stub_comp_unit_die */,
6921 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6922 this, &info_ptr,
3e225074 6923 &comp_unit_die,
c0ab21c2 6924 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6925 {
6926 /* Dummy die. */
c0ab21c2 6927 dummy_p = true;
a2ce51a0 6928 }
a2ce51a0
DE
6929}
6930
fd820528 6931/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6932 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6933
f4dc4d17
DE
6934 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6935 Otherwise the table specified in the comp unit header is read in and used.
6936 This is an optimization for when we already have the abbrev table.
6937
dee91e82 6938 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6939 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6940
c0ab21c2
TT
6941cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6942 struct abbrev_table *abbrev_table,
6751ebae 6943 int use_existing_cu,
c0ab21c2
TT
6944 bool skip_partial)
6945 : die_reader_specs {},
6751ebae 6946 m_this_cu (this_cu)
c906108c 6947{
ed2dc618 6948 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6949 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6950 struct dwarf2_section_info *section = this_cu->section;
96b79293 6951 bfd *abfd = section->get_bfd_owner ();
dee91e82 6952 struct dwarf2_cu *cu;
c0ab21c2 6953 const gdb_byte *begin_info_ptr;
dee91e82 6954 struct signatured_type *sig_type = NULL;
4bdcc0c1 6955 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6956 /* Non-zero if CU currently points to a DWO file and we need to
6957 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6958 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6959 int rereading_dwo_cu = 0;
c906108c 6960
b4f54984 6961 if (dwarf_die_debug)
9d8780f0 6962 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6963 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6964 sect_offset_str (this_cu->sect_off));
09406207 6965
a2ce51a0
DE
6966 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6967 file (instead of going through the stub), short-circuit all of this. */
6968 if (this_cu->reading_dwo_directly)
6969 {
6970 /* Narrow down the scope of possibilities to have to understand. */
6971 gdb_assert (this_cu->is_debug_types);
6972 gdb_assert (abbrev_table == NULL);
6751ebae 6973 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6974 return;
6975 }
6976
dee91e82 6977 /* This is cheap if the section is already read in. */
96b79293 6978 section->read (objfile);
dee91e82 6979
9c541725 6980 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6981
6982 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6983
6984 if (use_existing_cu && this_cu->cu != NULL)
6985 {
6986 cu = this_cu->cu;
42e7ad6c
DE
6987 /* If this CU is from a DWO file we need to start over, we need to
6988 refetch the attributes from the skeleton CU.
6989 This could be optimized by retrieving those attributes from when we
6990 were here the first time: the previous comp_unit_die was stored in
6991 comp_unit_obstack. But there's no data yet that we need this
6992 optimization. */
6993 if (cu->dwo_unit != NULL)
6994 rereading_dwo_cu = 1;
dee91e82
DE
6995 }
6996 else
6997 {
6998 /* If !use_existing_cu, this_cu->cu must be NULL. */
6999 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
7000 m_new_cu.reset (new dwarf2_cu (this_cu));
7001 cu = m_new_cu.get ();
42e7ad6c 7002 }
dee91e82 7003
b0c7bfa9 7004 /* Get the header. */
9c541725 7005 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7006 {
7007 /* We already have the header, there's no need to read it in again. */
9c541725 7008 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7009 }
7010 else
7011 {
3019eac3 7012 if (this_cu->is_debug_types)
dee91e82 7013 {
ed2dc618
SM
7014 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7015 &cu->header, section,
4bdcc0c1 7016 abbrev_section, info_ptr,
43988095 7017 rcuh_kind::TYPE);
dee91e82 7018
42e7ad6c
DE
7019 /* Since per_cu is the first member of struct signatured_type,
7020 we can go from a pointer to one to a pointer to the other. */
7021 sig_type = (struct signatured_type *) this_cu;
43988095 7022 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7023 gdb_assert (sig_type->type_offset_in_tu
7024 == cu->header.type_cu_offset_in_tu);
7025 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7026
42e7ad6c
DE
7027 /* LENGTH has not been set yet for type units if we're
7028 using .gdb_index. */
4057dfde 7029 this_cu->length = cu->header.get_length ();
3019eac3
DE
7030
7031 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7032 sig_type->type_offset_in_section =
7033 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7034
7035 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7036 }
7037 else
7038 {
ed2dc618
SM
7039 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7040 &cu->header, section,
4bdcc0c1 7041 abbrev_section,
43988095
JK
7042 info_ptr,
7043 rcuh_kind::COMPILE);
dee91e82 7044
9c541725 7045 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7046 if (this_cu->length == 0)
7047 this_cu->length = cu->header.get_length ();
7048 else
7049 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7050 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7051 }
7052 }
10b3939b 7053
6caca83c 7054 /* Skip dummy compilation units. */
dee91e82 7055 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7056 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7057 {
7058 dummy_p = true;
7059 return;
7060 }
6caca83c 7061
433df2d4
DE
7062 /* If we don't have them yet, read the abbrevs for this compilation unit.
7063 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7064 done. */
f4dc4d17 7065 if (abbrev_table != NULL)
685af9cd
TT
7066 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7067 else
f4dc4d17 7068 {
c0ab21c2 7069 m_abbrev_table_holder
86de1d91
TT
7070 = abbrev_table::read (objfile, abbrev_section,
7071 cu->header.abbrev_sect_off);
c0ab21c2 7072 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7073 }
af703f96 7074
dee91e82 7075 /* Read the top level CU/TU die. */
c0ab21c2 7076 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7077 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7078
58f0c718 7079 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7080 {
7081 dummy_p = true;
7082 return;
7083 }
58f0c718 7084
b0c7bfa9 7085 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7086 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7087 table from the DWO file and pass the ownership over to us. It will be
7088 referenced from READER, so we must make sure to free it after we're done
7089 with READER.
7090
b0c7bfa9
DE
7091 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7092 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7093 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7094 if (dwo_name != nullptr)
3019eac3 7095 {
3019eac3 7096 struct dwo_unit *dwo_unit;
b0c7bfa9 7097 struct die_info *dwo_comp_unit_die;
3019eac3 7098
3e225074 7099 if (comp_unit_die->has_children)
6a506a2d 7100 {
b98664d3 7101 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7102 " has children (offset %s) [in module %s]"),
7103 sect_offset_str (this_cu->sect_off),
7104 bfd_get_filename (abfd));
6a506a2d 7105 }
c0ab21c2 7106 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7107 if (dwo_unit != NULL)
3019eac3 7108 {
6a506a2d 7109 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7110 comp_unit_die, NULL,
c0ab21c2 7111 this, &info_ptr,
3e225074 7112 &dwo_comp_unit_die,
c0ab21c2 7113 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7114 {
7115 /* Dummy die. */
c0ab21c2 7116 dummy_p = true;
6a506a2d
DE
7117 return;
7118 }
7119 comp_unit_die = dwo_comp_unit_die;
7120 }
7121 else
7122 {
7123 /* Yikes, we couldn't find the rest of the DIE, we only have
7124 the stub. A complaint has already been logged. There's
7125 not much more we can do except pass on the stub DIE to
7126 die_reader_func. We don't want to throw an error on bad
7127 debug info. */
3019eac3
DE
7128 }
7129 }
c0ab21c2 7130}
3019eac3 7131
6751ebae
TT
7132void
7133cutu_reader::keep ()
c0ab21c2 7134{
b0c7bfa9 7135 /* Done, clean up. */
6751ebae
TT
7136 gdb_assert (!dummy_p);
7137 if (m_new_cu != NULL)
348e048f 7138 {
c0ab21c2
TT
7139 struct dwarf2_per_objfile *dwarf2_per_objfile
7140 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7141 /* Link this CU into read_in_chain. */
5989a64e
SM
7142 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7143 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7144 /* The chain owns it now. */
c0ab21c2 7145 m_new_cu.release ();
348e048f 7146 }
dee91e82
DE
7147}
7148
18a8505e
AT
7149/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7150 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7151 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7152
7153 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7154 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7155
7156 We fill in THIS_CU->length.
7157
dee91e82 7158 THIS_CU->cu is always freed when done.
3019eac3 7159 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7160 to care whether it refers to the "main" CU or the DWO CU.
7161
7162 When parent_cu is passed, it is used to provide a default value for
7163 str_offsets_base and addr_base from the parent. */
dee91e82 7164
c0ab21c2
TT
7165cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7166 struct dwarf2_cu *parent_cu,
7167 struct dwo_file *dwo_file)
7168 : die_reader_specs {},
7169 m_this_cu (this_cu)
dee91e82 7170{
ed2dc618 7171 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7172 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7173 struct dwarf2_section_info *section = this_cu->section;
96b79293 7174 bfd *abfd = section->get_bfd_owner ();
33e80786 7175 struct dwarf2_section_info *abbrev_section;
d521ce57 7176 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7177
b4f54984 7178 if (dwarf_die_debug)
9d8780f0 7179 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7180 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7181 sect_offset_str (this_cu->sect_off));
09406207 7182
dee91e82
DE
7183 gdb_assert (this_cu->cu == NULL);
7184
33e80786
DE
7185 abbrev_section = (dwo_file != NULL
7186 ? &dwo_file->sections.abbrev
7187 : get_abbrev_section_for_cu (this_cu));
7188
dee91e82 7189 /* This is cheap if the section is already read in. */
96b79293 7190 section->read (objfile);
dee91e82 7191
c0ab21c2 7192 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7193
9c541725 7194 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7195 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7196 &m_new_cu->header, section,
4bdcc0c1 7197 abbrev_section, info_ptr,
43988095
JK
7198 (this_cu->is_debug_types
7199 ? rcuh_kind::TYPE
7200 : rcuh_kind::COMPILE));
dee91e82 7201
18a8505e
AT
7202 if (parent_cu != nullptr)
7203 {
c0ab21c2
TT
7204 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7205 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7206 }
4057dfde 7207 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7208
7209 /* Skip dummy compilation units. */
7210 if (info_ptr >= begin_info_ptr + this_cu->length
7211 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7212 {
7213 dummy_p = true;
7214 return;
7215 }
72bf9492 7216
c0ab21c2 7217 m_abbrev_table_holder
86de1d91
TT
7218 = abbrev_table::read (objfile, abbrev_section,
7219 m_new_cu->header.abbrev_sect_off);
dee91e82 7220
c0ab21c2
TT
7221 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7222 m_abbrev_table_holder.get ());
3e225074 7223 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7224}
7225
0018ea6f
DE
7226\f
7227/* Type Unit Groups.
dee91e82 7228
0018ea6f
DE
7229 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7230 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7231 so that all types coming from the same compilation (.o file) are grouped
7232 together. A future step could be to put the types in the same symtab as
7233 the CU the types ultimately came from. */
ff013f42 7234
f4dc4d17
DE
7235static hashval_t
7236hash_type_unit_group (const void *item)
7237{
9a3c8263
SM
7238 const struct type_unit_group *tu_group
7239 = (const struct type_unit_group *) item;
f4dc4d17 7240
094b34ac 7241 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7242}
348e048f
DE
7243
7244static int
f4dc4d17 7245eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7246{
9a3c8263
SM
7247 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7248 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7249
094b34ac 7250 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7251}
348e048f 7252
f4dc4d17
DE
7253/* Allocate a hash table for type unit groups. */
7254
eaa5fa8b 7255static htab_up
298e9637 7256allocate_type_unit_groups_table ()
f4dc4d17 7257{
eaa5fa8b
TT
7258 return htab_up (htab_create_alloc (3,
7259 hash_type_unit_group,
7260 eq_type_unit_group,
7261 NULL, xcalloc, xfree));
f4dc4d17 7262}
dee91e82 7263
f4dc4d17
DE
7264/* Type units that don't have DW_AT_stmt_list are grouped into their own
7265 partial symtabs. We combine several TUs per psymtab to not let the size
7266 of any one psymtab grow too big. */
7267#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7268#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7269
094b34ac 7270/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7271 Create the type_unit_group object used to hold one or more TUs. */
7272
7273static struct type_unit_group *
094b34ac 7274create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7275{
518817b3
SM
7276 struct dwarf2_per_objfile *dwarf2_per_objfile
7277 = cu->per_cu->dwarf2_per_objfile;
094b34ac 7278 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7279 struct type_unit_group *tu_group;
f4dc4d17 7280
5989a64e 7281 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7282 struct type_unit_group);
094b34ac 7283 per_cu = &tu_group->per_cu;
518817b3 7284 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7285
5989a64e 7286 if (dwarf2_per_objfile->per_bfd->using_index)
094b34ac 7287 {
5989a64e 7288 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
094b34ac 7289 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7290 }
7291 else
7292 {
9c541725 7293 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7294 dwarf2_psymtab *pst;
528e1572 7295 std::string name;
094b34ac
DE
7296
7297 /* Give the symtab a useful name for debug purposes. */
7298 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7299 name = string_printf ("<type_units_%d>",
7300 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7301 else
528e1572 7302 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7303
528e1572 7304 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7305 pst->anonymous = true;
094b34ac 7306 }
f4dc4d17 7307
094b34ac 7308 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7309 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7310
7311 return tu_group;
7312}
7313
094b34ac
DE
7314/* Look up the type_unit_group for type unit CU, and create it if necessary.
7315 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7316
7317static struct type_unit_group *
ff39bb5e 7318get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7319{
518817b3
SM
7320 struct dwarf2_per_objfile *dwarf2_per_objfile
7321 = cu->per_cu->dwarf2_per_objfile;
5989a64e 7322 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7323 struct type_unit_group *tu_group;
7324 void **slot;
7325 unsigned int line_offset;
7326 struct type_unit_group type_unit_group_for_lookup;
7327
5989a64e
SM
7328 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7329 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7330
7331 /* Do we need to create a new group, or can we use an existing one? */
7332
7333 if (stmt_list)
7334 {
7335 line_offset = DW_UNSND (stmt_list);
7336 ++tu_stats->nr_symtab_sharers;
7337 }
7338 else
7339 {
7340 /* Ugh, no stmt_list. Rare, but we have to handle it.
7341 We can do various things here like create one group per TU or
7342 spread them over multiple groups to split up the expansion work.
7343 To avoid worst case scenarios (too many groups or too large groups)
7344 we, umm, group them in bunches. */
7345 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7346 | (tu_stats->nr_stmt_less_type_units
7347 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7348 ++tu_stats->nr_stmt_less_type_units;
7349 }
7350
094b34ac 7351 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7352 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7353 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7354 &type_unit_group_for_lookup, INSERT);
7355 if (*slot != NULL)
7356 {
9a3c8263 7357 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7358 gdb_assert (tu_group != NULL);
7359 }
7360 else
7361 {
9c541725 7362 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7363 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7364 *slot = tu_group;
7365 ++tu_stats->nr_symtabs;
7366 }
7367
7368 return tu_group;
7369}
0018ea6f
DE
7370\f
7371/* Partial symbol tables. */
7372
7373/* Create a psymtab named NAME and assign it to PER_CU.
7374
7375 The caller must fill in the following details:
7376 dirname, textlow, texthigh. */
7377
891813be 7378static dwarf2_psymtab *
0018ea6f
DE
7379create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7380{
e3b94546 7381 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7382 dwarf2_psymtab *pst;
0018ea6f 7383
9f4e76a4 7384 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7385
6d94535f 7386 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7387
7388 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7389 per_cu->v.psymtab = pst;
7390
7391 return pst;
7392}
7393
c0ab21c2 7394/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7395
7396static void
7397process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7398 const gdb_byte *info_ptr,
0018ea6f 7399 struct die_info *comp_unit_die,
c0ab21c2 7400 enum language pretend_language)
0018ea6f
DE
7401{
7402 struct dwarf2_cu *cu = reader->cu;
518817b3 7403 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7404 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7405 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7406 CORE_ADDR baseaddr;
7407 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7408 dwarf2_psymtab *pst;
3a2b436a 7409 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7410 const char *filename;
0018ea6f 7411
0018ea6f
DE
7412 gdb_assert (! per_cu->is_debug_types);
7413
c0ab21c2 7414 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7415
0018ea6f 7416 /* Allocate a new partial symbol table structure. */
2e927613
TV
7417 gdb::unique_xmalloc_ptr<char> debug_filename;
7418 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7419 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7420 if (filename == NULL)
0018ea6f 7421 filename = "";
2e927613
TV
7422 else if (strcmp (filename, artificial) == 0)
7423 {
7424 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7425 sect_offset_str (per_cu->sect_off),
7426 (char *) NULL));
2e927613
TV
7427 filename = debug_filename.get ();
7428 }
0018ea6f
DE
7429
7430 pst = create_partial_symtab (per_cu, filename);
7431
7432 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7433 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7434
b3b3bada 7435 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7436
7437 dwarf2_find_base_address (comp_unit_die, cu);
7438
7439 /* Possibly set the default values of LOWPC and HIGHPC from
7440 `DW_AT_ranges'. */
3a2b436a
JK
7441 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7442 &best_highpc, cu, pst);
7443 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7444 {
7445 CORE_ADDR low
7446 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7447 - baseaddr);
7448 CORE_ADDR high
7449 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7450 - baseaddr - 1);
7451 /* Store the contiguous range if it is not empty; it can be
7452 empty for CUs with no code. */
d320c2b5
TT
7453 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7454 low, high, pst);
79748972 7455 }
0018ea6f
DE
7456
7457 /* Check if comp unit has_children.
7458 If so, read the rest of the partial symbols from this comp unit.
7459 If not, there's no more debug_info for this comp unit. */
3e225074 7460 if (comp_unit_die->has_children)
0018ea6f
DE
7461 {
7462 struct partial_die_info *first_die;
7463 CORE_ADDR lowpc, highpc;
7464
7465 lowpc = ((CORE_ADDR) -1);
7466 highpc = ((CORE_ADDR) 0);
7467
7468 first_die = load_partial_dies (reader, info_ptr, 1);
7469
7470 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7471 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7472
7473 /* If we didn't find a lowpc, set it to highpc to avoid
7474 complaints from `maint check'. */
7475 if (lowpc == ((CORE_ADDR) -1))
7476 lowpc = highpc;
7477
7478 /* If the compilation unit didn't have an explicit address range,
7479 then use the information extracted from its child dies. */
e385593e 7480 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7481 {
7482 best_lowpc = lowpc;
7483 best_highpc = highpc;
7484 }
7485 }
4ae976d1 7486 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7487 best_lowpc + baseaddr)
7488 - baseaddr);
4ae976d1 7489 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7490 best_highpc + baseaddr)
7491 - baseaddr);
0018ea6f 7492
8763cede 7493 end_psymtab_common (objfile, pst);
0018ea6f 7494
ae640021 7495 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7496 {
7497 int i;
ae640021 7498 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7499
7500 /* Fill in 'dependencies' here; we fill in 'users' in a
7501 post-pass. */
7502 pst->number_of_dependencies = len;
a9342b62
TT
7503 pst->dependencies
7504 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7505 for (i = 0; i < len; ++i)
7506 {
7507 pst->dependencies[i]
7508 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7509 }
0018ea6f 7510
ae640021 7511 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7512 }
7513
7514 /* Get the list of files included in the current compilation unit,
7515 and build a psymtab for each of them. */
7516 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7517
b4f54984 7518 if (dwarf_read_debug)
b926417a
TT
7519 fprintf_unfiltered (gdb_stdlog,
7520 "Psymtab for %s unit @%s: %s - %s"
7521 ", %d global, %d static syms\n",
7522 per_cu->is_debug_types ? "type" : "comp",
7523 sect_offset_str (per_cu->sect_off),
7524 paddress (gdbarch, pst->text_low (objfile)),
7525 paddress (gdbarch, pst->text_high (objfile)),
7526 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7527}
7528
7529/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7530 Process compilation unit THIS_CU for a psymtab. */
7531
7532static void
7533process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7534 bool want_partial_unit,
b93601f3 7535 enum language pretend_language)
0018ea6f
DE
7536{
7537 /* If this compilation unit was already read in, free the
7538 cached copy in order to read it in again. This is
7539 necessary because we skipped some symbols when we first
7540 read in the compilation unit (see load_partial_dies).
7541 This problem could be avoided, but the benefit is unclear. */
7542 if (this_cu->cu != NULL)
7543 free_one_cached_comp_unit (this_cu);
7544
6751ebae 7545 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7546
58990295
TV
7547 switch (reader.comp_unit_die->tag)
7548 {
7549 case DW_TAG_compile_unit:
7550 this_cu->unit_type = DW_UT_compile;
7551 break;
7552 case DW_TAG_partial_unit:
7553 this_cu->unit_type = DW_UT_partial;
7554 break;
7555 default:
7556 abort ();
7557 }
7558
c0ab21c2 7559 if (reader.dummy_p)
f1902523 7560 {
c0ab21c2 7561 /* Nothing. */
f1902523 7562 }
c0ab21c2 7563 else if (this_cu->is_debug_types)
3e225074
TT
7564 build_type_psymtabs_reader (&reader, reader.info_ptr,
7565 reader.comp_unit_die);
135f5437
TT
7566 else if (want_partial_unit
7567 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7568 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7569 reader.comp_unit_die,
c0ab21c2 7570 pretend_language);
0018ea6f 7571
58990295
TV
7572 this_cu->lang = this_cu->cu->language;
7573
0018ea6f 7574 /* Age out any secondary CUs. */
ed2dc618 7575 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7576}
f4dc4d17
DE
7577
7578/* Reader function for build_type_psymtabs. */
7579
7580static void
7581build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7582 const gdb_byte *info_ptr,
3e225074 7583 struct die_info *type_unit_die)
f4dc4d17 7584{
ed2dc618 7585 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7586 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7587 struct objfile *objfile = dwarf2_per_objfile->objfile;
7588 struct dwarf2_cu *cu = reader->cu;
7589 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7590 struct signatured_type *sig_type;
f4dc4d17
DE
7591 struct type_unit_group *tu_group;
7592 struct attribute *attr;
7593 struct partial_die_info *first_die;
7594 CORE_ADDR lowpc, highpc;
891813be 7595 dwarf2_psymtab *pst;
f4dc4d17 7596
0186c6a7
DE
7597 gdb_assert (per_cu->is_debug_types);
7598 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7599
3e225074 7600 if (! type_unit_die->has_children)
f4dc4d17
DE
7601 return;
7602
052c8bb8 7603 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7604 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7605
df07e2c7 7606 if (tu_group->tus == nullptr)
a8b3b8e9 7607 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7608 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7609
7610 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7611 pst = create_partial_symtab (per_cu, "");
6d94535f 7612 pst->anonymous = true;
f4dc4d17
DE
7613
7614 first_die = load_partial_dies (reader, info_ptr, 1);
7615
7616 lowpc = (CORE_ADDR) -1;
7617 highpc = (CORE_ADDR) 0;
7618 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7619
8763cede 7620 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7621}
7622
73051182
DE
7623/* Struct used to sort TUs by their abbreviation table offset. */
7624
7625struct tu_abbrev_offset
7626{
b2bdb8cf
SM
7627 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7628 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7629 {}
7630
7631 signatured_type *sig_type;
73051182
DE
7632 sect_offset abbrev_offset;
7633};
7634
484cf504 7635/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7636
484cf504
TT
7637static bool
7638sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7639 const struct tu_abbrev_offset &b)
73051182 7640{
484cf504 7641 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7642}
7643
7644/* Efficiently read all the type units.
7645 This does the bulk of the work for build_type_psymtabs.
7646
7647 The efficiency is because we sort TUs by the abbrev table they use and
7648 only read each abbrev table once. In one program there are 200K TUs
7649 sharing 8K abbrev tables.
7650
7651 The main purpose of this function is to support building the
5989a64e 7652 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7653 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7654 can collapse the search space by grouping them by stmt_list.
7655 The savings can be significant, in the same program from above the 200K TUs
7656 share 8K stmt_list tables.
7657
7658 FUNC is expected to call get_type_unit_group, which will create the
7659 struct type_unit_group if necessary and add it to
5989a64e 7660 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7661
7662static void
ed2dc618 7663build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7664{
5989a64e 7665 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7666 abbrev_table_up abbrev_table;
73051182 7667 sect_offset abbrev_offset;
73051182
DE
7668
7669 /* It's up to the caller to not call us multiple times. */
5989a64e 7670 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7671
5989a64e 7672 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7673 return;
7674
7675 /* TUs typically share abbrev tables, and there can be way more TUs than
7676 abbrev tables. Sort by abbrev table to reduce the number of times we
7677 read each abbrev table in.
7678 Alternatives are to punt or to maintain a cache of abbrev tables.
7679 This is simpler and efficient enough for now.
7680
7681 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7682 symtab to use). Typically TUs with the same abbrev offset have the same
7683 stmt_list value too so in practice this should work well.
7684
7685 The basic algorithm here is:
7686
7687 sort TUs by abbrev table
7688 for each TU with same abbrev table:
7689 read abbrev table if first user
7690 read TU top level DIE
7691 [IWBN if DWO skeletons had DW_AT_stmt_list]
7692 call FUNC */
7693
b4f54984 7694 if (dwarf_read_debug)
73051182
DE
7695 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7696
7697 /* Sort in a separate table to maintain the order of all_type_units
7698 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7699 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7700 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7701
5989a64e 7702 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7703 sorted_by_abbrev.emplace_back
7704 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7705 sig_type->per_cu.section,
7706 sig_type->per_cu.sect_off));
73051182 7707
484cf504
TT
7708 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7709 sort_tu_by_abbrev_offset);
73051182 7710
9c541725 7711 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7712
b2bdb8cf 7713 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7714 {
73051182
DE
7715 /* Switch to the next abbrev table if necessary. */
7716 if (abbrev_table == NULL
b2bdb8cf 7717 || tu.abbrev_offset != abbrev_offset)
73051182 7718 {
b2bdb8cf 7719 abbrev_offset = tu.abbrev_offset;
73051182 7720 abbrev_table =
86de1d91 7721 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7722 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7723 abbrev_offset);
73051182
DE
7724 ++tu_stats->nr_uniq_abbrev_tables;
7725 }
7726
c0ab21c2 7727 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7728 0, false);
c0ab21c2
TT
7729 if (!reader.dummy_p)
7730 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7731 reader.comp_unit_die);
73051182 7732 }
6aa5f3a6 7733}
73051182 7734
6aa5f3a6
DE
7735/* Print collected type unit statistics. */
7736
7737static void
ed2dc618 7738print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7739{
5989a64e 7740 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7741
7742 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7743 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7744 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7745 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7746 tu_stats->nr_uniq_abbrev_tables);
7747 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7748 tu_stats->nr_symtabs);
7749 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7750 tu_stats->nr_symtab_sharers);
7751 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7752 tu_stats->nr_stmt_less_type_units);
7753 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7754 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7755}
7756
f4dc4d17
DE
7757/* Traversal function for build_type_psymtabs. */
7758
7759static int
7760build_type_psymtab_dependencies (void **slot, void *info)
7761{
ed2dc618
SM
7762 struct dwarf2_per_objfile *dwarf2_per_objfile
7763 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7764 struct objfile *objfile = dwarf2_per_objfile->objfile;
7765 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7766 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7767 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7768 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7769 int i;
7770
7771 gdb_assert (len > 0);
197400e8 7772 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7773
7774 pst->number_of_dependencies = len;
a9342b62 7775 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7776 for (i = 0; i < len; ++i)
f4dc4d17 7777 {
df07e2c7 7778 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7779 gdb_assert (iter->per_cu.is_debug_types);
7780 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7781 iter->type_unit_group = tu_group;
f4dc4d17
DE
7782 }
7783
df07e2c7
AB
7784 delete tu_group->tus;
7785 tu_group->tus = nullptr;
348e048f
DE
7786
7787 return 1;
7788}
7789
7790/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7791 Build partial symbol tables for the .debug_types comp-units. */
7792
7793static void
ed2dc618 7794build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7795{
ed2dc618 7796 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7797 return;
7798
ed2dc618 7799 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7800}
f4dc4d17 7801
6aa5f3a6
DE
7802/* Traversal function for process_skeletonless_type_unit.
7803 Read a TU in a DWO file and build partial symbols for it. */
7804
7805static int
7806process_skeletonless_type_unit (void **slot, void *info)
7807{
7808 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7809 struct dwarf2_per_objfile *dwarf2_per_objfile
7810 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7811 struct signatured_type find_entry, *entry;
7812
7813 /* If this TU doesn't exist in the global table, add it and read it in. */
7814
5989a64e
SM
7815 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7816 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7817
7818 find_entry.signature = dwo_unit->signature;
5989a64e 7819 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7820 &find_entry, INSERT);
6aa5f3a6
DE
7821 /* If we've already seen this type there's nothing to do. What's happening
7822 is we're doing our own version of comdat-folding here. */
7823 if (*slot != NULL)
7824 return 1;
7825
7826 /* This does the job that create_all_type_units would have done for
7827 this TU. */
ed2dc618
SM
7828 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7829 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7830 *slot = entry;
7831
7832 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7833 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7834 if (!reader.dummy_p)
7835 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7836 reader.comp_unit_die);
6aa5f3a6
DE
7837
7838 return 1;
7839}
7840
7841/* Traversal function for process_skeletonless_type_units. */
7842
7843static int
7844process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7845{
7846 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7847
7848 if (dwo_file->tus != NULL)
b0b6a987
TT
7849 htab_traverse_noresize (dwo_file->tus.get (),
7850 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7851
7852 return 1;
7853}
7854
7855/* Scan all TUs of DWO files, verifying we've processed them.
7856 This is needed in case a TU was emitted without its skeleton.
7857 Note: This can't be done until we know what all the DWO files are. */
7858
7859static void
ed2dc618 7860process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7861{
7862 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7863 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7864 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7865 {
5989a64e 7866 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7867 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7868 dwarf2_per_objfile);
6aa5f3a6 7869 }
348e048f
DE
7870}
7871
ed2dc618 7872/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7873
7874static void
ed2dc618 7875set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7876{
5989a64e 7877 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7878 {
891813be 7879 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7880
36586728
TT
7881 if (pst == NULL)
7882 continue;
7883
b76e467d 7884 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7885 {
7886 /* Set the 'user' field only if it is not already set. */
7887 if (pst->dependencies[j]->user == NULL)
7888 pst->dependencies[j]->user = pst;
7889 }
7890 }
7891}
7892
93311388
DE
7893/* Build the partial symbol table by doing a quick pass through the
7894 .debug_info and .debug_abbrev sections. */
72bf9492 7895
93311388 7896static void
ed2dc618 7897dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7898{
ed2dc618 7899 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7900
b4f54984 7901 if (dwarf_read_debug)
45cfd468
DE
7902 {
7903 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7904 objfile_name (objfile));
45cfd468
DE
7905 }
7906
76935768 7907 scoped_restore restore_reading_psyms
5989a64e 7908 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7909 true);
98bfdba5 7910
5989a64e 7911 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7912
93311388
DE
7913 /* Any cached compilation units will be linked by the per-objfile
7914 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7915 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7916
ed2dc618 7917 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7918
ed2dc618 7919 create_all_comp_units (dwarf2_per_objfile);
c906108c 7920
60606b2c
TT
7921 /* Create a temporary address map on a temporary obstack. We later
7922 copy this to the final obstack. */
8268c778 7923 auto_obstack temp_obstack;
791afaa2
TT
7924
7925 scoped_restore save_psymtabs_addrmap
d320c2b5 7926 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7927 addrmap_create_mutable (&temp_obstack));
72bf9492 7928
5989a64e 7929 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7930 {
7931 if (per_cu->v.psymtab != NULL)
7932 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7933 continue;
7934 process_psymtab_comp_unit (per_cu, false, language_minimal);
7935 }
ff013f42 7936
6aa5f3a6 7937 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7938 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7939
7940 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7941 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7942 {
5989a64e 7943 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7944 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7945 }
7946
b4f54984 7947 if (dwarf_read_debug)
ed2dc618 7948 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7949
ed2dc618 7950 set_partial_user (dwarf2_per_objfile);
95554aad 7951
d320c2b5
TT
7952 objfile->partial_symtabs->psymtabs_addrmap
7953 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7954 objfile->partial_symtabs->obstack ());
791afaa2
TT
7955 /* At this point we want to keep the address map. */
7956 save_psymtabs_addrmap.release ();
ff013f42 7957
b4f54984 7958 if (dwarf_read_debug)
45cfd468 7959 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7960 objfile_name (objfile));
ae038cb0
DJ
7961}
7962
dee91e82
DE
7963/* Load the partial DIEs for a secondary CU into memory.
7964 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7965
dee91e82
DE
7966static void
7967load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7968{
6751ebae 7969 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7970
7971 if (!reader.dummy_p)
7972 {
7973 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7974 language_minimal);
7975
7976 /* Check if comp unit has_children.
7977 If so, read the rest of the partial symbols from this comp unit.
7978 If not, there's no more debug_info for this comp unit. */
3e225074 7979 if (reader.comp_unit_die->has_children)
c0ab21c2 7980 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7981
7982 reader.keep ();
c0ab21c2 7983 }
ae038cb0
DJ
7984}
7985
ae038cb0 7986static void
ed2dc618 7987read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7988 struct dwarf2_section_info *section,
f1902523 7989 struct dwarf2_section_info *abbrev_section,
b76e467d 7990 unsigned int is_dwz)
ae038cb0 7991{
d521ce57 7992 const gdb_byte *info_ptr;
ed2dc618 7993 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7994
b4f54984 7995 if (dwarf_read_debug)
bf6af496 7996 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7997 section->get_name (),
7998 section->get_file_name ());
bf6af496 7999
96b79293 8000 section->read (objfile);
ae038cb0 8001
36586728 8002 info_ptr = section->buffer;
6e70227d 8003
36586728 8004 while (info_ptr < section->buffer + section->size)
ae038cb0 8005 {
ae038cb0 8006 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8007
9c541725 8008 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8009
f1902523 8010 comp_unit_head cu_header;
ed2dc618
SM
8011 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8012 abbrev_section, info_ptr,
8013 rcuh_kind::COMPILE);
ae038cb0
DJ
8014
8015 /* Save the compilation unit for later lookup. */
f1902523 8016 if (cu_header.unit_type != DW_UT_type)
5989a64e 8017 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8018 else
8019 {
5989a64e 8020 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8021 sig_type->signature = cu_header.signature;
8022 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8023 this_cu = &sig_type->per_cu;
8024 }
8025 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8026 this_cu->sect_off = sect_off;
f1902523 8027 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8028 this_cu->is_dwz = is_dwz;
e3b94546 8029 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8030 this_cu->section = section;
ae038cb0 8031
5989a64e 8032 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8033
8034 info_ptr = info_ptr + this_cu->length;
8035 }
36586728
TT
8036}
8037
8038/* Create a list of all compilation units in OBJFILE.
8039 This is only done for -readnow and building partial symtabs. */
8040
8041static void
ed2dc618 8042create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8043{
5989a64e
SM
8044 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8045 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8046 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8047
b76e467d 8048 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8049 if (dwz != NULL)
ed2dc618 8050 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8051 1);
c906108c
SS
8052}
8053
5734ee8b 8054/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8055 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8056 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8057 DW_AT_ranges). See the comments of add_partial_subprogram on how
8058 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8059
72bf9492
DJ
8060static void
8061scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8062 CORE_ADDR *highpc, int set_addrmap,
8063 struct dwarf2_cu *cu)
c906108c 8064{
72bf9492 8065 struct partial_die_info *pdi;
c906108c 8066
91c24f0a
DC
8067 /* Now, march along the PDI's, descending into ones which have
8068 interesting children but skipping the children of the other ones,
8069 until we reach the end of the compilation unit. */
c906108c 8070
72bf9492 8071 pdi = first_die;
91c24f0a 8072
72bf9492
DJ
8073 while (pdi != NULL)
8074 {
52356b79 8075 pdi->fixup (cu);
c906108c 8076
f55ee35c 8077 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8078 children, so we need to look at them. Ditto for anonymous
8079 enums. */
933c6fe4 8080
72bf9492 8081 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8082 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8083 || pdi->tag == DW_TAG_imported_unit
8084 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8085 {
72bf9492 8086 switch (pdi->tag)
c906108c
SS
8087 {
8088 case DW_TAG_subprogram:
b1dc1806 8089 case DW_TAG_inlined_subroutine:
cdc07690 8090 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8091 break;
72929c62 8092 case DW_TAG_constant:
c906108c
SS
8093 case DW_TAG_variable:
8094 case DW_TAG_typedef:
91c24f0a 8095 case DW_TAG_union_type:
317d2668
TV
8096 if (!pdi->is_declaration
8097 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8098 {
72bf9492 8099 add_partial_symbol (pdi, cu);
63d06c5c
DC
8100 }
8101 break;
c906108c 8102 case DW_TAG_class_type:
680b30c7 8103 case DW_TAG_interface_type:
c906108c 8104 case DW_TAG_structure_type:
72bf9492 8105 if (!pdi->is_declaration)
c906108c 8106 {
72bf9492 8107 add_partial_symbol (pdi, cu);
c906108c 8108 }
b7fee5a3
KS
8109 if ((cu->language == language_rust
8110 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8111 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8112 set_addrmap, cu);
c906108c 8113 break;
91c24f0a 8114 case DW_TAG_enumeration_type:
72bf9492
DJ
8115 if (!pdi->is_declaration)
8116 add_partial_enumeration (pdi, cu);
c906108c
SS
8117 break;
8118 case DW_TAG_base_type:
a02abb62 8119 case DW_TAG_subrange_type:
c906108c 8120 /* File scope base type definitions are added to the partial
c5aa993b 8121 symbol table. */
72bf9492 8122 add_partial_symbol (pdi, cu);
c906108c 8123 break;
d9fa45fe 8124 case DW_TAG_namespace:
cdc07690 8125 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8126 break;
5d7cb8df 8127 case DW_TAG_module:
59c35742
AB
8128 if (!pdi->is_declaration)
8129 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8130 break;
95554aad
TT
8131 case DW_TAG_imported_unit:
8132 {
8133 struct dwarf2_per_cu_data *per_cu;
8134
f4dc4d17
DE
8135 /* For now we don't handle imported units in type units. */
8136 if (cu->per_cu->is_debug_types)
8137 {
8138 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8139 " supported in type units [in module %s]"),
518817b3 8140 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8141 }
8142
e3b94546
SM
8143 per_cu = dwarf2_find_containing_comp_unit
8144 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8145 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8146
8147 /* Go read the partial unit, if needed. */
8148 if (per_cu->v.psymtab == NULL)
135f5437 8149 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8150
ae640021 8151 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8152 }
8153 break;
74921315
KS
8154 case DW_TAG_imported_declaration:
8155 add_partial_symbol (pdi, cu);
8156 break;
c906108c
SS
8157 default:
8158 break;
8159 }
8160 }
8161
72bf9492
DJ
8162 /* If the die has a sibling, skip to the sibling. */
8163
8164 pdi = pdi->die_sibling;
8165 }
8166}
8167
8168/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8169
72bf9492 8170 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8171 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8172 Enumerators are an exception; they use the scope of their parent
8173 enumeration type, i.e. the name of the enumeration type is not
8174 prepended to the enumerator.
91c24f0a 8175
72bf9492
DJ
8176 There are two complexities. One is DW_AT_specification; in this
8177 case "parent" means the parent of the target of the specification,
8178 instead of the direct parent of the DIE. The other is compilers
8179 which do not emit DW_TAG_namespace; in this case we try to guess
8180 the fully qualified name of structure types from their members'
8181 linkage names. This must be done using the DIE's children rather
8182 than the children of any DW_AT_specification target. We only need
8183 to do this for structures at the top level, i.e. if the target of
8184 any DW_AT_specification (if any; otherwise the DIE itself) does not
8185 have a parent. */
8186
8187/* Compute the scope prefix associated with PDI's parent, in
8188 compilation unit CU. The result will be allocated on CU's
8189 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8190 field. NULL is returned if no prefix is necessary. */
15d034d0 8191static const char *
72bf9492
DJ
8192partial_die_parent_scope (struct partial_die_info *pdi,
8193 struct dwarf2_cu *cu)
8194{
15d034d0 8195 const char *grandparent_scope;
72bf9492 8196 struct partial_die_info *parent, *real_pdi;
91c24f0a 8197
72bf9492
DJ
8198 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8199 then this means the parent of the specification DIE. */
8200
8201 real_pdi = pdi;
72bf9492 8202 while (real_pdi->has_specification)
fb816e8b 8203 {
122cf0f2
AB
8204 auto res = find_partial_die (real_pdi->spec_offset,
8205 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8206 real_pdi = res.pdi;
8207 cu = res.cu;
8208 }
72bf9492
DJ
8209
8210 parent = real_pdi->die_parent;
8211 if (parent == NULL)
8212 return NULL;
8213
8214 if (parent->scope_set)
8215 return parent->scope;
8216
52356b79 8217 parent->fixup (cu);
72bf9492 8218
10b3939b 8219 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8220
acebe513
UW
8221 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8222 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8223 Work around this problem here. */
8224 if (cu->language == language_cplus
6e70227d 8225 && parent->tag == DW_TAG_namespace
acebe513
UW
8226 && strcmp (parent->name, "::") == 0
8227 && grandparent_scope == NULL)
8228 {
8229 parent->scope = NULL;
8230 parent->scope_set = 1;
8231 return NULL;
8232 }
8233
0a4b0913 8234 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8235 if (pdi->tag == DW_TAG_enumerator)
8236 /* Enumerators should not get the name of the enumeration as a prefix. */
8237 parent->scope = grandparent_scope;
8238 else if (parent->tag == DW_TAG_namespace
f55ee35c 8239 || parent->tag == DW_TAG_module
72bf9492
DJ
8240 || parent->tag == DW_TAG_structure_type
8241 || parent->tag == DW_TAG_class_type
680b30c7 8242 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8243 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8244 || parent->tag == DW_TAG_enumeration_type
8245 || (cu->language == language_fortran
8246 && parent->tag == DW_TAG_subprogram
8247 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8248 {
8249 if (grandparent_scope == NULL)
8250 parent->scope = parent->name;
8251 else
3e43a32a
MS
8252 parent->scope = typename_concat (&cu->comp_unit_obstack,
8253 grandparent_scope,
f55ee35c 8254 parent->name, 0, cu);
72bf9492 8255 }
72bf9492
DJ
8256 else
8257 {
8258 /* FIXME drow/2004-04-01: What should we be doing with
8259 function-local names? For partial symbols, we should probably be
8260 ignoring them. */
fa9c3fa0
TT
8261 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8262 dwarf_tag_name (parent->tag),
8263 sect_offset_str (pdi->sect_off));
72bf9492 8264 parent->scope = grandparent_scope;
c906108c
SS
8265 }
8266
72bf9492
DJ
8267 parent->scope_set = 1;
8268 return parent->scope;
8269}
8270
8271/* Return the fully scoped name associated with PDI, from compilation unit
8272 CU. The result will be allocated with malloc. */
4568ecf9 8273
43816ebc 8274static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8275partial_die_full_name (struct partial_die_info *pdi,
8276 struct dwarf2_cu *cu)
8277{
15d034d0 8278 const char *parent_scope;
72bf9492 8279
98bfdba5
PA
8280 /* If this is a template instantiation, we can not work out the
8281 template arguments from partial DIEs. So, unfortunately, we have
8282 to go through the full DIEs. At least any work we do building
8283 types here will be reused if full symbols are loaded later. */
8284 if (pdi->has_template_arguments)
8285 {
52356b79 8286 pdi->fixup (cu);
98bfdba5
PA
8287
8288 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8289 {
8290 struct die_info *die;
8291 struct attribute attr;
8292 struct dwarf2_cu *ref_cu = cu;
8293
b64f50a1 8294 /* DW_FORM_ref_addr is using section offset. */
b4069958 8295 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8296 attr.form = DW_FORM_ref_addr;
9c541725 8297 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8298 die = follow_die_ref (NULL, &attr, &ref_cu);
8299
43816ebc 8300 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8301 }
8302 }
8303
72bf9492
DJ
8304 parent_scope = partial_die_parent_scope (pdi, cu);
8305 if (parent_scope == NULL)
8306 return NULL;
8307 else
43816ebc
TT
8308 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8309 pdi->name, 0, cu));
c906108c
SS
8310}
8311
8312static void
72bf9492 8313add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8314{
518817b3
SM
8315 struct dwarf2_per_objfile *dwarf2_per_objfile
8316 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8317 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8318 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8319 CORE_ADDR addr = 0;
15d034d0 8320 const char *actual_name = NULL;
e142c38c
DJ
8321 CORE_ADDR baseaddr;
8322
b3b3bada 8323 baseaddr = objfile->text_section_offset ();
c906108c 8324
43816ebc
TT
8325 gdb::unique_xmalloc_ptr<char> built_actual_name
8326 = partial_die_full_name (pdi, cu);
15d034d0 8327 if (built_actual_name != NULL)
43816ebc 8328 actual_name = built_actual_name.get ();
63d06c5c 8329
72bf9492
DJ
8330 if (actual_name == NULL)
8331 actual_name = pdi->name;
8332
76e288d1
TT
8333 partial_symbol psymbol;
8334 memset (&psymbol, 0, sizeof (psymbol));
8335 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8336 psymbol.ginfo.section = -1;
8337
8338 /* The code below indicates that the psymbol should be installed by
8339 setting this. */
8340 gdb::optional<psymbol_placement> where;
8341
c906108c
SS
8342 switch (pdi->tag)
8343 {
b1dc1806 8344 case DW_TAG_inlined_subroutine:
c906108c 8345 case DW_TAG_subprogram:
79748972
TT
8346 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8347 - baseaddr);
0a4b0913
AB
8348 if (pdi->is_external
8349 || cu->language == language_ada
8350 || (cu->language == language_fortran
8351 && pdi->die_parent != NULL
8352 && pdi->die_parent->tag == DW_TAG_subprogram))
8353 {
8354 /* Normally, only "external" DIEs are part of the global scope.
8355 But in Ada and Fortran, we want to be able to access nested
8356 procedures globally. So all Ada and Fortran subprograms are
8357 stored in the global scope. */
76e288d1 8358 where = psymbol_placement::GLOBAL;
c906108c
SS
8359 }
8360 else
76e288d1
TT
8361 where = psymbol_placement::STATIC;
8362
8363 psymbol.domain = VAR_DOMAIN;
8364 psymbol.aclass = LOC_BLOCK;
8365 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8366 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8367
8368 if (pdi->main_subprogram && actual_name != NULL)
8369 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8370 break;
72929c62 8371 case DW_TAG_constant:
76e288d1
TT
8372 psymbol.domain = VAR_DOMAIN;
8373 psymbol.aclass = LOC_STATIC;
8374 where = (pdi->is_external
8375 ? psymbol_placement::GLOBAL
8376 : psymbol_placement::STATIC);
72929c62 8377 break;
c906108c 8378 case DW_TAG_variable:
95554aad
TT
8379 if (pdi->d.locdesc)
8380 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8381
95554aad 8382 if (pdi->d.locdesc
caac4577 8383 && addr == 0
5989a64e 8384 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8385 {
8386 /* A global or static variable may also have been stripped
8387 out by the linker if unused, in which case its address
8388 will be nullified; do not add such variables into partial
8389 symbol table then. */
8390 }
8391 else if (pdi->is_external)
c906108c
SS
8392 {
8393 /* Global Variable.
8394 Don't enter into the minimal symbol tables as there is
8395 a minimal symbol table entry from the ELF symbols already.
8396 Enter into partial symbol table if it has a location
8397 descriptor or a type.
8398 If the location descriptor is missing, new_symbol will create
8399 a LOC_UNRESOLVED symbol, the address of the variable will then
8400 be determined from the minimal symbol table whenever the variable
8401 is referenced.
8402 The address for the partial symbol table entry is not
8403 used by GDB, but it comes in handy for debugging partial symbol
8404 table building. */
8405
95554aad 8406 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8407 {
8408 psymbol.domain = VAR_DOMAIN;
8409 psymbol.aclass = LOC_STATIC;
8410 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8411 psymbol.ginfo.value.address = addr;
8412 where = psymbol_placement::GLOBAL;
8413 }
c906108c
SS
8414 }
8415 else
8416 {
ff908ebf
AW
8417 int has_loc = pdi->d.locdesc != NULL;
8418
8419 /* Static Variable. Skip symbols whose value we cannot know (those
8420 without location descriptors or constant values). */
8421 if (!has_loc && !pdi->has_const_value)
43816ebc 8422 return;
ff908ebf 8423
76e288d1
TT
8424 psymbol.domain = VAR_DOMAIN;
8425 psymbol.aclass = LOC_STATIC;
8426 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8427 if (has_loc)
8428 psymbol.ginfo.value.address = addr;
8429 where = psymbol_placement::STATIC;
c906108c
SS
8430 }
8431 break;
8432 case DW_TAG_typedef:
8433 case DW_TAG_base_type:
a02abb62 8434 case DW_TAG_subrange_type:
76e288d1
TT
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_TYPEDEF;
8437 where = psymbol_placement::STATIC;
c906108c 8438 break;
74921315 8439 case DW_TAG_imported_declaration:
72bf9492 8440 case DW_TAG_namespace:
76e288d1
TT
8441 psymbol.domain = VAR_DOMAIN;
8442 psymbol.aclass = LOC_TYPEDEF;
8443 where = psymbol_placement::GLOBAL;
72bf9492 8444 break;
530e8392 8445 case DW_TAG_module:
a5fd13a9
BH
8446 /* With Fortran 77 there might be a "BLOCK DATA" module
8447 available without any name. If so, we skip the module as it
8448 doesn't bring any value. */
8449 if (actual_name != nullptr)
76e288d1
TT
8450 {
8451 psymbol.domain = MODULE_DOMAIN;
8452 psymbol.aclass = LOC_TYPEDEF;
8453 where = psymbol_placement::GLOBAL;
8454 }
530e8392 8455 break;
c906108c 8456 case DW_TAG_class_type:
680b30c7 8457 case DW_TAG_interface_type:
c906108c
SS
8458 case DW_TAG_structure_type:
8459 case DW_TAG_union_type:
8460 case DW_TAG_enumeration_type:
fa4028e9
JB
8461 /* Skip external references. The DWARF standard says in the section
8462 about "Structure, Union, and Class Type Entries": "An incomplete
8463 structure, union or class type is represented by a structure,
8464 union or class entry that does not have a byte size attribute
8465 and that has a DW_AT_declaration attribute." */
8466 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8467 return;
fa4028e9 8468
63d06c5c
DC
8469 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8470 static vs. global. */
76e288d1
TT
8471 psymbol.domain = STRUCT_DOMAIN;
8472 psymbol.aclass = LOC_TYPEDEF;
8473 where = (cu->language == language_cplus
8474 ? psymbol_placement::GLOBAL
8475 : psymbol_placement::STATIC);
c906108c
SS
8476 break;
8477 case DW_TAG_enumerator:
76e288d1
TT
8478 psymbol.domain = VAR_DOMAIN;
8479 psymbol.aclass = LOC_CONST;
8480 where = (cu->language == language_cplus
8481 ? psymbol_placement::GLOBAL
8482 : psymbol_placement::STATIC);
c906108c
SS
8483 break;
8484 default:
8485 break;
8486 }
76e288d1
TT
8487
8488 if (where.has_value ())
8489 {
f049a313
TT
8490 if (built_actual_name != nullptr)
8491 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8492 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8493 psymbol.ginfo.set_linkage_name (actual_name);
8494 else
8495 {
8496 psymbol.ginfo.set_demangled_name (actual_name,
8497 &objfile->objfile_obstack);
8498 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8499 }
76e288d1
TT
8500 add_psymbol_to_list (psymbol, *where, objfile);
8501 }
c906108c
SS
8502}
8503
5c4e30ca
DC
8504/* Read a partial die corresponding to a namespace; also, add a symbol
8505 corresponding to that namespace to the symbol table. NAMESPACE is
8506 the name of the enclosing namespace. */
91c24f0a 8507
72bf9492
DJ
8508static void
8509add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8510 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8511 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8512{
72bf9492 8513 /* Add a symbol for the namespace. */
e7c27a73 8514
72bf9492 8515 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8516
8517 /* Now scan partial symbols in that namespace. */
8518
91c24f0a 8519 if (pdi->has_children)
cdc07690 8520 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8521}
8522
5d7cb8df
JK
8523/* Read a partial die corresponding to a Fortran module. */
8524
8525static void
8526add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8527 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8528{
530e8392
KB
8529 /* Add a symbol for the namespace. */
8530
8531 add_partial_symbol (pdi, cu);
8532
f55ee35c 8533 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8534
8535 if (pdi->has_children)
cdc07690 8536 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8537}
8538
b1dc1806
XR
8539/* Read a partial die corresponding to a subprogram or an inlined
8540 subprogram and create a partial symbol for that subprogram.
8541 When the CU language allows it, this routine also defines a partial
8542 symbol for each nested subprogram that this subprogram contains.
8543 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8544 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8545
cdc07690
YQ
8546 PDI may also be a lexical block, in which case we simply search
8547 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8548 Again, this is only performed when the CU language allows this
8549 type of definitions. */
8550
8551static void
8552add_partial_subprogram (struct partial_die_info *pdi,
8553 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8554 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8555{
b1dc1806 8556 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8557 {
8558 if (pdi->has_pc_info)
8559 {
8560 if (pdi->lowpc < *lowpc)
8561 *lowpc = pdi->lowpc;
8562 if (pdi->highpc > *highpc)
8563 *highpc = pdi->highpc;
cdc07690 8564 if (set_addrmap)
5734ee8b 8565 {
518817b3 8566 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8567 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8568 CORE_ADDR baseaddr;
b926417a
TT
8569 CORE_ADDR this_highpc;
8570 CORE_ADDR this_lowpc;
5734ee8b 8571
b3b3bada 8572 baseaddr = objfile->text_section_offset ();
b926417a
TT
8573 this_lowpc
8574 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8575 pdi->lowpc + baseaddr)
8576 - baseaddr);
8577 this_highpc
8578 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8579 pdi->highpc + baseaddr)
8580 - baseaddr);
d320c2b5 8581 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8582 this_lowpc, this_highpc - 1,
9291a0cd 8583 cu->per_cu->v.psymtab);
5734ee8b 8584 }
481860b3
GB
8585 }
8586
8587 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8588 {
bc30ff58 8589 if (!pdi->is_declaration)
e8d05480
JB
8590 /* Ignore subprogram DIEs that do not have a name, they are
8591 illegal. Do not emit a complaint at this point, we will
8592 do so when we convert this psymtab into a symtab. */
8593 if (pdi->name)
8594 add_partial_symbol (pdi, cu);
bc30ff58
JB
8595 }
8596 }
6e70227d 8597
bc30ff58
JB
8598 if (! pdi->has_children)
8599 return;
8600
0a4b0913 8601 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8602 {
8603 pdi = pdi->die_child;
8604 while (pdi != NULL)
8605 {
52356b79 8606 pdi->fixup (cu);
bc30ff58 8607 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8608 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8609 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8610 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8611 pdi = pdi->die_sibling;
8612 }
8613 }
8614}
8615
91c24f0a
DC
8616/* Read a partial die corresponding to an enumeration type. */
8617
72bf9492
DJ
8618static void
8619add_partial_enumeration (struct partial_die_info *enum_pdi,
8620 struct dwarf2_cu *cu)
91c24f0a 8621{
72bf9492 8622 struct partial_die_info *pdi;
91c24f0a
DC
8623
8624 if (enum_pdi->name != NULL)
72bf9492
DJ
8625 add_partial_symbol (enum_pdi, cu);
8626
8627 pdi = enum_pdi->die_child;
8628 while (pdi)
91c24f0a 8629 {
72bf9492 8630 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8631 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8632 else
72bf9492
DJ
8633 add_partial_symbol (pdi, cu);
8634 pdi = pdi->die_sibling;
91c24f0a 8635 }
91c24f0a
DC
8636}
8637
6caca83c
CC
8638/* Return the initial uleb128 in the die at INFO_PTR. */
8639
8640static unsigned int
d521ce57 8641peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8642{
8643 unsigned int bytes_read;
8644
8645 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8646}
8647
685af9cd
TT
8648/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8649 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8650
4bb7a0a7
DJ
8651 Return the corresponding abbrev, or NULL if the number is zero (indicating
8652 an empty DIE). In either case *BYTES_READ will be set to the length of
8653 the initial number. */
8654
8655static struct abbrev_info *
685af9cd
TT
8656peek_die_abbrev (const die_reader_specs &reader,
8657 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8658{
685af9cd 8659 dwarf2_cu *cu = reader.cu;
518817b3 8660 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8661 unsigned int abbrev_number
8662 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8663
8664 if (abbrev_number == 0)
8665 return NULL;
8666
685af9cd 8667 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8668 if (!abbrev)
8669 {
422b9917 8670 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8671 " at offset %s [in module %s]"),
422b9917 8672 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8673 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8674 }
8675
8676 return abbrev;
8677}
8678
93311388
DE
8679/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8680 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8681 DIE. Any children of the skipped DIEs will also be skipped. */
8682
d521ce57
TT
8683static const gdb_byte *
8684skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8685{
4bb7a0a7
DJ
8686 while (1)
8687 {
685af9cd
TT
8688 unsigned int bytes_read;
8689 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8690
4bb7a0a7
DJ
8691 if (abbrev == NULL)
8692 return info_ptr + bytes_read;
8693 else
dee91e82 8694 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8695 }
8696}
8697
93311388
DE
8698/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8699 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8700 abbrev corresponding to that skipped uleb128 should be passed in
8701 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8702 children. */
8703
d521ce57
TT
8704static const gdb_byte *
8705skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8706 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8707{
8708 unsigned int bytes_read;
8709 struct attribute attr;
dee91e82
DE
8710 bfd *abfd = reader->abfd;
8711 struct dwarf2_cu *cu = reader->cu;
d521ce57 8712 const gdb_byte *buffer = reader->buffer;
f664829e 8713 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8714 unsigned int form, i;
8715
8716 for (i = 0; i < abbrev->num_attrs; i++)
8717 {
8718 /* The only abbrev we care about is DW_AT_sibling. */
8719 if (abbrev->attrs[i].name == DW_AT_sibling)
8720 {
18a8505e
AT
8721 bool ignored;
8722 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8723 &ignored);
4bb7a0a7 8724 if (attr.form == DW_FORM_ref_addr)
b98664d3 8725 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8726 else
b9502d3f 8727 {
0826b30a 8728 sect_offset off = attr.get_ref_die_offset ();
9c541725 8729 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8730
8731 if (sibling_ptr < info_ptr)
b98664d3 8732 complaint (_("DW_AT_sibling points backwards"));
22869d73 8733 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8734 reader->die_section->overflow_complaint ();
b9502d3f
WN
8735 else
8736 return sibling_ptr;
8737 }
4bb7a0a7
DJ
8738 }
8739
8740 /* If it isn't DW_AT_sibling, skip this attribute. */
8741 form = abbrev->attrs[i].form;
8742 skip_attribute:
8743 switch (form)
8744 {
4bb7a0a7 8745 case DW_FORM_ref_addr:
ae411497
TT
8746 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8747 and later it is offset sized. */
8748 if (cu->header.version == 2)
8749 info_ptr += cu->header.addr_size;
8750 else
8751 info_ptr += cu->header.offset_size;
8752 break;
36586728
TT
8753 case DW_FORM_GNU_ref_alt:
8754 info_ptr += cu->header.offset_size;
8755 break;
ae411497 8756 case DW_FORM_addr:
4bb7a0a7
DJ
8757 info_ptr += cu->header.addr_size;
8758 break;
8759 case DW_FORM_data1:
8760 case DW_FORM_ref1:
8761 case DW_FORM_flag:
8fe0f950 8762 case DW_FORM_strx1:
4bb7a0a7
DJ
8763 info_ptr += 1;
8764 break;
2dc7f7b3 8765 case DW_FORM_flag_present:
43988095 8766 case DW_FORM_implicit_const:
2dc7f7b3 8767 break;
4bb7a0a7
DJ
8768 case DW_FORM_data2:
8769 case DW_FORM_ref2:
8fe0f950 8770 case DW_FORM_strx2:
4bb7a0a7
DJ
8771 info_ptr += 2;
8772 break;
8fe0f950
AT
8773 case DW_FORM_strx3:
8774 info_ptr += 3;
8775 break;
4bb7a0a7
DJ
8776 case DW_FORM_data4:
8777 case DW_FORM_ref4:
8fe0f950 8778 case DW_FORM_strx4:
4bb7a0a7
DJ
8779 info_ptr += 4;
8780 break;
8781 case DW_FORM_data8:
8782 case DW_FORM_ref8:
55f1336d 8783 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8784 info_ptr += 8;
8785 break;
0224619f
JK
8786 case DW_FORM_data16:
8787 info_ptr += 16;
8788 break;
4bb7a0a7 8789 case DW_FORM_string:
9b1c24c8 8790 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8791 info_ptr += bytes_read;
8792 break;
2dc7f7b3 8793 case DW_FORM_sec_offset:
4bb7a0a7 8794 case DW_FORM_strp:
36586728 8795 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8796 info_ptr += cu->header.offset_size;
8797 break;
2dc7f7b3 8798 case DW_FORM_exprloc:
4bb7a0a7
DJ
8799 case DW_FORM_block:
8800 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8801 info_ptr += bytes_read;
8802 break;
8803 case DW_FORM_block1:
8804 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8805 break;
8806 case DW_FORM_block2:
8807 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8808 break;
8809 case DW_FORM_block4:
8810 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8811 break;
336d760d 8812 case DW_FORM_addrx:
cf532bd1 8813 case DW_FORM_strx:
4bb7a0a7
DJ
8814 case DW_FORM_sdata:
8815 case DW_FORM_udata:
8816 case DW_FORM_ref_udata:
3019eac3
DE
8817 case DW_FORM_GNU_addr_index:
8818 case DW_FORM_GNU_str_index:
18a8505e 8819 case DW_FORM_rnglistx:
41144253 8820 case DW_FORM_loclistx:
d521ce57 8821 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8822 break;
8823 case DW_FORM_indirect:
8824 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8825 info_ptr += bytes_read;
8826 /* We need to continue parsing from here, so just go back to
8827 the top. */
8828 goto skip_attribute;
8829
8830 default:
3e43a32a
MS
8831 error (_("Dwarf Error: Cannot handle %s "
8832 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8833 dwarf_form_name (form),
8834 bfd_get_filename (abfd));
8835 }
8836 }
8837
8838 if (abbrev->has_children)
dee91e82 8839 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8840 else
8841 return info_ptr;
8842}
8843
93311388 8844/* Locate ORIG_PDI's sibling.
dee91e82 8845 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8846
d521ce57 8847static const gdb_byte *
dee91e82
DE
8848locate_pdi_sibling (const struct die_reader_specs *reader,
8849 struct partial_die_info *orig_pdi,
d521ce57 8850 const gdb_byte *info_ptr)
91c24f0a
DC
8851{
8852 /* Do we know the sibling already? */
72bf9492 8853
91c24f0a
DC
8854 if (orig_pdi->sibling)
8855 return orig_pdi->sibling;
8856
8857 /* Are there any children to deal with? */
8858
8859 if (!orig_pdi->has_children)
8860 return info_ptr;
8861
4bb7a0a7 8862 /* Skip the children the long way. */
91c24f0a 8863
dee91e82 8864 return skip_children (reader, info_ptr);
91c24f0a
DC
8865}
8866
257e7a09 8867/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8868 not NULL. */
c906108c 8869
891813be
TT
8870void
8871dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8872{
ed2dc618
SM
8873 struct dwarf2_per_objfile *dwarf2_per_objfile
8874 = get_dwarf2_per_objfile (objfile);
8875
af758d11
SM
8876 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8877
077cbab2
TT
8878 /* If this psymtab is constructed from a debug-only objfile, the
8879 has_section_at_zero flag will not necessarily be correct. We
8880 can get the correct value for this flag by looking at the data
8881 associated with the (presumably stripped) associated objfile. */
8882 if (objfile->separate_debug_objfile_backlink)
c906108c 8883 {
077cbab2
TT
8884 struct dwarf2_per_objfile *dpo_backlink
8885 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8886
5989a64e
SM
8887 dwarf2_per_objfile->per_bfd->has_section_at_zero
8888 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8889 }
98bfdba5 8890
8566b89b 8891 expand_psymtab (objfile);
95554aad 8892
ed2dc618 8893 process_cu_includes (dwarf2_per_objfile);
c906108c 8894}
9cdd5dbd
DE
8895\f
8896/* Reading in full CUs. */
c906108c 8897
10b3939b
DJ
8898/* Add PER_CU to the queue. */
8899
8900static void
95554aad
TT
8901queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8902 enum language pretend_language)
10b3939b 8903{
10b3939b 8904 per_cu->queued = 1;
5989a64e 8905 per_cu->dwarf2_per_objfile->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8906}
8907
89e63ee4
DE
8908/* If PER_CU is not yet queued, add it to the queue.
8909 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8910 dependency.
0907af0c 8911 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8912 meaning either PER_CU is already queued or it is already loaded.
8913
8914 N.B. There is an invariant here that if a CU is queued then it is loaded.
8915 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8916
8917static int
89e63ee4 8918maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8919 struct dwarf2_per_cu_data *per_cu,
8920 enum language pretend_language)
8921{
8922 /* We may arrive here during partial symbol reading, if we need full
8923 DIEs to process an unusual case (e.g. template arguments). Do
8924 not queue PER_CU, just tell our caller to load its DIEs. */
5989a64e 8925 if (per_cu->dwarf2_per_objfile->per_bfd->reading_partial_symbols)
0907af0c
DE
8926 {
8927 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8928 return 1;
8929 return 0;
8930 }
8931
8932 /* Mark the dependence relation so that we don't flush PER_CU
8933 too early. */
89e63ee4
DE
8934 if (dependent_cu != NULL)
8935 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8936
8937 /* If it's already on the queue, we have nothing to do. */
8938 if (per_cu->queued)
8939 return 0;
8940
8941 /* If the compilation unit is already loaded, just mark it as
8942 used. */
8943 if (per_cu->cu != NULL)
8944 {
8945 per_cu->cu->last_used = 0;
8946 return 0;
8947 }
8948
8949 /* Add it to the queue. */
8950 queue_comp_unit (per_cu, pretend_language);
8951
8952 return 1;
8953}
8954
10b3939b
DJ
8955/* Process the queue. */
8956
8957static void
ed2dc618 8958process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8959{
b4f54984 8960 if (dwarf_read_debug)
45cfd468
DE
8961 {
8962 fprintf_unfiltered (gdb_stdlog,
8963 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8964 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8965 }
8966
03dd20cc
DJ
8967 /* The queue starts out with one item, but following a DIE reference
8968 may load a new CU, adding it to the end of the queue. */
5989a64e 8969 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8970 {
5989a64e 8971 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8972
af758d11 8973 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8974 /* Skip dummy CUs. */
39856def 8975 && item.per_cu->cu != NULL)
f4dc4d17 8976 {
39856def 8977 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8978 unsigned int debug_print_threshold;
247f5c4f 8979 char buf[100];
f4dc4d17 8980
247f5c4f 8981 if (per_cu->is_debug_types)
f4dc4d17 8982 {
247f5c4f
DE
8983 struct signatured_type *sig_type =
8984 (struct signatured_type *) per_cu;
8985
9d8780f0 8986 sprintf (buf, "TU %s at offset %s",
73be47f5 8987 hex_string (sig_type->signature),
9d8780f0 8988 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8989 /* There can be 100s of TUs.
8990 Only print them in verbose mode. */
8991 debug_print_threshold = 2;
f4dc4d17 8992 }
247f5c4f 8993 else
73be47f5 8994 {
9d8780f0
SM
8995 sprintf (buf, "CU at offset %s",
8996 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8997 debug_print_threshold = 1;
8998 }
247f5c4f 8999
b4f54984 9000 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9001 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9002
9003 if (per_cu->is_debug_types)
39856def 9004 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9005 else
39856def 9006 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9007
b4f54984 9008 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9009 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9010 }
10b3939b 9011
39856def 9012 item.per_cu->queued = 0;
5989a64e 9013 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9014 }
9015
b4f54984 9016 if (dwarf_read_debug)
45cfd468
DE
9017 {
9018 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9019 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9020 }
10b3939b
DJ
9021}
9022
10b3939b
DJ
9023/* Read in full symbols for PST, and anything it depends on. */
9024
8566b89b
TT
9025void
9026dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9027{
af758d11 9028 gdb_assert (!readin_p (objfile));
95554aad 9029
48993951 9030 expand_dependencies (objfile);
aaa75496 9031
b83470bf 9032 dw2_do_instantiate_symtab (per_cu_data, false);
5717c425 9033 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9034}
9035
af758d11
SM
9036/* See psympriv.h. */
9037
9038bool
9039dwarf2_psymtab::readin_p (struct objfile *objfile) const
9040{
9041 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9042 return per_objfile->symtab_set_p (per_cu_data);
9043}
9044
9045/* See psympriv.h. */
9046
9047compunit_symtab *
9048dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9049{
9050 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9051 return per_objfile->get_symtab (per_cu_data);
9052}
9053
dee91e82
DE
9054/* Trivial hash function for die_info: the hash value of a DIE
9055 is its offset in .debug_info for this objfile. */
10b3939b 9056
dee91e82
DE
9057static hashval_t
9058die_hash (const void *item)
10b3939b 9059{
9a3c8263 9060 const struct die_info *die = (const struct die_info *) item;
6502dd73 9061
9c541725 9062 return to_underlying (die->sect_off);
dee91e82 9063}
63d06c5c 9064
dee91e82
DE
9065/* Trivial comparison function for die_info structures: two DIEs
9066 are equal if they have the same offset. */
98bfdba5 9067
dee91e82
DE
9068static int
9069die_eq (const void *item_lhs, const void *item_rhs)
9070{
9a3c8263
SM
9071 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9072 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9073
9c541725 9074 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9075}
c906108c 9076
c0ab21c2 9077/* Load the DIEs associated with PER_CU into memory. */
c906108c 9078
dee91e82 9079static void
c0ab21c2
TT
9080load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9081 bool skip_partial,
9082 enum language pretend_language)
dee91e82 9083{
c0ab21c2
TT
9084 gdb_assert (! this_cu->is_debug_types);
9085
6751ebae 9086 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
9087 if (reader.dummy_p)
9088 return;
9089
9090 struct dwarf2_cu *cu = reader.cu;
9091 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9092
dee91e82
DE
9093 gdb_assert (cu->die_hash == NULL);
9094 cu->die_hash =
9095 htab_create_alloc_ex (cu->header.length / 12,
9096 die_hash,
9097 die_eq,
9098 NULL,
9099 &cu->comp_unit_obstack,
9100 hashtab_obstack_allocate,
9101 dummy_obstack_deallocate);
e142c38c 9102
3e225074 9103 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9104 reader.comp_unit_die->child
9105 = read_die_and_siblings (&reader, reader.info_ptr,
9106 &info_ptr, reader.comp_unit_die);
9107 cu->dies = reader.comp_unit_die;
dee91e82 9108 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9109
9110 /* We try not to read any attributes in this function, because not
9cdd5dbd 9111 all CUs needed for references have been loaded yet, and symbol
10b3939b 9112 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9113 or we won't be able to build types correctly.
9114 Similarly, if we do not read the producer, we can not apply
9115 producer-specific interpretation. */
c0ab21c2 9116 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9117
9118 reader.keep ();
10b3939b
DJ
9119}
9120
3da10d80
KS
9121/* Add a DIE to the delayed physname list. */
9122
9123static void
9124add_to_method_list (struct type *type, int fnfield_index, int index,
9125 const char *name, struct die_info *die,
9126 struct dwarf2_cu *cu)
9127{
9128 struct delayed_method_info mi;
9129 mi.type = type;
9130 mi.fnfield_index = fnfield_index;
9131 mi.index = index;
9132 mi.name = name;
9133 mi.die = die;
c89b44cd 9134 cu->method_list.push_back (mi);
3da10d80
KS
9135}
9136
3693fdb3
PA
9137/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9138 "const" / "volatile". If so, decrements LEN by the length of the
9139 modifier and return true. Otherwise return false. */
9140
9141template<size_t N>
9142static bool
9143check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9144{
9145 size_t mod_len = sizeof (mod) - 1;
9146 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9147 {
9148 len -= mod_len;
9149 return true;
9150 }
9151 return false;
9152}
9153
3da10d80
KS
9154/* Compute the physnames of any methods on the CU's method list.
9155
9156 The computation of method physnames is delayed in order to avoid the
9157 (bad) condition that one of the method's formal parameters is of an as yet
9158 incomplete type. */
9159
9160static void
9161compute_delayed_physnames (struct dwarf2_cu *cu)
9162{
3693fdb3 9163 /* Only C++ delays computing physnames. */
c89b44cd 9164 if (cu->method_list.empty ())
3693fdb3
PA
9165 return;
9166 gdb_assert (cu->language == language_cplus);
9167
52941706 9168 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9169 {
1d06ead6 9170 const char *physname;
3da10d80 9171 struct fn_fieldlist *fn_flp
c89b44cd
TT
9172 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9173 physname = dwarf2_physname (mi.name, mi.die, cu);
9174 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9175 = physname ? physname : "";
3693fdb3
PA
9176
9177 /* Since there's no tag to indicate whether a method is a
9178 const/volatile overload, extract that information out of the
9179 demangled name. */
9180 if (physname != NULL)
9181 {
9182 size_t len = strlen (physname);
9183
9184 while (1)
9185 {
9186 if (physname[len] == ')') /* shortcut */
9187 break;
9188 else if (check_modifier (physname, len, " const"))
c89b44cd 9189 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9190 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9191 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9192 else
9193 break;
9194 }
9195 }
3da10d80 9196 }
c89b44cd
TT
9197
9198 /* The list is no longer needed. */
9199 cu->method_list.clear ();
3da10d80
KS
9200}
9201
a766d390
DE
9202/* Go objects should be embedded in a DW_TAG_module DIE,
9203 and it's not clear if/how imported objects will appear.
9204 To keep Go support simple until that's worked out,
9205 go back through what we've read and create something usable.
9206 We could do this while processing each DIE, and feels kinda cleaner,
9207 but that way is more invasive.
9208 This is to, for example, allow the user to type "p var" or "b main"
9209 without having to specify the package name, and allow lookups
9210 of module.object to work in contexts that use the expression
9211 parser. */
9212
9213static void
9214fixup_go_packaging (struct dwarf2_cu *cu)
9215{
421d1616 9216 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9217 struct pending *list;
9218 int i;
9219
c24bdb02 9220 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9221 list != NULL;
9222 list = list->next)
a766d390
DE
9223 {
9224 for (i = 0; i < list->nsyms; ++i)
9225 {
9226 struct symbol *sym = list->symbol[i];
9227
c1b5c1eb 9228 if (sym->language () == language_go
a766d390
DE
9229 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9230 {
421d1616
TT
9231 gdb::unique_xmalloc_ptr<char> this_package_name
9232 (go_symbol_package_name (sym));
a766d390
DE
9233
9234 if (this_package_name == NULL)
9235 continue;
9236 if (package_name == NULL)
421d1616 9237 package_name = std::move (this_package_name);
a766d390
DE
9238 else
9239 {
518817b3
SM
9240 struct objfile *objfile
9241 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9242 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9243 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9244 (symbol_symtab (sym) != NULL
9245 ? symtab_to_filename_for_display
9246 (symbol_symtab (sym))
e3b94546 9247 : objfile_name (objfile)),
421d1616 9248 this_package_name.get (), package_name.get ());
a766d390
DE
9249 }
9250 }
9251 }
9252 }
9253
9254 if (package_name != NULL)
9255 {
518817b3 9256 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9257 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9258 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9259 saved_package_name);
a766d390
DE
9260 struct symbol *sym;
9261
8c14c3a3 9262 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9263 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9264 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9265 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9266 e.g., "main" finds the "main" module and not C's main(). */
9267 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9268 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9269 SYMBOL_TYPE (sym) = type;
9270
c24bdb02 9271 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9272 }
9273}
9274
c9317f21
TT
9275/* Allocate a fully-qualified name consisting of the two parts on the
9276 obstack. */
9277
9278static const char *
9279rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9280{
9281 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9282}
9283
9c6a1327
TT
9284/* A helper that allocates a variant part to attach to a Rust enum
9285 type. OBSTACK is where the results should be allocated. TYPE is
9286 the type we're processing. DISCRIMINANT_INDEX is the index of the
9287 discriminant. It must be the index of one of the fields of TYPE.
9288 DEFAULT_INDEX is the index of the default field; or -1 if there is
9289 no default. RANGES is indexed by "effective" field number (the
9290 field index, but omitting the discriminant and default fields) and
9291 must hold the discriminant values used by the variants. Note that
9292 RANGES must have a lifetime at least as long as OBSTACK -- either
9293 already allocated on it, or static. */
c9317f21 9294
9c6a1327
TT
9295static void
9296alloc_rust_variant (struct obstack *obstack, struct type *type,
9297 int discriminant_index, int default_index,
9298 gdb::array_view<discriminant_range> ranges)
9299{
9300 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9301 must be handled by the caller. */
9302 gdb_assert (discriminant_index >= 0
1f704f76 9303 && discriminant_index < type->num_fields ());
c9317f21 9304 gdb_assert (default_index == -1
1f704f76 9305 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9306
9c6a1327 9307 /* We have one variant for each non-discriminant field. */
1f704f76 9308 int n_variants = type->num_fields () - 1;
c9317f21 9309
9c6a1327
TT
9310 variant *variants = new (obstack) variant[n_variants];
9311 int var_idx = 0;
9312 int range_idx = 0;
1f704f76 9313 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9314 {
9315 if (i == discriminant_index)
9316 continue;
c9317f21 9317
9c6a1327
TT
9318 variants[var_idx].first_field = i;
9319 variants[var_idx].last_field = i + 1;
9320
9321 /* The default field does not need a range, but other fields do.
9322 We skipped the discriminant above. */
9323 if (i != default_index)
9324 {
9325 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9326 ++range_idx;
9327 }
c9317f21 9328
9c6a1327
TT
9329 ++var_idx;
9330 }
9331
9332 gdb_assert (range_idx == ranges.size ());
9333 gdb_assert (var_idx == n_variants);
9334
9335 variant_part *part = new (obstack) variant_part;
9336 part->discriminant_index = discriminant_index;
9337 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9338 discriminant_index));
9339 part->variants = gdb::array_view<variant> (variants, n_variants);
9340
9341 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9342 gdb::array_view<variant_part> *prop_value
9343 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9344
9c6a1327
TT
9345 struct dynamic_prop prop;
9346 prop.kind = PROP_VARIANT_PARTS;
9347 prop.data.variant_parts = prop_value;
9348
5c54719c 9349 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9350}
9351
9352/* Some versions of rustc emitted enums in an unusual way.
9353
9354 Ordinary enums were emitted as unions. The first element of each
9355 structure in the union was named "RUST$ENUM$DISR". This element
9356 held the discriminant.
9357
9358 These versions of Rust also implemented the "non-zero"
9359 optimization. When the enum had two values, and one is empty and
9360 the other holds a pointer that cannot be zero, the pointer is used
9361 as the discriminant, with a zero value meaning the empty variant.
9362 Here, the union's first member is of the form
9363 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9364 where the fieldnos are the indices of the fields that should be
9365 traversed in order to find the field (which may be several fields deep)
9366 and the variantname is the name of the variant of the case when the
9367 field is zero.
9368
9369 This function recognizes whether TYPE is of one of these forms,
9370 and, if so, smashes it to be a variant type. */
9371
9372static void
9373quirk_rust_enum (struct type *type, struct objfile *objfile)
9374{
78134374 9375 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9376
9377 /* We don't need to deal with empty enums. */
1f704f76 9378 if (type->num_fields () == 0)
c9317f21
TT
9379 return;
9380
9381#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9382 if (type->num_fields () == 1
c9317f21
TT
9383 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9384 {
9385 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9386
9387 /* Decode the field name to find the offset of the
9388 discriminant. */
9389 ULONGEST bit_offset = 0;
9390 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9391 while (name[0] >= '0' && name[0] <= '9')
9392 {
9393 char *tail;
9394 unsigned long index = strtoul (name, &tail, 10);
9395 name = tail;
9396 if (*name != '$'
1f704f76 9397 || index >= field_type->num_fields ()
c9317f21
TT
9398 || (TYPE_FIELD_LOC_KIND (field_type, index)
9399 != FIELD_LOC_KIND_BITPOS))
9400 {
b98664d3 9401 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9402 "[in module %s]"),
9403 TYPE_FIELD_NAME (type, 0),
9404 objfile_name (objfile));
9405 return;
9406 }
9407 ++name;
9408
9409 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9410 field_type = TYPE_FIELD_TYPE (field_type, index);
9411 }
9412
9c6a1327
TT
9413 /* Smash this type to be a structure type. We have to do this
9414 because the type has already been recorded. */
67607e24 9415 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9416 type->set_num_fields (3);
9c6a1327 9417 /* Save the field we care about. */
ceacbf6e 9418 struct field saved_field = type->field (0);
3cabb6b0
SM
9419 type->set_fields
9420 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9421
9c6a1327
TT
9422 /* Put the discriminant at index 0. */
9423 TYPE_FIELD_TYPE (type, 0) = field_type;
9424 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9425 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9426 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9427
9428 /* The order of fields doesn't really matter, so put the real
9429 field at index 1 and the data-less field at index 2. */
ceacbf6e 9430 type->field (1) = saved_field;
9c6a1327 9431 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9432 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9433 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9434 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9435 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9436
9437 const char *dataless_name
7d93a1e0 9438 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9439 name);
9440 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9441 dataless_name);
9c6a1327 9442 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9443 /* NAME points into the original discriminant name, which
9444 already has the correct lifetime. */
9c6a1327 9445 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9446 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9447
9c6a1327
TT
9448 /* Indicate that this is a variant type. */
9449 static discriminant_range ranges[1] = { { 0, 0 } };
9450 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9451 }
77c2dba3
TT
9452 /* A union with a single anonymous field is probably an old-style
9453 univariant enum. */
1f704f76 9454 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9455 {
c9317f21
TT
9456 /* Smash this type to be a structure type. We have to do this
9457 because the type has already been recorded. */
67607e24 9458 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9459
9c6a1327 9460 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9461 const char *variant_name
7d93a1e0 9462 = rust_last_path_segment (field_type->name ());
9c6a1327 9463 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9464 field_type->set_name
9465 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9466 type->name (), variant_name));
c9317f21
TT
9467 }
9468 else
9469 {
9470 struct type *disr_type = nullptr;
1f704f76 9471 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9472 {
9473 disr_type = TYPE_FIELD_TYPE (type, i);
9474
78134374 9475 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9476 {
9477 /* All fields of a true enum will be structs. */
9478 return;
9479 }
1f704f76 9480 else if (disr_type->num_fields () == 0)
c9317f21
TT
9481 {
9482 /* Could be data-less variant, so keep going. */
a037790e 9483 disr_type = nullptr;
c9317f21
TT
9484 }
9485 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9486 "RUST$ENUM$DISR") != 0)
9487 {
9488 /* Not a Rust enum. */
9489 return;
9490 }
9491 else
9492 {
9493 /* Found one. */
9494 break;
9495 }
9496 }
9497
9498 /* If we got here without a discriminant, then it's probably
9499 just a union. */
9500 if (disr_type == nullptr)
9501 return;
9502
9503 /* Smash this type to be a structure type. We have to do this
9504 because the type has already been recorded. */
67607e24 9505 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9506
9c6a1327 9507 /* Make space for the discriminant field. */
ceacbf6e 9508 struct field *disr_field = &disr_type->field (0);
9c6a1327 9509 field *new_fields
1f704f76 9510 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9511 * sizeof (struct field)));
80fc5e77 9512 memcpy (new_fields + 1, type->fields (),
1f704f76 9513 type->num_fields () * sizeof (struct field));
3cabb6b0 9514 type->set_fields (new_fields);
1f704f76 9515 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9516
9517 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9518 type->field (0) = *disr_field;
9c6a1327
TT
9519 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9520 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9521
9522 /* We need a way to find the correct discriminant given a
9523 variant name. For convenience we build a map here. */
9524 struct type *enum_type = FIELD_TYPE (*disr_field);
9525 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9526 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9527 {
9528 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9529 {
9530 const char *name
9531 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9532 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9533 }
9534 }
9535
1f704f76 9536 int n_fields = type->num_fields ();
9c6a1327
TT
9537 /* We don't need a range entry for the discriminant, but we do
9538 need one for every other field, as there is no default
9539 variant. */
9540 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9541 discriminant_range,
9542 n_fields - 1);
c9317f21
TT
9543 /* Skip the discriminant here. */
9544 for (int i = 1; i < n_fields; ++i)
9545 {
9546 /* Find the final word in the name of this variant's type.
9547 That name can be used to look up the correct
9548 discriminant. */
9549 const char *variant_name
7d93a1e0 9550 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9551
9552 auto iter = discriminant_map.find (variant_name);
9553 if (iter != discriminant_map.end ())
9c6a1327
TT
9554 {
9555 ranges[i].low = iter->second;
9556 ranges[i].high = iter->second;
9557 }
c9317f21 9558
bedda9ac 9559 /* Remove the discriminant field, if it exists. */
9c6a1327 9560 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9561 if (sub_type->num_fields () > 0)
bedda9ac 9562 {
5e33d5f4 9563 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9564 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9565 }
9c6a1327 9566 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9567 sub_type->set_name
9568 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9569 type->name (), variant_name));
c9317f21 9570 }
9c6a1327
TT
9571
9572 /* Indicate that this is a variant type. */
9573 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9574 gdb::array_view<discriminant_range> (ranges,
9575 n_fields - 1));
c9317f21
TT
9576 }
9577}
9578
9579/* Rewrite some Rust unions to be structures with variants parts. */
9580
9581static void
9582rust_union_quirks (struct dwarf2_cu *cu)
9583{
9584 gdb_assert (cu->language == language_rust);
52941706
SM
9585 for (type *type_ : cu->rust_unions)
9586 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9587 /* We don't need this any more. */
9588 cu->rust_unions.clear ();
c9317f21
TT
9589}
9590
95554aad
TT
9591/* A helper function for computing the list of all symbol tables
9592 included by PER_CU. */
9593
9594static void
4c39bc03 9595recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9596 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9597 struct dwarf2_per_cu_data *per_cu,
43f3e411 9598 struct compunit_symtab *immediate_parent)
95554aad 9599{
af758d11 9600 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9601 if (*slot != NULL)
9602 {
9603 /* This inclusion and its children have been processed. */
9604 return;
9605 }
9606
9607 *slot = per_cu;
af758d11 9608
95554aad 9609 /* Only add a CU if it has a symbol table. */
af758d11 9610 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
43f3e411 9611 if (cust != NULL)
ec94af83
DE
9612 {
9613 /* If this is a type unit only add its symbol table if we haven't
9614 seen it yet (type unit per_cu's can share symtabs). */
9615 if (per_cu->is_debug_types)
9616 {
43f3e411 9617 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9618 if (*slot == NULL)
9619 {
43f3e411 9620 *slot = cust;
4c39bc03 9621 result->push_back (cust);
43f3e411
DE
9622 if (cust->user == NULL)
9623 cust->user = immediate_parent;
ec94af83
DE
9624 }
9625 }
9626 else
f9125b6c 9627 {
4c39bc03 9628 result->push_back (cust);
43f3e411
DE
9629 if (cust->user == NULL)
9630 cust->user = immediate_parent;
f9125b6c 9631 }
ec94af83 9632 }
95554aad 9633
ae640021
AB
9634 if (!per_cu->imported_symtabs_empty ())
9635 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9636 {
9637 recursively_compute_inclusions (result, all_children,
9638 all_type_symtabs, ptr, cust);
9639 }
95554aad
TT
9640}
9641
43f3e411 9642/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9643 PER_CU. */
9644
9645static void
43f3e411 9646compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9647{
f4dc4d17
DE
9648 gdb_assert (! per_cu->is_debug_types);
9649
ae640021 9650 if (!per_cu->imported_symtabs_empty ())
95554aad 9651 {
ae640021 9652 int len;
4c39bc03 9653 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9654 htab_t all_children, all_type_symtabs;
af758d11 9655 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
95554aad
TT
9656
9657 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9658 if (cust == NULL)
95554aad
TT
9659 return;
9660
9661 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9662 NULL, xcalloc, xfree);
ec94af83
DE
9663 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9664 NULL, xcalloc, xfree);
95554aad 9665
ae640021 9666 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9667 {
9668 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9669 all_type_symtabs, ptr, cust);
ec94af83 9670 }
95554aad 9671
ec94af83 9672 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9673 len = result_symtabs.size ();
43f3e411 9674 cust->includes
ed2dc618 9675 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9676 struct compunit_symtab *, len + 1);
4c39bc03
TT
9677 memcpy (cust->includes, result_symtabs.data (),
9678 len * sizeof (compunit_symtab *));
43f3e411 9679 cust->includes[len] = NULL;
95554aad 9680
95554aad 9681 htab_delete (all_children);
ec94af83 9682 htab_delete (all_type_symtabs);
95554aad
TT
9683 }
9684}
9685
9686/* Compute the 'includes' field for the symtabs of all the CUs we just
9687 read. */
9688
9689static void
ed2dc618 9690process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9691{
5989a64e 9692 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9693 {
9694 if (! iter->is_debug_types)
43f3e411 9695 compute_compunit_symtab_includes (iter);
f4dc4d17 9696 }
95554aad 9697
5989a64e 9698 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9699}
9700
9cdd5dbd 9701/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9702 already been loaded into memory. */
9703
9704static void
95554aad
TT
9705process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9706 enum language pretend_language)
10b3939b 9707{
10b3939b 9708 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9709 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9710 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9711 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9712 CORE_ADDR lowpc, highpc;
43f3e411 9713 struct compunit_symtab *cust;
10b3939b 9714 CORE_ADDR baseaddr;
4359dff1 9715 struct block *static_block;
3e29f34a 9716 CORE_ADDR addr;
10b3939b 9717
b3b3bada 9718 baseaddr = objfile->text_section_offset ();
10b3939b 9719
c89b44cd
TT
9720 /* Clear the list here in case something was left over. */
9721 cu->method_list.clear ();
10b3939b 9722
95554aad
TT
9723 cu->language = pretend_language;
9724 cu->language_defn = language_def (cu->language);
9725
c906108c 9726 /* Do line number decoding in read_file_scope () */
10b3939b 9727 process_die (cu->dies, cu);
c906108c 9728
a766d390
DE
9729 /* For now fudge the Go package. */
9730 if (cu->language == language_go)
9731 fixup_go_packaging (cu);
9732
5f48f8f3 9733 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9734 should be complete, and it should now be safe to compute all of the
9735 physnames. */
9736 compute_delayed_physnames (cu);
3da10d80 9737
c9317f21
TT
9738 if (cu->language == language_rust)
9739 rust_union_quirks (cu);
9740
fae299cd
DC
9741 /* Some compilers don't define a DW_AT_high_pc attribute for the
9742 compilation unit. If the DW_AT_high_pc is missing, synthesize
9743 it, by scanning the DIE's below the compilation unit. */
10b3939b 9744 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9745
3e29f34a 9746 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9747 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9748
9749 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9750 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9751 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9752 addrmap to help ensure it has an accurate map of pc values belonging to
9753 this comp unit. */
9754 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9755
c24bdb02 9756 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9757 SECT_OFF_TEXT (objfile),
9758 0);
c906108c 9759
43f3e411 9760 if (cust != NULL)
c906108c 9761 {
df15bd07 9762 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9763
8be455d7
JK
9764 /* Set symtab language to language from DW_AT_language. If the
9765 compilation is from a C file generated by language preprocessors, do
9766 not set the language if it was already deduced by start_subfile. */
43f3e411 9767 if (!(cu->language == language_c
40e3ad0e 9768 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9769 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9770
9771 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9772 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9773 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9774 there were bugs in prologue debug info, fixed later in GCC-4.5
9775 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9776
9777 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9778 needed, it would be wrong due to missing DW_AT_producer there.
9779
9780 Still one can confuse GDB by using non-standard GCC compilation
9781 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9782 */
ab260dad 9783 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9784 cust->locations_valid = 1;
e0d00bc7
JK
9785
9786 if (gcc_4_minor >= 5)
43f3e411 9787 cust->epilogue_unwind_valid = 1;
96408a79 9788
43f3e411 9789 cust->call_site_htab = cu->call_site_htab;
c906108c 9790 }
9291a0cd 9791
af758d11 9792 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9793
95554aad 9794 /* Push it for inclusion processing later. */
5989a64e 9795 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9796
9797 /* Not needed any more. */
c24bdb02 9798 cu->reset_builder ();
f4dc4d17 9799}
45cfd468 9800
f4dc4d17
DE
9801/* Generate full symbol information for type unit PER_CU, whose DIEs have
9802 already been loaded into memory. */
9803
9804static void
9805process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9806 enum language pretend_language)
9807{
9808 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9809 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9810 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9811 struct compunit_symtab *cust;
0186c6a7
DE
9812 struct signatured_type *sig_type;
9813
9814 gdb_assert (per_cu->is_debug_types);
9815 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9816
c89b44cd
TT
9817 /* Clear the list here in case something was left over. */
9818 cu->method_list.clear ();
f4dc4d17 9819
f4dc4d17
DE
9820 cu->language = pretend_language;
9821 cu->language_defn = language_def (cu->language);
9822
9823 /* The symbol tables are set up in read_type_unit_scope. */
9824 process_die (cu->dies, cu);
9825
9826 /* For now fudge the Go package. */
9827 if (cu->language == language_go)
9828 fixup_go_packaging (cu);
9829
5f48f8f3 9830 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9831 should be complete, and it should now be safe to compute all of the
9832 physnames. */
9833 compute_delayed_physnames (cu);
f4dc4d17 9834
c9317f21
TT
9835 if (cu->language == language_rust)
9836 rust_union_quirks (cu);
9837
f4dc4d17
DE
9838 /* TUs share symbol tables.
9839 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9840 of it with end_expandable_symtab. Otherwise, complete the addition of
9841 this TU's symbols to the existing symtab. */
43f3e411 9842 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9843 {
c24bdb02
KS
9844 buildsym_compunit *builder = cu->get_builder ();
9845 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9846 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9847
43f3e411 9848 if (cust != NULL)
f4dc4d17
DE
9849 {
9850 /* Set symtab language to language from DW_AT_language. If the
9851 compilation is from a C file generated by language preprocessors,
9852 do not set the language if it was already deduced by
9853 start_subfile. */
43f3e411
DE
9854 if (!(cu->language == language_c
9855 && COMPUNIT_FILETABS (cust)->language != language_c))
9856 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9857 }
9858 }
9859 else
9860 {
c24bdb02 9861 cu->get_builder ()->augment_type_symtab ();
43f3e411 9862 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9863 }
9864
af758d11 9865 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9866
9867 /* Not needed any more. */
c24bdb02 9868 cu->reset_builder ();
c906108c
SS
9869}
9870
95554aad
TT
9871/* Process an imported unit DIE. */
9872
9873static void
9874process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9875{
9876 struct attribute *attr;
9877
f4dc4d17
DE
9878 /* For now we don't handle imported units in type units. */
9879 if (cu->per_cu->is_debug_types)
9880 {
9881 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9882 " supported in type units [in module %s]"),
518817b3 9883 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9884 }
9885
95554aad
TT
9886 attr = dwarf2_attr (die, DW_AT_import, cu);
9887 if (attr != NULL)
9888 {
0826b30a 9889 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9890 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9891 dwarf2_per_cu_data *per_cu
e3b94546 9892 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9893 cu->per_cu->dwarf2_per_objfile);
95554aad 9894
58990295
TV
9895 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9896 into another compilation unit, at root level. Regard this as a hint,
9897 and ignore it. */
9898 if (die->parent && die->parent->parent == NULL
9899 && per_cu->unit_type == DW_UT_compile
9900 && per_cu->lang == language_cplus)
9901 return;
9902
69d751e3 9903 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9904 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9905 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9906
ae640021 9907 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9908 }
9909}
9910
4c8aa72d
PA
9911/* RAII object that represents a process_die scope: i.e.,
9912 starts/finishes processing a DIE. */
9913class process_die_scope
adde2bff 9914{
4c8aa72d
PA
9915public:
9916 process_die_scope (die_info *die, dwarf2_cu *cu)
9917 : m_die (die), m_cu (cu)
9918 {
9919 /* We should only be processing DIEs not already in process. */
9920 gdb_assert (!m_die->in_process);
9921 m_die->in_process = true;
9922 }
8c3cb9fa 9923
4c8aa72d
PA
9924 ~process_die_scope ()
9925 {
9926 m_die->in_process = false;
9927
9928 /* If we're done processing the DIE for the CU that owns the line
9929 header, we don't need the line header anymore. */
9930 if (m_cu->line_header_die_owner == m_die)
9931 {
9932 delete m_cu->line_header;
9933 m_cu->line_header = NULL;
9934 m_cu->line_header_die_owner = NULL;
9935 }
9936 }
9937
9938private:
9939 die_info *m_die;
9940 dwarf2_cu *m_cu;
9941};
adde2bff 9942
c906108c
SS
9943/* Process a die and its children. */
9944
9945static void
e7c27a73 9946process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9947{
4c8aa72d 9948 process_die_scope scope (die, cu);
adde2bff 9949
c906108c
SS
9950 switch (die->tag)
9951 {
9952 case DW_TAG_padding:
9953 break;
9954 case DW_TAG_compile_unit:
95554aad 9955 case DW_TAG_partial_unit:
e7c27a73 9956 read_file_scope (die, cu);
c906108c 9957 break;
348e048f
DE
9958 case DW_TAG_type_unit:
9959 read_type_unit_scope (die, cu);
9960 break;
c906108c 9961 case DW_TAG_subprogram:
0a4b0913
AB
9962 /* Nested subprograms in Fortran get a prefix. */
9963 if (cu->language == language_fortran
9964 && die->parent != NULL
9965 && die->parent->tag == DW_TAG_subprogram)
9966 cu->processing_has_namespace_info = true;
9967 /* Fall through. */
c906108c 9968 case DW_TAG_inlined_subroutine:
edb3359d 9969 read_func_scope (die, cu);
c906108c
SS
9970 break;
9971 case DW_TAG_lexical_block:
14898363
L
9972 case DW_TAG_try_block:
9973 case DW_TAG_catch_block:
e7c27a73 9974 read_lexical_block_scope (die, cu);
c906108c 9975 break;
216f72a1 9976 case DW_TAG_call_site:
96408a79
SA
9977 case DW_TAG_GNU_call_site:
9978 read_call_site_scope (die, cu);
9979 break;
c906108c 9980 case DW_TAG_class_type:
680b30c7 9981 case DW_TAG_interface_type:
c906108c
SS
9982 case DW_TAG_structure_type:
9983 case DW_TAG_union_type:
134d01f1 9984 process_structure_scope (die, cu);
c906108c
SS
9985 break;
9986 case DW_TAG_enumeration_type:
134d01f1 9987 process_enumeration_scope (die, cu);
c906108c 9988 break;
134d01f1 9989
f792889a
DJ
9990 /* These dies have a type, but processing them does not create
9991 a symbol or recurse to process the children. Therefore we can
9992 read them on-demand through read_type_die. */
c906108c 9993 case DW_TAG_subroutine_type:
72019c9c 9994 case DW_TAG_set_type:
c906108c 9995 case DW_TAG_array_type:
c906108c 9996 case DW_TAG_pointer_type:
c906108c 9997 case DW_TAG_ptr_to_member_type:
c906108c 9998 case DW_TAG_reference_type:
4297a3f0 9999 case DW_TAG_rvalue_reference_type:
c906108c 10000 case DW_TAG_string_type:
c906108c 10001 break;
134d01f1 10002
c906108c 10003 case DW_TAG_base_type:
a02abb62 10004 case DW_TAG_subrange_type:
cb249c71 10005 case DW_TAG_typedef:
134d01f1
DJ
10006 /* Add a typedef symbol for the type definition, if it has a
10007 DW_AT_name. */
f792889a 10008 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10009 break;
c906108c 10010 case DW_TAG_common_block:
e7c27a73 10011 read_common_block (die, cu);
c906108c
SS
10012 break;
10013 case DW_TAG_common_inclusion:
10014 break;
d9fa45fe 10015 case DW_TAG_namespace:
9068261f 10016 cu->processing_has_namespace_info = true;
e7c27a73 10017 read_namespace (die, cu);
d9fa45fe 10018 break;
5d7cb8df 10019 case DW_TAG_module:
9068261f 10020 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10021 read_module (die, cu);
10022 break;
d9fa45fe 10023 case DW_TAG_imported_declaration:
9068261f 10024 cu->processing_has_namespace_info = true;
74921315
KS
10025 if (read_namespace_alias (die, cu))
10026 break;
86a73007
TT
10027 /* The declaration is not a global namespace alias. */
10028 /* Fall through. */
d9fa45fe 10029 case DW_TAG_imported_module:
9068261f 10030 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10031 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10032 || cu->language != language_fortran))
b98664d3 10033 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10034 dwarf_tag_name (die->tag));
10035 read_import_statement (die, cu);
d9fa45fe 10036 break;
95554aad
TT
10037
10038 case DW_TAG_imported_unit:
10039 process_imported_unit_die (die, cu);
10040 break;
10041
71a3c369
TT
10042 case DW_TAG_variable:
10043 read_variable (die, cu);
10044 break;
10045
c906108c 10046 default:
e7c27a73 10047 new_symbol (die, NULL, cu);
c906108c
SS
10048 break;
10049 }
10050}
ca69b9e6
DE
10051\f
10052/* DWARF name computation. */
c906108c 10053
94af9270
KS
10054/* A helper function for dwarf2_compute_name which determines whether DIE
10055 needs to have the name of the scope prepended to the name listed in the
10056 die. */
10057
10058static int
10059die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10060{
1c809c68
TT
10061 struct attribute *attr;
10062
94af9270
KS
10063 switch (die->tag)
10064 {
10065 case DW_TAG_namespace:
10066 case DW_TAG_typedef:
10067 case DW_TAG_class_type:
10068 case DW_TAG_interface_type:
10069 case DW_TAG_structure_type:
10070 case DW_TAG_union_type:
10071 case DW_TAG_enumeration_type:
10072 case DW_TAG_enumerator:
10073 case DW_TAG_subprogram:
08a76f8a 10074 case DW_TAG_inlined_subroutine:
94af9270 10075 case DW_TAG_member:
74921315 10076 case DW_TAG_imported_declaration:
94af9270
KS
10077 return 1;
10078
10079 case DW_TAG_variable:
c2b0a229 10080 case DW_TAG_constant:
94af9270
KS
10081 /* We only need to prefix "globally" visible variables. These include
10082 any variable marked with DW_AT_external or any variable that
10083 lives in a namespace. [Variables in anonymous namespaces
10084 require prefixing, but they are not DW_AT_external.] */
10085
10086 if (dwarf2_attr (die, DW_AT_specification, cu))
10087 {
10088 struct dwarf2_cu *spec_cu = cu;
9a619af0 10089
94af9270
KS
10090 return die_needs_namespace (die_specification (die, &spec_cu),
10091 spec_cu);
10092 }
10093
1c809c68 10094 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10095 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10096 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10097 return 0;
10098 /* A variable in a lexical block of some kind does not need a
10099 namespace, even though in C++ such variables may be external
10100 and have a mangled name. */
10101 if (die->parent->tag == DW_TAG_lexical_block
10102 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10103 || die->parent->tag == DW_TAG_catch_block
10104 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10105 return 0;
10106 return 1;
94af9270
KS
10107
10108 default:
10109 return 0;
10110 }
10111}
10112
73b9be8b
KS
10113/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10114 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10115 defined for the given DIE. */
10116
10117static struct attribute *
10118dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10119{
10120 struct attribute *attr;
10121
10122 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10123 if (attr == NULL)
10124 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10125
10126 return attr;
10127}
10128
10129/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10130 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10131 defined for the given DIE. */
10132
10133static const char *
10134dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10135{
10136 const char *linkage_name;
10137
10138 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10139 if (linkage_name == NULL)
10140 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10141
787de330
TT
10142 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10143 See https://github.com/rust-lang/rust/issues/32925. */
10144 if (cu->language == language_rust && linkage_name != NULL
10145 && strchr (linkage_name, '{') != NULL)
10146 linkage_name = NULL;
10147
73b9be8b
KS
10148 return linkage_name;
10149}
10150
94af9270 10151/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10152 compute the physname for the object, which include a method's:
9c37b5ae 10153 - formal parameters (C++),
a766d390 10154 - receiver type (Go),
a766d390
DE
10155
10156 The term "physname" is a bit confusing.
10157 For C++, for example, it is the demangled name.
10158 For Go, for example, it's the mangled name.
94af9270 10159
af6b7be1
JB
10160 For Ada, return the DIE's linkage name rather than the fully qualified
10161 name. PHYSNAME is ignored..
10162
5989a64e 10163 The result is allocated on the objfile->per_bfd's obstack and
45940949 10164 canonicalized. */
94af9270
KS
10165
10166static const char *
15d034d0
TT
10167dwarf2_compute_name (const char *name,
10168 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10169 int physname)
10170{
518817b3 10171 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10172
94af9270
KS
10173 if (name == NULL)
10174 name = dwarf2_name (die, cu);
10175
2ee7123e
DE
10176 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10177 but otherwise compute it by typename_concat inside GDB.
10178 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10179 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10180 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10181 will set the demangled name to the result of dwarf2_full_name, and it is
10182 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10183 if (cu->language == language_ada
10184 || (cu->language == language_fortran && physname))
10185 {
10186 /* For Ada unit, we prefer the linkage name over the name, as
10187 the former contains the exported name, which the user expects
10188 to be able to reference. Ideally, we want the user to be able
10189 to reference this entity using either natural or linkage name,
10190 but we haven't started looking at this enhancement yet. */
73b9be8b 10191 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10192
2ee7123e
DE
10193 if (linkage_name != NULL)
10194 return linkage_name;
f55ee35c
JK
10195 }
10196
94af9270
KS
10197 /* These are the only languages we know how to qualify names in. */
10198 if (name != NULL
9c37b5ae 10199 && (cu->language == language_cplus
c44af4eb
TT
10200 || cu->language == language_fortran || cu->language == language_d
10201 || cu->language == language_rust))
94af9270
KS
10202 {
10203 if (die_needs_namespace (die, cu))
10204 {
0d5cff50 10205 const char *prefix;
34a68019 10206 const char *canonical_name = NULL;
94af9270 10207
d7e74731
PA
10208 string_file buf;
10209
94af9270 10210 prefix = determine_prefix (die, cu);
94af9270
KS
10211 if (*prefix != '\0')
10212 {
43816ebc
TT
10213 gdb::unique_xmalloc_ptr<char> prefixed_name
10214 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10215
43816ebc 10216 buf.puts (prefixed_name.get ());
94af9270
KS
10217 }
10218 else
d7e74731 10219 buf.puts (name);
94af9270 10220
98bfdba5
PA
10221 /* Template parameters may be specified in the DIE's DW_AT_name, or
10222 as children with DW_TAG_template_type_param or
10223 DW_TAG_value_type_param. If the latter, add them to the name
10224 here. If the name already has template parameters, then
10225 skip this step; some versions of GCC emit both, and
10226 it is more efficient to use the pre-computed name.
10227
10228 Something to keep in mind about this process: it is very
10229 unlikely, or in some cases downright impossible, to produce
10230 something that will match the mangled name of a function.
10231 If the definition of the function has the same debug info,
10232 we should be able to match up with it anyway. But fallbacks
10233 using the minimal symbol, for instance to find a method
10234 implemented in a stripped copy of libstdc++, will not work.
10235 If we do not have debug info for the definition, we will have to
10236 match them up some other way.
10237
10238 When we do name matching there is a related problem with function
10239 templates; two instantiated function templates are allowed to
10240 differ only by their return types, which we do not add here. */
10241
10242 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10243 {
10244 struct attribute *attr;
10245 struct die_info *child;
10246 int first = 1;
10247
10248 die->building_fullname = 1;
10249
10250 for (child = die->child; child != NULL; child = child->sibling)
10251 {
10252 struct type *type;
12df843f 10253 LONGEST value;
d521ce57 10254 const gdb_byte *bytes;
98bfdba5
PA
10255 struct dwarf2_locexpr_baton *baton;
10256 struct value *v;
10257
10258 if (child->tag != DW_TAG_template_type_param
10259 && child->tag != DW_TAG_template_value_param)
10260 continue;
10261
10262 if (first)
10263 {
d7e74731 10264 buf.puts ("<");
98bfdba5
PA
10265 first = 0;
10266 }
10267 else
d7e74731 10268 buf.puts (", ");
98bfdba5
PA
10269
10270 attr = dwarf2_attr (child, DW_AT_type, cu);
10271 if (attr == NULL)
10272 {
b98664d3 10273 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10274 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10275 continue;
10276 }
10277 type = die_type (child, cu);
10278
10279 if (child->tag == DW_TAG_template_type_param)
10280 {
c1ec8cea
TT
10281 c_print_type (type, "", &buf, -1, 0, cu->language,
10282 &type_print_raw_options);
98bfdba5
PA
10283 continue;
10284 }
10285
10286 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10287 if (attr == NULL)
10288 {
b98664d3 10289 complaint (_("template parameter missing "
3e43a32a 10290 "DW_AT_const_value"));
d7e74731 10291 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10292 continue;
10293 }
10294
10295 dwarf2_const_value_attr (attr, type, name,
10296 &cu->comp_unit_obstack, cu,
10297 &value, &bytes, &baton);
10298
10299 if (TYPE_NOSIGN (type))
10300 /* GDB prints characters as NUMBER 'CHAR'. If that's
10301 changed, this can use value_print instead. */
d7e74731 10302 c_printchar (value, type, &buf);
98bfdba5
PA
10303 else
10304 {
10305 struct value_print_options opts;
10306
10307 if (baton != NULL)
10308 v = dwarf2_evaluate_loc_desc (type, NULL,
10309 baton->data,
10310 baton->size,
10311 baton->per_cu);
10312 else if (bytes != NULL)
10313 {
10314 v = allocate_value (type);
10315 memcpy (value_contents_writeable (v), bytes,
10316 TYPE_LENGTH (type));
10317 }
10318 else
10319 v = value_from_longest (type, value);
10320
3e43a32a
MS
10321 /* Specify decimal so that we do not depend on
10322 the radix. */
98bfdba5
PA
10323 get_formatted_print_options (&opts, 'd');
10324 opts.raw = 1;
d7e74731 10325 value_print (v, &buf, &opts);
98bfdba5 10326 release_value (v);
98bfdba5
PA
10327 }
10328 }
10329
10330 die->building_fullname = 0;
10331
10332 if (!first)
10333 {
10334 /* Close the argument list, with a space if necessary
10335 (nested templates). */
d7e74731
PA
10336 if (!buf.empty () && buf.string ().back () == '>')
10337 buf.puts (" >");
98bfdba5 10338 else
d7e74731 10339 buf.puts (">");
98bfdba5
PA
10340 }
10341 }
10342
9c37b5ae 10343 /* For C++ methods, append formal parameter type
94af9270 10344 information, if PHYSNAME. */
6e70227d 10345
94af9270 10346 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10347 && cu->language == language_cplus)
94af9270
KS
10348 {
10349 struct type *type = read_type_die (die, cu);
10350
d7e74731 10351 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10352 &type_print_raw_options);
94af9270 10353
9c37b5ae 10354 if (cu->language == language_cplus)
94af9270 10355 {
60430eff
DJ
10356 /* Assume that an artificial first parameter is
10357 "this", but do not crash if it is not. RealView
10358 marks unnamed (and thus unused) parameters as
10359 artificial; there is no way to differentiate
10360 the two cases. */
1f704f76 10361 if (type->num_fields () > 0
94af9270 10362 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10363 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10364 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10365 0))))
d7e74731 10366 buf.puts (" const");
94af9270
KS
10367 }
10368 }
10369
d7e74731 10370 const std::string &intermediate_name = buf.string ();
94af9270
KS
10371
10372 if (cu->language == language_cplus)
34a68019 10373 canonical_name
322a8516 10374 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10375 objfile);
34a68019
TT
10376
10377 /* If we only computed INTERMEDIATE_NAME, or if
10378 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10379 intern it. */
322a8516 10380 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10381 name = objfile->intern (intermediate_name);
34a68019
TT
10382 else
10383 name = canonical_name;
94af9270
KS
10384 }
10385 }
10386
10387 return name;
10388}
10389
0114d602
DJ
10390/* Return the fully qualified name of DIE, based on its DW_AT_name.
10391 If scope qualifiers are appropriate they will be added. The result
34a68019 10392 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10393 not have a name. NAME may either be from a previous call to
10394 dwarf2_name or NULL.
10395
9c37b5ae 10396 The output string will be canonicalized (if C++). */
0114d602
DJ
10397
10398static const char *
15d034d0 10399dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10400{
94af9270
KS
10401 return dwarf2_compute_name (name, die, cu, 0);
10402}
0114d602 10403
94af9270
KS
10404/* Construct a physname for the given DIE in CU. NAME may either be
10405 from a previous call to dwarf2_name or NULL. The result will be
10406 allocated on the objfile_objstack or NULL if the DIE does not have a
10407 name.
0114d602 10408
9c37b5ae 10409 The output string will be canonicalized (if C++). */
0114d602 10410
94af9270 10411static const char *
15d034d0 10412dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10413{
518817b3 10414 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10415 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10416 int need_copy = 1;
10417
10418 /* In this case dwarf2_compute_name is just a shortcut not building anything
10419 on its own. */
10420 if (!die_needs_namespace (die, cu))
10421 return dwarf2_compute_name (name, die, cu, 1);
10422
906bb4c5
TT
10423 if (cu->language != language_rust)
10424 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10425
10426 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10427 has computed. */
791afaa2 10428 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10429 if (mangled != NULL)
900e11f9 10430 {
900e11f9 10431
59cc4834
JB
10432 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10433 {
10434 /* Do nothing (do not demangle the symbol name). */
10435 }
10436 else if (cu->language == language_go)
a766d390 10437 {
5e2db402
TT
10438 /* This is a lie, but we already lie to the caller new_symbol.
10439 new_symbol assumes we return the mangled name.
a766d390 10440 This just undoes that lie until things are cleaned up. */
a766d390
DE
10441 }
10442 else
10443 {
0eb876f5
JB
10444 /* Use DMGL_RET_DROP for C++ template functions to suppress
10445 their return type. It is easier for GDB users to search
10446 for such functions as `name(params)' than `long name(params)'.
10447 In such case the minimal symbol names do not match the full
10448 symbol names but for template functions there is never a need
10449 to look up their definition from their declaration so
10450 the only disadvantage remains the minimal symbol variant
10451 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10452 demangled.reset (gdb_demangle (mangled,
10453 (DMGL_PARAMS | DMGL_ANSI
10454 | DMGL_RET_DROP)));
a766d390 10455 }
900e11f9 10456 if (demangled)
791afaa2 10457 canon = demangled.get ();
900e11f9
JK
10458 else
10459 {
10460 canon = mangled;
10461 need_copy = 0;
10462 }
10463 }
10464
10465 if (canon == NULL || check_physname)
10466 {
10467 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10468
10469 if (canon != NULL && strcmp (physname, canon) != 0)
10470 {
10471 /* It may not mean a bug in GDB. The compiler could also
10472 compute DW_AT_linkage_name incorrectly. But in such case
10473 GDB would need to be bug-to-bug compatible. */
10474
b98664d3 10475 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10476 "(from linkage <%s>) - DIE at %s [in module %s]"),
10477 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10478 objfile_name (objfile));
900e11f9
JK
10479
10480 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10481 is available here - over computed PHYSNAME. It is safer
10482 against both buggy GDB and buggy compilers. */
10483
10484 retval = canon;
10485 }
10486 else
10487 {
10488 retval = physname;
10489 need_copy = 0;
10490 }
10491 }
10492 else
10493 retval = canon;
10494
10495 if (need_copy)
be1e3d3e 10496 retval = objfile->intern (retval);
900e11f9 10497
900e11f9 10498 return retval;
0114d602
DJ
10499}
10500
74921315
KS
10501/* Inspect DIE in CU for a namespace alias. If one exists, record
10502 a new symbol for it.
10503
10504 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10505
10506static int
10507read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10508{
10509 struct attribute *attr;
10510
10511 /* If the die does not have a name, this is not a namespace
10512 alias. */
10513 attr = dwarf2_attr (die, DW_AT_name, cu);
10514 if (attr != NULL)
10515 {
10516 int num;
10517 struct die_info *d = die;
10518 struct dwarf2_cu *imported_cu = cu;
10519
10520 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10521 keep inspecting DIEs until we hit the underlying import. */
10522#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10523 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10524 {
10525 attr = dwarf2_attr (d, DW_AT_import, cu);
10526 if (attr == NULL)
10527 break;
10528
10529 d = follow_die_ref (d, attr, &imported_cu);
10530 if (d->tag != DW_TAG_imported_declaration)
10531 break;
10532 }
10533
10534 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10535 {
b98664d3 10536 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10537 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10538 return 0;
10539 }
10540
10541 if (attr != NULL)
10542 {
10543 struct type *type;
0826b30a 10544 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10545
9c541725 10546 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10547 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10548 {
10549 /* This declaration is a global namespace alias. Add
10550 a symbol for it whose type is the aliased namespace. */
10551 new_symbol (die, type, cu);
10552 return 1;
10553 }
10554 }
10555 }
10556
10557 return 0;
10558}
10559
22cee43f 10560/* Return the using directives repository (global or local?) to use in the
804d2729 10561 current context for CU.
22cee43f
PMR
10562
10563 For Ada, imported declarations can materialize renamings, which *may* be
10564 global. However it is impossible (for now?) in DWARF to distinguish
10565 "external" imported declarations and "static" ones. As all imported
10566 declarations seem to be static in all other languages, make them all CU-wide
10567 global only in Ada. */
10568
10569static struct using_direct **
804d2729 10570using_directives (struct dwarf2_cu *cu)
22cee43f 10571{
c24bdb02
KS
10572 if (cu->language == language_ada
10573 && cu->get_builder ()->outermost_context_p ())
10574 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10575 else
c24bdb02 10576 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10577}
10578
27aa8d6a
SW
10579/* Read the import statement specified by the given die and record it. */
10580
10581static void
10582read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10583{
518817b3 10584 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10585 struct attribute *import_attr;
32019081 10586 struct die_info *imported_die, *child_die;
de4affc9 10587 struct dwarf2_cu *imported_cu;
27aa8d6a 10588 const char *imported_name;
794684b6 10589 const char *imported_name_prefix;
13387711
SW
10590 const char *canonical_name;
10591 const char *import_alias;
10592 const char *imported_declaration = NULL;
794684b6 10593 const char *import_prefix;
eb1e02fd 10594 std::vector<const char *> excludes;
13387711 10595
27aa8d6a
SW
10596 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10597 if (import_attr == NULL)
10598 {
b98664d3 10599 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10600 dwarf_tag_name (die->tag));
10601 return;
10602 }
10603
de4affc9
CC
10604 imported_cu = cu;
10605 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10606 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10607 if (imported_name == NULL)
10608 {
10609 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10610
10611 The import in the following code:
10612 namespace A
10613 {
10614 typedef int B;
10615 }
10616
10617 int main ()
10618 {
10619 using A::B;
10620 B b;
10621 return b;
10622 }
10623
10624 ...
10625 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10626 <52> DW_AT_decl_file : 1
10627 <53> DW_AT_decl_line : 6
10628 <54> DW_AT_import : <0x75>
10629 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10630 <59> DW_AT_name : B
10631 <5b> DW_AT_decl_file : 1
10632 <5c> DW_AT_decl_line : 2
10633 <5d> DW_AT_type : <0x6e>
10634 ...
10635 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10636 <76> DW_AT_byte_size : 4
10637 <77> DW_AT_encoding : 5 (signed)
10638
10639 imports the wrong die ( 0x75 instead of 0x58 ).
10640 This case will be ignored until the gcc bug is fixed. */
10641 return;
10642 }
10643
82856980
SW
10644 /* Figure out the local name after import. */
10645 import_alias = dwarf2_name (die, cu);
27aa8d6a 10646
794684b6
SW
10647 /* Figure out where the statement is being imported to. */
10648 import_prefix = determine_prefix (die, cu);
10649
10650 /* Figure out what the scope of the imported die is and prepend it
10651 to the name of the imported die. */
de4affc9 10652 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10653
f55ee35c
JK
10654 if (imported_die->tag != DW_TAG_namespace
10655 && imported_die->tag != DW_TAG_module)
794684b6 10656 {
13387711
SW
10657 imported_declaration = imported_name;
10658 canonical_name = imported_name_prefix;
794684b6 10659 }
13387711 10660 else if (strlen (imported_name_prefix) > 0)
12aaed36 10661 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10662 imported_name_prefix,
10663 (cu->language == language_d ? "." : "::"),
10664 imported_name, (char *) NULL);
13387711
SW
10665 else
10666 canonical_name = imported_name;
794684b6 10667
32019081
JK
10668 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10669 for (child_die = die->child; child_die && child_die->tag;
436c571c 10670 child_die = child_die->sibling)
32019081
JK
10671 {
10672 /* DWARF-4: A Fortran use statement with a “rename list” may be
10673 represented by an imported module entry with an import attribute
10674 referring to the module and owned entries corresponding to those
10675 entities that are renamed as part of being imported. */
10676
10677 if (child_die->tag != DW_TAG_imported_declaration)
10678 {
b98664d3 10679 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10680 "- DIE at %s [in module %s]"),
10681 sect_offset_str (child_die->sect_off),
10682 objfile_name (objfile));
32019081
JK
10683 continue;
10684 }
10685
10686 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10687 if (import_attr == NULL)
10688 {
b98664d3 10689 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10690 dwarf_tag_name (child_die->tag));
10691 continue;
10692 }
10693
10694 imported_cu = cu;
10695 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10696 &imported_cu);
10697 imported_name = dwarf2_name (imported_die, imported_cu);
10698 if (imported_name == NULL)
10699 {
b98664d3 10700 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10701 "imported name - DIE at %s [in module %s]"),
10702 sect_offset_str (child_die->sect_off),
10703 objfile_name (objfile));
32019081
JK
10704 continue;
10705 }
10706
eb1e02fd 10707 excludes.push_back (imported_name);
32019081
JK
10708
10709 process_die (child_die, cu);
10710 }
10711
804d2729 10712 add_using_directive (using_directives (cu),
22cee43f
PMR
10713 import_prefix,
10714 canonical_name,
10715 import_alias,
10716 imported_declaration,
10717 excludes,
10718 0,
10719 &objfile->objfile_obstack);
27aa8d6a
SW
10720}
10721
5230b05a
WT
10722/* ICC<14 does not output the required DW_AT_declaration on incomplete
10723 types, but gives them a size of zero. Starting with version 14,
10724 ICC is compatible with GCC. */
10725
9068261f 10726static bool
5230b05a
WT
10727producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10728{
10729 if (!cu->checked_producer)
10730 check_producer (cu);
10731
10732 return cu->producer_is_icc_lt_14;
10733}
10734
eb77c9df
AB
10735/* ICC generates a DW_AT_type for C void functions. This was observed on
10736 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10737 which says that void functions should not have a DW_AT_type. */
10738
10739static bool
10740producer_is_icc (struct dwarf2_cu *cu)
10741{
10742 if (!cu->checked_producer)
10743 check_producer (cu);
10744
10745 return cu->producer_is_icc;
10746}
10747
1b80a9fa
JK
10748/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10749 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10750 this, it was first present in GCC release 4.3.0. */
10751
9068261f 10752static bool
1b80a9fa
JK
10753producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10754{
10755 if (!cu->checked_producer)
10756 check_producer (cu);
10757
10758 return cu->producer_is_gcc_lt_4_3;
10759}
10760
d721ba37
PA
10761static file_and_directory
10762find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10763{
d721ba37
PA
10764 file_and_directory res;
10765
9291a0cd
TT
10766 /* Find the filename. Do not use dwarf2_name here, since the filename
10767 is not a source language identifier. */
d721ba37
PA
10768 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10769 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10770
d721ba37
PA
10771 if (res.comp_dir == NULL
10772 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10773 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10774 {
d721ba37
PA
10775 res.comp_dir_storage = ldirname (res.name);
10776 if (!res.comp_dir_storage.empty ())
10777 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10778 }
d721ba37 10779 if (res.comp_dir != NULL)
9291a0cd
TT
10780 {
10781 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10782 directory, get rid of it. */
d721ba37 10783 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10784
d721ba37
PA
10785 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10786 res.comp_dir = cp + 1;
9291a0cd
TT
10787 }
10788
d721ba37
PA
10789 if (res.name == NULL)
10790 res.name = "<unknown>";
10791
10792 return res;
9291a0cd
TT
10793}
10794
f4dc4d17
DE
10795/* Handle DW_AT_stmt_list for a compilation unit.
10796 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10797 COMP_DIR is the compilation directory. LOWPC is passed to
10798 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10799
10800static void
10801handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10802 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10803{
518817b3
SM
10804 struct dwarf2_per_objfile *dwarf2_per_objfile
10805 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10806 struct attribute *attr;
527f3840
JK
10807 struct line_header line_header_local;
10808 hashval_t line_header_local_hash;
527f3840
JK
10809 void **slot;
10810 int decode_mapping;
2ab95328 10811
f4dc4d17
DE
10812 gdb_assert (! cu->per_cu->is_debug_types);
10813
2ab95328 10814 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10815 if (attr == NULL)
10816 return;
10817
9c541725 10818 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10819
10820 /* The line header hash table is only created if needed (it exists to
10821 prevent redundant reading of the line table for partial_units).
10822 If we're given a partial_unit, we'll need it. If we're given a
10823 compile_unit, then use the line header hash table if it's already
10824 created, but don't create one just yet. */
10825
5989a64e 10826 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10827 && die->tag == DW_TAG_partial_unit)
2ab95328 10828 {
5989a64e 10829 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10830 .reset (htab_create_alloc (127, line_header_hash_voidp,
10831 line_header_eq_voidp,
10832 free_line_header_voidp,
10833 xcalloc, xfree));
527f3840 10834 }
2ab95328 10835
9c541725 10836 line_header_local.sect_off = line_offset;
527f3840
JK
10837 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10838 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10839 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10840 {
5989a64e 10841 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10842 &line_header_local,
10843 line_header_local_hash, NO_INSERT);
10844
10845 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10846 is not present in *SLOT (since if there is something in *SLOT then
10847 it will be for a partial_unit). */
10848 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10849 {
527f3840 10850 gdb_assert (*slot != NULL);
9a3c8263 10851 cu->line_header = (struct line_header *) *slot;
527f3840 10852 return;
dee91e82 10853 }
2ab95328 10854 }
527f3840
JK
10855
10856 /* dwarf_decode_line_header does not yet provide sufficient information.
10857 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10858 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10859 if (lh == NULL)
527f3840 10860 return;
4c8aa72d
PA
10861
10862 cu->line_header = lh.release ();
10863 cu->line_header_die_owner = die;
527f3840 10864
5989a64e 10865 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10866 slot = NULL;
10867 else
10868 {
5989a64e 10869 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10870 &line_header_local,
10871 line_header_local_hash, INSERT);
10872 gdb_assert (slot != NULL);
10873 }
10874 if (slot != NULL && *slot == NULL)
10875 {
10876 /* This newly decoded line number information unit will be owned
10877 by line_header_hash hash table. */
10878 *slot = cu->line_header;
4c8aa72d 10879 cu->line_header_die_owner = NULL;
527f3840
JK
10880 }
10881 else
10882 {
10883 /* We cannot free any current entry in (*slot) as that struct line_header
10884 may be already used by multiple CUs. Create only temporary decoded
10885 line_header for this CU - it may happen at most once for each line
10886 number information unit. And if we're not using line_header_hash
10887 then this is what we want as well. */
10888 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10889 }
10890 decode_mapping = (die->tag != DW_TAG_partial_unit);
10891 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10892 decode_mapping);
fff8551c 10893
2ab95328
TT
10894}
10895
95554aad 10896/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10897
c906108c 10898static void
e7c27a73 10899read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10900{
518817b3
SM
10901 struct dwarf2_per_objfile *dwarf2_per_objfile
10902 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10903 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10904 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10905 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10906 CORE_ADDR highpc = ((CORE_ADDR) 0);
10907 struct attribute *attr;
c906108c 10908 struct die_info *child_die;
e142c38c 10909 CORE_ADDR baseaddr;
6e70227d 10910
380618d6 10911 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10912 baseaddr = objfile->text_section_offset ();
c906108c 10913
fae299cd 10914 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10915
10916 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10917 from finish_block. */
2acceee2 10918 if (lowpc == ((CORE_ADDR) -1))
c906108c 10919 lowpc = highpc;
3e29f34a 10920 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10921
d721ba37 10922 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10923
f4b8a18d
KW
10924 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10925 standardised yet. As a workaround for the language detection we fall
10926 back to the DW_AT_producer string. */
10927 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10928 cu->language = language_opencl;
10929
3019eac3
DE
10930 /* Similar hack for Go. */
10931 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10932 set_cu_language (DW_LANG_Go, cu);
10933
c24bdb02 10934 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10935
10936 /* Decode line number information if present. We do this before
10937 processing child DIEs, so that the line header table is available
10938 for DW_AT_decl_file. */
d721ba37 10939 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10940
10941 /* Process all dies in compilation unit. */
10942 if (die->child != NULL)
10943 {
10944 child_die = die->child;
10945 while (child_die && child_die->tag)
10946 {
10947 process_die (child_die, cu);
436c571c 10948 child_die = child_die->sibling;
3019eac3
DE
10949 }
10950 }
10951
10952 /* Decode macro information, if present. Dwarf 2 macro information
10953 refers to information in the line number info statement program
10954 header, so we can only read it if we've read the header
10955 successfully. */
0af92d60
JK
10956 attr = dwarf2_attr (die, DW_AT_macros, cu);
10957 if (attr == NULL)
10958 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10959 if (attr && cu->line_header)
10960 {
10961 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10962 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10963
43f3e411 10964 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10965 }
10966 else
10967 {
10968 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10969 if (attr && cu->line_header)
10970 {
10971 unsigned int macro_offset = DW_UNSND (attr);
10972
43f3e411 10973 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10974 }
10975 }
3019eac3
DE
10976}
10977
c24bdb02
KS
10978void
10979dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10980{
f4dc4d17
DE
10981 struct type_unit_group *tu_group;
10982 int first_time;
3019eac3 10983 struct attribute *attr;
9c541725 10984 unsigned int i;
0186c6a7 10985 struct signatured_type *sig_type;
3019eac3 10986
f4dc4d17 10987 gdb_assert (per_cu->is_debug_types);
0186c6a7 10988 sig_type = (struct signatured_type *) per_cu;
3019eac3 10989
c24bdb02 10990 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10991
f4dc4d17 10992 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10993 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10994 if (sig_type->type_unit_group == NULL)
c24bdb02 10995 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10996 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10997
10998 /* If we've already processed this stmt_list there's no real need to
10999 do it again, we could fake it and just recreate the part we need
11000 (file name,index -> symtab mapping). If data shows this optimization
11001 is useful we can do it then. */
43f3e411 11002 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11003
11004 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11005 debug info. */
fff8551c 11006 line_header_up lh;
f4dc4d17 11007 if (attr != NULL)
3019eac3 11008 {
9c541725 11009 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11010 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11011 }
11012 if (lh == NULL)
11013 {
11014 if (first_time)
c24bdb02 11015 start_symtab ("", NULL, 0);
f4dc4d17
DE
11016 else
11017 {
11018 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11019 gdb_assert (m_builder == nullptr);
804d2729 11020 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11021 m_builder.reset (new struct buildsym_compunit
11022 (COMPUNIT_OBJFILE (cust), "",
11023 COMPUNIT_DIRNAME (cust),
11024 compunit_language (cust),
11025 0, cust));
770479f2 11026 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11027 }
f4dc4d17 11028 return;
3019eac3
DE
11029 }
11030
c24bdb02
KS
11031 line_header = lh.release ();
11032 line_header_die_owner = die;
3019eac3 11033
f4dc4d17
DE
11034 if (first_time)
11035 {
c24bdb02 11036 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11037
1fd60fc0
DE
11038 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11039 still initializing it, and our caller (a few levels up)
11040 process_full_type_unit still needs to know if this is the first
11041 time. */
11042
4ac93832
TT
11043 tu_group->symtabs
11044 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11045 struct symtab *, line_header->file_names_size ());
3019eac3 11046
7ba99d21
AT
11047 auto &file_names = line_header->file_names ();
11048 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11049 {
7ba99d21 11050 file_entry &fe = file_names[i];
c24bdb02
KS
11051 dwarf2_start_subfile (this, fe.name,
11052 fe.include_dir (line_header));
11053 buildsym_compunit *b = get_builder ();
11054 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11055 {
4c8aa72d
PA
11056 /* NOTE: start_subfile will recognize when it's been
11057 passed a file it has already seen. So we can't
11058 assume there's a simple mapping from
11059 cu->line_header->file_names to subfiles, plus
11060 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11061 b->get_current_subfile ()->symtab
11062 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11063 }
11064
c24bdb02 11065 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11066 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11067 }
11068 }
11069 else
3019eac3 11070 {
c24bdb02 11071 gdb_assert (m_builder == nullptr);
804d2729 11072 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11073 m_builder.reset (new struct buildsym_compunit
11074 (COMPUNIT_OBJFILE (cust), "",
11075 COMPUNIT_DIRNAME (cust),
11076 compunit_language (cust),
11077 0, cust));
770479f2 11078 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11079
7ba99d21
AT
11080 auto &file_names = line_header->file_names ();
11081 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11082 {
7ba99d21 11083 file_entry &fe = file_names[i];
4c8aa72d 11084 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11085 }
3019eac3
DE
11086 }
11087
f4dc4d17
DE
11088 /* The main symtab is allocated last. Type units don't have DW_AT_name
11089 so they don't have a "real" (so to speak) symtab anyway.
11090 There is later code that will assign the main symtab to all symbols
11091 that don't have one. We need to handle the case of a symbol with a
11092 missing symtab (DW_AT_decl_file) anyway. */
11093}
3019eac3 11094
f4dc4d17
DE
11095/* Process DW_TAG_type_unit.
11096 For TUs we want to skip the first top level sibling if it's not the
11097 actual type being defined by this TU. In this case the first top
11098 level sibling is there to provide context only. */
3019eac3 11099
f4dc4d17
DE
11100static void
11101read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11102{
11103 struct die_info *child_die;
3019eac3 11104
f4dc4d17
DE
11105 prepare_one_comp_unit (cu, die, language_minimal);
11106
11107 /* Initialize (or reinitialize) the machinery for building symtabs.
11108 We do this before processing child DIEs, so that the line header table
11109 is available for DW_AT_decl_file. */
c24bdb02 11110 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11111
11112 if (die->child != NULL)
11113 {
11114 child_die = die->child;
11115 while (child_die && child_die->tag)
11116 {
11117 process_die (child_die, cu);
436c571c 11118 child_die = child_die->sibling;
f4dc4d17
DE
11119 }
11120 }
3019eac3
DE
11121}
11122\f
80626a55
DE
11123/* DWO/DWP files.
11124
11125 http://gcc.gnu.org/wiki/DebugFission
11126 http://gcc.gnu.org/wiki/DebugFissionDWP
11127
11128 To simplify handling of both DWO files ("object" files with the DWARF info)
11129 and DWP files (a file with the DWOs packaged up into one file), we treat
11130 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11131
11132static hashval_t
11133hash_dwo_file (const void *item)
11134{
9a3c8263 11135 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11136 hashval_t hash;
3019eac3 11137
a2ce51a0
DE
11138 hash = htab_hash_string (dwo_file->dwo_name);
11139 if (dwo_file->comp_dir != NULL)
11140 hash += htab_hash_string (dwo_file->comp_dir);
11141 return hash;
3019eac3
DE
11142}
11143
11144static int
11145eq_dwo_file (const void *item_lhs, const void *item_rhs)
11146{
9a3c8263
SM
11147 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11148 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11149
a2ce51a0
DE
11150 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11151 return 0;
11152 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11153 return lhs->comp_dir == rhs->comp_dir;
11154 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11155}
11156
11157/* Allocate a hash table for DWO files. */
11158
51ac9db5 11159static htab_up
298e9637 11160allocate_dwo_file_hash_table ()
3019eac3 11161{
51ac9db5
SM
11162 auto delete_dwo_file = [] (void *item)
11163 {
11164 struct dwo_file *dwo_file = (struct dwo_file *) item;
11165
11166 delete dwo_file;
11167 };
11168
bc68fb19
TT
11169 return htab_up (htab_create_alloc (41,
11170 hash_dwo_file,
11171 eq_dwo_file,
11172 delete_dwo_file,
11173 xcalloc, xfree));
3019eac3
DE
11174}
11175
80626a55
DE
11176/* Lookup DWO file DWO_NAME. */
11177
11178static void **
ed2dc618
SM
11179lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11180 const char *dwo_name,
11181 const char *comp_dir)
80626a55
DE
11182{
11183 struct dwo_file find_entry;
11184 void **slot;
11185
5989a64e
SM
11186 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11187 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11188
0ac5b59e
DE
11189 find_entry.dwo_name = dwo_name;
11190 find_entry.comp_dir = comp_dir;
5989a64e 11191 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11192 INSERT);
80626a55
DE
11193
11194 return slot;
11195}
11196
3019eac3
DE
11197static hashval_t
11198hash_dwo_unit (const void *item)
11199{
9a3c8263 11200 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11201
11202 /* This drops the top 32 bits of the id, but is ok for a hash. */
11203 return dwo_unit->signature;
11204}
11205
11206static int
11207eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11208{
9a3c8263
SM
11209 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11210 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11211
11212 /* The signature is assumed to be unique within the DWO file.
11213 So while object file CU dwo_id's always have the value zero,
11214 that's OK, assuming each object file DWO file has only one CU,
11215 and that's the rule for now. */
11216 return lhs->signature == rhs->signature;
11217}
11218
11219/* Allocate a hash table for DWO CUs,TUs.
11220 There is one of these tables for each of CUs,TUs for each DWO file. */
11221
b0b6a987 11222static htab_up
298e9637 11223allocate_dwo_unit_table ()
3019eac3
DE
11224{
11225 /* Start out with a pretty small number.
11226 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11227 return htab_up (htab_create_alloc (3,
11228 hash_dwo_unit,
11229 eq_dwo_unit,
11230 NULL, xcalloc, xfree));
3019eac3
DE
11231}
11232
19c3d4c9 11233/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11234
11235static void
19c3d4c9
DE
11236create_dwo_cu_reader (const struct die_reader_specs *reader,
11237 const gdb_byte *info_ptr,
11238 struct die_info *comp_unit_die,
c0ab21c2
TT
11239 struct dwo_file *dwo_file,
11240 struct dwo_unit *dwo_unit)
3019eac3
DE
11241{
11242 struct dwarf2_cu *cu = reader->cu;
9c541725 11243 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11244 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11245
a084a2a6
AT
11246 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11247 if (!signature.has_value ())
3019eac3 11248 {
b98664d3 11249 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11250 " its dwo_id [in module %s]"),
9d8780f0 11251 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11252 return;
11253 }
11254
3019eac3 11255 dwo_unit->dwo_file = dwo_file;
a084a2a6 11256 dwo_unit->signature = *signature;
8a0459fd 11257 dwo_unit->section = section;
9c541725 11258 dwo_unit->sect_off = sect_off;
3019eac3
DE
11259 dwo_unit->length = cu->per_cu->length;
11260
b4f54984 11261 if (dwarf_read_debug)
9d8780f0
SM
11262 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11263 sect_offset_str (sect_off),
9c541725 11264 hex_string (dwo_unit->signature));
3019eac3
DE
11265}
11266
33c5cd75 11267/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11268 Note: This function processes DWO files only, not DWP files. */
3019eac3 11269
33c5cd75 11270static void
ed2dc618 11271create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11272 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11273 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11274{
11275 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11276 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11277
96b79293 11278 section.read (objfile);
33c5cd75 11279 info_ptr = section.buffer;
3019eac3
DE
11280
11281 if (info_ptr == NULL)
33c5cd75 11282 return;
3019eac3 11283
b4f54984 11284 if (dwarf_read_debug)
19c3d4c9
DE
11285 {
11286 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11287 section.get_name (),
11288 section.get_file_name ());
19c3d4c9 11289 }
3019eac3 11290
33c5cd75 11291 end_ptr = info_ptr + section.size;
3019eac3
DE
11292 while (info_ptr < end_ptr)
11293 {
11294 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11295 struct dwo_unit read_unit {};
33c5cd75
DB
11296 struct dwo_unit *dwo_unit;
11297 void **slot;
11298 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11299
11300 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11301 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11302 per_cu.is_debug_types = 0;
33c5cd75
DB
11303 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11304 per_cu.section = &section;
11305
c0ab21c2
TT
11306 cutu_reader reader (&per_cu, cu, &dwo_file);
11307 if (!reader.dummy_p)
11308 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11309 &dwo_file, &read_unit);
33c5cd75
DB
11310 info_ptr += per_cu.length;
11311
11312 // If the unit could not be parsed, skip it.
c0ab21c2 11313 if (read_unit.dwo_file == NULL)
33c5cd75 11314 continue;
3019eac3 11315
33c5cd75 11316 if (cus_htab == NULL)
298e9637 11317 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11318
5989a64e 11319 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 11320 struct dwo_unit);
c0ab21c2 11321 *dwo_unit = read_unit;
b0b6a987 11322 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11323 gdb_assert (slot != NULL);
11324 if (*slot != NULL)
19c3d4c9 11325 {
33c5cd75
DB
11326 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11327 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11328
b98664d3 11329 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11330 " the entry at offset %s, signature %s"),
11331 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11332 hex_string (dwo_unit->signature));
19c3d4c9 11333 }
33c5cd75 11334 *slot = (void *)dwo_unit;
3019eac3 11335 }
3019eac3
DE
11336}
11337
80626a55
DE
11338/* DWP file .debug_{cu,tu}_index section format:
11339 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11340
d2415c6c
DE
11341 DWP Version 1:
11342
80626a55
DE
11343 Both index sections have the same format, and serve to map a 64-bit
11344 signature to a set of section numbers. Each section begins with a header,
11345 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11346 indexes, and a pool of 32-bit section numbers. The index sections will be
11347 aligned at 8-byte boundaries in the file.
11348
d2415c6c
DE
11349 The index section header consists of:
11350
11351 V, 32 bit version number
11352 -, 32 bits unused
11353 N, 32 bit number of compilation units or type units in the index
11354 M, 32 bit number of slots in the hash table
80626a55 11355
d2415c6c 11356 Numbers are recorded using the byte order of the application binary.
80626a55 11357
d2415c6c
DE
11358 The hash table begins at offset 16 in the section, and consists of an array
11359 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11360 order of the application binary). Unused slots in the hash table are 0.
11361 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11362
d2415c6c
DE
11363 The parallel table begins immediately after the hash table
11364 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11365 array of 32-bit indexes (using the byte order of the application binary),
11366 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11367 table contains a 32-bit index into the pool of section numbers. For unused
11368 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11369
73869dc2
DE
11370 The pool of section numbers begins immediately following the hash table
11371 (at offset 16 + 12 * M from the beginning of the section). The pool of
11372 section numbers consists of an array of 32-bit words (using the byte order
11373 of the application binary). Each item in the array is indexed starting
11374 from 0. The hash table entry provides the index of the first section
11375 number in the set. Additional section numbers in the set follow, and the
11376 set is terminated by a 0 entry (section number 0 is not used in ELF).
11377
11378 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11379 section must be the first entry in the set, and the .debug_abbrev.dwo must
11380 be the second entry. Other members of the set may follow in any order.
11381
11382 ---
11383
11384 DWP Version 2:
11385
11386 DWP Version 2 combines all the .debug_info, etc. sections into one,
11387 and the entries in the index tables are now offsets into these sections.
11388 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11389 section.
11390
11391 Index Section Contents:
11392 Header
11393 Hash Table of Signatures dwp_hash_table.hash_table
11394 Parallel Table of Indices dwp_hash_table.unit_table
11395 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11396 Table of Section Sizes dwp_hash_table.v2.sizes
11397
11398 The index section header consists of:
11399
11400 V, 32 bit version number
11401 L, 32 bit number of columns in the table of section offsets
11402 N, 32 bit number of compilation units or type units in the index
11403 M, 32 bit number of slots in the hash table
11404
11405 Numbers are recorded using the byte order of the application binary.
11406
11407 The hash table has the same format as version 1.
11408 The parallel table of indices has the same format as version 1,
11409 except that the entries are origin-1 indices into the table of sections
11410 offsets and the table of section sizes.
11411
11412 The table of offsets begins immediately following the parallel table
11413 (at offset 16 + 12 * M from the beginning of the section). The table is
11414 a two-dimensional array of 32-bit words (using the byte order of the
11415 application binary), with L columns and N+1 rows, in row-major order.
11416 Each row in the array is indexed starting from 0. The first row provides
11417 a key to the remaining rows: each column in this row provides an identifier
11418 for a debug section, and the offsets in the same column of subsequent rows
11419 refer to that section. The section identifiers are:
11420
11421 DW_SECT_INFO 1 .debug_info.dwo
11422 DW_SECT_TYPES 2 .debug_types.dwo
11423 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11424 DW_SECT_LINE 4 .debug_line.dwo
11425 DW_SECT_LOC 5 .debug_loc.dwo
11426 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11427 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11428 DW_SECT_MACRO 8 .debug_macro.dwo
11429
11430 The offsets provided by the CU and TU index sections are the base offsets
11431 for the contributions made by each CU or TU to the corresponding section
11432 in the package file. Each CU and TU header contains an abbrev_offset
11433 field, used to find the abbreviations table for that CU or TU within the
11434 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11435 be interpreted as relative to the base offset given in the index section.
11436 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11437 should be interpreted as relative to the base offset for .debug_line.dwo,
11438 and offsets into other debug sections obtained from DWARF attributes should
11439 also be interpreted as relative to the corresponding base offset.
11440
11441 The table of sizes begins immediately following the table of offsets.
11442 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11443 with L columns and N rows, in row-major order. Each row in the array is
11444 indexed starting from 1 (row 0 is shared by the two tables).
11445
11446 ---
11447
11448 Hash table lookup is handled the same in version 1 and 2:
11449
11450 We assume that N and M will not exceed 2^32 - 1.
11451 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11452
d2415c6c
DE
11453 Given a 64-bit compilation unit signature or a type signature S, an entry
11454 in the hash table is located as follows:
80626a55 11455
d2415c6c
DE
11456 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11457 the low-order k bits all set to 1.
80626a55 11458
d2415c6c 11459 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11460
d2415c6c
DE
11461 3) If the hash table entry at index H matches the signature, use that
11462 entry. If the hash table entry at index H is unused (all zeroes),
11463 terminate the search: the signature is not present in the table.
80626a55 11464
d2415c6c 11465 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11466
d2415c6c 11467 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11468 to stop at an unused slot or find the match. */
80626a55
DE
11469
11470/* Create a hash table to map DWO IDs to their CU/TU entry in
11471 .debug_{info,types}.dwo in DWP_FILE.
11472 Returns NULL if there isn't one.
11473 Note: This function processes DWP files only, not DWO files. */
11474
11475static struct dwp_hash_table *
ed2dc618
SM
11476create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11477 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11478{
11479 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11480 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11481 const gdb_byte *index_ptr, *index_end;
80626a55 11482 struct dwarf2_section_info *index;
73869dc2 11483 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11484 struct dwp_hash_table *htab;
11485
11486 if (is_debug_types)
11487 index = &dwp_file->sections.tu_index;
11488 else
11489 index = &dwp_file->sections.cu_index;
11490
96b79293 11491 if (index->empty ())
80626a55 11492 return NULL;
96b79293 11493 index->read (objfile);
80626a55
DE
11494
11495 index_ptr = index->buffer;
11496 index_end = index_ptr + index->size;
11497
11498 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11499 index_ptr += 4;
11500 if (version == 2)
11501 nr_columns = read_4_bytes (dbfd, index_ptr);
11502 else
11503 nr_columns = 0;
11504 index_ptr += 4;
80626a55
DE
11505 nr_units = read_4_bytes (dbfd, index_ptr);
11506 index_ptr += 4;
11507 nr_slots = read_4_bytes (dbfd, index_ptr);
11508 index_ptr += 4;
11509
73869dc2 11510 if (version != 1 && version != 2)
80626a55 11511 {
21aa081e 11512 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11513 " [in module %s]"),
21aa081e 11514 pulongest (version), dwp_file->name);
80626a55
DE
11515 }
11516 if (nr_slots != (nr_slots & -nr_slots))
11517 {
21aa081e 11518 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11519 " is not power of 2 [in module %s]"),
21aa081e 11520 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11521 }
11522
5989a64e 11523 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11524 htab->version = version;
11525 htab->nr_columns = nr_columns;
80626a55
DE
11526 htab->nr_units = nr_units;
11527 htab->nr_slots = nr_slots;
11528 htab->hash_table = index_ptr;
11529 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11530
11531 /* Exit early if the table is empty. */
11532 if (nr_slots == 0 || nr_units == 0
11533 || (version == 2 && nr_columns == 0))
11534 {
11535 /* All must be zero. */
11536 if (nr_slots != 0 || nr_units != 0
11537 || (version == 2 && nr_columns != 0))
11538 {
b98664d3 11539 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11540 " all zero [in modules %s]"),
11541 dwp_file->name);
11542 }
11543 return htab;
11544 }
11545
11546 if (version == 1)
11547 {
11548 htab->section_pool.v1.indices =
11549 htab->unit_table + sizeof (uint32_t) * nr_slots;
11550 /* It's harder to decide whether the section is too small in v1.
11551 V1 is deprecated anyway so we punt. */
11552 }
11553 else
11554 {
11555 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11556 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11557 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11558 /* Reverse map for error checking. */
11559 int ids_seen[DW_SECT_MAX + 1];
11560 int i;
11561
11562 if (nr_columns < 2)
11563 {
11564 error (_("Dwarf Error: bad DWP hash table, too few columns"
11565 " in section table [in module %s]"),
11566 dwp_file->name);
11567 }
11568 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11569 {
11570 error (_("Dwarf Error: bad DWP hash table, too many columns"
11571 " in section table [in module %s]"),
11572 dwp_file->name);
11573 }
04fd5eed
GB
11574 memset (ids, 255, sizeof_ids);
11575 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11576 for (i = 0; i < nr_columns; ++i)
11577 {
11578 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11579
11580 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11581 {
11582 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11583 " in section table [in module %s]"),
11584 id, dwp_file->name);
11585 }
11586 if (ids_seen[id] != -1)
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11589 " id %d in section table [in module %s]"),
11590 id, dwp_file->name);
11591 }
11592 ids_seen[id] = i;
11593 ids[i] = id;
11594 }
11595 /* Must have exactly one info or types section. */
11596 if (((ids_seen[DW_SECT_INFO] != -1)
11597 + (ids_seen[DW_SECT_TYPES] != -1))
11598 != 1)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11601 " DWO info/types section [in module %s]"),
11602 dwp_file->name);
11603 }
11604 /* Must have an abbrev section. */
11605 if (ids_seen[DW_SECT_ABBREV] == -1)
11606 {
11607 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11608 " section [in module %s]"),
11609 dwp_file->name);
11610 }
11611 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11612 htab->section_pool.v2.sizes =
11613 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11614 * nr_units * nr_columns);
11615 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11616 * nr_units * nr_columns))
11617 > index_end)
11618 {
11619 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11620 " [in module %s]"),
11621 dwp_file->name);
11622 }
11623 }
80626a55
DE
11624
11625 return htab;
11626}
11627
11628/* Update SECTIONS with the data from SECTP.
11629
11630 This function is like the other "locate" section routines that are
11631 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11632 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11633
11634 The result is non-zero for success, or zero if an error was found. */
11635
11636static int
73869dc2
DE
11637locate_v1_virtual_dwo_sections (asection *sectp,
11638 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11639{
11640 const struct dwop_section_names *names = &dwop_section_names;
11641
11642 if (section_is_p (sectp->name, &names->abbrev_dwo))
11643 {
11644 /* There can be only one. */
049412e3 11645 if (sections->abbrev.s.section != NULL)
80626a55 11646 return 0;
049412e3 11647 sections->abbrev.s.section = sectp;
fd361982 11648 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11649 }
11650 else if (section_is_p (sectp->name, &names->info_dwo)
11651 || section_is_p (sectp->name, &names->types_dwo))
11652 {
11653 /* There can be only one. */
049412e3 11654 if (sections->info_or_types.s.section != NULL)
80626a55 11655 return 0;
049412e3 11656 sections->info_or_types.s.section = sectp;
fd361982 11657 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11658 }
11659 else if (section_is_p (sectp->name, &names->line_dwo))
11660 {
11661 /* There can be only one. */
049412e3 11662 if (sections->line.s.section != NULL)
80626a55 11663 return 0;
049412e3 11664 sections->line.s.section = sectp;
fd361982 11665 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11666 }
11667 else if (section_is_p (sectp->name, &names->loc_dwo))
11668 {
11669 /* There can be only one. */
049412e3 11670 if (sections->loc.s.section != NULL)
80626a55 11671 return 0;
049412e3 11672 sections->loc.s.section = sectp;
fd361982 11673 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11674 }
11675 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11676 {
11677 /* There can be only one. */
049412e3 11678 if (sections->macinfo.s.section != NULL)
80626a55 11679 return 0;
049412e3 11680 sections->macinfo.s.section = sectp;
fd361982 11681 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11682 }
11683 else if (section_is_p (sectp->name, &names->macro_dwo))
11684 {
11685 /* There can be only one. */
049412e3 11686 if (sections->macro.s.section != NULL)
80626a55 11687 return 0;
049412e3 11688 sections->macro.s.section = sectp;
fd361982 11689 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11690 }
11691 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11692 {
11693 /* There can be only one. */
049412e3 11694 if (sections->str_offsets.s.section != NULL)
80626a55 11695 return 0;
049412e3 11696 sections->str_offsets.s.section = sectp;
fd361982 11697 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11698 }
11699 else
11700 {
11701 /* No other kind of section is valid. */
11702 return 0;
11703 }
11704
11705 return 1;
11706}
11707
73869dc2
DE
11708/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11709 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11710 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11711 This is for DWP version 1 files. */
80626a55
DE
11712
11713static struct dwo_unit *
ed2dc618
SM
11714create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11715 struct dwp_file *dwp_file,
73869dc2
DE
11716 uint32_t unit_index,
11717 const char *comp_dir,
11718 ULONGEST signature, int is_debug_types)
80626a55 11719{
73869dc2
DE
11720 const struct dwp_hash_table *dwp_htab =
11721 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11722 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11723 const char *kind = is_debug_types ? "TU" : "CU";
11724 struct dwo_file *dwo_file;
11725 struct dwo_unit *dwo_unit;
73869dc2 11726 struct virtual_v1_dwo_sections sections;
80626a55 11727 void **dwo_file_slot;
80626a55
DE
11728 int i;
11729
73869dc2
DE
11730 gdb_assert (dwp_file->version == 1);
11731
b4f54984 11732 if (dwarf_read_debug)
80626a55 11733 {
73869dc2 11734 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11735 kind,
73869dc2 11736 pulongest (unit_index), hex_string (signature),
80626a55
DE
11737 dwp_file->name);
11738 }
11739
19ac8c2e 11740 /* Fetch the sections of this DWO unit.
80626a55
DE
11741 Put a limit on the number of sections we look for so that bad data
11742 doesn't cause us to loop forever. */
11743
73869dc2 11744#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11745 (1 /* .debug_info or .debug_types */ \
11746 + 1 /* .debug_abbrev */ \
11747 + 1 /* .debug_line */ \
11748 + 1 /* .debug_loc */ \
11749 + 1 /* .debug_str_offsets */ \
19ac8c2e 11750 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11751 + 1 /* trailing zero */)
11752
11753 memset (&sections, 0, sizeof (sections));
80626a55 11754
73869dc2 11755 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11756 {
11757 asection *sectp;
11758 uint32_t section_nr =
11759 read_4_bytes (dbfd,
73869dc2
DE
11760 dwp_htab->section_pool.v1.indices
11761 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11762
11763 if (section_nr == 0)
11764 break;
11765 if (section_nr >= dwp_file->num_sections)
11766 {
11767 error (_("Dwarf Error: bad DWP hash table, section number too large"
11768 " [in module %s]"),
11769 dwp_file->name);
11770 }
11771
11772 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11773 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11774 {
11775 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11776 " [in module %s]"),
11777 dwp_file->name);
11778 }
11779 }
11780
11781 if (i < 2
96b79293
TT
11782 || sections.info_or_types.empty ()
11783 || sections.abbrev.empty ())
80626a55
DE
11784 {
11785 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11786 " [in module %s]"),
11787 dwp_file->name);
11788 }
73869dc2 11789 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11790 {
11791 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11792 " [in module %s]"),
11793 dwp_file->name);
11794 }
11795
11796 /* It's easier for the rest of the code if we fake a struct dwo_file and
11797 have dwo_unit "live" in that. At least for now.
11798
11799 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11800 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11801 file, we can combine them back into a virtual DWO file to save space
11802 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11803 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11804
791afaa2
TT
11805 std::string virtual_dwo_name =
11806 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11807 sections.abbrev.get_id (),
11808 sections.line.get_id (),
11809 sections.loc.get_id (),
11810 sections.str_offsets.get_id ());
80626a55 11811 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11812 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11813 virtual_dwo_name.c_str (),
11814 comp_dir);
80626a55
DE
11815 /* Create one if necessary. */
11816 if (*dwo_file_slot == NULL)
11817 {
b4f54984 11818 if (dwarf_read_debug)
80626a55
DE
11819 {
11820 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11821 virtual_dwo_name.c_str ());
80626a55 11822 }
51ac9db5 11823 dwo_file = new struct dwo_file;
45940949 11824 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11825 dwo_file->comp_dir = comp_dir;
80626a55
DE
11826 dwo_file->sections.abbrev = sections.abbrev;
11827 dwo_file->sections.line = sections.line;
11828 dwo_file->sections.loc = sections.loc;
11829 dwo_file->sections.macinfo = sections.macinfo;
11830 dwo_file->sections.macro = sections.macro;
11831 dwo_file->sections.str_offsets = sections.str_offsets;
11832 /* The "str" section is global to the entire DWP file. */
11833 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11834 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11835 there's no need to record it in dwo_file.
11836 Also, we can't simply record type sections in dwo_file because
11837 we record a pointer into the vector in dwo_unit. As we collect more
11838 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11839 for it, invalidating all copies of pointers into the previous
11840 contents. */
80626a55
DE
11841 *dwo_file_slot = dwo_file;
11842 }
11843 else
11844 {
b4f54984 11845 if (dwarf_read_debug)
80626a55
DE
11846 {
11847 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11848 virtual_dwo_name.c_str ());
80626a55 11849 }
9a3c8263 11850 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11851 }
80626a55 11852
5989a64e 11853 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11854 dwo_unit->dwo_file = dwo_file;
11855 dwo_unit->signature = signature;
8d749320 11856 dwo_unit->section =
5989a64e 11857 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11858 *dwo_unit->section = sections.info_or_types;
57d63ce2 11859 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11860
11861 return dwo_unit;
11862}
11863
73869dc2
DE
11864/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11865 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11866 piece within that section used by a TU/CU, return a virtual section
11867 of just that piece. */
11868
11869static struct dwarf2_section_info
ed2dc618
SM
11870create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11871 struct dwarf2_section_info *section,
73869dc2
DE
11872 bfd_size_type offset, bfd_size_type size)
11873{
11874 struct dwarf2_section_info result;
11875 asection *sectp;
11876
11877 gdb_assert (section != NULL);
11878 gdb_assert (!section->is_virtual);
11879
11880 memset (&result, 0, sizeof (result));
11881 result.s.containing_section = section;
dc4ccb6f 11882 result.is_virtual = true;
73869dc2
DE
11883
11884 if (size == 0)
11885 return result;
11886
96b79293 11887 sectp = section->get_bfd_section ();
73869dc2
DE
11888
11889 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11890 bounds of the real section. This is a pretty-rare event, so just
11891 flag an error (easier) instead of a warning and trying to cope. */
11892 if (sectp == NULL
fd361982 11893 || offset + size > bfd_section_size (sectp))
73869dc2 11894 {
73869dc2
DE
11895 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11896 " in section %s [in module %s]"),
fd361982 11897 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11898 objfile_name (dwarf2_per_objfile->objfile));
11899 }
11900
11901 result.virtual_offset = offset;
11902 result.size = size;
11903 return result;
11904}
11905
11906/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11907 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11908 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11909 This is for DWP version 2 files. */
11910
11911static struct dwo_unit *
ed2dc618
SM
11912create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11913 struct dwp_file *dwp_file,
73869dc2
DE
11914 uint32_t unit_index,
11915 const char *comp_dir,
11916 ULONGEST signature, int is_debug_types)
11917{
73869dc2
DE
11918 const struct dwp_hash_table *dwp_htab =
11919 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11920 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11921 const char *kind = is_debug_types ? "TU" : "CU";
11922 struct dwo_file *dwo_file;
11923 struct dwo_unit *dwo_unit;
11924 struct virtual_v2_dwo_sections sections;
11925 void **dwo_file_slot;
73869dc2
DE
11926 int i;
11927
11928 gdb_assert (dwp_file->version == 2);
11929
b4f54984 11930 if (dwarf_read_debug)
73869dc2
DE
11931 {
11932 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11933 kind,
11934 pulongest (unit_index), hex_string (signature),
11935 dwp_file->name);
11936 }
11937
11938 /* Fetch the section offsets of this DWO unit. */
11939
11940 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11941
11942 for (i = 0; i < dwp_htab->nr_columns; ++i)
11943 {
11944 uint32_t offset = read_4_bytes (dbfd,
11945 dwp_htab->section_pool.v2.offsets
11946 + (((unit_index - 1) * dwp_htab->nr_columns
11947 + i)
11948 * sizeof (uint32_t)));
11949 uint32_t size = read_4_bytes (dbfd,
11950 dwp_htab->section_pool.v2.sizes
11951 + (((unit_index - 1) * dwp_htab->nr_columns
11952 + i)
11953 * sizeof (uint32_t)));
11954
11955 switch (dwp_htab->section_pool.v2.section_ids[i])
11956 {
11957 case DW_SECT_INFO:
11958 case DW_SECT_TYPES:
11959 sections.info_or_types_offset = offset;
11960 sections.info_or_types_size = size;
11961 break;
11962 case DW_SECT_ABBREV:
11963 sections.abbrev_offset = offset;
11964 sections.abbrev_size = size;
11965 break;
11966 case DW_SECT_LINE:
11967 sections.line_offset = offset;
11968 sections.line_size = size;
11969 break;
11970 case DW_SECT_LOC:
11971 sections.loc_offset = offset;
11972 sections.loc_size = size;
11973 break;
11974 case DW_SECT_STR_OFFSETS:
11975 sections.str_offsets_offset = offset;
11976 sections.str_offsets_size = size;
11977 break;
11978 case DW_SECT_MACINFO:
11979 sections.macinfo_offset = offset;
11980 sections.macinfo_size = size;
11981 break;
11982 case DW_SECT_MACRO:
11983 sections.macro_offset = offset;
11984 sections.macro_size = size;
11985 break;
11986 }
11987 }
11988
11989 /* It's easier for the rest of the code if we fake a struct dwo_file and
11990 have dwo_unit "live" in that. At least for now.
11991
11992 The DWP file can be made up of a random collection of CUs and TUs.
11993 However, for each CU + set of TUs that came from the same original DWO
11994 file, we can combine them back into a virtual DWO file to save space
11995 (fewer struct dwo_file objects to allocate). Remember that for really
11996 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11997
791afaa2
TT
11998 std::string virtual_dwo_name =
11999 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12000 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12001 (long) (sections.line_size ? sections.line_offset : 0),
12002 (long) (sections.loc_size ? sections.loc_offset : 0),
12003 (long) (sections.str_offsets_size
12004 ? sections.str_offsets_offset : 0));
73869dc2 12005 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12006 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12007 virtual_dwo_name.c_str (),
12008 comp_dir);
73869dc2
DE
12009 /* Create one if necessary. */
12010 if (*dwo_file_slot == NULL)
12011 {
b4f54984 12012 if (dwarf_read_debug)
73869dc2
DE
12013 {
12014 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12015 virtual_dwo_name.c_str ());
73869dc2 12016 }
51ac9db5 12017 dwo_file = new struct dwo_file;
45940949 12018 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12019 dwo_file->comp_dir = comp_dir;
12020 dwo_file->sections.abbrev =
ed2dc618 12021 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12022 sections.abbrev_offset, sections.abbrev_size);
12023 dwo_file->sections.line =
ed2dc618 12024 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12025 sections.line_offset, sections.line_size);
12026 dwo_file->sections.loc =
ed2dc618 12027 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12028 sections.loc_offset, sections.loc_size);
12029 dwo_file->sections.macinfo =
ed2dc618 12030 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12031 sections.macinfo_offset, sections.macinfo_size);
12032 dwo_file->sections.macro =
ed2dc618 12033 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12034 sections.macro_offset, sections.macro_size);
12035 dwo_file->sections.str_offsets =
ed2dc618
SM
12036 create_dwp_v2_section (dwarf2_per_objfile,
12037 &dwp_file->sections.str_offsets,
73869dc2
DE
12038 sections.str_offsets_offset,
12039 sections.str_offsets_size);
12040 /* The "str" section is global to the entire DWP file. */
12041 dwo_file->sections.str = dwp_file->sections.str;
12042 /* The info or types section is assigned below to dwo_unit,
12043 there's no need to record it in dwo_file.
12044 Also, we can't simply record type sections in dwo_file because
12045 we record a pointer into the vector in dwo_unit. As we collect more
12046 types we'll grow the vector and eventually have to reallocate space
12047 for it, invalidating all copies of pointers into the previous
12048 contents. */
12049 *dwo_file_slot = dwo_file;
12050 }
12051 else
12052 {
b4f54984 12053 if (dwarf_read_debug)
73869dc2
DE
12054 {
12055 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12056 virtual_dwo_name.c_str ());
73869dc2 12057 }
9a3c8263 12058 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12059 }
73869dc2 12060
5989a64e 12061 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12062 dwo_unit->dwo_file = dwo_file;
12063 dwo_unit->signature = signature;
8d749320 12064 dwo_unit->section =
5989a64e 12065 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12066 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12067 is_debug_types
73869dc2
DE
12068 ? &dwp_file->sections.types
12069 : &dwp_file->sections.info,
12070 sections.info_or_types_offset,
12071 sections.info_or_types_size);
12072 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12073
12074 return dwo_unit;
12075}
12076
57d63ce2
DE
12077/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12078 Returns NULL if the signature isn't found. */
80626a55
DE
12079
12080static struct dwo_unit *
ed2dc618
SM
12081lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12082 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12083 ULONGEST signature, int is_debug_types)
80626a55 12084{
57d63ce2
DE
12085 const struct dwp_hash_table *dwp_htab =
12086 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12087 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12088 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12089 uint32_t hash = signature & mask;
12090 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12091 unsigned int i;
12092 void **slot;
870f88f7 12093 struct dwo_unit find_dwo_cu;
80626a55
DE
12094
12095 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12096 find_dwo_cu.signature = signature;
19ac8c2e 12097 slot = htab_find_slot (is_debug_types
48b490f2
TT
12098 ? dwp_file->loaded_tus.get ()
12099 : dwp_file->loaded_cus.get (),
19ac8c2e 12100 &find_dwo_cu, INSERT);
80626a55
DE
12101
12102 if (*slot != NULL)
9a3c8263 12103 return (struct dwo_unit *) *slot;
80626a55
DE
12104
12105 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12106 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12107 {
12108 ULONGEST signature_in_table;
12109
12110 signature_in_table =
57d63ce2 12111 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12112 if (signature_in_table == signature)
12113 {
57d63ce2
DE
12114 uint32_t unit_index =
12115 read_4_bytes (dbfd,
12116 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12117
73869dc2
DE
12118 if (dwp_file->version == 1)
12119 {
ed2dc618
SM
12120 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12121 dwp_file, unit_index,
73869dc2
DE
12122 comp_dir, signature,
12123 is_debug_types);
12124 }
12125 else
12126 {
ed2dc618
SM
12127 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12128 dwp_file, unit_index,
73869dc2
DE
12129 comp_dir, signature,
12130 is_debug_types);
12131 }
9a3c8263 12132 return (struct dwo_unit *) *slot;
80626a55
DE
12133 }
12134 if (signature_in_table == 0)
12135 return NULL;
12136 hash = (hash + hash2) & mask;
12137 }
12138
12139 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12140 " [in module %s]"),
12141 dwp_file->name);
12142}
12143
ab5088bf 12144/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12145 Open the file specified by FILE_NAME and hand it off to BFD for
12146 preliminary analysis. Return a newly initialized bfd *, which
12147 includes a canonicalized copy of FILE_NAME.
80626a55 12148 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12149 SEARCH_CWD is true if the current directory is to be searched.
12150 It will be searched before debug-file-directory.
13aaf454
DE
12151 If successful, the file is added to the bfd include table of the
12152 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12153 If unable to find/open the file, return NULL.
3019eac3
DE
12154 NOTE: This function is derived from symfile_bfd_open. */
12155
192b62ce 12156static gdb_bfd_ref_ptr
ed2dc618
SM
12157try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12158 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12159{
24b9144d 12160 int desc;
9c02c129
DE
12161 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12162 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12163 to debug_file_directory. */
e0cc99a6 12164 const char *search_path;
9c02c129
DE
12165 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12166
e0cc99a6 12167 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12168 if (search_cwd)
12169 {
12170 if (*debug_file_directory != '\0')
e0cc99a6
TT
12171 {
12172 search_path_holder.reset (concat (".", dirname_separator_string,
12173 debug_file_directory,
12174 (char *) NULL));
12175 search_path = search_path_holder.get ();
12176 }
6ac97d4c 12177 else
e0cc99a6 12178 search_path = ".";
6ac97d4c 12179 }
9c02c129 12180 else
e0cc99a6 12181 search_path = debug_file_directory;
3019eac3 12182
24b9144d 12183 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12184 if (is_dwp)
12185 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12186
12187 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12188 desc = openp (search_path, flags, file_name,
3019eac3
DE
12189 O_RDONLY | O_BINARY, &absolute_name);
12190 if (desc < 0)
12191 return NULL;
12192
e0cc99a6
TT
12193 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12194 gnutarget, desc));
9c02c129
DE
12195 if (sym_bfd == NULL)
12196 return NULL;
192b62ce 12197 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12198
192b62ce
TT
12199 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12200 return NULL;
3019eac3 12201
13aaf454
DE
12202 /* Success. Record the bfd as having been included by the objfile's bfd.
12203 This is important because things like demangled_names_hash lives in the
12204 objfile's per_bfd space and may have references to things like symbol
12205 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12206 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12207
3019eac3
DE
12208 return sym_bfd;
12209}
12210
ab5088bf 12211/* Try to open DWO file FILE_NAME.
3019eac3
DE
12212 COMP_DIR is the DW_AT_comp_dir attribute.
12213 The result is the bfd handle of the file.
12214 If there is a problem finding or opening the file, return NULL.
12215 Upon success, the canonicalized path of the file is stored in the bfd,
12216 same as symfile_bfd_open. */
12217
192b62ce 12218static gdb_bfd_ref_ptr
ed2dc618
SM
12219open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12220 const char *file_name, const char *comp_dir)
3019eac3 12221{
80626a55 12222 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12223 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12224 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12225
12226 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12227
12228 if (comp_dir != NULL)
12229 {
43816ebc
TT
12230 gdb::unique_xmalloc_ptr<char> path_to_try
12231 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12232
12233 /* NOTE: If comp_dir is a relative path, this will also try the
12234 search path, which seems useful. */
ed2dc618 12235 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12236 path_to_try.get (),
ed2dc618 12237 0 /*is_dwp*/,
192b62ce 12238 1 /*search_cwd*/));
3019eac3
DE
12239 if (abfd != NULL)
12240 return abfd;
12241 }
12242
12243 /* That didn't work, try debug-file-directory, which, despite its name,
12244 is a list of paths. */
12245
12246 if (*debug_file_directory == '\0')
12247 return NULL;
12248
ed2dc618
SM
12249 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12250 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12251}
12252
80626a55
DE
12253/* This function is mapped across the sections and remembers the offset and
12254 size of each of the DWO debugging sections we are interested in. */
12255
12256static void
12257dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12258{
9a3c8263 12259 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12260 const struct dwop_section_names *names = &dwop_section_names;
12261
12262 if (section_is_p (sectp->name, &names->abbrev_dwo))
12263 {
049412e3 12264 dwo_sections->abbrev.s.section = sectp;
fd361982 12265 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12266 }
12267 else if (section_is_p (sectp->name, &names->info_dwo))
12268 {
049412e3 12269 dwo_sections->info.s.section = sectp;
fd361982 12270 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12271 }
12272 else if (section_is_p (sectp->name, &names->line_dwo))
12273 {
049412e3 12274 dwo_sections->line.s.section = sectp;
fd361982 12275 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12276 }
12277 else if (section_is_p (sectp->name, &names->loc_dwo))
12278 {
049412e3 12279 dwo_sections->loc.s.section = sectp;
fd361982 12280 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12281 }
41144253 12282 else if (section_is_p (sectp->name, &names->loclists_dwo))
12283 {
12284 dwo_sections->loclists.s.section = sectp;
12285 dwo_sections->loclists.size = bfd_section_size (sectp);
12286 }
80626a55
DE
12287 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12288 {
049412e3 12289 dwo_sections->macinfo.s.section = sectp;
fd361982 12290 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12291 }
12292 else if (section_is_p (sectp->name, &names->macro_dwo))
12293 {
049412e3 12294 dwo_sections->macro.s.section = sectp;
fd361982 12295 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12296 }
12297 else if (section_is_p (sectp->name, &names->str_dwo))
12298 {
049412e3 12299 dwo_sections->str.s.section = sectp;
fd361982 12300 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12301 }
12302 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12303 {
049412e3 12304 dwo_sections->str_offsets.s.section = sectp;
fd361982 12305 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12306 }
12307 else if (section_is_p (sectp->name, &names->types_dwo))
12308 {
12309 struct dwarf2_section_info type_section;
12310
12311 memset (&type_section, 0, sizeof (type_section));
049412e3 12312 type_section.s.section = sectp;
fd361982 12313 type_section.size = bfd_section_size (sectp);
fd5866f6 12314 dwo_sections->types.push_back (type_section);
80626a55
DE
12315 }
12316}
12317
ab5088bf 12318/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12319 by PER_CU. This is for the non-DWP case.
80626a55 12320 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12321
12322static struct dwo_file *
0ac5b59e
DE
12323open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12324 const char *dwo_name, const char *comp_dir)
3019eac3 12325{
ed2dc618 12326 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12327
fb1eb2f9 12328 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12329 if (dbfd == NULL)
12330 {
b4f54984 12331 if (dwarf_read_debug)
80626a55
DE
12332 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12333 return NULL;
12334 }
263db9a1 12335
51ac9db5 12336 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12337 dwo_file->dwo_name = dwo_name;
12338 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12339 dwo_file->dbfd = std::move (dbfd);
3019eac3 12340
fb1eb2f9 12341 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12342 &dwo_file->sections);
3019eac3 12343
18a8505e
AT
12344 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12345 dwo_file->sections.info, dwo_file->cus);
3019eac3 12346
263db9a1 12347 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12348 dwo_file->sections.types, dwo_file->tus);
3019eac3 12349
b4f54984 12350 if (dwarf_read_debug)
80626a55
DE
12351 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12352
263db9a1 12353 return dwo_file.release ();
3019eac3
DE
12354}
12355
80626a55 12356/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12357 size of each of the DWP debugging sections common to version 1 and 2 that
12358 we are interested in. */
3019eac3 12359
80626a55 12360static void
73869dc2
DE
12361dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12362 void *dwp_file_ptr)
3019eac3 12363{
9a3c8263 12364 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12365 const struct dwop_section_names *names = &dwop_section_names;
12366 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12367
80626a55 12368 /* Record the ELF section number for later lookup: this is what the
73869dc2 12369 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12370 gdb_assert (elf_section_nr < dwp_file->num_sections);
12371 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12372
80626a55
DE
12373 /* Look for specific sections that we need. */
12374 if (section_is_p (sectp->name, &names->str_dwo))
12375 {
049412e3 12376 dwp_file->sections.str.s.section = sectp;
fd361982 12377 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12378 }
12379 else if (section_is_p (sectp->name, &names->cu_index))
12380 {
049412e3 12381 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12382 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12383 }
12384 else if (section_is_p (sectp->name, &names->tu_index))
12385 {
049412e3 12386 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12387 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12388 }
12389}
3019eac3 12390
73869dc2
DE
12391/* This function is mapped across the sections and remembers the offset and
12392 size of each of the DWP version 2 debugging sections that we are interested
12393 in. This is split into a separate function because we don't know if we
12394 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12395
12396static void
12397dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12398{
9a3c8263 12399 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12400 const struct dwop_section_names *names = &dwop_section_names;
12401 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12402
12403 /* Record the ELF section number for later lookup: this is what the
12404 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12405 gdb_assert (elf_section_nr < dwp_file->num_sections);
12406 dwp_file->elf_sections[elf_section_nr] = sectp;
12407
12408 /* Look for specific sections that we need. */
12409 if (section_is_p (sectp->name, &names->abbrev_dwo))
12410 {
049412e3 12411 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12412 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12413 }
12414 else if (section_is_p (sectp->name, &names->info_dwo))
12415 {
049412e3 12416 dwp_file->sections.info.s.section = sectp;
fd361982 12417 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12418 }
12419 else if (section_is_p (sectp->name, &names->line_dwo))
12420 {
049412e3 12421 dwp_file->sections.line.s.section = sectp;
fd361982 12422 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12423 }
12424 else if (section_is_p (sectp->name, &names->loc_dwo))
12425 {
049412e3 12426 dwp_file->sections.loc.s.section = sectp;
fd361982 12427 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12428 }
12429 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12430 {
049412e3 12431 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12432 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12433 }
12434 else if (section_is_p (sectp->name, &names->macro_dwo))
12435 {
049412e3 12436 dwp_file->sections.macro.s.section = sectp;
fd361982 12437 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12438 }
12439 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12440 {
049412e3 12441 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12442 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12443 }
12444 else if (section_is_p (sectp->name, &names->types_dwo))
12445 {
049412e3 12446 dwp_file->sections.types.s.section = sectp;
fd361982 12447 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12448 }
12449}
12450
80626a55 12451/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12452
80626a55
DE
12453static hashval_t
12454hash_dwp_loaded_cutus (const void *item)
12455{
9a3c8263 12456 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12457
80626a55
DE
12458 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12459 return dwo_unit->signature;
3019eac3
DE
12460}
12461
80626a55 12462/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12463
80626a55
DE
12464static int
12465eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12466{
9a3c8263
SM
12467 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12468 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12469
80626a55
DE
12470 return dua->signature == dub->signature;
12471}
3019eac3 12472
80626a55 12473/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12474
48b490f2 12475static htab_up
298e9637 12476allocate_dwp_loaded_cutus_table ()
80626a55 12477{
48b490f2
TT
12478 return htab_up (htab_create_alloc (3,
12479 hash_dwp_loaded_cutus,
12480 eq_dwp_loaded_cutus,
12481 NULL, xcalloc, xfree));
80626a55 12482}
3019eac3 12483
ab5088bf
DE
12484/* Try to open DWP file FILE_NAME.
12485 The result is the bfd handle of the file.
12486 If there is a problem finding or opening the file, return NULL.
12487 Upon success, the canonicalized path of the file is stored in the bfd,
12488 same as symfile_bfd_open. */
12489
192b62ce 12490static gdb_bfd_ref_ptr
ed2dc618
SM
12491open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12492 const char *file_name)
ab5088bf 12493{
ed2dc618
SM
12494 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12495 1 /*is_dwp*/,
192b62ce 12496 1 /*search_cwd*/));
6ac97d4c
DE
12497 if (abfd != NULL)
12498 return abfd;
12499
12500 /* Work around upstream bug 15652.
12501 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12502 [Whether that's a "bug" is debatable, but it is getting in our way.]
12503 We have no real idea where the dwp file is, because gdb's realpath-ing
12504 of the executable's path may have discarded the needed info.
12505 [IWBN if the dwp file name was recorded in the executable, akin to
12506 .gnu_debuglink, but that doesn't exist yet.]
12507 Strip the directory from FILE_NAME and search again. */
12508 if (*debug_file_directory != '\0')
12509 {
12510 /* Don't implicitly search the current directory here.
12511 If the user wants to search "." to handle this case,
12512 it must be added to debug-file-directory. */
ed2dc618
SM
12513 return try_open_dwop_file (dwarf2_per_objfile,
12514 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12515 0 /*search_cwd*/);
12516 }
12517
12518 return NULL;
ab5088bf
DE
12519}
12520
80626a55
DE
12521/* Initialize the use of the DWP file for the current objfile.
12522 By convention the name of the DWP file is ${objfile}.dwp.
12523 The result is NULL if it can't be found. */
a766d390 12524
400174b1 12525static std::unique_ptr<struct dwp_file>
ed2dc618 12526open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12527{
12528 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12529
82bf32bc
JK
12530 /* Try to find first .dwp for the binary file before any symbolic links
12531 resolving. */
6c447423
DE
12532
12533 /* If the objfile is a debug file, find the name of the real binary
12534 file and get the name of dwp file from there. */
d721ba37 12535 std::string dwp_name;
6c447423
DE
12536 if (objfile->separate_debug_objfile_backlink != NULL)
12537 {
12538 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12539 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12540
d721ba37 12541 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12542 }
12543 else
d721ba37
PA
12544 dwp_name = objfile->original_name;
12545
12546 dwp_name += ".dwp";
80626a55 12547
ed2dc618 12548 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12549 if (dbfd == NULL
12550 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12551 {
12552 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12553 dwp_name = objfile_name (objfile);
12554 dwp_name += ".dwp";
ed2dc618 12555 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12556 }
12557
80626a55
DE
12558 if (dbfd == NULL)
12559 {
b4f54984 12560 if (dwarf_read_debug)
d721ba37 12561 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12562 return std::unique_ptr<dwp_file> ();
3019eac3 12563 }
400174b1
TT
12564
12565 const char *name = bfd_get_filename (dbfd.get ());
12566 std::unique_ptr<struct dwp_file> dwp_file
12567 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12568
0a0f4c01 12569 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12570 dwp_file->elf_sections =
5989a64e 12571 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12572 dwp_file->num_sections, asection *);
12573
400174b1
TT
12574 bfd_map_over_sections (dwp_file->dbfd.get (),
12575 dwarf2_locate_common_dwp_sections,
12576 dwp_file.get ());
80626a55 12577
400174b1
TT
12578 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12579 0);
80626a55 12580
400174b1
TT
12581 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12582 1);
80626a55 12583
73869dc2 12584 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12585 if (dwp_file->cus && dwp_file->tus
12586 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12587 {
12588 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12589 pretty bizarre. We use pulongest here because that's the established
4d65956b 12590 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12591 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12592 " TU version %s [in DWP file %s]"),
12593 pulongest (dwp_file->cus->version),
d721ba37 12594 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12595 }
08302ed2
DE
12596
12597 if (dwp_file->cus)
12598 dwp_file->version = dwp_file->cus->version;
12599 else if (dwp_file->tus)
12600 dwp_file->version = dwp_file->tus->version;
12601 else
12602 dwp_file->version = 2;
73869dc2
DE
12603
12604 if (dwp_file->version == 2)
400174b1
TT
12605 bfd_map_over_sections (dwp_file->dbfd.get (),
12606 dwarf2_locate_v2_dwp_sections,
12607 dwp_file.get ());
73869dc2 12608
298e9637
SM
12609 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12610 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12611
b4f54984 12612 if (dwarf_read_debug)
80626a55
DE
12613 {
12614 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12615 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12616 " %s CUs, %s TUs\n",
12617 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12618 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12619 }
12620
12621 return dwp_file;
3019eac3 12622}
c906108c 12623
ab5088bf
DE
12624/* Wrapper around open_and_init_dwp_file, only open it once. */
12625
12626static struct dwp_file *
ed2dc618 12627get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12628{
5989a64e 12629 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12630 {
5989a64e 12631 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12632 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12633 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12634 }
5989a64e 12635 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12636}
12637
80626a55
DE
12638/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12639 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12640 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12641 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12642 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12643
12644 This is called, for example, when wanting to read a variable with a
12645 complex location. Therefore we don't want to do file i/o for every call.
12646 Therefore we don't want to look for a DWO file on every call.
12647 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12648 then we check if we've already seen DWO_NAME, and only THEN do we check
12649 for a DWO file.
12650
1c658ad5 12651 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12652 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12653
3019eac3 12654static struct dwo_unit *
80626a55
DE
12655lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12656 const char *dwo_name, const char *comp_dir,
12657 ULONGEST signature, int is_debug_types)
3019eac3 12658{
ed2dc618 12659 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12660 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12661 const char *kind = is_debug_types ? "TU" : "CU";
12662 void **dwo_file_slot;
3019eac3 12663 struct dwo_file *dwo_file;
80626a55 12664 struct dwp_file *dwp_file;
cb1df416 12665
6a506a2d
DE
12666 /* First see if there's a DWP file.
12667 If we have a DWP file but didn't find the DWO inside it, don't
12668 look for the original DWO file. It makes gdb behave differently
12669 depending on whether one is debugging in the build tree. */
cf2c3c16 12670
ed2dc618 12671 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12672 if (dwp_file != NULL)
cf2c3c16 12673 {
80626a55
DE
12674 const struct dwp_hash_table *dwp_htab =
12675 is_debug_types ? dwp_file->tus : dwp_file->cus;
12676
12677 if (dwp_htab != NULL)
12678 {
12679 struct dwo_unit *dwo_cutu =
ed2dc618 12680 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12681 signature, is_debug_types);
80626a55
DE
12682
12683 if (dwo_cutu != NULL)
12684 {
b4f54984 12685 if (dwarf_read_debug)
80626a55
DE
12686 {
12687 fprintf_unfiltered (gdb_stdlog,
12688 "Virtual DWO %s %s found: @%s\n",
12689 kind, hex_string (signature),
12690 host_address_to_string (dwo_cutu));
12691 }
12692 return dwo_cutu;
12693 }
12694 }
12695 }
6a506a2d 12696 else
80626a55 12697 {
6a506a2d 12698 /* No DWP file, look for the DWO file. */
80626a55 12699
ed2dc618
SM
12700 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12701 dwo_name, comp_dir);
6a506a2d 12702 if (*dwo_file_slot == NULL)
80626a55 12703 {
6a506a2d
DE
12704 /* Read in the file and build a table of the CUs/TUs it contains. */
12705 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12706 }
6a506a2d 12707 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12708 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12709
6a506a2d 12710 if (dwo_file != NULL)
19c3d4c9 12711 {
6a506a2d
DE
12712 struct dwo_unit *dwo_cutu = NULL;
12713
12714 if (is_debug_types && dwo_file->tus)
12715 {
12716 struct dwo_unit find_dwo_cutu;
12717
12718 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12719 find_dwo_cutu.signature = signature;
9a3c8263 12720 dwo_cutu
b0b6a987
TT
12721 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12722 &find_dwo_cutu);
6a506a2d 12723 }
33c5cd75 12724 else if (!is_debug_types && dwo_file->cus)
80626a55 12725 {
33c5cd75
DB
12726 struct dwo_unit find_dwo_cutu;
12727
12728 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12729 find_dwo_cutu.signature = signature;
b0b6a987 12730 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12731 &find_dwo_cutu);
6a506a2d
DE
12732 }
12733
12734 if (dwo_cutu != NULL)
12735 {
b4f54984 12736 if (dwarf_read_debug)
6a506a2d
DE
12737 {
12738 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12739 kind, dwo_name, hex_string (signature),
12740 host_address_to_string (dwo_cutu));
12741 }
12742 return dwo_cutu;
80626a55
DE
12743 }
12744 }
2e276125 12745 }
9cdd5dbd 12746
80626a55
DE
12747 /* We didn't find it. This could mean a dwo_id mismatch, or
12748 someone deleted the DWO/DWP file, or the search path isn't set up
12749 correctly to find the file. */
12750
b4f54984 12751 if (dwarf_read_debug)
80626a55
DE
12752 {
12753 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12754 kind, dwo_name, hex_string (signature));
12755 }
3019eac3 12756
6656a72d
DE
12757 /* This is a warning and not a complaint because it can be caused by
12758 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12759 {
12760 /* Print the name of the DWP file if we looked there, helps the user
12761 better diagnose the problem. */
791afaa2 12762 std::string dwp_text;
43942612
DE
12763
12764 if (dwp_file != NULL)
791afaa2
TT
12765 dwp_text = string_printf (" [in DWP file %s]",
12766 lbasename (dwp_file->name));
43942612 12767
9d8780f0 12768 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12769 " [in module %s]"),
12770 kind, dwo_name, hex_string (signature),
791afaa2 12771 dwp_text.c_str (),
43942612 12772 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12773 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12774 }
3019eac3 12775 return NULL;
5fb290d7
DJ
12776}
12777
80626a55
DE
12778/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12779 See lookup_dwo_cutu_unit for details. */
12780
12781static struct dwo_unit *
12782lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12783 const char *dwo_name, const char *comp_dir,
12784 ULONGEST signature)
12785{
12786 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12787}
12788
12789/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12790 See lookup_dwo_cutu_unit for details. */
12791
12792static struct dwo_unit *
12793lookup_dwo_type_unit (struct signatured_type *this_tu,
12794 const char *dwo_name, const char *comp_dir)
12795{
12796 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12797}
12798
89e63ee4
DE
12799/* Traversal function for queue_and_load_all_dwo_tus. */
12800
12801static int
12802queue_and_load_dwo_tu (void **slot, void *info)
12803{
12804 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12805 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12806 ULONGEST signature = dwo_unit->signature;
12807 struct signatured_type *sig_type =
12808 lookup_dwo_signatured_type (per_cu->cu, signature);
12809
12810 if (sig_type != NULL)
12811 {
12812 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12813
12814 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12815 a real dependency of PER_CU on SIG_TYPE. That is detected later
12816 while processing PER_CU. */
12817 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12818 load_full_type_unit (sig_cu);
ae640021 12819 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12820 }
12821
12822 return 1;
12823}
12824
12825/* Queue all TUs contained in the DWO of PER_CU to be read in.
12826 The DWO may have the only definition of the type, though it may not be
12827 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12828 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12829
12830static void
12831queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12832{
12833 struct dwo_unit *dwo_unit;
12834 struct dwo_file *dwo_file;
12835
12836 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12837 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12838 gdb_assert (per_cu->cu != NULL);
12839
12840 dwo_unit = per_cu->cu->dwo_unit;
12841 gdb_assert (dwo_unit != NULL);
12842
12843 dwo_file = dwo_unit->dwo_file;
12844 if (dwo_file->tus != NULL)
b0b6a987
TT
12845 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12846 per_cu);
89e63ee4
DE
12847}
12848
3019eac3 12849/* Read in various DIEs. */
348e048f 12850
d389af10 12851/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12852 Inherit only the children of the DW_AT_abstract_origin DIE not being
12853 already referenced by DW_AT_abstract_origin from the children of the
12854 current DIE. */
d389af10
JK
12855
12856static void
12857inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12858{
12859 struct die_info *child_die;
791afaa2 12860 sect_offset *offsetp;
d389af10
JK
12861 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12862 struct die_info *origin_die;
12863 /* Iterator of the ORIGIN_DIE children. */
12864 struct die_info *origin_child_die;
d389af10 12865 struct attribute *attr;
cd02d79d
PA
12866 struct dwarf2_cu *origin_cu;
12867 struct pending **origin_previous_list_in_scope;
d389af10
JK
12868
12869 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12870 if (!attr)
12871 return;
12872
cd02d79d
PA
12873 /* Note that following die references may follow to a die in a
12874 different cu. */
12875
12876 origin_cu = cu;
12877 origin_die = follow_die_ref (die, attr, &origin_cu);
12878
12879 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12880 symbols in. */
12881 origin_previous_list_in_scope = origin_cu->list_in_scope;
12882 origin_cu->list_in_scope = cu->list_in_scope;
12883
edb3359d
DJ
12884 if (die->tag != origin_die->tag
12885 && !(die->tag == DW_TAG_inlined_subroutine
12886 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12887 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12888 sect_offset_str (die->sect_off),
12889 sect_offset_str (origin_die->sect_off));
d389af10 12890
791afaa2 12891 std::vector<sect_offset> offsets;
d389af10 12892
3ea89b92
PMR
12893 for (child_die = die->child;
12894 child_die && child_die->tag;
436c571c 12895 child_die = child_die->sibling)
3ea89b92
PMR
12896 {
12897 struct die_info *child_origin_die;
12898 struct dwarf2_cu *child_origin_cu;
12899
12900 /* We are trying to process concrete instance entries:
216f72a1 12901 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12902 it's not relevant to our analysis here. i.e. detecting DIEs that are
12903 present in the abstract instance but not referenced in the concrete
12904 one. */
216f72a1
JK
12905 if (child_die->tag == DW_TAG_call_site
12906 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12907 continue;
12908
c38f313d
DJ
12909 /* For each CHILD_DIE, find the corresponding child of
12910 ORIGIN_DIE. If there is more than one layer of
12911 DW_AT_abstract_origin, follow them all; there shouldn't be,
12912 but GCC versions at least through 4.4 generate this (GCC PR
12913 40573). */
3ea89b92
PMR
12914 child_origin_die = child_die;
12915 child_origin_cu = cu;
c38f313d
DJ
12916 while (1)
12917 {
cd02d79d
PA
12918 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12919 child_origin_cu);
c38f313d
DJ
12920 if (attr == NULL)
12921 break;
cd02d79d
PA
12922 child_origin_die = follow_die_ref (child_origin_die, attr,
12923 &child_origin_cu);
c38f313d
DJ
12924 }
12925
d389af10
JK
12926 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12927 counterpart may exist. */
c38f313d 12928 if (child_origin_die != child_die)
d389af10 12929 {
edb3359d
DJ
12930 if (child_die->tag != child_origin_die->tag
12931 && !(child_die->tag == DW_TAG_inlined_subroutine
12932 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12933 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12934 "different tags"),
9d8780f0
SM
12935 sect_offset_str (child_die->sect_off),
12936 sect_offset_str (child_origin_die->sect_off));
c38f313d 12937 if (child_origin_die->parent != origin_die)
b98664d3 12938 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12939 "different parents"),
9d8780f0
SM
12940 sect_offset_str (child_die->sect_off),
12941 sect_offset_str (child_origin_die->sect_off));
c38f313d 12942 else
791afaa2 12943 offsets.push_back (child_origin_die->sect_off);
d389af10 12944 }
d389af10 12945 }
791afaa2
TT
12946 std::sort (offsets.begin (), offsets.end ());
12947 sect_offset *offsets_end = offsets.data () + offsets.size ();
12948 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12949 if (offsetp[-1] == *offsetp)
b98664d3 12950 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12951 "to DIE %s as their abstract origin"),
12952 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12953
791afaa2 12954 offsetp = offsets.data ();
d389af10
JK
12955 origin_child_die = origin_die->child;
12956 while (origin_child_die && origin_child_die->tag)
12957 {
12958 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12959 while (offsetp < offsets_end
9c541725 12960 && *offsetp < origin_child_die->sect_off)
d389af10 12961 offsetp++;
b64f50a1 12962 if (offsetp >= offsets_end
9c541725 12963 || *offsetp > origin_child_die->sect_off)
d389af10 12964 {
adde2bff
DE
12965 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12966 Check whether we're already processing ORIGIN_CHILD_DIE.
12967 This can happen with mutually referenced abstract_origins.
12968 PR 16581. */
12969 if (!origin_child_die->in_process)
12970 process_die (origin_child_die, origin_cu);
d389af10 12971 }
436c571c 12972 origin_child_die = origin_child_die->sibling;
d389af10 12973 }
cd02d79d 12974 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12975
12976 if (cu != origin_cu)
12977 compute_delayed_physnames (origin_cu);
d389af10
JK
12978}
12979
c906108c 12980static void
e7c27a73 12981read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12982{
518817b3 12983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12984 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12985 struct context_stack *newobj;
c906108c
SS
12986 CORE_ADDR lowpc;
12987 CORE_ADDR highpc;
12988 struct die_info *child_die;
edb3359d 12989 struct attribute *attr, *call_line, *call_file;
15d034d0 12990 const char *name;
e142c38c 12991 CORE_ADDR baseaddr;
801e3a5b 12992 struct block *block;
edb3359d 12993 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12994 std::vector<struct symbol *> template_args;
34eaf542 12995 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12996
12997 if (inlined_func)
12998 {
12999 /* If we do not have call site information, we can't show the
13000 caller of this inlined function. That's too confusing, so
13001 only use the scope for local variables. */
13002 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13003 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13004 if (call_line == NULL || call_file == NULL)
13005 {
13006 read_lexical_block_scope (die, cu);
13007 return;
13008 }
13009 }
c906108c 13010
b3b3bada 13011 baseaddr = objfile->text_section_offset ();
e142c38c 13012
94af9270 13013 name = dwarf2_name (die, cu);
c906108c 13014
e8d05480
JB
13015 /* Ignore functions with missing or empty names. These are actually
13016 illegal according to the DWARF standard. */
13017 if (name == NULL)
13018 {
b98664d3 13019 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13020 sect_offset_str (die->sect_off));
e8d05480
JB
13021 return;
13022 }
13023
13024 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13025 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13026 <= PC_BOUNDS_INVALID)
e8d05480 13027 {
ae4d0c03
PM
13028 attr = dwarf2_attr (die, DW_AT_external, cu);
13029 if (!attr || !DW_UNSND (attr))
b98664d3 13030 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13031 "for subprogram DIE at %s"),
13032 sect_offset_str (die->sect_off));
e8d05480
JB
13033 return;
13034 }
c906108c 13035
3e29f34a
MR
13036 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13037 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13038
34eaf542
TT
13039 /* If we have any template arguments, then we must allocate a
13040 different sort of symbol. */
436c571c 13041 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13042 {
13043 if (child_die->tag == DW_TAG_template_type_param
13044 || child_die->tag == DW_TAG_template_value_param)
13045 {
8c14c3a3 13046 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13047 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13048 break;
13049 }
13050 }
13051
c24bdb02 13052 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13053 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13054 (struct symbol *) templ_func);
4c2df51b 13055
81873cc8 13056 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13057 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13058 cu->language);
13059
4cecd739
DJ
13060 /* If there is a location expression for DW_AT_frame_base, record
13061 it. */
e142c38c 13062 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13063 if (attr != nullptr)
fe978cb0 13064 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13065
63e43d3a
PMR
13066 /* If there is a location for the static link, record it. */
13067 newobj->static_link = NULL;
13068 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13069 if (attr != nullptr)
63e43d3a 13070 {
224c3ddb
SM
13071 newobj->static_link
13072 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13073 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13074 cu->per_cu->addr_type ());
63e43d3a
PMR
13075 }
13076
c24bdb02 13077 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13078
639d11d3 13079 if (die->child != NULL)
c906108c 13080 {
639d11d3 13081 child_die = die->child;
c906108c
SS
13082 while (child_die && child_die->tag)
13083 {
34eaf542
TT
13084 if (child_die->tag == DW_TAG_template_type_param
13085 || child_die->tag == DW_TAG_template_value_param)
13086 {
13087 struct symbol *arg = new_symbol (child_die, NULL, cu);
13088
f1078f66 13089 if (arg != NULL)
2f4732b0 13090 template_args.push_back (arg);
34eaf542
TT
13091 }
13092 else
13093 process_die (child_die, cu);
436c571c 13094 child_die = child_die->sibling;
c906108c
SS
13095 }
13096 }
13097
d389af10
JK
13098 inherit_abstract_dies (die, cu);
13099
4a811a97
UW
13100 /* If we have a DW_AT_specification, we might need to import using
13101 directives from the context of the specification DIE. See the
13102 comment in determine_prefix. */
13103 if (cu->language == language_cplus
13104 && dwarf2_attr (die, DW_AT_specification, cu))
13105 {
13106 struct dwarf2_cu *spec_cu = cu;
13107 struct die_info *spec_die = die_specification (die, &spec_cu);
13108
13109 while (spec_die)
13110 {
13111 child_die = spec_die->child;
13112 while (child_die && child_die->tag)
13113 {
13114 if (child_die->tag == DW_TAG_imported_module)
13115 process_die (child_die, spec_cu);
436c571c 13116 child_die = child_die->sibling;
4a811a97
UW
13117 }
13118
13119 /* In some cases, GCC generates specification DIEs that
13120 themselves contain DW_AT_specification attributes. */
13121 spec_die = die_specification (spec_die, &spec_cu);
13122 }
13123 }
13124
c24bdb02 13125 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13126 /* Make a block for the local symbols within. */
c24bdb02 13127 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13128 cstk.static_link, lowpc, highpc);
801e3a5b 13129
df8a16a1 13130 /* For C++, set the block's scope. */
45280282
IB
13131 if ((cu->language == language_cplus
13132 || cu->language == language_fortran
c44af4eb
TT
13133 || cu->language == language_d
13134 || cu->language == language_rust)
4d4ec4e5 13135 && cu->processing_has_namespace_info)
195a3f6c
TT
13136 block_set_scope (block, determine_prefix (die, cu),
13137 &objfile->objfile_obstack);
df8a16a1 13138
801e3a5b
JB
13139 /* If we have address ranges, record them. */
13140 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13141
a60f3166 13142 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13143
34eaf542 13144 /* Attach template arguments to function. */
2f4732b0 13145 if (!template_args.empty ())
34eaf542
TT
13146 {
13147 gdb_assert (templ_func != NULL);
13148
2f4732b0 13149 templ_func->n_template_arguments = template_args.size ();
34eaf542 13150 templ_func->template_arguments
8d749320
SM
13151 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13152 templ_func->n_template_arguments);
34eaf542 13153 memcpy (templ_func->template_arguments,
2f4732b0 13154 template_args.data (),
34eaf542 13155 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13156
13157 /* Make sure that the symtab is set on the new symbols. Even
13158 though they don't appear in this symtab directly, other parts
13159 of gdb assume that symbols do, and this is reasonably
13160 true. */
8634679f 13161 for (symbol *sym : template_args)
3e1d3d8c 13162 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13163 }
13164
208d8187
JB
13165 /* In C++, we can have functions nested inside functions (e.g., when
13166 a function declares a class that has methods). This means that
13167 when we finish processing a function scope, we may need to go
13168 back to building a containing block's symbol lists. */
c24bdb02
KS
13169 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13170 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13171
921e78cf
JB
13172 /* If we've finished processing a top-level function, subsequent
13173 symbols go in the file symbol list. */
c24bdb02
KS
13174 if (cu->get_builder ()->outermost_context_p ())
13175 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13176}
13177
13178/* Process all the DIES contained within a lexical block scope. Start
13179 a new scope, process the dies, and then close the scope. */
13180
13181static void
e7c27a73 13182read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13183{
518817b3 13184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13185 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13186 CORE_ADDR lowpc, highpc;
13187 struct die_info *child_die;
e142c38c
DJ
13188 CORE_ADDR baseaddr;
13189
b3b3bada 13190 baseaddr = objfile->text_section_offset ();
c906108c
SS
13191
13192 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13193 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13194 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13195 be nasty. Might be easier to properly extend generic blocks to
af34e669 13196 describe ranges. */
e385593e
JK
13197 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13198 {
13199 case PC_BOUNDS_NOT_PRESENT:
13200 /* DW_TAG_lexical_block has no attributes, process its children as if
13201 there was no wrapping by that DW_TAG_lexical_block.
13202 GCC does no longer produces such DWARF since GCC r224161. */
13203 for (child_die = die->child;
13204 child_die != NULL && child_die->tag;
436c571c 13205 child_die = child_die->sibling)
4f7bc5ed
TT
13206 {
13207 /* We might already be processing this DIE. This can happen
13208 in an unusual circumstance -- where a subroutine A
13209 appears lexically in another subroutine B, but A actually
13210 inlines B. The recursion is broken here, rather than in
13211 inherit_abstract_dies, because it seems better to simply
13212 drop concrete children here. */
13213 if (!child_die->in_process)
13214 process_die (child_die, cu);
13215 }
e385593e
JK
13216 return;
13217 case PC_BOUNDS_INVALID:
13218 return;
13219 }
3e29f34a
MR
13220 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13221 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13222
c24bdb02 13223 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13224 if (die->child != NULL)
c906108c 13225 {
639d11d3 13226 child_die = die->child;
c906108c
SS
13227 while (child_die && child_die->tag)
13228 {
e7c27a73 13229 process_die (child_die, cu);
436c571c 13230 child_die = child_die->sibling;
c906108c
SS
13231 }
13232 }
3ea89b92 13233 inherit_abstract_dies (die, cu);
c24bdb02 13234 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13235
c24bdb02
KS
13236 if (*cu->get_builder ()->get_local_symbols () != NULL
13237 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13238 {
801e3a5b 13239 struct block *block
c24bdb02 13240 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13241 cstk.start_addr, highpc);
801e3a5b
JB
13242
13243 /* Note that recording ranges after traversing children, as we
13244 do here, means that recording a parent's ranges entails
13245 walking across all its children's ranges as they appear in
13246 the address map, which is quadratic behavior.
13247
13248 It would be nicer to record the parent's ranges before
13249 traversing its children, simply overriding whatever you find
13250 there. But since we don't even decide whether to create a
13251 block until after we've traversed its children, that's hard
13252 to do. */
13253 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13254 }
c24bdb02
KS
13255 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13256 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13257}
13258
216f72a1 13259/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13260
13261static void
13262read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13263{
a50264ba
TT
13264 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
13265 struct objfile *objfile = per_objfile->objfile;
08feed99 13266 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13267 CORE_ADDR pc, baseaddr;
13268 struct attribute *attr;
13269 struct call_site *call_site, call_site_local;
13270 void **slot;
13271 int nparams;
13272 struct die_info *child_die;
13273
b3b3bada 13274 baseaddr = objfile->text_section_offset ();
96408a79 13275
216f72a1
JK
13276 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13277 if (attr == NULL)
13278 {
13279 /* This was a pre-DWARF-5 GNU extension alias
13280 for DW_AT_call_return_pc. */
13281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13282 }
96408a79
SA
13283 if (!attr)
13284 {
b98664d3 13285 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13286 "DIE %s [in module %s]"),
13287 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13288 return;
13289 }
cd6c91b4 13290 pc = attr->value_as_address () + baseaddr;
3e29f34a 13291 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13292
13293 if (cu->call_site_htab == NULL)
13294 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13295 NULL, &objfile->objfile_obstack,
13296 hashtab_obstack_allocate, NULL);
13297 call_site_local.pc = pc;
13298 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13299 if (*slot != NULL)
13300 {
b98664d3 13301 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13302 "DIE %s [in module %s]"),
13303 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13304 objfile_name (objfile));
96408a79
SA
13305 return;
13306 }
13307
13308 /* Count parameters at the caller. */
13309
13310 nparams = 0;
13311 for (child_die = die->child; child_die && child_die->tag;
436c571c 13312 child_die = child_die->sibling)
96408a79 13313 {
216f72a1
JK
13314 if (child_die->tag != DW_TAG_call_site_parameter
13315 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13316 {
b98664d3 13317 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13318 "DW_TAG_call_site child DIE %s [in module %s]"),
13319 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13320 objfile_name (objfile));
96408a79
SA
13321 continue;
13322 }
13323
13324 nparams++;
13325 }
13326
224c3ddb
SM
13327 call_site
13328 = ((struct call_site *)
13329 obstack_alloc (&objfile->objfile_obstack,
13330 sizeof (*call_site)
13331 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13332 *slot = call_site;
13333 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13334 call_site->pc = pc;
13335
216f72a1
JK
13336 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13337 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13338 {
13339 struct die_info *func_die;
13340
13341 /* Skip also over DW_TAG_inlined_subroutine. */
13342 for (func_die = die->parent;
13343 func_die && func_die->tag != DW_TAG_subprogram
13344 && func_die->tag != DW_TAG_subroutine_type;
13345 func_die = func_die->parent);
13346
216f72a1
JK
13347 /* DW_AT_call_all_calls is a superset
13348 of DW_AT_call_all_tail_calls. */
96408a79 13349 if (func_die
216f72a1 13350 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13351 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13352 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13353 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13354 {
13355 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13356 not complete. But keep CALL_SITE for look ups via call_site_htab,
13357 both the initial caller containing the real return address PC and
13358 the final callee containing the current PC of a chain of tail
13359 calls do not need to have the tail call list complete. But any
13360 function candidate for a virtual tail call frame searched via
13361 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13362 determined unambiguously. */
13363 }
13364 else
13365 {
13366 struct type *func_type = NULL;
13367
13368 if (func_die)
13369 func_type = get_die_type (func_die, cu);
13370 if (func_type != NULL)
13371 {
78134374 13372 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13373
13374 /* Enlist this call site to the function. */
13375 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13376 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13377 }
13378 else
b98664d3 13379 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13380 "DIE %s [in module %s]"),
13381 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13382 }
13383 }
13384
216f72a1
JK
13385 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13386 if (attr == NULL)
13387 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13388 if (attr == NULL)
13389 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13390 if (attr == NULL)
216f72a1
JK
13391 {
13392 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13393 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13394 }
96408a79 13395 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13396 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13397 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13398 else if (attr->form_is_block ())
96408a79
SA
13399 {
13400 struct dwarf2_locexpr_baton *dlbaton;
13401
8d749320 13402 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13403 dlbaton->data = DW_BLOCK (attr)->data;
13404 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13405 dlbaton->per_objfile = per_objfile;
96408a79
SA
13406 dlbaton->per_cu = cu->per_cu;
13407
13408 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13409 }
cd6c91b4 13410 else if (attr->form_is_ref ())
96408a79 13411 {
96408a79
SA
13412 struct dwarf2_cu *target_cu = cu;
13413 struct die_info *target_die;
13414
ac9ec31b 13415 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13416 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13417 if (die_is_declaration (target_die, target_cu))
13418 {
7d45c7c3 13419 const char *target_physname;
9112db09
JK
13420
13421 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13422 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13423 if (target_physname == NULL)
9112db09 13424 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13425 if (target_physname == NULL)
b98664d3 13426 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13427 "physname, for referencing DIE %s [in module %s]"),
13428 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13429 else
7d455152 13430 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13431 }
13432 else
13433 {
13434 CORE_ADDR lowpc;
13435
13436 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13437 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13438 <= PC_BOUNDS_INVALID)
b98664d3 13439 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13440 "low pc, for referencing DIE %s [in module %s]"),
13441 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13442 else
3e29f34a
MR
13443 {
13444 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13445 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13446 }
96408a79
SA
13447 }
13448 }
13449 else
b98664d3 13450 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13451 "block nor reference, for DIE %s [in module %s]"),
13452 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13453
13454 call_site->per_cu = cu->per_cu;
13455
13456 for (child_die = die->child;
13457 child_die && child_die->tag;
436c571c 13458 child_die = child_die->sibling)
96408a79 13459 {
96408a79 13460 struct call_site_parameter *parameter;
1788b2d3 13461 struct attribute *loc, *origin;
96408a79 13462
216f72a1
JK
13463 if (child_die->tag != DW_TAG_call_site_parameter
13464 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13465 {
13466 /* Already printed the complaint above. */
13467 continue;
13468 }
13469
13470 gdb_assert (call_site->parameter_count < nparams);
13471 parameter = &call_site->parameter[call_site->parameter_count];
13472
1788b2d3
JK
13473 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13474 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13475 register is contained in DW_AT_call_value. */
96408a79 13476
24c5c679 13477 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13478 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13479 if (origin == NULL)
13480 {
13481 /* This was a pre-DWARF-5 GNU extension alias
13482 for DW_AT_call_parameter. */
13483 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13484 }
cd6c91b4 13485 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13486 {
1788b2d3 13487 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13488
0826b30a 13489 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13490 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13491 {
13492 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13493 binding can be done only inside one CU. Such referenced DIE
13494 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13495 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13496 "DW_TAG_call_site child DIE %s [in module %s]"),
13497 sect_offset_str (child_die->sect_off),
9c541725 13498 objfile_name (objfile));
d76b7dbc
JK
13499 continue;
13500 }
9c541725
PA
13501 parameter->u.param_cu_off
13502 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13503 }
4fc6c0d5 13504 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13505 {
b98664d3 13506 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13507 "DW_TAG_call_site child DIE %s [in module %s]"),
13508 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13509 continue;
13510 }
24c5c679 13511 else
96408a79 13512 {
24c5c679
JK
13513 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13514 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13515 if (parameter->u.dwarf_reg != -1)
13516 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13517 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13518 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13519 &parameter->u.fb_offset))
13520 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13521 else
13522 {
b98664d3 13523 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13524 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13525 "DW_TAG_call_site child DIE %s "
24c5c679 13526 "[in module %s]"),
9d8780f0 13527 sect_offset_str (child_die->sect_off),
9c541725 13528 objfile_name (objfile));
24c5c679
JK
13529 continue;
13530 }
96408a79
SA
13531 }
13532
216f72a1
JK
13533 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13534 if (attr == NULL)
13535 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13536 if (attr == NULL || !attr->form_is_block ())
96408a79 13537 {
b98664d3 13538 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13539 "DW_TAG_call_site child DIE %s [in module %s]"),
13540 sect_offset_str (child_die->sect_off),
9c541725 13541 objfile_name (objfile));
96408a79
SA
13542 continue;
13543 }
13544 parameter->value = DW_BLOCK (attr)->data;
13545 parameter->value_size = DW_BLOCK (attr)->size;
13546
13547 /* Parameters are not pre-cleared by memset above. */
13548 parameter->data_value = NULL;
13549 parameter->data_value_size = 0;
13550 call_site->parameter_count++;
13551
216f72a1
JK
13552 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13553 if (attr == NULL)
13554 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13555 if (attr != nullptr)
96408a79 13556 {
4fc6c0d5 13557 if (!attr->form_is_block ())
b98664d3 13558 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13559 "DW_TAG_call_site child DIE %s [in module %s]"),
13560 sect_offset_str (child_die->sect_off),
9c541725 13561 objfile_name (objfile));
96408a79
SA
13562 else
13563 {
13564 parameter->data_value = DW_BLOCK (attr)->data;
13565 parameter->data_value_size = DW_BLOCK (attr)->size;
13566 }
13567 }
13568 }
13569}
13570
71a3c369
TT
13571/* Helper function for read_variable. If DIE represents a virtual
13572 table, then return the type of the concrete object that is
13573 associated with the virtual table. Otherwise, return NULL. */
13574
13575static struct type *
13576rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13577{
13578 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13579 if (attr == NULL)
13580 return NULL;
13581
13582 /* Find the type DIE. */
13583 struct die_info *type_die = NULL;
13584 struct dwarf2_cu *type_cu = cu;
13585
cd6c91b4 13586 if (attr->form_is_ref ())
71a3c369
TT
13587 type_die = follow_die_ref (die, attr, &type_cu);
13588 if (type_die == NULL)
13589 return NULL;
13590
13591 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13592 return NULL;
13593 return die_containing_type (type_die, type_cu);
13594}
13595
13596/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13597
13598static void
13599read_variable (struct die_info *die, struct dwarf2_cu *cu)
13600{
13601 struct rust_vtable_symbol *storage = NULL;
13602
13603 if (cu->language == language_rust)
13604 {
13605 struct type *containing_type = rust_containing_type (die, cu);
13606
13607 if (containing_type != NULL)
13608 {
518817b3 13609 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13610
8c14c3a3 13611 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13612 storage->concrete_type = containing_type;
cf724bc9 13613 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13614 }
13615 }
13616
e4a62c65
TV
13617 struct symbol *res = new_symbol (die, NULL, cu, storage);
13618 struct attribute *abstract_origin
13619 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13620 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13621 if (res == NULL && loc && abstract_origin)
13622 {
13623 /* We have a variable without a name, but with a location and an abstract
13624 origin. This may be a concrete instance of an abstract variable
13625 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13626 later. */
13627 struct dwarf2_cu *origin_cu = cu;
13628 struct die_info *origin_die
13629 = follow_die_ref (die, abstract_origin, &origin_cu);
13630 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
5989a64e 13631 dpo->per_bfd->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13632 }
71a3c369
TT
13633}
13634
43988095
JK
13635/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13636 reading .debug_rnglists.
13637 Callback's type should be:
13638 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13639 Return true if the attributes are present and valid, otherwise,
13640 return false. */
13641
13642template <typename Callback>
13643static bool
13644dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13645 Callback &&callback)
13646{
ed2dc618 13647 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13648 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13649 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13650 bfd *obfd = objfile->obfd;
43988095 13651 /* Base address selection entry. */
2b24b6e4 13652 gdb::optional<CORE_ADDR> base;
43988095 13653 const gdb_byte *buffer;
43988095
JK
13654 CORE_ADDR baseaddr;
13655 bool overflow = false;
13656
43988095
JK
13657 base = cu->base_address;
13658
5989a64e
SM
13659 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13660 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13661 {
b98664d3 13662 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13663 offset);
13664 return false;
13665 }
5989a64e 13666 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13667
b3b3bada 13668 baseaddr = objfile->text_section_offset ();
43988095
JK
13669
13670 while (1)
13671 {
7814882a
JK
13672 /* Initialize it due to a false compiler warning. */
13673 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13674 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13675 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13676 unsigned int bytes_read;
13677
13678 if (buffer == buf_end)
13679 {
13680 overflow = true;
13681 break;
13682 }
13683 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13684 switch (rlet)
13685 {
13686 case DW_RLE_end_of_list:
13687 break;
13688 case DW_RLE_base_address:
13689 if (buffer + cu->header.addr_size > buf_end)
13690 {
13691 overflow = true;
13692 break;
13693 }
c8a7a66f 13694 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13695 buffer += bytes_read;
13696 break;
13697 case DW_RLE_start_length:
13698 if (buffer + cu->header.addr_size > buf_end)
13699 {
13700 overflow = true;
13701 break;
13702 }
c8a7a66f
TT
13703 range_beginning = cu->header.read_address (obfd, buffer,
13704 &bytes_read);
43988095
JK
13705 buffer += bytes_read;
13706 range_end = (range_beginning
13707 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13708 buffer += bytes_read;
13709 if (buffer > buf_end)
13710 {
13711 overflow = true;
13712 break;
13713 }
13714 break;
13715 case DW_RLE_offset_pair:
13716 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13717 buffer += bytes_read;
13718 if (buffer > buf_end)
13719 {
13720 overflow = true;
13721 break;
13722 }
13723 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13724 buffer += bytes_read;
13725 if (buffer > buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 break;
13731 case DW_RLE_start_end:
13732 if (buffer + 2 * cu->header.addr_size > buf_end)
13733 {
13734 overflow = true;
13735 break;
13736 }
c8a7a66f
TT
13737 range_beginning = cu->header.read_address (obfd, buffer,
13738 &bytes_read);
43988095 13739 buffer += bytes_read;
c8a7a66f 13740 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13741 buffer += bytes_read;
13742 break;
13743 default:
b98664d3 13744 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13745 return false;
13746 }
13747 if (rlet == DW_RLE_end_of_list || overflow)
13748 break;
13749 if (rlet == DW_RLE_base_address)
13750 continue;
13751
2b24b6e4 13752 if (!base.has_value ())
43988095
JK
13753 {
13754 /* We have no valid base address for the ranges
13755 data. */
b98664d3 13756 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13757 return false;
13758 }
13759
13760 if (range_beginning > range_end)
13761 {
13762 /* Inverted range entries are invalid. */
b98664d3 13763 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13764 return false;
13765 }
13766
13767 /* Empty range entries have no effect. */
13768 if (range_beginning == range_end)
13769 continue;
13770
2b24b6e4
TT
13771 range_beginning += *base;
13772 range_end += *base;
43988095
JK
13773
13774 /* A not-uncommon case of bad debug info.
13775 Don't pollute the addrmap with bad data. */
13776 if (range_beginning + baseaddr == 0
5989a64e 13777 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13778 {
b98664d3 13779 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13780 " [in module %s]"), objfile_name (objfile));
13781 continue;
13782 }
13783
13784 callback (range_beginning, range_end);
13785 }
13786
13787 if (overflow)
13788 {
b98664d3 13789 complaint (_("Offset %d is not terminated "
43988095
JK
13790 "for DW_AT_ranges attribute"),
13791 offset);
13792 return false;
13793 }
13794
13795 return true;
13796}
13797
13798/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13799 Callback's type should be:
13800 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13801 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13802
43988095 13803template <typename Callback>
43039443 13804static int
5f46c5a5 13805dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13806 Callback &&callback)
43039443 13807{
ed2dc618 13808 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13809 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13810 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13811 struct comp_unit_head *cu_header = &cu->header;
13812 bfd *obfd = objfile->obfd;
13813 unsigned int addr_size = cu_header->addr_size;
13814 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13815 /* Base address selection entry. */
2b24b6e4 13816 gdb::optional<CORE_ADDR> base;
43039443 13817 unsigned int dummy;
d521ce57 13818 const gdb_byte *buffer;
ff013f42 13819 CORE_ADDR baseaddr;
43039443 13820
43988095
JK
13821 if (cu_header->version >= 5)
13822 return dwarf2_rnglists_process (offset, cu, callback);
13823
d00adf39 13824 base = cu->base_address;
43039443 13825
5989a64e
SM
13826 dwarf2_per_objfile->per_bfd->ranges.read (objfile);
13827 if (offset >= dwarf2_per_objfile->per_bfd->ranges.size)
43039443 13828 {
b98664d3 13829 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13830 offset);
13831 return 0;
13832 }
5989a64e 13833 buffer = dwarf2_per_objfile->per_bfd->ranges.buffer + offset;
43039443 13834
b3b3bada 13835 baseaddr = objfile->text_section_offset ();
ff013f42 13836
43039443
JK
13837 while (1)
13838 {
13839 CORE_ADDR range_beginning, range_end;
13840
c8a7a66f 13841 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13842 buffer += addr_size;
c8a7a66f 13843 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13844 buffer += addr_size;
13845 offset += 2 * addr_size;
13846
13847 /* An end of list marker is a pair of zero addresses. */
13848 if (range_beginning == 0 && range_end == 0)
13849 /* Found the end of list entry. */
13850 break;
13851
13852 /* Each base address selection entry is a pair of 2 values.
13853 The first is the largest possible address, the second is
13854 the base address. Check for a base address here. */
13855 if ((range_beginning & mask) == mask)
13856 {
28d2bfb9
AB
13857 /* If we found the largest possible address, then we already
13858 have the base address in range_end. */
13859 base = range_end;
43039443
JK
13860 continue;
13861 }
13862
2b24b6e4 13863 if (!base.has_value ())
43039443
JK
13864 {
13865 /* We have no valid base address for the ranges
13866 data. */
b98664d3 13867 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13868 return 0;
13869 }
13870
9277c30c
UW
13871 if (range_beginning > range_end)
13872 {
13873 /* Inverted range entries are invalid. */
b98664d3 13874 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13875 return 0;
13876 }
13877
13878 /* Empty range entries have no effect. */
13879 if (range_beginning == range_end)
13880 continue;
13881
2b24b6e4
TT
13882 range_beginning += *base;
13883 range_end += *base;
43039443 13884
01093045
DE
13885 /* A not-uncommon case of bad debug info.
13886 Don't pollute the addrmap with bad data. */
13887 if (range_beginning + baseaddr == 0
5989a64e 13888 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
01093045 13889 {
b98664d3 13890 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13891 " [in module %s]"), objfile_name (objfile));
01093045
DE
13892 continue;
13893 }
13894
5f46c5a5
JK
13895 callback (range_beginning, range_end);
13896 }
13897
13898 return 1;
13899}
13900
13901/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13902 Return 1 if the attributes are present and valid, otherwise, return 0.
13903 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13904
13905static int
13906dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13907 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13908 dwarf2_psymtab *ranges_pst)
5f46c5a5 13909{
518817b3 13910 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13911 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13912 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13913 int low_set = 0;
13914 CORE_ADDR low = 0;
13915 CORE_ADDR high = 0;
13916 int retval;
13917
13918 retval = dwarf2_ranges_process (offset, cu,
13919 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13920 {
9277c30c 13921 if (ranges_pst != NULL)
3e29f34a
MR
13922 {
13923 CORE_ADDR lowpc;
13924 CORE_ADDR highpc;
13925
79748972
TT
13926 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13927 range_beginning + baseaddr)
13928 - baseaddr);
13929 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13930 range_end + baseaddr)
13931 - baseaddr);
d320c2b5
TT
13932 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13933 lowpc, highpc - 1, ranges_pst);
3e29f34a 13934 }
ff013f42 13935
43039443
JK
13936 /* FIXME: This is recording everything as a low-high
13937 segment of consecutive addresses. We should have a
13938 data structure for discontiguous block ranges
13939 instead. */
13940 if (! low_set)
13941 {
13942 low = range_beginning;
13943 high = range_end;
13944 low_set = 1;
13945 }
13946 else
13947 {
13948 if (range_beginning < low)
13949 low = range_beginning;
13950 if (range_end > high)
13951 high = range_end;
13952 }
5f46c5a5
JK
13953 });
13954 if (!retval)
13955 return 0;
43039443
JK
13956
13957 if (! low_set)
13958 /* If the first entry is an end-of-list marker, the range
13959 describes an empty scope, i.e. no instructions. */
13960 return 0;
13961
13962 if (low_return)
13963 *low_return = low;
13964 if (high_return)
13965 *high_return = high;
13966 return 1;
13967}
13968
3a2b436a
JK
13969/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13970 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13971 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13972
3a2b436a 13973static enum pc_bounds_kind
af34e669 13974dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13975 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13976 dwarf2_psymtab *pst)
c906108c 13977{
518817b3
SM
13978 struct dwarf2_per_objfile *dwarf2_per_objfile
13979 = cu->per_cu->dwarf2_per_objfile;
c906108c 13980 struct attribute *attr;
91da1414 13981 struct attribute *attr_high;
af34e669
DJ
13982 CORE_ADDR low = 0;
13983 CORE_ADDR high = 0;
e385593e 13984 enum pc_bounds_kind ret;
c906108c 13985
91da1414
MW
13986 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13987 if (attr_high)
af34e669 13988 {
e142c38c 13989 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13990 if (attr != nullptr)
91da1414 13991 {
cd6c91b4
TT
13992 low = attr->value_as_address ();
13993 high = attr_high->value_as_address ();
13994 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13995 high += low;
91da1414 13996 }
af34e669
DJ
13997 else
13998 /* Found high w/o low attribute. */
e385593e 13999 return PC_BOUNDS_INVALID;
af34e669
DJ
14000
14001 /* Found consecutive range of addresses. */
3a2b436a 14002 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14003 }
c906108c 14004 else
af34e669 14005 {
e142c38c 14006 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14007 if (attr != NULL)
14008 {
18a8505e 14009 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14010 We take advantage of the fact that DW_AT_ranges does not appear
14011 in DW_TAG_compile_unit of DWO files. */
14012 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14013 unsigned int ranges_offset = (DW_UNSND (attr)
14014 + (need_ranges_base
14015 ? cu->ranges_base
14016 : 0));
2e3cf129 14017
af34e669 14018 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14019 .debug_ranges section. */
2e3cf129 14020 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14021 return PC_BOUNDS_INVALID;
43039443 14022 /* Found discontinuous range of addresses. */
3a2b436a 14023 ret = PC_BOUNDS_RANGES;
af34e669 14024 }
e385593e
JK
14025 else
14026 return PC_BOUNDS_NOT_PRESENT;
af34e669 14027 }
c906108c 14028
48fbe735 14029 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14030 if (high <= low)
e385593e 14031 return PC_BOUNDS_INVALID;
c906108c
SS
14032
14033 /* When using the GNU linker, .gnu.linkonce. sections are used to
14034 eliminate duplicate copies of functions and vtables and such.
14035 The linker will arbitrarily choose one and discard the others.
14036 The AT_*_pc values for such functions refer to local labels in
14037 these sections. If the section from that file was discarded, the
14038 labels are not in the output, so the relocs get a value of 0.
14039 If this is a discarded function, mark the pc bounds as invalid,
14040 so that GDB will ignore it. */
5989a64e 14041 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14042 return PC_BOUNDS_INVALID;
c906108c
SS
14043
14044 *lowpc = low;
96408a79
SA
14045 if (highpc)
14046 *highpc = high;
af34e669 14047 return ret;
c906108c
SS
14048}
14049
b084d499
JB
14050/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14051 its low and high PC addresses. Do nothing if these addresses could not
14052 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14053 and HIGHPC to the high address if greater than HIGHPC. */
14054
14055static void
14056dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14057 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14058 struct dwarf2_cu *cu)
14059{
14060 CORE_ADDR low, high;
14061 struct die_info *child = die->child;
14062
e385593e 14063 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14064 {
325fac50
PA
14065 *lowpc = std::min (*lowpc, low);
14066 *highpc = std::max (*highpc, high);
b084d499
JB
14067 }
14068
14069 /* If the language does not allow nested subprograms (either inside
14070 subprograms or lexical blocks), we're done. */
14071 if (cu->language != language_ada)
14072 return;
6e70227d 14073
b084d499
JB
14074 /* Check all the children of the given DIE. If it contains nested
14075 subprograms, then check their pc bounds. Likewise, we need to
14076 check lexical blocks as well, as they may also contain subprogram
14077 definitions. */
14078 while (child && child->tag)
14079 {
14080 if (child->tag == DW_TAG_subprogram
14081 || child->tag == DW_TAG_lexical_block)
14082 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14083 child = child->sibling;
b084d499
JB
14084 }
14085}
14086
fae299cd
DC
14087/* Get the low and high pc's represented by the scope DIE, and store
14088 them in *LOWPC and *HIGHPC. If the correct values can't be
14089 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14090
14091static void
14092get_scope_pc_bounds (struct die_info *die,
14093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14094 struct dwarf2_cu *cu)
14095{
14096 CORE_ADDR best_low = (CORE_ADDR) -1;
14097 CORE_ADDR best_high = (CORE_ADDR) 0;
14098 CORE_ADDR current_low, current_high;
14099
3a2b436a 14100 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14101 >= PC_BOUNDS_RANGES)
fae299cd
DC
14102 {
14103 best_low = current_low;
14104 best_high = current_high;
14105 }
14106 else
14107 {
14108 struct die_info *child = die->child;
14109
14110 while (child && child->tag)
14111 {
14112 switch (child->tag) {
14113 case DW_TAG_subprogram:
b084d499 14114 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14115 break;
14116 case DW_TAG_namespace:
f55ee35c 14117 case DW_TAG_module:
fae299cd
DC
14118 /* FIXME: carlton/2004-01-16: Should we do this for
14119 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14120 that current GCC's always emit the DIEs corresponding
14121 to definitions of methods of classes as children of a
14122 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14123 the DIEs giving the declarations, which could be
14124 anywhere). But I don't see any reason why the
14125 standards says that they have to be there. */
14126 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14127
14128 if (current_low != ((CORE_ADDR) -1))
14129 {
325fac50
PA
14130 best_low = std::min (best_low, current_low);
14131 best_high = std::max (best_high, current_high);
fae299cd
DC
14132 }
14133 break;
14134 default:
0963b4bd 14135 /* Ignore. */
fae299cd
DC
14136 break;
14137 }
14138
436c571c 14139 child = child->sibling;
fae299cd
DC
14140 }
14141 }
14142
14143 *lowpc = best_low;
14144 *highpc = best_high;
14145}
14146
801e3a5b
JB
14147/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14148 in DIE. */
380bca97 14149
801e3a5b
JB
14150static void
14151dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14152 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14153{
518817b3 14154 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14155 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14156 struct attribute *attr;
91da1414 14157 struct attribute *attr_high;
801e3a5b 14158
91da1414
MW
14159 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14160 if (attr_high)
801e3a5b 14161 {
801e3a5b 14162 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14163 if (attr != nullptr)
801e3a5b 14164 {
cd6c91b4
TT
14165 CORE_ADDR low = attr->value_as_address ();
14166 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14167
cd6c91b4 14168 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14169 high += low;
9a619af0 14170
3e29f34a
MR
14171 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14172 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14173 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14174 }
14175 }
14176
14177 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14178 if (attr != nullptr)
801e3a5b 14179 {
18a8505e 14180 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14181 We take advantage of the fact that DW_AT_ranges does not appear
14182 in DW_TAG_compile_unit of DWO files. */
14183 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14184
14185 /* The value of the DW_AT_ranges attribute is the offset of the
14186 address range list in the .debug_ranges section. */
ab435259
DE
14187 unsigned long offset = (DW_UNSND (attr)
14188 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14189
2d5f09ec 14190 std::vector<blockrange> blockvec;
5f46c5a5
JK
14191 dwarf2_ranges_process (offset, cu,
14192 [&] (CORE_ADDR start, CORE_ADDR end)
14193 {
58fdfd2c
JK
14194 start += baseaddr;
14195 end += baseaddr;
5f46c5a5
JK
14196 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14197 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14198 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14199 blockvec.emplace_back (start, end);
5f46c5a5 14200 });
2d5f09ec
KB
14201
14202 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14203 }
14204}
14205
685b1105
JK
14206/* Check whether the producer field indicates either of GCC < 4.6, or the
14207 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14208
685b1105
JK
14209static void
14210check_producer (struct dwarf2_cu *cu)
60d5a603 14211{
38360086 14212 int major, minor;
60d5a603
JK
14213
14214 if (cu->producer == NULL)
14215 {
14216 /* For unknown compilers expect their behavior is DWARF version
14217 compliant.
14218
14219 GCC started to support .debug_types sections by -gdwarf-4 since
14220 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14221 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14222 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14223 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14224 }
b1ffba5a 14225 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14226 {
38360086
MW
14227 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14228 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14229 }
5230b05a 14230 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14231 {
14232 cu->producer_is_icc = true;
14233 cu->producer_is_icc_lt_14 = major < 14;
14234 }
c258c396
JD
14235 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14236 cu->producer_is_codewarrior = true;
685b1105
JK
14237 else
14238 {
14239 /* For other non-GCC compilers, expect their behavior is DWARF version
14240 compliant. */
60d5a603
JK
14241 }
14242
9068261f 14243 cu->checked_producer = true;
685b1105 14244}
ba919b58 14245
685b1105
JK
14246/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14247 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14248 during 4.6.0 experimental. */
14249
9068261f 14250static bool
685b1105
JK
14251producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14252{
14253 if (!cu->checked_producer)
14254 check_producer (cu);
14255
14256 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14257}
14258
c258c396
JD
14259
14260/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14261 with incorrect is_stmt attributes. */
14262
14263static bool
14264producer_is_codewarrior (struct dwarf2_cu *cu)
14265{
14266 if (!cu->checked_producer)
14267 check_producer (cu);
14268
14269 return cu->producer_is_codewarrior;
14270}
14271
405feb71 14272/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14273 DW_AT_accessibility. */
14274
14275static enum dwarf_access_attribute
14276dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14277{
14278 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14279 {
14280 /* The default DWARF 2 accessibility for members is public, the default
14281 accessibility for inheritance is private. */
14282
14283 if (die->tag != DW_TAG_inheritance)
14284 return DW_ACCESS_public;
14285 else
14286 return DW_ACCESS_private;
14287 }
14288 else
14289 {
14290 /* DWARF 3+ defines the default accessibility a different way. The same
14291 rules apply now for DW_TAG_inheritance as for the members and it only
14292 depends on the container kind. */
14293
14294 if (die->parent->tag == DW_TAG_class_type)
14295 return DW_ACCESS_private;
14296 else
14297 return DW_ACCESS_public;
14298 }
14299}
14300
74ac6d43
TT
14301/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14302 offset. If the attribute was not found return 0, otherwise return
14303 1. If it was found but could not properly be handled, set *OFFSET
14304 to 0. */
14305
14306static int
14307handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14308 LONGEST *offset)
14309{
14310 struct attribute *attr;
14311
14312 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14313 if (attr != NULL)
14314 {
14315 *offset = 0;
14316
14317 /* Note that we do not check for a section offset first here.
14318 This is because DW_AT_data_member_location is new in DWARF 4,
14319 so if we see it, we can assume that a constant form is really
14320 a constant and not a section offset. */
cd6c91b4 14321 if (attr->form_is_constant ())
0826b30a 14322 *offset = attr->constant_value (0);
cd6c91b4 14323 else if (attr->form_is_section_offset ())
74ac6d43 14324 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14325 else if (attr->form_is_block ())
74ac6d43
TT
14326 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14327 else
14328 dwarf2_complex_location_expr_complaint ();
14329
14330 return 1;
14331 }
14332
14333 return 0;
14334}
14335
7d79de9a
TT
14336/* Look for DW_AT_data_member_location and store the results in FIELD. */
14337
14338static void
14339handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14340 struct field *field)
14341{
14342 struct attribute *attr;
14343
14344 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14345 if (attr != NULL)
14346 {
14347 if (attr->form_is_constant ())
14348 {
14349 LONGEST offset = attr->constant_value (0);
14350 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14351 }
14352 else if (attr->form_is_section_offset ())
14353 dwarf2_complex_location_expr_complaint ();
14354 else if (attr->form_is_block ())
14355 {
14356 bool handled;
14357 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14358 if (handled)
14359 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14360 else
14361 {
14362 struct objfile *objfile
14363 = cu->per_cu->dwarf2_per_objfile->objfile;
14364 struct dwarf2_locexpr_baton *dlbaton
14365 = XOBNEW (&objfile->objfile_obstack,
14366 struct dwarf2_locexpr_baton);
14367 dlbaton->data = DW_BLOCK (attr)->data;
14368 dlbaton->size = DW_BLOCK (attr)->size;
14369 /* When using this baton, we want to compute the address
14370 of the field, not the value. This is why
14371 is_reference is set to false here. */
14372 dlbaton->is_reference = false;
a50264ba 14373 dlbaton->per_objfile = cu->per_cu->dwarf2_per_objfile;
7d79de9a
TT
14374 dlbaton->per_cu = cu->per_cu;
14375
14376 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14377 }
14378 }
14379 else
14380 dwarf2_complex_location_expr_complaint ();
14381 }
14382}
14383
c906108c
SS
14384/* Add an aggregate field to the field list. */
14385
14386static void
107d2387 14387dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14388 struct dwarf2_cu *cu)
6e70227d 14389{
518817b3 14390 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14391 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14392 struct nextfield *new_field;
14393 struct attribute *attr;
14394 struct field *fp;
15d034d0 14395 const char *fieldname = "";
c906108c 14396
7d0ccb61
DJ
14397 if (die->tag == DW_TAG_inheritance)
14398 {
be2daae6
TT
14399 fip->baseclasses.emplace_back ();
14400 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14401 }
14402 else
14403 {
be2daae6
TT
14404 fip->fields.emplace_back ();
14405 new_field = &fip->fields.back ();
7d0ccb61 14406 }
be2daae6 14407
9c6a1327
TT
14408 new_field->offset = die->sect_off;
14409
e142c38c 14410 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14411 if (attr != nullptr)
c906108c 14412 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14413 else
14414 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14415 if (new_field->accessibility != DW_ACCESS_public)
14416 fip->non_public_fields = 1;
60d5a603 14417
e142c38c 14418 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14419 if (attr != nullptr)
c906108c 14420 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14421 else
14422 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14423
14424 fp = &new_field->field;
a9a9bd0f 14425
e142c38c 14426 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14427 {
a9a9bd0f 14428 /* Data member other than a C++ static data member. */
6e70227d 14429
c906108c 14430 /* Get type of field. */
e7c27a73 14431 fp->type = die_type (die, cu);
c906108c 14432
d6a843b5 14433 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14434
c906108c 14435 /* Get bit size of field (zero if none). */
e142c38c 14436 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14437 if (attr != nullptr)
c906108c
SS
14438 {
14439 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14440 }
14441 else
14442 {
14443 FIELD_BITSIZE (*fp) = 0;
14444 }
14445
14446 /* Get bit offset of field. */
7d79de9a 14447 handle_data_member_location (die, cu, fp);
e142c38c 14448 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14449 if (attr != nullptr)
c906108c 14450 {
d5a22e77 14451 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14452 {
14453 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14454 additional bit offset from the MSB of the containing
14455 anonymous object to the MSB of the field. We don't
14456 have to do anything special since we don't need to
14457 know the size of the anonymous object. */
f41f5e61 14458 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14459 }
14460 else
14461 {
14462 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14463 MSB of the anonymous object, subtract off the number of
14464 bits from the MSB of the field to the MSB of the
14465 object, and then subtract off the number of bits of
14466 the field itself. The result is the bit offset of
14467 the LSB of the field. */
c906108c
SS
14468 int anonymous_size;
14469 int bit_offset = DW_UNSND (attr);
14470
e142c38c 14471 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14472 if (attr != nullptr)
c906108c
SS
14473 {
14474 /* The size of the anonymous object containing
14475 the bit field is explicit, so use the
14476 indicated size (in bytes). */
14477 anonymous_size = DW_UNSND (attr);
14478 }
14479 else
14480 {
14481 /* The size of the anonymous object containing
14482 the bit field must be inferred from the type
14483 attribute of the data member containing the
14484 bit field. */
14485 anonymous_size = TYPE_LENGTH (fp->type);
14486 }
f41f5e61
PA
14487 SET_FIELD_BITPOS (*fp,
14488 (FIELD_BITPOS (*fp)
14489 + anonymous_size * bits_per_byte
14490 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14491 }
14492 }
da5b30da
AA
14493 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14494 if (attr != NULL)
14495 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14496 + attr->constant_value (0)));
c906108c
SS
14497
14498 /* Get name of field. */
39cbfefa
DJ
14499 fieldname = dwarf2_name (die, cu);
14500 if (fieldname == NULL)
14501 fieldname = "";
d8151005
DJ
14502
14503 /* The name is already allocated along with this objfile, so we don't
14504 need to duplicate it for the type. */
14505 fp->name = fieldname;
c906108c
SS
14506
14507 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14508 pointer or virtual base class pointer) to private. */
e142c38c 14509 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14510 {
d48cc9dd 14511 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14512 new_field->accessibility = DW_ACCESS_private;
14513 fip->non_public_fields = 1;
14514 }
14515 }
a9a9bd0f 14516 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14517 {
a9a9bd0f
DC
14518 /* C++ static member. */
14519
14520 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14521 is a declaration, but all versions of G++ as of this writing
14522 (so through at least 3.2.1) incorrectly generate
14523 DW_TAG_variable tags. */
6e70227d 14524
ff355380 14525 const char *physname;
c906108c 14526
a9a9bd0f 14527 /* Get name of field. */
39cbfefa
DJ
14528 fieldname = dwarf2_name (die, cu);
14529 if (fieldname == NULL)
c906108c
SS
14530 return;
14531
254e6b9e 14532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14533 if (attr
14534 /* Only create a symbol if this is an external value.
14535 new_symbol checks this and puts the value in the global symbol
14536 table, which we want. If it is not external, new_symbol
14537 will try to put the value in cu->list_in_scope which is wrong. */
14538 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14539 {
14540 /* A static const member, not much different than an enum as far as
14541 we're concerned, except that we can support more types. */
14542 new_symbol (die, NULL, cu);
14543 }
14544
2df3850c 14545 /* Get physical name. */
ff355380 14546 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14547
d8151005
DJ
14548 /* The name is already allocated along with this objfile, so we don't
14549 need to duplicate it for the type. */
14550 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14551 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14552 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14553 }
14554 else if (die->tag == DW_TAG_inheritance)
14555 {
74ac6d43 14556 /* C++ base class field. */
7d79de9a 14557 handle_data_member_location (die, cu, fp);
c906108c 14558 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14559 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14560 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14561 }
2ddeaf8a
TT
14562 else
14563 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14564}
14565
883fd55a
KS
14566/* Can the type given by DIE define another type? */
14567
14568static bool
14569type_can_define_types (const struct die_info *die)
14570{
14571 switch (die->tag)
14572 {
14573 case DW_TAG_typedef:
14574 case DW_TAG_class_type:
14575 case DW_TAG_structure_type:
14576 case DW_TAG_union_type:
14577 case DW_TAG_enumeration_type:
14578 return true;
14579
14580 default:
14581 return false;
14582 }
14583}
14584
14585/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14586
14587static void
883fd55a
KS
14588dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14589 struct dwarf2_cu *cu)
6e70227d 14590{
be2daae6
TT
14591 struct decl_field fp;
14592 memset (&fp, 0, sizeof (fp));
98751a41 14593
883fd55a 14594 gdb_assert (type_can_define_types (die));
98751a41 14595
883fd55a 14596 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14597 fp.name = dwarf2_name (die, cu);
14598 fp.type = read_type_die (die, cu);
98751a41 14599
c191a687
KS
14600 /* Save accessibility. */
14601 enum dwarf_access_attribute accessibility;
14602 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14603 if (attr != NULL)
14604 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14605 else
14606 accessibility = dwarf2_default_access_attribute (die, cu);
14607 switch (accessibility)
14608 {
14609 case DW_ACCESS_public:
14610 /* The assumed value if neither private nor protected. */
14611 break;
14612 case DW_ACCESS_private:
be2daae6 14613 fp.is_private = 1;
c191a687
KS
14614 break;
14615 case DW_ACCESS_protected:
be2daae6 14616 fp.is_protected = 1;
c191a687
KS
14617 break;
14618 default:
b98664d3 14619 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14620 }
14621
883fd55a 14622 if (die->tag == DW_TAG_typedef)
be2daae6 14623 fip->typedef_field_list.push_back (fp);
883fd55a 14624 else
be2daae6 14625 fip->nested_types_list.push_back (fp);
98751a41
JK
14626}
14627
9c6a1327
TT
14628/* A convenience typedef that's used when finding the discriminant
14629 field for a variant part. */
1b95cdb7
SM
14630typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14631 offset_map_type;
9c6a1327
TT
14632
14633/* Compute the discriminant range for a given variant. OBSTACK is
14634 where the results will be stored. VARIANT is the variant to
14635 process. IS_UNSIGNED indicates whether the discriminant is signed
14636 or unsigned. */
14637
14638static const gdb::array_view<discriminant_range>
14639convert_variant_range (struct obstack *obstack, const variant_field &variant,
14640 bool is_unsigned)
14641{
14642 std::vector<discriminant_range> ranges;
14643
14644 if (variant.default_branch)
14645 return {};
14646
14647 if (variant.discr_list_data == nullptr)
14648 {
14649 discriminant_range r
14650 = {variant.discriminant_value, variant.discriminant_value};
14651 ranges.push_back (r);
14652 }
14653 else
14654 {
14655 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14656 variant.discr_list_data->size);
14657 while (!data.empty ())
14658 {
14659 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14660 {
14661 complaint (_("invalid discriminant marker: %d"), data[0]);
14662 break;
14663 }
14664 bool is_range = data[0] == DW_DSC_range;
14665 data = data.slice (1);
14666
14667 ULONGEST low, high;
14668 unsigned int bytes_read;
14669
14670 if (data.empty ())
14671 {
14672 complaint (_("DW_AT_discr_list missing low value"));
14673 break;
14674 }
14675 if (is_unsigned)
14676 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14677 else
14678 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14679 &bytes_read);
14680 data = data.slice (bytes_read);
14681
14682 if (is_range)
14683 {
14684 if (data.empty ())
14685 {
14686 complaint (_("DW_AT_discr_list missing high value"));
14687 break;
14688 }
14689 if (is_unsigned)
14690 high = read_unsigned_leb128 (nullptr, data.data (),
14691 &bytes_read);
14692 else
14693 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14694 &bytes_read);
14695 data = data.slice (bytes_read);
14696 }
14697 else
14698 high = low;
14699
14700 ranges.push_back ({ low, high });
14701 }
14702 }
14703
14704 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14705 ranges.size ());
14706 std::copy (ranges.begin (), ranges.end (), result);
14707 return gdb::array_view<discriminant_range> (result, ranges.size ());
14708}
14709
14710static const gdb::array_view<variant_part> create_variant_parts
14711 (struct obstack *obstack,
14712 const offset_map_type &offset_map,
14713 struct field_info *fi,
14714 const std::vector<variant_part_builder> &variant_parts);
14715
14716/* Fill in a "struct variant" for a given variant field. RESULT is
14717 the variant to fill in. OBSTACK is where any needed allocations
14718 will be done. OFFSET_MAP holds the mapping from section offsets to
14719 fields for the type. FI describes the fields of the type we're
14720 processing. FIELD is the variant field we're converting. */
14721
14722static void
14723create_one_variant (variant &result, struct obstack *obstack,
14724 const offset_map_type &offset_map,
14725 struct field_info *fi, const variant_field &field)
14726{
14727 result.discriminants = convert_variant_range (obstack, field, false);
14728 result.first_field = field.first_field + fi->baseclasses.size ();
14729 result.last_field = field.last_field + fi->baseclasses.size ();
14730 result.parts = create_variant_parts (obstack, offset_map, fi,
14731 field.variant_parts);
14732}
14733
14734/* Fill in a "struct variant_part" for a given variant part. RESULT
14735 is the variant part to fill in. OBSTACK is where any needed
14736 allocations will be done. OFFSET_MAP holds the mapping from
14737 section offsets to fields for the type. FI describes the fields of
14738 the type we're processing. BUILDER is the variant part to be
14739 converted. */
14740
14741static void
14742create_one_variant_part (variant_part &result,
14743 struct obstack *obstack,
14744 const offset_map_type &offset_map,
14745 struct field_info *fi,
14746 const variant_part_builder &builder)
14747{
14748 auto iter = offset_map.find (builder.discriminant_offset);
14749 if (iter == offset_map.end ())
14750 {
14751 result.discriminant_index = -1;
14752 /* Doesn't matter. */
14753 result.is_unsigned = false;
14754 }
14755 else
14756 {
14757 result.discriminant_index = iter->second;
14758 result.is_unsigned
14759 = TYPE_UNSIGNED (FIELD_TYPE
14760 (fi->fields[result.discriminant_index].field));
14761 }
14762
14763 size_t n = builder.variants.size ();
14764 variant *output = new (obstack) variant[n];
14765 for (size_t i = 0; i < n; ++i)
14766 create_one_variant (output[i], obstack, offset_map, fi,
14767 builder.variants[i]);
14768
14769 result.variants = gdb::array_view<variant> (output, n);
14770}
14771
14772/* Create a vector of variant parts that can be attached to a type.
14773 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14774 holds the mapping from section offsets to fields for the type. FI
14775 describes the fields of the type we're processing. VARIANT_PARTS
14776 is the vector to convert. */
14777
14778static const gdb::array_view<variant_part>
14779create_variant_parts (struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const std::vector<variant_part_builder> &variant_parts)
14783{
14784 if (variant_parts.empty ())
14785 return {};
14786
14787 size_t n = variant_parts.size ();
14788 variant_part *result = new (obstack) variant_part[n];
14789 for (size_t i = 0; i < n; ++i)
14790 create_one_variant_part (result[i], obstack, offset_map, fi,
14791 variant_parts[i]);
14792
14793 return gdb::array_view<variant_part> (result, n);
14794}
14795
14796/* Compute the variant part vector for FIP, attaching it to TYPE when
14797 done. */
14798
14799static void
14800add_variant_property (struct field_info *fip, struct type *type,
14801 struct dwarf2_cu *cu)
14802{
14803 /* Map section offsets of fields to their field index. Note the
14804 field index here does not take the number of baseclasses into
14805 account. */
14806 offset_map_type offset_map;
14807 for (int i = 0; i < fip->fields.size (); ++i)
14808 offset_map[fip->fields[i].offset] = i;
14809
14810 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14811 gdb::array_view<variant_part> parts
14812 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14813 fip->variant_parts);
14814
14815 struct dynamic_prop prop;
14816 prop.kind = PROP_VARIANT_PARTS;
14817 prop.data.variant_parts
14818 = ((gdb::array_view<variant_part> *)
14819 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14820
5c54719c 14821 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14822}
14823
c906108c
SS
14824/* Create the vector of fields, and attach it to the type. */
14825
14826static void
fba45db2 14827dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14828 struct dwarf2_cu *cu)
c906108c 14829{
317f7127 14830 int nfields = fip->nfields ();
c906108c
SS
14831
14832 /* Record the field count, allocate space for the array of fields,
14833 and create blank accessibility bitfields if necessary. */
5e33d5f4 14834 type->set_num_fields (nfields);
3cabb6b0
SM
14835 type->set_fields
14836 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14837
b4ba55a1 14838 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14839 {
14840 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14841
14842 TYPE_FIELD_PRIVATE_BITS (type) =
14843 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14844 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14845
14846 TYPE_FIELD_PROTECTED_BITS (type) =
14847 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14848 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14849
774b6a14
TT
14850 TYPE_FIELD_IGNORE_BITS (type) =
14851 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14852 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14853 }
14854
14855 /* If the type has baseclasses, allocate and clear a bit vector for
14856 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14857 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14858 {
be2daae6 14859 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14860 unsigned char *pointer;
c906108c
SS
14861
14862 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14863 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14864 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14865 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14866 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14867 }
14868
9c6a1327
TT
14869 if (!fip->variant_parts.empty ())
14870 add_variant_property (fip, type, cu);
2ddeaf8a 14871
be2daae6
TT
14872 /* Copy the saved-up fields into the field vector. */
14873 for (int i = 0; i < nfields; ++i)
c906108c 14874 {
be2daae6
TT
14875 struct nextfield &field
14876 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14877 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14878
ceacbf6e 14879 type->field (i) = field.field;
be2daae6 14880 switch (field.accessibility)
c906108c 14881 {
c5aa993b 14882 case DW_ACCESS_private:
b4ba55a1 14883 if (cu->language != language_ada)
be2daae6 14884 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14885 break;
c906108c 14886
c5aa993b 14887 case DW_ACCESS_protected:
b4ba55a1 14888 if (cu->language != language_ada)
be2daae6 14889 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14890 break;
c906108c 14891
c5aa993b
JM
14892 case DW_ACCESS_public:
14893 break;
c906108c 14894
c5aa993b
JM
14895 default:
14896 /* Unknown accessibility. Complain and treat it as public. */
14897 {
b98664d3 14898 complaint (_("unsupported accessibility %d"),
be2daae6 14899 field.accessibility);
c5aa993b
JM
14900 }
14901 break;
c906108c 14902 }
be2daae6 14903 if (i < fip->baseclasses.size ())
c906108c 14904 {
be2daae6 14905 switch (field.virtuality)
c906108c 14906 {
c5aa993b
JM
14907 case DW_VIRTUALITY_virtual:
14908 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14909 if (cu->language == language_ada)
a73c6dcd 14910 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14911 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14912 break;
c906108c
SS
14913 }
14914 }
c906108c
SS
14915 }
14916}
14917
7d27a96d
TT
14918/* Return true if this member function is a constructor, false
14919 otherwise. */
14920
14921static int
14922dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14923{
14924 const char *fieldname;
fe978cb0 14925 const char *type_name;
7d27a96d
TT
14926 int len;
14927
14928 if (die->parent == NULL)
14929 return 0;
14930
14931 if (die->parent->tag != DW_TAG_structure_type
14932 && die->parent->tag != DW_TAG_union_type
14933 && die->parent->tag != DW_TAG_class_type)
14934 return 0;
14935
14936 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14937 type_name = dwarf2_name (die->parent, cu);
14938 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14939 return 0;
14940
14941 len = strlen (fieldname);
fe978cb0
PA
14942 return (strncmp (fieldname, type_name, len) == 0
14943 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14944}
14945
e35000a7
TBA
14946/* Check if the given VALUE is a recognized enum
14947 dwarf_defaulted_attribute constant according to DWARF5 spec,
14948 Table 7.24. */
14949
14950static bool
14951is_valid_DW_AT_defaulted (ULONGEST value)
14952{
14953 switch (value)
14954 {
14955 case DW_DEFAULTED_no:
14956 case DW_DEFAULTED_in_class:
14957 case DW_DEFAULTED_out_of_class:
14958 return true;
14959 }
14960
3142e908 14961 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14962 return false;
14963}
14964
c906108c
SS
14965/* Add a member function to the proper fieldlist. */
14966
14967static void
107d2387 14968dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14969 struct type *type, struct dwarf2_cu *cu)
c906108c 14970{
518817b3 14971 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14972 struct attribute *attr;
c906108c 14973 int i;
be2daae6 14974 struct fnfieldlist *flp = nullptr;
c906108c 14975 struct fn_field *fnp;
15d034d0 14976 const char *fieldname;
f792889a 14977 struct type *this_type;
60d5a603 14978 enum dwarf_access_attribute accessibility;
c906108c 14979
b4ba55a1 14980 if (cu->language == language_ada)
a73c6dcd 14981 error (_("unexpected member function in Ada type"));
b4ba55a1 14982
2df3850c 14983 /* Get name of member function. */
39cbfefa
DJ
14984 fieldname = dwarf2_name (die, cu);
14985 if (fieldname == NULL)
2df3850c 14986 return;
c906108c 14987
c906108c 14988 /* Look up member function name in fieldlist. */
be2daae6 14989 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14990 {
27bfe10e 14991 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14992 {
14993 flp = &fip->fnfieldlists[i];
14994 break;
14995 }
c906108c
SS
14996 }
14997
be2daae6
TT
14998 /* Create a new fnfieldlist if necessary. */
14999 if (flp == nullptr)
c906108c 15000 {
be2daae6
TT
15001 fip->fnfieldlists.emplace_back ();
15002 flp = &fip->fnfieldlists.back ();
c906108c 15003 flp->name = fieldname;
be2daae6 15004 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15005 }
15006
be2daae6
TT
15007 /* Create a new member function field and add it to the vector of
15008 fnfieldlists. */
15009 flp->fnfields.emplace_back ();
15010 fnp = &flp->fnfields.back ();
3da10d80
KS
15011
15012 /* Delay processing of the physname until later. */
9c37b5ae 15013 if (cu->language == language_cplus)
be2daae6
TT
15014 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15015 die, cu);
3da10d80
KS
15016 else
15017 {
1d06ead6 15018 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15019 fnp->physname = physname ? physname : "";
15020 }
15021
c906108c 15022 fnp->type = alloc_type (objfile);
f792889a 15023 this_type = read_type_die (die, cu);
78134374 15024 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15025 {
1f704f76 15026 int nparams = this_type->num_fields ();
c906108c 15027
f792889a 15028 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15029 of the method itself (TYPE_CODE_METHOD). */
15030 smash_to_method_type (fnp->type, type,
f792889a 15031 TYPE_TARGET_TYPE (this_type),
80fc5e77 15032 this_type->fields (),
1f704f76 15033 this_type->num_fields (),
f792889a 15034 TYPE_VARARGS (this_type));
c906108c
SS
15035
15036 /* Handle static member functions.
c5aa993b 15037 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15038 member functions. G++ helps GDB by marking the first
15039 parameter for non-static member functions (which is the this
15040 pointer) as artificial. We obtain this information from
15041 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15042 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15043 fnp->voffset = VOFFSET_STATIC;
15044 }
15045 else
b98664d3 15046 complaint (_("member function type missing for '%s'"),
3da10d80 15047 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15048
15049 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15050 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15051 fnp->fcontext = die_containing_type (die, cu);
c906108c 15052
3e43a32a
MS
15053 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15054 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15055
15056 /* Get accessibility. */
e142c38c 15057 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15058 if (attr != nullptr)
aead7601 15059 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15060 else
15061 accessibility = dwarf2_default_access_attribute (die, cu);
15062 switch (accessibility)
c906108c 15063 {
60d5a603
JK
15064 case DW_ACCESS_private:
15065 fnp->is_private = 1;
15066 break;
15067 case DW_ACCESS_protected:
15068 fnp->is_protected = 1;
15069 break;
c906108c
SS
15070 }
15071
b02dede2 15072 /* Check for artificial methods. */
e142c38c 15073 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15074 if (attr && DW_UNSND (attr) != 0)
15075 fnp->is_artificial = 1;
15076
e35000a7
TBA
15077 /* Check for defaulted methods. */
15078 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15079 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15080 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15081
15082 /* Check for deleted methods. */
15083 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15084 if (attr != nullptr && DW_UNSND (attr) != 0)
15085 fnp->is_deleted = 1;
15086
7d27a96d
TT
15087 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15088
0d564a31 15089 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15090 function. For older versions of GCC, this is an offset in the
15091 appropriate virtual table, as specified by DW_AT_containing_type.
15092 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15093 to the object address. */
15094
e142c38c 15095 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15096 if (attr != nullptr)
8e19ed76 15097 {
4fc6c0d5 15098 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15099 {
aec5aa8b
TT
15100 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15101 {
15102 /* Old-style GCC. */
15103 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15104 }
15105 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15106 || (DW_BLOCK (attr)->size > 1
15107 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15108 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15109 {
aec5aa8b
TT
15110 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15111 if ((fnp->voffset % cu->header.addr_size) != 0)
15112 dwarf2_complex_location_expr_complaint ();
15113 else
15114 fnp->voffset /= cu->header.addr_size;
15115 fnp->voffset += 2;
15116 }
15117 else
15118 dwarf2_complex_location_expr_complaint ();
15119
15120 if (!fnp->fcontext)
7e993ebf
KS
15121 {
15122 /* If there is no `this' field and no DW_AT_containing_type,
15123 we cannot actually find a base class context for the
15124 vtable! */
1f704f76 15125 if (this_type->num_fields () == 0
7e993ebf
KS
15126 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15127 {
b98664d3 15128 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15129 "function \"%s\" (offset %s)"),
15130 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15131 }
15132 else
15133 {
15134 fnp->fcontext
15135 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15136 }
15137 }
aec5aa8b 15138 }
cd6c91b4 15139 else if (attr->form_is_section_offset ())
8e19ed76 15140 {
4d3c2250 15141 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15142 }
15143 else
15144 {
4d3c2250
KB
15145 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15146 fieldname);
8e19ed76 15147 }
0d564a31 15148 }
d48cc9dd
DJ
15149 else
15150 {
15151 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15152 if (attr && DW_UNSND (attr))
15153 {
15154 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15155 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15156 "but the vtable offset is not specified"),
9d8780f0 15157 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15158 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15159 TYPE_CPLUS_DYNAMIC (type) = 1;
15160 }
15161 }
c906108c
SS
15162}
15163
15164/* Create the vector of member function fields, and attach it to the type. */
15165
15166static void
fba45db2 15167dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15168 struct dwarf2_cu *cu)
c906108c 15169{
b4ba55a1 15170 if (cu->language == language_ada)
a73c6dcd 15171 error (_("unexpected member functions in Ada type"));
b4ba55a1 15172
c906108c
SS
15173 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15174 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15175 TYPE_ALLOC (type,
15176 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15177
be2daae6 15178 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15179 {
be2daae6 15180 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15181 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15182
be2daae6
TT
15183 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15184 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15185 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15186 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15187
15188 for (int k = 0; k < nf.fnfields.size (); ++k)
15189 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15190 }
15191
be2daae6 15192 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15193}
15194
1168df01
JB
15195/* Returns non-zero if NAME is the name of a vtable member in CU's
15196 language, zero otherwise. */
15197static int
15198is_vtable_name (const char *name, struct dwarf2_cu *cu)
15199{
15200 static const char vptr[] = "_vptr";
15201
9c37b5ae
TT
15202 /* Look for the C++ form of the vtable. */
15203 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15204 return 1;
15205
15206 return 0;
15207}
15208
c0dd20ea 15209/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15210 functions, with the ABI-specified layout. If TYPE describes
15211 such a structure, smash it into a member function type.
61049d3b
DJ
15212
15213 GCC shouldn't do this; it should just output pointer to member DIEs.
15214 This is GCC PR debug/28767. */
c0dd20ea 15215
0b92b5bb
TT
15216static void
15217quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15218{
09e2d7c7 15219 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15220
15221 /* Check for a structure with no name and two children. */
1f704f76 15222 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15223 return;
c0dd20ea
DJ
15224
15225 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15226 if (TYPE_FIELD_NAME (type, 0) == NULL
15227 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15228 || TYPE_FIELD_NAME (type, 1) == NULL
15229 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15230 return;
c0dd20ea
DJ
15231
15232 /* Find the type of the method. */
0b92b5bb 15233 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15234 if (pfn_type == NULL
78134374
SM
15235 || pfn_type->code () != TYPE_CODE_PTR
15236 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15237 return;
c0dd20ea
DJ
15238
15239 /* Look for the "this" argument. */
15240 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15241 if (pfn_type->num_fields () == 0
0b92b5bb 15242 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15243 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15244 return;
c0dd20ea 15245
09e2d7c7 15246 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15247 new_type = alloc_type (objfile);
09e2d7c7 15248 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15249 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15250 TYPE_VARARGS (pfn_type));
0b92b5bb 15251 smash_to_methodptr_type (type, new_type);
c0dd20ea 15252}
1168df01 15253
2b4424c3
TT
15254/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15255 appropriate error checking and issuing complaints if there is a
15256 problem. */
15257
15258static ULONGEST
15259get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15260{
15261 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15262
15263 if (attr == nullptr)
15264 return 0;
15265
cd6c91b4 15266 if (!attr->form_is_constant ())
2b4424c3 15267 {
b98664d3 15268 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15269 " - DIE at %s [in module %s]"),
15270 sect_offset_str (die->sect_off),
15271 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15272 return 0;
15273 }
15274
15275 ULONGEST align;
15276 if (attr->form == DW_FORM_sdata)
15277 {
15278 LONGEST val = DW_SND (attr);
15279 if (val < 0)
15280 {
b98664d3 15281 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15282 " - DIE at %s [in module %s]"),
15283 sect_offset_str (die->sect_off),
15284 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15285 return 0;
15286 }
15287 align = val;
15288 }
15289 else
15290 align = DW_UNSND (attr);
15291
15292 if (align == 0)
15293 {
b98664d3 15294 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15295 " - DIE at %s [in module %s]"),
15296 sect_offset_str (die->sect_off),
15297 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15298 return 0;
15299 }
15300 if ((align & (align - 1)) != 0)
15301 {
b98664d3 15302 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15303 " - DIE at %s [in module %s]"),
15304 sect_offset_str (die->sect_off),
15305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15306 return 0;
15307 }
15308
15309 return align;
15310}
15311
15312/* If the DIE has a DW_AT_alignment attribute, use its value to set
15313 the alignment for TYPE. */
15314
15315static void
15316maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15317 struct type *type)
15318{
15319 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15320 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15321 " - DIE at %s [in module %s]"),
15322 sect_offset_str (die->sect_off),
15323 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15324}
685b1105 15325
e35000a7
TBA
15326/* Check if the given VALUE is a valid enum dwarf_calling_convention
15327 constant for a type, according to DWARF5 spec, Table 5.5. */
15328
15329static bool
15330is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15331{
15332 switch (value)
15333 {
15334 case DW_CC_normal:
15335 case DW_CC_pass_by_reference:
15336 case DW_CC_pass_by_value:
15337 return true;
15338
15339 default:
15340 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15341 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15342 return false;
15343 }
15344}
15345
d0922fcf
TBA
15346/* Check if the given VALUE is a valid enum dwarf_calling_convention
15347 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15348 also according to GNU-specific values (see include/dwarf2.h). */
15349
15350static bool
15351is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15352{
15353 switch (value)
15354 {
15355 case DW_CC_normal:
15356 case DW_CC_program:
15357 case DW_CC_nocall:
15358 return true;
15359
15360 case DW_CC_GNU_renesas_sh:
15361 case DW_CC_GNU_borland_fastcall_i386:
15362 case DW_CC_GDB_IBM_OpenCL:
15363 return true;
15364
15365 default:
15366 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15367 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15368 return false;
15369 }
15370}
15371
c906108c 15372/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15373 (definition) to create a type for the structure or union. Fill in
15374 the type's name and general properties; the members will not be
83655187
DE
15375 processed until process_structure_scope. A symbol table entry for
15376 the type will also not be done until process_structure_scope (assuming
15377 the type has a name).
c906108c 15378
c767944b
DJ
15379 NOTE: we need to call these functions regardless of whether or not the
15380 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15381 structure or union. This gets the type entered into our set of
83655187 15382 user defined types. */
c906108c 15383
f792889a 15384static struct type *
134d01f1 15385read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15386{
518817b3 15387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15388 struct type *type;
15389 struct attribute *attr;
15d034d0 15390 const char *name;
c906108c 15391
348e048f
DE
15392 /* If the definition of this type lives in .debug_types, read that type.
15393 Don't follow DW_AT_specification though, that will take us back up
15394 the chain and we want to go down. */
052c8bb8 15395 attr = die->attr (DW_AT_signature);
435d3d88 15396 if (attr != nullptr)
348e048f 15397 {
ac9ec31b 15398 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15399
ac9ec31b 15400 /* The type's CU may not be the same as CU.
02142a6c 15401 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15402 return set_die_type (die, type, cu);
15403 }
15404
c0dd20ea 15405 type = alloc_type (objfile);
c906108c 15406 INIT_CPLUS_SPECIFIC (type);
93311388 15407
39cbfefa
DJ
15408 name = dwarf2_name (die, cu);
15409 if (name != NULL)
c906108c 15410 {
987504bb 15411 if (cu->language == language_cplus
c44af4eb
TT
15412 || cu->language == language_d
15413 || cu->language == language_rust)
63d06c5c 15414 {
15d034d0 15415 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15416
15417 /* dwarf2_full_name might have already finished building the DIE's
15418 type. If so, there is no need to continue. */
15419 if (get_die_type (die, cu) != NULL)
15420 return get_die_type (die, cu);
15421
d0e39ea2 15422 type->set_name (full_name);
63d06c5c
DC
15423 }
15424 else
15425 {
d8151005
DJ
15426 /* The name is already allocated along with this objfile, so
15427 we don't need to duplicate it for the type. */
d0e39ea2 15428 type->set_name (name);
63d06c5c 15429 }
c906108c
SS
15430 }
15431
15432 if (die->tag == DW_TAG_structure_type)
15433 {
67607e24 15434 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15435 }
15436 else if (die->tag == DW_TAG_union_type)
15437 {
67607e24 15438 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15439 }
15440 else
15441 {
67607e24 15442 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15443 }
15444
0cc2414c
TT
15445 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15446 TYPE_DECLARED_CLASS (type) = 1;
15447
e35000a7
TBA
15448 /* Store the calling convention in the type if it's available in
15449 the die. Otherwise the calling convention remains set to
15450 the default value DW_CC_normal. */
15451 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15452 if (attr != nullptr
15453 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15454 {
15455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15456 TYPE_CPLUS_CALLING_CONVENTION (type)
15457 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15458 }
15459
e142c38c 15460 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15461 if (attr != nullptr)
c906108c 15462 {
cd6c91b4 15463 if (attr->form_is_constant ())
155bfbd3
JB
15464 TYPE_LENGTH (type) = DW_UNSND (attr);
15465 else
15466 {
f8e89861
TT
15467 struct dynamic_prop prop;
15468 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15469 cu->per_cu->addr_type ()))
5c54719c 15470 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15471 TYPE_LENGTH (type) = 0;
15472 }
c906108c
SS
15473 }
15474 else
15475 {
15476 TYPE_LENGTH (type) = 0;
15477 }
15478
2b4424c3
TT
15479 maybe_set_alignment (cu, die, type);
15480
5230b05a 15481 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15482 {
5230b05a
WT
15483 /* ICC<14 does not output the required DW_AT_declaration on
15484 incomplete types, but gives them a size of zero. */
422b1cb0 15485 TYPE_STUB (type) = 1;
685b1105
JK
15486 }
15487 else
15488 TYPE_STUB_SUPPORTED (type) = 1;
15489
dc718098 15490 if (die_is_declaration (die, cu))
876cecd0 15491 TYPE_STUB (type) = 1;
a6c727b2
DJ
15492 else if (attr == NULL && die->child == NULL
15493 && producer_is_realview (cu->producer))
15494 /* RealView does not output the required DW_AT_declaration
15495 on incomplete types. */
15496 TYPE_STUB (type) = 1;
dc718098 15497
c906108c
SS
15498 /* We need to add the type field to the die immediately so we don't
15499 infinitely recurse when dealing with pointers to the structure
0963b4bd 15500 type within the structure itself. */
1c379e20 15501 set_die_type (die, type, cu);
c906108c 15502
7e314c57
JK
15503 /* set_die_type should be already done. */
15504 set_descriptive_type (type, die, cu);
15505
c767944b
DJ
15506 return type;
15507}
15508
9c6a1327
TT
15509static void handle_struct_member_die
15510 (struct die_info *child_die,
15511 struct type *type,
15512 struct field_info *fi,
15513 std::vector<struct symbol *> *template_args,
15514 struct dwarf2_cu *cu);
15515
15516/* A helper for handle_struct_member_die that handles
15517 DW_TAG_variant_part. */
15518
15519static void
15520handle_variant_part (struct die_info *die, struct type *type,
15521 struct field_info *fi,
15522 std::vector<struct symbol *> *template_args,
15523 struct dwarf2_cu *cu)
15524{
15525 variant_part_builder *new_part;
15526 if (fi->current_variant_part == nullptr)
15527 {
15528 fi->variant_parts.emplace_back ();
15529 new_part = &fi->variant_parts.back ();
15530 }
15531 else if (!fi->current_variant_part->processing_variant)
15532 {
15533 complaint (_("nested DW_TAG_variant_part seen "
15534 "- DIE at %s [in module %s]"),
15535 sect_offset_str (die->sect_off),
15536 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15537 return;
15538 }
15539 else
15540 {
15541 variant_field &current = fi->current_variant_part->variants.back ();
15542 current.variant_parts.emplace_back ();
15543 new_part = &current.variant_parts.back ();
15544 }
15545
15546 /* When we recurse, we want callees to add to this new variant
15547 part. */
15548 scoped_restore save_current_variant_part
15549 = make_scoped_restore (&fi->current_variant_part, new_part);
15550
15551 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15552 if (discr == NULL)
15553 {
15554 /* It's a univariant form, an extension we support. */
15555 }
15556 else if (discr->form_is_ref ())
15557 {
15558 struct dwarf2_cu *target_cu = cu;
15559 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15560
15561 new_part->discriminant_offset = target_die->sect_off;
15562 }
15563 else
15564 {
15565 complaint (_("DW_AT_discr does not have DIE reference form"
15566 " - DIE at %s [in module %s]"),
15567 sect_offset_str (die->sect_off),
15568 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15569 }
15570
15571 for (die_info *child_die = die->child;
15572 child_die != NULL;
15573 child_die = child_die->sibling)
15574 handle_struct_member_die (child_die, type, fi, template_args, cu);
15575}
15576
15577/* A helper for handle_struct_member_die that handles
15578 DW_TAG_variant. */
15579
15580static void
15581handle_variant (struct die_info *die, struct type *type,
15582 struct field_info *fi,
15583 std::vector<struct symbol *> *template_args,
15584 struct dwarf2_cu *cu)
15585{
15586 if (fi->current_variant_part == nullptr)
15587 {
15588 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15589 "- DIE at %s [in module %s]"),
15590 sect_offset_str (die->sect_off),
15591 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15592 return;
15593 }
15594 if (fi->current_variant_part->processing_variant)
15595 {
15596 complaint (_("nested DW_TAG_variant seen "
15597 "- DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
15599 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15600 return;
15601 }
15602
15603 scoped_restore save_processing_variant
15604 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15605 true);
15606
15607 fi->current_variant_part->variants.emplace_back ();
15608 variant_field &variant = fi->current_variant_part->variants.back ();
15609 variant.first_field = fi->fields.size ();
15610
15611 /* In a variant we want to get the discriminant and also add a
15612 field for our sole member child. */
15613 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15614 if (discr == nullptr)
15615 {
15616 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15617 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15618 variant.default_branch = true;
15619 else
15620 variant.discr_list_data = DW_BLOCK (discr);
15621 }
15622 else
15623 variant.discriminant_value = DW_UNSND (discr);
15624
15625 for (die_info *variant_child = die->child;
15626 variant_child != NULL;
15627 variant_child = variant_child->sibling)
15628 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15629
15630 variant.last_field = fi->fields.size ();
15631}
15632
2ddeaf8a
TT
15633/* A helper for process_structure_scope that handles a single member
15634 DIE. */
15635
15636static void
15637handle_struct_member_die (struct die_info *child_die, struct type *type,
15638 struct field_info *fi,
15639 std::vector<struct symbol *> *template_args,
15640 struct dwarf2_cu *cu)
15641{
15642 if (child_die->tag == DW_TAG_member
9c6a1327 15643 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15644 {
15645 /* NOTE: carlton/2002-11-05: A C++ static data member
15646 should be a DW_TAG_member that is a declaration, but
15647 all versions of G++ as of this writing (so through at
15648 least 3.2.1) incorrectly generate DW_TAG_variable
15649 tags for them instead. */
15650 dwarf2_add_field (fi, child_die, cu);
15651 }
15652 else if (child_die->tag == DW_TAG_subprogram)
15653 {
15654 /* Rust doesn't have member functions in the C++ sense.
15655 However, it does emit ordinary functions as children
15656 of a struct DIE. */
15657 if (cu->language == language_rust)
15658 read_func_scope (child_die, cu);
15659 else
15660 {
15661 /* C++ member function. */
15662 dwarf2_add_member_fn (fi, child_die, type, cu);
15663 }
15664 }
15665 else if (child_die->tag == DW_TAG_inheritance)
15666 {
15667 /* C++ base class field. */
15668 dwarf2_add_field (fi, child_die, cu);
15669 }
15670 else if (type_can_define_types (child_die))
15671 dwarf2_add_type_defn (fi, child_die, cu);
15672 else if (child_die->tag == DW_TAG_template_type_param
15673 || child_die->tag == DW_TAG_template_value_param)
15674 {
15675 struct symbol *arg = new_symbol (child_die, NULL, cu);
15676
15677 if (arg != NULL)
15678 template_args->push_back (arg);
15679 }
9c6a1327
TT
15680 else if (child_die->tag == DW_TAG_variant_part)
15681 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15682 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15683 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15684}
15685
c767944b
DJ
15686/* Finish creating a structure or union type, including filling in
15687 its members and creating a symbol for it. */
15688
15689static void
15690process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15691{
518817b3 15692 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15693 struct die_info *child_die;
c767944b
DJ
15694 struct type *type;
15695
15696 type = get_die_type (die, cu);
15697 if (type == NULL)
15698 type = read_structure_type (die, cu);
15699
3e1d3d8c 15700 bool has_template_parameters = false;
e142c38c 15701 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15702 {
15703 struct field_info fi;
2f4732b0 15704 std::vector<struct symbol *> template_args;
c906108c 15705
639d11d3 15706 child_die = die->child;
c906108c
SS
15707
15708 while (child_die && child_die->tag)
15709 {
2ddeaf8a 15710 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15711 child_die = child_die->sibling;
c906108c
SS
15712 }
15713
34eaf542 15714 /* Attach template arguments to type. */
2f4732b0 15715 if (!template_args.empty ())
34eaf542 15716 {
3e1d3d8c 15717 has_template_parameters = true;
34eaf542 15718 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15719 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15720 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15721 = XOBNEWVEC (&objfile->objfile_obstack,
15722 struct symbol *,
15723 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15724 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15725 template_args.data (),
34eaf542
TT
15726 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15727 * sizeof (struct symbol *)));
34eaf542
TT
15728 }
15729
c906108c 15730 /* Attach fields and member functions to the type. */
317f7127 15731 if (fi.nfields () > 0)
e7c27a73 15732 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15733 if (!fi.fnfieldlists.empty ())
c906108c 15734 {
e7c27a73 15735 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15736
c5aa993b 15737 /* Get the type which refers to the base class (possibly this
c906108c 15738 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15739 class from the DW_AT_containing_type attribute. This use of
15740 DW_AT_containing_type is a GNU extension. */
c906108c 15741
e142c38c 15742 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15743 {
e7c27a73 15744 struct type *t = die_containing_type (die, cu);
c906108c 15745
ae6ae975 15746 set_type_vptr_basetype (type, t);
c906108c
SS
15747 if (type == t)
15748 {
c906108c
SS
15749 int i;
15750
15751 /* Our own class provides vtbl ptr. */
1f704f76 15752 for (i = t->num_fields () - 1;
c906108c
SS
15753 i >= TYPE_N_BASECLASSES (t);
15754 --i)
15755 {
0d5cff50 15756 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15757
1168df01 15758 if (is_vtable_name (fieldname, cu))
c906108c 15759 {
ae6ae975 15760 set_type_vptr_fieldno (type, i);
c906108c
SS
15761 break;
15762 }
15763 }
15764
15765 /* Complain if virtual function table field not found. */
15766 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15767 complaint (_("virtual function table pointer "
3e43a32a 15768 "not found when defining class '%s'"),
7d93a1e0 15769 type->name () ? type->name () : "");
c906108c
SS
15770 }
15771 else
15772 {
ae6ae975 15773 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15774 }
15775 }
f6235d4c 15776 else if (cu->producer
61012eef 15777 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15778 {
15779 /* The IBM XLC compiler does not provide direct indication
15780 of the containing type, but the vtable pointer is
15781 always named __vfp. */
15782
15783 int i;
15784
1f704f76 15785 for (i = type->num_fields () - 1;
f6235d4c
EZ
15786 i >= TYPE_N_BASECLASSES (type);
15787 --i)
15788 {
15789 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15790 {
ae6ae975
DE
15791 set_type_vptr_fieldno (type, i);
15792 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15793 break;
15794 }
15795 }
15796 }
c906108c 15797 }
98751a41
JK
15798
15799 /* Copy fi.typedef_field_list linked list elements content into the
15800 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15801 if (!fi.typedef_field_list.empty ())
98751a41 15802 {
be2daae6 15803 int count = fi.typedef_field_list.size ();
98751a41 15804
a0d7a4ff 15805 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15806 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15807 = ((struct decl_field *)
be2daae6
TT
15808 TYPE_ALLOC (type,
15809 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15810 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15811
be2daae6
TT
15812 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15813 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15814 }
c767944b 15815
883fd55a
KS
15816 /* Copy fi.nested_types_list linked list elements content into the
15817 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15818 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15819 {
be2daae6 15820 int count = fi.nested_types_list.size ();
883fd55a
KS
15821
15822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15823 TYPE_NESTED_TYPES_ARRAY (type)
15824 = ((struct decl_field *)
be2daae6
TT
15825 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15826 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15827
be2daae6
TT
15828 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15829 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15830 }
c906108c 15831 }
63d06c5c 15832
bb5ed363 15833 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15834 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15835 cu->rust_unions.push_back (type);
0b92b5bb 15836
90aeadfc
DC
15837 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15838 snapshots) has been known to create a die giving a declaration
15839 for a class that has, as a child, a die giving a definition for a
15840 nested class. So we have to process our children even if the
15841 current die is a declaration. Normally, of course, a declaration
15842 won't have any children at all. */
134d01f1 15843
ca040673
DE
15844 child_die = die->child;
15845
90aeadfc
DC
15846 while (child_die != NULL && child_die->tag)
15847 {
15848 if (child_die->tag == DW_TAG_member
15849 || child_die->tag == DW_TAG_variable
34eaf542
TT
15850 || child_die->tag == DW_TAG_inheritance
15851 || child_die->tag == DW_TAG_template_value_param
15852 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15853 {
90aeadfc 15854 /* Do nothing. */
134d01f1 15855 }
90aeadfc
DC
15856 else
15857 process_die (child_die, cu);
134d01f1 15858
436c571c 15859 child_die = child_die->sibling;
134d01f1
DJ
15860 }
15861
fa4028e9
JB
15862 /* Do not consider external references. According to the DWARF standard,
15863 these DIEs are identified by the fact that they have no byte_size
15864 attribute, and a declaration attribute. */
15865 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15866 || !die_is_declaration (die, cu)
15867 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15868 {
15869 struct symbol *sym = new_symbol (die, type, cu);
15870
15871 if (has_template_parameters)
15872 {
a776957c
TT
15873 struct symtab *symtab;
15874 if (sym != nullptr)
15875 symtab = symbol_symtab (sym);
15876 else if (cu->line_header != nullptr)
15877 {
15878 /* Any related symtab will do. */
15879 symtab
7ba99d21 15880 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15881 }
15882 else
15883 {
15884 symtab = nullptr;
15885 complaint (_("could not find suitable "
15886 "symtab for template parameter"
15887 " - DIE at %s [in module %s]"),
15888 sect_offset_str (die->sect_off),
15889 objfile_name (objfile));
15890 }
15891
15892 if (symtab != nullptr)
15893 {
15894 /* Make sure that the symtab is set on the new symbols.
15895 Even though they don't appear in this symtab directly,
15896 other parts of gdb assume that symbols do, and this is
15897 reasonably true. */
15898 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15899 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15900 }
3e1d3d8c
TT
15901 }
15902 }
134d01f1
DJ
15903}
15904
ed6acedd
TT
15905/* Assuming DIE is an enumeration type, and TYPE is its associated
15906 type, update TYPE using some information only available in DIE's
15907 children. In particular, the fields are computed. */
55426c9d
JB
15908
15909static void
15910update_enumeration_type_from_children (struct die_info *die,
15911 struct type *type,
15912 struct dwarf2_cu *cu)
15913{
60f7655a 15914 struct die_info *child_die;
55426c9d
JB
15915 int unsigned_enum = 1;
15916 int flag_enum = 1;
55426c9d 15917
8268c778 15918 auto_obstack obstack;
ed6acedd 15919 std::vector<struct field> fields;
55426c9d 15920
60f7655a
DE
15921 for (child_die = die->child;
15922 child_die != NULL && child_die->tag;
436c571c 15923 child_die = child_die->sibling)
55426c9d
JB
15924 {
15925 struct attribute *attr;
15926 LONGEST value;
15927 const gdb_byte *bytes;
15928 struct dwarf2_locexpr_baton *baton;
15929 const char *name;
60f7655a 15930
55426c9d
JB
15931 if (child_die->tag != DW_TAG_enumerator)
15932 continue;
15933
15934 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15935 if (attr == NULL)
15936 continue;
15937
15938 name = dwarf2_name (child_die, cu);
15939 if (name == NULL)
15940 name = "<anonymous enumerator>";
15941
15942 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15943 &value, &bytes, &baton);
15944 if (value < 0)
15945 {
15946 unsigned_enum = 0;
15947 flag_enum = 0;
15948 }
55426c9d 15949 else
edd45eb0
SM
15950 {
15951 if (count_one_bits_ll (value) >= 2)
15952 flag_enum = 0;
edd45eb0 15953 }
55426c9d 15954
ed6acedd
TT
15955 fields.emplace_back ();
15956 struct field &field = fields.back ();
15957 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15958 SET_FIELD_ENUMVAL (field, value);
15959 }
15960
15961 if (!fields.empty ())
15962 {
5e33d5f4 15963 type->set_num_fields (fields.size ());
3cabb6b0
SM
15964 type->set_fields
15965 ((struct field *)
15966 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 15967 memcpy (type->fields (), fields.data (),
ed6acedd 15968 sizeof (struct field) * fields.size ());
55426c9d
JB
15969 }
15970
15971 if (unsigned_enum)
15972 TYPE_UNSIGNED (type) = 1;
15973 if (flag_enum)
15974 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15975}
15976
134d01f1
DJ
15977/* Given a DW_AT_enumeration_type die, set its type. We do not
15978 complete the type's fields yet, or create any symbols. */
c906108c 15979
f792889a 15980static struct type *
134d01f1 15981read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15982{
518817b3 15983 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15984 struct type *type;
c906108c 15985 struct attribute *attr;
0114d602 15986 const char *name;
134d01f1 15987
348e048f
DE
15988 /* If the definition of this type lives in .debug_types, read that type.
15989 Don't follow DW_AT_specification though, that will take us back up
15990 the chain and we want to go down. */
052c8bb8 15991 attr = die->attr (DW_AT_signature);
435d3d88 15992 if (attr != nullptr)
348e048f 15993 {
ac9ec31b 15994 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15995
ac9ec31b 15996 /* The type's CU may not be the same as CU.
02142a6c 15997 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15998 return set_die_type (die, type, cu);
15999 }
16000
c906108c
SS
16001 type = alloc_type (objfile);
16002
67607e24 16003 type->set_code (TYPE_CODE_ENUM);
94af9270 16004 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16005 if (name != NULL)
d0e39ea2 16006 type->set_name (name);
c906108c 16007
0626fc76
TT
16008 attr = dwarf2_attr (die, DW_AT_type, cu);
16009 if (attr != NULL)
16010 {
16011 struct type *underlying_type = die_type (die, cu);
16012
16013 TYPE_TARGET_TYPE (type) = underlying_type;
16014 }
16015
e142c38c 16016 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16017 if (attr != nullptr)
c906108c
SS
16018 {
16019 TYPE_LENGTH (type) = DW_UNSND (attr);
16020 }
16021 else
16022 {
16023 TYPE_LENGTH (type) = 0;
16024 }
16025
2b4424c3
TT
16026 maybe_set_alignment (cu, die, type);
16027
137033e9
JB
16028 /* The enumeration DIE can be incomplete. In Ada, any type can be
16029 declared as private in the package spec, and then defined only
16030 inside the package body. Such types are known as Taft Amendment
16031 Types. When another package uses such a type, an incomplete DIE
16032 may be generated by the compiler. */
02eb380e 16033 if (die_is_declaration (die, cu))
876cecd0 16034 TYPE_STUB (type) = 1;
02eb380e 16035
0626fc76
TT
16036 /* If this type has an underlying type that is not a stub, then we
16037 may use its attributes. We always use the "unsigned" attribute
16038 in this situation, because ordinarily we guess whether the type
16039 is unsigned -- but the guess can be wrong and the underlying type
16040 can tell us the reality. However, we defer to a local size
16041 attribute if one exists, because this lets the compiler override
16042 the underlying type if needed. */
16043 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16044 {
9e7c9a03
HD
16045 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16046 underlying_type = check_typedef (underlying_type);
16047 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16048 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16049 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16050 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16051 && TYPE_RAW_ALIGN (underlying_type) != 0)
16052 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16053 }
16054
3d567982
TT
16055 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16056
ed6acedd
TT
16057 set_die_type (die, type, cu);
16058
16059 /* Finish the creation of this type by using the enum's children.
16060 Note that, as usual, this must come after set_die_type to avoid
16061 infinite recursion when trying to compute the names of the
16062 enumerators. */
16063 update_enumeration_type_from_children (die, type, cu);
16064
16065 return type;
134d01f1
DJ
16066}
16067
16068/* Given a pointer to a die which begins an enumeration, process all
16069 the dies that define the members of the enumeration, and create the
16070 symbol for the enumeration type.
16071
16072 NOTE: We reverse the order of the element list. */
16073
16074static void
16075process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16076{
f792889a 16077 struct type *this_type;
134d01f1 16078
f792889a
DJ
16079 this_type = get_die_type (die, cu);
16080 if (this_type == NULL)
16081 this_type = read_enumeration_type (die, cu);
9dc481d3 16082
639d11d3 16083 if (die->child != NULL)
c906108c 16084 {
9dc481d3 16085 struct die_info *child_die;
15d034d0 16086 const char *name;
9dc481d3 16087
639d11d3 16088 child_die = die->child;
c906108c
SS
16089 while (child_die && child_die->tag)
16090 {
16091 if (child_die->tag != DW_TAG_enumerator)
16092 {
e7c27a73 16093 process_die (child_die, cu);
c906108c
SS
16094 }
16095 else
16096 {
39cbfefa
DJ
16097 name = dwarf2_name (child_die, cu);
16098 if (name)
ed6acedd 16099 new_symbol (child_die, this_type, cu);
c906108c
SS
16100 }
16101
436c571c 16102 child_die = child_die->sibling;
c906108c 16103 }
c906108c 16104 }
134d01f1 16105
6c83ed52
TT
16106 /* If we are reading an enum from a .debug_types unit, and the enum
16107 is a declaration, and the enum is not the signatured type in the
16108 unit, then we do not want to add a symbol for it. Adding a
16109 symbol would in some cases obscure the true definition of the
16110 enum, giving users an incomplete type when the definition is
16111 actually available. Note that we do not want to do this for all
16112 enums which are just declarations, because C++0x allows forward
16113 enum declarations. */
3019eac3 16114 if (cu->per_cu->is_debug_types
6c83ed52
TT
16115 && die_is_declaration (die, cu))
16116 {
52dc124a 16117 struct signatured_type *sig_type;
6c83ed52 16118
c0f78cd4 16119 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16121 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16122 return;
16123 }
16124
f792889a 16125 new_symbol (die, this_type, cu);
c906108c
SS
16126}
16127
16128/* Extract all information from a DW_TAG_array_type DIE and put it in
16129 the DIE's type field. For now, this only handles one dimensional
16130 arrays. */
16131
f792889a 16132static struct type *
e7c27a73 16133read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16134{
518817b3 16135 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16136 struct die_info *child_die;
7e314c57 16137 struct type *type;
c906108c 16138 struct type *element_type, *range_type, *index_type;
c906108c 16139 struct attribute *attr;
15d034d0 16140 const char *name;
a405673c 16141 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16142 unsigned int bit_stride = 0;
c906108c 16143
e7c27a73 16144 element_type = die_type (die, cu);
c906108c 16145
7e314c57
JK
16146 /* The die_type call above may have already set the type for this DIE. */
16147 type = get_die_type (die, cu);
16148 if (type)
16149 return type;
16150
dc53a7ad
JB
16151 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16152 if (attr != NULL)
a405673c
JB
16153 {
16154 int stride_ok;
09ba997f 16155 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16156
16157 byte_stride_prop
16158 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16159 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16160 prop_type);
a405673c
JB
16161 if (!stride_ok)
16162 {
b98664d3 16163 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16164 " - DIE at %s [in module %s]"),
16165 sect_offset_str (die->sect_off),
518817b3 16166 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16167 /* Ignore this attribute. We will likely not be able to print
16168 arrays of this type correctly, but there is little we can do
16169 to help if we cannot read the attribute's value. */
16170 byte_stride_prop = NULL;
16171 }
16172 }
dc53a7ad
JB
16173
16174 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16175 if (attr != NULL)
16176 bit_stride = DW_UNSND (attr);
16177
c906108c
SS
16178 /* Irix 6.2 native cc creates array types without children for
16179 arrays with unspecified length. */
639d11d3 16180 if (die->child == NULL)
c906108c 16181 {
46bf5051 16182 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16183 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16184 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16185 byte_stride_prop, bit_stride);
f792889a 16186 return set_die_type (die, type, cu);
c906108c
SS
16187 }
16188
791afaa2 16189 std::vector<struct type *> range_types;
639d11d3 16190 child_die = die->child;
c906108c
SS
16191 while (child_die && child_die->tag)
16192 {
16193 if (child_die->tag == DW_TAG_subrange_type)
16194 {
f792889a 16195 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16196
f792889a 16197 if (child_type != NULL)
a02abb62 16198 {
0963b4bd
MS
16199 /* The range type was succesfully read. Save it for the
16200 array type creation. */
791afaa2 16201 range_types.push_back (child_type);
a02abb62 16202 }
c906108c 16203 }
436c571c 16204 child_die = child_die->sibling;
c906108c
SS
16205 }
16206
16207 /* Dwarf2 dimensions are output from left to right, create the
16208 necessary array types in backwards order. */
7ca2d3a3 16209
c906108c 16210 type = element_type;
7ca2d3a3
DL
16211
16212 if (read_array_order (die, cu) == DW_ORD_col_major)
16213 {
16214 int i = 0;
9a619af0 16215
791afaa2 16216 while (i < range_types.size ())
dc53a7ad 16217 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16218 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16219 }
16220 else
16221 {
791afaa2 16222 size_t ndim = range_types.size ();
7ca2d3a3 16223 while (ndim-- > 0)
dc53a7ad 16224 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16225 byte_stride_prop, bit_stride);
7ca2d3a3 16226 }
c906108c 16227
f5f8a009
EZ
16228 /* Understand Dwarf2 support for vector types (like they occur on
16229 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16230 array type. This is not part of the Dwarf2/3 standard yet, but a
16231 custom vendor extension. The main difference between a regular
16232 array and the vector variant is that vectors are passed by value
16233 to functions. */
e142c38c 16234 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16235 if (attr != nullptr)
ea37ba09 16236 make_vector_type (type);
f5f8a009 16237
dbc98a8b
KW
16238 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16239 implementation may choose to implement triple vectors using this
16240 attribute. */
16241 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16242 if (attr != nullptr)
dbc98a8b
KW
16243 {
16244 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16245 TYPE_LENGTH (type) = DW_UNSND (attr);
16246 else
b98664d3 16247 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16248 "than the total size of elements"));
dbc98a8b
KW
16249 }
16250
39cbfefa
DJ
16251 name = dwarf2_name (die, cu);
16252 if (name)
d0e39ea2 16253 type->set_name (name);
6e70227d 16254
2b4424c3
TT
16255 maybe_set_alignment (cu, die, type);
16256
0963b4bd 16257 /* Install the type in the die. */
7e314c57
JK
16258 set_die_type (die, type, cu);
16259
16260 /* set_die_type should be already done. */
b4ba55a1
JB
16261 set_descriptive_type (type, die, cu);
16262
7e314c57 16263 return type;
c906108c
SS
16264}
16265
7ca2d3a3 16266static enum dwarf_array_dim_ordering
6e70227d 16267read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16268{
16269 struct attribute *attr;
16270
16271 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16272
435d3d88 16273 if (attr != nullptr)
aead7601 16274 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16275
0963b4bd
MS
16276 /* GNU F77 is a special case, as at 08/2004 array type info is the
16277 opposite order to the dwarf2 specification, but data is still
16278 laid out as per normal fortran.
7ca2d3a3 16279
0963b4bd
MS
16280 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16281 version checking. */
7ca2d3a3 16282
905e0470
PM
16283 if (cu->language == language_fortran
16284 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16285 {
16286 return DW_ORD_row_major;
16287 }
16288
6e70227d 16289 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16290 {
16291 case array_column_major:
16292 return DW_ORD_col_major;
16293 case array_row_major:
16294 default:
16295 return DW_ORD_row_major;
16296 };
16297}
16298
72019c9c 16299/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16300 the DIE's type field. */
72019c9c 16301
f792889a 16302static struct type *
72019c9c
GM
16303read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16304{
7e314c57
JK
16305 struct type *domain_type, *set_type;
16306 struct attribute *attr;
f792889a 16307
7e314c57
JK
16308 domain_type = die_type (die, cu);
16309
16310 /* The die_type call above may have already set the type for this DIE. */
16311 set_type = get_die_type (die, cu);
16312 if (set_type)
16313 return set_type;
16314
16315 set_type = create_set_type (NULL, domain_type);
16316
16317 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16318 if (attr != nullptr)
d09039dd 16319 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16320
2b4424c3
TT
16321 maybe_set_alignment (cu, die, set_type);
16322
f792889a 16323 return set_die_type (die, set_type, cu);
72019c9c 16324}
7ca2d3a3 16325
0971de02
TT
16326/* A helper for read_common_block that creates a locexpr baton.
16327 SYM is the symbol which we are marking as computed.
16328 COMMON_DIE is the DIE for the common block.
16329 COMMON_LOC is the location expression attribute for the common
16330 block itself.
16331 MEMBER_LOC is the location expression attribute for the particular
16332 member of the common block that we are processing.
16333 CU is the CU from which the above come. */
16334
16335static void
16336mark_common_block_symbol_computed (struct symbol *sym,
16337 struct die_info *common_die,
16338 struct attribute *common_loc,
16339 struct attribute *member_loc,
16340 struct dwarf2_cu *cu)
16341{
a50264ba
TT
16342 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
16343 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16344 struct dwarf2_locexpr_baton *baton;
16345 gdb_byte *ptr;
16346 unsigned int cu_off;
08feed99 16347 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16348 LONGEST offset = 0;
16349
16350 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16351 gdb_assert (common_loc->form_is_block ());
16352 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16353 || member_loc->form_is_constant ());
0971de02 16354
8d749320 16355 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16356 baton->per_objfile = per_objfile;
0971de02
TT
16357 baton->per_cu = cu->per_cu;
16358 gdb_assert (baton->per_cu);
16359
16360 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16361
cd6c91b4 16362 if (member_loc->form_is_constant ())
0971de02 16363 {
0826b30a 16364 offset = member_loc->constant_value (0);
0971de02
TT
16365 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16366 }
16367 else
16368 baton->size += DW_BLOCK (member_loc)->size;
16369
224c3ddb 16370 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16371 baton->data = ptr;
16372
16373 *ptr++ = DW_OP_call4;
9c541725 16374 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16375 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16376 ptr += 4;
16377
cd6c91b4 16378 if (member_loc->form_is_constant ())
0971de02
TT
16379 {
16380 *ptr++ = DW_OP_addr;
16381 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16382 ptr += cu->header.addr_size;
16383 }
16384 else
16385 {
16386 /* We have to copy the data here, because DW_OP_call4 will only
16387 use a DW_AT_location attribute. */
16388 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16389 ptr += DW_BLOCK (member_loc)->size;
16390 }
16391
16392 *ptr++ = DW_OP_plus;
16393 gdb_assert (ptr - baton->data == baton->size);
16394
0971de02 16395 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16396 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16397}
16398
4357ac6c
TT
16399/* Create appropriate locally-scoped variables for all the
16400 DW_TAG_common_block entries. Also create a struct common_block
16401 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16402 is used to separate the common blocks name namespace from regular
4357ac6c 16403 variable names. */
c906108c
SS
16404
16405static void
e7c27a73 16406read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16407{
0971de02
TT
16408 struct attribute *attr;
16409
16410 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16411 if (attr != nullptr)
0971de02
TT
16412 {
16413 /* Support the .debug_loc offsets. */
4fc6c0d5 16414 if (attr->form_is_block ())
0971de02
TT
16415 {
16416 /* Ok. */
16417 }
cd6c91b4 16418 else if (attr->form_is_section_offset ())
0971de02
TT
16419 {
16420 dwarf2_complex_location_expr_complaint ();
16421 attr = NULL;
16422 }
16423 else
16424 {
16425 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16426 "common block member");
16427 attr = NULL;
16428 }
16429 }
16430
639d11d3 16431 if (die->child != NULL)
c906108c 16432 {
518817b3 16433 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16434 struct die_info *child_die;
16435 size_t n_entries = 0, size;
16436 struct common_block *common_block;
16437 struct symbol *sym;
74ac6d43 16438
4357ac6c
TT
16439 for (child_die = die->child;
16440 child_die && child_die->tag;
436c571c 16441 child_die = child_die->sibling)
4357ac6c
TT
16442 ++n_entries;
16443
16444 size = (sizeof (struct common_block)
16445 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16446 common_block
16447 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16448 size);
4357ac6c
TT
16449 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16450 common_block->n_entries = 0;
16451
16452 for (child_die = die->child;
16453 child_die && child_die->tag;
436c571c 16454 child_die = child_die->sibling)
4357ac6c
TT
16455 {
16456 /* Create the symbol in the DW_TAG_common_block block in the current
16457 symbol scope. */
e7c27a73 16458 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16459 if (sym != NULL)
16460 {
16461 struct attribute *member_loc;
16462
16463 common_block->contents[common_block->n_entries++] = sym;
16464
16465 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16466 cu);
16467 if (member_loc)
16468 {
16469 /* GDB has handled this for a long time, but it is
16470 not specified by DWARF. It seems to have been
16471 emitted by gfortran at least as recently as:
16472 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16473 complaint (_("Variable in common block has "
0971de02 16474 "DW_AT_data_member_location "
9d8780f0
SM
16475 "- DIE at %s [in module %s]"),
16476 sect_offset_str (child_die->sect_off),
518817b3 16477 objfile_name (objfile));
0971de02 16478
cd6c91b4 16479 if (member_loc->form_is_section_offset ())
0971de02 16480 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16481 else if (member_loc->form_is_constant ()
4fc6c0d5 16482 || member_loc->form_is_block ())
0971de02 16483 {
435d3d88 16484 if (attr != nullptr)
0971de02
TT
16485 mark_common_block_symbol_computed (sym, die, attr,
16486 member_loc, cu);
16487 }
16488 else
16489 dwarf2_complex_location_expr_complaint ();
16490 }
16491 }
c906108c 16492 }
4357ac6c
TT
16493
16494 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16495 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16496 }
16497}
16498
0114d602 16499/* Create a type for a C++ namespace. */
d9fa45fe 16500
0114d602
DJ
16501static struct type *
16502read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16503{
518817b3 16504 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16505 const char *previous_prefix, *name;
9219021c 16506 int is_anonymous;
0114d602
DJ
16507 struct type *type;
16508
16509 /* For extensions, reuse the type of the original namespace. */
16510 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16511 {
16512 struct die_info *ext_die;
16513 struct dwarf2_cu *ext_cu = cu;
9a619af0 16514
0114d602
DJ
16515 ext_die = dwarf2_extension (die, &ext_cu);
16516 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16517
16518 /* EXT_CU may not be the same as CU.
02142a6c 16519 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16520 return set_die_type (die, type, cu);
16521 }
9219021c 16522
e142c38c 16523 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16524
16525 /* Now build the name of the current namespace. */
16526
0114d602
DJ
16527 previous_prefix = determine_prefix (die, cu);
16528 if (previous_prefix[0] != '\0')
16529 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16530 previous_prefix, name, 0, cu);
0114d602
DJ
16531
16532 /* Create the type. */
19f392bc 16533 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16534
60531b24 16535 return set_die_type (die, type, cu);
0114d602
DJ
16536}
16537
22cee43f 16538/* Read a namespace scope. */
0114d602
DJ
16539
16540static void
16541read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16542{
518817b3 16543 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16544 int is_anonymous;
9219021c 16545
5c4e30ca
DC
16546 /* Add a symbol associated to this if we haven't seen the namespace
16547 before. Also, add a using directive if it's an anonymous
16548 namespace. */
9219021c 16549
f2f0e013 16550 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16551 {
16552 struct type *type;
16553
0114d602 16554 type = read_type_die (die, cu);
e7c27a73 16555 new_symbol (die, type, cu);
5c4e30ca 16556
e8e80198 16557 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16558 if (is_anonymous)
0114d602
DJ
16559 {
16560 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16561
eb1e02fd 16562 std::vector<const char *> excludes;
804d2729 16563 add_using_directive (using_directives (cu),
7d93a1e0 16564 previous_prefix, type->name (), NULL,
eb1e02fd 16565 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16566 }
5c4e30ca 16567 }
9219021c 16568
639d11d3 16569 if (die->child != NULL)
d9fa45fe 16570 {
639d11d3 16571 struct die_info *child_die = die->child;
6e70227d 16572
d9fa45fe
DC
16573 while (child_die && child_die->tag)
16574 {
e7c27a73 16575 process_die (child_die, cu);
436c571c 16576 child_die = child_die->sibling;
d9fa45fe
DC
16577 }
16578 }
38d518c9
EZ
16579}
16580
f55ee35c
JK
16581/* Read a Fortran module as type. This DIE can be only a declaration used for
16582 imported module. Still we need that type as local Fortran "use ... only"
16583 declaration imports depend on the created type in determine_prefix. */
16584
16585static struct type *
16586read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16587{
518817b3 16588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16589 const char *module_name;
f55ee35c
JK
16590 struct type *type;
16591
16592 module_name = dwarf2_name (die, cu);
19f392bc 16593 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16594
f55ee35c
JK
16595 return set_die_type (die, type, cu);
16596}
16597
5d7cb8df
JK
16598/* Read a Fortran module. */
16599
16600static void
16601read_module (struct die_info *die, struct dwarf2_cu *cu)
16602{
16603 struct die_info *child_die = die->child;
530e8392
KB
16604 struct type *type;
16605
16606 type = read_type_die (die, cu);
16607 new_symbol (die, type, cu);
5d7cb8df 16608
5d7cb8df
JK
16609 while (child_die && child_die->tag)
16610 {
16611 process_die (child_die, cu);
436c571c 16612 child_die = child_die->sibling;
5d7cb8df
JK
16613 }
16614}
16615
38d518c9
EZ
16616/* Return the name of the namespace represented by DIE. Set
16617 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16618 namespace. */
16619
16620static const char *
e142c38c 16621namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16622{
16623 struct die_info *current_die;
16624 const char *name = NULL;
16625
16626 /* Loop through the extensions until we find a name. */
16627
16628 for (current_die = die;
16629 current_die != NULL;
f2f0e013 16630 current_die = dwarf2_extension (die, &cu))
38d518c9 16631 {
96553a0c
DE
16632 /* We don't use dwarf2_name here so that we can detect the absence
16633 of a name -> anonymous namespace. */
7d45c7c3 16634 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16635
38d518c9
EZ
16636 if (name != NULL)
16637 break;
16638 }
16639
16640 /* Is it an anonymous namespace? */
16641
16642 *is_anonymous = (name == NULL);
16643 if (*is_anonymous)
2b1dbab0 16644 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16645
16646 return name;
d9fa45fe
DC
16647}
16648
c906108c
SS
16649/* Extract all information from a DW_TAG_pointer_type DIE and add to
16650 the user defined type vector. */
16651
f792889a 16652static struct type *
e7c27a73 16653read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16654{
518817b3 16655 struct gdbarch *gdbarch
08feed99 16656 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16657 struct comp_unit_head *cu_header = &cu->header;
c906108c 16658 struct type *type;
8b2dbe47
KB
16659 struct attribute *attr_byte_size;
16660 struct attribute *attr_address_class;
16661 int byte_size, addr_class;
7e314c57
JK
16662 struct type *target_type;
16663
16664 target_type = die_type (die, cu);
c906108c 16665
7e314c57
JK
16666 /* The die_type call above may have already set the type for this DIE. */
16667 type = get_die_type (die, cu);
16668 if (type)
16669 return type;
16670
16671 type = lookup_pointer_type (target_type);
8b2dbe47 16672
e142c38c 16673 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16674 if (attr_byte_size)
16675 byte_size = DW_UNSND (attr_byte_size);
c906108c 16676 else
8b2dbe47
KB
16677 byte_size = cu_header->addr_size;
16678
e142c38c 16679 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16680 if (attr_address_class)
16681 addr_class = DW_UNSND (attr_address_class);
16682 else
16683 addr_class = DW_ADDR_none;
16684
2b4424c3
TT
16685 ULONGEST alignment = get_alignment (cu, die);
16686
16687 /* If the pointer size, alignment, or address class is different
16688 than the default, create a type variant marked as such and set
16689 the length accordingly. */
16690 if (TYPE_LENGTH (type) != byte_size
16691 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16692 && alignment != TYPE_RAW_ALIGN (type))
16693 || addr_class != DW_ADDR_none)
c906108c 16694 {
5e2b427d 16695 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16696 {
16697 int type_flags;
16698
849957d9 16699 type_flags = gdbarch_address_class_type_flags
5e2b427d 16700 (gdbarch, byte_size, addr_class);
876cecd0
TT
16701 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16702 == 0);
8b2dbe47
KB
16703 type = make_type_with_address_space (type, type_flags);
16704 }
16705 else if (TYPE_LENGTH (type) != byte_size)
16706 {
b98664d3 16707 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16708 }
2b4424c3
TT
16709 else if (TYPE_RAW_ALIGN (type) != alignment)
16710 {
b98664d3 16711 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16712 " - DIE at %s [in module %s]"),
16713 sect_offset_str (die->sect_off),
16714 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16715 }
6e70227d 16716 else
9a619af0
MS
16717 {
16718 /* Should we also complain about unhandled address classes? */
16719 }
c906108c 16720 }
8b2dbe47
KB
16721
16722 TYPE_LENGTH (type) = byte_size;
2b4424c3 16723 set_type_align (type, alignment);
f792889a 16724 return set_die_type (die, type, cu);
c906108c
SS
16725}
16726
16727/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16728 the user defined type vector. */
16729
f792889a 16730static struct type *
e7c27a73 16731read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16732{
16733 struct type *type;
16734 struct type *to_type;
16735 struct type *domain;
16736
e7c27a73
DJ
16737 to_type = die_type (die, cu);
16738 domain = die_containing_type (die, cu);
0d5de010 16739
7e314c57
JK
16740 /* The calls above may have already set the type for this DIE. */
16741 type = get_die_type (die, cu);
16742 if (type)
16743 return type;
16744
78134374 16745 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16746 type = lookup_methodptr_type (to_type);
78134374 16747 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16748 {
518817b3
SM
16749 struct type *new_type
16750 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16751
16752 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16753 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16754 TYPE_VARARGS (to_type));
16755 type = lookup_methodptr_type (new_type);
16756 }
0d5de010
DJ
16757 else
16758 type = lookup_memberptr_type (to_type, domain);
c906108c 16759
f792889a 16760 return set_die_type (die, type, cu);
c906108c
SS
16761}
16762
4297a3f0 16763/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16764 the user defined type vector. */
16765
f792889a 16766static struct type *
4297a3f0
AV
16767read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16768 enum type_code refcode)
c906108c 16769{
e7c27a73 16770 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16771 struct type *type, *target_type;
c906108c
SS
16772 struct attribute *attr;
16773
4297a3f0
AV
16774 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16775
7e314c57
JK
16776 target_type = die_type (die, cu);
16777
16778 /* The die_type call above may have already set the type for this DIE. */
16779 type = get_die_type (die, cu);
16780 if (type)
16781 return type;
16782
4297a3f0 16783 type = lookup_reference_type (target_type, refcode);
e142c38c 16784 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16785 if (attr != nullptr)
c906108c
SS
16786 {
16787 TYPE_LENGTH (type) = DW_UNSND (attr);
16788 }
16789 else
16790 {
107d2387 16791 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16792 }
2b4424c3 16793 maybe_set_alignment (cu, die, type);
f792889a 16794 return set_die_type (die, type, cu);
c906108c
SS
16795}
16796
cf363f18
MW
16797/* Add the given cv-qualifiers to the element type of the array. GCC
16798 outputs DWARF type qualifiers that apply to an array, not the
16799 element type. But GDB relies on the array element type to carry
16800 the cv-qualifiers. This mimics section 6.7.3 of the C99
16801 specification. */
16802
16803static struct type *
16804add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16805 struct type *base_type, int cnst, int voltl)
16806{
16807 struct type *el_type, *inner_array;
16808
16809 base_type = copy_type (base_type);
16810 inner_array = base_type;
16811
78134374 16812 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16813 {
16814 TYPE_TARGET_TYPE (inner_array) =
16815 copy_type (TYPE_TARGET_TYPE (inner_array));
16816 inner_array = TYPE_TARGET_TYPE (inner_array);
16817 }
16818
16819 el_type = TYPE_TARGET_TYPE (inner_array);
16820 cnst |= TYPE_CONST (el_type);
16821 voltl |= TYPE_VOLATILE (el_type);
16822 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16823
16824 return set_die_type (die, base_type, cu);
16825}
16826
f792889a 16827static struct type *
e7c27a73 16828read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16829{
f792889a 16830 struct type *base_type, *cv_type;
c906108c 16831
e7c27a73 16832 base_type = die_type (die, cu);
7e314c57
JK
16833
16834 /* The die_type call above may have already set the type for this DIE. */
16835 cv_type = get_die_type (die, cu);
16836 if (cv_type)
16837 return cv_type;
16838
2f608a3a
KW
16839 /* In case the const qualifier is applied to an array type, the element type
16840 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16841 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16842 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16843
f792889a
DJ
16844 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16845 return set_die_type (die, cv_type, cu);
c906108c
SS
16846}
16847
f792889a 16848static struct type *
e7c27a73 16849read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16850{
f792889a 16851 struct type *base_type, *cv_type;
c906108c 16852
e7c27a73 16853 base_type = die_type (die, cu);
7e314c57
JK
16854
16855 /* The die_type call above may have already set the type for this DIE. */
16856 cv_type = get_die_type (die, cu);
16857 if (cv_type)
16858 return cv_type;
16859
cf363f18
MW
16860 /* In case the volatile qualifier is applied to an array type, the
16861 element type is so qualified, not the array type (section 6.7.3
16862 of C99). */
78134374 16863 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16864 return add_array_cv_type (die, cu, base_type, 0, 1);
16865
f792889a
DJ
16866 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16867 return set_die_type (die, cv_type, cu);
c906108c
SS
16868}
16869
06d66ee9
TT
16870/* Handle DW_TAG_restrict_type. */
16871
16872static struct type *
16873read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16874{
16875 struct type *base_type, *cv_type;
16876
16877 base_type = die_type (die, cu);
16878
16879 /* The die_type call above may have already set the type for this DIE. */
16880 cv_type = get_die_type (die, cu);
16881 if (cv_type)
16882 return cv_type;
16883
16884 cv_type = make_restrict_type (base_type);
16885 return set_die_type (die, cv_type, cu);
16886}
16887
a2c2acaf
MW
16888/* Handle DW_TAG_atomic_type. */
16889
16890static struct type *
16891read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16892{
16893 struct type *base_type, *cv_type;
16894
16895 base_type = die_type (die, cu);
16896
16897 /* The die_type call above may have already set the type for this DIE. */
16898 cv_type = get_die_type (die, cu);
16899 if (cv_type)
16900 return cv_type;
16901
16902 cv_type = make_atomic_type (base_type);
16903 return set_die_type (die, cv_type, cu);
16904}
16905
c906108c
SS
16906/* Extract all information from a DW_TAG_string_type DIE and add to
16907 the user defined type vector. It isn't really a user defined type,
16908 but it behaves like one, with other DIE's using an AT_user_def_type
16909 attribute to reference it. */
16910
f792889a 16911static struct type *
e7c27a73 16912read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16913{
518817b3 16914 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16915 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16916 struct type *type, *range_type, *index_type, *char_type;
16917 struct attribute *attr;
216a7e6b
AB
16918 struct dynamic_prop prop;
16919 bool length_is_constant = true;
16920 LONGEST length;
16921
16922 /* There are a couple of places where bit sizes might be made use of
16923 when parsing a DW_TAG_string_type, however, no producer that we know
16924 of make use of these. Handling bit sizes that are a multiple of the
16925 byte size is easy enough, but what about other bit sizes? Lets deal
16926 with that problem when we have to. Warn about these attributes being
16927 unsupported, then parse the type and ignore them like we always
16928 have. */
16929 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16930 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16931 {
16932 static bool warning_printed = false;
16933 if (!warning_printed)
16934 {
16935 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16936 "currently supported on DW_TAG_string_type."));
16937 warning_printed = true;
16938 }
16939 }
c906108c 16940
e142c38c 16941 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16942 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16943 {
16944 /* The string length describes the location at which the length of
16945 the string can be found. The size of the length field can be
16946 specified with one of the attributes below. */
16947 struct type *prop_type;
16948 struct attribute *len
16949 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16950 if (len == nullptr)
16951 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16952 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16953 {
16954 /* Pass 0 as the default as we know this attribute is constant
16955 and the default value will not be returned. */
0826b30a 16956 LONGEST sz = len->constant_value (0);
09ba997f 16957 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16958 }
16959 else
16960 {
16961 /* If the size is not specified then we assume it is the size of
16962 an address on this target. */
09ba997f 16963 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16964 }
16965
16966 /* Convert the attribute into a dynamic property. */
16967 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16968 length = 1;
16969 else
16970 length_is_constant = false;
16971 }
16972 else if (attr != nullptr)
16973 {
16974 /* This DW_AT_string_length just contains the length with no
16975 indirection. There's no need to create a dynamic property in this
16976 case. Pass 0 for the default value as we know it will not be
16977 returned in this case. */
0826b30a 16978 length = attr->constant_value (0);
216a7e6b
AB
16979 }
16980 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16981 {
216a7e6b 16982 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16983 length = attr->constant_value (1);
c906108c
SS
16984 }
16985 else
16986 {
216a7e6b
AB
16987 /* Use 1 as a fallback length if we have nothing else. */
16988 length = 1;
c906108c 16989 }
6ccb9162 16990
46bf5051 16991 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16992 if (length_is_constant)
16993 range_type = create_static_range_type (NULL, index_type, 1, length);
16994 else
16995 {
16996 struct dynamic_prop low_bound;
16997
16998 low_bound.kind = PROP_CONST;
16999 low_bound.data.const_val = 1;
17000 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17001 }
3b7538c0
UW
17002 char_type = language_string_char_type (cu->language_defn, gdbarch);
17003 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17004
f792889a 17005 return set_die_type (die, type, cu);
c906108c
SS
17006}
17007
4d804846
JB
17008/* Assuming that DIE corresponds to a function, returns nonzero
17009 if the function is prototyped. */
17010
17011static int
17012prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17013{
17014 struct attribute *attr;
17015
17016 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17017 if (attr && (DW_UNSND (attr) != 0))
17018 return 1;
17019
17020 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17021 is only meaningful for C, but the concept also extends to other
4d804846
JB
17022 languages that allow unprototyped functions (Eg: Objective C).
17023 For all other languages, assume that functions are always
17024 prototyped. */
17025 if (cu->language != language_c
17026 && cu->language != language_objc
17027 && cu->language != language_opencl)
17028 return 1;
17029
17030 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17031 prototyped and unprototyped functions; default to prototyped,
17032 since that is more common in modern code (and RealView warns
17033 about unprototyped functions). */
17034 if (producer_is_realview (cu->producer))
17035 return 1;
17036
17037 return 0;
17038}
17039
c906108c
SS
17040/* Handle DIES due to C code like:
17041
17042 struct foo
c5aa993b
JM
17043 {
17044 int (*funcp)(int a, long l);
17045 int b;
17046 };
c906108c 17047
0963b4bd 17048 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17049
f792889a 17050static struct type *
e7c27a73 17051read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17052{
518817b3 17053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17054 struct type *type; /* Type that this function returns. */
17055 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17056 struct attribute *attr;
17057
e7c27a73 17058 type = die_type (die, cu);
7e314c57
JK
17059
17060 /* The die_type call above may have already set the type for this DIE. */
17061 ftype = get_die_type (die, cu);
17062 if (ftype)
17063 return ftype;
17064
0c8b41f1 17065 ftype = lookup_function_type (type);
c906108c 17066
4d804846 17067 if (prototyped_function_p (die, cu))
a6c727b2 17068 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17069
c055b101
CV
17070 /* Store the calling convention in the type if it's available in
17071 the subroutine die. Otherwise set the calling convention to
17072 the default value DW_CC_normal. */
17073 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17074 if (attr != nullptr
17075 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17076 TYPE_CALLING_CONVENTION (ftype)
17077 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17078 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17079 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17080 else
17081 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17082
743649fd
MW
17083 /* Record whether the function returns normally to its caller or not
17084 if the DWARF producer set that information. */
17085 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17086 if (attr && (DW_UNSND (attr) != 0))
17087 TYPE_NO_RETURN (ftype) = 1;
17088
76c10ea2
GM
17089 /* We need to add the subroutine type to the die immediately so
17090 we don't infinitely recurse when dealing with parameters
0963b4bd 17091 declared as the same subroutine type. */
76c10ea2 17092 set_die_type (die, ftype, cu);
6e70227d 17093
639d11d3 17094 if (die->child != NULL)
c906108c 17095 {
bb5ed363 17096 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17097 struct die_info *child_die;
8072405b 17098 int nparams, iparams;
c906108c
SS
17099
17100 /* Count the number of parameters.
17101 FIXME: GDB currently ignores vararg functions, but knows about
17102 vararg member functions. */
8072405b 17103 nparams = 0;
639d11d3 17104 child_die = die->child;
c906108c
SS
17105 while (child_die && child_die->tag)
17106 {
17107 if (child_die->tag == DW_TAG_formal_parameter)
17108 nparams++;
17109 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17110 TYPE_VARARGS (ftype) = 1;
436c571c 17111 child_die = child_die->sibling;
c906108c
SS
17112 }
17113
17114 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17115 ftype->set_num_fields (nparams);
3cabb6b0
SM
17116 ftype->set_fields
17117 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17118
8072405b
JK
17119 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17120 even if we error out during the parameters reading below. */
17121 for (iparams = 0; iparams < nparams; iparams++)
17122 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17123
17124 iparams = 0;
639d11d3 17125 child_die = die->child;
c906108c
SS
17126 while (child_die && child_die->tag)
17127 {
17128 if (child_die->tag == DW_TAG_formal_parameter)
17129 {
3ce3b1ba
PA
17130 struct type *arg_type;
17131
17132 /* DWARF version 2 has no clean way to discern C++
17133 static and non-static member functions. G++ helps
17134 GDB by marking the first parameter for non-static
17135 member functions (which is the this pointer) as
17136 artificial. We pass this information to
17137 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17138
17139 DWARF version 3 added DW_AT_object_pointer, which GCC
17140 4.5 does not yet generate. */
e142c38c 17141 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17142 if (attr != nullptr)
c906108c
SS
17143 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17144 else
9c37b5ae 17145 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17146 arg_type = die_type (child_die, cu);
17147
17148 /* RealView does not mark THIS as const, which the testsuite
17149 expects. GCC marks THIS as const in method definitions,
17150 but not in the class specifications (GCC PR 43053). */
17151 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17152 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17153 {
17154 int is_this = 0;
17155 struct dwarf2_cu *arg_cu = cu;
17156 const char *name = dwarf2_name (child_die, cu);
17157
17158 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17159 if (attr != nullptr)
3ce3b1ba
PA
17160 {
17161 /* If the compiler emits this, use it. */
17162 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17163 is_this = 1;
17164 }
17165 else if (name && strcmp (name, "this") == 0)
17166 /* Function definitions will have the argument names. */
17167 is_this = 1;
17168 else if (name == NULL && iparams == 0)
17169 /* Declarations may not have the names, so like
17170 elsewhere in GDB, assume an artificial first
17171 argument is "this". */
17172 is_this = 1;
17173
17174 if (is_this)
17175 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17176 arg_type, 0);
17177 }
17178
17179 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17180 iparams++;
17181 }
436c571c 17182 child_die = child_die->sibling;
c906108c
SS
17183 }
17184 }
17185
76c10ea2 17186 return ftype;
c906108c
SS
17187}
17188
f792889a 17189static struct type *
e7c27a73 17190read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17191{
518817b3 17192 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17193 const char *name = NULL;
3c8e0968 17194 struct type *this_type, *target_type;
c906108c 17195
94af9270 17196 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17197 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17198 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17199 set_die_type (die, this_type, cu);
3c8e0968
DE
17200 target_type = die_type (die, cu);
17201 if (target_type != this_type)
17202 TYPE_TARGET_TYPE (this_type) = target_type;
17203 else
17204 {
17205 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17206 spec and cause infinite loops in GDB. */
b98664d3 17207 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17208 "- DIE at %s [in module %s]"),
17209 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17210 TYPE_TARGET_TYPE (this_type) = NULL;
17211 }
e4003a34
TV
17212 if (name == NULL)
17213 {
17214 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17215 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17216 Handle these by just returning the target type, rather than
17217 constructing an anonymous typedef type and trying to handle this
17218 elsewhere. */
17219 set_die_type (die, target_type, cu);
17220 return target_type;
17221 }
f792889a 17222 return this_type;
c906108c
SS
17223}
17224
9b790ce7
UW
17225/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17226 (which may be different from NAME) to the architecture back-end to allow
17227 it to guess the correct format if necessary. */
17228
17229static struct type *
17230dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17231 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17232{
08feed99 17233 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17234 const struct floatformat **format;
17235 struct type *type;
17236
17237 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17238 if (format)
103a685e 17239 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17240 else
77b7c781 17241 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17242
17243 return type;
17244}
17245
eb77c9df
AB
17246/* Allocate an integer type of size BITS and name NAME. */
17247
17248static struct type *
17249dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17250 int bits, int unsigned_p, const char *name)
17251{
17252 struct type *type;
17253
17254 /* Versions of Intel's C Compiler generate an integer type called "void"
17255 instead of using DW_TAG_unspecified_type. This has been seen on
17256 at least versions 14, 17, and 18. */
35ee2dc2
AB
17257 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17258 && strcmp (name, "void") == 0)
eb77c9df
AB
17259 type = objfile_type (objfile)->builtin_void;
17260 else
17261 type = init_integer_type (objfile, bits, unsigned_p, name);
17262
17263 return type;
17264}
17265
8bdc1658
AB
17266/* Initialise and return a floating point type of size BITS suitable for
17267 use as a component of a complex number. The NAME_HINT is passed through
17268 when initialising the floating point type and is the name of the complex
17269 type.
17270
17271 As DWARF doesn't currently provide an explicit name for the components
17272 of a complex number, but it can be helpful to have these components
17273 named, we try to select a suitable name based on the size of the
17274 component. */
17275static struct type *
17276dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17277 struct objfile *objfile,
103a685e
TT
17278 int bits, const char *name_hint,
17279 enum bfd_endian byte_order)
8bdc1658 17280{
08feed99 17281 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17282 struct type *tt = nullptr;
17283
35add35e
AB
17284 /* Try to find a suitable floating point builtin type of size BITS.
17285 We're going to use the name of this type as the name for the complex
17286 target type that we are about to create. */
1db455a7 17287 switch (cu->language)
8bdc1658 17288 {
1db455a7
AB
17289 case language_fortran:
17290 switch (bits)
17291 {
17292 case 32:
17293 tt = builtin_f_type (gdbarch)->builtin_real;
17294 break;
17295 case 64:
17296 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17297 break;
17298 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17299 case 128:
17300 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17301 break;
17302 }
8bdc1658 17303 break;
1db455a7
AB
17304 default:
17305 switch (bits)
17306 {
17307 case 32:
17308 tt = builtin_type (gdbarch)->builtin_float;
17309 break;
17310 case 64:
17311 tt = builtin_type (gdbarch)->builtin_double;
17312 break;
17313 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17314 case 128:
17315 tt = builtin_type (gdbarch)->builtin_long_double;
17316 break;
17317 }
8bdc1658
AB
17318 break;
17319 }
17320
35add35e
AB
17321 /* If the type we found doesn't match the size we were looking for, then
17322 pretend we didn't find a type at all, the complex target type we
17323 create will then be nameless. */
a12e5744 17324 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17325 tt = nullptr;
17326
7d93a1e0 17327 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17328 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17329}
17330
c906108c
SS
17331/* Find a representation of a given base type and install
17332 it in the TYPE field of the die. */
17333
f792889a 17334static struct type *
e7c27a73 17335read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17336{
518817b3 17337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17338 struct type *type;
17339 struct attribute *attr;
19f392bc 17340 int encoding = 0, bits = 0;
15d034d0 17341 const char *name;
34877895 17342 gdbarch *arch;
c906108c 17343
e142c38c 17344 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17345 if (attr != nullptr)
34877895 17346 encoding = DW_UNSND (attr);
e142c38c 17347 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17348 if (attr != nullptr)
34877895 17349 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17350 name = dwarf2_name (die, cu);
6ccb9162 17351 if (!name)
34877895 17352 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17353
08feed99 17354 arch = objfile->arch ();
103a685e
TT
17355 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17356
34877895
PJ
17357 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17358 if (attr)
103a685e
TT
17359 {
17360 int endianity = DW_UNSND (attr);
17361
17362 switch (endianity)
17363 {
17364 case DW_END_big:
17365 byte_order = BFD_ENDIAN_BIG;
17366 break;
17367 case DW_END_little:
17368 byte_order = BFD_ENDIAN_LITTLE;
17369 break;
17370 default:
17371 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17372 break;
17373 }
17374 }
6ccb9162
UW
17375
17376 switch (encoding)
c906108c 17377 {
6ccb9162
UW
17378 case DW_ATE_address:
17379 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17380 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17381 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17382 break;
17383 case DW_ATE_boolean:
19f392bc 17384 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17385 break;
17386 case DW_ATE_complex_float:
103a685e
TT
17387 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17388 byte_order);
78134374 17389 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17390 {
17391 if (name == nullptr)
17392 {
17393 struct obstack *obstack
17394 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
7d93a1e0 17395 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17396 nullptr);
17397 }
17398 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17399 }
17400 else
17401 type = init_complex_type (name, type);
6ccb9162
UW
17402 break;
17403 case DW_ATE_decimal_float:
19f392bc 17404 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17405 break;
17406 case DW_ATE_float:
103a685e 17407 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17408 break;
17409 case DW_ATE_signed:
eb77c9df 17410 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17411 break;
17412 case DW_ATE_unsigned:
3b2b8fea
TT
17413 if (cu->language == language_fortran
17414 && name
61012eef 17415 && startswith (name, "character("))
19f392bc
UW
17416 type = init_character_type (objfile, bits, 1, name);
17417 else
eb77c9df 17418 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17419 break;
17420 case DW_ATE_signed_char:
6e70227d 17421 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17422 || cu->language == language_pascal
17423 || cu->language == language_fortran)
19f392bc
UW
17424 type = init_character_type (objfile, bits, 0, name);
17425 else
eb77c9df 17426 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17427 break;
17428 case DW_ATE_unsigned_char:
868a0084 17429 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17430 || cu->language == language_pascal
c44af4eb
TT
17431 || cu->language == language_fortran
17432 || cu->language == language_rust)
19f392bc
UW
17433 type = init_character_type (objfile, bits, 1, name);
17434 else
eb77c9df 17435 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17436 break;
75079b2b 17437 case DW_ATE_UTF:
53e710ac 17438 {
53e710ac
PA
17439 if (bits == 16)
17440 type = builtin_type (arch)->builtin_char16;
17441 else if (bits == 32)
17442 type = builtin_type (arch)->builtin_char32;
17443 else
17444 {
b98664d3 17445 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17446 bits);
eb77c9df 17447 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17448 }
17449 return set_die_type (die, type, cu);
17450 }
75079b2b
TT
17451 break;
17452
6ccb9162 17453 default:
b98664d3 17454 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17455 dwarf_type_encoding_name (encoding));
77b7c781 17456 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17457 break;
c906108c 17458 }
6ccb9162 17459
0114d602 17460 if (name && strcmp (name, "char") == 0)
876cecd0 17461 TYPE_NOSIGN (type) = 1;
0114d602 17462
2b4424c3
TT
17463 maybe_set_alignment (cu, die, type);
17464
103a685e 17465 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17466
f792889a 17467 return set_die_type (die, type, cu);
c906108c
SS
17468}
17469
80180f79
SA
17470/* Parse dwarf attribute if it's a block, reference or constant and put the
17471 resulting value of the attribute into struct bound_prop.
17472 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17473
17474static int
17475attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17476 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17477 struct type *default_type)
80180f79
SA
17478{
17479 struct dwarf2_property_baton *baton;
a50264ba
TT
17480 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
17481 struct objfile *objfile = per_objfile->objfile;
17482 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17483
9a49df9d
AB
17484 gdb_assert (default_type != NULL);
17485
80180f79
SA
17486 if (attr == NULL || prop == NULL)
17487 return 0;
17488
4fc6c0d5 17489 if (attr->form_is_block ())
80180f79 17490 {
8d749320 17491 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17492 baton->property_type = default_type;
80180f79 17493 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17494 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17495 baton->locexpr.size = DW_BLOCK (attr)->size;
17496 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17497 switch (attr->name)
17498 {
17499 case DW_AT_string_length:
17500 baton->locexpr.is_reference = true;
17501 break;
17502 default:
17503 baton->locexpr.is_reference = false;
17504 break;
17505 }
80180f79
SA
17506 prop->data.baton = baton;
17507 prop->kind = PROP_LOCEXPR;
17508 gdb_assert (prop->data.baton != NULL);
17509 }
cd6c91b4 17510 else if (attr->form_is_ref ())
80180f79
SA
17511 {
17512 struct dwarf2_cu *target_cu = cu;
17513 struct die_info *target_die;
17514 struct attribute *target_attr;
17515
17516 target_die = follow_die_ref (die, attr, &target_cu);
17517 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17518 if (target_attr == NULL)
17519 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17520 target_cu);
80180f79
SA
17521 if (target_attr == NULL)
17522 return 0;
17523
df25ebbd 17524 switch (target_attr->name)
80180f79 17525 {
df25ebbd 17526 case DW_AT_location:
cd6c91b4 17527 if (target_attr->form_is_section_offset ())
df25ebbd 17528 {
8d749320 17529 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17530 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17531 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17532 prop->data.baton = baton;
17533 prop->kind = PROP_LOCLIST;
17534 gdb_assert (prop->data.baton != NULL);
17535 }
4fc6c0d5 17536 else if (target_attr->form_is_block ())
df25ebbd 17537 {
8d749320 17538 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17539 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17540 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17541 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17542 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17543 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17544 baton->locexpr.is_reference = true;
df25ebbd
JB
17545 prop->data.baton = baton;
17546 prop->kind = PROP_LOCEXPR;
17547 gdb_assert (prop->data.baton != NULL);
17548 }
17549 else
17550 {
17551 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17552 "dynamic property");
17553 return 0;
17554 }
17555 break;
17556 case DW_AT_data_member_location:
17557 {
17558 LONGEST offset;
17559
17560 if (!handle_data_member_location (target_die, target_cu,
17561 &offset))
17562 return 0;
17563
8d749320 17564 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17565 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17566 target_cu);
df25ebbd
JB
17567 baton->offset_info.offset = offset;
17568 baton->offset_info.type = die_type (target_die, target_cu);
17569 prop->data.baton = baton;
17570 prop->kind = PROP_ADDR_OFFSET;
17571 break;
17572 }
80180f79
SA
17573 }
17574 }
cd6c91b4 17575 else if (attr->form_is_constant ())
80180f79 17576 {
0826b30a 17577 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17578 prop->kind = PROP_CONST;
17579 }
17580 else
17581 {
17582 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17583 dwarf2_name (die, cu));
17584 return 0;
17585 }
17586
17587 return 1;
17588}
17589
09ba997f 17590/* See read.h. */
9a49df9d 17591
09ba997f
TT
17592struct type *
17593dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17594{
09ba997f 17595 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17596 struct type *int_type;
17597
17598 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17599#define TRY_TYPE(F) \
17600 int_type = (unsigned_p \
17601 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17602 : objfile_type (objfile)->builtin_ ## F); \
17603 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17604 return int_type
17605
17606 TRY_TYPE (char);
17607 TRY_TYPE (short);
17608 TRY_TYPE (int);
17609 TRY_TYPE (long);
17610 TRY_TYPE (long_long);
17611
17612#undef TRY_TYPE
17613
17614 gdb_assert_not_reached ("unable to find suitable integer type");
17615}
17616
09ba997f 17617/* See read.h. */
11a8b164 17618
09ba997f
TT
17619struct type *
17620dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17621{
09ba997f
TT
17622 int addr_size = this->addr_size ();
17623 return int_type (addr_size, unsigned_p);
11a8b164
AB
17624}
17625
b86352cf
AB
17626/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17627 present (which is valid) then compute the default type based on the
17628 compilation units address size. */
17629
17630static struct type *
17631read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17632{
17633 struct type *index_type = die_type (die, cu);
17634
17635 /* Dwarf-2 specifications explicitly allows to create subrange types
17636 without specifying a base type.
17637 In that case, the base type must be set to the type of
17638 the lower bound, upper bound or count, in that order, if any of these
17639 three attributes references an object that has a type.
17640 If no base type is found, the Dwarf-2 specifications say that
17641 a signed integer type of size equal to the size of an address should
17642 be used.
17643 For the following C code: `extern char gdb_int [];'
17644 GCC produces an empty range DIE.
17645 FIXME: muller/2010-05-28: Possible references to object for low bound,
17646 high bound or count are not yet handled by this code. */
78134374 17647 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17648 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17649
17650 return index_type;
17651}
17652
a02abb62
JB
17653/* Read the given DW_AT_subrange DIE. */
17654
f792889a 17655static struct type *
a02abb62
JB
17656read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17657{
4c9ad8c2 17658 struct type *base_type, *orig_base_type;
a02abb62
JB
17659 struct type *range_type;
17660 struct attribute *attr;
729efb13 17661 struct dynamic_prop low, high;
4fae6e18 17662 int low_default_is_valid;
c451ebe5 17663 int high_bound_is_count = 0;
15d034d0 17664 const char *name;
d359392f 17665 ULONGEST negative_mask;
e77813c8 17666
b86352cf
AB
17667 orig_base_type = read_subrange_index_type (die, cu);
17668
4c9ad8c2
TT
17669 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17670 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17671 creating the range type, but we use the result of check_typedef
17672 when examining properties of the type. */
17673 base_type = check_typedef (orig_base_type);
a02abb62 17674
7e314c57
JK
17675 /* The die_type call above may have already set the type for this DIE. */
17676 range_type = get_die_type (die, cu);
17677 if (range_type)
17678 return range_type;
17679
729efb13
SA
17680 low.kind = PROP_CONST;
17681 high.kind = PROP_CONST;
17682 high.data.const_val = 0;
17683
4fae6e18
JK
17684 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17685 omitting DW_AT_lower_bound. */
17686 switch (cu->language)
6e70227d 17687 {
4fae6e18
JK
17688 case language_c:
17689 case language_cplus:
729efb13 17690 low.data.const_val = 0;
4fae6e18
JK
17691 low_default_is_valid = 1;
17692 break;
17693 case language_fortran:
729efb13 17694 low.data.const_val = 1;
4fae6e18
JK
17695 low_default_is_valid = 1;
17696 break;
17697 case language_d:
4fae6e18 17698 case language_objc:
c44af4eb 17699 case language_rust:
729efb13 17700 low.data.const_val = 0;
4fae6e18
JK
17701 low_default_is_valid = (cu->header.version >= 4);
17702 break;
17703 case language_ada:
17704 case language_m2:
17705 case language_pascal:
729efb13 17706 low.data.const_val = 1;
4fae6e18
JK
17707 low_default_is_valid = (cu->header.version >= 4);
17708 break;
17709 default:
729efb13 17710 low.data.const_val = 0;
4fae6e18
JK
17711 low_default_is_valid = 0;
17712 break;
a02abb62
JB
17713 }
17714
e142c38c 17715 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17716 if (attr != nullptr)
9a49df9d 17717 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17718 else if (!low_default_is_valid)
b98664d3 17719 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17720 "- DIE at %s [in module %s]"),
17721 sect_offset_str (die->sect_off),
518817b3 17722 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17723
506f5c41
TV
17724 struct attribute *attr_ub, *attr_count;
17725 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17726 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17727 {
506f5c41 17728 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17729 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17730 {
c451ebe5
SA
17731 /* If bounds are constant do the final calculation here. */
17732 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17733 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17734 else
17735 high_bound_is_count = 1;
c2ff108b 17736 }
506f5c41
TV
17737 else
17738 {
17739 if (attr_ub != NULL)
17740 complaint (_("Unresolved DW_AT_upper_bound "
17741 "- DIE at %s [in module %s]"),
17742 sect_offset_str (die->sect_off),
17743 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17744 if (attr_count != NULL)
17745 complaint (_("Unresolved DW_AT_count "
17746 "- DIE at %s [in module %s]"),
17747 sect_offset_str (die->sect_off),
17748 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17749 }
e77813c8 17750 }
a02abb62 17751
4e962e74
TT
17752 LONGEST bias = 0;
17753 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17754 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17755 bias = bias_attr->constant_value (0);
4e962e74 17756
dbb9c2b1
JB
17757 /* Normally, the DWARF producers are expected to use a signed
17758 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17759 But this is unfortunately not always the case, as witnessed
17760 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17761 is used instead. To work around that ambiguity, we treat
17762 the bounds as signed, and thus sign-extend their values, when
17763 the base type is signed. */
6e70227d 17764 negative_mask =
d359392f 17765 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17766 if (low.kind == PROP_CONST
17767 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17768 low.data.const_val |= negative_mask;
17769 if (high.kind == PROP_CONST
17770 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17771 high.data.const_val |= negative_mask;
43bbcdc2 17772
5bbd8269
AB
17773 /* Check for bit and byte strides. */
17774 struct dynamic_prop byte_stride_prop;
17775 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17776 if (attr_byte_stride != nullptr)
17777 {
09ba997f 17778 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17779 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17780 prop_type);
17781 }
17782
17783 struct dynamic_prop bit_stride_prop;
17784 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17785 if (attr_bit_stride != nullptr)
17786 {
17787 /* It only makes sense to have either a bit or byte stride. */
17788 if (attr_byte_stride != nullptr)
17789 {
17790 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17791 "- DIE at %s [in module %s]"),
17792 sect_offset_str (die->sect_off),
17793 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17794 attr_bit_stride = nullptr;
17795 }
17796 else
17797 {
09ba997f 17798 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17799 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17800 prop_type);
17801 }
17802 }
17803
17804 if (attr_byte_stride != nullptr
17805 || attr_bit_stride != nullptr)
17806 {
17807 bool byte_stride_p = (attr_byte_stride != nullptr);
17808 struct dynamic_prop *stride
17809 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17810
17811 range_type
17812 = create_range_type_with_stride (NULL, orig_base_type, &low,
17813 &high, bias, stride, byte_stride_p);
17814 }
17815 else
17816 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17817
c451ebe5
SA
17818 if (high_bound_is_count)
17819 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17820
c2ff108b
JK
17821 /* Ada expects an empty array on no boundary attributes. */
17822 if (attr == NULL && cu->language != language_ada)
729efb13 17823 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17824
39cbfefa
DJ
17825 name = dwarf2_name (die, cu);
17826 if (name)
d0e39ea2 17827 range_type->set_name (name);
6e70227d 17828
e142c38c 17829 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17830 if (attr != nullptr)
a02abb62
JB
17831 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17832
2b4424c3
TT
17833 maybe_set_alignment (cu, die, range_type);
17834
7e314c57
JK
17835 set_die_type (die, range_type, cu);
17836
17837 /* set_die_type should be already done. */
b4ba55a1
JB
17838 set_descriptive_type (range_type, die, cu);
17839
7e314c57 17840 return range_type;
a02abb62 17841}
6e70227d 17842
f792889a 17843static struct type *
81a17f79
JB
17844read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17845{
17846 struct type *type;
81a17f79 17847
518817b3
SM
17848 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17849 NULL);
d0e39ea2 17850 type->set_name (dwarf2_name (die, cu));
81a17f79 17851
74a2f8ff 17852 /* In Ada, an unspecified type is typically used when the description
85102364 17853 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17854 such a type, we treat it as a stub, and try to resolve it later on,
17855 when needed. */
17856 if (cu->language == language_ada)
17857 TYPE_STUB (type) = 1;
17858
f792889a 17859 return set_die_type (die, type, cu);
81a17f79 17860}
a02abb62 17861
639d11d3
DC
17862/* Read a single die and all its descendents. Set the die's sibling
17863 field to NULL; set other fields in the die correctly, and set all
17864 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17865 location of the info_ptr after reading all of those dies. PARENT
17866 is the parent of the die in question. */
17867
17868static struct die_info *
dee91e82 17869read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17870 const gdb_byte *info_ptr,
17871 const gdb_byte **new_info_ptr,
dee91e82 17872 struct die_info *parent)
639d11d3
DC
17873{
17874 struct die_info *die;
d521ce57 17875 const gdb_byte *cur_ptr;
639d11d3 17876
3e225074 17877 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17878 if (die == NULL)
17879 {
17880 *new_info_ptr = cur_ptr;
17881 return NULL;
17882 }
93311388 17883 store_in_ref_table (die, reader->cu);
639d11d3 17884
3e225074 17885 if (die->has_children)
bf6af496 17886 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17887 else
17888 {
17889 die->child = NULL;
17890 *new_info_ptr = cur_ptr;
17891 }
17892
17893 die->sibling = NULL;
17894 die->parent = parent;
17895 return die;
17896}
17897
17898/* Read a die, all of its descendents, and all of its siblings; set
17899 all of the fields of all of the dies correctly. Arguments are as
17900 in read_die_and_children. */
17901
17902static struct die_info *
bf6af496 17903read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17904 const gdb_byte *info_ptr,
17905 const gdb_byte **new_info_ptr,
bf6af496 17906 struct die_info *parent)
639d11d3
DC
17907{
17908 struct die_info *first_die, *last_sibling;
d521ce57 17909 const gdb_byte *cur_ptr;
639d11d3 17910
c906108c 17911 cur_ptr = info_ptr;
639d11d3
DC
17912 first_die = last_sibling = NULL;
17913
17914 while (1)
c906108c 17915 {
639d11d3 17916 struct die_info *die
dee91e82 17917 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17918
1d325ec1 17919 if (die == NULL)
c906108c 17920 {
639d11d3
DC
17921 *new_info_ptr = cur_ptr;
17922 return first_die;
c906108c 17923 }
1d325ec1
DJ
17924
17925 if (!first_die)
17926 first_die = die;
c906108c 17927 else
1d325ec1
DJ
17928 last_sibling->sibling = die;
17929
17930 last_sibling = die;
c906108c 17931 }
c906108c
SS
17932}
17933
bf6af496
DE
17934/* Read a die, all of its descendents, and all of its siblings; set
17935 all of the fields of all of the dies correctly. Arguments are as
17936 in read_die_and_children.
17937 This the main entry point for reading a DIE and all its children. */
17938
17939static struct die_info *
17940read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17941 const gdb_byte *info_ptr,
17942 const gdb_byte **new_info_ptr,
bf6af496
DE
17943 struct die_info *parent)
17944{
17945 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17946 new_info_ptr, parent);
17947
b4f54984 17948 if (dwarf_die_debug)
bf6af496
DE
17949 {
17950 fprintf_unfiltered (gdb_stdlog,
17951 "Read die from %s@0x%x of %s:\n",
96b79293 17952 reader->die_section->get_name (),
bf6af496
DE
17953 (unsigned) (info_ptr - reader->die_section->buffer),
17954 bfd_get_filename (reader->abfd));
b4f54984 17955 dump_die (die, dwarf_die_debug);
bf6af496
DE
17956 }
17957
17958 return die;
17959}
17960
3019eac3
DE
17961/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17962 attributes.
17963 The caller is responsible for filling in the extra attributes
17964 and updating (*DIEP)->num_attrs.
17965 Set DIEP to point to a newly allocated die with its information,
3e225074 17966 except for its child, sibling, and parent fields. */
93311388 17967
d521ce57 17968static const gdb_byte *
3019eac3 17969read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17970 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17971 int num_extra_attrs)
93311388 17972{
b64f50a1 17973 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17974 struct abbrev_info *abbrev;
17975 struct die_info *die;
17976 struct dwarf2_cu *cu = reader->cu;
17977 bfd *abfd = reader->abfd;
17978
9c541725 17979 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17980 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17981 info_ptr += bytes_read;
17982 if (!abbrev_number)
17983 {
17984 *diep = NULL;
93311388
DE
17985 return info_ptr;
17986 }
17987
685af9cd 17988 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17989 if (!abbrev)
348e048f
DE
17990 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17991 abbrev_number,
17992 bfd_get_filename (abfd));
17993
3019eac3 17994 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17995 die->sect_off = sect_off;
93311388
DE
17996 die->tag = abbrev->tag;
17997 die->abbrev = abbrev_number;
3e225074 17998 die->has_children = abbrev->has_children;
93311388 17999
3019eac3
DE
18000 /* Make the result usable.
18001 The caller needs to update num_attrs after adding the extra
18002 attributes. */
93311388
DE
18003 die->num_attrs = abbrev->num_attrs;
18004
18a8505e 18005 std::vector<int> indexes_that_need_reprocess;
93311388 18006 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18007 {
18008 bool need_reprocess;
18009 info_ptr =
18010 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18011 info_ptr, &need_reprocess);
18012 if (need_reprocess)
18013 indexes_that_need_reprocess.push_back (i);
18014 }
18015
052c8bb8 18016 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18017 if (attr != nullptr)
18018 cu->str_offsets_base = DW_UNSND (attr);
93311388 18019
41144253 18020 attr = die->attr (DW_AT_loclists_base);
18021 if (attr != nullptr)
18022 cu->loclist_base = DW_UNSND (attr);
18023
a39fdb41 18024 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18025 if (maybe_addr_base.has_value ())
18026 cu->addr_base = *maybe_addr_base;
18027 for (int index : indexes_that_need_reprocess)
18028 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18029 *diep = die;
93311388
DE
18030 return info_ptr;
18031}
18032
3019eac3
DE
18033/* Read a die and all its attributes.
18034 Set DIEP to point to a newly allocated die with its information,
3e225074 18035 except for its child, sibling, and parent fields. */
3019eac3 18036
d521ce57 18037static const gdb_byte *
3019eac3 18038read_full_die (const struct die_reader_specs *reader,
3e225074 18039 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18040{
d521ce57 18041 const gdb_byte *result;
bf6af496 18042
3e225074 18043 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18044
b4f54984 18045 if (dwarf_die_debug)
bf6af496
DE
18046 {
18047 fprintf_unfiltered (gdb_stdlog,
18048 "Read die from %s@0x%x of %s:\n",
96b79293 18049 reader->die_section->get_name (),
bf6af496
DE
18050 (unsigned) (info_ptr - reader->die_section->buffer),
18051 bfd_get_filename (reader->abfd));
b4f54984 18052 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18053 }
18054
18055 return result;
3019eac3 18056}
433df2d4 18057\f
c906108c 18058
72bf9492
DJ
18059/* Returns nonzero if TAG represents a type that we might generate a partial
18060 symbol for. */
18061
18062static int
18063is_type_tag_for_partial (int tag)
18064{
18065 switch (tag)
18066 {
18067#if 0
18068 /* Some types that would be reasonable to generate partial symbols for,
18069 that we don't at present. */
18070 case DW_TAG_array_type:
18071 case DW_TAG_file_type:
18072 case DW_TAG_ptr_to_member_type:
18073 case DW_TAG_set_type:
18074 case DW_TAG_string_type:
18075 case DW_TAG_subroutine_type:
18076#endif
18077 case DW_TAG_base_type:
18078 case DW_TAG_class_type:
680b30c7 18079 case DW_TAG_interface_type:
72bf9492
DJ
18080 case DW_TAG_enumeration_type:
18081 case DW_TAG_structure_type:
18082 case DW_TAG_subrange_type:
18083 case DW_TAG_typedef:
18084 case DW_TAG_union_type:
18085 return 1;
18086 default:
18087 return 0;
18088 }
18089}
18090
18091/* Load all DIEs that are interesting for partial symbols into memory. */
18092
18093static struct partial_die_info *
dee91e82 18094load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18095 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18096{
dee91e82 18097 struct dwarf2_cu *cu = reader->cu;
518817b3 18098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18099 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18100 unsigned int bytes_read;
5afb4e99 18101 unsigned int load_all = 0;
72bf9492
DJ
18102 int nesting_level = 1;
18103
18104 parent_die = NULL;
18105 last_die = NULL;
18106
7adf1e79
DE
18107 gdb_assert (cu->per_cu != NULL);
18108 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18109 load_all = 1;
18110
72bf9492
DJ
18111 cu->partial_dies
18112 = htab_create_alloc_ex (cu->header.length / 12,
18113 partial_die_hash,
18114 partial_die_eq,
18115 NULL,
18116 &cu->comp_unit_obstack,
18117 hashtab_obstack_allocate,
18118 dummy_obstack_deallocate);
18119
72bf9492
DJ
18120 while (1)
18121 {
685af9cd 18122 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18123
18124 /* A NULL abbrev means the end of a series of children. */
18125 if (abbrev == NULL)
18126 {
18127 if (--nesting_level == 0)
cd9983dd
YQ
18128 return first_die;
18129
72bf9492
DJ
18130 info_ptr += bytes_read;
18131 last_die = parent_die;
18132 parent_die = parent_die->die_parent;
18133 continue;
18134 }
18135
98bfdba5
PA
18136 /* Check for template arguments. We never save these; if
18137 they're seen, we just mark the parent, and go on our way. */
18138 if (parent_die != NULL
18139 && cu->language == language_cplus
18140 && (abbrev->tag == DW_TAG_template_type_param
18141 || abbrev->tag == DW_TAG_template_value_param))
18142 {
18143 parent_die->has_template_arguments = 1;
18144
18145 if (!load_all)
18146 {
18147 /* We don't need a partial DIE for the template argument. */
dee91e82 18148 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18149 continue;
18150 }
18151 }
18152
0d99eb77 18153 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18154 Skip their other children. */
18155 if (!load_all
18156 && cu->language == language_cplus
18157 && parent_die != NULL
18158 && parent_die->tag == DW_TAG_subprogram)
18159 {
dee91e82 18160 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18161 continue;
18162 }
18163
5afb4e99
DJ
18164 /* Check whether this DIE is interesting enough to save. Normally
18165 we would not be interested in members here, but there may be
18166 later variables referencing them via DW_AT_specification (for
18167 static members). */
18168 if (!load_all
18169 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18170 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18171 && abbrev->tag != DW_TAG_enumerator
18172 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18173 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18174 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18175 && abbrev->tag != DW_TAG_variable
5afb4e99 18176 && abbrev->tag != DW_TAG_namespace
f55ee35c 18177 && abbrev->tag != DW_TAG_module
95554aad 18178 && abbrev->tag != DW_TAG_member
74921315
KS
18179 && abbrev->tag != DW_TAG_imported_unit
18180 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18181 {
18182 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18183 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18184 continue;
18185 }
18186
6f06d47b
YQ
18187 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18188 abbrev);
cd9983dd 18189
48fbe735 18190 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18191
18192 /* This two-pass algorithm for processing partial symbols has a
18193 high cost in cache pressure. Thus, handle some simple cases
18194 here which cover the majority of C partial symbols. DIEs
18195 which neither have specification tags in them, nor could have
18196 specification tags elsewhere pointing at them, can simply be
18197 processed and discarded.
18198
18199 This segment is also optional; scan_partial_symbols and
18200 add_partial_symbol will handle these DIEs if we chain
18201 them in normally. When compilers which do not emit large
18202 quantities of duplicate debug information are more common,
18203 this code can probably be removed. */
18204
18205 /* Any complete simple types at the top level (pretty much all
18206 of them, for a language without namespaces), can be processed
18207 directly. */
18208 if (parent_die == NULL
cd9983dd
YQ
18209 && pdi.has_specification == 0
18210 && pdi.is_declaration == 0
18211 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18212 || pdi.tag == DW_TAG_base_type
18213 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18214 {
cd9983dd 18215 if (building_psymtab && pdi.name != NULL)
31edb802 18216 add_psymbol_to_list (pdi.name, false,
79748972 18217 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18218 psymbol_placement::STATIC,
1762568f 18219 0, cu->language, objfile);
cd9983dd 18220 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18221 continue;
18222 }
18223
d8228535
JK
18224 /* The exception for DW_TAG_typedef with has_children above is
18225 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18226 type_name_or_error will error on such types later.
d8228535
JK
18227
18228 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18229 it could not find the child DIEs referenced later, this is checked
18230 above. In correct DWARF DW_TAG_typedef should have no children. */
18231
cd9983dd 18232 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18233 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18234 "- DIE at %s [in module %s]"),
cd9983dd 18235 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18236
72bf9492
DJ
18237 /* If we're at the second level, and we're an enumerator, and
18238 our parent has no specification (meaning possibly lives in a
18239 namespace elsewhere), then we can add the partial symbol now
18240 instead of queueing it. */
cd9983dd 18241 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18242 && parent_die != NULL
18243 && parent_die->die_parent == NULL
18244 && parent_die->tag == DW_TAG_enumeration_type
18245 && parent_die->has_specification == 0)
18246 {
cd9983dd 18247 if (pdi.name == NULL)
b98664d3 18248 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18249 else if (building_psymtab)
31edb802 18250 add_psymbol_to_list (pdi.name, false,
79748972 18251 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18252 cu->language == language_cplus
75aedd27
TT
18253 ? psymbol_placement::GLOBAL
18254 : psymbol_placement::STATIC,
1762568f 18255 0, cu->language, objfile);
72bf9492 18256
cd9983dd 18257 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18258 continue;
18259 }
18260
cd9983dd 18261 struct partial_die_info *part_die
6f06d47b 18262 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18263
72bf9492
DJ
18264 /* We'll save this DIE so link it in. */
18265 part_die->die_parent = parent_die;
18266 part_die->die_sibling = NULL;
18267 part_die->die_child = NULL;
18268
18269 if (last_die && last_die == parent_die)
18270 last_die->die_child = part_die;
18271 else if (last_die)
18272 last_die->die_sibling = part_die;
18273
18274 last_die = part_die;
18275
18276 if (first_die == NULL)
18277 first_die = part_die;
18278
18279 /* Maybe add the DIE to the hash table. Not all DIEs that we
18280 find interesting need to be in the hash table, because we
18281 also have the parent/sibling/child chains; only those that we
18282 might refer to by offset later during partial symbol reading.
18283
18284 For now this means things that might have be the target of a
18285 DW_AT_specification, DW_AT_abstract_origin, or
18286 DW_AT_extension. DW_AT_extension will refer only to
18287 namespaces; DW_AT_abstract_origin refers to functions (and
18288 many things under the function DIE, but we do not recurse
18289 into function DIEs during partial symbol reading) and
18290 possibly variables as well; DW_AT_specification refers to
18291 declarations. Declarations ought to have the DW_AT_declaration
18292 flag. It happens that GCC forgets to put it in sometimes, but
18293 only for functions, not for types.
18294
18295 Adding more things than necessary to the hash table is harmless
18296 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18297 wasted time in find_partial_die, when we reread the compilation
18298 unit with load_all_dies set. */
72bf9492 18299
5afb4e99 18300 if (load_all
72929c62 18301 || abbrev->tag == DW_TAG_constant
5afb4e99 18302 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18303 || abbrev->tag == DW_TAG_variable
18304 || abbrev->tag == DW_TAG_namespace
18305 || part_die->is_declaration)
18306 {
18307 void **slot;
18308
18309 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18310 to_underlying (part_die->sect_off),
18311 INSERT);
72bf9492
DJ
18312 *slot = part_die;
18313 }
18314
72bf9492 18315 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18316 we have no reason to follow the children of structures; for other
98bfdba5
PA
18317 languages we have to, so that we can get at method physnames
18318 to infer fully qualified class names, for DW_AT_specification,
18319 and for C++ template arguments. For C++, we also look one level
18320 inside functions to find template arguments (if the name of the
18321 function does not already contain the template arguments).
bc30ff58 18322
0a4b0913
AB
18323 For Ada and Fortran, we need to scan the children of subprograms
18324 and lexical blocks as well because these languages allow the
18325 definition of nested entities that could be interesting for the
18326 debugger, such as nested subprograms for instance. */
72bf9492 18327 if (last_die->has_children
5afb4e99
DJ
18328 && (load_all
18329 || last_die->tag == DW_TAG_namespace
f55ee35c 18330 || last_die->tag == DW_TAG_module
72bf9492 18331 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18332 || (cu->language == language_cplus
18333 && last_die->tag == DW_TAG_subprogram
18334 && (last_die->name == NULL
18335 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18336 || (cu->language != language_c
18337 && (last_die->tag == DW_TAG_class_type
680b30c7 18338 || last_die->tag == DW_TAG_interface_type
72bf9492 18339 || last_die->tag == DW_TAG_structure_type
bc30ff58 18340 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18341 || ((cu->language == language_ada
18342 || cu->language == language_fortran)
bc30ff58
JB
18343 && (last_die->tag == DW_TAG_subprogram
18344 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18345 {
18346 nesting_level++;
18347 parent_die = last_die;
18348 continue;
18349 }
18350
18351 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18352 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18353
18354 /* Back to the top, do it again. */
18355 }
18356}
18357
6f06d47b
YQ
18358partial_die_info::partial_die_info (sect_offset sect_off_,
18359 struct abbrev_info *abbrev)
18360 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18361{
18362}
18363
35cc7ed7
YQ
18364/* Read a minimal amount of information into the minimal die structure.
18365 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18366
48fbe735
YQ
18367const gdb_byte *
18368partial_die_info::read (const struct die_reader_specs *reader,
18369 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18370{
dee91e82 18371 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18372 struct dwarf2_per_objfile *dwarf2_per_objfile
18373 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18374 unsigned int i;
c5aa993b 18375 int has_low_pc_attr = 0;
c906108c 18376 int has_high_pc_attr = 0;
91da1414 18377 int high_pc_relative = 0;
c906108c 18378
fd0a254f 18379 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18380 {
e7da7f8f 18381 attribute attr;
18a8505e 18382 bool need_reprocess;
e7da7f8f 18383 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18384 info_ptr, &need_reprocess);
18385 /* String and address offsets that need to do the reprocessing have
18386 already been read at this point, so there is no need to wait until
18387 the loop terminates to do the reprocessing. */
18388 if (need_reprocess)
e7da7f8f 18389 read_attribute_reprocess (reader, &attr);
c906108c 18390 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18391 partial symbol table. */
c906108c
SS
18392 switch (attr.name)
18393 {
18394 case DW_AT_name:
48fbe735 18395 switch (tag)
71c25dea
TT
18396 {
18397 case DW_TAG_compile_unit:
95554aad 18398 case DW_TAG_partial_unit:
348e048f 18399 case DW_TAG_type_unit:
71c25dea
TT
18400 /* Compilation units have a DW_AT_name that is a filename, not
18401 a source language identifier. */
18402 case DW_TAG_enumeration_type:
18403 case DW_TAG_enumerator:
18404 /* These tags always have simple identifiers already; no need
18405 to canonicalize them. */
48fbe735 18406 name = DW_STRING (&attr);
71c25dea
TT
18407 break;
18408 default:
48fbe735
YQ
18409 {
18410 struct objfile *objfile = dwarf2_per_objfile->objfile;
18411
18412 name
be1e3d3e 18413 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18414 }
71c25dea
TT
18415 break;
18416 }
c906108c 18417 break;
31ef98ae 18418 case DW_AT_linkage_name:
c906108c 18419 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18420 /* Note that both forms of linkage name might appear. We
18421 assume they will be the same, and we only store the last
18422 one we see. */
e61108c9 18423 linkage_name = attr.value_as_string ();
787de330
TT
18424 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18425 See https://github.com/rust-lang/rust/issues/32925. */
18426 if (cu->language == language_rust && linkage_name != NULL
18427 && strchr (linkage_name, '{') != NULL)
18428 linkage_name = NULL;
c906108c
SS
18429 break;
18430 case DW_AT_low_pc:
18431 has_low_pc_attr = 1;
cd6c91b4 18432 lowpc = attr.value_as_address ();
c906108c
SS
18433 break;
18434 case DW_AT_high_pc:
18435 has_high_pc_attr = 1;
cd6c91b4
TT
18436 highpc = attr.value_as_address ();
18437 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18438 high_pc_relative = 1;
c906108c
SS
18439 break;
18440 case DW_AT_location:
0963b4bd 18441 /* Support the .debug_loc offsets. */
4fc6c0d5 18442 if (attr.form_is_block ())
8e19ed76 18443 {
48fbe735 18444 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18445 }
cd6c91b4 18446 else if (attr.form_is_section_offset ())
8e19ed76 18447 {
4d3c2250 18448 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18449 }
18450 else
18451 {
4d3c2250
KB
18452 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18453 "partial symbol information");
8e19ed76 18454 }
c906108c 18455 break;
c906108c 18456 case DW_AT_external:
48fbe735 18457 is_external = DW_UNSND (&attr);
c906108c
SS
18458 break;
18459 case DW_AT_declaration:
48fbe735 18460 is_declaration = DW_UNSND (&attr);
c906108c
SS
18461 break;
18462 case DW_AT_type:
48fbe735 18463 has_type = 1;
c906108c
SS
18464 break;
18465 case DW_AT_abstract_origin:
18466 case DW_AT_specification:
72bf9492 18467 case DW_AT_extension:
48fbe735 18468 has_specification = 1;
0826b30a 18469 spec_offset = attr.get_ref_die_offset ();
48fbe735 18470 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18471 || cu->per_cu->is_dwz);
c906108c
SS
18472 break;
18473 case DW_AT_sibling:
18474 /* Ignore absolute siblings, they might point outside of
18475 the current compile unit. */
18476 if (attr.form == DW_FORM_ref_addr)
b98664d3 18477 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18478 else
b9502d3f 18479 {
48fbe735 18480 const gdb_byte *buffer = reader->buffer;
0826b30a 18481 sect_offset off = attr.get_ref_die_offset ();
9c541725 18482 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18483
18484 if (sibling_ptr < info_ptr)
b98664d3 18485 complaint (_("DW_AT_sibling points backwards"));
22869d73 18486 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18487 reader->die_section->overflow_complaint ();
b9502d3f 18488 else
48fbe735 18489 sibling = sibling_ptr;
b9502d3f 18490 }
c906108c 18491 break;
fa4028e9 18492 case DW_AT_byte_size:
48fbe735 18493 has_byte_size = 1;
fa4028e9 18494 break;
ff908ebf 18495 case DW_AT_const_value:
48fbe735 18496 has_const_value = 1;
ff908ebf 18497 break;
68511cec
CES
18498 case DW_AT_calling_convention:
18499 /* DWARF doesn't provide a way to identify a program's source-level
18500 entry point. DW_AT_calling_convention attributes are only meant
18501 to describe functions' calling conventions.
18502
18503 However, because it's a necessary piece of information in
0c1b455e
TT
18504 Fortran, and before DWARF 4 DW_CC_program was the only
18505 piece of debugging information whose definition refers to
18506 a 'main program' at all, several compilers marked Fortran
18507 main programs with DW_CC_program --- even when those
18508 functions use the standard calling conventions.
18509
18510 Although DWARF now specifies a way to provide this
18511 information, we support this practice for backward
18512 compatibility. */
68511cec 18513 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18514 && cu->language == language_fortran)
48fbe735 18515 main_subprogram = 1;
68511cec 18516 break;
481860b3
GB
18517 case DW_AT_inline:
18518 if (DW_UNSND (&attr) == DW_INL_inlined
18519 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18520 may_be_inlined = 1;
481860b3 18521 break;
95554aad
TT
18522
18523 case DW_AT_import:
48fbe735 18524 if (tag == DW_TAG_imported_unit)
36586728 18525 {
0826b30a 18526 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18527 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18528 || cu->per_cu->is_dwz);
18529 }
95554aad
TT
18530 break;
18531
0c1b455e 18532 case DW_AT_main_subprogram:
48fbe735 18533 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18534 break;
18535
05caa1d2
TT
18536 case DW_AT_ranges:
18537 {
18538 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18539 but that requires a full DIE, so instead we just
18540 reimplement it. */
18541 int need_ranges_base = tag != DW_TAG_compile_unit;
18542 unsigned int ranges_offset = (DW_UNSND (&attr)
18543 + (need_ranges_base
18544 ? cu->ranges_base
18545 : 0));
18546
18547 /* Value of the DW_AT_ranges attribute is the offset in the
18548 .debug_ranges section. */
18549 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18550 nullptr))
18551 has_pc_info = 1;
18552 }
18553 break;
18554
c906108c
SS
18555 default:
18556 break;
18557 }
18558 }
18559
10d06d82
TT
18560 /* For Ada, if both the name and the linkage name appear, we prefer
18561 the latter. This lets "catch exception" work better, regardless
18562 of the order in which the name and linkage name were emitted.
18563 Really, though, this is just a workaround for the fact that gdb
18564 doesn't store both the name and the linkage name. */
18565 if (cu->language == language_ada && linkage_name != nullptr)
18566 name = linkage_name;
18567
91da1414 18568 if (high_pc_relative)
48fbe735 18569 highpc += lowpc;
91da1414 18570
9373cf26
JK
18571 if (has_low_pc_attr && has_high_pc_attr)
18572 {
18573 /* When using the GNU linker, .gnu.linkonce. sections are used to
18574 eliminate duplicate copies of functions and vtables and such.
18575 The linker will arbitrarily choose one and discard the others.
18576 The AT_*_pc values for such functions refer to local labels in
18577 these sections. If the section from that file was discarded, the
18578 labels are not in the output, so the relocs get a value of 0.
18579 If this is a discarded function, mark the pc bounds as invalid,
18580 so that GDB will ignore it. */
5989a64e 18581 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18582 {
48fbe735 18583 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18584 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18585
b98664d3 18586 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18587 "for DIE at %s [in module %s]"),
48fbe735
YQ
18588 paddress (gdbarch, lowpc),
18589 sect_offset_str (sect_off),
9d8780f0 18590 objfile_name (objfile));
9373cf26
JK
18591 }
18592 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18593 else if (lowpc >= highpc)
9373cf26 18594 {
48fbe735 18595 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18596 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18597
b98664d3 18598 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18599 "for DIE at %s [in module %s]"),
48fbe735
YQ
18600 paddress (gdbarch, lowpc),
18601 paddress (gdbarch, highpc),
18602 sect_offset_str (sect_off),
9c541725 18603 objfile_name (objfile));
9373cf26
JK
18604 }
18605 else
48fbe735 18606 has_pc_info = 1;
9373cf26 18607 }
85cbf3d3 18608
c906108c
SS
18609 return info_ptr;
18610}
18611
72bf9492
DJ
18612/* Find a cached partial DIE at OFFSET in CU. */
18613
d590ff25
YQ
18614struct partial_die_info *
18615dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18616{
18617 struct partial_die_info *lookup_die = NULL;
6f06d47b 18618 struct partial_die_info part_die (sect_off);
72bf9492 18619
9a3c8263 18620 lookup_die = ((struct partial_die_info *)
d590ff25 18621 htab_find_with_hash (partial_dies, &part_die,
9c541725 18622 to_underlying (sect_off)));
72bf9492 18623
72bf9492
DJ
18624 return lookup_die;
18625}
18626
348e048f
DE
18627/* Find a partial DIE at OFFSET, which may or may not be in CU,
18628 except in the case of .debug_types DIEs which do not reference
18629 outside their CU (they do however referencing other types via
55f1336d 18630 DW_FORM_ref_sig8). */
72bf9492 18631
122cf0f2 18632static const struct cu_partial_die_info
9c541725 18633find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18634{
518817b3
SM
18635 struct dwarf2_per_objfile *dwarf2_per_objfile
18636 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18637 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18638 struct dwarf2_per_cu_data *per_cu = NULL;
18639 struct partial_die_info *pd = NULL;
72bf9492 18640
36586728 18641 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18642 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18643 {
d590ff25 18644 pd = cu->find_partial_die (sect_off);
5afb4e99 18645 if (pd != NULL)
fb816e8b 18646 return { cu, pd };
0d99eb77
DE
18647 /* We missed recording what we needed.
18648 Load all dies and try again. */
18649 per_cu = cu->per_cu;
5afb4e99 18650 }
0d99eb77
DE
18651 else
18652 {
18653 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18654 if (cu->per_cu->is_debug_types)
0d99eb77 18655 {
9d8780f0
SM
18656 error (_("Dwarf Error: Type Unit at offset %s contains"
18657 " external reference to offset %s [in module %s].\n"),
18658 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18659 bfd_get_filename (objfile->obfd));
18660 }
9c541725 18661 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18662 dwarf2_per_objfile);
72bf9492 18663
0d99eb77
DE
18664 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18665 load_partial_comp_unit (per_cu);
ae038cb0 18666
0d99eb77 18667 per_cu->cu->last_used = 0;
d590ff25 18668 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18669 }
5afb4e99 18670
dee91e82
DE
18671 /* If we didn't find it, and not all dies have been loaded,
18672 load them all and try again. */
18673
5afb4e99
DJ
18674 if (pd == NULL && per_cu->load_all_dies == 0)
18675 {
5afb4e99 18676 per_cu->load_all_dies = 1;
fd820528
DE
18677
18678 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18679 THIS_CU->cu may already be in use. So we can't just free it and
18680 replace its DIEs with the ones we read in. Instead, we leave those
18681 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18682 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18683 set. */
dee91e82 18684 load_partial_comp_unit (per_cu);
5afb4e99 18685
d590ff25 18686 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18687 }
18688
18689 if (pd == NULL)
18690 internal_error (__FILE__, __LINE__,
9d8780f0 18691 _("could not find partial DIE %s "
3e43a32a 18692 "in cache [from module %s]\n"),
9d8780f0 18693 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18694 return { per_cu->cu, pd };
72bf9492
DJ
18695}
18696
abc72ce4
DE
18697/* See if we can figure out if the class lives in a namespace. We do
18698 this by looking for a member function; its demangled name will
18699 contain namespace info, if there is any. */
18700
18701static void
18702guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18703 struct dwarf2_cu *cu)
18704{
18705 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18706 what template types look like, because the demangler
18707 frequently doesn't give the same name as the debug info. We
18708 could fix this by only using the demangled name to get the
18709 prefix (but see comment in read_structure_type). */
18710
18711 struct partial_die_info *real_pdi;
18712 struct partial_die_info *child_pdi;
18713
18714 /* If this DIE (this DIE's specification, if any) has a parent, then
18715 we should not do this. We'll prepend the parent's fully qualified
18716 name when we create the partial symbol. */
18717
18718 real_pdi = struct_pdi;
18719 while (real_pdi->has_specification)
fb816e8b 18720 {
122cf0f2
AB
18721 auto res = find_partial_die (real_pdi->spec_offset,
18722 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18723 real_pdi = res.pdi;
18724 cu = res.cu;
18725 }
abc72ce4
DE
18726
18727 if (real_pdi->die_parent != NULL)
18728 return;
18729
18730 for (child_pdi = struct_pdi->die_child;
18731 child_pdi != NULL;
18732 child_pdi = child_pdi->die_sibling)
18733 {
18734 if (child_pdi->tag == DW_TAG_subprogram
18735 && child_pdi->linkage_name != NULL)
18736 {
43816ebc
TT
18737 gdb::unique_xmalloc_ptr<char> actual_class_name
18738 (language_class_name_from_physname (cu->language_defn,
18739 child_pdi->linkage_name));
abc72ce4
DE
18740 if (actual_class_name != NULL)
18741 {
518817b3 18742 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18743 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18744 }
18745 break;
18746 }
18747 }
18748}
18749
25c11aca
TV
18750/* Return true if a DIE with TAG may have the DW_AT_const_value
18751 attribute. */
18752
18753static bool
18754can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18755{
18756 switch (tag)
18757 {
18758 case DW_TAG_constant:
18759 case DW_TAG_enumerator:
18760 case DW_TAG_formal_parameter:
18761 case DW_TAG_template_value_param:
18762 case DW_TAG_variable:
18763 return true;
18764 }
18765
18766 return false;
18767}
18768
52356b79
YQ
18769void
18770partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18771{
abc72ce4
DE
18772 /* Once we've fixed up a die, there's no point in doing so again.
18773 This also avoids a memory leak if we were to call
18774 guess_partial_die_structure_name multiple times. */
52356b79 18775 if (fixup_called)
abc72ce4
DE
18776 return;
18777
72bf9492
DJ
18778 /* If we found a reference attribute and the DIE has no name, try
18779 to find a name in the referred to DIE. */
18780
52356b79 18781 if (name == NULL && has_specification)
72bf9492
DJ
18782 {
18783 struct partial_die_info *spec_die;
72bf9492 18784
122cf0f2 18785 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18786 spec_die = res.pdi;
18787 cu = res.cu;
72bf9492 18788
52356b79 18789 spec_die->fixup (cu);
72bf9492
DJ
18790
18791 if (spec_die->name)
18792 {
52356b79 18793 name = spec_die->name;
72bf9492
DJ
18794
18795 /* Copy DW_AT_external attribute if it is set. */
18796 if (spec_die->is_external)
52356b79 18797 is_external = spec_die->is_external;
72bf9492
DJ
18798 }
18799 }
18800
25c11aca
TV
18801 if (!has_const_value && has_specification
18802 && can_have_DW_AT_const_value_p (tag))
18803 {
18804 struct partial_die_info *spec_die;
18805
18806 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18807 spec_die = res.pdi;
18808 cu = res.cu;
18809
18810 spec_die->fixup (cu);
18811
18812 if (spec_die->has_const_value)
18813 {
18814 /* Copy DW_AT_const_value attribute if it is set. */
18815 has_const_value = spec_die->has_const_value;
18816 }
18817 }
18818
72bf9492 18819 /* Set default names for some unnamed DIEs. */
72bf9492 18820
52356b79
YQ
18821 if (name == NULL && tag == DW_TAG_namespace)
18822 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18823
abc72ce4
DE
18824 /* If there is no parent die to provide a namespace, and there are
18825 children, see if we can determine the namespace from their linkage
122d1940 18826 name. */
abc72ce4 18827 if (cu->language == language_cplus
5989a64e 18828 && !cu->per_cu->dwarf2_per_objfile->per_bfd->types.empty ()
52356b79
YQ
18829 && die_parent == NULL
18830 && has_children
18831 && (tag == DW_TAG_class_type
18832 || tag == DW_TAG_structure_type
18833 || tag == DW_TAG_union_type))
18834 guess_partial_die_structure_name (this, cu);
abc72ce4 18835
53832f31
TT
18836 /* GCC might emit a nameless struct or union that has a linkage
18837 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18838 if (name == NULL
18839 && (tag == DW_TAG_class_type
18840 || tag == DW_TAG_interface_type
18841 || tag == DW_TAG_structure_type
18842 || tag == DW_TAG_union_type)
18843 && linkage_name != NULL)
53832f31 18844 {
43816ebc
TT
18845 gdb::unique_xmalloc_ptr<char> demangled
18846 (gdb_demangle (linkage_name, DMGL_TYPES));
18847 if (demangled != nullptr)
53832f31 18848 {
96408a79
SA
18849 const char *base;
18850
18851 /* Strip any leading namespaces/classes, keep only the base name.
18852 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18853 base = strrchr (demangled.get (), ':');
18854 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18855 base++;
18856 else
43816ebc 18857 base = demangled.get ();
96408a79 18858
518817b3 18859 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18860 name = objfile->intern (base);
53832f31
TT
18861 }
18862 }
18863
52356b79 18864 fixup_called = 1;
72bf9492
DJ
18865}
18866
41144253 18867/* Read the .debug_loclists header contents from the given SECTION in the
18868 HEADER. */
18869static void
18870read_loclist_header (struct loclist_header *header,
18871 struct dwarf2_section_info *section)
18872{
18873 unsigned int bytes_read;
18874 bfd *abfd = section->get_bfd_owner ();
18875 const gdb_byte *info_ptr = section->buffer;
18876 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18877 info_ptr += bytes_read;
18878 header->version = read_2_bytes (abfd, info_ptr);
18879 info_ptr += 2;
18880 header->addr_size = read_1_byte (abfd, info_ptr);
18881 info_ptr += 1;
18882 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18883 info_ptr += 1;
18884 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18885}
18886
18887/* Return the DW_AT_loclists_base value for the CU. */
18888static ULONGEST
18889lookup_loclist_base (struct dwarf2_cu *cu)
18890{
18891 /* For the .dwo unit, the loclist_base points to the first offset following
18892 the header. The header consists of the following entities-
18893 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18894 bit format)
18895 2. version (2 bytes)
18896 3. address size (1 byte)
18897 4. segment selector size (1 byte)
18898 5. offset entry count (4 bytes)
18899 These sizes are derived as per the DWARFv5 standard. */
18900 if (cu->dwo_unit != nullptr)
18901 {
18902 if (cu->header.initial_length_size == 4)
18903 return LOCLIST_HEADER_SIZE32;
18904 return LOCLIST_HEADER_SIZE64;
18905 }
18906 return cu->loclist_base;
18907}
18908
18909/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18910 array of offsets in the .debug_loclists section. */
18911static CORE_ADDR
18912read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18913{
18914 struct dwarf2_per_objfile *dwarf2_per_objfile
18915 = cu->per_cu->dwarf2_per_objfile;
18916 struct objfile *objfile = dwarf2_per_objfile->objfile;
18917 bfd *abfd = objfile->obfd;
18918 ULONGEST loclist_base = lookup_loclist_base (cu);
18919 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18920
18921 section->read (objfile);
18922 if (section->buffer == NULL)
18923 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18924 "section [in module %s]"), objfile_name (objfile));
18925 struct loclist_header header;
18926 read_loclist_header (&header, section);
18927 if (loclist_index >= header.offset_entry_count)
18928 complaint (_("DW_FORM_loclistx pointing outside of "
18929 ".debug_loclists offset array [in module %s]"),
18930 objfile_name (objfile));
18931 if (loclist_base + loclist_index * cu->header.offset_size
18932 >= section->size)
18933 complaint (_("DW_FORM_loclistx pointing outside of "
18934 ".debug_loclists section [in module %s]"),
18935 objfile_name (objfile));
18936 const gdb_byte *info_ptr
18937 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18938
18939 if (cu->header.offset_size == 4)
18940 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18941 else
18942 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18943}
18944
18a8505e
AT
18945/* Process the attributes that had to be skipped in the first round. These
18946 attributes are the ones that need str_offsets_base or addr_base attributes.
18947 They could not have been processed in the first round, because at the time
18948 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18949static void
18950read_attribute_reprocess (const struct die_reader_specs *reader,
18951 struct attribute *attr)
18a8505e
AT
18952{
18953 struct dwarf2_cu *cu = reader->cu;
18954 switch (attr->form)
18955 {
18956 case DW_FORM_addrx:
18957 case DW_FORM_GNU_addr_index:
18958 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18959 break;
41144253 18960 case DW_FORM_loclistx:
18961 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18962 break;
18a8505e
AT
18963 case DW_FORM_strx:
18964 case DW_FORM_strx1:
18965 case DW_FORM_strx2:
18966 case DW_FORM_strx3:
18967 case DW_FORM_strx4:
18968 case DW_FORM_GNU_str_index:
18969 {
18970 unsigned int str_index = DW_UNSND (attr);
18971 if (reader->dwo_file != NULL)
18972 {
18973 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18974 DW_STRING_IS_CANONICAL (attr) = 0;
18975 }
18976 else
18977 {
18978 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18979 DW_STRING_IS_CANONICAL (attr) = 0;
18980 }
18981 break;
18982 }
18983 default:
18984 gdb_assert_not_reached (_("Unexpected DWARF form."));
18985 }
18986}
18987
a8329558 18988/* Read an attribute value described by an attribute form. */
c906108c 18989
d521ce57 18990static const gdb_byte *
dee91e82
DE
18991read_attribute_value (const struct die_reader_specs *reader,
18992 struct attribute *attr, unsigned form,
18a8505e
AT
18993 LONGEST implicit_const, const gdb_byte *info_ptr,
18994 bool *need_reprocess)
c906108c 18995{
dee91e82 18996 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18997 struct dwarf2_per_objfile *dwarf2_per_objfile
18998 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18999 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19000 bfd *abfd = reader->abfd;
e7c27a73 19001 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19002 unsigned int bytes_read;
19003 struct dwarf_block *blk;
18a8505e 19004 *need_reprocess = false;
c906108c 19005
aead7601 19006 attr->form = (enum dwarf_form) form;
a8329558 19007 switch (form)
c906108c 19008 {
c906108c 19009 case DW_FORM_ref_addr:
ae411497 19010 if (cu->header.version == 2)
c8a7a66f
TT
19011 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19012 &bytes_read);
ae411497 19013 else
8266302d
TT
19014 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19015 &bytes_read);
ae411497
TT
19016 info_ptr += bytes_read;
19017 break;
36586728 19018 case DW_FORM_GNU_ref_alt:
8266302d 19019 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19020 info_ptr += bytes_read;
19021 break;
ae411497 19022 case DW_FORM_addr:
08feed99
TT
19023 {
19024 struct gdbarch *gdbarch = objfile->arch ();
19025 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19026 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19027 info_ptr += bytes_read;
19028 }
c906108c
SS
19029 break;
19030 case DW_FORM_block2:
7b5a2f43 19031 blk = dwarf_alloc_block (cu);
c906108c
SS
19032 blk->size = read_2_bytes (abfd, info_ptr);
19033 info_ptr += 2;
19034 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19035 info_ptr += blk->size;
19036 DW_BLOCK (attr) = blk;
19037 break;
19038 case DW_FORM_block4:
7b5a2f43 19039 blk = dwarf_alloc_block (cu);
c906108c
SS
19040 blk->size = read_4_bytes (abfd, info_ptr);
19041 info_ptr += 4;
19042 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19043 info_ptr += blk->size;
19044 DW_BLOCK (attr) = blk;
19045 break;
19046 case DW_FORM_data2:
19047 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19048 info_ptr += 2;
19049 break;
19050 case DW_FORM_data4:
19051 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19052 info_ptr += 4;
19053 break;
19054 case DW_FORM_data8:
19055 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19056 info_ptr += 8;
19057 break;
0224619f
JK
19058 case DW_FORM_data16:
19059 blk = dwarf_alloc_block (cu);
19060 blk->size = 16;
19061 blk->data = read_n_bytes (abfd, info_ptr, 16);
19062 info_ptr += 16;
19063 DW_BLOCK (attr) = blk;
19064 break;
2dc7f7b3 19065 case DW_FORM_sec_offset:
8266302d 19066 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19067 info_ptr += bytes_read;
19068 break;
41144253 19069 case DW_FORM_loclistx:
19070 {
19071 *need_reprocess = true;
19072 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19073 info_ptr += bytes_read;
19074 }
19075 break;
c906108c 19076 case DW_FORM_string:
9b1c24c8 19077 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19078 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19079 info_ptr += bytes_read;
19080 break;
4bdf3d34 19081 case DW_FORM_strp:
36586728
TT
19082 if (!cu->per_cu->is_dwz)
19083 {
ed2dc618
SM
19084 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19085 abfd, info_ptr, cu_header,
36586728
TT
19086 &bytes_read);
19087 DW_STRING_IS_CANONICAL (attr) = 0;
19088 info_ptr += bytes_read;
19089 break;
19090 }
19091 /* FALLTHROUGH */
43988095
JK
19092 case DW_FORM_line_strp:
19093 if (!cu->per_cu->is_dwz)
19094 {
86c0bb4c
TT
19095 DW_STRING (attr)
19096 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19097 &bytes_read);
43988095
JK
19098 DW_STRING_IS_CANONICAL (attr) = 0;
19099 info_ptr += bytes_read;
19100 break;
19101 }
19102 /* FALLTHROUGH */
36586728
TT
19103 case DW_FORM_GNU_strp_alt:
19104 {
ed2dc618 19105 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
19106 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19107 &bytes_read);
36586728 19108
0314b390 19109 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19110 DW_STRING_IS_CANONICAL (attr) = 0;
19111 info_ptr += bytes_read;
19112 }
4bdf3d34 19113 break;
2dc7f7b3 19114 case DW_FORM_exprloc:
c906108c 19115 case DW_FORM_block:
7b5a2f43 19116 blk = dwarf_alloc_block (cu);
c906108c
SS
19117 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19118 info_ptr += bytes_read;
19119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19120 info_ptr += blk->size;
19121 DW_BLOCK (attr) = blk;
19122 break;
19123 case DW_FORM_block1:
7b5a2f43 19124 blk = dwarf_alloc_block (cu);
c906108c
SS
19125 blk->size = read_1_byte (abfd, info_ptr);
19126 info_ptr += 1;
19127 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19128 info_ptr += blk->size;
19129 DW_BLOCK (attr) = blk;
19130 break;
19131 case DW_FORM_data1:
19132 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19133 info_ptr += 1;
19134 break;
19135 case DW_FORM_flag:
19136 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19137 info_ptr += 1;
19138 break;
2dc7f7b3
TT
19139 case DW_FORM_flag_present:
19140 DW_UNSND (attr) = 1;
19141 break;
c906108c
SS
19142 case DW_FORM_sdata:
19143 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19144 info_ptr += bytes_read;
19145 break;
19146 case DW_FORM_udata:
18a8505e 19147 case DW_FORM_rnglistx:
c906108c
SS
19148 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19149 info_ptr += bytes_read;
19150 break;
19151 case DW_FORM_ref1:
9c541725 19152 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19153 + read_1_byte (abfd, info_ptr));
c906108c
SS
19154 info_ptr += 1;
19155 break;
19156 case DW_FORM_ref2:
9c541725 19157 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19158 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19159 info_ptr += 2;
19160 break;
19161 case DW_FORM_ref4:
9c541725 19162 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19163 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19164 info_ptr += 4;
19165 break;
613e1657 19166 case DW_FORM_ref8:
9c541725 19167 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19168 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19169 info_ptr += 8;
19170 break;
55f1336d 19171 case DW_FORM_ref_sig8:
ac9ec31b 19172 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19173 info_ptr += 8;
19174 break;
c906108c 19175 case DW_FORM_ref_udata:
9c541725 19176 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19177 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19178 info_ptr += bytes_read;
19179 break;
c906108c 19180 case DW_FORM_indirect:
a8329558
KW
19181 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19182 info_ptr += bytes_read;
43988095
JK
19183 if (form == DW_FORM_implicit_const)
19184 {
19185 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19186 info_ptr += bytes_read;
19187 }
19188 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19189 info_ptr, need_reprocess);
43988095
JK
19190 break;
19191 case DW_FORM_implicit_const:
19192 DW_SND (attr) = implicit_const;
a8329558 19193 break;
336d760d 19194 case DW_FORM_addrx:
3019eac3 19195 case DW_FORM_GNU_addr_index:
18a8505e
AT
19196 *need_reprocess = true;
19197 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19198 info_ptr += bytes_read;
19199 break;
cf532bd1 19200 case DW_FORM_strx:
15f18d14
AT
19201 case DW_FORM_strx1:
19202 case DW_FORM_strx2:
19203 case DW_FORM_strx3:
19204 case DW_FORM_strx4:
3019eac3 19205 case DW_FORM_GNU_str_index:
3019eac3 19206 {
15f18d14
AT
19207 ULONGEST str_index;
19208 if (form == DW_FORM_strx1)
19209 {
19210 str_index = read_1_byte (abfd, info_ptr);
19211 info_ptr += 1;
19212 }
19213 else if (form == DW_FORM_strx2)
19214 {
19215 str_index = read_2_bytes (abfd, info_ptr);
19216 info_ptr += 2;
19217 }
19218 else if (form == DW_FORM_strx3)
19219 {
19220 str_index = read_3_bytes (abfd, info_ptr);
19221 info_ptr += 3;
19222 }
19223 else if (form == DW_FORM_strx4)
19224 {
19225 str_index = read_4_bytes (abfd, info_ptr);
19226 info_ptr += 4;
19227 }
19228 else
19229 {
19230 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19231 info_ptr += bytes_read;
19232 }
18a8505e
AT
19233 *need_reprocess = true;
19234 DW_UNSND (attr) = str_index;
19235 }
3019eac3 19236 break;
c906108c 19237 default:
8a3fe4f8 19238 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19239 dwarf_form_name (form),
19240 bfd_get_filename (abfd));
c906108c 19241 }
28e94949 19242
36586728 19243 /* Super hack. */
cd6c91b4 19244 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19245 attr->form = DW_FORM_GNU_ref_alt;
19246
28e94949
JB
19247 /* We have seen instances where the compiler tried to emit a byte
19248 size attribute of -1 which ended up being encoded as an unsigned
19249 0xffffffff. Although 0xffffffff is technically a valid size value,
19250 an object of this size seems pretty unlikely so we can relatively
19251 safely treat these cases as if the size attribute was invalid and
19252 treat them as zero by default. */
19253 if (attr->name == DW_AT_byte_size
19254 && form == DW_FORM_data4
19255 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19256 {
19257 complaint
b98664d3 19258 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19259 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19260 DW_UNSND (attr) = 0;
19261 }
28e94949 19262
c906108c
SS
19263 return info_ptr;
19264}
19265
a8329558
KW
19266/* Read an attribute described by an abbreviated attribute. */
19267
d521ce57 19268static const gdb_byte *
dee91e82
DE
19269read_attribute (const struct die_reader_specs *reader,
19270 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19271 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19272{
19273 attr->name = abbrev->name;
43988095 19274 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19275 abbrev->implicit_const, info_ptr,
19276 need_reprocess);
a8329558
KW
19277}
19278
43988095
JK
19279/* Return pointer to string at .debug_str offset STR_OFFSET. */
19280
19281static const char *
ed2dc618 19282read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19283 LONGEST str_offset)
43988095 19284{
5989a64e
SM
19285 return dwarf2_per_objfile->per_bfd->str.read_string
19286 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19287}
19288
43988095
JK
19289/* Return pointer to string at .debug_str offset as read from BUF.
19290 BUF is assumed to be in a compilation unit described by CU_HEADER.
19291 Return *BYTES_READ_PTR count of bytes read from BUF. */
19292
d521ce57 19293static const char *
ed2dc618
SM
19294read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19295 const gdb_byte *buf,
cf2c3c16
TT
19296 const struct comp_unit_head *cu_header,
19297 unsigned int *bytes_read_ptr)
19298{
8266302d 19299 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19300
4f44ae6c 19301 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19302}
19303
86c0bb4c 19304/* See read.h. */
43988095 19305
86c0bb4c
TT
19306const char *
19307dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19308 const struct comp_unit_head *cu_header,
19309 unsigned int *bytes_read_ptr)
43988095 19310{
86c0bb4c 19311 bfd *abfd = objfile->obfd;
8266302d 19312 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19313
5989a64e 19314 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19315}
19316
3019eac3 19317/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19318 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19319 ADDR_SIZE is the size of addresses from the CU header. */
19320
19321static CORE_ADDR
ed2dc618 19322read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19323 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19324 int addr_size)
3019eac3
DE
19325{
19326 struct objfile *objfile = dwarf2_per_objfile->objfile;
19327 bfd *abfd = objfile->obfd;
19328 const gdb_byte *info_ptr;
18a8505e 19329 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19330
5989a64e
SM
19331 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19332 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19333 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19334 objfile_name (objfile));
18a8505e 19335 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19336 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19337 error (_("DW_FORM_addr_index pointing outside of "
19338 ".debug_addr section [in module %s]"),
4262abfb 19339 objfile_name (objfile));
5989a64e 19340 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19341 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19342 if (addr_size == 4)
19343 return bfd_get_32 (abfd, info_ptr);
19344 else
19345 return bfd_get_64 (abfd, info_ptr);
19346}
19347
19348/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19349
19350static CORE_ADDR
19351read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19352{
518817b3
SM
19353 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19354 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19355}
19356
19357/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19358
19359static CORE_ADDR
d521ce57 19360read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19361 unsigned int *bytes_read)
19362{
518817b3 19363 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19364 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19365
19366 return read_addr_index (cu, addr_index);
19367}
19368
450a1bfc 19369/* See read.h. */
3019eac3
DE
19370
19371CORE_ADDR
450a1bfc 19372dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19373{
ed2dc618 19374 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19375 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19376 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19377 int addr_size;
19378
3019eac3
DE
19379 /* We need addr_base and addr_size.
19380 If we don't have PER_CU->cu, we have to get it.
19381 Nasty, but the alternative is storing the needed info in PER_CU,
19382 which at this point doesn't seem justified: it's not clear how frequently
19383 it would get used and it would increase the size of every PER_CU.
19384 Entry points like dwarf2_per_cu_addr_size do a similar thing
19385 so we're not in uncharted territory here.
19386 Alas we need to be a bit more complicated as addr_base is contained
19387 in the DIE.
19388
19389 We don't need to read the entire CU(/TU).
19390 We just need the header and top level die.
a1b64ce1 19391
3019eac3 19392 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19393 For now we skip this optimization. */
3019eac3
DE
19394
19395 if (cu != NULL)
19396 {
19397 addr_base = cu->addr_base;
19398 addr_size = cu->header.addr_size;
19399 }
19400 else
19401 {
6751ebae 19402 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19403 addr_base = reader.cu->addr_base;
19404 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19405 }
19406
ed2dc618
SM
19407 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19408 addr_size);
3019eac3
DE
19409}
19410
18a8505e
AT
19411/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19412 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19413 DWO file. */
3019eac3 19414
d521ce57 19415static const char *
18a8505e
AT
19416read_str_index (struct dwarf2_cu *cu,
19417 struct dwarf2_section_info *str_section,
19418 struct dwarf2_section_info *str_offsets_section,
19419 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19420{
518817b3
SM
19421 struct dwarf2_per_objfile *dwarf2_per_objfile
19422 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19423 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19424 const char *objf_name = objfile_name (objfile);
3019eac3 19425 bfd *abfd = objfile->obfd;
d521ce57 19426 const gdb_byte *info_ptr;
3019eac3 19427 ULONGEST str_offset;
cf532bd1 19428 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19429
96b79293
TT
19430 str_section->read (objfile);
19431 str_offsets_section->read (objfile);
73869dc2 19432 if (str_section->buffer == NULL)
18a8505e 19433 error (_("%s used without %s section"
9d8780f0 19434 " in CU at offset %s [in module %s]"),
96b79293 19435 form_name, str_section->get_name (),
18a8505e 19436 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19437 if (str_offsets_section->buffer == NULL)
18a8505e 19438 error (_("%s used without %s section"
9d8780f0 19439 " in CU at offset %s [in module %s]"),
96b79293 19440 form_name, str_section->get_name (),
18a8505e 19441 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19442 info_ptr = (str_offsets_section->buffer
18a8505e 19443 + str_offsets_base
3019eac3
DE
19444 + str_index * cu->header.offset_size);
19445 if (cu->header.offset_size == 4)
19446 str_offset = bfd_get_32 (abfd, info_ptr);
19447 else
19448 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19449 if (str_offset >= str_section->size)
57d63ce2 19450 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19451 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19452 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19453 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19454}
19455
18a8505e
AT
19456/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19457
19458static const char *
19459read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19460{
19461 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19462 ? reader->cu->header.addr_size : 0;
19463 return read_str_index (reader->cu,
19464 &reader->dwo_file->sections.str,
19465 &reader->dwo_file->sections.str_offsets,
19466 str_offsets_base, str_index);
19467}
19468
19469/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19470
19471static const char *
19472read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19473{
19474 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19475 const char *objf_name = objfile_name (objfile);
19476 static const char form_name[] = "DW_FORM_GNU_str_index";
19477 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19478
19479 if (!cu->str_offsets_base.has_value ())
19480 error (_("%s used in Fission stub without %s"
19481 " in CU at offset 0x%lx [in module %s]"),
19482 form_name, str_offsets_attr_name,
19483 (long) cu->header.offset_size, objf_name);
19484
19485 return read_str_index (cu,
5989a64e
SM
19486 &cu->per_cu->dwarf2_per_objfile->per_bfd->str,
19487 &cu->per_cu->dwarf2_per_objfile->per_bfd->str_offsets,
18a8505e
AT
19488 *cu->str_offsets_base, str_index);
19489}
19490
3019eac3
DE
19491/* Return the length of an LEB128 number in BUF. */
19492
19493static int
19494leb128_size (const gdb_byte *buf)
19495{
19496 const gdb_byte *begin = buf;
19497 gdb_byte byte;
19498
19499 while (1)
19500 {
19501 byte = *buf++;
19502 if ((byte & 128) == 0)
19503 return buf - begin;
19504 }
19505}
19506
c906108c 19507static void
e142c38c 19508set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19509{
19510 switch (lang)
19511 {
19512 case DW_LANG_C89:
76bee0cc 19513 case DW_LANG_C99:
0cfd832f 19514 case DW_LANG_C11:
c906108c 19515 case DW_LANG_C:
d1be3247 19516 case DW_LANG_UPC:
e142c38c 19517 cu->language = language_c;
c906108c 19518 break;
9c37b5ae 19519 case DW_LANG_Java:
c906108c 19520 case DW_LANG_C_plus_plus:
0cfd832f
MW
19521 case DW_LANG_C_plus_plus_11:
19522 case DW_LANG_C_plus_plus_14:
e142c38c 19523 cu->language = language_cplus;
c906108c 19524 break;
6aecb9c2
JB
19525 case DW_LANG_D:
19526 cu->language = language_d;
19527 break;
c906108c
SS
19528 case DW_LANG_Fortran77:
19529 case DW_LANG_Fortran90:
b21b22e0 19530 case DW_LANG_Fortran95:
f7de9aab
MW
19531 case DW_LANG_Fortran03:
19532 case DW_LANG_Fortran08:
e142c38c 19533 cu->language = language_fortran;
c906108c 19534 break;
a766d390
DE
19535 case DW_LANG_Go:
19536 cu->language = language_go;
19537 break;
c906108c 19538 case DW_LANG_Mips_Assembler:
e142c38c 19539 cu->language = language_asm;
c906108c
SS
19540 break;
19541 case DW_LANG_Ada83:
8aaf0b47 19542 case DW_LANG_Ada95:
bc5f45f8
JB
19543 cu->language = language_ada;
19544 break;
72019c9c
GM
19545 case DW_LANG_Modula2:
19546 cu->language = language_m2;
19547 break;
fe8e67fd
PM
19548 case DW_LANG_Pascal83:
19549 cu->language = language_pascal;
19550 break;
22566fbd
DJ
19551 case DW_LANG_ObjC:
19552 cu->language = language_objc;
19553 break;
c44af4eb
TT
19554 case DW_LANG_Rust:
19555 case DW_LANG_Rust_old:
19556 cu->language = language_rust;
19557 break;
c906108c
SS
19558 case DW_LANG_Cobol74:
19559 case DW_LANG_Cobol85:
c906108c 19560 default:
e142c38c 19561 cu->language = language_minimal;
c906108c
SS
19562 break;
19563 }
e142c38c 19564 cu->language_defn = language_def (cu->language);
c906108c
SS
19565}
19566
19567/* Return the named attribute or NULL if not there. */
19568
19569static struct attribute *
e142c38c 19570dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19571{
a48e046c 19572 for (;;)
c906108c 19573 {
a48e046c
TT
19574 unsigned int i;
19575 struct attribute *spec = NULL;
19576
19577 for (i = 0; i < die->num_attrs; ++i)
19578 {
19579 if (die->attrs[i].name == name)
19580 return &die->attrs[i];
19581 if (die->attrs[i].name == DW_AT_specification
19582 || die->attrs[i].name == DW_AT_abstract_origin)
19583 spec = &die->attrs[i];
19584 }
19585
19586 if (!spec)
19587 break;
c906108c 19588
f2f0e013 19589 die = follow_die_ref (die, spec, &cu);
f2f0e013 19590 }
c5aa993b 19591
c906108c
SS
19592 return NULL;
19593}
19594
7d45c7c3
KB
19595/* Return the string associated with a string-typed attribute, or NULL if it
19596 is either not found or is of an incorrect type. */
19597
19598static const char *
19599dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19600{
19601 struct attribute *attr;
19602 const char *str = NULL;
19603
19604 attr = dwarf2_attr (die, name, cu);
19605
19606 if (attr != NULL)
19607 {
e61108c9
TT
19608 str = attr->value_as_string ();
19609 if (str == nullptr)
b98664d3 19610 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19611 "DIE at %s in module %s"),
19612 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19613 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19614 }
19615
19616 return str;
19617}
19618
a084a2a6 19619/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19620 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19621static const char *
19622dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19623{
19624 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19625 if (dwo_name == nullptr)
19626 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19627 return dwo_name;
19628}
19629
05cf31d1
JB
19630/* Return non-zero iff the attribute NAME is defined for the given DIE,
19631 and holds a non-zero value. This function should only be used for
2dc7f7b3 19632 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19633
19634static int
19635dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19636{
19637 struct attribute *attr = dwarf2_attr (die, name, cu);
19638
19639 return (attr && DW_UNSND (attr));
19640}
19641
3ca72b44 19642static int
e142c38c 19643die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19644{
05cf31d1
JB
19645 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19646 which value is non-zero. However, we have to be careful with
19647 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19648 (via dwarf2_flag_true_p) follows this attribute. So we may
19649 end up accidently finding a declaration attribute that belongs
19650 to a different DIE referenced by the specification attribute,
19651 even though the given DIE does not have a declaration attribute. */
19652 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19653 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19654}
19655
63d06c5c 19656/* Return the die giving the specification for DIE, if there is
f2f0e013 19657 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19658 containing the return value on output. If there is no
19659 specification, but there is an abstract origin, that is
19660 returned. */
63d06c5c
DC
19661
19662static struct die_info *
f2f0e013 19663die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19664{
f2f0e013
DJ
19665 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19666 *spec_cu);
63d06c5c 19667
edb3359d
DJ
19668 if (spec_attr == NULL)
19669 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19670
63d06c5c
DC
19671 if (spec_attr == NULL)
19672 return NULL;
19673 else
f2f0e013 19674 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19675}
c906108c 19676
527f3840
JK
19677/* Stub for free_line_header to match void * callback types. */
19678
19679static void
19680free_line_header_voidp (void *arg)
19681{
9a3c8263 19682 struct line_header *lh = (struct line_header *) arg;
527f3840 19683
fff8551c 19684 delete lh;
527f3840
JK
19685}
19686
83769d0b 19687/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19688
19689static struct dwarf2_section_info *
19690get_debug_line_section (struct dwarf2_cu *cu)
19691{
19692 struct dwarf2_section_info *section;
518817b3
SM
19693 struct dwarf2_per_objfile *dwarf2_per_objfile
19694 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19695
19696 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19697 DWO file. */
19698 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19699 section = &cu->dwo_unit->dwo_file->sections.line;
19700 else if (cu->per_cu->is_dwz)
19701 {
ed2dc618 19702 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19703
19704 section = &dwz->line;
19705 }
19706 else
5989a64e 19707 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19708
19709 return section;
19710}
19711
debd256d 19712/* Read the statement program header starting at OFFSET in
3019eac3 19713 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19714 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19715 Returns NULL if there is a problem reading the header, e.g., if it
19716 has a version we don't understand.
debd256d
JB
19717
19718 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19719 the returned object point into the dwarf line section buffer,
19720 and must not be freed. */
ae2de4f8 19721
fff8551c 19722static line_header_up
9c541725 19723dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19724{
3019eac3 19725 struct dwarf2_section_info *section;
518817b3
SM
19726 struct dwarf2_per_objfile *dwarf2_per_objfile
19727 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19728
36586728 19729 section = get_debug_line_section (cu);
96b79293 19730 section->read (dwarf2_per_objfile->objfile);
3019eac3 19731 if (section->buffer == NULL)
debd256d 19732 {
3019eac3 19733 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19734 complaint (_("missing .debug_line.dwo section"));
3019eac3 19735 else
b98664d3 19736 complaint (_("missing .debug_line section"));
debd256d
JB
19737 return 0;
19738 }
19739
0df7ad3a
TT
19740 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19741 dwarf2_per_objfile, section,
19742 &cu->header);
debd256d 19743}
c906108c 19744
c6da4cef 19745/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19746 Return the file name of the psymtab for the given file_entry.
c6da4cef 19747 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19748 If space for the result is malloc'd, *NAME_HOLDER will be set.
19749 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19750
d521ce57 19751static const char *
7ba99d21 19752psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19753 const dwarf2_psymtab *pst,
c89b44cd
TT
19754 const char *comp_dir,
19755 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19756{
d521ce57
TT
19757 const char *include_name = fe.name;
19758 const char *include_name_to_compare = include_name;
72b9f47f 19759 const char *pst_filename;
c6da4cef
DE
19760 int file_is_pst;
19761
8c43009f 19762 const char *dir_name = fe.include_dir (lh);
c6da4cef 19763
c89b44cd 19764 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19765 if (!IS_ABSOLUTE_PATH (include_name)
19766 && (dir_name != NULL || comp_dir != NULL))
19767 {
19768 /* Avoid creating a duplicate psymtab for PST.
19769 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19770 Before we do the comparison, however, we need to account
19771 for DIR_NAME and COMP_DIR.
19772 First prepend dir_name (if non-NULL). If we still don't
19773 have an absolute path prepend comp_dir (if non-NULL).
19774 However, the directory we record in the include-file's
19775 psymtab does not contain COMP_DIR (to match the
19776 corresponding symtab(s)).
19777
19778 Example:
19779
19780 bash$ cd /tmp
19781 bash$ gcc -g ./hello.c
19782 include_name = "hello.c"
19783 dir_name = "."
19784 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19785 DW_AT_name = "./hello.c"
19786
19787 */
c6da4cef
DE
19788
19789 if (dir_name != NULL)
19790 {
c89b44cd
TT
19791 name_holder->reset (concat (dir_name, SLASH_STRING,
19792 include_name, (char *) NULL));
19793 include_name = name_holder->get ();
c6da4cef 19794 include_name_to_compare = include_name;
c6da4cef
DE
19795 }
19796 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19797 {
c89b44cd
TT
19798 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19799 include_name, (char *) NULL));
19800 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19801 }
19802 }
19803
19804 pst_filename = pst->filename;
c89b44cd 19805 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19806 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19807 {
c89b44cd
TT
19808 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19809 pst_filename, (char *) NULL));
19810 pst_filename = copied_name.get ();
c6da4cef
DE
19811 }
19812
1e3fad37 19813 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19814
c6da4cef
DE
19815 if (file_is_pst)
19816 return NULL;
19817 return include_name;
19818}
19819
d9b3de22
DE
19820/* State machine to track the state of the line number program. */
19821
6f77053d 19822class lnp_state_machine
d9b3de22 19823{
6f77053d
PA
19824public:
19825 /* Initialize a machine state for the start of a line number
19826 program. */
804d2729
TT
19827 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19828 bool record_lines_p);
6f77053d 19829
8c43009f
PA
19830 file_entry *current_file ()
19831 {
19832 /* lh->file_names is 0-based, but the file name numbers in the
19833 statement program are 1-based. */
6f77053d
PA
19834 return m_line_header->file_name_at (m_file);
19835 }
19836
19837 /* Record the line in the state machine. END_SEQUENCE is true if
19838 we're processing the end of a sequence. */
19839 void record_line (bool end_sequence);
19840
7ab6656f
OJ
19841 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19842 nop-out rest of the lines in this sequence. */
6f77053d
PA
19843 void check_line_address (struct dwarf2_cu *cu,
19844 const gdb_byte *line_ptr,
7ab6656f 19845 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19846
19847 void handle_set_discriminator (unsigned int discriminator)
19848 {
19849 m_discriminator = discriminator;
19850 m_line_has_non_zero_discriminator |= discriminator != 0;
19851 }
19852
19853 /* Handle DW_LNE_set_address. */
19854 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19855 {
19856 m_op_index = 0;
19857 address += baseaddr;
19858 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19859 }
19860
19861 /* Handle DW_LNS_advance_pc. */
19862 void handle_advance_pc (CORE_ADDR adjust);
19863
19864 /* Handle a special opcode. */
19865 void handle_special_opcode (unsigned char op_code);
19866
19867 /* Handle DW_LNS_advance_line. */
19868 void handle_advance_line (int line_delta)
19869 {
19870 advance_line (line_delta);
19871 }
19872
19873 /* Handle DW_LNS_set_file. */
19874 void handle_set_file (file_name_index file);
19875
19876 /* Handle DW_LNS_negate_stmt. */
19877 void handle_negate_stmt ()
19878 {
19879 m_is_stmt = !m_is_stmt;
19880 }
19881
19882 /* Handle DW_LNS_const_add_pc. */
19883 void handle_const_add_pc ();
19884
19885 /* Handle DW_LNS_fixed_advance_pc. */
19886 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19887 {
19888 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19889 m_op_index = 0;
19890 }
19891
19892 /* Handle DW_LNS_copy. */
19893 void handle_copy ()
19894 {
19895 record_line (false);
19896 m_discriminator = 0;
19897 }
19898
19899 /* Handle DW_LNE_end_sequence. */
19900 void handle_end_sequence ()
19901 {
804d2729 19902 m_currently_recording_lines = true;
6f77053d
PA
19903 }
19904
19905private:
19906 /* Advance the line by LINE_DELTA. */
19907 void advance_line (int line_delta)
19908 {
19909 m_line += line_delta;
19910
19911 if (line_delta != 0)
19912 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19913 }
19914
804d2729
TT
19915 struct dwarf2_cu *m_cu;
19916
6f77053d
PA
19917 gdbarch *m_gdbarch;
19918
19919 /* True if we're recording lines.
19920 Otherwise we're building partial symtabs and are just interested in
19921 finding include files mentioned by the line number program. */
19922 bool m_record_lines_p;
19923
8c43009f 19924 /* The line number header. */
6f77053d 19925 line_header *m_line_header;
8c43009f 19926
6f77053d
PA
19927 /* These are part of the standard DWARF line number state machine,
19928 and initialized according to the DWARF spec. */
d9b3de22 19929
6f77053d 19930 unsigned char m_op_index = 0;
7ba99d21
AT
19931 /* The line table index of the current file. */
19932 file_name_index m_file = 1;
6f77053d
PA
19933 unsigned int m_line = 1;
19934
19935 /* These are initialized in the constructor. */
19936
19937 CORE_ADDR m_address;
19938 bool m_is_stmt;
19939 unsigned int m_discriminator;
d9b3de22
DE
19940
19941 /* Additional bits of state we need to track. */
19942
19943 /* The last file that we called dwarf2_start_subfile for.
19944 This is only used for TLLs. */
6f77053d 19945 unsigned int m_last_file = 0;
d9b3de22 19946 /* The last file a line number was recorded for. */
6f77053d 19947 struct subfile *m_last_subfile = NULL;
d9b3de22 19948
804d2729
TT
19949 /* When true, record the lines we decode. */
19950 bool m_currently_recording_lines = false;
d9b3de22
DE
19951
19952 /* The last line number that was recorded, used to coalesce
19953 consecutive entries for the same line. This can happen, for
19954 example, when discriminators are present. PR 17276. */
6f77053d
PA
19955 unsigned int m_last_line = 0;
19956 bool m_line_has_non_zero_discriminator = false;
8c43009f 19957};
d9b3de22 19958
6f77053d
PA
19959void
19960lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19961{
19962 CORE_ADDR addr_adj = (((m_op_index + adjust)
19963 / m_line_header->maximum_ops_per_instruction)
19964 * m_line_header->minimum_instruction_length);
19965 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19966 m_op_index = ((m_op_index + adjust)
19967 % m_line_header->maximum_ops_per_instruction);
19968}
d9b3de22 19969
6f77053d
PA
19970void
19971lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19972{
6f77053d 19973 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19974 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19975 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19976 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19977 / m_line_header->maximum_ops_per_instruction)
19978 * m_line_header->minimum_instruction_length);
19979 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19980 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19981 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19982
258bf0ee 19983 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19984 advance_line (line_delta);
19985 record_line (false);
19986 m_discriminator = 0;
19987}
d9b3de22 19988
6f77053d
PA
19989void
19990lnp_state_machine::handle_set_file (file_name_index file)
19991{
19992 m_file = file;
19993
19994 const file_entry *fe = current_file ();
19995 if (fe == NULL)
19996 dwarf2_debug_line_missing_file_complaint ();
19997 else if (m_record_lines_p)
19998 {
19999 const char *dir = fe->include_dir (m_line_header);
20000
c24bdb02 20001 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20002 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20003 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20004 }
20005}
20006
20007void
20008lnp_state_machine::handle_const_add_pc ()
20009{
20010 CORE_ADDR adjust
20011 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20012
20013 CORE_ADDR addr_adj
20014 = (((m_op_index + adjust)
20015 / m_line_header->maximum_ops_per_instruction)
20016 * m_line_header->minimum_instruction_length);
20017
20018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20019 m_op_index = ((m_op_index + adjust)
20020 % m_line_header->maximum_ops_per_instruction);
20021}
d9b3de22 20022
a05a36a5
DE
20023/* Return non-zero if we should add LINE to the line number table.
20024 LINE is the line to add, LAST_LINE is the last line that was added,
20025 LAST_SUBFILE is the subfile for LAST_LINE.
20026 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20027 had a non-zero discriminator.
20028
20029 We have to be careful in the presence of discriminators.
20030 E.g., for this line:
20031
20032 for (i = 0; i < 100000; i++);
20033
20034 clang can emit four line number entries for that one line,
20035 each with a different discriminator.
20036 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20037
20038 However, we want gdb to coalesce all four entries into one.
20039 Otherwise the user could stepi into the middle of the line and
20040 gdb would get confused about whether the pc really was in the
20041 middle of the line.
20042
20043 Things are further complicated by the fact that two consecutive
20044 line number entries for the same line is a heuristic used by gcc
20045 to denote the end of the prologue. So we can't just discard duplicate
20046 entries, we have to be selective about it. The heuristic we use is
20047 that we only collapse consecutive entries for the same line if at least
20048 one of those entries has a non-zero discriminator. PR 17276.
20049
20050 Note: Addresses in the line number state machine can never go backwards
20051 within one sequence, thus this coalescing is ok. */
20052
20053static int
804d2729
TT
20054dwarf_record_line_p (struct dwarf2_cu *cu,
20055 unsigned int line, unsigned int last_line,
a05a36a5
DE
20056 int line_has_non_zero_discriminator,
20057 struct subfile *last_subfile)
20058{
c24bdb02 20059 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20060 return 1;
20061 if (line != last_line)
20062 return 1;
20063 /* Same line for the same file that we've seen already.
20064 As a last check, for pr 17276, only record the line if the line
20065 has never had a non-zero discriminator. */
20066 if (!line_has_non_zero_discriminator)
20067 return 1;
20068 return 0;
20069}
20070
804d2729
TT
20071/* Use the CU's builder to record line number LINE beginning at
20072 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20073
20074static void
d9b3de22 20075dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20076 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20077 struct dwarf2_cu *cu)
252a6764
DE
20078{
20079 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20080
27e0867f
DE
20081 if (dwarf_line_debug)
20082 {
20083 fprintf_unfiltered (gdb_stdlog,
20084 "Recording line %u, file %s, address %s\n",
20085 line, lbasename (subfile->name),
20086 paddress (gdbarch, address));
20087 }
20088
804d2729 20089 if (cu != nullptr)
8c95582d 20090 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20091}
20092
20093/* Subroutine of dwarf_decode_lines_1 to simplify it.
20094 Mark the end of a set of line number records.
d9b3de22 20095 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20096 If SUBFILE is NULL the request is ignored. */
20097
20098static void
20099dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20100 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20101{
27e0867f
DE
20102 if (subfile == NULL)
20103 return;
20104
20105 if (dwarf_line_debug)
20106 {
20107 fprintf_unfiltered (gdb_stdlog,
20108 "Finishing current line, file %s, address %s\n",
20109 lbasename (subfile->name),
20110 paddress (gdbarch, address));
20111 }
20112
8c95582d 20113 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20114}
20115
6f77053d
PA
20116void
20117lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20118{
d9b3de22
DE
20119 if (dwarf_line_debug)
20120 {
20121 fprintf_unfiltered (gdb_stdlog,
20122 "Processing actual line %u: file %u,"
94a72be7 20123 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20124 m_line, m_file,
6f77053d 20125 paddress (m_gdbarch, m_address),
94a72be7
AB
20126 m_is_stmt, m_discriminator,
20127 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20128 }
20129
6f77053d 20130 file_entry *fe = current_file ();
8c43009f
PA
20131
20132 if (fe == NULL)
d9b3de22
DE
20133 dwarf2_debug_line_missing_file_complaint ();
20134 /* For now we ignore lines not starting on an instruction boundary.
20135 But not when processing end_sequence for compatibility with the
20136 previous version of the code. */
6f77053d 20137 else if (m_op_index == 0 || end_sequence)
d9b3de22 20138 {
8c43009f 20139 fe->included_p = 1;
8c95582d 20140 if (m_record_lines_p)
d9b3de22 20141 {
c24bdb02 20142 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20143 || end_sequence)
d9b3de22 20144 {
804d2729
TT
20145 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20146 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20147 }
20148
20149 if (!end_sequence)
20150 {
8c95582d
AB
20151 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20152
804d2729 20153 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20154 m_line_has_non_zero_discriminator,
20155 m_last_subfile))
d9b3de22 20156 {
c24bdb02 20157 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20158 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20159 builder->get_current_subfile (),
8c95582d 20160 m_line, m_address, is_stmt,
804d2729 20161 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20162 }
c24bdb02 20163 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20164 m_last_line = m_line;
d9b3de22
DE
20165 }
20166 }
20167 }
20168}
20169
804d2729
TT
20170lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20171 line_header *lh, bool record_lines_p)
d9b3de22 20172{
804d2729 20173 m_cu = cu;
6f77053d
PA
20174 m_gdbarch = arch;
20175 m_record_lines_p = record_lines_p;
20176 m_line_header = lh;
d9b3de22 20177
804d2729 20178 m_currently_recording_lines = true;
d9b3de22 20179
d9b3de22
DE
20180 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20181 was a line entry for it so that the backend has a chance to adjust it
20182 and also record it in case it needs it. This is currently used by MIPS
20183 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20184 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20185 m_is_stmt = lh->default_is_stmt;
20186 m_discriminator = 0;
252a6764
DE
20187}
20188
6f77053d
PA
20189void
20190lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20191 const gdb_byte *line_ptr,
7ab6656f 20192 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20193{
7ab6656f
OJ
20194 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20195 the pc range of the CU. However, we restrict the test to only ADDRESS
20196 values of zero to preserve GDB's previous behaviour which is to handle
20197 the specific case of a function being GC'd by the linker. */
924c2928 20198
7ab6656f 20199 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20200 {
20201 /* This line table is for a function which has been
20202 GCd by the linker. Ignore it. PR gdb/12528 */
20203
518817b3 20204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20205 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20206
b98664d3 20207 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20208 line_offset, objfile_name (objfile));
804d2729
TT
20209 m_currently_recording_lines = false;
20210 /* Note: m_currently_recording_lines is left as false until we see
20211 DW_LNE_end_sequence. */
924c2928
DE
20212 }
20213}
20214
f3f5162e 20215/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20216 Process the line number information in LH.
20217 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20218 program in order to set included_p for every referenced header. */
debd256d 20219
c906108c 20220static void
43f3e411
DE
20221dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20222 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20223{
d521ce57
TT
20224 const gdb_byte *line_ptr, *extended_end;
20225 const gdb_byte *line_end;
a8c50c1f 20226 unsigned int bytes_read, extended_len;
699ca60a 20227 unsigned char op_code, extended_op;
e142c38c 20228 CORE_ADDR baseaddr;
518817b3 20229 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20230 bfd *abfd = objfile->obfd;
08feed99 20231 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20232 /* True if we're recording line info (as opposed to building partial
20233 symtabs and just interested in finding include files mentioned by
20234 the line number program). */
20235 bool record_lines_p = !decode_for_pst_p;
e142c38c 20236
b3b3bada 20237 baseaddr = objfile->text_section_offset ();
c906108c 20238
debd256d
JB
20239 line_ptr = lh->statement_program_start;
20240 line_end = lh->statement_program_end;
c906108c
SS
20241
20242 /* Read the statement sequences until there's nothing left. */
20243 while (line_ptr < line_end)
20244 {
6f77053d
PA
20245 /* The DWARF line number program state machine. Reset the state
20246 machine at the start of each sequence. */
804d2729 20247 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20248 bool end_sequence = false;
d9b3de22 20249
8c43009f 20250 if (record_lines_p)
c906108c 20251 {
8c43009f
PA
20252 /* Start a subfile for the current file of the state
20253 machine. */
20254 const file_entry *fe = state_machine.current_file ();
20255
20256 if (fe != NULL)
804d2729 20257 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20258 }
20259
a738430d 20260 /* Decode the table. */
d9b3de22 20261 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20262 {
20263 op_code = read_1_byte (abfd, line_ptr);
20264 line_ptr += 1;
9aa1fe7e 20265
debd256d 20266 if (op_code >= lh->opcode_base)
6e70227d 20267 {
8e07a239 20268 /* Special opcode. */
6f77053d 20269 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20270 }
20271 else switch (op_code)
c906108c
SS
20272 {
20273 case DW_LNS_extended_op:
3e43a32a
MS
20274 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20275 &bytes_read);
473b7be6 20276 line_ptr += bytes_read;
a8c50c1f 20277 extended_end = line_ptr + extended_len;
c906108c
SS
20278 extended_op = read_1_byte (abfd, line_ptr);
20279 line_ptr += 1;
20280 switch (extended_op)
20281 {
20282 case DW_LNE_end_sequence:
6f77053d
PA
20283 state_machine.handle_end_sequence ();
20284 end_sequence = true;
c906108c
SS
20285 break;
20286 case DW_LNE_set_address:
d9b3de22
DE
20287 {
20288 CORE_ADDR address
c8a7a66f 20289 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20290 line_ptr += bytes_read;
6f77053d
PA
20291
20292 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20293 lowpc - baseaddr, address);
6f77053d 20294 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20295 }
c906108c
SS
20296 break;
20297 case DW_LNE_define_file:
debd256d 20298 {
d521ce57 20299 const char *cur_file;
ecfb656c
PA
20300 unsigned int mod_time, length;
20301 dir_index dindex;
6e70227d 20302
3e43a32a
MS
20303 cur_file = read_direct_string (abfd, line_ptr,
20304 &bytes_read);
debd256d 20305 line_ptr += bytes_read;
ecfb656c 20306 dindex = (dir_index)
debd256d
JB
20307 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20308 line_ptr += bytes_read;
20309 mod_time =
20310 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20311 line_ptr += bytes_read;
20312 length =
20313 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20314 line_ptr += bytes_read;
ecfb656c 20315 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20316 }
c906108c 20317 break;
d0c6ba3d 20318 case DW_LNE_set_discriminator:
6f77053d
PA
20319 {
20320 /* The discriminator is not interesting to the
20321 debugger; just ignore it. We still need to
20322 check its value though:
20323 if there are consecutive entries for the same
20324 (non-prologue) line we want to coalesce them.
20325 PR 17276. */
20326 unsigned int discr
20327 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20328 line_ptr += bytes_read;
20329
20330 state_machine.handle_set_discriminator (discr);
20331 }
d0c6ba3d 20332 break;
c906108c 20333 default:
b98664d3 20334 complaint (_("mangled .debug_line section"));
debd256d 20335 return;
c906108c 20336 }
a8c50c1f
DJ
20337 /* Make sure that we parsed the extended op correctly. If e.g.
20338 we expected a different address size than the producer used,
20339 we may have read the wrong number of bytes. */
20340 if (line_ptr != extended_end)
20341 {
b98664d3 20342 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20343 return;
20344 }
c906108c
SS
20345 break;
20346 case DW_LNS_copy:
6f77053d 20347 state_machine.handle_copy ();
c906108c
SS
20348 break;
20349 case DW_LNS_advance_pc:
2dc7f7b3
TT
20350 {
20351 CORE_ADDR adjust
20352 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20353 line_ptr += bytes_read;
6f77053d
PA
20354
20355 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20356 }
c906108c
SS
20357 break;
20358 case DW_LNS_advance_line:
a05a36a5
DE
20359 {
20360 int line_delta
20361 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20362 line_ptr += bytes_read;
6f77053d
PA
20363
20364 state_machine.handle_advance_line (line_delta);
a05a36a5 20365 }
c906108c
SS
20366 break;
20367 case DW_LNS_set_file:
d9b3de22 20368 {
6f77053d 20369 file_name_index file
ecfb656c
PA
20370 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20371 &bytes_read);
d9b3de22 20372 line_ptr += bytes_read;
8c43009f 20373
6f77053d 20374 state_machine.handle_set_file (file);
d9b3de22 20375 }
c906108c
SS
20376 break;
20377 case DW_LNS_set_column:
0ad93d4f 20378 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20379 line_ptr += bytes_read;
20380 break;
20381 case DW_LNS_negate_stmt:
6f77053d 20382 state_machine.handle_negate_stmt ();
c906108c
SS
20383 break;
20384 case DW_LNS_set_basic_block:
c906108c 20385 break;
c2c6d25f
JM
20386 /* Add to the address register of the state machine the
20387 address increment value corresponding to special opcode
a738430d
MK
20388 255. I.e., this value is scaled by the minimum
20389 instruction length since special opcode 255 would have
b021a221 20390 scaled the increment. */
c906108c 20391 case DW_LNS_const_add_pc:
6f77053d 20392 state_machine.handle_const_add_pc ();
c906108c
SS
20393 break;
20394 case DW_LNS_fixed_advance_pc:
3e29f34a 20395 {
6f77053d 20396 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20397 line_ptr += 2;
6f77053d
PA
20398
20399 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20400 }
c906108c 20401 break;
9aa1fe7e 20402 default:
a738430d
MK
20403 {
20404 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20405 int i;
a738430d 20406
debd256d 20407 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20408 {
20409 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20410 line_ptr += bytes_read;
20411 }
20412 }
c906108c
SS
20413 }
20414 }
d9b3de22
DE
20415
20416 if (!end_sequence)
20417 dwarf2_debug_line_missing_end_sequence_complaint ();
20418
20419 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20420 in which case we still finish recording the last line). */
6f77053d 20421 state_machine.record_line (true);
c906108c 20422 }
f3f5162e
DE
20423}
20424
20425/* Decode the Line Number Program (LNP) for the given line_header
20426 structure and CU. The actual information extracted and the type
20427 of structures created from the LNP depends on the value of PST.
20428
20429 1. If PST is NULL, then this procedure uses the data from the program
20430 to create all necessary symbol tables, and their linetables.
20431
20432 2. If PST is not NULL, this procedure reads the program to determine
20433 the list of files included by the unit represented by PST, and
20434 builds all the associated partial symbol tables.
20435
20436 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20437 It is used for relative paths in the line table.
20438 NOTE: When processing partial symtabs (pst != NULL),
20439 comp_dir == pst->dirname.
20440
20441 NOTE: It is important that psymtabs have the same file name (via strcmp)
20442 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20443 symtab we don't use it in the name of the psymtabs we create.
20444 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20445 A good testcase for this is mb-inline.exp.
20446
527f3840
JK
20447 LOWPC is the lowest address in CU (or 0 if not known).
20448
20449 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20450 for its PC<->lines mapping information. Otherwise only the filename
20451 table is read in. */
f3f5162e
DE
20452
20453static void
20454dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20455 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20456 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20457{
518817b3 20458 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20459 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20460
527f3840
JK
20461 if (decode_mapping)
20462 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20463
20464 if (decode_for_pst_p)
20465 {
aaa75496
JB
20466 /* Now that we're done scanning the Line Header Program, we can
20467 create the psymtab of each included file. */
7ba99d21
AT
20468 for (auto &file_entry : lh->file_names ())
20469 if (file_entry.included_p == 1)
aaa75496 20470 {
c89b44cd 20471 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20472 const char *include_name =
7ba99d21
AT
20473 psymtab_include_file_name (lh, file_entry, pst,
20474 comp_dir, &name_holder);
c6da4cef 20475 if (include_name != NULL)
aaa75496
JB
20476 dwarf2_create_include_psymtab (include_name, pst, objfile);
20477 }
20478 }
cb1df416
DJ
20479 else
20480 {
20481 /* Make sure a symtab is created for every file, even files
20482 which contain only variables (i.e. no code with associated
20483 line numbers). */
c24bdb02
KS
20484 buildsym_compunit *builder = cu->get_builder ();
20485 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20486
7ba99d21 20487 for (auto &fe : lh->file_names ())
cb1df416 20488 {
804d2729 20489 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20490 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20491 {
c24bdb02 20492 builder->get_current_subfile ()->symtab
804d2729 20493 = allocate_symtab (cust,
c24bdb02 20494 builder->get_current_subfile ()->name);
43f3e411 20495 }
c24bdb02 20496 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20497 }
20498 }
c906108c
SS
20499}
20500
20501/* Start a subfile for DWARF. FILENAME is the name of the file and
20502 DIRNAME the name of the source directory which contains FILENAME
4d663531 20503 or NULL if not known.
c906108c
SS
20504 This routine tries to keep line numbers from identical absolute and
20505 relative file names in a common subfile.
20506
20507 Using the `list' example from the GDB testsuite, which resides in
20508 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20509 of /srcdir/list0.c yields the following debugging information for list0.c:
20510
c5aa993b 20511 DW_AT_name: /srcdir/list0.c
4d663531 20512 DW_AT_comp_dir: /compdir
357e46e7 20513 files.files[0].name: list0.h
c5aa993b 20514 files.files[0].dir: /srcdir
357e46e7 20515 files.files[1].name: list0.c
c5aa993b 20516 files.files[1].dir: /srcdir
c906108c
SS
20517
20518 The line number information for list0.c has to end up in a single
4f1520fb
FR
20519 subfile, so that `break /srcdir/list0.c:1' works as expected.
20520 start_subfile will ensure that this happens provided that we pass the
20521 concatenation of files.files[1].dir and files.files[1].name as the
20522 subfile's name. */
c906108c
SS
20523
20524static void
804d2729
TT
20525dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20526 const char *dirname)
c906108c 20527{
43816ebc 20528 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20529
4d663531 20530 /* In order not to lose the line information directory,
4f1520fb
FR
20531 we concatenate it to the filename when it makes sense.
20532 Note that the Dwarf3 standard says (speaking of filenames in line
20533 information): ``The directory index is ignored for file names
20534 that represent full path names''. Thus ignoring dirname in the
20535 `else' branch below isn't an issue. */
c906108c 20536
d5166ae1 20537 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20538 {
43816ebc
TT
20539 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20540 filename = copy.get ();
d521ce57 20541 }
c906108c 20542
c24bdb02 20543 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20544}
20545
804d2729
TT
20546/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20547 buildsym_compunit constructor. */
f4dc4d17 20548
c24bdb02
KS
20549struct compunit_symtab *
20550dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20551 CORE_ADDR low_pc)
f4dc4d17 20552{
c24bdb02 20553 gdb_assert (m_builder == nullptr);
43f3e411 20554
c24bdb02
KS
20555 m_builder.reset (new struct buildsym_compunit
20556 (per_cu->dwarf2_per_objfile->objfile,
20557 name, comp_dir, language, low_pc));
93b8bea4 20558
c24bdb02 20559 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20560
c24bdb02
KS
20561 get_builder ()->record_debugformat ("DWARF 2");
20562 get_builder ()->record_producer (producer);
f4dc4d17 20563
c24bdb02 20564 processing_has_namespace_info = false;
43f3e411 20565
c24bdb02 20566 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20567}
20568
4c2df51b
DJ
20569static void
20570var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20571 struct dwarf2_cu *cu)
4c2df51b 20572{
518817b3 20573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20574 struct comp_unit_head *cu_header = &cu->header;
20575
4c2df51b
DJ
20576 /* NOTE drow/2003-01-30: There used to be a comment and some special
20577 code here to turn a symbol with DW_AT_external and a
20578 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20579 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20580 with some versions of binutils) where shared libraries could have
20581 relocations against symbols in their debug information - the
20582 minimal symbol would have the right address, but the debug info
20583 would not. It's no longer necessary, because we will explicitly
20584 apply relocations when we read in the debug information now. */
20585
20586 /* A DW_AT_location attribute with no contents indicates that a
20587 variable has been optimized away. */
4fc6c0d5 20588 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20589 {
f1e6e072 20590 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20591 return;
20592 }
20593
20594 /* Handle one degenerate form of location expression specially, to
20595 preserve GDB's previous behavior when section offsets are
336d760d
AT
20596 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20597 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20598
4fc6c0d5 20599 if (attr->form_is_block ()
3019eac3
DE
20600 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20601 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20602 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20603 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20604 && (DW_BLOCK (attr)->size
20605 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20606 {
891d2f0b 20607 unsigned int dummy;
4c2df51b 20608
3019eac3 20609 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20610 SET_SYMBOL_VALUE_ADDRESS
20611 (sym, cu->header.read_address (objfile->obfd,
20612 DW_BLOCK (attr)->data + 1,
20613 &dummy));
3019eac3 20614 else
38583298
TT
20615 SET_SYMBOL_VALUE_ADDRESS
20616 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20617 &dummy));
f1e6e072 20618 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20619 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20620 SET_SYMBOL_VALUE_ADDRESS
20621 (sym,
20622 SYMBOL_VALUE_ADDRESS (sym)
20623 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20624 return;
20625 }
20626
20627 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20628 expression evaluator, and use LOC_COMPUTED only when necessary
20629 (i.e. when the value of a register or memory location is
20630 referenced, or a thread-local block, etc.). Then again, it might
20631 not be worthwhile. I'm assuming that it isn't unless performance
20632 or memory numbers show me otherwise. */
20633
f1e6e072 20634 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20635
f1e6e072 20636 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20637 cu->has_loclist = true;
4c2df51b
DJ
20638}
20639
c906108c
SS
20640/* Given a pointer to a DWARF information entry, figure out if we need
20641 to make a symbol table entry for it, and if so, create a new entry
20642 and return a pointer to it.
20643 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20644 used the passed type.
20645 If SPACE is not NULL, use it to hold the new symbol. If it is
20646 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20647
20648static struct symbol *
5e2db402
TT
20649new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20650 struct symbol *space)
c906108c 20651{
518817b3
SM
20652 struct dwarf2_per_objfile *dwarf2_per_objfile
20653 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20654 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20655 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20656 struct symbol *sym = NULL;
15d034d0 20657 const char *name;
c906108c
SS
20658 struct attribute *attr = NULL;
20659 struct attribute *attr2 = NULL;
e142c38c 20660 CORE_ADDR baseaddr;
e37fd15a
SW
20661 struct pending **list_to_add = NULL;
20662
edb3359d 20663 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20664
b3b3bada 20665 baseaddr = objfile->text_section_offset ();
c906108c 20666
94af9270 20667 name = dwarf2_name (die, cu);
c906108c
SS
20668 if (name)
20669 {
34eaf542 20670 int suppress_add = 0;
94af9270 20671
34eaf542
TT
20672 if (space)
20673 sym = space;
20674 else
8c14c3a3 20675 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20676 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20677
20678 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20679 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20680 /* Fortran does not have mangling standard and the mangling does differ
20681 between gfortran, iFort etc. */
bcfe6157
TT
20682 const char *physname
20683 = (cu->language == language_fortran
20684 ? dwarf2_full_name (name, die, cu)
20685 : dwarf2_physname (name, die, cu));
20686 const char *linkagename = dw2_linkage_name (die, cu);
20687
20688 if (linkagename == nullptr || cu->language == language_ada)
20689 sym->set_linkage_name (physname);
20690 else
20691 {
20692 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20693 sym->set_linkage_name (linkagename);
20694 }
f55ee35c 20695
c906108c 20696 /* Default assumptions.
c5aa993b 20697 Use the passed type or decode it from the die. */
176620f1 20698 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20699 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20700 if (type != NULL)
20701 SYMBOL_TYPE (sym) = type;
20702 else
e7c27a73 20703 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20704 attr = dwarf2_attr (die,
20705 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20706 cu);
435d3d88 20707 if (attr != nullptr)
c906108c
SS
20708 {
20709 SYMBOL_LINE (sym) = DW_UNSND (attr);
20710 }
cb1df416 20711
edb3359d
DJ
20712 attr = dwarf2_attr (die,
20713 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20714 cu);
435d3d88 20715 if (attr != nullptr)
cb1df416 20716 {
ecfb656c 20717 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20718 struct file_entry *fe;
9a619af0 20719
ecfb656c
PA
20720 if (cu->line_header != NULL)
20721 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20722 else
20723 fe = NULL;
20724
20725 if (fe == NULL)
b98664d3 20726 complaint (_("file index out of range"));
8c43009f
PA
20727 else
20728 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20729 }
20730
c906108c
SS
20731 switch (die->tag)
20732 {
20733 case DW_TAG_label:
e142c38c 20734 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20735 if (attr != nullptr)
3e29f34a
MR
20736 {
20737 CORE_ADDR addr;
20738
cd6c91b4 20739 addr = attr->value_as_address ();
3e29f34a 20740 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20741 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20742 }
0f5238ed
TT
20743 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20744 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20745 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20746 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20747 break;
20748 case DW_TAG_subprogram:
20749 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20750 finish_block. */
f1e6e072 20751 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20752 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20753 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20754 || cu->language == language_ada
20755 || cu->language == language_fortran)
c906108c 20756 {
2cfa0c8d 20757 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20758 Ada and Fortran subprograms, whether marked external or
20759 not, are always stored as a global symbol, because we want
20760 to be able to access them globally. For instance, we want
20761 to be able to break on a nested subprogram without having
20762 to specify the context. */
c24bdb02 20763 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20764 }
20765 else
20766 {
e37fd15a 20767 list_to_add = cu->list_in_scope;
c906108c
SS
20768 }
20769 break;
edb3359d
DJ
20770 case DW_TAG_inlined_subroutine:
20771 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20772 finish_block. */
f1e6e072 20773 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20774 SYMBOL_INLINED (sym) = 1;
481860b3 20775 list_to_add = cu->list_in_scope;
edb3359d 20776 break;
34eaf542
TT
20777 case DW_TAG_template_value_param:
20778 suppress_add = 1;
20779 /* Fall through. */
72929c62 20780 case DW_TAG_constant:
c906108c 20781 case DW_TAG_variable:
254e6b9e 20782 case DW_TAG_member:
0963b4bd
MS
20783 /* Compilation with minimal debug info may result in
20784 variables with missing type entries. Change the
20785 misleading `void' type to something sensible. */
78134374 20786 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20787 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20788
e142c38c 20789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20790 /* In the case of DW_TAG_member, we should only be called for
20791 static const members. */
20792 if (die->tag == DW_TAG_member)
20793 {
3863f96c
DE
20794 /* dwarf2_add_field uses die_is_declaration,
20795 so we do the same. */
254e6b9e
DE
20796 gdb_assert (die_is_declaration (die, cu));
20797 gdb_assert (attr);
20798 }
435d3d88 20799 if (attr != nullptr)
c906108c 20800 {
e7c27a73 20801 dwarf2_const_value (attr, sym, cu);
e142c38c 20802 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20803 if (!suppress_add)
34eaf542
TT
20804 {
20805 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20806 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20807 else
e37fd15a 20808 list_to_add = cu->list_in_scope;
34eaf542 20809 }
c906108c
SS
20810 break;
20811 }
e142c38c 20812 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20813 if (attr != nullptr)
c906108c 20814 {
e7c27a73 20815 var_decode_location (attr, sym, cu);
e142c38c 20816 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20817
20818 /* Fortran explicitly imports any global symbols to the local
20819 scope by DW_TAG_common_block. */
20820 if (cu->language == language_fortran && die->parent
20821 && die->parent->tag == DW_TAG_common_block)
20822 attr2 = NULL;
20823
caac4577
JG
20824 if (SYMBOL_CLASS (sym) == LOC_STATIC
20825 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20826 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20827 {
20828 /* When a static variable is eliminated by the linker,
20829 the corresponding debug information is not stripped
20830 out, but the variable address is set to null;
20831 do not add such variables into symbol table. */
20832 }
20833 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20834 {
4b610737
TT
20835 if (SYMBOL_CLASS (sym) == LOC_STATIC
20836 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20837 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20838 {
20839 /* A global static variable might be subject to
20840 copy relocation. We first check for a local
20841 minsym, though, because maybe the symbol was
20842 marked hidden, in which case this would not
20843 apply. */
20844 bound_minimal_symbol found
20845 = (lookup_minimal_symbol_linkage
987012b8 20846 (sym->linkage_name (), objfile));
4b610737
TT
20847 if (found.minsym != nullptr)
20848 sym->maybe_copied = 1;
20849 }
f55ee35c 20850
1c809c68
TT
20851 /* A variable with DW_AT_external is never static,
20852 but it may be block-scoped. */
804d2729 20853 list_to_add
c24bdb02
KS
20854 = ((cu->list_in_scope
20855 == cu->get_builder ()->get_file_symbols ())
20856 ? cu->get_builder ()->get_global_symbols ()
804d2729 20857 : cu->list_in_scope);
1c809c68 20858 }
c906108c 20859 else
e37fd15a 20860 list_to_add = cu->list_in_scope;
c906108c
SS
20861 }
20862 else
20863 {
20864 /* We do not know the address of this symbol.
c5aa993b
JM
20865 If it is an external symbol and we have type information
20866 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20867 The address of the variable will then be determined from
20868 the minimal symbol table whenever the variable is
20869 referenced. */
e142c38c 20870 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20871
20872 /* Fortran explicitly imports any global symbols to the local
20873 scope by DW_TAG_common_block. */
20874 if (cu->language == language_fortran && die->parent
20875 && die->parent->tag == DW_TAG_common_block)
20876 {
20877 /* SYMBOL_CLASS doesn't matter here because
20878 read_common_block is going to reset it. */
20879 if (!suppress_add)
20880 list_to_add = cu->list_in_scope;
20881 }
20882 else if (attr2 && (DW_UNSND (attr2) != 0)
20883 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20884 {
0fe7935b
DJ
20885 /* A variable with DW_AT_external is never static, but it
20886 may be block-scoped. */
804d2729 20887 list_to_add
c24bdb02
KS
20888 = ((cu->list_in_scope
20889 == cu->get_builder ()->get_file_symbols ())
20890 ? cu->get_builder ()->get_global_symbols ()
804d2729 20891 : cu->list_in_scope);
0fe7935b 20892
f1e6e072 20893 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20894 }
442ddf59
JK
20895 else if (!die_is_declaration (die, cu))
20896 {
20897 /* Use the default LOC_OPTIMIZED_OUT class. */
20898 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20899 if (!suppress_add)
20900 list_to_add = cu->list_in_scope;
442ddf59 20901 }
c906108c
SS
20902 }
20903 break;
20904 case DW_TAG_formal_parameter:
a60f3166
TT
20905 {
20906 /* If we are inside a function, mark this as an argument. If
20907 not, we might be looking at an argument to an inlined function
20908 when we do not have enough information to show inlined frames;
20909 pretend it's a local variable in that case so that the user can
20910 still see it. */
804d2729 20911 struct context_stack *curr
c24bdb02 20912 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20913 if (curr != nullptr && curr->name != nullptr)
20914 SYMBOL_IS_ARGUMENT (sym) = 1;
20915 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20916 if (attr != nullptr)
a60f3166
TT
20917 {
20918 var_decode_location (attr, sym, cu);
20919 }
20920 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20921 if (attr != nullptr)
a60f3166
TT
20922 {
20923 dwarf2_const_value (attr, sym, cu);
20924 }
f346a30d 20925
a60f3166
TT
20926 list_to_add = cu->list_in_scope;
20927 }
c906108c
SS
20928 break;
20929 case DW_TAG_unspecified_parameters:
20930 /* From varargs functions; gdb doesn't seem to have any
20931 interest in this information, so just ignore it for now.
20932 (FIXME?) */
20933 break;
34eaf542
TT
20934 case DW_TAG_template_type_param:
20935 suppress_add = 1;
20936 /* Fall through. */
c906108c 20937 case DW_TAG_class_type:
680b30c7 20938 case DW_TAG_interface_type:
c906108c
SS
20939 case DW_TAG_structure_type:
20940 case DW_TAG_union_type:
72019c9c 20941 case DW_TAG_set_type:
c906108c 20942 case DW_TAG_enumeration_type:
f1e6e072 20943 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20944 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20945
63d06c5c 20946 {
9c37b5ae 20947 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20948 really ever be static objects: otherwise, if you try
20949 to, say, break of a class's method and you're in a file
20950 which doesn't mention that class, it won't work unless
20951 the check for all static symbols in lookup_symbol_aux
20952 saves you. See the OtherFileClass tests in
20953 gdb.c++/namespace.exp. */
20954
e37fd15a 20955 if (!suppress_add)
34eaf542 20956 {
c24bdb02 20957 buildsym_compunit *builder = cu->get_builder ();
804d2729 20958 list_to_add
c24bdb02 20959 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20960 && cu->language == language_cplus
c24bdb02 20961 ? builder->get_global_symbols ()
804d2729 20962 : cu->list_in_scope);
63d06c5c 20963
64382290 20964 /* The semantics of C++ state that "struct foo {
9c37b5ae 20965 ... }" also defines a typedef for "foo". */
64382290 20966 if (cu->language == language_cplus
45280282 20967 || cu->language == language_ada
c44af4eb
TT
20968 || cu->language == language_d
20969 || cu->language == language_rust)
64382290
TT
20970 {
20971 /* The symbol's name is already allocated along
20972 with this objfile, so we don't need to
20973 duplicate it for the type. */
7d93a1e0 20974 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20975 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20976 }
63d06c5c
DC
20977 }
20978 }
c906108c
SS
20979 break;
20980 case DW_TAG_typedef:
f1e6e072 20981 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20982 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20983 list_to_add = cu->list_in_scope;
63d06c5c 20984 break;
c906108c 20985 case DW_TAG_base_type:
a02abb62 20986 case DW_TAG_subrange_type:
f1e6e072 20987 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20988 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20989 list_to_add = cu->list_in_scope;
c906108c
SS
20990 break;
20991 case DW_TAG_enumerator:
e142c38c 20992 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20993 if (attr != nullptr)
c906108c 20994 {
e7c27a73 20995 dwarf2_const_value (attr, sym, cu);
c906108c 20996 }
63d06c5c
DC
20997 {
20998 /* NOTE: carlton/2003-11-10: See comment above in the
20999 DW_TAG_class_type, etc. block. */
21000
804d2729 21001 list_to_add
c24bdb02 21002 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21003 && cu->language == language_cplus
c24bdb02 21004 ? cu->get_builder ()->get_global_symbols ()
804d2729 21005 : cu->list_in_scope);
63d06c5c 21006 }
c906108c 21007 break;
74921315 21008 case DW_TAG_imported_declaration:
5c4e30ca 21009 case DW_TAG_namespace:
f1e6e072 21010 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21011 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21012 break;
530e8392
KB
21013 case DW_TAG_module:
21014 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21015 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21016 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21017 break;
4357ac6c 21018 case DW_TAG_common_block:
f1e6e072 21019 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21020 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21021 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21022 break;
c906108c
SS
21023 default:
21024 /* Not a tag we recognize. Hopefully we aren't processing
21025 trash data, but since we must specifically ignore things
21026 we don't recognize, there is nothing else we should do at
0963b4bd 21027 this point. */
b98664d3 21028 complaint (_("unsupported tag: '%s'"),
4d3c2250 21029 dwarf_tag_name (die->tag));
c906108c
SS
21030 break;
21031 }
df8a16a1 21032
e37fd15a
SW
21033 if (suppress_add)
21034 {
21035 sym->hash_next = objfile->template_symbols;
21036 objfile->template_symbols = sym;
21037 list_to_add = NULL;
21038 }
21039
21040 if (list_to_add != NULL)
d3cb6808 21041 add_symbol_to_list (sym, list_to_add);
e37fd15a 21042
df8a16a1
DJ
21043 /* For the benefit of old versions of GCC, check for anonymous
21044 namespaces based on the demangled name. */
4d4ec4e5 21045 if (!cu->processing_has_namespace_info
94af9270 21046 && cu->language == language_cplus)
c24bdb02 21047 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21048 }
21049 return (sym);
21050}
21051
98bfdba5
PA
21052/* Given an attr with a DW_FORM_dataN value in host byte order,
21053 zero-extend it as appropriate for the symbol's type. The DWARF
21054 standard (v4) is not entirely clear about the meaning of using
21055 DW_FORM_dataN for a constant with a signed type, where the type is
21056 wider than the data. The conclusion of a discussion on the DWARF
21057 list was that this is unspecified. We choose to always zero-extend
21058 because that is the interpretation long in use by GCC. */
c906108c 21059
98bfdba5 21060static gdb_byte *
ff39bb5e 21061dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21062 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21063{
518817b3 21064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21065 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21066 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21067 LONGEST l = DW_UNSND (attr);
21068
21069 if (bits < sizeof (*value) * 8)
21070 {
21071 l &= ((LONGEST) 1 << bits) - 1;
21072 *value = l;
21073 }
21074 else if (bits == sizeof (*value) * 8)
21075 *value = l;
21076 else
21077 {
224c3ddb 21078 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21079 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21080 return bytes;
21081 }
21082
21083 return NULL;
21084}
21085
21086/* Read a constant value from an attribute. Either set *VALUE, or if
21087 the value does not fit in *VALUE, set *BYTES - either already
21088 allocated on the objfile obstack, or newly allocated on OBSTACK,
21089 or, set *BATON, if we translated the constant to a location
21090 expression. */
21091
21092static void
ff39bb5e 21093dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21094 const char *name, struct obstack *obstack,
21095 struct dwarf2_cu *cu,
d521ce57 21096 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21097 struct dwarf2_locexpr_baton **baton)
21098{
a50264ba
TT
21099 dwarf2_per_objfile *per_objfile = cu->per_cu->dwarf2_per_objfile;
21100 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21101 struct comp_unit_head *cu_header = &cu->header;
c906108c 21102 struct dwarf_block *blk;
98bfdba5
PA
21103 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21104 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21105
21106 *value = 0;
21107 *bytes = NULL;
21108 *baton = NULL;
c906108c
SS
21109
21110 switch (attr->form)
21111 {
21112 case DW_FORM_addr:
336d760d 21113 case DW_FORM_addrx:
3019eac3 21114 case DW_FORM_GNU_addr_index:
ac56253d 21115 {
ac56253d
TT
21116 gdb_byte *data;
21117
98bfdba5
PA
21118 if (TYPE_LENGTH (type) != cu_header->addr_size)
21119 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21120 cu_header->addr_size,
98bfdba5 21121 TYPE_LENGTH (type));
ac56253d
TT
21122 /* Symbols of this form are reasonably rare, so we just
21123 piggyback on the existing location code rather than writing
21124 a new implementation of symbol_computed_ops. */
8d749320 21125 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21126 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21127 (*baton)->per_cu = cu->per_cu;
21128 gdb_assert ((*baton)->per_cu);
ac56253d 21129
98bfdba5 21130 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21131 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21132 (*baton)->data = data;
ac56253d
TT
21133
21134 data[0] = DW_OP_addr;
21135 store_unsigned_integer (&data[1], cu_header->addr_size,
21136 byte_order, DW_ADDR (attr));
21137 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21138 }
c906108c 21139 break;
4ac36638 21140 case DW_FORM_string:
93b5768b 21141 case DW_FORM_strp:
cf532bd1 21142 case DW_FORM_strx:
3019eac3 21143 case DW_FORM_GNU_str_index:
36586728 21144 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21145 /* DW_STRING is already allocated on the objfile obstack, point
21146 directly to it. */
d521ce57 21147 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21148 break;
c906108c
SS
21149 case DW_FORM_block1:
21150 case DW_FORM_block2:
21151 case DW_FORM_block4:
21152 case DW_FORM_block:
2dc7f7b3 21153 case DW_FORM_exprloc:
0224619f 21154 case DW_FORM_data16:
c906108c 21155 blk = DW_BLOCK (attr);
98bfdba5
PA
21156 if (TYPE_LENGTH (type) != blk->size)
21157 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21158 TYPE_LENGTH (type));
21159 *bytes = blk->data;
c906108c 21160 break;
2df3850c
JM
21161
21162 /* The DW_AT_const_value attributes are supposed to carry the
21163 symbol's value "represented as it would be on the target
21164 architecture." By the time we get here, it's already been
21165 converted to host endianness, so we just need to sign- or
21166 zero-extend it as appropriate. */
21167 case DW_FORM_data1:
3aef2284 21168 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21169 break;
c906108c 21170 case DW_FORM_data2:
3aef2284 21171 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21172 break;
c906108c 21173 case DW_FORM_data4:
3aef2284 21174 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21175 break;
c906108c 21176 case DW_FORM_data8:
3aef2284 21177 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21178 break;
21179
c906108c 21180 case DW_FORM_sdata:
663c44ac 21181 case DW_FORM_implicit_const:
98bfdba5 21182 *value = DW_SND (attr);
2df3850c
JM
21183 break;
21184
c906108c 21185 case DW_FORM_udata:
98bfdba5 21186 *value = DW_UNSND (attr);
c906108c 21187 break;
2df3850c 21188
c906108c 21189 default:
b98664d3 21190 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21191 dwarf_form_name (attr->form));
98bfdba5 21192 *value = 0;
c906108c
SS
21193 break;
21194 }
21195}
21196
2df3850c 21197
98bfdba5
PA
21198/* Copy constant value from an attribute to a symbol. */
21199
2df3850c 21200static void
ff39bb5e 21201dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21202 struct dwarf2_cu *cu)
2df3850c 21203{
518817b3 21204 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21205 LONGEST value;
d521ce57 21206 const gdb_byte *bytes;
98bfdba5 21207 struct dwarf2_locexpr_baton *baton;
2df3850c 21208
98bfdba5 21209 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21210 sym->print_name (),
98bfdba5
PA
21211 &objfile->objfile_obstack, cu,
21212 &value, &bytes, &baton);
2df3850c 21213
98bfdba5
PA
21214 if (baton != NULL)
21215 {
98bfdba5 21216 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21217 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21218 }
21219 else if (bytes != NULL)
21220 {
21221 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21222 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21223 }
21224 else
21225 {
21226 SYMBOL_VALUE (sym) = value;
f1e6e072 21227 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21228 }
2df3850c
JM
21229}
21230
c906108c
SS
21231/* Return the type of the die in question using its DW_AT_type attribute. */
21232
21233static struct type *
e7c27a73 21234die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21235{
c906108c 21236 struct attribute *type_attr;
c906108c 21237
e142c38c 21238 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21239 if (!type_attr)
21240 {
518817b3 21241 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21242 /* A missing DW_AT_type represents a void type. */
518817b3 21243 return objfile_type (objfile)->builtin_void;
c906108c 21244 }
348e048f 21245
673bfd45 21246 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21247}
21248
b4ba55a1
JB
21249/* True iff CU's producer generates GNAT Ada auxiliary information
21250 that allows to find parallel types through that information instead
21251 of having to do expensive parallel lookups by type name. */
21252
21253static int
21254need_gnat_info (struct dwarf2_cu *cu)
21255{
de4cb04a
JB
21256 /* Assume that the Ada compiler was GNAT, which always produces
21257 the auxiliary information. */
21258 return (cu->language == language_ada);
b4ba55a1
JB
21259}
21260
b4ba55a1
JB
21261/* Return the auxiliary type of the die in question using its
21262 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21263 attribute is not present. */
21264
21265static struct type *
21266die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21267{
b4ba55a1 21268 struct attribute *type_attr;
b4ba55a1
JB
21269
21270 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21271 if (!type_attr)
21272 return NULL;
21273
673bfd45 21274 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21275}
21276
21277/* If DIE has a descriptive_type attribute, then set the TYPE's
21278 descriptive type accordingly. */
21279
21280static void
21281set_descriptive_type (struct type *type, struct die_info *die,
21282 struct dwarf2_cu *cu)
21283{
21284 struct type *descriptive_type = die_descriptive_type (die, cu);
21285
21286 if (descriptive_type)
21287 {
21288 ALLOCATE_GNAT_AUX_TYPE (type);
21289 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21290 }
21291}
21292
c906108c
SS
21293/* Return the containing type of the die in question using its
21294 DW_AT_containing_type attribute. */
21295
21296static struct type *
e7c27a73 21297die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21298{
c906108c 21299 struct attribute *type_attr;
518817b3 21300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21301
e142c38c 21302 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21303 if (!type_attr)
21304 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21305 "[in module %s]"), objfile_name (objfile));
33ac96f0 21306
673bfd45 21307 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21308}
21309
ac9ec31b
DE
21310/* Return an error marker type to use for the ill formed type in DIE/CU. */
21311
21312static struct type *
21313build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21314{
518817b3
SM
21315 struct dwarf2_per_objfile *dwarf2_per_objfile
21316 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21317 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21318 char *saved;
ac9ec31b 21319
528e1572
SM
21320 std::string message
21321 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21322 objfile_name (objfile),
21323 sect_offset_str (cu->header.sect_off),
21324 sect_offset_str (die->sect_off));
efba19b0 21325 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21326
19f392bc 21327 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21328}
21329
673bfd45 21330/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21331 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21332 DW_AT_containing_type.
673bfd45
DE
21333 If there is no type substitute an error marker. */
21334
c906108c 21335static struct type *
ff39bb5e 21336lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21337 struct dwarf2_cu *cu)
c906108c 21338{
518817b3
SM
21339 struct dwarf2_per_objfile *dwarf2_per_objfile
21340 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21341 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21342 struct type *this_type;
21343
ac9ec31b
DE
21344 gdb_assert (attr->name == DW_AT_type
21345 || attr->name == DW_AT_GNAT_descriptive_type
21346 || attr->name == DW_AT_containing_type);
21347
673bfd45
DE
21348 /* First see if we have it cached. */
21349
36586728
TT
21350 if (attr->form == DW_FORM_GNU_ref_alt)
21351 {
21352 struct dwarf2_per_cu_data *per_cu;
0826b30a 21353 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21354
ed2dc618
SM
21355 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21356 dwarf2_per_objfile);
9c541725 21357 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21358 }
cd6c91b4 21359 else if (attr->form_is_ref ())
673bfd45 21360 {
0826b30a 21361 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21362
9c541725 21363 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21364 }
55f1336d 21365 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21366 {
ac9ec31b 21367 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21368
ac9ec31b 21369 return get_signatured_type (die, signature, cu);
673bfd45
DE
21370 }
21371 else
21372 {
b98664d3 21373 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21374 " at %s [in module %s]"),
21375 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21376 objfile_name (objfile));
ac9ec31b 21377 return build_error_marker_type (cu, die);
673bfd45
DE
21378 }
21379
21380 /* If not cached we need to read it in. */
21381
21382 if (this_type == NULL)
21383 {
ac9ec31b 21384 struct die_info *type_die = NULL;
673bfd45
DE
21385 struct dwarf2_cu *type_cu = cu;
21386
cd6c91b4 21387 if (attr->form_is_ref ())
ac9ec31b
DE
21388 type_die = follow_die_ref (die, attr, &type_cu);
21389 if (type_die == NULL)
21390 return build_error_marker_type (cu, die);
21391 /* If we find the type now, it's probably because the type came
3019eac3
DE
21392 from an inter-CU reference and the type's CU got expanded before
21393 ours. */
ac9ec31b 21394 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21395 }
21396
21397 /* If we still don't have a type use an error marker. */
21398
21399 if (this_type == NULL)
ac9ec31b 21400 return build_error_marker_type (cu, die);
673bfd45 21401
f792889a 21402 return this_type;
c906108c
SS
21403}
21404
673bfd45
DE
21405/* Return the type in DIE, CU.
21406 Returns NULL for invalid types.
21407
02142a6c 21408 This first does a lookup in die_type_hash,
673bfd45
DE
21409 and only reads the die in if necessary.
21410
21411 NOTE: This can be called when reading in partial or full symbols. */
21412
f792889a 21413static struct type *
e7c27a73 21414read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21415{
f792889a
DJ
21416 struct type *this_type;
21417
21418 this_type = get_die_type (die, cu);
21419 if (this_type)
21420 return this_type;
21421
673bfd45
DE
21422 return read_type_die_1 (die, cu);
21423}
21424
21425/* Read the type in DIE, CU.
21426 Returns NULL for invalid types. */
21427
21428static struct type *
21429read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21430{
21431 struct type *this_type = NULL;
21432
c906108c
SS
21433 switch (die->tag)
21434 {
21435 case DW_TAG_class_type:
680b30c7 21436 case DW_TAG_interface_type:
c906108c
SS
21437 case DW_TAG_structure_type:
21438 case DW_TAG_union_type:
f792889a 21439 this_type = read_structure_type (die, cu);
c906108c
SS
21440 break;
21441 case DW_TAG_enumeration_type:
f792889a 21442 this_type = read_enumeration_type (die, cu);
c906108c
SS
21443 break;
21444 case DW_TAG_subprogram:
21445 case DW_TAG_subroutine_type:
edb3359d 21446 case DW_TAG_inlined_subroutine:
f792889a 21447 this_type = read_subroutine_type (die, cu);
c906108c
SS
21448 break;
21449 case DW_TAG_array_type:
f792889a 21450 this_type = read_array_type (die, cu);
c906108c 21451 break;
72019c9c 21452 case DW_TAG_set_type:
f792889a 21453 this_type = read_set_type (die, cu);
72019c9c 21454 break;
c906108c 21455 case DW_TAG_pointer_type:
f792889a 21456 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21457 break;
21458 case DW_TAG_ptr_to_member_type:
f792889a 21459 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21460 break;
21461 case DW_TAG_reference_type:
4297a3f0
AV
21462 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21463 break;
21464 case DW_TAG_rvalue_reference_type:
21465 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21466 break;
21467 case DW_TAG_const_type:
f792889a 21468 this_type = read_tag_const_type (die, cu);
c906108c
SS
21469 break;
21470 case DW_TAG_volatile_type:
f792889a 21471 this_type = read_tag_volatile_type (die, cu);
c906108c 21472 break;
06d66ee9
TT
21473 case DW_TAG_restrict_type:
21474 this_type = read_tag_restrict_type (die, cu);
21475 break;
c906108c 21476 case DW_TAG_string_type:
f792889a 21477 this_type = read_tag_string_type (die, cu);
c906108c
SS
21478 break;
21479 case DW_TAG_typedef:
f792889a 21480 this_type = read_typedef (die, cu);
c906108c 21481 break;
a02abb62 21482 case DW_TAG_subrange_type:
f792889a 21483 this_type = read_subrange_type (die, cu);
a02abb62 21484 break;
c906108c 21485 case DW_TAG_base_type:
f792889a 21486 this_type = read_base_type (die, cu);
c906108c 21487 break;
81a17f79 21488 case DW_TAG_unspecified_type:
f792889a 21489 this_type = read_unspecified_type (die, cu);
81a17f79 21490 break;
0114d602
DJ
21491 case DW_TAG_namespace:
21492 this_type = read_namespace_type (die, cu);
21493 break;
f55ee35c
JK
21494 case DW_TAG_module:
21495 this_type = read_module_type (die, cu);
21496 break;
a2c2acaf
MW
21497 case DW_TAG_atomic_type:
21498 this_type = read_tag_atomic_type (die, cu);
21499 break;
c906108c 21500 default:
b98664d3 21501 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21502 dwarf_tag_name (die->tag));
c906108c
SS
21503 break;
21504 }
63d06c5c 21505
f792889a 21506 return this_type;
63d06c5c
DC
21507}
21508
abc72ce4
DE
21509/* See if we can figure out if the class lives in a namespace. We do
21510 this by looking for a member function; its demangled name will
21511 contain namespace info, if there is any.
21512 Return the computed name or NULL.
21513 Space for the result is allocated on the objfile's obstack.
21514 This is the full-die version of guess_partial_die_structure_name.
21515 In this case we know DIE has no useful parent. */
21516
43816ebc 21517static const char *
abc72ce4
DE
21518guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21519{
21520 struct die_info *spec_die;
21521 struct dwarf2_cu *spec_cu;
21522 struct die_info *child;
518817b3 21523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21524
21525 spec_cu = cu;
21526 spec_die = die_specification (die, &spec_cu);
21527 if (spec_die != NULL)
21528 {
21529 die = spec_die;
21530 cu = spec_cu;
21531 }
21532
21533 for (child = die->child;
21534 child != NULL;
21535 child = child->sibling)
21536 {
21537 if (child->tag == DW_TAG_subprogram)
21538 {
73b9be8b 21539 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21540
7d45c7c3 21541 if (linkage_name != NULL)
abc72ce4 21542 {
43816ebc
TT
21543 gdb::unique_xmalloc_ptr<char> actual_name
21544 (language_class_name_from_physname (cu->language_defn,
21545 linkage_name));
21546 const char *name = NULL;
abc72ce4
DE
21547
21548 if (actual_name != NULL)
21549 {
15d034d0 21550 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21551
21552 if (die_name != NULL
43816ebc 21553 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21554 {
21555 /* Strip off the class name from the full name.
21556 We want the prefix. */
21557 int die_name_len = strlen (die_name);
43816ebc
TT
21558 int actual_name_len = strlen (actual_name.get ());
21559 const char *ptr = actual_name.get ();
abc72ce4
DE
21560
21561 /* Test for '::' as a sanity check. */
21562 if (actual_name_len > die_name_len + 2
43816ebc 21563 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21564 name = obstack_strndup (
e3b94546 21565 &objfile->per_bfd->storage_obstack,
43816ebc 21566 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21567 }
21568 }
abc72ce4
DE
21569 return name;
21570 }
21571 }
21572 }
21573
21574 return NULL;
21575}
21576
96408a79
SA
21577/* GCC might emit a nameless typedef that has a linkage name. Determine the
21578 prefix part in such case. See
21579 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21580
a121b7c1 21581static const char *
96408a79
SA
21582anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21583{
21584 struct attribute *attr;
e6a959d6 21585 const char *base;
96408a79
SA
21586
21587 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21588 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21589 return NULL;
21590
7d45c7c3 21591 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21592 return NULL;
21593
73b9be8b 21594 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21595 if (attr == NULL || DW_STRING (attr) == NULL)
21596 return NULL;
21597
21598 /* dwarf2_name had to be already called. */
21599 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21600
21601 /* Strip the base name, keep any leading namespaces/classes. */
21602 base = strrchr (DW_STRING (attr), ':');
21603 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21604 return "";
21605
518817b3 21606 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21607 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21608 DW_STRING (attr),
21609 &base[-1] - DW_STRING (attr));
96408a79
SA
21610}
21611
fdde2d81 21612/* Return the name of the namespace/class that DIE is defined within,
0114d602 21613 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21614
0114d602
DJ
21615 For example, if we're within the method foo() in the following
21616 code:
21617
21618 namespace N {
21619 class C {
21620 void foo () {
21621 }
21622 };
21623 }
21624
21625 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21626
0d5cff50 21627static const char *
e142c38c 21628determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21629{
518817b3
SM
21630 struct dwarf2_per_objfile *dwarf2_per_objfile
21631 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21632 struct die_info *parent, *spec_die;
21633 struct dwarf2_cu *spec_cu;
21634 struct type *parent_type;
a121b7c1 21635 const char *retval;
63d06c5c 21636
9c37b5ae 21637 if (cu->language != language_cplus
c44af4eb
TT
21638 && cu->language != language_fortran && cu->language != language_d
21639 && cu->language != language_rust)
0114d602
DJ
21640 return "";
21641
96408a79
SA
21642 retval = anonymous_struct_prefix (die, cu);
21643 if (retval)
21644 return retval;
21645
0114d602
DJ
21646 /* We have to be careful in the presence of DW_AT_specification.
21647 For example, with GCC 3.4, given the code
21648
21649 namespace N {
21650 void foo() {
21651 // Definition of N::foo.
21652 }
21653 }
21654
21655 then we'll have a tree of DIEs like this:
21656
21657 1: DW_TAG_compile_unit
21658 2: DW_TAG_namespace // N
21659 3: DW_TAG_subprogram // declaration of N::foo
21660 4: DW_TAG_subprogram // definition of N::foo
21661 DW_AT_specification // refers to die #3
21662
21663 Thus, when processing die #4, we have to pretend that we're in
21664 the context of its DW_AT_specification, namely the contex of die
21665 #3. */
21666 spec_cu = cu;
21667 spec_die = die_specification (die, &spec_cu);
21668 if (spec_die == NULL)
21669 parent = die->parent;
21670 else
63d06c5c 21671 {
0114d602
DJ
21672 parent = spec_die->parent;
21673 cu = spec_cu;
63d06c5c 21674 }
0114d602
DJ
21675
21676 if (parent == NULL)
21677 return "";
98bfdba5
PA
21678 else if (parent->building_fullname)
21679 {
21680 const char *name;
21681 const char *parent_name;
21682
21683 /* It has been seen on RealView 2.2 built binaries,
21684 DW_TAG_template_type_param types actually _defined_ as
21685 children of the parent class:
21686
21687 enum E {};
21688 template class <class Enum> Class{};
21689 Class<enum E> class_e;
21690
21691 1: DW_TAG_class_type (Class)
21692 2: DW_TAG_enumeration_type (E)
21693 3: DW_TAG_enumerator (enum1:0)
21694 3: DW_TAG_enumerator (enum2:1)
21695 ...
21696 2: DW_TAG_template_type_param
21697 DW_AT_type DW_FORM_ref_udata (E)
21698
21699 Besides being broken debug info, it can put GDB into an
21700 infinite loop. Consider:
21701
21702 When we're building the full name for Class<E>, we'll start
21703 at Class, and go look over its template type parameters,
21704 finding E. We'll then try to build the full name of E, and
21705 reach here. We're now trying to build the full name of E,
21706 and look over the parent DIE for containing scope. In the
21707 broken case, if we followed the parent DIE of E, we'd again
21708 find Class, and once again go look at its template type
21709 arguments, etc., etc. Simply don't consider such parent die
21710 as source-level parent of this die (it can't be, the language
21711 doesn't allow it), and break the loop here. */
21712 name = dwarf2_name (die, cu);
21713 parent_name = dwarf2_name (parent, cu);
b98664d3 21714 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21715 name ? name : "<unknown>",
21716 parent_name ? parent_name : "<unknown>");
21717 return "";
21718 }
63d06c5c 21719 else
0114d602
DJ
21720 switch (parent->tag)
21721 {
63d06c5c 21722 case DW_TAG_namespace:
0114d602 21723 parent_type = read_type_die (parent, cu);
acebe513
UW
21724 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21725 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21726 Work around this problem here. */
21727 if (cu->language == language_cplus
7d93a1e0 21728 && strcmp (parent_type->name (), "::") == 0)
acebe513 21729 return "";
0114d602 21730 /* We give a name to even anonymous namespaces. */
7d93a1e0 21731 return parent_type->name ();
63d06c5c 21732 case DW_TAG_class_type:
680b30c7 21733 case DW_TAG_interface_type:
63d06c5c 21734 case DW_TAG_structure_type:
0114d602 21735 case DW_TAG_union_type:
f55ee35c 21736 case DW_TAG_module:
0114d602 21737 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21738 if (parent_type->name () != NULL)
21739 return parent_type->name ();
0114d602
DJ
21740 else
21741 /* An anonymous structure is only allowed non-static data
21742 members; no typedefs, no member functions, et cetera.
21743 So it does not need a prefix. */
21744 return "";
abc72ce4 21745 case DW_TAG_compile_unit:
95554aad 21746 case DW_TAG_partial_unit:
abc72ce4
DE
21747 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21748 if (cu->language == language_cplus
5989a64e 21749 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21750 && die->child != NULL
21751 && (die->tag == DW_TAG_class_type
21752 || die->tag == DW_TAG_structure_type
21753 || die->tag == DW_TAG_union_type))
21754 {
43816ebc 21755 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21756 if (name != NULL)
21757 return name;
21758 }
21759 return "";
0a4b0913
AB
21760 case DW_TAG_subprogram:
21761 /* Nested subroutines in Fortran get a prefix with the name
21762 of the parent's subroutine. */
21763 if (cu->language == language_fortran)
21764 {
21765 if ((die->tag == DW_TAG_subprogram)
21766 && (dwarf2_name (parent, cu) != NULL))
21767 return dwarf2_name (parent, cu);
21768 }
21769 return determine_prefix (parent, cu);
3d567982
TT
21770 case DW_TAG_enumeration_type:
21771 parent_type = read_type_die (parent, cu);
21772 if (TYPE_DECLARED_CLASS (parent_type))
21773 {
7d93a1e0
SM
21774 if (parent_type->name () != NULL)
21775 return parent_type->name ();
3d567982
TT
21776 return "";
21777 }
21778 /* Fall through. */
63d06c5c 21779 default:
8176b9b8 21780 return determine_prefix (parent, cu);
63d06c5c 21781 }
63d06c5c
DC
21782}
21783
3e43a32a
MS
21784/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21785 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21786 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21787 an obconcat, otherwise allocate storage for the result. The CU argument is
21788 used to determine the language and hence, the appropriate separator. */
987504bb 21789
f55ee35c 21790#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21791
21792static char *
f55ee35c
JK
21793typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21794 int physname, struct dwarf2_cu *cu)
63d06c5c 21795{
f55ee35c 21796 const char *lead = "";
5c315b68 21797 const char *sep;
63d06c5c 21798
3e43a32a
MS
21799 if (suffix == NULL || suffix[0] == '\0'
21800 || prefix == NULL || prefix[0] == '\0')
987504bb 21801 sep = "";
45280282
IB
21802 else if (cu->language == language_d)
21803 {
21804 /* For D, the 'main' function could be defined in any module, but it
21805 should never be prefixed. */
21806 if (strcmp (suffix, "D main") == 0)
21807 {
21808 prefix = "";
21809 sep = "";
21810 }
21811 else
21812 sep = ".";
21813 }
f55ee35c
JK
21814 else if (cu->language == language_fortran && physname)
21815 {
21816 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21817 DW_AT_MIPS_linkage_name is preferred and used instead. */
21818
21819 lead = "__";
21820 sep = "_MOD_";
21821 }
987504bb
JJ
21822 else
21823 sep = "::";
63d06c5c 21824
6dd47d34
DE
21825 if (prefix == NULL)
21826 prefix = "";
21827 if (suffix == NULL)
21828 suffix = "";
21829
987504bb
JJ
21830 if (obs == NULL)
21831 {
3e43a32a 21832 char *retval
224c3ddb
SM
21833 = ((char *)
21834 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21835
f55ee35c
JK
21836 strcpy (retval, lead);
21837 strcat (retval, prefix);
6dd47d34
DE
21838 strcat (retval, sep);
21839 strcat (retval, suffix);
63d06c5c
DC
21840 return retval;
21841 }
987504bb
JJ
21842 else
21843 {
21844 /* We have an obstack. */
f55ee35c 21845 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21846 }
63d06c5c
DC
21847}
21848
71c25dea
TT
21849/* Get name of a die, return NULL if not found. */
21850
15d034d0
TT
21851static const char *
21852dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21853 struct objfile *objfile)
71c25dea
TT
21854{
21855 if (name && cu->language == language_cplus)
21856 {
596dc4ad
TT
21857 gdb::unique_xmalloc_ptr<char> canon_name
21858 = cp_canonicalize_string (name);
71c25dea 21859
596dc4ad
TT
21860 if (canon_name != nullptr)
21861 name = objfile->intern (canon_name.get ());
71c25dea
TT
21862 }
21863
21864 return name;
c906108c
SS
21865}
21866
96553a0c
DE
21867/* Get name of a die, return NULL if not found.
21868 Anonymous namespaces are converted to their magic string. */
9219021c 21869
15d034d0 21870static const char *
e142c38c 21871dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21872{
21873 struct attribute *attr;
518817b3 21874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21875
e142c38c 21876 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21877 if ((!attr || !DW_STRING (attr))
96553a0c 21878 && die->tag != DW_TAG_namespace
53832f31
TT
21879 && die->tag != DW_TAG_class_type
21880 && die->tag != DW_TAG_interface_type
21881 && die->tag != DW_TAG_structure_type
21882 && die->tag != DW_TAG_union_type)
71c25dea
TT
21883 return NULL;
21884
21885 switch (die->tag)
21886 {
21887 case DW_TAG_compile_unit:
95554aad 21888 case DW_TAG_partial_unit:
71c25dea
TT
21889 /* Compilation units have a DW_AT_name that is a filename, not
21890 a source language identifier. */
21891 case DW_TAG_enumeration_type:
21892 case DW_TAG_enumerator:
21893 /* These tags always have simple identifiers already; no need
21894 to canonicalize them. */
21895 return DW_STRING (attr);
907af001 21896
96553a0c
DE
21897 case DW_TAG_namespace:
21898 if (attr != NULL && DW_STRING (attr) != NULL)
21899 return DW_STRING (attr);
21900 return CP_ANONYMOUS_NAMESPACE_STR;
21901
907af001
UW
21902 case DW_TAG_class_type:
21903 case DW_TAG_interface_type:
21904 case DW_TAG_structure_type:
21905 case DW_TAG_union_type:
21906 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21907 structures or unions. These were of the form "._%d" in GCC 4.1,
21908 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21909 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21910 if (attr && DW_STRING (attr)
61012eef
GB
21911 && (startswith (DW_STRING (attr), "._")
21912 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21913 return NULL;
53832f31
TT
21914
21915 /* GCC might emit a nameless typedef that has a linkage name. See
21916 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21917 if (!attr || DW_STRING (attr) == NULL)
21918 {
73b9be8b 21919 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21920 if (attr == NULL || DW_STRING (attr) == NULL)
21921 return NULL;
21922
df5c6c50
JK
21923 /* Avoid demangling DW_STRING (attr) the second time on a second
21924 call for the same DIE. */
21925 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21926 {
43816ebc
TT
21927 gdb::unique_xmalloc_ptr<char> demangled
21928 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21929 if (demangled == nullptr)
21930 return nullptr;
43816ebc 21931
be1e3d3e 21932 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21933 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21934 }
67430cd0
TT
21935
21936 /* Strip any leading namespaces/classes, keep only the base name.
21937 DW_AT_name for named DIEs does not contain the prefixes. */
21938 const char *base = strrchr (DW_STRING (attr), ':');
21939 if (base && base > DW_STRING (attr) && base[-1] == ':')
21940 return &base[1];
21941 else
21942 return DW_STRING (attr);
53832f31 21943 }
907af001
UW
21944 break;
21945
71c25dea 21946 default:
907af001
UW
21947 break;
21948 }
21949
21950 if (!DW_STRING_IS_CANONICAL (attr))
21951 {
be1e3d3e
TT
21952 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21953 objfile);
907af001 21954 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21955 }
907af001 21956 return DW_STRING (attr);
9219021c
DC
21957}
21958
21959/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21960 is none. *EXT_CU is the CU containing DIE on input, and the CU
21961 containing the return value on output. */
9219021c
DC
21962
21963static struct die_info *
f2f0e013 21964dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21965{
21966 struct attribute *attr;
9219021c 21967
f2f0e013 21968 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21969 if (attr == NULL)
21970 return NULL;
21971
f2f0e013 21972 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21973}
21974
f9aca02d 21975static void
d97bc12b 21976dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21977{
21978 unsigned int i;
21979
d97bc12b 21980 print_spaces (indent, f);
9d8780f0 21981 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21982 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21983 sect_offset_str (die->sect_off));
d97bc12b
DE
21984
21985 if (die->parent != NULL)
21986 {
21987 print_spaces (indent, f);
9d8780f0
SM
21988 fprintf_unfiltered (f, " parent at offset: %s\n",
21989 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21990 }
21991
21992 print_spaces (indent, f);
21993 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21994 dwarf_bool_name (die->child != NULL));
c906108c 21995
d97bc12b
DE
21996 print_spaces (indent, f);
21997 fprintf_unfiltered (f, " attributes:\n");
21998
c906108c
SS
21999 for (i = 0; i < die->num_attrs; ++i)
22000 {
d97bc12b
DE
22001 print_spaces (indent, f);
22002 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22003 dwarf_attr_name (die->attrs[i].name),
22004 dwarf_form_name (die->attrs[i].form));
d97bc12b 22005
c906108c
SS
22006 switch (die->attrs[i].form)
22007 {
c906108c 22008 case DW_FORM_addr:
336d760d 22009 case DW_FORM_addrx:
3019eac3 22010 case DW_FORM_GNU_addr_index:
d97bc12b 22011 fprintf_unfiltered (f, "address: ");
5af949e3 22012 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22013 break;
22014 case DW_FORM_block2:
22015 case DW_FORM_block4:
22016 case DW_FORM_block:
22017 case DW_FORM_block1:
56eb65bd
SP
22018 fprintf_unfiltered (f, "block: size %s",
22019 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22020 break;
2dc7f7b3 22021 case DW_FORM_exprloc:
56eb65bd
SP
22022 fprintf_unfiltered (f, "expression: size %s",
22023 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22024 break;
0224619f
JK
22025 case DW_FORM_data16:
22026 fprintf_unfiltered (f, "constant of 16 bytes");
22027 break;
4568ecf9
DE
22028 case DW_FORM_ref_addr:
22029 fprintf_unfiltered (f, "ref address: ");
22030 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22031 break;
36586728
TT
22032 case DW_FORM_GNU_ref_alt:
22033 fprintf_unfiltered (f, "alt ref address: ");
22034 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22035 break;
10b3939b
DJ
22036 case DW_FORM_ref1:
22037 case DW_FORM_ref2:
22038 case DW_FORM_ref4:
4568ecf9
DE
22039 case DW_FORM_ref8:
22040 case DW_FORM_ref_udata:
d97bc12b 22041 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22042 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22043 break;
c906108c
SS
22044 case DW_FORM_data1:
22045 case DW_FORM_data2:
22046 case DW_FORM_data4:
ce5d95e1 22047 case DW_FORM_data8:
c906108c
SS
22048 case DW_FORM_udata:
22049 case DW_FORM_sdata:
43bbcdc2
PH
22050 fprintf_unfiltered (f, "constant: %s",
22051 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22052 break;
2dc7f7b3
TT
22053 case DW_FORM_sec_offset:
22054 fprintf_unfiltered (f, "section offset: %s",
22055 pulongest (DW_UNSND (&die->attrs[i])));
22056 break;
55f1336d 22057 case DW_FORM_ref_sig8:
ac9ec31b
DE
22058 fprintf_unfiltered (f, "signature: %s",
22059 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22060 break;
c906108c 22061 case DW_FORM_string:
4bdf3d34 22062 case DW_FORM_strp:
43988095 22063 case DW_FORM_line_strp:
cf532bd1 22064 case DW_FORM_strx:
3019eac3 22065 case DW_FORM_GNU_str_index:
36586728 22066 case DW_FORM_GNU_strp_alt:
8285870a 22067 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22068 DW_STRING (&die->attrs[i])
8285870a
JK
22069 ? DW_STRING (&die->attrs[i]) : "",
22070 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22071 break;
22072 case DW_FORM_flag:
22073 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22074 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22075 else
d97bc12b 22076 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22077 break;
2dc7f7b3
TT
22078 case DW_FORM_flag_present:
22079 fprintf_unfiltered (f, "flag: TRUE");
22080 break;
a8329558 22081 case DW_FORM_indirect:
0963b4bd
MS
22082 /* The reader will have reduced the indirect form to
22083 the "base form" so this form should not occur. */
5f48f8f3 22084 fprintf_unfiltered (f,
3e43a32a 22085 "unexpected attribute form: DW_FORM_indirect");
a8329558 22086 break;
663c44ac
JK
22087 case DW_FORM_implicit_const:
22088 fprintf_unfiltered (f, "constant: %s",
22089 plongest (DW_SND (&die->attrs[i])));
22090 break;
c906108c 22091 default:
d97bc12b 22092 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22093 die->attrs[i].form);
d97bc12b 22094 break;
c906108c 22095 }
d97bc12b 22096 fprintf_unfiltered (f, "\n");
c906108c
SS
22097 }
22098}
22099
f9aca02d 22100static void
d97bc12b 22101dump_die_for_error (struct die_info *die)
c906108c 22102{
d97bc12b
DE
22103 dump_die_shallow (gdb_stderr, 0, die);
22104}
22105
22106static void
22107dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22108{
22109 int indent = level * 4;
22110
22111 gdb_assert (die != NULL);
22112
22113 if (level >= max_level)
22114 return;
22115
22116 dump_die_shallow (f, indent, die);
22117
22118 if (die->child != NULL)
c906108c 22119 {
d97bc12b
DE
22120 print_spaces (indent, f);
22121 fprintf_unfiltered (f, " Children:");
22122 if (level + 1 < max_level)
22123 {
22124 fprintf_unfiltered (f, "\n");
22125 dump_die_1 (f, level + 1, max_level, die->child);
22126 }
22127 else
22128 {
3e43a32a
MS
22129 fprintf_unfiltered (f,
22130 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22131 }
22132 }
22133
22134 if (die->sibling != NULL && level > 0)
22135 {
22136 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22137 }
22138}
22139
d97bc12b
DE
22140/* This is called from the pdie macro in gdbinit.in.
22141 It's not static so gcc will keep a copy callable from gdb. */
22142
22143void
22144dump_die (struct die_info *die, int max_level)
22145{
22146 dump_die_1 (gdb_stdlog, 0, max_level, die);
22147}
22148
f9aca02d 22149static void
51545339 22150store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22151{
51545339 22152 void **slot;
c906108c 22153
9c541725
PA
22154 slot = htab_find_slot_with_hash (cu->die_hash, die,
22155 to_underlying (die->sect_off),
b64f50a1 22156 INSERT);
51545339
DJ
22157
22158 *slot = die;
c906108c
SS
22159}
22160
348e048f
DE
22161/* Follow reference or signature attribute ATTR of SRC_DIE.
22162 On entry *REF_CU is the CU of SRC_DIE.
22163 On exit *REF_CU is the CU of the result. */
22164
22165static struct die_info *
ff39bb5e 22166follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22167 struct dwarf2_cu **ref_cu)
22168{
22169 struct die_info *die;
22170
cd6c91b4 22171 if (attr->form_is_ref ())
348e048f 22172 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22173 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22174 die = follow_die_sig (src_die, attr, ref_cu);
22175 else
22176 {
22177 dump_die_for_error (src_die);
22178 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22179 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22180 }
22181
22182 return die;
03dd20cc
DJ
22183}
22184
5c631832 22185/* Follow reference OFFSET.
673bfd45
DE
22186 On entry *REF_CU is the CU of the source die referencing OFFSET.
22187 On exit *REF_CU is the CU of the result.
22188 Returns NULL if OFFSET is invalid. */
f504f079 22189
f9aca02d 22190static struct die_info *
9c541725 22191follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22192 struct dwarf2_cu **ref_cu)
c906108c 22193{
10b3939b 22194 struct die_info temp_die;
f2f0e013 22195 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22196 struct dwarf2_per_objfile *dwarf2_per_objfile
22197 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22198
348e048f
DE
22199 gdb_assert (cu->per_cu != NULL);
22200
98bfdba5
PA
22201 target_cu = cu;
22202
3019eac3 22203 if (cu->per_cu->is_debug_types)
348e048f
DE
22204 {
22205 /* .debug_types CUs cannot reference anything outside their CU.
22206 If they need to, they have to reference a signatured type via
55f1336d 22207 DW_FORM_ref_sig8. */
4057dfde 22208 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22209 return NULL;
348e048f 22210 }
36586728 22211 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22212 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22213 {
22214 struct dwarf2_per_cu_data *per_cu;
9a619af0 22215
9c541725 22216 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22217 dwarf2_per_objfile);
03dd20cc
DJ
22218
22219 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22220 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22221 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22222
10b3939b
DJ
22223 target_cu = per_cu->cu;
22224 }
98bfdba5
PA
22225 else if (cu->dies == NULL)
22226 {
22227 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22228 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
58f0c718 22229 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22230 }
c906108c 22231
f2f0e013 22232 *ref_cu = target_cu;
9c541725 22233 temp_die.sect_off = sect_off;
c24bdb02
KS
22234
22235 if (target_cu != cu)
22236 target_cu->ancestor = cu;
22237
9a3c8263 22238 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22239 &temp_die,
22240 to_underlying (sect_off));
5c631832 22241}
10b3939b 22242
5c631832
JK
22243/* Follow reference attribute ATTR of SRC_DIE.
22244 On entry *REF_CU is the CU of SRC_DIE.
22245 On exit *REF_CU is the CU of the result. */
22246
22247static struct die_info *
ff39bb5e 22248follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22249 struct dwarf2_cu **ref_cu)
22250{
0826b30a 22251 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22252 struct dwarf2_cu *cu = *ref_cu;
22253 struct die_info *die;
22254
9c541725 22255 die = follow_die_offset (sect_off,
36586728
TT
22256 (attr->form == DW_FORM_GNU_ref_alt
22257 || cu->per_cu->is_dwz),
22258 ref_cu);
5c631832 22259 if (!die)
9d8780f0
SM
22260 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22261 "at %s [in module %s]"),
22262 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22263 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22264
5c631832
JK
22265 return die;
22266}
22267
d4c9a4f8 22268/* See read.h. */
5c631832
JK
22269
22270struct dwarf2_locexpr_baton
9c541725 22271dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22272 dwarf2_per_cu_data *per_cu,
8b9737bf 22273 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22274 void *baton, bool resolve_abstract_p)
5c631832 22275{
918dd910 22276 struct dwarf2_cu *cu;
5c631832
JK
22277 struct die_info *die;
22278 struct attribute *attr;
22279 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22280 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22281 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22282
918dd910 22283 if (per_cu->cu == NULL)
58f0c718 22284 load_cu (per_cu, false);
918dd910 22285 cu = per_cu->cu;
cc12ce38
DE
22286 if (cu == NULL)
22287 {
22288 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22289 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22290 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22291 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22292 }
918dd910 22293
9c541725 22294 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22295 if (!die)
9d8780f0
SM
22296 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22297 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22298
22299 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22300 if (!attr && resolve_abstract_p
5989a64e
SM
22301 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22302 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22303 {
22304 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22305 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22306 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22307
3360b6e7 22308 for (const auto &cand_off
5989a64e 22309 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22310 {
3360b6e7
TV
22311 struct dwarf2_cu *cand_cu = cu;
22312 struct die_info *cand
22313 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22314 if (!cand
22315 || !cand->parent
e4a62c65
TV
22316 || cand->parent->tag != DW_TAG_subprogram)
22317 continue;
22318
22319 CORE_ADDR pc_low, pc_high;
22320 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22321 if (pc_low == ((CORE_ADDR) -1))
22322 continue;
22323 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22324 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22325 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22326 continue;
22327
22328 die = cand;
22329 attr = dwarf2_attr (die, DW_AT_location, cu);
22330 break;
22331 }
22332 }
22333
5c631832
JK
22334 if (!attr)
22335 {
e103e986
JK
22336 /* DWARF: "If there is no such attribute, then there is no effect.".
22337 DATA is ignored if SIZE is 0. */
5c631832 22338
e103e986 22339 retval.data = NULL;
5c631832
JK
22340 retval.size = 0;
22341 }
cd6c91b4 22342 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22343 {
22344 struct dwarf2_loclist_baton loclist_baton;
22345 CORE_ADDR pc = (*get_frame_pc) (baton);
22346 size_t size;
22347
22348 fill_in_loclist_baton (cu, &loclist_baton, attr);
22349
22350 retval.data = dwarf2_find_location_expression (&loclist_baton,
22351 &size, pc);
22352 retval.size = size;
22353 }
5c631832
JK
22354 else
22355 {
4fc6c0d5 22356 if (!attr->form_is_block ())
9d8780f0 22357 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22358 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22359 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22360
22361 retval.data = DW_BLOCK (attr)->data;
22362 retval.size = DW_BLOCK (attr)->size;
22363 }
a50264ba 22364 retval.per_objfile = dwarf2_per_objfile;
5c631832 22365 retval.per_cu = cu->per_cu;
918dd910 22366
ed2dc618 22367 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22368
5c631832 22369 return retval;
348e048f
DE
22370}
22371
d4c9a4f8 22372/* See read.h. */
8b9737bf
TT
22373
22374struct dwarf2_locexpr_baton
22375dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22376 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22377 CORE_ADDR (*get_frame_pc) (void *baton),
22378 void *baton)
22379{
9c541725 22380 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22381
9c541725 22382 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22383}
22384
b6807d98
TT
22385/* Write a constant of a given type as target-ordered bytes into
22386 OBSTACK. */
22387
22388static const gdb_byte *
22389write_constant_as_bytes (struct obstack *obstack,
22390 enum bfd_endian byte_order,
22391 struct type *type,
22392 ULONGEST value,
22393 LONGEST *len)
22394{
22395 gdb_byte *result;
22396
22397 *len = TYPE_LENGTH (type);
224c3ddb 22398 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22399 store_unsigned_integer (result, *len, byte_order, value);
22400
22401 return result;
22402}
22403
d4c9a4f8 22404/* See read.h. */
b6807d98
TT
22405
22406const gdb_byte *
9c541725 22407dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22408 dwarf2_per_cu_data *per_cu,
22409 obstack *obstack,
b6807d98
TT
22410 LONGEST *len)
22411{
22412 struct dwarf2_cu *cu;
22413 struct die_info *die;
22414 struct attribute *attr;
22415 const gdb_byte *result = NULL;
22416 struct type *type;
22417 LONGEST value;
22418 enum bfd_endian byte_order;
e3b94546 22419 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22420
b6807d98 22421 if (per_cu->cu == NULL)
58f0c718 22422 load_cu (per_cu, false);
b6807d98 22423 cu = per_cu->cu;
cc12ce38
DE
22424 if (cu == NULL)
22425 {
22426 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22427 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22428 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22429 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22430 }
b6807d98 22431
9c541725 22432 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22433 if (!die)
9d8780f0
SM
22434 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22435 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22436
22437 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22438 if (attr == NULL)
22439 return NULL;
22440
e3b94546 22441 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22442 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22443
22444 switch (attr->form)
22445 {
22446 case DW_FORM_addr:
336d760d 22447 case DW_FORM_addrx:
b6807d98
TT
22448 case DW_FORM_GNU_addr_index:
22449 {
22450 gdb_byte *tem;
22451
22452 *len = cu->header.addr_size;
224c3ddb 22453 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22454 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22455 result = tem;
22456 }
22457 break;
22458 case DW_FORM_string:
22459 case DW_FORM_strp:
cf532bd1 22460 case DW_FORM_strx:
b6807d98
TT
22461 case DW_FORM_GNU_str_index:
22462 case DW_FORM_GNU_strp_alt:
22463 /* DW_STRING is already allocated on the objfile obstack, point
22464 directly to it. */
22465 result = (const gdb_byte *) DW_STRING (attr);
22466 *len = strlen (DW_STRING (attr));
22467 break;
22468 case DW_FORM_block1:
22469 case DW_FORM_block2:
22470 case DW_FORM_block4:
22471 case DW_FORM_block:
22472 case DW_FORM_exprloc:
0224619f 22473 case DW_FORM_data16:
b6807d98
TT
22474 result = DW_BLOCK (attr)->data;
22475 *len = DW_BLOCK (attr)->size;
22476 break;
22477
22478 /* The DW_AT_const_value attributes are supposed to carry the
22479 symbol's value "represented as it would be on the target
22480 architecture." By the time we get here, it's already been
22481 converted to host endianness, so we just need to sign- or
22482 zero-extend it as appropriate. */
22483 case DW_FORM_data1:
22484 type = die_type (die, cu);
22485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22486 if (result == NULL)
22487 result = write_constant_as_bytes (obstack, byte_order,
22488 type, value, len);
22489 break;
22490 case DW_FORM_data2:
22491 type = die_type (die, cu);
22492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22493 if (result == NULL)
22494 result = write_constant_as_bytes (obstack, byte_order,
22495 type, value, len);
22496 break;
22497 case DW_FORM_data4:
22498 type = die_type (die, cu);
22499 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22500 if (result == NULL)
22501 result = write_constant_as_bytes (obstack, byte_order,
22502 type, value, len);
22503 break;
22504 case DW_FORM_data8:
22505 type = die_type (die, cu);
22506 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22507 if (result == NULL)
22508 result = write_constant_as_bytes (obstack, byte_order,
22509 type, value, len);
22510 break;
22511
22512 case DW_FORM_sdata:
663c44ac 22513 case DW_FORM_implicit_const:
b6807d98
TT
22514 type = die_type (die, cu);
22515 result = write_constant_as_bytes (obstack, byte_order,
22516 type, DW_SND (attr), len);
22517 break;
22518
22519 case DW_FORM_udata:
22520 type = die_type (die, cu);
22521 result = write_constant_as_bytes (obstack, byte_order,
22522 type, DW_UNSND (attr), len);
22523 break;
22524
22525 default:
b98664d3 22526 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22527 dwarf_form_name (attr->form));
22528 break;
22529 }
22530
22531 return result;
22532}
22533
d4c9a4f8 22534/* See read.h. */
7942e96e
AA
22535
22536struct type *
9c541725 22537dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22538 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22539{
22540 struct dwarf2_cu *cu;
22541 struct die_info *die;
22542
7942e96e 22543 if (per_cu->cu == NULL)
58f0c718 22544 load_cu (per_cu, false);
7942e96e
AA
22545 cu = per_cu->cu;
22546 if (!cu)
22547 return NULL;
22548
9c541725 22549 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22550 if (!die)
22551 return NULL;
22552
22553 return die_type (die, cu);
22554}
22555
8cb5117c 22556/* See read.h. */
8a9b8146
TT
22557
22558struct type *
b64f50a1 22559dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22560 struct dwarf2_per_cu_data *per_cu)
22561{
9c541725 22562 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22563 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22564}
22565
ac9ec31b 22566/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22567 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22568 On exit *REF_CU is the CU of the result.
22569 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22570
22571static struct die_info *
ac9ec31b
DE
22572follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22573 struct dwarf2_cu **ref_cu)
348e048f 22574{
348e048f 22575 struct die_info temp_die;
c24bdb02 22576 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22577 struct die_info *die;
22578
ac9ec31b
DE
22579 /* While it might be nice to assert sig_type->type == NULL here,
22580 we can get here for DW_AT_imported_declaration where we need
22581 the DIE not the type. */
348e048f
DE
22582
22583 /* If necessary, add it to the queue and load its DIEs. */
22584
95554aad 22585 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22586 read_signatured_type (sig_type);
348e048f 22587
348e048f 22588 sig_cu = sig_type->per_cu.cu;
69d751e3 22589 gdb_assert (sig_cu != NULL);
9c541725
PA
22590 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22591 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22592 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22593 to_underlying (temp_die.sect_off));
348e048f
DE
22594 if (die)
22595 {
ed2dc618 22596 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22597 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22598
796a7ff8
DE
22599 /* For .gdb_index version 7 keep track of included TUs.
22600 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22601 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22602 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22603 {
ae640021 22604 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22605 }
22606
348e048f 22607 *ref_cu = sig_cu;
c24bdb02
KS
22608 if (sig_cu != cu)
22609 sig_cu->ancestor = cu;
22610
348e048f
DE
22611 return die;
22612 }
22613
ac9ec31b
DE
22614 return NULL;
22615}
22616
22617/* Follow signatured type referenced by ATTR in SRC_DIE.
22618 On entry *REF_CU is the CU of SRC_DIE.
22619 On exit *REF_CU is the CU of the result.
22620 The result is the DIE of the type.
22621 If the referenced type cannot be found an error is thrown. */
22622
22623static struct die_info *
ff39bb5e 22624follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22625 struct dwarf2_cu **ref_cu)
22626{
22627 ULONGEST signature = DW_SIGNATURE (attr);
22628 struct signatured_type *sig_type;
22629 struct die_info *die;
22630
22631 gdb_assert (attr->form == DW_FORM_ref_sig8);
22632
a2ce51a0 22633 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22634 /* sig_type will be NULL if the signatured type is missing from
22635 the debug info. */
22636 if (sig_type == NULL)
22637 {
22638 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22639 " from DIE at %s [in module %s]"),
22640 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22641 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22642 }
22643
22644 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22645 if (die == NULL)
22646 {
22647 dump_die_for_error (src_die);
22648 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22649 " from DIE at %s [in module %s]"),
22650 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22651 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22652 }
22653
22654 return die;
22655}
22656
22657/* Get the type specified by SIGNATURE referenced in DIE/CU,
22658 reading in and processing the type unit if necessary. */
22659
22660static struct type *
22661get_signatured_type (struct die_info *die, ULONGEST signature,
22662 struct dwarf2_cu *cu)
22663{
518817b3
SM
22664 struct dwarf2_per_objfile *dwarf2_per_objfile
22665 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22666 struct signatured_type *sig_type;
22667 struct dwarf2_cu *type_cu;
22668 struct die_info *type_die;
22669 struct type *type;
22670
a2ce51a0 22671 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22672 /* sig_type will be NULL if the signatured type is missing from
22673 the debug info. */
22674 if (sig_type == NULL)
22675 {
b98664d3 22676 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22677 " from DIE at %s [in module %s]"),
22678 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22679 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22680 return build_error_marker_type (cu, die);
22681 }
22682
22683 /* If we already know the type we're done. */
22684 if (sig_type->type != NULL)
22685 return sig_type->type;
22686
22687 type_cu = cu;
22688 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22689 if (type_die != NULL)
22690 {
22691 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22692 is created. This is important, for example, because for c++ classes
22693 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22694 type = read_type_die (type_die, type_cu);
22695 if (type == NULL)
22696 {
b98664d3 22697 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22698 " referenced from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22700 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22701 type = build_error_marker_type (cu, die);
22702 }
22703 }
22704 else
22705 {
b98664d3 22706 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22707 " from DIE at %s [in module %s]"),
22708 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22709 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22710 type = build_error_marker_type (cu, die);
22711 }
22712 sig_type->type = type;
22713
22714 return type;
22715}
22716
22717/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22718 reading in and processing the type unit if necessary. */
22719
22720static struct type *
ff39bb5e 22721get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22722 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22723{
22724 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22725 if (attr->form_is_ref ())
ac9ec31b
DE
22726 {
22727 struct dwarf2_cu *type_cu = cu;
22728 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22729
22730 return read_type_die (type_die, type_cu);
22731 }
22732 else if (attr->form == DW_FORM_ref_sig8)
22733 {
22734 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22735 }
22736 else
22737 {
518817b3
SM
22738 struct dwarf2_per_objfile *dwarf2_per_objfile
22739 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22740
b98664d3 22741 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22742 " at %s [in module %s]"),
22743 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22744 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22745 return build_error_marker_type (cu, die);
22746 }
348e048f
DE
22747}
22748
e5fe5e75 22749/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22750
22751static void
e5fe5e75 22752load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22753{
52dc124a 22754 struct signatured_type *sig_type;
348e048f 22755
f4dc4d17 22756 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22757 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22758
6721b2ec
DE
22759 /* We have the per_cu, but we need the signatured_type.
22760 Fortunately this is an easy translation. */
22761 gdb_assert (per_cu->is_debug_types);
22762 sig_type = (struct signatured_type *) per_cu;
348e048f 22763
6721b2ec 22764 gdb_assert (per_cu->cu == NULL);
348e048f 22765
52dc124a 22766 read_signatured_type (sig_type);
348e048f 22767
6721b2ec 22768 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22769}
22770
3019eac3
DE
22771/* Read in a signatured type and build its CU and DIEs.
22772 If the type is a stub for the real type in a DWO file,
22773 read in the real type from the DWO file as well. */
dee91e82
DE
22774
22775static void
22776read_signatured_type (struct signatured_type *sig_type)
22777{
22778 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22779
3019eac3 22780 gdb_assert (per_cu->is_debug_types);
dee91e82 22781 gdb_assert (per_cu->cu == NULL);
348e048f 22782
6751ebae 22783 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22784
22785 if (!reader.dummy_p)
22786 {
22787 struct dwarf2_cu *cu = reader.cu;
22788 const gdb_byte *info_ptr = reader.info_ptr;
22789
22790 gdb_assert (cu->die_hash == NULL);
22791 cu->die_hash =
22792 htab_create_alloc_ex (cu->header.length / 12,
22793 die_hash,
22794 die_eq,
22795 NULL,
22796 &cu->comp_unit_obstack,
22797 hashtab_obstack_allocate,
22798 dummy_obstack_deallocate);
22799
3e225074 22800 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22801 reader.comp_unit_die->child
22802 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22803 reader.comp_unit_die);
22804 cu->dies = reader.comp_unit_die;
22805 /* comp_unit_die is not stored in die_hash, no need. */
22806
22807 /* We try not to read any attributes in this function, because
22808 not all CUs needed for references have been loaded yet, and
22809 symbol table processing isn't initialized. But we have to
22810 set the CU language, or we won't be able to build types
22811 correctly. Similarly, if we do not read the producer, we can
22812 not apply producer-specific interpretation. */
22813 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22814
22815 reader.keep ();
c0ab21c2
TT
22816 }
22817
7ee85ab1 22818 sig_type->per_cu.tu_read = 1;
c906108c
SS
22819}
22820
c906108c
SS
22821/* Decode simple location descriptions.
22822 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22823 the location and return the value. If COMPUTED is non-null, it is
22824 set to true to indicate that decoding was successful, and false
22825 otherwise. If COMPUTED is null, then this function may emit a
22826 complaint. */
c906108c
SS
22827
22828static CORE_ADDR
7d79de9a 22829decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22830{
518817b3 22831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22832 size_t i;
22833 size_t size = blk->size;
d521ce57 22834 const gdb_byte *data = blk->data;
21ae7a4d
JK
22835 CORE_ADDR stack[64];
22836 int stacki;
22837 unsigned int bytes_read, unsnd;
22838 gdb_byte op;
c906108c 22839
7d79de9a
TT
22840 if (computed != nullptr)
22841 *computed = false;
22842
21ae7a4d
JK
22843 i = 0;
22844 stacki = 0;
22845 stack[stacki] = 0;
22846 stack[++stacki] = 0;
22847
22848 while (i < size)
22849 {
22850 op = data[i++];
22851 switch (op)
22852 {
22853 case DW_OP_lit0:
22854 case DW_OP_lit1:
22855 case DW_OP_lit2:
22856 case DW_OP_lit3:
22857 case DW_OP_lit4:
22858 case DW_OP_lit5:
22859 case DW_OP_lit6:
22860 case DW_OP_lit7:
22861 case DW_OP_lit8:
22862 case DW_OP_lit9:
22863 case DW_OP_lit10:
22864 case DW_OP_lit11:
22865 case DW_OP_lit12:
22866 case DW_OP_lit13:
22867 case DW_OP_lit14:
22868 case DW_OP_lit15:
22869 case DW_OP_lit16:
22870 case DW_OP_lit17:
22871 case DW_OP_lit18:
22872 case DW_OP_lit19:
22873 case DW_OP_lit20:
22874 case DW_OP_lit21:
22875 case DW_OP_lit22:
22876 case DW_OP_lit23:
22877 case DW_OP_lit24:
22878 case DW_OP_lit25:
22879 case DW_OP_lit26:
22880 case DW_OP_lit27:
22881 case DW_OP_lit28:
22882 case DW_OP_lit29:
22883 case DW_OP_lit30:
22884 case DW_OP_lit31:
22885 stack[++stacki] = op - DW_OP_lit0;
22886 break;
f1bea926 22887
21ae7a4d
JK
22888 case DW_OP_reg0:
22889 case DW_OP_reg1:
22890 case DW_OP_reg2:
22891 case DW_OP_reg3:
22892 case DW_OP_reg4:
22893 case DW_OP_reg5:
22894 case DW_OP_reg6:
22895 case DW_OP_reg7:
22896 case DW_OP_reg8:
22897 case DW_OP_reg9:
22898 case DW_OP_reg10:
22899 case DW_OP_reg11:
22900 case DW_OP_reg12:
22901 case DW_OP_reg13:
22902 case DW_OP_reg14:
22903 case DW_OP_reg15:
22904 case DW_OP_reg16:
22905 case DW_OP_reg17:
22906 case DW_OP_reg18:
22907 case DW_OP_reg19:
22908 case DW_OP_reg20:
22909 case DW_OP_reg21:
22910 case DW_OP_reg22:
22911 case DW_OP_reg23:
22912 case DW_OP_reg24:
22913 case DW_OP_reg25:
22914 case DW_OP_reg26:
22915 case DW_OP_reg27:
22916 case DW_OP_reg28:
22917 case DW_OP_reg29:
22918 case DW_OP_reg30:
22919 case DW_OP_reg31:
22920 stack[++stacki] = op - DW_OP_reg0;
22921 if (i < size)
7d79de9a
TT
22922 {
22923 if (computed == nullptr)
22924 dwarf2_complex_location_expr_complaint ();
22925 else
22926 return 0;
22927 }
21ae7a4d 22928 break;
c906108c 22929
21ae7a4d
JK
22930 case DW_OP_regx:
22931 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22932 i += bytes_read;
22933 stack[++stacki] = unsnd;
22934 if (i < size)
7d79de9a
TT
22935 {
22936 if (computed == nullptr)
22937 dwarf2_complex_location_expr_complaint ();
22938 else
22939 return 0;
22940 }
21ae7a4d 22941 break;
c906108c 22942
21ae7a4d 22943 case DW_OP_addr:
c8a7a66f
TT
22944 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22945 &bytes_read);
21ae7a4d
JK
22946 i += bytes_read;
22947 break;
d53d4ac5 22948
21ae7a4d
JK
22949 case DW_OP_const1u:
22950 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22951 i += 1;
22952 break;
22953
22954 case DW_OP_const1s:
22955 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22956 i += 1;
22957 break;
22958
22959 case DW_OP_const2u:
22960 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22961 i += 2;
22962 break;
22963
22964 case DW_OP_const2s:
22965 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22966 i += 2;
22967 break;
d53d4ac5 22968
21ae7a4d
JK
22969 case DW_OP_const4u:
22970 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22971 i += 4;
22972 break;
22973
22974 case DW_OP_const4s:
22975 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22976 i += 4;
22977 break;
22978
585861ea
JK
22979 case DW_OP_const8u:
22980 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22981 i += 8;
22982 break;
22983
21ae7a4d
JK
22984 case DW_OP_constu:
22985 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22986 &bytes_read);
22987 i += bytes_read;
22988 break;
22989
22990 case DW_OP_consts:
22991 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22992 i += bytes_read;
22993 break;
22994
22995 case DW_OP_dup:
22996 stack[stacki + 1] = stack[stacki];
22997 stacki++;
22998 break;
22999
23000 case DW_OP_plus:
23001 stack[stacki - 1] += stack[stacki];
23002 stacki--;
23003 break;
23004
23005 case DW_OP_plus_uconst:
23006 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23007 &bytes_read);
23008 i += bytes_read;
23009 break;
23010
23011 case DW_OP_minus:
23012 stack[stacki - 1] -= stack[stacki];
23013 stacki--;
23014 break;
23015
23016 case DW_OP_deref:
23017 /* If we're not the last op, then we definitely can't encode
23018 this using GDB's address_class enum. This is valid for partial
23019 global symbols, although the variable's address will be bogus
23020 in the psymtab. */
23021 if (i < size)
7d79de9a
TT
23022 {
23023 if (computed == nullptr)
23024 dwarf2_complex_location_expr_complaint ();
23025 else
23026 return 0;
23027 }
21ae7a4d
JK
23028 break;
23029
23030 case DW_OP_GNU_push_tls_address:
4aa4e28b 23031 case DW_OP_form_tls_address:
21ae7a4d
JK
23032 /* The top of the stack has the offset from the beginning
23033 of the thread control block at which the variable is located. */
23034 /* Nothing should follow this operator, so the top of stack would
23035 be returned. */
23036 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23037 address will be bogus in the psymtab. Make it always at least
23038 non-zero to not look as a variable garbage collected by linker
23039 which have DW_OP_addr 0. */
21ae7a4d 23040 if (i < size)
7d79de9a
TT
23041 {
23042 if (computed == nullptr)
23043 dwarf2_complex_location_expr_complaint ();
23044 else
23045 return 0;
23046 }
585861ea 23047 stack[stacki]++;
21ae7a4d
JK
23048 break;
23049
23050 case DW_OP_GNU_uninit:
7d79de9a
TT
23051 if (computed != nullptr)
23052 return 0;
21ae7a4d
JK
23053 break;
23054
336d760d 23055 case DW_OP_addrx:
3019eac3 23056 case DW_OP_GNU_addr_index:
49f6c839 23057 case DW_OP_GNU_const_index:
3019eac3
DE
23058 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23059 &bytes_read);
23060 i += bytes_read;
23061 break;
23062
21ae7a4d 23063 default:
7d79de9a
TT
23064 if (computed == nullptr)
23065 {
23066 const char *name = get_DW_OP_name (op);
21ae7a4d 23067
7d79de9a
TT
23068 if (name)
23069 complaint (_("unsupported stack op: '%s'"),
23070 name);
23071 else
23072 complaint (_("unsupported stack op: '%02x'"),
23073 op);
23074 }
21ae7a4d
JK
23075
23076 return (stack[stacki]);
d53d4ac5 23077 }
3c6e0cb3 23078
21ae7a4d
JK
23079 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23080 outside of the allocated space. Also enforce minimum>0. */
23081 if (stacki >= ARRAY_SIZE (stack) - 1)
23082 {
7d79de9a
TT
23083 if (computed == nullptr)
23084 complaint (_("location description stack overflow"));
21ae7a4d
JK
23085 return 0;
23086 }
23087
23088 if (stacki <= 0)
23089 {
7d79de9a
TT
23090 if (computed == nullptr)
23091 complaint (_("location description stack underflow"));
21ae7a4d
JK
23092 return 0;
23093 }
23094 }
7d79de9a
TT
23095
23096 if (computed != nullptr)
23097 *computed = true;
21ae7a4d 23098 return (stack[stacki]);
c906108c
SS
23099}
23100
23101/* memory allocation interface */
23102
c906108c 23103static struct dwarf_block *
7b5a2f43 23104dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23105{
8d749320 23106 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23107}
23108
c906108c 23109static struct die_info *
b60c80d6 23110dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23111{
23112 struct die_info *die;
b60c80d6
DJ
23113 size_t size = sizeof (struct die_info);
23114
23115 if (num_attrs > 1)
23116 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23117
b60c80d6 23118 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23119 memset (die, 0, sizeof (struct die_info));
23120 return (die);
23121}
2e276125
JB
23122
23123\f
a036ba48 23124
c90ec28a 23125/* Macro support. */
cf2c3c16 23126
9eac9650
TT
23127/* An overload of dwarf_decode_macros that finds the correct section
23128 and ensures it is read in before calling the other overload. */
23129
23130static void
23131dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23132 int section_is_gnu)
23133{
23134 struct dwarf2_per_objfile *dwarf2_per_objfile
23135 = cu->per_cu->dwarf2_per_objfile;
23136 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23137 const struct line_header *lh = cu->line_header;
9eac9650
TT
23138 unsigned int offset_size = cu->header.offset_size;
23139 struct dwarf2_section_info *section;
23140 const char *section_name;
23141
23142 if (cu->dwo_unit != nullptr)
23143 {
23144 if (section_is_gnu)
23145 {
23146 section = &cu->dwo_unit->dwo_file->sections.macro;
23147 section_name = ".debug_macro.dwo";
23148 }
23149 else
23150 {
23151 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23152 section_name = ".debug_macinfo.dwo";
23153 }
23154 }
23155 else
23156 {
23157 if (section_is_gnu)
23158 {
5989a64e 23159 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23160 section_name = ".debug_macro";
23161 }
23162 else
23163 {
5989a64e 23164 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23165 section_name = ".debug_macinfo";
23166 }
23167 }
23168
23169 section->read (objfile);
23170 if (section->buffer == nullptr)
23171 {
23172 complaint (_("missing %s section"), section_name);
23173 return;
23174 }
23175
23176 buildsym_compunit *builder = cu->get_builder ();
23177
23178 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23179 offset_size, offset, section_is_gnu);
23180}
23181
3019eac3
DE
23182/* Return the .debug_loc section to use for CU.
23183 For DWO files use .debug_loc.dwo. */
23184
23185static struct dwarf2_section_info *
23186cu_debug_loc_section (struct dwarf2_cu *cu)
23187{
518817b3
SM
23188 struct dwarf2_per_objfile *dwarf2_per_objfile
23189 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23190
3019eac3 23191 if (cu->dwo_unit)
43988095
JK
23192 {
23193 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23194
43988095
JK
23195 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23196 }
5989a64e
SM
23197 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23198 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23199}
23200
8cf6f0b1
TT
23201/* A helper function that fills in a dwarf2_loclist_baton. */
23202
23203static void
23204fill_in_loclist_baton (struct dwarf2_cu *cu,
23205 struct dwarf2_loclist_baton *baton,
ff39bb5e 23206 const struct attribute *attr)
8cf6f0b1 23207{
518817b3
SM
23208 struct dwarf2_per_objfile *dwarf2_per_objfile
23209 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23210 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23211
96b79293 23212 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23213
a50264ba 23214 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23215 baton->per_cu = cu->per_cu;
23216 gdb_assert (baton->per_cu);
23217 /* We don't know how long the location list is, but make sure we
23218 don't run off the edge of the section. */
3019eac3
DE
23219 baton->size = section->size - DW_UNSND (attr);
23220 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23221 if (cu->base_address.has_value ())
23222 baton->base_address = *cu->base_address;
23223 else
23224 baton->base_address = 0;
f664829e 23225 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23226}
23227
4c2df51b 23228static void
ff39bb5e 23229dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23230 struct dwarf2_cu *cu, int is_block)
4c2df51b 23231{
518817b3
SM
23232 struct dwarf2_per_objfile *dwarf2_per_objfile
23233 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23234 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23235 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23236
cd6c91b4 23237 if (attr->form_is_section_offset ()
3019eac3 23238 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23239 the section. If so, fall through to the complaint in the
23240 other branch. */
2c7d5afc 23241 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23242 {
0d53c4c4 23243 struct dwarf2_loclist_baton *baton;
4c2df51b 23244
8d749320 23245 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23246
8cf6f0b1 23247 fill_in_loclist_baton (cu, baton, attr);
be391dca 23248
2b24b6e4 23249 if (!cu->base_address.has_value ())
b98664d3 23250 complaint (_("Location list used without "
3e43a32a 23251 "specifying the CU base address."));
4c2df51b 23252
f1e6e072
TT
23253 SYMBOL_ACLASS_INDEX (sym) = (is_block
23254 ? dwarf2_loclist_block_index
23255 : dwarf2_loclist_index);
0d53c4c4
DJ
23256 SYMBOL_LOCATION_BATON (sym) = baton;
23257 }
23258 else
23259 {
23260 struct dwarf2_locexpr_baton *baton;
23261
8d749320 23262 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23263 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23264 baton->per_cu = cu->per_cu;
23265 gdb_assert (baton->per_cu);
0d53c4c4 23266
4fc6c0d5 23267 if (attr->form_is_block ())
0d53c4c4
DJ
23268 {
23269 /* Note that we're just copying the block's data pointer
23270 here, not the actual data. We're still pointing into the
6502dd73
DJ
23271 info_buffer for SYM's objfile; right now we never release
23272 that buffer, but when we do clean up properly this may
23273 need to change. */
0d53c4c4
DJ
23274 baton->size = DW_BLOCK (attr)->size;
23275 baton->data = DW_BLOCK (attr)->data;
23276 }
23277 else
23278 {
23279 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23280 sym->natural_name ());
0d53c4c4 23281 baton->size = 0;
0d53c4c4 23282 }
6e70227d 23283
f1e6e072
TT
23284 SYMBOL_ACLASS_INDEX (sym) = (is_block
23285 ? dwarf2_locexpr_block_index
23286 : dwarf2_locexpr_index);
0d53c4c4
DJ
23287 SYMBOL_LOCATION_BATON (sym) = baton;
23288 }
4c2df51b 23289}
6502dd73 23290
09ba997f 23291/* See read.h. */
ae0d2f24
UW
23292
23293struct objfile *
09ba997f 23294dwarf2_per_cu_data::objfile () const
ae0d2f24 23295{
09ba997f 23296 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23297
23298 /* Return the master objfile, so that we can report and look up the
23299 correct file containing this variable. */
23300 if (objfile->separate_debug_objfile_backlink)
23301 objfile = objfile->separate_debug_objfile_backlink;
23302
23303 return objfile;
23304}
23305
96408a79
SA
23306/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23307 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23308 CU_HEADERP first. */
23309
23310static const struct comp_unit_head *
23311per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23312 const struct dwarf2_per_cu_data *per_cu)
96408a79 23313{
d521ce57 23314 const gdb_byte *info_ptr;
96408a79
SA
23315
23316 if (per_cu->cu)
23317 return &per_cu->cu->header;
23318
9c541725 23319 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23320
23321 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23322 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23323 rcuh_kind::COMPILE);
96408a79
SA
23324
23325 return cu_headerp;
23326}
23327
09ba997f 23328/* See read.h. */
ae0d2f24 23329
98714339 23330int
09ba997f 23331dwarf2_per_cu_data::addr_size () const
ae0d2f24 23332{
96408a79
SA
23333 struct comp_unit_head cu_header_local;
23334 const struct comp_unit_head *cu_headerp;
c471e790 23335
09ba997f 23336 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23337
23338 return cu_headerp->addr_size;
ae0d2f24
UW
23339}
23340
09ba997f 23341/* See read.h. */
9eae7c52
TT
23342
23343int
09ba997f 23344dwarf2_per_cu_data::offset_size () const
9eae7c52 23345{
96408a79
SA
23346 struct comp_unit_head cu_header_local;
23347 const struct comp_unit_head *cu_headerp;
9c6c53f7 23348
09ba997f 23349 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23350
23351 return cu_headerp->offset_size;
23352}
23353
09ba997f 23354/* See read.h. */
96408a79
SA
23355
23356int
09ba997f 23357dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23358{
23359 struct comp_unit_head cu_header_local;
23360 const struct comp_unit_head *cu_headerp;
23361
09ba997f 23362 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23363
23364 if (cu_headerp->version == 2)
23365 return cu_headerp->addr_size;
23366 else
23367 return cu_headerp->offset_size;
181cebd4
JK
23368}
23369
09ba997f 23370/* See read.h. */
9aa1f1e3
TT
23371
23372CORE_ADDR
09ba997f 23373dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23374{
09ba997f
TT
23375 struct objfile *objfile = dwarf2_per_objfile->objfile;
23376
23377 return objfile->text_section_offset ();
9aa1f1e3
TT
23378}
23379
09ba997f
TT
23380/* See read.h. */
23381
23382struct type *
23383dwarf2_per_cu_data::addr_type () const
9a49df9d 23384{
09ba997f 23385 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23386 struct type *void_type = objfile_type (objfile)->builtin_void;
23387 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23388 int addr_size = this->addr_size ();
9a49df9d
AB
23389
23390 if (TYPE_LENGTH (addr_type) == addr_size)
23391 return addr_type;
23392
09ba997f 23393 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23394 return addr_type;
23395}
23396
22b6cd70
TT
23397/* A helper function for dwarf2_find_containing_comp_unit that returns
23398 the index of the result, and that searches a vector. It will
23399 return a result even if the offset in question does not actually
23400 occur in any CU. This is separate so that it can be unit
23401 tested. */
ae038cb0 23402
22b6cd70
TT
23403static int
23404dwarf2_find_containing_comp_unit
23405 (sect_offset sect_off,
23406 unsigned int offset_in_dwz,
23407 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23408{
ae038cb0
DJ
23409 int low, high;
23410
ae038cb0 23411 low = 0;
22b6cd70 23412 high = all_comp_units.size () - 1;
ae038cb0
DJ
23413 while (high > low)
23414 {
36586728 23415 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23416 int mid = low + (high - low) / 2;
9a619af0 23417
22b6cd70 23418 mid_cu = all_comp_units[mid];
36586728 23419 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23420 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23421 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23422 high = mid;
23423 else
23424 low = mid + 1;
23425 }
23426 gdb_assert (low == high);
22b6cd70
TT
23427 return low;
23428}
23429
23430/* Locate the .debug_info compilation unit from CU's objfile which contains
23431 the DIE at OFFSET. Raises an error on failure. */
23432
23433static struct dwarf2_per_cu_data *
23434dwarf2_find_containing_comp_unit (sect_offset sect_off,
23435 unsigned int offset_in_dwz,
23436 struct dwarf2_per_objfile *dwarf2_per_objfile)
23437{
23438 int low
23439 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23440 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23441 struct dwarf2_per_cu_data *this_cu
5989a64e 23442 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23443
45b8ae0c 23444 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23445 {
36586728 23446 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23447 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23448 "offset %s [in module %s]"),
23449 sect_offset_str (sect_off),
ed2dc618 23450 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23451
5989a64e 23452 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23453 <= sect_off);
5989a64e 23454 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23455 }
23456 else
23457 {
5989a64e 23458 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23459 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23460 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23461 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23462 return this_cu;
23463 }
23464}
23465
22b6cd70
TT
23466#if GDB_SELF_TEST
23467
23468namespace selftests {
23469namespace find_containing_comp_unit {
23470
23471static void
23472run_test ()
23473{
23474 struct dwarf2_per_cu_data one {};
23475 struct dwarf2_per_cu_data two {};
23476 struct dwarf2_per_cu_data three {};
23477 struct dwarf2_per_cu_data four {};
23478
23479 one.length = 5;
23480 two.sect_off = sect_offset (one.length);
23481 two.length = 7;
23482
23483 three.length = 5;
23484 three.is_dwz = 1;
23485 four.sect_off = sect_offset (three.length);
23486 four.length = 7;
23487 four.is_dwz = 1;
23488
23489 std::vector<dwarf2_per_cu_data *> units;
23490 units.push_back (&one);
23491 units.push_back (&two);
23492 units.push_back (&three);
23493 units.push_back (&four);
23494
23495 int result;
23496
23497 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23498 SELF_CHECK (units[result] == &one);
23499 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23500 SELF_CHECK (units[result] == &one);
23501 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23502 SELF_CHECK (units[result] == &two);
23503
23504 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23505 SELF_CHECK (units[result] == &three);
23506 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23507 SELF_CHECK (units[result] == &three);
23508 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23509 SELF_CHECK (units[result] == &four);
23510}
23511
23512}
23513}
23514
23515#endif /* GDB_SELF_TEST */
23516
23745b47 23517/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23518
fcd3b13d
SM
23519dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23520 : per_cu (per_cu_),
9068261f
AB
23521 mark (false),
23522 has_loclist (false),
23523 checked_producer (false),
23524 producer_is_gxx_lt_4_6 (false),
23525 producer_is_gcc_lt_4_3 (false),
eb77c9df 23526 producer_is_icc (false),
9068261f 23527 producer_is_icc_lt_14 (false),
c258c396 23528 producer_is_codewarrior (false),
9068261f 23529 processing_has_namespace_info (false)
93311388 23530{
fcd3b13d
SM
23531 per_cu->cu = this;
23532}
23533
23534/* Destroy a dwarf2_cu. */
23535
23536dwarf2_cu::~dwarf2_cu ()
23537{
23538 per_cu->cu = NULL;
9816fde3
JK
23539}
23540
23541/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23542
23543static void
95554aad
TT
23544prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23545 enum language pretend_language)
9816fde3
JK
23546{
23547 struct attribute *attr;
23548
23549 /* Set the language we're debugging. */
23550 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23551 if (attr != nullptr)
9816fde3
JK
23552 set_cu_language (DW_UNSND (attr), cu);
23553 else
9cded63f 23554 {
95554aad 23555 cu->language = pretend_language;
9cded63f
TT
23556 cu->language_defn = language_def (cu->language);
23557 }
dee91e82 23558
7d45c7c3 23559 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23560}
23561
ae038cb0
DJ
23562/* Increase the age counter on each cached compilation unit, and free
23563 any that are too old. */
23564
23565static void
ed2dc618 23566age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23567{
23568 struct dwarf2_per_cu_data *per_cu, **last_chain;
23569
5989a64e
SM
23570 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23571 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23572 while (per_cu != NULL)
23573 {
23574 per_cu->cu->last_used ++;
b4f54984 23575 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23576 dwarf2_mark (per_cu->cu);
23577 per_cu = per_cu->cu->read_in_chain;
23578 }
23579
5989a64e
SM
23580 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23581 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23582 while (per_cu != NULL)
23583 {
23584 struct dwarf2_per_cu_data *next_cu;
23585
23586 next_cu = per_cu->cu->read_in_chain;
23587
23588 if (!per_cu->cu->mark)
23589 {
fcd3b13d 23590 delete per_cu->cu;
ae038cb0
DJ
23591 *last_chain = next_cu;
23592 }
23593 else
23594 last_chain = &per_cu->cu->read_in_chain;
23595
23596 per_cu = next_cu;
23597 }
23598}
23599
23600/* Remove a single compilation unit from the cache. */
23601
23602static void
dee91e82 23603free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23604{
23605 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23606 struct dwarf2_per_objfile *dwarf2_per_objfile
23607 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23608
5989a64e
SM
23609 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23610 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23611 while (per_cu != NULL)
23612 {
23613 struct dwarf2_per_cu_data *next_cu;
23614
23615 next_cu = per_cu->cu->read_in_chain;
23616
dee91e82 23617 if (per_cu == target_per_cu)
ae038cb0 23618 {
fcd3b13d 23619 delete per_cu->cu;
dee91e82 23620 per_cu->cu = NULL;
ae038cb0
DJ
23621 *last_chain = next_cu;
23622 break;
23623 }
23624 else
23625 last_chain = &per_cu->cu->read_in_chain;
23626
23627 per_cu = next_cu;
23628 }
23629}
23630
dee91e82
DE
23631/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23632 We store these in a hash table separate from the DIEs, and preserve them
23633 when the DIEs are flushed out of cache.
23634
23635 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23636 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23637 or the type may come from a DWO file. Furthermore, while it's more logical
23638 to use per_cu->section+offset, with Fission the section with the data is in
23639 the DWO file but we don't know that section at the point we need it.
23640 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23641 because we can enter the lookup routine, get_die_type_at_offset, from
23642 outside this file, and thus won't necessarily have PER_CU->cu.
23643 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23644
dee91e82 23645struct dwarf2_per_cu_offset_and_type
1c379e20 23646{
dee91e82 23647 const struct dwarf2_per_cu_data *per_cu;
9c541725 23648 sect_offset sect_off;
1c379e20
DJ
23649 struct type *type;
23650};
23651
dee91e82 23652/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23653
23654static hashval_t
dee91e82 23655per_cu_offset_and_type_hash (const void *item)
1c379e20 23656{
9a3c8263
SM
23657 const struct dwarf2_per_cu_offset_and_type *ofs
23658 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23659
9c541725 23660 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23661}
23662
dee91e82 23663/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23664
23665static int
dee91e82 23666per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23667{
9a3c8263
SM
23668 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23669 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23670 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23671 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23672
dee91e82 23673 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23674 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23675}
23676
23677/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23678 table if necessary. For convenience, return TYPE.
23679
23680 The DIEs reading must have careful ordering to:
85102364 23681 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23682 reading current DIE.
23683 * Not trying to dereference contents of still incompletely read in types
23684 while reading in other DIEs.
23685 * Enable referencing still incompletely read in types just by a pointer to
23686 the type without accessing its fields.
23687
23688 Therefore caller should follow these rules:
23689 * Try to fetch any prerequisite types we may need to build this DIE type
23690 before building the type and calling set_die_type.
e71ec853 23691 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23692 possible before fetching more types to complete the current type.
23693 * Make the type as complete as possible before fetching more types. */
1c379e20 23694
f792889a 23695static struct type *
1c379e20
DJ
23696set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23697{
518817b3
SM
23698 struct dwarf2_per_objfile *dwarf2_per_objfile
23699 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23700 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23701 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23702 struct attribute *attr;
23703 struct dynamic_prop prop;
1c379e20 23704
b4ba55a1
JB
23705 /* For Ada types, make sure that the gnat-specific data is always
23706 initialized (if not already set). There are a few types where
23707 we should not be doing so, because the type-specific area is
23708 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23709 where the type-specific area is used to store the floatformat).
23710 But this is not a problem, because the gnat-specific information
23711 is actually not needed for these types. */
23712 if (need_gnat_info (cu)
78134374
SM
23713 && type->code () != TYPE_CODE_FUNC
23714 && type->code () != TYPE_CODE_FLT
23715 && type->code () != TYPE_CODE_METHODPTR
23716 && type->code () != TYPE_CODE_MEMBERPTR
23717 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23718 && !HAVE_GNAT_AUX_INFO (type))
23719 INIT_GNAT_SPECIFIC (type);
23720
3f2f83dd
KB
23721 /* Read DW_AT_allocated and set in type. */
23722 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23723 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23724 {
09ba997f 23725 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23726 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23727 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23728 }
23729 else if (attr != NULL)
23730 {
b98664d3 23731 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23732 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23733 sect_offset_str (die->sect_off));
3f2f83dd
KB
23734 }
23735
23736 /* Read DW_AT_associated and set in type. */
23737 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23738 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23739 {
09ba997f 23740 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23741 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23742 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23743 }
23744 else if (attr != NULL)
23745 {
b98664d3 23746 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23747 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23748 sect_offset_str (die->sect_off));
3f2f83dd
KB
23749 }
23750
3cdcd0ce
JB
23751 /* Read DW_AT_data_location and set in type. */
23752 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23753 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23754 cu->per_cu->addr_type ()))
5c54719c 23755 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23756
ae090bdb
SM
23757 if (dwarf2_per_objfile->die_type_hash == NULL)
23758 dwarf2_per_objfile->die_type_hash
0335378b
TT
23759 = htab_up (htab_create_alloc (127,
23760 per_cu_offset_and_type_hash,
23761 per_cu_offset_and_type_eq,
23762 NULL, xcalloc, xfree));
1c379e20 23763
dee91e82 23764 ofs.per_cu = cu->per_cu;
9c541725 23765 ofs.sect_off = die->sect_off;
1c379e20 23766 ofs.type = type;
dee91e82 23767 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23768 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23769 if (*slot)
b98664d3 23770 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23771 sect_offset_str (die->sect_off));
8d749320
SM
23772 *slot = XOBNEW (&objfile->objfile_obstack,
23773 struct dwarf2_per_cu_offset_and_type);
1c379e20 23774 **slot = ofs;
f792889a 23775 return type;
1c379e20
DJ
23776}
23777
9c541725 23778/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23779 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23780
23781static struct type *
9c541725 23782get_die_type_at_offset (sect_offset sect_off,
673bfd45 23783 struct dwarf2_per_cu_data *per_cu)
1c379e20 23784{
dee91e82 23785 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23786 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23787
ae090bdb 23788 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23789 return NULL;
1c379e20 23790
dee91e82 23791 ofs.per_cu = per_cu;
9c541725 23792 ofs.sect_off = sect_off;
9a3c8263 23793 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23794 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23795 if (slot)
23796 return slot->type;
23797 else
23798 return NULL;
23799}
23800
02142a6c 23801/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23802 or return NULL if DIE does not have a saved type. */
23803
23804static struct type *
23805get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23806{
9c541725 23807 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23808}
23809
10b3939b
DJ
23810/* Add a dependence relationship from CU to REF_PER_CU. */
23811
23812static void
23813dwarf2_add_dependence (struct dwarf2_cu *cu,
23814 struct dwarf2_per_cu_data *ref_per_cu)
23815{
23816 void **slot;
23817
23818 if (cu->dependencies == NULL)
23819 cu->dependencies
23820 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23821 NULL, &cu->comp_unit_obstack,
23822 hashtab_obstack_allocate,
23823 dummy_obstack_deallocate);
23824
23825 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23826 if (*slot == NULL)
23827 *slot = ref_per_cu;
23828}
1c379e20 23829
f504f079
DE
23830/* Subroutine of dwarf2_mark to pass to htab_traverse.
23831 Set the mark field in every compilation unit in the
ae038cb0
DJ
23832 cache that we must keep because we are keeping CU. */
23833
10b3939b
DJ
23834static int
23835dwarf2_mark_helper (void **slot, void *data)
23836{
23837 struct dwarf2_per_cu_data *per_cu;
23838
23839 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23840
23841 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23842 reading of the chain. As such dependencies remain valid it is not much
23843 useful to track and undo them during QUIT cleanups. */
23844 if (per_cu->cu == NULL)
23845 return 1;
23846
10b3939b
DJ
23847 if (per_cu->cu->mark)
23848 return 1;
9068261f 23849 per_cu->cu->mark = true;
10b3939b
DJ
23850
23851 if (per_cu->cu->dependencies != NULL)
23852 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23853
23854 return 1;
23855}
23856
f504f079
DE
23857/* Set the mark field in CU and in every other compilation unit in the
23858 cache that we must keep because we are keeping CU. */
23859
ae038cb0
DJ
23860static void
23861dwarf2_mark (struct dwarf2_cu *cu)
23862{
23863 if (cu->mark)
23864 return;
9068261f 23865 cu->mark = true;
10b3939b
DJ
23866 if (cu->dependencies != NULL)
23867 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23868}
23869
23870static void
23871dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23872{
23873 while (per_cu)
23874 {
9068261f 23875 per_cu->cu->mark = false;
ae038cb0
DJ
23876 per_cu = per_cu->cu->read_in_chain;
23877 }
72bf9492
DJ
23878}
23879
72bf9492
DJ
23880/* Trivial hash function for partial_die_info: the hash value of a DIE
23881 is its offset in .debug_info for this objfile. */
23882
23883static hashval_t
23884partial_die_hash (const void *item)
23885{
9a3c8263
SM
23886 const struct partial_die_info *part_die
23887 = (const struct partial_die_info *) item;
9a619af0 23888
9c541725 23889 return to_underlying (part_die->sect_off);
72bf9492
DJ
23890}
23891
23892/* Trivial comparison function for partial_die_info structures: two DIEs
23893 are equal if they have the same offset. */
23894
23895static int
23896partial_die_eq (const void *item_lhs, const void *item_rhs)
23897{
9a3c8263
SM
23898 const struct partial_die_info *part_die_lhs
23899 = (const struct partial_die_info *) item_lhs;
23900 const struct partial_die_info *part_die_rhs
23901 = (const struct partial_die_info *) item_rhs;
9a619af0 23902
9c541725 23903 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23904}
23905
3c3bb058
AB
23906struct cmd_list_element *set_dwarf_cmdlist;
23907struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23908
9291a0cd 23909static void
cd4fb1b2
SM
23910show_check_physname (struct ui_file *file, int from_tty,
23911 struct cmd_list_element *c, const char *value)
9291a0cd 23912{
cd4fb1b2
SM
23913 fprintf_filtered (file,
23914 _("Whether to check \"physname\" is %s.\n"),
23915 value);
9291a0cd
TT
23916}
23917
6c265988 23918void _initialize_dwarf2_read ();
cd4fb1b2 23919void
6c265988 23920_initialize_dwarf2_read ()
9291a0cd 23921{
0743fc83 23922 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23923Set DWARF specific variables.\n\
590042fc 23924Configure DWARF variables such as the cache size."),
0743fc83
TT
23925 &set_dwarf_cmdlist, "maintenance set dwarf ",
23926 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23927
0743fc83 23928 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23929Show DWARF specific variables.\n\
23930Show DWARF variables such as the cache size."),
0743fc83
TT
23931 &show_dwarf_cmdlist, "maintenance show dwarf ",
23932 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23933
cd4fb1b2
SM
23934 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23935 &dwarf_max_cache_age, _("\
23936Set the upper bound on the age of cached DWARF compilation units."), _("\
23937Show the upper bound on the age of cached DWARF compilation units."), _("\
23938A higher limit means that cached compilation units will be stored\n\
23939in memory longer, and more total memory will be used. Zero disables\n\
23940caching, which can slow down startup."),
23941 NULL,
23942 show_dwarf_max_cache_age,
23943 &set_dwarf_cmdlist,
23944 &show_dwarf_cmdlist);
156942c7 23945
cd4fb1b2
SM
23946 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23947Set debugging of the DWARF reader."), _("\
23948Show debugging of the DWARF reader."), _("\
23949When enabled (non-zero), debugging messages are printed during DWARF\n\
23950reading and symtab expansion. A value of 1 (one) provides basic\n\
23951information. A value greater than 1 provides more verbose information."),
23952 NULL,
23953 NULL,
23954 &setdebuglist, &showdebuglist);
9291a0cd 23955
cd4fb1b2
SM
23956 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23957Set debugging of the DWARF DIE reader."), _("\
23958Show debugging of the DWARF DIE reader."), _("\
23959When enabled (non-zero), DIEs are dumped after they are read in.\n\
23960The value is the maximum depth to print."),
23961 NULL,
23962 NULL,
23963 &setdebuglist, &showdebuglist);
9291a0cd 23964
cd4fb1b2
SM
23965 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23966Set debugging of the dwarf line reader."), _("\
23967Show debugging of the dwarf line reader."), _("\
23968When enabled (non-zero), line number entries are dumped as they are read in.\n\
23969A value of 1 (one) provides basic information.\n\
23970A value greater than 1 provides more verbose information."),
23971 NULL,
23972 NULL,
23973 &setdebuglist, &showdebuglist);
437afbb8 23974
cd4fb1b2
SM
23975 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23976Set cross-checking of \"physname\" code against demangler."), _("\
23977Show cross-checking of \"physname\" code against demangler."), _("\
23978When enabled, GDB's internal \"physname\" code is checked against\n\
23979the demangler."),
23980 NULL, show_check_physname,
23981 &setdebuglist, &showdebuglist);
900e11f9 23982
e615022a
DE
23983 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23984 no_class, &use_deprecated_index_sections, _("\
23985Set whether to use deprecated gdb_index sections."), _("\
23986Show whether to use deprecated gdb_index sections."), _("\
23987When enabled, deprecated .gdb_index sections are used anyway.\n\
23988Normally they are ignored either because of a missing feature or\n\
23989performance issue.\n\
23990Warning: This option must be enabled before gdb reads the file."),
23991 NULL,
23992 NULL,
23993 &setlist, &showlist);
23994
f1e6e072
TT
23995 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23996 &dwarf2_locexpr_funcs);
23997 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23998 &dwarf2_loclist_funcs);
23999
24000 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24001 &dwarf2_block_frame_base_locexpr_funcs);
24002 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24003 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24004
24005#if GDB_SELF_TEST
24006 selftests::register_test ("dw2_expand_symtabs_matching",
24007 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24008 selftests::register_test ("dwarf2_find_containing_comp_unit",
24009 selftests::find_containing_comp_unit::run_test);
c62446b1 24010#endif
6502dd73 24011}
This page took 5.319636 seconds and 4 git commands to generate.