[PATCH 1/4]: microblaze: remove duplicate prototypes
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
2ddeaf8a
TT
1085/* When reading a variant or variant part, we track a bit more
1086 information about the field, and store it in an object of this
1087 type. */
1088
1089struct variant_field
1090{
1091 /* If we see a DW_TAG_variant, then this will be the discriminant
1092 value. */
1093 ULONGEST discriminant_value;
1094 /* If we see a DW_TAG_variant, then this will be set if this is the
1095 default branch. */
1096 bool default_branch;
1097 /* While reading a DW_TAG_variant_part, this will be set if this
1098 field is the discriminant. */
1099 bool is_discriminant;
1100};
1101
52059ffd
TT
1102struct nextfield
1103{
be2daae6
TT
1104 int accessibility = 0;
1105 int virtuality = 0;
2ddeaf8a 1106 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1107 struct variant_field variant {};
1108 struct field field {};
52059ffd
TT
1109};
1110
1111struct fnfieldlist
1112{
be2daae6
TT
1113 const char *name = nullptr;
1114 std::vector<struct fn_field> fnfields;
52059ffd
TT
1115};
1116
c906108c
SS
1117/* The routines that read and process dies for a C struct or C++ class
1118 pass lists of data member fields and lists of member function fields
1119 in an instance of a field_info structure, as defined below. */
1120struct field_info
c5aa993b 1121 {
0963b4bd 1122 /* List of data member and baseclasses fields. */
be2daae6
TT
1123 std::vector<struct nextfield> fields;
1124 std::vector<struct nextfield> baseclasses;
c906108c 1125
85102364 1126 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1127 int non_public_fields = 0;
c906108c 1128
c5aa993b
JM
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
be2daae6 1132 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1133
1134 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1135 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1136 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1137
1138 /* Nested types defined by this class and the number of elements in this
1139 list. */
be2daae6 1140 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1141
1142 /* Return the total number of fields (including baseclasses). */
1143 int nfields () const
1144 {
1145 return fields.size () + baseclasses.size ();
1146 }
c5aa993b 1147 };
c906108c 1148
ae038cb0
DJ
1149/* Loaded secondary compilation units are kept in memory until they
1150 have not been referenced for the processing of this many
1151 compilation units. Set this to zero to disable caching. Cache
1152 sizes of up to at least twenty will improve startup time for
1153 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1154static int dwarf_max_cache_age = 5;
920d2a44 1155static void
b4f54984
DE
1156show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1157 struct cmd_list_element *c, const char *value)
920d2a44 1158{
3e43a32a 1159 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1160 "DWARF compilation units is %s.\n"),
920d2a44
AC
1161 value);
1162}
4390d890 1163\f
c906108c
SS
1164/* local function prototypes */
1165
918dd910
JK
1166static void dwarf2_find_base_address (struct die_info *die,
1167 struct dwarf2_cu *cu);
1168
891813be 1169static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1170 (struct dwarf2_per_cu_data *per_cu, const char *name);
1171
f1902523
JK
1172static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1173 const gdb_byte *info_ptr,
3e225074 1174 struct die_info *type_unit_die);
f1902523 1175
ed2dc618
SM
1176static void dwarf2_build_psymtabs_hard
1177 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1178
72bf9492
DJ
1179static void scan_partial_symbols (struct partial_die_info *,
1180 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1181 int, struct dwarf2_cu *);
c906108c 1182
72bf9492
DJ
1183static void add_partial_symbol (struct partial_die_info *,
1184 struct dwarf2_cu *);
63d06c5c 1185
72bf9492
DJ
1186static void add_partial_namespace (struct partial_die_info *pdi,
1187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1188 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1189
5d7cb8df 1190static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1191 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1192 struct dwarf2_cu *cu);
1193
72bf9492
DJ
1194static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1195 struct dwarf2_cu *cu);
91c24f0a 1196
bc30ff58
JB
1197static void add_partial_subprogram (struct partial_die_info *pdi,
1198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1199 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1200
d521ce57 1201static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1202
dee91e82 1203static struct partial_die_info *load_partial_dies
d521ce57 1204 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1205
fb816e8b
TV
1206/* A pair of partial_die_info and compilation unit. */
1207struct cu_partial_die_info
1208{
1209 /* The compilation unit of the partial_die_info. */
1210 struct dwarf2_cu *cu;
1211 /* A partial_die_info. */
1212 struct partial_die_info *pdi;
122cf0f2
AB
1213
1214 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1215 : cu (cu),
1216 pdi (pdi)
405feb71 1217 { /* Nothing. */ }
122cf0f2
AB
1218
1219private:
1220 cu_partial_die_info () = delete;
fb816e8b
TV
1221};
1222
122cf0f2
AB
1223static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1224 struct dwarf2_cu *);
72bf9492 1225
d521ce57
TT
1226static const gdb_byte *read_attribute (const struct die_reader_specs *,
1227 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1228 const gdb_byte *, bool *need_reprocess);
1229
1230static void read_attribute_reprocess (const struct die_reader_specs *reader,
1231 struct attribute *attr);
1232
1233static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1234
ed2dc618
SM
1235static sect_offset read_abbrev_offset
1236 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1237 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1238
ed2dc618
SM
1239static const char *read_indirect_string
1240 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1241 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1242
ed2dc618 1243static const char *read_indirect_string_at_offset
4f44ae6c 1244 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1245
d521ce57
TT
1246static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1247 const gdb_byte *,
3019eac3
DE
1248 unsigned int *);
1249
18a8505e
AT
1250static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1251 ULONGEST str_index);
1252
1253static const char *read_stub_str_index (struct dwarf2_cu *cu,
1254 ULONGEST str_index);
3019eac3 1255
e142c38c 1256static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1257
e142c38c
DJ
1258static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1259 struct dwarf2_cu *);
c906108c 1260
7d45c7c3
KB
1261static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1262 struct dwarf2_cu *cu);
1263
a084a2a6
AT
1264static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1265
05cf31d1
JB
1266static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1267 struct dwarf2_cu *cu);
1268
e142c38c 1269static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1270
e142c38c 1271static struct die_info *die_specification (struct die_info *die,
f2f0e013 1272 struct dwarf2_cu **);
63d06c5c 1273
9c541725 1274static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1275 struct dwarf2_cu *cu);
debd256d 1276
f3f5162e 1277static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1278 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1279 CORE_ADDR, int decode_mapping);
c906108c 1280
804d2729
TT
1281static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1282 const char *);
c906108c 1283
a14ed312 1284static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1285 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1286
ff39bb5e 1287static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1288 struct dwarf2_cu *);
c906108c 1289
ff39bb5e 1290static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1291 struct type *type,
1292 const char *name,
1293 struct obstack *obstack,
12df843f 1294 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1295 const gdb_byte **bytes,
98bfdba5 1296 struct dwarf2_locexpr_baton **baton);
2df3850c 1297
e7c27a73 1298static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1299
b4ba55a1
JB
1300static int need_gnat_info (struct dwarf2_cu *);
1301
3e43a32a
MS
1302static struct type *die_descriptive_type (struct die_info *,
1303 struct dwarf2_cu *);
b4ba55a1
JB
1304
1305static void set_descriptive_type (struct type *, struct die_info *,
1306 struct dwarf2_cu *);
1307
e7c27a73
DJ
1308static struct type *die_containing_type (struct die_info *,
1309 struct dwarf2_cu *);
c906108c 1310
ff39bb5e 1311static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1312 struct dwarf2_cu *);
c906108c 1313
f792889a 1314static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1315
673bfd45
DE
1316static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1317
0d5cff50 1318static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1319
6e70227d 1320static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1321 const char *suffix, int physname,
1322 struct dwarf2_cu *cu);
63d06c5c 1323
e7c27a73 1324static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1325
348e048f
DE
1326static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1327
e7c27a73 1328static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1329
e7c27a73 1330static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1331
96408a79
SA
1332static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1333
71a3c369
TT
1334static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1335
ff013f42 1336static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1337 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1338
41144253 1339/* Return the .debug_loclists section to use for cu. */
1340static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1341
3a2b436a 1342/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1343 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1344enum pc_bounds_kind
1345{
e385593e 1346 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1347 PC_BOUNDS_NOT_PRESENT,
1348
e385593e
JK
1349 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1350 were present but they do not form a valid range of PC addresses. */
1351 PC_BOUNDS_INVALID,
1352
3a2b436a
JK
1353 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1354 PC_BOUNDS_RANGES,
1355
1356 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1357 PC_BOUNDS_HIGH_LOW,
1358};
1359
1360static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1361 CORE_ADDR *, CORE_ADDR *,
1362 struct dwarf2_cu *,
891813be 1363 dwarf2_psymtab *);
c906108c 1364
fae299cd
DC
1365static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
801e3a5b
JB
1369static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
a14ed312 1372static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1373 struct dwarf2_cu *);
c906108c 1374
a14ed312 1375static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1376 struct type *, struct dwarf2_cu *);
c906108c 1377
a14ed312 1378static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1379 struct die_info *, struct type *,
e7c27a73 1380 struct dwarf2_cu *);
c906108c 1381
a14ed312 1382static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1383 struct type *,
1384 struct dwarf2_cu *);
c906108c 1385
134d01f1 1386static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1387
e7c27a73 1388static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1389
e7c27a73 1390static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1391
5d7cb8df
JK
1392static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
804d2729 1394static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1395
27aa8d6a
SW
1396static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1397
74921315
KS
1398static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1399
f55ee35c
JK
1400static struct type *read_module_type (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
38d518c9 1403static const char *namespace_name (struct die_info *die,
e142c38c 1404 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1405
134d01f1 1406static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1407
e7c27a73 1408static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1409
6e70227d 1410static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1411 struct dwarf2_cu *);
1412
bf6af496 1413static struct die_info *read_die_and_siblings_1
d521ce57 1414 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1415 struct die_info *);
639d11d3 1416
dee91e82 1417static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1418 const gdb_byte *info_ptr,
1419 const gdb_byte **new_info_ptr,
639d11d3
DC
1420 struct die_info *parent);
1421
d521ce57
TT
1422static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1423 struct die_info **, const gdb_byte *,
3e225074 1424 int);
3019eac3 1425
d521ce57 1426static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1427 struct die_info **, const gdb_byte *);
93311388 1428
e7c27a73 1429static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1430
15d034d0 1431static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1432 struct objfile *);
71c25dea 1433
15d034d0 1434static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1435
15d034d0 1436static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1437 struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
ca69b9e6
DE
1440static const char *dwarf2_physname (const char *name, struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
e142c38c 1443static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1444 struct dwarf2_cu **);
9219021c 1445
d97bc12b
DE
1446static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1447
1448static void dump_die_for_error (struct die_info *);
1449
1450static void dump_die_1 (struct ui_file *, int level, int max_level,
1451 struct die_info *);
c906108c 1452
d97bc12b 1453/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1454
51545339 1455static void store_in_ref_table (struct die_info *,
10b3939b 1456 struct dwarf2_cu *);
c906108c 1457
348e048f 1458static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1459 const struct attribute *,
348e048f
DE
1460 struct dwarf2_cu **);
1461
10b3939b 1462static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1463 const struct attribute *,
f2f0e013 1464 struct dwarf2_cu **);
c906108c 1465
348e048f 1466static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1467 const struct attribute *,
348e048f
DE
1468 struct dwarf2_cu **);
1469
ac9ec31b
DE
1470static struct type *get_signatured_type (struct die_info *, ULONGEST,
1471 struct dwarf2_cu *);
1472
1473static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1474 const struct attribute *,
ac9ec31b
DE
1475 struct dwarf2_cu *);
1476
e5fe5e75 1477static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1478
52dc124a 1479static void read_signatured_type (struct signatured_type *);
348e048f 1480
63e43d3a
PMR
1481static int attr_to_dynamic_prop (const struct attribute *attr,
1482 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1483 struct dynamic_prop *prop, struct type *type);
63e43d3a 1484
c906108c
SS
1485/* memory allocation interface */
1486
7b5a2f43 1487static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1488
b60c80d6 1489static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1490
43f3e411 1491static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1492
8cf6f0b1
TT
1493static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1494 struct dwarf2_loclist_baton *baton,
ff39bb5e 1495 const struct attribute *attr);
8cf6f0b1 1496
ff39bb5e 1497static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1498 struct symbol *sym,
f1e6e072
TT
1499 struct dwarf2_cu *cu,
1500 int is_block);
4c2df51b 1501
d521ce57
TT
1502static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 const gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
4bb7a0a7 1505
72bf9492
DJ
1506static hashval_t partial_die_hash (const void *item);
1507
1508static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1509
ae038cb0 1510static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1511 (sect_offset sect_off, unsigned int offset_in_dwz,
1512 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1513
9816fde3 1514static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1515 struct die_info *comp_unit_die,
1516 enum language pretend_language);
93311388 1517
ed2dc618 1518static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1519
dee91e82 1520static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1521
f792889a
DJ
1522static struct type *set_die_type (struct die_info *, struct type *,
1523 struct dwarf2_cu *);
1c379e20 1524
ed2dc618 1525static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1526
ed2dc618 1527static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1528
58f0c718 1529static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1530 enum language);
10b3939b 1531
95554aad
TT
1532static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1533 enum language);
10b3939b 1534
f4dc4d17
DE
1535static void process_full_type_unit (struct dwarf2_per_cu_data *,
1536 enum language);
1537
10b3939b
DJ
1538static void dwarf2_add_dependence (struct dwarf2_cu *,
1539 struct dwarf2_per_cu_data *);
1540
ae038cb0
DJ
1541static void dwarf2_mark (struct dwarf2_cu *);
1542
1543static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1544
b64f50a1 1545static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1546 struct dwarf2_per_cu_data *);
673bfd45 1547
f792889a 1548static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1549
95554aad
TT
1550static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1551 enum language pretend_language);
1552
ed2dc618 1553static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1554
b303c6f6
AB
1555/* Class, the destructor of which frees all allocated queue entries. This
1556 will only have work to do if an error was thrown while processing the
1557 dwarf. If no error was thrown then the queue entries should have all
1558 been processed, and freed, as we went along. */
1559
1560class dwarf2_queue_guard
1561{
1562public:
39856def
TT
1563 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1564 : m_per_objfile (per_objfile)
1565 {
1566 }
b303c6f6
AB
1567
1568 /* Free any entries remaining on the queue. There should only be
1569 entries left if we hit an error while processing the dwarf. */
1570 ~dwarf2_queue_guard ()
1571 {
39856def
TT
1572 /* Ensure that no memory is allocated by the queue. */
1573 std::queue<dwarf2_queue_item> empty;
1574 std::swap (m_per_objfile->queue, empty);
1575 }
b303c6f6 1576
39856def 1577 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1578
39856def
TT
1579private:
1580 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1581};
1582
39856def
TT
1583dwarf2_queue_item::~dwarf2_queue_item ()
1584{
1585 /* Anything still marked queued is likely to be in an
1586 inconsistent state, so discard it. */
1587 if (per_cu->queued)
1588 {
1589 if (per_cu->cu != NULL)
1590 free_one_cached_comp_unit (per_cu);
1591 per_cu->queued = 0;
1592 }
1593}
1594
d721ba37
PA
1595/* The return type of find_file_and_directory. Note, the enclosed
1596 string pointers are only valid while this object is valid. */
1597
1598struct file_and_directory
1599{
1600 /* The filename. This is never NULL. */
1601 const char *name;
1602
1603 /* The compilation directory. NULL if not known. If we needed to
1604 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1605 points directly to the DW_AT_comp_dir string attribute owned by
1606 the obstack that owns the DIE. */
1607 const char *comp_dir;
1608
1609 /* If we needed to build a new string for comp_dir, this is what
1610 owns the storage. */
1611 std::string comp_dir_storage;
1612};
1613
1614static file_and_directory find_file_and_directory (struct die_info *die,
1615 struct dwarf2_cu *cu);
9291a0cd 1616
298e9637 1617static htab_up allocate_signatured_type_table ();
1fd400ff 1618
298e9637 1619static htab_up allocate_dwo_unit_table ();
3019eac3 1620
57d63ce2 1621static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1622 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1623 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1624 ULONGEST signature, int is_debug_types);
a2ce51a0 1625
ed2dc618
SM
1626static struct dwp_file *get_dwp_file
1627 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1628
3019eac3 1629static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1630 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1631
1632static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1633 (struct signatured_type *, const char *, const char *);
3019eac3 1634
89e63ee4
DE
1635static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1636
263db9a1
TT
1637/* A unique pointer to a dwo_file. */
1638
51ac9db5 1639typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1640
ed2dc618 1641static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1642
1b80a9fa 1643static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1644
1645static void free_line_header_voidp (void *arg);
4390d890
DE
1646\f
1647/* Various complaints about symbol reading that don't abort the process. */
1648
4390d890
DE
1649static void
1650dwarf2_debug_line_missing_file_complaint (void)
1651{
b98664d3 1652 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1653}
1654
1655static void
1656dwarf2_debug_line_missing_end_sequence_complaint (void)
1657{
b98664d3 1658 complaint (_(".debug_line section has line "
4390d890
DE
1659 "program sequence without an end"));
1660}
1661
1662static void
1663dwarf2_complex_location_expr_complaint (void)
1664{
b98664d3 1665 complaint (_("location expression too complex"));
4390d890
DE
1666}
1667
1668static void
1669dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1670 int arg3)
1671{
b98664d3 1672 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1673 arg1, arg2, arg3);
1674}
1675
4390d890
DE
1676static void
1677dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1678{
b98664d3 1679 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1680 arg1, arg2);
1681}
527f3840
JK
1682
1683/* Hash function for line_header_hash. */
1684
1685static hashval_t
1686line_header_hash (const struct line_header *ofs)
1687{
9c541725 1688 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1689}
1690
1691/* Hash function for htab_create_alloc_ex for line_header_hash. */
1692
1693static hashval_t
1694line_header_hash_voidp (const void *item)
1695{
9a3c8263 1696 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1697
1698 return line_header_hash (ofs);
1699}
1700
1701/* Equality function for line_header_hash. */
1702
1703static int
1704line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1705{
9a3c8263
SM
1706 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1707 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1708
9c541725 1709 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1710 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1711}
1712
4390d890 1713\f
9291a0cd 1714
330cdd98
PA
1715/* See declaration. */
1716
1717dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1718 const dwarf2_debug_sections *names,
1719 bool can_copy_)
1720 : objfile (objfile_),
1721 can_copy (can_copy_)
330cdd98
PA
1722{
1723 if (names == NULL)
1724 names = &dwarf2_elf_names;
1725
1726 bfd *obfd = objfile->obfd;
1727
1728 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1729 locate_sections (obfd, sec, *names);
1730}
1731
1732dwarf2_per_objfile::~dwarf2_per_objfile ()
1733{
1734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1735 free_cached_comp_units ();
1736
b76e467d 1737 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1738 per_cu->imported_symtabs_free ();
fc8e7e75 1739
b2bdb8cf 1740 for (signatured_type *sig_type : all_type_units)
ae640021 1741 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1742
330cdd98
PA
1743 /* Everything else should be on the objfile obstack. */
1744}
1745
1746/* See declaration. */
1747
1748void
1749dwarf2_per_objfile::free_cached_comp_units ()
1750{
1751 dwarf2_per_cu_data *per_cu = read_in_chain;
1752 dwarf2_per_cu_data **last_chain = &read_in_chain;
1753 while (per_cu != NULL)
1754 {
1755 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1756
fcd3b13d 1757 delete per_cu->cu;
330cdd98
PA
1758 *last_chain = next_cu;
1759 per_cu = next_cu;
1760 }
1761}
1762
11ed8cad
TT
1763/* A helper class that calls free_cached_comp_units on
1764 destruction. */
1765
1766class free_cached_comp_units
1767{
1768public:
1769
1770 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1771 : m_per_objfile (per_objfile)
1772 {
1773 }
1774
1775 ~free_cached_comp_units ()
1776 {
1777 m_per_objfile->free_cached_comp_units ();
1778 }
1779
1780 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1781
1782private:
1783
1784 dwarf2_per_objfile *m_per_objfile;
1785};
1786
c906108c 1787/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1788 information and return true if we have enough to do something.
1789 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1790 ELF names are used. CAN_COPY is true for formats where symbol
1791 interposition is possible and so symbol values must follow copy
1792 relocation rules. */
c906108c
SS
1793
1794int
251d32d9 1795dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1796 const struct dwarf2_debug_sections *names,
1797 bool can_copy)
c906108c 1798{
97cbe998
SDJ
1799 if (objfile->flags & OBJF_READNEVER)
1800 return 0;
1801
ed2dc618
SM
1802 struct dwarf2_per_objfile *dwarf2_per_objfile
1803 = get_dwarf2_per_objfile (objfile);
1804
1805 if (dwarf2_per_objfile == NULL)
5bfd760d 1806 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1807 names,
1808 can_copy);
5bfd760d 1809
73869dc2 1810 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1811 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1812 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1813 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1814}
1815
251d32d9
TG
1816/* When loading sections, we look either for uncompressed section or for
1817 compressed section names. */
233a11ab
CS
1818
1819static int
251d32d9
TG
1820section_is_p (const char *section_name,
1821 const struct dwarf2_section_names *names)
233a11ab 1822{
251d32d9
TG
1823 if (names->normal != NULL
1824 && strcmp (section_name, names->normal) == 0)
1825 return 1;
1826 if (names->compressed != NULL
1827 && strcmp (section_name, names->compressed) == 0)
1828 return 1;
1829 return 0;
233a11ab
CS
1830}
1831
330cdd98 1832/* See declaration. */
c906108c 1833
330cdd98
PA
1834void
1835dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1836 const dwarf2_debug_sections &names)
c906108c 1837{
fd361982 1838 flagword aflag = bfd_section_flags (sectp);
251d32d9 1839
dc7650b8
JK
1840 if ((aflag & SEC_HAS_CONTENTS) == 0)
1841 {
1842 }
950b7495
KS
1843 else if (elf_section_data (sectp)->this_hdr.sh_size
1844 > bfd_get_file_size (abfd))
1845 {
1846 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1847 warning (_("Discarding section %s which has a section size (%s"
1848 ") larger than the file size [in module %s]"),
1849 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1850 bfd_get_filename (abfd));
1851 }
330cdd98 1852 else if (section_is_p (sectp->name, &names.info))
c906108c 1853 {
330cdd98 1854 this->info.s.section = sectp;
fd361982 1855 this->info.size = bfd_section_size (sectp);
c906108c 1856 }
330cdd98 1857 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1858 {
330cdd98 1859 this->abbrev.s.section = sectp;
fd361982 1860 this->abbrev.size = bfd_section_size (sectp);
c906108c 1861 }
330cdd98 1862 else if (section_is_p (sectp->name, &names.line))
c906108c 1863 {
330cdd98 1864 this->line.s.section = sectp;
fd361982 1865 this->line.size = bfd_section_size (sectp);
c906108c 1866 }
330cdd98 1867 else if (section_is_p (sectp->name, &names.loc))
c906108c 1868 {
330cdd98 1869 this->loc.s.section = sectp;
fd361982 1870 this->loc.size = bfd_section_size (sectp);
c906108c 1871 }
330cdd98 1872 else if (section_is_p (sectp->name, &names.loclists))
43988095 1873 {
330cdd98 1874 this->loclists.s.section = sectp;
fd361982 1875 this->loclists.size = bfd_section_size (sectp);
43988095 1876 }
330cdd98 1877 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1878 {
330cdd98 1879 this->macinfo.s.section = sectp;
fd361982 1880 this->macinfo.size = bfd_section_size (sectp);
c906108c 1881 }
330cdd98 1882 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1883 {
330cdd98 1884 this->macro.s.section = sectp;
fd361982 1885 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1886 }
330cdd98 1887 else if (section_is_p (sectp->name, &names.str))
c906108c 1888 {
330cdd98 1889 this->str.s.section = sectp;
fd361982 1890 this->str.size = bfd_section_size (sectp);
c906108c 1891 }
18a8505e
AT
1892 else if (section_is_p (sectp->name, &names.str_offsets))
1893 {
1894 this->str_offsets.s.section = sectp;
1895 this->str_offsets.size = bfd_section_size (sectp);
1896 }
330cdd98 1897 else if (section_is_p (sectp->name, &names.line_str))
43988095 1898 {
330cdd98 1899 this->line_str.s.section = sectp;
fd361982 1900 this->line_str.size = bfd_section_size (sectp);
43988095 1901 }
330cdd98 1902 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1903 {
330cdd98 1904 this->addr.s.section = sectp;
fd361982 1905 this->addr.size = bfd_section_size (sectp);
3019eac3 1906 }
330cdd98 1907 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1908 {
330cdd98 1909 this->frame.s.section = sectp;
fd361982 1910 this->frame.size = bfd_section_size (sectp);
b6af0555 1911 }
330cdd98 1912 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1913 {
330cdd98 1914 this->eh_frame.s.section = sectp;
fd361982 1915 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1916 }
330cdd98 1917 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1918 {
330cdd98 1919 this->ranges.s.section = sectp;
fd361982 1920 this->ranges.size = bfd_section_size (sectp);
af34e669 1921 }
330cdd98 1922 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1923 {
330cdd98 1924 this->rnglists.s.section = sectp;
fd361982 1925 this->rnglists.size = bfd_section_size (sectp);
43988095 1926 }
330cdd98 1927 else if (section_is_p (sectp->name, &names.types))
348e048f 1928 {
8b70b953
TT
1929 struct dwarf2_section_info type_section;
1930
1931 memset (&type_section, 0, sizeof (type_section));
049412e3 1932 type_section.s.section = sectp;
fd361982 1933 type_section.size = bfd_section_size (sectp);
8b70b953 1934
fd5866f6 1935 this->types.push_back (type_section);
348e048f 1936 }
330cdd98 1937 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1938 {
330cdd98 1939 this->gdb_index.s.section = sectp;
fd361982 1940 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1941 }
927aa2e7
JK
1942 else if (section_is_p (sectp->name, &names.debug_names))
1943 {
1944 this->debug_names.s.section = sectp;
fd361982 1945 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1946 }
1947 else if (section_is_p (sectp->name, &names.debug_aranges))
1948 {
1949 this->debug_aranges.s.section = sectp;
fd361982 1950 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1951 }
dce234bc 1952
fd361982
AM
1953 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1954 && bfd_section_vma (sectp) == 0)
330cdd98 1955 this->has_section_at_zero = true;
c906108c
SS
1956}
1957
dce234bc 1958/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1959 SECTION_NAME. */
af34e669 1960
dce234bc 1961void
3017a003
TG
1962dwarf2_get_section_info (struct objfile *objfile,
1963 enum dwarf2_section_enum sect,
d521ce57 1964 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1965 bfd_size_type *sizep)
1966{
5bfd760d 1967 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1968 struct dwarf2_section_info *info;
a3b2a86b
TT
1969
1970 /* We may see an objfile without any DWARF, in which case we just
1971 return nothing. */
1972 if (data == NULL)
1973 {
1974 *sectp = NULL;
1975 *bufp = NULL;
1976 *sizep = 0;
1977 return;
1978 }
3017a003
TG
1979 switch (sect)
1980 {
1981 case DWARF2_DEBUG_FRAME:
1982 info = &data->frame;
1983 break;
1984 case DWARF2_EH_FRAME:
1985 info = &data->eh_frame;
1986 break;
1987 default:
1988 gdb_assert_not_reached ("unexpected section");
1989 }
dce234bc 1990
96b79293 1991 info->read (objfile);
dce234bc 1992
96b79293 1993 *sectp = info->get_bfd_section ();
dce234bc
PP
1994 *bufp = info->buffer;
1995 *sizep = info->size;
1996}
1997
36586728
TT
1998/* A helper function to find the sections for a .dwz file. */
1999
2000static void
2001locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2002{
9a3c8263 2003 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2004
2005 /* Note that we only support the standard ELF names, because .dwz
2006 is ELF-only (at the time of writing). */
2007 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2008 {
049412e3 2009 dwz_file->abbrev.s.section = sectp;
fd361982 2010 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2013 {
049412e3 2014 dwz_file->info.s.section = sectp;
fd361982 2015 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2018 {
049412e3 2019 dwz_file->str.s.section = sectp;
fd361982 2020 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2023 {
049412e3 2024 dwz_file->line.s.section = sectp;
fd361982 2025 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2028 {
049412e3 2029 dwz_file->macro.s.section = sectp;
fd361982 2030 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2031 }
2ec9a5e0
TT
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2033 {
049412e3 2034 dwz_file->gdb_index.s.section = sectp;
fd361982 2035 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2036 }
927aa2e7
JK
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2038 {
2039 dwz_file->debug_names.s.section = sectp;
fd361982 2040 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2041 }
36586728
TT
2042}
2043
c4973306 2044/* See dwarf2read.h. */
36586728 2045
c4973306 2046struct dwz_file *
ed2dc618 2047dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2048{
36586728 2049 const char *filename;
acd13123 2050 bfd_size_type buildid_len_arg;
dc294be5
TT
2051 size_t buildid_len;
2052 bfd_byte *buildid;
36586728
TT
2053
2054 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2055 return dwarf2_per_objfile->dwz_file.get ();
36586728 2056
4db1a1dc 2057 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2058 gdb::unique_xmalloc_ptr<char> data
2059 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2060 &buildid_len_arg, &buildid));
4db1a1dc
TT
2061 if (data == NULL)
2062 {
2063 if (bfd_get_error () == bfd_error_no_error)
2064 return NULL;
2065 error (_("could not read '.gnu_debugaltlink' section: %s"),
2066 bfd_errmsg (bfd_get_error ()));
2067 }
791afaa2
TT
2068
2069 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2070
acd13123
TT
2071 buildid_len = (size_t) buildid_len_arg;
2072
791afaa2 2073 filename = data.get ();
d721ba37
PA
2074
2075 std::string abs_storage;
36586728
TT
2076 if (!IS_ABSOLUTE_PATH (filename))
2077 {
14278e1f
TT
2078 gdb::unique_xmalloc_ptr<char> abs
2079 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2080
14278e1f 2081 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2082 filename = abs_storage.c_str ();
36586728
TT
2083 }
2084
dc294be5
TT
2085 /* First try the file name given in the section. If that doesn't
2086 work, try to use the build-id instead. */
192b62ce 2087 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2088 if (dwz_bfd != NULL)
36586728 2089 {
192b62ce 2090 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2091 dwz_bfd.reset (nullptr);
36586728
TT
2092 }
2093
dc294be5
TT
2094 if (dwz_bfd == NULL)
2095 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2096
0d79cdc4
AM
2097 if (dwz_bfd == nullptr)
2098 {
2099 gdb::unique_xmalloc_ptr<char> alt_filename;
2100 const char *origname = dwarf2_per_objfile->objfile->original_name;
2101
2102 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2103 buildid_len,
2104 origname,
2105 &alt_filename));
2106
2107 if (fd.get () >= 0)
2108 {
2109 /* File successfully retrieved from server. */
2110 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2111
2112 if (dwz_bfd == nullptr)
2113 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2114 alt_filename.get ());
2115 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2116 dwz_bfd.reset (nullptr);
2117 }
2118 }
2119
dc294be5
TT
2120 if (dwz_bfd == NULL)
2121 error (_("could not find '.gnu_debugaltlink' file for %s"),
2122 objfile_name (dwarf2_per_objfile->objfile));
2123
7ff8cb8c
TT
2124 std::unique_ptr<struct dwz_file> result
2125 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2126
7ff8cb8c
TT
2127 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2128 result.get ());
36586728 2129
7ff8cb8c
TT
2130 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2131 result->dwz_bfd.get ());
2132 dwarf2_per_objfile->dwz_file = std::move (result);
2133 return dwarf2_per_objfile->dwz_file.get ();
36586728 2134}
9291a0cd 2135\f
7b9f3c50
DE
2136/* DWARF quick_symbols_functions support. */
2137
2138/* TUs can share .debug_line entries, and there can be a lot more TUs than
2139 unique line tables, so we maintain a separate table of all .debug_line
2140 derived entries to support the sharing.
2141 All the quick functions need is the list of file names. We discard the
2142 line_header when we're done and don't need to record it here. */
2143struct quick_file_names
2144{
094b34ac
DE
2145 /* The data used to construct the hash key. */
2146 struct stmt_list_hash hash;
7b9f3c50
DE
2147
2148 /* The number of entries in file_names, real_names. */
2149 unsigned int num_file_names;
2150
2151 /* The file names from the line table, after being run through
2152 file_full_name. */
2153 const char **file_names;
2154
2155 /* The file names from the line table after being run through
2156 gdb_realpath. These are computed lazily. */
2157 const char **real_names;
2158};
2159
2160/* When using the index (and thus not using psymtabs), each CU has an
2161 object of this type. This is used to hold information needed by
2162 the various "quick" methods. */
2163struct dwarf2_per_cu_quick_data
2164{
2165 /* The file table. This can be NULL if there was no file table
2166 or it's currently not read in.
2167 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2168 struct quick_file_names *file_names;
2169
2170 /* The corresponding symbol table. This is NULL if symbols for this
2171 CU have not yet been read. */
43f3e411 2172 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2173
2174 /* A temporary mark bit used when iterating over all CUs in
2175 expand_symtabs_matching. */
2176 unsigned int mark : 1;
2177
2178 /* True if we've tried to read the file table and found there isn't one.
2179 There will be no point in trying to read it again next time. */
2180 unsigned int no_file_data : 1;
2181};
2182
094b34ac
DE
2183/* Utility hash function for a stmt_list_hash. */
2184
2185static hashval_t
2186hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2187{
2188 hashval_t v = 0;
2189
2190 if (stmt_list_hash->dwo_unit != NULL)
2191 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2192 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2193 return v;
2194}
2195
2196/* Utility equality function for a stmt_list_hash. */
2197
2198static int
2199eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2200 const struct stmt_list_hash *rhs)
2201{
2202 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2203 return 0;
2204 if (lhs->dwo_unit != NULL
2205 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2206 return 0;
2207
9c541725 2208 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2209}
2210
7b9f3c50
DE
2211/* Hash function for a quick_file_names. */
2212
2213static hashval_t
2214hash_file_name_entry (const void *e)
2215{
9a3c8263
SM
2216 const struct quick_file_names *file_data
2217 = (const struct quick_file_names *) e;
7b9f3c50 2218
094b34ac 2219 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2220}
2221
2222/* Equality function for a quick_file_names. */
2223
2224static int
2225eq_file_name_entry (const void *a, const void *b)
2226{
9a3c8263
SM
2227 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2228 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2229
094b34ac 2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2231}
2232
2233/* Delete function for a quick_file_names. */
2234
2235static void
2236delete_file_name_entry (void *e)
2237{
9a3c8263 2238 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250}
2251
2252/* Create a quick_file_names hash table. */
2253
5895093f 2254static htab_up
7b9f3c50
DE
2255create_quick_file_names_table (unsigned int nr_initial_entries)
2256{
5895093f
TT
2257 return htab_up (htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2260}
9291a0cd 2261
918dd910
JK
2262/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266static void
58f0c718 2267load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2268{
3019eac3 2269 if (per_cu->is_debug_types)
e5fe5e75 2270 load_full_type_unit (per_cu);
918dd910 2271 else
58f0c718 2272 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2273
cc12ce38
DE
2274 if (per_cu->cu == NULL)
2275 return; /* Dummy CU. */
2dc860c0
DE
2276
2277 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2278}
2279
a0f42c21 2280/* Read in the symbols for PER_CU. */
2fdf6df6 2281
9291a0cd 2282static void
58f0c718 2283dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2284{
ed2dc618 2285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2286
f4dc4d17
DE
2287 /* Skip type_unit_groups, reading the type units they contain
2288 is handled elsewhere. */
197400e8 2289 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2290 return;
2291
b303c6f6
AB
2292 /* The destructor of dwarf2_queue_guard frees any entries left on
2293 the queue. After this point we're guaranteed to leave this function
2294 with the dwarf queue empty. */
39856def 2295 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2296
95554aad 2297 if (dwarf2_per_objfile->using_index
43f3e411 2298 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2299 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2300 {
2301 queue_comp_unit (per_cu, language_minimal);
58f0c718 2302 load_cu (per_cu, skip_partial);
89e63ee4
DE
2303
2304 /* If we just loaded a CU from a DWO, and we're working with an index
2305 that may badly handle TUs, load all the TUs in that DWO as well.
2306 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2307 if (!per_cu->is_debug_types
cc12ce38 2308 && per_cu->cu != NULL
89e63ee4
DE
2309 && per_cu->cu->dwo_unit != NULL
2310 && dwarf2_per_objfile->index_table != NULL
2311 && dwarf2_per_objfile->index_table->version <= 7
2312 /* DWP files aren't supported yet. */
ed2dc618 2313 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2314 queue_and_load_all_dwo_tus (per_cu);
95554aad 2315 }
9291a0cd 2316
ed2dc618 2317 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2318
2319 /* Age the cache, releasing compilation units that have not
2320 been used recently. */
ed2dc618 2321 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2322}
2323
2324/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2325 the objfile from which this CU came. Returns the resulting symbol
2326 table. */
2fdf6df6 2327
43f3e411 2328static struct compunit_symtab *
58f0c718 2329dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2330{
ed2dc618
SM
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
95554aad 2333 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2334 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2335 {
11ed8cad 2336 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2337 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2338 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2339 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2340 }
f194fefb 2341
43f3e411 2342 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2343}
2344
ff4c9fec 2345/* See declaration. */
f4dc4d17 2346
ff4c9fec
SM
2347dwarf2_per_cu_data *
2348dwarf2_per_objfile::get_cutu (int index)
2349{
b76e467d 2350 if (index >= this->all_comp_units.size ())
ff4c9fec 2351 {
b76e467d 2352 index -= this->all_comp_units.size ();
b2bdb8cf 2353 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2354 return &this->all_type_units[index]->per_cu;
2355 }
f4dc4d17 2356
ff4c9fec
SM
2357 return this->all_comp_units[index];
2358}
f4dc4d17 2359
ff4c9fec 2360/* See declaration. */
2fdf6df6 2361
ff4c9fec
SM
2362dwarf2_per_cu_data *
2363dwarf2_per_objfile::get_cu (int index)
1fd400ff 2364{
b76e467d 2365 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2366
ff4c9fec 2367 return this->all_comp_units[index];
f4dc4d17
DE
2368}
2369
ff4c9fec 2370/* See declaration. */
f4dc4d17 2371
ff4c9fec
SM
2372signatured_type *
2373dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2374{
b2bdb8cf 2375 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2376
ff4c9fec 2377 return this->all_type_units[index];
1fd400ff
TT
2378}
2379
4b514bc8
JK
2380/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2381 objfile_obstack, and constructed with the specified field
2382 values. */
2383
2384static dwarf2_per_cu_data *
ed2dc618 2385create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 sect_offset sect_off, ULONGEST length)
2389{
ed2dc618 2390 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2391 dwarf2_per_cu_data *the_cu
2392 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_data);
2394 the_cu->sect_off = sect_off;
2395 the_cu->length = length;
e3b94546 2396 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2397 the_cu->section = section;
2398 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_quick_data);
2400 the_cu->is_dwz = is_dwz;
2401 return the_cu;
2402}
2403
2ec9a5e0
TT
2404/* A helper for create_cus_from_index that handles a given list of
2405 CUs. */
2fdf6df6 2406
74a0d9f6 2407static void
12359b5e 2408create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2409 const gdb_byte *cu_list, offset_type n_elements,
2410 struct dwarf2_section_info *section,
b76e467d 2411 int is_dwz)
9291a0cd 2412{
12359b5e 2413 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2414 {
74a0d9f6 2415 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2416
2417 sect_offset sect_off
2418 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2419 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2420 cu_list += 2 * 8;
2421
b76e467d 2422 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2423 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2424 sect_off, length);
b76e467d 2425 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2426 }
9291a0cd
TT
2427}
2428
2ec9a5e0 2429/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2430 the CU objects for this objfile. */
2ec9a5e0 2431
74a0d9f6 2432static void
12359b5e 2433create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436{
b76e467d
SM
2437 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2438 dwarf2_per_objfile->all_comp_units.reserve
2439 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2440
12359b5e 2441 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2442 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2443
2444 if (dwz_elements == 0)
74a0d9f6 2445 return;
2ec9a5e0 2446
12359b5e
SM
2447 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2448 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2449 &dwz->info, 1);
2ec9a5e0
TT
2450}
2451
1fd400ff 2452/* Create the signatured type hash table from the index. */
673bfd45 2453
74a0d9f6 2454static void
12359b5e
SM
2455create_signatured_type_table_from_index
2456 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 struct dwarf2_section_info *section,
2458 const gdb_byte *bytes,
2459 offset_type elements)
1fd400ff 2460{
12359b5e 2461 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2462
b2bdb8cf
SM
2463 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2464 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2465
298e9637 2466 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2467
12359b5e 2468 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2469 {
52dc124a 2470 struct signatured_type *sig_type;
9c541725 2471 ULONGEST signature;
1fd400ff 2472 void **slot;
9c541725 2473 cu_offset type_offset_in_tu;
1fd400ff 2474
74a0d9f6 2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2476 sect_offset sect_off
2477 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu
2479 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2480 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2481 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 bytes += 3 * 8;
2483
52dc124a 2484 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2485 struct signatured_type);
52dc124a 2486 sig_type->signature = signature;
9c541725 2487 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2488 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2489 sig_type->per_cu.section = section;
9c541725 2490 sig_type->per_cu.sect_off = sect_off;
e3b94546 2491 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2492 sig_type->per_cu.v.quick
1fd400ff
TT
2493 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2494 struct dwarf2_per_cu_quick_data);
2495
b0b6a987 2496 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2497 *slot = sig_type;
1fd400ff 2498
b2bdb8cf 2499 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2500 }
2501
b0b6a987 2502 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2503}
2504
927aa2e7
JK
2505/* Create the signatured type hash table from .debug_names. */
2506
2507static void
2508create_signatured_type_table_from_debug_names
ed2dc618 2509 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2510 const mapped_debug_names &map,
2511 struct dwarf2_section_info *section,
2512 struct dwarf2_section_info *abbrev_section)
2513{
ed2dc618
SM
2514 struct objfile *objfile = dwarf2_per_objfile->objfile;
2515
96b79293
TT
2516 section->read (objfile);
2517 abbrev_section->read (objfile);
927aa2e7 2518
b2bdb8cf
SM
2519 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2520 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2521
298e9637 2522 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2523
2524 for (uint32_t i = 0; i < map.tu_count; ++i)
2525 {
2526 struct signatured_type *sig_type;
927aa2e7 2527 void **slot;
927aa2e7
JK
2528
2529 sect_offset sect_off
2530 = (sect_offset) (extract_unsigned_integer
2531 (map.tu_table_reordered + i * map.offset_size,
2532 map.offset_size,
2533 map.dwarf5_byte_order));
2534
2535 comp_unit_head cu_header;
ed2dc618
SM
2536 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2537 abbrev_section,
927aa2e7
JK
2538 section->buffer + to_underlying (sect_off),
2539 rcuh_kind::TYPE);
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = cu_header.signature;
2544 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
e3b94546 2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
b0b6a987 2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2554 *slot = sig_type;
2555
b2bdb8cf 2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2557 }
2558
b0b6a987 2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2560}
2561
9291a0cd
TT
2562/* Read the address map data from the mapped index, and use it to
2563 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2564
9291a0cd 2565static void
ed2dc618
SM
2566create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 struct mapped_index *index)
9291a0cd 2568{
ed2dc618 2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2570 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2571 const gdb_byte *iter, *end;
9291a0cd 2572 struct addrmap *mutable_map;
9291a0cd
TT
2573 CORE_ADDR baseaddr;
2574
8268c778
PA
2575 auto_obstack temp_obstack;
2576
9291a0cd
TT
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
f00a2de2
PA
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
9291a0cd 2581
b3b3bada 2582 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
f652bce2 2593
24a55014 2594 if (lo > hi)
f652bce2 2595 {
b98664d3 2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2597 hex_string (lo), hex_string (hi));
24a55014 2598 continue;
f652bce2 2599 }
24a55014 2600
b76e467d 2601 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2602 {
b98664d3 2603 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2604 (unsigned) cu_index);
24a55014 2605 continue;
f652bce2 2606 }
24a55014 2607
79748972
TT
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2610 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2611 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2612 }
2613
d320c2b5 2614 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2615 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2616}
2617
927aa2e7
JK
2618/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2619 populate the objfile's psymtabs_addrmap. */
2620
2621static void
ed2dc618 2622create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2623 struct dwarf2_section_info *section)
2624{
ed2dc618 2625 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2626 bfd *abfd = objfile->obfd;
2627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2628 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2629
2630 auto_obstack temp_obstack;
2631 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
b76e467d 2637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2638 {
927aa2e7
JK
2639 const auto insertpair
2640 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2641 if (!insertpair.second)
2642 {
2643 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2644 "debug_info_offset %s, ignoring .debug_aranges."),
2645 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2646 return;
2647 }
2648 }
2649
96b79293 2650 section->read (objfile);
927aa2e7
JK
2651
2652 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2653
2654 const gdb_byte *addr = section->buffer;
2655
2656 while (addr < section->buffer + section->size)
2657 {
2658 const gdb_byte *const entry_addr = addr;
2659 unsigned int bytes_read;
2660
2661 const LONGEST entry_length = read_initial_length (abfd, addr,
2662 &bytes_read);
2663 addr += bytes_read;
2664
2665 const gdb_byte *const entry_end = addr + entry_length;
2666 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2667 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2668 if (addr + entry_length > section->buffer + section->size)
2669 {
47e3f474 2670 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2671 "length %s exceeds section length %s, "
2672 "ignoring .debug_aranges."),
47e3f474
TV
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
927aa2e7
JK
2675 plongest (bytes_read + entry_length),
2676 pulongest (section->size));
2677 return;
2678 }
2679
2680 /* The version number. */
2681 const uint16_t version = read_2_bytes (abfd, addr);
2682 addr += 2;
2683 if (version != 2)
2684 {
47e3f474 2685 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2686 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2689 return;
2690 }
2691
2692 const uint64_t debug_info_offset
2693 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2694 addr += offset_size;
2695 const auto per_cu_it
2696 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2697 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2698 {
47e3f474 2699 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2700 "debug_info_offset %s does not exists, "
2701 "ignoring .debug_aranges."),
47e3f474
TV
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
927aa2e7
JK
2704 pulongest (debug_info_offset));
2705 return;
2706 }
2707 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2708
2709 const uint8_t address_size = *addr++;
2710 if (address_size < 1 || address_size > 8)
2711 {
47e3f474 2712 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2713 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2716 return;
2717 }
2718
2719 const uint8_t segment_selector_size = *addr++;
2720 if (segment_selector_size != 0)
2721 {
47e3f474 2722 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2723 "segment_selector_size %u is not supported, "
2724 "ignoring .debug_aranges."),
47e3f474
TV
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
927aa2e7
JK
2727 segment_selector_size);
2728 return;
2729 }
2730
2731 /* Must pad to an alignment boundary that is twice the address
2732 size. It is undocumented by the DWARF standard but GCC does
2733 use it. */
2734 for (size_t padding = ((-(addr - section->buffer))
2735 & (2 * address_size - 1));
2736 padding > 0; padding--)
2737 if (*addr++ != 0)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer));
927aa2e7
JK
2743 return;
2744 }
2745
2746 for (;;)
2747 {
2748 if (addr + 2 * address_size > entry_end)
2749 {
47e3f474 2750 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2751 "address list is not properly terminated, "
2752 "ignoring .debug_aranges."),
47e3f474
TV
2753 objfile_name (objfile),
2754 plongest (entry_addr - section->buffer));
927aa2e7
JK
2755 return;
2756 }
2757 ULONGEST start = extract_unsigned_integer (addr, address_size,
2758 dwarf5_byte_order);
2759 addr += address_size;
2760 ULONGEST length = extract_unsigned_integer (addr, address_size,
2761 dwarf5_byte_order);
2762 addr += address_size;
2763 if (start == 0 && length == 0)
2764 break;
2765 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2766 {
2767 /* Symbol was eliminated due to a COMDAT group. */
2768 continue;
2769 }
2770 ULONGEST end = start + length;
79748972
TT
2771 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2772 - baseaddr);
2773 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2774 - baseaddr);
927aa2e7
JK
2775 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2776 }
2777 }
2778
d320c2b5 2779 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2780 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2781}
2782
9291a0cd
TT
2783/* Find a slot in the mapped index INDEX for the object named NAME.
2784 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2785 constant pool and return true. If NAME cannot be found, return
2786 false. */
2fdf6df6 2787
109483d9 2788static bool
9291a0cd
TT
2789find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2790 offset_type **vec_out)
2791{
0cf03b49 2792 offset_type hash;
9291a0cd 2793 offset_type slot, step;
559a7a62 2794 int (*cmp) (const char *, const char *);
9291a0cd 2795
791afaa2 2796 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2797 if (current_language->la_language == language_cplus
45280282
IB
2798 || current_language->la_language == language_fortran
2799 || current_language->la_language == language_d)
0cf03b49
JK
2800 {
2801 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2802 not contain any. */
a8719064 2803
72998fb3 2804 if (strchr (name, '(') != NULL)
0cf03b49 2805 {
109483d9 2806 without_params = cp_remove_params (name);
0cf03b49 2807
72998fb3 2808 if (without_params != NULL)
791afaa2 2809 name = without_params.get ();
0cf03b49
JK
2810 }
2811 }
2812
559a7a62 2813 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2814 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2815 simulate our NAME being searched is also lowercased. */
2816 hash = mapped_index_string_hash ((index->version == 4
2817 && case_sensitivity == case_sensitive_off
2818 ? 5 : index->version),
2819 name);
2820
f00a2de2
PA
2821 slot = hash & (index->symbol_table.size () - 1);
2822 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2823 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2824
2825 for (;;)
2826 {
9291a0cd 2827 const char *str;
f00a2de2
PA
2828
2829 const auto &bucket = index->symbol_table[slot];
2830 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2831 return false;
9291a0cd 2832
f00a2de2 2833 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2834 if (!cmp (name, str))
9291a0cd
TT
2835 {
2836 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2837 + MAYBE_SWAP (bucket.vec));
109483d9 2838 return true;
9291a0cd
TT
2839 }
2840
f00a2de2 2841 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2842 }
2843}
2844
4485a1c1
SM
2845/* A helper function that reads the .gdb_index from BUFFER and fills
2846 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2847 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2848 ok to use deprecated sections.
2849
2850 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2851 out parameters that are filled in with information about the CU and
2852 TU lists in the section.
2853
4485a1c1 2854 Returns true if all went well, false otherwise. */
2fdf6df6 2855
d33bc52e 2856static bool
4485a1c1
SM
2857read_gdb_index_from_buffer (struct objfile *objfile,
2858 const char *filename,
2859 bool deprecated_ok,
2860 gdb::array_view<const gdb_byte> buffer,
2861 struct mapped_index *map,
2862 const gdb_byte **cu_list,
2863 offset_type *cu_list_elements,
2864 const gdb_byte **types_list,
2865 offset_type *types_list_elements)
2866{
2867 const gdb_byte *addr = &buffer[0];
82430852 2868
9291a0cd 2869 /* Version check. */
4485a1c1 2870 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2871 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2872 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2873 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2874 indices. */
831adc1f 2875 if (version < 4)
481860b3
GB
2876 {
2877 static int warning_printed = 0;
2878 if (!warning_printed)
2879 {
2880 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2881 filename);
481860b3
GB
2882 warning_printed = 1;
2883 }
2884 return 0;
2885 }
2886 /* Index version 4 uses a different hash function than index version
2887 5 and later.
2888
2889 Versions earlier than 6 did not emit psymbols for inlined
2890 functions. Using these files will cause GDB not to be able to
2891 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2892 indices unless the user has done
2893 "set use-deprecated-index-sections on". */
2ec9a5e0 2894 if (version < 6 && !deprecated_ok)
481860b3
GB
2895 {
2896 static int warning_printed = 0;
2897 if (!warning_printed)
2898 {
e615022a
DE
2899 warning (_("\
2900Skipping deprecated .gdb_index section in %s.\n\
2901Do \"set use-deprecated-index-sections on\" before the file is read\n\
2902to use the section anyway."),
2ec9a5e0 2903 filename);
481860b3
GB
2904 warning_printed = 1;
2905 }
2906 return 0;
2907 }
796a7ff8 2908 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2909 of the TU (for symbols coming from TUs),
2910 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2911 Plus gold-generated indices can have duplicate entries for global symbols,
2912 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2913 These are just performance bugs, and we can't distinguish gdb-generated
2914 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2915
481860b3 2916 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2917 longer backward compatible. */
796a7ff8 2918 if (version > 8)
594e8718 2919 return 0;
9291a0cd 2920
559a7a62 2921 map->version = version;
9291a0cd 2922
4485a1c1 2923 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2924
4485a1c1 2925 int i = 0;
2ec9a5e0
TT
2926 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2927 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2928 / 8);
1fd400ff
TT
2929 ++i;
2930
2ec9a5e0
TT
2931 *types_list = addr + MAYBE_SWAP (metadata[i]);
2932 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2933 - MAYBE_SWAP (metadata[i]))
2934 / 8);
987d643c 2935 ++i;
1fd400ff 2936
f00a2de2
PA
2937 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2938 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2939 map->address_table
2940 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2941 ++i;
2942
f00a2de2
PA
2943 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2944 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2945 map->symbol_table
2946 = gdb::array_view<mapped_index::symbol_table_slot>
2947 ((mapped_index::symbol_table_slot *) symbol_table,
2948 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2949
f00a2de2 2950 ++i;
f9d83a0b 2951 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2952
2ec9a5e0
TT
2953 return 1;
2954}
2955
4485a1c1
SM
2956/* Callback types for dwarf2_read_gdb_index. */
2957
2958typedef gdb::function_view
2959 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2960 get_gdb_index_contents_ftype;
2961typedef gdb::function_view
2962 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2963 get_gdb_index_contents_dwz_ftype;
2964
927aa2e7 2965/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2966 elements of all the CUs and return 1. Otherwise, return 0. */
2967
2968static int
4485a1c1
SM
2969dwarf2_read_gdb_index
2970 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2971 get_gdb_index_contents_ftype get_gdb_index_contents,
2972 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2973{
2ec9a5e0
TT
2974 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2975 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2976 struct dwz_file *dwz;
12359b5e 2977 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 2978
4485a1c1
SM
2979 gdb::array_view<const gdb_byte> main_index_contents
2980 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2981
2982 if (main_index_contents.empty ())
2983 return 0;
2984
3063847f 2985 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
2986 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2987 use_deprecated_index_sections,
2988 main_index_contents, map.get (), &cu_list,
2989 &cu_list_elements, &types_list,
2990 &types_list_elements))
2ec9a5e0
TT
2991 return 0;
2992
0fefef59 2993 /* Don't use the index if it's empty. */
3063847f 2994 if (map->symbol_table.empty ())
0fefef59
DE
2995 return 0;
2996
2ec9a5e0
TT
2997 /* If there is a .dwz file, read it so we can get its CU list as
2998 well. */
ed2dc618 2999 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3000 if (dwz != NULL)
2ec9a5e0 3001 {
2ec9a5e0
TT
3002 struct mapped_index dwz_map;
3003 const gdb_byte *dwz_types_ignore;
3004 offset_type dwz_types_elements_ignore;
3005
4485a1c1
SM
3006 gdb::array_view<const gdb_byte> dwz_index_content
3007 = get_gdb_index_contents_dwz (objfile, dwz);
3008
3009 if (dwz_index_content.empty ())
3010 return 0;
3011
3012 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3013 bfd_get_filename (dwz->dwz_bfd.get ()),
3014 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3015 &dwz_list, &dwz_list_elements,
3016 &dwz_types_ignore,
3017 &dwz_types_elements_ignore))
2ec9a5e0
TT
3018 {
3019 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3020 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3021 return 0;
3022 }
3023 }
3024
12359b5e
SM
3025 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3026 dwz_list, dwz_list_elements);
1fd400ff 3027
8b70b953
TT
3028 if (types_list_elements)
3029 {
8b70b953
TT
3030 /* We can only handle a single .debug_types when we have an
3031 index. */
fd5866f6 3032 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3033 return 0;
3034
fd5866f6 3035 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3036
12359b5e
SM
3037 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3038 types_list, types_list_elements);
8b70b953 3039 }
9291a0cd 3040
3063847f 3041 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3042
3063847f 3043 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3044 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3045 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3046 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3047
3048 return 1;
3049}
3050
dee91e82 3051/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3052
dee91e82
DE
3053static void
3054dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3055 const gdb_byte *info_ptr,
3e225074 3056 struct die_info *comp_unit_die)
9291a0cd 3057{
dee91e82 3058 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3059 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3060 struct dwarf2_per_objfile *dwarf2_per_objfile
3061 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3063 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3064 struct attribute *attr;
7b9f3c50
DE
3065 void **slot;
3066 struct quick_file_names *qfn;
9291a0cd 3067
0186c6a7
DE
3068 gdb_assert (! this_cu->is_debug_types);
3069
07261596
TT
3070 /* Our callers never want to match partial units -- instead they
3071 will match the enclosing full CU. */
3072 if (comp_unit_die->tag == DW_TAG_partial_unit)
3073 {
3074 this_cu->v.quick->no_file_data = 1;
3075 return;
3076 }
3077
0186c6a7 3078 lh_cu = this_cu;
7b9f3c50 3079 slot = NULL;
dee91e82 3080
fff8551c 3081 line_header_up lh;
9c541725 3082 sect_offset line_offset {};
fff8551c 3083
dee91e82 3084 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3085 if (attr != nullptr)
9291a0cd 3086 {
7b9f3c50
DE
3087 struct quick_file_names find_entry;
3088
9c541725 3089 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3090
3091 /* We may have already read in this line header (TU line header sharing).
3092 If we have we're done. */
094b34ac 3093 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3094 find_entry.hash.line_sect_off = line_offset;
5895093f 3095 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3096 &find_entry, INSERT);
3097 if (*slot != NULL)
3098 {
9a3c8263 3099 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3100 return;
7b9f3c50
DE
3101 }
3102
3019eac3 3103 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3104 }
3105 if (lh == NULL)
3106 {
094b34ac 3107 lh_cu->v.quick->no_file_data = 1;
dee91e82 3108 return;
9291a0cd
TT
3109 }
3110
8d749320 3111 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3112 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3113 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3114 gdb_assert (slot != NULL);
3115 *slot = qfn;
9291a0cd 3116
d721ba37 3117 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3118
aa391654
TT
3119 int offset = 0;
3120 if (strcmp (fnd.name, "<unknown>") != 0)
3121 ++offset;
3122
7ba99d21 3123 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3124 qfn->file_names =
aa391654
TT
3125 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3126 if (offset != 0)
3127 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3128 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3129 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3130 fnd.comp_dir).release ();
7b9f3c50 3131 qfn->real_names = NULL;
9291a0cd 3132
094b34ac 3133 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3134}
3135
3136/* A helper for the "quick" functions which attempts to read the line
3137 table for THIS_CU. */
3138
3139static struct quick_file_names *
e4a48d9d 3140dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3141{
0186c6a7
DE
3142 /* This should never be called for TUs. */
3143 gdb_assert (! this_cu->is_debug_types);
3144 /* Nor type unit groups. */
197400e8 3145 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3146
dee91e82
DE
3147 if (this_cu->v.quick->file_names != NULL)
3148 return this_cu->v.quick->file_names;
3149 /* If we know there is no line data, no point in looking again. */
3150 if (this_cu->v.quick->no_file_data)
3151 return NULL;
3152
c0ab21c2
TT
3153 cutu_reader reader (this_cu);
3154 if (!reader.dummy_p)
3e225074 3155 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3156
3157 if (this_cu->v.quick->no_file_data)
3158 return NULL;
3159 return this_cu->v.quick->file_names;
9291a0cd
TT
3160}
3161
3162/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3163 real path for a given file name from the line table. */
2fdf6df6 3164
9291a0cd 3165static const char *
7b9f3c50
DE
3166dw2_get_real_path (struct objfile *objfile,
3167 struct quick_file_names *qfn, int index)
9291a0cd 3168{
7b9f3c50
DE
3169 if (qfn->real_names == NULL)
3170 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3171 qfn->num_file_names, const char *);
9291a0cd 3172
7b9f3c50 3173 if (qfn->real_names[index] == NULL)
14278e1f 3174 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3175
7b9f3c50 3176 return qfn->real_names[index];
9291a0cd
TT
3177}
3178
3179static struct symtab *
3180dw2_find_last_source_symtab (struct objfile *objfile)
3181{
ed2dc618
SM
3182 struct dwarf2_per_objfile *dwarf2_per_objfile
3183 = get_dwarf2_per_objfile (objfile);
b76e467d 3184 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3185 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3186
43f3e411
DE
3187 if (cust == NULL)
3188 return NULL;
ed2dc618 3189
43f3e411 3190 return compunit_primary_filetab (cust);
9291a0cd
TT
3191}
3192
7b9f3c50
DE
3193/* Traversal function for dw2_forget_cached_source_info. */
3194
3195static int
3196dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3197{
7b9f3c50 3198 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3199
7b9f3c50 3200 if (file_data->real_names)
9291a0cd 3201 {
7b9f3c50 3202 int i;
9291a0cd 3203
7b9f3c50 3204 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3205 {
7b9f3c50
DE
3206 xfree ((void*) file_data->real_names[i]);
3207 file_data->real_names[i] = NULL;
9291a0cd
TT
3208 }
3209 }
7b9f3c50
DE
3210
3211 return 1;
3212}
3213
3214static void
3215dw2_forget_cached_source_info (struct objfile *objfile)
3216{
ed2dc618
SM
3217 struct dwarf2_per_objfile *dwarf2_per_objfile
3218 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3219
5895093f 3220 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3221 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3222}
3223
f8eba3c6
TT
3224/* Helper function for dw2_map_symtabs_matching_filename that expands
3225 the symtabs and calls the iterator. */
3226
3227static int
3228dw2_map_expand_apply (struct objfile *objfile,
3229 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3230 const char *name, const char *real_path,
14bc53a8 3231 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3232{
43f3e411 3233 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3234
3235 /* Don't visit already-expanded CUs. */
43f3e411 3236 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3237 return 0;
3238
3239 /* This may expand more than one symtab, and we want to iterate over
3240 all of them. */
58f0c718 3241 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3242
14bc53a8
PA
3243 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3244 last_made, callback);
f8eba3c6
TT
3245}
3246
3247/* Implementation of the map_symtabs_matching_filename method. */
3248
14bc53a8
PA
3249static bool
3250dw2_map_symtabs_matching_filename
3251 (struct objfile *objfile, const char *name, const char *real_path,
3252 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3253{
c011a4f4 3254 const char *name_basename = lbasename (name);
ed2dc618
SM
3255 struct dwarf2_per_objfile *dwarf2_per_objfile
3256 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3257
848e3e78
DE
3258 /* The rule is CUs specify all the files, including those used by
3259 any TU, so there's no need to scan TUs here. */
f4dc4d17 3260
b76e467d 3261 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3262 {
3d7bb9d9 3263 /* We only need to look at symtabs not already expanded. */
43f3e411 3264 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3265 continue;
3266
b76e467d 3267 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3268 if (file_data == NULL)
9291a0cd
TT
3269 continue;
3270
b76e467d 3271 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3272 {
7b9f3c50 3273 const char *this_name = file_data->file_names[j];
da235a7c 3274 const char *this_real_name;
9291a0cd 3275
af529f8f 3276 if (compare_filenames_for_search (this_name, name))
9291a0cd 3277 {
f5b95b50 3278 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3279 callback))
3280 return true;
288e77a7 3281 continue;
4aac40c8 3282 }
9291a0cd 3283
c011a4f4
DE
3284 /* Before we invoke realpath, which can get expensive when many
3285 files are involved, do a quick comparison of the basenames. */
3286 if (! basenames_may_differ
3287 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3288 continue;
3289
da235a7c
JK
3290 this_real_name = dw2_get_real_path (objfile, file_data, j);
3291 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3292 {
da235a7c 3293 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3294 callback))
3295 return true;
288e77a7 3296 continue;
da235a7c 3297 }
9291a0cd 3298
da235a7c
JK
3299 if (real_path != NULL)
3300 {
af529f8f
JK
3301 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3302 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3303 if (this_real_name != NULL
af529f8f 3304 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3305 {
f5b95b50 3306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3307 callback))
3308 return true;
288e77a7 3309 continue;
9291a0cd
TT
3310 }
3311 }
3312 }
3313 }
3314
14bc53a8 3315 return false;
9291a0cd
TT
3316}
3317
da51c347
DE
3318/* Struct used to manage iterating over all CUs looking for a symbol. */
3319
3320struct dw2_symtab_iterator
9291a0cd 3321{
ed2dc618
SM
3322 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3323 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3324 /* If set, only look for symbols that match that block. Valid values are
3325 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3326 gdb::optional<block_enum> block_index;
da51c347
DE
3327 /* The kind of symbol we're looking for. */
3328 domain_enum domain;
3329 /* The list of CUs from the index entry of the symbol,
3330 or NULL if not found. */
3331 offset_type *vec;
3332 /* The next element in VEC to look at. */
3333 int next;
3334 /* The number of elements in VEC, or zero if there is no match. */
3335 int length;
8943b874
DE
3336 /* Have we seen a global version of the symbol?
3337 If so we can ignore all further global instances.
3338 This is to work around gold/15646, inefficient gold-generated
3339 indices. */
3340 int global_seen;
da51c347 3341};
9291a0cd 3342
2b79f376 3343/* Initialize the index symtab iterator ITER. */
2fdf6df6 3344
9291a0cd 3345static void
da51c347 3346dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3347 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3348 gdb::optional<block_enum> block_index,
da51c347
DE
3349 domain_enum domain,
3350 const char *name)
3351{
ed2dc618 3352 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3353 iter->block_index = block_index;
3354 iter->domain = domain;
3355 iter->next = 0;
8943b874 3356 iter->global_seen = 0;
da51c347 3357
3063847f 3358 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3359
3360 /* index is NULL if OBJF_READNOW. */
3361 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3362 iter->length = MAYBE_SWAP (*iter->vec);
3363 else
3364 {
3365 iter->vec = NULL;
3366 iter->length = 0;
3367 }
3368}
3369
3370/* Return the next matching CU or NULL if there are no more. */
3371
3372static struct dwarf2_per_cu_data *
3373dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3374{
ed2dc618
SM
3375 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3376
da51c347
DE
3377 for ( ; iter->next < iter->length; ++iter->next)
3378 {
3379 offset_type cu_index_and_attrs =
3380 MAYBE_SWAP (iter->vec[iter->next + 1]);
3381 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3382 gdb_index_symbol_kind symbol_kind =
3383 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3384 /* Only check the symbol attributes if they're present.
3385 Indices prior to version 7 don't record them,
3386 and indices >= 7 may elide them for certain symbols
3387 (gold does this). */
3388 int attrs_valid =
ed2dc618 3389 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3390 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3391
3190f0c6 3392 /* Don't crash on bad data. */
b76e467d 3393 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3394 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3395 {
b98664d3 3396 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3397 " [in module %s]"),
3398 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3399 continue;
3400 }
3401
ff4c9fec 3402 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3403
da51c347 3404 /* Skip if already read in. */
43f3e411 3405 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3406 continue;
3407
8943b874
DE
3408 /* Check static vs global. */
3409 if (attrs_valid)
3410 {
2b79f376
SM
3411 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3412
3413 if (iter->block_index.has_value ())
3414 {
3415 bool want_static = *iter->block_index == STATIC_BLOCK;
3416
3417 if (is_static != want_static)
3418 continue;
3419 }
3420
8943b874
DE
3421 /* Work around gold/15646. */
3422 if (!is_static && iter->global_seen)
3423 continue;
3424 if (!is_static)
3425 iter->global_seen = 1;
3426 }
da51c347
DE
3427
3428 /* Only check the symbol's kind if it has one. */
3429 if (attrs_valid)
3430 {
3431 switch (iter->domain)
3432 {
3433 case VAR_DOMAIN:
3434 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3435 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3436 /* Some types are also in VAR_DOMAIN. */
3437 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3438 continue;
3439 break;
3440 case STRUCT_DOMAIN:
3441 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3442 continue;
3443 break;
3444 case LABEL_DOMAIN:
3445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3446 continue;
3447 break;
59c35742
AB
3448 case MODULE_DOMAIN:
3449 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3450 continue;
3451 break;
da51c347
DE
3452 default:
3453 break;
3454 }
3455 }
3456
3457 ++iter->next;
3458 return per_cu;
3459 }
3460
3461 return NULL;
3462}
3463
43f3e411 3464static struct compunit_symtab *
c7f839cb 3465dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3466 const char *name, domain_enum domain)
9291a0cd 3467{
43f3e411 3468 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3469 struct dwarf2_per_objfile *dwarf2_per_objfile
3470 = get_dwarf2_per_objfile (objfile);
9291a0cd 3471
b5ec771e
PA
3472 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3473
ed2dc618
SM
3474 struct dw2_symtab_iterator iter;
3475 struct dwarf2_per_cu_data *per_cu;
da51c347 3476
2b79f376 3477 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3478
ed2dc618
SM
3479 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3480 {
3481 struct symbol *sym, *with_opaque = NULL;
58f0c718 3482 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3483 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3484 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3485
ed2dc618
SM
3486 sym = block_find_symbol (block, name, domain,
3487 block_find_non_opaque_type_preferred,
3488 &with_opaque);
b2e2f908 3489
ed2dc618
SM
3490 /* Some caution must be observed with overloaded functions
3491 and methods, since the index will not contain any overload
3492 information (but NAME might contain it). */
da51c347 3493
ed2dc618
SM
3494 if (sym != NULL
3495 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3496 return stab;
3497 if (with_opaque != NULL
3498 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3499 stab_best = stab;
da51c347 3500
ed2dc618 3501 /* Keep looking through other CUs. */
9291a0cd 3502 }
9291a0cd 3503
da51c347 3504 return stab_best;
9291a0cd
TT
3505}
3506
3507static void
3508dw2_print_stats (struct objfile *objfile)
3509{
ed2dc618
SM
3510 struct dwarf2_per_objfile *dwarf2_per_objfile
3511 = get_dwarf2_per_objfile (objfile);
b76e467d 3512 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3513 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3514 int count = 0;
9291a0cd 3515
ed2dc618 3516 for (int i = 0; i < total; ++i)
9291a0cd 3517 {
ff4c9fec 3518 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3519
43f3e411 3520 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3521 ++count;
3522 }
e4a48d9d 3523 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3524 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3525}
3526
779bd270
DE
3527/* This dumps minimal information about the index.
3528 It is called via "mt print objfiles".
3529 One use is to verify .gdb_index has been loaded by the
3530 gdb.dwarf2/gdb-index.exp testcase. */
3531
9291a0cd
TT
3532static void
3533dw2_dump (struct objfile *objfile)
3534{
ed2dc618
SM
3535 struct dwarf2_per_objfile *dwarf2_per_objfile
3536 = get_dwarf2_per_objfile (objfile);
3537
779bd270
DE
3538 gdb_assert (dwarf2_per_objfile->using_index);
3539 printf_filtered (".gdb_index:");
3540 if (dwarf2_per_objfile->index_table != NULL)
3541 {
3542 printf_filtered (" version %d\n",
3543 dwarf2_per_objfile->index_table->version);
3544 }
3545 else
3546 printf_filtered (" faked for \"readnow\"\n");
3547 printf_filtered ("\n");
9291a0cd
TT
3548}
3549
9291a0cd
TT
3550static void
3551dw2_expand_symtabs_for_function (struct objfile *objfile,
3552 const char *func_name)
3553{
ed2dc618
SM
3554 struct dwarf2_per_objfile *dwarf2_per_objfile
3555 = get_dwarf2_per_objfile (objfile);
da51c347 3556
ed2dc618
SM
3557 struct dw2_symtab_iterator iter;
3558 struct dwarf2_per_cu_data *per_cu;
da51c347 3559
2b79f376 3560 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3561
ed2dc618 3562 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3563 dw2_instantiate_symtab (per_cu, false);
da51c347 3564
9291a0cd
TT
3565}
3566
3567static void
3568dw2_expand_all_symtabs (struct objfile *objfile)
3569{
ed2dc618
SM
3570 struct dwarf2_per_objfile *dwarf2_per_objfile
3571 = get_dwarf2_per_objfile (objfile);
b76e467d 3572 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3573 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3574
ed2dc618 3575 for (int i = 0; i < total_units; ++i)
9291a0cd 3576 {
ff4c9fec 3577 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3578
58f0c718
TT
3579 /* We don't want to directly expand a partial CU, because if we
3580 read it with the wrong language, then assertion failures can
3581 be triggered later on. See PR symtab/23010. So, tell
3582 dw2_instantiate_symtab to skip partial CUs -- any important
3583 partial CU will be read via DW_TAG_imported_unit anyway. */
3584 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3585 }
3586}
3587
3588static void
652a8996
JK
3589dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3590 const char *fullname)
9291a0cd 3591{
ed2dc618
SM
3592 struct dwarf2_per_objfile *dwarf2_per_objfile
3593 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3594
3595 /* We don't need to consider type units here.
3596 This is only called for examining code, e.g. expand_line_sal.
3597 There can be an order of magnitude (or more) more type units
3598 than comp units, and we avoid them if we can. */
3599
b76e467d 3600 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3601 {
3d7bb9d9 3602 /* We only need to look at symtabs not already expanded. */
43f3e411 3603 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3604 continue;
3605
b76e467d 3606 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3607 if (file_data == NULL)
9291a0cd
TT
3608 continue;
3609
b76e467d 3610 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3611 {
652a8996
JK
3612 const char *this_fullname = file_data->file_names[j];
3613
3614 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3615 {
58f0c718 3616 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3617 break;
3618 }
3619 }
3620 }
3621}
3622
9291a0cd 3623static void
199b4314
TT
3624dw2_map_matching_symbols
3625 (struct objfile *objfile,
b054970d 3626 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3627 int global,
3628 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3629 symbol_compare_ftype *ordered_compare)
9291a0cd 3630{
1aa98955
TV
3631 /* Used for Ada. */
3632 struct dwarf2_per_objfile *dwarf2_per_objfile
3633 = get_dwarf2_per_objfile (objfile);
3634
3635 if (dwarf2_per_objfile->index_table != nullptr)
3636 {
3637 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3638 here though if the current language is Ada for a non-Ada objfile
3639 using GNU index. As Ada does not look for non-Ada symbols this
3640 function should just return. */
3641 return;
3642 }
3643
3644 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3645 inline psym_map_matching_symbols here, assuming all partial symtabs have
3646 been read in. */
3647 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3648
3649 for (compunit_symtab *cust : objfile->compunits ())
3650 {
3651 const struct block *block;
3652
3653 if (cust == NULL)
3654 continue;
3655 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3656 if (!iterate_over_symbols_terminated (block, name,
3657 domain, callback))
3658 return;
3659 }
9291a0cd
TT
3660}
3661
e1ef7d7a
PA
3662/* Starting from a search name, return the string that finds the upper
3663 bound of all strings that start with SEARCH_NAME in a sorted name
3664 list. Returns the empty string to indicate that the upper bound is
3665 the end of the list. */
3666
3667static std::string
3668make_sort_after_prefix_name (const char *search_name)
3669{
3670 /* When looking to complete "func", we find the upper bound of all
3671 symbols that start with "func" by looking for where we'd insert
3672 the closest string that would follow "func" in lexicographical
3673 order. Usually, that's "func"-with-last-character-incremented,
3674 i.e. "fund". Mind non-ASCII characters, though. Usually those
3675 will be UTF-8 multi-byte sequences, but we can't be certain.
3676 Especially mind the 0xff character, which is a valid character in
3677 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3678 rule out compilers allowing it in identifiers. Note that
3679 conveniently, strcmp/strcasecmp are specified to compare
3680 characters interpreted as unsigned char. So what we do is treat
3681 the whole string as a base 256 number composed of a sequence of
3682 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3683 to 0, and carries 1 to the following more-significant position.
3684 If the very first character in SEARCH_NAME ends up incremented
3685 and carries/overflows, then the upper bound is the end of the
3686 list. The string after the empty string is also the empty
3687 string.
3688
3689 Some examples of this operation:
3690
3691 SEARCH_NAME => "+1" RESULT
3692
3693 "abc" => "abd"
3694 "ab\xff" => "ac"
3695 "\xff" "a" "\xff" => "\xff" "b"
3696 "\xff" => ""
3697 "\xff\xff" => ""
3698 "" => ""
3699
3700 Then, with these symbols for example:
3701
3702 func
3703 func1
3704 fund
3705
3706 completing "func" looks for symbols between "func" and
3707 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3708 which finds "func" and "func1", but not "fund".
3709
3710 And with:
3711
3712 funcÿ (Latin1 'ÿ' [0xff])
3713 funcÿ1
3714 fund
3715
3716 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3717 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3718
3719 And with:
3720
3721 ÿÿ (Latin1 'ÿ' [0xff])
3722 ÿÿ1
3723
3724 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3725 the end of the list.
3726 */
3727 std::string after = search_name;
3728 while (!after.empty () && (unsigned char) after.back () == 0xff)
3729 after.pop_back ();
3730 if (!after.empty ())
3731 after.back () = (unsigned char) after.back () + 1;
3732 return after;
3733}
3734
5c58de74 3735/* See declaration. */
61d96d7e 3736
5c58de74
PA
3737std::pair<std::vector<name_component>::const_iterator,
3738 std::vector<name_component>::const_iterator>
44ed8f3e 3739mapped_index_base::find_name_components_bounds
3b00ef10 3740 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3741{
5c58de74
PA
3742 auto *name_cmp
3743 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3744
3b00ef10 3745 const char *lang_name
e0802d59 3746 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3747
3f563c84
PA
3748 /* Comparison function object for lower_bound that matches against a
3749 given symbol name. */
3750 auto lookup_compare_lower = [&] (const name_component &elem,
3751 const char *name)
3752 {
5c58de74 3753 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3754 const char *elem_name = elem_qualified + elem.name_offset;
3755 return name_cmp (elem_name, name) < 0;
3756 };
3757
3758 /* Comparison function object for upper_bound that matches against a
3759 given symbol name. */
3760 auto lookup_compare_upper = [&] (const char *name,
3761 const name_component &elem)
3762 {
5c58de74 3763 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3764 const char *elem_name = elem_qualified + elem.name_offset;
3765 return name_cmp (name, elem_name) < 0;
3766 };
3767
5c58de74
PA
3768 auto begin = this->name_components.begin ();
3769 auto end = this->name_components.end ();
3f563c84
PA
3770
3771 /* Find the lower bound. */
3772 auto lower = [&] ()
3773 {
3b00ef10 3774 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3775 return begin;
3776 else
3b00ef10 3777 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3778 } ();
3779
3780 /* Find the upper bound. */
3781 auto upper = [&] ()
3782 {
5c58de74 3783 if (lookup_name_without_params.completion_mode ())
3f563c84 3784 {
e1ef7d7a
PA
3785 /* In completion mode, we want UPPER to point past all
3786 symbols names that have the same prefix. I.e., with
3787 these symbols, and completing "func":
3788
3789 function << lower bound
3790 function1
3791 other_function << upper bound
3792
3793 We find the upper bound by looking for the insertion
3794 point of "func"-with-last-character-incremented,
3795 i.e. "fund". */
3b00ef10 3796 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3797 if (after.empty ())
3f563c84 3798 return end;
e6b2f5ef
PA
3799 return std::lower_bound (lower, end, after.c_str (),
3800 lookup_compare_lower);
3f563c84
PA
3801 }
3802 else
3b00ef10 3803 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3804 } ();
3805
5c58de74
PA
3806 return {lower, upper};
3807}
3808
3809/* See declaration. */
3810
3811void
44ed8f3e 3812mapped_index_base::build_name_components ()
5c58de74
PA
3813{
3814 if (!this->name_components.empty ())
3815 return;
3816
3817 this->name_components_casing = case_sensitivity;
3818 auto *name_cmp
3819 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3820
3821 /* The code below only knows how to break apart components of C++
3822 symbol names (and other languages that use '::' as
3b00ef10 3823 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3824 auto count = this->symbol_name_count ();
3825 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3826 {
44ed8f3e 3827 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3828 continue;
3829
3830 const char *name = this->symbol_name_at (idx);
3831
3832 /* Add each name component to the name component table. */
3833 unsigned int previous_len = 0;
3b00ef10
TT
3834
3835 if (strstr (name, "::") != nullptr)
3836 {
3837 for (unsigned int current_len = cp_find_first_component (name);
3838 name[current_len] != '\0';
3839 current_len += cp_find_first_component (name + current_len))
3840 {
3841 gdb_assert (name[current_len] == ':');
3842 this->name_components.push_back ({previous_len, idx});
3843 /* Skip the '::'. */
3844 current_len += 2;
3845 previous_len = current_len;
3846 }
3847 }
3848 else
5c58de74 3849 {
3b00ef10
TT
3850 /* Handle the Ada encoded (aka mangled) form here. */
3851 for (const char *iter = strstr (name, "__");
3852 iter != nullptr;
3853 iter = strstr (iter, "__"))
3854 {
3855 this->name_components.push_back ({previous_len, idx});
3856 iter += 2;
3857 previous_len = iter - name;
3858 }
5c58de74 3859 }
3b00ef10 3860
5c58de74
PA
3861 this->name_components.push_back ({previous_len, idx});
3862 }
3863
3864 /* Sort name_components elements by name. */
3865 auto name_comp_compare = [&] (const name_component &left,
3866 const name_component &right)
3867 {
3868 const char *left_qualified = this->symbol_name_at (left.idx);
3869 const char *right_qualified = this->symbol_name_at (right.idx);
3870
3871 const char *left_name = left_qualified + left.name_offset;
3872 const char *right_name = right_qualified + right.name_offset;
3873
3874 return name_cmp (left_name, right_name) < 0;
3875 };
3876
3877 std::sort (this->name_components.begin (),
3878 this->name_components.end (),
3879 name_comp_compare);
3880}
3881
3882/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3883 mapped_index_base instead of the containing objfile. This is split
3884 to a separate function in order to be able to unit test the
3885 name_components matching using a mock mapped_index_base. For each
5c58de74 3886 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3887 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3888
3889static void
3890dw2_expand_symtabs_matching_symbol
44ed8f3e 3891 (mapped_index_base &index,
5c58de74
PA
3892 const lookup_name_info &lookup_name_in,
3893 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3894 enum search_domain kind,
3b00ef10 3895 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3896{
3897 lookup_name_info lookup_name_without_params
3898 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3899
3900 /* Build the symbol name component sorted vector, if we haven't
3901 yet. */
3902 index.build_name_components ();
3903
3f563c84
PA
3904 /* The same symbol may appear more than once in the range though.
3905 E.g., if we're looking for symbols that complete "w", and we have
3906 a symbol named "w1::w2", we'll find the two name components for
3907 that same symbol in the range. To be sure we only call the
3908 callback once per symbol, we first collect the symbol name
3909 indexes that matched in a temporary vector and ignore
3910 duplicates. */
3911 std::vector<offset_type> matches;
3f563c84 3912
3b00ef10
TT
3913 struct name_and_matcher
3914 {
3915 symbol_name_matcher_ftype *matcher;
3916 const std::string &name;
3917
3918 bool operator== (const name_and_matcher &other) const
3f563c84 3919 {
3b00ef10
TT
3920 return matcher == other.matcher && name == other.name;
3921 }
3922 };
3923
3924 /* A vector holding all the different symbol name matchers, for all
3925 languages. */
3926 std::vector<name_and_matcher> matchers;
3927
3928 for (int i = 0; i < nr_languages; i++)
3929 {
3930 enum language lang_e = (enum language) i;
3931
3932 const language_defn *lang = language_def (lang_e);
3933 symbol_name_matcher_ftype *name_matcher
3934 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3935
3b00ef10
TT
3936 name_and_matcher key {
3937 name_matcher,
3938 lookup_name_without_params.language_lookup_name (lang_e)
3939 };
3940
3941 /* Don't insert the same comparison routine more than once.
3942 Note that we do this linear walk. This is not a problem in
3943 practice because the number of supported languages is
3944 low. */
3945 if (std::find (matchers.begin (), matchers.end (), key)
3946 != matchers.end ())
9291a0cd 3947 continue;
3b00ef10
TT
3948 matchers.push_back (std::move (key));
3949
3950 auto bounds
3951 = index.find_name_components_bounds (lookup_name_without_params,
3952 lang_e);
3953
3954 /* Now for each symbol name in range, check to see if we have a name
3955 match, and if so, call the MATCH_CALLBACK callback. */
3956
3957 for (; bounds.first != bounds.second; ++bounds.first)
3958 {
3959 const char *qualified = index.symbol_name_at (bounds.first->idx);
3960
3961 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3962 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3963 continue;
9291a0cd 3964
3b00ef10
TT
3965 matches.push_back (bounds.first->idx);
3966 }
3f563c84
PA
3967 }
3968
3969 std::sort (matches.begin (), matches.end ());
3970
3971 /* Finally call the callback, once per match. */
3972 ULONGEST prev = -1;
3973 for (offset_type idx : matches)
3974 {
3975 if (prev != idx)
3976 {
3b00ef10
TT
3977 if (!match_callback (idx))
3978 break;
3f563c84
PA
3979 prev = idx;
3980 }
3981 }
3982
3983 /* Above we use a type wider than idx's for 'prev', since 0 and
3984 (offset_type)-1 are both possible values. */
3985 static_assert (sizeof (prev) > sizeof (offset_type), "");
3986}
3987
c62446b1
PA
3988#if GDB_SELF_TEST
3989
3990namespace selftests { namespace dw2_expand_symtabs_matching {
3991
a3c5fafd
PA
3992/* A mock .gdb_index/.debug_names-like name index table, enough to
3993 exercise dw2_expand_symtabs_matching_symbol, which works with the
3994 mapped_index_base interface. Builds an index from the symbol list
3995 passed as parameter to the constructor. */
3996class mock_mapped_index : public mapped_index_base
c62446b1
PA
3997{
3998public:
a3c5fafd
PA
3999 mock_mapped_index (gdb::array_view<const char *> symbols)
4000 : m_symbol_table (symbols)
c62446b1
PA
4001 {}
4002
a3c5fafd 4003 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4004
a3c5fafd 4005 /* Return the number of names in the symbol table. */
632e107b 4006 size_t symbol_name_count () const override
c62446b1 4007 {
a3c5fafd 4008 return m_symbol_table.size ();
c62446b1
PA
4009 }
4010
a3c5fafd 4011 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4012 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4013 {
4014 return m_symbol_table[idx];
4015 }
c62446b1 4016
a3c5fafd
PA
4017private:
4018 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4019};
4020
4021/* Convenience function that converts a NULL pointer to a "<null>"
4022 string, to pass to print routines. */
4023
4024static const char *
4025string_or_null (const char *str)
4026{
4027 return str != NULL ? str : "<null>";
4028}
4029
4030/* Check if a lookup_name_info built from
4031 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4032 index. EXPECTED_LIST is the list of expected matches, in expected
4033 matching order. If no match expected, then an empty list is
4034 specified. Returns true on success. On failure prints a warning
4035 indicating the file:line that failed, and returns false. */
4036
4037static bool
4038check_match (const char *file, int line,
4039 mock_mapped_index &mock_index,
4040 const char *name, symbol_name_match_type match_type,
4041 bool completion_mode,
4042 std::initializer_list<const char *> expected_list)
4043{
4044 lookup_name_info lookup_name (name, match_type, completion_mode);
4045
4046 bool matched = true;
4047
4048 auto mismatch = [&] (const char *expected_str,
4049 const char *got)
4050 {
4051 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4052 "expected=\"%s\", got=\"%s\"\n"),
4053 file, line,
4054 (match_type == symbol_name_match_type::FULL
4055 ? "FULL" : "WILD"),
4056 name, string_or_null (expected_str), string_or_null (got));
4057 matched = false;
4058 };
4059
4060 auto expected_it = expected_list.begin ();
4061 auto expected_end = expected_list.end ();
4062
a3c5fafd 4063 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4064 NULL, ALL_DOMAIN,
4065 [&] (offset_type idx)
4066 {
a3c5fafd 4067 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4068 const char *expected_str
4069 = expected_it == expected_end ? NULL : *expected_it++;
4070
4071 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4072 mismatch (expected_str, matched_name);
3b00ef10 4073 return true;
c62446b1
PA
4074 });
4075
4076 const char *expected_str
4077 = expected_it == expected_end ? NULL : *expected_it++;
4078 if (expected_str != NULL)
4079 mismatch (expected_str, NULL);
4080
4081 return matched;
4082}
4083
4084/* The symbols added to the mock mapped_index for testing (in
4085 canonical form). */
4086static const char *test_symbols[] = {
4087 "function",
4088 "std::bar",
4089 "std::zfunction",
4090 "std::zfunction2",
4091 "w1::w2",
4092 "ns::foo<char*>",
4093 "ns::foo<int>",
4094 "ns::foo<long>",
a20714ff
PA
4095 "ns2::tmpl<int>::foo2",
4096 "(anonymous namespace)::A::B::C",
c62446b1 4097
e1ef7d7a
PA
4098 /* These are used to check that the increment-last-char in the
4099 matching algorithm for completion doesn't match "t1_fund" when
4100 completing "t1_func". */
4101 "t1_func",
4102 "t1_func1",
4103 "t1_fund",
4104 "t1_fund1",
4105
4106 /* A UTF-8 name with multi-byte sequences to make sure that
4107 cp-name-parser understands this as a single identifier ("função"
4108 is "function" in PT). */
4109 u8"u8função",
4110
4111 /* \377 (0xff) is Latin1 'ÿ'. */
4112 "yfunc\377",
4113
4114 /* \377 (0xff) is Latin1 'ÿ'. */
4115 "\377",
4116 "\377\377123",
4117
c62446b1
PA
4118 /* A name with all sorts of complications. Starts with "z" to make
4119 it easier for the completion tests below. */
4120#define Z_SYM_NAME \
4121 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4122 "::tuple<(anonymous namespace)::ui*, " \
4123 "std::default_delete<(anonymous namespace)::ui>, void>"
4124
4125 Z_SYM_NAME
4126};
4127
a3c5fafd
PA
4128/* Returns true if the mapped_index_base::find_name_component_bounds
4129 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4130 in completion mode. */
5c58de74
PA
4131
4132static bool
a3c5fafd 4133check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4134 const char *search_name,
4135 gdb::array_view<const char *> expected_syms)
4136{
4137 lookup_name_info lookup_name (search_name,
4138 symbol_name_match_type::FULL, true);
4139
3b00ef10
TT
4140 auto bounds = index.find_name_components_bounds (lookup_name,
4141 language_cplus);
5c58de74
PA
4142
4143 size_t distance = std::distance (bounds.first, bounds.second);
4144 if (distance != expected_syms.size ())
4145 return false;
4146
4147 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4148 {
4149 auto nc_elem = bounds.first + exp_elem;
4150 const char *qualified = index.symbol_name_at (nc_elem->idx);
4151 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4152 return false;
4153 }
4154
4155 return true;
4156}
4157
4158/* Test the lower-level mapped_index::find_name_component_bounds
4159 method. */
4160
c62446b1 4161static void
5c58de74
PA
4162test_mapped_index_find_name_component_bounds ()
4163{
4164 mock_mapped_index mock_index (test_symbols);
4165
a3c5fafd 4166 mock_index.build_name_components ();
5c58de74
PA
4167
4168 /* Test the lower-level mapped_index::find_name_component_bounds
4169 method in completion mode. */
4170 {
4171 static const char *expected_syms[] = {
4172 "t1_func",
4173 "t1_func1",
5c58de74
PA
4174 };
4175
a3c5fafd 4176 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4177 "t1_func", expected_syms));
4178 }
4179
4180 /* Check that the increment-last-char in the name matching algorithm
4181 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4182 {
4183 static const char *expected_syms1[] = {
4184 "\377",
4185 "\377\377123",
4186 };
a3c5fafd 4187 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4188 "\377", expected_syms1));
4189
4190 static const char *expected_syms2[] = {
4191 "\377\377123",
4192 };
a3c5fafd 4193 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4194 "\377\377", expected_syms2));
4195 }
4196}
4197
4198/* Test dw2_expand_symtabs_matching_symbol. */
4199
4200static void
4201test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4202{
4203 mock_mapped_index mock_index (test_symbols);
4204
4205 /* We let all tests run until the end even if some fails, for debug
4206 convenience. */
4207 bool any_mismatch = false;
4208
4209 /* Create the expected symbols list (an initializer_list). Needed
4210 because lists have commas, and we need to pass them to CHECK,
4211 which is a macro. */
4212#define EXPECT(...) { __VA_ARGS__ }
4213
4214 /* Wrapper for check_match that passes down the current
4215 __FILE__/__LINE__. */
4216#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4217 any_mismatch |= !check_match (__FILE__, __LINE__, \
4218 mock_index, \
4219 NAME, MATCH_TYPE, COMPLETION_MODE, \
4220 EXPECTED_LIST)
4221
4222 /* Identity checks. */
4223 for (const char *sym : test_symbols)
4224 {
4225 /* Should be able to match all existing symbols. */
4226 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4227 EXPECT (sym));
4228
4229 /* Should be able to match all existing symbols with
4230 parameters. */
4231 std::string with_params = std::string (sym) + "(int)";
4232 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4233 EXPECT (sym));
4234
4235 /* Should be able to match all existing symbols with
4236 parameters and qualifiers. */
4237 with_params = std::string (sym) + " ( int ) const";
4238 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4239 EXPECT (sym));
4240
4241 /* This should really find sym, but cp-name-parser.y doesn't
4242 know about lvalue/rvalue qualifiers yet. */
4243 with_params = std::string (sym) + " ( int ) &&";
4244 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4245 {});
4246 }
4247
e1ef7d7a
PA
4248 /* Check that the name matching algorithm for completion doesn't get
4249 confused with Latin1 'ÿ' / 0xff. */
4250 {
4251 static const char str[] = "\377";
4252 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4253 EXPECT ("\377", "\377\377123"));
4254 }
4255
4256 /* Check that the increment-last-char in the matching algorithm for
4257 completion doesn't match "t1_fund" when completing "t1_func". */
4258 {
4259 static const char str[] = "t1_func";
4260 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4261 EXPECT ("t1_func", "t1_func1"));
4262 }
4263
c62446b1
PA
4264 /* Check that completion mode works at each prefix of the expected
4265 symbol name. */
4266 {
4267 static const char str[] = "function(int)";
4268 size_t len = strlen (str);
4269 std::string lookup;
4270
4271 for (size_t i = 1; i < len; i++)
4272 {
4273 lookup.assign (str, i);
4274 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4275 EXPECT ("function"));
4276 }
4277 }
4278
4279 /* While "w" is a prefix of both components, the match function
4280 should still only be called once. */
4281 {
4282 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4283 EXPECT ("w1::w2"));
a20714ff
PA
4284 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4285 EXPECT ("w1::w2"));
c62446b1
PA
4286 }
4287
4288 /* Same, with a "complicated" symbol. */
4289 {
4290 static const char str[] = Z_SYM_NAME;
4291 size_t len = strlen (str);
4292 std::string lookup;
4293
4294 for (size_t i = 1; i < len; i++)
4295 {
4296 lookup.assign (str, i);
4297 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4298 EXPECT (Z_SYM_NAME));
4299 }
4300 }
4301
4302 /* In FULL mode, an incomplete symbol doesn't match. */
4303 {
4304 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4305 {});
4306 }
4307
4308 /* A complete symbol with parameters matches any overload, since the
4309 index has no overload info. */
4310 {
4311 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4312 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4313 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4314 EXPECT ("std::zfunction", "std::zfunction2"));
4315 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4316 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4317 }
4318
4319 /* Check that whitespace is ignored appropriately. A symbol with a
4320 template argument list. */
4321 {
4322 static const char expected[] = "ns::foo<int>";
4323 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4324 EXPECT (expected));
a20714ff
PA
4325 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4326 EXPECT (expected));
c62446b1
PA
4327 }
4328
4329 /* Check that whitespace is ignored appropriately. A symbol with a
4330 template argument list that includes a pointer. */
4331 {
4332 static const char expected[] = "ns::foo<char*>";
4333 /* Try both completion and non-completion modes. */
4334 static const bool completion_mode[2] = {false, true};
4335 for (size_t i = 0; i < 2; i++)
4336 {
4337 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4338 completion_mode[i], EXPECT (expected));
a20714ff
PA
4339 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4340 completion_mode[i], EXPECT (expected));
c62446b1
PA
4341
4342 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4343 completion_mode[i], EXPECT (expected));
a20714ff
PA
4344 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4345 completion_mode[i], EXPECT (expected));
c62446b1
PA
4346 }
4347 }
4348
4349 {
4350 /* Check method qualifiers are ignored. */
4351 static const char expected[] = "ns::foo<char*>";
4352 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4353 symbol_name_match_type::FULL, true, EXPECT (expected));
4354 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4355 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4356 CHECK_MATCH ("foo < char * > ( int ) const",
4357 symbol_name_match_type::WILD, true, EXPECT (expected));
4358 CHECK_MATCH ("foo < char * > ( int ) &&",
4359 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4360 }
4361
4362 /* Test lookup names that don't match anything. */
4363 {
a20714ff
PA
4364 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4365 {});
4366
c62446b1
PA
4367 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4368 {});
4369 }
4370
a20714ff
PA
4371 /* Some wild matching tests, exercising "(anonymous namespace)",
4372 which should not be confused with a parameter list. */
4373 {
4374 static const char *syms[] = {
4375 "A::B::C",
4376 "B::C",
4377 "C",
4378 "A :: B :: C ( int )",
4379 "B :: C ( int )",
4380 "C ( int )",
4381 };
4382
4383 for (const char *s : syms)
4384 {
4385 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4386 EXPECT ("(anonymous namespace)::A::B::C"));
4387 }
4388 }
4389
4390 {
4391 static const char expected[] = "ns2::tmpl<int>::foo2";
4392 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4393 EXPECT (expected));
4394 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4395 EXPECT (expected));
4396 }
4397
c62446b1
PA
4398 SELF_CHECK (!any_mismatch);
4399
4400#undef EXPECT
4401#undef CHECK_MATCH
4402}
4403
5c58de74
PA
4404static void
4405run_test ()
4406{
4407 test_mapped_index_find_name_component_bounds ();
4408 test_dw2_expand_symtabs_matching_symbol ();
4409}
4410
c62446b1
PA
4411}} // namespace selftests::dw2_expand_symtabs_matching
4412
4413#endif /* GDB_SELF_TEST */
4414
4b514bc8
JK
4415/* If FILE_MATCHER is NULL or if PER_CU has
4416 dwarf2_per_cu_quick_data::MARK set (see
4417 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4418 EXPANSION_NOTIFY on it. */
4419
4420static void
4421dw2_expand_symtabs_matching_one
4422 (struct dwarf2_per_cu_data *per_cu,
4423 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4424 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4425{
4426 if (file_matcher == NULL || per_cu->v.quick->mark)
4427 {
4428 bool symtab_was_null
4429 = (per_cu->v.quick->compunit_symtab == NULL);
4430
58f0c718 4431 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4432
4433 if (expansion_notify != NULL
4434 && symtab_was_null
4435 && per_cu->v.quick->compunit_symtab != NULL)
4436 expansion_notify (per_cu->v.quick->compunit_symtab);
4437 }
4438}
4439
3f563c84
PA
4440/* Helper for dw2_expand_matching symtabs. Called on each symbol
4441 matched, to expand corresponding CUs that were marked. IDX is the
4442 index of the symbol name that matched. */
4443
4444static void
4445dw2_expand_marked_cus
ed2dc618 4446 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4447 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4448 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4449 search_domain kind)
4450{
3f563c84
PA
4451 offset_type *vec, vec_len, vec_idx;
4452 bool global_seen = false;
ed2dc618 4453 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4454
61920122 4455 vec = (offset_type *) (index.constant_pool
f00a2de2 4456 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4457 vec_len = MAYBE_SWAP (vec[0]);
4458 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4459 {
61920122
PA
4460 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4461 /* This value is only valid for index versions >= 7. */
4462 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4463 gdb_index_symbol_kind symbol_kind =
4464 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4465 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4466 /* Only check the symbol attributes if they're present.
4467 Indices prior to version 7 don't record them,
4468 and indices >= 7 may elide them for certain symbols
4469 (gold does this). */
4470 int attrs_valid =
4471 (index.version >= 7
4472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4473
4474 /* Work around gold/15646. */
4475 if (attrs_valid)
9291a0cd 4476 {
61920122
PA
4477 if (!is_static && global_seen)
4478 continue;
4479 if (!is_static)
4480 global_seen = true;
4481 }
3190f0c6 4482
61920122
PA
4483 /* Only check the symbol's kind if it has one. */
4484 if (attrs_valid)
4485 {
4486 switch (kind)
8943b874 4487 {
61920122
PA
4488 case VARIABLES_DOMAIN:
4489 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4490 continue;
4491 break;
4492 case FUNCTIONS_DOMAIN:
4493 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4494 continue;
61920122
PA
4495 break;
4496 case TYPES_DOMAIN:
4497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4498 continue;
4499 break;
59c35742
AB
4500 case MODULES_DOMAIN:
4501 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4502 continue;
4503 break;
61920122
PA
4504 default:
4505 break;
8943b874 4506 }
61920122 4507 }
8943b874 4508
61920122 4509 /* Don't crash on bad data. */
b76e467d 4510 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4511 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4512 {
b98664d3 4513 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4514 " [in module %s]"),
4515 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4516 continue;
4517 }
4518
ff4c9fec 4519 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4520 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4521 expansion_notify);
61920122
PA
4522 }
4523}
4524
4b514bc8
JK
4525/* If FILE_MATCHER is non-NULL, set all the
4526 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4527 that match FILE_MATCHER. */
4528
61920122 4529static void
4b514bc8 4530dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4531 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4532 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4533{
4b514bc8 4534 if (file_matcher == NULL)
61920122
PA
4535 return;
4536
4b514bc8
JK
4537 objfile *const objfile = dwarf2_per_objfile->objfile;
4538
4539 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4540 htab_eq_pointer,
4541 NULL, xcalloc, xfree));
4542 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4543 htab_eq_pointer,
4544 NULL, xcalloc, xfree));
61920122 4545
4b514bc8
JK
4546 /* The rule is CUs specify all the files, including those used by
4547 any TU, so there's no need to scan TUs here. */
61920122 4548
b76e467d 4549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4550 {
927aa2e7
JK
4551 QUIT;
4552
4553 per_cu->v.quick->mark = 0;
4554
4555 /* We only need to look at symtabs not already expanded. */
4556 if (per_cu->v.quick->compunit_symtab)
4557 continue;
4558
b76e467d 4559 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4560 if (file_data == NULL)
4561 continue;
4562
4563 if (htab_find (visited_not_found.get (), file_data) != NULL)
4564 continue;
4565 else if (htab_find (visited_found.get (), file_data) != NULL)
4566 {
4567 per_cu->v.quick->mark = 1;
4568 continue;
4569 }
4570
b76e467d 4571 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4572 {
4573 const char *this_real_name;
4574
4575 if (file_matcher (file_data->file_names[j], false))
4576 {
4577 per_cu->v.quick->mark = 1;
4578 break;
4579 }
4580
4581 /* Before we invoke realpath, which can get expensive when many
4582 files are involved, do a quick comparison of the basenames. */
4583 if (!basenames_may_differ
4584 && !file_matcher (lbasename (file_data->file_names[j]),
4585 true))
4586 continue;
4587
4588 this_real_name = dw2_get_real_path (objfile, file_data, j);
4589 if (file_matcher (this_real_name, false))
4590 {
4591 per_cu->v.quick->mark = 1;
4592 break;
4593 }
4594 }
4595
b76e467d
SM
4596 void **slot = htab_find_slot (per_cu->v.quick->mark
4597 ? visited_found.get ()
4598 : visited_not_found.get (),
4599 file_data, INSERT);
927aa2e7
JK
4600 *slot = file_data;
4601 }
4602}
4603
4604static void
4605dw2_expand_symtabs_matching
4606 (struct objfile *objfile,
4607 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4608 const lookup_name_info &lookup_name,
4609 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4610 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4611 enum search_domain kind)
4612{
ed2dc618
SM
4613 struct dwarf2_per_objfile *dwarf2_per_objfile
4614 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4615
4616 /* index_table is NULL if OBJF_READNOW. */
4617 if (!dwarf2_per_objfile->index_table)
4618 return;
4619
ed2dc618 4620 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4621
4622 mapped_index &index = *dwarf2_per_objfile->index_table;
4623
4624 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4625 symbol_matcher,
4626 kind, [&] (offset_type idx)
4627 {
ed2dc618 4628 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4629 expansion_notify, kind);
3b00ef10 4630 return true;
927aa2e7
JK
4631 });
4632}
4633
4634/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4635 symtab. */
4636
4637static struct compunit_symtab *
4638recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4639 CORE_ADDR pc)
4640{
4641 int i;
4642
4643 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4644 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4645 return cust;
4646
4647 if (cust->includes == NULL)
4648 return NULL;
4649
4650 for (i = 0; cust->includes[i]; ++i)
4651 {
4652 struct compunit_symtab *s = cust->includes[i];
4653
4654 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4655 if (s != NULL)
4656 return s;
4657 }
4658
4659 return NULL;
4660}
4661
4662static struct compunit_symtab *
4663dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4664 struct bound_minimal_symbol msymbol,
4665 CORE_ADDR pc,
4666 struct obj_section *section,
4667 int warn_if_readin)
4668{
4669 struct dwarf2_per_cu_data *data;
4670 struct compunit_symtab *result;
4671
d320c2b5 4672 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4673 return NULL;
4674
b3b3bada 4675 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4676 data = (struct dwarf2_per_cu_data *) addrmap_find
4677 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4678 if (!data)
4679 return NULL;
4680
4681 if (warn_if_readin && data->v.quick->compunit_symtab)
4682 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4683 paddress (get_objfile_arch (objfile), pc));
4684
4685 result
58f0c718
TT
4686 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4687 false),
927aa2e7
JK
4688 pc);
4689 gdb_assert (result != NULL);
4690 return result;
4691}
4692
4693static void
4694dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4695 void *data, int need_fullname)
4696{
ed2dc618
SM
4697 struct dwarf2_per_objfile *dwarf2_per_objfile
4698 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4699
4700 if (!dwarf2_per_objfile->filenames_cache)
4701 {
4702 dwarf2_per_objfile->filenames_cache.emplace ();
4703
4704 htab_up visited (htab_create_alloc (10,
4705 htab_hash_pointer, htab_eq_pointer,
4706 NULL, xcalloc, xfree));
4707
4708 /* The rule is CUs specify all the files, including those used
4709 by any TU, so there's no need to scan TUs here. We can
4710 ignore file names coming from already-expanded CUs. */
4711
b76e467d 4712 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4713 {
927aa2e7
JK
4714 if (per_cu->v.quick->compunit_symtab)
4715 {
4716 void **slot = htab_find_slot (visited.get (),
4717 per_cu->v.quick->file_names,
4718 INSERT);
4719
4720 *slot = per_cu->v.quick->file_names;
4721 }
4722 }
4723
b76e467d 4724 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4725 {
927aa2e7
JK
4726 /* We only need to look at symtabs not already expanded. */
4727 if (per_cu->v.quick->compunit_symtab)
4728 continue;
4729
b76e467d 4730 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4731 if (file_data == NULL)
4732 continue;
4733
b76e467d 4734 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4735 if (*slot)
4736 {
4737 /* Already visited. */
4738 continue;
4739 }
4740 *slot = file_data;
4741
4742 for (int j = 0; j < file_data->num_file_names; ++j)
4743 {
4744 const char *filename = file_data->file_names[j];
4745 dwarf2_per_objfile->filenames_cache->seen (filename);
4746 }
4747 }
4748 }
4749
4750 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4751 {
4752 gdb::unique_xmalloc_ptr<char> this_real_name;
4753
4754 if (need_fullname)
4755 this_real_name = gdb_realpath (filename);
4756 (*fun) (filename, this_real_name.get (), data);
4757 });
4758}
4759
4760static int
4761dw2_has_symbols (struct objfile *objfile)
4762{
4763 return 1;
4764}
4765
4766const struct quick_symbol_functions dwarf2_gdb_index_functions =
4767{
4768 dw2_has_symbols,
4769 dw2_find_last_source_symtab,
4770 dw2_forget_cached_source_info,
4771 dw2_map_symtabs_matching_filename,
4772 dw2_lookup_symbol,
d3214198 4773 NULL,
927aa2e7
JK
4774 dw2_print_stats,
4775 dw2_dump,
927aa2e7
JK
4776 dw2_expand_symtabs_for_function,
4777 dw2_expand_all_symtabs,
4778 dw2_expand_symtabs_with_fullname,
4779 dw2_map_matching_symbols,
4780 dw2_expand_symtabs_matching,
4781 dw2_find_pc_sect_compunit_symtab,
4782 NULL,
4783 dw2_map_symbol_filenames
4784};
4785
4786/* DWARF-5 debug_names reader. */
4787
4788/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4789static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4790
4791/* A helper function that reads the .debug_names section in SECTION
4792 and fills in MAP. FILENAME is the name of the file containing the
4793 section; it is used for error reporting.
4794
4795 Returns true if all went well, false otherwise. */
4796
4797static bool
4798read_debug_names_from_section (struct objfile *objfile,
4799 const char *filename,
4800 struct dwarf2_section_info *section,
4801 mapped_debug_names &map)
4802{
96b79293 4803 if (section->empty ())
927aa2e7
JK
4804 return false;
4805
4806 /* Older elfutils strip versions could keep the section in the main
4807 executable while splitting it for the separate debug info file. */
96b79293 4808 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4809 return false;
4810
96b79293 4811 section->read (objfile);
927aa2e7
JK
4812
4813 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4814
4815 const gdb_byte *addr = section->buffer;
4816
96b79293 4817 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4818
4819 unsigned int bytes_read;
4820 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4821 addr += bytes_read;
4822
4823 map.dwarf5_is_dwarf64 = bytes_read != 4;
4824 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4825 if (bytes_read + length != section->size)
4826 {
4827 /* There may be multiple per-CU indices. */
4828 warning (_("Section .debug_names in %s length %s does not match "
4829 "section length %s, ignoring .debug_names."),
4830 filename, plongest (bytes_read + length),
4831 pulongest (section->size));
4832 return false;
4833 }
4834
4835 /* The version number. */
4836 uint16_t version = read_2_bytes (abfd, addr);
4837 addr += 2;
4838 if (version != 5)
4839 {
4840 warning (_("Section .debug_names in %s has unsupported version %d, "
4841 "ignoring .debug_names."),
4842 filename, version);
4843 return false;
4844 }
4845
4846 /* Padding. */
4847 uint16_t padding = read_2_bytes (abfd, addr);
4848 addr += 2;
4849 if (padding != 0)
4850 {
4851 warning (_("Section .debug_names in %s has unsupported padding %d, "
4852 "ignoring .debug_names."),
4853 filename, padding);
4854 return false;
4855 }
4856
4857 /* comp_unit_count - The number of CUs in the CU list. */
4858 map.cu_count = read_4_bytes (abfd, addr);
4859 addr += 4;
4860
4861 /* local_type_unit_count - The number of TUs in the local TU
4862 list. */
4863 map.tu_count = read_4_bytes (abfd, addr);
4864 addr += 4;
4865
4866 /* foreign_type_unit_count - The number of TUs in the foreign TU
4867 list. */
4868 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870 if (foreign_tu_count != 0)
4871 {
4872 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4873 "ignoring .debug_names."),
4874 filename, static_cast<unsigned long> (foreign_tu_count));
4875 return false;
4876 }
4877
4878 /* bucket_count - The number of hash buckets in the hash lookup
4879 table. */
4880 map.bucket_count = read_4_bytes (abfd, addr);
4881 addr += 4;
4882
4883 /* name_count - The number of unique names in the index. */
4884 map.name_count = read_4_bytes (abfd, addr);
4885 addr += 4;
4886
4887 /* abbrev_table_size - The size in bytes of the abbreviations
4888 table. */
4889 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4890 addr += 4;
4891
4892 /* augmentation_string_size - The size in bytes of the augmentation
4893 string. This value is rounded up to a multiple of 4. */
4894 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4895 addr += 4;
4896 map.augmentation_is_gdb = ((augmentation_string_size
4897 == sizeof (dwarf5_augmentation))
4898 && memcmp (addr, dwarf5_augmentation,
4899 sizeof (dwarf5_augmentation)) == 0);
4900 augmentation_string_size += (-augmentation_string_size) & 3;
4901 addr += augmentation_string_size;
4902
4903 /* List of CUs */
4904 map.cu_table_reordered = addr;
4905 addr += map.cu_count * map.offset_size;
4906
4907 /* List of Local TUs */
4908 map.tu_table_reordered = addr;
4909 addr += map.tu_count * map.offset_size;
4910
4911 /* Hash Lookup Table */
4912 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4913 addr += map.bucket_count * 4;
4914 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4915 addr += map.name_count * 4;
4916
4917 /* Name Table */
4918 map.name_table_string_offs_reordered = addr;
4919 addr += map.name_count * map.offset_size;
4920 map.name_table_entry_offs_reordered = addr;
4921 addr += map.name_count * map.offset_size;
4922
4923 const gdb_byte *abbrev_table_start = addr;
4924 for (;;)
4925 {
927aa2e7
JK
4926 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4927 addr += bytes_read;
4928 if (index_num == 0)
4929 break;
4930
4931 const auto insertpair
4932 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4933 if (!insertpair.second)
4934 {
4935 warning (_("Section .debug_names in %s has duplicate index %s, "
4936 "ignoring .debug_names."),
4937 filename, pulongest (index_num));
4938 return false;
4939 }
4940 mapped_debug_names::index_val &indexval = insertpair.first->second;
4941 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4942 addr += bytes_read;
4943
4944 for (;;)
4945 {
4946 mapped_debug_names::index_val::attr attr;
4947 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4948 addr += bytes_read;
4949 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4950 addr += bytes_read;
4951 if (attr.form == DW_FORM_implicit_const)
4952 {
4953 attr.implicit_const = read_signed_leb128 (abfd, addr,
4954 &bytes_read);
4955 addr += bytes_read;
4956 }
4957 if (attr.dw_idx == 0 && attr.form == 0)
4958 break;
4959 indexval.attr_vec.push_back (std::move (attr));
4960 }
4961 }
4962 if (addr != abbrev_table_start + abbrev_table_size)
4963 {
4964 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4965 "of size %s vs. written as %u, ignoring .debug_names."),
4966 filename, plongest (addr - abbrev_table_start),
4967 abbrev_table_size);
927aa2e7
JK
4968 return false;
4969 }
4970 map.entry_pool = addr;
4971
4972 return true;
4973}
4974
4975/* A helper for create_cus_from_debug_names that handles the MAP's CU
4976 list. */
4977
4978static void
ed2dc618 4979create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4980 const mapped_debug_names &map,
4981 dwarf2_section_info &section,
b76e467d 4982 bool is_dwz)
927aa2e7
JK
4983{
4984 sect_offset sect_off_prev;
4985 for (uint32_t i = 0; i <= map.cu_count; ++i)
4986 {
4987 sect_offset sect_off_next;
4988 if (i < map.cu_count)
4989 {
4990 sect_off_next
4991 = (sect_offset) (extract_unsigned_integer
4992 (map.cu_table_reordered + i * map.offset_size,
4993 map.offset_size,
4994 map.dwarf5_byte_order));
4995 }
4996 else
4997 sect_off_next = (sect_offset) section.size;
4998 if (i >= 1)
4999 {
5000 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5001 dwarf2_per_cu_data *per_cu
ed2dc618 5002 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5003 sect_off_prev, length);
b76e467d 5004 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5005 }
5006 sect_off_prev = sect_off_next;
5007 }
5008}
5009
5010/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5011 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5012
5013static void
ed2dc618 5014create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5015 const mapped_debug_names &map,
5016 const mapped_debug_names &dwz_map)
5017{
b76e467d
SM
5018 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5019 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5020
ed2dc618
SM
5021 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5022 dwarf2_per_objfile->info,
b76e467d 5023 false /* is_dwz */);
927aa2e7
JK
5024
5025 if (dwz_map.cu_count == 0)
5026 return;
5027
ed2dc618
SM
5028 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5029 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5030 true /* is_dwz */);
927aa2e7
JK
5031}
5032
5033/* Read .debug_names. If everything went ok, initialize the "quick"
5034 elements of all the CUs and return true. Otherwise, return false. */
5035
5036static bool
ed2dc618 5037dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5038{
22ca247e
TT
5039 std::unique_ptr<mapped_debug_names> map
5040 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5041 mapped_debug_names dwz_map (dwarf2_per_objfile);
5042 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5043
5044 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5045 &dwarf2_per_objfile->debug_names,
22ca247e 5046 *map))
927aa2e7
JK
5047 return false;
5048
5049 /* Don't use the index if it's empty. */
22ca247e 5050 if (map->name_count == 0)
927aa2e7
JK
5051 return false;
5052
5053 /* If there is a .dwz file, read it so we can get its CU list as
5054 well. */
ed2dc618 5055 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5056 if (dwz != NULL)
5057 {
5058 if (!read_debug_names_from_section (objfile,
00f93c44 5059 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5060 &dwz->debug_names, dwz_map))
5061 {
5062 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5063 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5064 return false;
5065 }
5066 }
5067
22ca247e 5068 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5069
22ca247e 5070 if (map->tu_count != 0)
927aa2e7
JK
5071 {
5072 /* We can only handle a single .debug_types when we have an
5073 index. */
fd5866f6 5074 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5075 return false;
5076
fd5866f6 5077 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5078
5079 create_signatured_type_table_from_debug_names
22ca247e 5080 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5081 }
5082
ed2dc618
SM
5083 create_addrmap_from_aranges (dwarf2_per_objfile,
5084 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5085
22ca247e 5086 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5087 dwarf2_per_objfile->using_index = 1;
5088 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5089 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5090
5091 return true;
5092}
5093
927aa2e7
JK
5094/* Type used to manage iterating over all CUs looking for a symbol for
5095 .debug_names. */
5096
5097class dw2_debug_names_iterator
5098{
5099public:
927aa2e7 5100 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5101 gdb::optional<block_enum> block_index,
5102 domain_enum domain,
927aa2e7 5103 const char *name)
2b79f376 5104 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5105 m_addr (find_vec_in_debug_names (map, name))
5106 {}
5107
5108 dw2_debug_names_iterator (const mapped_debug_names &map,
5109 search_domain search, uint32_t namei)
5110 : m_map (map),
5111 m_search (search),
5112 m_addr (find_vec_in_debug_names (map, namei))
5113 {}
5114
3b00ef10
TT
5115 dw2_debug_names_iterator (const mapped_debug_names &map,
5116 block_enum block_index, domain_enum domain,
5117 uint32_t namei)
5118 : m_map (map), m_block_index (block_index), m_domain (domain),
5119 m_addr (find_vec_in_debug_names (map, namei))
5120 {}
5121
927aa2e7
JK
5122 /* Return the next matching CU or NULL if there are no more. */
5123 dwarf2_per_cu_data *next ();
5124
5125private:
5126 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5127 const char *name);
5128 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5129 uint32_t namei);
5130
5131 /* The internalized form of .debug_names. */
5132 const mapped_debug_names &m_map;
5133
2b79f376
SM
5134 /* If set, only look for symbols that match that block. Valid values are
5135 GLOBAL_BLOCK and STATIC_BLOCK. */
5136 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5137
5138 /* The kind of symbol we're looking for. */
5139 const domain_enum m_domain = UNDEF_DOMAIN;
5140 const search_domain m_search = ALL_DOMAIN;
5141
5142 /* The list of CUs from the index entry of the symbol, or NULL if
5143 not found. */
5144 const gdb_byte *m_addr;
5145};
5146
5147const char *
5148mapped_debug_names::namei_to_name (uint32_t namei) const
5149{
5150 const ULONGEST namei_string_offs
5151 = extract_unsigned_integer ((name_table_string_offs_reordered
5152 + namei * offset_size),
5153 offset_size,
5154 dwarf5_byte_order);
4f44ae6c
TT
5155 return read_indirect_string_at_offset (dwarf2_per_objfile,
5156 namei_string_offs);
927aa2e7
JK
5157}
5158
5159/* Find a slot in .debug_names for the object named NAME. If NAME is
5160 found, return pointer to its pool data. If NAME cannot be found,
5161 return NULL. */
5162
5163const gdb_byte *
5164dw2_debug_names_iterator::find_vec_in_debug_names
5165 (const mapped_debug_names &map, const char *name)
5166{
5167 int (*cmp) (const char *, const char *);
5168
54ee4252 5169 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5170 if (current_language->la_language == language_cplus
5171 || current_language->la_language == language_fortran
5172 || current_language->la_language == language_d)
5173 {
5174 /* NAME is already canonical. Drop any qualifiers as
5175 .debug_names does not contain any. */
5176
5177 if (strchr (name, '(') != NULL)
5178 {
54ee4252 5179 without_params = cp_remove_params (name);
927aa2e7 5180 if (without_params != NULL)
54ee4252 5181 name = without_params.get ();
927aa2e7
JK
5182 }
5183 }
5184
5185 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5186
5187 const uint32_t full_hash = dwarf5_djb_hash (name);
5188 uint32_t namei
5189 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5190 (map.bucket_table_reordered
5191 + (full_hash % map.bucket_count)), 4,
5192 map.dwarf5_byte_order);
5193 if (namei == 0)
5194 return NULL;
5195 --namei;
5196 if (namei >= map.name_count)
5197 {
b98664d3 5198 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5199 "[in module %s]"),
5200 namei, map.name_count,
ed2dc618 5201 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5202 return NULL;
5203 }
5204
5205 for (;;)
5206 {
5207 const uint32_t namei_full_hash
5208 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5209 (map.hash_table_reordered + namei), 4,
5210 map.dwarf5_byte_order);
5211 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5212 return NULL;
5213
5214 if (full_hash == namei_full_hash)
5215 {
5216 const char *const namei_string = map.namei_to_name (namei);
5217
5218#if 0 /* An expensive sanity check. */
5219 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5220 {
b98664d3 5221 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5222 "[in module %s]"),
5223 namei, objfile_name (dwarf2_per_objfile->objfile));
5224 return NULL;
5225 }
5226#endif
5227
5228 if (cmp (namei_string, name) == 0)
5229 {
5230 const ULONGEST namei_entry_offs
5231 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5232 + namei * map.offset_size),
5233 map.offset_size, map.dwarf5_byte_order);
5234 return map.entry_pool + namei_entry_offs;
5235 }
5236 }
5237
5238 ++namei;
5239 if (namei >= map.name_count)
5240 return NULL;
5241 }
5242}
5243
5244const gdb_byte *
5245dw2_debug_names_iterator::find_vec_in_debug_names
5246 (const mapped_debug_names &map, uint32_t namei)
5247{
5248 if (namei >= map.name_count)
5249 {
b98664d3 5250 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5251 "[in module %s]"),
5252 namei, map.name_count,
ed2dc618 5253 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5254 return NULL;
5255 }
5256
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262}
5263
5264/* See dw2_debug_names_iterator. */
5265
5266dwarf2_per_cu_data *
5267dw2_debug_names_iterator::next ()
5268{
5269 if (m_addr == NULL)
5270 return NULL;
5271
ed2dc618
SM
5272 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5273 struct objfile *objfile = dwarf2_per_objfile->objfile;
5274 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5275
5276 again:
5277
5278 unsigned int bytes_read;
5279 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5280 m_addr += bytes_read;
5281 if (abbrev == 0)
5282 return NULL;
5283
5284 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5285 if (indexval_it == m_map.abbrev_map.cend ())
5286 {
b98664d3 5287 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5288 "[in module %s]"),
ed2dc618 5289 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5290 return NULL;
5291 }
5292 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5293 enum class symbol_linkage {
5294 unknown,
5295 static_,
5296 extern_,
23c13d42 5297 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5298 dwarf2_per_cu_data *per_cu = NULL;
5299 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5300 {
5301 ULONGEST ull;
5302 switch (attr.form)
5303 {
5304 case DW_FORM_implicit_const:
5305 ull = attr.implicit_const;
5306 break;
5307 case DW_FORM_flag_present:
5308 ull = 1;
5309 break;
5310 case DW_FORM_udata:
5311 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5312 m_addr += bytes_read;
5313 break;
5314 default:
b98664d3 5315 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5316 dwarf_form_name (attr.form),
ed2dc618 5317 objfile_name (objfile));
927aa2e7
JK
5318 return NULL;
5319 }
5320 switch (attr.dw_idx)
5321 {
5322 case DW_IDX_compile_unit:
5323 /* Don't crash on bad data. */
b76e467d 5324 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5325 {
b98664d3 5326 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5327 " [in module %s]"),
5328 pulongest (ull),
5329 objfile_name (dwarf2_per_objfile->objfile));
5330 continue;
5331 }
ff4c9fec 5332 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5333 break;
8af5c486
JK
5334 case DW_IDX_type_unit:
5335 /* Don't crash on bad data. */
b2bdb8cf 5336 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5337 {
b98664d3 5338 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5339 " [in module %s]"),
5340 pulongest (ull),
5341 objfile_name (dwarf2_per_objfile->objfile));
5342 continue;
5343 }
ff4c9fec 5344 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5345 break;
927aa2e7
JK
5346 case DW_IDX_GNU_internal:
5347 if (!m_map.augmentation_is_gdb)
5348 break;
23c13d42 5349 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5350 break;
5351 case DW_IDX_GNU_external:
5352 if (!m_map.augmentation_is_gdb)
5353 break;
23c13d42 5354 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5355 break;
5356 }
5357 }
5358
5359 /* Skip if already read in. */
5360 if (per_cu->v.quick->compunit_symtab)
5361 goto again;
5362
5363 /* Check static vs global. */
23c13d42 5364 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5365 {
2b79f376 5366 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5367 const bool symbol_is_static =
5368 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5369 if (want_static != symbol_is_static)
2b79f376 5370 goto again;
927aa2e7
JK
5371 }
5372
5373 /* Match dw2_symtab_iter_next, symbol_kind
5374 and debug_names::psymbol_tag. */
5375 switch (m_domain)
5376 {
5377 case VAR_DOMAIN:
5378 switch (indexval.dwarf_tag)
5379 {
5380 case DW_TAG_variable:
5381 case DW_TAG_subprogram:
5382 /* Some types are also in VAR_DOMAIN. */
5383 case DW_TAG_typedef:
5384 case DW_TAG_structure_type:
5385 break;
5386 default:
5387 goto again;
5388 }
5389 break;
5390 case STRUCT_DOMAIN:
5391 switch (indexval.dwarf_tag)
5392 {
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case LABEL_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case 0:
5404 case DW_TAG_variable:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
59c35742
AB
5410 case MODULE_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case DW_TAG_module:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
927aa2e7
JK
5419 default:
5420 break;
5421 }
5422
5423 /* Match dw2_expand_symtabs_matching, symbol_kind and
5424 debug_names::psymbol_tag. */
5425 switch (m_search)
4b514bc8 5426 {
927aa2e7
JK
5427 case VARIABLES_DOMAIN:
5428 switch (indexval.dwarf_tag)
4b514bc8 5429 {
927aa2e7
JK
5430 case DW_TAG_variable:
5431 break;
5432 default:
5433 goto again;
4b514bc8 5434 }
927aa2e7
JK
5435 break;
5436 case FUNCTIONS_DOMAIN:
5437 switch (indexval.dwarf_tag)
4b514bc8 5438 {
927aa2e7
JK
5439 case DW_TAG_subprogram:
5440 break;
5441 default:
5442 goto again;
4b514bc8 5443 }
927aa2e7
JK
5444 break;
5445 case TYPES_DOMAIN:
5446 switch (indexval.dwarf_tag)
5447 {
5448 case DW_TAG_typedef:
5449 case DW_TAG_structure_type:
5450 break;
5451 default:
5452 goto again;
5453 }
5454 break;
59c35742
AB
5455 case MODULES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_module:
5459 break;
5460 default:
5461 goto again;
5462 }
927aa2e7
JK
5463 default:
5464 break;
4b514bc8 5465 }
927aa2e7
JK
5466
5467 return per_cu;
4b514bc8 5468}
61920122 5469
927aa2e7 5470static struct compunit_symtab *
c7f839cb 5471dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5472 const char *name, domain_enum domain)
4b514bc8 5473{
ed2dc618
SM
5474 struct dwarf2_per_objfile *dwarf2_per_objfile
5475 = get_dwarf2_per_objfile (objfile);
61920122 5476
927aa2e7
JK
5477 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5478 if (!mapp)
61920122 5479 {
927aa2e7
JK
5480 /* index is NULL if OBJF_READNOW. */
5481 return NULL;
5482 }
5483 const auto &map = *mapp;
9291a0cd 5484
2b79f376 5485 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5486
927aa2e7
JK
5487 struct compunit_symtab *stab_best = NULL;
5488 struct dwarf2_per_cu_data *per_cu;
5489 while ((per_cu = iter.next ()) != NULL)
5490 {
5491 struct symbol *sym, *with_opaque = NULL;
58f0c718 5492 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5493 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5494 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5495
927aa2e7
JK
5496 sym = block_find_symbol (block, name, domain,
5497 block_find_non_opaque_type_preferred,
5498 &with_opaque);
9703b513 5499
927aa2e7
JK
5500 /* Some caution must be observed with overloaded functions and
5501 methods, since the index will not contain any overload
5502 information (but NAME might contain it). */
a3ec0bb1 5503
927aa2e7 5504 if (sym != NULL
987012b8 5505 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5506 return stab;
5507 if (with_opaque != NULL
987012b8 5508 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5509 stab_best = stab;
9703b513 5510
927aa2e7 5511 /* Keep looking through other CUs. */
9703b513
TT
5512 }
5513
927aa2e7 5514 return stab_best;
9703b513
TT
5515}
5516
927aa2e7
JK
5517/* This dumps minimal information about .debug_names. It is called
5518 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5519 uses this to verify that .debug_names has been loaded. */
9291a0cd 5520
927aa2e7
JK
5521static void
5522dw2_debug_names_dump (struct objfile *objfile)
5523{
ed2dc618
SM
5524 struct dwarf2_per_objfile *dwarf2_per_objfile
5525 = get_dwarf2_per_objfile (objfile);
5526
927aa2e7
JK
5527 gdb_assert (dwarf2_per_objfile->using_index);
5528 printf_filtered (".debug_names:");
5529 if (dwarf2_per_objfile->debug_names_table)
5530 printf_filtered (" exists\n");
5531 else
5532 printf_filtered (" faked for \"readnow\"\n");
5533 printf_filtered ("\n");
9291a0cd
TT
5534}
5535
9291a0cd 5536static void
927aa2e7
JK
5537dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5538 const char *func_name)
9291a0cd 5539{
ed2dc618
SM
5540 struct dwarf2_per_objfile *dwarf2_per_objfile
5541 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5542
927aa2e7
JK
5543 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5544 if (dwarf2_per_objfile->debug_names_table)
24c79950 5545 {
927aa2e7 5546 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5547
2b79f376 5548 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5549
927aa2e7
JK
5550 struct dwarf2_per_cu_data *per_cu;
5551 while ((per_cu = iter.next ()) != NULL)
58f0c718 5552 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5553 }
5554}
24c79950 5555
3b00ef10
TT
5556static void
5557dw2_debug_names_map_matching_symbols
5558 (struct objfile *objfile,
5559 const lookup_name_info &name, domain_enum domain,
5560 int global,
5561 gdb::function_view<symbol_found_callback_ftype> callback,
5562 symbol_compare_ftype *ordered_compare)
5563{
5564 struct dwarf2_per_objfile *dwarf2_per_objfile
5565 = get_dwarf2_per_objfile (objfile);
5566
5567 /* debug_names_table is NULL if OBJF_READNOW. */
5568 if (!dwarf2_per_objfile->debug_names_table)
5569 return;
5570
5571 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5572 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5573
5574 const char *match_name = name.ada ().lookup_name ().c_str ();
5575 auto matcher = [&] (const char *symname)
5576 {
5577 if (ordered_compare == nullptr)
5578 return true;
5579 return ordered_compare (symname, match_name) == 0;
5580 };
5581
5582 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5583 [&] (offset_type namei)
5584 {
5585 /* The name was matched, now expand corresponding CUs that were
5586 marked. */
5587 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5588
5589 struct dwarf2_per_cu_data *per_cu;
5590 while ((per_cu = iter.next ()) != NULL)
5591 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5592 return true;
5593 });
5594
5595 /* It's a shame we couldn't do this inside the
5596 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5597 that have already been expanded. Instead, this loop matches what
5598 the psymtab code does. */
5599 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5600 {
5601 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5602 if (cust != nullptr)
5603 {
5604 const struct block *block
5605 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5606 if (!iterate_over_symbols_terminated (block, name,
5607 domain, callback))
5608 break;
5609 }
5610 }
5611}
5612
927aa2e7
JK
5613static void
5614dw2_debug_names_expand_symtabs_matching
5615 (struct objfile *objfile,
5616 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5617 const lookup_name_info &lookup_name,
5618 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5619 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5620 enum search_domain kind)
5621{
ed2dc618
SM
5622 struct dwarf2_per_objfile *dwarf2_per_objfile
5623 = get_dwarf2_per_objfile (objfile);
9291a0cd 5624
927aa2e7
JK
5625 /* debug_names_table is NULL if OBJF_READNOW. */
5626 if (!dwarf2_per_objfile->debug_names_table)
5627 return;
9291a0cd 5628
ed2dc618 5629 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5630
44ed8f3e 5631 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5632
44ed8f3e
PA
5633 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5634 symbol_matcher,
5635 kind, [&] (offset_type namei)
927aa2e7 5636 {
927aa2e7
JK
5637 /* The name was matched, now expand corresponding CUs that were
5638 marked. */
5639 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5640
927aa2e7
JK
5641 struct dwarf2_per_cu_data *per_cu;
5642 while ((per_cu = iter.next ()) != NULL)
5643 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5644 expansion_notify);
3b00ef10 5645 return true;
44ed8f3e 5646 });
9291a0cd
TT
5647}
5648
927aa2e7 5649const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5650{
5651 dw2_has_symbols,
5652 dw2_find_last_source_symtab,
5653 dw2_forget_cached_source_info,
f8eba3c6 5654 dw2_map_symtabs_matching_filename,
927aa2e7 5655 dw2_debug_names_lookup_symbol,
d3214198 5656 NULL,
9291a0cd 5657 dw2_print_stats,
927aa2e7 5658 dw2_debug_names_dump,
927aa2e7 5659 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5660 dw2_expand_all_symtabs,
652a8996 5661 dw2_expand_symtabs_with_fullname,
3b00ef10 5662 dw2_debug_names_map_matching_symbols,
927aa2e7 5663 dw2_debug_names_expand_symtabs_matching,
43f3e411 5664 dw2_find_pc_sect_compunit_symtab,
71a3c369 5665 NULL,
9291a0cd
TT
5666 dw2_map_symbol_filenames
5667};
5668
4485a1c1
SM
5669/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5670 to either a dwarf2_per_objfile or dwz_file object. */
5671
5672template <typename T>
5673static gdb::array_view<const gdb_byte>
5674get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5675{
5676 dwarf2_section_info *section = &section_owner->gdb_index;
5677
96b79293 5678 if (section->empty ())
4485a1c1
SM
5679 return {};
5680
5681 /* Older elfutils strip versions could keep the section in the main
5682 executable while splitting it for the separate debug info file. */
96b79293 5683 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5684 return {};
5685
96b79293 5686 section->read (obj);
4485a1c1 5687
8bebfcda
PA
5688 /* dwarf2_section_info::size is a bfd_size_type, while
5689 gdb::array_view works with size_t. On 32-bit hosts, with
5690 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5691 is 32-bit. So we need an explicit narrowing conversion here.
5692 This is fine, because it's impossible to allocate or mmap an
5693 array/buffer larger than what size_t can represent. */
5694 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5695}
5696
87d6a7aa
SM
5697/* Lookup the index cache for the contents of the index associated to
5698 DWARF2_OBJ. */
5699
5700static gdb::array_view<const gdb_byte>
5701get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5702{
5703 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5704 if (build_id == nullptr)
5705 return {};
5706
5707 return global_index_cache.lookup_gdb_index (build_id,
5708 &dwarf2_obj->index_cache_res);
5709}
5710
5711/* Same as the above, but for DWZ. */
5712
5713static gdb::array_view<const gdb_byte>
5714get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5715{
5716 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5717 if (build_id == nullptr)
5718 return {};
5719
5720 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5721}
5722
3c0aa29a 5723/* See symfile.h. */
9291a0cd 5724
3c0aa29a
PA
5725bool
5726dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5727{
ed2dc618
SM
5728 struct dwarf2_per_objfile *dwarf2_per_objfile
5729 = get_dwarf2_per_objfile (objfile);
5730
9291a0cd
TT
5731 /* If we're about to read full symbols, don't bother with the
5732 indices. In this case we also don't care if some other debug
5733 format is making psymtabs, because they are all about to be
5734 expanded anyway. */
5735 if ((objfile->flags & OBJF_READNOW))
5736 {
9291a0cd 5737 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5738 create_all_comp_units (dwarf2_per_objfile);
5739 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5740 dwarf2_per_objfile->quick_file_names_table
5741 = create_quick_file_names_table
5742 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5743
b76e467d 5744 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5745 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5746 {
ff4c9fec 5747 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5748
e254ef6a
DE
5749 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5750 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5751 }
5752
5753 /* Return 1 so that gdb sees the "quick" functions. However,
5754 these functions will be no-ops because we will have expanded
5755 all symtabs. */
3c0aa29a
PA
5756 *index_kind = dw_index_kind::GDB_INDEX;
5757 return true;
9291a0cd
TT
5758 }
5759
ed2dc618 5760 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5761 {
5762 *index_kind = dw_index_kind::DEBUG_NAMES;
5763 return true;
5764 }
927aa2e7 5765
4485a1c1
SM
5766 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5767 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5768 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5769 {
5770 *index_kind = dw_index_kind::GDB_INDEX;
5771 return true;
5772 }
9291a0cd 5773
87d6a7aa
SM
5774 /* ... otherwise, try to find the index in the index cache. */
5775 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5776 get_gdb_index_contents_from_cache,
5777 get_gdb_index_contents_from_cache_dwz))
5778 {
5779 global_index_cache.hit ();
5780 *index_kind = dw_index_kind::GDB_INDEX;
5781 return true;
5782 }
5783
5784 global_index_cache.miss ();
3c0aa29a 5785 return false;
9291a0cd
TT
5786}
5787
5788\f
5789
dce234bc
PP
5790/* Build a partial symbol table. */
5791
5792void
f29dff0a 5793dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5794{
ed2dc618
SM
5795 struct dwarf2_per_objfile *dwarf2_per_objfile
5796 = get_dwarf2_per_objfile (objfile);
c9bf0622 5797
6eee24ce 5798 init_psymbol_list (objfile, 1024);
c906108c 5799
a70b8144 5800 try
c9bf0622
TT
5801 {
5802 /* This isn't really ideal: all the data we allocate on the
5803 objfile's obstack is still uselessly kept around. However,
5804 freeing it seems unsafe. */
906768f9 5805 psymtab_discarder psymtabs (objfile);
ed2dc618 5806 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5807 psymtabs.keep ();
87d6a7aa
SM
5808
5809 /* (maybe) store an index in the cache. */
5810 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5811 }
230d2906 5812 catch (const gdb_exception_error &except)
492d29ea
PA
5813 {
5814 exception_print (gdb_stderr, except);
5815 }
c906108c 5816}
c906108c 5817
3b80fe9b
DE
5818/* Find the base address of the compilation unit for range lists and
5819 location lists. It will normally be specified by DW_AT_low_pc.
5820 In DWARF-3 draft 4, the base address could be overridden by
5821 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5822 compilation units with discontinuous ranges. */
5823
5824static void
5825dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5826{
5827 struct attribute *attr;
5828
2b24b6e4 5829 cu->base_address.reset ();
3b80fe9b
DE
5830
5831 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5832 if (attr != nullptr)
2b24b6e4 5833 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5834 else
5835 {
5836 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5837 if (attr != nullptr)
2b24b6e4 5838 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5839 }
5840}
5841
36586728
TT
5842/* Helper function that returns the proper abbrev section for
5843 THIS_CU. */
5844
5845static struct dwarf2_section_info *
5846get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5847{
5848 struct dwarf2_section_info *abbrev;
ed2dc618 5849 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5850
5851 if (this_cu->is_dwz)
ed2dc618 5852 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5853 else
5854 abbrev = &dwarf2_per_objfile->abbrev;
5855
5856 return abbrev;
5857}
5858
f4dc4d17
DE
5859/* Fetch the abbreviation table offset from a comp or type unit header. */
5860
5861static sect_offset
ed2dc618
SM
5862read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5863 struct dwarf2_section_info *section,
9c541725 5864 sect_offset sect_off)
f4dc4d17 5865{
96b79293 5866 bfd *abfd = section->get_bfd_owner ();
d521ce57 5867 const gdb_byte *info_ptr;
ac298888 5868 unsigned int initial_length_size, offset_size;
43988095 5869 uint16_t version;
f4dc4d17 5870
96b79293 5871 section->read (dwarf2_per_objfile->objfile);
9c541725 5872 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5873 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5874 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5875 info_ptr += initial_length_size;
5876
5877 version = read_2_bytes (abfd, info_ptr);
5878 info_ptr += 2;
5879 if (version >= 5)
5880 {
5881 /* Skip unit type and address size. */
5882 info_ptr += 2;
5883 }
5884
24aa364d 5885 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5886}
5887
b83470bf
TT
5888/* A partial symtab that is used only for include files. */
5889struct dwarf2_include_psymtab : public partial_symtab
5890{
5891 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5892 : partial_symtab (filename, objfile)
5893 {
5894 }
5895
5896 void read_symtab (struct objfile *objfile) override
5897 {
5898 expand_psymtab (objfile);
5899 }
5900
5901 void expand_psymtab (struct objfile *objfile) override
5902 {
5903 if (m_readin)
5904 return;
5905 /* It's an include file, no symbols to read for it.
5906 Everything is in the parent symtab. */
48993951 5907 expand_dependencies (objfile);
b83470bf
TT
5908 m_readin = true;
5909 }
5910
5911 bool readin_p () const override
5912 {
5913 return m_readin;
5914 }
5915
5916 struct compunit_symtab *get_compunit_symtab () const override
5917 {
5918 return nullptr;
5919 }
5920
5921private:
5922
5923 bool m_readin = false;
5924};
5925
aaa75496
JB
5926/* Allocate a new partial symtab for file named NAME and mark this new
5927 partial symtab as being an include of PST. */
5928
5929static void
891813be 5930dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5931 struct objfile *objfile)
5932{
b83470bf 5933 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5934
fbd9ab74
JK
5935 if (!IS_ABSOLUTE_PATH (subpst->filename))
5936 {
5937 /* It shares objfile->objfile_obstack. */
5938 subpst->dirname = pst->dirname;
5939 }
5940
a9342b62 5941 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5942 subpst->dependencies[0] = pst;
5943 subpst->number_of_dependencies = 1;
aaa75496
JB
5944}
5945
5946/* Read the Line Number Program data and extract the list of files
5947 included by the source file represented by PST. Build an include
d85a05f0 5948 partial symtab for each of these included files. */
aaa75496
JB
5949
5950static void
5951dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5952 struct die_info *die,
891813be 5953 dwarf2_psymtab *pst)
aaa75496 5954{
fff8551c 5955 line_header_up lh;
d85a05f0 5956 struct attribute *attr;
aaa75496 5957
d85a05f0 5958 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5959 if (attr != nullptr)
9c541725 5960 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5961 if (lh == NULL)
5962 return; /* No linetable, so no includes. */
5963
79748972
TT
5964 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5965 that we pass in the raw text_low here; that is ok because we're
5966 only decoding the line table to make include partial symtabs, and
5967 so the addresses aren't really used. */
4ae976d1 5968 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5969 pst->raw_text_low (), 1);
aaa75496
JB
5970}
5971
348e048f 5972static hashval_t
52dc124a 5973hash_signatured_type (const void *item)
348e048f 5974{
9a3c8263
SM
5975 const struct signatured_type *sig_type
5976 = (const struct signatured_type *) item;
9a619af0 5977
348e048f 5978 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5979 return sig_type->signature;
348e048f
DE
5980}
5981
5982static int
52dc124a 5983eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5984{
9a3c8263
SM
5985 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5986 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5987
348e048f
DE
5988 return lhs->signature == rhs->signature;
5989}
5990
1fd400ff
TT
5991/* Allocate a hash table for signatured types. */
5992
b0b6a987 5993static htab_up
298e9637 5994allocate_signatured_type_table ()
1fd400ff 5995{
b0b6a987
TT
5996 return htab_up (htab_create_alloc (41,
5997 hash_signatured_type,
5998 eq_signatured_type,
5999 NULL, xcalloc, xfree));
1fd400ff
TT
6000}
6001
d467dd73 6002/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6003
6004static int
d467dd73 6005add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6006{
9a3c8263 6007 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6008 std::vector<signatured_type *> *all_type_units
6009 = (std::vector<signatured_type *> *) datum;
1fd400ff 6010
b2bdb8cf 6011 all_type_units->push_back (sigt);
1fd400ff
TT
6012
6013 return 1;
6014}
6015
78d4d2c5 6016/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6017 and fill them into TYPES_HTAB. It will process only type units,
6018 therefore DW_UT_type. */
c88ee1f0 6019
78d4d2c5 6020static void
ed2dc618
SM
6021create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6022 struct dwo_file *dwo_file,
b0b6a987 6023 dwarf2_section_info *section, htab_up &types_htab,
43988095 6024 rcuh_kind section_kind)
348e048f 6025{
3019eac3 6026 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6027 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6028 bfd *abfd;
6029 const gdb_byte *info_ptr, *end_ptr;
348e048f 6030
4bdcc0c1
DE
6031 abbrev_section = (dwo_file != NULL
6032 ? &dwo_file->sections.abbrev
6033 : &dwarf2_per_objfile->abbrev);
6034
b4f54984 6035 if (dwarf_read_debug)
43988095 6036 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6037 section->get_name (),
6038 abbrev_section->get_file_name ());
09406207 6039
96b79293 6040 section->read (objfile);
78d4d2c5 6041 info_ptr = section->buffer;
348e048f 6042
78d4d2c5
JK
6043 if (info_ptr == NULL)
6044 return;
348e048f 6045
78d4d2c5
JK
6046 /* We can't set abfd until now because the section may be empty or
6047 not present, in which case the bfd is unknown. */
96b79293 6048 abfd = section->get_bfd_owner ();
348e048f 6049
c0ab21c2
TT
6050 /* We don't use cutu_reader here because we don't need to read
6051 any dies: the signature is in the header. */
3019eac3 6052
78d4d2c5
JK
6053 end_ptr = info_ptr + section->size;
6054 while (info_ptr < end_ptr)
6055 {
78d4d2c5
JK
6056 struct signatured_type *sig_type;
6057 struct dwo_unit *dwo_tu;
6058 void **slot;
6059 const gdb_byte *ptr = info_ptr;
6060 struct comp_unit_head header;
6061 unsigned int length;
8b70b953 6062
9c541725 6063 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6064
a49dd8dd
JK
6065 /* Initialize it due to a false compiler warning. */
6066 header.signature = -1;
9c541725 6067 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6068
78d4d2c5
JK
6069 /* We need to read the type's signature in order to build the hash
6070 table, but we don't need anything else just yet. */
348e048f 6071
ed2dc618 6072 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6073 abbrev_section, ptr, section_kind);
348e048f 6074
4057dfde 6075 length = header.get_length ();
6caca83c 6076
78d4d2c5
JK
6077 /* Skip dummy type units. */
6078 if (ptr >= info_ptr + length
43988095
JK
6079 || peek_abbrev_code (abfd, ptr) == 0
6080 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6081 {
6082 info_ptr += length;
6083 continue;
6084 }
dee91e82 6085
78d4d2c5
JK
6086 if (types_htab == NULL)
6087 {
6088 if (dwo_file)
298e9637 6089 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6090 else
298e9637 6091 types_htab = allocate_signatured_type_table ();
78d4d2c5 6092 }
8b70b953 6093
78d4d2c5
JK
6094 if (dwo_file)
6095 {
6096 sig_type = NULL;
6097 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6098 struct dwo_unit);
6099 dwo_tu->dwo_file = dwo_file;
43988095 6100 dwo_tu->signature = header.signature;
9c541725 6101 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6102 dwo_tu->section = section;
9c541725 6103 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6104 dwo_tu->length = length;
6105 }
6106 else
6107 {
6108 /* N.B.: type_offset is not usable if this type uses a DWO file.
6109 The real type_offset is in the DWO file. */
6110 dwo_tu = NULL;
6111 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6112 struct signatured_type);
43988095 6113 sig_type->signature = header.signature;
9c541725 6114 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6116 sig_type->per_cu.is_debug_types = 1;
6117 sig_type->per_cu.section = section;
9c541725 6118 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6119 sig_type->per_cu.length = length;
6120 }
6121
b0b6a987 6122 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6123 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6124 INSERT);
6125 gdb_assert (slot != NULL);
6126 if (*slot != NULL)
6127 {
9c541725 6128 sect_offset dup_sect_off;
0349ea22 6129
3019eac3
DE
6130 if (dwo_file)
6131 {
78d4d2c5
JK
6132 const struct dwo_unit *dup_tu
6133 = (const struct dwo_unit *) *slot;
6134
9c541725 6135 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6136 }
6137 else
6138 {
78d4d2c5
JK
6139 const struct signatured_type *dup_tu
6140 = (const struct signatured_type *) *slot;
6141
9c541725 6142 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6143 }
8b70b953 6144
b98664d3 6145 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6146 " the entry at offset %s, signature %s"),
6147 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6148 hex_string (header.signature));
78d4d2c5
JK
6149 }
6150 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6151
78d4d2c5 6152 if (dwarf_read_debug > 1)
9d8780f0
SM
6153 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6154 sect_offset_str (sect_off),
43988095 6155 hex_string (header.signature));
3019eac3 6156
78d4d2c5
JK
6157 info_ptr += length;
6158 }
6159}
3019eac3 6160
78d4d2c5
JK
6161/* Create the hash table of all entries in the .debug_types
6162 (or .debug_types.dwo) section(s).
6163 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6164 otherwise it is NULL.
b3c8eb43 6165
78d4d2c5 6166 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6167
78d4d2c5 6168 Note: This function processes DWO files only, not DWP files. */
348e048f 6169
78d4d2c5 6170static void
ed2dc618
SM
6171create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6172 struct dwo_file *dwo_file,
fd5866f6 6173 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6174 htab_up &types_htab)
78d4d2c5 6175{
fd5866f6
SM
6176 for (dwarf2_section_info &section : type_sections)
6177 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6178 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6179}
6180
6181/* Create the hash table of all entries in the .debug_types section,
6182 and initialize all_type_units.
6183 The result is zero if there is an error (e.g. missing .debug_types section),
6184 otherwise non-zero. */
6185
6186static int
ed2dc618 6187create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6188{
b0b6a987 6189 htab_up types_htab;
3019eac3 6190
ed2dc618
SM
6191 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6192 &dwarf2_per_objfile->info, types_htab,
43988095 6193 rcuh_kind::COMPILE);
ed2dc618
SM
6194 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6195 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6196 if (types_htab == NULL)
6197 {
6198 dwarf2_per_objfile->signatured_types = NULL;
6199 return 0;
6200 }
6201
b0b6a987 6202 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6203
b2bdb8cf 6204 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6205 dwarf2_per_objfile->all_type_units.reserve
6206 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6207
b0b6a987
TT
6208 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6209 add_signatured_type_cu_to_table,
b2bdb8cf 6210 &dwarf2_per_objfile->all_type_units);
1fd400ff 6211
348e048f
DE
6212 return 1;
6213}
6214
6aa5f3a6
DE
6215/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6216 If SLOT is non-NULL, it is the entry to use in the hash table.
6217 Otherwise we find one. */
6218
6219static struct signatured_type *
ed2dc618
SM
6220add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6221 void **slot)
6aa5f3a6
DE
6222{
6223 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6224
b2bdb8cf
SM
6225 if (dwarf2_per_objfile->all_type_units.size ()
6226 == dwarf2_per_objfile->all_type_units.capacity ())
6227 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6228
b2bdb8cf
SM
6229 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6230 struct signatured_type);
6231
6232 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6233 sig_type->signature = sig;
6234 sig_type->per_cu.is_debug_types = 1;
6235 if (dwarf2_per_objfile->using_index)
6236 {
6237 sig_type->per_cu.v.quick =
6238 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6239 struct dwarf2_per_cu_quick_data);
6240 }
6241
6242 if (slot == NULL)
6243 {
b0b6a987 6244 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6245 sig_type, INSERT);
6246 }
6247 gdb_assert (*slot == NULL);
6248 *slot = sig_type;
6249 /* The rest of sig_type must be filled in by the caller. */
6250 return sig_type;
6251}
6252
a2ce51a0
DE
6253/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6254 Fill in SIG_ENTRY with DWO_ENTRY. */
6255
6256static void
ed2dc618 6257fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6258 struct signatured_type *sig_entry,
6259 struct dwo_unit *dwo_entry)
6260{
7ee85ab1 6261 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6262 gdb_assert (! sig_entry->per_cu.queued);
6263 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6264 if (dwarf2_per_objfile->using_index)
6265 {
6266 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6267 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6268 }
6269 else
6270 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6271 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6272 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6273 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6274 gdb_assert (sig_entry->dwo_unit == NULL);
6275
6276 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6277 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6278 sig_entry->per_cu.length = dwo_entry->length;
6279 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6280 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6281 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6282 sig_entry->dwo_unit = dwo_entry;
6283}
6284
6285/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6286 If we haven't read the TU yet, create the signatured_type data structure
6287 for a TU to be read in directly from a DWO file, bypassing the stub.
6288 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6289 using .gdb_index, then when reading a CU we want to stay in the DWO file
6290 containing that CU. Otherwise we could end up reading several other DWO
6291 files (due to comdat folding) to process the transitive closure of all the
6292 mentioned TUs, and that can be slow. The current DWO file will have every
6293 type signature that it needs.
a2ce51a0
DE
6294 We only do this for .gdb_index because in the psymtab case we already have
6295 to read all the DWOs to build the type unit groups. */
6296
6297static struct signatured_type *
6298lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6299{
518817b3
SM
6300 struct dwarf2_per_objfile *dwarf2_per_objfile
6301 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6302 struct dwo_file *dwo_file;
6303 struct dwo_unit find_dwo_entry, *dwo_entry;
6304 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6305 void **slot;
a2ce51a0
DE
6306
6307 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6308
6aa5f3a6
DE
6309 /* If TU skeletons have been removed then we may not have read in any
6310 TUs yet. */
6311 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6312 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6313
6314 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6315 Use the global signatured_types array to do our own comdat-folding
6316 of types. If this is the first time we're reading this TU, and
6317 the TU has an entry in .gdb_index, replace the recorded data from
6318 .gdb_index with this TU. */
a2ce51a0 6319
a2ce51a0 6320 find_sig_entry.signature = sig;
b0b6a987 6321 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6322 &find_sig_entry, INSERT);
9a3c8263 6323 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6324
6325 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6326 read. Don't reassign the global entry to point to this DWO if that's
6327 the case. Also note that if the TU is already being read, it may not
6328 have come from a DWO, the program may be a mix of Fission-compiled
6329 code and non-Fission-compiled code. */
6330
6331 /* Have we already tried to read this TU?
6332 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6333 needn't exist in the global table yet). */
6334 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6335 return sig_entry;
6336
6aa5f3a6
DE
6337 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6338 dwo_unit of the TU itself. */
6339 dwo_file = cu->dwo_unit->dwo_file;
6340
a2ce51a0
DE
6341 /* Ok, this is the first time we're reading this TU. */
6342 if (dwo_file->tus == NULL)
6343 return NULL;
6344 find_dwo_entry.signature = sig;
b0b6a987
TT
6345 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6346 &find_dwo_entry);
a2ce51a0
DE
6347 if (dwo_entry == NULL)
6348 return NULL;
6349
6aa5f3a6
DE
6350 /* If the global table doesn't have an entry for this TU, add one. */
6351 if (sig_entry == NULL)
ed2dc618 6352 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6353
ed2dc618 6354 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6355 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6356 return sig_entry;
6357}
6358
a2ce51a0
DE
6359/* Subroutine of lookup_signatured_type.
6360 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6361 then try the DWP file. If the TU stub (skeleton) has been removed then
6362 it won't be in .gdb_index. */
a2ce51a0
DE
6363
6364static struct signatured_type *
6365lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6366{
518817b3
SM
6367 struct dwarf2_per_objfile *dwarf2_per_objfile
6368 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6369 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6370 struct dwo_unit *dwo_entry;
6371 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6372 void **slot;
a2ce51a0
DE
6373
6374 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6375 gdb_assert (dwp_file != NULL);
6376
6aa5f3a6
DE
6377 /* If TU skeletons have been removed then we may not have read in any
6378 TUs yet. */
6379 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6380 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6381
6aa5f3a6 6382 find_sig_entry.signature = sig;
b0b6a987 6383 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6384 &find_sig_entry, INSERT);
9a3c8263 6385 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6386
6387 /* Have we already tried to read this TU?
6388 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6389 needn't exist in the global table yet). */
6390 if (sig_entry != NULL)
6391 return sig_entry;
6392
a2ce51a0
DE
6393 if (dwp_file->tus == NULL)
6394 return NULL;
ed2dc618 6395 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6396 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6397 if (dwo_entry == NULL)
6398 return NULL;
6399
ed2dc618
SM
6400 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6401 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6402
a2ce51a0
DE
6403 return sig_entry;
6404}
6405
380bca97 6406/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6407 Returns NULL if signature SIG is not present in the table.
6408 It is up to the caller to complain about this. */
348e048f
DE
6409
6410static struct signatured_type *
a2ce51a0 6411lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6412{
518817b3
SM
6413 struct dwarf2_per_objfile *dwarf2_per_objfile
6414 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6415
a2ce51a0
DE
6416 if (cu->dwo_unit
6417 && dwarf2_per_objfile->using_index)
6418 {
6419 /* We're in a DWO/DWP file, and we're using .gdb_index.
6420 These cases require special processing. */
ed2dc618 6421 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6422 return lookup_dwo_signatured_type (cu, sig);
6423 else
6424 return lookup_dwp_signatured_type (cu, sig);
6425 }
6426 else
6427 {
6428 struct signatured_type find_entry, *entry;
348e048f 6429
a2ce51a0
DE
6430 if (dwarf2_per_objfile->signatured_types == NULL)
6431 return NULL;
6432 find_entry.signature = sig;
9a3c8263 6433 entry = ((struct signatured_type *)
b0b6a987
TT
6434 htab_find (dwarf2_per_objfile->signatured_types.get (),
6435 &find_entry));
a2ce51a0
DE
6436 return entry;
6437 }
348e048f 6438}
18a8505e 6439
42e7ad6c 6440/* Low level DIE reading support. */
348e048f 6441
d85a05f0
DJ
6442/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6443
6444static void
6445init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6446 struct dwarf2_cu *cu,
3019eac3 6447 struct dwarf2_section_info *section,
685af9cd
TT
6448 struct dwo_file *dwo_file,
6449 struct abbrev_table *abbrev_table)
d85a05f0 6450{
fceca515 6451 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6452 reader->abfd = section->get_bfd_owner ();
d85a05f0 6453 reader->cu = cu;
3019eac3 6454 reader->dwo_file = dwo_file;
dee91e82
DE
6455 reader->die_section = section;
6456 reader->buffer = section->buffer;
f664829e 6457 reader->buffer_end = section->buffer + section->size;
685af9cd 6458 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6459}
6460
c0ab21c2 6461/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6462 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6463 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6464 already.
6465
6466 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6467 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6468 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6469 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6470 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6471 STUB_COMP_DIR may be non-NULL.
3e225074 6472 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6473 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6474 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6475 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6476 kept around for at least as long as *RESULT_READER.
6477
b0c7bfa9
DE
6478 The result is non-zero if a valid (non-dummy) DIE was found. */
6479
6480static int
6481read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6482 struct dwo_unit *dwo_unit,
b0c7bfa9 6483 struct die_info *stub_comp_unit_die,
a2ce51a0 6484 const char *stub_comp_dir,
b0c7bfa9 6485 struct die_reader_specs *result_reader,
d521ce57 6486 const gdb_byte **result_info_ptr,
b0c7bfa9 6487 struct die_info **result_comp_unit_die,
685af9cd 6488 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6489{
ed2dc618 6490 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6491 struct objfile *objfile = dwarf2_per_objfile->objfile;
6492 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6493 bfd *abfd;
d521ce57 6494 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6495 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6496 int i,num_extra_attrs;
6497 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6498 struct die_info *comp_unit_die;
6499
b0aeadb3
DE
6500 /* At most one of these may be provided. */
6501 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6502
b0c7bfa9
DE
6503 /* These attributes aren't processed until later:
6504 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6505 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6506 referenced later. However, these attributes are found in the stub
6507 which we won't have later. In order to not impose this complication
6508 on the rest of the code, we read them here and copy them to the
6509 DWO CU/TU die. */
b0c7bfa9
DE
6510
6511 stmt_list = NULL;
6512 low_pc = NULL;
6513 high_pc = NULL;
6514 ranges = NULL;
6515 comp_dir = NULL;
6516
6517 if (stub_comp_unit_die != NULL)
6518 {
6519 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6520 DWO file. */
6521 if (! this_cu->is_debug_types)
6522 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6523 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6524 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6525 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6526 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6527
a39fdb41 6528 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6529
18a8505e
AT
6530 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6531 here (if needed). We need the value before we can process
6532 DW_AT_ranges. */
a39fdb41 6533 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6534 }
a2ce51a0
DE
6535 else if (stub_comp_dir != NULL)
6536 {
6537 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6538 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6539 comp_dir->name = DW_AT_comp_dir;
6540 comp_dir->form = DW_FORM_string;
6541 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6542 DW_STRING (comp_dir) = stub_comp_dir;
6543 }
b0c7bfa9
DE
6544
6545 /* Set up for reading the DWO CU/TU. */
6546 cu->dwo_unit = dwo_unit;
685af9cd 6547 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6548 section->read (objfile);
6549 abfd = section->get_bfd_owner ();
9c541725
PA
6550 begin_info_ptr = info_ptr = (section->buffer
6551 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6552 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6553
6554 if (this_cu->is_debug_types)
6555 {
b0c7bfa9
DE
6556 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6557
ed2dc618
SM
6558 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6559 &cu->header, section,
b0c7bfa9 6560 dwo_abbrev_section,
43988095 6561 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6562 /* This is not an assert because it can be caused by bad debug info. */
43988095 6563 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6564 {
6565 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6566 " TU at offset %s [in module %s]"),
a2ce51a0 6567 hex_string (sig_type->signature),
43988095 6568 hex_string (cu->header.signature),
9d8780f0 6569 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6570 bfd_get_filename (abfd));
6571 }
9c541725 6572 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6573 /* For DWOs coming from DWP files, we don't know the CU length
6574 nor the type's offset in the TU until now. */
4057dfde 6575 dwo_unit->length = cu->header.get_length ();
9c541725 6576 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6577
6578 /* Establish the type offset that can be used to lookup the type.
6579 For DWO files, we don't know it until now. */
9c541725
PA
6580 sig_type->type_offset_in_section
6581 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6582 }
6583 else
6584 {
ed2dc618
SM
6585 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6586 &cu->header, section,
b0c7bfa9 6587 dwo_abbrev_section,
43988095 6588 info_ptr, rcuh_kind::COMPILE);
9c541725 6589 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6590 /* For DWOs coming from DWP files, we don't know the CU length
6591 until now. */
4057dfde 6592 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6593 }
6594
685af9cd 6595 *result_dwo_abbrev_table
86de1d91
TT
6596 = abbrev_table::read (objfile, dwo_abbrev_section,
6597 cu->header.abbrev_sect_off);
685af9cd
TT
6598 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6599 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6600
6601 /* Read in the die, but leave space to copy over the attributes
6602 from the stub. This has the benefit of simplifying the rest of
6603 the code - all the work to maintain the illusion of a single
6604 DW_TAG_{compile,type}_unit DIE is done here. */
6605 num_extra_attrs = ((stmt_list != NULL)
6606 + (low_pc != NULL)
6607 + (high_pc != NULL)
6608 + (ranges != NULL)
6609 + (comp_dir != NULL));
6610 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6611 num_extra_attrs);
b0c7bfa9
DE
6612
6613 /* Copy over the attributes from the stub to the DIE we just read in. */
6614 comp_unit_die = *result_comp_unit_die;
6615 i = comp_unit_die->num_attrs;
6616 if (stmt_list != NULL)
6617 comp_unit_die->attrs[i++] = *stmt_list;
6618 if (low_pc != NULL)
6619 comp_unit_die->attrs[i++] = *low_pc;
6620 if (high_pc != NULL)
6621 comp_unit_die->attrs[i++] = *high_pc;
6622 if (ranges != NULL)
6623 comp_unit_die->attrs[i++] = *ranges;
6624 if (comp_dir != NULL)
6625 comp_unit_die->attrs[i++] = *comp_dir;
6626 comp_unit_die->num_attrs += num_extra_attrs;
6627
b4f54984 6628 if (dwarf_die_debug)
bf6af496
DE
6629 {
6630 fprintf_unfiltered (gdb_stdlog,
6631 "Read die from %s@0x%x of %s:\n",
96b79293 6632 section->get_name (),
bf6af496
DE
6633 (unsigned) (begin_info_ptr - section->buffer),
6634 bfd_get_filename (abfd));
b4f54984 6635 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6636 }
6637
b0c7bfa9
DE
6638 /* Skip dummy compilation units. */
6639 if (info_ptr >= begin_info_ptr + dwo_unit->length
6640 || peek_abbrev_code (abfd, info_ptr) == 0)
6641 return 0;
6642
6643 *result_info_ptr = info_ptr;
6644 return 1;
6645}
6646
a084a2a6
AT
6647/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6648 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6649 signature is part of the header. */
6650static gdb::optional<ULONGEST>
6651lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6652{
6653 if (cu->header.version >= 5)
6654 return cu->header.signature;
6655 struct attribute *attr;
6656 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6657 if (attr == nullptr)
6658 return gdb::optional<ULONGEST> ();
6659 return DW_UNSND (attr);
6660}
6661
c0ab21c2 6662/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6663 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6664 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6665
6666static struct dwo_unit *
6667lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6668 struct die_info *comp_unit_die,
6669 const char *dwo_name)
b0c7bfa9
DE
6670{
6671 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6672 struct dwo_unit *dwo_unit;
c0ab21c2 6673 const char *comp_dir;
b0c7bfa9 6674
a2ce51a0
DE
6675 gdb_assert (cu != NULL);
6676
b0c7bfa9 6677 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6678 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6679 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6680
6681 if (this_cu->is_debug_types)
6682 {
6683 struct signatured_type *sig_type;
6684
6685 /* Since this_cu is the first member of struct signatured_type,
6686 we can go from a pointer to one to a pointer to the other. */
6687 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6688 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6689 }
6690 else
6691 {
a084a2a6
AT
6692 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6693 if (!signature.has_value ())
b0c7bfa9
DE
6694 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6695 " [in module %s]"),
e3b94546 6696 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6697 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6698 *signature);
b0c7bfa9
DE
6699 }
6700
b0c7bfa9
DE
6701 return dwo_unit;
6702}
6703
c0ab21c2 6704/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6705 See it for a description of the parameters.
fcd3b13d 6706 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6707
c0ab21c2
TT
6708void
6709cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6710 int use_existing_cu)
a2ce51a0 6711{
a2ce51a0 6712 struct signatured_type *sig_type;
a2ce51a0
DE
6713
6714 /* Verify we can do the following downcast, and that we have the
6715 data we need. */
6716 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6717 sig_type = (struct signatured_type *) this_cu;
6718 gdb_assert (sig_type->dwo_unit != NULL);
6719
6aa5f3a6
DE
6720 if (use_existing_cu && this_cu->cu != NULL)
6721 {
6722 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6723 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6724 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6725 }
6726 else
6727 {
6728 /* If !use_existing_cu, this_cu->cu must be NULL. */
6729 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6730 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6731 }
6732
6733 /* A future optimization, if needed, would be to use an existing
6734 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6735 could share abbrev tables. */
a2ce51a0
DE
6736
6737 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6738 NULL /* stub_comp_unit_die */,
6739 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6740 this, &info_ptr,
3e225074 6741 &comp_unit_die,
c0ab21c2 6742 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6743 {
6744 /* Dummy die. */
c0ab21c2 6745 dummy_p = true;
a2ce51a0 6746 }
a2ce51a0
DE
6747}
6748
fd820528 6749/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6750 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6751
f4dc4d17
DE
6752 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6753 Otherwise the table specified in the comp unit header is read in and used.
6754 This is an optimization for when we already have the abbrev table.
6755
dee91e82 6756 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6757 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6758
c0ab21c2
TT
6759cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6760 struct abbrev_table *abbrev_table,
6751ebae 6761 int use_existing_cu,
c0ab21c2
TT
6762 bool skip_partial)
6763 : die_reader_specs {},
6751ebae 6764 m_this_cu (this_cu)
c906108c 6765{
ed2dc618 6766 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6767 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6768 struct dwarf2_section_info *section = this_cu->section;
96b79293 6769 bfd *abfd = section->get_bfd_owner ();
dee91e82 6770 struct dwarf2_cu *cu;
c0ab21c2 6771 const gdb_byte *begin_info_ptr;
dee91e82 6772 struct signatured_type *sig_type = NULL;
4bdcc0c1 6773 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6774 /* Non-zero if CU currently points to a DWO file and we need to
6775 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6776 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6777 int rereading_dwo_cu = 0;
c906108c 6778
b4f54984 6779 if (dwarf_die_debug)
9d8780f0 6780 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6781 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6782 sect_offset_str (this_cu->sect_off));
09406207 6783
a2ce51a0
DE
6784 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6785 file (instead of going through the stub), short-circuit all of this. */
6786 if (this_cu->reading_dwo_directly)
6787 {
6788 /* Narrow down the scope of possibilities to have to understand. */
6789 gdb_assert (this_cu->is_debug_types);
6790 gdb_assert (abbrev_table == NULL);
6751ebae 6791 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6792 return;
6793 }
6794
dee91e82 6795 /* This is cheap if the section is already read in. */
96b79293 6796 section->read (objfile);
dee91e82 6797
9c541725 6798 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6799
6800 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6801
6802 if (use_existing_cu && this_cu->cu != NULL)
6803 {
6804 cu = this_cu->cu;
42e7ad6c
DE
6805 /* If this CU is from a DWO file we need to start over, we need to
6806 refetch the attributes from the skeleton CU.
6807 This could be optimized by retrieving those attributes from when we
6808 were here the first time: the previous comp_unit_die was stored in
6809 comp_unit_obstack. But there's no data yet that we need this
6810 optimization. */
6811 if (cu->dwo_unit != NULL)
6812 rereading_dwo_cu = 1;
dee91e82
DE
6813 }
6814 else
6815 {
6816 /* If !use_existing_cu, this_cu->cu must be NULL. */
6817 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6818 m_new_cu.reset (new dwarf2_cu (this_cu));
6819 cu = m_new_cu.get ();
42e7ad6c 6820 }
dee91e82 6821
b0c7bfa9 6822 /* Get the header. */
9c541725 6823 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6824 {
6825 /* We already have the header, there's no need to read it in again. */
9c541725 6826 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6827 }
6828 else
6829 {
3019eac3 6830 if (this_cu->is_debug_types)
dee91e82 6831 {
ed2dc618
SM
6832 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6833 &cu->header, section,
4bdcc0c1 6834 abbrev_section, info_ptr,
43988095 6835 rcuh_kind::TYPE);
dee91e82 6836
42e7ad6c
DE
6837 /* Since per_cu is the first member of struct signatured_type,
6838 we can go from a pointer to one to a pointer to the other. */
6839 sig_type = (struct signatured_type *) this_cu;
43988095 6840 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6841 gdb_assert (sig_type->type_offset_in_tu
6842 == cu->header.type_cu_offset_in_tu);
6843 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6844
42e7ad6c
DE
6845 /* LENGTH has not been set yet for type units if we're
6846 using .gdb_index. */
4057dfde 6847 this_cu->length = cu->header.get_length ();
3019eac3
DE
6848
6849 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6850 sig_type->type_offset_in_section =
6851 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6852
6853 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6854 }
6855 else
6856 {
ed2dc618
SM
6857 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6858 &cu->header, section,
4bdcc0c1 6859 abbrev_section,
43988095
JK
6860 info_ptr,
6861 rcuh_kind::COMPILE);
dee91e82 6862
9c541725 6863 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6864 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6865 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6866 }
6867 }
10b3939b 6868
6caca83c 6869 /* Skip dummy compilation units. */
dee91e82 6870 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6871 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6872 {
6873 dummy_p = true;
6874 return;
6875 }
6caca83c 6876
433df2d4
DE
6877 /* If we don't have them yet, read the abbrevs for this compilation unit.
6878 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6879 done. */
f4dc4d17 6880 if (abbrev_table != NULL)
685af9cd
TT
6881 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6882 else
f4dc4d17 6883 {
c0ab21c2 6884 m_abbrev_table_holder
86de1d91
TT
6885 = abbrev_table::read (objfile, abbrev_section,
6886 cu->header.abbrev_sect_off);
c0ab21c2 6887 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6888 }
af703f96 6889
dee91e82 6890 /* Read the top level CU/TU die. */
c0ab21c2 6891 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6892 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6893
58f0c718 6894 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6895 {
6896 dummy_p = true;
6897 return;
6898 }
58f0c718 6899
b0c7bfa9 6900 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6901 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6902 table from the DWO file and pass the ownership over to us. It will be
6903 referenced from READER, so we must make sure to free it after we're done
6904 with READER.
6905
b0c7bfa9
DE
6906 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6907 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6908 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6909 if (dwo_name != nullptr)
3019eac3 6910 {
3019eac3 6911 struct dwo_unit *dwo_unit;
b0c7bfa9 6912 struct die_info *dwo_comp_unit_die;
3019eac3 6913
3e225074 6914 if (comp_unit_die->has_children)
6a506a2d 6915 {
b98664d3 6916 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6917 " has children (offset %s) [in module %s]"),
6918 sect_offset_str (this_cu->sect_off),
6919 bfd_get_filename (abfd));
6a506a2d 6920 }
c0ab21c2 6921 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6922 if (dwo_unit != NULL)
3019eac3 6923 {
6a506a2d 6924 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6925 comp_unit_die, NULL,
c0ab21c2 6926 this, &info_ptr,
3e225074 6927 &dwo_comp_unit_die,
c0ab21c2 6928 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6929 {
6930 /* Dummy die. */
c0ab21c2 6931 dummy_p = true;
6a506a2d
DE
6932 return;
6933 }
6934 comp_unit_die = dwo_comp_unit_die;
6935 }
6936 else
6937 {
6938 /* Yikes, we couldn't find the rest of the DIE, we only have
6939 the stub. A complaint has already been logged. There's
6940 not much more we can do except pass on the stub DIE to
6941 die_reader_func. We don't want to throw an error on bad
6942 debug info. */
3019eac3
DE
6943 }
6944 }
c0ab21c2 6945}
3019eac3 6946
6751ebae
TT
6947void
6948cutu_reader::keep ()
c0ab21c2 6949{
b0c7bfa9 6950 /* Done, clean up. */
6751ebae
TT
6951 gdb_assert (!dummy_p);
6952 if (m_new_cu != NULL)
348e048f 6953 {
c0ab21c2
TT
6954 struct dwarf2_per_objfile *dwarf2_per_objfile
6955 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6956 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6957 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6958 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6959 /* The chain owns it now. */
c0ab21c2 6960 m_new_cu.release ();
348e048f 6961 }
dee91e82
DE
6962}
6963
18a8505e
AT
6964/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6965 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6966 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6967
6968 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6969 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6970
6971 We fill in THIS_CU->length.
6972
dee91e82 6973 THIS_CU->cu is always freed when done.
3019eac3 6974 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6975 to care whether it refers to the "main" CU or the DWO CU.
6976
6977 When parent_cu is passed, it is used to provide a default value for
6978 str_offsets_base and addr_base from the parent. */
dee91e82 6979
c0ab21c2
TT
6980cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6981 struct dwarf2_cu *parent_cu,
6982 struct dwo_file *dwo_file)
6983 : die_reader_specs {},
6984 m_this_cu (this_cu)
dee91e82 6985{
ed2dc618 6986 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6987 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6988 struct dwarf2_section_info *section = this_cu->section;
96b79293 6989 bfd *abfd = section->get_bfd_owner ();
33e80786 6990 struct dwarf2_section_info *abbrev_section;
d521ce57 6991 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6992
b4f54984 6993 if (dwarf_die_debug)
9d8780f0 6994 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6995 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6996 sect_offset_str (this_cu->sect_off));
09406207 6997
dee91e82
DE
6998 gdb_assert (this_cu->cu == NULL);
6999
33e80786
DE
7000 abbrev_section = (dwo_file != NULL
7001 ? &dwo_file->sections.abbrev
7002 : get_abbrev_section_for_cu (this_cu));
7003
dee91e82 7004 /* This is cheap if the section is already read in. */
96b79293 7005 section->read (objfile);
dee91e82 7006
c0ab21c2 7007 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7008
9c541725 7009 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7010 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7011 &m_new_cu->header, section,
4bdcc0c1 7012 abbrev_section, info_ptr,
43988095
JK
7013 (this_cu->is_debug_types
7014 ? rcuh_kind::TYPE
7015 : rcuh_kind::COMPILE));
dee91e82 7016
18a8505e
AT
7017 if (parent_cu != nullptr)
7018 {
c0ab21c2
TT
7019 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7020 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7021 }
4057dfde 7022 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7023
7024 /* Skip dummy compilation units. */
7025 if (info_ptr >= begin_info_ptr + this_cu->length
7026 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7027 {
7028 dummy_p = true;
7029 return;
7030 }
72bf9492 7031
c0ab21c2 7032 m_abbrev_table_holder
86de1d91
TT
7033 = abbrev_table::read (objfile, abbrev_section,
7034 m_new_cu->header.abbrev_sect_off);
dee91e82 7035
c0ab21c2
TT
7036 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7037 m_abbrev_table_holder.get ());
3e225074 7038 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7039}
7040
0018ea6f
DE
7041\f
7042/* Type Unit Groups.
dee91e82 7043
0018ea6f
DE
7044 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7045 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7046 so that all types coming from the same compilation (.o file) are grouped
7047 together. A future step could be to put the types in the same symtab as
7048 the CU the types ultimately came from. */
ff013f42 7049
f4dc4d17
DE
7050static hashval_t
7051hash_type_unit_group (const void *item)
7052{
9a3c8263
SM
7053 const struct type_unit_group *tu_group
7054 = (const struct type_unit_group *) item;
f4dc4d17 7055
094b34ac 7056 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7057}
348e048f
DE
7058
7059static int
f4dc4d17 7060eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7061{
9a3c8263
SM
7062 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7063 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7064
094b34ac 7065 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7066}
348e048f 7067
f4dc4d17
DE
7068/* Allocate a hash table for type unit groups. */
7069
eaa5fa8b 7070static htab_up
298e9637 7071allocate_type_unit_groups_table ()
f4dc4d17 7072{
eaa5fa8b
TT
7073 return htab_up (htab_create_alloc (3,
7074 hash_type_unit_group,
7075 eq_type_unit_group,
7076 NULL, xcalloc, xfree));
f4dc4d17 7077}
dee91e82 7078
f4dc4d17
DE
7079/* Type units that don't have DW_AT_stmt_list are grouped into their own
7080 partial symtabs. We combine several TUs per psymtab to not let the size
7081 of any one psymtab grow too big. */
7082#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7083#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7084
094b34ac 7085/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7086 Create the type_unit_group object used to hold one or more TUs. */
7087
7088static struct type_unit_group *
094b34ac 7089create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7090{
518817b3
SM
7091 struct dwarf2_per_objfile *dwarf2_per_objfile
7092 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7093 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7094 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7095 struct type_unit_group *tu_group;
f4dc4d17
DE
7096
7097 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7098 struct type_unit_group);
094b34ac 7099 per_cu = &tu_group->per_cu;
518817b3 7100 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7101
094b34ac
DE
7102 if (dwarf2_per_objfile->using_index)
7103 {
7104 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7105 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7106 }
7107 else
7108 {
9c541725 7109 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7110 dwarf2_psymtab *pst;
528e1572 7111 std::string name;
094b34ac
DE
7112
7113 /* Give the symtab a useful name for debug purposes. */
7114 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7115 name = string_printf ("<type_units_%d>",
7116 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7117 else
528e1572 7118 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7119
528e1572 7120 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7121 pst->anonymous = true;
094b34ac 7122 }
f4dc4d17 7123
094b34ac 7124 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7125 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7126
7127 return tu_group;
7128}
7129
094b34ac
DE
7130/* Look up the type_unit_group for type unit CU, and create it if necessary.
7131 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7132
7133static struct type_unit_group *
ff39bb5e 7134get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7135{
518817b3
SM
7136 struct dwarf2_per_objfile *dwarf2_per_objfile
7137 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7138 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7139 struct type_unit_group *tu_group;
7140 void **slot;
7141 unsigned int line_offset;
7142 struct type_unit_group type_unit_group_for_lookup;
7143
7144 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7145 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7146
7147 /* Do we need to create a new group, or can we use an existing one? */
7148
7149 if (stmt_list)
7150 {
7151 line_offset = DW_UNSND (stmt_list);
7152 ++tu_stats->nr_symtab_sharers;
7153 }
7154 else
7155 {
7156 /* Ugh, no stmt_list. Rare, but we have to handle it.
7157 We can do various things here like create one group per TU or
7158 spread them over multiple groups to split up the expansion work.
7159 To avoid worst case scenarios (too many groups or too large groups)
7160 we, umm, group them in bunches. */
7161 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7162 | (tu_stats->nr_stmt_less_type_units
7163 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7164 ++tu_stats->nr_stmt_less_type_units;
7165 }
7166
094b34ac 7167 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7168 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7169 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7170 &type_unit_group_for_lookup, INSERT);
7171 if (*slot != NULL)
7172 {
9a3c8263 7173 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7174 gdb_assert (tu_group != NULL);
7175 }
7176 else
7177 {
9c541725 7178 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7179 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7180 *slot = tu_group;
7181 ++tu_stats->nr_symtabs;
7182 }
7183
7184 return tu_group;
7185}
0018ea6f
DE
7186\f
7187/* Partial symbol tables. */
7188
7189/* Create a psymtab named NAME and assign it to PER_CU.
7190
7191 The caller must fill in the following details:
7192 dirname, textlow, texthigh. */
7193
891813be 7194static dwarf2_psymtab *
0018ea6f
DE
7195create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7196{
e3b94546 7197 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7198 dwarf2_psymtab *pst;
0018ea6f 7199
9f4e76a4 7200 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7201
6d94535f 7202 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7203
7204 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7205 per_cu->v.psymtab = pst;
7206
7207 return pst;
7208}
7209
c0ab21c2 7210/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7211
7212static void
7213process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7214 const gdb_byte *info_ptr,
0018ea6f 7215 struct die_info *comp_unit_die,
c0ab21c2 7216 enum language pretend_language)
0018ea6f
DE
7217{
7218 struct dwarf2_cu *cu = reader->cu;
518817b3 7219 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7220 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7221 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7222 CORE_ADDR baseaddr;
7223 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7224 dwarf2_psymtab *pst;
3a2b436a 7225 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7226 const char *filename;
0018ea6f 7227
0018ea6f
DE
7228 gdb_assert (! per_cu->is_debug_types);
7229
c0ab21c2 7230 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7231
0018ea6f 7232 /* Allocate a new partial symbol table structure. */
2e927613
TV
7233 gdb::unique_xmalloc_ptr<char> debug_filename;
7234 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7235 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7236 if (filename == NULL)
0018ea6f 7237 filename = "";
2e927613
TV
7238 else if (strcmp (filename, artificial) == 0)
7239 {
7240 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7241 sect_offset_str (per_cu->sect_off),
7242 (char *) NULL));
2e927613
TV
7243 filename = debug_filename.get ();
7244 }
0018ea6f
DE
7245
7246 pst = create_partial_symtab (per_cu, filename);
7247
7248 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7249 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7250
b3b3bada 7251 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7252
7253 dwarf2_find_base_address (comp_unit_die, cu);
7254
7255 /* Possibly set the default values of LOWPC and HIGHPC from
7256 `DW_AT_ranges'. */
3a2b436a
JK
7257 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7258 &best_highpc, cu, pst);
7259 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7260 {
7261 CORE_ADDR low
7262 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7263 - baseaddr);
7264 CORE_ADDR high
7265 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7266 - baseaddr - 1);
7267 /* Store the contiguous range if it is not empty; it can be
7268 empty for CUs with no code. */
d320c2b5
TT
7269 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7270 low, high, pst);
79748972 7271 }
0018ea6f
DE
7272
7273 /* Check if comp unit has_children.
7274 If so, read the rest of the partial symbols from this comp unit.
7275 If not, there's no more debug_info for this comp unit. */
3e225074 7276 if (comp_unit_die->has_children)
0018ea6f
DE
7277 {
7278 struct partial_die_info *first_die;
7279 CORE_ADDR lowpc, highpc;
7280
7281 lowpc = ((CORE_ADDR) -1);
7282 highpc = ((CORE_ADDR) 0);
7283
7284 first_die = load_partial_dies (reader, info_ptr, 1);
7285
7286 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7287 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7288
7289 /* If we didn't find a lowpc, set it to highpc to avoid
7290 complaints from `maint check'. */
7291 if (lowpc == ((CORE_ADDR) -1))
7292 lowpc = highpc;
7293
7294 /* If the compilation unit didn't have an explicit address range,
7295 then use the information extracted from its child dies. */
e385593e 7296 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7297 {
7298 best_lowpc = lowpc;
7299 best_highpc = highpc;
7300 }
7301 }
4ae976d1 7302 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7303 best_lowpc + baseaddr)
7304 - baseaddr);
4ae976d1 7305 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7306 best_highpc + baseaddr)
7307 - baseaddr);
0018ea6f 7308
8763cede 7309 end_psymtab_common (objfile, pst);
0018ea6f 7310
ae640021 7311 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7312 {
7313 int i;
ae640021 7314 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7315
7316 /* Fill in 'dependencies' here; we fill in 'users' in a
7317 post-pass. */
7318 pst->number_of_dependencies = len;
a9342b62
TT
7319 pst->dependencies
7320 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7321 for (i = 0; i < len; ++i)
7322 {
7323 pst->dependencies[i]
7324 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7325 }
0018ea6f 7326
ae640021 7327 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7328 }
7329
7330 /* Get the list of files included in the current compilation unit,
7331 and build a psymtab for each of them. */
7332 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7333
b4f54984 7334 if (dwarf_read_debug)
b926417a
TT
7335 fprintf_unfiltered (gdb_stdlog,
7336 "Psymtab for %s unit @%s: %s - %s"
7337 ", %d global, %d static syms\n",
7338 per_cu->is_debug_types ? "type" : "comp",
7339 sect_offset_str (per_cu->sect_off),
7340 paddress (gdbarch, pst->text_low (objfile)),
7341 paddress (gdbarch, pst->text_high (objfile)),
7342 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7343}
7344
7345/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7346 Process compilation unit THIS_CU for a psymtab. */
7347
7348static void
7349process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7350 bool want_partial_unit,
b93601f3 7351 enum language pretend_language)
0018ea6f
DE
7352{
7353 /* If this compilation unit was already read in, free the
7354 cached copy in order to read it in again. This is
7355 necessary because we skipped some symbols when we first
7356 read in the compilation unit (see load_partial_dies).
7357 This problem could be avoided, but the benefit is unclear. */
7358 if (this_cu->cu != NULL)
7359 free_one_cached_comp_unit (this_cu);
7360
6751ebae 7361 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7362
58990295
TV
7363 switch (reader.comp_unit_die->tag)
7364 {
7365 case DW_TAG_compile_unit:
7366 this_cu->unit_type = DW_UT_compile;
7367 break;
7368 case DW_TAG_partial_unit:
7369 this_cu->unit_type = DW_UT_partial;
7370 break;
7371 default:
7372 abort ();
7373 }
7374
c0ab21c2 7375 if (reader.dummy_p)
f1902523 7376 {
c0ab21c2 7377 /* Nothing. */
f1902523 7378 }
c0ab21c2 7379 else if (this_cu->is_debug_types)
3e225074
TT
7380 build_type_psymtabs_reader (&reader, reader.info_ptr,
7381 reader.comp_unit_die);
135f5437
TT
7382 else if (want_partial_unit
7383 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7384 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7385 reader.comp_unit_die,
c0ab21c2 7386 pretend_language);
0018ea6f 7387
58990295
TV
7388 this_cu->lang = this_cu->cu->language;
7389
0018ea6f 7390 /* Age out any secondary CUs. */
ed2dc618 7391 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7392}
f4dc4d17
DE
7393
7394/* Reader function for build_type_psymtabs. */
7395
7396static void
7397build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7398 const gdb_byte *info_ptr,
3e225074 7399 struct die_info *type_unit_die)
f4dc4d17 7400{
ed2dc618 7401 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7402 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7403 struct objfile *objfile = dwarf2_per_objfile->objfile;
7404 struct dwarf2_cu *cu = reader->cu;
7405 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7406 struct signatured_type *sig_type;
f4dc4d17
DE
7407 struct type_unit_group *tu_group;
7408 struct attribute *attr;
7409 struct partial_die_info *first_die;
7410 CORE_ADDR lowpc, highpc;
891813be 7411 dwarf2_psymtab *pst;
f4dc4d17 7412
0186c6a7
DE
7413 gdb_assert (per_cu->is_debug_types);
7414 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7415
3e225074 7416 if (! type_unit_die->has_children)
f4dc4d17
DE
7417 return;
7418
052c8bb8 7419 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7420 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7421
df07e2c7 7422 if (tu_group->tus == nullptr)
a8b3b8e9 7423 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7424 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7425
7426 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7427 pst = create_partial_symtab (per_cu, "");
6d94535f 7428 pst->anonymous = true;
f4dc4d17
DE
7429
7430 first_die = load_partial_dies (reader, info_ptr, 1);
7431
7432 lowpc = (CORE_ADDR) -1;
7433 highpc = (CORE_ADDR) 0;
7434 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7435
8763cede 7436 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7437}
7438
73051182
DE
7439/* Struct used to sort TUs by their abbreviation table offset. */
7440
7441struct tu_abbrev_offset
7442{
b2bdb8cf
SM
7443 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7444 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7445 {}
7446
7447 signatured_type *sig_type;
73051182
DE
7448 sect_offset abbrev_offset;
7449};
7450
484cf504 7451/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7452
484cf504
TT
7453static bool
7454sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7455 const struct tu_abbrev_offset &b)
73051182 7456{
484cf504 7457 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7458}
7459
7460/* Efficiently read all the type units.
7461 This does the bulk of the work for build_type_psymtabs.
7462
7463 The efficiency is because we sort TUs by the abbrev table they use and
7464 only read each abbrev table once. In one program there are 200K TUs
7465 sharing 8K abbrev tables.
7466
7467 The main purpose of this function is to support building the
7468 dwarf2_per_objfile->type_unit_groups table.
7469 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7470 can collapse the search space by grouping them by stmt_list.
7471 The savings can be significant, in the same program from above the 200K TUs
7472 share 8K stmt_list tables.
7473
7474 FUNC is expected to call get_type_unit_group, which will create the
7475 struct type_unit_group if necessary and add it to
7476 dwarf2_per_objfile->type_unit_groups. */
7477
7478static void
ed2dc618 7479build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7480{
73051182 7481 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7482 abbrev_table_up abbrev_table;
73051182 7483 sect_offset abbrev_offset;
73051182
DE
7484
7485 /* It's up to the caller to not call us multiple times. */
7486 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7487
b2bdb8cf 7488 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7489 return;
7490
7491 /* TUs typically share abbrev tables, and there can be way more TUs than
7492 abbrev tables. Sort by abbrev table to reduce the number of times we
7493 read each abbrev table in.
7494 Alternatives are to punt or to maintain a cache of abbrev tables.
7495 This is simpler and efficient enough for now.
7496
7497 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7498 symtab to use). Typically TUs with the same abbrev offset have the same
7499 stmt_list value too so in practice this should work well.
7500
7501 The basic algorithm here is:
7502
7503 sort TUs by abbrev table
7504 for each TU with same abbrev table:
7505 read abbrev table if first user
7506 read TU top level DIE
7507 [IWBN if DWO skeletons had DW_AT_stmt_list]
7508 call FUNC */
7509
b4f54984 7510 if (dwarf_read_debug)
73051182
DE
7511 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7512
7513 /* Sort in a separate table to maintain the order of all_type_units
7514 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7515 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7516 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7517
7518 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7519 sorted_by_abbrev.emplace_back
7520 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7521 sig_type->per_cu.section,
7522 sig_type->per_cu.sect_off));
73051182 7523
484cf504
TT
7524 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7525 sort_tu_by_abbrev_offset);
73051182 7526
9c541725 7527 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7528
b2bdb8cf 7529 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7530 {
73051182
DE
7531 /* Switch to the next abbrev table if necessary. */
7532 if (abbrev_table == NULL
b2bdb8cf 7533 || tu.abbrev_offset != abbrev_offset)
73051182 7534 {
b2bdb8cf 7535 abbrev_offset = tu.abbrev_offset;
73051182 7536 abbrev_table =
86de1d91
TT
7537 abbrev_table::read (dwarf2_per_objfile->objfile,
7538 &dwarf2_per_objfile->abbrev,
7539 abbrev_offset);
73051182
DE
7540 ++tu_stats->nr_uniq_abbrev_tables;
7541 }
7542
c0ab21c2 7543 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7544 0, false);
c0ab21c2
TT
7545 if (!reader.dummy_p)
7546 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7547 reader.comp_unit_die);
73051182 7548 }
6aa5f3a6 7549}
73051182 7550
6aa5f3a6
DE
7551/* Print collected type unit statistics. */
7552
7553static void
ed2dc618 7554print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7555{
7556 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7557
7558 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7559 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7560 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7561 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7562 tu_stats->nr_uniq_abbrev_tables);
7563 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7564 tu_stats->nr_symtabs);
7565 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7566 tu_stats->nr_symtab_sharers);
7567 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7568 tu_stats->nr_stmt_less_type_units);
7569 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7570 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7571}
7572
f4dc4d17
DE
7573/* Traversal function for build_type_psymtabs. */
7574
7575static int
7576build_type_psymtab_dependencies (void **slot, void *info)
7577{
ed2dc618
SM
7578 struct dwarf2_per_objfile *dwarf2_per_objfile
7579 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7580 struct objfile *objfile = dwarf2_per_objfile->objfile;
7581 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7582 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7583 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7584 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7585 int i;
7586
7587 gdb_assert (len > 0);
197400e8 7588 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7589
7590 pst->number_of_dependencies = len;
a9342b62 7591 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7592 for (i = 0; i < len; ++i)
f4dc4d17 7593 {
df07e2c7 7594 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7595 gdb_assert (iter->per_cu.is_debug_types);
7596 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7597 iter->type_unit_group = tu_group;
f4dc4d17
DE
7598 }
7599
df07e2c7
AB
7600 delete tu_group->tus;
7601 tu_group->tus = nullptr;
348e048f
DE
7602
7603 return 1;
7604}
7605
7606/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7607 Build partial symbol tables for the .debug_types comp-units. */
7608
7609static void
ed2dc618 7610build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7611{
ed2dc618 7612 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7613 return;
7614
ed2dc618 7615 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7616}
f4dc4d17 7617
6aa5f3a6
DE
7618/* Traversal function for process_skeletonless_type_unit.
7619 Read a TU in a DWO file and build partial symbols for it. */
7620
7621static int
7622process_skeletonless_type_unit (void **slot, void *info)
7623{
7624 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7625 struct dwarf2_per_objfile *dwarf2_per_objfile
7626 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7627 struct signatured_type find_entry, *entry;
7628
7629 /* If this TU doesn't exist in the global table, add it and read it in. */
7630
7631 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7632 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7633
7634 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7635 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7636 &find_entry, INSERT);
6aa5f3a6
DE
7637 /* If we've already seen this type there's nothing to do. What's happening
7638 is we're doing our own version of comdat-folding here. */
7639 if (*slot != NULL)
7640 return 1;
7641
7642 /* This does the job that create_all_type_units would have done for
7643 this TU. */
ed2dc618
SM
7644 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7645 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7646 *slot = entry;
7647
7648 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7649 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7650 if (!reader.dummy_p)
7651 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7652 reader.comp_unit_die);
6aa5f3a6
DE
7653
7654 return 1;
7655}
7656
7657/* Traversal function for process_skeletonless_type_units. */
7658
7659static int
7660process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7661{
7662 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7663
7664 if (dwo_file->tus != NULL)
b0b6a987
TT
7665 htab_traverse_noresize (dwo_file->tus.get (),
7666 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7667
7668 return 1;
7669}
7670
7671/* Scan all TUs of DWO files, verifying we've processed them.
7672 This is needed in case a TU was emitted without its skeleton.
7673 Note: This can't be done until we know what all the DWO files are. */
7674
7675static void
ed2dc618 7676process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7677{
7678 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7679 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7680 && dwarf2_per_objfile->dwo_files != NULL)
7681 {
51ac9db5 7682 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7683 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7684 dwarf2_per_objfile);
6aa5f3a6 7685 }
348e048f
DE
7686}
7687
ed2dc618 7688/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7689
7690static void
ed2dc618 7691set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7692{
b76e467d 7693 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7694 {
891813be 7695 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7696
36586728
TT
7697 if (pst == NULL)
7698 continue;
7699
b76e467d 7700 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7701 {
7702 /* Set the 'user' field only if it is not already set. */
7703 if (pst->dependencies[j]->user == NULL)
7704 pst->dependencies[j]->user = pst;
7705 }
7706 }
7707}
7708
93311388
DE
7709/* Build the partial symbol table by doing a quick pass through the
7710 .debug_info and .debug_abbrev sections. */
72bf9492 7711
93311388 7712static void
ed2dc618 7713dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7714{
ed2dc618 7715 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7716
b4f54984 7717 if (dwarf_read_debug)
45cfd468
DE
7718 {
7719 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7720 objfile_name (objfile));
45cfd468
DE
7721 }
7722
76935768
TT
7723 scoped_restore restore_reading_psyms
7724 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7725 true);
98bfdba5 7726
96b79293 7727 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7728
93311388
DE
7729 /* Any cached compilation units will be linked by the per-objfile
7730 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7731 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7732
ed2dc618 7733 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7734
ed2dc618 7735 create_all_comp_units (dwarf2_per_objfile);
c906108c 7736
60606b2c
TT
7737 /* Create a temporary address map on a temporary obstack. We later
7738 copy this to the final obstack. */
8268c778 7739 auto_obstack temp_obstack;
791afaa2
TT
7740
7741 scoped_restore save_psymtabs_addrmap
d320c2b5 7742 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7743 addrmap_create_mutable (&temp_obstack));
72bf9492 7744
b76e467d 7745 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7746 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7747
6aa5f3a6 7748 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7749 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7750
7751 /* Now that all TUs have been processed we can fill in the dependencies. */
7752 if (dwarf2_per_objfile->type_unit_groups != NULL)
7753 {
eaa5fa8b 7754 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7755 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7756 }
7757
b4f54984 7758 if (dwarf_read_debug)
ed2dc618 7759 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7760
ed2dc618 7761 set_partial_user (dwarf2_per_objfile);
95554aad 7762
d320c2b5
TT
7763 objfile->partial_symtabs->psymtabs_addrmap
7764 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7765 objfile->partial_symtabs->obstack ());
791afaa2
TT
7766 /* At this point we want to keep the address map. */
7767 save_psymtabs_addrmap.release ();
ff013f42 7768
b4f54984 7769 if (dwarf_read_debug)
45cfd468 7770 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7771 objfile_name (objfile));
ae038cb0
DJ
7772}
7773
dee91e82
DE
7774/* Load the partial DIEs for a secondary CU into memory.
7775 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7776
dee91e82
DE
7777static void
7778load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7779{
6751ebae 7780 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7781
7782 if (!reader.dummy_p)
7783 {
7784 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7785 language_minimal);
7786
7787 /* Check if comp unit has_children.
7788 If so, read the rest of the partial symbols from this comp unit.
7789 If not, there's no more debug_info for this comp unit. */
3e225074 7790 if (reader.comp_unit_die->has_children)
c0ab21c2 7791 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7792
7793 reader.keep ();
c0ab21c2 7794 }
ae038cb0
DJ
7795}
7796
ae038cb0 7797static void
ed2dc618 7798read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7799 struct dwarf2_section_info *section,
f1902523 7800 struct dwarf2_section_info *abbrev_section,
b76e467d 7801 unsigned int is_dwz)
ae038cb0 7802{
d521ce57 7803 const gdb_byte *info_ptr;
ed2dc618 7804 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7805
b4f54984 7806 if (dwarf_read_debug)
bf6af496 7807 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7808 section->get_name (),
7809 section->get_file_name ());
bf6af496 7810
96b79293 7811 section->read (objfile);
ae038cb0 7812
36586728 7813 info_ptr = section->buffer;
6e70227d 7814
36586728 7815 while (info_ptr < section->buffer + section->size)
ae038cb0 7816 {
ae038cb0 7817 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7818
9c541725 7819 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7820
f1902523 7821 comp_unit_head cu_header;
ed2dc618
SM
7822 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7823 abbrev_section, info_ptr,
7824 rcuh_kind::COMPILE);
ae038cb0
DJ
7825
7826 /* Save the compilation unit for later lookup. */
f1902523
JK
7827 if (cu_header.unit_type != DW_UT_type)
7828 {
7829 this_cu = XOBNEW (&objfile->objfile_obstack,
7830 struct dwarf2_per_cu_data);
7831 memset (this_cu, 0, sizeof (*this_cu));
7832 }
7833 else
7834 {
7835 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7836 struct signatured_type);
7837 memset (sig_type, 0, sizeof (*sig_type));
7838 sig_type->signature = cu_header.signature;
7839 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7840 this_cu = &sig_type->per_cu;
7841 }
7842 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7843 this_cu->sect_off = sect_off;
f1902523 7844 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7845 this_cu->is_dwz = is_dwz;
e3b94546 7846 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7847 this_cu->section = section;
ae038cb0 7848
b76e467d 7849 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7850
7851 info_ptr = info_ptr + this_cu->length;
7852 }
36586728
TT
7853}
7854
7855/* Create a list of all compilation units in OBJFILE.
7856 This is only done for -readnow and building partial symtabs. */
7857
7858static void
ed2dc618 7859create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7860{
b76e467d 7861 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7862 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7863 &dwarf2_per_objfile->abbrev, 0);
36586728 7864
b76e467d 7865 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7866 if (dwz != NULL)
ed2dc618 7867 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7868 1);
c906108c
SS
7869}
7870
5734ee8b 7871/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7872 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7873 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7874 DW_AT_ranges). See the comments of add_partial_subprogram on how
7875 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7876
72bf9492
DJ
7877static void
7878scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7879 CORE_ADDR *highpc, int set_addrmap,
7880 struct dwarf2_cu *cu)
c906108c 7881{
72bf9492 7882 struct partial_die_info *pdi;
c906108c 7883
91c24f0a
DC
7884 /* Now, march along the PDI's, descending into ones which have
7885 interesting children but skipping the children of the other ones,
7886 until we reach the end of the compilation unit. */
c906108c 7887
72bf9492 7888 pdi = first_die;
91c24f0a 7889
72bf9492
DJ
7890 while (pdi != NULL)
7891 {
52356b79 7892 pdi->fixup (cu);
c906108c 7893
f55ee35c 7894 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7895 children, so we need to look at them. Ditto for anonymous
7896 enums. */
933c6fe4 7897
72bf9492 7898 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7899 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7900 || pdi->tag == DW_TAG_imported_unit
7901 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7902 {
72bf9492 7903 switch (pdi->tag)
c906108c
SS
7904 {
7905 case DW_TAG_subprogram:
b1dc1806 7906 case DW_TAG_inlined_subroutine:
cdc07690 7907 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7908 break;
72929c62 7909 case DW_TAG_constant:
c906108c
SS
7910 case DW_TAG_variable:
7911 case DW_TAG_typedef:
91c24f0a 7912 case DW_TAG_union_type:
72bf9492 7913 if (!pdi->is_declaration)
63d06c5c 7914 {
72bf9492 7915 add_partial_symbol (pdi, cu);
63d06c5c
DC
7916 }
7917 break;
c906108c 7918 case DW_TAG_class_type:
680b30c7 7919 case DW_TAG_interface_type:
c906108c 7920 case DW_TAG_structure_type:
72bf9492 7921 if (!pdi->is_declaration)
c906108c 7922 {
72bf9492 7923 add_partial_symbol (pdi, cu);
c906108c 7924 }
b7fee5a3
KS
7925 if ((cu->language == language_rust
7926 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7927 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7928 set_addrmap, cu);
c906108c 7929 break;
91c24f0a 7930 case DW_TAG_enumeration_type:
72bf9492
DJ
7931 if (!pdi->is_declaration)
7932 add_partial_enumeration (pdi, cu);
c906108c
SS
7933 break;
7934 case DW_TAG_base_type:
a02abb62 7935 case DW_TAG_subrange_type:
c906108c 7936 /* File scope base type definitions are added to the partial
c5aa993b 7937 symbol table. */
72bf9492 7938 add_partial_symbol (pdi, cu);
c906108c 7939 break;
d9fa45fe 7940 case DW_TAG_namespace:
cdc07690 7941 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7942 break;
5d7cb8df 7943 case DW_TAG_module:
59c35742
AB
7944 if (!pdi->is_declaration)
7945 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7946 break;
95554aad
TT
7947 case DW_TAG_imported_unit:
7948 {
7949 struct dwarf2_per_cu_data *per_cu;
7950
f4dc4d17
DE
7951 /* For now we don't handle imported units in type units. */
7952 if (cu->per_cu->is_debug_types)
7953 {
7954 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7955 " supported in type units [in module %s]"),
518817b3 7956 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7957 }
7958
e3b94546
SM
7959 per_cu = dwarf2_find_containing_comp_unit
7960 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7961 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7962
7963 /* Go read the partial unit, if needed. */
7964 if (per_cu->v.psymtab == NULL)
135f5437 7965 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7966
ae640021 7967 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7968 }
7969 break;
74921315
KS
7970 case DW_TAG_imported_declaration:
7971 add_partial_symbol (pdi, cu);
7972 break;
c906108c
SS
7973 default:
7974 break;
7975 }
7976 }
7977
72bf9492
DJ
7978 /* If the die has a sibling, skip to the sibling. */
7979
7980 pdi = pdi->die_sibling;
7981 }
7982}
7983
7984/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7985
72bf9492 7986 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7987 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7988 Enumerators are an exception; they use the scope of their parent
7989 enumeration type, i.e. the name of the enumeration type is not
7990 prepended to the enumerator.
91c24f0a 7991
72bf9492
DJ
7992 There are two complexities. One is DW_AT_specification; in this
7993 case "parent" means the parent of the target of the specification,
7994 instead of the direct parent of the DIE. The other is compilers
7995 which do not emit DW_TAG_namespace; in this case we try to guess
7996 the fully qualified name of structure types from their members'
7997 linkage names. This must be done using the DIE's children rather
7998 than the children of any DW_AT_specification target. We only need
7999 to do this for structures at the top level, i.e. if the target of
8000 any DW_AT_specification (if any; otherwise the DIE itself) does not
8001 have a parent. */
8002
8003/* Compute the scope prefix associated with PDI's parent, in
8004 compilation unit CU. The result will be allocated on CU's
8005 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8006 field. NULL is returned if no prefix is necessary. */
15d034d0 8007static const char *
72bf9492
DJ
8008partial_die_parent_scope (struct partial_die_info *pdi,
8009 struct dwarf2_cu *cu)
8010{
15d034d0 8011 const char *grandparent_scope;
72bf9492 8012 struct partial_die_info *parent, *real_pdi;
91c24f0a 8013
72bf9492
DJ
8014 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8015 then this means the parent of the specification DIE. */
8016
8017 real_pdi = pdi;
72bf9492 8018 while (real_pdi->has_specification)
fb816e8b 8019 {
122cf0f2
AB
8020 auto res = find_partial_die (real_pdi->spec_offset,
8021 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8022 real_pdi = res.pdi;
8023 cu = res.cu;
8024 }
72bf9492
DJ
8025
8026 parent = real_pdi->die_parent;
8027 if (parent == NULL)
8028 return NULL;
8029
8030 if (parent->scope_set)
8031 return parent->scope;
8032
52356b79 8033 parent->fixup (cu);
72bf9492 8034
10b3939b 8035 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8036
acebe513
UW
8037 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8038 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8039 Work around this problem here. */
8040 if (cu->language == language_cplus
6e70227d 8041 && parent->tag == DW_TAG_namespace
acebe513
UW
8042 && strcmp (parent->name, "::") == 0
8043 && grandparent_scope == NULL)
8044 {
8045 parent->scope = NULL;
8046 parent->scope_set = 1;
8047 return NULL;
8048 }
8049
0a4b0913 8050 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8051 if (pdi->tag == DW_TAG_enumerator)
8052 /* Enumerators should not get the name of the enumeration as a prefix. */
8053 parent->scope = grandparent_scope;
8054 else if (parent->tag == DW_TAG_namespace
f55ee35c 8055 || parent->tag == DW_TAG_module
72bf9492
DJ
8056 || parent->tag == DW_TAG_structure_type
8057 || parent->tag == DW_TAG_class_type
680b30c7 8058 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8059 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8060 || parent->tag == DW_TAG_enumeration_type
8061 || (cu->language == language_fortran
8062 && parent->tag == DW_TAG_subprogram
8063 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8064 {
8065 if (grandparent_scope == NULL)
8066 parent->scope = parent->name;
8067 else
3e43a32a
MS
8068 parent->scope = typename_concat (&cu->comp_unit_obstack,
8069 grandparent_scope,
f55ee35c 8070 parent->name, 0, cu);
72bf9492 8071 }
72bf9492
DJ
8072 else
8073 {
8074 /* FIXME drow/2004-04-01: What should we be doing with
8075 function-local names? For partial symbols, we should probably be
8076 ignoring them. */
fa9c3fa0
TT
8077 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8078 dwarf_tag_name (parent->tag),
8079 sect_offset_str (pdi->sect_off));
72bf9492 8080 parent->scope = grandparent_scope;
c906108c
SS
8081 }
8082
72bf9492
DJ
8083 parent->scope_set = 1;
8084 return parent->scope;
8085}
8086
8087/* Return the fully scoped name associated with PDI, from compilation unit
8088 CU. The result will be allocated with malloc. */
4568ecf9 8089
43816ebc 8090static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8091partial_die_full_name (struct partial_die_info *pdi,
8092 struct dwarf2_cu *cu)
8093{
15d034d0 8094 const char *parent_scope;
72bf9492 8095
98bfdba5
PA
8096 /* If this is a template instantiation, we can not work out the
8097 template arguments from partial DIEs. So, unfortunately, we have
8098 to go through the full DIEs. At least any work we do building
8099 types here will be reused if full symbols are loaded later. */
8100 if (pdi->has_template_arguments)
8101 {
52356b79 8102 pdi->fixup (cu);
98bfdba5
PA
8103
8104 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8105 {
8106 struct die_info *die;
8107 struct attribute attr;
8108 struct dwarf2_cu *ref_cu = cu;
8109
b64f50a1 8110 /* DW_FORM_ref_addr is using section offset. */
b4069958 8111 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8112 attr.form = DW_FORM_ref_addr;
9c541725 8113 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8114 die = follow_die_ref (NULL, &attr, &ref_cu);
8115
43816ebc 8116 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8117 }
8118 }
8119
72bf9492
DJ
8120 parent_scope = partial_die_parent_scope (pdi, cu);
8121 if (parent_scope == NULL)
8122 return NULL;
8123 else
43816ebc
TT
8124 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8125 pdi->name, 0, cu));
c906108c
SS
8126}
8127
8128static void
72bf9492 8129add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8130{
518817b3
SM
8131 struct dwarf2_per_objfile *dwarf2_per_objfile
8132 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8133 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8134 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8135 CORE_ADDR addr = 0;
15d034d0 8136 const char *actual_name = NULL;
e142c38c
DJ
8137 CORE_ADDR baseaddr;
8138
b3b3bada 8139 baseaddr = objfile->text_section_offset ();
c906108c 8140
43816ebc
TT
8141 gdb::unique_xmalloc_ptr<char> built_actual_name
8142 = partial_die_full_name (pdi, cu);
15d034d0 8143 if (built_actual_name != NULL)
43816ebc 8144 actual_name = built_actual_name.get ();
63d06c5c 8145
72bf9492
DJ
8146 if (actual_name == NULL)
8147 actual_name = pdi->name;
8148
c906108c
SS
8149 switch (pdi->tag)
8150 {
b1dc1806 8151 case DW_TAG_inlined_subroutine:
c906108c 8152 case DW_TAG_subprogram:
79748972
TT
8153 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8154 - baseaddr);
0a4b0913
AB
8155 if (pdi->is_external
8156 || cu->language == language_ada
8157 || (cu->language == language_fortran
8158 && pdi->die_parent != NULL
8159 && pdi->die_parent->tag == DW_TAG_subprogram))
8160 {
8161 /* Normally, only "external" DIEs are part of the global scope.
8162 But in Ada and Fortran, we want to be able to access nested
8163 procedures globally. So all Ada and Fortran subprograms are
8164 stored in the global scope. */
31edb802 8165 add_psymbol_to_list (actual_name,
15d034d0 8166 built_actual_name != NULL,
f47fb265 8167 VAR_DOMAIN, LOC_BLOCK,
79748972 8168 SECT_OFF_TEXT (objfile),
75aedd27 8169 psymbol_placement::GLOBAL,
79748972
TT
8170 addr,
8171 cu->language, objfile);
c906108c
SS
8172 }
8173 else
8174 {
31edb802 8175 add_psymbol_to_list (actual_name,
15d034d0 8176 built_actual_name != NULL,
f47fb265 8177 VAR_DOMAIN, LOC_BLOCK,
79748972 8178 SECT_OFF_TEXT (objfile),
75aedd27 8179 psymbol_placement::STATIC,
1762568f 8180 addr, cu->language, objfile);
c906108c 8181 }
0c1b455e
TT
8182
8183 if (pdi->main_subprogram && actual_name != NULL)
8184 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8185 break;
72929c62 8186 case DW_TAG_constant:
31edb802 8187 add_psymbol_to_list (actual_name,
75aedd27
TT
8188 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8189 -1, (pdi->is_external
8190 ? psymbol_placement::GLOBAL
8191 : psymbol_placement::STATIC),
8192 0, cu->language, objfile);
72929c62 8193 break;
c906108c 8194 case DW_TAG_variable:
95554aad
TT
8195 if (pdi->d.locdesc)
8196 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8197
95554aad 8198 if (pdi->d.locdesc
caac4577
JG
8199 && addr == 0
8200 && !dwarf2_per_objfile->has_section_at_zero)
8201 {
8202 /* A global or static variable may also have been stripped
8203 out by the linker if unused, in which case its address
8204 will be nullified; do not add such variables into partial
8205 symbol table then. */
8206 }
8207 else if (pdi->is_external)
c906108c
SS
8208 {
8209 /* Global Variable.
8210 Don't enter into the minimal symbol tables as there is
8211 a minimal symbol table entry from the ELF symbols already.
8212 Enter into partial symbol table if it has a location
8213 descriptor or a type.
8214 If the location descriptor is missing, new_symbol will create
8215 a LOC_UNRESOLVED symbol, the address of the variable will then
8216 be determined from the minimal symbol table whenever the variable
8217 is referenced.
8218 The address for the partial symbol table entry is not
8219 used by GDB, but it comes in handy for debugging partial symbol
8220 table building. */
8221
95554aad 8222 if (pdi->d.locdesc || pdi->has_type)
31edb802 8223 add_psymbol_to_list (actual_name,
15d034d0 8224 built_actual_name != NULL,
f47fb265 8225 VAR_DOMAIN, LOC_STATIC,
79748972 8226 SECT_OFF_TEXT (objfile),
75aedd27 8227 psymbol_placement::GLOBAL,
79748972 8228 addr, cu->language, objfile);
c906108c
SS
8229 }
8230 else
8231 {
ff908ebf
AW
8232 int has_loc = pdi->d.locdesc != NULL;
8233
8234 /* Static Variable. Skip symbols whose value we cannot know (those
8235 without location descriptors or constant values). */
8236 if (!has_loc && !pdi->has_const_value)
43816ebc 8237 return;
ff908ebf 8238
31edb802 8239 add_psymbol_to_list (actual_name,
15d034d0 8240 built_actual_name != NULL,
f47fb265 8241 VAR_DOMAIN, LOC_STATIC,
79748972 8242 SECT_OFF_TEXT (objfile),
75aedd27 8243 psymbol_placement::STATIC,
79748972 8244 has_loc ? addr : 0,
f47fb265 8245 cu->language, objfile);
c906108c
SS
8246 }
8247 break;
8248 case DW_TAG_typedef:
8249 case DW_TAG_base_type:
a02abb62 8250 case DW_TAG_subrange_type:
31edb802 8251 add_psymbol_to_list (actual_name,
15d034d0 8252 built_actual_name != NULL,
79748972 8253 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8254 psymbol_placement::STATIC,
1762568f 8255 0, cu->language, objfile);
c906108c 8256 break;
74921315 8257 case DW_TAG_imported_declaration:
72bf9492 8258 case DW_TAG_namespace:
31edb802 8259 add_psymbol_to_list (actual_name,
15d034d0 8260 built_actual_name != NULL,
79748972 8261 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8262 psymbol_placement::GLOBAL,
1762568f 8263 0, cu->language, objfile);
72bf9492 8264 break;
530e8392 8265 case DW_TAG_module:
a5fd13a9
BH
8266 /* With Fortran 77 there might be a "BLOCK DATA" module
8267 available without any name. If so, we skip the module as it
8268 doesn't bring any value. */
8269 if (actual_name != nullptr)
31edb802 8270 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8271 built_actual_name != NULL,
8272 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8273 psymbol_placement::GLOBAL,
8274 0, cu->language, objfile);
530e8392 8275 break;
c906108c 8276 case DW_TAG_class_type:
680b30c7 8277 case DW_TAG_interface_type:
c906108c
SS
8278 case DW_TAG_structure_type:
8279 case DW_TAG_union_type:
8280 case DW_TAG_enumeration_type:
fa4028e9
JB
8281 /* Skip external references. The DWARF standard says in the section
8282 about "Structure, Union, and Class Type Entries": "An incomplete
8283 structure, union or class type is represented by a structure,
8284 union or class entry that does not have a byte size attribute
8285 and that has a DW_AT_declaration attribute." */
8286 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8287 return;
fa4028e9 8288
63d06c5c
DC
8289 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8290 static vs. global. */
31edb802 8291 add_psymbol_to_list (actual_name,
15d034d0 8292 built_actual_name != NULL,
79748972 8293 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8294 cu->language == language_cplus
75aedd27
TT
8295 ? psymbol_placement::GLOBAL
8296 : psymbol_placement::STATIC,
1762568f 8297 0, cu->language, objfile);
c906108c 8298
c906108c
SS
8299 break;
8300 case DW_TAG_enumerator:
31edb802 8301 add_psymbol_to_list (actual_name,
15d034d0 8302 built_actual_name != NULL,
79748972 8303 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8304 cu->language == language_cplus
75aedd27
TT
8305 ? psymbol_placement::GLOBAL
8306 : psymbol_placement::STATIC,
1762568f 8307 0, cu->language, objfile);
c906108c
SS
8308 break;
8309 default:
8310 break;
8311 }
8312}
8313
5c4e30ca
DC
8314/* Read a partial die corresponding to a namespace; also, add a symbol
8315 corresponding to that namespace to the symbol table. NAMESPACE is
8316 the name of the enclosing namespace. */
91c24f0a 8317
72bf9492
DJ
8318static void
8319add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8320 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8321 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8322{
72bf9492 8323 /* Add a symbol for the namespace. */
e7c27a73 8324
72bf9492 8325 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8326
8327 /* Now scan partial symbols in that namespace. */
8328
91c24f0a 8329 if (pdi->has_children)
cdc07690 8330 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8331}
8332
5d7cb8df
JK
8333/* Read a partial die corresponding to a Fortran module. */
8334
8335static void
8336add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8337 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8338{
530e8392
KB
8339 /* Add a symbol for the namespace. */
8340
8341 add_partial_symbol (pdi, cu);
8342
f55ee35c 8343 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8344
8345 if (pdi->has_children)
cdc07690 8346 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8347}
8348
b1dc1806
XR
8349/* Read a partial die corresponding to a subprogram or an inlined
8350 subprogram and create a partial symbol for that subprogram.
8351 When the CU language allows it, this routine also defines a partial
8352 symbol for each nested subprogram that this subprogram contains.
8353 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8354 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8355
cdc07690
YQ
8356 PDI may also be a lexical block, in which case we simply search
8357 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8358 Again, this is only performed when the CU language allows this
8359 type of definitions. */
8360
8361static void
8362add_partial_subprogram (struct partial_die_info *pdi,
8363 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8364 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8365{
b1dc1806 8366 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8367 {
8368 if (pdi->has_pc_info)
8369 {
8370 if (pdi->lowpc < *lowpc)
8371 *lowpc = pdi->lowpc;
8372 if (pdi->highpc > *highpc)
8373 *highpc = pdi->highpc;
cdc07690 8374 if (set_addrmap)
5734ee8b 8375 {
518817b3 8376 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8377 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8378 CORE_ADDR baseaddr;
b926417a
TT
8379 CORE_ADDR this_highpc;
8380 CORE_ADDR this_lowpc;
5734ee8b 8381
b3b3bada 8382 baseaddr = objfile->text_section_offset ();
b926417a
TT
8383 this_lowpc
8384 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8385 pdi->lowpc + baseaddr)
8386 - baseaddr);
8387 this_highpc
8388 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8389 pdi->highpc + baseaddr)
8390 - baseaddr);
d320c2b5 8391 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8392 this_lowpc, this_highpc - 1,
9291a0cd 8393 cu->per_cu->v.psymtab);
5734ee8b 8394 }
481860b3
GB
8395 }
8396
8397 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8398 {
bc30ff58 8399 if (!pdi->is_declaration)
e8d05480
JB
8400 /* Ignore subprogram DIEs that do not have a name, they are
8401 illegal. Do not emit a complaint at this point, we will
8402 do so when we convert this psymtab into a symtab. */
8403 if (pdi->name)
8404 add_partial_symbol (pdi, cu);
bc30ff58
JB
8405 }
8406 }
6e70227d 8407
bc30ff58
JB
8408 if (! pdi->has_children)
8409 return;
8410
0a4b0913 8411 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8412 {
8413 pdi = pdi->die_child;
8414 while (pdi != NULL)
8415 {
52356b79 8416 pdi->fixup (cu);
bc30ff58 8417 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8418 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8419 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8420 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8421 pdi = pdi->die_sibling;
8422 }
8423 }
8424}
8425
91c24f0a
DC
8426/* Read a partial die corresponding to an enumeration type. */
8427
72bf9492
DJ
8428static void
8429add_partial_enumeration (struct partial_die_info *enum_pdi,
8430 struct dwarf2_cu *cu)
91c24f0a 8431{
72bf9492 8432 struct partial_die_info *pdi;
91c24f0a
DC
8433
8434 if (enum_pdi->name != NULL)
72bf9492
DJ
8435 add_partial_symbol (enum_pdi, cu);
8436
8437 pdi = enum_pdi->die_child;
8438 while (pdi)
91c24f0a 8439 {
72bf9492 8440 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8441 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8442 else
72bf9492
DJ
8443 add_partial_symbol (pdi, cu);
8444 pdi = pdi->die_sibling;
91c24f0a 8445 }
91c24f0a
DC
8446}
8447
6caca83c
CC
8448/* Return the initial uleb128 in the die at INFO_PTR. */
8449
8450static unsigned int
d521ce57 8451peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8452{
8453 unsigned int bytes_read;
8454
8455 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8456}
8457
685af9cd
TT
8458/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8459 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8460
4bb7a0a7
DJ
8461 Return the corresponding abbrev, or NULL if the number is zero (indicating
8462 an empty DIE). In either case *BYTES_READ will be set to the length of
8463 the initial number. */
8464
8465static struct abbrev_info *
685af9cd
TT
8466peek_die_abbrev (const die_reader_specs &reader,
8467 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8468{
685af9cd 8469 dwarf2_cu *cu = reader.cu;
518817b3 8470 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8471 unsigned int abbrev_number
8472 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8473
8474 if (abbrev_number == 0)
8475 return NULL;
8476
685af9cd 8477 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8478 if (!abbrev)
8479 {
422b9917 8480 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8481 " at offset %s [in module %s]"),
422b9917 8482 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8483 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8484 }
8485
8486 return abbrev;
8487}
8488
93311388
DE
8489/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8490 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8491 DIE. Any children of the skipped DIEs will also be skipped. */
8492
d521ce57
TT
8493static const gdb_byte *
8494skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8495{
4bb7a0a7
DJ
8496 while (1)
8497 {
685af9cd
TT
8498 unsigned int bytes_read;
8499 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8500
4bb7a0a7
DJ
8501 if (abbrev == NULL)
8502 return info_ptr + bytes_read;
8503 else
dee91e82 8504 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8505 }
8506}
8507
93311388
DE
8508/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8509 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8510 abbrev corresponding to that skipped uleb128 should be passed in
8511 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8512 children. */
8513
d521ce57
TT
8514static const gdb_byte *
8515skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8516 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8517{
8518 unsigned int bytes_read;
8519 struct attribute attr;
dee91e82
DE
8520 bfd *abfd = reader->abfd;
8521 struct dwarf2_cu *cu = reader->cu;
d521ce57 8522 const gdb_byte *buffer = reader->buffer;
f664829e 8523 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8524 unsigned int form, i;
8525
8526 for (i = 0; i < abbrev->num_attrs; i++)
8527 {
8528 /* The only abbrev we care about is DW_AT_sibling. */
8529 if (abbrev->attrs[i].name == DW_AT_sibling)
8530 {
18a8505e
AT
8531 bool ignored;
8532 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8533 &ignored);
4bb7a0a7 8534 if (attr.form == DW_FORM_ref_addr)
b98664d3 8535 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8536 else
b9502d3f 8537 {
0826b30a 8538 sect_offset off = attr.get_ref_die_offset ();
9c541725 8539 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8540
8541 if (sibling_ptr < info_ptr)
b98664d3 8542 complaint (_("DW_AT_sibling points backwards"));
22869d73 8543 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8544 reader->die_section->overflow_complaint ();
b9502d3f
WN
8545 else
8546 return sibling_ptr;
8547 }
4bb7a0a7
DJ
8548 }
8549
8550 /* If it isn't DW_AT_sibling, skip this attribute. */
8551 form = abbrev->attrs[i].form;
8552 skip_attribute:
8553 switch (form)
8554 {
4bb7a0a7 8555 case DW_FORM_ref_addr:
ae411497
TT
8556 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8557 and later it is offset sized. */
8558 if (cu->header.version == 2)
8559 info_ptr += cu->header.addr_size;
8560 else
8561 info_ptr += cu->header.offset_size;
8562 break;
36586728
TT
8563 case DW_FORM_GNU_ref_alt:
8564 info_ptr += cu->header.offset_size;
8565 break;
ae411497 8566 case DW_FORM_addr:
4bb7a0a7
DJ
8567 info_ptr += cu->header.addr_size;
8568 break;
8569 case DW_FORM_data1:
8570 case DW_FORM_ref1:
8571 case DW_FORM_flag:
8fe0f950 8572 case DW_FORM_strx1:
4bb7a0a7
DJ
8573 info_ptr += 1;
8574 break;
2dc7f7b3 8575 case DW_FORM_flag_present:
43988095 8576 case DW_FORM_implicit_const:
2dc7f7b3 8577 break;
4bb7a0a7
DJ
8578 case DW_FORM_data2:
8579 case DW_FORM_ref2:
8fe0f950 8580 case DW_FORM_strx2:
4bb7a0a7
DJ
8581 info_ptr += 2;
8582 break;
8fe0f950
AT
8583 case DW_FORM_strx3:
8584 info_ptr += 3;
8585 break;
4bb7a0a7
DJ
8586 case DW_FORM_data4:
8587 case DW_FORM_ref4:
8fe0f950 8588 case DW_FORM_strx4:
4bb7a0a7
DJ
8589 info_ptr += 4;
8590 break;
8591 case DW_FORM_data8:
8592 case DW_FORM_ref8:
55f1336d 8593 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8594 info_ptr += 8;
8595 break;
0224619f
JK
8596 case DW_FORM_data16:
8597 info_ptr += 16;
8598 break;
4bb7a0a7 8599 case DW_FORM_string:
9b1c24c8 8600 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8601 info_ptr += bytes_read;
8602 break;
2dc7f7b3 8603 case DW_FORM_sec_offset:
4bb7a0a7 8604 case DW_FORM_strp:
36586728 8605 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8606 info_ptr += cu->header.offset_size;
8607 break;
2dc7f7b3 8608 case DW_FORM_exprloc:
4bb7a0a7
DJ
8609 case DW_FORM_block:
8610 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8611 info_ptr += bytes_read;
8612 break;
8613 case DW_FORM_block1:
8614 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8615 break;
8616 case DW_FORM_block2:
8617 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8618 break;
8619 case DW_FORM_block4:
8620 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8621 break;
336d760d 8622 case DW_FORM_addrx:
cf532bd1 8623 case DW_FORM_strx:
4bb7a0a7
DJ
8624 case DW_FORM_sdata:
8625 case DW_FORM_udata:
8626 case DW_FORM_ref_udata:
3019eac3
DE
8627 case DW_FORM_GNU_addr_index:
8628 case DW_FORM_GNU_str_index:
18a8505e 8629 case DW_FORM_rnglistx:
41144253 8630 case DW_FORM_loclistx:
d521ce57 8631 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8632 break;
8633 case DW_FORM_indirect:
8634 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8635 info_ptr += bytes_read;
8636 /* We need to continue parsing from here, so just go back to
8637 the top. */
8638 goto skip_attribute;
8639
8640 default:
3e43a32a
MS
8641 error (_("Dwarf Error: Cannot handle %s "
8642 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8643 dwarf_form_name (form),
8644 bfd_get_filename (abfd));
8645 }
8646 }
8647
8648 if (abbrev->has_children)
dee91e82 8649 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8650 else
8651 return info_ptr;
8652}
8653
93311388 8654/* Locate ORIG_PDI's sibling.
dee91e82 8655 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8656
d521ce57 8657static const gdb_byte *
dee91e82
DE
8658locate_pdi_sibling (const struct die_reader_specs *reader,
8659 struct partial_die_info *orig_pdi,
d521ce57 8660 const gdb_byte *info_ptr)
91c24f0a
DC
8661{
8662 /* Do we know the sibling already? */
72bf9492 8663
91c24f0a
DC
8664 if (orig_pdi->sibling)
8665 return orig_pdi->sibling;
8666
8667 /* Are there any children to deal with? */
8668
8669 if (!orig_pdi->has_children)
8670 return info_ptr;
8671
4bb7a0a7 8672 /* Skip the children the long way. */
91c24f0a 8673
dee91e82 8674 return skip_children (reader, info_ptr);
91c24f0a
DC
8675}
8676
257e7a09 8677/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8678 not NULL. */
c906108c 8679
891813be
TT
8680void
8681dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8682{
ed2dc618
SM
8683 struct dwarf2_per_objfile *dwarf2_per_objfile
8684 = get_dwarf2_per_objfile (objfile);
8685
077cbab2
TT
8686 gdb_assert (!readin);
8687 /* If this psymtab is constructed from a debug-only objfile, the
8688 has_section_at_zero flag will not necessarily be correct. We
8689 can get the correct value for this flag by looking at the data
8690 associated with the (presumably stripped) associated objfile. */
8691 if (objfile->separate_debug_objfile_backlink)
c906108c 8692 {
077cbab2
TT
8693 struct dwarf2_per_objfile *dpo_backlink
8694 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8695
077cbab2
TT
8696 dwarf2_per_objfile->has_section_at_zero
8697 = dpo_backlink->has_section_at_zero;
8698 }
98bfdba5 8699
8566b89b 8700 expand_psymtab (objfile);
95554aad 8701
ed2dc618 8702 process_cu_includes (dwarf2_per_objfile);
c906108c 8703}
9cdd5dbd
DE
8704\f
8705/* Reading in full CUs. */
c906108c 8706
10b3939b
DJ
8707/* Add PER_CU to the queue. */
8708
8709static void
95554aad
TT
8710queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8711 enum language pretend_language)
10b3939b 8712{
10b3939b 8713 per_cu->queued = 1;
39856def 8714 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8715}
8716
89e63ee4
DE
8717/* If PER_CU is not yet queued, add it to the queue.
8718 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8719 dependency.
0907af0c 8720 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8721 meaning either PER_CU is already queued or it is already loaded.
8722
8723 N.B. There is an invariant here that if a CU is queued then it is loaded.
8724 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8725
8726static int
89e63ee4 8727maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8728 struct dwarf2_per_cu_data *per_cu,
8729 enum language pretend_language)
8730{
8731 /* We may arrive here during partial symbol reading, if we need full
8732 DIEs to process an unusual case (e.g. template arguments). Do
8733 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8734 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8735 {
8736 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8737 return 1;
8738 return 0;
8739 }
8740
8741 /* Mark the dependence relation so that we don't flush PER_CU
8742 too early. */
89e63ee4
DE
8743 if (dependent_cu != NULL)
8744 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8745
8746 /* If it's already on the queue, we have nothing to do. */
8747 if (per_cu->queued)
8748 return 0;
8749
8750 /* If the compilation unit is already loaded, just mark it as
8751 used. */
8752 if (per_cu->cu != NULL)
8753 {
8754 per_cu->cu->last_used = 0;
8755 return 0;
8756 }
8757
8758 /* Add it to the queue. */
8759 queue_comp_unit (per_cu, pretend_language);
8760
8761 return 1;
8762}
8763
10b3939b
DJ
8764/* Process the queue. */
8765
8766static void
ed2dc618 8767process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8768{
b4f54984 8769 if (dwarf_read_debug)
45cfd468
DE
8770 {
8771 fprintf_unfiltered (gdb_stdlog,
8772 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8773 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8774 }
8775
03dd20cc
DJ
8776 /* The queue starts out with one item, but following a DIE reference
8777 may load a new CU, adding it to the end of the queue. */
39856def 8778 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8779 {
39856def
TT
8780 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8781
cc12ce38 8782 if ((dwarf2_per_objfile->using_index
39856def
TT
8783 ? !item.per_cu->v.quick->compunit_symtab
8784 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8785 /* Skip dummy CUs. */
39856def 8786 && item.per_cu->cu != NULL)
f4dc4d17 8787 {
39856def 8788 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8789 unsigned int debug_print_threshold;
247f5c4f 8790 char buf[100];
f4dc4d17 8791
247f5c4f 8792 if (per_cu->is_debug_types)
f4dc4d17 8793 {
247f5c4f
DE
8794 struct signatured_type *sig_type =
8795 (struct signatured_type *) per_cu;
8796
9d8780f0 8797 sprintf (buf, "TU %s at offset %s",
73be47f5 8798 hex_string (sig_type->signature),
9d8780f0 8799 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8800 /* There can be 100s of TUs.
8801 Only print them in verbose mode. */
8802 debug_print_threshold = 2;
f4dc4d17 8803 }
247f5c4f 8804 else
73be47f5 8805 {
9d8780f0
SM
8806 sprintf (buf, "CU at offset %s",
8807 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8808 debug_print_threshold = 1;
8809 }
247f5c4f 8810
b4f54984 8811 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8812 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8813
8814 if (per_cu->is_debug_types)
39856def 8815 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8816 else
39856def 8817 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8818
b4f54984 8819 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8820 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8821 }
10b3939b 8822
39856def
TT
8823 item.per_cu->queued = 0;
8824 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8825 }
8826
b4f54984 8827 if (dwarf_read_debug)
45cfd468
DE
8828 {
8829 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8830 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8831 }
10b3939b
DJ
8832}
8833
10b3939b
DJ
8834/* Read in full symbols for PST, and anything it depends on. */
8835
8566b89b
TT
8836void
8837dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8838{
8566b89b 8839 if (readin)
95554aad
TT
8840 return;
8841
48993951 8842 expand_dependencies (objfile);
aaa75496 8843
b83470bf
TT
8844 dw2_do_instantiate_symtab (per_cu_data, false);
8845 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8846}
8847
dee91e82
DE
8848/* Trivial hash function for die_info: the hash value of a DIE
8849 is its offset in .debug_info for this objfile. */
10b3939b 8850
dee91e82
DE
8851static hashval_t
8852die_hash (const void *item)
10b3939b 8853{
9a3c8263 8854 const struct die_info *die = (const struct die_info *) item;
6502dd73 8855
9c541725 8856 return to_underlying (die->sect_off);
dee91e82 8857}
63d06c5c 8858
dee91e82
DE
8859/* Trivial comparison function for die_info structures: two DIEs
8860 are equal if they have the same offset. */
98bfdba5 8861
dee91e82
DE
8862static int
8863die_eq (const void *item_lhs, const void *item_rhs)
8864{
9a3c8263
SM
8865 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8866 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8867
9c541725 8868 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8869}
c906108c 8870
c0ab21c2 8871/* Load the DIEs associated with PER_CU into memory. */
c906108c 8872
dee91e82 8873static void
c0ab21c2
TT
8874load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8875 bool skip_partial,
8876 enum language pretend_language)
dee91e82 8877{
c0ab21c2
TT
8878 gdb_assert (! this_cu->is_debug_types);
8879
6751ebae 8880 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8881 if (reader.dummy_p)
8882 return;
8883
8884 struct dwarf2_cu *cu = reader.cu;
8885 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8886
dee91e82
DE
8887 gdb_assert (cu->die_hash == NULL);
8888 cu->die_hash =
8889 htab_create_alloc_ex (cu->header.length / 12,
8890 die_hash,
8891 die_eq,
8892 NULL,
8893 &cu->comp_unit_obstack,
8894 hashtab_obstack_allocate,
8895 dummy_obstack_deallocate);
e142c38c 8896
3e225074 8897 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8898 reader.comp_unit_die->child
8899 = read_die_and_siblings (&reader, reader.info_ptr,
8900 &info_ptr, reader.comp_unit_die);
8901 cu->dies = reader.comp_unit_die;
dee91e82 8902 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8903
8904 /* We try not to read any attributes in this function, because not
9cdd5dbd 8905 all CUs needed for references have been loaded yet, and symbol
10b3939b 8906 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8907 or we won't be able to build types correctly.
8908 Similarly, if we do not read the producer, we can not apply
8909 producer-specific interpretation. */
c0ab21c2 8910 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8911
8912 reader.keep ();
10b3939b
DJ
8913}
8914
3da10d80
KS
8915/* Add a DIE to the delayed physname list. */
8916
8917static void
8918add_to_method_list (struct type *type, int fnfield_index, int index,
8919 const char *name, struct die_info *die,
8920 struct dwarf2_cu *cu)
8921{
8922 struct delayed_method_info mi;
8923 mi.type = type;
8924 mi.fnfield_index = fnfield_index;
8925 mi.index = index;
8926 mi.name = name;
8927 mi.die = die;
c89b44cd 8928 cu->method_list.push_back (mi);
3da10d80
KS
8929}
8930
3693fdb3
PA
8931/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8932 "const" / "volatile". If so, decrements LEN by the length of the
8933 modifier and return true. Otherwise return false. */
8934
8935template<size_t N>
8936static bool
8937check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8938{
8939 size_t mod_len = sizeof (mod) - 1;
8940 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8941 {
8942 len -= mod_len;
8943 return true;
8944 }
8945 return false;
8946}
8947
3da10d80
KS
8948/* Compute the physnames of any methods on the CU's method list.
8949
8950 The computation of method physnames is delayed in order to avoid the
8951 (bad) condition that one of the method's formal parameters is of an as yet
8952 incomplete type. */
8953
8954static void
8955compute_delayed_physnames (struct dwarf2_cu *cu)
8956{
3693fdb3 8957 /* Only C++ delays computing physnames. */
c89b44cd 8958 if (cu->method_list.empty ())
3693fdb3
PA
8959 return;
8960 gdb_assert (cu->language == language_cplus);
8961
52941706 8962 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8963 {
1d06ead6 8964 const char *physname;
3da10d80 8965 struct fn_fieldlist *fn_flp
c89b44cd
TT
8966 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8967 physname = dwarf2_physname (mi.name, mi.die, cu);
8968 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8969 = physname ? physname : "";
3693fdb3
PA
8970
8971 /* Since there's no tag to indicate whether a method is a
8972 const/volatile overload, extract that information out of the
8973 demangled name. */
8974 if (physname != NULL)
8975 {
8976 size_t len = strlen (physname);
8977
8978 while (1)
8979 {
8980 if (physname[len] == ')') /* shortcut */
8981 break;
8982 else if (check_modifier (physname, len, " const"))
c89b44cd 8983 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8984 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8985 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8986 else
8987 break;
8988 }
8989 }
3da10d80 8990 }
c89b44cd
TT
8991
8992 /* The list is no longer needed. */
8993 cu->method_list.clear ();
3da10d80
KS
8994}
8995
a766d390
DE
8996/* Go objects should be embedded in a DW_TAG_module DIE,
8997 and it's not clear if/how imported objects will appear.
8998 To keep Go support simple until that's worked out,
8999 go back through what we've read and create something usable.
9000 We could do this while processing each DIE, and feels kinda cleaner,
9001 but that way is more invasive.
9002 This is to, for example, allow the user to type "p var" or "b main"
9003 without having to specify the package name, and allow lookups
9004 of module.object to work in contexts that use the expression
9005 parser. */
9006
9007static void
9008fixup_go_packaging (struct dwarf2_cu *cu)
9009{
421d1616 9010 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9011 struct pending *list;
9012 int i;
9013
c24bdb02 9014 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9015 list != NULL;
9016 list = list->next)
a766d390
DE
9017 {
9018 for (i = 0; i < list->nsyms; ++i)
9019 {
9020 struct symbol *sym = list->symbol[i];
9021
c1b5c1eb 9022 if (sym->language () == language_go
a766d390
DE
9023 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9024 {
421d1616
TT
9025 gdb::unique_xmalloc_ptr<char> this_package_name
9026 (go_symbol_package_name (sym));
a766d390
DE
9027
9028 if (this_package_name == NULL)
9029 continue;
9030 if (package_name == NULL)
421d1616 9031 package_name = std::move (this_package_name);
a766d390
DE
9032 else
9033 {
518817b3
SM
9034 struct objfile *objfile
9035 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9036 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9037 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9038 (symbol_symtab (sym) != NULL
9039 ? symtab_to_filename_for_display
9040 (symbol_symtab (sym))
e3b94546 9041 : objfile_name (objfile)),
421d1616 9042 this_package_name.get (), package_name.get ());
a766d390
DE
9043 }
9044 }
9045 }
9046 }
9047
9048 if (package_name != NULL)
9049 {
518817b3 9050 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9051 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9052 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9053 saved_package_name);
a766d390
DE
9054 struct symbol *sym;
9055
e623cf5d 9056 sym = allocate_symbol (objfile);
d3ecddab 9057 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9058 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9059 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9060 e.g., "main" finds the "main" module and not C's main(). */
9061 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9062 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9063 SYMBOL_TYPE (sym) = type;
9064
c24bdb02 9065 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9066 }
9067}
9068
c9317f21
TT
9069/* Allocate a fully-qualified name consisting of the two parts on the
9070 obstack. */
9071
9072static const char *
9073rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9074{
9075 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9076}
9077
9078/* A helper that allocates a struct discriminant_info to attach to a
9079 union type. */
9080
9081static struct discriminant_info *
9082alloc_discriminant_info (struct type *type, int discriminant_index,
9083 int default_index)
9084{
9085 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9086 gdb_assert (discriminant_index == -1
9087 || (discriminant_index >= 0
9088 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9089 gdb_assert (default_index == -1
c7b15a66 9090 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9091
9092 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9093
9094 struct discriminant_info *disc
9095 = ((struct discriminant_info *)
9096 TYPE_ZALLOC (type,
9097 offsetof (struct discriminant_info, discriminants)
9098 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9099 disc->default_index = default_index;
9100 disc->discriminant_index = discriminant_index;
9101
9102 struct dynamic_prop prop;
9103 prop.kind = PROP_UNDEFINED;
9104 prop.data.baton = disc;
9105
9106 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9107
9108 return disc;
9109}
9110
9111/* Some versions of rustc emitted enums in an unusual way.
9112
9113 Ordinary enums were emitted as unions. The first element of each
9114 structure in the union was named "RUST$ENUM$DISR". This element
9115 held the discriminant.
9116
9117 These versions of Rust also implemented the "non-zero"
9118 optimization. When the enum had two values, and one is empty and
9119 the other holds a pointer that cannot be zero, the pointer is used
9120 as the discriminant, with a zero value meaning the empty variant.
9121 Here, the union's first member is of the form
9122 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9123 where the fieldnos are the indices of the fields that should be
9124 traversed in order to find the field (which may be several fields deep)
9125 and the variantname is the name of the variant of the case when the
9126 field is zero.
9127
9128 This function recognizes whether TYPE is of one of these forms,
9129 and, if so, smashes it to be a variant type. */
9130
9131static void
9132quirk_rust_enum (struct type *type, struct objfile *objfile)
9133{
9134 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9135
9136 /* We don't need to deal with empty enums. */
9137 if (TYPE_NFIELDS (type) == 0)
9138 return;
9139
9140#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9141 if (TYPE_NFIELDS (type) == 1
9142 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9143 {
9144 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9145
9146 /* Decode the field name to find the offset of the
9147 discriminant. */
9148 ULONGEST bit_offset = 0;
9149 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9150 while (name[0] >= '0' && name[0] <= '9')
9151 {
9152 char *tail;
9153 unsigned long index = strtoul (name, &tail, 10);
9154 name = tail;
9155 if (*name != '$'
9156 || index >= TYPE_NFIELDS (field_type)
9157 || (TYPE_FIELD_LOC_KIND (field_type, index)
9158 != FIELD_LOC_KIND_BITPOS))
9159 {
b98664d3 9160 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9161 "[in module %s]"),
9162 TYPE_FIELD_NAME (type, 0),
9163 objfile_name (objfile));
9164 return;
9165 }
9166 ++name;
9167
9168 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9169 field_type = TYPE_FIELD_TYPE (field_type, index);
9170 }
9171
9172 /* Make a union to hold the variants. */
9173 struct type *union_type = alloc_type (objfile);
9174 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9175 TYPE_NFIELDS (union_type) = 3;
9176 TYPE_FIELDS (union_type)
9177 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9178 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9179 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9180
9181 /* Put the discriminant must at index 0. */
9182 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9183 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9184 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9185 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9186
9187 /* The order of fields doesn't really matter, so put the real
9188 field at index 1 and the data-less field at index 2. */
9189 struct discriminant_info *disc
9190 = alloc_discriminant_info (union_type, 0, 1);
9191 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9192 TYPE_FIELD_NAME (union_type, 1)
9193 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9194 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9195 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9196 TYPE_FIELD_NAME (union_type, 1));
9197
9198 const char *dataless_name
9199 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9200 name);
9201 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9202 dataless_name);
9203 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9204 /* NAME points into the original discriminant name, which
9205 already has the correct lifetime. */
9206 TYPE_FIELD_NAME (union_type, 2) = name;
9207 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9208 disc->discriminants[2] = 0;
9209
9210 /* Smash this type to be a structure type. We have to do this
9211 because the type has already been recorded. */
9212 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9213 TYPE_NFIELDS (type) = 1;
9214 TYPE_FIELDS (type)
9215 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9216
9217 /* Install the variant part. */
9218 TYPE_FIELD_TYPE (type, 0) = union_type;
9219 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9220 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9221 }
77c2dba3
TT
9222 /* A union with a single anonymous field is probably an old-style
9223 univariant enum. */
9224 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9225 {
c9317f21
TT
9226 /* Smash this type to be a structure type. We have to do this
9227 because the type has already been recorded. */
9228 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9229
9230 /* Make a union to hold the variants. */
9231 struct type *union_type = alloc_type (objfile);
9232 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9233 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9234 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9235 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9236 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9237
9238 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9239 const char *variant_name
9240 = rust_last_path_segment (TYPE_NAME (field_type));
9241 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9242 TYPE_NAME (field_type)
9243 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9244 TYPE_NAME (type), variant_name);
c9317f21
TT
9245
9246 /* Install the union in the outer struct type. */
9247 TYPE_NFIELDS (type) = 1;
9248 TYPE_FIELDS (type)
9249 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9250 TYPE_FIELD_TYPE (type, 0) = union_type;
9251 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9252 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9253
9254 alloc_discriminant_info (union_type, -1, 0);
9255 }
9256 else
9257 {
9258 struct type *disr_type = nullptr;
9259 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9260 {
9261 disr_type = TYPE_FIELD_TYPE (type, i);
9262
a037790e
TT
9263 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9264 {
9265 /* All fields of a true enum will be structs. */
9266 return;
9267 }
9268 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9269 {
9270 /* Could be data-less variant, so keep going. */
a037790e 9271 disr_type = nullptr;
c9317f21
TT
9272 }
9273 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9274 "RUST$ENUM$DISR") != 0)
9275 {
9276 /* Not a Rust enum. */
9277 return;
9278 }
9279 else
9280 {
9281 /* Found one. */
9282 break;
9283 }
9284 }
9285
9286 /* If we got here without a discriminant, then it's probably
9287 just a union. */
9288 if (disr_type == nullptr)
9289 return;
9290
9291 /* Smash this type to be a structure type. We have to do this
9292 because the type has already been recorded. */
9293 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9294
9295 /* Make a union to hold the variants. */
9296 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9297 struct type *union_type = alloc_type (objfile);
9298 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9299 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9300 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9301 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9302 TYPE_FIELDS (union_type)
9303 = (struct field *) TYPE_ZALLOC (union_type,
9304 (TYPE_NFIELDS (union_type)
9305 * sizeof (struct field)));
9306
9307 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9308 TYPE_NFIELDS (type) * sizeof (struct field));
9309
9310 /* Install the discriminant at index 0 in the union. */
9311 TYPE_FIELD (union_type, 0) = *disr_field;
9312 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9313 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9314
9315 /* Install the union in the outer struct type. */
9316 TYPE_FIELD_TYPE (type, 0) = union_type;
9317 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9318 TYPE_NFIELDS (type) = 1;
9319
9320 /* Set the size and offset of the union type. */
9321 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9322
9323 /* We need a way to find the correct discriminant given a
9324 variant name. For convenience we build a map here. */
9325 struct type *enum_type = FIELD_TYPE (*disr_field);
9326 std::unordered_map<std::string, ULONGEST> discriminant_map;
9327 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9328 {
9329 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9330 {
9331 const char *name
9332 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9333 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9334 }
9335 }
9336
9337 int n_fields = TYPE_NFIELDS (union_type);
9338 struct discriminant_info *disc
9339 = alloc_discriminant_info (union_type, 0, -1);
9340 /* Skip the discriminant here. */
9341 for (int i = 1; i < n_fields; ++i)
9342 {
9343 /* Find the final word in the name of this variant's type.
9344 That name can be used to look up the correct
9345 discriminant. */
9346 const char *variant_name
9347 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9348 i)));
9349
9350 auto iter = discriminant_map.find (variant_name);
9351 if (iter != discriminant_map.end ())
9352 disc->discriminants[i] = iter->second;
9353
bedda9ac 9354 /* Remove the discriminant field, if it exists. */
c9317f21 9355 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9356 if (TYPE_NFIELDS (sub_type) > 0)
9357 {
9358 --TYPE_NFIELDS (sub_type);
9359 ++TYPE_FIELDS (sub_type);
9360 }
c9317f21
TT
9361 TYPE_FIELD_NAME (union_type, i) = variant_name;
9362 TYPE_NAME (sub_type)
9363 = rust_fully_qualify (&objfile->objfile_obstack,
9364 TYPE_NAME (type), variant_name);
9365 }
9366 }
9367}
9368
9369/* Rewrite some Rust unions to be structures with variants parts. */
9370
9371static void
9372rust_union_quirks (struct dwarf2_cu *cu)
9373{
9374 gdb_assert (cu->language == language_rust);
52941706
SM
9375 for (type *type_ : cu->rust_unions)
9376 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9377 /* We don't need this any more. */
9378 cu->rust_unions.clear ();
c9317f21
TT
9379}
9380
95554aad
TT
9381/* Return the symtab for PER_CU. This works properly regardless of
9382 whether we're using the index or psymtabs. */
9383
43f3e411
DE
9384static struct compunit_symtab *
9385get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9386{
ed2dc618 9387 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9388 ? per_cu->v.quick->compunit_symtab
9389 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9390}
9391
9392/* A helper function for computing the list of all symbol tables
9393 included by PER_CU. */
9394
9395static void
4c39bc03 9396recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9397 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9398 struct dwarf2_per_cu_data *per_cu,
43f3e411 9399 struct compunit_symtab *immediate_parent)
95554aad
TT
9400{
9401 void **slot;
43f3e411 9402 struct compunit_symtab *cust;
95554aad
TT
9403
9404 slot = htab_find_slot (all_children, per_cu, INSERT);
9405 if (*slot != NULL)
9406 {
9407 /* This inclusion and its children have been processed. */
9408 return;
9409 }
9410
9411 *slot = per_cu;
9412 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9413 cust = get_compunit_symtab (per_cu);
9414 if (cust != NULL)
ec94af83
DE
9415 {
9416 /* If this is a type unit only add its symbol table if we haven't
9417 seen it yet (type unit per_cu's can share symtabs). */
9418 if (per_cu->is_debug_types)
9419 {
43f3e411 9420 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9421 if (*slot == NULL)
9422 {
43f3e411 9423 *slot = cust;
4c39bc03 9424 result->push_back (cust);
43f3e411
DE
9425 if (cust->user == NULL)
9426 cust->user = immediate_parent;
ec94af83
DE
9427 }
9428 }
9429 else
f9125b6c 9430 {
4c39bc03 9431 result->push_back (cust);
43f3e411
DE
9432 if (cust->user == NULL)
9433 cust->user = immediate_parent;
f9125b6c 9434 }
ec94af83 9435 }
95554aad 9436
ae640021
AB
9437 if (!per_cu->imported_symtabs_empty ())
9438 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9439 {
9440 recursively_compute_inclusions (result, all_children,
9441 all_type_symtabs, ptr, cust);
9442 }
95554aad
TT
9443}
9444
43f3e411 9445/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9446 PER_CU. */
9447
9448static void
43f3e411 9449compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9450{
f4dc4d17
DE
9451 gdb_assert (! per_cu->is_debug_types);
9452
ae640021 9453 if (!per_cu->imported_symtabs_empty ())
95554aad 9454 {
ae640021 9455 int len;
4c39bc03 9456 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9457 htab_t all_children, all_type_symtabs;
43f3e411 9458 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9459
9460 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9461 if (cust == NULL)
95554aad
TT
9462 return;
9463
9464 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9465 NULL, xcalloc, xfree);
ec94af83
DE
9466 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9467 NULL, xcalloc, xfree);
95554aad 9468
ae640021 9469 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9470 {
9471 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9472 all_type_symtabs, ptr, cust);
ec94af83 9473 }
95554aad 9474
ec94af83 9475 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9476 len = result_symtabs.size ();
43f3e411 9477 cust->includes
ed2dc618 9478 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9479 struct compunit_symtab *, len + 1);
4c39bc03
TT
9480 memcpy (cust->includes, result_symtabs.data (),
9481 len * sizeof (compunit_symtab *));
43f3e411 9482 cust->includes[len] = NULL;
95554aad 9483
95554aad 9484 htab_delete (all_children);
ec94af83 9485 htab_delete (all_type_symtabs);
95554aad
TT
9486 }
9487}
9488
9489/* Compute the 'includes' field for the symtabs of all the CUs we just
9490 read. */
9491
9492static void
ed2dc618 9493process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9494{
71b73764 9495 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9496 {
9497 if (! iter->is_debug_types)
43f3e411 9498 compute_compunit_symtab_includes (iter);
f4dc4d17 9499 }
95554aad 9500
c5d0225d 9501 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9502}
9503
9cdd5dbd 9504/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9505 already been loaded into memory. */
9506
9507static void
95554aad
TT
9508process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9509 enum language pretend_language)
10b3939b 9510{
10b3939b 9511 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9512 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9513 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9514 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9515 CORE_ADDR lowpc, highpc;
43f3e411 9516 struct compunit_symtab *cust;
10b3939b 9517 CORE_ADDR baseaddr;
4359dff1 9518 struct block *static_block;
3e29f34a 9519 CORE_ADDR addr;
10b3939b 9520
b3b3bada 9521 baseaddr = objfile->text_section_offset ();
10b3939b 9522
c89b44cd
TT
9523 /* Clear the list here in case something was left over. */
9524 cu->method_list.clear ();
10b3939b 9525
95554aad
TT
9526 cu->language = pretend_language;
9527 cu->language_defn = language_def (cu->language);
9528
c906108c 9529 /* Do line number decoding in read_file_scope () */
10b3939b 9530 process_die (cu->dies, cu);
c906108c 9531
a766d390
DE
9532 /* For now fudge the Go package. */
9533 if (cu->language == language_go)
9534 fixup_go_packaging (cu);
9535
5f48f8f3 9536 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9537 should be complete, and it should now be safe to compute all of the
9538 physnames. */
9539 compute_delayed_physnames (cu);
3da10d80 9540
c9317f21
TT
9541 if (cu->language == language_rust)
9542 rust_union_quirks (cu);
9543
fae299cd
DC
9544 /* Some compilers don't define a DW_AT_high_pc attribute for the
9545 compilation unit. If the DW_AT_high_pc is missing, synthesize
9546 it, by scanning the DIE's below the compilation unit. */
10b3939b 9547 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9548
3e29f34a 9549 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9550 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9551
9552 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9553 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9554 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9555 addrmap to help ensure it has an accurate map of pc values belonging to
9556 this comp unit. */
9557 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9558
c24bdb02 9559 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9560 SECT_OFF_TEXT (objfile),
9561 0);
c906108c 9562
43f3e411 9563 if (cust != NULL)
c906108c 9564 {
df15bd07 9565 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9566
8be455d7
JK
9567 /* Set symtab language to language from DW_AT_language. If the
9568 compilation is from a C file generated by language preprocessors, do
9569 not set the language if it was already deduced by start_subfile. */
43f3e411 9570 if (!(cu->language == language_c
40e3ad0e 9571 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9572 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9573
9574 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9575 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9576 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9577 there were bugs in prologue debug info, fixed later in GCC-4.5
9578 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9579
9580 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9581 needed, it would be wrong due to missing DW_AT_producer there.
9582
9583 Still one can confuse GDB by using non-standard GCC compilation
9584 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9585 */
ab260dad 9586 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9587 cust->locations_valid = 1;
e0d00bc7
JK
9588
9589 if (gcc_4_minor >= 5)
43f3e411 9590 cust->epilogue_unwind_valid = 1;
96408a79 9591
43f3e411 9592 cust->call_site_htab = cu->call_site_htab;
c906108c 9593 }
9291a0cd
TT
9594
9595 if (dwarf2_per_objfile->using_index)
43f3e411 9596 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9597 else
9598 {
891813be 9599 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9600 pst->compunit_symtab = cust;
6d94535f 9601 pst->readin = true;
9291a0cd 9602 }
c906108c 9603
95554aad 9604 /* Push it for inclusion processing later. */
c5d0225d 9605 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9606
9607 /* Not needed any more. */
c24bdb02 9608 cu->reset_builder ();
f4dc4d17 9609}
45cfd468 9610
f4dc4d17
DE
9611/* Generate full symbol information for type unit PER_CU, whose DIEs have
9612 already been loaded into memory. */
9613
9614static void
9615process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9616 enum language pretend_language)
9617{
9618 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9619 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9620 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9621 struct compunit_symtab *cust;
0186c6a7
DE
9622 struct signatured_type *sig_type;
9623
9624 gdb_assert (per_cu->is_debug_types);
9625 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9626
c89b44cd
TT
9627 /* Clear the list here in case something was left over. */
9628 cu->method_list.clear ();
f4dc4d17 9629
f4dc4d17
DE
9630 cu->language = pretend_language;
9631 cu->language_defn = language_def (cu->language);
9632
9633 /* The symbol tables are set up in read_type_unit_scope. */
9634 process_die (cu->dies, cu);
9635
9636 /* For now fudge the Go package. */
9637 if (cu->language == language_go)
9638 fixup_go_packaging (cu);
9639
5f48f8f3 9640 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9641 should be complete, and it should now be safe to compute all of the
9642 physnames. */
9643 compute_delayed_physnames (cu);
f4dc4d17 9644
c9317f21
TT
9645 if (cu->language == language_rust)
9646 rust_union_quirks (cu);
9647
f4dc4d17
DE
9648 /* TUs share symbol tables.
9649 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9650 of it with end_expandable_symtab. Otherwise, complete the addition of
9651 this TU's symbols to the existing symtab. */
43f3e411 9652 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9653 {
c24bdb02
KS
9654 buildsym_compunit *builder = cu->get_builder ();
9655 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9656 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9657
43f3e411 9658 if (cust != NULL)
f4dc4d17
DE
9659 {
9660 /* Set symtab language to language from DW_AT_language. If the
9661 compilation is from a C file generated by language preprocessors,
9662 do not set the language if it was already deduced by
9663 start_subfile. */
43f3e411
DE
9664 if (!(cu->language == language_c
9665 && COMPUNIT_FILETABS (cust)->language != language_c))
9666 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9667 }
9668 }
9669 else
9670 {
c24bdb02 9671 cu->get_builder ()->augment_type_symtab ();
43f3e411 9672 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9673 }
9674
9675 if (dwarf2_per_objfile->using_index)
43f3e411 9676 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9677 else
9678 {
891813be 9679 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9680 pst->compunit_symtab = cust;
6d94535f 9681 pst->readin = true;
45cfd468 9682 }
804d2729
TT
9683
9684 /* Not needed any more. */
c24bdb02 9685 cu->reset_builder ();
c906108c
SS
9686}
9687
95554aad
TT
9688/* Process an imported unit DIE. */
9689
9690static void
9691process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9692{
9693 struct attribute *attr;
9694
f4dc4d17
DE
9695 /* For now we don't handle imported units in type units. */
9696 if (cu->per_cu->is_debug_types)
9697 {
9698 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9699 " supported in type units [in module %s]"),
518817b3 9700 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9701 }
9702
95554aad
TT
9703 attr = dwarf2_attr (die, DW_AT_import, cu);
9704 if (attr != NULL)
9705 {
0826b30a 9706 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9707 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9708 dwarf2_per_cu_data *per_cu
e3b94546 9709 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9710 cu->per_cu->dwarf2_per_objfile);
95554aad 9711
58990295
TV
9712 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9713 into another compilation unit, at root level. Regard this as a hint,
9714 and ignore it. */
9715 if (die->parent && die->parent->parent == NULL
9716 && per_cu->unit_type == DW_UT_compile
9717 && per_cu->lang == language_cplus)
9718 return;
9719
69d751e3 9720 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9721 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9722 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9723
ae640021 9724 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9725 }
9726}
9727
4c8aa72d
PA
9728/* RAII object that represents a process_die scope: i.e.,
9729 starts/finishes processing a DIE. */
9730class process_die_scope
adde2bff 9731{
4c8aa72d
PA
9732public:
9733 process_die_scope (die_info *die, dwarf2_cu *cu)
9734 : m_die (die), m_cu (cu)
9735 {
9736 /* We should only be processing DIEs not already in process. */
9737 gdb_assert (!m_die->in_process);
9738 m_die->in_process = true;
9739 }
8c3cb9fa 9740
4c8aa72d
PA
9741 ~process_die_scope ()
9742 {
9743 m_die->in_process = false;
9744
9745 /* If we're done processing the DIE for the CU that owns the line
9746 header, we don't need the line header anymore. */
9747 if (m_cu->line_header_die_owner == m_die)
9748 {
9749 delete m_cu->line_header;
9750 m_cu->line_header = NULL;
9751 m_cu->line_header_die_owner = NULL;
9752 }
9753 }
9754
9755private:
9756 die_info *m_die;
9757 dwarf2_cu *m_cu;
9758};
adde2bff 9759
c906108c
SS
9760/* Process a die and its children. */
9761
9762static void
e7c27a73 9763process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9764{
4c8aa72d 9765 process_die_scope scope (die, cu);
adde2bff 9766
c906108c
SS
9767 switch (die->tag)
9768 {
9769 case DW_TAG_padding:
9770 break;
9771 case DW_TAG_compile_unit:
95554aad 9772 case DW_TAG_partial_unit:
e7c27a73 9773 read_file_scope (die, cu);
c906108c 9774 break;
348e048f
DE
9775 case DW_TAG_type_unit:
9776 read_type_unit_scope (die, cu);
9777 break;
c906108c 9778 case DW_TAG_subprogram:
0a4b0913
AB
9779 /* Nested subprograms in Fortran get a prefix. */
9780 if (cu->language == language_fortran
9781 && die->parent != NULL
9782 && die->parent->tag == DW_TAG_subprogram)
9783 cu->processing_has_namespace_info = true;
9784 /* Fall through. */
c906108c 9785 case DW_TAG_inlined_subroutine:
edb3359d 9786 read_func_scope (die, cu);
c906108c
SS
9787 break;
9788 case DW_TAG_lexical_block:
14898363
L
9789 case DW_TAG_try_block:
9790 case DW_TAG_catch_block:
e7c27a73 9791 read_lexical_block_scope (die, cu);
c906108c 9792 break;
216f72a1 9793 case DW_TAG_call_site:
96408a79
SA
9794 case DW_TAG_GNU_call_site:
9795 read_call_site_scope (die, cu);
9796 break;
c906108c 9797 case DW_TAG_class_type:
680b30c7 9798 case DW_TAG_interface_type:
c906108c
SS
9799 case DW_TAG_structure_type:
9800 case DW_TAG_union_type:
134d01f1 9801 process_structure_scope (die, cu);
c906108c
SS
9802 break;
9803 case DW_TAG_enumeration_type:
134d01f1 9804 process_enumeration_scope (die, cu);
c906108c 9805 break;
134d01f1 9806
f792889a
DJ
9807 /* These dies have a type, but processing them does not create
9808 a symbol or recurse to process the children. Therefore we can
9809 read them on-demand through read_type_die. */
c906108c 9810 case DW_TAG_subroutine_type:
72019c9c 9811 case DW_TAG_set_type:
c906108c 9812 case DW_TAG_array_type:
c906108c 9813 case DW_TAG_pointer_type:
c906108c 9814 case DW_TAG_ptr_to_member_type:
c906108c 9815 case DW_TAG_reference_type:
4297a3f0 9816 case DW_TAG_rvalue_reference_type:
c906108c 9817 case DW_TAG_string_type:
c906108c 9818 break;
134d01f1 9819
c906108c 9820 case DW_TAG_base_type:
a02abb62 9821 case DW_TAG_subrange_type:
cb249c71 9822 case DW_TAG_typedef:
134d01f1
DJ
9823 /* Add a typedef symbol for the type definition, if it has a
9824 DW_AT_name. */
f792889a 9825 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9826 break;
c906108c 9827 case DW_TAG_common_block:
e7c27a73 9828 read_common_block (die, cu);
c906108c
SS
9829 break;
9830 case DW_TAG_common_inclusion:
9831 break;
d9fa45fe 9832 case DW_TAG_namespace:
9068261f 9833 cu->processing_has_namespace_info = true;
e7c27a73 9834 read_namespace (die, cu);
d9fa45fe 9835 break;
5d7cb8df 9836 case DW_TAG_module:
9068261f 9837 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9838 read_module (die, cu);
9839 break;
d9fa45fe 9840 case DW_TAG_imported_declaration:
9068261f 9841 cu->processing_has_namespace_info = true;
74921315
KS
9842 if (read_namespace_alias (die, cu))
9843 break;
86a73007
TT
9844 /* The declaration is not a global namespace alias. */
9845 /* Fall through. */
d9fa45fe 9846 case DW_TAG_imported_module:
9068261f 9847 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9848 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9849 || cu->language != language_fortran))
b98664d3 9850 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9851 dwarf_tag_name (die->tag));
9852 read_import_statement (die, cu);
d9fa45fe 9853 break;
95554aad
TT
9854
9855 case DW_TAG_imported_unit:
9856 process_imported_unit_die (die, cu);
9857 break;
9858
71a3c369
TT
9859 case DW_TAG_variable:
9860 read_variable (die, cu);
9861 break;
9862
c906108c 9863 default:
e7c27a73 9864 new_symbol (die, NULL, cu);
c906108c
SS
9865 break;
9866 }
9867}
ca69b9e6
DE
9868\f
9869/* DWARF name computation. */
c906108c 9870
94af9270
KS
9871/* A helper function for dwarf2_compute_name which determines whether DIE
9872 needs to have the name of the scope prepended to the name listed in the
9873 die. */
9874
9875static int
9876die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9877{
1c809c68
TT
9878 struct attribute *attr;
9879
94af9270
KS
9880 switch (die->tag)
9881 {
9882 case DW_TAG_namespace:
9883 case DW_TAG_typedef:
9884 case DW_TAG_class_type:
9885 case DW_TAG_interface_type:
9886 case DW_TAG_structure_type:
9887 case DW_TAG_union_type:
9888 case DW_TAG_enumeration_type:
9889 case DW_TAG_enumerator:
9890 case DW_TAG_subprogram:
08a76f8a 9891 case DW_TAG_inlined_subroutine:
94af9270 9892 case DW_TAG_member:
74921315 9893 case DW_TAG_imported_declaration:
94af9270
KS
9894 return 1;
9895
9896 case DW_TAG_variable:
c2b0a229 9897 case DW_TAG_constant:
94af9270
KS
9898 /* We only need to prefix "globally" visible variables. These include
9899 any variable marked with DW_AT_external or any variable that
9900 lives in a namespace. [Variables in anonymous namespaces
9901 require prefixing, but they are not DW_AT_external.] */
9902
9903 if (dwarf2_attr (die, DW_AT_specification, cu))
9904 {
9905 struct dwarf2_cu *spec_cu = cu;
9a619af0 9906
94af9270
KS
9907 return die_needs_namespace (die_specification (die, &spec_cu),
9908 spec_cu);
9909 }
9910
1c809c68 9911 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9912 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9913 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9914 return 0;
9915 /* A variable in a lexical block of some kind does not need a
9916 namespace, even though in C++ such variables may be external
9917 and have a mangled name. */
9918 if (die->parent->tag == DW_TAG_lexical_block
9919 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9920 || die->parent->tag == DW_TAG_catch_block
9921 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9922 return 0;
9923 return 1;
94af9270
KS
9924
9925 default:
9926 return 0;
9927 }
9928}
9929
73b9be8b
KS
9930/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9931 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9932 defined for the given DIE. */
9933
9934static struct attribute *
9935dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9936{
9937 struct attribute *attr;
9938
9939 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9940 if (attr == NULL)
9941 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9942
9943 return attr;
9944}
9945
9946/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9947 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9948 defined for the given DIE. */
9949
9950static const char *
9951dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9952{
9953 const char *linkage_name;
9954
9955 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9956 if (linkage_name == NULL)
9957 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9958
9959 return linkage_name;
9960}
9961
94af9270 9962/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9963 compute the physname for the object, which include a method's:
9c37b5ae 9964 - formal parameters (C++),
a766d390 9965 - receiver type (Go),
a766d390
DE
9966
9967 The term "physname" is a bit confusing.
9968 For C++, for example, it is the demangled name.
9969 For Go, for example, it's the mangled name.
94af9270 9970
af6b7be1
JB
9971 For Ada, return the DIE's linkage name rather than the fully qualified
9972 name. PHYSNAME is ignored..
9973
94af9270
KS
9974 The result is allocated on the objfile_obstack and canonicalized. */
9975
9976static const char *
15d034d0
TT
9977dwarf2_compute_name (const char *name,
9978 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9979 int physname)
9980{
518817b3 9981 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9982
94af9270
KS
9983 if (name == NULL)
9984 name = dwarf2_name (die, cu);
9985
2ee7123e
DE
9986 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9987 but otherwise compute it by typename_concat inside GDB.
9988 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9989 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9990 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9991 will set the demangled name to the result of dwarf2_full_name, and it is
9992 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9993 if (cu->language == language_ada
9994 || (cu->language == language_fortran && physname))
9995 {
9996 /* For Ada unit, we prefer the linkage name over the name, as
9997 the former contains the exported name, which the user expects
9998 to be able to reference. Ideally, we want the user to be able
9999 to reference this entity using either natural or linkage name,
10000 but we haven't started looking at this enhancement yet. */
73b9be8b 10001 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10002
2ee7123e
DE
10003 if (linkage_name != NULL)
10004 return linkage_name;
f55ee35c
JK
10005 }
10006
94af9270
KS
10007 /* These are the only languages we know how to qualify names in. */
10008 if (name != NULL
9c37b5ae 10009 && (cu->language == language_cplus
c44af4eb
TT
10010 || cu->language == language_fortran || cu->language == language_d
10011 || cu->language == language_rust))
94af9270
KS
10012 {
10013 if (die_needs_namespace (die, cu))
10014 {
0d5cff50 10015 const char *prefix;
34a68019 10016 const char *canonical_name = NULL;
94af9270 10017
d7e74731
PA
10018 string_file buf;
10019
94af9270 10020 prefix = determine_prefix (die, cu);
94af9270
KS
10021 if (*prefix != '\0')
10022 {
43816ebc
TT
10023 gdb::unique_xmalloc_ptr<char> prefixed_name
10024 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10025
43816ebc 10026 buf.puts (prefixed_name.get ());
94af9270
KS
10027 }
10028 else
d7e74731 10029 buf.puts (name);
94af9270 10030
98bfdba5
PA
10031 /* Template parameters may be specified in the DIE's DW_AT_name, or
10032 as children with DW_TAG_template_type_param or
10033 DW_TAG_value_type_param. If the latter, add them to the name
10034 here. If the name already has template parameters, then
10035 skip this step; some versions of GCC emit both, and
10036 it is more efficient to use the pre-computed name.
10037
10038 Something to keep in mind about this process: it is very
10039 unlikely, or in some cases downright impossible, to produce
10040 something that will match the mangled name of a function.
10041 If the definition of the function has the same debug info,
10042 we should be able to match up with it anyway. But fallbacks
10043 using the minimal symbol, for instance to find a method
10044 implemented in a stripped copy of libstdc++, will not work.
10045 If we do not have debug info for the definition, we will have to
10046 match them up some other way.
10047
10048 When we do name matching there is a related problem with function
10049 templates; two instantiated function templates are allowed to
10050 differ only by their return types, which we do not add here. */
10051
10052 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10053 {
10054 struct attribute *attr;
10055 struct die_info *child;
10056 int first = 1;
10057
10058 die->building_fullname = 1;
10059
10060 for (child = die->child; child != NULL; child = child->sibling)
10061 {
10062 struct type *type;
12df843f 10063 LONGEST value;
d521ce57 10064 const gdb_byte *bytes;
98bfdba5
PA
10065 struct dwarf2_locexpr_baton *baton;
10066 struct value *v;
10067
10068 if (child->tag != DW_TAG_template_type_param
10069 && child->tag != DW_TAG_template_value_param)
10070 continue;
10071
10072 if (first)
10073 {
d7e74731 10074 buf.puts ("<");
98bfdba5
PA
10075 first = 0;
10076 }
10077 else
d7e74731 10078 buf.puts (", ");
98bfdba5
PA
10079
10080 attr = dwarf2_attr (child, DW_AT_type, cu);
10081 if (attr == NULL)
10082 {
b98664d3 10083 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10084 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10085 continue;
10086 }
10087 type = die_type (child, cu);
10088
10089 if (child->tag == DW_TAG_template_type_param)
10090 {
c1ec8cea
TT
10091 c_print_type (type, "", &buf, -1, 0, cu->language,
10092 &type_print_raw_options);
98bfdba5
PA
10093 continue;
10094 }
10095
10096 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10097 if (attr == NULL)
10098 {
b98664d3 10099 complaint (_("template parameter missing "
3e43a32a 10100 "DW_AT_const_value"));
d7e74731 10101 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10102 continue;
10103 }
10104
10105 dwarf2_const_value_attr (attr, type, name,
10106 &cu->comp_unit_obstack, cu,
10107 &value, &bytes, &baton);
10108
10109 if (TYPE_NOSIGN (type))
10110 /* GDB prints characters as NUMBER 'CHAR'. If that's
10111 changed, this can use value_print instead. */
d7e74731 10112 c_printchar (value, type, &buf);
98bfdba5
PA
10113 else
10114 {
10115 struct value_print_options opts;
10116
10117 if (baton != NULL)
10118 v = dwarf2_evaluate_loc_desc (type, NULL,
10119 baton->data,
10120 baton->size,
10121 baton->per_cu);
10122 else if (bytes != NULL)
10123 {
10124 v = allocate_value (type);
10125 memcpy (value_contents_writeable (v), bytes,
10126 TYPE_LENGTH (type));
10127 }
10128 else
10129 v = value_from_longest (type, value);
10130
3e43a32a
MS
10131 /* Specify decimal so that we do not depend on
10132 the radix. */
98bfdba5
PA
10133 get_formatted_print_options (&opts, 'd');
10134 opts.raw = 1;
d7e74731 10135 value_print (v, &buf, &opts);
98bfdba5 10136 release_value (v);
98bfdba5
PA
10137 }
10138 }
10139
10140 die->building_fullname = 0;
10141
10142 if (!first)
10143 {
10144 /* Close the argument list, with a space if necessary
10145 (nested templates). */
d7e74731
PA
10146 if (!buf.empty () && buf.string ().back () == '>')
10147 buf.puts (" >");
98bfdba5 10148 else
d7e74731 10149 buf.puts (">");
98bfdba5
PA
10150 }
10151 }
10152
9c37b5ae 10153 /* For C++ methods, append formal parameter type
94af9270 10154 information, if PHYSNAME. */
6e70227d 10155
94af9270 10156 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10157 && cu->language == language_cplus)
94af9270
KS
10158 {
10159 struct type *type = read_type_die (die, cu);
10160
d7e74731 10161 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10162 &type_print_raw_options);
94af9270 10163
9c37b5ae 10164 if (cu->language == language_cplus)
94af9270 10165 {
60430eff
DJ
10166 /* Assume that an artificial first parameter is
10167 "this", but do not crash if it is not. RealView
10168 marks unnamed (and thus unused) parameters as
10169 artificial; there is no way to differentiate
10170 the two cases. */
94af9270
KS
10171 if (TYPE_NFIELDS (type) > 0
10172 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10173 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10174 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10175 0))))
d7e74731 10176 buf.puts (" const");
94af9270
KS
10177 }
10178 }
10179
d7e74731 10180 const std::string &intermediate_name = buf.string ();
94af9270
KS
10181
10182 if (cu->language == language_cplus)
34a68019 10183 canonical_name
322a8516 10184 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10185 objfile);
34a68019
TT
10186
10187 /* If we only computed INTERMEDIATE_NAME, or if
10188 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10189 intern it. */
322a8516 10190 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10191 name = objfile->intern (intermediate_name);
34a68019
TT
10192 else
10193 name = canonical_name;
94af9270
KS
10194 }
10195 }
10196
10197 return name;
10198}
10199
0114d602
DJ
10200/* Return the fully qualified name of DIE, based on its DW_AT_name.
10201 If scope qualifiers are appropriate they will be added. The result
34a68019 10202 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10203 not have a name. NAME may either be from a previous call to
10204 dwarf2_name or NULL.
10205
9c37b5ae 10206 The output string will be canonicalized (if C++). */
0114d602
DJ
10207
10208static const char *
15d034d0 10209dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10210{
94af9270
KS
10211 return dwarf2_compute_name (name, die, cu, 0);
10212}
0114d602 10213
94af9270
KS
10214/* Construct a physname for the given DIE in CU. NAME may either be
10215 from a previous call to dwarf2_name or NULL. The result will be
10216 allocated on the objfile_objstack or NULL if the DIE does not have a
10217 name.
0114d602 10218
9c37b5ae 10219 The output string will be canonicalized (if C++). */
0114d602 10220
94af9270 10221static const char *
15d034d0 10222dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10223{
518817b3 10224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10225 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10226 int need_copy = 1;
10227
10228 /* In this case dwarf2_compute_name is just a shortcut not building anything
10229 on its own. */
10230 if (!die_needs_namespace (die, cu))
10231 return dwarf2_compute_name (name, die, cu, 1);
10232
73b9be8b 10233 mangled = dw2_linkage_name (die, cu);
900e11f9 10234
e98c9e7c
TT
10235 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10236 See https://github.com/rust-lang/rust/issues/32925. */
10237 if (cu->language == language_rust && mangled != NULL
10238 && strchr (mangled, '{') != NULL)
10239 mangled = NULL;
10240
900e11f9
JK
10241 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10242 has computed. */
791afaa2 10243 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10244 if (mangled != NULL)
900e11f9 10245 {
900e11f9 10246
59cc4834
JB
10247 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10248 {
10249 /* Do nothing (do not demangle the symbol name). */
10250 }
10251 else if (cu->language == language_go)
a766d390 10252 {
5e2db402
TT
10253 /* This is a lie, but we already lie to the caller new_symbol.
10254 new_symbol assumes we return the mangled name.
a766d390 10255 This just undoes that lie until things are cleaned up. */
a766d390
DE
10256 }
10257 else
10258 {
0eb876f5
JB
10259 /* Use DMGL_RET_DROP for C++ template functions to suppress
10260 their return type. It is easier for GDB users to search
10261 for such functions as `name(params)' than `long name(params)'.
10262 In such case the minimal symbol names do not match the full
10263 symbol names but for template functions there is never a need
10264 to look up their definition from their declaration so
10265 the only disadvantage remains the minimal symbol variant
10266 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10267 demangled.reset (gdb_demangle (mangled,
10268 (DMGL_PARAMS | DMGL_ANSI
10269 | DMGL_RET_DROP)));
a766d390 10270 }
900e11f9 10271 if (demangled)
791afaa2 10272 canon = demangled.get ();
900e11f9
JK
10273 else
10274 {
10275 canon = mangled;
10276 need_copy = 0;
10277 }
10278 }
10279
10280 if (canon == NULL || check_physname)
10281 {
10282 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10283
10284 if (canon != NULL && strcmp (physname, canon) != 0)
10285 {
10286 /* It may not mean a bug in GDB. The compiler could also
10287 compute DW_AT_linkage_name incorrectly. But in such case
10288 GDB would need to be bug-to-bug compatible. */
10289
b98664d3 10290 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10291 "(from linkage <%s>) - DIE at %s [in module %s]"),
10292 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10293 objfile_name (objfile));
900e11f9
JK
10294
10295 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10296 is available here - over computed PHYSNAME. It is safer
10297 against both buggy GDB and buggy compilers. */
10298
10299 retval = canon;
10300 }
10301 else
10302 {
10303 retval = physname;
10304 need_copy = 0;
10305 }
10306 }
10307 else
10308 retval = canon;
10309
10310 if (need_copy)
be1e3d3e 10311 retval = objfile->intern (retval);
900e11f9 10312
900e11f9 10313 return retval;
0114d602
DJ
10314}
10315
74921315
KS
10316/* Inspect DIE in CU for a namespace alias. If one exists, record
10317 a new symbol for it.
10318
10319 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10320
10321static int
10322read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10323{
10324 struct attribute *attr;
10325
10326 /* If the die does not have a name, this is not a namespace
10327 alias. */
10328 attr = dwarf2_attr (die, DW_AT_name, cu);
10329 if (attr != NULL)
10330 {
10331 int num;
10332 struct die_info *d = die;
10333 struct dwarf2_cu *imported_cu = cu;
10334
10335 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10336 keep inspecting DIEs until we hit the underlying import. */
10337#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10338 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10339 {
10340 attr = dwarf2_attr (d, DW_AT_import, cu);
10341 if (attr == NULL)
10342 break;
10343
10344 d = follow_die_ref (d, attr, &imported_cu);
10345 if (d->tag != DW_TAG_imported_declaration)
10346 break;
10347 }
10348
10349 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10350 {
b98664d3 10351 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10352 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10353 return 0;
10354 }
10355
10356 if (attr != NULL)
10357 {
10358 struct type *type;
0826b30a 10359 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10360
9c541725 10361 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10362 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10363 {
10364 /* This declaration is a global namespace alias. Add
10365 a symbol for it whose type is the aliased namespace. */
10366 new_symbol (die, type, cu);
10367 return 1;
10368 }
10369 }
10370 }
10371
10372 return 0;
10373}
10374
22cee43f 10375/* Return the using directives repository (global or local?) to use in the
804d2729 10376 current context for CU.
22cee43f
PMR
10377
10378 For Ada, imported declarations can materialize renamings, which *may* be
10379 global. However it is impossible (for now?) in DWARF to distinguish
10380 "external" imported declarations and "static" ones. As all imported
10381 declarations seem to be static in all other languages, make them all CU-wide
10382 global only in Ada. */
10383
10384static struct using_direct **
804d2729 10385using_directives (struct dwarf2_cu *cu)
22cee43f 10386{
c24bdb02
KS
10387 if (cu->language == language_ada
10388 && cu->get_builder ()->outermost_context_p ())
10389 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10390 else
c24bdb02 10391 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10392}
10393
27aa8d6a
SW
10394/* Read the import statement specified by the given die and record it. */
10395
10396static void
10397read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10398{
518817b3 10399 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10400 struct attribute *import_attr;
32019081 10401 struct die_info *imported_die, *child_die;
de4affc9 10402 struct dwarf2_cu *imported_cu;
27aa8d6a 10403 const char *imported_name;
794684b6 10404 const char *imported_name_prefix;
13387711
SW
10405 const char *canonical_name;
10406 const char *import_alias;
10407 const char *imported_declaration = NULL;
794684b6 10408 const char *import_prefix;
eb1e02fd 10409 std::vector<const char *> excludes;
13387711 10410
27aa8d6a
SW
10411 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10412 if (import_attr == NULL)
10413 {
b98664d3 10414 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10415 dwarf_tag_name (die->tag));
10416 return;
10417 }
10418
de4affc9
CC
10419 imported_cu = cu;
10420 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10421 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10422 if (imported_name == NULL)
10423 {
10424 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10425
10426 The import in the following code:
10427 namespace A
10428 {
10429 typedef int B;
10430 }
10431
10432 int main ()
10433 {
10434 using A::B;
10435 B b;
10436 return b;
10437 }
10438
10439 ...
10440 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10441 <52> DW_AT_decl_file : 1
10442 <53> DW_AT_decl_line : 6
10443 <54> DW_AT_import : <0x75>
10444 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10445 <59> DW_AT_name : B
10446 <5b> DW_AT_decl_file : 1
10447 <5c> DW_AT_decl_line : 2
10448 <5d> DW_AT_type : <0x6e>
10449 ...
10450 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10451 <76> DW_AT_byte_size : 4
10452 <77> DW_AT_encoding : 5 (signed)
10453
10454 imports the wrong die ( 0x75 instead of 0x58 ).
10455 This case will be ignored until the gcc bug is fixed. */
10456 return;
10457 }
10458
82856980
SW
10459 /* Figure out the local name after import. */
10460 import_alias = dwarf2_name (die, cu);
27aa8d6a 10461
794684b6
SW
10462 /* Figure out where the statement is being imported to. */
10463 import_prefix = determine_prefix (die, cu);
10464
10465 /* Figure out what the scope of the imported die is and prepend it
10466 to the name of the imported die. */
de4affc9 10467 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10468
f55ee35c
JK
10469 if (imported_die->tag != DW_TAG_namespace
10470 && imported_die->tag != DW_TAG_module)
794684b6 10471 {
13387711
SW
10472 imported_declaration = imported_name;
10473 canonical_name = imported_name_prefix;
794684b6 10474 }
13387711 10475 else if (strlen (imported_name_prefix) > 0)
12aaed36 10476 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10477 imported_name_prefix,
10478 (cu->language == language_d ? "." : "::"),
10479 imported_name, (char *) NULL);
13387711
SW
10480 else
10481 canonical_name = imported_name;
794684b6 10482
32019081
JK
10483 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10484 for (child_die = die->child; child_die && child_die->tag;
436c571c 10485 child_die = child_die->sibling)
32019081
JK
10486 {
10487 /* DWARF-4: A Fortran use statement with a “rename list” may be
10488 represented by an imported module entry with an import attribute
10489 referring to the module and owned entries corresponding to those
10490 entities that are renamed as part of being imported. */
10491
10492 if (child_die->tag != DW_TAG_imported_declaration)
10493 {
b98664d3 10494 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10495 "- DIE at %s [in module %s]"),
10496 sect_offset_str (child_die->sect_off),
10497 objfile_name (objfile));
32019081
JK
10498 continue;
10499 }
10500
10501 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10502 if (import_attr == NULL)
10503 {
b98664d3 10504 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10505 dwarf_tag_name (child_die->tag));
10506 continue;
10507 }
10508
10509 imported_cu = cu;
10510 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10511 &imported_cu);
10512 imported_name = dwarf2_name (imported_die, imported_cu);
10513 if (imported_name == NULL)
10514 {
b98664d3 10515 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10516 "imported name - DIE at %s [in module %s]"),
10517 sect_offset_str (child_die->sect_off),
10518 objfile_name (objfile));
32019081
JK
10519 continue;
10520 }
10521
eb1e02fd 10522 excludes.push_back (imported_name);
32019081
JK
10523
10524 process_die (child_die, cu);
10525 }
10526
804d2729 10527 add_using_directive (using_directives (cu),
22cee43f
PMR
10528 import_prefix,
10529 canonical_name,
10530 import_alias,
10531 imported_declaration,
10532 excludes,
10533 0,
10534 &objfile->objfile_obstack);
27aa8d6a
SW
10535}
10536
5230b05a
WT
10537/* ICC<14 does not output the required DW_AT_declaration on incomplete
10538 types, but gives them a size of zero. Starting with version 14,
10539 ICC is compatible with GCC. */
10540
9068261f 10541static bool
5230b05a
WT
10542producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10543{
10544 if (!cu->checked_producer)
10545 check_producer (cu);
10546
10547 return cu->producer_is_icc_lt_14;
10548}
10549
eb77c9df
AB
10550/* ICC generates a DW_AT_type for C void functions. This was observed on
10551 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10552 which says that void functions should not have a DW_AT_type. */
10553
10554static bool
10555producer_is_icc (struct dwarf2_cu *cu)
10556{
10557 if (!cu->checked_producer)
10558 check_producer (cu);
10559
10560 return cu->producer_is_icc;
10561}
10562
1b80a9fa
JK
10563/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10564 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10565 this, it was first present in GCC release 4.3.0. */
10566
9068261f 10567static bool
1b80a9fa
JK
10568producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10569{
10570 if (!cu->checked_producer)
10571 check_producer (cu);
10572
10573 return cu->producer_is_gcc_lt_4_3;
10574}
10575
d721ba37
PA
10576static file_and_directory
10577find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10578{
d721ba37
PA
10579 file_and_directory res;
10580
9291a0cd
TT
10581 /* Find the filename. Do not use dwarf2_name here, since the filename
10582 is not a source language identifier. */
d721ba37
PA
10583 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10584 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10585
d721ba37
PA
10586 if (res.comp_dir == NULL
10587 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10588 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10589 {
d721ba37
PA
10590 res.comp_dir_storage = ldirname (res.name);
10591 if (!res.comp_dir_storage.empty ())
10592 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10593 }
d721ba37 10594 if (res.comp_dir != NULL)
9291a0cd
TT
10595 {
10596 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10597 directory, get rid of it. */
d721ba37 10598 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10599
d721ba37
PA
10600 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10601 res.comp_dir = cp + 1;
9291a0cd
TT
10602 }
10603
d721ba37
PA
10604 if (res.name == NULL)
10605 res.name = "<unknown>";
10606
10607 return res;
9291a0cd
TT
10608}
10609
f4dc4d17
DE
10610/* Handle DW_AT_stmt_list for a compilation unit.
10611 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10612 COMP_DIR is the compilation directory. LOWPC is passed to
10613 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10614
10615static void
10616handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10617 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10618{
518817b3
SM
10619 struct dwarf2_per_objfile *dwarf2_per_objfile
10620 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10621 struct attribute *attr;
527f3840
JK
10622 struct line_header line_header_local;
10623 hashval_t line_header_local_hash;
527f3840
JK
10624 void **slot;
10625 int decode_mapping;
2ab95328 10626
f4dc4d17
DE
10627 gdb_assert (! cu->per_cu->is_debug_types);
10628
2ab95328 10629 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10630 if (attr == NULL)
10631 return;
10632
9c541725 10633 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10634
10635 /* The line header hash table is only created if needed (it exists to
10636 prevent redundant reading of the line table for partial_units).
10637 If we're given a partial_unit, we'll need it. If we're given a
10638 compile_unit, then use the line header hash table if it's already
10639 created, but don't create one just yet. */
10640
10641 if (dwarf2_per_objfile->line_header_hash == NULL
10642 && die->tag == DW_TAG_partial_unit)
2ab95328 10643 {
527f3840 10644 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10645 .reset (htab_create_alloc (127, line_header_hash_voidp,
10646 line_header_eq_voidp,
10647 free_line_header_voidp,
10648 xcalloc, xfree));
527f3840 10649 }
2ab95328 10650
9c541725 10651 line_header_local.sect_off = line_offset;
527f3840
JK
10652 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10653 line_header_local_hash = line_header_hash (&line_header_local);
10654 if (dwarf2_per_objfile->line_header_hash != NULL)
10655 {
d15acc42 10656 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10657 &line_header_local,
10658 line_header_local_hash, NO_INSERT);
10659
10660 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10661 is not present in *SLOT (since if there is something in *SLOT then
10662 it will be for a partial_unit). */
10663 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10664 {
527f3840 10665 gdb_assert (*slot != NULL);
9a3c8263 10666 cu->line_header = (struct line_header *) *slot;
527f3840 10667 return;
dee91e82 10668 }
2ab95328 10669 }
527f3840
JK
10670
10671 /* dwarf_decode_line_header does not yet provide sufficient information.
10672 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10673 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10674 if (lh == NULL)
527f3840 10675 return;
4c8aa72d
PA
10676
10677 cu->line_header = lh.release ();
10678 cu->line_header_die_owner = die;
527f3840
JK
10679
10680 if (dwarf2_per_objfile->line_header_hash == NULL)
10681 slot = NULL;
10682 else
10683 {
d15acc42 10684 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10685 &line_header_local,
10686 line_header_local_hash, INSERT);
10687 gdb_assert (slot != NULL);
10688 }
10689 if (slot != NULL && *slot == NULL)
10690 {
10691 /* This newly decoded line number information unit will be owned
10692 by line_header_hash hash table. */
10693 *slot = cu->line_header;
4c8aa72d 10694 cu->line_header_die_owner = NULL;
527f3840
JK
10695 }
10696 else
10697 {
10698 /* We cannot free any current entry in (*slot) as that struct line_header
10699 may be already used by multiple CUs. Create only temporary decoded
10700 line_header for this CU - it may happen at most once for each line
10701 number information unit. And if we're not using line_header_hash
10702 then this is what we want as well. */
10703 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10704 }
10705 decode_mapping = (die->tag != DW_TAG_partial_unit);
10706 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10707 decode_mapping);
fff8551c 10708
2ab95328
TT
10709}
10710
95554aad 10711/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10712
c906108c 10713static void
e7c27a73 10714read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10715{
518817b3
SM
10716 struct dwarf2_per_objfile *dwarf2_per_objfile
10717 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10718 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10719 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10720 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10721 CORE_ADDR highpc = ((CORE_ADDR) 0);
10722 struct attribute *attr;
c906108c 10723 struct die_info *child_die;
e142c38c 10724 CORE_ADDR baseaddr;
6e70227d 10725
380618d6 10726 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10727 baseaddr = objfile->text_section_offset ();
c906108c 10728
fae299cd 10729 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10730
10731 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10732 from finish_block. */
2acceee2 10733 if (lowpc == ((CORE_ADDR) -1))
c906108c 10734 lowpc = highpc;
3e29f34a 10735 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10736
d721ba37 10737 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10738
f4b8a18d
KW
10739 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10740 standardised yet. As a workaround for the language detection we fall
10741 back to the DW_AT_producer string. */
10742 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10743 cu->language = language_opencl;
10744
3019eac3
DE
10745 /* Similar hack for Go. */
10746 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10747 set_cu_language (DW_LANG_Go, cu);
10748
c24bdb02 10749 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10750
10751 /* Decode line number information if present. We do this before
10752 processing child DIEs, so that the line header table is available
10753 for DW_AT_decl_file. */
d721ba37 10754 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10755
10756 /* Process all dies in compilation unit. */
10757 if (die->child != NULL)
10758 {
10759 child_die = die->child;
10760 while (child_die && child_die->tag)
10761 {
10762 process_die (child_die, cu);
436c571c 10763 child_die = child_die->sibling;
3019eac3
DE
10764 }
10765 }
10766
10767 /* Decode macro information, if present. Dwarf 2 macro information
10768 refers to information in the line number info statement program
10769 header, so we can only read it if we've read the header
10770 successfully. */
0af92d60
JK
10771 attr = dwarf2_attr (die, DW_AT_macros, cu);
10772 if (attr == NULL)
10773 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10774 if (attr && cu->line_header)
10775 {
10776 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10777 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10778
43f3e411 10779 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10780 }
10781 else
10782 {
10783 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10784 if (attr && cu->line_header)
10785 {
10786 unsigned int macro_offset = DW_UNSND (attr);
10787
43f3e411 10788 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10789 }
10790 }
3019eac3
DE
10791}
10792
c24bdb02
KS
10793void
10794dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10795{
f4dc4d17
DE
10796 struct type_unit_group *tu_group;
10797 int first_time;
3019eac3 10798 struct attribute *attr;
9c541725 10799 unsigned int i;
0186c6a7 10800 struct signatured_type *sig_type;
3019eac3 10801
f4dc4d17 10802 gdb_assert (per_cu->is_debug_types);
0186c6a7 10803 sig_type = (struct signatured_type *) per_cu;
3019eac3 10804
c24bdb02 10805 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10806
f4dc4d17 10807 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10808 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10809 if (sig_type->type_unit_group == NULL)
c24bdb02 10810 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10811 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10812
10813 /* If we've already processed this stmt_list there's no real need to
10814 do it again, we could fake it and just recreate the part we need
10815 (file name,index -> symtab mapping). If data shows this optimization
10816 is useful we can do it then. */
43f3e411 10817 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10818
10819 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10820 debug info. */
fff8551c 10821 line_header_up lh;
f4dc4d17 10822 if (attr != NULL)
3019eac3 10823 {
9c541725 10824 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10825 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10826 }
10827 if (lh == NULL)
10828 {
10829 if (first_time)
c24bdb02 10830 start_symtab ("", NULL, 0);
f4dc4d17
DE
10831 else
10832 {
10833 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10834 gdb_assert (m_builder == nullptr);
804d2729 10835 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10836 m_builder.reset (new struct buildsym_compunit
10837 (COMPUNIT_OBJFILE (cust), "",
10838 COMPUNIT_DIRNAME (cust),
10839 compunit_language (cust),
10840 0, cust));
f4dc4d17 10841 }
f4dc4d17 10842 return;
3019eac3
DE
10843 }
10844
c24bdb02
KS
10845 line_header = lh.release ();
10846 line_header_die_owner = die;
3019eac3 10847
f4dc4d17
DE
10848 if (first_time)
10849 {
c24bdb02 10850 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10851
1fd60fc0
DE
10852 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10853 still initializing it, and our caller (a few levels up)
10854 process_full_type_unit still needs to know if this is the first
10855 time. */
10856
4ac93832
TT
10857 tu_group->symtabs
10858 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10859 struct symtab *, line_header->file_names_size ());
3019eac3 10860
7ba99d21
AT
10861 auto &file_names = line_header->file_names ();
10862 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10863 {
7ba99d21 10864 file_entry &fe = file_names[i];
c24bdb02
KS
10865 dwarf2_start_subfile (this, fe.name,
10866 fe.include_dir (line_header));
10867 buildsym_compunit *b = get_builder ();
10868 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10869 {
4c8aa72d
PA
10870 /* NOTE: start_subfile will recognize when it's been
10871 passed a file it has already seen. So we can't
10872 assume there's a simple mapping from
10873 cu->line_header->file_names to subfiles, plus
10874 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10875 b->get_current_subfile ()->symtab
10876 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10877 }
10878
c24bdb02 10879 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10880 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10881 }
10882 }
10883 else
3019eac3 10884 {
c24bdb02 10885 gdb_assert (m_builder == nullptr);
804d2729 10886 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10887 m_builder.reset (new struct buildsym_compunit
10888 (COMPUNIT_OBJFILE (cust), "",
10889 COMPUNIT_DIRNAME (cust),
10890 compunit_language (cust),
10891 0, cust));
f4dc4d17 10892
7ba99d21
AT
10893 auto &file_names = line_header->file_names ();
10894 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10895 {
7ba99d21 10896 file_entry &fe = file_names[i];
4c8aa72d 10897 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10898 }
3019eac3
DE
10899 }
10900
f4dc4d17
DE
10901 /* The main symtab is allocated last. Type units don't have DW_AT_name
10902 so they don't have a "real" (so to speak) symtab anyway.
10903 There is later code that will assign the main symtab to all symbols
10904 that don't have one. We need to handle the case of a symbol with a
10905 missing symtab (DW_AT_decl_file) anyway. */
10906}
3019eac3 10907
f4dc4d17
DE
10908/* Process DW_TAG_type_unit.
10909 For TUs we want to skip the first top level sibling if it's not the
10910 actual type being defined by this TU. In this case the first top
10911 level sibling is there to provide context only. */
3019eac3 10912
f4dc4d17
DE
10913static void
10914read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10915{
10916 struct die_info *child_die;
3019eac3 10917
f4dc4d17
DE
10918 prepare_one_comp_unit (cu, die, language_minimal);
10919
10920 /* Initialize (or reinitialize) the machinery for building symtabs.
10921 We do this before processing child DIEs, so that the line header table
10922 is available for DW_AT_decl_file. */
c24bdb02 10923 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10924
10925 if (die->child != NULL)
10926 {
10927 child_die = die->child;
10928 while (child_die && child_die->tag)
10929 {
10930 process_die (child_die, cu);
436c571c 10931 child_die = child_die->sibling;
f4dc4d17
DE
10932 }
10933 }
3019eac3
DE
10934}
10935\f
80626a55
DE
10936/* DWO/DWP files.
10937
10938 http://gcc.gnu.org/wiki/DebugFission
10939 http://gcc.gnu.org/wiki/DebugFissionDWP
10940
10941 To simplify handling of both DWO files ("object" files with the DWARF info)
10942 and DWP files (a file with the DWOs packaged up into one file), we treat
10943 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10944
10945static hashval_t
10946hash_dwo_file (const void *item)
10947{
9a3c8263 10948 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10949 hashval_t hash;
3019eac3 10950
a2ce51a0
DE
10951 hash = htab_hash_string (dwo_file->dwo_name);
10952 if (dwo_file->comp_dir != NULL)
10953 hash += htab_hash_string (dwo_file->comp_dir);
10954 return hash;
3019eac3
DE
10955}
10956
10957static int
10958eq_dwo_file (const void *item_lhs, const void *item_rhs)
10959{
9a3c8263
SM
10960 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10961 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10962
a2ce51a0
DE
10963 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10964 return 0;
10965 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10966 return lhs->comp_dir == rhs->comp_dir;
10967 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10968}
10969
10970/* Allocate a hash table for DWO files. */
10971
51ac9db5 10972static htab_up
298e9637 10973allocate_dwo_file_hash_table ()
3019eac3 10974{
51ac9db5
SM
10975 auto delete_dwo_file = [] (void *item)
10976 {
10977 struct dwo_file *dwo_file = (struct dwo_file *) item;
10978
10979 delete dwo_file;
10980 };
10981
bc68fb19
TT
10982 return htab_up (htab_create_alloc (41,
10983 hash_dwo_file,
10984 eq_dwo_file,
10985 delete_dwo_file,
10986 xcalloc, xfree));
3019eac3
DE
10987}
10988
80626a55
DE
10989/* Lookup DWO file DWO_NAME. */
10990
10991static void **
ed2dc618
SM
10992lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10993 const char *dwo_name,
10994 const char *comp_dir)
80626a55
DE
10995{
10996 struct dwo_file find_entry;
10997 void **slot;
10998
10999 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11000 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11001
0ac5b59e
DE
11002 find_entry.dwo_name = dwo_name;
11003 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11004 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11005 INSERT);
80626a55
DE
11006
11007 return slot;
11008}
11009
3019eac3
DE
11010static hashval_t
11011hash_dwo_unit (const void *item)
11012{
9a3c8263 11013 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11014
11015 /* This drops the top 32 bits of the id, but is ok for a hash. */
11016 return dwo_unit->signature;
11017}
11018
11019static int
11020eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11021{
9a3c8263
SM
11022 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11023 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11024
11025 /* The signature is assumed to be unique within the DWO file.
11026 So while object file CU dwo_id's always have the value zero,
11027 that's OK, assuming each object file DWO file has only one CU,
11028 and that's the rule for now. */
11029 return lhs->signature == rhs->signature;
11030}
11031
11032/* Allocate a hash table for DWO CUs,TUs.
11033 There is one of these tables for each of CUs,TUs for each DWO file. */
11034
b0b6a987 11035static htab_up
298e9637 11036allocate_dwo_unit_table ()
3019eac3
DE
11037{
11038 /* Start out with a pretty small number.
11039 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11040 return htab_up (htab_create_alloc (3,
11041 hash_dwo_unit,
11042 eq_dwo_unit,
11043 NULL, xcalloc, xfree));
3019eac3
DE
11044}
11045
19c3d4c9 11046/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11047
11048static void
19c3d4c9
DE
11049create_dwo_cu_reader (const struct die_reader_specs *reader,
11050 const gdb_byte *info_ptr,
11051 struct die_info *comp_unit_die,
c0ab21c2
TT
11052 struct dwo_file *dwo_file,
11053 struct dwo_unit *dwo_unit)
3019eac3
DE
11054{
11055 struct dwarf2_cu *cu = reader->cu;
9c541725 11056 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11057 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11058
a084a2a6
AT
11059 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11060 if (!signature.has_value ())
3019eac3 11061 {
b98664d3 11062 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11063 " its dwo_id [in module %s]"),
9d8780f0 11064 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11065 return;
11066 }
11067
3019eac3 11068 dwo_unit->dwo_file = dwo_file;
a084a2a6 11069 dwo_unit->signature = *signature;
8a0459fd 11070 dwo_unit->section = section;
9c541725 11071 dwo_unit->sect_off = sect_off;
3019eac3
DE
11072 dwo_unit->length = cu->per_cu->length;
11073
b4f54984 11074 if (dwarf_read_debug)
9d8780f0
SM
11075 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11076 sect_offset_str (sect_off),
9c541725 11077 hex_string (dwo_unit->signature));
3019eac3
DE
11078}
11079
33c5cd75 11080/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11081 Note: This function processes DWO files only, not DWP files. */
3019eac3 11082
33c5cd75 11083static void
ed2dc618 11084create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11085 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11086 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11087{
11088 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11089 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11090
96b79293 11091 section.read (objfile);
33c5cd75 11092 info_ptr = section.buffer;
3019eac3
DE
11093
11094 if (info_ptr == NULL)
33c5cd75 11095 return;
3019eac3 11096
b4f54984 11097 if (dwarf_read_debug)
19c3d4c9
DE
11098 {
11099 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11100 section.get_name (),
11101 section.get_file_name ());
19c3d4c9 11102 }
3019eac3 11103
33c5cd75 11104 end_ptr = info_ptr + section.size;
3019eac3
DE
11105 while (info_ptr < end_ptr)
11106 {
11107 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11108 struct dwo_unit read_unit {};
33c5cd75
DB
11109 struct dwo_unit *dwo_unit;
11110 void **slot;
11111 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11112
11113 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11114 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11115 per_cu.is_debug_types = 0;
33c5cd75
DB
11116 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11117 per_cu.section = &section;
11118
c0ab21c2
TT
11119 cutu_reader reader (&per_cu, cu, &dwo_file);
11120 if (!reader.dummy_p)
11121 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11122 &dwo_file, &read_unit);
33c5cd75
DB
11123 info_ptr += per_cu.length;
11124
11125 // If the unit could not be parsed, skip it.
c0ab21c2 11126 if (read_unit.dwo_file == NULL)
33c5cd75 11127 continue;
3019eac3 11128
33c5cd75 11129 if (cus_htab == NULL)
298e9637 11130 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11131
33c5cd75 11132 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11133 *dwo_unit = read_unit;
b0b6a987 11134 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11135 gdb_assert (slot != NULL);
11136 if (*slot != NULL)
19c3d4c9 11137 {
33c5cd75
DB
11138 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11139 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11140
b98664d3 11141 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11142 " the entry at offset %s, signature %s"),
11143 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11144 hex_string (dwo_unit->signature));
19c3d4c9 11145 }
33c5cd75 11146 *slot = (void *)dwo_unit;
3019eac3 11147 }
3019eac3
DE
11148}
11149
80626a55
DE
11150/* DWP file .debug_{cu,tu}_index section format:
11151 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11152
d2415c6c
DE
11153 DWP Version 1:
11154
80626a55
DE
11155 Both index sections have the same format, and serve to map a 64-bit
11156 signature to a set of section numbers. Each section begins with a header,
11157 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11158 indexes, and a pool of 32-bit section numbers. The index sections will be
11159 aligned at 8-byte boundaries in the file.
11160
d2415c6c
DE
11161 The index section header consists of:
11162
11163 V, 32 bit version number
11164 -, 32 bits unused
11165 N, 32 bit number of compilation units or type units in the index
11166 M, 32 bit number of slots in the hash table
80626a55 11167
d2415c6c 11168 Numbers are recorded using the byte order of the application binary.
80626a55 11169
d2415c6c
DE
11170 The hash table begins at offset 16 in the section, and consists of an array
11171 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11172 order of the application binary). Unused slots in the hash table are 0.
11173 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11174
d2415c6c
DE
11175 The parallel table begins immediately after the hash table
11176 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11177 array of 32-bit indexes (using the byte order of the application binary),
11178 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11179 table contains a 32-bit index into the pool of section numbers. For unused
11180 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11181
73869dc2
DE
11182 The pool of section numbers begins immediately following the hash table
11183 (at offset 16 + 12 * M from the beginning of the section). The pool of
11184 section numbers consists of an array of 32-bit words (using the byte order
11185 of the application binary). Each item in the array is indexed starting
11186 from 0. The hash table entry provides the index of the first section
11187 number in the set. Additional section numbers in the set follow, and the
11188 set is terminated by a 0 entry (section number 0 is not used in ELF).
11189
11190 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11191 section must be the first entry in the set, and the .debug_abbrev.dwo must
11192 be the second entry. Other members of the set may follow in any order.
11193
11194 ---
11195
11196 DWP Version 2:
11197
11198 DWP Version 2 combines all the .debug_info, etc. sections into one,
11199 and the entries in the index tables are now offsets into these sections.
11200 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11201 section.
11202
11203 Index Section Contents:
11204 Header
11205 Hash Table of Signatures dwp_hash_table.hash_table
11206 Parallel Table of Indices dwp_hash_table.unit_table
11207 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11208 Table of Section Sizes dwp_hash_table.v2.sizes
11209
11210 The index section header consists of:
11211
11212 V, 32 bit version number
11213 L, 32 bit number of columns in the table of section offsets
11214 N, 32 bit number of compilation units or type units in the index
11215 M, 32 bit number of slots in the hash table
11216
11217 Numbers are recorded using the byte order of the application binary.
11218
11219 The hash table has the same format as version 1.
11220 The parallel table of indices has the same format as version 1,
11221 except that the entries are origin-1 indices into the table of sections
11222 offsets and the table of section sizes.
11223
11224 The table of offsets begins immediately following the parallel table
11225 (at offset 16 + 12 * M from the beginning of the section). The table is
11226 a two-dimensional array of 32-bit words (using the byte order of the
11227 application binary), with L columns and N+1 rows, in row-major order.
11228 Each row in the array is indexed starting from 0. The first row provides
11229 a key to the remaining rows: each column in this row provides an identifier
11230 for a debug section, and the offsets in the same column of subsequent rows
11231 refer to that section. The section identifiers are:
11232
11233 DW_SECT_INFO 1 .debug_info.dwo
11234 DW_SECT_TYPES 2 .debug_types.dwo
11235 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11236 DW_SECT_LINE 4 .debug_line.dwo
11237 DW_SECT_LOC 5 .debug_loc.dwo
11238 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11239 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11240 DW_SECT_MACRO 8 .debug_macro.dwo
11241
11242 The offsets provided by the CU and TU index sections are the base offsets
11243 for the contributions made by each CU or TU to the corresponding section
11244 in the package file. Each CU and TU header contains an abbrev_offset
11245 field, used to find the abbreviations table for that CU or TU within the
11246 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11247 be interpreted as relative to the base offset given in the index section.
11248 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11249 should be interpreted as relative to the base offset for .debug_line.dwo,
11250 and offsets into other debug sections obtained from DWARF attributes should
11251 also be interpreted as relative to the corresponding base offset.
11252
11253 The table of sizes begins immediately following the table of offsets.
11254 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11255 with L columns and N rows, in row-major order. Each row in the array is
11256 indexed starting from 1 (row 0 is shared by the two tables).
11257
11258 ---
11259
11260 Hash table lookup is handled the same in version 1 and 2:
11261
11262 We assume that N and M will not exceed 2^32 - 1.
11263 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11264
d2415c6c
DE
11265 Given a 64-bit compilation unit signature or a type signature S, an entry
11266 in the hash table is located as follows:
80626a55 11267
d2415c6c
DE
11268 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11269 the low-order k bits all set to 1.
80626a55 11270
d2415c6c 11271 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11272
d2415c6c
DE
11273 3) If the hash table entry at index H matches the signature, use that
11274 entry. If the hash table entry at index H is unused (all zeroes),
11275 terminate the search: the signature is not present in the table.
80626a55 11276
d2415c6c 11277 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11278
d2415c6c 11279 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11280 to stop at an unused slot or find the match. */
80626a55
DE
11281
11282/* Create a hash table to map DWO IDs to their CU/TU entry in
11283 .debug_{info,types}.dwo in DWP_FILE.
11284 Returns NULL if there isn't one.
11285 Note: This function processes DWP files only, not DWO files. */
11286
11287static struct dwp_hash_table *
ed2dc618
SM
11288create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11289 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11290{
11291 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11292 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11293 const gdb_byte *index_ptr, *index_end;
80626a55 11294 struct dwarf2_section_info *index;
73869dc2 11295 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11296 struct dwp_hash_table *htab;
11297
11298 if (is_debug_types)
11299 index = &dwp_file->sections.tu_index;
11300 else
11301 index = &dwp_file->sections.cu_index;
11302
96b79293 11303 if (index->empty ())
80626a55 11304 return NULL;
96b79293 11305 index->read (objfile);
80626a55
DE
11306
11307 index_ptr = index->buffer;
11308 index_end = index_ptr + index->size;
11309
11310 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11311 index_ptr += 4;
11312 if (version == 2)
11313 nr_columns = read_4_bytes (dbfd, index_ptr);
11314 else
11315 nr_columns = 0;
11316 index_ptr += 4;
80626a55
DE
11317 nr_units = read_4_bytes (dbfd, index_ptr);
11318 index_ptr += 4;
11319 nr_slots = read_4_bytes (dbfd, index_ptr);
11320 index_ptr += 4;
11321
73869dc2 11322 if (version != 1 && version != 2)
80626a55 11323 {
21aa081e 11324 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11325 " [in module %s]"),
21aa081e 11326 pulongest (version), dwp_file->name);
80626a55
DE
11327 }
11328 if (nr_slots != (nr_slots & -nr_slots))
11329 {
21aa081e 11330 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11331 " is not power of 2 [in module %s]"),
21aa081e 11332 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11333 }
11334
11335 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11336 htab->version = version;
11337 htab->nr_columns = nr_columns;
80626a55
DE
11338 htab->nr_units = nr_units;
11339 htab->nr_slots = nr_slots;
11340 htab->hash_table = index_ptr;
11341 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11342
11343 /* Exit early if the table is empty. */
11344 if (nr_slots == 0 || nr_units == 0
11345 || (version == 2 && nr_columns == 0))
11346 {
11347 /* All must be zero. */
11348 if (nr_slots != 0 || nr_units != 0
11349 || (version == 2 && nr_columns != 0))
11350 {
b98664d3 11351 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11352 " all zero [in modules %s]"),
11353 dwp_file->name);
11354 }
11355 return htab;
11356 }
11357
11358 if (version == 1)
11359 {
11360 htab->section_pool.v1.indices =
11361 htab->unit_table + sizeof (uint32_t) * nr_slots;
11362 /* It's harder to decide whether the section is too small in v1.
11363 V1 is deprecated anyway so we punt. */
11364 }
11365 else
11366 {
11367 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11368 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11369 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11370 /* Reverse map for error checking. */
11371 int ids_seen[DW_SECT_MAX + 1];
11372 int i;
11373
11374 if (nr_columns < 2)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, too few columns"
11377 " in section table [in module %s]"),
11378 dwp_file->name);
11379 }
11380 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11381 {
11382 error (_("Dwarf Error: bad DWP hash table, too many columns"
11383 " in section table [in module %s]"),
11384 dwp_file->name);
11385 }
04fd5eed
GB
11386 memset (ids, 255, sizeof_ids);
11387 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11388 for (i = 0; i < nr_columns; ++i)
11389 {
11390 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11391
11392 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11393 {
11394 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11395 " in section table [in module %s]"),
11396 id, dwp_file->name);
11397 }
11398 if (ids_seen[id] != -1)
11399 {
11400 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11401 " id %d in section table [in module %s]"),
11402 id, dwp_file->name);
11403 }
11404 ids_seen[id] = i;
11405 ids[i] = id;
11406 }
11407 /* Must have exactly one info or types section. */
11408 if (((ids_seen[DW_SECT_INFO] != -1)
11409 + (ids_seen[DW_SECT_TYPES] != -1))
11410 != 1)
11411 {
11412 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11413 " DWO info/types section [in module %s]"),
11414 dwp_file->name);
11415 }
11416 /* Must have an abbrev section. */
11417 if (ids_seen[DW_SECT_ABBREV] == -1)
11418 {
11419 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11420 " section [in module %s]"),
11421 dwp_file->name);
11422 }
11423 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11424 htab->section_pool.v2.sizes =
11425 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11426 * nr_units * nr_columns);
11427 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11428 * nr_units * nr_columns))
11429 > index_end)
11430 {
11431 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11432 " [in module %s]"),
11433 dwp_file->name);
11434 }
11435 }
80626a55
DE
11436
11437 return htab;
11438}
11439
11440/* Update SECTIONS with the data from SECTP.
11441
11442 This function is like the other "locate" section routines that are
11443 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11444 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11445
11446 The result is non-zero for success, or zero if an error was found. */
11447
11448static int
73869dc2
DE
11449locate_v1_virtual_dwo_sections (asection *sectp,
11450 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11451{
11452 const struct dwop_section_names *names = &dwop_section_names;
11453
11454 if (section_is_p (sectp->name, &names->abbrev_dwo))
11455 {
11456 /* There can be only one. */
049412e3 11457 if (sections->abbrev.s.section != NULL)
80626a55 11458 return 0;
049412e3 11459 sections->abbrev.s.section = sectp;
fd361982 11460 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11461 }
11462 else if (section_is_p (sectp->name, &names->info_dwo)
11463 || section_is_p (sectp->name, &names->types_dwo))
11464 {
11465 /* There can be only one. */
049412e3 11466 if (sections->info_or_types.s.section != NULL)
80626a55 11467 return 0;
049412e3 11468 sections->info_or_types.s.section = sectp;
fd361982 11469 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11470 }
11471 else if (section_is_p (sectp->name, &names->line_dwo))
11472 {
11473 /* There can be only one. */
049412e3 11474 if (sections->line.s.section != NULL)
80626a55 11475 return 0;
049412e3 11476 sections->line.s.section = sectp;
fd361982 11477 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11478 }
11479 else if (section_is_p (sectp->name, &names->loc_dwo))
11480 {
11481 /* There can be only one. */
049412e3 11482 if (sections->loc.s.section != NULL)
80626a55 11483 return 0;
049412e3 11484 sections->loc.s.section = sectp;
fd361982 11485 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11486 }
11487 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11488 {
11489 /* There can be only one. */
049412e3 11490 if (sections->macinfo.s.section != NULL)
80626a55 11491 return 0;
049412e3 11492 sections->macinfo.s.section = sectp;
fd361982 11493 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11494 }
11495 else if (section_is_p (sectp->name, &names->macro_dwo))
11496 {
11497 /* There can be only one. */
049412e3 11498 if (sections->macro.s.section != NULL)
80626a55 11499 return 0;
049412e3 11500 sections->macro.s.section = sectp;
fd361982 11501 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11502 }
11503 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11504 {
11505 /* There can be only one. */
049412e3 11506 if (sections->str_offsets.s.section != NULL)
80626a55 11507 return 0;
049412e3 11508 sections->str_offsets.s.section = sectp;
fd361982 11509 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11510 }
11511 else
11512 {
11513 /* No other kind of section is valid. */
11514 return 0;
11515 }
11516
11517 return 1;
11518}
11519
73869dc2
DE
11520/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11521 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11522 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11523 This is for DWP version 1 files. */
80626a55
DE
11524
11525static struct dwo_unit *
ed2dc618
SM
11526create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11527 struct dwp_file *dwp_file,
73869dc2
DE
11528 uint32_t unit_index,
11529 const char *comp_dir,
11530 ULONGEST signature, int is_debug_types)
80626a55
DE
11531{
11532 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11533 const struct dwp_hash_table *dwp_htab =
11534 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11535 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11536 const char *kind = is_debug_types ? "TU" : "CU";
11537 struct dwo_file *dwo_file;
11538 struct dwo_unit *dwo_unit;
73869dc2 11539 struct virtual_v1_dwo_sections sections;
80626a55 11540 void **dwo_file_slot;
80626a55
DE
11541 int i;
11542
73869dc2
DE
11543 gdb_assert (dwp_file->version == 1);
11544
b4f54984 11545 if (dwarf_read_debug)
80626a55 11546 {
73869dc2 11547 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11548 kind,
73869dc2 11549 pulongest (unit_index), hex_string (signature),
80626a55
DE
11550 dwp_file->name);
11551 }
11552
19ac8c2e 11553 /* Fetch the sections of this DWO unit.
80626a55
DE
11554 Put a limit on the number of sections we look for so that bad data
11555 doesn't cause us to loop forever. */
11556
73869dc2 11557#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11558 (1 /* .debug_info or .debug_types */ \
11559 + 1 /* .debug_abbrev */ \
11560 + 1 /* .debug_line */ \
11561 + 1 /* .debug_loc */ \
11562 + 1 /* .debug_str_offsets */ \
19ac8c2e 11563 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11564 + 1 /* trailing zero */)
11565
11566 memset (&sections, 0, sizeof (sections));
80626a55 11567
73869dc2 11568 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11569 {
11570 asection *sectp;
11571 uint32_t section_nr =
11572 read_4_bytes (dbfd,
73869dc2
DE
11573 dwp_htab->section_pool.v1.indices
11574 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11575
11576 if (section_nr == 0)
11577 break;
11578 if (section_nr >= dwp_file->num_sections)
11579 {
11580 error (_("Dwarf Error: bad DWP hash table, section number too large"
11581 " [in module %s]"),
11582 dwp_file->name);
11583 }
11584
11585 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11586 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11587 {
11588 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11589 " [in module %s]"),
11590 dwp_file->name);
11591 }
11592 }
11593
11594 if (i < 2
96b79293
TT
11595 || sections.info_or_types.empty ()
11596 || sections.abbrev.empty ())
80626a55
DE
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11599 " [in module %s]"),
11600 dwp_file->name);
11601 }
73869dc2 11602 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11603 {
11604 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11605 " [in module %s]"),
11606 dwp_file->name);
11607 }
11608
11609 /* It's easier for the rest of the code if we fake a struct dwo_file and
11610 have dwo_unit "live" in that. At least for now.
11611
11612 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11613 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11614 file, we can combine them back into a virtual DWO file to save space
11615 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11616 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11617
791afaa2
TT
11618 std::string virtual_dwo_name =
11619 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11620 sections.abbrev.get_id (),
11621 sections.line.get_id (),
11622 sections.loc.get_id (),
11623 sections.str_offsets.get_id ());
80626a55 11624 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11625 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11626 virtual_dwo_name.c_str (),
11627 comp_dir);
80626a55
DE
11628 /* Create one if necessary. */
11629 if (*dwo_file_slot == NULL)
11630 {
b4f54984 11631 if (dwarf_read_debug)
80626a55
DE
11632 {
11633 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11634 virtual_dwo_name.c_str ());
80626a55 11635 }
51ac9db5 11636 dwo_file = new struct dwo_file;
be1e3d3e 11637 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11638 dwo_file->comp_dir = comp_dir;
80626a55
DE
11639 dwo_file->sections.abbrev = sections.abbrev;
11640 dwo_file->sections.line = sections.line;
11641 dwo_file->sections.loc = sections.loc;
11642 dwo_file->sections.macinfo = sections.macinfo;
11643 dwo_file->sections.macro = sections.macro;
11644 dwo_file->sections.str_offsets = sections.str_offsets;
11645 /* The "str" section is global to the entire DWP file. */
11646 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11647 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11648 there's no need to record it in dwo_file.
11649 Also, we can't simply record type sections in dwo_file because
11650 we record a pointer into the vector in dwo_unit. As we collect more
11651 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11652 for it, invalidating all copies of pointers into the previous
11653 contents. */
80626a55
DE
11654 *dwo_file_slot = dwo_file;
11655 }
11656 else
11657 {
b4f54984 11658 if (dwarf_read_debug)
80626a55
DE
11659 {
11660 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11661 virtual_dwo_name.c_str ());
80626a55 11662 }
9a3c8263 11663 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11664 }
80626a55
DE
11665
11666 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11667 dwo_unit->dwo_file = dwo_file;
11668 dwo_unit->signature = signature;
8d749320
SM
11669 dwo_unit->section =
11670 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11671 *dwo_unit->section = sections.info_or_types;
57d63ce2 11672 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11673
11674 return dwo_unit;
11675}
11676
73869dc2
DE
11677/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11678 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11679 piece within that section used by a TU/CU, return a virtual section
11680 of just that piece. */
11681
11682static struct dwarf2_section_info
ed2dc618
SM
11683create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11684 struct dwarf2_section_info *section,
73869dc2
DE
11685 bfd_size_type offset, bfd_size_type size)
11686{
11687 struct dwarf2_section_info result;
11688 asection *sectp;
11689
11690 gdb_assert (section != NULL);
11691 gdb_assert (!section->is_virtual);
11692
11693 memset (&result, 0, sizeof (result));
11694 result.s.containing_section = section;
dc4ccb6f 11695 result.is_virtual = true;
73869dc2
DE
11696
11697 if (size == 0)
11698 return result;
11699
96b79293 11700 sectp = section->get_bfd_section ();
73869dc2
DE
11701
11702 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11703 bounds of the real section. This is a pretty-rare event, so just
11704 flag an error (easier) instead of a warning and trying to cope. */
11705 if (sectp == NULL
fd361982 11706 || offset + size > bfd_section_size (sectp))
73869dc2 11707 {
73869dc2
DE
11708 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11709 " in section %s [in module %s]"),
fd361982 11710 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11711 objfile_name (dwarf2_per_objfile->objfile));
11712 }
11713
11714 result.virtual_offset = offset;
11715 result.size = size;
11716 return result;
11717}
11718
11719/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11720 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11721 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11722 This is for DWP version 2 files. */
11723
11724static struct dwo_unit *
ed2dc618
SM
11725create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11726 struct dwp_file *dwp_file,
73869dc2
DE
11727 uint32_t unit_index,
11728 const char *comp_dir,
11729 ULONGEST signature, int is_debug_types)
11730{
11731 struct objfile *objfile = dwarf2_per_objfile->objfile;
11732 const struct dwp_hash_table *dwp_htab =
11733 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11734 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11735 const char *kind = is_debug_types ? "TU" : "CU";
11736 struct dwo_file *dwo_file;
11737 struct dwo_unit *dwo_unit;
11738 struct virtual_v2_dwo_sections sections;
11739 void **dwo_file_slot;
73869dc2
DE
11740 int i;
11741
11742 gdb_assert (dwp_file->version == 2);
11743
b4f54984 11744 if (dwarf_read_debug)
73869dc2
DE
11745 {
11746 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11747 kind,
11748 pulongest (unit_index), hex_string (signature),
11749 dwp_file->name);
11750 }
11751
11752 /* Fetch the section offsets of this DWO unit. */
11753
11754 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11755
11756 for (i = 0; i < dwp_htab->nr_columns; ++i)
11757 {
11758 uint32_t offset = read_4_bytes (dbfd,
11759 dwp_htab->section_pool.v2.offsets
11760 + (((unit_index - 1) * dwp_htab->nr_columns
11761 + i)
11762 * sizeof (uint32_t)));
11763 uint32_t size = read_4_bytes (dbfd,
11764 dwp_htab->section_pool.v2.sizes
11765 + (((unit_index - 1) * dwp_htab->nr_columns
11766 + i)
11767 * sizeof (uint32_t)));
11768
11769 switch (dwp_htab->section_pool.v2.section_ids[i])
11770 {
11771 case DW_SECT_INFO:
11772 case DW_SECT_TYPES:
11773 sections.info_or_types_offset = offset;
11774 sections.info_or_types_size = size;
11775 break;
11776 case DW_SECT_ABBREV:
11777 sections.abbrev_offset = offset;
11778 sections.abbrev_size = size;
11779 break;
11780 case DW_SECT_LINE:
11781 sections.line_offset = offset;
11782 sections.line_size = size;
11783 break;
11784 case DW_SECT_LOC:
11785 sections.loc_offset = offset;
11786 sections.loc_size = size;
11787 break;
11788 case DW_SECT_STR_OFFSETS:
11789 sections.str_offsets_offset = offset;
11790 sections.str_offsets_size = size;
11791 break;
11792 case DW_SECT_MACINFO:
11793 sections.macinfo_offset = offset;
11794 sections.macinfo_size = size;
11795 break;
11796 case DW_SECT_MACRO:
11797 sections.macro_offset = offset;
11798 sections.macro_size = size;
11799 break;
11800 }
11801 }
11802
11803 /* It's easier for the rest of the code if we fake a struct dwo_file and
11804 have dwo_unit "live" in that. At least for now.
11805
11806 The DWP file can be made up of a random collection of CUs and TUs.
11807 However, for each CU + set of TUs that came from the same original DWO
11808 file, we can combine them back into a virtual DWO file to save space
11809 (fewer struct dwo_file objects to allocate). Remember that for really
11810 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11811
791afaa2
TT
11812 std::string virtual_dwo_name =
11813 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11814 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11815 (long) (sections.line_size ? sections.line_offset : 0),
11816 (long) (sections.loc_size ? sections.loc_offset : 0),
11817 (long) (sections.str_offsets_size
11818 ? sections.str_offsets_offset : 0));
73869dc2 11819 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11820 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11821 virtual_dwo_name.c_str (),
11822 comp_dir);
73869dc2
DE
11823 /* Create one if necessary. */
11824 if (*dwo_file_slot == NULL)
11825 {
b4f54984 11826 if (dwarf_read_debug)
73869dc2
DE
11827 {
11828 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11829 virtual_dwo_name.c_str ());
73869dc2 11830 }
51ac9db5 11831 dwo_file = new struct dwo_file;
be1e3d3e 11832 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11833 dwo_file->comp_dir = comp_dir;
11834 dwo_file->sections.abbrev =
ed2dc618 11835 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11836 sections.abbrev_offset, sections.abbrev_size);
11837 dwo_file->sections.line =
ed2dc618 11838 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11839 sections.line_offset, sections.line_size);
11840 dwo_file->sections.loc =
ed2dc618 11841 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11842 sections.loc_offset, sections.loc_size);
11843 dwo_file->sections.macinfo =
ed2dc618 11844 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11845 sections.macinfo_offset, sections.macinfo_size);
11846 dwo_file->sections.macro =
ed2dc618 11847 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11848 sections.macro_offset, sections.macro_size);
11849 dwo_file->sections.str_offsets =
ed2dc618
SM
11850 create_dwp_v2_section (dwarf2_per_objfile,
11851 &dwp_file->sections.str_offsets,
73869dc2
DE
11852 sections.str_offsets_offset,
11853 sections.str_offsets_size);
11854 /* The "str" section is global to the entire DWP file. */
11855 dwo_file->sections.str = dwp_file->sections.str;
11856 /* The info or types section is assigned below to dwo_unit,
11857 there's no need to record it in dwo_file.
11858 Also, we can't simply record type sections in dwo_file because
11859 we record a pointer into the vector in dwo_unit. As we collect more
11860 types we'll grow the vector and eventually have to reallocate space
11861 for it, invalidating all copies of pointers into the previous
11862 contents. */
11863 *dwo_file_slot = dwo_file;
11864 }
11865 else
11866 {
b4f54984 11867 if (dwarf_read_debug)
73869dc2
DE
11868 {
11869 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11870 virtual_dwo_name.c_str ());
73869dc2 11871 }
9a3c8263 11872 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11873 }
73869dc2
DE
11874
11875 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11876 dwo_unit->dwo_file = dwo_file;
11877 dwo_unit->signature = signature;
8d749320
SM
11878 dwo_unit->section =
11879 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11880 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11881 is_debug_types
73869dc2
DE
11882 ? &dwp_file->sections.types
11883 : &dwp_file->sections.info,
11884 sections.info_or_types_offset,
11885 sections.info_or_types_size);
11886 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11887
11888 return dwo_unit;
11889}
11890
57d63ce2
DE
11891/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11892 Returns NULL if the signature isn't found. */
80626a55
DE
11893
11894static struct dwo_unit *
ed2dc618
SM
11895lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11896 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11897 ULONGEST signature, int is_debug_types)
80626a55 11898{
57d63ce2
DE
11899 const struct dwp_hash_table *dwp_htab =
11900 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11901 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11902 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11903 uint32_t hash = signature & mask;
11904 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11905 unsigned int i;
11906 void **slot;
870f88f7 11907 struct dwo_unit find_dwo_cu;
80626a55
DE
11908
11909 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11910 find_dwo_cu.signature = signature;
19ac8c2e 11911 slot = htab_find_slot (is_debug_types
48b490f2
TT
11912 ? dwp_file->loaded_tus.get ()
11913 : dwp_file->loaded_cus.get (),
19ac8c2e 11914 &find_dwo_cu, INSERT);
80626a55
DE
11915
11916 if (*slot != NULL)
9a3c8263 11917 return (struct dwo_unit *) *slot;
80626a55
DE
11918
11919 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11920 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11921 {
11922 ULONGEST signature_in_table;
11923
11924 signature_in_table =
57d63ce2 11925 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11926 if (signature_in_table == signature)
11927 {
57d63ce2
DE
11928 uint32_t unit_index =
11929 read_4_bytes (dbfd,
11930 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11931
73869dc2
DE
11932 if (dwp_file->version == 1)
11933 {
ed2dc618
SM
11934 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11935 dwp_file, unit_index,
73869dc2
DE
11936 comp_dir, signature,
11937 is_debug_types);
11938 }
11939 else
11940 {
ed2dc618
SM
11941 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11942 dwp_file, unit_index,
73869dc2
DE
11943 comp_dir, signature,
11944 is_debug_types);
11945 }
9a3c8263 11946 return (struct dwo_unit *) *slot;
80626a55
DE
11947 }
11948 if (signature_in_table == 0)
11949 return NULL;
11950 hash = (hash + hash2) & mask;
11951 }
11952
11953 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11954 " [in module %s]"),
11955 dwp_file->name);
11956}
11957
ab5088bf 11958/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11959 Open the file specified by FILE_NAME and hand it off to BFD for
11960 preliminary analysis. Return a newly initialized bfd *, which
11961 includes a canonicalized copy of FILE_NAME.
80626a55 11962 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11963 SEARCH_CWD is true if the current directory is to be searched.
11964 It will be searched before debug-file-directory.
13aaf454
DE
11965 If successful, the file is added to the bfd include table of the
11966 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11967 If unable to find/open the file, return NULL.
3019eac3
DE
11968 NOTE: This function is derived from symfile_bfd_open. */
11969
192b62ce 11970static gdb_bfd_ref_ptr
ed2dc618
SM
11971try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11972 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11973{
24b9144d 11974 int desc;
9c02c129
DE
11975 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11976 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11977 to debug_file_directory. */
e0cc99a6 11978 const char *search_path;
9c02c129
DE
11979 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11980
e0cc99a6 11981 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11982 if (search_cwd)
11983 {
11984 if (*debug_file_directory != '\0')
e0cc99a6
TT
11985 {
11986 search_path_holder.reset (concat (".", dirname_separator_string,
11987 debug_file_directory,
11988 (char *) NULL));
11989 search_path = search_path_holder.get ();
11990 }
6ac97d4c 11991 else
e0cc99a6 11992 search_path = ".";
6ac97d4c 11993 }
9c02c129 11994 else
e0cc99a6 11995 search_path = debug_file_directory;
3019eac3 11996
24b9144d 11997 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
11998 if (is_dwp)
11999 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12000
12001 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12002 desc = openp (search_path, flags, file_name,
3019eac3
DE
12003 O_RDONLY | O_BINARY, &absolute_name);
12004 if (desc < 0)
12005 return NULL;
12006
e0cc99a6
TT
12007 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12008 gnutarget, desc));
9c02c129
DE
12009 if (sym_bfd == NULL)
12010 return NULL;
192b62ce 12011 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12012
192b62ce
TT
12013 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12014 return NULL;
3019eac3 12015
13aaf454
DE
12016 /* Success. Record the bfd as having been included by the objfile's bfd.
12017 This is important because things like demangled_names_hash lives in the
12018 objfile's per_bfd space and may have references to things like symbol
12019 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12020 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12021
3019eac3
DE
12022 return sym_bfd;
12023}
12024
ab5088bf 12025/* Try to open DWO file FILE_NAME.
3019eac3
DE
12026 COMP_DIR is the DW_AT_comp_dir attribute.
12027 The result is the bfd handle of the file.
12028 If there is a problem finding or opening the file, return NULL.
12029 Upon success, the canonicalized path of the file is stored in the bfd,
12030 same as symfile_bfd_open. */
12031
192b62ce 12032static gdb_bfd_ref_ptr
ed2dc618
SM
12033open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12034 const char *file_name, const char *comp_dir)
3019eac3 12035{
80626a55 12036 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12037 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12038 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12039
12040 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12041
12042 if (comp_dir != NULL)
12043 {
43816ebc
TT
12044 gdb::unique_xmalloc_ptr<char> path_to_try
12045 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12046
12047 /* NOTE: If comp_dir is a relative path, this will also try the
12048 search path, which seems useful. */
ed2dc618 12049 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12050 path_to_try.get (),
ed2dc618 12051 0 /*is_dwp*/,
192b62ce 12052 1 /*search_cwd*/));
3019eac3
DE
12053 if (abfd != NULL)
12054 return abfd;
12055 }
12056
12057 /* That didn't work, try debug-file-directory, which, despite its name,
12058 is a list of paths. */
12059
12060 if (*debug_file_directory == '\0')
12061 return NULL;
12062
ed2dc618
SM
12063 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12064 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12065}
12066
80626a55
DE
12067/* This function is mapped across the sections and remembers the offset and
12068 size of each of the DWO debugging sections we are interested in. */
12069
12070static void
12071dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12072{
9a3c8263 12073 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12074 const struct dwop_section_names *names = &dwop_section_names;
12075
12076 if (section_is_p (sectp->name, &names->abbrev_dwo))
12077 {
049412e3 12078 dwo_sections->abbrev.s.section = sectp;
fd361982 12079 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12080 }
12081 else if (section_is_p (sectp->name, &names->info_dwo))
12082 {
049412e3 12083 dwo_sections->info.s.section = sectp;
fd361982 12084 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12085 }
12086 else if (section_is_p (sectp->name, &names->line_dwo))
12087 {
049412e3 12088 dwo_sections->line.s.section = sectp;
fd361982 12089 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12090 }
12091 else if (section_is_p (sectp->name, &names->loc_dwo))
12092 {
049412e3 12093 dwo_sections->loc.s.section = sectp;
fd361982 12094 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12095 }
41144253 12096 else if (section_is_p (sectp->name, &names->loclists_dwo))
12097 {
12098 dwo_sections->loclists.s.section = sectp;
12099 dwo_sections->loclists.size = bfd_section_size (sectp);
12100 }
80626a55
DE
12101 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12102 {
049412e3 12103 dwo_sections->macinfo.s.section = sectp;
fd361982 12104 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12105 }
12106 else if (section_is_p (sectp->name, &names->macro_dwo))
12107 {
049412e3 12108 dwo_sections->macro.s.section = sectp;
fd361982 12109 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12110 }
12111 else if (section_is_p (sectp->name, &names->str_dwo))
12112 {
049412e3 12113 dwo_sections->str.s.section = sectp;
fd361982 12114 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12115 }
12116 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12117 {
049412e3 12118 dwo_sections->str_offsets.s.section = sectp;
fd361982 12119 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12120 }
12121 else if (section_is_p (sectp->name, &names->types_dwo))
12122 {
12123 struct dwarf2_section_info type_section;
12124
12125 memset (&type_section, 0, sizeof (type_section));
049412e3 12126 type_section.s.section = sectp;
fd361982 12127 type_section.size = bfd_section_size (sectp);
fd5866f6 12128 dwo_sections->types.push_back (type_section);
80626a55
DE
12129 }
12130}
12131
ab5088bf 12132/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12133 by PER_CU. This is for the non-DWP case.
80626a55 12134 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12135
12136static struct dwo_file *
0ac5b59e
DE
12137open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12138 const char *dwo_name, const char *comp_dir)
3019eac3 12139{
ed2dc618 12140 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12141
fb1eb2f9 12142 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12143 if (dbfd == NULL)
12144 {
b4f54984 12145 if (dwarf_read_debug)
80626a55
DE
12146 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12147 return NULL;
12148 }
263db9a1 12149
51ac9db5 12150 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12151 dwo_file->dwo_name = dwo_name;
12152 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12153 dwo_file->dbfd = std::move (dbfd);
3019eac3 12154
fb1eb2f9 12155 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12156 &dwo_file->sections);
3019eac3 12157
18a8505e
AT
12158 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12159 dwo_file->sections.info, dwo_file->cus);
3019eac3 12160
263db9a1 12161 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12162 dwo_file->sections.types, dwo_file->tus);
3019eac3 12163
b4f54984 12164 if (dwarf_read_debug)
80626a55
DE
12165 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12166
263db9a1 12167 return dwo_file.release ();
3019eac3
DE
12168}
12169
80626a55 12170/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12171 size of each of the DWP debugging sections common to version 1 and 2 that
12172 we are interested in. */
3019eac3 12173
80626a55 12174static void
73869dc2
DE
12175dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12176 void *dwp_file_ptr)
3019eac3 12177{
9a3c8263 12178 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12179 const struct dwop_section_names *names = &dwop_section_names;
12180 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12181
80626a55 12182 /* Record the ELF section number for later lookup: this is what the
73869dc2 12183 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12184 gdb_assert (elf_section_nr < dwp_file->num_sections);
12185 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12186
80626a55
DE
12187 /* Look for specific sections that we need. */
12188 if (section_is_p (sectp->name, &names->str_dwo))
12189 {
049412e3 12190 dwp_file->sections.str.s.section = sectp;
fd361982 12191 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12192 }
12193 else if (section_is_p (sectp->name, &names->cu_index))
12194 {
049412e3 12195 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12196 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12197 }
12198 else if (section_is_p (sectp->name, &names->tu_index))
12199 {
049412e3 12200 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12201 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12202 }
12203}
3019eac3 12204
73869dc2
DE
12205/* This function is mapped across the sections and remembers the offset and
12206 size of each of the DWP version 2 debugging sections that we are interested
12207 in. This is split into a separate function because we don't know if we
12208 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12209
12210static void
12211dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12212{
9a3c8263 12213 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12214 const struct dwop_section_names *names = &dwop_section_names;
12215 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12216
12217 /* Record the ELF section number for later lookup: this is what the
12218 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12219 gdb_assert (elf_section_nr < dwp_file->num_sections);
12220 dwp_file->elf_sections[elf_section_nr] = sectp;
12221
12222 /* Look for specific sections that we need. */
12223 if (section_is_p (sectp->name, &names->abbrev_dwo))
12224 {
049412e3 12225 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12226 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12227 }
12228 else if (section_is_p (sectp->name, &names->info_dwo))
12229 {
049412e3 12230 dwp_file->sections.info.s.section = sectp;
fd361982 12231 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12232 }
12233 else if (section_is_p (sectp->name, &names->line_dwo))
12234 {
049412e3 12235 dwp_file->sections.line.s.section = sectp;
fd361982 12236 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12237 }
12238 else if (section_is_p (sectp->name, &names->loc_dwo))
12239 {
049412e3 12240 dwp_file->sections.loc.s.section = sectp;
fd361982 12241 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12242 }
12243 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12244 {
049412e3 12245 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12246 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12247 }
12248 else if (section_is_p (sectp->name, &names->macro_dwo))
12249 {
049412e3 12250 dwp_file->sections.macro.s.section = sectp;
fd361982 12251 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12252 }
12253 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12254 {
049412e3 12255 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12256 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12257 }
12258 else if (section_is_p (sectp->name, &names->types_dwo))
12259 {
049412e3 12260 dwp_file->sections.types.s.section = sectp;
fd361982 12261 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12262 }
12263}
12264
80626a55 12265/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12266
80626a55
DE
12267static hashval_t
12268hash_dwp_loaded_cutus (const void *item)
12269{
9a3c8263 12270 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12271
80626a55
DE
12272 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12273 return dwo_unit->signature;
3019eac3
DE
12274}
12275
80626a55 12276/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12277
80626a55
DE
12278static int
12279eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12280{
9a3c8263
SM
12281 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12282 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12283
80626a55
DE
12284 return dua->signature == dub->signature;
12285}
3019eac3 12286
80626a55 12287/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12288
48b490f2 12289static htab_up
298e9637 12290allocate_dwp_loaded_cutus_table ()
80626a55 12291{
48b490f2
TT
12292 return htab_up (htab_create_alloc (3,
12293 hash_dwp_loaded_cutus,
12294 eq_dwp_loaded_cutus,
12295 NULL, xcalloc, xfree));
80626a55 12296}
3019eac3 12297
ab5088bf
DE
12298/* Try to open DWP file FILE_NAME.
12299 The result is the bfd handle of the file.
12300 If there is a problem finding or opening the file, return NULL.
12301 Upon success, the canonicalized path of the file is stored in the bfd,
12302 same as symfile_bfd_open. */
12303
192b62ce 12304static gdb_bfd_ref_ptr
ed2dc618
SM
12305open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12306 const char *file_name)
ab5088bf 12307{
ed2dc618
SM
12308 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12309 1 /*is_dwp*/,
192b62ce 12310 1 /*search_cwd*/));
6ac97d4c
DE
12311 if (abfd != NULL)
12312 return abfd;
12313
12314 /* Work around upstream bug 15652.
12315 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12316 [Whether that's a "bug" is debatable, but it is getting in our way.]
12317 We have no real idea where the dwp file is, because gdb's realpath-ing
12318 of the executable's path may have discarded the needed info.
12319 [IWBN if the dwp file name was recorded in the executable, akin to
12320 .gnu_debuglink, but that doesn't exist yet.]
12321 Strip the directory from FILE_NAME and search again. */
12322 if (*debug_file_directory != '\0')
12323 {
12324 /* Don't implicitly search the current directory here.
12325 If the user wants to search "." to handle this case,
12326 it must be added to debug-file-directory. */
ed2dc618
SM
12327 return try_open_dwop_file (dwarf2_per_objfile,
12328 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12329 0 /*search_cwd*/);
12330 }
12331
12332 return NULL;
ab5088bf
DE
12333}
12334
80626a55
DE
12335/* Initialize the use of the DWP file for the current objfile.
12336 By convention the name of the DWP file is ${objfile}.dwp.
12337 The result is NULL if it can't be found. */
a766d390 12338
400174b1 12339static std::unique_ptr<struct dwp_file>
ed2dc618 12340open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12341{
12342 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12343
82bf32bc
JK
12344 /* Try to find first .dwp for the binary file before any symbolic links
12345 resolving. */
6c447423
DE
12346
12347 /* If the objfile is a debug file, find the name of the real binary
12348 file and get the name of dwp file from there. */
d721ba37 12349 std::string dwp_name;
6c447423
DE
12350 if (objfile->separate_debug_objfile_backlink != NULL)
12351 {
12352 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12353 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12354
d721ba37 12355 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12356 }
12357 else
d721ba37
PA
12358 dwp_name = objfile->original_name;
12359
12360 dwp_name += ".dwp";
80626a55 12361
ed2dc618 12362 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12363 if (dbfd == NULL
12364 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12365 {
12366 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12367 dwp_name = objfile_name (objfile);
12368 dwp_name += ".dwp";
ed2dc618 12369 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12370 }
12371
80626a55
DE
12372 if (dbfd == NULL)
12373 {
b4f54984 12374 if (dwarf_read_debug)
d721ba37 12375 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12376 return std::unique_ptr<dwp_file> ();
3019eac3 12377 }
400174b1
TT
12378
12379 const char *name = bfd_get_filename (dbfd.get ());
12380 std::unique_ptr<struct dwp_file> dwp_file
12381 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12382
0a0f4c01 12383 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12384 dwp_file->elf_sections =
12385 OBSTACK_CALLOC (&objfile->objfile_obstack,
12386 dwp_file->num_sections, asection *);
12387
400174b1
TT
12388 bfd_map_over_sections (dwp_file->dbfd.get (),
12389 dwarf2_locate_common_dwp_sections,
12390 dwp_file.get ());
80626a55 12391
400174b1
TT
12392 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12393 0);
80626a55 12394
400174b1
TT
12395 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12396 1);
80626a55 12397
73869dc2 12398 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12399 if (dwp_file->cus && dwp_file->tus
12400 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12401 {
12402 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12403 pretty bizarre. We use pulongest here because that's the established
4d65956b 12404 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12405 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12406 " TU version %s [in DWP file %s]"),
12407 pulongest (dwp_file->cus->version),
d721ba37 12408 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12409 }
08302ed2
DE
12410
12411 if (dwp_file->cus)
12412 dwp_file->version = dwp_file->cus->version;
12413 else if (dwp_file->tus)
12414 dwp_file->version = dwp_file->tus->version;
12415 else
12416 dwp_file->version = 2;
73869dc2
DE
12417
12418 if (dwp_file->version == 2)
400174b1
TT
12419 bfd_map_over_sections (dwp_file->dbfd.get (),
12420 dwarf2_locate_v2_dwp_sections,
12421 dwp_file.get ());
73869dc2 12422
298e9637
SM
12423 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12424 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12425
b4f54984 12426 if (dwarf_read_debug)
80626a55
DE
12427 {
12428 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12429 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12430 " %s CUs, %s TUs\n",
12431 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12432 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12433 }
12434
12435 return dwp_file;
3019eac3 12436}
c906108c 12437
ab5088bf
DE
12438/* Wrapper around open_and_init_dwp_file, only open it once. */
12439
12440static struct dwp_file *
ed2dc618 12441get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12442{
12443 if (! dwarf2_per_objfile->dwp_checked)
12444 {
ed2dc618
SM
12445 dwarf2_per_objfile->dwp_file
12446 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12447 dwarf2_per_objfile->dwp_checked = 1;
12448 }
400174b1 12449 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12450}
12451
80626a55
DE
12452/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12453 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12454 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12455 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12456 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12457
12458 This is called, for example, when wanting to read a variable with a
12459 complex location. Therefore we don't want to do file i/o for every call.
12460 Therefore we don't want to look for a DWO file on every call.
12461 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12462 then we check if we've already seen DWO_NAME, and only THEN do we check
12463 for a DWO file.
12464
1c658ad5 12465 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12466 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12467
3019eac3 12468static struct dwo_unit *
80626a55
DE
12469lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12470 const char *dwo_name, const char *comp_dir,
12471 ULONGEST signature, int is_debug_types)
3019eac3 12472{
ed2dc618 12473 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12474 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12475 const char *kind = is_debug_types ? "TU" : "CU";
12476 void **dwo_file_slot;
3019eac3 12477 struct dwo_file *dwo_file;
80626a55 12478 struct dwp_file *dwp_file;
cb1df416 12479
6a506a2d
DE
12480 /* First see if there's a DWP file.
12481 If we have a DWP file but didn't find the DWO inside it, don't
12482 look for the original DWO file. It makes gdb behave differently
12483 depending on whether one is debugging in the build tree. */
cf2c3c16 12484
ed2dc618 12485 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12486 if (dwp_file != NULL)
cf2c3c16 12487 {
80626a55
DE
12488 const struct dwp_hash_table *dwp_htab =
12489 is_debug_types ? dwp_file->tus : dwp_file->cus;
12490
12491 if (dwp_htab != NULL)
12492 {
12493 struct dwo_unit *dwo_cutu =
ed2dc618 12494 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12495 signature, is_debug_types);
80626a55
DE
12496
12497 if (dwo_cutu != NULL)
12498 {
b4f54984 12499 if (dwarf_read_debug)
80626a55
DE
12500 {
12501 fprintf_unfiltered (gdb_stdlog,
12502 "Virtual DWO %s %s found: @%s\n",
12503 kind, hex_string (signature),
12504 host_address_to_string (dwo_cutu));
12505 }
12506 return dwo_cutu;
12507 }
12508 }
12509 }
6a506a2d 12510 else
80626a55 12511 {
6a506a2d 12512 /* No DWP file, look for the DWO file. */
80626a55 12513
ed2dc618
SM
12514 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12515 dwo_name, comp_dir);
6a506a2d 12516 if (*dwo_file_slot == NULL)
80626a55 12517 {
6a506a2d
DE
12518 /* Read in the file and build a table of the CUs/TUs it contains. */
12519 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12520 }
6a506a2d 12521 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12522 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12523
6a506a2d 12524 if (dwo_file != NULL)
19c3d4c9 12525 {
6a506a2d
DE
12526 struct dwo_unit *dwo_cutu = NULL;
12527
12528 if (is_debug_types && dwo_file->tus)
12529 {
12530 struct dwo_unit find_dwo_cutu;
12531
12532 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12533 find_dwo_cutu.signature = signature;
9a3c8263 12534 dwo_cutu
b0b6a987
TT
12535 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12536 &find_dwo_cutu);
6a506a2d 12537 }
33c5cd75 12538 else if (!is_debug_types && dwo_file->cus)
80626a55 12539 {
33c5cd75
DB
12540 struct dwo_unit find_dwo_cutu;
12541
12542 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12543 find_dwo_cutu.signature = signature;
b0b6a987 12544 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12545 &find_dwo_cutu);
6a506a2d
DE
12546 }
12547
12548 if (dwo_cutu != NULL)
12549 {
b4f54984 12550 if (dwarf_read_debug)
6a506a2d
DE
12551 {
12552 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12553 kind, dwo_name, hex_string (signature),
12554 host_address_to_string (dwo_cutu));
12555 }
12556 return dwo_cutu;
80626a55
DE
12557 }
12558 }
2e276125 12559 }
9cdd5dbd 12560
80626a55
DE
12561 /* We didn't find it. This could mean a dwo_id mismatch, or
12562 someone deleted the DWO/DWP file, or the search path isn't set up
12563 correctly to find the file. */
12564
b4f54984 12565 if (dwarf_read_debug)
80626a55
DE
12566 {
12567 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12568 kind, dwo_name, hex_string (signature));
12569 }
3019eac3 12570
6656a72d
DE
12571 /* This is a warning and not a complaint because it can be caused by
12572 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12573 {
12574 /* Print the name of the DWP file if we looked there, helps the user
12575 better diagnose the problem. */
791afaa2 12576 std::string dwp_text;
43942612
DE
12577
12578 if (dwp_file != NULL)
791afaa2
TT
12579 dwp_text = string_printf (" [in DWP file %s]",
12580 lbasename (dwp_file->name));
43942612 12581
9d8780f0 12582 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12583 " [in module %s]"),
12584 kind, dwo_name, hex_string (signature),
791afaa2 12585 dwp_text.c_str (),
43942612 12586 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12587 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12588 }
3019eac3 12589 return NULL;
5fb290d7
DJ
12590}
12591
80626a55
DE
12592/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12593 See lookup_dwo_cutu_unit for details. */
12594
12595static struct dwo_unit *
12596lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12597 const char *dwo_name, const char *comp_dir,
12598 ULONGEST signature)
12599{
12600 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12601}
12602
12603/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12604 See lookup_dwo_cutu_unit for details. */
12605
12606static struct dwo_unit *
12607lookup_dwo_type_unit (struct signatured_type *this_tu,
12608 const char *dwo_name, const char *comp_dir)
12609{
12610 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12611}
12612
89e63ee4
DE
12613/* Traversal function for queue_and_load_all_dwo_tus. */
12614
12615static int
12616queue_and_load_dwo_tu (void **slot, void *info)
12617{
12618 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12619 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12620 ULONGEST signature = dwo_unit->signature;
12621 struct signatured_type *sig_type =
12622 lookup_dwo_signatured_type (per_cu->cu, signature);
12623
12624 if (sig_type != NULL)
12625 {
12626 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12627
12628 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12629 a real dependency of PER_CU on SIG_TYPE. That is detected later
12630 while processing PER_CU. */
12631 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12632 load_full_type_unit (sig_cu);
ae640021 12633 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12634 }
12635
12636 return 1;
12637}
12638
12639/* Queue all TUs contained in the DWO of PER_CU to be read in.
12640 The DWO may have the only definition of the type, though it may not be
12641 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12642 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12643
12644static void
12645queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12646{
12647 struct dwo_unit *dwo_unit;
12648 struct dwo_file *dwo_file;
12649
12650 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12651 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12652 gdb_assert (per_cu->cu != NULL);
12653
12654 dwo_unit = per_cu->cu->dwo_unit;
12655 gdb_assert (dwo_unit != NULL);
12656
12657 dwo_file = dwo_unit->dwo_file;
12658 if (dwo_file->tus != NULL)
b0b6a987
TT
12659 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12660 per_cu);
89e63ee4
DE
12661}
12662
3019eac3 12663/* Read in various DIEs. */
348e048f 12664
d389af10 12665/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12666 Inherit only the children of the DW_AT_abstract_origin DIE not being
12667 already referenced by DW_AT_abstract_origin from the children of the
12668 current DIE. */
d389af10
JK
12669
12670static void
12671inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12672{
12673 struct die_info *child_die;
791afaa2 12674 sect_offset *offsetp;
d389af10
JK
12675 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12676 struct die_info *origin_die;
12677 /* Iterator of the ORIGIN_DIE children. */
12678 struct die_info *origin_child_die;
d389af10 12679 struct attribute *attr;
cd02d79d
PA
12680 struct dwarf2_cu *origin_cu;
12681 struct pending **origin_previous_list_in_scope;
d389af10
JK
12682
12683 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12684 if (!attr)
12685 return;
12686
cd02d79d
PA
12687 /* Note that following die references may follow to a die in a
12688 different cu. */
12689
12690 origin_cu = cu;
12691 origin_die = follow_die_ref (die, attr, &origin_cu);
12692
12693 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12694 symbols in. */
12695 origin_previous_list_in_scope = origin_cu->list_in_scope;
12696 origin_cu->list_in_scope = cu->list_in_scope;
12697
edb3359d
DJ
12698 if (die->tag != origin_die->tag
12699 && !(die->tag == DW_TAG_inlined_subroutine
12700 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12701 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12702 sect_offset_str (die->sect_off),
12703 sect_offset_str (origin_die->sect_off));
d389af10 12704
791afaa2 12705 std::vector<sect_offset> offsets;
d389af10 12706
3ea89b92
PMR
12707 for (child_die = die->child;
12708 child_die && child_die->tag;
436c571c 12709 child_die = child_die->sibling)
3ea89b92
PMR
12710 {
12711 struct die_info *child_origin_die;
12712 struct dwarf2_cu *child_origin_cu;
12713
12714 /* We are trying to process concrete instance entries:
216f72a1 12715 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12716 it's not relevant to our analysis here. i.e. detecting DIEs that are
12717 present in the abstract instance but not referenced in the concrete
12718 one. */
216f72a1
JK
12719 if (child_die->tag == DW_TAG_call_site
12720 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12721 continue;
12722
c38f313d
DJ
12723 /* For each CHILD_DIE, find the corresponding child of
12724 ORIGIN_DIE. If there is more than one layer of
12725 DW_AT_abstract_origin, follow them all; there shouldn't be,
12726 but GCC versions at least through 4.4 generate this (GCC PR
12727 40573). */
3ea89b92
PMR
12728 child_origin_die = child_die;
12729 child_origin_cu = cu;
c38f313d
DJ
12730 while (1)
12731 {
cd02d79d
PA
12732 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12733 child_origin_cu);
c38f313d
DJ
12734 if (attr == NULL)
12735 break;
cd02d79d
PA
12736 child_origin_die = follow_die_ref (child_origin_die, attr,
12737 &child_origin_cu);
c38f313d
DJ
12738 }
12739
d389af10
JK
12740 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12741 counterpart may exist. */
c38f313d 12742 if (child_origin_die != child_die)
d389af10 12743 {
edb3359d
DJ
12744 if (child_die->tag != child_origin_die->tag
12745 && !(child_die->tag == DW_TAG_inlined_subroutine
12746 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12747 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12748 "different tags"),
9d8780f0
SM
12749 sect_offset_str (child_die->sect_off),
12750 sect_offset_str (child_origin_die->sect_off));
c38f313d 12751 if (child_origin_die->parent != origin_die)
b98664d3 12752 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12753 "different parents"),
9d8780f0
SM
12754 sect_offset_str (child_die->sect_off),
12755 sect_offset_str (child_origin_die->sect_off));
c38f313d 12756 else
791afaa2 12757 offsets.push_back (child_origin_die->sect_off);
d389af10 12758 }
d389af10 12759 }
791afaa2
TT
12760 std::sort (offsets.begin (), offsets.end ());
12761 sect_offset *offsets_end = offsets.data () + offsets.size ();
12762 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12763 if (offsetp[-1] == *offsetp)
b98664d3 12764 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12765 "to DIE %s as their abstract origin"),
12766 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12767
791afaa2 12768 offsetp = offsets.data ();
d389af10
JK
12769 origin_child_die = origin_die->child;
12770 while (origin_child_die && origin_child_die->tag)
12771 {
12772 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12773 while (offsetp < offsets_end
9c541725 12774 && *offsetp < origin_child_die->sect_off)
d389af10 12775 offsetp++;
b64f50a1 12776 if (offsetp >= offsets_end
9c541725 12777 || *offsetp > origin_child_die->sect_off)
d389af10 12778 {
adde2bff
DE
12779 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12780 Check whether we're already processing ORIGIN_CHILD_DIE.
12781 This can happen with mutually referenced abstract_origins.
12782 PR 16581. */
12783 if (!origin_child_die->in_process)
12784 process_die (origin_child_die, origin_cu);
d389af10 12785 }
436c571c 12786 origin_child_die = origin_child_die->sibling;
d389af10 12787 }
cd02d79d 12788 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12789
12790 if (cu != origin_cu)
12791 compute_delayed_physnames (origin_cu);
d389af10
JK
12792}
12793
c906108c 12794static void
e7c27a73 12795read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12796{
518817b3 12797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12799 struct context_stack *newobj;
c906108c
SS
12800 CORE_ADDR lowpc;
12801 CORE_ADDR highpc;
12802 struct die_info *child_die;
edb3359d 12803 struct attribute *attr, *call_line, *call_file;
15d034d0 12804 const char *name;
e142c38c 12805 CORE_ADDR baseaddr;
801e3a5b 12806 struct block *block;
edb3359d 12807 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12808 std::vector<struct symbol *> template_args;
34eaf542 12809 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12810
12811 if (inlined_func)
12812 {
12813 /* If we do not have call site information, we can't show the
12814 caller of this inlined function. That's too confusing, so
12815 only use the scope for local variables. */
12816 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12817 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12818 if (call_line == NULL || call_file == NULL)
12819 {
12820 read_lexical_block_scope (die, cu);
12821 return;
12822 }
12823 }
c906108c 12824
b3b3bada 12825 baseaddr = objfile->text_section_offset ();
e142c38c 12826
94af9270 12827 name = dwarf2_name (die, cu);
c906108c 12828
e8d05480
JB
12829 /* Ignore functions with missing or empty names. These are actually
12830 illegal according to the DWARF standard. */
12831 if (name == NULL)
12832 {
b98664d3 12833 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12834 sect_offset_str (die->sect_off));
e8d05480
JB
12835 return;
12836 }
12837
12838 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12839 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12840 <= PC_BOUNDS_INVALID)
e8d05480 12841 {
ae4d0c03
PM
12842 attr = dwarf2_attr (die, DW_AT_external, cu);
12843 if (!attr || !DW_UNSND (attr))
b98664d3 12844 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12845 "for subprogram DIE at %s"),
12846 sect_offset_str (die->sect_off));
e8d05480
JB
12847 return;
12848 }
c906108c 12849
3e29f34a
MR
12850 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12851 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12852
34eaf542
TT
12853 /* If we have any template arguments, then we must allocate a
12854 different sort of symbol. */
436c571c 12855 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12856 {
12857 if (child_die->tag == DW_TAG_template_type_param
12858 || child_die->tag == DW_TAG_template_value_param)
12859 {
e623cf5d 12860 templ_func = allocate_template_symbol (objfile);
cf724bc9 12861 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12862 break;
12863 }
12864 }
12865
c24bdb02 12866 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12867 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12868 (struct symbol *) templ_func);
4c2df51b 12869
81873cc8 12870 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12871 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12872 cu->language);
12873
4cecd739
DJ
12874 /* If there is a location expression for DW_AT_frame_base, record
12875 it. */
e142c38c 12876 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12877 if (attr != nullptr)
fe978cb0 12878 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12879
63e43d3a
PMR
12880 /* If there is a location for the static link, record it. */
12881 newobj->static_link = NULL;
12882 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12883 if (attr != nullptr)
63e43d3a 12884 {
224c3ddb
SM
12885 newobj->static_link
12886 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12887 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12888 cu->per_cu->addr_type ());
63e43d3a
PMR
12889 }
12890
c24bdb02 12891 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12892
639d11d3 12893 if (die->child != NULL)
c906108c 12894 {
639d11d3 12895 child_die = die->child;
c906108c
SS
12896 while (child_die && child_die->tag)
12897 {
34eaf542
TT
12898 if (child_die->tag == DW_TAG_template_type_param
12899 || child_die->tag == DW_TAG_template_value_param)
12900 {
12901 struct symbol *arg = new_symbol (child_die, NULL, cu);
12902
f1078f66 12903 if (arg != NULL)
2f4732b0 12904 template_args.push_back (arg);
34eaf542
TT
12905 }
12906 else
12907 process_die (child_die, cu);
436c571c 12908 child_die = child_die->sibling;
c906108c
SS
12909 }
12910 }
12911
d389af10
JK
12912 inherit_abstract_dies (die, cu);
12913
4a811a97
UW
12914 /* If we have a DW_AT_specification, we might need to import using
12915 directives from the context of the specification DIE. See the
12916 comment in determine_prefix. */
12917 if (cu->language == language_cplus
12918 && dwarf2_attr (die, DW_AT_specification, cu))
12919 {
12920 struct dwarf2_cu *spec_cu = cu;
12921 struct die_info *spec_die = die_specification (die, &spec_cu);
12922
12923 while (spec_die)
12924 {
12925 child_die = spec_die->child;
12926 while (child_die && child_die->tag)
12927 {
12928 if (child_die->tag == DW_TAG_imported_module)
12929 process_die (child_die, spec_cu);
436c571c 12930 child_die = child_die->sibling;
4a811a97
UW
12931 }
12932
12933 /* In some cases, GCC generates specification DIEs that
12934 themselves contain DW_AT_specification attributes. */
12935 spec_die = die_specification (spec_die, &spec_cu);
12936 }
12937 }
12938
c24bdb02 12939 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12940 /* Make a block for the local symbols within. */
c24bdb02 12941 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12942 cstk.static_link, lowpc, highpc);
801e3a5b 12943
df8a16a1 12944 /* For C++, set the block's scope. */
45280282
IB
12945 if ((cu->language == language_cplus
12946 || cu->language == language_fortran
c44af4eb
TT
12947 || cu->language == language_d
12948 || cu->language == language_rust)
4d4ec4e5 12949 && cu->processing_has_namespace_info)
195a3f6c
TT
12950 block_set_scope (block, determine_prefix (die, cu),
12951 &objfile->objfile_obstack);
df8a16a1 12952
801e3a5b
JB
12953 /* If we have address ranges, record them. */
12954 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12955
a60f3166 12956 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12957
34eaf542 12958 /* Attach template arguments to function. */
2f4732b0 12959 if (!template_args.empty ())
34eaf542
TT
12960 {
12961 gdb_assert (templ_func != NULL);
12962
2f4732b0 12963 templ_func->n_template_arguments = template_args.size ();
34eaf542 12964 templ_func->template_arguments
8d749320
SM
12965 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12966 templ_func->n_template_arguments);
34eaf542 12967 memcpy (templ_func->template_arguments,
2f4732b0 12968 template_args.data (),
34eaf542 12969 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12970
12971 /* Make sure that the symtab is set on the new symbols. Even
12972 though they don't appear in this symtab directly, other parts
12973 of gdb assume that symbols do, and this is reasonably
12974 true. */
8634679f 12975 for (symbol *sym : template_args)
3e1d3d8c 12976 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12977 }
12978
208d8187
JB
12979 /* In C++, we can have functions nested inside functions (e.g., when
12980 a function declares a class that has methods). This means that
12981 when we finish processing a function scope, we may need to go
12982 back to building a containing block's symbol lists. */
c24bdb02
KS
12983 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12984 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12985
921e78cf
JB
12986 /* If we've finished processing a top-level function, subsequent
12987 symbols go in the file symbol list. */
c24bdb02
KS
12988 if (cu->get_builder ()->outermost_context_p ())
12989 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12990}
12991
12992/* Process all the DIES contained within a lexical block scope. Start
12993 a new scope, process the dies, and then close the scope. */
12994
12995static void
e7c27a73 12996read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12997{
518817b3 12998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12999 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13000 CORE_ADDR lowpc, highpc;
13001 struct die_info *child_die;
e142c38c
DJ
13002 CORE_ADDR baseaddr;
13003
b3b3bada 13004 baseaddr = objfile->text_section_offset ();
c906108c
SS
13005
13006 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13007 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13008 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13009 be nasty. Might be easier to properly extend generic blocks to
af34e669 13010 describe ranges. */
e385593e
JK
13011 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13012 {
13013 case PC_BOUNDS_NOT_PRESENT:
13014 /* DW_TAG_lexical_block has no attributes, process its children as if
13015 there was no wrapping by that DW_TAG_lexical_block.
13016 GCC does no longer produces such DWARF since GCC r224161. */
13017 for (child_die = die->child;
13018 child_die != NULL && child_die->tag;
436c571c 13019 child_die = child_die->sibling)
e385593e
JK
13020 process_die (child_die, cu);
13021 return;
13022 case PC_BOUNDS_INVALID:
13023 return;
13024 }
3e29f34a
MR
13025 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13026 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13027
c24bdb02 13028 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13029 if (die->child != NULL)
c906108c 13030 {
639d11d3 13031 child_die = die->child;
c906108c
SS
13032 while (child_die && child_die->tag)
13033 {
e7c27a73 13034 process_die (child_die, cu);
436c571c 13035 child_die = child_die->sibling;
c906108c
SS
13036 }
13037 }
3ea89b92 13038 inherit_abstract_dies (die, cu);
c24bdb02 13039 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13040
c24bdb02
KS
13041 if (*cu->get_builder ()->get_local_symbols () != NULL
13042 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13043 {
801e3a5b 13044 struct block *block
c24bdb02 13045 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13046 cstk.start_addr, highpc);
801e3a5b
JB
13047
13048 /* Note that recording ranges after traversing children, as we
13049 do here, means that recording a parent's ranges entails
13050 walking across all its children's ranges as they appear in
13051 the address map, which is quadratic behavior.
13052
13053 It would be nicer to record the parent's ranges before
13054 traversing its children, simply overriding whatever you find
13055 there. But since we don't even decide whether to create a
13056 block until after we've traversed its children, that's hard
13057 to do. */
13058 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13059 }
c24bdb02
KS
13060 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13061 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13062}
13063
216f72a1 13064/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13065
13066static void
13067read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13068{
518817b3 13069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13071 CORE_ADDR pc, baseaddr;
13072 struct attribute *attr;
13073 struct call_site *call_site, call_site_local;
13074 void **slot;
13075 int nparams;
13076 struct die_info *child_die;
13077
b3b3bada 13078 baseaddr = objfile->text_section_offset ();
96408a79 13079
216f72a1
JK
13080 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13081 if (attr == NULL)
13082 {
13083 /* This was a pre-DWARF-5 GNU extension alias
13084 for DW_AT_call_return_pc. */
13085 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13086 }
96408a79
SA
13087 if (!attr)
13088 {
b98664d3 13089 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13090 "DIE %s [in module %s]"),
13091 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13092 return;
13093 }
cd6c91b4 13094 pc = attr->value_as_address () + baseaddr;
3e29f34a 13095 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13096
13097 if (cu->call_site_htab == NULL)
13098 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13099 NULL, &objfile->objfile_obstack,
13100 hashtab_obstack_allocate, NULL);
13101 call_site_local.pc = pc;
13102 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13103 if (*slot != NULL)
13104 {
b98664d3 13105 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13106 "DIE %s [in module %s]"),
13107 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13108 objfile_name (objfile));
96408a79
SA
13109 return;
13110 }
13111
13112 /* Count parameters at the caller. */
13113
13114 nparams = 0;
13115 for (child_die = die->child; child_die && child_die->tag;
436c571c 13116 child_die = child_die->sibling)
96408a79 13117 {
216f72a1
JK
13118 if (child_die->tag != DW_TAG_call_site_parameter
13119 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13120 {
b98664d3 13121 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13122 "DW_TAG_call_site child DIE %s [in module %s]"),
13123 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13124 objfile_name (objfile));
96408a79
SA
13125 continue;
13126 }
13127
13128 nparams++;
13129 }
13130
224c3ddb
SM
13131 call_site
13132 = ((struct call_site *)
13133 obstack_alloc (&objfile->objfile_obstack,
13134 sizeof (*call_site)
13135 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13136 *slot = call_site;
13137 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13138 call_site->pc = pc;
13139
216f72a1
JK
13140 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13141 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13142 {
13143 struct die_info *func_die;
13144
13145 /* Skip also over DW_TAG_inlined_subroutine. */
13146 for (func_die = die->parent;
13147 func_die && func_die->tag != DW_TAG_subprogram
13148 && func_die->tag != DW_TAG_subroutine_type;
13149 func_die = func_die->parent);
13150
216f72a1
JK
13151 /* DW_AT_call_all_calls is a superset
13152 of DW_AT_call_all_tail_calls. */
96408a79 13153 if (func_die
216f72a1 13154 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13155 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13156 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13157 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13158 {
13159 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13160 not complete. But keep CALL_SITE for look ups via call_site_htab,
13161 both the initial caller containing the real return address PC and
13162 the final callee containing the current PC of a chain of tail
13163 calls do not need to have the tail call list complete. But any
13164 function candidate for a virtual tail call frame searched via
13165 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13166 determined unambiguously. */
13167 }
13168 else
13169 {
13170 struct type *func_type = NULL;
13171
13172 if (func_die)
13173 func_type = get_die_type (func_die, cu);
13174 if (func_type != NULL)
13175 {
13176 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13177
13178 /* Enlist this call site to the function. */
13179 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13180 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13181 }
13182 else
b98664d3 13183 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13184 "DIE %s [in module %s]"),
13185 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13186 }
13187 }
13188
216f72a1
JK
13189 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13190 if (attr == NULL)
13191 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13192 if (attr == NULL)
13193 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13194 if (attr == NULL)
216f72a1
JK
13195 {
13196 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13197 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13198 }
96408a79 13199 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13200 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13201 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13202 else if (attr->form_is_block ())
96408a79
SA
13203 {
13204 struct dwarf2_locexpr_baton *dlbaton;
13205
8d749320 13206 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13207 dlbaton->data = DW_BLOCK (attr)->data;
13208 dlbaton->size = DW_BLOCK (attr)->size;
13209 dlbaton->per_cu = cu->per_cu;
13210
13211 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13212 }
cd6c91b4 13213 else if (attr->form_is_ref ())
96408a79 13214 {
96408a79
SA
13215 struct dwarf2_cu *target_cu = cu;
13216 struct die_info *target_die;
13217
ac9ec31b 13218 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13219 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13220 if (die_is_declaration (target_die, target_cu))
13221 {
7d45c7c3 13222 const char *target_physname;
9112db09
JK
13223
13224 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13225 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13226 if (target_physname == NULL)
9112db09 13227 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13228 if (target_physname == NULL)
b98664d3 13229 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13230 "physname, for referencing DIE %s [in module %s]"),
13231 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13232 else
7d455152 13233 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13234 }
13235 else
13236 {
13237 CORE_ADDR lowpc;
13238
13239 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13240 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13241 <= PC_BOUNDS_INVALID)
b98664d3 13242 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13243 "low pc, for referencing DIE %s [in module %s]"),
13244 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13245 else
3e29f34a
MR
13246 {
13247 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13248 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13249 }
96408a79
SA
13250 }
13251 }
13252 else
b98664d3 13253 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13254 "block nor reference, for DIE %s [in module %s]"),
13255 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13256
13257 call_site->per_cu = cu->per_cu;
13258
13259 for (child_die = die->child;
13260 child_die && child_die->tag;
436c571c 13261 child_die = child_die->sibling)
96408a79 13262 {
96408a79 13263 struct call_site_parameter *parameter;
1788b2d3 13264 struct attribute *loc, *origin;
96408a79 13265
216f72a1
JK
13266 if (child_die->tag != DW_TAG_call_site_parameter
13267 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13268 {
13269 /* Already printed the complaint above. */
13270 continue;
13271 }
13272
13273 gdb_assert (call_site->parameter_count < nparams);
13274 parameter = &call_site->parameter[call_site->parameter_count];
13275
1788b2d3
JK
13276 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13277 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13278 register is contained in DW_AT_call_value. */
96408a79 13279
24c5c679 13280 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13281 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13282 if (origin == NULL)
13283 {
13284 /* This was a pre-DWARF-5 GNU extension alias
13285 for DW_AT_call_parameter. */
13286 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13287 }
cd6c91b4 13288 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13289 {
1788b2d3 13290 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13291
0826b30a 13292 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13293 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13294 {
13295 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13296 binding can be done only inside one CU. Such referenced DIE
13297 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13298 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13299 "DW_TAG_call_site child DIE %s [in module %s]"),
13300 sect_offset_str (child_die->sect_off),
9c541725 13301 objfile_name (objfile));
d76b7dbc
JK
13302 continue;
13303 }
9c541725
PA
13304 parameter->u.param_cu_off
13305 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13306 }
4fc6c0d5 13307 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13308 {
b98664d3 13309 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13310 "DW_TAG_call_site child DIE %s [in module %s]"),
13311 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13312 continue;
13313 }
24c5c679 13314 else
96408a79 13315 {
24c5c679
JK
13316 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13317 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13318 if (parameter->u.dwarf_reg != -1)
13319 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13320 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13321 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13322 &parameter->u.fb_offset))
13323 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13324 else
13325 {
b98664d3 13326 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13327 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13328 "DW_TAG_call_site child DIE %s "
24c5c679 13329 "[in module %s]"),
9d8780f0 13330 sect_offset_str (child_die->sect_off),
9c541725 13331 objfile_name (objfile));
24c5c679
JK
13332 continue;
13333 }
96408a79
SA
13334 }
13335
216f72a1
JK
13336 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13337 if (attr == NULL)
13338 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13339 if (attr == NULL || !attr->form_is_block ())
96408a79 13340 {
b98664d3 13341 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13342 "DW_TAG_call_site child DIE %s [in module %s]"),
13343 sect_offset_str (child_die->sect_off),
9c541725 13344 objfile_name (objfile));
96408a79
SA
13345 continue;
13346 }
13347 parameter->value = DW_BLOCK (attr)->data;
13348 parameter->value_size = DW_BLOCK (attr)->size;
13349
13350 /* Parameters are not pre-cleared by memset above. */
13351 parameter->data_value = NULL;
13352 parameter->data_value_size = 0;
13353 call_site->parameter_count++;
13354
216f72a1
JK
13355 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13356 if (attr == NULL)
13357 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13358 if (attr != nullptr)
96408a79 13359 {
4fc6c0d5 13360 if (!attr->form_is_block ())
b98664d3 13361 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13362 "DW_TAG_call_site child DIE %s [in module %s]"),
13363 sect_offset_str (child_die->sect_off),
9c541725 13364 objfile_name (objfile));
96408a79
SA
13365 else
13366 {
13367 parameter->data_value = DW_BLOCK (attr)->data;
13368 parameter->data_value_size = DW_BLOCK (attr)->size;
13369 }
13370 }
13371 }
13372}
13373
71a3c369
TT
13374/* Helper function for read_variable. If DIE represents a virtual
13375 table, then return the type of the concrete object that is
13376 associated with the virtual table. Otherwise, return NULL. */
13377
13378static struct type *
13379rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13380{
13381 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13382 if (attr == NULL)
13383 return NULL;
13384
13385 /* Find the type DIE. */
13386 struct die_info *type_die = NULL;
13387 struct dwarf2_cu *type_cu = cu;
13388
cd6c91b4 13389 if (attr->form_is_ref ())
71a3c369
TT
13390 type_die = follow_die_ref (die, attr, &type_cu);
13391 if (type_die == NULL)
13392 return NULL;
13393
13394 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13395 return NULL;
13396 return die_containing_type (type_die, type_cu);
13397}
13398
13399/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13400
13401static void
13402read_variable (struct die_info *die, struct dwarf2_cu *cu)
13403{
13404 struct rust_vtable_symbol *storage = NULL;
13405
13406 if (cu->language == language_rust)
13407 {
13408 struct type *containing_type = rust_containing_type (die, cu);
13409
13410 if (containing_type != NULL)
13411 {
518817b3 13412 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13413
468c0cbb 13414 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13415 initialize_objfile_symbol (storage);
13416 storage->concrete_type = containing_type;
cf724bc9 13417 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13418 }
13419 }
13420
e4a62c65
TV
13421 struct symbol *res = new_symbol (die, NULL, cu, storage);
13422 struct attribute *abstract_origin
13423 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13424 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13425 if (res == NULL && loc && abstract_origin)
13426 {
13427 /* We have a variable without a name, but with a location and an abstract
13428 origin. This may be a concrete instance of an abstract variable
13429 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13430 later. */
13431 struct dwarf2_cu *origin_cu = cu;
13432 struct die_info *origin_die
13433 = follow_die_ref (die, abstract_origin, &origin_cu);
13434 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13435 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13436 }
71a3c369
TT
13437}
13438
43988095
JK
13439/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13440 reading .debug_rnglists.
13441 Callback's type should be:
13442 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13443 Return true if the attributes are present and valid, otherwise,
13444 return false. */
13445
13446template <typename Callback>
13447static bool
13448dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13449 Callback &&callback)
13450{
ed2dc618 13451 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13452 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13453 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13454 bfd *obfd = objfile->obfd;
43988095 13455 /* Base address selection entry. */
2b24b6e4 13456 gdb::optional<CORE_ADDR> base;
43988095 13457 const gdb_byte *buffer;
43988095
JK
13458 CORE_ADDR baseaddr;
13459 bool overflow = false;
13460
43988095
JK
13461 base = cu->base_address;
13462
96b79293 13463 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13464 if (offset >= dwarf2_per_objfile->rnglists.size)
13465 {
b98664d3 13466 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13467 offset);
13468 return false;
13469 }
13470 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13471
b3b3bada 13472 baseaddr = objfile->text_section_offset ();
43988095
JK
13473
13474 while (1)
13475 {
7814882a
JK
13476 /* Initialize it due to a false compiler warning. */
13477 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13478 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13479 + dwarf2_per_objfile->rnglists.size);
13480 unsigned int bytes_read;
13481
13482 if (buffer == buf_end)
13483 {
13484 overflow = true;
13485 break;
13486 }
13487 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13488 switch (rlet)
13489 {
13490 case DW_RLE_end_of_list:
13491 break;
13492 case DW_RLE_base_address:
13493 if (buffer + cu->header.addr_size > buf_end)
13494 {
13495 overflow = true;
13496 break;
13497 }
c8a7a66f 13498 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13499 buffer += bytes_read;
13500 break;
13501 case DW_RLE_start_length:
13502 if (buffer + cu->header.addr_size > buf_end)
13503 {
13504 overflow = true;
13505 break;
13506 }
c8a7a66f
TT
13507 range_beginning = cu->header.read_address (obfd, buffer,
13508 &bytes_read);
43988095
JK
13509 buffer += bytes_read;
13510 range_end = (range_beginning
13511 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13512 buffer += bytes_read;
13513 if (buffer > buf_end)
13514 {
13515 overflow = true;
13516 break;
13517 }
13518 break;
13519 case DW_RLE_offset_pair:
13520 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13521 buffer += bytes_read;
13522 if (buffer > buf_end)
13523 {
13524 overflow = true;
13525 break;
13526 }
13527 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13528 buffer += bytes_read;
13529 if (buffer > buf_end)
13530 {
13531 overflow = true;
13532 break;
13533 }
13534 break;
13535 case DW_RLE_start_end:
13536 if (buffer + 2 * cu->header.addr_size > buf_end)
13537 {
13538 overflow = true;
13539 break;
13540 }
c8a7a66f
TT
13541 range_beginning = cu->header.read_address (obfd, buffer,
13542 &bytes_read);
43988095 13543 buffer += bytes_read;
c8a7a66f 13544 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13545 buffer += bytes_read;
13546 break;
13547 default:
b98664d3 13548 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13549 return false;
13550 }
13551 if (rlet == DW_RLE_end_of_list || overflow)
13552 break;
13553 if (rlet == DW_RLE_base_address)
13554 continue;
13555
2b24b6e4 13556 if (!base.has_value ())
43988095
JK
13557 {
13558 /* We have no valid base address for the ranges
13559 data. */
b98664d3 13560 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13561 return false;
13562 }
13563
13564 if (range_beginning > range_end)
13565 {
13566 /* Inverted range entries are invalid. */
b98664d3 13567 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13568 return false;
13569 }
13570
13571 /* Empty range entries have no effect. */
13572 if (range_beginning == range_end)
13573 continue;
13574
2b24b6e4
TT
13575 range_beginning += *base;
13576 range_end += *base;
43988095
JK
13577
13578 /* A not-uncommon case of bad debug info.
13579 Don't pollute the addrmap with bad data. */
13580 if (range_beginning + baseaddr == 0
13581 && !dwarf2_per_objfile->has_section_at_zero)
13582 {
b98664d3 13583 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13584 " [in module %s]"), objfile_name (objfile));
13585 continue;
13586 }
13587
13588 callback (range_beginning, range_end);
13589 }
13590
13591 if (overflow)
13592 {
b98664d3 13593 complaint (_("Offset %d is not terminated "
43988095
JK
13594 "for DW_AT_ranges attribute"),
13595 offset);
13596 return false;
13597 }
13598
13599 return true;
13600}
13601
13602/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13603 Callback's type should be:
13604 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13605 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13606
43988095 13607template <typename Callback>
43039443 13608static int
5f46c5a5 13609dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13610 Callback &&callback)
43039443 13611{
ed2dc618 13612 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13613 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13614 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13615 struct comp_unit_head *cu_header = &cu->header;
13616 bfd *obfd = objfile->obfd;
13617 unsigned int addr_size = cu_header->addr_size;
13618 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13619 /* Base address selection entry. */
2b24b6e4 13620 gdb::optional<CORE_ADDR> base;
43039443 13621 unsigned int dummy;
d521ce57 13622 const gdb_byte *buffer;
ff013f42 13623 CORE_ADDR baseaddr;
43039443 13624
43988095
JK
13625 if (cu_header->version >= 5)
13626 return dwarf2_rnglists_process (offset, cu, callback);
13627
d00adf39 13628 base = cu->base_address;
43039443 13629
96b79293 13630 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13631 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13632 {
b98664d3 13633 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13634 offset);
13635 return 0;
13636 }
dce234bc 13637 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13638
b3b3bada 13639 baseaddr = objfile->text_section_offset ();
ff013f42 13640
43039443
JK
13641 while (1)
13642 {
13643 CORE_ADDR range_beginning, range_end;
13644
c8a7a66f 13645 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13646 buffer += addr_size;
c8a7a66f 13647 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13648 buffer += addr_size;
13649 offset += 2 * addr_size;
13650
13651 /* An end of list marker is a pair of zero addresses. */
13652 if (range_beginning == 0 && range_end == 0)
13653 /* Found the end of list entry. */
13654 break;
13655
13656 /* Each base address selection entry is a pair of 2 values.
13657 The first is the largest possible address, the second is
13658 the base address. Check for a base address here. */
13659 if ((range_beginning & mask) == mask)
13660 {
28d2bfb9
AB
13661 /* If we found the largest possible address, then we already
13662 have the base address in range_end. */
13663 base = range_end;
43039443
JK
13664 continue;
13665 }
13666
2b24b6e4 13667 if (!base.has_value ())
43039443
JK
13668 {
13669 /* We have no valid base address for the ranges
13670 data. */
b98664d3 13671 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13672 return 0;
13673 }
13674
9277c30c
UW
13675 if (range_beginning > range_end)
13676 {
13677 /* Inverted range entries are invalid. */
b98664d3 13678 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13679 return 0;
13680 }
13681
13682 /* Empty range entries have no effect. */
13683 if (range_beginning == range_end)
13684 continue;
13685
2b24b6e4
TT
13686 range_beginning += *base;
13687 range_end += *base;
43039443 13688
01093045
DE
13689 /* A not-uncommon case of bad debug info.
13690 Don't pollute the addrmap with bad data. */
13691 if (range_beginning + baseaddr == 0
13692 && !dwarf2_per_objfile->has_section_at_zero)
13693 {
b98664d3 13694 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13695 " [in module %s]"), objfile_name (objfile));
01093045
DE
13696 continue;
13697 }
13698
5f46c5a5
JK
13699 callback (range_beginning, range_end);
13700 }
13701
13702 return 1;
13703}
13704
13705/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13706 Return 1 if the attributes are present and valid, otherwise, return 0.
13707 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13708
13709static int
13710dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13711 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13712 dwarf2_psymtab *ranges_pst)
5f46c5a5 13713{
518817b3 13714 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13715 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13716 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13717 int low_set = 0;
13718 CORE_ADDR low = 0;
13719 CORE_ADDR high = 0;
13720 int retval;
13721
13722 retval = dwarf2_ranges_process (offset, cu,
13723 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13724 {
9277c30c 13725 if (ranges_pst != NULL)
3e29f34a
MR
13726 {
13727 CORE_ADDR lowpc;
13728 CORE_ADDR highpc;
13729
79748972
TT
13730 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13731 range_beginning + baseaddr)
13732 - baseaddr);
13733 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13734 range_end + baseaddr)
13735 - baseaddr);
d320c2b5
TT
13736 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13737 lowpc, highpc - 1, ranges_pst);
3e29f34a 13738 }
ff013f42 13739
43039443
JK
13740 /* FIXME: This is recording everything as a low-high
13741 segment of consecutive addresses. We should have a
13742 data structure for discontiguous block ranges
13743 instead. */
13744 if (! low_set)
13745 {
13746 low = range_beginning;
13747 high = range_end;
13748 low_set = 1;
13749 }
13750 else
13751 {
13752 if (range_beginning < low)
13753 low = range_beginning;
13754 if (range_end > high)
13755 high = range_end;
13756 }
5f46c5a5
JK
13757 });
13758 if (!retval)
13759 return 0;
43039443
JK
13760
13761 if (! low_set)
13762 /* If the first entry is an end-of-list marker, the range
13763 describes an empty scope, i.e. no instructions. */
13764 return 0;
13765
13766 if (low_return)
13767 *low_return = low;
13768 if (high_return)
13769 *high_return = high;
13770 return 1;
13771}
13772
3a2b436a
JK
13773/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13774 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13775 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13776
3a2b436a 13777static enum pc_bounds_kind
af34e669 13778dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13779 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13780 dwarf2_psymtab *pst)
c906108c 13781{
518817b3
SM
13782 struct dwarf2_per_objfile *dwarf2_per_objfile
13783 = cu->per_cu->dwarf2_per_objfile;
c906108c 13784 struct attribute *attr;
91da1414 13785 struct attribute *attr_high;
af34e669
DJ
13786 CORE_ADDR low = 0;
13787 CORE_ADDR high = 0;
e385593e 13788 enum pc_bounds_kind ret;
c906108c 13789
91da1414
MW
13790 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13791 if (attr_high)
af34e669 13792 {
e142c38c 13793 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13794 if (attr != nullptr)
91da1414 13795 {
cd6c91b4
TT
13796 low = attr->value_as_address ();
13797 high = attr_high->value_as_address ();
13798 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13799 high += low;
91da1414 13800 }
af34e669
DJ
13801 else
13802 /* Found high w/o low attribute. */
e385593e 13803 return PC_BOUNDS_INVALID;
af34e669
DJ
13804
13805 /* Found consecutive range of addresses. */
3a2b436a 13806 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13807 }
c906108c 13808 else
af34e669 13809 {
e142c38c 13810 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13811 if (attr != NULL)
13812 {
18a8505e 13813 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13814 We take advantage of the fact that DW_AT_ranges does not appear
13815 in DW_TAG_compile_unit of DWO files. */
13816 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13817 unsigned int ranges_offset = (DW_UNSND (attr)
13818 + (need_ranges_base
13819 ? cu->ranges_base
13820 : 0));
2e3cf129 13821
af34e669 13822 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13823 .debug_ranges section. */
2e3cf129 13824 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13825 return PC_BOUNDS_INVALID;
43039443 13826 /* Found discontinuous range of addresses. */
3a2b436a 13827 ret = PC_BOUNDS_RANGES;
af34e669 13828 }
e385593e
JK
13829 else
13830 return PC_BOUNDS_NOT_PRESENT;
af34e669 13831 }
c906108c 13832
48fbe735 13833 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13834 if (high <= low)
e385593e 13835 return PC_BOUNDS_INVALID;
c906108c
SS
13836
13837 /* When using the GNU linker, .gnu.linkonce. sections are used to
13838 eliminate duplicate copies of functions and vtables and such.
13839 The linker will arbitrarily choose one and discard the others.
13840 The AT_*_pc values for such functions refer to local labels in
13841 these sections. If the section from that file was discarded, the
13842 labels are not in the output, so the relocs get a value of 0.
13843 If this is a discarded function, mark the pc bounds as invalid,
13844 so that GDB will ignore it. */
72dca2f5 13845 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13846 return PC_BOUNDS_INVALID;
c906108c
SS
13847
13848 *lowpc = low;
96408a79
SA
13849 if (highpc)
13850 *highpc = high;
af34e669 13851 return ret;
c906108c
SS
13852}
13853
b084d499
JB
13854/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13855 its low and high PC addresses. Do nothing if these addresses could not
13856 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13857 and HIGHPC to the high address if greater than HIGHPC. */
13858
13859static void
13860dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13861 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13862 struct dwarf2_cu *cu)
13863{
13864 CORE_ADDR low, high;
13865 struct die_info *child = die->child;
13866
e385593e 13867 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13868 {
325fac50
PA
13869 *lowpc = std::min (*lowpc, low);
13870 *highpc = std::max (*highpc, high);
b084d499
JB
13871 }
13872
13873 /* If the language does not allow nested subprograms (either inside
13874 subprograms or lexical blocks), we're done. */
13875 if (cu->language != language_ada)
13876 return;
6e70227d 13877
b084d499
JB
13878 /* Check all the children of the given DIE. If it contains nested
13879 subprograms, then check their pc bounds. Likewise, we need to
13880 check lexical blocks as well, as they may also contain subprogram
13881 definitions. */
13882 while (child && child->tag)
13883 {
13884 if (child->tag == DW_TAG_subprogram
13885 || child->tag == DW_TAG_lexical_block)
13886 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13887 child = child->sibling;
b084d499
JB
13888 }
13889}
13890
fae299cd
DC
13891/* Get the low and high pc's represented by the scope DIE, and store
13892 them in *LOWPC and *HIGHPC. If the correct values can't be
13893 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13894
13895static void
13896get_scope_pc_bounds (struct die_info *die,
13897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13898 struct dwarf2_cu *cu)
13899{
13900 CORE_ADDR best_low = (CORE_ADDR) -1;
13901 CORE_ADDR best_high = (CORE_ADDR) 0;
13902 CORE_ADDR current_low, current_high;
13903
3a2b436a 13904 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13905 >= PC_BOUNDS_RANGES)
fae299cd
DC
13906 {
13907 best_low = current_low;
13908 best_high = current_high;
13909 }
13910 else
13911 {
13912 struct die_info *child = die->child;
13913
13914 while (child && child->tag)
13915 {
13916 switch (child->tag) {
13917 case DW_TAG_subprogram:
b084d499 13918 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13919 break;
13920 case DW_TAG_namespace:
f55ee35c 13921 case DW_TAG_module:
fae299cd
DC
13922 /* FIXME: carlton/2004-01-16: Should we do this for
13923 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13924 that current GCC's always emit the DIEs corresponding
13925 to definitions of methods of classes as children of a
13926 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13927 the DIEs giving the declarations, which could be
13928 anywhere). But I don't see any reason why the
13929 standards says that they have to be there. */
13930 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13931
13932 if (current_low != ((CORE_ADDR) -1))
13933 {
325fac50
PA
13934 best_low = std::min (best_low, current_low);
13935 best_high = std::max (best_high, current_high);
fae299cd
DC
13936 }
13937 break;
13938 default:
0963b4bd 13939 /* Ignore. */
fae299cd
DC
13940 break;
13941 }
13942
436c571c 13943 child = child->sibling;
fae299cd
DC
13944 }
13945 }
13946
13947 *lowpc = best_low;
13948 *highpc = best_high;
13949}
13950
801e3a5b
JB
13951/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13952 in DIE. */
380bca97 13953
801e3a5b
JB
13954static void
13955dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13956 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13957{
518817b3 13958 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13960 struct attribute *attr;
91da1414 13961 struct attribute *attr_high;
801e3a5b 13962
91da1414
MW
13963 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13964 if (attr_high)
801e3a5b 13965 {
801e3a5b 13966 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13967 if (attr != nullptr)
801e3a5b 13968 {
cd6c91b4
TT
13969 CORE_ADDR low = attr->value_as_address ();
13970 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13971
cd6c91b4 13972 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13973 high += low;
9a619af0 13974
3e29f34a
MR
13975 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13976 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13977 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13978 }
13979 }
13980
13981 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13982 if (attr != nullptr)
801e3a5b 13983 {
18a8505e 13984 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13985 We take advantage of the fact that DW_AT_ranges does not appear
13986 in DW_TAG_compile_unit of DWO files. */
13987 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13988
13989 /* The value of the DW_AT_ranges attribute is the offset of the
13990 address range list in the .debug_ranges section. */
ab435259
DE
13991 unsigned long offset = (DW_UNSND (attr)
13992 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 13993
2d5f09ec 13994 std::vector<blockrange> blockvec;
5f46c5a5
JK
13995 dwarf2_ranges_process (offset, cu,
13996 [&] (CORE_ADDR start, CORE_ADDR end)
13997 {
58fdfd2c
JK
13998 start += baseaddr;
13999 end += baseaddr;
5f46c5a5
JK
14000 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14001 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14002 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14003 blockvec.emplace_back (start, end);
5f46c5a5 14004 });
2d5f09ec
KB
14005
14006 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14007 }
14008}
14009
685b1105
JK
14010/* Check whether the producer field indicates either of GCC < 4.6, or the
14011 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14012
685b1105
JK
14013static void
14014check_producer (struct dwarf2_cu *cu)
60d5a603 14015{
38360086 14016 int major, minor;
60d5a603
JK
14017
14018 if (cu->producer == NULL)
14019 {
14020 /* For unknown compilers expect their behavior is DWARF version
14021 compliant.
14022
14023 GCC started to support .debug_types sections by -gdwarf-4 since
14024 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14025 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14026 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14027 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14028 }
b1ffba5a 14029 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14030 {
38360086
MW
14031 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14032 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14033 }
5230b05a 14034 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14035 {
14036 cu->producer_is_icc = true;
14037 cu->producer_is_icc_lt_14 = major < 14;
14038 }
c258c396
JD
14039 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14040 cu->producer_is_codewarrior = true;
685b1105
JK
14041 else
14042 {
14043 /* For other non-GCC compilers, expect their behavior is DWARF version
14044 compliant. */
60d5a603
JK
14045 }
14046
9068261f 14047 cu->checked_producer = true;
685b1105 14048}
ba919b58 14049
685b1105
JK
14050/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14051 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14052 during 4.6.0 experimental. */
14053
9068261f 14054static bool
685b1105
JK
14055producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14056{
14057 if (!cu->checked_producer)
14058 check_producer (cu);
14059
14060 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14061}
14062
c258c396
JD
14063
14064/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14065 with incorrect is_stmt attributes. */
14066
14067static bool
14068producer_is_codewarrior (struct dwarf2_cu *cu)
14069{
14070 if (!cu->checked_producer)
14071 check_producer (cu);
14072
14073 return cu->producer_is_codewarrior;
14074}
14075
405feb71 14076/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14077 DW_AT_accessibility. */
14078
14079static enum dwarf_access_attribute
14080dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14081{
14082 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14083 {
14084 /* The default DWARF 2 accessibility for members is public, the default
14085 accessibility for inheritance is private. */
14086
14087 if (die->tag != DW_TAG_inheritance)
14088 return DW_ACCESS_public;
14089 else
14090 return DW_ACCESS_private;
14091 }
14092 else
14093 {
14094 /* DWARF 3+ defines the default accessibility a different way. The same
14095 rules apply now for DW_TAG_inheritance as for the members and it only
14096 depends on the container kind. */
14097
14098 if (die->parent->tag == DW_TAG_class_type)
14099 return DW_ACCESS_private;
14100 else
14101 return DW_ACCESS_public;
14102 }
14103}
14104
74ac6d43
TT
14105/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14106 offset. If the attribute was not found return 0, otherwise return
14107 1. If it was found but could not properly be handled, set *OFFSET
14108 to 0. */
14109
14110static int
14111handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14112 LONGEST *offset)
14113{
14114 struct attribute *attr;
14115
14116 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14117 if (attr != NULL)
14118 {
14119 *offset = 0;
14120
14121 /* Note that we do not check for a section offset first here.
14122 This is because DW_AT_data_member_location is new in DWARF 4,
14123 so if we see it, we can assume that a constant form is really
14124 a constant and not a section offset. */
cd6c91b4 14125 if (attr->form_is_constant ())
0826b30a 14126 *offset = attr->constant_value (0);
cd6c91b4 14127 else if (attr->form_is_section_offset ())
74ac6d43 14128 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14129 else if (attr->form_is_block ())
74ac6d43
TT
14130 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14131 else
14132 dwarf2_complex_location_expr_complaint ();
14133
14134 return 1;
14135 }
14136
14137 return 0;
14138}
14139
c906108c
SS
14140/* Add an aggregate field to the field list. */
14141
14142static void
107d2387 14143dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14144 struct dwarf2_cu *cu)
6e70227d 14145{
518817b3 14146 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14147 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14148 struct nextfield *new_field;
14149 struct attribute *attr;
14150 struct field *fp;
15d034d0 14151 const char *fieldname = "";
c906108c 14152
7d0ccb61
DJ
14153 if (die->tag == DW_TAG_inheritance)
14154 {
be2daae6
TT
14155 fip->baseclasses.emplace_back ();
14156 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14157 }
14158 else
14159 {
be2daae6
TT
14160 fip->fields.emplace_back ();
14161 new_field = &fip->fields.back ();
7d0ccb61 14162 }
be2daae6 14163
e142c38c 14164 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14165 if (attr != nullptr)
c906108c 14166 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14167 else
14168 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14169 if (new_field->accessibility != DW_ACCESS_public)
14170 fip->non_public_fields = 1;
60d5a603 14171
e142c38c 14172 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14173 if (attr != nullptr)
c906108c 14174 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14175 else
14176 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14177
14178 fp = &new_field->field;
a9a9bd0f 14179
e142c38c 14180 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14181 {
74ac6d43
TT
14182 LONGEST offset;
14183
a9a9bd0f 14184 /* Data member other than a C++ static data member. */
6e70227d 14185
c906108c 14186 /* Get type of field. */
e7c27a73 14187 fp->type = die_type (die, cu);
c906108c 14188
d6a843b5 14189 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14190
c906108c 14191 /* Get bit size of field (zero if none). */
e142c38c 14192 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14193 if (attr != nullptr)
c906108c
SS
14194 {
14195 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14196 }
14197 else
14198 {
14199 FIELD_BITSIZE (*fp) = 0;
14200 }
14201
14202 /* Get bit offset of field. */
74ac6d43
TT
14203 if (handle_data_member_location (die, cu, &offset))
14204 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14205 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14206 if (attr != nullptr)
c906108c 14207 {
d5a22e77 14208 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14209 {
14210 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14211 additional bit offset from the MSB of the containing
14212 anonymous object to the MSB of the field. We don't
14213 have to do anything special since we don't need to
14214 know the size of the anonymous object. */
f41f5e61 14215 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14216 }
14217 else
14218 {
14219 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14220 MSB of the anonymous object, subtract off the number of
14221 bits from the MSB of the field to the MSB of the
14222 object, and then subtract off the number of bits of
14223 the field itself. The result is the bit offset of
14224 the LSB of the field. */
c906108c
SS
14225 int anonymous_size;
14226 int bit_offset = DW_UNSND (attr);
14227
e142c38c 14228 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14229 if (attr != nullptr)
c906108c
SS
14230 {
14231 /* The size of the anonymous object containing
14232 the bit field is explicit, so use the
14233 indicated size (in bytes). */
14234 anonymous_size = DW_UNSND (attr);
14235 }
14236 else
14237 {
14238 /* The size of the anonymous object containing
14239 the bit field must be inferred from the type
14240 attribute of the data member containing the
14241 bit field. */
14242 anonymous_size = TYPE_LENGTH (fp->type);
14243 }
f41f5e61
PA
14244 SET_FIELD_BITPOS (*fp,
14245 (FIELD_BITPOS (*fp)
14246 + anonymous_size * bits_per_byte
14247 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14248 }
14249 }
da5b30da
AA
14250 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14251 if (attr != NULL)
14252 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14253 + attr->constant_value (0)));
c906108c
SS
14254
14255 /* Get name of field. */
39cbfefa
DJ
14256 fieldname = dwarf2_name (die, cu);
14257 if (fieldname == NULL)
14258 fieldname = "";
d8151005
DJ
14259
14260 /* The name is already allocated along with this objfile, so we don't
14261 need to duplicate it for the type. */
14262 fp->name = fieldname;
c906108c
SS
14263
14264 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14265 pointer or virtual base class pointer) to private. */
e142c38c 14266 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14267 {
d48cc9dd 14268 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14269 new_field->accessibility = DW_ACCESS_private;
14270 fip->non_public_fields = 1;
14271 }
14272 }
a9a9bd0f 14273 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14274 {
a9a9bd0f
DC
14275 /* C++ static member. */
14276
14277 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14278 is a declaration, but all versions of G++ as of this writing
14279 (so through at least 3.2.1) incorrectly generate
14280 DW_TAG_variable tags. */
6e70227d 14281
ff355380 14282 const char *physname;
c906108c 14283
a9a9bd0f 14284 /* Get name of field. */
39cbfefa
DJ
14285 fieldname = dwarf2_name (die, cu);
14286 if (fieldname == NULL)
c906108c
SS
14287 return;
14288
254e6b9e 14289 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14290 if (attr
14291 /* Only create a symbol if this is an external value.
14292 new_symbol checks this and puts the value in the global symbol
14293 table, which we want. If it is not external, new_symbol
14294 will try to put the value in cu->list_in_scope which is wrong. */
14295 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14296 {
14297 /* A static const member, not much different than an enum as far as
14298 we're concerned, except that we can support more types. */
14299 new_symbol (die, NULL, cu);
14300 }
14301
2df3850c 14302 /* Get physical name. */
ff355380 14303 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14304
d8151005
DJ
14305 /* The name is already allocated along with this objfile, so we don't
14306 need to duplicate it for the type. */
14307 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14308 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14309 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14310 }
14311 else if (die->tag == DW_TAG_inheritance)
14312 {
74ac6d43 14313 LONGEST offset;
d4b96c9a 14314
74ac6d43
TT
14315 /* C++ base class field. */
14316 if (handle_data_member_location (die, cu, &offset))
14317 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14318 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14319 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14320 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14321 }
2ddeaf8a
TT
14322 else if (die->tag == DW_TAG_variant_part)
14323 {
14324 /* process_structure_scope will treat this DIE as a union. */
14325 process_structure_scope (die, cu);
14326
14327 /* The variant part is relative to the start of the enclosing
14328 structure. */
14329 SET_FIELD_BITPOS (*fp, 0);
14330 fp->type = get_die_type (die, cu);
14331 fp->artificial = 1;
14332 fp->name = "<<variant>>";
c8c81635
TT
14333
14334 /* Normally a DW_TAG_variant_part won't have a size, but our
14335 representation requires one, so set it to the maximum of the
489dbda6
TT
14336 child sizes, being sure to account for the offset at which
14337 each child is seen. */
c8c81635
TT
14338 if (TYPE_LENGTH (fp->type) == 0)
14339 {
14340 unsigned max = 0;
14341 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14342 {
14343 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14344 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14345 if (len > max)
14346 max = len;
14347 }
c8c81635
TT
14348 TYPE_LENGTH (fp->type) = max;
14349 }
2ddeaf8a
TT
14350 }
14351 else
14352 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14353}
14354
883fd55a
KS
14355/* Can the type given by DIE define another type? */
14356
14357static bool
14358type_can_define_types (const struct die_info *die)
14359{
14360 switch (die->tag)
14361 {
14362 case DW_TAG_typedef:
14363 case DW_TAG_class_type:
14364 case DW_TAG_structure_type:
14365 case DW_TAG_union_type:
14366 case DW_TAG_enumeration_type:
14367 return true;
14368
14369 default:
14370 return false;
14371 }
14372}
14373
14374/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14375
14376static void
883fd55a
KS
14377dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14378 struct dwarf2_cu *cu)
6e70227d 14379{
be2daae6
TT
14380 struct decl_field fp;
14381 memset (&fp, 0, sizeof (fp));
98751a41 14382
883fd55a 14383 gdb_assert (type_can_define_types (die));
98751a41 14384
883fd55a 14385 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14386 fp.name = dwarf2_name (die, cu);
14387 fp.type = read_type_die (die, cu);
98751a41 14388
c191a687
KS
14389 /* Save accessibility. */
14390 enum dwarf_access_attribute accessibility;
14391 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14392 if (attr != NULL)
14393 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14394 else
14395 accessibility = dwarf2_default_access_attribute (die, cu);
14396 switch (accessibility)
14397 {
14398 case DW_ACCESS_public:
14399 /* The assumed value if neither private nor protected. */
14400 break;
14401 case DW_ACCESS_private:
be2daae6 14402 fp.is_private = 1;
c191a687
KS
14403 break;
14404 case DW_ACCESS_protected:
be2daae6 14405 fp.is_protected = 1;
c191a687
KS
14406 break;
14407 default:
b98664d3 14408 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14409 }
14410
883fd55a 14411 if (die->tag == DW_TAG_typedef)
be2daae6 14412 fip->typedef_field_list.push_back (fp);
883fd55a 14413 else
be2daae6 14414 fip->nested_types_list.push_back (fp);
98751a41
JK
14415}
14416
c906108c
SS
14417/* Create the vector of fields, and attach it to the type. */
14418
14419static void
fba45db2 14420dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14421 struct dwarf2_cu *cu)
c906108c 14422{
317f7127 14423 int nfields = fip->nfields ();
c906108c
SS
14424
14425 /* Record the field count, allocate space for the array of fields,
14426 and create blank accessibility bitfields if necessary. */
14427 TYPE_NFIELDS (type) = nfields;
14428 TYPE_FIELDS (type) = (struct field *)
be2daae6 14429 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14430
b4ba55a1 14431 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14432 {
14433 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14434
14435 TYPE_FIELD_PRIVATE_BITS (type) =
14436 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14437 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14438
14439 TYPE_FIELD_PROTECTED_BITS (type) =
14440 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14441 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14442
774b6a14
TT
14443 TYPE_FIELD_IGNORE_BITS (type) =
14444 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14445 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14446 }
14447
14448 /* If the type has baseclasses, allocate and clear a bit vector for
14449 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14450 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14451 {
be2daae6 14452 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14453 unsigned char *pointer;
c906108c
SS
14454
14455 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14456 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14457 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14458 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14459 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14460 }
14461
2ddeaf8a
TT
14462 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14463 {
14464 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14465
be2daae6 14466 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14467 {
be2daae6
TT
14468 struct nextfield &field = fip->fields[index];
14469
14470 if (field.variant.is_discriminant)
2ddeaf8a 14471 di->discriminant_index = index;
be2daae6 14472 else if (field.variant.default_branch)
2ddeaf8a
TT
14473 di->default_index = index;
14474 else
be2daae6 14475 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14476 }
14477 }
14478
be2daae6
TT
14479 /* Copy the saved-up fields into the field vector. */
14480 for (int i = 0; i < nfields; ++i)
c906108c 14481 {
be2daae6
TT
14482 struct nextfield &field
14483 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14484 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14485
be2daae6
TT
14486 TYPE_FIELD (type, i) = field.field;
14487 switch (field.accessibility)
c906108c 14488 {
c5aa993b 14489 case DW_ACCESS_private:
b4ba55a1 14490 if (cu->language != language_ada)
be2daae6 14491 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14492 break;
c906108c 14493
c5aa993b 14494 case DW_ACCESS_protected:
b4ba55a1 14495 if (cu->language != language_ada)
be2daae6 14496 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14497 break;
c906108c 14498
c5aa993b
JM
14499 case DW_ACCESS_public:
14500 break;
c906108c 14501
c5aa993b
JM
14502 default:
14503 /* Unknown accessibility. Complain and treat it as public. */
14504 {
b98664d3 14505 complaint (_("unsupported accessibility %d"),
be2daae6 14506 field.accessibility);
c5aa993b
JM
14507 }
14508 break;
c906108c 14509 }
be2daae6 14510 if (i < fip->baseclasses.size ())
c906108c 14511 {
be2daae6 14512 switch (field.virtuality)
c906108c 14513 {
c5aa993b
JM
14514 case DW_VIRTUALITY_virtual:
14515 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14516 if (cu->language == language_ada)
a73c6dcd 14517 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14518 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14519 break;
c906108c
SS
14520 }
14521 }
c906108c
SS
14522 }
14523}
14524
7d27a96d
TT
14525/* Return true if this member function is a constructor, false
14526 otherwise. */
14527
14528static int
14529dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14530{
14531 const char *fieldname;
fe978cb0 14532 const char *type_name;
7d27a96d
TT
14533 int len;
14534
14535 if (die->parent == NULL)
14536 return 0;
14537
14538 if (die->parent->tag != DW_TAG_structure_type
14539 && die->parent->tag != DW_TAG_union_type
14540 && die->parent->tag != DW_TAG_class_type)
14541 return 0;
14542
14543 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14544 type_name = dwarf2_name (die->parent, cu);
14545 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14546 return 0;
14547
14548 len = strlen (fieldname);
fe978cb0
PA
14549 return (strncmp (fieldname, type_name, len) == 0
14550 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14551}
14552
e35000a7
TBA
14553/* Check if the given VALUE is a recognized enum
14554 dwarf_defaulted_attribute constant according to DWARF5 spec,
14555 Table 7.24. */
14556
14557static bool
14558is_valid_DW_AT_defaulted (ULONGEST value)
14559{
14560 switch (value)
14561 {
14562 case DW_DEFAULTED_no:
14563 case DW_DEFAULTED_in_class:
14564 case DW_DEFAULTED_out_of_class:
14565 return true;
14566 }
14567
3142e908 14568 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14569 return false;
14570}
14571
c906108c
SS
14572/* Add a member function to the proper fieldlist. */
14573
14574static void
107d2387 14575dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14576 struct type *type, struct dwarf2_cu *cu)
c906108c 14577{
518817b3 14578 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14579 struct attribute *attr;
c906108c 14580 int i;
be2daae6 14581 struct fnfieldlist *flp = nullptr;
c906108c 14582 struct fn_field *fnp;
15d034d0 14583 const char *fieldname;
f792889a 14584 struct type *this_type;
60d5a603 14585 enum dwarf_access_attribute accessibility;
c906108c 14586
b4ba55a1 14587 if (cu->language == language_ada)
a73c6dcd 14588 error (_("unexpected member function in Ada type"));
b4ba55a1 14589
2df3850c 14590 /* Get name of member function. */
39cbfefa
DJ
14591 fieldname = dwarf2_name (die, cu);
14592 if (fieldname == NULL)
2df3850c 14593 return;
c906108c 14594
c906108c 14595 /* Look up member function name in fieldlist. */
be2daae6 14596 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14597 {
27bfe10e 14598 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14599 {
14600 flp = &fip->fnfieldlists[i];
14601 break;
14602 }
c906108c
SS
14603 }
14604
be2daae6
TT
14605 /* Create a new fnfieldlist if necessary. */
14606 if (flp == nullptr)
c906108c 14607 {
be2daae6
TT
14608 fip->fnfieldlists.emplace_back ();
14609 flp = &fip->fnfieldlists.back ();
c906108c 14610 flp->name = fieldname;
be2daae6 14611 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14612 }
14613
be2daae6
TT
14614 /* Create a new member function field and add it to the vector of
14615 fnfieldlists. */
14616 flp->fnfields.emplace_back ();
14617 fnp = &flp->fnfields.back ();
3da10d80
KS
14618
14619 /* Delay processing of the physname until later. */
9c37b5ae 14620 if (cu->language == language_cplus)
be2daae6
TT
14621 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14622 die, cu);
3da10d80
KS
14623 else
14624 {
1d06ead6 14625 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14626 fnp->physname = physname ? physname : "";
14627 }
14628
c906108c 14629 fnp->type = alloc_type (objfile);
f792889a
DJ
14630 this_type = read_type_die (die, cu);
14631 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14632 {
f792889a 14633 int nparams = TYPE_NFIELDS (this_type);
c906108c 14634
f792889a 14635 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14636 of the method itself (TYPE_CODE_METHOD). */
14637 smash_to_method_type (fnp->type, type,
f792889a
DJ
14638 TYPE_TARGET_TYPE (this_type),
14639 TYPE_FIELDS (this_type),
14640 TYPE_NFIELDS (this_type),
14641 TYPE_VARARGS (this_type));
c906108c
SS
14642
14643 /* Handle static member functions.
c5aa993b 14644 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14645 member functions. G++ helps GDB by marking the first
14646 parameter for non-static member functions (which is the this
14647 pointer) as artificial. We obtain this information from
14648 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14649 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14650 fnp->voffset = VOFFSET_STATIC;
14651 }
14652 else
b98664d3 14653 complaint (_("member function type missing for '%s'"),
3da10d80 14654 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14655
14656 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14657 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14658 fnp->fcontext = die_containing_type (die, cu);
c906108c 14659
3e43a32a
MS
14660 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14661 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14662
14663 /* Get accessibility. */
e142c38c 14664 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14665 if (attr != nullptr)
aead7601 14666 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14667 else
14668 accessibility = dwarf2_default_access_attribute (die, cu);
14669 switch (accessibility)
c906108c 14670 {
60d5a603
JK
14671 case DW_ACCESS_private:
14672 fnp->is_private = 1;
14673 break;
14674 case DW_ACCESS_protected:
14675 fnp->is_protected = 1;
14676 break;
c906108c
SS
14677 }
14678
b02dede2 14679 /* Check for artificial methods. */
e142c38c 14680 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14681 if (attr && DW_UNSND (attr) != 0)
14682 fnp->is_artificial = 1;
14683
e35000a7
TBA
14684 /* Check for defaulted methods. */
14685 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14686 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14687 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14688
14689 /* Check for deleted methods. */
14690 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14691 if (attr != nullptr && DW_UNSND (attr) != 0)
14692 fnp->is_deleted = 1;
14693
7d27a96d
TT
14694 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14695
0d564a31 14696 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14697 function. For older versions of GCC, this is an offset in the
14698 appropriate virtual table, as specified by DW_AT_containing_type.
14699 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14700 to the object address. */
14701
e142c38c 14702 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14703 if (attr != nullptr)
8e19ed76 14704 {
4fc6c0d5 14705 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14706 {
aec5aa8b
TT
14707 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14708 {
14709 /* Old-style GCC. */
14710 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14711 }
14712 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14713 || (DW_BLOCK (attr)->size > 1
14714 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14715 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14716 {
aec5aa8b
TT
14717 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14718 if ((fnp->voffset % cu->header.addr_size) != 0)
14719 dwarf2_complex_location_expr_complaint ();
14720 else
14721 fnp->voffset /= cu->header.addr_size;
14722 fnp->voffset += 2;
14723 }
14724 else
14725 dwarf2_complex_location_expr_complaint ();
14726
14727 if (!fnp->fcontext)
7e993ebf
KS
14728 {
14729 /* If there is no `this' field and no DW_AT_containing_type,
14730 we cannot actually find a base class context for the
14731 vtable! */
14732 if (TYPE_NFIELDS (this_type) == 0
14733 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14734 {
b98664d3 14735 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14736 "function \"%s\" (offset %s)"),
14737 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14738 }
14739 else
14740 {
14741 fnp->fcontext
14742 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14743 }
14744 }
aec5aa8b 14745 }
cd6c91b4 14746 else if (attr->form_is_section_offset ())
8e19ed76 14747 {
4d3c2250 14748 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14749 }
14750 else
14751 {
4d3c2250
KB
14752 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14753 fieldname);
8e19ed76 14754 }
0d564a31 14755 }
d48cc9dd
DJ
14756 else
14757 {
14758 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14759 if (attr && DW_UNSND (attr))
14760 {
14761 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14762 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14763 "but the vtable offset is not specified"),
9d8780f0 14764 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14765 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14766 TYPE_CPLUS_DYNAMIC (type) = 1;
14767 }
14768 }
c906108c
SS
14769}
14770
14771/* Create the vector of member function fields, and attach it to the type. */
14772
14773static void
fba45db2 14774dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14775 struct dwarf2_cu *cu)
c906108c 14776{
b4ba55a1 14777 if (cu->language == language_ada)
a73c6dcd 14778 error (_("unexpected member functions in Ada type"));
b4ba55a1 14779
c906108c
SS
14780 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14781 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14782 TYPE_ALLOC (type,
14783 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14784
be2daae6 14785 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14786 {
be2daae6 14787 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14788 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14789
be2daae6
TT
14790 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14791 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14792 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14793 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14794
14795 for (int k = 0; k < nf.fnfields.size (); ++k)
14796 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14797 }
14798
be2daae6 14799 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14800}
14801
1168df01
JB
14802/* Returns non-zero if NAME is the name of a vtable member in CU's
14803 language, zero otherwise. */
14804static int
14805is_vtable_name (const char *name, struct dwarf2_cu *cu)
14806{
14807 static const char vptr[] = "_vptr";
14808
9c37b5ae
TT
14809 /* Look for the C++ form of the vtable. */
14810 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14811 return 1;
14812
14813 return 0;
14814}
14815
c0dd20ea 14816/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14817 functions, with the ABI-specified layout. If TYPE describes
14818 such a structure, smash it into a member function type.
61049d3b
DJ
14819
14820 GCC shouldn't do this; it should just output pointer to member DIEs.
14821 This is GCC PR debug/28767. */
c0dd20ea 14822
0b92b5bb
TT
14823static void
14824quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14825{
09e2d7c7 14826 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14827
14828 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14829 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14830 return;
c0dd20ea
DJ
14831
14832 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14833 if (TYPE_FIELD_NAME (type, 0) == NULL
14834 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14835 || TYPE_FIELD_NAME (type, 1) == NULL
14836 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14837 return;
c0dd20ea
DJ
14838
14839 /* Find the type of the method. */
0b92b5bb 14840 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14841 if (pfn_type == NULL
14842 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14843 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14844 return;
c0dd20ea
DJ
14845
14846 /* Look for the "this" argument. */
14847 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14848 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14849 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14850 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14851 return;
c0dd20ea 14852
09e2d7c7 14853 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14854 new_type = alloc_type (objfile);
09e2d7c7 14855 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14856 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14857 TYPE_VARARGS (pfn_type));
0b92b5bb 14858 smash_to_methodptr_type (type, new_type);
c0dd20ea 14859}
1168df01 14860
2b4424c3
TT
14861/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14862 appropriate error checking and issuing complaints if there is a
14863 problem. */
14864
14865static ULONGEST
14866get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14867{
14868 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14869
14870 if (attr == nullptr)
14871 return 0;
14872
cd6c91b4 14873 if (!attr->form_is_constant ())
2b4424c3 14874 {
b98664d3 14875 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14876 " - DIE at %s [in module %s]"),
14877 sect_offset_str (die->sect_off),
14878 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14879 return 0;
14880 }
14881
14882 ULONGEST align;
14883 if (attr->form == DW_FORM_sdata)
14884 {
14885 LONGEST val = DW_SND (attr);
14886 if (val < 0)
14887 {
b98664d3 14888 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14889 " - DIE at %s [in module %s]"),
14890 sect_offset_str (die->sect_off),
14891 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14892 return 0;
14893 }
14894 align = val;
14895 }
14896 else
14897 align = DW_UNSND (attr);
14898
14899 if (align == 0)
14900 {
b98664d3 14901 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14902 " - DIE at %s [in module %s]"),
14903 sect_offset_str (die->sect_off),
14904 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14905 return 0;
14906 }
14907 if ((align & (align - 1)) != 0)
14908 {
b98664d3 14909 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14910 " - DIE at %s [in module %s]"),
14911 sect_offset_str (die->sect_off),
14912 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14913 return 0;
14914 }
14915
14916 return align;
14917}
14918
14919/* If the DIE has a DW_AT_alignment attribute, use its value to set
14920 the alignment for TYPE. */
14921
14922static void
14923maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14924 struct type *type)
14925{
14926 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14927 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14928 " - DIE at %s [in module %s]"),
14929 sect_offset_str (die->sect_off),
14930 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14931}
685b1105 14932
e35000a7
TBA
14933/* Check if the given VALUE is a valid enum dwarf_calling_convention
14934 constant for a type, according to DWARF5 spec, Table 5.5. */
14935
14936static bool
14937is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14938{
14939 switch (value)
14940 {
14941 case DW_CC_normal:
14942 case DW_CC_pass_by_reference:
14943 case DW_CC_pass_by_value:
14944 return true;
14945
14946 default:
14947 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14948 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14949 return false;
14950 }
14951}
14952
d0922fcf
TBA
14953/* Check if the given VALUE is a valid enum dwarf_calling_convention
14954 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14955 also according to GNU-specific values (see include/dwarf2.h). */
14956
14957static bool
14958is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14959{
14960 switch (value)
14961 {
14962 case DW_CC_normal:
14963 case DW_CC_program:
14964 case DW_CC_nocall:
14965 return true;
14966
14967 case DW_CC_GNU_renesas_sh:
14968 case DW_CC_GNU_borland_fastcall_i386:
14969 case DW_CC_GDB_IBM_OpenCL:
14970 return true;
14971
14972 default:
14973 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14974 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14975 return false;
14976 }
14977}
14978
c906108c 14979/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14980 (definition) to create a type for the structure or union. Fill in
14981 the type's name and general properties; the members will not be
83655187
DE
14982 processed until process_structure_scope. A symbol table entry for
14983 the type will also not be done until process_structure_scope (assuming
14984 the type has a name).
c906108c 14985
c767944b
DJ
14986 NOTE: we need to call these functions regardless of whether or not the
14987 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14988 structure or union. This gets the type entered into our set of
83655187 14989 user defined types. */
c906108c 14990
f792889a 14991static struct type *
134d01f1 14992read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14993{
518817b3 14994 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
14995 struct type *type;
14996 struct attribute *attr;
15d034d0 14997 const char *name;
c906108c 14998
348e048f
DE
14999 /* If the definition of this type lives in .debug_types, read that type.
15000 Don't follow DW_AT_specification though, that will take us back up
15001 the chain and we want to go down. */
052c8bb8 15002 attr = die->attr (DW_AT_signature);
435d3d88 15003 if (attr != nullptr)
348e048f 15004 {
ac9ec31b 15005 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15006
ac9ec31b 15007 /* The type's CU may not be the same as CU.
02142a6c 15008 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15009 return set_die_type (die, type, cu);
15010 }
15011
c0dd20ea 15012 type = alloc_type (objfile);
c906108c 15013 INIT_CPLUS_SPECIFIC (type);
93311388 15014
39cbfefa
DJ
15015 name = dwarf2_name (die, cu);
15016 if (name != NULL)
c906108c 15017 {
987504bb 15018 if (cu->language == language_cplus
c44af4eb
TT
15019 || cu->language == language_d
15020 || cu->language == language_rust)
63d06c5c 15021 {
15d034d0 15022 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15023
15024 /* dwarf2_full_name might have already finished building the DIE's
15025 type. If so, there is no need to continue. */
15026 if (get_die_type (die, cu) != NULL)
15027 return get_die_type (die, cu);
15028
e86ca25f 15029 TYPE_NAME (type) = full_name;
63d06c5c
DC
15030 }
15031 else
15032 {
d8151005
DJ
15033 /* The name is already allocated along with this objfile, so
15034 we don't need to duplicate it for the type. */
e86ca25f 15035 TYPE_NAME (type) = name;
63d06c5c 15036 }
c906108c
SS
15037 }
15038
15039 if (die->tag == DW_TAG_structure_type)
15040 {
15041 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15042 }
15043 else if (die->tag == DW_TAG_union_type)
15044 {
15045 TYPE_CODE (type) = TYPE_CODE_UNION;
15046 }
2ddeaf8a
TT
15047 else if (die->tag == DW_TAG_variant_part)
15048 {
15049 TYPE_CODE (type) = TYPE_CODE_UNION;
15050 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15051 }
c906108c
SS
15052 else
15053 {
4753d33b 15054 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15055 }
15056
0cc2414c
TT
15057 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15058 TYPE_DECLARED_CLASS (type) = 1;
15059
e35000a7
TBA
15060 /* Store the calling convention in the type if it's available in
15061 the die. Otherwise the calling convention remains set to
15062 the default value DW_CC_normal. */
15063 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15064 if (attr != nullptr
15065 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15066 {
15067 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15068 TYPE_CPLUS_CALLING_CONVENTION (type)
15069 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15070 }
15071
e142c38c 15072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15073 if (attr != nullptr)
c906108c 15074 {
cd6c91b4 15075 if (attr->form_is_constant ())
155bfbd3
JB
15076 TYPE_LENGTH (type) = DW_UNSND (attr);
15077 else
15078 {
15079 /* For the moment, dynamic type sizes are not supported
15080 by GDB's struct type. The actual size is determined
15081 on-demand when resolving the type of a given object,
15082 so set the type's length to zero for now. Otherwise,
15083 we record an expression as the length, and that expression
15084 could lead to a very large value, which could eventually
15085 lead to us trying to allocate that much memory when creating
15086 a value of that type. */
15087 TYPE_LENGTH (type) = 0;
15088 }
c906108c
SS
15089 }
15090 else
15091 {
15092 TYPE_LENGTH (type) = 0;
15093 }
15094
2b4424c3
TT
15095 maybe_set_alignment (cu, die, type);
15096
5230b05a 15097 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15098 {
5230b05a
WT
15099 /* ICC<14 does not output the required DW_AT_declaration on
15100 incomplete types, but gives them a size of zero. */
422b1cb0 15101 TYPE_STUB (type) = 1;
685b1105
JK
15102 }
15103 else
15104 TYPE_STUB_SUPPORTED (type) = 1;
15105
dc718098 15106 if (die_is_declaration (die, cu))
876cecd0 15107 TYPE_STUB (type) = 1;
a6c727b2
DJ
15108 else if (attr == NULL && die->child == NULL
15109 && producer_is_realview (cu->producer))
15110 /* RealView does not output the required DW_AT_declaration
15111 on incomplete types. */
15112 TYPE_STUB (type) = 1;
dc718098 15113
c906108c
SS
15114 /* We need to add the type field to the die immediately so we don't
15115 infinitely recurse when dealing with pointers to the structure
0963b4bd 15116 type within the structure itself. */
1c379e20 15117 set_die_type (die, type, cu);
c906108c 15118
7e314c57
JK
15119 /* set_die_type should be already done. */
15120 set_descriptive_type (type, die, cu);
15121
c767944b
DJ
15122 return type;
15123}
15124
2ddeaf8a
TT
15125/* A helper for process_structure_scope that handles a single member
15126 DIE. */
15127
15128static void
15129handle_struct_member_die (struct die_info *child_die, struct type *type,
15130 struct field_info *fi,
15131 std::vector<struct symbol *> *template_args,
15132 struct dwarf2_cu *cu)
15133{
15134 if (child_die->tag == DW_TAG_member
15135 || child_die->tag == DW_TAG_variable
15136 || child_die->tag == DW_TAG_variant_part)
15137 {
15138 /* NOTE: carlton/2002-11-05: A C++ static data member
15139 should be a DW_TAG_member that is a declaration, but
15140 all versions of G++ as of this writing (so through at
15141 least 3.2.1) incorrectly generate DW_TAG_variable
15142 tags for them instead. */
15143 dwarf2_add_field (fi, child_die, cu);
15144 }
15145 else if (child_die->tag == DW_TAG_subprogram)
15146 {
15147 /* Rust doesn't have member functions in the C++ sense.
15148 However, it does emit ordinary functions as children
15149 of a struct DIE. */
15150 if (cu->language == language_rust)
15151 read_func_scope (child_die, cu);
15152 else
15153 {
15154 /* C++ member function. */
15155 dwarf2_add_member_fn (fi, child_die, type, cu);
15156 }
15157 }
15158 else if (child_die->tag == DW_TAG_inheritance)
15159 {
15160 /* C++ base class field. */
15161 dwarf2_add_field (fi, child_die, cu);
15162 }
15163 else if (type_can_define_types (child_die))
15164 dwarf2_add_type_defn (fi, child_die, cu);
15165 else if (child_die->tag == DW_TAG_template_type_param
15166 || child_die->tag == DW_TAG_template_value_param)
15167 {
15168 struct symbol *arg = new_symbol (child_die, NULL, cu);
15169
15170 if (arg != NULL)
15171 template_args->push_back (arg);
15172 }
15173 else if (child_die->tag == DW_TAG_variant)
15174 {
15175 /* In a variant we want to get the discriminant and also add a
15176 field for our sole member child. */
15177 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15178
bde09ab7 15179 for (die_info *variant_child = child_die->child;
2ddeaf8a 15180 variant_child != NULL;
436c571c 15181 variant_child = variant_child->sibling)
2ddeaf8a
TT
15182 {
15183 if (variant_child->tag == DW_TAG_member)
15184 {
15185 handle_struct_member_die (variant_child, type, fi,
15186 template_args, cu);
15187 /* Only handle the one. */
15188 break;
15189 }
15190 }
15191
15192 /* We don't handle this but we might as well report it if we see
15193 it. */
15194 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15195 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15196 " - DIE at %s [in module %s]"),
15197 sect_offset_str (child_die->sect_off),
15198 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15199
15200 /* The first field was just added, so we can stash the
15201 discriminant there. */
be2daae6 15202 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15203 if (discr == NULL)
be2daae6 15204 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15205 else
be2daae6 15206 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15207 }
15208}
15209
c767944b
DJ
15210/* Finish creating a structure or union type, including filling in
15211 its members and creating a symbol for it. */
15212
15213static void
15214process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15215{
518817b3 15216 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15217 struct die_info *child_die;
c767944b
DJ
15218 struct type *type;
15219
15220 type = get_die_type (die, cu);
15221 if (type == NULL)
15222 type = read_structure_type (die, cu);
15223
2ddeaf8a
TT
15224 /* When reading a DW_TAG_variant_part, we need to notice when we
15225 read the discriminant member, so we can record it later in the
15226 discriminant_info. */
15227 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15228 sect_offset discr_offset {};
3e1d3d8c 15229 bool has_template_parameters = false;
2ddeaf8a
TT
15230
15231 if (is_variant_part)
15232 {
15233 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15234 if (discr == NULL)
15235 {
15236 /* Maybe it's a univariant form, an extension we support.
15237 In this case arrange not to check the offset. */
15238 is_variant_part = false;
15239 }
cd6c91b4 15240 else if (discr->form_is_ref ())
2ddeaf8a
TT
15241 {
15242 struct dwarf2_cu *target_cu = cu;
15243 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15244
15245 discr_offset = target_die->sect_off;
15246 }
15247 else
15248 {
b98664d3 15249 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15250 " - DIE at %s [in module %s]"),
15251 sect_offset_str (die->sect_off),
15252 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15253 is_variant_part = false;
15254 }
15255 }
15256
e142c38c 15257 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15258 {
15259 struct field_info fi;
2f4732b0 15260 std::vector<struct symbol *> template_args;
c906108c 15261
639d11d3 15262 child_die = die->child;
c906108c
SS
15263
15264 while (child_die && child_die->tag)
15265 {
2ddeaf8a 15266 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15267
2ddeaf8a 15268 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15269 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15270
436c571c 15271 child_die = child_die->sibling;
c906108c
SS
15272 }
15273
34eaf542 15274 /* Attach template arguments to type. */
2f4732b0 15275 if (!template_args.empty ())
34eaf542 15276 {
3e1d3d8c 15277 has_template_parameters = true;
34eaf542 15278 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15279 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15280 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15281 = XOBNEWVEC (&objfile->objfile_obstack,
15282 struct symbol *,
15283 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15284 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15285 template_args.data (),
34eaf542
TT
15286 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15287 * sizeof (struct symbol *)));
34eaf542
TT
15288 }
15289
c906108c 15290 /* Attach fields and member functions to the type. */
317f7127 15291 if (fi.nfields () > 0)
e7c27a73 15292 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15293 if (!fi.fnfieldlists.empty ())
c906108c 15294 {
e7c27a73 15295 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15296
c5aa993b 15297 /* Get the type which refers to the base class (possibly this
c906108c 15298 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15299 class from the DW_AT_containing_type attribute. This use of
15300 DW_AT_containing_type is a GNU extension. */
c906108c 15301
e142c38c 15302 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15303 {
e7c27a73 15304 struct type *t = die_containing_type (die, cu);
c906108c 15305
ae6ae975 15306 set_type_vptr_basetype (type, t);
c906108c
SS
15307 if (type == t)
15308 {
c906108c
SS
15309 int i;
15310
15311 /* Our own class provides vtbl ptr. */
15312 for (i = TYPE_NFIELDS (t) - 1;
15313 i >= TYPE_N_BASECLASSES (t);
15314 --i)
15315 {
0d5cff50 15316 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15317
1168df01 15318 if (is_vtable_name (fieldname, cu))
c906108c 15319 {
ae6ae975 15320 set_type_vptr_fieldno (type, i);
c906108c
SS
15321 break;
15322 }
15323 }
15324
15325 /* Complain if virtual function table field not found. */
15326 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15327 complaint (_("virtual function table pointer "
3e43a32a 15328 "not found when defining class '%s'"),
e86ca25f 15329 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15330 }
15331 else
15332 {
ae6ae975 15333 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15334 }
15335 }
f6235d4c 15336 else if (cu->producer
61012eef 15337 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15338 {
15339 /* The IBM XLC compiler does not provide direct indication
15340 of the containing type, but the vtable pointer is
15341 always named __vfp. */
15342
15343 int i;
15344
15345 for (i = TYPE_NFIELDS (type) - 1;
15346 i >= TYPE_N_BASECLASSES (type);
15347 --i)
15348 {
15349 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15350 {
ae6ae975
DE
15351 set_type_vptr_fieldno (type, i);
15352 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15353 break;
15354 }
15355 }
15356 }
c906108c 15357 }
98751a41
JK
15358
15359 /* Copy fi.typedef_field_list linked list elements content into the
15360 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15361 if (!fi.typedef_field_list.empty ())
98751a41 15362 {
be2daae6 15363 int count = fi.typedef_field_list.size ();
98751a41 15364
a0d7a4ff 15365 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15366 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15367 = ((struct decl_field *)
be2daae6
TT
15368 TYPE_ALLOC (type,
15369 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15370 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15371
be2daae6
TT
15372 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15373 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15374 }
c767944b 15375
883fd55a
KS
15376 /* Copy fi.nested_types_list linked list elements content into the
15377 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15378 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15379 {
be2daae6 15380 int count = fi.nested_types_list.size ();
883fd55a
KS
15381
15382 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15383 TYPE_NESTED_TYPES_ARRAY (type)
15384 = ((struct decl_field *)
be2daae6
TT
15385 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15386 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15387
be2daae6
TT
15388 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15389 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15390 }
c906108c 15391 }
63d06c5c 15392
bb5ed363 15393 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15394 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15395 cu->rust_unions.push_back (type);
0b92b5bb 15396
90aeadfc
DC
15397 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15398 snapshots) has been known to create a die giving a declaration
15399 for a class that has, as a child, a die giving a definition for a
15400 nested class. So we have to process our children even if the
15401 current die is a declaration. Normally, of course, a declaration
15402 won't have any children at all. */
134d01f1 15403
ca040673
DE
15404 child_die = die->child;
15405
90aeadfc
DC
15406 while (child_die != NULL && child_die->tag)
15407 {
15408 if (child_die->tag == DW_TAG_member
15409 || child_die->tag == DW_TAG_variable
34eaf542
TT
15410 || child_die->tag == DW_TAG_inheritance
15411 || child_die->tag == DW_TAG_template_value_param
15412 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15413 {
90aeadfc 15414 /* Do nothing. */
134d01f1 15415 }
90aeadfc
DC
15416 else
15417 process_die (child_die, cu);
134d01f1 15418
436c571c 15419 child_die = child_die->sibling;
134d01f1
DJ
15420 }
15421
fa4028e9
JB
15422 /* Do not consider external references. According to the DWARF standard,
15423 these DIEs are identified by the fact that they have no byte_size
15424 attribute, and a declaration attribute. */
15425 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15426 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15427 {
15428 struct symbol *sym = new_symbol (die, type, cu);
15429
15430 if (has_template_parameters)
15431 {
a776957c
TT
15432 struct symtab *symtab;
15433 if (sym != nullptr)
15434 symtab = symbol_symtab (sym);
15435 else if (cu->line_header != nullptr)
15436 {
15437 /* Any related symtab will do. */
15438 symtab
7ba99d21 15439 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15440 }
15441 else
15442 {
15443 symtab = nullptr;
15444 complaint (_("could not find suitable "
15445 "symtab for template parameter"
15446 " - DIE at %s [in module %s]"),
15447 sect_offset_str (die->sect_off),
15448 objfile_name (objfile));
15449 }
15450
15451 if (symtab != nullptr)
15452 {
15453 /* Make sure that the symtab is set on the new symbols.
15454 Even though they don't appear in this symtab directly,
15455 other parts of gdb assume that symbols do, and this is
15456 reasonably true. */
15457 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15458 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15459 }
3e1d3d8c
TT
15460 }
15461 }
134d01f1
DJ
15462}
15463
55426c9d
JB
15464/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15465 update TYPE using some information only available in DIE's children. */
15466
15467static void
15468update_enumeration_type_from_children (struct die_info *die,
15469 struct type *type,
15470 struct dwarf2_cu *cu)
15471{
60f7655a 15472 struct die_info *child_die;
55426c9d
JB
15473 int unsigned_enum = 1;
15474 int flag_enum = 1;
55426c9d 15475
8268c778 15476 auto_obstack obstack;
55426c9d 15477
60f7655a
DE
15478 for (child_die = die->child;
15479 child_die != NULL && child_die->tag;
436c571c 15480 child_die = child_die->sibling)
55426c9d
JB
15481 {
15482 struct attribute *attr;
15483 LONGEST value;
15484 const gdb_byte *bytes;
15485 struct dwarf2_locexpr_baton *baton;
15486 const char *name;
60f7655a 15487
55426c9d
JB
15488 if (child_die->tag != DW_TAG_enumerator)
15489 continue;
15490
15491 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15492 if (attr == NULL)
15493 continue;
15494
15495 name = dwarf2_name (child_die, cu);
15496 if (name == NULL)
15497 name = "<anonymous enumerator>";
15498
15499 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15500 &value, &bytes, &baton);
15501 if (value < 0)
15502 {
15503 unsigned_enum = 0;
15504 flag_enum = 0;
15505 }
55426c9d 15506 else
edd45eb0
SM
15507 {
15508 if (count_one_bits_ll (value) >= 2)
15509 flag_enum = 0;
edd45eb0 15510 }
55426c9d
JB
15511
15512 /* If we already know that the enum type is neither unsigned, nor
15513 a flag type, no need to look at the rest of the enumerates. */
15514 if (!unsigned_enum && !flag_enum)
15515 break;
55426c9d
JB
15516 }
15517
15518 if (unsigned_enum)
15519 TYPE_UNSIGNED (type) = 1;
15520 if (flag_enum)
15521 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15522}
15523
134d01f1
DJ
15524/* Given a DW_AT_enumeration_type die, set its type. We do not
15525 complete the type's fields yet, or create any symbols. */
c906108c 15526
f792889a 15527static struct type *
134d01f1 15528read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15529{
518817b3 15530 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15531 struct type *type;
c906108c 15532 struct attribute *attr;
0114d602 15533 const char *name;
134d01f1 15534
348e048f
DE
15535 /* If the definition of this type lives in .debug_types, read that type.
15536 Don't follow DW_AT_specification though, that will take us back up
15537 the chain and we want to go down. */
052c8bb8 15538 attr = die->attr (DW_AT_signature);
435d3d88 15539 if (attr != nullptr)
348e048f 15540 {
ac9ec31b 15541 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15542
ac9ec31b 15543 /* The type's CU may not be the same as CU.
02142a6c 15544 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15545 return set_die_type (die, type, cu);
15546 }
15547
c906108c
SS
15548 type = alloc_type (objfile);
15549
15550 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15551 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15552 if (name != NULL)
e86ca25f 15553 TYPE_NAME (type) = name;
c906108c 15554
0626fc76
TT
15555 attr = dwarf2_attr (die, DW_AT_type, cu);
15556 if (attr != NULL)
15557 {
15558 struct type *underlying_type = die_type (die, cu);
15559
15560 TYPE_TARGET_TYPE (type) = underlying_type;
15561 }
15562
e142c38c 15563 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15564 if (attr != nullptr)
c906108c
SS
15565 {
15566 TYPE_LENGTH (type) = DW_UNSND (attr);
15567 }
15568 else
15569 {
15570 TYPE_LENGTH (type) = 0;
15571 }
15572
2b4424c3
TT
15573 maybe_set_alignment (cu, die, type);
15574
137033e9
JB
15575 /* The enumeration DIE can be incomplete. In Ada, any type can be
15576 declared as private in the package spec, and then defined only
15577 inside the package body. Such types are known as Taft Amendment
15578 Types. When another package uses such a type, an incomplete DIE
15579 may be generated by the compiler. */
02eb380e 15580 if (die_is_declaration (die, cu))
876cecd0 15581 TYPE_STUB (type) = 1;
02eb380e 15582
0626fc76
TT
15583 /* Finish the creation of this type by using the enum's children.
15584 We must call this even when the underlying type has been provided
15585 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15586 update_enumeration_type_from_children (die, type, cu);
15587
0626fc76
TT
15588 /* If this type has an underlying type that is not a stub, then we
15589 may use its attributes. We always use the "unsigned" attribute
15590 in this situation, because ordinarily we guess whether the type
15591 is unsigned -- but the guess can be wrong and the underlying type
15592 can tell us the reality. However, we defer to a local size
15593 attribute if one exists, because this lets the compiler override
15594 the underlying type if needed. */
15595 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15596 {
9e7c9a03
HD
15597 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15598 underlying_type = check_typedef (underlying_type);
15599 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 15600 if (TYPE_LENGTH (type) == 0)
9e7c9a03 15601 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 15602 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
15603 && TYPE_RAW_ALIGN (underlying_type) != 0)
15604 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
15605 }
15606
3d567982
TT
15607 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15608
f792889a 15609 return set_die_type (die, type, cu);
134d01f1
DJ
15610}
15611
15612/* Given a pointer to a die which begins an enumeration, process all
15613 the dies that define the members of the enumeration, and create the
15614 symbol for the enumeration type.
15615
15616 NOTE: We reverse the order of the element list. */
15617
15618static void
15619process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15620{
f792889a 15621 struct type *this_type;
134d01f1 15622
f792889a
DJ
15623 this_type = get_die_type (die, cu);
15624 if (this_type == NULL)
15625 this_type = read_enumeration_type (die, cu);
9dc481d3 15626
639d11d3 15627 if (die->child != NULL)
c906108c 15628 {
9dc481d3
DE
15629 struct die_info *child_die;
15630 struct symbol *sym;
43816ebc 15631 std::vector<struct field> fields;
15d034d0 15632 const char *name;
9dc481d3 15633
639d11d3 15634 child_die = die->child;
c906108c
SS
15635 while (child_die && child_die->tag)
15636 {
15637 if (child_die->tag != DW_TAG_enumerator)
15638 {
e7c27a73 15639 process_die (child_die, cu);
c906108c
SS
15640 }
15641 else
15642 {
39cbfefa
DJ
15643 name = dwarf2_name (child_die, cu);
15644 if (name)
c906108c 15645 {
f792889a 15646 sym = new_symbol (child_die, this_type, cu);
c906108c 15647
43816ebc
TT
15648 fields.emplace_back ();
15649 struct field &field = fields.back ();
c906108c 15650
43816ebc
TT
15651 FIELD_NAME (field) = sym->linkage_name ();
15652 FIELD_TYPE (field) = NULL;
15653 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15654 FIELD_BITSIZE (field) = 0;
c906108c
SS
15655 }
15656 }
15657
436c571c 15658 child_die = child_die->sibling;
c906108c
SS
15659 }
15660
43816ebc 15661 if (!fields.empty ())
c906108c 15662 {
43816ebc 15663 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15664 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15665 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15666 memcpy (TYPE_FIELDS (this_type), fields.data (),
15667 sizeof (struct field) * fields.size ());
c906108c 15668 }
c906108c 15669 }
134d01f1 15670
6c83ed52
TT
15671 /* If we are reading an enum from a .debug_types unit, and the enum
15672 is a declaration, and the enum is not the signatured type in the
15673 unit, then we do not want to add a symbol for it. Adding a
15674 symbol would in some cases obscure the true definition of the
15675 enum, giving users an incomplete type when the definition is
15676 actually available. Note that we do not want to do this for all
15677 enums which are just declarations, because C++0x allows forward
15678 enum declarations. */
3019eac3 15679 if (cu->per_cu->is_debug_types
6c83ed52
TT
15680 && die_is_declaration (die, cu))
15681 {
52dc124a 15682 struct signatured_type *sig_type;
6c83ed52 15683
c0f78cd4 15684 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15685 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15686 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15687 return;
15688 }
15689
f792889a 15690 new_symbol (die, this_type, cu);
c906108c
SS
15691}
15692
15693/* Extract all information from a DW_TAG_array_type DIE and put it in
15694 the DIE's type field. For now, this only handles one dimensional
15695 arrays. */
15696
f792889a 15697static struct type *
e7c27a73 15698read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15699{
518817b3 15700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15701 struct die_info *child_die;
7e314c57 15702 struct type *type;
c906108c 15703 struct type *element_type, *range_type, *index_type;
c906108c 15704 struct attribute *attr;
15d034d0 15705 const char *name;
a405673c 15706 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15707 unsigned int bit_stride = 0;
c906108c 15708
e7c27a73 15709 element_type = die_type (die, cu);
c906108c 15710
7e314c57
JK
15711 /* The die_type call above may have already set the type for this DIE. */
15712 type = get_die_type (die, cu);
15713 if (type)
15714 return type;
15715
dc53a7ad
JB
15716 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15717 if (attr != NULL)
a405673c
JB
15718 {
15719 int stride_ok;
09ba997f 15720 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15721
15722 byte_stride_prop
15723 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15724 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15725 prop_type);
a405673c
JB
15726 if (!stride_ok)
15727 {
b98664d3 15728 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15729 " - DIE at %s [in module %s]"),
15730 sect_offset_str (die->sect_off),
518817b3 15731 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15732 /* Ignore this attribute. We will likely not be able to print
15733 arrays of this type correctly, but there is little we can do
15734 to help if we cannot read the attribute's value. */
15735 byte_stride_prop = NULL;
15736 }
15737 }
dc53a7ad
JB
15738
15739 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15740 if (attr != NULL)
15741 bit_stride = DW_UNSND (attr);
15742
c906108c
SS
15743 /* Irix 6.2 native cc creates array types without children for
15744 arrays with unspecified length. */
639d11d3 15745 if (die->child == NULL)
c906108c 15746 {
46bf5051 15747 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15748 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15749 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15750 byte_stride_prop, bit_stride);
f792889a 15751 return set_die_type (die, type, cu);
c906108c
SS
15752 }
15753
791afaa2 15754 std::vector<struct type *> range_types;
639d11d3 15755 child_die = die->child;
c906108c
SS
15756 while (child_die && child_die->tag)
15757 {
15758 if (child_die->tag == DW_TAG_subrange_type)
15759 {
f792889a 15760 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15761
f792889a 15762 if (child_type != NULL)
a02abb62 15763 {
0963b4bd
MS
15764 /* The range type was succesfully read. Save it for the
15765 array type creation. */
791afaa2 15766 range_types.push_back (child_type);
a02abb62 15767 }
c906108c 15768 }
436c571c 15769 child_die = child_die->sibling;
c906108c
SS
15770 }
15771
15772 /* Dwarf2 dimensions are output from left to right, create the
15773 necessary array types in backwards order. */
7ca2d3a3 15774
c906108c 15775 type = element_type;
7ca2d3a3
DL
15776
15777 if (read_array_order (die, cu) == DW_ORD_col_major)
15778 {
15779 int i = 0;
9a619af0 15780
791afaa2 15781 while (i < range_types.size ())
dc53a7ad 15782 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15783 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15784 }
15785 else
15786 {
791afaa2 15787 size_t ndim = range_types.size ();
7ca2d3a3 15788 while (ndim-- > 0)
dc53a7ad 15789 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15790 byte_stride_prop, bit_stride);
7ca2d3a3 15791 }
c906108c 15792
f5f8a009
EZ
15793 /* Understand Dwarf2 support for vector types (like they occur on
15794 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15795 array type. This is not part of the Dwarf2/3 standard yet, but a
15796 custom vendor extension. The main difference between a regular
15797 array and the vector variant is that vectors are passed by value
15798 to functions. */
e142c38c 15799 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15800 if (attr != nullptr)
ea37ba09 15801 make_vector_type (type);
f5f8a009 15802
dbc98a8b
KW
15803 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15804 implementation may choose to implement triple vectors using this
15805 attribute. */
15806 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15807 if (attr != nullptr)
dbc98a8b
KW
15808 {
15809 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15810 TYPE_LENGTH (type) = DW_UNSND (attr);
15811 else
b98664d3 15812 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15813 "than the total size of elements"));
dbc98a8b
KW
15814 }
15815
39cbfefa
DJ
15816 name = dwarf2_name (die, cu);
15817 if (name)
15818 TYPE_NAME (type) = name;
6e70227d 15819
2b4424c3
TT
15820 maybe_set_alignment (cu, die, type);
15821
0963b4bd 15822 /* Install the type in the die. */
7e314c57
JK
15823 set_die_type (die, type, cu);
15824
15825 /* set_die_type should be already done. */
b4ba55a1
JB
15826 set_descriptive_type (type, die, cu);
15827
7e314c57 15828 return type;
c906108c
SS
15829}
15830
7ca2d3a3 15831static enum dwarf_array_dim_ordering
6e70227d 15832read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15833{
15834 struct attribute *attr;
15835
15836 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15837
435d3d88 15838 if (attr != nullptr)
aead7601 15839 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15840
0963b4bd
MS
15841 /* GNU F77 is a special case, as at 08/2004 array type info is the
15842 opposite order to the dwarf2 specification, but data is still
15843 laid out as per normal fortran.
7ca2d3a3 15844
0963b4bd
MS
15845 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15846 version checking. */
7ca2d3a3 15847
905e0470
PM
15848 if (cu->language == language_fortran
15849 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15850 {
15851 return DW_ORD_row_major;
15852 }
15853
6e70227d 15854 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15855 {
15856 case array_column_major:
15857 return DW_ORD_col_major;
15858 case array_row_major:
15859 default:
15860 return DW_ORD_row_major;
15861 };
15862}
15863
72019c9c 15864/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15865 the DIE's type field. */
72019c9c 15866
f792889a 15867static struct type *
72019c9c
GM
15868read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15869{
7e314c57
JK
15870 struct type *domain_type, *set_type;
15871 struct attribute *attr;
f792889a 15872
7e314c57
JK
15873 domain_type = die_type (die, cu);
15874
15875 /* The die_type call above may have already set the type for this DIE. */
15876 set_type = get_die_type (die, cu);
15877 if (set_type)
15878 return set_type;
15879
15880 set_type = create_set_type (NULL, domain_type);
15881
15882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15883 if (attr != nullptr)
d09039dd 15884 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15885
2b4424c3
TT
15886 maybe_set_alignment (cu, die, set_type);
15887
f792889a 15888 return set_die_type (die, set_type, cu);
72019c9c 15889}
7ca2d3a3 15890
0971de02
TT
15891/* A helper for read_common_block that creates a locexpr baton.
15892 SYM is the symbol which we are marking as computed.
15893 COMMON_DIE is the DIE for the common block.
15894 COMMON_LOC is the location expression attribute for the common
15895 block itself.
15896 MEMBER_LOC is the location expression attribute for the particular
15897 member of the common block that we are processing.
15898 CU is the CU from which the above come. */
15899
15900static void
15901mark_common_block_symbol_computed (struct symbol *sym,
15902 struct die_info *common_die,
15903 struct attribute *common_loc,
15904 struct attribute *member_loc,
15905 struct dwarf2_cu *cu)
15906{
518817b3
SM
15907 struct dwarf2_per_objfile *dwarf2_per_objfile
15908 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15909 struct objfile *objfile = dwarf2_per_objfile->objfile;
15910 struct dwarf2_locexpr_baton *baton;
15911 gdb_byte *ptr;
15912 unsigned int cu_off;
15913 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15914 LONGEST offset = 0;
15915
15916 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15917 gdb_assert (common_loc->form_is_block ());
15918 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15919 || member_loc->form_is_constant ());
0971de02 15920
8d749320 15921 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15922 baton->per_cu = cu->per_cu;
15923 gdb_assert (baton->per_cu);
15924
15925 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15926
cd6c91b4 15927 if (member_loc->form_is_constant ())
0971de02 15928 {
0826b30a 15929 offset = member_loc->constant_value (0);
0971de02
TT
15930 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15931 }
15932 else
15933 baton->size += DW_BLOCK (member_loc)->size;
15934
224c3ddb 15935 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15936 baton->data = ptr;
15937
15938 *ptr++ = DW_OP_call4;
9c541725 15939 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15940 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15941 ptr += 4;
15942
cd6c91b4 15943 if (member_loc->form_is_constant ())
0971de02
TT
15944 {
15945 *ptr++ = DW_OP_addr;
15946 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15947 ptr += cu->header.addr_size;
15948 }
15949 else
15950 {
15951 /* We have to copy the data here, because DW_OP_call4 will only
15952 use a DW_AT_location attribute. */
15953 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15954 ptr += DW_BLOCK (member_loc)->size;
15955 }
15956
15957 *ptr++ = DW_OP_plus;
15958 gdb_assert (ptr - baton->data == baton->size);
15959
0971de02 15960 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15961 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15962}
15963
4357ac6c
TT
15964/* Create appropriate locally-scoped variables for all the
15965 DW_TAG_common_block entries. Also create a struct common_block
15966 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15967 is used to separate the common blocks name namespace from regular
4357ac6c 15968 variable names. */
c906108c
SS
15969
15970static void
e7c27a73 15971read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15972{
0971de02
TT
15973 struct attribute *attr;
15974
15975 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15976 if (attr != nullptr)
0971de02
TT
15977 {
15978 /* Support the .debug_loc offsets. */
4fc6c0d5 15979 if (attr->form_is_block ())
0971de02
TT
15980 {
15981 /* Ok. */
15982 }
cd6c91b4 15983 else if (attr->form_is_section_offset ())
0971de02
TT
15984 {
15985 dwarf2_complex_location_expr_complaint ();
15986 attr = NULL;
15987 }
15988 else
15989 {
15990 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15991 "common block member");
15992 attr = NULL;
15993 }
15994 }
15995
639d11d3 15996 if (die->child != NULL)
c906108c 15997 {
518817b3 15998 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
15999 struct die_info *child_die;
16000 size_t n_entries = 0, size;
16001 struct common_block *common_block;
16002 struct symbol *sym;
74ac6d43 16003
4357ac6c
TT
16004 for (child_die = die->child;
16005 child_die && child_die->tag;
436c571c 16006 child_die = child_die->sibling)
4357ac6c
TT
16007 ++n_entries;
16008
16009 size = (sizeof (struct common_block)
16010 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16011 common_block
16012 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16013 size);
4357ac6c
TT
16014 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16015 common_block->n_entries = 0;
16016
16017 for (child_die = die->child;
16018 child_die && child_die->tag;
436c571c 16019 child_die = child_die->sibling)
4357ac6c
TT
16020 {
16021 /* Create the symbol in the DW_TAG_common_block block in the current
16022 symbol scope. */
e7c27a73 16023 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16024 if (sym != NULL)
16025 {
16026 struct attribute *member_loc;
16027
16028 common_block->contents[common_block->n_entries++] = sym;
16029
16030 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16031 cu);
16032 if (member_loc)
16033 {
16034 /* GDB has handled this for a long time, but it is
16035 not specified by DWARF. It seems to have been
16036 emitted by gfortran at least as recently as:
16037 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16038 complaint (_("Variable in common block has "
0971de02 16039 "DW_AT_data_member_location "
9d8780f0
SM
16040 "- DIE at %s [in module %s]"),
16041 sect_offset_str (child_die->sect_off),
518817b3 16042 objfile_name (objfile));
0971de02 16043
cd6c91b4 16044 if (member_loc->form_is_section_offset ())
0971de02 16045 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16046 else if (member_loc->form_is_constant ()
4fc6c0d5 16047 || member_loc->form_is_block ())
0971de02 16048 {
435d3d88 16049 if (attr != nullptr)
0971de02
TT
16050 mark_common_block_symbol_computed (sym, die, attr,
16051 member_loc, cu);
16052 }
16053 else
16054 dwarf2_complex_location_expr_complaint ();
16055 }
16056 }
c906108c 16057 }
4357ac6c
TT
16058
16059 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16060 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16061 }
16062}
16063
0114d602 16064/* Create a type for a C++ namespace. */
d9fa45fe 16065
0114d602
DJ
16066static struct type *
16067read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16068{
518817b3 16069 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16070 const char *previous_prefix, *name;
9219021c 16071 int is_anonymous;
0114d602
DJ
16072 struct type *type;
16073
16074 /* For extensions, reuse the type of the original namespace. */
16075 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16076 {
16077 struct die_info *ext_die;
16078 struct dwarf2_cu *ext_cu = cu;
9a619af0 16079
0114d602
DJ
16080 ext_die = dwarf2_extension (die, &ext_cu);
16081 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16082
16083 /* EXT_CU may not be the same as CU.
02142a6c 16084 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16085 return set_die_type (die, type, cu);
16086 }
9219021c 16087
e142c38c 16088 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16089
16090 /* Now build the name of the current namespace. */
16091
0114d602
DJ
16092 previous_prefix = determine_prefix (die, cu);
16093 if (previous_prefix[0] != '\0')
16094 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16095 previous_prefix, name, 0, cu);
0114d602
DJ
16096
16097 /* Create the type. */
19f392bc 16098 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16099
60531b24 16100 return set_die_type (die, type, cu);
0114d602
DJ
16101}
16102
22cee43f 16103/* Read a namespace scope. */
0114d602
DJ
16104
16105static void
16106read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16107{
518817b3 16108 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16109 int is_anonymous;
9219021c 16110
5c4e30ca
DC
16111 /* Add a symbol associated to this if we haven't seen the namespace
16112 before. Also, add a using directive if it's an anonymous
16113 namespace. */
9219021c 16114
f2f0e013 16115 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16116 {
16117 struct type *type;
16118
0114d602 16119 type = read_type_die (die, cu);
e7c27a73 16120 new_symbol (die, type, cu);
5c4e30ca 16121
e8e80198 16122 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16123 if (is_anonymous)
0114d602
DJ
16124 {
16125 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16126
eb1e02fd 16127 std::vector<const char *> excludes;
804d2729 16128 add_using_directive (using_directives (cu),
22cee43f 16129 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16130 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16131 }
5c4e30ca 16132 }
9219021c 16133
639d11d3 16134 if (die->child != NULL)
d9fa45fe 16135 {
639d11d3 16136 struct die_info *child_die = die->child;
6e70227d 16137
d9fa45fe
DC
16138 while (child_die && child_die->tag)
16139 {
e7c27a73 16140 process_die (child_die, cu);
436c571c 16141 child_die = child_die->sibling;
d9fa45fe
DC
16142 }
16143 }
38d518c9
EZ
16144}
16145
f55ee35c
JK
16146/* Read a Fortran module as type. This DIE can be only a declaration used for
16147 imported module. Still we need that type as local Fortran "use ... only"
16148 declaration imports depend on the created type in determine_prefix. */
16149
16150static struct type *
16151read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16152{
518817b3 16153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16154 const char *module_name;
f55ee35c
JK
16155 struct type *type;
16156
16157 module_name = dwarf2_name (die, cu);
19f392bc 16158 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16159
f55ee35c
JK
16160 return set_die_type (die, type, cu);
16161}
16162
5d7cb8df
JK
16163/* Read a Fortran module. */
16164
16165static void
16166read_module (struct die_info *die, struct dwarf2_cu *cu)
16167{
16168 struct die_info *child_die = die->child;
530e8392
KB
16169 struct type *type;
16170
16171 type = read_type_die (die, cu);
16172 new_symbol (die, type, cu);
5d7cb8df 16173
5d7cb8df
JK
16174 while (child_die && child_die->tag)
16175 {
16176 process_die (child_die, cu);
436c571c 16177 child_die = child_die->sibling;
5d7cb8df
JK
16178 }
16179}
16180
38d518c9
EZ
16181/* Return the name of the namespace represented by DIE. Set
16182 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16183 namespace. */
16184
16185static const char *
e142c38c 16186namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16187{
16188 struct die_info *current_die;
16189 const char *name = NULL;
16190
16191 /* Loop through the extensions until we find a name. */
16192
16193 for (current_die = die;
16194 current_die != NULL;
f2f0e013 16195 current_die = dwarf2_extension (die, &cu))
38d518c9 16196 {
96553a0c
DE
16197 /* We don't use dwarf2_name here so that we can detect the absence
16198 of a name -> anonymous namespace. */
7d45c7c3 16199 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16200
38d518c9
EZ
16201 if (name != NULL)
16202 break;
16203 }
16204
16205 /* Is it an anonymous namespace? */
16206
16207 *is_anonymous = (name == NULL);
16208 if (*is_anonymous)
2b1dbab0 16209 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16210
16211 return name;
d9fa45fe
DC
16212}
16213
c906108c
SS
16214/* Extract all information from a DW_TAG_pointer_type DIE and add to
16215 the user defined type vector. */
16216
f792889a 16217static struct type *
e7c27a73 16218read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16219{
518817b3
SM
16220 struct gdbarch *gdbarch
16221 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16222 struct comp_unit_head *cu_header = &cu->header;
c906108c 16223 struct type *type;
8b2dbe47
KB
16224 struct attribute *attr_byte_size;
16225 struct attribute *attr_address_class;
16226 int byte_size, addr_class;
7e314c57
JK
16227 struct type *target_type;
16228
16229 target_type = die_type (die, cu);
c906108c 16230
7e314c57
JK
16231 /* The die_type call above may have already set the type for this DIE. */
16232 type = get_die_type (die, cu);
16233 if (type)
16234 return type;
16235
16236 type = lookup_pointer_type (target_type);
8b2dbe47 16237
e142c38c 16238 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16239 if (attr_byte_size)
16240 byte_size = DW_UNSND (attr_byte_size);
c906108c 16241 else
8b2dbe47
KB
16242 byte_size = cu_header->addr_size;
16243
e142c38c 16244 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16245 if (attr_address_class)
16246 addr_class = DW_UNSND (attr_address_class);
16247 else
16248 addr_class = DW_ADDR_none;
16249
2b4424c3
TT
16250 ULONGEST alignment = get_alignment (cu, die);
16251
16252 /* If the pointer size, alignment, or address class is different
16253 than the default, create a type variant marked as such and set
16254 the length accordingly. */
16255 if (TYPE_LENGTH (type) != byte_size
16256 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16257 && alignment != TYPE_RAW_ALIGN (type))
16258 || addr_class != DW_ADDR_none)
c906108c 16259 {
5e2b427d 16260 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16261 {
16262 int type_flags;
16263
849957d9 16264 type_flags = gdbarch_address_class_type_flags
5e2b427d 16265 (gdbarch, byte_size, addr_class);
876cecd0
TT
16266 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16267 == 0);
8b2dbe47
KB
16268 type = make_type_with_address_space (type, type_flags);
16269 }
16270 else if (TYPE_LENGTH (type) != byte_size)
16271 {
b98664d3 16272 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16273 }
2b4424c3
TT
16274 else if (TYPE_RAW_ALIGN (type) != alignment)
16275 {
b98664d3 16276 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16277 " - DIE at %s [in module %s]"),
16278 sect_offset_str (die->sect_off),
16279 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16280 }
6e70227d 16281 else
9a619af0
MS
16282 {
16283 /* Should we also complain about unhandled address classes? */
16284 }
c906108c 16285 }
8b2dbe47
KB
16286
16287 TYPE_LENGTH (type) = byte_size;
2b4424c3 16288 set_type_align (type, alignment);
f792889a 16289 return set_die_type (die, type, cu);
c906108c
SS
16290}
16291
16292/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16293 the user defined type vector. */
16294
f792889a 16295static struct type *
e7c27a73 16296read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16297{
16298 struct type *type;
16299 struct type *to_type;
16300 struct type *domain;
16301
e7c27a73
DJ
16302 to_type = die_type (die, cu);
16303 domain = die_containing_type (die, cu);
0d5de010 16304
7e314c57
JK
16305 /* The calls above may have already set the type for this DIE. */
16306 type = get_die_type (die, cu);
16307 if (type)
16308 return type;
16309
0d5de010
DJ
16310 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16311 type = lookup_methodptr_type (to_type);
7078baeb
TT
16312 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16313 {
518817b3
SM
16314 struct type *new_type
16315 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16316
16317 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16318 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16319 TYPE_VARARGS (to_type));
16320 type = lookup_methodptr_type (new_type);
16321 }
0d5de010
DJ
16322 else
16323 type = lookup_memberptr_type (to_type, domain);
c906108c 16324
f792889a 16325 return set_die_type (die, type, cu);
c906108c
SS
16326}
16327
4297a3f0 16328/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16329 the user defined type vector. */
16330
f792889a 16331static struct type *
4297a3f0
AV
16332read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16333 enum type_code refcode)
c906108c 16334{
e7c27a73 16335 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16336 struct type *type, *target_type;
c906108c
SS
16337 struct attribute *attr;
16338
4297a3f0
AV
16339 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16340
7e314c57
JK
16341 target_type = die_type (die, cu);
16342
16343 /* The die_type call above may have already set the type for this DIE. */
16344 type = get_die_type (die, cu);
16345 if (type)
16346 return type;
16347
4297a3f0 16348 type = lookup_reference_type (target_type, refcode);
e142c38c 16349 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16350 if (attr != nullptr)
c906108c
SS
16351 {
16352 TYPE_LENGTH (type) = DW_UNSND (attr);
16353 }
16354 else
16355 {
107d2387 16356 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16357 }
2b4424c3 16358 maybe_set_alignment (cu, die, type);
f792889a 16359 return set_die_type (die, type, cu);
c906108c
SS
16360}
16361
cf363f18
MW
16362/* Add the given cv-qualifiers to the element type of the array. GCC
16363 outputs DWARF type qualifiers that apply to an array, not the
16364 element type. But GDB relies on the array element type to carry
16365 the cv-qualifiers. This mimics section 6.7.3 of the C99
16366 specification. */
16367
16368static struct type *
16369add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16370 struct type *base_type, int cnst, int voltl)
16371{
16372 struct type *el_type, *inner_array;
16373
16374 base_type = copy_type (base_type);
16375 inner_array = base_type;
16376
16377 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16378 {
16379 TYPE_TARGET_TYPE (inner_array) =
16380 copy_type (TYPE_TARGET_TYPE (inner_array));
16381 inner_array = TYPE_TARGET_TYPE (inner_array);
16382 }
16383
16384 el_type = TYPE_TARGET_TYPE (inner_array);
16385 cnst |= TYPE_CONST (el_type);
16386 voltl |= TYPE_VOLATILE (el_type);
16387 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16388
16389 return set_die_type (die, base_type, cu);
16390}
16391
f792889a 16392static struct type *
e7c27a73 16393read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16394{
f792889a 16395 struct type *base_type, *cv_type;
c906108c 16396
e7c27a73 16397 base_type = die_type (die, cu);
7e314c57
JK
16398
16399 /* The die_type call above may have already set the type for this DIE. */
16400 cv_type = get_die_type (die, cu);
16401 if (cv_type)
16402 return cv_type;
16403
2f608a3a
KW
16404 /* In case the const qualifier is applied to an array type, the element type
16405 is so qualified, not the array type (section 6.7.3 of C99). */
16406 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16407 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16408
f792889a
DJ
16409 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16410 return set_die_type (die, cv_type, cu);
c906108c
SS
16411}
16412
f792889a 16413static struct type *
e7c27a73 16414read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16415{
f792889a 16416 struct type *base_type, *cv_type;
c906108c 16417
e7c27a73 16418 base_type = die_type (die, cu);
7e314c57
JK
16419
16420 /* The die_type call above may have already set the type for this DIE. */
16421 cv_type = get_die_type (die, cu);
16422 if (cv_type)
16423 return cv_type;
16424
cf363f18
MW
16425 /* In case the volatile qualifier is applied to an array type, the
16426 element type is so qualified, not the array type (section 6.7.3
16427 of C99). */
16428 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16429 return add_array_cv_type (die, cu, base_type, 0, 1);
16430
f792889a
DJ
16431 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16432 return set_die_type (die, cv_type, cu);
c906108c
SS
16433}
16434
06d66ee9
TT
16435/* Handle DW_TAG_restrict_type. */
16436
16437static struct type *
16438read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16439{
16440 struct type *base_type, *cv_type;
16441
16442 base_type = die_type (die, cu);
16443
16444 /* The die_type call above may have already set the type for this DIE. */
16445 cv_type = get_die_type (die, cu);
16446 if (cv_type)
16447 return cv_type;
16448
16449 cv_type = make_restrict_type (base_type);
16450 return set_die_type (die, cv_type, cu);
16451}
16452
a2c2acaf
MW
16453/* Handle DW_TAG_atomic_type. */
16454
16455static struct type *
16456read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16457{
16458 struct type *base_type, *cv_type;
16459
16460 base_type = die_type (die, cu);
16461
16462 /* The die_type call above may have already set the type for this DIE. */
16463 cv_type = get_die_type (die, cu);
16464 if (cv_type)
16465 return cv_type;
16466
16467 cv_type = make_atomic_type (base_type);
16468 return set_die_type (die, cv_type, cu);
16469}
16470
c906108c
SS
16471/* Extract all information from a DW_TAG_string_type DIE and add to
16472 the user defined type vector. It isn't really a user defined type,
16473 but it behaves like one, with other DIE's using an AT_user_def_type
16474 attribute to reference it. */
16475
f792889a 16476static struct type *
e7c27a73 16477read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16478{
518817b3 16479 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16480 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16481 struct type *type, *range_type, *index_type, *char_type;
16482 struct attribute *attr;
216a7e6b
AB
16483 struct dynamic_prop prop;
16484 bool length_is_constant = true;
16485 LONGEST length;
16486
16487 /* There are a couple of places where bit sizes might be made use of
16488 when parsing a DW_TAG_string_type, however, no producer that we know
16489 of make use of these. Handling bit sizes that are a multiple of the
16490 byte size is easy enough, but what about other bit sizes? Lets deal
16491 with that problem when we have to. Warn about these attributes being
16492 unsupported, then parse the type and ignore them like we always
16493 have. */
16494 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16495 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16496 {
16497 static bool warning_printed = false;
16498 if (!warning_printed)
16499 {
16500 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16501 "currently supported on DW_TAG_string_type."));
16502 warning_printed = true;
16503 }
16504 }
c906108c 16505
e142c38c 16506 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16507 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16508 {
16509 /* The string length describes the location at which the length of
16510 the string can be found. The size of the length field can be
16511 specified with one of the attributes below. */
16512 struct type *prop_type;
16513 struct attribute *len
16514 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16515 if (len == nullptr)
16516 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16517 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16518 {
16519 /* Pass 0 as the default as we know this attribute is constant
16520 and the default value will not be returned. */
0826b30a 16521 LONGEST sz = len->constant_value (0);
09ba997f 16522 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16523 }
16524 else
16525 {
16526 /* If the size is not specified then we assume it is the size of
16527 an address on this target. */
09ba997f 16528 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16529 }
16530
16531 /* Convert the attribute into a dynamic property. */
16532 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16533 length = 1;
16534 else
16535 length_is_constant = false;
16536 }
16537 else if (attr != nullptr)
16538 {
16539 /* This DW_AT_string_length just contains the length with no
16540 indirection. There's no need to create a dynamic property in this
16541 case. Pass 0 for the default value as we know it will not be
16542 returned in this case. */
0826b30a 16543 length = attr->constant_value (0);
216a7e6b
AB
16544 }
16545 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16546 {
216a7e6b 16547 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16548 length = attr->constant_value (1);
c906108c
SS
16549 }
16550 else
16551 {
216a7e6b
AB
16552 /* Use 1 as a fallback length if we have nothing else. */
16553 length = 1;
c906108c 16554 }
6ccb9162 16555
46bf5051 16556 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16557 if (length_is_constant)
16558 range_type = create_static_range_type (NULL, index_type, 1, length);
16559 else
16560 {
16561 struct dynamic_prop low_bound;
16562
16563 low_bound.kind = PROP_CONST;
16564 low_bound.data.const_val = 1;
16565 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16566 }
3b7538c0
UW
16567 char_type = language_string_char_type (cu->language_defn, gdbarch);
16568 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16569
f792889a 16570 return set_die_type (die, type, cu);
c906108c
SS
16571}
16572
4d804846
JB
16573/* Assuming that DIE corresponds to a function, returns nonzero
16574 if the function is prototyped. */
16575
16576static int
16577prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16578{
16579 struct attribute *attr;
16580
16581 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16582 if (attr && (DW_UNSND (attr) != 0))
16583 return 1;
16584
16585 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16586 is only meaningful for C, but the concept also extends to other
4d804846
JB
16587 languages that allow unprototyped functions (Eg: Objective C).
16588 For all other languages, assume that functions are always
16589 prototyped. */
16590 if (cu->language != language_c
16591 && cu->language != language_objc
16592 && cu->language != language_opencl)
16593 return 1;
16594
16595 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16596 prototyped and unprototyped functions; default to prototyped,
16597 since that is more common in modern code (and RealView warns
16598 about unprototyped functions). */
16599 if (producer_is_realview (cu->producer))
16600 return 1;
16601
16602 return 0;
16603}
16604
c906108c
SS
16605/* Handle DIES due to C code like:
16606
16607 struct foo
c5aa993b
JM
16608 {
16609 int (*funcp)(int a, long l);
16610 int b;
16611 };
c906108c 16612
0963b4bd 16613 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16614
f792889a 16615static struct type *
e7c27a73 16616read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16617{
518817b3 16618 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16619 struct type *type; /* Type that this function returns. */
16620 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16621 struct attribute *attr;
16622
e7c27a73 16623 type = die_type (die, cu);
7e314c57
JK
16624
16625 /* The die_type call above may have already set the type for this DIE. */
16626 ftype = get_die_type (die, cu);
16627 if (ftype)
16628 return ftype;
16629
0c8b41f1 16630 ftype = lookup_function_type (type);
c906108c 16631
4d804846 16632 if (prototyped_function_p (die, cu))
a6c727b2 16633 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16634
c055b101
CV
16635 /* Store the calling convention in the type if it's available in
16636 the subroutine die. Otherwise set the calling convention to
16637 the default value DW_CC_normal. */
16638 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16639 if (attr != nullptr
16640 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16641 TYPE_CALLING_CONVENTION (ftype)
16642 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16643 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16644 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16645 else
16646 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16647
743649fd
MW
16648 /* Record whether the function returns normally to its caller or not
16649 if the DWARF producer set that information. */
16650 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16651 if (attr && (DW_UNSND (attr) != 0))
16652 TYPE_NO_RETURN (ftype) = 1;
16653
76c10ea2
GM
16654 /* We need to add the subroutine type to the die immediately so
16655 we don't infinitely recurse when dealing with parameters
0963b4bd 16656 declared as the same subroutine type. */
76c10ea2 16657 set_die_type (die, ftype, cu);
6e70227d 16658
639d11d3 16659 if (die->child != NULL)
c906108c 16660 {
bb5ed363 16661 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16662 struct die_info *child_die;
8072405b 16663 int nparams, iparams;
c906108c
SS
16664
16665 /* Count the number of parameters.
16666 FIXME: GDB currently ignores vararg functions, but knows about
16667 vararg member functions. */
8072405b 16668 nparams = 0;
639d11d3 16669 child_die = die->child;
c906108c
SS
16670 while (child_die && child_die->tag)
16671 {
16672 if (child_die->tag == DW_TAG_formal_parameter)
16673 nparams++;
16674 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16675 TYPE_VARARGS (ftype) = 1;
436c571c 16676 child_die = child_die->sibling;
c906108c
SS
16677 }
16678
16679 /* Allocate storage for parameters and fill them in. */
16680 TYPE_NFIELDS (ftype) = nparams;
16681 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16682 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16683
8072405b
JK
16684 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16685 even if we error out during the parameters reading below. */
16686 for (iparams = 0; iparams < nparams; iparams++)
16687 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16688
16689 iparams = 0;
639d11d3 16690 child_die = die->child;
c906108c
SS
16691 while (child_die && child_die->tag)
16692 {
16693 if (child_die->tag == DW_TAG_formal_parameter)
16694 {
3ce3b1ba
PA
16695 struct type *arg_type;
16696
16697 /* DWARF version 2 has no clean way to discern C++
16698 static and non-static member functions. G++ helps
16699 GDB by marking the first parameter for non-static
16700 member functions (which is the this pointer) as
16701 artificial. We pass this information to
16702 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16703
16704 DWARF version 3 added DW_AT_object_pointer, which GCC
16705 4.5 does not yet generate. */
e142c38c 16706 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16707 if (attr != nullptr)
c906108c
SS
16708 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16709 else
9c37b5ae 16710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16711 arg_type = die_type (child_die, cu);
16712
16713 /* RealView does not mark THIS as const, which the testsuite
16714 expects. GCC marks THIS as const in method definitions,
16715 but not in the class specifications (GCC PR 43053). */
16716 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16717 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16718 {
16719 int is_this = 0;
16720 struct dwarf2_cu *arg_cu = cu;
16721 const char *name = dwarf2_name (child_die, cu);
16722
16723 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16724 if (attr != nullptr)
3ce3b1ba
PA
16725 {
16726 /* If the compiler emits this, use it. */
16727 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16728 is_this = 1;
16729 }
16730 else if (name && strcmp (name, "this") == 0)
16731 /* Function definitions will have the argument names. */
16732 is_this = 1;
16733 else if (name == NULL && iparams == 0)
16734 /* Declarations may not have the names, so like
16735 elsewhere in GDB, assume an artificial first
16736 argument is "this". */
16737 is_this = 1;
16738
16739 if (is_this)
16740 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16741 arg_type, 0);
16742 }
16743
16744 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16745 iparams++;
16746 }
436c571c 16747 child_die = child_die->sibling;
c906108c
SS
16748 }
16749 }
16750
76c10ea2 16751 return ftype;
c906108c
SS
16752}
16753
f792889a 16754static struct type *
e7c27a73 16755read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16756{
518817b3 16757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16758 const char *name = NULL;
3c8e0968 16759 struct type *this_type, *target_type;
c906108c 16760
94af9270 16761 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16762 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16763 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16764 set_die_type (die, this_type, cu);
3c8e0968
DE
16765 target_type = die_type (die, cu);
16766 if (target_type != this_type)
16767 TYPE_TARGET_TYPE (this_type) = target_type;
16768 else
16769 {
16770 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16771 spec and cause infinite loops in GDB. */
b98664d3 16772 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16773 "- DIE at %s [in module %s]"),
16774 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16775 TYPE_TARGET_TYPE (this_type) = NULL;
16776 }
e4003a34
TV
16777 if (name == NULL)
16778 {
16779 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16780 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16781 Handle these by just returning the target type, rather than
16782 constructing an anonymous typedef type and trying to handle this
16783 elsewhere. */
16784 set_die_type (die, target_type, cu);
16785 return target_type;
16786 }
f792889a 16787 return this_type;
c906108c
SS
16788}
16789
9b790ce7
UW
16790/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16791 (which may be different from NAME) to the architecture back-end to allow
16792 it to guess the correct format if necessary. */
16793
16794static struct type *
16795dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16796 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16797{
16798 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16799 const struct floatformat **format;
16800 struct type *type;
16801
16802 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16803 if (format)
103a685e 16804 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16805 else
77b7c781 16806 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16807
16808 return type;
16809}
16810
eb77c9df
AB
16811/* Allocate an integer type of size BITS and name NAME. */
16812
16813static struct type *
16814dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16815 int bits, int unsigned_p, const char *name)
16816{
16817 struct type *type;
16818
16819 /* Versions of Intel's C Compiler generate an integer type called "void"
16820 instead of using DW_TAG_unspecified_type. This has been seen on
16821 at least versions 14, 17, and 18. */
35ee2dc2
AB
16822 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16823 && strcmp (name, "void") == 0)
eb77c9df
AB
16824 type = objfile_type (objfile)->builtin_void;
16825 else
16826 type = init_integer_type (objfile, bits, unsigned_p, name);
16827
16828 return type;
16829}
16830
8bdc1658
AB
16831/* Initialise and return a floating point type of size BITS suitable for
16832 use as a component of a complex number. The NAME_HINT is passed through
16833 when initialising the floating point type and is the name of the complex
16834 type.
16835
16836 As DWARF doesn't currently provide an explicit name for the components
16837 of a complex number, but it can be helpful to have these components
16838 named, we try to select a suitable name based on the size of the
16839 component. */
16840static struct type *
16841dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16842 struct objfile *objfile,
103a685e
TT
16843 int bits, const char *name_hint,
16844 enum bfd_endian byte_order)
8bdc1658
AB
16845{
16846 gdbarch *gdbarch = get_objfile_arch (objfile);
16847 struct type *tt = nullptr;
16848
35add35e
AB
16849 /* Try to find a suitable floating point builtin type of size BITS.
16850 We're going to use the name of this type as the name for the complex
16851 target type that we are about to create. */
1db455a7 16852 switch (cu->language)
8bdc1658 16853 {
1db455a7
AB
16854 case language_fortran:
16855 switch (bits)
16856 {
16857 case 32:
16858 tt = builtin_f_type (gdbarch)->builtin_real;
16859 break;
16860 case 64:
16861 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16862 break;
16863 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16864 case 128:
16865 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16866 break;
16867 }
8bdc1658 16868 break;
1db455a7
AB
16869 default:
16870 switch (bits)
16871 {
16872 case 32:
16873 tt = builtin_type (gdbarch)->builtin_float;
16874 break;
16875 case 64:
16876 tt = builtin_type (gdbarch)->builtin_double;
16877 break;
16878 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16879 case 128:
16880 tt = builtin_type (gdbarch)->builtin_long_double;
16881 break;
16882 }
8bdc1658
AB
16883 break;
16884 }
16885
35add35e
AB
16886 /* If the type we found doesn't match the size we were looking for, then
16887 pretend we didn't find a type at all, the complex target type we
16888 create will then be nameless. */
a12e5744 16889 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16890 tt = nullptr;
16891
8bdc1658 16892 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16893 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16894}
16895
c906108c
SS
16896/* Find a representation of a given base type and install
16897 it in the TYPE field of the die. */
16898
f792889a 16899static struct type *
e7c27a73 16900read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16901{
518817b3 16902 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16903 struct type *type;
16904 struct attribute *attr;
19f392bc 16905 int encoding = 0, bits = 0;
15d034d0 16906 const char *name;
34877895 16907 gdbarch *arch;
c906108c 16908
e142c38c 16909 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16910 if (attr != nullptr)
34877895 16911 encoding = DW_UNSND (attr);
e142c38c 16912 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16913 if (attr != nullptr)
34877895 16914 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16915 name = dwarf2_name (die, cu);
6ccb9162 16916 if (!name)
34877895 16917 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16918
16919 arch = get_objfile_arch (objfile);
16920 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16921
34877895
PJ
16922 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16923 if (attr)
103a685e
TT
16924 {
16925 int endianity = DW_UNSND (attr);
16926
16927 switch (endianity)
16928 {
16929 case DW_END_big:
16930 byte_order = BFD_ENDIAN_BIG;
16931 break;
16932 case DW_END_little:
16933 byte_order = BFD_ENDIAN_LITTLE;
16934 break;
16935 default:
16936 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16937 break;
16938 }
16939 }
6ccb9162
UW
16940
16941 switch (encoding)
c906108c 16942 {
6ccb9162
UW
16943 case DW_ATE_address:
16944 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16945 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16946 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16947 break;
16948 case DW_ATE_boolean:
19f392bc 16949 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16950 break;
16951 case DW_ATE_complex_float:
103a685e
TT
16952 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16953 byte_order);
93689ce9
TT
16954 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16955 {
16956 if (name == nullptr)
16957 {
16958 struct obstack *obstack
16959 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16960 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16961 nullptr);
16962 }
16963 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16964 }
16965 else
16966 type = init_complex_type (name, type);
6ccb9162
UW
16967 break;
16968 case DW_ATE_decimal_float:
19f392bc 16969 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16970 break;
16971 case DW_ATE_float:
103a685e 16972 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16973 break;
16974 case DW_ATE_signed:
eb77c9df 16975 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16976 break;
16977 case DW_ATE_unsigned:
3b2b8fea
TT
16978 if (cu->language == language_fortran
16979 && name
61012eef 16980 && startswith (name, "character("))
19f392bc
UW
16981 type = init_character_type (objfile, bits, 1, name);
16982 else
eb77c9df 16983 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16984 break;
16985 case DW_ATE_signed_char:
6e70227d 16986 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16987 || cu->language == language_pascal
16988 || cu->language == language_fortran)
19f392bc
UW
16989 type = init_character_type (objfile, bits, 0, name);
16990 else
eb77c9df 16991 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16992 break;
16993 case DW_ATE_unsigned_char:
868a0084 16994 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16995 || cu->language == language_pascal
c44af4eb
TT
16996 || cu->language == language_fortran
16997 || cu->language == language_rust)
19f392bc
UW
16998 type = init_character_type (objfile, bits, 1, name);
16999 else
eb77c9df 17000 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17001 break;
75079b2b 17002 case DW_ATE_UTF:
53e710ac 17003 {
53e710ac
PA
17004 if (bits == 16)
17005 type = builtin_type (arch)->builtin_char16;
17006 else if (bits == 32)
17007 type = builtin_type (arch)->builtin_char32;
17008 else
17009 {
b98664d3 17010 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17011 bits);
eb77c9df 17012 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17013 }
17014 return set_die_type (die, type, cu);
17015 }
75079b2b
TT
17016 break;
17017
6ccb9162 17018 default:
b98664d3 17019 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17020 dwarf_type_encoding_name (encoding));
77b7c781 17021 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17022 break;
c906108c 17023 }
6ccb9162 17024
0114d602 17025 if (name && strcmp (name, "char") == 0)
876cecd0 17026 TYPE_NOSIGN (type) = 1;
0114d602 17027
2b4424c3
TT
17028 maybe_set_alignment (cu, die, type);
17029
103a685e 17030 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17031
f792889a 17032 return set_die_type (die, type, cu);
c906108c
SS
17033}
17034
80180f79
SA
17035/* Parse dwarf attribute if it's a block, reference or constant and put the
17036 resulting value of the attribute into struct bound_prop.
17037 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17038
17039static int
17040attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17041 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17042 struct type *default_type)
80180f79
SA
17043{
17044 struct dwarf2_property_baton *baton;
518817b3
SM
17045 struct obstack *obstack
17046 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17047
9a49df9d
AB
17048 gdb_assert (default_type != NULL);
17049
80180f79
SA
17050 if (attr == NULL || prop == NULL)
17051 return 0;
17052
4fc6c0d5 17053 if (attr->form_is_block ())
80180f79 17054 {
8d749320 17055 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17056 baton->property_type = default_type;
80180f79
SA
17057 baton->locexpr.per_cu = cu->per_cu;
17058 baton->locexpr.size = DW_BLOCK (attr)->size;
17059 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17060 switch (attr->name)
17061 {
17062 case DW_AT_string_length:
17063 baton->locexpr.is_reference = true;
17064 break;
17065 default:
17066 baton->locexpr.is_reference = false;
17067 break;
17068 }
80180f79
SA
17069 prop->data.baton = baton;
17070 prop->kind = PROP_LOCEXPR;
17071 gdb_assert (prop->data.baton != NULL);
17072 }
cd6c91b4 17073 else if (attr->form_is_ref ())
80180f79
SA
17074 {
17075 struct dwarf2_cu *target_cu = cu;
17076 struct die_info *target_die;
17077 struct attribute *target_attr;
17078
17079 target_die = follow_die_ref (die, attr, &target_cu);
17080 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17081 if (target_attr == NULL)
17082 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17083 target_cu);
80180f79
SA
17084 if (target_attr == NULL)
17085 return 0;
17086
df25ebbd 17087 switch (target_attr->name)
80180f79 17088 {
df25ebbd 17089 case DW_AT_location:
cd6c91b4 17090 if (target_attr->form_is_section_offset ())
df25ebbd 17091 {
8d749320 17092 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17093 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17094 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17095 prop->data.baton = baton;
17096 prop->kind = PROP_LOCLIST;
17097 gdb_assert (prop->data.baton != NULL);
17098 }
4fc6c0d5 17099 else if (target_attr->form_is_block ())
df25ebbd 17100 {
8d749320 17101 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17102 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17103 baton->locexpr.per_cu = cu->per_cu;
17104 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17105 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17106 baton->locexpr.is_reference = true;
df25ebbd
JB
17107 prop->data.baton = baton;
17108 prop->kind = PROP_LOCEXPR;
17109 gdb_assert (prop->data.baton != NULL);
17110 }
17111 else
17112 {
17113 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17114 "dynamic property");
17115 return 0;
17116 }
17117 break;
17118 case DW_AT_data_member_location:
17119 {
17120 LONGEST offset;
17121
17122 if (!handle_data_member_location (target_die, target_cu,
17123 &offset))
17124 return 0;
17125
8d749320 17126 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17127 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17128 target_cu);
df25ebbd
JB
17129 baton->offset_info.offset = offset;
17130 baton->offset_info.type = die_type (target_die, target_cu);
17131 prop->data.baton = baton;
17132 prop->kind = PROP_ADDR_OFFSET;
17133 break;
17134 }
80180f79
SA
17135 }
17136 }
cd6c91b4 17137 else if (attr->form_is_constant ())
80180f79 17138 {
0826b30a 17139 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17140 prop->kind = PROP_CONST;
17141 }
17142 else
17143 {
17144 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17145 dwarf2_name (die, cu));
17146 return 0;
17147 }
17148
17149 return 1;
17150}
17151
09ba997f 17152/* See read.h. */
9a49df9d 17153
09ba997f
TT
17154struct type *
17155dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17156{
09ba997f 17157 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17158 struct type *int_type;
17159
17160 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17161#define TRY_TYPE(F) \
17162 int_type = (unsigned_p \
17163 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17164 : objfile_type (objfile)->builtin_ ## F); \
17165 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17166 return int_type
17167
17168 TRY_TYPE (char);
17169 TRY_TYPE (short);
17170 TRY_TYPE (int);
17171 TRY_TYPE (long);
17172 TRY_TYPE (long_long);
17173
17174#undef TRY_TYPE
17175
17176 gdb_assert_not_reached ("unable to find suitable integer type");
17177}
17178
09ba997f 17179/* See read.h. */
11a8b164 17180
09ba997f
TT
17181struct type *
17182dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17183{
09ba997f
TT
17184 int addr_size = this->addr_size ();
17185 return int_type (addr_size, unsigned_p);
11a8b164
AB
17186}
17187
b86352cf
AB
17188/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17189 present (which is valid) then compute the default type based on the
17190 compilation units address size. */
17191
17192static struct type *
17193read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17194{
17195 struct type *index_type = die_type (die, cu);
17196
17197 /* Dwarf-2 specifications explicitly allows to create subrange types
17198 without specifying a base type.
17199 In that case, the base type must be set to the type of
17200 the lower bound, upper bound or count, in that order, if any of these
17201 three attributes references an object that has a type.
17202 If no base type is found, the Dwarf-2 specifications say that
17203 a signed integer type of size equal to the size of an address should
17204 be used.
17205 For the following C code: `extern char gdb_int [];'
17206 GCC produces an empty range DIE.
17207 FIXME: muller/2010-05-28: Possible references to object for low bound,
17208 high bound or count are not yet handled by this code. */
17209 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17210 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17211
17212 return index_type;
17213}
17214
a02abb62
JB
17215/* Read the given DW_AT_subrange DIE. */
17216
f792889a 17217static struct type *
a02abb62
JB
17218read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17219{
4c9ad8c2 17220 struct type *base_type, *orig_base_type;
a02abb62
JB
17221 struct type *range_type;
17222 struct attribute *attr;
729efb13 17223 struct dynamic_prop low, high;
4fae6e18 17224 int low_default_is_valid;
c451ebe5 17225 int high_bound_is_count = 0;
15d034d0 17226 const char *name;
d359392f 17227 ULONGEST negative_mask;
e77813c8 17228
b86352cf
AB
17229 orig_base_type = read_subrange_index_type (die, cu);
17230
4c9ad8c2
TT
17231 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17232 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17233 creating the range type, but we use the result of check_typedef
17234 when examining properties of the type. */
17235 base_type = check_typedef (orig_base_type);
a02abb62 17236
7e314c57
JK
17237 /* The die_type call above may have already set the type for this DIE. */
17238 range_type = get_die_type (die, cu);
17239 if (range_type)
17240 return range_type;
17241
729efb13
SA
17242 low.kind = PROP_CONST;
17243 high.kind = PROP_CONST;
17244 high.data.const_val = 0;
17245
4fae6e18
JK
17246 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17247 omitting DW_AT_lower_bound. */
17248 switch (cu->language)
6e70227d 17249 {
4fae6e18
JK
17250 case language_c:
17251 case language_cplus:
729efb13 17252 low.data.const_val = 0;
4fae6e18
JK
17253 low_default_is_valid = 1;
17254 break;
17255 case language_fortran:
729efb13 17256 low.data.const_val = 1;
4fae6e18
JK
17257 low_default_is_valid = 1;
17258 break;
17259 case language_d:
4fae6e18 17260 case language_objc:
c44af4eb 17261 case language_rust:
729efb13 17262 low.data.const_val = 0;
4fae6e18
JK
17263 low_default_is_valid = (cu->header.version >= 4);
17264 break;
17265 case language_ada:
17266 case language_m2:
17267 case language_pascal:
729efb13 17268 low.data.const_val = 1;
4fae6e18
JK
17269 low_default_is_valid = (cu->header.version >= 4);
17270 break;
17271 default:
729efb13 17272 low.data.const_val = 0;
4fae6e18
JK
17273 low_default_is_valid = 0;
17274 break;
a02abb62
JB
17275 }
17276
e142c38c 17277 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17278 if (attr != nullptr)
9a49df9d 17279 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17280 else if (!low_default_is_valid)
b98664d3 17281 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17282 "- DIE at %s [in module %s]"),
17283 sect_offset_str (die->sect_off),
518817b3 17284 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17285
506f5c41
TV
17286 struct attribute *attr_ub, *attr_count;
17287 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17288 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17289 {
506f5c41 17290 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17291 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17292 {
c451ebe5
SA
17293 /* If bounds are constant do the final calculation here. */
17294 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17295 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17296 else
17297 high_bound_is_count = 1;
c2ff108b 17298 }
506f5c41
TV
17299 else
17300 {
17301 if (attr_ub != NULL)
17302 complaint (_("Unresolved DW_AT_upper_bound "
17303 "- DIE at %s [in module %s]"),
17304 sect_offset_str (die->sect_off),
17305 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17306 if (attr_count != NULL)
17307 complaint (_("Unresolved DW_AT_count "
17308 "- DIE at %s [in module %s]"),
17309 sect_offset_str (die->sect_off),
17310 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17311 }
e77813c8 17312 }
a02abb62 17313
4e962e74
TT
17314 LONGEST bias = 0;
17315 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17316 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17317 bias = bias_attr->constant_value (0);
4e962e74 17318
dbb9c2b1
JB
17319 /* Normally, the DWARF producers are expected to use a signed
17320 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17321 But this is unfortunately not always the case, as witnessed
17322 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17323 is used instead. To work around that ambiguity, we treat
17324 the bounds as signed, and thus sign-extend their values, when
17325 the base type is signed. */
6e70227d 17326 negative_mask =
d359392f 17327 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17328 if (low.kind == PROP_CONST
17329 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17330 low.data.const_val |= negative_mask;
17331 if (high.kind == PROP_CONST
17332 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17333 high.data.const_val |= negative_mask;
43bbcdc2 17334
5bbd8269
AB
17335 /* Check for bit and byte strides. */
17336 struct dynamic_prop byte_stride_prop;
17337 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17338 if (attr_byte_stride != nullptr)
17339 {
09ba997f 17340 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17341 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17342 prop_type);
17343 }
17344
17345 struct dynamic_prop bit_stride_prop;
17346 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17347 if (attr_bit_stride != nullptr)
17348 {
17349 /* It only makes sense to have either a bit or byte stride. */
17350 if (attr_byte_stride != nullptr)
17351 {
17352 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17353 "- DIE at %s [in module %s]"),
17354 sect_offset_str (die->sect_off),
17355 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17356 attr_bit_stride = nullptr;
17357 }
17358 else
17359 {
09ba997f 17360 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17361 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17362 prop_type);
17363 }
17364 }
17365
17366 if (attr_byte_stride != nullptr
17367 || attr_bit_stride != nullptr)
17368 {
17369 bool byte_stride_p = (attr_byte_stride != nullptr);
17370 struct dynamic_prop *stride
17371 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17372
17373 range_type
17374 = create_range_type_with_stride (NULL, orig_base_type, &low,
17375 &high, bias, stride, byte_stride_p);
17376 }
17377 else
17378 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17379
c451ebe5
SA
17380 if (high_bound_is_count)
17381 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17382
c2ff108b
JK
17383 /* Ada expects an empty array on no boundary attributes. */
17384 if (attr == NULL && cu->language != language_ada)
729efb13 17385 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17386
39cbfefa
DJ
17387 name = dwarf2_name (die, cu);
17388 if (name)
17389 TYPE_NAME (range_type) = name;
6e70227d 17390
e142c38c 17391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17392 if (attr != nullptr)
a02abb62
JB
17393 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17394
2b4424c3
TT
17395 maybe_set_alignment (cu, die, range_type);
17396
7e314c57
JK
17397 set_die_type (die, range_type, cu);
17398
17399 /* set_die_type should be already done. */
b4ba55a1
JB
17400 set_descriptive_type (range_type, die, cu);
17401
7e314c57 17402 return range_type;
a02abb62 17403}
6e70227d 17404
f792889a 17405static struct type *
81a17f79
JB
17406read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17407{
17408 struct type *type;
81a17f79 17409
518817b3
SM
17410 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17411 NULL);
0114d602 17412 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17413
74a2f8ff 17414 /* In Ada, an unspecified type is typically used when the description
85102364 17415 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17416 such a type, we treat it as a stub, and try to resolve it later on,
17417 when needed. */
17418 if (cu->language == language_ada)
17419 TYPE_STUB (type) = 1;
17420
f792889a 17421 return set_die_type (die, type, cu);
81a17f79 17422}
a02abb62 17423
639d11d3
DC
17424/* Read a single die and all its descendents. Set the die's sibling
17425 field to NULL; set other fields in the die correctly, and set all
17426 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17427 location of the info_ptr after reading all of those dies. PARENT
17428 is the parent of the die in question. */
17429
17430static struct die_info *
dee91e82 17431read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17432 const gdb_byte *info_ptr,
17433 const gdb_byte **new_info_ptr,
dee91e82 17434 struct die_info *parent)
639d11d3
DC
17435{
17436 struct die_info *die;
d521ce57 17437 const gdb_byte *cur_ptr;
639d11d3 17438
3e225074 17439 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17440 if (die == NULL)
17441 {
17442 *new_info_ptr = cur_ptr;
17443 return NULL;
17444 }
93311388 17445 store_in_ref_table (die, reader->cu);
639d11d3 17446
3e225074 17447 if (die->has_children)
bf6af496 17448 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17449 else
17450 {
17451 die->child = NULL;
17452 *new_info_ptr = cur_ptr;
17453 }
17454
17455 die->sibling = NULL;
17456 die->parent = parent;
17457 return die;
17458}
17459
17460/* Read a die, all of its descendents, and all of its siblings; set
17461 all of the fields of all of the dies correctly. Arguments are as
17462 in read_die_and_children. */
17463
17464static struct die_info *
bf6af496 17465read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17466 const gdb_byte *info_ptr,
17467 const gdb_byte **new_info_ptr,
bf6af496 17468 struct die_info *parent)
639d11d3
DC
17469{
17470 struct die_info *first_die, *last_sibling;
d521ce57 17471 const gdb_byte *cur_ptr;
639d11d3 17472
c906108c 17473 cur_ptr = info_ptr;
639d11d3
DC
17474 first_die = last_sibling = NULL;
17475
17476 while (1)
c906108c 17477 {
639d11d3 17478 struct die_info *die
dee91e82 17479 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17480
1d325ec1 17481 if (die == NULL)
c906108c 17482 {
639d11d3
DC
17483 *new_info_ptr = cur_ptr;
17484 return first_die;
c906108c 17485 }
1d325ec1
DJ
17486
17487 if (!first_die)
17488 first_die = die;
c906108c 17489 else
1d325ec1
DJ
17490 last_sibling->sibling = die;
17491
17492 last_sibling = die;
c906108c 17493 }
c906108c
SS
17494}
17495
bf6af496
DE
17496/* Read a die, all of its descendents, and all of its siblings; set
17497 all of the fields of all of the dies correctly. Arguments are as
17498 in read_die_and_children.
17499 This the main entry point for reading a DIE and all its children. */
17500
17501static struct die_info *
17502read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17503 const gdb_byte *info_ptr,
17504 const gdb_byte **new_info_ptr,
bf6af496
DE
17505 struct die_info *parent)
17506{
17507 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17508 new_info_ptr, parent);
17509
b4f54984 17510 if (dwarf_die_debug)
bf6af496
DE
17511 {
17512 fprintf_unfiltered (gdb_stdlog,
17513 "Read die from %s@0x%x of %s:\n",
96b79293 17514 reader->die_section->get_name (),
bf6af496
DE
17515 (unsigned) (info_ptr - reader->die_section->buffer),
17516 bfd_get_filename (reader->abfd));
b4f54984 17517 dump_die (die, dwarf_die_debug);
bf6af496
DE
17518 }
17519
17520 return die;
17521}
17522
3019eac3
DE
17523/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17524 attributes.
17525 The caller is responsible for filling in the extra attributes
17526 and updating (*DIEP)->num_attrs.
17527 Set DIEP to point to a newly allocated die with its information,
3e225074 17528 except for its child, sibling, and parent fields. */
93311388 17529
d521ce57 17530static const gdb_byte *
3019eac3 17531read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17532 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17533 int num_extra_attrs)
93311388 17534{
b64f50a1 17535 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17536 struct abbrev_info *abbrev;
17537 struct die_info *die;
17538 struct dwarf2_cu *cu = reader->cu;
17539 bfd *abfd = reader->abfd;
17540
9c541725 17541 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17542 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17543 info_ptr += bytes_read;
17544 if (!abbrev_number)
17545 {
17546 *diep = NULL;
93311388
DE
17547 return info_ptr;
17548 }
17549
685af9cd 17550 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17551 if (!abbrev)
348e048f
DE
17552 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17553 abbrev_number,
17554 bfd_get_filename (abfd));
17555
3019eac3 17556 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17557 die->sect_off = sect_off;
93311388
DE
17558 die->tag = abbrev->tag;
17559 die->abbrev = abbrev_number;
3e225074 17560 die->has_children = abbrev->has_children;
93311388 17561
3019eac3
DE
17562 /* Make the result usable.
17563 The caller needs to update num_attrs after adding the extra
17564 attributes. */
93311388
DE
17565 die->num_attrs = abbrev->num_attrs;
17566
18a8505e 17567 std::vector<int> indexes_that_need_reprocess;
93311388 17568 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17569 {
17570 bool need_reprocess;
17571 info_ptr =
17572 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17573 info_ptr, &need_reprocess);
17574 if (need_reprocess)
17575 indexes_that_need_reprocess.push_back (i);
17576 }
17577
052c8bb8 17578 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17579 if (attr != nullptr)
17580 cu->str_offsets_base = DW_UNSND (attr);
93311388 17581
41144253 17582 attr = die->attr (DW_AT_loclists_base);
17583 if (attr != nullptr)
17584 cu->loclist_base = DW_UNSND (attr);
17585
a39fdb41 17586 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17587 if (maybe_addr_base.has_value ())
17588 cu->addr_base = *maybe_addr_base;
17589 for (int index : indexes_that_need_reprocess)
17590 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17591 *diep = die;
93311388
DE
17592 return info_ptr;
17593}
17594
3019eac3
DE
17595/* Read a die and all its attributes.
17596 Set DIEP to point to a newly allocated die with its information,
3e225074 17597 except for its child, sibling, and parent fields. */
3019eac3 17598
d521ce57 17599static const gdb_byte *
3019eac3 17600read_full_die (const struct die_reader_specs *reader,
3e225074 17601 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17602{
d521ce57 17603 const gdb_byte *result;
bf6af496 17604
3e225074 17605 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17606
b4f54984 17607 if (dwarf_die_debug)
bf6af496
DE
17608 {
17609 fprintf_unfiltered (gdb_stdlog,
17610 "Read die from %s@0x%x of %s:\n",
96b79293 17611 reader->die_section->get_name (),
bf6af496
DE
17612 (unsigned) (info_ptr - reader->die_section->buffer),
17613 bfd_get_filename (reader->abfd));
b4f54984 17614 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17615 }
17616
17617 return result;
3019eac3 17618}
433df2d4 17619\f
c906108c 17620
72bf9492
DJ
17621/* Returns nonzero if TAG represents a type that we might generate a partial
17622 symbol for. */
17623
17624static int
17625is_type_tag_for_partial (int tag)
17626{
17627 switch (tag)
17628 {
17629#if 0
17630 /* Some types that would be reasonable to generate partial symbols for,
17631 that we don't at present. */
17632 case DW_TAG_array_type:
17633 case DW_TAG_file_type:
17634 case DW_TAG_ptr_to_member_type:
17635 case DW_TAG_set_type:
17636 case DW_TAG_string_type:
17637 case DW_TAG_subroutine_type:
17638#endif
17639 case DW_TAG_base_type:
17640 case DW_TAG_class_type:
680b30c7 17641 case DW_TAG_interface_type:
72bf9492
DJ
17642 case DW_TAG_enumeration_type:
17643 case DW_TAG_structure_type:
17644 case DW_TAG_subrange_type:
17645 case DW_TAG_typedef:
17646 case DW_TAG_union_type:
17647 return 1;
17648 default:
17649 return 0;
17650 }
17651}
17652
17653/* Load all DIEs that are interesting for partial symbols into memory. */
17654
17655static struct partial_die_info *
dee91e82 17656load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17657 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17658{
dee91e82 17659 struct dwarf2_cu *cu = reader->cu;
518817b3 17660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17661 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17662 unsigned int bytes_read;
5afb4e99 17663 unsigned int load_all = 0;
72bf9492
DJ
17664 int nesting_level = 1;
17665
17666 parent_die = NULL;
17667 last_die = NULL;
17668
7adf1e79
DE
17669 gdb_assert (cu->per_cu != NULL);
17670 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17671 load_all = 1;
17672
72bf9492
DJ
17673 cu->partial_dies
17674 = htab_create_alloc_ex (cu->header.length / 12,
17675 partial_die_hash,
17676 partial_die_eq,
17677 NULL,
17678 &cu->comp_unit_obstack,
17679 hashtab_obstack_allocate,
17680 dummy_obstack_deallocate);
17681
72bf9492
DJ
17682 while (1)
17683 {
685af9cd 17684 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17685
17686 /* A NULL abbrev means the end of a series of children. */
17687 if (abbrev == NULL)
17688 {
17689 if (--nesting_level == 0)
cd9983dd
YQ
17690 return first_die;
17691
72bf9492
DJ
17692 info_ptr += bytes_read;
17693 last_die = parent_die;
17694 parent_die = parent_die->die_parent;
17695 continue;
17696 }
17697
98bfdba5
PA
17698 /* Check for template arguments. We never save these; if
17699 they're seen, we just mark the parent, and go on our way. */
17700 if (parent_die != NULL
17701 && cu->language == language_cplus
17702 && (abbrev->tag == DW_TAG_template_type_param
17703 || abbrev->tag == DW_TAG_template_value_param))
17704 {
17705 parent_die->has_template_arguments = 1;
17706
17707 if (!load_all)
17708 {
17709 /* We don't need a partial DIE for the template argument. */
dee91e82 17710 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17711 continue;
17712 }
17713 }
17714
0d99eb77 17715 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17716 Skip their other children. */
17717 if (!load_all
17718 && cu->language == language_cplus
17719 && parent_die != NULL
17720 && parent_die->tag == DW_TAG_subprogram)
17721 {
dee91e82 17722 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17723 continue;
17724 }
17725
5afb4e99
DJ
17726 /* Check whether this DIE is interesting enough to save. Normally
17727 we would not be interested in members here, but there may be
17728 later variables referencing them via DW_AT_specification (for
17729 static members). */
17730 if (!load_all
17731 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17732 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17733 && abbrev->tag != DW_TAG_enumerator
17734 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17735 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17736 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17737 && abbrev->tag != DW_TAG_variable
5afb4e99 17738 && abbrev->tag != DW_TAG_namespace
f55ee35c 17739 && abbrev->tag != DW_TAG_module
95554aad 17740 && abbrev->tag != DW_TAG_member
74921315
KS
17741 && abbrev->tag != DW_TAG_imported_unit
17742 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17743 {
17744 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17745 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17746 continue;
17747 }
17748
6f06d47b
YQ
17749 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17750 abbrev);
cd9983dd 17751
48fbe735 17752 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17753
17754 /* This two-pass algorithm for processing partial symbols has a
17755 high cost in cache pressure. Thus, handle some simple cases
17756 here which cover the majority of C partial symbols. DIEs
17757 which neither have specification tags in them, nor could have
17758 specification tags elsewhere pointing at them, can simply be
17759 processed and discarded.
17760
17761 This segment is also optional; scan_partial_symbols and
17762 add_partial_symbol will handle these DIEs if we chain
17763 them in normally. When compilers which do not emit large
17764 quantities of duplicate debug information are more common,
17765 this code can probably be removed. */
17766
17767 /* Any complete simple types at the top level (pretty much all
17768 of them, for a language without namespaces), can be processed
17769 directly. */
17770 if (parent_die == NULL
cd9983dd
YQ
17771 && pdi.has_specification == 0
17772 && pdi.is_declaration == 0
17773 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17774 || pdi.tag == DW_TAG_base_type
17775 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17776 {
cd9983dd 17777 if (building_psymtab && pdi.name != NULL)
31edb802 17778 add_psymbol_to_list (pdi.name, false,
79748972 17779 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17780 psymbol_placement::STATIC,
1762568f 17781 0, cu->language, objfile);
cd9983dd 17782 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17783 continue;
17784 }
17785
d8228535
JK
17786 /* The exception for DW_TAG_typedef with has_children above is
17787 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17788 type_name_or_error will error on such types later.
d8228535
JK
17789
17790 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17791 it could not find the child DIEs referenced later, this is checked
17792 above. In correct DWARF DW_TAG_typedef should have no children. */
17793
cd9983dd 17794 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17795 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17796 "- DIE at %s [in module %s]"),
cd9983dd 17797 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17798
72bf9492
DJ
17799 /* If we're at the second level, and we're an enumerator, and
17800 our parent has no specification (meaning possibly lives in a
17801 namespace elsewhere), then we can add the partial symbol now
17802 instead of queueing it. */
cd9983dd 17803 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17804 && parent_die != NULL
17805 && parent_die->die_parent == NULL
17806 && parent_die->tag == DW_TAG_enumeration_type
17807 && parent_die->has_specification == 0)
17808 {
cd9983dd 17809 if (pdi.name == NULL)
b98664d3 17810 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17811 else if (building_psymtab)
31edb802 17812 add_psymbol_to_list (pdi.name, false,
79748972 17813 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17814 cu->language == language_cplus
75aedd27
TT
17815 ? psymbol_placement::GLOBAL
17816 : psymbol_placement::STATIC,
1762568f 17817 0, cu->language, objfile);
72bf9492 17818
cd9983dd 17819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17820 continue;
17821 }
17822
cd9983dd 17823 struct partial_die_info *part_die
6f06d47b 17824 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17825
72bf9492
DJ
17826 /* We'll save this DIE so link it in. */
17827 part_die->die_parent = parent_die;
17828 part_die->die_sibling = NULL;
17829 part_die->die_child = NULL;
17830
17831 if (last_die && last_die == parent_die)
17832 last_die->die_child = part_die;
17833 else if (last_die)
17834 last_die->die_sibling = part_die;
17835
17836 last_die = part_die;
17837
17838 if (first_die == NULL)
17839 first_die = part_die;
17840
17841 /* Maybe add the DIE to the hash table. Not all DIEs that we
17842 find interesting need to be in the hash table, because we
17843 also have the parent/sibling/child chains; only those that we
17844 might refer to by offset later during partial symbol reading.
17845
17846 For now this means things that might have be the target of a
17847 DW_AT_specification, DW_AT_abstract_origin, or
17848 DW_AT_extension. DW_AT_extension will refer only to
17849 namespaces; DW_AT_abstract_origin refers to functions (and
17850 many things under the function DIE, but we do not recurse
17851 into function DIEs during partial symbol reading) and
17852 possibly variables as well; DW_AT_specification refers to
17853 declarations. Declarations ought to have the DW_AT_declaration
17854 flag. It happens that GCC forgets to put it in sometimes, but
17855 only for functions, not for types.
17856
17857 Adding more things than necessary to the hash table is harmless
17858 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17859 wasted time in find_partial_die, when we reread the compilation
17860 unit with load_all_dies set. */
72bf9492 17861
5afb4e99 17862 if (load_all
72929c62 17863 || abbrev->tag == DW_TAG_constant
5afb4e99 17864 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17865 || abbrev->tag == DW_TAG_variable
17866 || abbrev->tag == DW_TAG_namespace
17867 || part_die->is_declaration)
17868 {
17869 void **slot;
17870
17871 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17872 to_underlying (part_die->sect_off),
17873 INSERT);
72bf9492
DJ
17874 *slot = part_die;
17875 }
17876
72bf9492 17877 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17878 we have no reason to follow the children of structures; for other
98bfdba5
PA
17879 languages we have to, so that we can get at method physnames
17880 to infer fully qualified class names, for DW_AT_specification,
17881 and for C++ template arguments. For C++, we also look one level
17882 inside functions to find template arguments (if the name of the
17883 function does not already contain the template arguments).
bc30ff58 17884
0a4b0913
AB
17885 For Ada and Fortran, we need to scan the children of subprograms
17886 and lexical blocks as well because these languages allow the
17887 definition of nested entities that could be interesting for the
17888 debugger, such as nested subprograms for instance. */
72bf9492 17889 if (last_die->has_children
5afb4e99
DJ
17890 && (load_all
17891 || last_die->tag == DW_TAG_namespace
f55ee35c 17892 || last_die->tag == DW_TAG_module
72bf9492 17893 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17894 || (cu->language == language_cplus
17895 && last_die->tag == DW_TAG_subprogram
17896 && (last_die->name == NULL
17897 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17898 || (cu->language != language_c
17899 && (last_die->tag == DW_TAG_class_type
680b30c7 17900 || last_die->tag == DW_TAG_interface_type
72bf9492 17901 || last_die->tag == DW_TAG_structure_type
bc30ff58 17902 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17903 || ((cu->language == language_ada
17904 || cu->language == language_fortran)
bc30ff58
JB
17905 && (last_die->tag == DW_TAG_subprogram
17906 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17907 {
17908 nesting_level++;
17909 parent_die = last_die;
17910 continue;
17911 }
17912
17913 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17914 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17915
17916 /* Back to the top, do it again. */
17917 }
17918}
17919
6f06d47b
YQ
17920partial_die_info::partial_die_info (sect_offset sect_off_,
17921 struct abbrev_info *abbrev)
17922 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17923{
17924}
17925
35cc7ed7
YQ
17926/* Read a minimal amount of information into the minimal die structure.
17927 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17928
48fbe735
YQ
17929const gdb_byte *
17930partial_die_info::read (const struct die_reader_specs *reader,
17931 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17932{
dee91e82 17933 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17934 struct dwarf2_per_objfile *dwarf2_per_objfile
17935 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17936 unsigned int i;
c5aa993b 17937 int has_low_pc_attr = 0;
c906108c 17938 int has_high_pc_attr = 0;
91da1414 17939 int high_pc_relative = 0;
c906108c 17940
fd0a254f 17941 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17942 {
e7da7f8f 17943 attribute attr;
18a8505e 17944 bool need_reprocess;
e7da7f8f 17945 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
17946 info_ptr, &need_reprocess);
17947 /* String and address offsets that need to do the reprocessing have
17948 already been read at this point, so there is no need to wait until
17949 the loop terminates to do the reprocessing. */
17950 if (need_reprocess)
e7da7f8f 17951 read_attribute_reprocess (reader, &attr);
c906108c 17952 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17953 partial symbol table. */
c906108c
SS
17954 switch (attr.name)
17955 {
17956 case DW_AT_name:
48fbe735 17957 switch (tag)
71c25dea
TT
17958 {
17959 case DW_TAG_compile_unit:
95554aad 17960 case DW_TAG_partial_unit:
348e048f 17961 case DW_TAG_type_unit:
71c25dea
TT
17962 /* Compilation units have a DW_AT_name that is a filename, not
17963 a source language identifier. */
17964 case DW_TAG_enumeration_type:
17965 case DW_TAG_enumerator:
17966 /* These tags always have simple identifiers already; no need
17967 to canonicalize them. */
48fbe735 17968 name = DW_STRING (&attr);
71c25dea
TT
17969 break;
17970 default:
48fbe735
YQ
17971 {
17972 struct objfile *objfile = dwarf2_per_objfile->objfile;
17973
17974 name
be1e3d3e 17975 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 17976 }
71c25dea
TT
17977 break;
17978 }
c906108c 17979 break;
31ef98ae 17980 case DW_AT_linkage_name:
c906108c 17981 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17982 /* Note that both forms of linkage name might appear. We
17983 assume they will be the same, and we only store the last
17984 one we see. */
48fbe735 17985 linkage_name = DW_STRING (&attr);
c906108c
SS
17986 break;
17987 case DW_AT_low_pc:
17988 has_low_pc_attr = 1;
cd6c91b4 17989 lowpc = attr.value_as_address ();
c906108c
SS
17990 break;
17991 case DW_AT_high_pc:
17992 has_high_pc_attr = 1;
cd6c91b4
TT
17993 highpc = attr.value_as_address ();
17994 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17995 high_pc_relative = 1;
c906108c
SS
17996 break;
17997 case DW_AT_location:
0963b4bd 17998 /* Support the .debug_loc offsets. */
4fc6c0d5 17999 if (attr.form_is_block ())
8e19ed76 18000 {
48fbe735 18001 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18002 }
cd6c91b4 18003 else if (attr.form_is_section_offset ())
8e19ed76 18004 {
4d3c2250 18005 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18006 }
18007 else
18008 {
4d3c2250
KB
18009 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18010 "partial symbol information");
8e19ed76 18011 }
c906108c 18012 break;
c906108c 18013 case DW_AT_external:
48fbe735 18014 is_external = DW_UNSND (&attr);
c906108c
SS
18015 break;
18016 case DW_AT_declaration:
48fbe735 18017 is_declaration = DW_UNSND (&attr);
c906108c
SS
18018 break;
18019 case DW_AT_type:
48fbe735 18020 has_type = 1;
c906108c
SS
18021 break;
18022 case DW_AT_abstract_origin:
18023 case DW_AT_specification:
72bf9492 18024 case DW_AT_extension:
48fbe735 18025 has_specification = 1;
0826b30a 18026 spec_offset = attr.get_ref_die_offset ();
48fbe735 18027 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18028 || cu->per_cu->is_dwz);
c906108c
SS
18029 break;
18030 case DW_AT_sibling:
18031 /* Ignore absolute siblings, they might point outside of
18032 the current compile unit. */
18033 if (attr.form == DW_FORM_ref_addr)
b98664d3 18034 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18035 else
b9502d3f 18036 {
48fbe735 18037 const gdb_byte *buffer = reader->buffer;
0826b30a 18038 sect_offset off = attr.get_ref_die_offset ();
9c541725 18039 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18040
18041 if (sibling_ptr < info_ptr)
b98664d3 18042 complaint (_("DW_AT_sibling points backwards"));
22869d73 18043 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18044 reader->die_section->overflow_complaint ();
b9502d3f 18045 else
48fbe735 18046 sibling = sibling_ptr;
b9502d3f 18047 }
c906108c 18048 break;
fa4028e9 18049 case DW_AT_byte_size:
48fbe735 18050 has_byte_size = 1;
fa4028e9 18051 break;
ff908ebf 18052 case DW_AT_const_value:
48fbe735 18053 has_const_value = 1;
ff908ebf 18054 break;
68511cec
CES
18055 case DW_AT_calling_convention:
18056 /* DWARF doesn't provide a way to identify a program's source-level
18057 entry point. DW_AT_calling_convention attributes are only meant
18058 to describe functions' calling conventions.
18059
18060 However, because it's a necessary piece of information in
0c1b455e
TT
18061 Fortran, and before DWARF 4 DW_CC_program was the only
18062 piece of debugging information whose definition refers to
18063 a 'main program' at all, several compilers marked Fortran
18064 main programs with DW_CC_program --- even when those
18065 functions use the standard calling conventions.
18066
18067 Although DWARF now specifies a way to provide this
18068 information, we support this practice for backward
18069 compatibility. */
68511cec 18070 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18071 && cu->language == language_fortran)
48fbe735 18072 main_subprogram = 1;
68511cec 18073 break;
481860b3
GB
18074 case DW_AT_inline:
18075 if (DW_UNSND (&attr) == DW_INL_inlined
18076 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18077 may_be_inlined = 1;
481860b3 18078 break;
95554aad
TT
18079
18080 case DW_AT_import:
48fbe735 18081 if (tag == DW_TAG_imported_unit)
36586728 18082 {
0826b30a 18083 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18084 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18085 || cu->per_cu->is_dwz);
18086 }
95554aad
TT
18087 break;
18088
0c1b455e 18089 case DW_AT_main_subprogram:
48fbe735 18090 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18091 break;
18092
05caa1d2
TT
18093 case DW_AT_ranges:
18094 {
18095 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18096 but that requires a full DIE, so instead we just
18097 reimplement it. */
18098 int need_ranges_base = tag != DW_TAG_compile_unit;
18099 unsigned int ranges_offset = (DW_UNSND (&attr)
18100 + (need_ranges_base
18101 ? cu->ranges_base
18102 : 0));
18103
18104 /* Value of the DW_AT_ranges attribute is the offset in the
18105 .debug_ranges section. */
18106 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18107 nullptr))
18108 has_pc_info = 1;
18109 }
18110 break;
18111
c906108c
SS
18112 default:
18113 break;
18114 }
18115 }
18116
10d06d82
TT
18117 /* For Ada, if both the name and the linkage name appear, we prefer
18118 the latter. This lets "catch exception" work better, regardless
18119 of the order in which the name and linkage name were emitted.
18120 Really, though, this is just a workaround for the fact that gdb
18121 doesn't store both the name and the linkage name. */
18122 if (cu->language == language_ada && linkage_name != nullptr)
18123 name = linkage_name;
18124
91da1414 18125 if (high_pc_relative)
48fbe735 18126 highpc += lowpc;
91da1414 18127
9373cf26
JK
18128 if (has_low_pc_attr && has_high_pc_attr)
18129 {
18130 /* When using the GNU linker, .gnu.linkonce. sections are used to
18131 eliminate duplicate copies of functions and vtables and such.
18132 The linker will arbitrarily choose one and discard the others.
18133 The AT_*_pc values for such functions refer to local labels in
18134 these sections. If the section from that file was discarded, the
18135 labels are not in the output, so the relocs get a value of 0.
18136 If this is a discarded function, mark the pc bounds as invalid,
18137 so that GDB will ignore it. */
48fbe735 18138 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18139 {
48fbe735 18140 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18141 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18142
b98664d3 18143 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18144 "for DIE at %s [in module %s]"),
48fbe735
YQ
18145 paddress (gdbarch, lowpc),
18146 sect_offset_str (sect_off),
9d8780f0 18147 objfile_name (objfile));
9373cf26
JK
18148 }
18149 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18150 else if (lowpc >= highpc)
9373cf26 18151 {
48fbe735 18152 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18153 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18154
b98664d3 18155 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18156 "for DIE at %s [in module %s]"),
48fbe735
YQ
18157 paddress (gdbarch, lowpc),
18158 paddress (gdbarch, highpc),
18159 sect_offset_str (sect_off),
9c541725 18160 objfile_name (objfile));
9373cf26
JK
18161 }
18162 else
48fbe735 18163 has_pc_info = 1;
9373cf26 18164 }
85cbf3d3 18165
c906108c
SS
18166 return info_ptr;
18167}
18168
72bf9492
DJ
18169/* Find a cached partial DIE at OFFSET in CU. */
18170
d590ff25
YQ
18171struct partial_die_info *
18172dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18173{
18174 struct partial_die_info *lookup_die = NULL;
6f06d47b 18175 struct partial_die_info part_die (sect_off);
72bf9492 18176
9a3c8263 18177 lookup_die = ((struct partial_die_info *)
d590ff25 18178 htab_find_with_hash (partial_dies, &part_die,
9c541725 18179 to_underlying (sect_off)));
72bf9492 18180
72bf9492
DJ
18181 return lookup_die;
18182}
18183
348e048f
DE
18184/* Find a partial DIE at OFFSET, which may or may not be in CU,
18185 except in the case of .debug_types DIEs which do not reference
18186 outside their CU (they do however referencing other types via
55f1336d 18187 DW_FORM_ref_sig8). */
72bf9492 18188
122cf0f2 18189static const struct cu_partial_die_info
9c541725 18190find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18191{
518817b3
SM
18192 struct dwarf2_per_objfile *dwarf2_per_objfile
18193 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18194 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18195 struct dwarf2_per_cu_data *per_cu = NULL;
18196 struct partial_die_info *pd = NULL;
72bf9492 18197
36586728 18198 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18199 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18200 {
d590ff25 18201 pd = cu->find_partial_die (sect_off);
5afb4e99 18202 if (pd != NULL)
fb816e8b 18203 return { cu, pd };
0d99eb77
DE
18204 /* We missed recording what we needed.
18205 Load all dies and try again. */
18206 per_cu = cu->per_cu;
5afb4e99 18207 }
0d99eb77
DE
18208 else
18209 {
18210 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18211 if (cu->per_cu->is_debug_types)
0d99eb77 18212 {
9d8780f0
SM
18213 error (_("Dwarf Error: Type Unit at offset %s contains"
18214 " external reference to offset %s [in module %s].\n"),
18215 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18216 bfd_get_filename (objfile->obfd));
18217 }
9c541725 18218 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18219 dwarf2_per_objfile);
72bf9492 18220
0d99eb77
DE
18221 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18222 load_partial_comp_unit (per_cu);
ae038cb0 18223
0d99eb77 18224 per_cu->cu->last_used = 0;
d590ff25 18225 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18226 }
5afb4e99 18227
dee91e82
DE
18228 /* If we didn't find it, and not all dies have been loaded,
18229 load them all and try again. */
18230
5afb4e99
DJ
18231 if (pd == NULL && per_cu->load_all_dies == 0)
18232 {
5afb4e99 18233 per_cu->load_all_dies = 1;
fd820528
DE
18234
18235 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18236 THIS_CU->cu may already be in use. So we can't just free it and
18237 replace its DIEs with the ones we read in. Instead, we leave those
18238 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18239 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18240 set. */
dee91e82 18241 load_partial_comp_unit (per_cu);
5afb4e99 18242
d590ff25 18243 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18244 }
18245
18246 if (pd == NULL)
18247 internal_error (__FILE__, __LINE__,
9d8780f0 18248 _("could not find partial DIE %s "
3e43a32a 18249 "in cache [from module %s]\n"),
9d8780f0 18250 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18251 return { per_cu->cu, pd };
72bf9492
DJ
18252}
18253
abc72ce4
DE
18254/* See if we can figure out if the class lives in a namespace. We do
18255 this by looking for a member function; its demangled name will
18256 contain namespace info, if there is any. */
18257
18258static void
18259guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18260 struct dwarf2_cu *cu)
18261{
18262 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18263 what template types look like, because the demangler
18264 frequently doesn't give the same name as the debug info. We
18265 could fix this by only using the demangled name to get the
18266 prefix (but see comment in read_structure_type). */
18267
18268 struct partial_die_info *real_pdi;
18269 struct partial_die_info *child_pdi;
18270
18271 /* If this DIE (this DIE's specification, if any) has a parent, then
18272 we should not do this. We'll prepend the parent's fully qualified
18273 name when we create the partial symbol. */
18274
18275 real_pdi = struct_pdi;
18276 while (real_pdi->has_specification)
fb816e8b 18277 {
122cf0f2
AB
18278 auto res = find_partial_die (real_pdi->spec_offset,
18279 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18280 real_pdi = res.pdi;
18281 cu = res.cu;
18282 }
abc72ce4
DE
18283
18284 if (real_pdi->die_parent != NULL)
18285 return;
18286
18287 for (child_pdi = struct_pdi->die_child;
18288 child_pdi != NULL;
18289 child_pdi = child_pdi->die_sibling)
18290 {
18291 if (child_pdi->tag == DW_TAG_subprogram
18292 && child_pdi->linkage_name != NULL)
18293 {
43816ebc
TT
18294 gdb::unique_xmalloc_ptr<char> actual_class_name
18295 (language_class_name_from_physname (cu->language_defn,
18296 child_pdi->linkage_name));
abc72ce4
DE
18297 if (actual_class_name != NULL)
18298 {
518817b3 18299 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18300 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18301 }
18302 break;
18303 }
18304 }
18305}
18306
25c11aca
TV
18307/* Return true if a DIE with TAG may have the DW_AT_const_value
18308 attribute. */
18309
18310static bool
18311can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18312{
18313 switch (tag)
18314 {
18315 case DW_TAG_constant:
18316 case DW_TAG_enumerator:
18317 case DW_TAG_formal_parameter:
18318 case DW_TAG_template_value_param:
18319 case DW_TAG_variable:
18320 return true;
18321 }
18322
18323 return false;
18324}
18325
52356b79
YQ
18326void
18327partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18328{
abc72ce4
DE
18329 /* Once we've fixed up a die, there's no point in doing so again.
18330 This also avoids a memory leak if we were to call
18331 guess_partial_die_structure_name multiple times. */
52356b79 18332 if (fixup_called)
abc72ce4
DE
18333 return;
18334
72bf9492
DJ
18335 /* If we found a reference attribute and the DIE has no name, try
18336 to find a name in the referred to DIE. */
18337
52356b79 18338 if (name == NULL && has_specification)
72bf9492
DJ
18339 {
18340 struct partial_die_info *spec_die;
72bf9492 18341
122cf0f2 18342 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18343 spec_die = res.pdi;
18344 cu = res.cu;
72bf9492 18345
52356b79 18346 spec_die->fixup (cu);
72bf9492
DJ
18347
18348 if (spec_die->name)
18349 {
52356b79 18350 name = spec_die->name;
72bf9492
DJ
18351
18352 /* Copy DW_AT_external attribute if it is set. */
18353 if (spec_die->is_external)
52356b79 18354 is_external = spec_die->is_external;
72bf9492
DJ
18355 }
18356 }
18357
25c11aca
TV
18358 if (!has_const_value && has_specification
18359 && can_have_DW_AT_const_value_p (tag))
18360 {
18361 struct partial_die_info *spec_die;
18362
18363 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18364 spec_die = res.pdi;
18365 cu = res.cu;
18366
18367 spec_die->fixup (cu);
18368
18369 if (spec_die->has_const_value)
18370 {
18371 /* Copy DW_AT_const_value attribute if it is set. */
18372 has_const_value = spec_die->has_const_value;
18373 }
18374 }
18375
72bf9492 18376 /* Set default names for some unnamed DIEs. */
72bf9492 18377
52356b79
YQ
18378 if (name == NULL && tag == DW_TAG_namespace)
18379 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18380
abc72ce4
DE
18381 /* If there is no parent die to provide a namespace, and there are
18382 children, see if we can determine the namespace from their linkage
122d1940 18383 name. */
abc72ce4 18384 if (cu->language == language_cplus
fd5866f6 18385 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18386 && die_parent == NULL
18387 && has_children
18388 && (tag == DW_TAG_class_type
18389 || tag == DW_TAG_structure_type
18390 || tag == DW_TAG_union_type))
18391 guess_partial_die_structure_name (this, cu);
abc72ce4 18392
53832f31
TT
18393 /* GCC might emit a nameless struct or union that has a linkage
18394 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18395 if (name == NULL
18396 && (tag == DW_TAG_class_type
18397 || tag == DW_TAG_interface_type
18398 || tag == DW_TAG_structure_type
18399 || tag == DW_TAG_union_type)
18400 && linkage_name != NULL)
53832f31 18401 {
43816ebc
TT
18402 gdb::unique_xmalloc_ptr<char> demangled
18403 (gdb_demangle (linkage_name, DMGL_TYPES));
18404 if (demangled != nullptr)
53832f31 18405 {
96408a79
SA
18406 const char *base;
18407
18408 /* Strip any leading namespaces/classes, keep only the base name.
18409 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18410 base = strrchr (demangled.get (), ':');
18411 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18412 base++;
18413 else
43816ebc 18414 base = demangled.get ();
96408a79 18415
518817b3 18416 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18417 name = objfile->intern (base);
53832f31
TT
18418 }
18419 }
18420
52356b79 18421 fixup_called = 1;
72bf9492
DJ
18422}
18423
41144253 18424/* Read the .debug_loclists header contents from the given SECTION in the
18425 HEADER. */
18426static void
18427read_loclist_header (struct loclist_header *header,
18428 struct dwarf2_section_info *section)
18429{
18430 unsigned int bytes_read;
18431 bfd *abfd = section->get_bfd_owner ();
18432 const gdb_byte *info_ptr = section->buffer;
18433 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18434 info_ptr += bytes_read;
18435 header->version = read_2_bytes (abfd, info_ptr);
18436 info_ptr += 2;
18437 header->addr_size = read_1_byte (abfd, info_ptr);
18438 info_ptr += 1;
18439 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18440 info_ptr += 1;
18441 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18442}
18443
18444/* Return the DW_AT_loclists_base value for the CU. */
18445static ULONGEST
18446lookup_loclist_base (struct dwarf2_cu *cu)
18447{
18448 /* For the .dwo unit, the loclist_base points to the first offset following
18449 the header. The header consists of the following entities-
18450 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18451 bit format)
18452 2. version (2 bytes)
18453 3. address size (1 byte)
18454 4. segment selector size (1 byte)
18455 5. offset entry count (4 bytes)
18456 These sizes are derived as per the DWARFv5 standard. */
18457 if (cu->dwo_unit != nullptr)
18458 {
18459 if (cu->header.initial_length_size == 4)
18460 return LOCLIST_HEADER_SIZE32;
18461 return LOCLIST_HEADER_SIZE64;
18462 }
18463 return cu->loclist_base;
18464}
18465
18466/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18467 array of offsets in the .debug_loclists section. */
18468static CORE_ADDR
18469read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18470{
18471 struct dwarf2_per_objfile *dwarf2_per_objfile
18472 = cu->per_cu->dwarf2_per_objfile;
18473 struct objfile *objfile = dwarf2_per_objfile->objfile;
18474 bfd *abfd = objfile->obfd;
18475 ULONGEST loclist_base = lookup_loclist_base (cu);
18476 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18477
18478 section->read (objfile);
18479 if (section->buffer == NULL)
18480 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18481 "section [in module %s]"), objfile_name (objfile));
18482 struct loclist_header header;
18483 read_loclist_header (&header, section);
18484 if (loclist_index >= header.offset_entry_count)
18485 complaint (_("DW_FORM_loclistx pointing outside of "
18486 ".debug_loclists offset array [in module %s]"),
18487 objfile_name (objfile));
18488 if (loclist_base + loclist_index * cu->header.offset_size
18489 >= section->size)
18490 complaint (_("DW_FORM_loclistx pointing outside of "
18491 ".debug_loclists section [in module %s]"),
18492 objfile_name (objfile));
18493 const gdb_byte *info_ptr
18494 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18495
18496 if (cu->header.offset_size == 4)
18497 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18498 else
18499 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18500}
18501
18a8505e
AT
18502/* Process the attributes that had to be skipped in the first round. These
18503 attributes are the ones that need str_offsets_base or addr_base attributes.
18504 They could not have been processed in the first round, because at the time
18505 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18506static void
18507read_attribute_reprocess (const struct die_reader_specs *reader,
18508 struct attribute *attr)
18a8505e
AT
18509{
18510 struct dwarf2_cu *cu = reader->cu;
18511 switch (attr->form)
18512 {
18513 case DW_FORM_addrx:
18514 case DW_FORM_GNU_addr_index:
18515 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18516 break;
41144253 18517 case DW_FORM_loclistx:
18518 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18519 break;
18a8505e
AT
18520 case DW_FORM_strx:
18521 case DW_FORM_strx1:
18522 case DW_FORM_strx2:
18523 case DW_FORM_strx3:
18524 case DW_FORM_strx4:
18525 case DW_FORM_GNU_str_index:
18526 {
18527 unsigned int str_index = DW_UNSND (attr);
18528 if (reader->dwo_file != NULL)
18529 {
18530 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18531 DW_STRING_IS_CANONICAL (attr) = 0;
18532 }
18533 else
18534 {
18535 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18536 DW_STRING_IS_CANONICAL (attr) = 0;
18537 }
18538 break;
18539 }
18540 default:
18541 gdb_assert_not_reached (_("Unexpected DWARF form."));
18542 }
18543}
18544
a8329558 18545/* Read an attribute value described by an attribute form. */
c906108c 18546
d521ce57 18547static const gdb_byte *
dee91e82
DE
18548read_attribute_value (const struct die_reader_specs *reader,
18549 struct attribute *attr, unsigned form,
18a8505e
AT
18550 LONGEST implicit_const, const gdb_byte *info_ptr,
18551 bool *need_reprocess)
c906108c 18552{
dee91e82 18553 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18554 struct dwarf2_per_objfile *dwarf2_per_objfile
18555 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18556 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18558 bfd *abfd = reader->abfd;
e7c27a73 18559 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18560 unsigned int bytes_read;
18561 struct dwarf_block *blk;
18a8505e 18562 *need_reprocess = false;
c906108c 18563
aead7601 18564 attr->form = (enum dwarf_form) form;
a8329558 18565 switch (form)
c906108c 18566 {
c906108c 18567 case DW_FORM_ref_addr:
ae411497 18568 if (cu->header.version == 2)
c8a7a66f
TT
18569 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18570 &bytes_read);
ae411497 18571 else
8266302d
TT
18572 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18573 &bytes_read);
ae411497
TT
18574 info_ptr += bytes_read;
18575 break;
36586728 18576 case DW_FORM_GNU_ref_alt:
8266302d 18577 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18578 info_ptr += bytes_read;
18579 break;
ae411497 18580 case DW_FORM_addr:
c8a7a66f 18581 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18582 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18583 info_ptr += bytes_read;
c906108c
SS
18584 break;
18585 case DW_FORM_block2:
7b5a2f43 18586 blk = dwarf_alloc_block (cu);
c906108c
SS
18587 blk->size = read_2_bytes (abfd, info_ptr);
18588 info_ptr += 2;
18589 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18590 info_ptr += blk->size;
18591 DW_BLOCK (attr) = blk;
18592 break;
18593 case DW_FORM_block4:
7b5a2f43 18594 blk = dwarf_alloc_block (cu);
c906108c
SS
18595 blk->size = read_4_bytes (abfd, info_ptr);
18596 info_ptr += 4;
18597 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18598 info_ptr += blk->size;
18599 DW_BLOCK (attr) = blk;
18600 break;
18601 case DW_FORM_data2:
18602 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18603 info_ptr += 2;
18604 break;
18605 case DW_FORM_data4:
18606 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18607 info_ptr += 4;
18608 break;
18609 case DW_FORM_data8:
18610 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18611 info_ptr += 8;
18612 break;
0224619f
JK
18613 case DW_FORM_data16:
18614 blk = dwarf_alloc_block (cu);
18615 blk->size = 16;
18616 blk->data = read_n_bytes (abfd, info_ptr, 16);
18617 info_ptr += 16;
18618 DW_BLOCK (attr) = blk;
18619 break;
2dc7f7b3 18620 case DW_FORM_sec_offset:
8266302d 18621 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18622 info_ptr += bytes_read;
18623 break;
41144253 18624 case DW_FORM_loclistx:
18625 {
18626 *need_reprocess = true;
18627 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18628 info_ptr += bytes_read;
18629 }
18630 break;
c906108c 18631 case DW_FORM_string:
9b1c24c8 18632 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18633 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18634 info_ptr += bytes_read;
18635 break;
4bdf3d34 18636 case DW_FORM_strp:
36586728
TT
18637 if (!cu->per_cu->is_dwz)
18638 {
ed2dc618
SM
18639 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18640 abfd, info_ptr, cu_header,
36586728
TT
18641 &bytes_read);
18642 DW_STRING_IS_CANONICAL (attr) = 0;
18643 info_ptr += bytes_read;
18644 break;
18645 }
18646 /* FALLTHROUGH */
43988095
JK
18647 case DW_FORM_line_strp:
18648 if (!cu->per_cu->is_dwz)
18649 {
86c0bb4c
TT
18650 DW_STRING (attr)
18651 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18652 &bytes_read);
43988095
JK
18653 DW_STRING_IS_CANONICAL (attr) = 0;
18654 info_ptr += bytes_read;
18655 break;
18656 }
18657 /* FALLTHROUGH */
36586728
TT
18658 case DW_FORM_GNU_strp_alt:
18659 {
ed2dc618 18660 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18661 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18662 &bytes_read);
36586728 18663
0314b390 18664 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18665 DW_STRING_IS_CANONICAL (attr) = 0;
18666 info_ptr += bytes_read;
18667 }
4bdf3d34 18668 break;
2dc7f7b3 18669 case DW_FORM_exprloc:
c906108c 18670 case DW_FORM_block:
7b5a2f43 18671 blk = dwarf_alloc_block (cu);
c906108c
SS
18672 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18673 info_ptr += bytes_read;
18674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18675 info_ptr += blk->size;
18676 DW_BLOCK (attr) = blk;
18677 break;
18678 case DW_FORM_block1:
7b5a2f43 18679 blk = dwarf_alloc_block (cu);
c906108c
SS
18680 blk->size = read_1_byte (abfd, info_ptr);
18681 info_ptr += 1;
18682 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18683 info_ptr += blk->size;
18684 DW_BLOCK (attr) = blk;
18685 break;
18686 case DW_FORM_data1:
18687 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18688 info_ptr += 1;
18689 break;
18690 case DW_FORM_flag:
18691 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18692 info_ptr += 1;
18693 break;
2dc7f7b3
TT
18694 case DW_FORM_flag_present:
18695 DW_UNSND (attr) = 1;
18696 break;
c906108c
SS
18697 case DW_FORM_sdata:
18698 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18699 info_ptr += bytes_read;
18700 break;
18701 case DW_FORM_udata:
18a8505e 18702 case DW_FORM_rnglistx:
c906108c
SS
18703 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18704 info_ptr += bytes_read;
18705 break;
18706 case DW_FORM_ref1:
9c541725 18707 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18708 + read_1_byte (abfd, info_ptr));
c906108c
SS
18709 info_ptr += 1;
18710 break;
18711 case DW_FORM_ref2:
9c541725 18712 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18713 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18714 info_ptr += 2;
18715 break;
18716 case DW_FORM_ref4:
9c541725 18717 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18718 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18719 info_ptr += 4;
18720 break;
613e1657 18721 case DW_FORM_ref8:
9c541725 18722 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18723 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18724 info_ptr += 8;
18725 break;
55f1336d 18726 case DW_FORM_ref_sig8:
ac9ec31b 18727 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18728 info_ptr += 8;
18729 break;
c906108c 18730 case DW_FORM_ref_udata:
9c541725 18731 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18732 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18733 info_ptr += bytes_read;
18734 break;
c906108c 18735 case DW_FORM_indirect:
a8329558
KW
18736 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18737 info_ptr += bytes_read;
43988095
JK
18738 if (form == DW_FORM_implicit_const)
18739 {
18740 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18741 info_ptr += bytes_read;
18742 }
18743 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18744 info_ptr, need_reprocess);
43988095
JK
18745 break;
18746 case DW_FORM_implicit_const:
18747 DW_SND (attr) = implicit_const;
a8329558 18748 break;
336d760d 18749 case DW_FORM_addrx:
3019eac3 18750 case DW_FORM_GNU_addr_index:
18a8505e
AT
18751 *need_reprocess = true;
18752 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18753 info_ptr += bytes_read;
18754 break;
cf532bd1 18755 case DW_FORM_strx:
15f18d14
AT
18756 case DW_FORM_strx1:
18757 case DW_FORM_strx2:
18758 case DW_FORM_strx3:
18759 case DW_FORM_strx4:
3019eac3 18760 case DW_FORM_GNU_str_index:
3019eac3 18761 {
15f18d14
AT
18762 ULONGEST str_index;
18763 if (form == DW_FORM_strx1)
18764 {
18765 str_index = read_1_byte (abfd, info_ptr);
18766 info_ptr += 1;
18767 }
18768 else if (form == DW_FORM_strx2)
18769 {
18770 str_index = read_2_bytes (abfd, info_ptr);
18771 info_ptr += 2;
18772 }
18773 else if (form == DW_FORM_strx3)
18774 {
18775 str_index = read_3_bytes (abfd, info_ptr);
18776 info_ptr += 3;
18777 }
18778 else if (form == DW_FORM_strx4)
18779 {
18780 str_index = read_4_bytes (abfd, info_ptr);
18781 info_ptr += 4;
18782 }
18783 else
18784 {
18785 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18786 info_ptr += bytes_read;
18787 }
18a8505e
AT
18788 *need_reprocess = true;
18789 DW_UNSND (attr) = str_index;
18790 }
3019eac3 18791 break;
c906108c 18792 default:
8a3fe4f8 18793 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18794 dwarf_form_name (form),
18795 bfd_get_filename (abfd));
c906108c 18796 }
28e94949 18797
36586728 18798 /* Super hack. */
cd6c91b4 18799 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18800 attr->form = DW_FORM_GNU_ref_alt;
18801
28e94949
JB
18802 /* We have seen instances where the compiler tried to emit a byte
18803 size attribute of -1 which ended up being encoded as an unsigned
18804 0xffffffff. Although 0xffffffff is technically a valid size value,
18805 an object of this size seems pretty unlikely so we can relatively
18806 safely treat these cases as if the size attribute was invalid and
18807 treat them as zero by default. */
18808 if (attr->name == DW_AT_byte_size
18809 && form == DW_FORM_data4
18810 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18811 {
18812 complaint
b98664d3 18813 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18814 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18815 DW_UNSND (attr) = 0;
18816 }
28e94949 18817
c906108c
SS
18818 return info_ptr;
18819}
18820
a8329558
KW
18821/* Read an attribute described by an abbreviated attribute. */
18822
d521ce57 18823static const gdb_byte *
dee91e82
DE
18824read_attribute (const struct die_reader_specs *reader,
18825 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18826 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18827{
18828 attr->name = abbrev->name;
43988095 18829 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18830 abbrev->implicit_const, info_ptr,
18831 need_reprocess);
a8329558
KW
18832}
18833
43988095
JK
18834/* Return pointer to string at .debug_str offset STR_OFFSET. */
18835
18836static const char *
ed2dc618 18837read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18838 LONGEST str_offset)
43988095 18839{
4f44ae6c
TT
18840 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18841 str_offset, "DW_FORM_strp");
c906108c
SS
18842}
18843
43988095
JK
18844/* Return pointer to string at .debug_str offset as read from BUF.
18845 BUF is assumed to be in a compilation unit described by CU_HEADER.
18846 Return *BYTES_READ_PTR count of bytes read from BUF. */
18847
d521ce57 18848static const char *
ed2dc618
SM
18849read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18850 const gdb_byte *buf,
cf2c3c16
TT
18851 const struct comp_unit_head *cu_header,
18852 unsigned int *bytes_read_ptr)
18853{
8266302d 18854 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18855
4f44ae6c 18856 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18857}
18858
86c0bb4c 18859/* See read.h. */
43988095 18860
86c0bb4c
TT
18861const char *
18862dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18863 const struct comp_unit_head *cu_header,
18864 unsigned int *bytes_read_ptr)
18865{
86c0bb4c 18866 bfd *abfd = objfile->obfd;
8266302d 18867 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18868
86c0bb4c 18869 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18870}
18871
3019eac3 18872/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18873 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18874 ADDR_SIZE is the size of addresses from the CU header. */
18875
18876static CORE_ADDR
ed2dc618 18877read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18878 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18879 int addr_size)
3019eac3
DE
18880{
18881 struct objfile *objfile = dwarf2_per_objfile->objfile;
18882 bfd *abfd = objfile->obfd;
18883 const gdb_byte *info_ptr;
18a8505e 18884 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18885
96b79293 18886 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18887 if (dwarf2_per_objfile->addr.buffer == NULL)
18888 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18889 objfile_name (objfile));
18a8505e
AT
18890 if (addr_base_or_zero + addr_index * addr_size
18891 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18892 error (_("DW_FORM_addr_index pointing outside of "
18893 ".debug_addr section [in module %s]"),
4262abfb 18894 objfile_name (objfile));
3019eac3 18895 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18896 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18897 if (addr_size == 4)
18898 return bfd_get_32 (abfd, info_ptr);
18899 else
18900 return bfd_get_64 (abfd, info_ptr);
18901}
18902
18903/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18904
18905static CORE_ADDR
18906read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18907{
518817b3
SM
18908 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18909 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18910}
18911
18912/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18913
18914static CORE_ADDR
d521ce57 18915read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18916 unsigned int *bytes_read)
18917{
518817b3 18918 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18919 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18920
18921 return read_addr_index (cu, addr_index);
18922}
18923
450a1bfc 18924/* See read.h. */
3019eac3
DE
18925
18926CORE_ADDR
450a1bfc 18927dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18928{
ed2dc618 18929 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18930 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18931 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18932 int addr_size;
18933
3019eac3
DE
18934 /* We need addr_base and addr_size.
18935 If we don't have PER_CU->cu, we have to get it.
18936 Nasty, but the alternative is storing the needed info in PER_CU,
18937 which at this point doesn't seem justified: it's not clear how frequently
18938 it would get used and it would increase the size of every PER_CU.
18939 Entry points like dwarf2_per_cu_addr_size do a similar thing
18940 so we're not in uncharted territory here.
18941 Alas we need to be a bit more complicated as addr_base is contained
18942 in the DIE.
18943
18944 We don't need to read the entire CU(/TU).
18945 We just need the header and top level die.
a1b64ce1 18946
3019eac3 18947 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18948 For now we skip this optimization. */
3019eac3
DE
18949
18950 if (cu != NULL)
18951 {
18952 addr_base = cu->addr_base;
18953 addr_size = cu->header.addr_size;
18954 }
18955 else
18956 {
6751ebae 18957 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18958 addr_base = reader.cu->addr_base;
18959 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18960 }
18961
ed2dc618
SM
18962 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18963 addr_size);
3019eac3
DE
18964}
18965
18a8505e
AT
18966/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18967 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18968 DWO file. */
3019eac3 18969
d521ce57 18970static const char *
18a8505e
AT
18971read_str_index (struct dwarf2_cu *cu,
18972 struct dwarf2_section_info *str_section,
18973 struct dwarf2_section_info *str_offsets_section,
18974 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18975{
518817b3
SM
18976 struct dwarf2_per_objfile *dwarf2_per_objfile
18977 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18978 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18979 const char *objf_name = objfile_name (objfile);
3019eac3 18980 bfd *abfd = objfile->obfd;
d521ce57 18981 const gdb_byte *info_ptr;
3019eac3 18982 ULONGEST str_offset;
cf532bd1 18983 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18984
96b79293
TT
18985 str_section->read (objfile);
18986 str_offsets_section->read (objfile);
73869dc2 18987 if (str_section->buffer == NULL)
18a8505e 18988 error (_("%s used without %s section"
9d8780f0 18989 " in CU at offset %s [in module %s]"),
96b79293 18990 form_name, str_section->get_name (),
18a8505e 18991 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18992 if (str_offsets_section->buffer == NULL)
18a8505e 18993 error (_("%s used without %s section"
9d8780f0 18994 " in CU at offset %s [in module %s]"),
96b79293 18995 form_name, str_section->get_name (),
18a8505e 18996 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18997 info_ptr = (str_offsets_section->buffer
18a8505e 18998 + str_offsets_base
3019eac3
DE
18999 + str_index * cu->header.offset_size);
19000 if (cu->header.offset_size == 4)
19001 str_offset = bfd_get_32 (abfd, info_ptr);
19002 else
19003 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19004 if (str_offset >= str_section->size)
57d63ce2 19005 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19006 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19007 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19008 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19009}
19010
18a8505e
AT
19011/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19012
19013static const char *
19014read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19015{
19016 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19017 ? reader->cu->header.addr_size : 0;
19018 return read_str_index (reader->cu,
19019 &reader->dwo_file->sections.str,
19020 &reader->dwo_file->sections.str_offsets,
19021 str_offsets_base, str_index);
19022}
19023
19024/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19025
19026static const char *
19027read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19028{
19029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19030 const char *objf_name = objfile_name (objfile);
19031 static const char form_name[] = "DW_FORM_GNU_str_index";
19032 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19033
19034 if (!cu->str_offsets_base.has_value ())
19035 error (_("%s used in Fission stub without %s"
19036 " in CU at offset 0x%lx [in module %s]"),
19037 form_name, str_offsets_attr_name,
19038 (long) cu->header.offset_size, objf_name);
19039
19040 return read_str_index (cu,
19041 &cu->per_cu->dwarf2_per_objfile->str,
19042 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19043 *cu->str_offsets_base, str_index);
19044}
19045
3019eac3
DE
19046/* Return the length of an LEB128 number in BUF. */
19047
19048static int
19049leb128_size (const gdb_byte *buf)
19050{
19051 const gdb_byte *begin = buf;
19052 gdb_byte byte;
19053
19054 while (1)
19055 {
19056 byte = *buf++;
19057 if ((byte & 128) == 0)
19058 return buf - begin;
19059 }
19060}
19061
c906108c 19062static void
e142c38c 19063set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19064{
19065 switch (lang)
19066 {
19067 case DW_LANG_C89:
76bee0cc 19068 case DW_LANG_C99:
0cfd832f 19069 case DW_LANG_C11:
c906108c 19070 case DW_LANG_C:
d1be3247 19071 case DW_LANG_UPC:
e142c38c 19072 cu->language = language_c;
c906108c 19073 break;
9c37b5ae 19074 case DW_LANG_Java:
c906108c 19075 case DW_LANG_C_plus_plus:
0cfd832f
MW
19076 case DW_LANG_C_plus_plus_11:
19077 case DW_LANG_C_plus_plus_14:
e142c38c 19078 cu->language = language_cplus;
c906108c 19079 break;
6aecb9c2
JB
19080 case DW_LANG_D:
19081 cu->language = language_d;
19082 break;
c906108c
SS
19083 case DW_LANG_Fortran77:
19084 case DW_LANG_Fortran90:
b21b22e0 19085 case DW_LANG_Fortran95:
f7de9aab
MW
19086 case DW_LANG_Fortran03:
19087 case DW_LANG_Fortran08:
e142c38c 19088 cu->language = language_fortran;
c906108c 19089 break;
a766d390
DE
19090 case DW_LANG_Go:
19091 cu->language = language_go;
19092 break;
c906108c 19093 case DW_LANG_Mips_Assembler:
e142c38c 19094 cu->language = language_asm;
c906108c
SS
19095 break;
19096 case DW_LANG_Ada83:
8aaf0b47 19097 case DW_LANG_Ada95:
bc5f45f8
JB
19098 cu->language = language_ada;
19099 break;
72019c9c
GM
19100 case DW_LANG_Modula2:
19101 cu->language = language_m2;
19102 break;
fe8e67fd
PM
19103 case DW_LANG_Pascal83:
19104 cu->language = language_pascal;
19105 break;
22566fbd
DJ
19106 case DW_LANG_ObjC:
19107 cu->language = language_objc;
19108 break;
c44af4eb
TT
19109 case DW_LANG_Rust:
19110 case DW_LANG_Rust_old:
19111 cu->language = language_rust;
19112 break;
c906108c
SS
19113 case DW_LANG_Cobol74:
19114 case DW_LANG_Cobol85:
c906108c 19115 default:
e142c38c 19116 cu->language = language_minimal;
c906108c
SS
19117 break;
19118 }
e142c38c 19119 cu->language_defn = language_def (cu->language);
c906108c
SS
19120}
19121
19122/* Return the named attribute or NULL if not there. */
19123
19124static struct attribute *
e142c38c 19125dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19126{
a48e046c 19127 for (;;)
c906108c 19128 {
a48e046c
TT
19129 unsigned int i;
19130 struct attribute *spec = NULL;
19131
19132 for (i = 0; i < die->num_attrs; ++i)
19133 {
19134 if (die->attrs[i].name == name)
19135 return &die->attrs[i];
19136 if (die->attrs[i].name == DW_AT_specification
19137 || die->attrs[i].name == DW_AT_abstract_origin)
19138 spec = &die->attrs[i];
19139 }
19140
19141 if (!spec)
19142 break;
c906108c 19143
f2f0e013 19144 die = follow_die_ref (die, spec, &cu);
f2f0e013 19145 }
c5aa993b 19146
c906108c
SS
19147 return NULL;
19148}
19149
7d45c7c3
KB
19150/* Return the string associated with a string-typed attribute, or NULL if it
19151 is either not found or is of an incorrect type. */
19152
19153static const char *
19154dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19155{
19156 struct attribute *attr;
19157 const char *str = NULL;
19158
19159 attr = dwarf2_attr (die, name, cu);
19160
19161 if (attr != NULL)
19162 {
43988095 19163 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19164 || attr->form == DW_FORM_string
cf532bd1 19165 || attr->form == DW_FORM_strx
8fe0f950
AT
19166 || attr->form == DW_FORM_strx1
19167 || attr->form == DW_FORM_strx2
19168 || attr->form == DW_FORM_strx3
19169 || attr->form == DW_FORM_strx4
b3340438 19170 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19171 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19172 str = DW_STRING (attr);
19173 else
b98664d3 19174 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19175 "DIE at %s in module %s"),
19176 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19177 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19178 }
19179
19180 return str;
19181}
19182
a084a2a6 19183/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19184 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19185static const char *
19186dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19187{
19188 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19189 if (dwo_name == nullptr)
19190 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19191 return dwo_name;
19192}
19193
05cf31d1
JB
19194/* Return non-zero iff the attribute NAME is defined for the given DIE,
19195 and holds a non-zero value. This function should only be used for
2dc7f7b3 19196 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19197
19198static int
19199dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19200{
19201 struct attribute *attr = dwarf2_attr (die, name, cu);
19202
19203 return (attr && DW_UNSND (attr));
19204}
19205
3ca72b44 19206static int
e142c38c 19207die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19208{
05cf31d1
JB
19209 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19210 which value is non-zero. However, we have to be careful with
19211 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19212 (via dwarf2_flag_true_p) follows this attribute. So we may
19213 end up accidently finding a declaration attribute that belongs
19214 to a different DIE referenced by the specification attribute,
19215 even though the given DIE does not have a declaration attribute. */
19216 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19217 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19218}
19219
63d06c5c 19220/* Return the die giving the specification for DIE, if there is
f2f0e013 19221 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19222 containing the return value on output. If there is no
19223 specification, but there is an abstract origin, that is
19224 returned. */
63d06c5c
DC
19225
19226static struct die_info *
f2f0e013 19227die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19228{
f2f0e013
DJ
19229 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19230 *spec_cu);
63d06c5c 19231
edb3359d
DJ
19232 if (spec_attr == NULL)
19233 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19234
63d06c5c
DC
19235 if (spec_attr == NULL)
19236 return NULL;
19237 else
f2f0e013 19238 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19239}
c906108c 19240
527f3840
JK
19241/* Stub for free_line_header to match void * callback types. */
19242
19243static void
19244free_line_header_voidp (void *arg)
19245{
9a3c8263 19246 struct line_header *lh = (struct line_header *) arg;
527f3840 19247
fff8551c 19248 delete lh;
527f3840
JK
19249}
19250
83769d0b 19251/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19252
19253static struct dwarf2_section_info *
19254get_debug_line_section (struct dwarf2_cu *cu)
19255{
19256 struct dwarf2_section_info *section;
518817b3
SM
19257 struct dwarf2_per_objfile *dwarf2_per_objfile
19258 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19259
19260 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19261 DWO file. */
19262 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19263 section = &cu->dwo_unit->dwo_file->sections.line;
19264 else if (cu->per_cu->is_dwz)
19265 {
ed2dc618 19266 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19267
19268 section = &dwz->line;
19269 }
19270 else
19271 section = &dwarf2_per_objfile->line;
19272
19273 return section;
19274}
19275
debd256d 19276/* Read the statement program header starting at OFFSET in
3019eac3 19277 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19278 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19279 Returns NULL if there is a problem reading the header, e.g., if it
19280 has a version we don't understand.
debd256d
JB
19281
19282 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19283 the returned object point into the dwarf line section buffer,
19284 and must not be freed. */
ae2de4f8 19285
fff8551c 19286static line_header_up
9c541725 19287dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19288{
3019eac3 19289 struct dwarf2_section_info *section;
518817b3
SM
19290 struct dwarf2_per_objfile *dwarf2_per_objfile
19291 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19292
36586728 19293 section = get_debug_line_section (cu);
96b79293 19294 section->read (dwarf2_per_objfile->objfile);
3019eac3 19295 if (section->buffer == NULL)
debd256d 19296 {
3019eac3 19297 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19298 complaint (_("missing .debug_line.dwo section"));
3019eac3 19299 else
b98664d3 19300 complaint (_("missing .debug_line section"));
debd256d
JB
19301 return 0;
19302 }
19303
0df7ad3a
TT
19304 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19305 dwarf2_per_objfile, section,
19306 &cu->header);
debd256d 19307}
c906108c 19308
c6da4cef 19309/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19310 Return the file name of the psymtab for the given file_entry.
c6da4cef 19311 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19312 If space for the result is malloc'd, *NAME_HOLDER will be set.
19313 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19314
d521ce57 19315static const char *
7ba99d21 19316psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19317 const dwarf2_psymtab *pst,
c89b44cd
TT
19318 const char *comp_dir,
19319 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19320{
d521ce57
TT
19321 const char *include_name = fe.name;
19322 const char *include_name_to_compare = include_name;
72b9f47f 19323 const char *pst_filename;
c6da4cef
DE
19324 int file_is_pst;
19325
8c43009f 19326 const char *dir_name = fe.include_dir (lh);
c6da4cef 19327
c89b44cd 19328 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19329 if (!IS_ABSOLUTE_PATH (include_name)
19330 && (dir_name != NULL || comp_dir != NULL))
19331 {
19332 /* Avoid creating a duplicate psymtab for PST.
19333 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19334 Before we do the comparison, however, we need to account
19335 for DIR_NAME and COMP_DIR.
19336 First prepend dir_name (if non-NULL). If we still don't
19337 have an absolute path prepend comp_dir (if non-NULL).
19338 However, the directory we record in the include-file's
19339 psymtab does not contain COMP_DIR (to match the
19340 corresponding symtab(s)).
19341
19342 Example:
19343
19344 bash$ cd /tmp
19345 bash$ gcc -g ./hello.c
19346 include_name = "hello.c"
19347 dir_name = "."
19348 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19349 DW_AT_name = "./hello.c"
19350
19351 */
c6da4cef
DE
19352
19353 if (dir_name != NULL)
19354 {
c89b44cd
TT
19355 name_holder->reset (concat (dir_name, SLASH_STRING,
19356 include_name, (char *) NULL));
19357 include_name = name_holder->get ();
c6da4cef 19358 include_name_to_compare = include_name;
c6da4cef
DE
19359 }
19360 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19361 {
c89b44cd
TT
19362 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19363 include_name, (char *) NULL));
19364 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19365 }
19366 }
19367
19368 pst_filename = pst->filename;
c89b44cd 19369 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19370 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19371 {
c89b44cd
TT
19372 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19373 pst_filename, (char *) NULL));
19374 pst_filename = copied_name.get ();
c6da4cef
DE
19375 }
19376
1e3fad37 19377 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19378
c6da4cef
DE
19379 if (file_is_pst)
19380 return NULL;
19381 return include_name;
19382}
19383
d9b3de22
DE
19384/* State machine to track the state of the line number program. */
19385
6f77053d 19386class lnp_state_machine
d9b3de22 19387{
6f77053d
PA
19388public:
19389 /* Initialize a machine state for the start of a line number
19390 program. */
804d2729
TT
19391 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19392 bool record_lines_p);
6f77053d 19393
8c43009f
PA
19394 file_entry *current_file ()
19395 {
19396 /* lh->file_names is 0-based, but the file name numbers in the
19397 statement program are 1-based. */
6f77053d
PA
19398 return m_line_header->file_name_at (m_file);
19399 }
19400
19401 /* Record the line in the state machine. END_SEQUENCE is true if
19402 we're processing the end of a sequence. */
19403 void record_line (bool end_sequence);
19404
7ab6656f
OJ
19405 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19406 nop-out rest of the lines in this sequence. */
6f77053d
PA
19407 void check_line_address (struct dwarf2_cu *cu,
19408 const gdb_byte *line_ptr,
7ab6656f 19409 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19410
19411 void handle_set_discriminator (unsigned int discriminator)
19412 {
19413 m_discriminator = discriminator;
19414 m_line_has_non_zero_discriminator |= discriminator != 0;
19415 }
19416
19417 /* Handle DW_LNE_set_address. */
19418 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19419 {
19420 m_op_index = 0;
19421 address += baseaddr;
19422 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19423 }
19424
19425 /* Handle DW_LNS_advance_pc. */
19426 void handle_advance_pc (CORE_ADDR adjust);
19427
19428 /* Handle a special opcode. */
19429 void handle_special_opcode (unsigned char op_code);
19430
19431 /* Handle DW_LNS_advance_line. */
19432 void handle_advance_line (int line_delta)
19433 {
19434 advance_line (line_delta);
19435 }
19436
19437 /* Handle DW_LNS_set_file. */
19438 void handle_set_file (file_name_index file);
19439
19440 /* Handle DW_LNS_negate_stmt. */
19441 void handle_negate_stmt ()
19442 {
19443 m_is_stmt = !m_is_stmt;
19444 }
19445
19446 /* Handle DW_LNS_const_add_pc. */
19447 void handle_const_add_pc ();
19448
19449 /* Handle DW_LNS_fixed_advance_pc. */
19450 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19451 {
19452 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19453 m_op_index = 0;
19454 }
19455
19456 /* Handle DW_LNS_copy. */
19457 void handle_copy ()
19458 {
19459 record_line (false);
19460 m_discriminator = 0;
19461 }
19462
19463 /* Handle DW_LNE_end_sequence. */
19464 void handle_end_sequence ()
19465 {
804d2729 19466 m_currently_recording_lines = true;
6f77053d
PA
19467 }
19468
19469private:
19470 /* Advance the line by LINE_DELTA. */
19471 void advance_line (int line_delta)
19472 {
19473 m_line += line_delta;
19474
19475 if (line_delta != 0)
19476 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19477 }
19478
804d2729
TT
19479 struct dwarf2_cu *m_cu;
19480
6f77053d
PA
19481 gdbarch *m_gdbarch;
19482
19483 /* True if we're recording lines.
19484 Otherwise we're building partial symtabs and are just interested in
19485 finding include files mentioned by the line number program. */
19486 bool m_record_lines_p;
19487
8c43009f 19488 /* The line number header. */
6f77053d 19489 line_header *m_line_header;
8c43009f 19490
6f77053d
PA
19491 /* These are part of the standard DWARF line number state machine,
19492 and initialized according to the DWARF spec. */
d9b3de22 19493
6f77053d 19494 unsigned char m_op_index = 0;
7ba99d21
AT
19495 /* The line table index of the current file. */
19496 file_name_index m_file = 1;
6f77053d
PA
19497 unsigned int m_line = 1;
19498
19499 /* These are initialized in the constructor. */
19500
19501 CORE_ADDR m_address;
19502 bool m_is_stmt;
19503 unsigned int m_discriminator;
d9b3de22
DE
19504
19505 /* Additional bits of state we need to track. */
19506
19507 /* The last file that we called dwarf2_start_subfile for.
19508 This is only used for TLLs. */
6f77053d 19509 unsigned int m_last_file = 0;
d9b3de22 19510 /* The last file a line number was recorded for. */
6f77053d 19511 struct subfile *m_last_subfile = NULL;
d9b3de22 19512
804d2729
TT
19513 /* When true, record the lines we decode. */
19514 bool m_currently_recording_lines = false;
d9b3de22
DE
19515
19516 /* The last line number that was recorded, used to coalesce
19517 consecutive entries for the same line. This can happen, for
19518 example, when discriminators are present. PR 17276. */
6f77053d
PA
19519 unsigned int m_last_line = 0;
19520 bool m_line_has_non_zero_discriminator = false;
8c43009f 19521};
d9b3de22 19522
6f77053d
PA
19523void
19524lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19525{
19526 CORE_ADDR addr_adj = (((m_op_index + adjust)
19527 / m_line_header->maximum_ops_per_instruction)
19528 * m_line_header->minimum_instruction_length);
19529 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19530 m_op_index = ((m_op_index + adjust)
19531 % m_line_header->maximum_ops_per_instruction);
19532}
d9b3de22 19533
6f77053d
PA
19534void
19535lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19536{
6f77053d 19537 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19538 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19539 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19540 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19541 / m_line_header->maximum_ops_per_instruction)
19542 * m_line_header->minimum_instruction_length);
19543 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19544 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19545 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19546
258bf0ee 19547 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19548 advance_line (line_delta);
19549 record_line (false);
19550 m_discriminator = 0;
19551}
d9b3de22 19552
6f77053d
PA
19553void
19554lnp_state_machine::handle_set_file (file_name_index file)
19555{
19556 m_file = file;
19557
19558 const file_entry *fe = current_file ();
19559 if (fe == NULL)
19560 dwarf2_debug_line_missing_file_complaint ();
19561 else if (m_record_lines_p)
19562 {
19563 const char *dir = fe->include_dir (m_line_header);
19564
c24bdb02 19565 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19566 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19567 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19568 }
19569}
19570
19571void
19572lnp_state_machine::handle_const_add_pc ()
19573{
19574 CORE_ADDR adjust
19575 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19576
19577 CORE_ADDR addr_adj
19578 = (((m_op_index + adjust)
19579 / m_line_header->maximum_ops_per_instruction)
19580 * m_line_header->minimum_instruction_length);
19581
19582 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19583 m_op_index = ((m_op_index + adjust)
19584 % m_line_header->maximum_ops_per_instruction);
19585}
d9b3de22 19586
a05a36a5
DE
19587/* Return non-zero if we should add LINE to the line number table.
19588 LINE is the line to add, LAST_LINE is the last line that was added,
19589 LAST_SUBFILE is the subfile for LAST_LINE.
19590 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19591 had a non-zero discriminator.
19592
19593 We have to be careful in the presence of discriminators.
19594 E.g., for this line:
19595
19596 for (i = 0; i < 100000; i++);
19597
19598 clang can emit four line number entries for that one line,
19599 each with a different discriminator.
19600 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19601
19602 However, we want gdb to coalesce all four entries into one.
19603 Otherwise the user could stepi into the middle of the line and
19604 gdb would get confused about whether the pc really was in the
19605 middle of the line.
19606
19607 Things are further complicated by the fact that two consecutive
19608 line number entries for the same line is a heuristic used by gcc
19609 to denote the end of the prologue. So we can't just discard duplicate
19610 entries, we have to be selective about it. The heuristic we use is
19611 that we only collapse consecutive entries for the same line if at least
19612 one of those entries has a non-zero discriminator. PR 17276.
19613
19614 Note: Addresses in the line number state machine can never go backwards
19615 within one sequence, thus this coalescing is ok. */
19616
19617static int
804d2729
TT
19618dwarf_record_line_p (struct dwarf2_cu *cu,
19619 unsigned int line, unsigned int last_line,
a05a36a5
DE
19620 int line_has_non_zero_discriminator,
19621 struct subfile *last_subfile)
19622{
c24bdb02 19623 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19624 return 1;
19625 if (line != last_line)
19626 return 1;
19627 /* Same line for the same file that we've seen already.
19628 As a last check, for pr 17276, only record the line if the line
19629 has never had a non-zero discriminator. */
19630 if (!line_has_non_zero_discriminator)
19631 return 1;
19632 return 0;
19633}
19634
804d2729
TT
19635/* Use the CU's builder to record line number LINE beginning at
19636 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19637
19638static void
d9b3de22 19639dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19640 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19641 struct dwarf2_cu *cu)
252a6764
DE
19642{
19643 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19644
27e0867f
DE
19645 if (dwarf_line_debug)
19646 {
19647 fprintf_unfiltered (gdb_stdlog,
19648 "Recording line %u, file %s, address %s\n",
19649 line, lbasename (subfile->name),
19650 paddress (gdbarch, address));
19651 }
19652
804d2729 19653 if (cu != nullptr)
8c95582d 19654 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19655}
19656
19657/* Subroutine of dwarf_decode_lines_1 to simplify it.
19658 Mark the end of a set of line number records.
d9b3de22 19659 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19660 If SUBFILE is NULL the request is ignored. */
19661
19662static void
19663dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19664 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19665{
27e0867f
DE
19666 if (subfile == NULL)
19667 return;
19668
19669 if (dwarf_line_debug)
19670 {
19671 fprintf_unfiltered (gdb_stdlog,
19672 "Finishing current line, file %s, address %s\n",
19673 lbasename (subfile->name),
19674 paddress (gdbarch, address));
19675 }
19676
8c95582d 19677 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19678}
19679
6f77053d
PA
19680void
19681lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19682{
d9b3de22
DE
19683 if (dwarf_line_debug)
19684 {
19685 fprintf_unfiltered (gdb_stdlog,
19686 "Processing actual line %u: file %u,"
94a72be7 19687 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19688 m_line, m_file,
6f77053d 19689 paddress (m_gdbarch, m_address),
94a72be7
AB
19690 m_is_stmt, m_discriminator,
19691 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19692 }
19693
6f77053d 19694 file_entry *fe = current_file ();
8c43009f
PA
19695
19696 if (fe == NULL)
d9b3de22
DE
19697 dwarf2_debug_line_missing_file_complaint ();
19698 /* For now we ignore lines not starting on an instruction boundary.
19699 But not when processing end_sequence for compatibility with the
19700 previous version of the code. */
6f77053d 19701 else if (m_op_index == 0 || end_sequence)
d9b3de22 19702 {
8c43009f 19703 fe->included_p = 1;
8c95582d 19704 if (m_record_lines_p)
d9b3de22 19705 {
c24bdb02 19706 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19707 || end_sequence)
d9b3de22 19708 {
804d2729
TT
19709 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19710 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19711 }
19712
19713 if (!end_sequence)
19714 {
8c95582d
AB
19715 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19716
804d2729 19717 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19718 m_line_has_non_zero_discriminator,
19719 m_last_subfile))
d9b3de22 19720 {
c24bdb02 19721 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19722 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19723 builder->get_current_subfile (),
8c95582d 19724 m_line, m_address, is_stmt,
804d2729 19725 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19726 }
c24bdb02 19727 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19728 m_last_line = m_line;
d9b3de22
DE
19729 }
19730 }
19731 }
19732}
19733
804d2729
TT
19734lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19735 line_header *lh, bool record_lines_p)
d9b3de22 19736{
804d2729 19737 m_cu = cu;
6f77053d
PA
19738 m_gdbarch = arch;
19739 m_record_lines_p = record_lines_p;
19740 m_line_header = lh;
d9b3de22 19741
804d2729 19742 m_currently_recording_lines = true;
d9b3de22 19743
d9b3de22
DE
19744 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19745 was a line entry for it so that the backend has a chance to adjust it
19746 and also record it in case it needs it. This is currently used by MIPS
19747 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19748 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19749 m_is_stmt = lh->default_is_stmt;
19750 m_discriminator = 0;
252a6764
DE
19751}
19752
6f77053d
PA
19753void
19754lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19755 const gdb_byte *line_ptr,
7ab6656f 19756 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19757{
7ab6656f
OJ
19758 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19759 the pc range of the CU. However, we restrict the test to only ADDRESS
19760 values of zero to preserve GDB's previous behaviour which is to handle
19761 the specific case of a function being GC'd by the linker. */
924c2928 19762
7ab6656f 19763 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19764 {
19765 /* This line table is for a function which has been
19766 GCd by the linker. Ignore it. PR gdb/12528 */
19767
518817b3 19768 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
19769 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19770
b98664d3 19771 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 19772 line_offset, objfile_name (objfile));
804d2729
TT
19773 m_currently_recording_lines = false;
19774 /* Note: m_currently_recording_lines is left as false until we see
19775 DW_LNE_end_sequence. */
924c2928
DE
19776 }
19777}
19778
f3f5162e 19779/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19780 Process the line number information in LH.
19781 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19782 program in order to set included_p for every referenced header. */
debd256d 19783
c906108c 19784static void
43f3e411
DE
19785dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19786 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19787{
d521ce57
TT
19788 const gdb_byte *line_ptr, *extended_end;
19789 const gdb_byte *line_end;
a8c50c1f 19790 unsigned int bytes_read, extended_len;
699ca60a 19791 unsigned char op_code, extended_op;
e142c38c 19792 CORE_ADDR baseaddr;
518817b3 19793 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19794 bfd *abfd = objfile->obfd;
fbf65064 19795 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19796 /* True if we're recording line info (as opposed to building partial
19797 symtabs and just interested in finding include files mentioned by
19798 the line number program). */
19799 bool record_lines_p = !decode_for_pst_p;
e142c38c 19800
b3b3bada 19801 baseaddr = objfile->text_section_offset ();
c906108c 19802
debd256d
JB
19803 line_ptr = lh->statement_program_start;
19804 line_end = lh->statement_program_end;
c906108c
SS
19805
19806 /* Read the statement sequences until there's nothing left. */
19807 while (line_ptr < line_end)
19808 {
6f77053d
PA
19809 /* The DWARF line number program state machine. Reset the state
19810 machine at the start of each sequence. */
804d2729 19811 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 19812 bool end_sequence = false;
d9b3de22 19813
8c43009f 19814 if (record_lines_p)
c906108c 19815 {
8c43009f
PA
19816 /* Start a subfile for the current file of the state
19817 machine. */
19818 const file_entry *fe = state_machine.current_file ();
19819
19820 if (fe != NULL)
804d2729 19821 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
19822 }
19823
a738430d 19824 /* Decode the table. */
d9b3de22 19825 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19826 {
19827 op_code = read_1_byte (abfd, line_ptr);
19828 line_ptr += 1;
9aa1fe7e 19829
debd256d 19830 if (op_code >= lh->opcode_base)
6e70227d 19831 {
8e07a239 19832 /* Special opcode. */
6f77053d 19833 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19834 }
19835 else switch (op_code)
c906108c
SS
19836 {
19837 case DW_LNS_extended_op:
3e43a32a
MS
19838 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19839 &bytes_read);
473b7be6 19840 line_ptr += bytes_read;
a8c50c1f 19841 extended_end = line_ptr + extended_len;
c906108c
SS
19842 extended_op = read_1_byte (abfd, line_ptr);
19843 line_ptr += 1;
19844 switch (extended_op)
19845 {
19846 case DW_LNE_end_sequence:
6f77053d
PA
19847 state_machine.handle_end_sequence ();
19848 end_sequence = true;
c906108c
SS
19849 break;
19850 case DW_LNE_set_address:
d9b3de22
DE
19851 {
19852 CORE_ADDR address
c8a7a66f 19853 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 19854 line_ptr += bytes_read;
6f77053d
PA
19855
19856 state_machine.check_line_address (cu, line_ptr,
7ab6656f 19857 lowpc - baseaddr, address);
6f77053d 19858 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19859 }
c906108c
SS
19860 break;
19861 case DW_LNE_define_file:
debd256d 19862 {
d521ce57 19863 const char *cur_file;
ecfb656c
PA
19864 unsigned int mod_time, length;
19865 dir_index dindex;
6e70227d 19866
3e43a32a
MS
19867 cur_file = read_direct_string (abfd, line_ptr,
19868 &bytes_read);
debd256d 19869 line_ptr += bytes_read;
ecfb656c 19870 dindex = (dir_index)
debd256d
JB
19871 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19872 line_ptr += bytes_read;
19873 mod_time =
19874 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19875 line_ptr += bytes_read;
19876 length =
19877 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19878 line_ptr += bytes_read;
ecfb656c 19879 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19880 }
c906108c 19881 break;
d0c6ba3d 19882 case DW_LNE_set_discriminator:
6f77053d
PA
19883 {
19884 /* The discriminator is not interesting to the
19885 debugger; just ignore it. We still need to
19886 check its value though:
19887 if there are consecutive entries for the same
19888 (non-prologue) line we want to coalesce them.
19889 PR 17276. */
19890 unsigned int discr
19891 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19892 line_ptr += bytes_read;
19893
19894 state_machine.handle_set_discriminator (discr);
19895 }
d0c6ba3d 19896 break;
c906108c 19897 default:
b98664d3 19898 complaint (_("mangled .debug_line section"));
debd256d 19899 return;
c906108c 19900 }
a8c50c1f
DJ
19901 /* Make sure that we parsed the extended op correctly. If e.g.
19902 we expected a different address size than the producer used,
19903 we may have read the wrong number of bytes. */
19904 if (line_ptr != extended_end)
19905 {
b98664d3 19906 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
19907 return;
19908 }
c906108c
SS
19909 break;
19910 case DW_LNS_copy:
6f77053d 19911 state_machine.handle_copy ();
c906108c
SS
19912 break;
19913 case DW_LNS_advance_pc:
2dc7f7b3
TT
19914 {
19915 CORE_ADDR adjust
19916 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19917 line_ptr += bytes_read;
6f77053d
PA
19918
19919 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19920 }
c906108c
SS
19921 break;
19922 case DW_LNS_advance_line:
a05a36a5
DE
19923 {
19924 int line_delta
19925 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19926 line_ptr += bytes_read;
6f77053d
PA
19927
19928 state_machine.handle_advance_line (line_delta);
a05a36a5 19929 }
c906108c
SS
19930 break;
19931 case DW_LNS_set_file:
d9b3de22 19932 {
6f77053d 19933 file_name_index file
ecfb656c
PA
19934 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19935 &bytes_read);
d9b3de22 19936 line_ptr += bytes_read;
8c43009f 19937
6f77053d 19938 state_machine.handle_set_file (file);
d9b3de22 19939 }
c906108c
SS
19940 break;
19941 case DW_LNS_set_column:
0ad93d4f 19942 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19943 line_ptr += bytes_read;
19944 break;
19945 case DW_LNS_negate_stmt:
6f77053d 19946 state_machine.handle_negate_stmt ();
c906108c
SS
19947 break;
19948 case DW_LNS_set_basic_block:
c906108c 19949 break;
c2c6d25f
JM
19950 /* Add to the address register of the state machine the
19951 address increment value corresponding to special opcode
a738430d
MK
19952 255. I.e., this value is scaled by the minimum
19953 instruction length since special opcode 255 would have
b021a221 19954 scaled the increment. */
c906108c 19955 case DW_LNS_const_add_pc:
6f77053d 19956 state_machine.handle_const_add_pc ();
c906108c
SS
19957 break;
19958 case DW_LNS_fixed_advance_pc:
3e29f34a 19959 {
6f77053d 19960 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19961 line_ptr += 2;
6f77053d
PA
19962
19963 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19964 }
c906108c 19965 break;
9aa1fe7e 19966 default:
a738430d
MK
19967 {
19968 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19969 int i;
a738430d 19970
debd256d 19971 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19972 {
19973 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19974 line_ptr += bytes_read;
19975 }
19976 }
c906108c
SS
19977 }
19978 }
d9b3de22
DE
19979
19980 if (!end_sequence)
19981 dwarf2_debug_line_missing_end_sequence_complaint ();
19982
19983 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19984 in which case we still finish recording the last line). */
6f77053d 19985 state_machine.record_line (true);
c906108c 19986 }
f3f5162e
DE
19987}
19988
19989/* Decode the Line Number Program (LNP) for the given line_header
19990 structure and CU. The actual information extracted and the type
19991 of structures created from the LNP depends on the value of PST.
19992
19993 1. If PST is NULL, then this procedure uses the data from the program
19994 to create all necessary symbol tables, and their linetables.
19995
19996 2. If PST is not NULL, this procedure reads the program to determine
19997 the list of files included by the unit represented by PST, and
19998 builds all the associated partial symbol tables.
19999
20000 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20001 It is used for relative paths in the line table.
20002 NOTE: When processing partial symtabs (pst != NULL),
20003 comp_dir == pst->dirname.
20004
20005 NOTE: It is important that psymtabs have the same file name (via strcmp)
20006 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20007 symtab we don't use it in the name of the psymtabs we create.
20008 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20009 A good testcase for this is mb-inline.exp.
20010
527f3840
JK
20011 LOWPC is the lowest address in CU (or 0 if not known).
20012
20013 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20014 for its PC<->lines mapping information. Otherwise only the filename
20015 table is read in. */
f3f5162e
DE
20016
20017static void
20018dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20019 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20020 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20021{
518817b3 20022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20023 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20024
527f3840
JK
20025 if (decode_mapping)
20026 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20027
20028 if (decode_for_pst_p)
20029 {
aaa75496
JB
20030 /* Now that we're done scanning the Line Header Program, we can
20031 create the psymtab of each included file. */
7ba99d21
AT
20032 for (auto &file_entry : lh->file_names ())
20033 if (file_entry.included_p == 1)
aaa75496 20034 {
c89b44cd 20035 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20036 const char *include_name =
7ba99d21
AT
20037 psymtab_include_file_name (lh, file_entry, pst,
20038 comp_dir, &name_holder);
c6da4cef 20039 if (include_name != NULL)
aaa75496
JB
20040 dwarf2_create_include_psymtab (include_name, pst, objfile);
20041 }
20042 }
cb1df416
DJ
20043 else
20044 {
20045 /* Make sure a symtab is created for every file, even files
20046 which contain only variables (i.e. no code with associated
20047 line numbers). */
c24bdb02
KS
20048 buildsym_compunit *builder = cu->get_builder ();
20049 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20050
7ba99d21 20051 for (auto &fe : lh->file_names ())
cb1df416 20052 {
804d2729 20053 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20054 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20055 {
c24bdb02 20056 builder->get_current_subfile ()->symtab
804d2729 20057 = allocate_symtab (cust,
c24bdb02 20058 builder->get_current_subfile ()->name);
43f3e411 20059 }
c24bdb02 20060 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20061 }
20062 }
c906108c
SS
20063}
20064
20065/* Start a subfile for DWARF. FILENAME is the name of the file and
20066 DIRNAME the name of the source directory which contains FILENAME
4d663531 20067 or NULL if not known.
c906108c
SS
20068 This routine tries to keep line numbers from identical absolute and
20069 relative file names in a common subfile.
20070
20071 Using the `list' example from the GDB testsuite, which resides in
20072 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20073 of /srcdir/list0.c yields the following debugging information for list0.c:
20074
c5aa993b 20075 DW_AT_name: /srcdir/list0.c
4d663531 20076 DW_AT_comp_dir: /compdir
357e46e7 20077 files.files[0].name: list0.h
c5aa993b 20078 files.files[0].dir: /srcdir
357e46e7 20079 files.files[1].name: list0.c
c5aa993b 20080 files.files[1].dir: /srcdir
c906108c
SS
20081
20082 The line number information for list0.c has to end up in a single
4f1520fb
FR
20083 subfile, so that `break /srcdir/list0.c:1' works as expected.
20084 start_subfile will ensure that this happens provided that we pass the
20085 concatenation of files.files[1].dir and files.files[1].name as the
20086 subfile's name. */
c906108c
SS
20087
20088static void
804d2729
TT
20089dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20090 const char *dirname)
c906108c 20091{
43816ebc 20092 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20093
4d663531 20094 /* In order not to lose the line information directory,
4f1520fb
FR
20095 we concatenate it to the filename when it makes sense.
20096 Note that the Dwarf3 standard says (speaking of filenames in line
20097 information): ``The directory index is ignored for file names
20098 that represent full path names''. Thus ignoring dirname in the
20099 `else' branch below isn't an issue. */
c906108c 20100
d5166ae1 20101 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20102 {
43816ebc
TT
20103 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20104 filename = copy.get ();
d521ce57 20105 }
c906108c 20106
c24bdb02 20107 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20108}
20109
804d2729
TT
20110/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20111 buildsym_compunit constructor. */
f4dc4d17 20112
c24bdb02
KS
20113struct compunit_symtab *
20114dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20115 CORE_ADDR low_pc)
f4dc4d17 20116{
c24bdb02 20117 gdb_assert (m_builder == nullptr);
43f3e411 20118
c24bdb02
KS
20119 m_builder.reset (new struct buildsym_compunit
20120 (per_cu->dwarf2_per_objfile->objfile,
20121 name, comp_dir, language, low_pc));
93b8bea4 20122
c24bdb02 20123 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20124
c24bdb02
KS
20125 get_builder ()->record_debugformat ("DWARF 2");
20126 get_builder ()->record_producer (producer);
f4dc4d17 20127
c24bdb02 20128 processing_has_namespace_info = false;
43f3e411 20129
c24bdb02 20130 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20131}
20132
4c2df51b
DJ
20133static void
20134var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20135 struct dwarf2_cu *cu)
4c2df51b 20136{
518817b3 20137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20138 struct comp_unit_head *cu_header = &cu->header;
20139
4c2df51b
DJ
20140 /* NOTE drow/2003-01-30: There used to be a comment and some special
20141 code here to turn a symbol with DW_AT_external and a
20142 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20143 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20144 with some versions of binutils) where shared libraries could have
20145 relocations against symbols in their debug information - the
20146 minimal symbol would have the right address, but the debug info
20147 would not. It's no longer necessary, because we will explicitly
20148 apply relocations when we read in the debug information now. */
20149
20150 /* A DW_AT_location attribute with no contents indicates that a
20151 variable has been optimized away. */
4fc6c0d5 20152 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20153 {
f1e6e072 20154 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20155 return;
20156 }
20157
20158 /* Handle one degenerate form of location expression specially, to
20159 preserve GDB's previous behavior when section offsets are
336d760d
AT
20160 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20161 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20162
4fc6c0d5 20163 if (attr->form_is_block ()
3019eac3
DE
20164 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20165 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20166 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20167 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20168 && (DW_BLOCK (attr)->size
20169 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20170 {
891d2f0b 20171 unsigned int dummy;
4c2df51b 20172
3019eac3 20173 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20174 SET_SYMBOL_VALUE_ADDRESS
20175 (sym, cu->header.read_address (objfile->obfd,
20176 DW_BLOCK (attr)->data + 1,
20177 &dummy));
3019eac3 20178 else
38583298
TT
20179 SET_SYMBOL_VALUE_ADDRESS
20180 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20181 &dummy));
f1e6e072 20182 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20183 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20184 SET_SYMBOL_VALUE_ADDRESS
20185 (sym,
20186 SYMBOL_VALUE_ADDRESS (sym)
20187 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20188 return;
20189 }
20190
20191 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20192 expression evaluator, and use LOC_COMPUTED only when necessary
20193 (i.e. when the value of a register or memory location is
20194 referenced, or a thread-local block, etc.). Then again, it might
20195 not be worthwhile. I'm assuming that it isn't unless performance
20196 or memory numbers show me otherwise. */
20197
f1e6e072 20198 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20199
f1e6e072 20200 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20201 cu->has_loclist = true;
4c2df51b
DJ
20202}
20203
c906108c
SS
20204/* Given a pointer to a DWARF information entry, figure out if we need
20205 to make a symbol table entry for it, and if so, create a new entry
20206 and return a pointer to it.
20207 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20208 used the passed type.
20209 If SPACE is not NULL, use it to hold the new symbol. If it is
20210 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20211
20212static struct symbol *
5e2db402
TT
20213new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20214 struct symbol *space)
c906108c 20215{
518817b3
SM
20216 struct dwarf2_per_objfile *dwarf2_per_objfile
20217 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20218 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20220 struct symbol *sym = NULL;
15d034d0 20221 const char *name;
c906108c
SS
20222 struct attribute *attr = NULL;
20223 struct attribute *attr2 = NULL;
e142c38c 20224 CORE_ADDR baseaddr;
e37fd15a
SW
20225 struct pending **list_to_add = NULL;
20226
edb3359d 20227 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20228
b3b3bada 20229 baseaddr = objfile->text_section_offset ();
c906108c 20230
94af9270 20231 name = dwarf2_name (die, cu);
c906108c
SS
20232 if (name)
20233 {
94af9270 20234 const char *linkagename;
34eaf542 20235 int suppress_add = 0;
94af9270 20236
34eaf542
TT
20237 if (space)
20238 sym = space;
20239 else
e623cf5d 20240 sym = allocate_symbol (objfile);
c906108c 20241 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20242
20243 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20244 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20245 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20246 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20247
f55ee35c
JK
20248 /* Fortran does not have mangling standard and the mangling does differ
20249 between gfortran, iFort etc. */
20250 if (cu->language == language_fortran
468c0cbb
CB
20251 && symbol_get_demangled_name (sym) == NULL)
20252 symbol_set_demangled_name (sym,
cfc594ee 20253 dwarf2_full_name (name, die, cu),
29df156d 20254 NULL);
f55ee35c 20255
c906108c 20256 /* Default assumptions.
c5aa993b 20257 Use the passed type or decode it from the die. */
176620f1 20258 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20259 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20260 if (type != NULL)
20261 SYMBOL_TYPE (sym) = type;
20262 else
e7c27a73 20263 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20264 attr = dwarf2_attr (die,
20265 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20266 cu);
435d3d88 20267 if (attr != nullptr)
c906108c
SS
20268 {
20269 SYMBOL_LINE (sym) = DW_UNSND (attr);
20270 }
cb1df416 20271
edb3359d
DJ
20272 attr = dwarf2_attr (die,
20273 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20274 cu);
435d3d88 20275 if (attr != nullptr)
cb1df416 20276 {
ecfb656c 20277 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20278 struct file_entry *fe;
9a619af0 20279
ecfb656c
PA
20280 if (cu->line_header != NULL)
20281 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20282 else
20283 fe = NULL;
20284
20285 if (fe == NULL)
b98664d3 20286 complaint (_("file index out of range"));
8c43009f
PA
20287 else
20288 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20289 }
20290
c906108c
SS
20291 switch (die->tag)
20292 {
20293 case DW_TAG_label:
e142c38c 20294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20295 if (attr != nullptr)
3e29f34a
MR
20296 {
20297 CORE_ADDR addr;
20298
cd6c91b4 20299 addr = attr->value_as_address ();
3e29f34a 20300 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20301 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20302 }
0f5238ed
TT
20303 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20304 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20305 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20306 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20307 break;
20308 case DW_TAG_subprogram:
20309 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20310 finish_block. */
f1e6e072 20311 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20312 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20313 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20314 || cu->language == language_ada
20315 || cu->language == language_fortran)
c906108c 20316 {
2cfa0c8d 20317 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20318 Ada and Fortran subprograms, whether marked external or
20319 not, are always stored as a global symbol, because we want
20320 to be able to access them globally. For instance, we want
20321 to be able to break on a nested subprogram without having
20322 to specify the context. */
c24bdb02 20323 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20324 }
20325 else
20326 {
e37fd15a 20327 list_to_add = cu->list_in_scope;
c906108c
SS
20328 }
20329 break;
edb3359d
DJ
20330 case DW_TAG_inlined_subroutine:
20331 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20332 finish_block. */
f1e6e072 20333 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20334 SYMBOL_INLINED (sym) = 1;
481860b3 20335 list_to_add = cu->list_in_scope;
edb3359d 20336 break;
34eaf542
TT
20337 case DW_TAG_template_value_param:
20338 suppress_add = 1;
20339 /* Fall through. */
72929c62 20340 case DW_TAG_constant:
c906108c 20341 case DW_TAG_variable:
254e6b9e 20342 case DW_TAG_member:
0963b4bd
MS
20343 /* Compilation with minimal debug info may result in
20344 variables with missing type entries. Change the
20345 misleading `void' type to something sensible. */
c906108c 20346 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20347 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20348
e142c38c 20349 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20350 /* In the case of DW_TAG_member, we should only be called for
20351 static const members. */
20352 if (die->tag == DW_TAG_member)
20353 {
3863f96c
DE
20354 /* dwarf2_add_field uses die_is_declaration,
20355 so we do the same. */
254e6b9e
DE
20356 gdb_assert (die_is_declaration (die, cu));
20357 gdb_assert (attr);
20358 }
435d3d88 20359 if (attr != nullptr)
c906108c 20360 {
e7c27a73 20361 dwarf2_const_value (attr, sym, cu);
e142c38c 20362 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20363 if (!suppress_add)
34eaf542
TT
20364 {
20365 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20366 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20367 else
e37fd15a 20368 list_to_add = cu->list_in_scope;
34eaf542 20369 }
c906108c
SS
20370 break;
20371 }
e142c38c 20372 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20373 if (attr != nullptr)
c906108c 20374 {
e7c27a73 20375 var_decode_location (attr, sym, cu);
e142c38c 20376 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20377
20378 /* Fortran explicitly imports any global symbols to the local
20379 scope by DW_TAG_common_block. */
20380 if (cu->language == language_fortran && die->parent
20381 && die->parent->tag == DW_TAG_common_block)
20382 attr2 = NULL;
20383
caac4577
JG
20384 if (SYMBOL_CLASS (sym) == LOC_STATIC
20385 && SYMBOL_VALUE_ADDRESS (sym) == 0
20386 && !dwarf2_per_objfile->has_section_at_zero)
20387 {
20388 /* When a static variable is eliminated by the linker,
20389 the corresponding debug information is not stripped
20390 out, but the variable address is set to null;
20391 do not add such variables into symbol table. */
20392 }
20393 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20394 {
4b610737
TT
20395 if (SYMBOL_CLASS (sym) == LOC_STATIC
20396 && (objfile->flags & OBJF_MAINLINE) == 0
20397 && dwarf2_per_objfile->can_copy)
20398 {
20399 /* A global static variable might be subject to
20400 copy relocation. We first check for a local
20401 minsym, though, because maybe the symbol was
20402 marked hidden, in which case this would not
20403 apply. */
20404 bound_minimal_symbol found
20405 = (lookup_minimal_symbol_linkage
987012b8 20406 (sym->linkage_name (), objfile));
4b610737
TT
20407 if (found.minsym != nullptr)
20408 sym->maybe_copied = 1;
20409 }
f55ee35c 20410
1c809c68
TT
20411 /* A variable with DW_AT_external is never static,
20412 but it may be block-scoped. */
804d2729 20413 list_to_add
c24bdb02
KS
20414 = ((cu->list_in_scope
20415 == cu->get_builder ()->get_file_symbols ())
20416 ? cu->get_builder ()->get_global_symbols ()
804d2729 20417 : cu->list_in_scope);
1c809c68 20418 }
c906108c 20419 else
e37fd15a 20420 list_to_add = cu->list_in_scope;
c906108c
SS
20421 }
20422 else
20423 {
20424 /* We do not know the address of this symbol.
c5aa993b
JM
20425 If it is an external symbol and we have type information
20426 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20427 The address of the variable will then be determined from
20428 the minimal symbol table whenever the variable is
20429 referenced. */
e142c38c 20430 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20431
20432 /* Fortran explicitly imports any global symbols to the local
20433 scope by DW_TAG_common_block. */
20434 if (cu->language == language_fortran && die->parent
20435 && die->parent->tag == DW_TAG_common_block)
20436 {
20437 /* SYMBOL_CLASS doesn't matter here because
20438 read_common_block is going to reset it. */
20439 if (!suppress_add)
20440 list_to_add = cu->list_in_scope;
20441 }
20442 else if (attr2 && (DW_UNSND (attr2) != 0)
20443 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20444 {
0fe7935b
DJ
20445 /* A variable with DW_AT_external is never static, but it
20446 may be block-scoped. */
804d2729 20447 list_to_add
c24bdb02
KS
20448 = ((cu->list_in_scope
20449 == cu->get_builder ()->get_file_symbols ())
20450 ? cu->get_builder ()->get_global_symbols ()
804d2729 20451 : cu->list_in_scope);
0fe7935b 20452
f1e6e072 20453 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20454 }
442ddf59
JK
20455 else if (!die_is_declaration (die, cu))
20456 {
20457 /* Use the default LOC_OPTIMIZED_OUT class. */
20458 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20459 if (!suppress_add)
20460 list_to_add = cu->list_in_scope;
442ddf59 20461 }
c906108c
SS
20462 }
20463 break;
20464 case DW_TAG_formal_parameter:
a60f3166
TT
20465 {
20466 /* If we are inside a function, mark this as an argument. If
20467 not, we might be looking at an argument to an inlined function
20468 when we do not have enough information to show inlined frames;
20469 pretend it's a local variable in that case so that the user can
20470 still see it. */
804d2729 20471 struct context_stack *curr
c24bdb02 20472 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20473 if (curr != nullptr && curr->name != nullptr)
20474 SYMBOL_IS_ARGUMENT (sym) = 1;
20475 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20476 if (attr != nullptr)
a60f3166
TT
20477 {
20478 var_decode_location (attr, sym, cu);
20479 }
20480 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20481 if (attr != nullptr)
a60f3166
TT
20482 {
20483 dwarf2_const_value (attr, sym, cu);
20484 }
f346a30d 20485
a60f3166
TT
20486 list_to_add = cu->list_in_scope;
20487 }
c906108c
SS
20488 break;
20489 case DW_TAG_unspecified_parameters:
20490 /* From varargs functions; gdb doesn't seem to have any
20491 interest in this information, so just ignore it for now.
20492 (FIXME?) */
20493 break;
34eaf542
TT
20494 case DW_TAG_template_type_param:
20495 suppress_add = 1;
20496 /* Fall through. */
c906108c 20497 case DW_TAG_class_type:
680b30c7 20498 case DW_TAG_interface_type:
c906108c
SS
20499 case DW_TAG_structure_type:
20500 case DW_TAG_union_type:
72019c9c 20501 case DW_TAG_set_type:
c906108c 20502 case DW_TAG_enumeration_type:
f1e6e072 20503 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20504 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20505
63d06c5c 20506 {
9c37b5ae 20507 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20508 really ever be static objects: otherwise, if you try
20509 to, say, break of a class's method and you're in a file
20510 which doesn't mention that class, it won't work unless
20511 the check for all static symbols in lookup_symbol_aux
20512 saves you. See the OtherFileClass tests in
20513 gdb.c++/namespace.exp. */
20514
e37fd15a 20515 if (!suppress_add)
34eaf542 20516 {
c24bdb02 20517 buildsym_compunit *builder = cu->get_builder ();
804d2729 20518 list_to_add
c24bdb02 20519 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20520 && cu->language == language_cplus
c24bdb02 20521 ? builder->get_global_symbols ()
804d2729 20522 : cu->list_in_scope);
63d06c5c 20523
64382290 20524 /* The semantics of C++ state that "struct foo {
9c37b5ae 20525 ... }" also defines a typedef for "foo". */
64382290 20526 if (cu->language == language_cplus
45280282 20527 || cu->language == language_ada
c44af4eb
TT
20528 || cu->language == language_d
20529 || cu->language == language_rust)
64382290
TT
20530 {
20531 /* The symbol's name is already allocated along
20532 with this objfile, so we don't need to
20533 duplicate it for the type. */
20534 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20535 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20536 }
63d06c5c
DC
20537 }
20538 }
c906108c
SS
20539 break;
20540 case DW_TAG_typedef:
f1e6e072 20541 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20543 list_to_add = cu->list_in_scope;
63d06c5c 20544 break;
c906108c 20545 case DW_TAG_base_type:
a02abb62 20546 case DW_TAG_subrange_type:
f1e6e072 20547 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20548 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20549 list_to_add = cu->list_in_scope;
c906108c
SS
20550 break;
20551 case DW_TAG_enumerator:
e142c38c 20552 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20553 if (attr != nullptr)
c906108c 20554 {
e7c27a73 20555 dwarf2_const_value (attr, sym, cu);
c906108c 20556 }
63d06c5c
DC
20557 {
20558 /* NOTE: carlton/2003-11-10: See comment above in the
20559 DW_TAG_class_type, etc. block. */
20560
804d2729 20561 list_to_add
c24bdb02 20562 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20563 && cu->language == language_cplus
c24bdb02 20564 ? cu->get_builder ()->get_global_symbols ()
804d2729 20565 : cu->list_in_scope);
63d06c5c 20566 }
c906108c 20567 break;
74921315 20568 case DW_TAG_imported_declaration:
5c4e30ca 20569 case DW_TAG_namespace:
f1e6e072 20570 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20571 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20572 break;
530e8392
KB
20573 case DW_TAG_module:
20574 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20575 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20576 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20577 break;
4357ac6c 20578 case DW_TAG_common_block:
f1e6e072 20579 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20580 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20581 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20582 break;
c906108c
SS
20583 default:
20584 /* Not a tag we recognize. Hopefully we aren't processing
20585 trash data, but since we must specifically ignore things
20586 we don't recognize, there is nothing else we should do at
0963b4bd 20587 this point. */
b98664d3 20588 complaint (_("unsupported tag: '%s'"),
4d3c2250 20589 dwarf_tag_name (die->tag));
c906108c
SS
20590 break;
20591 }
df8a16a1 20592
e37fd15a
SW
20593 if (suppress_add)
20594 {
20595 sym->hash_next = objfile->template_symbols;
20596 objfile->template_symbols = sym;
20597 list_to_add = NULL;
20598 }
20599
20600 if (list_to_add != NULL)
d3cb6808 20601 add_symbol_to_list (sym, list_to_add);
e37fd15a 20602
df8a16a1
DJ
20603 /* For the benefit of old versions of GCC, check for anonymous
20604 namespaces based on the demangled name. */
4d4ec4e5 20605 if (!cu->processing_has_namespace_info
94af9270 20606 && cu->language == language_cplus)
c24bdb02 20607 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20608 }
20609 return (sym);
20610}
20611
98bfdba5
PA
20612/* Given an attr with a DW_FORM_dataN value in host byte order,
20613 zero-extend it as appropriate for the symbol's type. The DWARF
20614 standard (v4) is not entirely clear about the meaning of using
20615 DW_FORM_dataN for a constant with a signed type, where the type is
20616 wider than the data. The conclusion of a discussion on the DWARF
20617 list was that this is unspecified. We choose to always zero-extend
20618 because that is the interpretation long in use by GCC. */
c906108c 20619
98bfdba5 20620static gdb_byte *
ff39bb5e 20621dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20622 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20623{
518817b3 20624 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20625 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20626 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20627 LONGEST l = DW_UNSND (attr);
20628
20629 if (bits < sizeof (*value) * 8)
20630 {
20631 l &= ((LONGEST) 1 << bits) - 1;
20632 *value = l;
20633 }
20634 else if (bits == sizeof (*value) * 8)
20635 *value = l;
20636 else
20637 {
224c3ddb 20638 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20639 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20640 return bytes;
20641 }
20642
20643 return NULL;
20644}
20645
20646/* Read a constant value from an attribute. Either set *VALUE, or if
20647 the value does not fit in *VALUE, set *BYTES - either already
20648 allocated on the objfile obstack, or newly allocated on OBSTACK,
20649 or, set *BATON, if we translated the constant to a location
20650 expression. */
20651
20652static void
ff39bb5e 20653dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20654 const char *name, struct obstack *obstack,
20655 struct dwarf2_cu *cu,
d521ce57 20656 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20657 struct dwarf2_locexpr_baton **baton)
20658{
518817b3 20659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20660 struct comp_unit_head *cu_header = &cu->header;
c906108c 20661 struct dwarf_block *blk;
98bfdba5
PA
20662 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20663 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20664
20665 *value = 0;
20666 *bytes = NULL;
20667 *baton = NULL;
c906108c
SS
20668
20669 switch (attr->form)
20670 {
20671 case DW_FORM_addr:
336d760d 20672 case DW_FORM_addrx:
3019eac3 20673 case DW_FORM_GNU_addr_index:
ac56253d 20674 {
ac56253d
TT
20675 gdb_byte *data;
20676
98bfdba5
PA
20677 if (TYPE_LENGTH (type) != cu_header->addr_size)
20678 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20679 cu_header->addr_size,
98bfdba5 20680 TYPE_LENGTH (type));
ac56253d
TT
20681 /* Symbols of this form are reasonably rare, so we just
20682 piggyback on the existing location code rather than writing
20683 a new implementation of symbol_computed_ops. */
8d749320 20684 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20685 (*baton)->per_cu = cu->per_cu;
20686 gdb_assert ((*baton)->per_cu);
ac56253d 20687
98bfdba5 20688 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20689 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20690 (*baton)->data = data;
ac56253d
TT
20691
20692 data[0] = DW_OP_addr;
20693 store_unsigned_integer (&data[1], cu_header->addr_size,
20694 byte_order, DW_ADDR (attr));
20695 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20696 }
c906108c 20697 break;
4ac36638 20698 case DW_FORM_string:
93b5768b 20699 case DW_FORM_strp:
cf532bd1 20700 case DW_FORM_strx:
3019eac3 20701 case DW_FORM_GNU_str_index:
36586728 20702 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20703 /* DW_STRING is already allocated on the objfile obstack, point
20704 directly to it. */
d521ce57 20705 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20706 break;
c906108c
SS
20707 case DW_FORM_block1:
20708 case DW_FORM_block2:
20709 case DW_FORM_block4:
20710 case DW_FORM_block:
2dc7f7b3 20711 case DW_FORM_exprloc:
0224619f 20712 case DW_FORM_data16:
c906108c 20713 blk = DW_BLOCK (attr);
98bfdba5
PA
20714 if (TYPE_LENGTH (type) != blk->size)
20715 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20716 TYPE_LENGTH (type));
20717 *bytes = blk->data;
c906108c 20718 break;
2df3850c
JM
20719
20720 /* The DW_AT_const_value attributes are supposed to carry the
20721 symbol's value "represented as it would be on the target
20722 architecture." By the time we get here, it's already been
20723 converted to host endianness, so we just need to sign- or
20724 zero-extend it as appropriate. */
20725 case DW_FORM_data1:
3aef2284 20726 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20727 break;
c906108c 20728 case DW_FORM_data2:
3aef2284 20729 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20730 break;
c906108c 20731 case DW_FORM_data4:
3aef2284 20732 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20733 break;
c906108c 20734 case DW_FORM_data8:
3aef2284 20735 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20736 break;
20737
c906108c 20738 case DW_FORM_sdata:
663c44ac 20739 case DW_FORM_implicit_const:
98bfdba5 20740 *value = DW_SND (attr);
2df3850c
JM
20741 break;
20742
c906108c 20743 case DW_FORM_udata:
98bfdba5 20744 *value = DW_UNSND (attr);
c906108c 20745 break;
2df3850c 20746
c906108c 20747 default:
b98664d3 20748 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20749 dwarf_form_name (attr->form));
98bfdba5 20750 *value = 0;
c906108c
SS
20751 break;
20752 }
20753}
20754
2df3850c 20755
98bfdba5
PA
20756/* Copy constant value from an attribute to a symbol. */
20757
2df3850c 20758static void
ff39bb5e 20759dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20760 struct dwarf2_cu *cu)
2df3850c 20761{
518817b3 20762 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20763 LONGEST value;
d521ce57 20764 const gdb_byte *bytes;
98bfdba5 20765 struct dwarf2_locexpr_baton *baton;
2df3850c 20766
98bfdba5 20767 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 20768 sym->print_name (),
98bfdba5
PA
20769 &objfile->objfile_obstack, cu,
20770 &value, &bytes, &baton);
2df3850c 20771
98bfdba5
PA
20772 if (baton != NULL)
20773 {
98bfdba5 20774 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20775 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20776 }
20777 else if (bytes != NULL)
20778 {
20779 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20780 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20781 }
20782 else
20783 {
20784 SYMBOL_VALUE (sym) = value;
f1e6e072 20785 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20786 }
2df3850c
JM
20787}
20788
c906108c
SS
20789/* Return the type of the die in question using its DW_AT_type attribute. */
20790
20791static struct type *
e7c27a73 20792die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20793{
c906108c 20794 struct attribute *type_attr;
c906108c 20795
e142c38c 20796 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20797 if (!type_attr)
20798 {
518817b3 20799 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20800 /* A missing DW_AT_type represents a void type. */
518817b3 20801 return objfile_type (objfile)->builtin_void;
c906108c 20802 }
348e048f 20803
673bfd45 20804 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20805}
20806
b4ba55a1
JB
20807/* True iff CU's producer generates GNAT Ada auxiliary information
20808 that allows to find parallel types through that information instead
20809 of having to do expensive parallel lookups by type name. */
20810
20811static int
20812need_gnat_info (struct dwarf2_cu *cu)
20813{
de4cb04a
JB
20814 /* Assume that the Ada compiler was GNAT, which always produces
20815 the auxiliary information. */
20816 return (cu->language == language_ada);
b4ba55a1
JB
20817}
20818
b4ba55a1
JB
20819/* Return the auxiliary type of the die in question using its
20820 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20821 attribute is not present. */
20822
20823static struct type *
20824die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20825{
b4ba55a1 20826 struct attribute *type_attr;
b4ba55a1
JB
20827
20828 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20829 if (!type_attr)
20830 return NULL;
20831
673bfd45 20832 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20833}
20834
20835/* If DIE has a descriptive_type attribute, then set the TYPE's
20836 descriptive type accordingly. */
20837
20838static void
20839set_descriptive_type (struct type *type, struct die_info *die,
20840 struct dwarf2_cu *cu)
20841{
20842 struct type *descriptive_type = die_descriptive_type (die, cu);
20843
20844 if (descriptive_type)
20845 {
20846 ALLOCATE_GNAT_AUX_TYPE (type);
20847 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20848 }
20849}
20850
c906108c
SS
20851/* Return the containing type of the die in question using its
20852 DW_AT_containing_type attribute. */
20853
20854static struct type *
e7c27a73 20855die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20856{
c906108c 20857 struct attribute *type_attr;
518817b3 20858 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20859
e142c38c 20860 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20861 if (!type_attr)
20862 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 20863 "[in module %s]"), objfile_name (objfile));
33ac96f0 20864
673bfd45 20865 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20866}
20867
ac9ec31b
DE
20868/* Return an error marker type to use for the ill formed type in DIE/CU. */
20869
20870static struct type *
20871build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20872{
518817b3
SM
20873 struct dwarf2_per_objfile *dwarf2_per_objfile
20874 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 20875 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 20876 char *saved;
ac9ec31b 20877
528e1572
SM
20878 std::string message
20879 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20880 objfile_name (objfile),
20881 sect_offset_str (cu->header.sect_off),
20882 sect_offset_str (die->sect_off));
efba19b0 20883 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 20884
19f392bc 20885 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20886}
20887
673bfd45 20888/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20889 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20890 DW_AT_containing_type.
673bfd45
DE
20891 If there is no type substitute an error marker. */
20892
c906108c 20893static struct type *
ff39bb5e 20894lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20895 struct dwarf2_cu *cu)
c906108c 20896{
518817b3
SM
20897 struct dwarf2_per_objfile *dwarf2_per_objfile
20898 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20899 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
20900 struct type *this_type;
20901
ac9ec31b
DE
20902 gdb_assert (attr->name == DW_AT_type
20903 || attr->name == DW_AT_GNAT_descriptive_type
20904 || attr->name == DW_AT_containing_type);
20905
673bfd45
DE
20906 /* First see if we have it cached. */
20907
36586728
TT
20908 if (attr->form == DW_FORM_GNU_ref_alt)
20909 {
20910 struct dwarf2_per_cu_data *per_cu;
0826b30a 20911 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 20912
ed2dc618
SM
20913 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20914 dwarf2_per_objfile);
9c541725 20915 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20916 }
cd6c91b4 20917 else if (attr->form_is_ref ())
673bfd45 20918 {
0826b30a 20919 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 20920
9c541725 20921 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20922 }
55f1336d 20923 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20924 {
ac9ec31b 20925 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20926
ac9ec31b 20927 return get_signatured_type (die, signature, cu);
673bfd45
DE
20928 }
20929 else
20930 {
b98664d3 20931 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
20932 " at %s [in module %s]"),
20933 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 20934 objfile_name (objfile));
ac9ec31b 20935 return build_error_marker_type (cu, die);
673bfd45
DE
20936 }
20937
20938 /* If not cached we need to read it in. */
20939
20940 if (this_type == NULL)
20941 {
ac9ec31b 20942 struct die_info *type_die = NULL;
673bfd45
DE
20943 struct dwarf2_cu *type_cu = cu;
20944
cd6c91b4 20945 if (attr->form_is_ref ())
ac9ec31b
DE
20946 type_die = follow_die_ref (die, attr, &type_cu);
20947 if (type_die == NULL)
20948 return build_error_marker_type (cu, die);
20949 /* If we find the type now, it's probably because the type came
3019eac3
DE
20950 from an inter-CU reference and the type's CU got expanded before
20951 ours. */
ac9ec31b 20952 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20953 }
20954
20955 /* If we still don't have a type use an error marker. */
20956
20957 if (this_type == NULL)
ac9ec31b 20958 return build_error_marker_type (cu, die);
673bfd45 20959
f792889a 20960 return this_type;
c906108c
SS
20961}
20962
673bfd45
DE
20963/* Return the type in DIE, CU.
20964 Returns NULL for invalid types.
20965
02142a6c 20966 This first does a lookup in die_type_hash,
673bfd45
DE
20967 and only reads the die in if necessary.
20968
20969 NOTE: This can be called when reading in partial or full symbols. */
20970
f792889a 20971static struct type *
e7c27a73 20972read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20973{
f792889a
DJ
20974 struct type *this_type;
20975
20976 this_type = get_die_type (die, cu);
20977 if (this_type)
20978 return this_type;
20979
673bfd45
DE
20980 return read_type_die_1 (die, cu);
20981}
20982
20983/* Read the type in DIE, CU.
20984 Returns NULL for invalid types. */
20985
20986static struct type *
20987read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20988{
20989 struct type *this_type = NULL;
20990
c906108c
SS
20991 switch (die->tag)
20992 {
20993 case DW_TAG_class_type:
680b30c7 20994 case DW_TAG_interface_type:
c906108c
SS
20995 case DW_TAG_structure_type:
20996 case DW_TAG_union_type:
f792889a 20997 this_type = read_structure_type (die, cu);
c906108c
SS
20998 break;
20999 case DW_TAG_enumeration_type:
f792889a 21000 this_type = read_enumeration_type (die, cu);
c906108c
SS
21001 break;
21002 case DW_TAG_subprogram:
21003 case DW_TAG_subroutine_type:
edb3359d 21004 case DW_TAG_inlined_subroutine:
f792889a 21005 this_type = read_subroutine_type (die, cu);
c906108c
SS
21006 break;
21007 case DW_TAG_array_type:
f792889a 21008 this_type = read_array_type (die, cu);
c906108c 21009 break;
72019c9c 21010 case DW_TAG_set_type:
f792889a 21011 this_type = read_set_type (die, cu);
72019c9c 21012 break;
c906108c 21013 case DW_TAG_pointer_type:
f792889a 21014 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21015 break;
21016 case DW_TAG_ptr_to_member_type:
f792889a 21017 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21018 break;
21019 case DW_TAG_reference_type:
4297a3f0
AV
21020 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21021 break;
21022 case DW_TAG_rvalue_reference_type:
21023 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21024 break;
21025 case DW_TAG_const_type:
f792889a 21026 this_type = read_tag_const_type (die, cu);
c906108c
SS
21027 break;
21028 case DW_TAG_volatile_type:
f792889a 21029 this_type = read_tag_volatile_type (die, cu);
c906108c 21030 break;
06d66ee9
TT
21031 case DW_TAG_restrict_type:
21032 this_type = read_tag_restrict_type (die, cu);
21033 break;
c906108c 21034 case DW_TAG_string_type:
f792889a 21035 this_type = read_tag_string_type (die, cu);
c906108c
SS
21036 break;
21037 case DW_TAG_typedef:
f792889a 21038 this_type = read_typedef (die, cu);
c906108c 21039 break;
a02abb62 21040 case DW_TAG_subrange_type:
f792889a 21041 this_type = read_subrange_type (die, cu);
a02abb62 21042 break;
c906108c 21043 case DW_TAG_base_type:
f792889a 21044 this_type = read_base_type (die, cu);
c906108c 21045 break;
81a17f79 21046 case DW_TAG_unspecified_type:
f792889a 21047 this_type = read_unspecified_type (die, cu);
81a17f79 21048 break;
0114d602
DJ
21049 case DW_TAG_namespace:
21050 this_type = read_namespace_type (die, cu);
21051 break;
f55ee35c
JK
21052 case DW_TAG_module:
21053 this_type = read_module_type (die, cu);
21054 break;
a2c2acaf
MW
21055 case DW_TAG_atomic_type:
21056 this_type = read_tag_atomic_type (die, cu);
21057 break;
c906108c 21058 default:
b98664d3 21059 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21060 dwarf_tag_name (die->tag));
c906108c
SS
21061 break;
21062 }
63d06c5c 21063
f792889a 21064 return this_type;
63d06c5c
DC
21065}
21066
abc72ce4
DE
21067/* See if we can figure out if the class lives in a namespace. We do
21068 this by looking for a member function; its demangled name will
21069 contain namespace info, if there is any.
21070 Return the computed name or NULL.
21071 Space for the result is allocated on the objfile's obstack.
21072 This is the full-die version of guess_partial_die_structure_name.
21073 In this case we know DIE has no useful parent. */
21074
43816ebc 21075static const char *
abc72ce4
DE
21076guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21077{
21078 struct die_info *spec_die;
21079 struct dwarf2_cu *spec_cu;
21080 struct die_info *child;
518817b3 21081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21082
21083 spec_cu = cu;
21084 spec_die = die_specification (die, &spec_cu);
21085 if (spec_die != NULL)
21086 {
21087 die = spec_die;
21088 cu = spec_cu;
21089 }
21090
21091 for (child = die->child;
21092 child != NULL;
21093 child = child->sibling)
21094 {
21095 if (child->tag == DW_TAG_subprogram)
21096 {
73b9be8b 21097 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21098
7d45c7c3 21099 if (linkage_name != NULL)
abc72ce4 21100 {
43816ebc
TT
21101 gdb::unique_xmalloc_ptr<char> actual_name
21102 (language_class_name_from_physname (cu->language_defn,
21103 linkage_name));
21104 const char *name = NULL;
abc72ce4
DE
21105
21106 if (actual_name != NULL)
21107 {
15d034d0 21108 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21109
21110 if (die_name != NULL
43816ebc 21111 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21112 {
21113 /* Strip off the class name from the full name.
21114 We want the prefix. */
21115 int die_name_len = strlen (die_name);
43816ebc
TT
21116 int actual_name_len = strlen (actual_name.get ());
21117 const char *ptr = actual_name.get ();
abc72ce4
DE
21118
21119 /* Test for '::' as a sanity check. */
21120 if (actual_name_len > die_name_len + 2
43816ebc 21121 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21122 name = obstack_strndup (
e3b94546 21123 &objfile->per_bfd->storage_obstack,
43816ebc 21124 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21125 }
21126 }
abc72ce4
DE
21127 return name;
21128 }
21129 }
21130 }
21131
21132 return NULL;
21133}
21134
96408a79
SA
21135/* GCC might emit a nameless typedef that has a linkage name. Determine the
21136 prefix part in such case. See
21137 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21138
a121b7c1 21139static const char *
96408a79
SA
21140anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21141{
21142 struct attribute *attr;
e6a959d6 21143 const char *base;
96408a79
SA
21144
21145 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21146 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21147 return NULL;
21148
7d45c7c3 21149 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21150 return NULL;
21151
73b9be8b 21152 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21153 if (attr == NULL || DW_STRING (attr) == NULL)
21154 return NULL;
21155
21156 /* dwarf2_name had to be already called. */
21157 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21158
21159 /* Strip the base name, keep any leading namespaces/classes. */
21160 base = strrchr (DW_STRING (attr), ':');
21161 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21162 return "";
21163
518817b3 21164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21165 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21166 DW_STRING (attr),
21167 &base[-1] - DW_STRING (attr));
96408a79
SA
21168}
21169
fdde2d81 21170/* Return the name of the namespace/class that DIE is defined within,
0114d602 21171 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21172
0114d602
DJ
21173 For example, if we're within the method foo() in the following
21174 code:
21175
21176 namespace N {
21177 class C {
21178 void foo () {
21179 }
21180 };
21181 }
21182
21183 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21184
0d5cff50 21185static const char *
e142c38c 21186determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21187{
518817b3
SM
21188 struct dwarf2_per_objfile *dwarf2_per_objfile
21189 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21190 struct die_info *parent, *spec_die;
21191 struct dwarf2_cu *spec_cu;
21192 struct type *parent_type;
a121b7c1 21193 const char *retval;
63d06c5c 21194
9c37b5ae 21195 if (cu->language != language_cplus
c44af4eb
TT
21196 && cu->language != language_fortran && cu->language != language_d
21197 && cu->language != language_rust)
0114d602
DJ
21198 return "";
21199
96408a79
SA
21200 retval = anonymous_struct_prefix (die, cu);
21201 if (retval)
21202 return retval;
21203
0114d602
DJ
21204 /* We have to be careful in the presence of DW_AT_specification.
21205 For example, with GCC 3.4, given the code
21206
21207 namespace N {
21208 void foo() {
21209 // Definition of N::foo.
21210 }
21211 }
21212
21213 then we'll have a tree of DIEs like this:
21214
21215 1: DW_TAG_compile_unit
21216 2: DW_TAG_namespace // N
21217 3: DW_TAG_subprogram // declaration of N::foo
21218 4: DW_TAG_subprogram // definition of N::foo
21219 DW_AT_specification // refers to die #3
21220
21221 Thus, when processing die #4, we have to pretend that we're in
21222 the context of its DW_AT_specification, namely the contex of die
21223 #3. */
21224 spec_cu = cu;
21225 spec_die = die_specification (die, &spec_cu);
21226 if (spec_die == NULL)
21227 parent = die->parent;
21228 else
63d06c5c 21229 {
0114d602
DJ
21230 parent = spec_die->parent;
21231 cu = spec_cu;
63d06c5c 21232 }
0114d602
DJ
21233
21234 if (parent == NULL)
21235 return "";
98bfdba5
PA
21236 else if (parent->building_fullname)
21237 {
21238 const char *name;
21239 const char *parent_name;
21240
21241 /* It has been seen on RealView 2.2 built binaries,
21242 DW_TAG_template_type_param types actually _defined_ as
21243 children of the parent class:
21244
21245 enum E {};
21246 template class <class Enum> Class{};
21247 Class<enum E> class_e;
21248
21249 1: DW_TAG_class_type (Class)
21250 2: DW_TAG_enumeration_type (E)
21251 3: DW_TAG_enumerator (enum1:0)
21252 3: DW_TAG_enumerator (enum2:1)
21253 ...
21254 2: DW_TAG_template_type_param
21255 DW_AT_type DW_FORM_ref_udata (E)
21256
21257 Besides being broken debug info, it can put GDB into an
21258 infinite loop. Consider:
21259
21260 When we're building the full name for Class<E>, we'll start
21261 at Class, and go look over its template type parameters,
21262 finding E. We'll then try to build the full name of E, and
21263 reach here. We're now trying to build the full name of E,
21264 and look over the parent DIE for containing scope. In the
21265 broken case, if we followed the parent DIE of E, we'd again
21266 find Class, and once again go look at its template type
21267 arguments, etc., etc. Simply don't consider such parent die
21268 as source-level parent of this die (it can't be, the language
21269 doesn't allow it), and break the loop here. */
21270 name = dwarf2_name (die, cu);
21271 parent_name = dwarf2_name (parent, cu);
b98664d3 21272 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21273 name ? name : "<unknown>",
21274 parent_name ? parent_name : "<unknown>");
21275 return "";
21276 }
63d06c5c 21277 else
0114d602
DJ
21278 switch (parent->tag)
21279 {
63d06c5c 21280 case DW_TAG_namespace:
0114d602 21281 parent_type = read_type_die (parent, cu);
acebe513
UW
21282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21284 Work around this problem here. */
21285 if (cu->language == language_cplus
e86ca25f 21286 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21287 return "";
0114d602 21288 /* We give a name to even anonymous namespaces. */
e86ca25f 21289 return TYPE_NAME (parent_type);
63d06c5c 21290 case DW_TAG_class_type:
680b30c7 21291 case DW_TAG_interface_type:
63d06c5c 21292 case DW_TAG_structure_type:
0114d602 21293 case DW_TAG_union_type:
f55ee35c 21294 case DW_TAG_module:
0114d602 21295 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21296 if (TYPE_NAME (parent_type) != NULL)
21297 return TYPE_NAME (parent_type);
0114d602
DJ
21298 else
21299 /* An anonymous structure is only allowed non-static data
21300 members; no typedefs, no member functions, et cetera.
21301 So it does not need a prefix. */
21302 return "";
abc72ce4 21303 case DW_TAG_compile_unit:
95554aad 21304 case DW_TAG_partial_unit:
abc72ce4
DE
21305 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21306 if (cu->language == language_cplus
fd5866f6 21307 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21308 && die->child != NULL
21309 && (die->tag == DW_TAG_class_type
21310 || die->tag == DW_TAG_structure_type
21311 || die->tag == DW_TAG_union_type))
21312 {
43816ebc 21313 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21314 if (name != NULL)
21315 return name;
21316 }
21317 return "";
0a4b0913
AB
21318 case DW_TAG_subprogram:
21319 /* Nested subroutines in Fortran get a prefix with the name
21320 of the parent's subroutine. */
21321 if (cu->language == language_fortran)
21322 {
21323 if ((die->tag == DW_TAG_subprogram)
21324 && (dwarf2_name (parent, cu) != NULL))
21325 return dwarf2_name (parent, cu);
21326 }
21327 return determine_prefix (parent, cu);
3d567982
TT
21328 case DW_TAG_enumeration_type:
21329 parent_type = read_type_die (parent, cu);
21330 if (TYPE_DECLARED_CLASS (parent_type))
21331 {
e86ca25f
TT
21332 if (TYPE_NAME (parent_type) != NULL)
21333 return TYPE_NAME (parent_type);
3d567982
TT
21334 return "";
21335 }
21336 /* Fall through. */
63d06c5c 21337 default:
8176b9b8 21338 return determine_prefix (parent, cu);
63d06c5c 21339 }
63d06c5c
DC
21340}
21341
3e43a32a
MS
21342/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21343 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21344 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21345 an obconcat, otherwise allocate storage for the result. The CU argument is
21346 used to determine the language and hence, the appropriate separator. */
987504bb 21347
f55ee35c 21348#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21349
21350static char *
f55ee35c
JK
21351typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21352 int physname, struct dwarf2_cu *cu)
63d06c5c 21353{
f55ee35c 21354 const char *lead = "";
5c315b68 21355 const char *sep;
63d06c5c 21356
3e43a32a
MS
21357 if (suffix == NULL || suffix[0] == '\0'
21358 || prefix == NULL || prefix[0] == '\0')
987504bb 21359 sep = "";
45280282
IB
21360 else if (cu->language == language_d)
21361 {
21362 /* For D, the 'main' function could be defined in any module, but it
21363 should never be prefixed. */
21364 if (strcmp (suffix, "D main") == 0)
21365 {
21366 prefix = "";
21367 sep = "";
21368 }
21369 else
21370 sep = ".";
21371 }
f55ee35c
JK
21372 else if (cu->language == language_fortran && physname)
21373 {
21374 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21375 DW_AT_MIPS_linkage_name is preferred and used instead. */
21376
21377 lead = "__";
21378 sep = "_MOD_";
21379 }
987504bb
JJ
21380 else
21381 sep = "::";
63d06c5c 21382
6dd47d34
DE
21383 if (prefix == NULL)
21384 prefix = "";
21385 if (suffix == NULL)
21386 suffix = "";
21387
987504bb
JJ
21388 if (obs == NULL)
21389 {
3e43a32a 21390 char *retval
224c3ddb
SM
21391 = ((char *)
21392 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21393
f55ee35c
JK
21394 strcpy (retval, lead);
21395 strcat (retval, prefix);
6dd47d34
DE
21396 strcat (retval, sep);
21397 strcat (retval, suffix);
63d06c5c
DC
21398 return retval;
21399 }
987504bb
JJ
21400 else
21401 {
21402 /* We have an obstack. */
f55ee35c 21403 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21404 }
63d06c5c
DC
21405}
21406
71c25dea
TT
21407/* Get name of a die, return NULL if not found. */
21408
15d034d0
TT
21409static const char *
21410dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21411 struct objfile *objfile)
71c25dea
TT
21412{
21413 if (name && cu->language == language_cplus)
21414 {
2f408ecb 21415 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21416
2f408ecb 21417 if (!canon_name.empty ())
71c25dea 21418 {
2f408ecb 21419 if (canon_name != name)
be1e3d3e 21420 name = objfile->intern (canon_name);
71c25dea
TT
21421 }
21422 }
21423
21424 return name;
c906108c
SS
21425}
21426
96553a0c
DE
21427/* Get name of a die, return NULL if not found.
21428 Anonymous namespaces are converted to their magic string. */
9219021c 21429
15d034d0 21430static const char *
e142c38c 21431dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21432{
21433 struct attribute *attr;
518817b3 21434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21435
e142c38c 21436 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21437 if ((!attr || !DW_STRING (attr))
96553a0c 21438 && die->tag != DW_TAG_namespace
53832f31
TT
21439 && die->tag != DW_TAG_class_type
21440 && die->tag != DW_TAG_interface_type
21441 && die->tag != DW_TAG_structure_type
21442 && die->tag != DW_TAG_union_type)
71c25dea
TT
21443 return NULL;
21444
21445 switch (die->tag)
21446 {
21447 case DW_TAG_compile_unit:
95554aad 21448 case DW_TAG_partial_unit:
71c25dea
TT
21449 /* Compilation units have a DW_AT_name that is a filename, not
21450 a source language identifier. */
21451 case DW_TAG_enumeration_type:
21452 case DW_TAG_enumerator:
21453 /* These tags always have simple identifiers already; no need
21454 to canonicalize them. */
21455 return DW_STRING (attr);
907af001 21456
96553a0c
DE
21457 case DW_TAG_namespace:
21458 if (attr != NULL && DW_STRING (attr) != NULL)
21459 return DW_STRING (attr);
21460 return CP_ANONYMOUS_NAMESPACE_STR;
21461
907af001
UW
21462 case DW_TAG_class_type:
21463 case DW_TAG_interface_type:
21464 case DW_TAG_structure_type:
21465 case DW_TAG_union_type:
21466 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21467 structures or unions. These were of the form "._%d" in GCC 4.1,
21468 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21469 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21470 if (attr && DW_STRING (attr)
61012eef
GB
21471 && (startswith (DW_STRING (attr), "._")
21472 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21473 return NULL;
53832f31
TT
21474
21475 /* GCC might emit a nameless typedef that has a linkage name. See
21476 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21477 if (!attr || DW_STRING (attr) == NULL)
21478 {
73b9be8b 21479 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21480 if (attr == NULL || DW_STRING (attr) == NULL)
21481 return NULL;
21482
df5c6c50
JK
21483 /* Avoid demangling DW_STRING (attr) the second time on a second
21484 call for the same DIE. */
21485 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21486 {
43816ebc
TT
21487 gdb::unique_xmalloc_ptr<char> demangled
21488 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21489 if (demangled == nullptr)
21490 return nullptr;
43816ebc 21491
be1e3d3e 21492 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21493 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21494 }
67430cd0
TT
21495
21496 /* Strip any leading namespaces/classes, keep only the base name.
21497 DW_AT_name for named DIEs does not contain the prefixes. */
21498 const char *base = strrchr (DW_STRING (attr), ':');
21499 if (base && base > DW_STRING (attr) && base[-1] == ':')
21500 return &base[1];
21501 else
21502 return DW_STRING (attr);
53832f31 21503 }
907af001
UW
21504 break;
21505
71c25dea 21506 default:
907af001
UW
21507 break;
21508 }
21509
21510 if (!DW_STRING_IS_CANONICAL (attr))
21511 {
be1e3d3e
TT
21512 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21513 objfile);
907af001 21514 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21515 }
907af001 21516 return DW_STRING (attr);
9219021c
DC
21517}
21518
21519/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21520 is none. *EXT_CU is the CU containing DIE on input, and the CU
21521 containing the return value on output. */
9219021c
DC
21522
21523static struct die_info *
f2f0e013 21524dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21525{
21526 struct attribute *attr;
9219021c 21527
f2f0e013 21528 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21529 if (attr == NULL)
21530 return NULL;
21531
f2f0e013 21532 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21533}
21534
f9aca02d 21535static void
d97bc12b 21536dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21537{
21538 unsigned int i;
21539
d97bc12b 21540 print_spaces (indent, f);
9d8780f0 21541 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21542 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21543 sect_offset_str (die->sect_off));
d97bc12b
DE
21544
21545 if (die->parent != NULL)
21546 {
21547 print_spaces (indent, f);
9d8780f0
SM
21548 fprintf_unfiltered (f, " parent at offset: %s\n",
21549 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21550 }
21551
21552 print_spaces (indent, f);
21553 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21554 dwarf_bool_name (die->child != NULL));
c906108c 21555
d97bc12b
DE
21556 print_spaces (indent, f);
21557 fprintf_unfiltered (f, " attributes:\n");
21558
c906108c
SS
21559 for (i = 0; i < die->num_attrs; ++i)
21560 {
d97bc12b
DE
21561 print_spaces (indent, f);
21562 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21563 dwarf_attr_name (die->attrs[i].name),
21564 dwarf_form_name (die->attrs[i].form));
d97bc12b 21565
c906108c
SS
21566 switch (die->attrs[i].form)
21567 {
c906108c 21568 case DW_FORM_addr:
336d760d 21569 case DW_FORM_addrx:
3019eac3 21570 case DW_FORM_GNU_addr_index:
d97bc12b 21571 fprintf_unfiltered (f, "address: ");
5af949e3 21572 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21573 break;
21574 case DW_FORM_block2:
21575 case DW_FORM_block4:
21576 case DW_FORM_block:
21577 case DW_FORM_block1:
56eb65bd
SP
21578 fprintf_unfiltered (f, "block: size %s",
21579 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21580 break;
2dc7f7b3 21581 case DW_FORM_exprloc:
56eb65bd
SP
21582 fprintf_unfiltered (f, "expression: size %s",
21583 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21584 break;
0224619f
JK
21585 case DW_FORM_data16:
21586 fprintf_unfiltered (f, "constant of 16 bytes");
21587 break;
4568ecf9
DE
21588 case DW_FORM_ref_addr:
21589 fprintf_unfiltered (f, "ref address: ");
21590 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21591 break;
36586728
TT
21592 case DW_FORM_GNU_ref_alt:
21593 fprintf_unfiltered (f, "alt ref address: ");
21594 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21595 break;
10b3939b
DJ
21596 case DW_FORM_ref1:
21597 case DW_FORM_ref2:
21598 case DW_FORM_ref4:
4568ecf9
DE
21599 case DW_FORM_ref8:
21600 case DW_FORM_ref_udata:
d97bc12b 21601 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21602 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21603 break;
c906108c
SS
21604 case DW_FORM_data1:
21605 case DW_FORM_data2:
21606 case DW_FORM_data4:
ce5d95e1 21607 case DW_FORM_data8:
c906108c
SS
21608 case DW_FORM_udata:
21609 case DW_FORM_sdata:
43bbcdc2
PH
21610 fprintf_unfiltered (f, "constant: %s",
21611 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21612 break;
2dc7f7b3
TT
21613 case DW_FORM_sec_offset:
21614 fprintf_unfiltered (f, "section offset: %s",
21615 pulongest (DW_UNSND (&die->attrs[i])));
21616 break;
55f1336d 21617 case DW_FORM_ref_sig8:
ac9ec31b
DE
21618 fprintf_unfiltered (f, "signature: %s",
21619 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21620 break;
c906108c 21621 case DW_FORM_string:
4bdf3d34 21622 case DW_FORM_strp:
43988095 21623 case DW_FORM_line_strp:
cf532bd1 21624 case DW_FORM_strx:
3019eac3 21625 case DW_FORM_GNU_str_index:
36586728 21626 case DW_FORM_GNU_strp_alt:
8285870a 21627 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21628 DW_STRING (&die->attrs[i])
8285870a
JK
21629 ? DW_STRING (&die->attrs[i]) : "",
21630 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21631 break;
21632 case DW_FORM_flag:
21633 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21634 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21635 else
d97bc12b 21636 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21637 break;
2dc7f7b3
TT
21638 case DW_FORM_flag_present:
21639 fprintf_unfiltered (f, "flag: TRUE");
21640 break;
a8329558 21641 case DW_FORM_indirect:
0963b4bd
MS
21642 /* The reader will have reduced the indirect form to
21643 the "base form" so this form should not occur. */
5f48f8f3 21644 fprintf_unfiltered (f,
3e43a32a 21645 "unexpected attribute form: DW_FORM_indirect");
a8329558 21646 break;
663c44ac
JK
21647 case DW_FORM_implicit_const:
21648 fprintf_unfiltered (f, "constant: %s",
21649 plongest (DW_SND (&die->attrs[i])));
21650 break;
c906108c 21651 default:
d97bc12b 21652 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21653 die->attrs[i].form);
d97bc12b 21654 break;
c906108c 21655 }
d97bc12b 21656 fprintf_unfiltered (f, "\n");
c906108c
SS
21657 }
21658}
21659
f9aca02d 21660static void
d97bc12b 21661dump_die_for_error (struct die_info *die)
c906108c 21662{
d97bc12b
DE
21663 dump_die_shallow (gdb_stderr, 0, die);
21664}
21665
21666static void
21667dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21668{
21669 int indent = level * 4;
21670
21671 gdb_assert (die != NULL);
21672
21673 if (level >= max_level)
21674 return;
21675
21676 dump_die_shallow (f, indent, die);
21677
21678 if (die->child != NULL)
c906108c 21679 {
d97bc12b
DE
21680 print_spaces (indent, f);
21681 fprintf_unfiltered (f, " Children:");
21682 if (level + 1 < max_level)
21683 {
21684 fprintf_unfiltered (f, "\n");
21685 dump_die_1 (f, level + 1, max_level, die->child);
21686 }
21687 else
21688 {
3e43a32a
MS
21689 fprintf_unfiltered (f,
21690 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21691 }
21692 }
21693
21694 if (die->sibling != NULL && level > 0)
21695 {
21696 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21697 }
21698}
21699
d97bc12b
DE
21700/* This is called from the pdie macro in gdbinit.in.
21701 It's not static so gcc will keep a copy callable from gdb. */
21702
21703void
21704dump_die (struct die_info *die, int max_level)
21705{
21706 dump_die_1 (gdb_stdlog, 0, max_level, die);
21707}
21708
f9aca02d 21709static void
51545339 21710store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21711{
51545339 21712 void **slot;
c906108c 21713
9c541725
PA
21714 slot = htab_find_slot_with_hash (cu->die_hash, die,
21715 to_underlying (die->sect_off),
b64f50a1 21716 INSERT);
51545339
DJ
21717
21718 *slot = die;
c906108c
SS
21719}
21720
348e048f
DE
21721/* Follow reference or signature attribute ATTR of SRC_DIE.
21722 On entry *REF_CU is the CU of SRC_DIE.
21723 On exit *REF_CU is the CU of the result. */
21724
21725static struct die_info *
ff39bb5e 21726follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21727 struct dwarf2_cu **ref_cu)
21728{
21729 struct die_info *die;
21730
cd6c91b4 21731 if (attr->form_is_ref ())
348e048f 21732 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21733 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21734 die = follow_die_sig (src_die, attr, ref_cu);
21735 else
21736 {
21737 dump_die_for_error (src_die);
21738 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 21739 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
21740 }
21741
21742 return die;
03dd20cc
DJ
21743}
21744
5c631832 21745/* Follow reference OFFSET.
673bfd45
DE
21746 On entry *REF_CU is the CU of the source die referencing OFFSET.
21747 On exit *REF_CU is the CU of the result.
21748 Returns NULL if OFFSET is invalid. */
f504f079 21749
f9aca02d 21750static struct die_info *
9c541725 21751follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21752 struct dwarf2_cu **ref_cu)
c906108c 21753{
10b3939b 21754 struct die_info temp_die;
f2f0e013 21755 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
21756 struct dwarf2_per_objfile *dwarf2_per_objfile
21757 = cu->per_cu->dwarf2_per_objfile;
10b3939b 21758
348e048f
DE
21759 gdb_assert (cu->per_cu != NULL);
21760
98bfdba5
PA
21761 target_cu = cu;
21762
3019eac3 21763 if (cu->per_cu->is_debug_types)
348e048f
DE
21764 {
21765 /* .debug_types CUs cannot reference anything outside their CU.
21766 If they need to, they have to reference a signatured type via
55f1336d 21767 DW_FORM_ref_sig8. */
4057dfde 21768 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 21769 return NULL;
348e048f 21770 }
36586728 21771 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 21772 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
21773 {
21774 struct dwarf2_per_cu_data *per_cu;
9a619af0 21775
9c541725 21776 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 21777 dwarf2_per_objfile);
03dd20cc
DJ
21778
21779 /* If necessary, add it to the queue and load its DIEs. */
95554aad 21780 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 21781 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 21782
10b3939b
DJ
21783 target_cu = per_cu->cu;
21784 }
98bfdba5
PA
21785 else if (cu->dies == NULL)
21786 {
21787 /* We're loading full DIEs during partial symbol reading. */
21788 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 21789 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 21790 }
c906108c 21791
f2f0e013 21792 *ref_cu = target_cu;
9c541725 21793 temp_die.sect_off = sect_off;
c24bdb02
KS
21794
21795 if (target_cu != cu)
21796 target_cu->ancestor = cu;
21797
9a3c8263 21798 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21799 &temp_die,
21800 to_underlying (sect_off));
5c631832 21801}
10b3939b 21802
5c631832
JK
21803/* Follow reference attribute ATTR of SRC_DIE.
21804 On entry *REF_CU is the CU of SRC_DIE.
21805 On exit *REF_CU is the CU of the result. */
21806
21807static struct die_info *
ff39bb5e 21808follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21809 struct dwarf2_cu **ref_cu)
21810{
0826b30a 21811 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
21812 struct dwarf2_cu *cu = *ref_cu;
21813 struct die_info *die;
21814
9c541725 21815 die = follow_die_offset (sect_off,
36586728
TT
21816 (attr->form == DW_FORM_GNU_ref_alt
21817 || cu->per_cu->is_dwz),
21818 ref_cu);
5c631832 21819 if (!die)
9d8780f0
SM
21820 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21821 "at %s [in module %s]"),
21822 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 21823 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 21824
5c631832
JK
21825 return die;
21826}
21827
d4c9a4f8 21828/* See read.h. */
5c631832
JK
21829
21830struct dwarf2_locexpr_baton
9c541725 21831dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 21832 dwarf2_per_cu_data *per_cu,
8b9737bf 21833 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 21834 void *baton, bool resolve_abstract_p)
5c631832 21835{
918dd910 21836 struct dwarf2_cu *cu;
5c631832
JK
21837 struct die_info *die;
21838 struct attribute *attr;
21839 struct dwarf2_locexpr_baton retval;
12359b5e
SM
21840 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21841 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 21842
918dd910 21843 if (per_cu->cu == NULL)
58f0c718 21844 load_cu (per_cu, false);
918dd910 21845 cu = per_cu->cu;
cc12ce38
DE
21846 if (cu == NULL)
21847 {
21848 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21849 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21850 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21851 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21852 }
918dd910 21853
9c541725 21854 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 21855 if (!die)
9d8780f0
SM
21856 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21857 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21858
21859 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 21860 if (!attr && resolve_abstract_p
3360b6e7 21861 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
21862 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21863 {
21864 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 21865 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 21866 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 21867
3360b6e7
TV
21868 for (const auto &cand_off
21869 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 21870 {
3360b6e7
TV
21871 struct dwarf2_cu *cand_cu = cu;
21872 struct die_info *cand
21873 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21874 if (!cand
21875 || !cand->parent
e4a62c65
TV
21876 || cand->parent->tag != DW_TAG_subprogram)
21877 continue;
21878
21879 CORE_ADDR pc_low, pc_high;
21880 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
21881 if (pc_low == ((CORE_ADDR) -1))
21882 continue;
21883 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21884 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21885 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
21886 continue;
21887
21888 die = cand;
21889 attr = dwarf2_attr (die, DW_AT_location, cu);
21890 break;
21891 }
21892 }
21893
5c631832
JK
21894 if (!attr)
21895 {
e103e986
JK
21896 /* DWARF: "If there is no such attribute, then there is no effect.".
21897 DATA is ignored if SIZE is 0. */
5c631832 21898
e103e986 21899 retval.data = NULL;
5c631832
JK
21900 retval.size = 0;
21901 }
cd6c91b4 21902 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
21903 {
21904 struct dwarf2_loclist_baton loclist_baton;
21905 CORE_ADDR pc = (*get_frame_pc) (baton);
21906 size_t size;
21907
21908 fill_in_loclist_baton (cu, &loclist_baton, attr);
21909
21910 retval.data = dwarf2_find_location_expression (&loclist_baton,
21911 &size, pc);
21912 retval.size = size;
21913 }
5c631832
JK
21914 else
21915 {
4fc6c0d5 21916 if (!attr->form_is_block ())
9d8780f0 21917 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 21918 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 21919 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21920
21921 retval.data = DW_BLOCK (attr)->data;
21922 retval.size = DW_BLOCK (attr)->size;
21923 }
21924 retval.per_cu = cu->per_cu;
918dd910 21925
ed2dc618 21926 age_cached_comp_units (dwarf2_per_objfile);
918dd910 21927
5c631832 21928 return retval;
348e048f
DE
21929}
21930
d4c9a4f8 21931/* See read.h. */
8b9737bf
TT
21932
21933struct dwarf2_locexpr_baton
21934dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 21935 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
21936 CORE_ADDR (*get_frame_pc) (void *baton),
21937 void *baton)
21938{
9c541725 21939 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21940
9c541725 21941 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21942}
21943
b6807d98
TT
21944/* Write a constant of a given type as target-ordered bytes into
21945 OBSTACK. */
21946
21947static const gdb_byte *
21948write_constant_as_bytes (struct obstack *obstack,
21949 enum bfd_endian byte_order,
21950 struct type *type,
21951 ULONGEST value,
21952 LONGEST *len)
21953{
21954 gdb_byte *result;
21955
21956 *len = TYPE_LENGTH (type);
224c3ddb 21957 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21958 store_unsigned_integer (result, *len, byte_order, value);
21959
21960 return result;
21961}
21962
d4c9a4f8 21963/* See read.h. */
b6807d98
TT
21964
21965const gdb_byte *
9c541725 21966dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
21967 dwarf2_per_cu_data *per_cu,
21968 obstack *obstack,
b6807d98
TT
21969 LONGEST *len)
21970{
21971 struct dwarf2_cu *cu;
21972 struct die_info *die;
21973 struct attribute *attr;
21974 const gdb_byte *result = NULL;
21975 struct type *type;
21976 LONGEST value;
21977 enum bfd_endian byte_order;
e3b94546 21978 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 21979
b6807d98 21980 if (per_cu->cu == NULL)
58f0c718 21981 load_cu (per_cu, false);
b6807d98 21982 cu = per_cu->cu;
cc12ce38
DE
21983 if (cu == NULL)
21984 {
21985 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21986 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21987 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21988 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21989 }
b6807d98 21990
9c541725 21991 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 21992 if (!die)
9d8780f0
SM
21993 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21994 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
21995
21996 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21997 if (attr == NULL)
21998 return NULL;
21999
e3b94546 22000 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22001 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22002
22003 switch (attr->form)
22004 {
22005 case DW_FORM_addr:
336d760d 22006 case DW_FORM_addrx:
b6807d98
TT
22007 case DW_FORM_GNU_addr_index:
22008 {
22009 gdb_byte *tem;
22010
22011 *len = cu->header.addr_size;
224c3ddb 22012 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22013 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22014 result = tem;
22015 }
22016 break;
22017 case DW_FORM_string:
22018 case DW_FORM_strp:
cf532bd1 22019 case DW_FORM_strx:
b6807d98
TT
22020 case DW_FORM_GNU_str_index:
22021 case DW_FORM_GNU_strp_alt:
22022 /* DW_STRING is already allocated on the objfile obstack, point
22023 directly to it. */
22024 result = (const gdb_byte *) DW_STRING (attr);
22025 *len = strlen (DW_STRING (attr));
22026 break;
22027 case DW_FORM_block1:
22028 case DW_FORM_block2:
22029 case DW_FORM_block4:
22030 case DW_FORM_block:
22031 case DW_FORM_exprloc:
0224619f 22032 case DW_FORM_data16:
b6807d98
TT
22033 result = DW_BLOCK (attr)->data;
22034 *len = DW_BLOCK (attr)->size;
22035 break;
22036
22037 /* The DW_AT_const_value attributes are supposed to carry the
22038 symbol's value "represented as it would be on the target
22039 architecture." By the time we get here, it's already been
22040 converted to host endianness, so we just need to sign- or
22041 zero-extend it as appropriate. */
22042 case DW_FORM_data1:
22043 type = die_type (die, cu);
22044 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22045 if (result == NULL)
22046 result = write_constant_as_bytes (obstack, byte_order,
22047 type, value, len);
22048 break;
22049 case DW_FORM_data2:
22050 type = die_type (die, cu);
22051 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22052 if (result == NULL)
22053 result = write_constant_as_bytes (obstack, byte_order,
22054 type, value, len);
22055 break;
22056 case DW_FORM_data4:
22057 type = die_type (die, cu);
22058 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22059 if (result == NULL)
22060 result = write_constant_as_bytes (obstack, byte_order,
22061 type, value, len);
22062 break;
22063 case DW_FORM_data8:
22064 type = die_type (die, cu);
22065 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22066 if (result == NULL)
22067 result = write_constant_as_bytes (obstack, byte_order,
22068 type, value, len);
22069 break;
22070
22071 case DW_FORM_sdata:
663c44ac 22072 case DW_FORM_implicit_const:
b6807d98
TT
22073 type = die_type (die, cu);
22074 result = write_constant_as_bytes (obstack, byte_order,
22075 type, DW_SND (attr), len);
22076 break;
22077
22078 case DW_FORM_udata:
22079 type = die_type (die, cu);
22080 result = write_constant_as_bytes (obstack, byte_order,
22081 type, DW_UNSND (attr), len);
22082 break;
22083
22084 default:
b98664d3 22085 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22086 dwarf_form_name (attr->form));
22087 break;
22088 }
22089
22090 return result;
22091}
22092
d4c9a4f8 22093/* See read.h. */
7942e96e
AA
22094
22095struct type *
9c541725 22096dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22097 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22098{
22099 struct dwarf2_cu *cu;
22100 struct die_info *die;
22101
7942e96e 22102 if (per_cu->cu == NULL)
58f0c718 22103 load_cu (per_cu, false);
7942e96e
AA
22104 cu = per_cu->cu;
22105 if (!cu)
22106 return NULL;
22107
9c541725 22108 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22109 if (!die)
22110 return NULL;
22111
22112 return die_type (die, cu);
22113}
22114
8cb5117c 22115/* See read.h. */
8a9b8146
TT
22116
22117struct type *
b64f50a1 22118dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22119 struct dwarf2_per_cu_data *per_cu)
22120{
9c541725 22121 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22122 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22123}
22124
ac9ec31b 22125/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22126 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22127 On exit *REF_CU is the CU of the result.
22128 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22129
22130static struct die_info *
ac9ec31b
DE
22131follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22132 struct dwarf2_cu **ref_cu)
348e048f 22133{
348e048f 22134 struct die_info temp_die;
c24bdb02 22135 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22136 struct die_info *die;
22137
ac9ec31b
DE
22138 /* While it might be nice to assert sig_type->type == NULL here,
22139 we can get here for DW_AT_imported_declaration where we need
22140 the DIE not the type. */
348e048f
DE
22141
22142 /* If necessary, add it to the queue and load its DIEs. */
22143
95554aad 22144 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22145 read_signatured_type (sig_type);
348e048f 22146
348e048f 22147 sig_cu = sig_type->per_cu.cu;
69d751e3 22148 gdb_assert (sig_cu != NULL);
9c541725
PA
22149 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22150 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22151 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22152 to_underlying (temp_die.sect_off));
348e048f
DE
22153 if (die)
22154 {
ed2dc618 22155 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22156 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22157
796a7ff8
DE
22158 /* For .gdb_index version 7 keep track of included TUs.
22159 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22160 if (dwarf2_per_objfile->index_table != NULL
22161 && dwarf2_per_objfile->index_table->version <= 7)
22162 {
ae640021 22163 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22164 }
22165
348e048f 22166 *ref_cu = sig_cu;
c24bdb02
KS
22167 if (sig_cu != cu)
22168 sig_cu->ancestor = cu;
22169
348e048f
DE
22170 return die;
22171 }
22172
ac9ec31b
DE
22173 return NULL;
22174}
22175
22176/* Follow signatured type referenced by ATTR in SRC_DIE.
22177 On entry *REF_CU is the CU of SRC_DIE.
22178 On exit *REF_CU is the CU of the result.
22179 The result is the DIE of the type.
22180 If the referenced type cannot be found an error is thrown. */
22181
22182static struct die_info *
ff39bb5e 22183follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22184 struct dwarf2_cu **ref_cu)
22185{
22186 ULONGEST signature = DW_SIGNATURE (attr);
22187 struct signatured_type *sig_type;
22188 struct die_info *die;
22189
22190 gdb_assert (attr->form == DW_FORM_ref_sig8);
22191
a2ce51a0 22192 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22193 /* sig_type will be NULL if the signatured type is missing from
22194 the debug info. */
22195 if (sig_type == NULL)
22196 {
22197 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22198 " from DIE at %s [in module %s]"),
22199 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22200 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22201 }
22202
22203 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22204 if (die == NULL)
22205 {
22206 dump_die_for_error (src_die);
22207 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22208 " from DIE at %s [in module %s]"),
22209 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22210 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22211 }
22212
22213 return die;
22214}
22215
22216/* Get the type specified by SIGNATURE referenced in DIE/CU,
22217 reading in and processing the type unit if necessary. */
22218
22219static struct type *
22220get_signatured_type (struct die_info *die, ULONGEST signature,
22221 struct dwarf2_cu *cu)
22222{
518817b3
SM
22223 struct dwarf2_per_objfile *dwarf2_per_objfile
22224 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22225 struct signatured_type *sig_type;
22226 struct dwarf2_cu *type_cu;
22227 struct die_info *type_die;
22228 struct type *type;
22229
a2ce51a0 22230 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22231 /* sig_type will be NULL if the signatured type is missing from
22232 the debug info. */
22233 if (sig_type == NULL)
22234 {
b98664d3 22235 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22236 " from DIE at %s [in module %s]"),
22237 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22238 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22239 return build_error_marker_type (cu, die);
22240 }
22241
22242 /* If we already know the type we're done. */
22243 if (sig_type->type != NULL)
22244 return sig_type->type;
22245
22246 type_cu = cu;
22247 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22248 if (type_die != NULL)
22249 {
22250 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22251 is created. This is important, for example, because for c++ classes
22252 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22253 type = read_type_die (type_die, type_cu);
22254 if (type == NULL)
22255 {
b98664d3 22256 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22257 " referenced from DIE at %s [in module %s]"),
22258 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22259 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22260 type = build_error_marker_type (cu, die);
22261 }
22262 }
22263 else
22264 {
b98664d3 22265 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22266 " from DIE at %s [in module %s]"),
22267 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22268 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22269 type = build_error_marker_type (cu, die);
22270 }
22271 sig_type->type = type;
22272
22273 return type;
22274}
22275
22276/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22277 reading in and processing the type unit if necessary. */
22278
22279static struct type *
ff39bb5e 22280get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22281 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22282{
22283 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22284 if (attr->form_is_ref ())
ac9ec31b
DE
22285 {
22286 struct dwarf2_cu *type_cu = cu;
22287 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22288
22289 return read_type_die (type_die, type_cu);
22290 }
22291 else if (attr->form == DW_FORM_ref_sig8)
22292 {
22293 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22294 }
22295 else
22296 {
518817b3
SM
22297 struct dwarf2_per_objfile *dwarf2_per_objfile
22298 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22299
b98664d3 22300 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22301 " at %s [in module %s]"),
22302 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22303 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22304 return build_error_marker_type (cu, die);
22305 }
348e048f
DE
22306}
22307
e5fe5e75 22308/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22309
22310static void
e5fe5e75 22311load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22312{
52dc124a 22313 struct signatured_type *sig_type;
348e048f 22314
f4dc4d17 22315 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22316 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22317
6721b2ec
DE
22318 /* We have the per_cu, but we need the signatured_type.
22319 Fortunately this is an easy translation. */
22320 gdb_assert (per_cu->is_debug_types);
22321 sig_type = (struct signatured_type *) per_cu;
348e048f 22322
6721b2ec 22323 gdb_assert (per_cu->cu == NULL);
348e048f 22324
52dc124a 22325 read_signatured_type (sig_type);
348e048f 22326
6721b2ec 22327 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22328}
22329
3019eac3
DE
22330/* Read in a signatured type and build its CU and DIEs.
22331 If the type is a stub for the real type in a DWO file,
22332 read in the real type from the DWO file as well. */
dee91e82
DE
22333
22334static void
22335read_signatured_type (struct signatured_type *sig_type)
22336{
22337 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22338
3019eac3 22339 gdb_assert (per_cu->is_debug_types);
dee91e82 22340 gdb_assert (per_cu->cu == NULL);
348e048f 22341
6751ebae 22342 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22343
22344 if (!reader.dummy_p)
22345 {
22346 struct dwarf2_cu *cu = reader.cu;
22347 const gdb_byte *info_ptr = reader.info_ptr;
22348
22349 gdb_assert (cu->die_hash == NULL);
22350 cu->die_hash =
22351 htab_create_alloc_ex (cu->header.length / 12,
22352 die_hash,
22353 die_eq,
22354 NULL,
22355 &cu->comp_unit_obstack,
22356 hashtab_obstack_allocate,
22357 dummy_obstack_deallocate);
22358
3e225074 22359 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22360 reader.comp_unit_die->child
22361 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22362 reader.comp_unit_die);
22363 cu->dies = reader.comp_unit_die;
22364 /* comp_unit_die is not stored in die_hash, no need. */
22365
22366 /* We try not to read any attributes in this function, because
22367 not all CUs needed for references have been loaded yet, and
22368 symbol table processing isn't initialized. But we have to
22369 set the CU language, or we won't be able to build types
22370 correctly. Similarly, if we do not read the producer, we can
22371 not apply producer-specific interpretation. */
22372 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22373
22374 reader.keep ();
c0ab21c2
TT
22375 }
22376
7ee85ab1 22377 sig_type->per_cu.tu_read = 1;
c906108c
SS
22378}
22379
c906108c
SS
22380/* Decode simple location descriptions.
22381 Given a pointer to a dwarf block that defines a location, compute
22382 the location and return the value.
22383
4cecd739
DJ
22384 NOTE drow/2003-11-18: This function is called in two situations
22385 now: for the address of static or global variables (partial symbols
22386 only) and for offsets into structures which are expected to be
22387 (more or less) constant. The partial symbol case should go away,
22388 and only the constant case should remain. That will let this
22389 function complain more accurately. A few special modes are allowed
22390 without complaint for global variables (for instance, global
22391 register values and thread-local values).
c906108c
SS
22392
22393 A location description containing no operations indicates that the
4cecd739 22394 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22395 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22396 callers will only want a very basic result and this can become a
21ae7a4d
JK
22397 complaint.
22398
22399 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22400
22401static CORE_ADDR
e7c27a73 22402decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22403{
518817b3 22404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22405 size_t i;
22406 size_t size = blk->size;
d521ce57 22407 const gdb_byte *data = blk->data;
21ae7a4d
JK
22408 CORE_ADDR stack[64];
22409 int stacki;
22410 unsigned int bytes_read, unsnd;
22411 gdb_byte op;
c906108c 22412
21ae7a4d
JK
22413 i = 0;
22414 stacki = 0;
22415 stack[stacki] = 0;
22416 stack[++stacki] = 0;
22417
22418 while (i < size)
22419 {
22420 op = data[i++];
22421 switch (op)
22422 {
22423 case DW_OP_lit0:
22424 case DW_OP_lit1:
22425 case DW_OP_lit2:
22426 case DW_OP_lit3:
22427 case DW_OP_lit4:
22428 case DW_OP_lit5:
22429 case DW_OP_lit6:
22430 case DW_OP_lit7:
22431 case DW_OP_lit8:
22432 case DW_OP_lit9:
22433 case DW_OP_lit10:
22434 case DW_OP_lit11:
22435 case DW_OP_lit12:
22436 case DW_OP_lit13:
22437 case DW_OP_lit14:
22438 case DW_OP_lit15:
22439 case DW_OP_lit16:
22440 case DW_OP_lit17:
22441 case DW_OP_lit18:
22442 case DW_OP_lit19:
22443 case DW_OP_lit20:
22444 case DW_OP_lit21:
22445 case DW_OP_lit22:
22446 case DW_OP_lit23:
22447 case DW_OP_lit24:
22448 case DW_OP_lit25:
22449 case DW_OP_lit26:
22450 case DW_OP_lit27:
22451 case DW_OP_lit28:
22452 case DW_OP_lit29:
22453 case DW_OP_lit30:
22454 case DW_OP_lit31:
22455 stack[++stacki] = op - DW_OP_lit0;
22456 break;
f1bea926 22457
21ae7a4d
JK
22458 case DW_OP_reg0:
22459 case DW_OP_reg1:
22460 case DW_OP_reg2:
22461 case DW_OP_reg3:
22462 case DW_OP_reg4:
22463 case DW_OP_reg5:
22464 case DW_OP_reg6:
22465 case DW_OP_reg7:
22466 case DW_OP_reg8:
22467 case DW_OP_reg9:
22468 case DW_OP_reg10:
22469 case DW_OP_reg11:
22470 case DW_OP_reg12:
22471 case DW_OP_reg13:
22472 case DW_OP_reg14:
22473 case DW_OP_reg15:
22474 case DW_OP_reg16:
22475 case DW_OP_reg17:
22476 case DW_OP_reg18:
22477 case DW_OP_reg19:
22478 case DW_OP_reg20:
22479 case DW_OP_reg21:
22480 case DW_OP_reg22:
22481 case DW_OP_reg23:
22482 case DW_OP_reg24:
22483 case DW_OP_reg25:
22484 case DW_OP_reg26:
22485 case DW_OP_reg27:
22486 case DW_OP_reg28:
22487 case DW_OP_reg29:
22488 case DW_OP_reg30:
22489 case DW_OP_reg31:
22490 stack[++stacki] = op - DW_OP_reg0;
22491 if (i < size)
22492 dwarf2_complex_location_expr_complaint ();
22493 break;
c906108c 22494
21ae7a4d
JK
22495 case DW_OP_regx:
22496 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22497 i += bytes_read;
22498 stack[++stacki] = unsnd;
22499 if (i < size)
22500 dwarf2_complex_location_expr_complaint ();
22501 break;
c906108c 22502
21ae7a4d 22503 case DW_OP_addr:
c8a7a66f
TT
22504 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22505 &bytes_read);
21ae7a4d
JK
22506 i += bytes_read;
22507 break;
d53d4ac5 22508
21ae7a4d
JK
22509 case DW_OP_const1u:
22510 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22511 i += 1;
22512 break;
22513
22514 case DW_OP_const1s:
22515 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22516 i += 1;
22517 break;
22518
22519 case DW_OP_const2u:
22520 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22521 i += 2;
22522 break;
22523
22524 case DW_OP_const2s:
22525 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22526 i += 2;
22527 break;
d53d4ac5 22528
21ae7a4d
JK
22529 case DW_OP_const4u:
22530 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22531 i += 4;
22532 break;
22533
22534 case DW_OP_const4s:
22535 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22536 i += 4;
22537 break;
22538
585861ea
JK
22539 case DW_OP_const8u:
22540 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22541 i += 8;
22542 break;
22543
21ae7a4d
JK
22544 case DW_OP_constu:
22545 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22546 &bytes_read);
22547 i += bytes_read;
22548 break;
22549
22550 case DW_OP_consts:
22551 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22552 i += bytes_read;
22553 break;
22554
22555 case DW_OP_dup:
22556 stack[stacki + 1] = stack[stacki];
22557 stacki++;
22558 break;
22559
22560 case DW_OP_plus:
22561 stack[stacki - 1] += stack[stacki];
22562 stacki--;
22563 break;
22564
22565 case DW_OP_plus_uconst:
22566 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22567 &bytes_read);
22568 i += bytes_read;
22569 break;
22570
22571 case DW_OP_minus:
22572 stack[stacki - 1] -= stack[stacki];
22573 stacki--;
22574 break;
22575
22576 case DW_OP_deref:
22577 /* If we're not the last op, then we definitely can't encode
22578 this using GDB's address_class enum. This is valid for partial
22579 global symbols, although the variable's address will be bogus
22580 in the psymtab. */
22581 if (i < size)
22582 dwarf2_complex_location_expr_complaint ();
22583 break;
22584
22585 case DW_OP_GNU_push_tls_address:
4aa4e28b 22586 case DW_OP_form_tls_address:
21ae7a4d
JK
22587 /* The top of the stack has the offset from the beginning
22588 of the thread control block at which the variable is located. */
22589 /* Nothing should follow this operator, so the top of stack would
22590 be returned. */
22591 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22592 address will be bogus in the psymtab. Make it always at least
22593 non-zero to not look as a variable garbage collected by linker
22594 which have DW_OP_addr 0. */
21ae7a4d
JK
22595 if (i < size)
22596 dwarf2_complex_location_expr_complaint ();
585861ea 22597 stack[stacki]++;
21ae7a4d
JK
22598 break;
22599
22600 case DW_OP_GNU_uninit:
22601 break;
22602
336d760d 22603 case DW_OP_addrx:
3019eac3 22604 case DW_OP_GNU_addr_index:
49f6c839 22605 case DW_OP_GNU_const_index:
3019eac3
DE
22606 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22607 &bytes_read);
22608 i += bytes_read;
22609 break;
22610
21ae7a4d
JK
22611 default:
22612 {
f39c6ffd 22613 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22614
22615 if (name)
b98664d3 22616 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22617 name);
22618 else
b98664d3 22619 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22620 op);
22621 }
22622
22623 return (stack[stacki]);
d53d4ac5 22624 }
3c6e0cb3 22625
21ae7a4d
JK
22626 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22627 outside of the allocated space. Also enforce minimum>0. */
22628 if (stacki >= ARRAY_SIZE (stack) - 1)
22629 {
b98664d3 22630 complaint (_("location description stack overflow"));
21ae7a4d
JK
22631 return 0;
22632 }
22633
22634 if (stacki <= 0)
22635 {
b98664d3 22636 complaint (_("location description stack underflow"));
21ae7a4d
JK
22637 return 0;
22638 }
22639 }
22640 return (stack[stacki]);
c906108c
SS
22641}
22642
22643/* memory allocation interface */
22644
c906108c 22645static struct dwarf_block *
7b5a2f43 22646dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22647{
8d749320 22648 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22649}
22650
c906108c 22651static struct die_info *
b60c80d6 22652dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22653{
22654 struct die_info *die;
b60c80d6
DJ
22655 size_t size = sizeof (struct die_info);
22656
22657 if (num_attrs > 1)
22658 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22659
b60c80d6 22660 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22661 memset (die, 0, sizeof (struct die_info));
22662 return (die);
22663}
2e276125
JB
22664
22665\f
a036ba48 22666
c90ec28a 22667/* Macro support. */
cf2c3c16 22668
9eac9650
TT
22669/* An overload of dwarf_decode_macros that finds the correct section
22670 and ensures it is read in before calling the other overload. */
22671
22672static void
22673dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22674 int section_is_gnu)
22675{
22676 struct dwarf2_per_objfile *dwarf2_per_objfile
22677 = cu->per_cu->dwarf2_per_objfile;
22678 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 22679 const struct line_header *lh = cu->line_header;
9eac9650
TT
22680 unsigned int offset_size = cu->header.offset_size;
22681 struct dwarf2_section_info *section;
22682 const char *section_name;
22683
22684 if (cu->dwo_unit != nullptr)
22685 {
22686 if (section_is_gnu)
22687 {
22688 section = &cu->dwo_unit->dwo_file->sections.macro;
22689 section_name = ".debug_macro.dwo";
22690 }
22691 else
22692 {
22693 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22694 section_name = ".debug_macinfo.dwo";
22695 }
22696 }
22697 else
22698 {
22699 if (section_is_gnu)
22700 {
22701 section = &dwarf2_per_objfile->macro;
22702 section_name = ".debug_macro";
22703 }
22704 else
22705 {
22706 section = &dwarf2_per_objfile->macinfo;
22707 section_name = ".debug_macinfo";
22708 }
22709 }
22710
22711 section->read (objfile);
22712 if (section->buffer == nullptr)
22713 {
22714 complaint (_("missing %s section"), section_name);
22715 return;
22716 }
22717
22718 buildsym_compunit *builder = cu->get_builder ();
22719
22720 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22721 offset_size, offset, section_is_gnu);
22722}
22723
3019eac3
DE
22724/* Return the .debug_loc section to use for CU.
22725 For DWO files use .debug_loc.dwo. */
22726
22727static struct dwarf2_section_info *
22728cu_debug_loc_section (struct dwarf2_cu *cu)
22729{
518817b3
SM
22730 struct dwarf2_per_objfile *dwarf2_per_objfile
22731 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22732
3019eac3 22733 if (cu->dwo_unit)
43988095
JK
22734 {
22735 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 22736
43988095
JK
22737 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22738 }
22739 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22740 : &dwarf2_per_objfile->loc);
3019eac3
DE
22741}
22742
8cf6f0b1
TT
22743/* A helper function that fills in a dwarf2_loclist_baton. */
22744
22745static void
22746fill_in_loclist_baton (struct dwarf2_cu *cu,
22747 struct dwarf2_loclist_baton *baton,
ff39bb5e 22748 const struct attribute *attr)
8cf6f0b1 22749{
518817b3
SM
22750 struct dwarf2_per_objfile *dwarf2_per_objfile
22751 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
22752 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22753
96b79293 22754 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
22755
22756 baton->per_cu = cu->per_cu;
22757 gdb_assert (baton->per_cu);
22758 /* We don't know how long the location list is, but make sure we
22759 don't run off the edge of the section. */
3019eac3
DE
22760 baton->size = section->size - DW_UNSND (attr);
22761 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
22762 if (cu->base_address.has_value ())
22763 baton->base_address = *cu->base_address;
22764 else
22765 baton->base_address = 0;
f664829e 22766 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22767}
22768
4c2df51b 22769static void
ff39bb5e 22770dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22771 struct dwarf2_cu *cu, int is_block)
4c2df51b 22772{
518817b3
SM
22773 struct dwarf2_per_objfile *dwarf2_per_objfile
22774 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 22775 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22776 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22777
cd6c91b4 22778 if (attr->form_is_section_offset ()
3019eac3 22779 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22780 the section. If so, fall through to the complaint in the
22781 other branch. */
2c7d5afc 22782 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 22783 {
0d53c4c4 22784 struct dwarf2_loclist_baton *baton;
4c2df51b 22785
8d749320 22786 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22787
8cf6f0b1 22788 fill_in_loclist_baton (cu, baton, attr);
be391dca 22789
2b24b6e4 22790 if (!cu->base_address.has_value ())
b98664d3 22791 complaint (_("Location list used without "
3e43a32a 22792 "specifying the CU base address."));
4c2df51b 22793
f1e6e072
TT
22794 SYMBOL_ACLASS_INDEX (sym) = (is_block
22795 ? dwarf2_loclist_block_index
22796 : dwarf2_loclist_index);
0d53c4c4
DJ
22797 SYMBOL_LOCATION_BATON (sym) = baton;
22798 }
22799 else
22800 {
22801 struct dwarf2_locexpr_baton *baton;
22802
8d749320 22803 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22804 baton->per_cu = cu->per_cu;
22805 gdb_assert (baton->per_cu);
0d53c4c4 22806
4fc6c0d5 22807 if (attr->form_is_block ())
0d53c4c4
DJ
22808 {
22809 /* Note that we're just copying the block's data pointer
22810 here, not the actual data. We're still pointing into the
6502dd73
DJ
22811 info_buffer for SYM's objfile; right now we never release
22812 that buffer, but when we do clean up properly this may
22813 need to change. */
0d53c4c4
DJ
22814 baton->size = DW_BLOCK (attr)->size;
22815 baton->data = DW_BLOCK (attr)->data;
22816 }
22817 else
22818 {
22819 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 22820 sym->natural_name ());
0d53c4c4 22821 baton->size = 0;
0d53c4c4 22822 }
6e70227d 22823
f1e6e072
TT
22824 SYMBOL_ACLASS_INDEX (sym) = (is_block
22825 ? dwarf2_locexpr_block_index
22826 : dwarf2_locexpr_index);
0d53c4c4
DJ
22827 SYMBOL_LOCATION_BATON (sym) = baton;
22828 }
4c2df51b 22829}
6502dd73 22830
09ba997f 22831/* See read.h. */
ae0d2f24
UW
22832
22833struct objfile *
09ba997f 22834dwarf2_per_cu_data::objfile () const
ae0d2f24 22835{
09ba997f 22836 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
22837
22838 /* Return the master objfile, so that we can report and look up the
22839 correct file containing this variable. */
22840 if (objfile->separate_debug_objfile_backlink)
22841 objfile = objfile->separate_debug_objfile_backlink;
22842
22843 return objfile;
22844}
22845
96408a79
SA
22846/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22847 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22848 CU_HEADERP first. */
22849
22850static const struct comp_unit_head *
22851per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 22852 const struct dwarf2_per_cu_data *per_cu)
96408a79 22853{
d521ce57 22854 const gdb_byte *info_ptr;
96408a79
SA
22855
22856 if (per_cu->cu)
22857 return &per_cu->cu->header;
22858
9c541725 22859 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22860
22861 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22862 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22863 rcuh_kind::COMPILE);
96408a79
SA
22864
22865 return cu_headerp;
22866}
22867
09ba997f 22868/* See read.h. */
ae0d2f24 22869
98714339 22870int
09ba997f 22871dwarf2_per_cu_data::addr_size () const
ae0d2f24 22872{
96408a79
SA
22873 struct comp_unit_head cu_header_local;
22874 const struct comp_unit_head *cu_headerp;
c471e790 22875
09ba997f 22876 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22877
22878 return cu_headerp->addr_size;
ae0d2f24
UW
22879}
22880
09ba997f 22881/* See read.h. */
9eae7c52
TT
22882
22883int
09ba997f 22884dwarf2_per_cu_data::offset_size () const
9eae7c52 22885{
96408a79
SA
22886 struct comp_unit_head cu_header_local;
22887 const struct comp_unit_head *cu_headerp;
9c6c53f7 22888
09ba997f 22889 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22890
22891 return cu_headerp->offset_size;
22892}
22893
09ba997f 22894/* See read.h. */
96408a79
SA
22895
22896int
09ba997f 22897dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
22898{
22899 struct comp_unit_head cu_header_local;
22900 const struct comp_unit_head *cu_headerp;
22901
09ba997f 22902 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22903
22904 if (cu_headerp->version == 2)
22905 return cu_headerp->addr_size;
22906 else
22907 return cu_headerp->offset_size;
181cebd4
JK
22908}
22909
09ba997f 22910/* See read.h. */
9aa1f1e3
TT
22911
22912CORE_ADDR
09ba997f 22913dwarf2_per_cu_data::text_offset () const
9aa1f1e3 22914{
09ba997f
TT
22915 struct objfile *objfile = dwarf2_per_objfile->objfile;
22916
22917 return objfile->text_section_offset ();
9aa1f1e3
TT
22918}
22919
09ba997f
TT
22920/* See read.h. */
22921
22922struct type *
22923dwarf2_per_cu_data::addr_type () const
9a49df9d 22924{
09ba997f 22925 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
22926 struct type *void_type = objfile_type (objfile)->builtin_void;
22927 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 22928 int addr_size = this->addr_size ();
9a49df9d
AB
22929
22930 if (TYPE_LENGTH (addr_type) == addr_size)
22931 return addr_type;
22932
09ba997f 22933 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
22934 return addr_type;
22935}
22936
22b6cd70
TT
22937/* A helper function for dwarf2_find_containing_comp_unit that returns
22938 the index of the result, and that searches a vector. It will
22939 return a result even if the offset in question does not actually
22940 occur in any CU. This is separate so that it can be unit
22941 tested. */
ae038cb0 22942
22b6cd70
TT
22943static int
22944dwarf2_find_containing_comp_unit
22945 (sect_offset sect_off,
22946 unsigned int offset_in_dwz,
22947 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 22948{
ae038cb0
DJ
22949 int low, high;
22950
ae038cb0 22951 low = 0;
22b6cd70 22952 high = all_comp_units.size () - 1;
ae038cb0
DJ
22953 while (high > low)
22954 {
36586728 22955 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22956 int mid = low + (high - low) / 2;
9a619af0 22957
22b6cd70 22958 mid_cu = all_comp_units[mid];
36586728 22959 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 22960 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 22961 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
22962 high = mid;
22963 else
22964 low = mid + 1;
22965 }
22966 gdb_assert (low == high);
22b6cd70
TT
22967 return low;
22968}
22969
22970/* Locate the .debug_info compilation unit from CU's objfile which contains
22971 the DIE at OFFSET. Raises an error on failure. */
22972
22973static struct dwarf2_per_cu_data *
22974dwarf2_find_containing_comp_unit (sect_offset sect_off,
22975 unsigned int offset_in_dwz,
22976 struct dwarf2_per_objfile *dwarf2_per_objfile)
22977{
22978 int low
22979 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22980 dwarf2_per_objfile->all_comp_units);
22981 struct dwarf2_per_cu_data *this_cu
22982 = dwarf2_per_objfile->all_comp_units[low];
22983
45b8ae0c 22984 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 22985 {
36586728 22986 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22987 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
22988 "offset %s [in module %s]"),
22989 sect_offset_str (sect_off),
ed2dc618 22990 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 22991
9c541725
PA
22992 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22993 <= sect_off);
ae038cb0
DJ
22994 return dwarf2_per_objfile->all_comp_units[low-1];
22995 }
22996 else
22997 {
b76e467d 22998 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 22999 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23000 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23001 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23002 return this_cu;
23003 }
23004}
23005
22b6cd70
TT
23006#if GDB_SELF_TEST
23007
23008namespace selftests {
23009namespace find_containing_comp_unit {
23010
23011static void
23012run_test ()
23013{
23014 struct dwarf2_per_cu_data one {};
23015 struct dwarf2_per_cu_data two {};
23016 struct dwarf2_per_cu_data three {};
23017 struct dwarf2_per_cu_data four {};
23018
23019 one.length = 5;
23020 two.sect_off = sect_offset (one.length);
23021 two.length = 7;
23022
23023 three.length = 5;
23024 three.is_dwz = 1;
23025 four.sect_off = sect_offset (three.length);
23026 four.length = 7;
23027 four.is_dwz = 1;
23028
23029 std::vector<dwarf2_per_cu_data *> units;
23030 units.push_back (&one);
23031 units.push_back (&two);
23032 units.push_back (&three);
23033 units.push_back (&four);
23034
23035 int result;
23036
23037 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23038 SELF_CHECK (units[result] == &one);
23039 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23040 SELF_CHECK (units[result] == &one);
23041 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23042 SELF_CHECK (units[result] == &two);
23043
23044 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23045 SELF_CHECK (units[result] == &three);
23046 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23047 SELF_CHECK (units[result] == &three);
23048 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23049 SELF_CHECK (units[result] == &four);
23050}
23051
23052}
23053}
23054
23055#endif /* GDB_SELF_TEST */
23056
23745b47 23057/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23058
fcd3b13d
SM
23059dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23060 : per_cu (per_cu_),
9068261f
AB
23061 mark (false),
23062 has_loclist (false),
23063 checked_producer (false),
23064 producer_is_gxx_lt_4_6 (false),
23065 producer_is_gcc_lt_4_3 (false),
eb77c9df 23066 producer_is_icc (false),
9068261f 23067 producer_is_icc_lt_14 (false),
c258c396 23068 producer_is_codewarrior (false),
9068261f 23069 processing_has_namespace_info (false)
93311388 23070{
fcd3b13d
SM
23071 per_cu->cu = this;
23072}
23073
23074/* Destroy a dwarf2_cu. */
23075
23076dwarf2_cu::~dwarf2_cu ()
23077{
23078 per_cu->cu = NULL;
9816fde3
JK
23079}
23080
23081/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23082
23083static void
95554aad
TT
23084prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23085 enum language pretend_language)
9816fde3
JK
23086{
23087 struct attribute *attr;
23088
23089 /* Set the language we're debugging. */
23090 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23091 if (attr != nullptr)
9816fde3
JK
23092 set_cu_language (DW_UNSND (attr), cu);
23093 else
9cded63f 23094 {
95554aad 23095 cu->language = pretend_language;
9cded63f
TT
23096 cu->language_defn = language_def (cu->language);
23097 }
dee91e82 23098
7d45c7c3 23099 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23100}
23101
ae038cb0
DJ
23102/* Increase the age counter on each cached compilation unit, and free
23103 any that are too old. */
23104
23105static void
ed2dc618 23106age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23107{
23108 struct dwarf2_per_cu_data *per_cu, **last_chain;
23109
23110 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23111 per_cu = dwarf2_per_objfile->read_in_chain;
23112 while (per_cu != NULL)
23113 {
23114 per_cu->cu->last_used ++;
b4f54984 23115 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23116 dwarf2_mark (per_cu->cu);
23117 per_cu = per_cu->cu->read_in_chain;
23118 }
23119
23120 per_cu = dwarf2_per_objfile->read_in_chain;
23121 last_chain = &dwarf2_per_objfile->read_in_chain;
23122 while (per_cu != NULL)
23123 {
23124 struct dwarf2_per_cu_data *next_cu;
23125
23126 next_cu = per_cu->cu->read_in_chain;
23127
23128 if (!per_cu->cu->mark)
23129 {
fcd3b13d 23130 delete per_cu->cu;
ae038cb0
DJ
23131 *last_chain = next_cu;
23132 }
23133 else
23134 last_chain = &per_cu->cu->read_in_chain;
23135
23136 per_cu = next_cu;
23137 }
23138}
23139
23140/* Remove a single compilation unit from the cache. */
23141
23142static void
dee91e82 23143free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23144{
23145 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23146 struct dwarf2_per_objfile *dwarf2_per_objfile
23147 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23148
23149 per_cu = dwarf2_per_objfile->read_in_chain;
23150 last_chain = &dwarf2_per_objfile->read_in_chain;
23151 while (per_cu != NULL)
23152 {
23153 struct dwarf2_per_cu_data *next_cu;
23154
23155 next_cu = per_cu->cu->read_in_chain;
23156
dee91e82 23157 if (per_cu == target_per_cu)
ae038cb0 23158 {
fcd3b13d 23159 delete per_cu->cu;
dee91e82 23160 per_cu->cu = NULL;
ae038cb0
DJ
23161 *last_chain = next_cu;
23162 break;
23163 }
23164 else
23165 last_chain = &per_cu->cu->read_in_chain;
23166
23167 per_cu = next_cu;
23168 }
23169}
23170
dee91e82
DE
23171/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23172 We store these in a hash table separate from the DIEs, and preserve them
23173 when the DIEs are flushed out of cache.
23174
23175 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23176 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23177 or the type may come from a DWO file. Furthermore, while it's more logical
23178 to use per_cu->section+offset, with Fission the section with the data is in
23179 the DWO file but we don't know that section at the point we need it.
23180 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23181 because we can enter the lookup routine, get_die_type_at_offset, from
23182 outside this file, and thus won't necessarily have PER_CU->cu.
23183 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23184
dee91e82 23185struct dwarf2_per_cu_offset_and_type
1c379e20 23186{
dee91e82 23187 const struct dwarf2_per_cu_data *per_cu;
9c541725 23188 sect_offset sect_off;
1c379e20
DJ
23189 struct type *type;
23190};
23191
dee91e82 23192/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23193
23194static hashval_t
dee91e82 23195per_cu_offset_and_type_hash (const void *item)
1c379e20 23196{
9a3c8263
SM
23197 const struct dwarf2_per_cu_offset_and_type *ofs
23198 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23199
9c541725 23200 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23201}
23202
dee91e82 23203/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23204
23205static int
dee91e82 23206per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23207{
9a3c8263
SM
23208 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23209 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23210 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23211 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23212
dee91e82 23213 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23214 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23215}
23216
23217/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23218 table if necessary. For convenience, return TYPE.
23219
23220 The DIEs reading must have careful ordering to:
85102364 23221 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23222 reading current DIE.
23223 * Not trying to dereference contents of still incompletely read in types
23224 while reading in other DIEs.
23225 * Enable referencing still incompletely read in types just by a pointer to
23226 the type without accessing its fields.
23227
23228 Therefore caller should follow these rules:
23229 * Try to fetch any prerequisite types we may need to build this DIE type
23230 before building the type and calling set_die_type.
e71ec853 23231 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23232 possible before fetching more types to complete the current type.
23233 * Make the type as complete as possible before fetching more types. */
1c379e20 23234
f792889a 23235static struct type *
1c379e20
DJ
23236set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23237{
518817b3
SM
23238 struct dwarf2_per_objfile *dwarf2_per_objfile
23239 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23240 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23241 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23242 struct attribute *attr;
23243 struct dynamic_prop prop;
1c379e20 23244
b4ba55a1
JB
23245 /* For Ada types, make sure that the gnat-specific data is always
23246 initialized (if not already set). There are a few types where
23247 we should not be doing so, because the type-specific area is
23248 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23249 where the type-specific area is used to store the floatformat).
23250 But this is not a problem, because the gnat-specific information
23251 is actually not needed for these types. */
23252 if (need_gnat_info (cu)
23253 && TYPE_CODE (type) != TYPE_CODE_FUNC
23254 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23255 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23256 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23257 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23258 && !HAVE_GNAT_AUX_INFO (type))
23259 INIT_GNAT_SPECIFIC (type);
23260
3f2f83dd
KB
23261 /* Read DW_AT_allocated and set in type. */
23262 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23263 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23264 {
09ba997f 23265 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23266 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23267 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23268 }
23269 else if (attr != NULL)
23270 {
b98664d3 23271 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23272 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23273 sect_offset_str (die->sect_off));
3f2f83dd
KB
23274 }
23275
23276 /* Read DW_AT_associated and set in type. */
23277 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23278 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23279 {
09ba997f 23280 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23281 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23282 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23283 }
23284 else if (attr != NULL)
23285 {
b98664d3 23286 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23287 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23288 sect_offset_str (die->sect_off));
3f2f83dd
KB
23289 }
23290
3cdcd0ce
JB
23291 /* Read DW_AT_data_location and set in type. */
23292 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23293 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23294 cu->per_cu->addr_type ()))
50a82047 23295 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23296
dee91e82 23297 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23298 dwarf2_per_objfile->die_type_hash
23299 = htab_up (htab_create_alloc (127,
23300 per_cu_offset_and_type_hash,
23301 per_cu_offset_and_type_eq,
23302 NULL, xcalloc, xfree));
1c379e20 23303
dee91e82 23304 ofs.per_cu = cu->per_cu;
9c541725 23305 ofs.sect_off = die->sect_off;
1c379e20 23306 ofs.type = type;
dee91e82 23307 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23308 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23309 if (*slot)
b98664d3 23310 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23311 sect_offset_str (die->sect_off));
8d749320
SM
23312 *slot = XOBNEW (&objfile->objfile_obstack,
23313 struct dwarf2_per_cu_offset_and_type);
1c379e20 23314 **slot = ofs;
f792889a 23315 return type;
1c379e20
DJ
23316}
23317
9c541725 23318/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23319 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23320
23321static struct type *
9c541725 23322get_die_type_at_offset (sect_offset sect_off,
673bfd45 23323 struct dwarf2_per_cu_data *per_cu)
1c379e20 23324{
dee91e82 23325 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23326 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23327
dee91e82 23328 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23329 return NULL;
1c379e20 23330
dee91e82 23331 ofs.per_cu = per_cu;
9c541725 23332 ofs.sect_off = sect_off;
9a3c8263 23333 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23334 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23335 if (slot)
23336 return slot->type;
23337 else
23338 return NULL;
23339}
23340
02142a6c 23341/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23342 or return NULL if DIE does not have a saved type. */
23343
23344static struct type *
23345get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23346{
9c541725 23347 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23348}
23349
10b3939b
DJ
23350/* Add a dependence relationship from CU to REF_PER_CU. */
23351
23352static void
23353dwarf2_add_dependence (struct dwarf2_cu *cu,
23354 struct dwarf2_per_cu_data *ref_per_cu)
23355{
23356 void **slot;
23357
23358 if (cu->dependencies == NULL)
23359 cu->dependencies
23360 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23361 NULL, &cu->comp_unit_obstack,
23362 hashtab_obstack_allocate,
23363 dummy_obstack_deallocate);
23364
23365 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23366 if (*slot == NULL)
23367 *slot = ref_per_cu;
23368}
1c379e20 23369
f504f079
DE
23370/* Subroutine of dwarf2_mark to pass to htab_traverse.
23371 Set the mark field in every compilation unit in the
ae038cb0
DJ
23372 cache that we must keep because we are keeping CU. */
23373
10b3939b
DJ
23374static int
23375dwarf2_mark_helper (void **slot, void *data)
23376{
23377 struct dwarf2_per_cu_data *per_cu;
23378
23379 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23380
23381 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23382 reading of the chain. As such dependencies remain valid it is not much
23383 useful to track and undo them during QUIT cleanups. */
23384 if (per_cu->cu == NULL)
23385 return 1;
23386
10b3939b
DJ
23387 if (per_cu->cu->mark)
23388 return 1;
9068261f 23389 per_cu->cu->mark = true;
10b3939b
DJ
23390
23391 if (per_cu->cu->dependencies != NULL)
23392 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23393
23394 return 1;
23395}
23396
f504f079
DE
23397/* Set the mark field in CU and in every other compilation unit in the
23398 cache that we must keep because we are keeping CU. */
23399
ae038cb0
DJ
23400static void
23401dwarf2_mark (struct dwarf2_cu *cu)
23402{
23403 if (cu->mark)
23404 return;
9068261f 23405 cu->mark = true;
10b3939b
DJ
23406 if (cu->dependencies != NULL)
23407 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23408}
23409
23410static void
23411dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23412{
23413 while (per_cu)
23414 {
9068261f 23415 per_cu->cu->mark = false;
ae038cb0
DJ
23416 per_cu = per_cu->cu->read_in_chain;
23417 }
72bf9492
DJ
23418}
23419
72bf9492
DJ
23420/* Trivial hash function for partial_die_info: the hash value of a DIE
23421 is its offset in .debug_info for this objfile. */
23422
23423static hashval_t
23424partial_die_hash (const void *item)
23425{
9a3c8263
SM
23426 const struct partial_die_info *part_die
23427 = (const struct partial_die_info *) item;
9a619af0 23428
9c541725 23429 return to_underlying (part_die->sect_off);
72bf9492
DJ
23430}
23431
23432/* Trivial comparison function for partial_die_info structures: two DIEs
23433 are equal if they have the same offset. */
23434
23435static int
23436partial_die_eq (const void *item_lhs, const void *item_rhs)
23437{
9a3c8263
SM
23438 const struct partial_die_info *part_die_lhs
23439 = (const struct partial_die_info *) item_lhs;
23440 const struct partial_die_info *part_die_rhs
23441 = (const struct partial_die_info *) item_rhs;
9a619af0 23442
9c541725 23443 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23444}
23445
3c3bb058
AB
23446struct cmd_list_element *set_dwarf_cmdlist;
23447struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23448
23449static void
981a3fb3 23450set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23451{
b4f54984 23452 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23453 gdb_stdout);
ae038cb0
DJ
23454}
23455
23456static void
981a3fb3 23457show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23458{
b4f54984 23459 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23460}
23461
9291a0cd 23462static void
cd4fb1b2
SM
23463show_check_physname (struct ui_file *file, int from_tty,
23464 struct cmd_list_element *c, const char *value)
9291a0cd 23465{
cd4fb1b2
SM
23466 fprintf_filtered (file,
23467 _("Whether to check \"physname\" is %s.\n"),
23468 value);
9291a0cd
TT
23469}
23470
6c265988 23471void _initialize_dwarf2_read ();
cd4fb1b2 23472void
6c265988 23473_initialize_dwarf2_read ()
9291a0cd 23474{
cd4fb1b2
SM
23475 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23476Set DWARF specific variables.\n\
590042fc 23477Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
23478 &set_dwarf_cmdlist, "maintenance set dwarf ",
23479 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23480
cd4fb1b2 23481 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
23482Show DWARF specific variables.\n\
23483Show DWARF variables such as the cache size."),
cd4fb1b2
SM
23484 &show_dwarf_cmdlist, "maintenance show dwarf ",
23485 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23486
cd4fb1b2
SM
23487 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23488 &dwarf_max_cache_age, _("\
23489Set the upper bound on the age of cached DWARF compilation units."), _("\
23490Show the upper bound on the age of cached DWARF compilation units."), _("\
23491A higher limit means that cached compilation units will be stored\n\
23492in memory longer, and more total memory will be used. Zero disables\n\
23493caching, which can slow down startup."),
23494 NULL,
23495 show_dwarf_max_cache_age,
23496 &set_dwarf_cmdlist,
23497 &show_dwarf_cmdlist);
156942c7 23498
cd4fb1b2
SM
23499 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23500Set debugging of the DWARF reader."), _("\
23501Show debugging of the DWARF reader."), _("\
23502When enabled (non-zero), debugging messages are printed during DWARF\n\
23503reading and symtab expansion. A value of 1 (one) provides basic\n\
23504information. A value greater than 1 provides more verbose information."),
23505 NULL,
23506 NULL,
23507 &setdebuglist, &showdebuglist);
9291a0cd 23508
cd4fb1b2
SM
23509 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23510Set debugging of the DWARF DIE reader."), _("\
23511Show debugging of the DWARF DIE reader."), _("\
23512When enabled (non-zero), DIEs are dumped after they are read in.\n\
23513The value is the maximum depth to print."),
23514 NULL,
23515 NULL,
23516 &setdebuglist, &showdebuglist);
9291a0cd 23517
cd4fb1b2
SM
23518 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23519Set debugging of the dwarf line reader."), _("\
23520Show debugging of the dwarf line reader."), _("\
23521When enabled (non-zero), line number entries are dumped as they are read in.\n\
23522A value of 1 (one) provides basic information.\n\
23523A value greater than 1 provides more verbose information."),
23524 NULL,
23525 NULL,
23526 &setdebuglist, &showdebuglist);
437afbb8 23527
cd4fb1b2
SM
23528 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23529Set cross-checking of \"physname\" code against demangler."), _("\
23530Show cross-checking of \"physname\" code against demangler."), _("\
23531When enabled, GDB's internal \"physname\" code is checked against\n\
23532the demangler."),
23533 NULL, show_check_physname,
23534 &setdebuglist, &showdebuglist);
900e11f9 23535
e615022a
DE
23536 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23537 no_class, &use_deprecated_index_sections, _("\
23538Set whether to use deprecated gdb_index sections."), _("\
23539Show whether to use deprecated gdb_index sections."), _("\
23540When enabled, deprecated .gdb_index sections are used anyway.\n\
23541Normally they are ignored either because of a missing feature or\n\
23542performance issue.\n\
23543Warning: This option must be enabled before gdb reads the file."),
23544 NULL,
23545 NULL,
23546 &setlist, &showlist);
23547
f1e6e072
TT
23548 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23549 &dwarf2_locexpr_funcs);
23550 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23551 &dwarf2_loclist_funcs);
23552
23553 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23554 &dwarf2_block_frame_base_locexpr_funcs);
23555 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23556 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23557
23558#if GDB_SELF_TEST
23559 selftests::register_test ("dw2_expand_symtabs_matching",
23560 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23561 selftests::register_test ("dwarf2_find_containing_comp_unit",
23562 selftests::find_containing_comp_unit::run_test);
c62446b1 23563#endif
6502dd73 23564}
This page took 5.32074 seconds and 4 git commands to generate.