Add "objfile" parameter to two partial_symtab methods
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
9c6a1327
TT
1085struct variant_part_builder;
1086
1087/* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
2ddeaf8a
TT
1089
1090struct variant_field
1091{
9c6a1327
TT
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
2ddeaf8a
TT
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
9c6a1327
TT
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107};
1108
1109/* This represents a DW_TAG_variant_part. */
1110
1111struct variant_part_builder
1112{
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
2ddeaf8a
TT
1121};
1122
52059ffd
TT
1123struct nextfield
1124{
be2daae6
TT
1125 int accessibility = 0;
1126 int virtuality = 0;
9c6a1327
TT
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
be2daae6 1131 struct field field {};
52059ffd
TT
1132};
1133
1134struct fnfieldlist
1135{
be2daae6
TT
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
52059ffd
TT
1138};
1139
c906108c
SS
1140/* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143struct field_info
c5aa993b 1144 {
0963b4bd 1145 /* List of data member and baseclasses fields. */
be2daae6
TT
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
c906108c 1148
85102364 1149 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1150 int non_public_fields = 0;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
be2daae6 1155 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1159 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
be2daae6 1163 std::vector<struct decl_field> nested_types_list;
317f7127 1164
9c6a1327
TT
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
317f7127
TT
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
c5aa993b 1177 };
c906108c 1178
ae038cb0
DJ
1179/* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1184static int dwarf_max_cache_age = 5;
920d2a44 1185static void
b4f54984
DE
1186show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
920d2a44 1188{
3e43a32a 1189 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1190 "DWARF compilation units is %s.\n"),
920d2a44
AC
1191 value);
1192}
4390d890 1193\f
c906108c
SS
1194/* local function prototypes */
1195
918dd910
JK
1196static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
891813be 1199static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
f1902523
JK
1202static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
3e225074 1204 struct die_info *type_unit_die);
f1902523 1205
ed2dc618
SM
1206static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1208
72bf9492
DJ
1209static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1211 int, struct dwarf2_cu *);
c906108c 1212
72bf9492
DJ
1213static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
63d06c5c 1215
72bf9492
DJ
1216static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1218 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1219
5d7cb8df 1220static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1221 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1222 struct dwarf2_cu *cu);
1223
72bf9492
DJ
1224static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
91c24f0a 1226
bc30ff58
JB
1227static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1229 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1230
d521ce57 1231static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1232
dee91e82 1233static struct partial_die_info *load_partial_dies
d521ce57 1234 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1235
fb816e8b
TV
1236/* A pair of partial_die_info and compilation unit. */
1237struct cu_partial_die_info
1238{
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
122cf0f2
AB
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
405feb71 1247 { /* Nothing. */ }
122cf0f2
AB
1248
1249private:
1250 cu_partial_die_info () = delete;
fb816e8b
TV
1251};
1252
122cf0f2
AB
1253static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
72bf9492 1255
d521ce57
TT
1256static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1258 const gdb_byte *, bool *need_reprocess);
1259
1260static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1264
ed2dc618
SM
1265static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1268
ed2dc618
SM
1269static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1272
ed2dc618 1273static const char *read_indirect_string_at_offset
4f44ae6c 1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1275
d521ce57
TT
1276static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
3019eac3
DE
1278 unsigned int *);
1279
18a8505e
AT
1280static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
3019eac3 1285
e142c38c 1286static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1287
e142c38c
DJ
1288static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
c906108c 1290
7d45c7c3
KB
1291static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
a084a2a6
AT
1294static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
05cf31d1
JB
1296static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
e142c38c 1299static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1300
e142c38c 1301static struct die_info *die_specification (struct die_info *die,
f2f0e013 1302 struct dwarf2_cu **);
63d06c5c 1303
9c541725 1304static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1305 struct dwarf2_cu *cu);
debd256d 1306
f3f5162e 1307static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1308 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1309 CORE_ADDR, int decode_mapping);
c906108c 1310
804d2729
TT
1311static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
c906108c 1313
a14ed312 1314static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1315 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1316
ff39bb5e 1317static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1318 struct dwarf2_cu *);
c906108c 1319
ff39bb5e 1320static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
12df843f 1324 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1325 const gdb_byte **bytes,
98bfdba5 1326 struct dwarf2_locexpr_baton **baton);
2df3850c 1327
e7c27a73 1328static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1329
b4ba55a1
JB
1330static int need_gnat_info (struct dwarf2_cu *);
1331
3e43a32a
MS
1332static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
b4ba55a1
JB
1334
1335static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
e7c27a73
DJ
1338static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
c906108c 1340
ff39bb5e 1341static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1342 struct dwarf2_cu *);
c906108c 1343
f792889a 1344static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1345
673bfd45
DE
1346static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
0d5cff50 1348static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1349
6e70227d 1350static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
63d06c5c 1353
e7c27a73 1354static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1355
348e048f
DE
1356static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
e7c27a73 1358static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1359
e7c27a73 1360static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1361
96408a79
SA
1362static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
71a3c369
TT
1364static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
ff013f42 1366static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1367 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1368
41144253 1369/* Return the .debug_loclists section to use for cu. */
1370static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
3a2b436a 1372/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1373 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1374enum pc_bounds_kind
1375{
e385593e 1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1377 PC_BOUNDS_NOT_PRESENT,
1378
e385593e
JK
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
3a2b436a
JK
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388};
1389
1390static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
891813be 1393 dwarf2_psymtab *);
c906108c 1394
fae299cd
DC
1395static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
801e3a5b
JB
1399static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
a14ed312 1402static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
a14ed312 1405static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1406 struct type *, struct dwarf2_cu *);
c906108c 1407
a14ed312 1408static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1409 struct die_info *, struct type *,
e7c27a73 1410 struct dwarf2_cu *);
c906108c 1411
a14ed312 1412static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1413 struct type *,
1414 struct dwarf2_cu *);
c906108c 1415
134d01f1 1416static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1419
e7c27a73 1420static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1421
5d7cb8df
JK
1422static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
804d2729 1424static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1425
27aa8d6a
SW
1426static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
74921315
KS
1428static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
f55ee35c
JK
1430static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
38d518c9 1433static const char *namespace_name (struct die_info *die,
e142c38c 1434 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1435
134d01f1 1436static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
7d79de9a
TT
1438static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
c906108c 1440
6e70227d 1441static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1442 struct dwarf2_cu *);
1443
bf6af496 1444static struct die_info *read_die_and_siblings_1
d521ce57 1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1446 struct die_info *);
639d11d3 1447
dee91e82 1448static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
639d11d3
DC
1451 struct die_info *parent);
1452
d521ce57
TT
1453static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
3e225074 1455 int);
3019eac3 1456
d521ce57 1457static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1458 struct die_info **, const gdb_byte *);
93311388 1459
e7c27a73 1460static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1461
15d034d0 1462static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1463 struct objfile *);
71c25dea 1464
15d034d0 1465static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1466
15d034d0 1467static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
ca69b9e6
DE
1471static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
e142c38c 1474static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1475 struct dwarf2_cu **);
9219021c 1476
d97bc12b
DE
1477static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479static void dump_die_for_error (struct die_info *);
1480
1481static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
c906108c 1483
d97bc12b 1484/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1485
51545339 1486static void store_in_ref_table (struct die_info *,
10b3939b 1487 struct dwarf2_cu *);
c906108c 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
4390d890
DE
1680static void
1681dwarf2_debug_line_missing_file_complaint (void)
1682{
b98664d3 1683 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_end_sequence_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line "
4390d890
DE
1690 "program sequence without an end"));
1691}
1692
1693static void
1694dwarf2_complex_location_expr_complaint (void)
1695{
b98664d3 1696 complaint (_("location expression too complex"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702{
b98664d3 1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1704 arg1, arg2, arg3);
1705}
1706
4390d890
DE
1707static void
1708dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709{
b98664d3 1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1711 arg1, arg2);
1712}
527f3840
JK
1713
1714/* Hash function for line_header_hash. */
1715
1716static hashval_t
1717line_header_hash (const struct line_header *ofs)
1718{
9c541725 1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1720}
1721
1722/* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724static hashval_t
1725line_header_hash_voidp (const void *item)
1726{
9a3c8263 1727 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1728
1729 return line_header_hash (ofs);
1730}
1731
1732/* Equality function for line_header_hash. */
1733
1734static int
1735line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736{
9a3c8263
SM
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1739
9c541725 1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742}
1743
4390d890 1744\f
9291a0cd 1745
330cdd98
PA
1746/* See declaration. */
1747
1748dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
330cdd98
PA
1753{
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761}
1762
1763dwarf2_per_objfile::~dwarf2_per_objfile ()
1764{
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
b76e467d 1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1769 per_cu->imported_symtabs_free ();
fc8e7e75 1770
b2bdb8cf 1771 for (signatured_type *sig_type : all_type_units)
ae640021 1772 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1773
330cdd98
PA
1774 /* Everything else should be on the objfile obstack. */
1775}
1776
1777/* See declaration. */
1778
1779void
1780dwarf2_per_objfile::free_cached_comp_units ()
1781{
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
fcd3b13d 1788 delete per_cu->cu;
330cdd98
PA
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792}
1793
11ed8cad
TT
1794/* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797class free_cached_comp_units
1798{
1799public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816};
1817
c906108c 1818/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
c906108c
SS
1824
1825int
251d32d9 1826dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
c906108c 1829{
97cbe998
SDJ
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
ed2dc618
SM
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
5bfd760d 1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1838 names,
1839 can_copy);
5bfd760d 1840
73869dc2 1841 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1842 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1843 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1845}
1846
251d32d9
TG
1847/* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
233a11ab
CS
1849
1850static int
251d32d9
TG
1851section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
233a11ab 1853{
251d32d9
TG
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
233a11ab
CS
1861}
1862
330cdd98 1863/* See declaration. */
c906108c 1864
330cdd98
PA
1865void
1866dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
c906108c 1868{
fd361982 1869 flagword aflag = bfd_section_flags (sectp);
251d32d9 1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
950b7495
KS
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
330cdd98 1883 else if (section_is_p (sectp->name, &names.info))
c906108c 1884 {
330cdd98 1885 this->info.s.section = sectp;
fd361982 1886 this->info.size = bfd_section_size (sectp);
c906108c 1887 }
330cdd98 1888 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1889 {
330cdd98 1890 this->abbrev.s.section = sectp;
fd361982 1891 this->abbrev.size = bfd_section_size (sectp);
c906108c 1892 }
330cdd98 1893 else if (section_is_p (sectp->name, &names.line))
c906108c 1894 {
330cdd98 1895 this->line.s.section = sectp;
fd361982 1896 this->line.size = bfd_section_size (sectp);
c906108c 1897 }
330cdd98 1898 else if (section_is_p (sectp->name, &names.loc))
c906108c 1899 {
330cdd98 1900 this->loc.s.section = sectp;
fd361982 1901 this->loc.size = bfd_section_size (sectp);
c906108c 1902 }
330cdd98 1903 else if (section_is_p (sectp->name, &names.loclists))
43988095 1904 {
330cdd98 1905 this->loclists.s.section = sectp;
fd361982 1906 this->loclists.size = bfd_section_size (sectp);
43988095 1907 }
330cdd98 1908 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1909 {
330cdd98 1910 this->macinfo.s.section = sectp;
fd361982 1911 this->macinfo.size = bfd_section_size (sectp);
c906108c 1912 }
330cdd98 1913 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1914 {
330cdd98 1915 this->macro.s.section = sectp;
fd361982 1916 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1917 }
330cdd98 1918 else if (section_is_p (sectp->name, &names.str))
c906108c 1919 {
330cdd98 1920 this->str.s.section = sectp;
fd361982 1921 this->str.size = bfd_section_size (sectp);
c906108c 1922 }
18a8505e
AT
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
330cdd98 1928 else if (section_is_p (sectp->name, &names.line_str))
43988095 1929 {
330cdd98 1930 this->line_str.s.section = sectp;
fd361982 1931 this->line_str.size = bfd_section_size (sectp);
43988095 1932 }
330cdd98 1933 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1934 {
330cdd98 1935 this->addr.s.section = sectp;
fd361982 1936 this->addr.size = bfd_section_size (sectp);
3019eac3 1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1939 {
330cdd98 1940 this->frame.s.section = sectp;
fd361982 1941 this->frame.size = bfd_section_size (sectp);
b6af0555 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1944 {
330cdd98 1945 this->eh_frame.s.section = sectp;
fd361982 1946 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1949 {
330cdd98 1950 this->ranges.s.section = sectp;
fd361982 1951 this->ranges.size = bfd_section_size (sectp);
af34e669 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1954 {
330cdd98 1955 this->rnglists.s.section = sectp;
fd361982 1956 this->rnglists.size = bfd_section_size (sectp);
43988095 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.types))
348e048f 1959 {
8b70b953
TT
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
049412e3 1963 type_section.s.section = sectp;
fd361982 1964 type_section.size = bfd_section_size (sectp);
8b70b953 1965
fd5866f6 1966 this->types.push_back (type_section);
348e048f 1967 }
330cdd98 1968 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1969 {
330cdd98 1970 this->gdb_index.s.section = sectp;
fd361982 1971 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1972 }
927aa2e7
JK
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
fd361982 1976 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
fd361982 1981 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1982 }
dce234bc 1983
fd361982
AM
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
330cdd98 1986 this->has_section_at_zero = true;
c906108c
SS
1987}
1988
dce234bc 1989/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1990 SECTION_NAME. */
af34e669 1991
dce234bc 1992void
3017a003
TG
1993dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
d521ce57 1995 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1996 bfd_size_type *sizep)
1997{
5bfd760d 1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1999 struct dwarf2_section_info *info;
a3b2a86b
TT
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
3017a003
TG
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
dce234bc 2021
96b79293 2022 info->read (objfile);
dce234bc 2023
96b79293 2024 *sectp = info->get_bfd_section ();
dce234bc
PP
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027}
2028
36586728
TT
2029/* A helper function to find the sections for a .dwz file. */
2030
2031static void
2032locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033{
9a3c8263 2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
049412e3 2040 dwz_file->abbrev.s.section = sectp;
fd361982 2041 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
049412e3 2045 dwz_file->info.s.section = sectp;
fd361982 2046 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
049412e3 2050 dwz_file->str.s.section = sectp;
fd361982 2051 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
049412e3 2055 dwz_file->line.s.section = sectp;
fd361982 2056 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
049412e3 2060 dwz_file->macro.s.section = sectp;
fd361982 2061 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2062 }
2ec9a5e0
TT
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
049412e3 2065 dwz_file->gdb_index.s.section = sectp;
fd361982 2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2067 }
927aa2e7
JK
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
fd361982 2071 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2072 }
36586728
TT
2073}
2074
c4973306 2075/* See dwarf2read.h. */
36586728 2076
c4973306 2077struct dwz_file *
ed2dc618 2078dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2079{
36586728 2080 const char *filename;
acd13123 2081 bfd_size_type buildid_len_arg;
dc294be5
TT
2082 size_t buildid_len;
2083 bfd_byte *buildid;
36586728
TT
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2086 return dwarf2_per_objfile->dwz_file.get ();
36586728 2087
4db1a1dc 2088 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
4db1a1dc
TT
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
791afaa2
TT
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2101
acd13123
TT
2102 buildid_len = (size_t) buildid_len_arg;
2103
791afaa2 2104 filename = data.get ();
d721ba37
PA
2105
2106 std::string abs_storage;
36586728
TT
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
14278e1f
TT
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2111
14278e1f 2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2113 filename = abs_storage.c_str ();
36586728
TT
2114 }
2115
dc294be5
TT
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
ad80db5b 2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2119 if (dwz_bfd != NULL)
36586728 2120 {
192b62ce 2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2122 dwz_bfd.reset (nullptr);
36586728
TT
2123 }
2124
dc294be5
TT
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
0d79cdc4
AM
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
ad80db5b 2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
dc294be5
TT
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
7ff8cb8c
TT
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2157
7ff8cb8c
TT
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
36586728 2160
7ff8cb8c
TT
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
36586728 2165}
9291a0cd 2166\f
7b9f3c50
DE
2167/* DWARF quick_symbols_functions support. */
2168
2169/* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174struct quick_file_names
2175{
094b34ac
DE
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
7b9f3c50
DE
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189};
2190
2191/* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194struct dwarf2_per_cu_quick_data
2195{
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
43f3e411 2203 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212};
2213
094b34ac
DE
2214/* Utility hash function for a stmt_list_hash. */
2215
2216static hashval_t
2217hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218{
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2223 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2224 return v;
2225}
2226
2227/* Utility equality function for a stmt_list_hash. */
2228
2229static int
2230eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232{
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
9c541725 2239 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2240}
2241
7b9f3c50
DE
2242/* Hash function for a quick_file_names. */
2243
2244static hashval_t
2245hash_file_name_entry (const void *e)
2246{
9a3c8263
SM
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
7b9f3c50 2249
094b34ac 2250 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2251}
2252
2253/* Equality function for a quick_file_names. */
2254
2255static int
2256eq_file_name_entry (const void *a, const void *b)
2257{
9a3c8263
SM
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2260
094b34ac 2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2262}
2263
2264/* Delete function for a quick_file_names. */
2265
2266static void
2267delete_file_name_entry (void *e)
2268{
9a3c8263 2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
45940949
TT
2279 /* The space for the struct itself lives on the obstack, so we don't
2280 free it here. */
7b9f3c50
DE
2281}
2282
2283/* Create a quick_file_names hash table. */
2284
5895093f 2285static htab_up
7b9f3c50
DE
2286create_quick_file_names_table (unsigned int nr_initial_entries)
2287{
5895093f
TT
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2291}
9291a0cd 2292
918dd910
JK
2293/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297static void
58f0c718 2298load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2299{
3019eac3 2300 if (per_cu->is_debug_types)
e5fe5e75 2301 load_full_type_unit (per_cu);
918dd910 2302 else
58f0c718 2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2304
cc12ce38
DE
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2dc860c0
DE
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2309}
2310
a0f42c21 2311/* Read in the symbols for PER_CU. */
2fdf6df6 2312
9291a0cd 2313static void
58f0c718 2314dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2315{
ed2dc618 2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2317
f4dc4d17
DE
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
197400e8 2320 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2321 return;
2322
b303c6f6
AB
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
39856def 2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2327
95554aad 2328 if (dwarf2_per_objfile->using_index
43f3e411 2329 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
58f0c718 2333 load_cu (per_cu, skip_partial);
89e63ee4
DE
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
cc12ce38 2339 && per_cu->cu != NULL
89e63ee4
DE
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
ed2dc618 2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2345 queue_and_load_all_dwo_tus (per_cu);
95554aad 2346 }
9291a0cd 2347
ed2dc618 2348 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
ed2dc618 2352 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
43f3e411 2359static struct compunit_symtab *
58f0c718 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2361{
ed2dc618
SM
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
95554aad 2364 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2365 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2366 {
11ed8cad 2367 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2368 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2370 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2371 }
f194fefb 2372
43f3e411 2373 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2374}
2375
ff4c9fec 2376/* See declaration. */
f4dc4d17 2377
ff4c9fec
SM
2378dwarf2_per_cu_data *
2379dwarf2_per_objfile::get_cutu (int index)
2380{
b76e467d 2381 if (index >= this->all_comp_units.size ())
ff4c9fec 2382 {
b76e467d 2383 index -= this->all_comp_units.size ();
b2bdb8cf 2384 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2385 return &this->all_type_units[index]->per_cu;
2386 }
f4dc4d17 2387
ff4c9fec
SM
2388 return this->all_comp_units[index];
2389}
f4dc4d17 2390
ff4c9fec 2391/* See declaration. */
2fdf6df6 2392
ff4c9fec
SM
2393dwarf2_per_cu_data *
2394dwarf2_per_objfile::get_cu (int index)
1fd400ff 2395{
b76e467d 2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2397
ff4c9fec 2398 return this->all_comp_units[index];
f4dc4d17
DE
2399}
2400
ff4c9fec 2401/* See declaration. */
f4dc4d17 2402
ff4c9fec
SM
2403signatured_type *
2404dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2405{
b2bdb8cf 2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2407
ff4c9fec 2408 return this->all_type_units[index];
1fd400ff
TT
2409}
2410
45940949
TT
2411/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2412 obstack, and constructed with the specified field values. */
4b514bc8
JK
2413
2414static dwarf2_per_cu_data *
ed2dc618 2415create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2416 struct dwarf2_section_info *section,
2417 int is_dwz,
2418 sect_offset sect_off, ULONGEST length)
2419{
2420 dwarf2_per_cu_data *the_cu
45940949 2421 = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
4b514bc8
JK
2422 struct dwarf2_per_cu_data);
2423 the_cu->sect_off = sect_off;
2424 the_cu->length = length;
e3b94546 2425 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2426 the_cu->section = section;
45940949
TT
2427 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
2428 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2429 the_cu->is_dwz = is_dwz;
2430 return the_cu;
2431}
2432
2ec9a5e0
TT
2433/* A helper for create_cus_from_index that handles a given list of
2434 CUs. */
2fdf6df6 2435
74a0d9f6 2436static void
12359b5e 2437create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2438 const gdb_byte *cu_list, offset_type n_elements,
2439 struct dwarf2_section_info *section,
b76e467d 2440 int is_dwz)
9291a0cd 2441{
12359b5e 2442 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2443 {
74a0d9f6 2444 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2445
2446 sect_offset sect_off
2447 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2448 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2449 cu_list += 2 * 8;
2450
b76e467d 2451 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2452 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2453 sect_off, length);
b76e467d 2454 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2455 }
9291a0cd
TT
2456}
2457
2ec9a5e0 2458/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2459 the CU objects for this objfile. */
2ec9a5e0 2460
74a0d9f6 2461static void
12359b5e 2462create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2463 const gdb_byte *cu_list, offset_type cu_list_elements,
2464 const gdb_byte *dwz_list, offset_type dwz_elements)
2465{
b76e467d
SM
2466 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2467 dwarf2_per_objfile->all_comp_units.reserve
2468 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2469
12359b5e 2470 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2471 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2472
2473 if (dwz_elements == 0)
74a0d9f6 2474 return;
2ec9a5e0 2475
12359b5e
SM
2476 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2477 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2478 &dwz->info, 1);
2ec9a5e0
TT
2479}
2480
1fd400ff 2481/* Create the signatured type hash table from the index. */
673bfd45 2482
74a0d9f6 2483static void
12359b5e
SM
2484create_signatured_type_table_from_index
2485 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2486 struct dwarf2_section_info *section,
2487 const gdb_byte *bytes,
2488 offset_type elements)
1fd400ff 2489{
b2bdb8cf
SM
2490 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2491 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2492
298e9637 2493 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2494
12359b5e 2495 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2496 {
52dc124a 2497 struct signatured_type *sig_type;
9c541725 2498 ULONGEST signature;
1fd400ff 2499 void **slot;
9c541725 2500 cu_offset type_offset_in_tu;
1fd400ff 2501
74a0d9f6 2502 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2503 sect_offset sect_off
2504 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2505 type_offset_in_tu
2506 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2507 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2508 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2509 bytes += 3 * 8;
2510
45940949 2511 sig_type = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
1fd400ff 2512 struct signatured_type);
52dc124a 2513 sig_type->signature = signature;
9c541725 2514 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2515 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2516 sig_type->per_cu.section = section;
9c541725 2517 sig_type->per_cu.sect_off = sect_off;
e3b94546 2518 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2519 sig_type->per_cu.v.quick
45940949 2520 = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
1fd400ff
TT
2521 struct dwarf2_per_cu_quick_data);
2522
b0b6a987 2523 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2524 *slot = sig_type;
1fd400ff 2525
b2bdb8cf 2526 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2527 }
2528
b0b6a987 2529 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2530}
2531
927aa2e7
JK
2532/* Create the signatured type hash table from .debug_names. */
2533
2534static void
2535create_signatured_type_table_from_debug_names
ed2dc618 2536 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2537 const mapped_debug_names &map,
2538 struct dwarf2_section_info *section,
2539 struct dwarf2_section_info *abbrev_section)
2540{
ed2dc618
SM
2541 struct objfile *objfile = dwarf2_per_objfile->objfile;
2542
96b79293
TT
2543 section->read (objfile);
2544 abbrev_section->read (objfile);
927aa2e7 2545
b2bdb8cf
SM
2546 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2547 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2548
298e9637 2549 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2550
2551 for (uint32_t i = 0; i < map.tu_count; ++i)
2552 {
2553 struct signatured_type *sig_type;
927aa2e7 2554 void **slot;
927aa2e7
JK
2555
2556 sect_offset sect_off
2557 = (sect_offset) (extract_unsigned_integer
2558 (map.tu_table_reordered + i * map.offset_size,
2559 map.offset_size,
2560 map.dwarf5_byte_order));
2561
2562 comp_unit_head cu_header;
ed2dc618
SM
2563 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2564 abbrev_section,
927aa2e7
JK
2565 section->buffer + to_underlying (sect_off),
2566 rcuh_kind::TYPE);
2567
45940949 2568 sig_type = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
927aa2e7
JK
2569 struct signatured_type);
2570 sig_type->signature = cu_header.signature;
2571 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2572 sig_type->per_cu.is_debug_types = 1;
2573 sig_type->per_cu.section = section;
2574 sig_type->per_cu.sect_off = sect_off;
e3b94546 2575 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2576 sig_type->per_cu.v.quick
45940949 2577 = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
927aa2e7
JK
2578 struct dwarf2_per_cu_quick_data);
2579
b0b6a987 2580 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2581 *slot = sig_type;
2582
b2bdb8cf 2583 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2584 }
2585
b0b6a987 2586 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2587}
2588
9291a0cd
TT
2589/* Read the address map data from the mapped index, and use it to
2590 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2591
9291a0cd 2592static void
ed2dc618
SM
2593create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2594 struct mapped_index *index)
9291a0cd 2595{
ed2dc618 2596 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2597 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2598 const gdb_byte *iter, *end;
9291a0cd 2599 struct addrmap *mutable_map;
9291a0cd
TT
2600 CORE_ADDR baseaddr;
2601
8268c778
PA
2602 auto_obstack temp_obstack;
2603
9291a0cd
TT
2604 mutable_map = addrmap_create_mutable (&temp_obstack);
2605
f00a2de2
PA
2606 iter = index->address_table.data ();
2607 end = iter + index->address_table.size ();
9291a0cd 2608
b3b3bada 2609 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2610
2611 while (iter < end)
2612 {
2613 ULONGEST hi, lo, cu_index;
2614 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2615 iter += 8;
2616 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2617 iter += 8;
2618 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2619 iter += 4;
f652bce2 2620
24a55014 2621 if (lo > hi)
f652bce2 2622 {
b98664d3 2623 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2624 hex_string (lo), hex_string (hi));
24a55014 2625 continue;
f652bce2 2626 }
24a55014 2627
b76e467d 2628 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2629 {
b98664d3 2630 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2631 (unsigned) cu_index);
24a55014 2632 continue;
f652bce2 2633 }
24a55014 2634
79748972
TT
2635 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2636 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2637 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2638 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2639 }
2640
d320c2b5 2641 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2642 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2643}
2644
927aa2e7
JK
2645/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2646 populate the objfile's psymtabs_addrmap. */
2647
2648static void
ed2dc618 2649create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2650 struct dwarf2_section_info *section)
2651{
ed2dc618 2652 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2653 bfd *abfd = objfile->obfd;
08feed99 2654 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2655 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2656
2657 auto_obstack temp_obstack;
2658 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2659
2660 std::unordered_map<sect_offset,
2661 dwarf2_per_cu_data *,
2662 gdb::hash_enum<sect_offset>>
2663 debug_info_offset_to_per_cu;
b76e467d 2664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2665 {
927aa2e7
JK
2666 const auto insertpair
2667 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2668 if (!insertpair.second)
2669 {
2670 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2671 "debug_info_offset %s, ignoring .debug_aranges."),
2672 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2673 return;
2674 }
2675 }
2676
96b79293 2677 section->read (objfile);
927aa2e7
JK
2678
2679 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2680
2681 const gdb_byte *addr = section->buffer;
2682
2683 while (addr < section->buffer + section->size)
2684 {
2685 const gdb_byte *const entry_addr = addr;
2686 unsigned int bytes_read;
2687
2688 const LONGEST entry_length = read_initial_length (abfd, addr,
2689 &bytes_read);
2690 addr += bytes_read;
2691
2692 const gdb_byte *const entry_end = addr + entry_length;
2693 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2694 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2695 if (addr + entry_length > section->buffer + section->size)
2696 {
47e3f474 2697 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2698 "length %s exceeds section length %s, "
2699 "ignoring .debug_aranges."),
47e3f474
TV
2700 objfile_name (objfile),
2701 plongest (entry_addr - section->buffer),
927aa2e7
JK
2702 plongest (bytes_read + entry_length),
2703 pulongest (section->size));
2704 return;
2705 }
2706
2707 /* The version number. */
2708 const uint16_t version = read_2_bytes (abfd, addr);
2709 addr += 2;
2710 if (version != 2)
2711 {
47e3f474 2712 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2713 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2716 return;
2717 }
2718
2719 const uint64_t debug_info_offset
2720 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2721 addr += offset_size;
2722 const auto per_cu_it
2723 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2724 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2725 {
47e3f474 2726 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2727 "debug_info_offset %s does not exists, "
2728 "ignoring .debug_aranges."),
47e3f474
TV
2729 objfile_name (objfile),
2730 plongest (entry_addr - section->buffer),
927aa2e7
JK
2731 pulongest (debug_info_offset));
2732 return;
2733 }
2734 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2735
2736 const uint8_t address_size = *addr++;
2737 if (address_size < 1 || address_size > 8)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2743 return;
2744 }
2745
2746 const uint8_t segment_selector_size = *addr++;
2747 if (segment_selector_size != 0)
2748 {
47e3f474 2749 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2750 "segment_selector_size %u is not supported, "
2751 "ignoring .debug_aranges."),
47e3f474
TV
2752 objfile_name (objfile),
2753 plongest (entry_addr - section->buffer),
927aa2e7
JK
2754 segment_selector_size);
2755 return;
2756 }
2757
2758 /* Must pad to an alignment boundary that is twice the address
2759 size. It is undocumented by the DWARF standard but GCC does
2760 use it. */
2761 for (size_t padding = ((-(addr - section->buffer))
2762 & (2 * address_size - 1));
2763 padding > 0; padding--)
2764 if (*addr++ != 0)
2765 {
47e3f474 2766 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2767 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer));
927aa2e7
JK
2770 return;
2771 }
2772
2773 for (;;)
2774 {
2775 if (addr + 2 * address_size > entry_end)
2776 {
47e3f474 2777 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2778 "address list is not properly terminated, "
2779 "ignoring .debug_aranges."),
47e3f474
TV
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer));
927aa2e7
JK
2782 return;
2783 }
2784 ULONGEST start = extract_unsigned_integer (addr, address_size,
2785 dwarf5_byte_order);
2786 addr += address_size;
2787 ULONGEST length = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 if (start == 0 && length == 0)
2791 break;
2792 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2793 {
2794 /* Symbol was eliminated due to a COMDAT group. */
2795 continue;
2796 }
2797 ULONGEST end = start + length;
79748972
TT
2798 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2799 - baseaddr);
2800 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2801 - baseaddr);
927aa2e7
JK
2802 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2803 }
2804 }
2805
d320c2b5 2806 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2807 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2808}
2809
9291a0cd
TT
2810/* Find a slot in the mapped index INDEX for the object named NAME.
2811 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2812 constant pool and return true. If NAME cannot be found, return
2813 false. */
2fdf6df6 2814
109483d9 2815static bool
9291a0cd
TT
2816find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2817 offset_type **vec_out)
2818{
0cf03b49 2819 offset_type hash;
9291a0cd 2820 offset_type slot, step;
559a7a62 2821 int (*cmp) (const char *, const char *);
9291a0cd 2822
791afaa2 2823 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2824 if (current_language->la_language == language_cplus
45280282
IB
2825 || current_language->la_language == language_fortran
2826 || current_language->la_language == language_d)
0cf03b49
JK
2827 {
2828 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2829 not contain any. */
a8719064 2830
72998fb3 2831 if (strchr (name, '(') != NULL)
0cf03b49 2832 {
109483d9 2833 without_params = cp_remove_params (name);
0cf03b49 2834
72998fb3 2835 if (without_params != NULL)
791afaa2 2836 name = without_params.get ();
0cf03b49
JK
2837 }
2838 }
2839
559a7a62 2840 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2841 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2842 simulate our NAME being searched is also lowercased. */
2843 hash = mapped_index_string_hash ((index->version == 4
2844 && case_sensitivity == case_sensitive_off
2845 ? 5 : index->version),
2846 name);
2847
f00a2de2
PA
2848 slot = hash & (index->symbol_table.size () - 1);
2849 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2850 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2851
2852 for (;;)
2853 {
9291a0cd 2854 const char *str;
f00a2de2
PA
2855
2856 const auto &bucket = index->symbol_table[slot];
2857 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2858 return false;
9291a0cd 2859
f00a2de2 2860 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2861 if (!cmp (name, str))
9291a0cd
TT
2862 {
2863 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2864 + MAYBE_SWAP (bucket.vec));
109483d9 2865 return true;
9291a0cd
TT
2866 }
2867
f00a2de2 2868 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2869 }
2870}
2871
4485a1c1
SM
2872/* A helper function that reads the .gdb_index from BUFFER and fills
2873 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2874 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2875 ok to use deprecated sections.
2876
2877 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2878 out parameters that are filled in with information about the CU and
2879 TU lists in the section.
2880
4485a1c1 2881 Returns true if all went well, false otherwise. */
2fdf6df6 2882
d33bc52e 2883static bool
3810f182 2884read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2885 bool deprecated_ok,
2886 gdb::array_view<const gdb_byte> buffer,
2887 struct mapped_index *map,
2888 const gdb_byte **cu_list,
2889 offset_type *cu_list_elements,
2890 const gdb_byte **types_list,
2891 offset_type *types_list_elements)
2892{
2893 const gdb_byte *addr = &buffer[0];
82430852 2894
9291a0cd 2895 /* Version check. */
4485a1c1 2896 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2897 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2898 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2899 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2900 indices. */
831adc1f 2901 if (version < 4)
481860b3
GB
2902 {
2903 static int warning_printed = 0;
2904 if (!warning_printed)
2905 {
2906 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2907 filename);
481860b3
GB
2908 warning_printed = 1;
2909 }
2910 return 0;
2911 }
2912 /* Index version 4 uses a different hash function than index version
2913 5 and later.
2914
2915 Versions earlier than 6 did not emit psymbols for inlined
2916 functions. Using these files will cause GDB not to be able to
2917 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2918 indices unless the user has done
2919 "set use-deprecated-index-sections on". */
2ec9a5e0 2920 if (version < 6 && !deprecated_ok)
481860b3
GB
2921 {
2922 static int warning_printed = 0;
2923 if (!warning_printed)
2924 {
e615022a
DE
2925 warning (_("\
2926Skipping deprecated .gdb_index section in %s.\n\
2927Do \"set use-deprecated-index-sections on\" before the file is read\n\
2928to use the section anyway."),
2ec9a5e0 2929 filename);
481860b3
GB
2930 warning_printed = 1;
2931 }
2932 return 0;
2933 }
796a7ff8 2934 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2935 of the TU (for symbols coming from TUs),
2936 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2937 Plus gold-generated indices can have duplicate entries for global symbols,
2938 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2939 These are just performance bugs, and we can't distinguish gdb-generated
2940 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2941
481860b3 2942 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2943 longer backward compatible. */
796a7ff8 2944 if (version > 8)
594e8718 2945 return 0;
9291a0cd 2946
559a7a62 2947 map->version = version;
9291a0cd 2948
4485a1c1 2949 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2950
4485a1c1 2951 int i = 0;
2ec9a5e0
TT
2952 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2953 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2954 / 8);
1fd400ff
TT
2955 ++i;
2956
2ec9a5e0
TT
2957 *types_list = addr + MAYBE_SWAP (metadata[i]);
2958 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2959 - MAYBE_SWAP (metadata[i]))
2960 / 8);
987d643c 2961 ++i;
1fd400ff 2962
f00a2de2
PA
2963 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2964 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2965 map->address_table
2966 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2967 ++i;
2968
f00a2de2
PA
2969 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2970 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2971 map->symbol_table
2972 = gdb::array_view<mapped_index::symbol_table_slot>
2973 ((mapped_index::symbol_table_slot *) symbol_table,
2974 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2975
f00a2de2 2976 ++i;
f9d83a0b 2977 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2978
2ec9a5e0
TT
2979 return 1;
2980}
2981
4485a1c1
SM
2982/* Callback types for dwarf2_read_gdb_index. */
2983
2984typedef gdb::function_view
2985 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2986 get_gdb_index_contents_ftype;
2987typedef gdb::function_view
2988 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2989 get_gdb_index_contents_dwz_ftype;
2990
927aa2e7 2991/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2992 elements of all the CUs and return 1. Otherwise, return 0. */
2993
2994static int
4485a1c1
SM
2995dwarf2_read_gdb_index
2996 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2997 get_gdb_index_contents_ftype get_gdb_index_contents,
2998 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2999{
2ec9a5e0
TT
3000 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3001 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3002 struct dwz_file *dwz;
12359b5e 3003 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3004
4485a1c1
SM
3005 gdb::array_view<const gdb_byte> main_index_contents
3006 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3007
3008 if (main_index_contents.empty ())
3009 return 0;
3010
3063847f 3011 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3012 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3013 use_deprecated_index_sections,
3014 main_index_contents, map.get (), &cu_list,
3015 &cu_list_elements, &types_list,
3016 &types_list_elements))
2ec9a5e0
TT
3017 return 0;
3018
0fefef59 3019 /* Don't use the index if it's empty. */
3063847f 3020 if (map->symbol_table.empty ())
0fefef59
DE
3021 return 0;
3022
2ec9a5e0
TT
3023 /* If there is a .dwz file, read it so we can get its CU list as
3024 well. */
ed2dc618 3025 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3026 if (dwz != NULL)
2ec9a5e0 3027 {
2ec9a5e0
TT
3028 struct mapped_index dwz_map;
3029 const gdb_byte *dwz_types_ignore;
3030 offset_type dwz_types_elements_ignore;
3031
4485a1c1
SM
3032 gdb::array_view<const gdb_byte> dwz_index_content
3033 = get_gdb_index_contents_dwz (objfile, dwz);
3034
3035 if (dwz_index_content.empty ())
3036 return 0;
3037
3810f182 3038 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3039 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3040 &dwz_list, &dwz_list_elements,
3041 &dwz_types_ignore,
3042 &dwz_types_elements_ignore))
2ec9a5e0
TT
3043 {
3044 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3045 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3046 return 0;
3047 }
3048 }
3049
12359b5e
SM
3050 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3051 dwz_list, dwz_list_elements);
1fd400ff 3052
8b70b953
TT
3053 if (types_list_elements)
3054 {
8b70b953
TT
3055 /* We can only handle a single .debug_types when we have an
3056 index. */
fd5866f6 3057 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3058 return 0;
3059
fd5866f6 3060 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3061
12359b5e
SM
3062 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3063 types_list, types_list_elements);
8b70b953 3064 }
9291a0cd 3065
3063847f 3066 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3067
3063847f 3068 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3069 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3070 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3071 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3072
3073 return 1;
3074}
3075
dee91e82 3076/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3077
dee91e82
DE
3078static void
3079dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3080 const gdb_byte *info_ptr,
3e225074 3081 struct die_info *comp_unit_die)
9291a0cd 3082{
dee91e82 3083 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3084 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3085 struct dwarf2_per_objfile *dwarf2_per_objfile
3086 = cu->per_cu->dwarf2_per_objfile;
094b34ac 3087 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3088 struct attribute *attr;
7b9f3c50
DE
3089 void **slot;
3090 struct quick_file_names *qfn;
9291a0cd 3091
0186c6a7
DE
3092 gdb_assert (! this_cu->is_debug_types);
3093
07261596
TT
3094 /* Our callers never want to match partial units -- instead they
3095 will match the enclosing full CU. */
3096 if (comp_unit_die->tag == DW_TAG_partial_unit)
3097 {
3098 this_cu->v.quick->no_file_data = 1;
3099 return;
3100 }
3101
0186c6a7 3102 lh_cu = this_cu;
7b9f3c50 3103 slot = NULL;
dee91e82 3104
fff8551c 3105 line_header_up lh;
9c541725 3106 sect_offset line_offset {};
fff8551c 3107
dee91e82 3108 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3109 if (attr != nullptr)
9291a0cd 3110 {
7b9f3c50
DE
3111 struct quick_file_names find_entry;
3112
9c541725 3113 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3114
3115 /* We may have already read in this line header (TU line header sharing).
3116 If we have we're done. */
094b34ac 3117 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3118 find_entry.hash.line_sect_off = line_offset;
5895093f 3119 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3120 &find_entry, INSERT);
3121 if (*slot != NULL)
3122 {
9a3c8263 3123 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3124 return;
7b9f3c50
DE
3125 }
3126
3019eac3 3127 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3128 }
3129 if (lh == NULL)
3130 {
094b34ac 3131 lh_cu->v.quick->no_file_data = 1;
dee91e82 3132 return;
9291a0cd
TT
3133 }
3134
45940949 3135 qfn = XOBNEW (&dwarf2_per_objfile->obstack, struct quick_file_names);
094b34ac 3136 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3137 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3138 gdb_assert (slot != NULL);
3139 *slot = qfn;
9291a0cd 3140
d721ba37 3141 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3142
aa391654
TT
3143 int offset = 0;
3144 if (strcmp (fnd.name, "<unknown>") != 0)
3145 ++offset;
3146
7ba99d21 3147 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3148 qfn->file_names =
45940949
TT
3149 XOBNEWVEC (&dwarf2_per_objfile->obstack, const char *,
3150 qfn->num_file_names);
aa391654
TT
3151 if (offset != 0)
3152 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3153 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3154 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3155 fnd.comp_dir).release ();
7b9f3c50 3156 qfn->real_names = NULL;
9291a0cd 3157
094b34ac 3158 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3159}
3160
3161/* A helper for the "quick" functions which attempts to read the line
3162 table for THIS_CU. */
3163
3164static struct quick_file_names *
e4a48d9d 3165dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3166{
0186c6a7
DE
3167 /* This should never be called for TUs. */
3168 gdb_assert (! this_cu->is_debug_types);
3169 /* Nor type unit groups. */
197400e8 3170 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3171
dee91e82
DE
3172 if (this_cu->v.quick->file_names != NULL)
3173 return this_cu->v.quick->file_names;
3174 /* If we know there is no line data, no point in looking again. */
3175 if (this_cu->v.quick->no_file_data)
3176 return NULL;
3177
c0ab21c2
TT
3178 cutu_reader reader (this_cu);
3179 if (!reader.dummy_p)
3e225074 3180 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3181
3182 if (this_cu->v.quick->no_file_data)
3183 return NULL;
3184 return this_cu->v.quick->file_names;
9291a0cd
TT
3185}
3186
3187/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3188 real path for a given file name from the line table. */
2fdf6df6 3189
9291a0cd 3190static const char *
45940949 3191dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3192 struct quick_file_names *qfn, int index)
9291a0cd 3193{
7b9f3c50 3194 if (qfn->real_names == NULL)
45940949 3195 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->obstack,
26f2dc30 3196 qfn->num_file_names, const char *);
9291a0cd 3197
7b9f3c50 3198 if (qfn->real_names[index] == NULL)
14278e1f 3199 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3200
7b9f3c50 3201 return qfn->real_names[index];
9291a0cd
TT
3202}
3203
3204static struct symtab *
3205dw2_find_last_source_symtab (struct objfile *objfile)
3206{
ed2dc618
SM
3207 struct dwarf2_per_objfile *dwarf2_per_objfile
3208 = get_dwarf2_per_objfile (objfile);
b76e467d 3209 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3210 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3211
43f3e411
DE
3212 if (cust == NULL)
3213 return NULL;
ed2dc618 3214
43f3e411 3215 return compunit_primary_filetab (cust);
9291a0cd
TT
3216}
3217
7b9f3c50
DE
3218/* Traversal function for dw2_forget_cached_source_info. */
3219
3220static int
3221dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3222{
7b9f3c50 3223 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3224
7b9f3c50 3225 if (file_data->real_names)
9291a0cd 3226 {
7b9f3c50 3227 int i;
9291a0cd 3228
7b9f3c50 3229 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3230 {
7b9f3c50
DE
3231 xfree ((void*) file_data->real_names[i]);
3232 file_data->real_names[i] = NULL;
9291a0cd
TT
3233 }
3234 }
7b9f3c50
DE
3235
3236 return 1;
3237}
3238
3239static void
3240dw2_forget_cached_source_info (struct objfile *objfile)
3241{
ed2dc618
SM
3242 struct dwarf2_per_objfile *dwarf2_per_objfile
3243 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3244
5895093f 3245 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3246 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3247}
3248
f8eba3c6
TT
3249/* Helper function for dw2_map_symtabs_matching_filename that expands
3250 the symtabs and calls the iterator. */
3251
3252static int
3253dw2_map_expand_apply (struct objfile *objfile,
3254 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3255 const char *name, const char *real_path,
14bc53a8 3256 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3257{
43f3e411 3258 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3259
3260 /* Don't visit already-expanded CUs. */
43f3e411 3261 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3262 return 0;
3263
3264 /* This may expand more than one symtab, and we want to iterate over
3265 all of them. */
58f0c718 3266 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3267
14bc53a8
PA
3268 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3269 last_made, callback);
f8eba3c6
TT
3270}
3271
3272/* Implementation of the map_symtabs_matching_filename method. */
3273
14bc53a8
PA
3274static bool
3275dw2_map_symtabs_matching_filename
3276 (struct objfile *objfile, const char *name, const char *real_path,
3277 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3278{
c011a4f4 3279 const char *name_basename = lbasename (name);
ed2dc618
SM
3280 struct dwarf2_per_objfile *dwarf2_per_objfile
3281 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3282
848e3e78
DE
3283 /* The rule is CUs specify all the files, including those used by
3284 any TU, so there's no need to scan TUs here. */
f4dc4d17 3285
b76e467d 3286 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3287 {
3d7bb9d9 3288 /* We only need to look at symtabs not already expanded. */
43f3e411 3289 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3290 continue;
3291
b76e467d 3292 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3293 if (file_data == NULL)
9291a0cd
TT
3294 continue;
3295
b76e467d 3296 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3297 {
7b9f3c50 3298 const char *this_name = file_data->file_names[j];
da235a7c 3299 const char *this_real_name;
9291a0cd 3300
af529f8f 3301 if (compare_filenames_for_search (this_name, name))
9291a0cd 3302 {
f5b95b50 3303 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3304 callback))
3305 return true;
288e77a7 3306 continue;
4aac40c8 3307 }
9291a0cd 3308
c011a4f4
DE
3309 /* Before we invoke realpath, which can get expensive when many
3310 files are involved, do a quick comparison of the basenames. */
3311 if (! basenames_may_differ
3312 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3313 continue;
3314
45940949
TT
3315 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3316 file_data, j);
da235a7c 3317 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3318 {
da235a7c 3319 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3320 callback))
3321 return true;
288e77a7 3322 continue;
da235a7c 3323 }
9291a0cd 3324
da235a7c
JK
3325 if (real_path != NULL)
3326 {
af529f8f
JK
3327 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3328 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3329 if (this_real_name != NULL
af529f8f 3330 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3331 {
f5b95b50 3332 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3333 callback))
3334 return true;
288e77a7 3335 continue;
9291a0cd
TT
3336 }
3337 }
3338 }
3339 }
3340
14bc53a8 3341 return false;
9291a0cd
TT
3342}
3343
da51c347
DE
3344/* Struct used to manage iterating over all CUs looking for a symbol. */
3345
3346struct dw2_symtab_iterator
9291a0cd 3347{
ed2dc618
SM
3348 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3349 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3350 /* If set, only look for symbols that match that block. Valid values are
3351 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3352 gdb::optional<block_enum> block_index;
da51c347
DE
3353 /* The kind of symbol we're looking for. */
3354 domain_enum domain;
3355 /* The list of CUs from the index entry of the symbol,
3356 or NULL if not found. */
3357 offset_type *vec;
3358 /* The next element in VEC to look at. */
3359 int next;
3360 /* The number of elements in VEC, or zero if there is no match. */
3361 int length;
8943b874
DE
3362 /* Have we seen a global version of the symbol?
3363 If so we can ignore all further global instances.
3364 This is to work around gold/15646, inefficient gold-generated
3365 indices. */
3366 int global_seen;
da51c347 3367};
9291a0cd 3368
2b79f376 3369/* Initialize the index symtab iterator ITER. */
2fdf6df6 3370
9291a0cd 3371static void
da51c347 3372dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3373 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3374 gdb::optional<block_enum> block_index,
da51c347
DE
3375 domain_enum domain,
3376 const char *name)
3377{
ed2dc618 3378 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3379 iter->block_index = block_index;
3380 iter->domain = domain;
3381 iter->next = 0;
8943b874 3382 iter->global_seen = 0;
da51c347 3383
3063847f 3384 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3385
3386 /* index is NULL if OBJF_READNOW. */
3387 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3388 iter->length = MAYBE_SWAP (*iter->vec);
3389 else
3390 {
3391 iter->vec = NULL;
3392 iter->length = 0;
3393 }
3394}
3395
3396/* Return the next matching CU or NULL if there are no more. */
3397
3398static struct dwarf2_per_cu_data *
3399dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3400{
ed2dc618
SM
3401 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3402
da51c347
DE
3403 for ( ; iter->next < iter->length; ++iter->next)
3404 {
3405 offset_type cu_index_and_attrs =
3406 MAYBE_SWAP (iter->vec[iter->next + 1]);
3407 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3408 gdb_index_symbol_kind symbol_kind =
3409 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3410 /* Only check the symbol attributes if they're present.
3411 Indices prior to version 7 don't record them,
3412 and indices >= 7 may elide them for certain symbols
3413 (gold does this). */
3414 int attrs_valid =
ed2dc618 3415 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3416 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3417
3190f0c6 3418 /* Don't crash on bad data. */
b76e467d 3419 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3420 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3421 {
b98664d3 3422 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3423 " [in module %s]"),
3424 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3425 continue;
3426 }
3427
ff4c9fec 3428 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3429
da51c347 3430 /* Skip if already read in. */
43f3e411 3431 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3432 continue;
3433
8943b874
DE
3434 /* Check static vs global. */
3435 if (attrs_valid)
3436 {
2b79f376
SM
3437 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3438
3439 if (iter->block_index.has_value ())
3440 {
3441 bool want_static = *iter->block_index == STATIC_BLOCK;
3442
3443 if (is_static != want_static)
3444 continue;
3445 }
3446
8943b874
DE
3447 /* Work around gold/15646. */
3448 if (!is_static && iter->global_seen)
3449 continue;
3450 if (!is_static)
3451 iter->global_seen = 1;
3452 }
da51c347
DE
3453
3454 /* Only check the symbol's kind if it has one. */
3455 if (attrs_valid)
3456 {
3457 switch (iter->domain)
3458 {
3459 case VAR_DOMAIN:
3460 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3461 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3462 /* Some types are also in VAR_DOMAIN. */
3463 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3464 continue;
3465 break;
3466 case STRUCT_DOMAIN:
3467 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3468 continue;
3469 break;
3470 case LABEL_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3472 continue;
3473 break;
59c35742
AB
3474 case MODULE_DOMAIN:
3475 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3476 continue;
3477 break;
da51c347
DE
3478 default:
3479 break;
3480 }
3481 }
3482
3483 ++iter->next;
3484 return per_cu;
3485 }
3486
3487 return NULL;
3488}
3489
43f3e411 3490static struct compunit_symtab *
c7f839cb 3491dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3492 const char *name, domain_enum domain)
9291a0cd 3493{
43f3e411 3494 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3495 struct dwarf2_per_objfile *dwarf2_per_objfile
3496 = get_dwarf2_per_objfile (objfile);
9291a0cd 3497
b5ec771e
PA
3498 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3499
ed2dc618
SM
3500 struct dw2_symtab_iterator iter;
3501 struct dwarf2_per_cu_data *per_cu;
da51c347 3502
2b79f376 3503 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3504
ed2dc618
SM
3505 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3506 {
3507 struct symbol *sym, *with_opaque = NULL;
58f0c718 3508 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3509 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3510 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3511
ed2dc618
SM
3512 sym = block_find_symbol (block, name, domain,
3513 block_find_non_opaque_type_preferred,
3514 &with_opaque);
b2e2f908 3515
ed2dc618
SM
3516 /* Some caution must be observed with overloaded functions
3517 and methods, since the index will not contain any overload
3518 information (but NAME might contain it). */
da51c347 3519
ed2dc618
SM
3520 if (sym != NULL
3521 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3522 return stab;
3523 if (with_opaque != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3525 stab_best = stab;
da51c347 3526
ed2dc618 3527 /* Keep looking through other CUs. */
9291a0cd 3528 }
9291a0cd 3529
da51c347 3530 return stab_best;
9291a0cd
TT
3531}
3532
3533static void
3534dw2_print_stats (struct objfile *objfile)
3535{
ed2dc618
SM
3536 struct dwarf2_per_objfile *dwarf2_per_objfile
3537 = get_dwarf2_per_objfile (objfile);
b76e467d 3538 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3539 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3540 int count = 0;
9291a0cd 3541
ed2dc618 3542 for (int i = 0; i < total; ++i)
9291a0cd 3543 {
ff4c9fec 3544 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3545
43f3e411 3546 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3547 ++count;
3548 }
e4a48d9d 3549 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3550 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3551}
3552
779bd270
DE
3553/* This dumps minimal information about the index.
3554 It is called via "mt print objfiles".
3555 One use is to verify .gdb_index has been loaded by the
3556 gdb.dwarf2/gdb-index.exp testcase. */
3557
9291a0cd
TT
3558static void
3559dw2_dump (struct objfile *objfile)
3560{
ed2dc618
SM
3561 struct dwarf2_per_objfile *dwarf2_per_objfile
3562 = get_dwarf2_per_objfile (objfile);
3563
779bd270
DE
3564 gdb_assert (dwarf2_per_objfile->using_index);
3565 printf_filtered (".gdb_index:");
3566 if (dwarf2_per_objfile->index_table != NULL)
3567 {
3568 printf_filtered (" version %d\n",
3569 dwarf2_per_objfile->index_table->version);
3570 }
3571 else
3572 printf_filtered (" faked for \"readnow\"\n");
3573 printf_filtered ("\n");
9291a0cd
TT
3574}
3575
9291a0cd
TT
3576static void
3577dw2_expand_symtabs_for_function (struct objfile *objfile,
3578 const char *func_name)
3579{
ed2dc618
SM
3580 struct dwarf2_per_objfile *dwarf2_per_objfile
3581 = get_dwarf2_per_objfile (objfile);
da51c347 3582
ed2dc618
SM
3583 struct dw2_symtab_iterator iter;
3584 struct dwarf2_per_cu_data *per_cu;
da51c347 3585
2b79f376 3586 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3587
ed2dc618 3588 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3589 dw2_instantiate_symtab (per_cu, false);
da51c347 3590
9291a0cd
TT
3591}
3592
3593static void
3594dw2_expand_all_symtabs (struct objfile *objfile)
3595{
ed2dc618
SM
3596 struct dwarf2_per_objfile *dwarf2_per_objfile
3597 = get_dwarf2_per_objfile (objfile);
b76e467d 3598 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3599 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3600
ed2dc618 3601 for (int i = 0; i < total_units; ++i)
9291a0cd 3602 {
ff4c9fec 3603 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3604
58f0c718
TT
3605 /* We don't want to directly expand a partial CU, because if we
3606 read it with the wrong language, then assertion failures can
3607 be triggered later on. See PR symtab/23010. So, tell
3608 dw2_instantiate_symtab to skip partial CUs -- any important
3609 partial CU will be read via DW_TAG_imported_unit anyway. */
3610 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3611 }
3612}
3613
3614static void
652a8996
JK
3615dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3616 const char *fullname)
9291a0cd 3617{
ed2dc618
SM
3618 struct dwarf2_per_objfile *dwarf2_per_objfile
3619 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3620
3621 /* We don't need to consider type units here.
3622 This is only called for examining code, e.g. expand_line_sal.
3623 There can be an order of magnitude (or more) more type units
3624 than comp units, and we avoid them if we can. */
3625
b76e467d 3626 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3627 {
3d7bb9d9 3628 /* We only need to look at symtabs not already expanded. */
43f3e411 3629 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3630 continue;
3631
b76e467d 3632 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3633 if (file_data == NULL)
9291a0cd
TT
3634 continue;
3635
b76e467d 3636 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3637 {
652a8996
JK
3638 const char *this_fullname = file_data->file_names[j];
3639
3640 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3641 {
58f0c718 3642 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3643 break;
3644 }
3645 }
3646 }
3647}
3648
9a0bacfb
TV
3649static void
3650dw2_expand_symtabs_matching_symbol
3651 (mapped_index_base &index,
3652 const lookup_name_info &lookup_name_in,
3653 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3654 enum search_domain kind,
3655 gdb::function_view<bool (offset_type)> match_callback);
3656
3657static void
3658dw2_expand_symtabs_matching_one
3659 (struct dwarf2_per_cu_data *per_cu,
3660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3661 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3662
9291a0cd 3663static void
199b4314
TT
3664dw2_map_matching_symbols
3665 (struct objfile *objfile,
b054970d 3666 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3667 int global,
3668 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3669 symbol_compare_ftype *ordered_compare)
9291a0cd 3670{
1aa98955
TV
3671 /* Used for Ada. */
3672 struct dwarf2_per_objfile *dwarf2_per_objfile
3673 = get_dwarf2_per_objfile (objfile);
3674
9a0bacfb
TV
3675 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3676
1aa98955
TV
3677 if (dwarf2_per_objfile->index_table != nullptr)
3678 {
3679 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3680 here though if the current language is Ada for a non-Ada objfile
9a0bacfb
TV
3681 using GNU index. */
3682 mapped_index &index = *dwarf2_per_objfile->index_table;
1aa98955 3683
9a0bacfb
TV
3684 const char *match_name = name.ada ().lookup_name ().c_str ();
3685 auto matcher = [&] (const char *symname)
3686 {
3687 if (ordered_compare == nullptr)
3688 return true;
3689 return ordered_compare (symname, match_name) == 0;
3690 };
3691
3692 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3693 [&] (offset_type namei)
3694 {
3695 struct dw2_symtab_iterator iter;
3696 struct dwarf2_per_cu_data *per_cu;
3697
3698 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3699 match_name);
3700 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3701 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
3702 return true;
3703 });
3704 }
3705 else
3706 {
3707 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3708 proceed assuming all symtabs have been read in. */
3709 }
1aa98955
TV
3710
3711 for (compunit_symtab *cust : objfile->compunits ())
3712 {
3713 const struct block *block;
3714
3715 if (cust == NULL)
3716 continue;
3717 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3718 if (!iterate_over_symbols_terminated (block, name,
3719 domain, callback))
3720 return;
3721 }
9291a0cd
TT
3722}
3723
e1ef7d7a
PA
3724/* Starting from a search name, return the string that finds the upper
3725 bound of all strings that start with SEARCH_NAME in a sorted name
3726 list. Returns the empty string to indicate that the upper bound is
3727 the end of the list. */
3728
3729static std::string
3730make_sort_after_prefix_name (const char *search_name)
3731{
3732 /* When looking to complete "func", we find the upper bound of all
3733 symbols that start with "func" by looking for where we'd insert
3734 the closest string that would follow "func" in lexicographical
3735 order. Usually, that's "func"-with-last-character-incremented,
3736 i.e. "fund". Mind non-ASCII characters, though. Usually those
3737 will be UTF-8 multi-byte sequences, but we can't be certain.
3738 Especially mind the 0xff character, which is a valid character in
3739 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3740 rule out compilers allowing it in identifiers. Note that
3741 conveniently, strcmp/strcasecmp are specified to compare
3742 characters interpreted as unsigned char. So what we do is treat
3743 the whole string as a base 256 number composed of a sequence of
3744 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3745 to 0, and carries 1 to the following more-significant position.
3746 If the very first character in SEARCH_NAME ends up incremented
3747 and carries/overflows, then the upper bound is the end of the
3748 list. The string after the empty string is also the empty
3749 string.
3750
3751 Some examples of this operation:
3752
3753 SEARCH_NAME => "+1" RESULT
3754
3755 "abc" => "abd"
3756 "ab\xff" => "ac"
3757 "\xff" "a" "\xff" => "\xff" "b"
3758 "\xff" => ""
3759 "\xff\xff" => ""
3760 "" => ""
3761
3762 Then, with these symbols for example:
3763
3764 func
3765 func1
3766 fund
3767
3768 completing "func" looks for symbols between "func" and
3769 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3770 which finds "func" and "func1", but not "fund".
3771
3772 And with:
3773
3774 funcÿ (Latin1 'ÿ' [0xff])
3775 funcÿ1
3776 fund
3777
3778 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3779 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3780
3781 And with:
3782
3783 ÿÿ (Latin1 'ÿ' [0xff])
3784 ÿÿ1
3785
3786 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3787 the end of the list.
3788 */
3789 std::string after = search_name;
3790 while (!after.empty () && (unsigned char) after.back () == 0xff)
3791 after.pop_back ();
3792 if (!after.empty ())
3793 after.back () = (unsigned char) after.back () + 1;
3794 return after;
3795}
3796
5c58de74 3797/* See declaration. */
61d96d7e 3798
5c58de74
PA
3799std::pair<std::vector<name_component>::const_iterator,
3800 std::vector<name_component>::const_iterator>
44ed8f3e 3801mapped_index_base::find_name_components_bounds
3b00ef10 3802 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3803{
5c58de74
PA
3804 auto *name_cmp
3805 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3806
3b00ef10 3807 const char *lang_name
e0802d59 3808 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3809
3f563c84
PA
3810 /* Comparison function object for lower_bound that matches against a
3811 given symbol name. */
3812 auto lookup_compare_lower = [&] (const name_component &elem,
3813 const char *name)
3814 {
5c58de74 3815 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3816 const char *elem_name = elem_qualified + elem.name_offset;
3817 return name_cmp (elem_name, name) < 0;
3818 };
3819
3820 /* Comparison function object for upper_bound that matches against a
3821 given symbol name. */
3822 auto lookup_compare_upper = [&] (const char *name,
3823 const name_component &elem)
3824 {
5c58de74 3825 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3826 const char *elem_name = elem_qualified + elem.name_offset;
3827 return name_cmp (name, elem_name) < 0;
3828 };
3829
5c58de74
PA
3830 auto begin = this->name_components.begin ();
3831 auto end = this->name_components.end ();
3f563c84
PA
3832
3833 /* Find the lower bound. */
3834 auto lower = [&] ()
3835 {
3b00ef10 3836 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3837 return begin;
3838 else
3b00ef10 3839 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3840 } ();
3841
3842 /* Find the upper bound. */
3843 auto upper = [&] ()
3844 {
5c58de74 3845 if (lookup_name_without_params.completion_mode ())
3f563c84 3846 {
e1ef7d7a
PA
3847 /* In completion mode, we want UPPER to point past all
3848 symbols names that have the same prefix. I.e., with
3849 these symbols, and completing "func":
3850
3851 function << lower bound
3852 function1
3853 other_function << upper bound
3854
3855 We find the upper bound by looking for the insertion
3856 point of "func"-with-last-character-incremented,
3857 i.e. "fund". */
3b00ef10 3858 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3859 if (after.empty ())
3f563c84 3860 return end;
e6b2f5ef
PA
3861 return std::lower_bound (lower, end, after.c_str (),
3862 lookup_compare_lower);
3f563c84
PA
3863 }
3864 else
3b00ef10 3865 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3866 } ();
3867
5c58de74
PA
3868 return {lower, upper};
3869}
3870
3871/* See declaration. */
3872
3873void
44ed8f3e 3874mapped_index_base::build_name_components ()
5c58de74
PA
3875{
3876 if (!this->name_components.empty ())
3877 return;
3878
3879 this->name_components_casing = case_sensitivity;
3880 auto *name_cmp
3881 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3882
3883 /* The code below only knows how to break apart components of C++
3884 symbol names (and other languages that use '::' as
3b00ef10 3885 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3886 auto count = this->symbol_name_count ();
3887 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3888 {
44ed8f3e 3889 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3890 continue;
3891
3892 const char *name = this->symbol_name_at (idx);
3893
3894 /* Add each name component to the name component table. */
3895 unsigned int previous_len = 0;
3b00ef10
TT
3896
3897 if (strstr (name, "::") != nullptr)
3898 {
3899 for (unsigned int current_len = cp_find_first_component (name);
3900 name[current_len] != '\0';
3901 current_len += cp_find_first_component (name + current_len))
3902 {
3903 gdb_assert (name[current_len] == ':');
3904 this->name_components.push_back ({previous_len, idx});
3905 /* Skip the '::'. */
3906 current_len += 2;
3907 previous_len = current_len;
3908 }
3909 }
3910 else
5c58de74 3911 {
3b00ef10
TT
3912 /* Handle the Ada encoded (aka mangled) form here. */
3913 for (const char *iter = strstr (name, "__");
3914 iter != nullptr;
3915 iter = strstr (iter, "__"))
3916 {
3917 this->name_components.push_back ({previous_len, idx});
3918 iter += 2;
3919 previous_len = iter - name;
3920 }
5c58de74 3921 }
3b00ef10 3922
5c58de74
PA
3923 this->name_components.push_back ({previous_len, idx});
3924 }
3925
3926 /* Sort name_components elements by name. */
3927 auto name_comp_compare = [&] (const name_component &left,
3928 const name_component &right)
3929 {
3930 const char *left_qualified = this->symbol_name_at (left.idx);
3931 const char *right_qualified = this->symbol_name_at (right.idx);
3932
3933 const char *left_name = left_qualified + left.name_offset;
3934 const char *right_name = right_qualified + right.name_offset;
3935
3936 return name_cmp (left_name, right_name) < 0;
3937 };
3938
3939 std::sort (this->name_components.begin (),
3940 this->name_components.end (),
3941 name_comp_compare);
3942}
3943
3944/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3945 mapped_index_base instead of the containing objfile. This is split
3946 to a separate function in order to be able to unit test the
3947 name_components matching using a mock mapped_index_base. For each
5c58de74 3948 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3949 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3950
3951static void
3952dw2_expand_symtabs_matching_symbol
44ed8f3e 3953 (mapped_index_base &index,
5c58de74
PA
3954 const lookup_name_info &lookup_name_in,
3955 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3956 enum search_domain kind,
3b00ef10 3957 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3958{
3959 lookup_name_info lookup_name_without_params
3960 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3961
3962 /* Build the symbol name component sorted vector, if we haven't
3963 yet. */
3964 index.build_name_components ();
3965
3f563c84
PA
3966 /* The same symbol may appear more than once in the range though.
3967 E.g., if we're looking for symbols that complete "w", and we have
3968 a symbol named "w1::w2", we'll find the two name components for
3969 that same symbol in the range. To be sure we only call the
3970 callback once per symbol, we first collect the symbol name
3971 indexes that matched in a temporary vector and ignore
3972 duplicates. */
3973 std::vector<offset_type> matches;
3f563c84 3974
3b00ef10
TT
3975 struct name_and_matcher
3976 {
3977 symbol_name_matcher_ftype *matcher;
ecc6c606 3978 const char *name;
3b00ef10
TT
3979
3980 bool operator== (const name_and_matcher &other) const
3f563c84 3981 {
ecc6c606 3982 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3983 }
3984 };
3985
3986 /* A vector holding all the different symbol name matchers, for all
3987 languages. */
3988 std::vector<name_and_matcher> matchers;
3989
3990 for (int i = 0; i < nr_languages; i++)
3991 {
3992 enum language lang_e = (enum language) i;
3993
3994 const language_defn *lang = language_def (lang_e);
3995 symbol_name_matcher_ftype *name_matcher
3996 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3997
3b00ef10
TT
3998 name_and_matcher key {
3999 name_matcher,
4000 lookup_name_without_params.language_lookup_name (lang_e)
4001 };
4002
4003 /* Don't insert the same comparison routine more than once.
4004 Note that we do this linear walk. This is not a problem in
4005 practice because the number of supported languages is
4006 low. */
4007 if (std::find (matchers.begin (), matchers.end (), key)
4008 != matchers.end ())
9291a0cd 4009 continue;
3b00ef10
TT
4010 matchers.push_back (std::move (key));
4011
4012 auto bounds
4013 = index.find_name_components_bounds (lookup_name_without_params,
4014 lang_e);
4015
4016 /* Now for each symbol name in range, check to see if we have a name
4017 match, and if so, call the MATCH_CALLBACK callback. */
4018
4019 for (; bounds.first != bounds.second; ++bounds.first)
4020 {
4021 const char *qualified = index.symbol_name_at (bounds.first->idx);
4022
4023 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4024 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4025 continue;
9291a0cd 4026
3b00ef10
TT
4027 matches.push_back (bounds.first->idx);
4028 }
3f563c84
PA
4029 }
4030
4031 std::sort (matches.begin (), matches.end ());
4032
4033 /* Finally call the callback, once per match. */
4034 ULONGEST prev = -1;
4035 for (offset_type idx : matches)
4036 {
4037 if (prev != idx)
4038 {
3b00ef10
TT
4039 if (!match_callback (idx))
4040 break;
3f563c84
PA
4041 prev = idx;
4042 }
4043 }
4044
4045 /* Above we use a type wider than idx's for 'prev', since 0 and
4046 (offset_type)-1 are both possible values. */
4047 static_assert (sizeof (prev) > sizeof (offset_type), "");
4048}
4049
c62446b1
PA
4050#if GDB_SELF_TEST
4051
4052namespace selftests { namespace dw2_expand_symtabs_matching {
4053
a3c5fafd
PA
4054/* A mock .gdb_index/.debug_names-like name index table, enough to
4055 exercise dw2_expand_symtabs_matching_symbol, which works with the
4056 mapped_index_base interface. Builds an index from the symbol list
4057 passed as parameter to the constructor. */
4058class mock_mapped_index : public mapped_index_base
c62446b1
PA
4059{
4060public:
a3c5fafd
PA
4061 mock_mapped_index (gdb::array_view<const char *> symbols)
4062 : m_symbol_table (symbols)
c62446b1
PA
4063 {}
4064
a3c5fafd 4065 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4066
a3c5fafd 4067 /* Return the number of names in the symbol table. */
632e107b 4068 size_t symbol_name_count () const override
c62446b1 4069 {
a3c5fafd 4070 return m_symbol_table.size ();
c62446b1
PA
4071 }
4072
a3c5fafd 4073 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4074 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4075 {
4076 return m_symbol_table[idx];
4077 }
c62446b1 4078
a3c5fafd
PA
4079private:
4080 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4081};
4082
4083/* Convenience function that converts a NULL pointer to a "<null>"
4084 string, to pass to print routines. */
4085
4086static const char *
4087string_or_null (const char *str)
4088{
4089 return str != NULL ? str : "<null>";
4090}
4091
4092/* Check if a lookup_name_info built from
4093 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4094 index. EXPECTED_LIST is the list of expected matches, in expected
4095 matching order. If no match expected, then an empty list is
4096 specified. Returns true on success. On failure prints a warning
4097 indicating the file:line that failed, and returns false. */
4098
4099static bool
4100check_match (const char *file, int line,
4101 mock_mapped_index &mock_index,
4102 const char *name, symbol_name_match_type match_type,
4103 bool completion_mode,
4104 std::initializer_list<const char *> expected_list)
4105{
4106 lookup_name_info lookup_name (name, match_type, completion_mode);
4107
4108 bool matched = true;
4109
4110 auto mismatch = [&] (const char *expected_str,
4111 const char *got)
4112 {
4113 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4114 "expected=\"%s\", got=\"%s\"\n"),
4115 file, line,
4116 (match_type == symbol_name_match_type::FULL
4117 ? "FULL" : "WILD"),
4118 name, string_or_null (expected_str), string_or_null (got));
4119 matched = false;
4120 };
4121
4122 auto expected_it = expected_list.begin ();
4123 auto expected_end = expected_list.end ();
4124
a3c5fafd 4125 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4126 NULL, ALL_DOMAIN,
4127 [&] (offset_type idx)
4128 {
a3c5fafd 4129 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4130 const char *expected_str
4131 = expected_it == expected_end ? NULL : *expected_it++;
4132
4133 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4134 mismatch (expected_str, matched_name);
3b00ef10 4135 return true;
c62446b1
PA
4136 });
4137
4138 const char *expected_str
4139 = expected_it == expected_end ? NULL : *expected_it++;
4140 if (expected_str != NULL)
4141 mismatch (expected_str, NULL);
4142
4143 return matched;
4144}
4145
4146/* The symbols added to the mock mapped_index for testing (in
4147 canonical form). */
4148static const char *test_symbols[] = {
4149 "function",
4150 "std::bar",
4151 "std::zfunction",
4152 "std::zfunction2",
4153 "w1::w2",
4154 "ns::foo<char*>",
4155 "ns::foo<int>",
4156 "ns::foo<long>",
a20714ff
PA
4157 "ns2::tmpl<int>::foo2",
4158 "(anonymous namespace)::A::B::C",
c62446b1 4159
e1ef7d7a
PA
4160 /* These are used to check that the increment-last-char in the
4161 matching algorithm for completion doesn't match "t1_fund" when
4162 completing "t1_func". */
4163 "t1_func",
4164 "t1_func1",
4165 "t1_fund",
4166 "t1_fund1",
4167
4168 /* A UTF-8 name with multi-byte sequences to make sure that
4169 cp-name-parser understands this as a single identifier ("função"
4170 is "function" in PT). */
4171 u8"u8função",
4172
4173 /* \377 (0xff) is Latin1 'ÿ'. */
4174 "yfunc\377",
4175
4176 /* \377 (0xff) is Latin1 'ÿ'. */
4177 "\377",
4178 "\377\377123",
4179
c62446b1
PA
4180 /* A name with all sorts of complications. Starts with "z" to make
4181 it easier for the completion tests below. */
4182#define Z_SYM_NAME \
4183 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4184 "::tuple<(anonymous namespace)::ui*, " \
4185 "std::default_delete<(anonymous namespace)::ui>, void>"
4186
4187 Z_SYM_NAME
4188};
4189
a3c5fafd
PA
4190/* Returns true if the mapped_index_base::find_name_component_bounds
4191 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4192 in completion mode. */
5c58de74
PA
4193
4194static bool
a3c5fafd 4195check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4196 const char *search_name,
4197 gdb::array_view<const char *> expected_syms)
4198{
4199 lookup_name_info lookup_name (search_name,
4200 symbol_name_match_type::FULL, true);
4201
3b00ef10
TT
4202 auto bounds = index.find_name_components_bounds (lookup_name,
4203 language_cplus);
5c58de74
PA
4204
4205 size_t distance = std::distance (bounds.first, bounds.second);
4206 if (distance != expected_syms.size ())
4207 return false;
4208
4209 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4210 {
4211 auto nc_elem = bounds.first + exp_elem;
4212 const char *qualified = index.symbol_name_at (nc_elem->idx);
4213 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4214 return false;
4215 }
4216
4217 return true;
4218}
4219
4220/* Test the lower-level mapped_index::find_name_component_bounds
4221 method. */
4222
c62446b1 4223static void
5c58de74
PA
4224test_mapped_index_find_name_component_bounds ()
4225{
4226 mock_mapped_index mock_index (test_symbols);
4227
a3c5fafd 4228 mock_index.build_name_components ();
5c58de74
PA
4229
4230 /* Test the lower-level mapped_index::find_name_component_bounds
4231 method in completion mode. */
4232 {
4233 static const char *expected_syms[] = {
4234 "t1_func",
4235 "t1_func1",
5c58de74
PA
4236 };
4237
a3c5fafd 4238 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4239 "t1_func", expected_syms));
4240 }
4241
4242 /* Check that the increment-last-char in the name matching algorithm
4243 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4244 {
4245 static const char *expected_syms1[] = {
4246 "\377",
4247 "\377\377123",
4248 };
a3c5fafd 4249 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4250 "\377", expected_syms1));
4251
4252 static const char *expected_syms2[] = {
4253 "\377\377123",
4254 };
a3c5fafd 4255 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4256 "\377\377", expected_syms2));
4257 }
4258}
4259
4260/* Test dw2_expand_symtabs_matching_symbol. */
4261
4262static void
4263test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4264{
4265 mock_mapped_index mock_index (test_symbols);
4266
4267 /* We let all tests run until the end even if some fails, for debug
4268 convenience. */
4269 bool any_mismatch = false;
4270
4271 /* Create the expected symbols list (an initializer_list). Needed
4272 because lists have commas, and we need to pass them to CHECK,
4273 which is a macro. */
4274#define EXPECT(...) { __VA_ARGS__ }
4275
4276 /* Wrapper for check_match that passes down the current
4277 __FILE__/__LINE__. */
4278#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4279 any_mismatch |= !check_match (__FILE__, __LINE__, \
4280 mock_index, \
4281 NAME, MATCH_TYPE, COMPLETION_MODE, \
4282 EXPECTED_LIST)
4283
4284 /* Identity checks. */
4285 for (const char *sym : test_symbols)
4286 {
4287 /* Should be able to match all existing symbols. */
4288 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4289 EXPECT (sym));
4290
4291 /* Should be able to match all existing symbols with
4292 parameters. */
4293 std::string with_params = std::string (sym) + "(int)";
4294 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4295 EXPECT (sym));
4296
4297 /* Should be able to match all existing symbols with
4298 parameters and qualifiers. */
4299 with_params = std::string (sym) + " ( int ) const";
4300 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4301 EXPECT (sym));
4302
4303 /* This should really find sym, but cp-name-parser.y doesn't
4304 know about lvalue/rvalue qualifiers yet. */
4305 with_params = std::string (sym) + " ( int ) &&";
4306 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4307 {});
4308 }
4309
e1ef7d7a
PA
4310 /* Check that the name matching algorithm for completion doesn't get
4311 confused with Latin1 'ÿ' / 0xff. */
4312 {
4313 static const char str[] = "\377";
4314 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4315 EXPECT ("\377", "\377\377123"));
4316 }
4317
4318 /* Check that the increment-last-char in the matching algorithm for
4319 completion doesn't match "t1_fund" when completing "t1_func". */
4320 {
4321 static const char str[] = "t1_func";
4322 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4323 EXPECT ("t1_func", "t1_func1"));
4324 }
4325
c62446b1
PA
4326 /* Check that completion mode works at each prefix of the expected
4327 symbol name. */
4328 {
4329 static const char str[] = "function(int)";
4330 size_t len = strlen (str);
4331 std::string lookup;
4332
4333 for (size_t i = 1; i < len; i++)
4334 {
4335 lookup.assign (str, i);
4336 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4337 EXPECT ("function"));
4338 }
4339 }
4340
4341 /* While "w" is a prefix of both components, the match function
4342 should still only be called once. */
4343 {
4344 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4345 EXPECT ("w1::w2"));
a20714ff
PA
4346 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4347 EXPECT ("w1::w2"));
c62446b1
PA
4348 }
4349
4350 /* Same, with a "complicated" symbol. */
4351 {
4352 static const char str[] = Z_SYM_NAME;
4353 size_t len = strlen (str);
4354 std::string lookup;
4355
4356 for (size_t i = 1; i < len; i++)
4357 {
4358 lookup.assign (str, i);
4359 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4360 EXPECT (Z_SYM_NAME));
4361 }
4362 }
4363
4364 /* In FULL mode, an incomplete symbol doesn't match. */
4365 {
4366 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4367 {});
4368 }
4369
4370 /* A complete symbol with parameters matches any overload, since the
4371 index has no overload info. */
4372 {
4373 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4374 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4375 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4376 EXPECT ("std::zfunction", "std::zfunction2"));
4377 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4378 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4379 }
4380
4381 /* Check that whitespace is ignored appropriately. A symbol with a
4382 template argument list. */
4383 {
4384 static const char expected[] = "ns::foo<int>";
4385 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4386 EXPECT (expected));
a20714ff
PA
4387 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4388 EXPECT (expected));
c62446b1
PA
4389 }
4390
4391 /* Check that whitespace is ignored appropriately. A symbol with a
4392 template argument list that includes a pointer. */
4393 {
4394 static const char expected[] = "ns::foo<char*>";
4395 /* Try both completion and non-completion modes. */
4396 static const bool completion_mode[2] = {false, true};
4397 for (size_t i = 0; i < 2; i++)
4398 {
4399 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4400 completion_mode[i], EXPECT (expected));
a20714ff
PA
4401 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4402 completion_mode[i], EXPECT (expected));
c62446b1
PA
4403
4404 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4405 completion_mode[i], EXPECT (expected));
a20714ff
PA
4406 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4407 completion_mode[i], EXPECT (expected));
c62446b1
PA
4408 }
4409 }
4410
4411 {
4412 /* Check method qualifiers are ignored. */
4413 static const char expected[] = "ns::foo<char*>";
4414 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4415 symbol_name_match_type::FULL, true, EXPECT (expected));
4416 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4417 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4418 CHECK_MATCH ("foo < char * > ( int ) const",
4419 symbol_name_match_type::WILD, true, EXPECT (expected));
4420 CHECK_MATCH ("foo < char * > ( int ) &&",
4421 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4422 }
4423
4424 /* Test lookup names that don't match anything. */
4425 {
a20714ff
PA
4426 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4427 {});
4428
c62446b1
PA
4429 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4430 {});
4431 }
4432
a20714ff
PA
4433 /* Some wild matching tests, exercising "(anonymous namespace)",
4434 which should not be confused with a parameter list. */
4435 {
4436 static const char *syms[] = {
4437 "A::B::C",
4438 "B::C",
4439 "C",
4440 "A :: B :: C ( int )",
4441 "B :: C ( int )",
4442 "C ( int )",
4443 };
4444
4445 for (const char *s : syms)
4446 {
4447 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4448 EXPECT ("(anonymous namespace)::A::B::C"));
4449 }
4450 }
4451
4452 {
4453 static const char expected[] = "ns2::tmpl<int>::foo2";
4454 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4455 EXPECT (expected));
4456 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4457 EXPECT (expected));
4458 }
4459
c62446b1
PA
4460 SELF_CHECK (!any_mismatch);
4461
4462#undef EXPECT
4463#undef CHECK_MATCH
4464}
4465
5c58de74
PA
4466static void
4467run_test ()
4468{
4469 test_mapped_index_find_name_component_bounds ();
4470 test_dw2_expand_symtabs_matching_symbol ();
4471}
4472
c62446b1
PA
4473}} // namespace selftests::dw2_expand_symtabs_matching
4474
4475#endif /* GDB_SELF_TEST */
4476
4b514bc8
JK
4477/* If FILE_MATCHER is NULL or if PER_CU has
4478 dwarf2_per_cu_quick_data::MARK set (see
4479 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4480 EXPANSION_NOTIFY on it. */
4481
4482static void
4483dw2_expand_symtabs_matching_one
4484 (struct dwarf2_per_cu_data *per_cu,
4485 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4486 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4487{
4488 if (file_matcher == NULL || per_cu->v.quick->mark)
4489 {
4490 bool symtab_was_null
4491 = (per_cu->v.quick->compunit_symtab == NULL);
4492
58f0c718 4493 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4494
4495 if (expansion_notify != NULL
4496 && symtab_was_null
4497 && per_cu->v.quick->compunit_symtab != NULL)
4498 expansion_notify (per_cu->v.quick->compunit_symtab);
4499 }
4500}
4501
3f563c84
PA
4502/* Helper for dw2_expand_matching symtabs. Called on each symbol
4503 matched, to expand corresponding CUs that were marked. IDX is the
4504 index of the symbol name that matched. */
4505
4506static void
4507dw2_expand_marked_cus
ed2dc618 4508 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4509 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4510 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4511 search_domain kind)
4512{
3f563c84
PA
4513 offset_type *vec, vec_len, vec_idx;
4514 bool global_seen = false;
ed2dc618 4515 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4516
61920122 4517 vec = (offset_type *) (index.constant_pool
f00a2de2 4518 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4519 vec_len = MAYBE_SWAP (vec[0]);
4520 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4521 {
61920122
PA
4522 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4523 /* This value is only valid for index versions >= 7. */
4524 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4525 gdb_index_symbol_kind symbol_kind =
4526 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4527 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4528 /* Only check the symbol attributes if they're present.
4529 Indices prior to version 7 don't record them,
4530 and indices >= 7 may elide them for certain symbols
4531 (gold does this). */
4532 int attrs_valid =
4533 (index.version >= 7
4534 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4535
4536 /* Work around gold/15646. */
4537 if (attrs_valid)
9291a0cd 4538 {
61920122
PA
4539 if (!is_static && global_seen)
4540 continue;
4541 if (!is_static)
4542 global_seen = true;
4543 }
3190f0c6 4544
61920122
PA
4545 /* Only check the symbol's kind if it has one. */
4546 if (attrs_valid)
4547 {
4548 switch (kind)
8943b874 4549 {
61920122
PA
4550 case VARIABLES_DOMAIN:
4551 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4552 continue;
4553 break;
4554 case FUNCTIONS_DOMAIN:
4555 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4556 continue;
61920122
PA
4557 break;
4558 case TYPES_DOMAIN:
4559 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4560 continue;
4561 break;
59c35742
AB
4562 case MODULES_DOMAIN:
4563 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4564 continue;
4565 break;
61920122
PA
4566 default:
4567 break;
8943b874 4568 }
61920122 4569 }
8943b874 4570
61920122 4571 /* Don't crash on bad data. */
b76e467d 4572 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4573 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4574 {
b98664d3 4575 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4576 " [in module %s]"),
4577 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4578 continue;
4579 }
4580
ff4c9fec 4581 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4582 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4583 expansion_notify);
61920122
PA
4584 }
4585}
4586
4b514bc8
JK
4587/* If FILE_MATCHER is non-NULL, set all the
4588 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4589 that match FILE_MATCHER. */
4590
61920122 4591static void
4b514bc8 4592dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4593 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4594 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4595{
4b514bc8 4596 if (file_matcher == NULL)
61920122
PA
4597 return;
4598
4b514bc8
JK
4599 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4600 htab_eq_pointer,
4601 NULL, xcalloc, xfree));
4602 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4603 htab_eq_pointer,
4604 NULL, xcalloc, xfree));
61920122 4605
4b514bc8
JK
4606 /* The rule is CUs specify all the files, including those used by
4607 any TU, so there's no need to scan TUs here. */
61920122 4608
b76e467d 4609 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4610 {
927aa2e7
JK
4611 QUIT;
4612
4613 per_cu->v.quick->mark = 0;
4614
4615 /* We only need to look at symtabs not already expanded. */
4616 if (per_cu->v.quick->compunit_symtab)
4617 continue;
4618
b76e467d 4619 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4620 if (file_data == NULL)
4621 continue;
4622
4623 if (htab_find (visited_not_found.get (), file_data) != NULL)
4624 continue;
4625 else if (htab_find (visited_found.get (), file_data) != NULL)
4626 {
4627 per_cu->v.quick->mark = 1;
4628 continue;
4629 }
4630
b76e467d 4631 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4632 {
4633 const char *this_real_name;
4634
4635 if (file_matcher (file_data->file_names[j], false))
4636 {
4637 per_cu->v.quick->mark = 1;
4638 break;
4639 }
4640
4641 /* Before we invoke realpath, which can get expensive when many
4642 files are involved, do a quick comparison of the basenames. */
4643 if (!basenames_may_differ
4644 && !file_matcher (lbasename (file_data->file_names[j]),
4645 true))
4646 continue;
4647
45940949
TT
4648 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4649 file_data, j);
927aa2e7
JK
4650 if (file_matcher (this_real_name, false))
4651 {
4652 per_cu->v.quick->mark = 1;
4653 break;
4654 }
4655 }
4656
b76e467d
SM
4657 void **slot = htab_find_slot (per_cu->v.quick->mark
4658 ? visited_found.get ()
4659 : visited_not_found.get (),
4660 file_data, INSERT);
927aa2e7
JK
4661 *slot = file_data;
4662 }
4663}
4664
4665static void
4666dw2_expand_symtabs_matching
4667 (struct objfile *objfile,
4668 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4669 const lookup_name_info *lookup_name,
927aa2e7
JK
4670 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4671 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4672 enum search_domain kind)
4673{
ed2dc618
SM
4674 struct dwarf2_per_objfile *dwarf2_per_objfile
4675 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4676
4677 /* index_table is NULL if OBJF_READNOW. */
4678 if (!dwarf2_per_objfile->index_table)
4679 return;
4680
ed2dc618 4681 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4682
c1a66c06
TV
4683 if (symbol_matcher == NULL && lookup_name == NULL)
4684 {
4685 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4686 {
4687 QUIT;
4688
4689 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4690 expansion_notify);
4691 }
4692 return;
4693 }
4694
927aa2e7
JK
4695 mapped_index &index = *dwarf2_per_objfile->index_table;
4696
c1a66c06 4697 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4698 symbol_matcher,
4699 kind, [&] (offset_type idx)
4700 {
ed2dc618 4701 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4702 expansion_notify, kind);
3b00ef10 4703 return true;
927aa2e7
JK
4704 });
4705}
4706
4707/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4708 symtab. */
4709
4710static struct compunit_symtab *
4711recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4712 CORE_ADDR pc)
4713{
4714 int i;
4715
4716 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4717 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4718 return cust;
4719
4720 if (cust->includes == NULL)
4721 return NULL;
4722
4723 for (i = 0; cust->includes[i]; ++i)
4724 {
4725 struct compunit_symtab *s = cust->includes[i];
4726
4727 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4728 if (s != NULL)
4729 return s;
4730 }
4731
4732 return NULL;
4733}
4734
4735static struct compunit_symtab *
4736dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4737 struct bound_minimal_symbol msymbol,
4738 CORE_ADDR pc,
4739 struct obj_section *section,
4740 int warn_if_readin)
4741{
4742 struct dwarf2_per_cu_data *data;
4743 struct compunit_symtab *result;
4744
d320c2b5 4745 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4746 return NULL;
4747
b3b3bada 4748 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4749 data = (struct dwarf2_per_cu_data *) addrmap_find
4750 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4751 if (!data)
4752 return NULL;
4753
4754 if (warn_if_readin && data->v.quick->compunit_symtab)
4755 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4756 paddress (objfile->arch (), pc));
927aa2e7
JK
4757
4758 result
58f0c718
TT
4759 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4760 false),
927aa2e7
JK
4761 pc);
4762 gdb_assert (result != NULL);
4763 return result;
4764}
4765
4766static void
4767dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4768 void *data, int need_fullname)
4769{
ed2dc618
SM
4770 struct dwarf2_per_objfile *dwarf2_per_objfile
4771 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4772
4773 if (!dwarf2_per_objfile->filenames_cache)
4774 {
4775 dwarf2_per_objfile->filenames_cache.emplace ();
4776
4777 htab_up visited (htab_create_alloc (10,
4778 htab_hash_pointer, htab_eq_pointer,
4779 NULL, xcalloc, xfree));
4780
4781 /* The rule is CUs specify all the files, including those used
4782 by any TU, so there's no need to scan TUs here. We can
4783 ignore file names coming from already-expanded CUs. */
4784
b76e467d 4785 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4786 {
927aa2e7
JK
4787 if (per_cu->v.quick->compunit_symtab)
4788 {
4789 void **slot = htab_find_slot (visited.get (),
4790 per_cu->v.quick->file_names,
4791 INSERT);
4792
4793 *slot = per_cu->v.quick->file_names;
4794 }
4795 }
4796
b76e467d 4797 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4798 {
927aa2e7
JK
4799 /* We only need to look at symtabs not already expanded. */
4800 if (per_cu->v.quick->compunit_symtab)
4801 continue;
4802
b76e467d 4803 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4804 if (file_data == NULL)
4805 continue;
4806
b76e467d 4807 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4808 if (*slot)
4809 {
4810 /* Already visited. */
4811 continue;
4812 }
4813 *slot = file_data;
4814
4815 for (int j = 0; j < file_data->num_file_names; ++j)
4816 {
4817 const char *filename = file_data->file_names[j];
4818 dwarf2_per_objfile->filenames_cache->seen (filename);
4819 }
4820 }
4821 }
4822
4823 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4824 {
4825 gdb::unique_xmalloc_ptr<char> this_real_name;
4826
4827 if (need_fullname)
4828 this_real_name = gdb_realpath (filename);
4829 (*fun) (filename, this_real_name.get (), data);
4830 });
4831}
4832
4833static int
4834dw2_has_symbols (struct objfile *objfile)
4835{
4836 return 1;
4837}
4838
4839const struct quick_symbol_functions dwarf2_gdb_index_functions =
4840{
4841 dw2_has_symbols,
4842 dw2_find_last_source_symtab,
4843 dw2_forget_cached_source_info,
4844 dw2_map_symtabs_matching_filename,
4845 dw2_lookup_symbol,
d3214198 4846 NULL,
927aa2e7
JK
4847 dw2_print_stats,
4848 dw2_dump,
927aa2e7
JK
4849 dw2_expand_symtabs_for_function,
4850 dw2_expand_all_symtabs,
4851 dw2_expand_symtabs_with_fullname,
4852 dw2_map_matching_symbols,
4853 dw2_expand_symtabs_matching,
4854 dw2_find_pc_sect_compunit_symtab,
4855 NULL,
4856 dw2_map_symbol_filenames
4857};
4858
4859/* DWARF-5 debug_names reader. */
4860
4861/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4862static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4863
4864/* A helper function that reads the .debug_names section in SECTION
4865 and fills in MAP. FILENAME is the name of the file containing the
4866 section; it is used for error reporting.
4867
4868 Returns true if all went well, false otherwise. */
4869
4870static bool
4871read_debug_names_from_section (struct objfile *objfile,
4872 const char *filename,
4873 struct dwarf2_section_info *section,
4874 mapped_debug_names &map)
4875{
96b79293 4876 if (section->empty ())
927aa2e7
JK
4877 return false;
4878
4879 /* Older elfutils strip versions could keep the section in the main
4880 executable while splitting it for the separate debug info file. */
96b79293 4881 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4882 return false;
4883
96b79293 4884 section->read (objfile);
927aa2e7 4885
08feed99 4886 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4887
4888 const gdb_byte *addr = section->buffer;
4889
96b79293 4890 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4891
4892 unsigned int bytes_read;
4893 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4894 addr += bytes_read;
4895
4896 map.dwarf5_is_dwarf64 = bytes_read != 4;
4897 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4898 if (bytes_read + length != section->size)
4899 {
4900 /* There may be multiple per-CU indices. */
4901 warning (_("Section .debug_names in %s length %s does not match "
4902 "section length %s, ignoring .debug_names."),
4903 filename, plongest (bytes_read + length),
4904 pulongest (section->size));
4905 return false;
4906 }
4907
4908 /* The version number. */
4909 uint16_t version = read_2_bytes (abfd, addr);
4910 addr += 2;
4911 if (version != 5)
4912 {
4913 warning (_("Section .debug_names in %s has unsupported version %d, "
4914 "ignoring .debug_names."),
4915 filename, version);
4916 return false;
4917 }
4918
4919 /* Padding. */
4920 uint16_t padding = read_2_bytes (abfd, addr);
4921 addr += 2;
4922 if (padding != 0)
4923 {
4924 warning (_("Section .debug_names in %s has unsupported padding %d, "
4925 "ignoring .debug_names."),
4926 filename, padding);
4927 return false;
4928 }
4929
4930 /* comp_unit_count - The number of CUs in the CU list. */
4931 map.cu_count = read_4_bytes (abfd, addr);
4932 addr += 4;
4933
4934 /* local_type_unit_count - The number of TUs in the local TU
4935 list. */
4936 map.tu_count = read_4_bytes (abfd, addr);
4937 addr += 4;
4938
4939 /* foreign_type_unit_count - The number of TUs in the foreign TU
4940 list. */
4941 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4942 addr += 4;
4943 if (foreign_tu_count != 0)
4944 {
4945 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4946 "ignoring .debug_names."),
4947 filename, static_cast<unsigned long> (foreign_tu_count));
4948 return false;
4949 }
4950
4951 /* bucket_count - The number of hash buckets in the hash lookup
4952 table. */
4953 map.bucket_count = read_4_bytes (abfd, addr);
4954 addr += 4;
4955
4956 /* name_count - The number of unique names in the index. */
4957 map.name_count = read_4_bytes (abfd, addr);
4958 addr += 4;
4959
4960 /* abbrev_table_size - The size in bytes of the abbreviations
4961 table. */
4962 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4963 addr += 4;
4964
4965 /* augmentation_string_size - The size in bytes of the augmentation
4966 string. This value is rounded up to a multiple of 4. */
4967 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4968 addr += 4;
4969 map.augmentation_is_gdb = ((augmentation_string_size
4970 == sizeof (dwarf5_augmentation))
4971 && memcmp (addr, dwarf5_augmentation,
4972 sizeof (dwarf5_augmentation)) == 0);
4973 augmentation_string_size += (-augmentation_string_size) & 3;
4974 addr += augmentation_string_size;
4975
4976 /* List of CUs */
4977 map.cu_table_reordered = addr;
4978 addr += map.cu_count * map.offset_size;
4979
4980 /* List of Local TUs */
4981 map.tu_table_reordered = addr;
4982 addr += map.tu_count * map.offset_size;
4983
4984 /* Hash Lookup Table */
4985 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4986 addr += map.bucket_count * 4;
4987 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4988 addr += map.name_count * 4;
4989
4990 /* Name Table */
4991 map.name_table_string_offs_reordered = addr;
4992 addr += map.name_count * map.offset_size;
4993 map.name_table_entry_offs_reordered = addr;
4994 addr += map.name_count * map.offset_size;
4995
4996 const gdb_byte *abbrev_table_start = addr;
4997 for (;;)
4998 {
927aa2e7
JK
4999 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5000 addr += bytes_read;
5001 if (index_num == 0)
5002 break;
5003
5004 const auto insertpair
5005 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5006 if (!insertpair.second)
5007 {
5008 warning (_("Section .debug_names in %s has duplicate index %s, "
5009 "ignoring .debug_names."),
5010 filename, pulongest (index_num));
5011 return false;
5012 }
5013 mapped_debug_names::index_val &indexval = insertpair.first->second;
5014 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5015 addr += bytes_read;
5016
5017 for (;;)
5018 {
5019 mapped_debug_names::index_val::attr attr;
5020 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5021 addr += bytes_read;
5022 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5023 addr += bytes_read;
5024 if (attr.form == DW_FORM_implicit_const)
5025 {
5026 attr.implicit_const = read_signed_leb128 (abfd, addr,
5027 &bytes_read);
5028 addr += bytes_read;
5029 }
5030 if (attr.dw_idx == 0 && attr.form == 0)
5031 break;
5032 indexval.attr_vec.push_back (std::move (attr));
5033 }
5034 }
5035 if (addr != abbrev_table_start + abbrev_table_size)
5036 {
5037 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5038 "of size %s vs. written as %u, ignoring .debug_names."),
5039 filename, plongest (addr - abbrev_table_start),
5040 abbrev_table_size);
927aa2e7
JK
5041 return false;
5042 }
5043 map.entry_pool = addr;
5044
5045 return true;
5046}
5047
5048/* A helper for create_cus_from_debug_names that handles the MAP's CU
5049 list. */
5050
5051static void
ed2dc618 5052create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5053 const mapped_debug_names &map,
5054 dwarf2_section_info &section,
b76e467d 5055 bool is_dwz)
927aa2e7 5056{
3ee6bb11
TV
5057 if (!map.augmentation_is_gdb)
5058 {
5059 for (uint32_t i = 0; i < map.cu_count; ++i)
5060 {
5061 sect_offset sect_off
5062 = (sect_offset) (extract_unsigned_integer
5063 (map.cu_table_reordered + i * map.offset_size,
5064 map.offset_size,
5065 map.dwarf5_byte_order));
5066 /* We don't know the length of the CU, because the CU list in a
5067 .debug_names index can be incomplete, so we can't use the start of
5068 the next CU as end of this CU. We create the CUs here with length 0,
5069 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5070 dwarf2_per_cu_data *per_cu
5071 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5072 sect_off, 0);
5073 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
5074 }
5075 }
5076
927aa2e7
JK
5077 sect_offset sect_off_prev;
5078 for (uint32_t i = 0; i <= map.cu_count; ++i)
5079 {
5080 sect_offset sect_off_next;
5081 if (i < map.cu_count)
5082 {
5083 sect_off_next
5084 = (sect_offset) (extract_unsigned_integer
5085 (map.cu_table_reordered + i * map.offset_size,
5086 map.offset_size,
5087 map.dwarf5_byte_order));
5088 }
5089 else
5090 sect_off_next = (sect_offset) section.size;
5091 if (i >= 1)
5092 {
5093 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5094 dwarf2_per_cu_data *per_cu
ed2dc618 5095 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5096 sect_off_prev, length);
b76e467d 5097 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5098 }
5099 sect_off_prev = sect_off_next;
5100 }
5101}
5102
5103/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5104 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5105
5106static void
ed2dc618 5107create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5108 const mapped_debug_names &map,
5109 const mapped_debug_names &dwz_map)
5110{
b76e467d
SM
5111 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5112 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5113
ed2dc618
SM
5114 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5115 dwarf2_per_objfile->info,
b76e467d 5116 false /* is_dwz */);
927aa2e7
JK
5117
5118 if (dwz_map.cu_count == 0)
5119 return;
5120
ed2dc618
SM
5121 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5122 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5123 true /* is_dwz */);
927aa2e7
JK
5124}
5125
5126/* Read .debug_names. If everything went ok, initialize the "quick"
5127 elements of all the CUs and return true. Otherwise, return false. */
5128
5129static bool
ed2dc618 5130dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5131{
22ca247e
TT
5132 std::unique_ptr<mapped_debug_names> map
5133 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5134 mapped_debug_names dwz_map (dwarf2_per_objfile);
5135 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5136
5137 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5138 &dwarf2_per_objfile->debug_names,
22ca247e 5139 *map))
927aa2e7
JK
5140 return false;
5141
5142 /* Don't use the index if it's empty. */
22ca247e 5143 if (map->name_count == 0)
927aa2e7
JK
5144 return false;
5145
5146 /* If there is a .dwz file, read it so we can get its CU list as
5147 well. */
ed2dc618 5148 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5149 if (dwz != NULL)
5150 {
5151 if (!read_debug_names_from_section (objfile,
00f93c44 5152 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5153 &dwz->debug_names, dwz_map))
5154 {
5155 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5156 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5157 return false;
5158 }
5159 }
5160
22ca247e 5161 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5162
22ca247e 5163 if (map->tu_count != 0)
927aa2e7
JK
5164 {
5165 /* We can only handle a single .debug_types when we have an
5166 index. */
fd5866f6 5167 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5168 return false;
5169
fd5866f6 5170 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5171
5172 create_signatured_type_table_from_debug_names
22ca247e 5173 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5174 }
5175
ed2dc618
SM
5176 create_addrmap_from_aranges (dwarf2_per_objfile,
5177 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5178
22ca247e 5179 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5180 dwarf2_per_objfile->using_index = 1;
5181 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5182 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5183
5184 return true;
5185}
5186
927aa2e7
JK
5187/* Type used to manage iterating over all CUs looking for a symbol for
5188 .debug_names. */
5189
5190class dw2_debug_names_iterator
5191{
5192public:
927aa2e7 5193 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5194 gdb::optional<block_enum> block_index,
5195 domain_enum domain,
927aa2e7 5196 const char *name)
2b79f376 5197 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5198 m_addr (find_vec_in_debug_names (map, name))
5199 {}
5200
5201 dw2_debug_names_iterator (const mapped_debug_names &map,
5202 search_domain search, uint32_t namei)
5203 : m_map (map),
5204 m_search (search),
5205 m_addr (find_vec_in_debug_names (map, namei))
5206 {}
5207
3b00ef10
TT
5208 dw2_debug_names_iterator (const mapped_debug_names &map,
5209 block_enum block_index, domain_enum domain,
5210 uint32_t namei)
5211 : m_map (map), m_block_index (block_index), m_domain (domain),
5212 m_addr (find_vec_in_debug_names (map, namei))
5213 {}
5214
927aa2e7
JK
5215 /* Return the next matching CU or NULL if there are no more. */
5216 dwarf2_per_cu_data *next ();
5217
5218private:
5219 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5220 const char *name);
5221 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5222 uint32_t namei);
5223
5224 /* The internalized form of .debug_names. */
5225 const mapped_debug_names &m_map;
5226
2b79f376
SM
5227 /* If set, only look for symbols that match that block. Valid values are
5228 GLOBAL_BLOCK and STATIC_BLOCK. */
5229 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5230
5231 /* The kind of symbol we're looking for. */
5232 const domain_enum m_domain = UNDEF_DOMAIN;
5233 const search_domain m_search = ALL_DOMAIN;
5234
5235 /* The list of CUs from the index entry of the symbol, or NULL if
5236 not found. */
5237 const gdb_byte *m_addr;
5238};
5239
5240const char *
5241mapped_debug_names::namei_to_name (uint32_t namei) const
5242{
5243 const ULONGEST namei_string_offs
5244 = extract_unsigned_integer ((name_table_string_offs_reordered
5245 + namei * offset_size),
5246 offset_size,
5247 dwarf5_byte_order);
4f44ae6c
TT
5248 return read_indirect_string_at_offset (dwarf2_per_objfile,
5249 namei_string_offs);
927aa2e7
JK
5250}
5251
5252/* Find a slot in .debug_names for the object named NAME. If NAME is
5253 found, return pointer to its pool data. If NAME cannot be found,
5254 return NULL. */
5255
5256const gdb_byte *
5257dw2_debug_names_iterator::find_vec_in_debug_names
5258 (const mapped_debug_names &map, const char *name)
5259{
5260 int (*cmp) (const char *, const char *);
5261
54ee4252 5262 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5263 if (current_language->la_language == language_cplus
5264 || current_language->la_language == language_fortran
5265 || current_language->la_language == language_d)
5266 {
5267 /* NAME is already canonical. Drop any qualifiers as
5268 .debug_names does not contain any. */
5269
5270 if (strchr (name, '(') != NULL)
5271 {
54ee4252 5272 without_params = cp_remove_params (name);
927aa2e7 5273 if (without_params != NULL)
54ee4252 5274 name = without_params.get ();
927aa2e7
JK
5275 }
5276 }
5277
5278 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5279
5280 const uint32_t full_hash = dwarf5_djb_hash (name);
5281 uint32_t namei
5282 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5283 (map.bucket_table_reordered
5284 + (full_hash % map.bucket_count)), 4,
5285 map.dwarf5_byte_order);
5286 if (namei == 0)
5287 return NULL;
5288 --namei;
5289 if (namei >= map.name_count)
5290 {
b98664d3 5291 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5292 "[in module %s]"),
5293 namei, map.name_count,
ed2dc618 5294 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5295 return NULL;
5296 }
5297
5298 for (;;)
5299 {
5300 const uint32_t namei_full_hash
5301 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5302 (map.hash_table_reordered + namei), 4,
5303 map.dwarf5_byte_order);
5304 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5305 return NULL;
5306
5307 if (full_hash == namei_full_hash)
5308 {
5309 const char *const namei_string = map.namei_to_name (namei);
5310
5311#if 0 /* An expensive sanity check. */
5312 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5313 {
b98664d3 5314 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5315 "[in module %s]"),
5316 namei, objfile_name (dwarf2_per_objfile->objfile));
5317 return NULL;
5318 }
5319#endif
5320
5321 if (cmp (namei_string, name) == 0)
5322 {
5323 const ULONGEST namei_entry_offs
5324 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5325 + namei * map.offset_size),
5326 map.offset_size, map.dwarf5_byte_order);
5327 return map.entry_pool + namei_entry_offs;
5328 }
5329 }
5330
5331 ++namei;
5332 if (namei >= map.name_count)
5333 return NULL;
5334 }
5335}
5336
5337const gdb_byte *
5338dw2_debug_names_iterator::find_vec_in_debug_names
5339 (const mapped_debug_names &map, uint32_t namei)
5340{
5341 if (namei >= map.name_count)
5342 {
b98664d3 5343 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5344 "[in module %s]"),
5345 namei, map.name_count,
ed2dc618 5346 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5347 return NULL;
5348 }
5349
5350 const ULONGEST namei_entry_offs
5351 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5352 + namei * map.offset_size),
5353 map.offset_size, map.dwarf5_byte_order);
5354 return map.entry_pool + namei_entry_offs;
5355}
5356
5357/* See dw2_debug_names_iterator. */
5358
5359dwarf2_per_cu_data *
5360dw2_debug_names_iterator::next ()
5361{
5362 if (m_addr == NULL)
5363 return NULL;
5364
ed2dc618
SM
5365 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5366 struct objfile *objfile = dwarf2_per_objfile->objfile;
5367 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5368
5369 again:
5370
5371 unsigned int bytes_read;
5372 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5373 m_addr += bytes_read;
5374 if (abbrev == 0)
5375 return NULL;
5376
5377 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5378 if (indexval_it == m_map.abbrev_map.cend ())
5379 {
b98664d3 5380 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5381 "[in module %s]"),
ed2dc618 5382 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5383 return NULL;
5384 }
5385 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5386 enum class symbol_linkage {
5387 unknown,
5388 static_,
5389 extern_,
23c13d42 5390 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5391 dwarf2_per_cu_data *per_cu = NULL;
5392 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5393 {
5394 ULONGEST ull;
5395 switch (attr.form)
5396 {
5397 case DW_FORM_implicit_const:
5398 ull = attr.implicit_const;
5399 break;
5400 case DW_FORM_flag_present:
5401 ull = 1;
5402 break;
5403 case DW_FORM_udata:
5404 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5405 m_addr += bytes_read;
5406 break;
6dc55ce9 5407 case DW_FORM_ref4:
5408 ull = read_4_bytes (abfd, m_addr);
5409 m_addr += 4;
5410 break;
5411 case DW_FORM_ref8:
5412 ull = read_8_bytes (abfd, m_addr);
5413 m_addr += 8;
5414 break;
5415 case DW_FORM_ref_sig8:
5416 ull = read_8_bytes (abfd, m_addr);
5417 m_addr += 8;
5418 break;
927aa2e7 5419 default:
b98664d3 5420 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5421 dwarf_form_name (attr.form),
ed2dc618 5422 objfile_name (objfile));
927aa2e7
JK
5423 return NULL;
5424 }
5425 switch (attr.dw_idx)
5426 {
5427 case DW_IDX_compile_unit:
5428 /* Don't crash on bad data. */
b76e467d 5429 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5430 {
b98664d3 5431 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5432 " [in module %s]"),
5433 pulongest (ull),
5434 objfile_name (dwarf2_per_objfile->objfile));
5435 continue;
5436 }
ff4c9fec 5437 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5438 break;
8af5c486
JK
5439 case DW_IDX_type_unit:
5440 /* Don't crash on bad data. */
b2bdb8cf 5441 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5442 {
b98664d3 5443 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5444 " [in module %s]"),
5445 pulongest (ull),
5446 objfile_name (dwarf2_per_objfile->objfile));
5447 continue;
5448 }
ff4c9fec 5449 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5450 break;
6dc55ce9 5451 case DW_IDX_die_offset:
5452 /* In a per-CU index (as opposed to a per-module index), index
5453 entries without CU attribute implicitly refer to the single CU. */
5454 if (per_cu == NULL)
5455 per_cu = dwarf2_per_objfile->get_cu (0);
5456 break;
927aa2e7
JK
5457 case DW_IDX_GNU_internal:
5458 if (!m_map.augmentation_is_gdb)
5459 break;
23c13d42 5460 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5461 break;
5462 case DW_IDX_GNU_external:
5463 if (!m_map.augmentation_is_gdb)
5464 break;
23c13d42 5465 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5466 break;
5467 }
5468 }
5469
5470 /* Skip if already read in. */
5471 if (per_cu->v.quick->compunit_symtab)
5472 goto again;
5473
5474 /* Check static vs global. */
23c13d42 5475 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5476 {
2b79f376 5477 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5478 const bool symbol_is_static =
5479 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5480 if (want_static != symbol_is_static)
2b79f376 5481 goto again;
927aa2e7
JK
5482 }
5483
5484 /* Match dw2_symtab_iter_next, symbol_kind
5485 and debug_names::psymbol_tag. */
5486 switch (m_domain)
5487 {
5488 case VAR_DOMAIN:
5489 switch (indexval.dwarf_tag)
5490 {
5491 case DW_TAG_variable:
5492 case DW_TAG_subprogram:
5493 /* Some types are also in VAR_DOMAIN. */
5494 case DW_TAG_typedef:
5495 case DW_TAG_structure_type:
5496 break;
5497 default:
5498 goto again;
5499 }
5500 break;
5501 case STRUCT_DOMAIN:
5502 switch (indexval.dwarf_tag)
5503 {
5504 case DW_TAG_typedef:
5505 case DW_TAG_structure_type:
5506 break;
5507 default:
5508 goto again;
5509 }
5510 break;
5511 case LABEL_DOMAIN:
5512 switch (indexval.dwarf_tag)
5513 {
5514 case 0:
5515 case DW_TAG_variable:
5516 break;
5517 default:
5518 goto again;
5519 }
5520 break;
59c35742
AB
5521 case MODULE_DOMAIN:
5522 switch (indexval.dwarf_tag)
5523 {
5524 case DW_TAG_module:
5525 break;
5526 default:
5527 goto again;
5528 }
5529 break;
927aa2e7
JK
5530 default:
5531 break;
5532 }
5533
5534 /* Match dw2_expand_symtabs_matching, symbol_kind and
5535 debug_names::psymbol_tag. */
5536 switch (m_search)
4b514bc8 5537 {
927aa2e7
JK
5538 case VARIABLES_DOMAIN:
5539 switch (indexval.dwarf_tag)
4b514bc8 5540 {
927aa2e7
JK
5541 case DW_TAG_variable:
5542 break;
5543 default:
5544 goto again;
4b514bc8 5545 }
927aa2e7
JK
5546 break;
5547 case FUNCTIONS_DOMAIN:
5548 switch (indexval.dwarf_tag)
4b514bc8 5549 {
927aa2e7
JK
5550 case DW_TAG_subprogram:
5551 break;
5552 default:
5553 goto again;
4b514bc8 5554 }
927aa2e7
JK
5555 break;
5556 case TYPES_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_typedef:
5560 case DW_TAG_structure_type:
5561 break;
5562 default:
5563 goto again;
5564 }
5565 break;
59c35742
AB
5566 case MODULES_DOMAIN:
5567 switch (indexval.dwarf_tag)
5568 {
5569 case DW_TAG_module:
5570 break;
5571 default:
5572 goto again;
5573 }
927aa2e7
JK
5574 default:
5575 break;
4b514bc8 5576 }
927aa2e7
JK
5577
5578 return per_cu;
4b514bc8 5579}
61920122 5580
927aa2e7 5581static struct compunit_symtab *
c7f839cb 5582dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5583 const char *name, domain_enum domain)
4b514bc8 5584{
ed2dc618
SM
5585 struct dwarf2_per_objfile *dwarf2_per_objfile
5586 = get_dwarf2_per_objfile (objfile);
61920122 5587
927aa2e7
JK
5588 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5589 if (!mapp)
61920122 5590 {
927aa2e7
JK
5591 /* index is NULL if OBJF_READNOW. */
5592 return NULL;
5593 }
5594 const auto &map = *mapp;
9291a0cd 5595
2b79f376 5596 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5597
927aa2e7
JK
5598 struct compunit_symtab *stab_best = NULL;
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 {
5602 struct symbol *sym, *with_opaque = NULL;
58f0c718 5603 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5604 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5605 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5606
927aa2e7
JK
5607 sym = block_find_symbol (block, name, domain,
5608 block_find_non_opaque_type_preferred,
5609 &with_opaque);
9703b513 5610
927aa2e7
JK
5611 /* Some caution must be observed with overloaded functions and
5612 methods, since the index will not contain any overload
5613 information (but NAME might contain it). */
a3ec0bb1 5614
927aa2e7 5615 if (sym != NULL
987012b8 5616 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5617 return stab;
5618 if (with_opaque != NULL
987012b8 5619 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5620 stab_best = stab;
9703b513 5621
927aa2e7 5622 /* Keep looking through other CUs. */
9703b513
TT
5623 }
5624
927aa2e7 5625 return stab_best;
9703b513
TT
5626}
5627
927aa2e7
JK
5628/* This dumps minimal information about .debug_names. It is called
5629 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5630 uses this to verify that .debug_names has been loaded. */
9291a0cd 5631
927aa2e7
JK
5632static void
5633dw2_debug_names_dump (struct objfile *objfile)
5634{
ed2dc618
SM
5635 struct dwarf2_per_objfile *dwarf2_per_objfile
5636 = get_dwarf2_per_objfile (objfile);
5637
927aa2e7
JK
5638 gdb_assert (dwarf2_per_objfile->using_index);
5639 printf_filtered (".debug_names:");
5640 if (dwarf2_per_objfile->debug_names_table)
5641 printf_filtered (" exists\n");
5642 else
5643 printf_filtered (" faked for \"readnow\"\n");
5644 printf_filtered ("\n");
9291a0cd
TT
5645}
5646
9291a0cd 5647static void
927aa2e7
JK
5648dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5649 const char *func_name)
9291a0cd 5650{
ed2dc618
SM
5651 struct dwarf2_per_objfile *dwarf2_per_objfile
5652 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5653
927aa2e7
JK
5654 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5655 if (dwarf2_per_objfile->debug_names_table)
24c79950 5656 {
927aa2e7 5657 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5658
2b79f376 5659 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5660
927aa2e7
JK
5661 struct dwarf2_per_cu_data *per_cu;
5662 while ((per_cu = iter.next ()) != NULL)
58f0c718 5663 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5664 }
5665}
24c79950 5666
3b00ef10
TT
5667static void
5668dw2_debug_names_map_matching_symbols
5669 (struct objfile *objfile,
5670 const lookup_name_info &name, domain_enum domain,
5671 int global,
5672 gdb::function_view<symbol_found_callback_ftype> callback,
5673 symbol_compare_ftype *ordered_compare)
5674{
5675 struct dwarf2_per_objfile *dwarf2_per_objfile
5676 = get_dwarf2_per_objfile (objfile);
5677
5678 /* debug_names_table is NULL if OBJF_READNOW. */
5679 if (!dwarf2_per_objfile->debug_names_table)
5680 return;
5681
5682 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5683 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5684
5685 const char *match_name = name.ada ().lookup_name ().c_str ();
5686 auto matcher = [&] (const char *symname)
5687 {
5688 if (ordered_compare == nullptr)
5689 return true;
5690 return ordered_compare (symname, match_name) == 0;
5691 };
5692
5693 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5694 [&] (offset_type namei)
5695 {
5696 /* The name was matched, now expand corresponding CUs that were
5697 marked. */
5698 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5699
5700 struct dwarf2_per_cu_data *per_cu;
5701 while ((per_cu = iter.next ()) != NULL)
5702 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5703 return true;
5704 });
5705
5706 /* It's a shame we couldn't do this inside the
5707 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5708 that have already been expanded. Instead, this loop matches what
5709 the psymtab code does. */
5710 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5711 {
5712 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5713 if (cust != nullptr)
5714 {
5715 const struct block *block
5716 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5717 if (!iterate_over_symbols_terminated (block, name,
5718 domain, callback))
5719 break;
5720 }
5721 }
5722}
5723
927aa2e7
JK
5724static void
5725dw2_debug_names_expand_symtabs_matching
5726 (struct objfile *objfile,
5727 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5728 const lookup_name_info *lookup_name,
927aa2e7
JK
5729 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5730 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5731 enum search_domain kind)
5732{
ed2dc618
SM
5733 struct dwarf2_per_objfile *dwarf2_per_objfile
5734 = get_dwarf2_per_objfile (objfile);
9291a0cd 5735
927aa2e7
JK
5736 /* debug_names_table is NULL if OBJF_READNOW. */
5737 if (!dwarf2_per_objfile->debug_names_table)
5738 return;
9291a0cd 5739
ed2dc618 5740 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5741
c1a66c06
TV
5742 if (symbol_matcher == NULL && lookup_name == NULL)
5743 {
5744 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5745 {
5746 QUIT;
5747
5748 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5749 expansion_notify);
5750 }
5751 return;
5752 }
5753
44ed8f3e 5754 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5755
c1a66c06 5756 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5757 symbol_matcher,
5758 kind, [&] (offset_type namei)
927aa2e7 5759 {
927aa2e7
JK
5760 /* The name was matched, now expand corresponding CUs that were
5761 marked. */
5762 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5763
927aa2e7
JK
5764 struct dwarf2_per_cu_data *per_cu;
5765 while ((per_cu = iter.next ()) != NULL)
5766 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5767 expansion_notify);
3b00ef10 5768 return true;
44ed8f3e 5769 });
9291a0cd
TT
5770}
5771
927aa2e7 5772const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5773{
5774 dw2_has_symbols,
5775 dw2_find_last_source_symtab,
5776 dw2_forget_cached_source_info,
f8eba3c6 5777 dw2_map_symtabs_matching_filename,
927aa2e7 5778 dw2_debug_names_lookup_symbol,
d3214198 5779 NULL,
9291a0cd 5780 dw2_print_stats,
927aa2e7 5781 dw2_debug_names_dump,
927aa2e7 5782 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5783 dw2_expand_all_symtabs,
652a8996 5784 dw2_expand_symtabs_with_fullname,
3b00ef10 5785 dw2_debug_names_map_matching_symbols,
927aa2e7 5786 dw2_debug_names_expand_symtabs_matching,
43f3e411 5787 dw2_find_pc_sect_compunit_symtab,
71a3c369 5788 NULL,
9291a0cd
TT
5789 dw2_map_symbol_filenames
5790};
5791
4485a1c1
SM
5792/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5793 to either a dwarf2_per_objfile or dwz_file object. */
5794
5795template <typename T>
5796static gdb::array_view<const gdb_byte>
5797get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5798{
5799 dwarf2_section_info *section = &section_owner->gdb_index;
5800
96b79293 5801 if (section->empty ())
4485a1c1
SM
5802 return {};
5803
5804 /* Older elfutils strip versions could keep the section in the main
5805 executable while splitting it for the separate debug info file. */
96b79293 5806 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5807 return {};
5808
96b79293 5809 section->read (obj);
4485a1c1 5810
8bebfcda
PA
5811 /* dwarf2_section_info::size is a bfd_size_type, while
5812 gdb::array_view works with size_t. On 32-bit hosts, with
5813 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5814 is 32-bit. So we need an explicit narrowing conversion here.
5815 This is fine, because it's impossible to allocate or mmap an
5816 array/buffer larger than what size_t can represent. */
5817 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5818}
5819
87d6a7aa
SM
5820/* Lookup the index cache for the contents of the index associated to
5821 DWARF2_OBJ. */
5822
5823static gdb::array_view<const gdb_byte>
5824get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5825{
5826 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5827 if (build_id == nullptr)
5828 return {};
5829
5830 return global_index_cache.lookup_gdb_index (build_id,
5831 &dwarf2_obj->index_cache_res);
5832}
5833
5834/* Same as the above, but for DWZ. */
5835
5836static gdb::array_view<const gdb_byte>
5837get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5838{
5839 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5840 if (build_id == nullptr)
5841 return {};
5842
5843 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5844}
5845
3c0aa29a 5846/* See symfile.h. */
9291a0cd 5847
3c0aa29a
PA
5848bool
5849dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5850{
ed2dc618
SM
5851 struct dwarf2_per_objfile *dwarf2_per_objfile
5852 = get_dwarf2_per_objfile (objfile);
5853
9291a0cd
TT
5854 /* If we're about to read full symbols, don't bother with the
5855 indices. In this case we also don't care if some other debug
5856 format is making psymtabs, because they are all about to be
5857 expanded anyway. */
5858 if ((objfile->flags & OBJF_READNOW))
5859 {
9291a0cd 5860 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5861 create_all_comp_units (dwarf2_per_objfile);
5862 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5863 dwarf2_per_objfile->quick_file_names_table
5864 = create_quick_file_names_table
5865 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5866
b76e467d 5867 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5868 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5869 {
ff4c9fec 5870 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5871
45940949 5872 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
e254ef6a 5873 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5874 }
5875
5876 /* Return 1 so that gdb sees the "quick" functions. However,
5877 these functions will be no-ops because we will have expanded
5878 all symtabs. */
3c0aa29a
PA
5879 *index_kind = dw_index_kind::GDB_INDEX;
5880 return true;
9291a0cd
TT
5881 }
5882
ed2dc618 5883 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5884 {
5885 *index_kind = dw_index_kind::DEBUG_NAMES;
5886 return true;
5887 }
927aa2e7 5888
4485a1c1
SM
5889 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5890 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5891 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5892 {
5893 *index_kind = dw_index_kind::GDB_INDEX;
5894 return true;
5895 }
9291a0cd 5896
87d6a7aa
SM
5897 /* ... otherwise, try to find the index in the index cache. */
5898 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5899 get_gdb_index_contents_from_cache,
5900 get_gdb_index_contents_from_cache_dwz))
5901 {
5902 global_index_cache.hit ();
5903 *index_kind = dw_index_kind::GDB_INDEX;
5904 return true;
5905 }
5906
5907 global_index_cache.miss ();
3c0aa29a 5908 return false;
9291a0cd
TT
5909}
5910
5911\f
5912
dce234bc
PP
5913/* Build a partial symbol table. */
5914
5915void
f29dff0a 5916dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5917{
ed2dc618
SM
5918 struct dwarf2_per_objfile *dwarf2_per_objfile
5919 = get_dwarf2_per_objfile (objfile);
c9bf0622 5920
6eee24ce 5921 init_psymbol_list (objfile, 1024);
c906108c 5922
a70b8144 5923 try
c9bf0622
TT
5924 {
5925 /* This isn't really ideal: all the data we allocate on the
5926 objfile's obstack is still uselessly kept around. However,
5927 freeing it seems unsafe. */
906768f9 5928 psymtab_discarder psymtabs (objfile);
ed2dc618 5929 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5930 psymtabs.keep ();
87d6a7aa
SM
5931
5932 /* (maybe) store an index in the cache. */
5933 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5934 }
230d2906 5935 catch (const gdb_exception_error &except)
492d29ea
PA
5936 {
5937 exception_print (gdb_stderr, except);
5938 }
c906108c 5939}
c906108c 5940
3b80fe9b
DE
5941/* Find the base address of the compilation unit for range lists and
5942 location lists. It will normally be specified by DW_AT_low_pc.
5943 In DWARF-3 draft 4, the base address could be overridden by
5944 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5945 compilation units with discontinuous ranges. */
5946
5947static void
5948dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5949{
5950 struct attribute *attr;
5951
2b24b6e4 5952 cu->base_address.reset ();
3b80fe9b
DE
5953
5954 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5955 if (attr != nullptr)
2b24b6e4 5956 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5957 else
5958 {
5959 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5960 if (attr != nullptr)
2b24b6e4 5961 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5962 }
5963}
5964
36586728
TT
5965/* Helper function that returns the proper abbrev section for
5966 THIS_CU. */
5967
5968static struct dwarf2_section_info *
5969get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5970{
5971 struct dwarf2_section_info *abbrev;
ed2dc618 5972 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5973
5974 if (this_cu->is_dwz)
ed2dc618 5975 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5976 else
5977 abbrev = &dwarf2_per_objfile->abbrev;
5978
5979 return abbrev;
5980}
5981
f4dc4d17
DE
5982/* Fetch the abbreviation table offset from a comp or type unit header. */
5983
5984static sect_offset
ed2dc618
SM
5985read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5986 struct dwarf2_section_info *section,
9c541725 5987 sect_offset sect_off)
f4dc4d17 5988{
96b79293 5989 bfd *abfd = section->get_bfd_owner ();
d521ce57 5990 const gdb_byte *info_ptr;
ac298888 5991 unsigned int initial_length_size, offset_size;
43988095 5992 uint16_t version;
f4dc4d17 5993
96b79293 5994 section->read (dwarf2_per_objfile->objfile);
9c541725 5995 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5996 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5997 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5998 info_ptr += initial_length_size;
5999
6000 version = read_2_bytes (abfd, info_ptr);
6001 info_ptr += 2;
6002 if (version >= 5)
6003 {
6004 /* Skip unit type and address size. */
6005 info_ptr += 2;
6006 }
6007
24aa364d 6008 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6009}
6010
b83470bf
TT
6011/* A partial symtab that is used only for include files. */
6012struct dwarf2_include_psymtab : public partial_symtab
6013{
6014 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6015 : partial_symtab (filename, objfile)
6016 {
6017 }
6018
6019 void read_symtab (struct objfile *objfile) override
6020 {
194d088f
TV
6021 /* It's an include file, no symbols to read for it.
6022 Everything is in the includer symtab. */
6023
6024 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6025 expansion of the includer psymtab. We use the dependencies[0] field to
6026 model the includer. But if we go the regular route of calling
6027 expand_psymtab here, and having expand_psymtab call expand_dependencies
6028 to expand the includer, we'll only use expand_psymtab on the includer
6029 (making it a non-toplevel psymtab), while if we expand the includer via
6030 another path, we'll use read_symtab (making it a toplevel psymtab).
6031 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6032 psymtab, and trigger read_symtab on the includer here directly. */
6033 includer ()->read_symtab (objfile);
b83470bf
TT
6034 }
6035
6036 void expand_psymtab (struct objfile *objfile) override
6037 {
194d088f
TV
6038 /* This is not called by read_symtab, and should not be called by any
6039 expand_dependencies. */
6040 gdb_assert (false);
b83470bf
TT
6041 }
6042
5717c425 6043 bool readin_p (struct objfile *objfile) const override
b83470bf 6044 {
5717c425 6045 return includer ()->readin_p (objfile);
b83470bf
TT
6046 }
6047
5717c425 6048 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6049 {
6050 return nullptr;
6051 }
6052
6053private:
194d088f
TV
6054 partial_symtab *includer () const
6055 {
6056 /* An include psymtab has exactly one dependency: the psymtab that
6057 includes it. */
6058 gdb_assert (this->number_of_dependencies == 1);
6059 return this->dependencies[0];
6060 }
b83470bf
TT
6061};
6062
aaa75496
JB
6063/* Allocate a new partial symtab for file named NAME and mark this new
6064 partial symtab as being an include of PST. */
6065
6066static void
891813be 6067dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6068 struct objfile *objfile)
6069{
b83470bf 6070 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6071
fbd9ab74 6072 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6073 subpst->dirname = pst->dirname;
fbd9ab74 6074
a9342b62 6075 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6076 subpst->dependencies[0] = pst;
6077 subpst->number_of_dependencies = 1;
aaa75496
JB
6078}
6079
6080/* Read the Line Number Program data and extract the list of files
6081 included by the source file represented by PST. Build an include
d85a05f0 6082 partial symtab for each of these included files. */
aaa75496
JB
6083
6084static void
6085dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6086 struct die_info *die,
891813be 6087 dwarf2_psymtab *pst)
aaa75496 6088{
fff8551c 6089 line_header_up lh;
d85a05f0 6090 struct attribute *attr;
aaa75496 6091
d85a05f0 6092 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6093 if (attr != nullptr)
9c541725 6094 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6095 if (lh == NULL)
6096 return; /* No linetable, so no includes. */
6097
79748972
TT
6098 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6099 that we pass in the raw text_low here; that is ok because we're
6100 only decoding the line table to make include partial symtabs, and
6101 so the addresses aren't really used. */
4ae976d1 6102 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6103 pst->raw_text_low (), 1);
aaa75496
JB
6104}
6105
348e048f 6106static hashval_t
52dc124a 6107hash_signatured_type (const void *item)
348e048f 6108{
9a3c8263
SM
6109 const struct signatured_type *sig_type
6110 = (const struct signatured_type *) item;
9a619af0 6111
348e048f 6112 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6113 return sig_type->signature;
348e048f
DE
6114}
6115
6116static int
52dc124a 6117eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6118{
9a3c8263
SM
6119 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6120 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6121
348e048f
DE
6122 return lhs->signature == rhs->signature;
6123}
6124
1fd400ff
TT
6125/* Allocate a hash table for signatured types. */
6126
b0b6a987 6127static htab_up
298e9637 6128allocate_signatured_type_table ()
1fd400ff 6129{
b0b6a987
TT
6130 return htab_up (htab_create_alloc (41,
6131 hash_signatured_type,
6132 eq_signatured_type,
6133 NULL, xcalloc, xfree));
1fd400ff
TT
6134}
6135
d467dd73 6136/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6137
6138static int
d467dd73 6139add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6140{
9a3c8263 6141 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6142 std::vector<signatured_type *> *all_type_units
6143 = (std::vector<signatured_type *> *) datum;
1fd400ff 6144
b2bdb8cf 6145 all_type_units->push_back (sigt);
1fd400ff
TT
6146
6147 return 1;
6148}
6149
78d4d2c5 6150/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6151 and fill them into TYPES_HTAB. It will process only type units,
6152 therefore DW_UT_type. */
c88ee1f0 6153
78d4d2c5 6154static void
ed2dc618
SM
6155create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6156 struct dwo_file *dwo_file,
b0b6a987 6157 dwarf2_section_info *section, htab_up &types_htab,
43988095 6158 rcuh_kind section_kind)
348e048f 6159{
3019eac3 6160 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6161 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6162 bfd *abfd;
6163 const gdb_byte *info_ptr, *end_ptr;
348e048f 6164
4bdcc0c1
DE
6165 abbrev_section = (dwo_file != NULL
6166 ? &dwo_file->sections.abbrev
6167 : &dwarf2_per_objfile->abbrev);
6168
b4f54984 6169 if (dwarf_read_debug)
43988095 6170 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6171 section->get_name (),
6172 abbrev_section->get_file_name ());
09406207 6173
96b79293 6174 section->read (objfile);
78d4d2c5 6175 info_ptr = section->buffer;
348e048f 6176
78d4d2c5
JK
6177 if (info_ptr == NULL)
6178 return;
348e048f 6179
78d4d2c5
JK
6180 /* We can't set abfd until now because the section may be empty or
6181 not present, in which case the bfd is unknown. */
96b79293 6182 abfd = section->get_bfd_owner ();
348e048f 6183
c0ab21c2
TT
6184 /* We don't use cutu_reader here because we don't need to read
6185 any dies: the signature is in the header. */
3019eac3 6186
78d4d2c5
JK
6187 end_ptr = info_ptr + section->size;
6188 while (info_ptr < end_ptr)
6189 {
78d4d2c5
JK
6190 struct signatured_type *sig_type;
6191 struct dwo_unit *dwo_tu;
6192 void **slot;
6193 const gdb_byte *ptr = info_ptr;
6194 struct comp_unit_head header;
6195 unsigned int length;
8b70b953 6196
9c541725 6197 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6198
a49dd8dd
JK
6199 /* Initialize it due to a false compiler warning. */
6200 header.signature = -1;
9c541725 6201 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6202
78d4d2c5
JK
6203 /* We need to read the type's signature in order to build the hash
6204 table, but we don't need anything else just yet. */
348e048f 6205
ed2dc618 6206 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6207 abbrev_section, ptr, section_kind);
348e048f 6208
4057dfde 6209 length = header.get_length ();
6caca83c 6210
78d4d2c5
JK
6211 /* Skip dummy type units. */
6212 if (ptr >= info_ptr + length
43988095
JK
6213 || peek_abbrev_code (abfd, ptr) == 0
6214 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6215 {
6216 info_ptr += length;
6217 continue;
6218 }
dee91e82 6219
78d4d2c5
JK
6220 if (types_htab == NULL)
6221 {
6222 if (dwo_file)
298e9637 6223 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6224 else
298e9637 6225 types_htab = allocate_signatured_type_table ();
78d4d2c5 6226 }
8b70b953 6227
78d4d2c5
JK
6228 if (dwo_file)
6229 {
6230 sig_type = NULL;
45940949 6231 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
78d4d2c5
JK
6232 struct dwo_unit);
6233 dwo_tu->dwo_file = dwo_file;
43988095 6234 dwo_tu->signature = header.signature;
9c541725 6235 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6236 dwo_tu->section = section;
9c541725 6237 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6238 dwo_tu->length = length;
6239 }
6240 else
6241 {
6242 /* N.B.: type_offset is not usable if this type uses a DWO file.
6243 The real type_offset is in the DWO file. */
6244 dwo_tu = NULL;
45940949 6245 sig_type = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
78d4d2c5 6246 struct signatured_type);
43988095 6247 sig_type->signature = header.signature;
9c541725 6248 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6249 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6250 sig_type->per_cu.is_debug_types = 1;
6251 sig_type->per_cu.section = section;
9c541725 6252 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6253 sig_type->per_cu.length = length;
6254 }
6255
b0b6a987 6256 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6257 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6258 INSERT);
6259 gdb_assert (slot != NULL);
6260 if (*slot != NULL)
6261 {
9c541725 6262 sect_offset dup_sect_off;
0349ea22 6263
3019eac3
DE
6264 if (dwo_file)
6265 {
78d4d2c5
JK
6266 const struct dwo_unit *dup_tu
6267 = (const struct dwo_unit *) *slot;
6268
9c541725 6269 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6270 }
6271 else
6272 {
78d4d2c5
JK
6273 const struct signatured_type *dup_tu
6274 = (const struct signatured_type *) *slot;
6275
9c541725 6276 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6277 }
8b70b953 6278
b98664d3 6279 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6280 " the entry at offset %s, signature %s"),
6281 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6282 hex_string (header.signature));
78d4d2c5
JK
6283 }
6284 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6285
78d4d2c5 6286 if (dwarf_read_debug > 1)
9d8780f0
SM
6287 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6288 sect_offset_str (sect_off),
43988095 6289 hex_string (header.signature));
3019eac3 6290
78d4d2c5
JK
6291 info_ptr += length;
6292 }
6293}
3019eac3 6294
78d4d2c5
JK
6295/* Create the hash table of all entries in the .debug_types
6296 (or .debug_types.dwo) section(s).
6297 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6298 otherwise it is NULL.
b3c8eb43 6299
78d4d2c5 6300 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6301
78d4d2c5 6302 Note: This function processes DWO files only, not DWP files. */
348e048f 6303
78d4d2c5 6304static void
ed2dc618
SM
6305create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6306 struct dwo_file *dwo_file,
fd5866f6 6307 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6308 htab_up &types_htab)
78d4d2c5 6309{
fd5866f6
SM
6310 for (dwarf2_section_info &section : type_sections)
6311 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6312 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6313}
6314
6315/* Create the hash table of all entries in the .debug_types section,
6316 and initialize all_type_units.
6317 The result is zero if there is an error (e.g. missing .debug_types section),
6318 otherwise non-zero. */
6319
6320static int
ed2dc618 6321create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6322{
b0b6a987 6323 htab_up types_htab;
3019eac3 6324
ed2dc618
SM
6325 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6326 &dwarf2_per_objfile->info, types_htab,
43988095 6327 rcuh_kind::COMPILE);
ed2dc618
SM
6328 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6329 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6330 if (types_htab == NULL)
6331 {
6332 dwarf2_per_objfile->signatured_types = NULL;
6333 return 0;
6334 }
6335
b0b6a987 6336 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6337
b2bdb8cf 6338 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6339 dwarf2_per_objfile->all_type_units.reserve
6340 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6341
b0b6a987
TT
6342 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6343 add_signatured_type_cu_to_table,
b2bdb8cf 6344 &dwarf2_per_objfile->all_type_units);
1fd400ff 6345
348e048f
DE
6346 return 1;
6347}
6348
6aa5f3a6
DE
6349/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6350 If SLOT is non-NULL, it is the entry to use in the hash table.
6351 Otherwise we find one. */
6352
6353static struct signatured_type *
ed2dc618
SM
6354add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6355 void **slot)
6aa5f3a6 6356{
b2bdb8cf
SM
6357 if (dwarf2_per_objfile->all_type_units.size ()
6358 == dwarf2_per_objfile->all_type_units.capacity ())
6359 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6360
45940949 6361 signatured_type *sig_type = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
b2bdb8cf
SM
6362 struct signatured_type);
6363
6364 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6365 sig_type->signature = sig;
6366 sig_type->per_cu.is_debug_types = 1;
6367 if (dwarf2_per_objfile->using_index)
6368 {
6369 sig_type->per_cu.v.quick =
45940949 6370 OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
6aa5f3a6
DE
6371 struct dwarf2_per_cu_quick_data);
6372 }
6373
6374 if (slot == NULL)
6375 {
b0b6a987 6376 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6377 sig_type, INSERT);
6378 }
6379 gdb_assert (*slot == NULL);
6380 *slot = sig_type;
6381 /* The rest of sig_type must be filled in by the caller. */
6382 return sig_type;
6383}
6384
a2ce51a0
DE
6385/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6386 Fill in SIG_ENTRY with DWO_ENTRY. */
6387
6388static void
ed2dc618 6389fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6390 struct signatured_type *sig_entry,
6391 struct dwo_unit *dwo_entry)
6392{
7ee85ab1 6393 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6394 gdb_assert (! sig_entry->per_cu.queued);
6395 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6396 if (dwarf2_per_objfile->using_index)
6397 {
6398 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6399 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6400 }
6401 else
6402 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6403 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6404 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6405 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6406 gdb_assert (sig_entry->dwo_unit == NULL);
6407
6408 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6409 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6410 sig_entry->per_cu.length = dwo_entry->length;
6411 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6412 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6413 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6414 sig_entry->dwo_unit = dwo_entry;
6415}
6416
6417/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6418 If we haven't read the TU yet, create the signatured_type data structure
6419 for a TU to be read in directly from a DWO file, bypassing the stub.
6420 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6421 using .gdb_index, then when reading a CU we want to stay in the DWO file
6422 containing that CU. Otherwise we could end up reading several other DWO
6423 files (due to comdat folding) to process the transitive closure of all the
6424 mentioned TUs, and that can be slow. The current DWO file will have every
6425 type signature that it needs.
a2ce51a0
DE
6426 We only do this for .gdb_index because in the psymtab case we already have
6427 to read all the DWOs to build the type unit groups. */
6428
6429static struct signatured_type *
6430lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6431{
518817b3
SM
6432 struct dwarf2_per_objfile *dwarf2_per_objfile
6433 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6434 struct dwo_file *dwo_file;
6435 struct dwo_unit find_dwo_entry, *dwo_entry;
6436 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6437 void **slot;
a2ce51a0
DE
6438
6439 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6440
6aa5f3a6
DE
6441 /* If TU skeletons have been removed then we may not have read in any
6442 TUs yet. */
6443 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6444 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6445
6446 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6447 Use the global signatured_types array to do our own comdat-folding
6448 of types. If this is the first time we're reading this TU, and
6449 the TU has an entry in .gdb_index, replace the recorded data from
6450 .gdb_index with this TU. */
a2ce51a0 6451
a2ce51a0 6452 find_sig_entry.signature = sig;
b0b6a987 6453 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6454 &find_sig_entry, INSERT);
9a3c8263 6455 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6456
6457 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6458 read. Don't reassign the global entry to point to this DWO if that's
6459 the case. Also note that if the TU is already being read, it may not
6460 have come from a DWO, the program may be a mix of Fission-compiled
6461 code and non-Fission-compiled code. */
6462
6463 /* Have we already tried to read this TU?
6464 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6465 needn't exist in the global table yet). */
6466 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6467 return sig_entry;
6468
6aa5f3a6
DE
6469 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6470 dwo_unit of the TU itself. */
6471 dwo_file = cu->dwo_unit->dwo_file;
6472
a2ce51a0
DE
6473 /* Ok, this is the first time we're reading this TU. */
6474 if (dwo_file->tus == NULL)
6475 return NULL;
6476 find_dwo_entry.signature = sig;
b0b6a987
TT
6477 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6478 &find_dwo_entry);
a2ce51a0
DE
6479 if (dwo_entry == NULL)
6480 return NULL;
6481
6aa5f3a6
DE
6482 /* If the global table doesn't have an entry for this TU, add one. */
6483 if (sig_entry == NULL)
ed2dc618 6484 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6485
ed2dc618 6486 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6487 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6488 return sig_entry;
6489}
6490
a2ce51a0
DE
6491/* Subroutine of lookup_signatured_type.
6492 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6493 then try the DWP file. If the TU stub (skeleton) has been removed then
6494 it won't be in .gdb_index. */
a2ce51a0
DE
6495
6496static struct signatured_type *
6497lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6498{
518817b3
SM
6499 struct dwarf2_per_objfile *dwarf2_per_objfile
6500 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6501 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6502 struct dwo_unit *dwo_entry;
6503 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6504 void **slot;
a2ce51a0
DE
6505
6506 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6507 gdb_assert (dwp_file != NULL);
6508
6aa5f3a6
DE
6509 /* If TU skeletons have been removed then we may not have read in any
6510 TUs yet. */
6511 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6512 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6513
6aa5f3a6 6514 find_sig_entry.signature = sig;
b0b6a987 6515 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6516 &find_sig_entry, INSERT);
9a3c8263 6517 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6518
6519 /* Have we already tried to read this TU?
6520 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6521 needn't exist in the global table yet). */
6522 if (sig_entry != NULL)
6523 return sig_entry;
6524
a2ce51a0
DE
6525 if (dwp_file->tus == NULL)
6526 return NULL;
ed2dc618 6527 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6528 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6529 if (dwo_entry == NULL)
6530 return NULL;
6531
ed2dc618
SM
6532 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6533 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6534
a2ce51a0
DE
6535 return sig_entry;
6536}
6537
380bca97 6538/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6539 Returns NULL if signature SIG is not present in the table.
6540 It is up to the caller to complain about this. */
348e048f
DE
6541
6542static struct signatured_type *
a2ce51a0 6543lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6544{
518817b3
SM
6545 struct dwarf2_per_objfile *dwarf2_per_objfile
6546 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6547
a2ce51a0
DE
6548 if (cu->dwo_unit
6549 && dwarf2_per_objfile->using_index)
6550 {
6551 /* We're in a DWO/DWP file, and we're using .gdb_index.
6552 These cases require special processing. */
ed2dc618 6553 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6554 return lookup_dwo_signatured_type (cu, sig);
6555 else
6556 return lookup_dwp_signatured_type (cu, sig);
6557 }
6558 else
6559 {
6560 struct signatured_type find_entry, *entry;
348e048f 6561
a2ce51a0
DE
6562 if (dwarf2_per_objfile->signatured_types == NULL)
6563 return NULL;
6564 find_entry.signature = sig;
9a3c8263 6565 entry = ((struct signatured_type *)
b0b6a987
TT
6566 htab_find (dwarf2_per_objfile->signatured_types.get (),
6567 &find_entry));
a2ce51a0
DE
6568 return entry;
6569 }
348e048f 6570}
18a8505e 6571
42e7ad6c 6572/* Low level DIE reading support. */
348e048f 6573
d85a05f0
DJ
6574/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6575
6576static void
6577init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6578 struct dwarf2_cu *cu,
3019eac3 6579 struct dwarf2_section_info *section,
685af9cd
TT
6580 struct dwo_file *dwo_file,
6581 struct abbrev_table *abbrev_table)
d85a05f0 6582{
fceca515 6583 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6584 reader->abfd = section->get_bfd_owner ();
d85a05f0 6585 reader->cu = cu;
3019eac3 6586 reader->dwo_file = dwo_file;
dee91e82
DE
6587 reader->die_section = section;
6588 reader->buffer = section->buffer;
f664829e 6589 reader->buffer_end = section->buffer + section->size;
685af9cd 6590 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6591}
6592
c0ab21c2 6593/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6594 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6595 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6596 already.
6597
6598 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6599 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6600 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6601 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6602 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6603 STUB_COMP_DIR may be non-NULL.
3e225074 6604 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6605 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6606 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6607 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6608 kept around for at least as long as *RESULT_READER.
6609
b0c7bfa9
DE
6610 The result is non-zero if a valid (non-dummy) DIE was found. */
6611
6612static int
6613read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6614 struct dwo_unit *dwo_unit,
b0c7bfa9 6615 struct die_info *stub_comp_unit_die,
a2ce51a0 6616 const char *stub_comp_dir,
b0c7bfa9 6617 struct die_reader_specs *result_reader,
d521ce57 6618 const gdb_byte **result_info_ptr,
b0c7bfa9 6619 struct die_info **result_comp_unit_die,
685af9cd 6620 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6621{
ed2dc618 6622 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6623 struct objfile *objfile = dwarf2_per_objfile->objfile;
6624 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6625 bfd *abfd;
d521ce57 6626 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6627 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6628 int i,num_extra_attrs;
6629 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6630 struct die_info *comp_unit_die;
6631
b0aeadb3
DE
6632 /* At most one of these may be provided. */
6633 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6634
b0c7bfa9
DE
6635 /* These attributes aren't processed until later:
6636 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6637 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6638 referenced later. However, these attributes are found in the stub
6639 which we won't have later. In order to not impose this complication
6640 on the rest of the code, we read them here and copy them to the
6641 DWO CU/TU die. */
b0c7bfa9
DE
6642
6643 stmt_list = NULL;
6644 low_pc = NULL;
6645 high_pc = NULL;
6646 ranges = NULL;
6647 comp_dir = NULL;
6648
6649 if (stub_comp_unit_die != NULL)
6650 {
6651 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6652 DWO file. */
6653 if (! this_cu->is_debug_types)
6654 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6655 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6656 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6657 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6658 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6659
a39fdb41 6660 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6661
18a8505e
AT
6662 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6663 here (if needed). We need the value before we can process
6664 DW_AT_ranges. */
a39fdb41 6665 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6666 }
a2ce51a0
DE
6667 else if (stub_comp_dir != NULL)
6668 {
6669 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6670 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6671 comp_dir->name = DW_AT_comp_dir;
6672 comp_dir->form = DW_FORM_string;
6673 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6674 DW_STRING (comp_dir) = stub_comp_dir;
6675 }
b0c7bfa9
DE
6676
6677 /* Set up for reading the DWO CU/TU. */
6678 cu->dwo_unit = dwo_unit;
685af9cd 6679 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6680 section->read (objfile);
6681 abfd = section->get_bfd_owner ();
9c541725
PA
6682 begin_info_ptr = info_ptr = (section->buffer
6683 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6684 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6685
6686 if (this_cu->is_debug_types)
6687 {
b0c7bfa9
DE
6688 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6689
ed2dc618
SM
6690 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6691 &cu->header, section,
b0c7bfa9 6692 dwo_abbrev_section,
43988095 6693 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6694 /* This is not an assert because it can be caused by bad debug info. */
43988095 6695 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6696 {
6697 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6698 " TU at offset %s [in module %s]"),
a2ce51a0 6699 hex_string (sig_type->signature),
43988095 6700 hex_string (cu->header.signature),
9d8780f0 6701 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6702 bfd_get_filename (abfd));
6703 }
9c541725 6704 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6705 /* For DWOs coming from DWP files, we don't know the CU length
6706 nor the type's offset in the TU until now. */
4057dfde 6707 dwo_unit->length = cu->header.get_length ();
9c541725 6708 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6709
6710 /* Establish the type offset that can be used to lookup the type.
6711 For DWO files, we don't know it until now. */
9c541725
PA
6712 sig_type->type_offset_in_section
6713 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6714 }
6715 else
6716 {
ed2dc618
SM
6717 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6718 &cu->header, section,
b0c7bfa9 6719 dwo_abbrev_section,
43988095 6720 info_ptr, rcuh_kind::COMPILE);
9c541725 6721 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6722 /* For DWOs coming from DWP files, we don't know the CU length
6723 until now. */
4057dfde 6724 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6725 }
6726
685af9cd 6727 *result_dwo_abbrev_table
86de1d91
TT
6728 = abbrev_table::read (objfile, dwo_abbrev_section,
6729 cu->header.abbrev_sect_off);
685af9cd
TT
6730 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6731 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6732
6733 /* Read in the die, but leave space to copy over the attributes
6734 from the stub. This has the benefit of simplifying the rest of
6735 the code - all the work to maintain the illusion of a single
6736 DW_TAG_{compile,type}_unit DIE is done here. */
6737 num_extra_attrs = ((stmt_list != NULL)
6738 + (low_pc != NULL)
6739 + (high_pc != NULL)
6740 + (ranges != NULL)
6741 + (comp_dir != NULL));
6742 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6743 num_extra_attrs);
b0c7bfa9
DE
6744
6745 /* Copy over the attributes from the stub to the DIE we just read in. */
6746 comp_unit_die = *result_comp_unit_die;
6747 i = comp_unit_die->num_attrs;
6748 if (stmt_list != NULL)
6749 comp_unit_die->attrs[i++] = *stmt_list;
6750 if (low_pc != NULL)
6751 comp_unit_die->attrs[i++] = *low_pc;
6752 if (high_pc != NULL)
6753 comp_unit_die->attrs[i++] = *high_pc;
6754 if (ranges != NULL)
6755 comp_unit_die->attrs[i++] = *ranges;
6756 if (comp_dir != NULL)
6757 comp_unit_die->attrs[i++] = *comp_dir;
6758 comp_unit_die->num_attrs += num_extra_attrs;
6759
b4f54984 6760 if (dwarf_die_debug)
bf6af496
DE
6761 {
6762 fprintf_unfiltered (gdb_stdlog,
6763 "Read die from %s@0x%x of %s:\n",
96b79293 6764 section->get_name (),
bf6af496
DE
6765 (unsigned) (begin_info_ptr - section->buffer),
6766 bfd_get_filename (abfd));
b4f54984 6767 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6768 }
6769
b0c7bfa9
DE
6770 /* Skip dummy compilation units. */
6771 if (info_ptr >= begin_info_ptr + dwo_unit->length
6772 || peek_abbrev_code (abfd, info_ptr) == 0)
6773 return 0;
6774
6775 *result_info_ptr = info_ptr;
6776 return 1;
6777}
6778
a084a2a6
AT
6779/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6780 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6781 signature is part of the header. */
6782static gdb::optional<ULONGEST>
6783lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6784{
6785 if (cu->header.version >= 5)
6786 return cu->header.signature;
6787 struct attribute *attr;
6788 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6789 if (attr == nullptr)
6790 return gdb::optional<ULONGEST> ();
6791 return DW_UNSND (attr);
6792}
6793
c0ab21c2 6794/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6795 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6796 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6797
6798static struct dwo_unit *
6799lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6800 struct die_info *comp_unit_die,
6801 const char *dwo_name)
b0c7bfa9
DE
6802{
6803 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6804 struct dwo_unit *dwo_unit;
c0ab21c2 6805 const char *comp_dir;
b0c7bfa9 6806
a2ce51a0
DE
6807 gdb_assert (cu != NULL);
6808
b0c7bfa9 6809 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6810 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6811 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6812
6813 if (this_cu->is_debug_types)
6814 {
6815 struct signatured_type *sig_type;
6816
6817 /* Since this_cu is the first member of struct signatured_type,
6818 we can go from a pointer to one to a pointer to the other. */
6819 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6820 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6821 }
6822 else
6823 {
a084a2a6
AT
6824 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6825 if (!signature.has_value ())
b0c7bfa9
DE
6826 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6827 " [in module %s]"),
e3b94546 6828 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6829 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6830 *signature);
b0c7bfa9
DE
6831 }
6832
b0c7bfa9
DE
6833 return dwo_unit;
6834}
6835
c0ab21c2 6836/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6837 See it for a description of the parameters.
fcd3b13d 6838 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6839
c0ab21c2
TT
6840void
6841cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6842 int use_existing_cu)
a2ce51a0 6843{
a2ce51a0 6844 struct signatured_type *sig_type;
a2ce51a0
DE
6845
6846 /* Verify we can do the following downcast, and that we have the
6847 data we need. */
6848 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6849 sig_type = (struct signatured_type *) this_cu;
6850 gdb_assert (sig_type->dwo_unit != NULL);
6851
6aa5f3a6
DE
6852 if (use_existing_cu && this_cu->cu != NULL)
6853 {
6854 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6855 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6856 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6857 }
6858 else
6859 {
6860 /* If !use_existing_cu, this_cu->cu must be NULL. */
6861 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6862 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6863 }
6864
6865 /* A future optimization, if needed, would be to use an existing
6866 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6867 could share abbrev tables. */
a2ce51a0
DE
6868
6869 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6870 NULL /* stub_comp_unit_die */,
6871 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6872 this, &info_ptr,
3e225074 6873 &comp_unit_die,
c0ab21c2 6874 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6875 {
6876 /* Dummy die. */
c0ab21c2 6877 dummy_p = true;
a2ce51a0 6878 }
a2ce51a0
DE
6879}
6880
fd820528 6881/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6882 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6883
f4dc4d17
DE
6884 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6885 Otherwise the table specified in the comp unit header is read in and used.
6886 This is an optimization for when we already have the abbrev table.
6887
dee91e82 6888 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6889 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6890
c0ab21c2
TT
6891cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6892 struct abbrev_table *abbrev_table,
6751ebae 6893 int use_existing_cu,
c0ab21c2
TT
6894 bool skip_partial)
6895 : die_reader_specs {},
6751ebae 6896 m_this_cu (this_cu)
c906108c 6897{
ed2dc618 6898 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6899 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6900 struct dwarf2_section_info *section = this_cu->section;
96b79293 6901 bfd *abfd = section->get_bfd_owner ();
dee91e82 6902 struct dwarf2_cu *cu;
c0ab21c2 6903 const gdb_byte *begin_info_ptr;
dee91e82 6904 struct signatured_type *sig_type = NULL;
4bdcc0c1 6905 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6906 /* Non-zero if CU currently points to a DWO file and we need to
6907 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6908 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6909 int rereading_dwo_cu = 0;
c906108c 6910
b4f54984 6911 if (dwarf_die_debug)
9d8780f0 6912 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6913 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6914 sect_offset_str (this_cu->sect_off));
09406207 6915
a2ce51a0
DE
6916 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6917 file (instead of going through the stub), short-circuit all of this. */
6918 if (this_cu->reading_dwo_directly)
6919 {
6920 /* Narrow down the scope of possibilities to have to understand. */
6921 gdb_assert (this_cu->is_debug_types);
6922 gdb_assert (abbrev_table == NULL);
6751ebae 6923 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6924 return;
6925 }
6926
dee91e82 6927 /* This is cheap if the section is already read in. */
96b79293 6928 section->read (objfile);
dee91e82 6929
9c541725 6930 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6931
6932 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6933
6934 if (use_existing_cu && this_cu->cu != NULL)
6935 {
6936 cu = this_cu->cu;
42e7ad6c
DE
6937 /* If this CU is from a DWO file we need to start over, we need to
6938 refetch the attributes from the skeleton CU.
6939 This could be optimized by retrieving those attributes from when we
6940 were here the first time: the previous comp_unit_die was stored in
6941 comp_unit_obstack. But there's no data yet that we need this
6942 optimization. */
6943 if (cu->dwo_unit != NULL)
6944 rereading_dwo_cu = 1;
dee91e82
DE
6945 }
6946 else
6947 {
6948 /* If !use_existing_cu, this_cu->cu must be NULL. */
6949 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6950 m_new_cu.reset (new dwarf2_cu (this_cu));
6951 cu = m_new_cu.get ();
42e7ad6c 6952 }
dee91e82 6953
b0c7bfa9 6954 /* Get the header. */
9c541725 6955 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6956 {
6957 /* We already have the header, there's no need to read it in again. */
9c541725 6958 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6959 }
6960 else
6961 {
3019eac3 6962 if (this_cu->is_debug_types)
dee91e82 6963 {
ed2dc618
SM
6964 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6965 &cu->header, section,
4bdcc0c1 6966 abbrev_section, info_ptr,
43988095 6967 rcuh_kind::TYPE);
dee91e82 6968
42e7ad6c
DE
6969 /* Since per_cu is the first member of struct signatured_type,
6970 we can go from a pointer to one to a pointer to the other. */
6971 sig_type = (struct signatured_type *) this_cu;
43988095 6972 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6973 gdb_assert (sig_type->type_offset_in_tu
6974 == cu->header.type_cu_offset_in_tu);
6975 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6976
42e7ad6c
DE
6977 /* LENGTH has not been set yet for type units if we're
6978 using .gdb_index. */
4057dfde 6979 this_cu->length = cu->header.get_length ();
3019eac3
DE
6980
6981 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6982 sig_type->type_offset_in_section =
6983 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6984
6985 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6986 }
6987 else
6988 {
ed2dc618
SM
6989 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6990 &cu->header, section,
4bdcc0c1 6991 abbrev_section,
43988095
JK
6992 info_ptr,
6993 rcuh_kind::COMPILE);
dee91e82 6994
9c541725 6995 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
6996 if (this_cu->length == 0)
6997 this_cu->length = cu->header.get_length ();
6998 else
6999 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7000 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7001 }
7002 }
10b3939b 7003
6caca83c 7004 /* Skip dummy compilation units. */
dee91e82 7005 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7006 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7007 {
7008 dummy_p = true;
7009 return;
7010 }
6caca83c 7011
433df2d4
DE
7012 /* If we don't have them yet, read the abbrevs for this compilation unit.
7013 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7014 done. */
f4dc4d17 7015 if (abbrev_table != NULL)
685af9cd
TT
7016 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7017 else
f4dc4d17 7018 {
c0ab21c2 7019 m_abbrev_table_holder
86de1d91
TT
7020 = abbrev_table::read (objfile, abbrev_section,
7021 cu->header.abbrev_sect_off);
c0ab21c2 7022 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7023 }
af703f96 7024
dee91e82 7025 /* Read the top level CU/TU die. */
c0ab21c2 7026 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7027 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7028
58f0c718 7029 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7030 {
7031 dummy_p = true;
7032 return;
7033 }
58f0c718 7034
b0c7bfa9 7035 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7036 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7037 table from the DWO file and pass the ownership over to us. It will be
7038 referenced from READER, so we must make sure to free it after we're done
7039 with READER.
7040
b0c7bfa9
DE
7041 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7042 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7043 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7044 if (dwo_name != nullptr)
3019eac3 7045 {
3019eac3 7046 struct dwo_unit *dwo_unit;
b0c7bfa9 7047 struct die_info *dwo_comp_unit_die;
3019eac3 7048
3e225074 7049 if (comp_unit_die->has_children)
6a506a2d 7050 {
b98664d3 7051 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7052 " has children (offset %s) [in module %s]"),
7053 sect_offset_str (this_cu->sect_off),
7054 bfd_get_filename (abfd));
6a506a2d 7055 }
c0ab21c2 7056 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7057 if (dwo_unit != NULL)
3019eac3 7058 {
6a506a2d 7059 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7060 comp_unit_die, NULL,
c0ab21c2 7061 this, &info_ptr,
3e225074 7062 &dwo_comp_unit_die,
c0ab21c2 7063 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7064 {
7065 /* Dummy die. */
c0ab21c2 7066 dummy_p = true;
6a506a2d
DE
7067 return;
7068 }
7069 comp_unit_die = dwo_comp_unit_die;
7070 }
7071 else
7072 {
7073 /* Yikes, we couldn't find the rest of the DIE, we only have
7074 the stub. A complaint has already been logged. There's
7075 not much more we can do except pass on the stub DIE to
7076 die_reader_func. We don't want to throw an error on bad
7077 debug info. */
3019eac3
DE
7078 }
7079 }
c0ab21c2 7080}
3019eac3 7081
6751ebae
TT
7082void
7083cutu_reader::keep ()
c0ab21c2 7084{
b0c7bfa9 7085 /* Done, clean up. */
6751ebae
TT
7086 gdb_assert (!dummy_p);
7087 if (m_new_cu != NULL)
348e048f 7088 {
c0ab21c2
TT
7089 struct dwarf2_per_objfile *dwarf2_per_objfile
7090 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7091 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7092 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7093 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7094 /* The chain owns it now. */
c0ab21c2 7095 m_new_cu.release ();
348e048f 7096 }
dee91e82
DE
7097}
7098
18a8505e
AT
7099/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7100 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7101 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7102
7103 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7104 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7105
7106 We fill in THIS_CU->length.
7107
dee91e82 7108 THIS_CU->cu is always freed when done.
3019eac3 7109 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7110 to care whether it refers to the "main" CU or the DWO CU.
7111
7112 When parent_cu is passed, it is used to provide a default value for
7113 str_offsets_base and addr_base from the parent. */
dee91e82 7114
c0ab21c2
TT
7115cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7116 struct dwarf2_cu *parent_cu,
7117 struct dwo_file *dwo_file)
7118 : die_reader_specs {},
7119 m_this_cu (this_cu)
dee91e82 7120{
ed2dc618 7121 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7122 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7123 struct dwarf2_section_info *section = this_cu->section;
96b79293 7124 bfd *abfd = section->get_bfd_owner ();
33e80786 7125 struct dwarf2_section_info *abbrev_section;
d521ce57 7126 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7127
b4f54984 7128 if (dwarf_die_debug)
9d8780f0 7129 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7130 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7131 sect_offset_str (this_cu->sect_off));
09406207 7132
dee91e82
DE
7133 gdb_assert (this_cu->cu == NULL);
7134
33e80786
DE
7135 abbrev_section = (dwo_file != NULL
7136 ? &dwo_file->sections.abbrev
7137 : get_abbrev_section_for_cu (this_cu));
7138
dee91e82 7139 /* This is cheap if the section is already read in. */
96b79293 7140 section->read (objfile);
dee91e82 7141
c0ab21c2 7142 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7143
9c541725 7144 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7145 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7146 &m_new_cu->header, section,
4bdcc0c1 7147 abbrev_section, info_ptr,
43988095
JK
7148 (this_cu->is_debug_types
7149 ? rcuh_kind::TYPE
7150 : rcuh_kind::COMPILE));
dee91e82 7151
18a8505e
AT
7152 if (parent_cu != nullptr)
7153 {
c0ab21c2
TT
7154 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7155 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7156 }
4057dfde 7157 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7158
7159 /* Skip dummy compilation units. */
7160 if (info_ptr >= begin_info_ptr + this_cu->length
7161 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7162 {
7163 dummy_p = true;
7164 return;
7165 }
72bf9492 7166
c0ab21c2 7167 m_abbrev_table_holder
86de1d91
TT
7168 = abbrev_table::read (objfile, abbrev_section,
7169 m_new_cu->header.abbrev_sect_off);
dee91e82 7170
c0ab21c2
TT
7171 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7172 m_abbrev_table_holder.get ());
3e225074 7173 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7174}
7175
0018ea6f
DE
7176\f
7177/* Type Unit Groups.
dee91e82 7178
0018ea6f
DE
7179 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7180 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7181 so that all types coming from the same compilation (.o file) are grouped
7182 together. A future step could be to put the types in the same symtab as
7183 the CU the types ultimately came from. */
ff013f42 7184
f4dc4d17
DE
7185static hashval_t
7186hash_type_unit_group (const void *item)
7187{
9a3c8263
SM
7188 const struct type_unit_group *tu_group
7189 = (const struct type_unit_group *) item;
f4dc4d17 7190
094b34ac 7191 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7192}
348e048f
DE
7193
7194static int
f4dc4d17 7195eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7196{
9a3c8263
SM
7197 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7198 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7199
094b34ac 7200 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7201}
348e048f 7202
f4dc4d17
DE
7203/* Allocate a hash table for type unit groups. */
7204
eaa5fa8b 7205static htab_up
298e9637 7206allocate_type_unit_groups_table ()
f4dc4d17 7207{
eaa5fa8b
TT
7208 return htab_up (htab_create_alloc (3,
7209 hash_type_unit_group,
7210 eq_type_unit_group,
7211 NULL, xcalloc, xfree));
f4dc4d17 7212}
dee91e82 7213
f4dc4d17
DE
7214/* Type units that don't have DW_AT_stmt_list are grouped into their own
7215 partial symtabs. We combine several TUs per psymtab to not let the size
7216 of any one psymtab grow too big. */
7217#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7218#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7219
094b34ac 7220/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7221 Create the type_unit_group object used to hold one or more TUs. */
7222
7223static struct type_unit_group *
094b34ac 7224create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7225{
518817b3
SM
7226 struct dwarf2_per_objfile *dwarf2_per_objfile
7227 = cu->per_cu->dwarf2_per_objfile;
094b34ac 7228 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7229 struct type_unit_group *tu_group;
f4dc4d17 7230
45940949 7231 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
f4dc4d17 7232 struct type_unit_group);
094b34ac 7233 per_cu = &tu_group->per_cu;
518817b3 7234 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7235
094b34ac
DE
7236 if (dwarf2_per_objfile->using_index)
7237 {
45940949 7238 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
094b34ac 7239 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7240 }
7241 else
7242 {
9c541725 7243 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7244 dwarf2_psymtab *pst;
528e1572 7245 std::string name;
094b34ac
DE
7246
7247 /* Give the symtab a useful name for debug purposes. */
7248 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7249 name = string_printf ("<type_units_%d>",
7250 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7251 else
528e1572 7252 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7253
528e1572 7254 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7255 pst->anonymous = true;
094b34ac 7256 }
f4dc4d17 7257
094b34ac 7258 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7259 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7260
7261 return tu_group;
7262}
7263
094b34ac
DE
7264/* Look up the type_unit_group for type unit CU, and create it if necessary.
7265 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7266
7267static struct type_unit_group *
ff39bb5e 7268get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7269{
518817b3
SM
7270 struct dwarf2_per_objfile *dwarf2_per_objfile
7271 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7272 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7273 struct type_unit_group *tu_group;
7274 void **slot;
7275 unsigned int line_offset;
7276 struct type_unit_group type_unit_group_for_lookup;
7277
7278 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7279 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7280
7281 /* Do we need to create a new group, or can we use an existing one? */
7282
7283 if (stmt_list)
7284 {
7285 line_offset = DW_UNSND (stmt_list);
7286 ++tu_stats->nr_symtab_sharers;
7287 }
7288 else
7289 {
7290 /* Ugh, no stmt_list. Rare, but we have to handle it.
7291 We can do various things here like create one group per TU or
7292 spread them over multiple groups to split up the expansion work.
7293 To avoid worst case scenarios (too many groups or too large groups)
7294 we, umm, group them in bunches. */
7295 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7296 | (tu_stats->nr_stmt_less_type_units
7297 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7298 ++tu_stats->nr_stmt_less_type_units;
7299 }
7300
094b34ac 7301 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7302 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7303 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7304 &type_unit_group_for_lookup, INSERT);
7305 if (*slot != NULL)
7306 {
9a3c8263 7307 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7308 gdb_assert (tu_group != NULL);
7309 }
7310 else
7311 {
9c541725 7312 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7313 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7314 *slot = tu_group;
7315 ++tu_stats->nr_symtabs;
7316 }
7317
7318 return tu_group;
7319}
0018ea6f
DE
7320\f
7321/* Partial symbol tables. */
7322
7323/* Create a psymtab named NAME and assign it to PER_CU.
7324
7325 The caller must fill in the following details:
7326 dirname, textlow, texthigh. */
7327
891813be 7328static dwarf2_psymtab *
0018ea6f
DE
7329create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7330{
e3b94546 7331 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7332 dwarf2_psymtab *pst;
0018ea6f 7333
9f4e76a4 7334 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7335
6d94535f 7336 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7337
7338 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7339 per_cu->v.psymtab = pst;
7340
7341 return pst;
7342}
7343
c0ab21c2 7344/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7345
7346static void
7347process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7348 const gdb_byte *info_ptr,
0018ea6f 7349 struct die_info *comp_unit_die,
c0ab21c2 7350 enum language pretend_language)
0018ea6f
DE
7351{
7352 struct dwarf2_cu *cu = reader->cu;
518817b3 7353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7354 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7355 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7356 CORE_ADDR baseaddr;
7357 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7358 dwarf2_psymtab *pst;
3a2b436a 7359 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7360 const char *filename;
0018ea6f 7361
0018ea6f
DE
7362 gdb_assert (! per_cu->is_debug_types);
7363
c0ab21c2 7364 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7365
0018ea6f 7366 /* Allocate a new partial symbol table structure. */
2e927613
TV
7367 gdb::unique_xmalloc_ptr<char> debug_filename;
7368 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7369 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7370 if (filename == NULL)
0018ea6f 7371 filename = "";
2e927613
TV
7372 else if (strcmp (filename, artificial) == 0)
7373 {
7374 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7375 sect_offset_str (per_cu->sect_off),
7376 (char *) NULL));
2e927613
TV
7377 filename = debug_filename.get ();
7378 }
0018ea6f
DE
7379
7380 pst = create_partial_symtab (per_cu, filename);
7381
7382 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7383 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7384
b3b3bada 7385 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7386
7387 dwarf2_find_base_address (comp_unit_die, cu);
7388
7389 /* Possibly set the default values of LOWPC and HIGHPC from
7390 `DW_AT_ranges'. */
3a2b436a
JK
7391 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7392 &best_highpc, cu, pst);
7393 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7394 {
7395 CORE_ADDR low
7396 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7397 - baseaddr);
7398 CORE_ADDR high
7399 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7400 - baseaddr - 1);
7401 /* Store the contiguous range if it is not empty; it can be
7402 empty for CUs with no code. */
d320c2b5
TT
7403 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7404 low, high, pst);
79748972 7405 }
0018ea6f
DE
7406
7407 /* Check if comp unit has_children.
7408 If so, read the rest of the partial symbols from this comp unit.
7409 If not, there's no more debug_info for this comp unit. */
3e225074 7410 if (comp_unit_die->has_children)
0018ea6f
DE
7411 {
7412 struct partial_die_info *first_die;
7413 CORE_ADDR lowpc, highpc;
7414
7415 lowpc = ((CORE_ADDR) -1);
7416 highpc = ((CORE_ADDR) 0);
7417
7418 first_die = load_partial_dies (reader, info_ptr, 1);
7419
7420 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7421 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7422
7423 /* If we didn't find a lowpc, set it to highpc to avoid
7424 complaints from `maint check'. */
7425 if (lowpc == ((CORE_ADDR) -1))
7426 lowpc = highpc;
7427
7428 /* If the compilation unit didn't have an explicit address range,
7429 then use the information extracted from its child dies. */
e385593e 7430 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7431 {
7432 best_lowpc = lowpc;
7433 best_highpc = highpc;
7434 }
7435 }
4ae976d1 7436 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7437 best_lowpc + baseaddr)
7438 - baseaddr);
4ae976d1 7439 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7440 best_highpc + baseaddr)
7441 - baseaddr);
0018ea6f 7442
8763cede 7443 end_psymtab_common (objfile, pst);
0018ea6f 7444
ae640021 7445 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7446 {
7447 int i;
ae640021 7448 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7449
7450 /* Fill in 'dependencies' here; we fill in 'users' in a
7451 post-pass. */
7452 pst->number_of_dependencies = len;
a9342b62
TT
7453 pst->dependencies
7454 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7455 for (i = 0; i < len; ++i)
7456 {
7457 pst->dependencies[i]
7458 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7459 }
0018ea6f 7460
ae640021 7461 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7462 }
7463
7464 /* Get the list of files included in the current compilation unit,
7465 and build a psymtab for each of them. */
7466 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7467
b4f54984 7468 if (dwarf_read_debug)
b926417a
TT
7469 fprintf_unfiltered (gdb_stdlog,
7470 "Psymtab for %s unit @%s: %s - %s"
7471 ", %d global, %d static syms\n",
7472 per_cu->is_debug_types ? "type" : "comp",
7473 sect_offset_str (per_cu->sect_off),
7474 paddress (gdbarch, pst->text_low (objfile)),
7475 paddress (gdbarch, pst->text_high (objfile)),
7476 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7477}
7478
7479/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7480 Process compilation unit THIS_CU for a psymtab. */
7481
7482static void
7483process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7484 bool want_partial_unit,
b93601f3 7485 enum language pretend_language)
0018ea6f
DE
7486{
7487 /* If this compilation unit was already read in, free the
7488 cached copy in order to read it in again. This is
7489 necessary because we skipped some symbols when we first
7490 read in the compilation unit (see load_partial_dies).
7491 This problem could be avoided, but the benefit is unclear. */
7492 if (this_cu->cu != NULL)
7493 free_one_cached_comp_unit (this_cu);
7494
6751ebae 7495 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7496
58990295
TV
7497 switch (reader.comp_unit_die->tag)
7498 {
7499 case DW_TAG_compile_unit:
7500 this_cu->unit_type = DW_UT_compile;
7501 break;
7502 case DW_TAG_partial_unit:
7503 this_cu->unit_type = DW_UT_partial;
7504 break;
7505 default:
7506 abort ();
7507 }
7508
c0ab21c2 7509 if (reader.dummy_p)
f1902523 7510 {
c0ab21c2 7511 /* Nothing. */
f1902523 7512 }
c0ab21c2 7513 else if (this_cu->is_debug_types)
3e225074
TT
7514 build_type_psymtabs_reader (&reader, reader.info_ptr,
7515 reader.comp_unit_die);
135f5437
TT
7516 else if (want_partial_unit
7517 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7518 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7519 reader.comp_unit_die,
c0ab21c2 7520 pretend_language);
0018ea6f 7521
58990295
TV
7522 this_cu->lang = this_cu->cu->language;
7523
0018ea6f 7524 /* Age out any secondary CUs. */
ed2dc618 7525 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7526}
f4dc4d17
DE
7527
7528/* Reader function for build_type_psymtabs. */
7529
7530static void
7531build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7532 const gdb_byte *info_ptr,
3e225074 7533 struct die_info *type_unit_die)
f4dc4d17 7534{
ed2dc618 7535 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7536 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7537 struct objfile *objfile = dwarf2_per_objfile->objfile;
7538 struct dwarf2_cu *cu = reader->cu;
7539 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7540 struct signatured_type *sig_type;
f4dc4d17
DE
7541 struct type_unit_group *tu_group;
7542 struct attribute *attr;
7543 struct partial_die_info *first_die;
7544 CORE_ADDR lowpc, highpc;
891813be 7545 dwarf2_psymtab *pst;
f4dc4d17 7546
0186c6a7
DE
7547 gdb_assert (per_cu->is_debug_types);
7548 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7549
3e225074 7550 if (! type_unit_die->has_children)
f4dc4d17
DE
7551 return;
7552
052c8bb8 7553 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7554 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7555
df07e2c7 7556 if (tu_group->tus == nullptr)
a8b3b8e9 7557 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7558 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7559
7560 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7561 pst = create_partial_symtab (per_cu, "");
6d94535f 7562 pst->anonymous = true;
f4dc4d17
DE
7563
7564 first_die = load_partial_dies (reader, info_ptr, 1);
7565
7566 lowpc = (CORE_ADDR) -1;
7567 highpc = (CORE_ADDR) 0;
7568 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7569
8763cede 7570 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7571}
7572
73051182
DE
7573/* Struct used to sort TUs by their abbreviation table offset. */
7574
7575struct tu_abbrev_offset
7576{
b2bdb8cf
SM
7577 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7578 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7579 {}
7580
7581 signatured_type *sig_type;
73051182
DE
7582 sect_offset abbrev_offset;
7583};
7584
484cf504 7585/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7586
484cf504
TT
7587static bool
7588sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7589 const struct tu_abbrev_offset &b)
73051182 7590{
484cf504 7591 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7592}
7593
7594/* Efficiently read all the type units.
7595 This does the bulk of the work for build_type_psymtabs.
7596
7597 The efficiency is because we sort TUs by the abbrev table they use and
7598 only read each abbrev table once. In one program there are 200K TUs
7599 sharing 8K abbrev tables.
7600
7601 The main purpose of this function is to support building the
7602 dwarf2_per_objfile->type_unit_groups table.
7603 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7604 can collapse the search space by grouping them by stmt_list.
7605 The savings can be significant, in the same program from above the 200K TUs
7606 share 8K stmt_list tables.
7607
7608 FUNC is expected to call get_type_unit_group, which will create the
7609 struct type_unit_group if necessary and add it to
7610 dwarf2_per_objfile->type_unit_groups. */
7611
7612static void
ed2dc618 7613build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7614{
73051182 7615 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7616 abbrev_table_up abbrev_table;
73051182 7617 sect_offset abbrev_offset;
73051182
DE
7618
7619 /* It's up to the caller to not call us multiple times. */
7620 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7621
b2bdb8cf 7622 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7623 return;
7624
7625 /* TUs typically share abbrev tables, and there can be way more TUs than
7626 abbrev tables. Sort by abbrev table to reduce the number of times we
7627 read each abbrev table in.
7628 Alternatives are to punt or to maintain a cache of abbrev tables.
7629 This is simpler and efficient enough for now.
7630
7631 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7632 symtab to use). Typically TUs with the same abbrev offset have the same
7633 stmt_list value too so in practice this should work well.
7634
7635 The basic algorithm here is:
7636
7637 sort TUs by abbrev table
7638 for each TU with same abbrev table:
7639 read abbrev table if first user
7640 read TU top level DIE
7641 [IWBN if DWO skeletons had DW_AT_stmt_list]
7642 call FUNC */
7643
b4f54984 7644 if (dwarf_read_debug)
73051182
DE
7645 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7646
7647 /* Sort in a separate table to maintain the order of all_type_units
7648 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7649 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7650 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7651
7652 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7653 sorted_by_abbrev.emplace_back
7654 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7655 sig_type->per_cu.section,
7656 sig_type->per_cu.sect_off));
73051182 7657
484cf504
TT
7658 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7659 sort_tu_by_abbrev_offset);
73051182 7660
9c541725 7661 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7662
b2bdb8cf 7663 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7664 {
73051182
DE
7665 /* Switch to the next abbrev table if necessary. */
7666 if (abbrev_table == NULL
b2bdb8cf 7667 || tu.abbrev_offset != abbrev_offset)
73051182 7668 {
b2bdb8cf 7669 abbrev_offset = tu.abbrev_offset;
73051182 7670 abbrev_table =
86de1d91
TT
7671 abbrev_table::read (dwarf2_per_objfile->objfile,
7672 &dwarf2_per_objfile->abbrev,
7673 abbrev_offset);
73051182
DE
7674 ++tu_stats->nr_uniq_abbrev_tables;
7675 }
7676
c0ab21c2 7677 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7678 0, false);
c0ab21c2
TT
7679 if (!reader.dummy_p)
7680 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7681 reader.comp_unit_die);
73051182 7682 }
6aa5f3a6 7683}
73051182 7684
6aa5f3a6
DE
7685/* Print collected type unit statistics. */
7686
7687static void
ed2dc618 7688print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7689{
7690 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7691
7692 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7693 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7694 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7695 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7696 tu_stats->nr_uniq_abbrev_tables);
7697 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7698 tu_stats->nr_symtabs);
7699 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7700 tu_stats->nr_symtab_sharers);
7701 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7702 tu_stats->nr_stmt_less_type_units);
7703 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7704 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7705}
7706
f4dc4d17
DE
7707/* Traversal function for build_type_psymtabs. */
7708
7709static int
7710build_type_psymtab_dependencies (void **slot, void *info)
7711{
ed2dc618
SM
7712 struct dwarf2_per_objfile *dwarf2_per_objfile
7713 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7714 struct objfile *objfile = dwarf2_per_objfile->objfile;
7715 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7716 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7717 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7718 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7719 int i;
7720
7721 gdb_assert (len > 0);
197400e8 7722 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7723
7724 pst->number_of_dependencies = len;
a9342b62 7725 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7726 for (i = 0; i < len; ++i)
f4dc4d17 7727 {
df07e2c7 7728 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7729 gdb_assert (iter->per_cu.is_debug_types);
7730 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7731 iter->type_unit_group = tu_group;
f4dc4d17
DE
7732 }
7733
df07e2c7
AB
7734 delete tu_group->tus;
7735 tu_group->tus = nullptr;
348e048f
DE
7736
7737 return 1;
7738}
7739
7740/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7741 Build partial symbol tables for the .debug_types comp-units. */
7742
7743static void
ed2dc618 7744build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7745{
ed2dc618 7746 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7747 return;
7748
ed2dc618 7749 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7750}
f4dc4d17 7751
6aa5f3a6
DE
7752/* Traversal function for process_skeletonless_type_unit.
7753 Read a TU in a DWO file and build partial symbols for it. */
7754
7755static int
7756process_skeletonless_type_unit (void **slot, void *info)
7757{
7758 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7759 struct dwarf2_per_objfile *dwarf2_per_objfile
7760 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7761 struct signatured_type find_entry, *entry;
7762
7763 /* If this TU doesn't exist in the global table, add it and read it in. */
7764
7765 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7766 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7767
7768 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7769 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7770 &find_entry, INSERT);
6aa5f3a6
DE
7771 /* If we've already seen this type there's nothing to do. What's happening
7772 is we're doing our own version of comdat-folding here. */
7773 if (*slot != NULL)
7774 return 1;
7775
7776 /* This does the job that create_all_type_units would have done for
7777 this TU. */
ed2dc618
SM
7778 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7779 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7780 *slot = entry;
7781
7782 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7783 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7784 if (!reader.dummy_p)
7785 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7786 reader.comp_unit_die);
6aa5f3a6
DE
7787
7788 return 1;
7789}
7790
7791/* Traversal function for process_skeletonless_type_units. */
7792
7793static int
7794process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7795{
7796 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7797
7798 if (dwo_file->tus != NULL)
b0b6a987
TT
7799 htab_traverse_noresize (dwo_file->tus.get (),
7800 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7801
7802 return 1;
7803}
7804
7805/* Scan all TUs of DWO files, verifying we've processed them.
7806 This is needed in case a TU was emitted without its skeleton.
7807 Note: This can't be done until we know what all the DWO files are. */
7808
7809static void
ed2dc618 7810process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7811{
7812 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7813 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7814 && dwarf2_per_objfile->dwo_files != NULL)
7815 {
51ac9db5 7816 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7817 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7818 dwarf2_per_objfile);
6aa5f3a6 7819 }
348e048f
DE
7820}
7821
ed2dc618 7822/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7823
7824static void
ed2dc618 7825set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7826{
b76e467d 7827 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7828 {
891813be 7829 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7830
36586728
TT
7831 if (pst == NULL)
7832 continue;
7833
b76e467d 7834 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7835 {
7836 /* Set the 'user' field only if it is not already set. */
7837 if (pst->dependencies[j]->user == NULL)
7838 pst->dependencies[j]->user = pst;
7839 }
7840 }
7841}
7842
93311388
DE
7843/* Build the partial symbol table by doing a quick pass through the
7844 .debug_info and .debug_abbrev sections. */
72bf9492 7845
93311388 7846static void
ed2dc618 7847dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7848{
ed2dc618 7849 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7850
b4f54984 7851 if (dwarf_read_debug)
45cfd468
DE
7852 {
7853 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7854 objfile_name (objfile));
45cfd468
DE
7855 }
7856
76935768
TT
7857 scoped_restore restore_reading_psyms
7858 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7859 true);
98bfdba5 7860
96b79293 7861 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7862
93311388
DE
7863 /* Any cached compilation units will be linked by the per-objfile
7864 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7865 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7866
ed2dc618 7867 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7868
ed2dc618 7869 create_all_comp_units (dwarf2_per_objfile);
c906108c 7870
60606b2c
TT
7871 /* Create a temporary address map on a temporary obstack. We later
7872 copy this to the final obstack. */
8268c778 7873 auto_obstack temp_obstack;
791afaa2
TT
7874
7875 scoped_restore save_psymtabs_addrmap
d320c2b5 7876 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7877 addrmap_create_mutable (&temp_obstack));
72bf9492 7878
b76e467d 7879 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3d5afab3
TV
7880 {
7881 if (per_cu->v.psymtab != NULL)
7882 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7883 continue;
7884 process_psymtab_comp_unit (per_cu, false, language_minimal);
7885 }
ff013f42 7886
6aa5f3a6 7887 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7888 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7889
7890 /* Now that all TUs have been processed we can fill in the dependencies. */
7891 if (dwarf2_per_objfile->type_unit_groups != NULL)
7892 {
eaa5fa8b 7893 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7894 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7895 }
7896
b4f54984 7897 if (dwarf_read_debug)
ed2dc618 7898 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7899
ed2dc618 7900 set_partial_user (dwarf2_per_objfile);
95554aad 7901
d320c2b5
TT
7902 objfile->partial_symtabs->psymtabs_addrmap
7903 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7904 objfile->partial_symtabs->obstack ());
791afaa2
TT
7905 /* At this point we want to keep the address map. */
7906 save_psymtabs_addrmap.release ();
ff013f42 7907
b4f54984 7908 if (dwarf_read_debug)
45cfd468 7909 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7910 objfile_name (objfile));
ae038cb0
DJ
7911}
7912
dee91e82
DE
7913/* Load the partial DIEs for a secondary CU into memory.
7914 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7915
dee91e82
DE
7916static void
7917load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7918{
6751ebae 7919 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7920
7921 if (!reader.dummy_p)
7922 {
7923 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7924 language_minimal);
7925
7926 /* Check if comp unit has_children.
7927 If so, read the rest of the partial symbols from this comp unit.
7928 If not, there's no more debug_info for this comp unit. */
3e225074 7929 if (reader.comp_unit_die->has_children)
c0ab21c2 7930 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7931
7932 reader.keep ();
c0ab21c2 7933 }
ae038cb0
DJ
7934}
7935
ae038cb0 7936static void
ed2dc618 7937read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7938 struct dwarf2_section_info *section,
f1902523 7939 struct dwarf2_section_info *abbrev_section,
b76e467d 7940 unsigned int is_dwz)
ae038cb0 7941{
d521ce57 7942 const gdb_byte *info_ptr;
ed2dc618 7943 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7944
b4f54984 7945 if (dwarf_read_debug)
bf6af496 7946 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7947 section->get_name (),
7948 section->get_file_name ());
bf6af496 7949
96b79293 7950 section->read (objfile);
ae038cb0 7951
36586728 7952 info_ptr = section->buffer;
6e70227d 7953
36586728 7954 while (info_ptr < section->buffer + section->size)
ae038cb0 7955 {
ae038cb0 7956 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7957
9c541725 7958 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7959
f1902523 7960 comp_unit_head cu_header;
ed2dc618
SM
7961 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7962 abbrev_section, info_ptr,
7963 rcuh_kind::COMPILE);
ae038cb0
DJ
7964
7965 /* Save the compilation unit for later lookup. */
f1902523
JK
7966 if (cu_header.unit_type != DW_UT_type)
7967 {
45940949 7968 this_cu = XOBNEW (&dwarf2_per_objfile->obstack,
f1902523
JK
7969 struct dwarf2_per_cu_data);
7970 memset (this_cu, 0, sizeof (*this_cu));
7971 }
7972 else
7973 {
45940949 7974 auto sig_type = XOBNEW (&dwarf2_per_objfile->obstack,
f1902523
JK
7975 struct signatured_type);
7976 memset (sig_type, 0, sizeof (*sig_type));
7977 sig_type->signature = cu_header.signature;
7978 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7979 this_cu = &sig_type->per_cu;
7980 }
7981 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7982 this_cu->sect_off = sect_off;
f1902523 7983 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7984 this_cu->is_dwz = is_dwz;
e3b94546 7985 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7986 this_cu->section = section;
ae038cb0 7987
b76e467d 7988 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7989
7990 info_ptr = info_ptr + this_cu->length;
7991 }
36586728
TT
7992}
7993
7994/* Create a list of all compilation units in OBJFILE.
7995 This is only done for -readnow and building partial symtabs. */
7996
7997static void
ed2dc618 7998create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7999{
b76e467d 8000 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8001 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8002 &dwarf2_per_objfile->abbrev, 0);
36586728 8003
b76e467d 8004 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8005 if (dwz != NULL)
ed2dc618 8006 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8007 1);
c906108c
SS
8008}
8009
5734ee8b 8010/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8011 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8012 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8013 DW_AT_ranges). See the comments of add_partial_subprogram on how
8014 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8015
72bf9492
DJ
8016static void
8017scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8018 CORE_ADDR *highpc, int set_addrmap,
8019 struct dwarf2_cu *cu)
c906108c 8020{
72bf9492 8021 struct partial_die_info *pdi;
c906108c 8022
91c24f0a
DC
8023 /* Now, march along the PDI's, descending into ones which have
8024 interesting children but skipping the children of the other ones,
8025 until we reach the end of the compilation unit. */
c906108c 8026
72bf9492 8027 pdi = first_die;
91c24f0a 8028
72bf9492
DJ
8029 while (pdi != NULL)
8030 {
52356b79 8031 pdi->fixup (cu);
c906108c 8032
f55ee35c 8033 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8034 children, so we need to look at them. Ditto for anonymous
8035 enums. */
933c6fe4 8036
72bf9492 8037 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8038 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8039 || pdi->tag == DW_TAG_imported_unit
8040 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8041 {
72bf9492 8042 switch (pdi->tag)
c906108c
SS
8043 {
8044 case DW_TAG_subprogram:
b1dc1806 8045 case DW_TAG_inlined_subroutine:
cdc07690 8046 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8047 break;
72929c62 8048 case DW_TAG_constant:
c906108c
SS
8049 case DW_TAG_variable:
8050 case DW_TAG_typedef:
91c24f0a 8051 case DW_TAG_union_type:
317d2668
TV
8052 if (!pdi->is_declaration
8053 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8054 {
72bf9492 8055 add_partial_symbol (pdi, cu);
63d06c5c
DC
8056 }
8057 break;
c906108c 8058 case DW_TAG_class_type:
680b30c7 8059 case DW_TAG_interface_type:
c906108c 8060 case DW_TAG_structure_type:
72bf9492 8061 if (!pdi->is_declaration)
c906108c 8062 {
72bf9492 8063 add_partial_symbol (pdi, cu);
c906108c 8064 }
b7fee5a3
KS
8065 if ((cu->language == language_rust
8066 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8067 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8068 set_addrmap, cu);
c906108c 8069 break;
91c24f0a 8070 case DW_TAG_enumeration_type:
72bf9492
DJ
8071 if (!pdi->is_declaration)
8072 add_partial_enumeration (pdi, cu);
c906108c
SS
8073 break;
8074 case DW_TAG_base_type:
a02abb62 8075 case DW_TAG_subrange_type:
c906108c 8076 /* File scope base type definitions are added to the partial
c5aa993b 8077 symbol table. */
72bf9492 8078 add_partial_symbol (pdi, cu);
c906108c 8079 break;
d9fa45fe 8080 case DW_TAG_namespace:
cdc07690 8081 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8082 break;
5d7cb8df 8083 case DW_TAG_module:
59c35742
AB
8084 if (!pdi->is_declaration)
8085 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8086 break;
95554aad
TT
8087 case DW_TAG_imported_unit:
8088 {
8089 struct dwarf2_per_cu_data *per_cu;
8090
f4dc4d17
DE
8091 /* For now we don't handle imported units in type units. */
8092 if (cu->per_cu->is_debug_types)
8093 {
8094 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8095 " supported in type units [in module %s]"),
518817b3 8096 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8097 }
8098
e3b94546
SM
8099 per_cu = dwarf2_find_containing_comp_unit
8100 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8101 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8102
8103 /* Go read the partial unit, if needed. */
8104 if (per_cu->v.psymtab == NULL)
135f5437 8105 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8106
ae640021 8107 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8108 }
8109 break;
74921315
KS
8110 case DW_TAG_imported_declaration:
8111 add_partial_symbol (pdi, cu);
8112 break;
c906108c
SS
8113 default:
8114 break;
8115 }
8116 }
8117
72bf9492
DJ
8118 /* If the die has a sibling, skip to the sibling. */
8119
8120 pdi = pdi->die_sibling;
8121 }
8122}
8123
8124/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8125
72bf9492 8126 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8127 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8128 Enumerators are an exception; they use the scope of their parent
8129 enumeration type, i.e. the name of the enumeration type is not
8130 prepended to the enumerator.
91c24f0a 8131
72bf9492
DJ
8132 There are two complexities. One is DW_AT_specification; in this
8133 case "parent" means the parent of the target of the specification,
8134 instead of the direct parent of the DIE. The other is compilers
8135 which do not emit DW_TAG_namespace; in this case we try to guess
8136 the fully qualified name of structure types from their members'
8137 linkage names. This must be done using the DIE's children rather
8138 than the children of any DW_AT_specification target. We only need
8139 to do this for structures at the top level, i.e. if the target of
8140 any DW_AT_specification (if any; otherwise the DIE itself) does not
8141 have a parent. */
8142
8143/* Compute the scope prefix associated with PDI's parent, in
8144 compilation unit CU. The result will be allocated on CU's
8145 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8146 field. NULL is returned if no prefix is necessary. */
15d034d0 8147static const char *
72bf9492
DJ
8148partial_die_parent_scope (struct partial_die_info *pdi,
8149 struct dwarf2_cu *cu)
8150{
15d034d0 8151 const char *grandparent_scope;
72bf9492 8152 struct partial_die_info *parent, *real_pdi;
91c24f0a 8153
72bf9492
DJ
8154 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8155 then this means the parent of the specification DIE. */
8156
8157 real_pdi = pdi;
72bf9492 8158 while (real_pdi->has_specification)
fb816e8b 8159 {
122cf0f2
AB
8160 auto res = find_partial_die (real_pdi->spec_offset,
8161 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8162 real_pdi = res.pdi;
8163 cu = res.cu;
8164 }
72bf9492
DJ
8165
8166 parent = real_pdi->die_parent;
8167 if (parent == NULL)
8168 return NULL;
8169
8170 if (parent->scope_set)
8171 return parent->scope;
8172
52356b79 8173 parent->fixup (cu);
72bf9492 8174
10b3939b 8175 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8176
acebe513
UW
8177 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8178 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8179 Work around this problem here. */
8180 if (cu->language == language_cplus
6e70227d 8181 && parent->tag == DW_TAG_namespace
acebe513
UW
8182 && strcmp (parent->name, "::") == 0
8183 && grandparent_scope == NULL)
8184 {
8185 parent->scope = NULL;
8186 parent->scope_set = 1;
8187 return NULL;
8188 }
8189
0a4b0913 8190 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8191 if (pdi->tag == DW_TAG_enumerator)
8192 /* Enumerators should not get the name of the enumeration as a prefix. */
8193 parent->scope = grandparent_scope;
8194 else if (parent->tag == DW_TAG_namespace
f55ee35c 8195 || parent->tag == DW_TAG_module
72bf9492
DJ
8196 || parent->tag == DW_TAG_structure_type
8197 || parent->tag == DW_TAG_class_type
680b30c7 8198 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8199 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8200 || parent->tag == DW_TAG_enumeration_type
8201 || (cu->language == language_fortran
8202 && parent->tag == DW_TAG_subprogram
8203 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8204 {
8205 if (grandparent_scope == NULL)
8206 parent->scope = parent->name;
8207 else
3e43a32a
MS
8208 parent->scope = typename_concat (&cu->comp_unit_obstack,
8209 grandparent_scope,
f55ee35c 8210 parent->name, 0, cu);
72bf9492 8211 }
72bf9492
DJ
8212 else
8213 {
8214 /* FIXME drow/2004-04-01: What should we be doing with
8215 function-local names? For partial symbols, we should probably be
8216 ignoring them. */
fa9c3fa0
TT
8217 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8218 dwarf_tag_name (parent->tag),
8219 sect_offset_str (pdi->sect_off));
72bf9492 8220 parent->scope = grandparent_scope;
c906108c
SS
8221 }
8222
72bf9492
DJ
8223 parent->scope_set = 1;
8224 return parent->scope;
8225}
8226
8227/* Return the fully scoped name associated with PDI, from compilation unit
8228 CU. The result will be allocated with malloc. */
4568ecf9 8229
43816ebc 8230static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8231partial_die_full_name (struct partial_die_info *pdi,
8232 struct dwarf2_cu *cu)
8233{
15d034d0 8234 const char *parent_scope;
72bf9492 8235
98bfdba5
PA
8236 /* If this is a template instantiation, we can not work out the
8237 template arguments from partial DIEs. So, unfortunately, we have
8238 to go through the full DIEs. At least any work we do building
8239 types here will be reused if full symbols are loaded later. */
8240 if (pdi->has_template_arguments)
8241 {
52356b79 8242 pdi->fixup (cu);
98bfdba5
PA
8243
8244 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8245 {
8246 struct die_info *die;
8247 struct attribute attr;
8248 struct dwarf2_cu *ref_cu = cu;
8249
b64f50a1 8250 /* DW_FORM_ref_addr is using section offset. */
b4069958 8251 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8252 attr.form = DW_FORM_ref_addr;
9c541725 8253 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8254 die = follow_die_ref (NULL, &attr, &ref_cu);
8255
43816ebc 8256 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8257 }
8258 }
8259
72bf9492
DJ
8260 parent_scope = partial_die_parent_scope (pdi, cu);
8261 if (parent_scope == NULL)
8262 return NULL;
8263 else
43816ebc
TT
8264 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8265 pdi->name, 0, cu));
c906108c
SS
8266}
8267
8268static void
72bf9492 8269add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8270{
518817b3
SM
8271 struct dwarf2_per_objfile *dwarf2_per_objfile
8272 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8273 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8274 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8275 CORE_ADDR addr = 0;
15d034d0 8276 const char *actual_name = NULL;
e142c38c
DJ
8277 CORE_ADDR baseaddr;
8278
b3b3bada 8279 baseaddr = objfile->text_section_offset ();
c906108c 8280
43816ebc
TT
8281 gdb::unique_xmalloc_ptr<char> built_actual_name
8282 = partial_die_full_name (pdi, cu);
15d034d0 8283 if (built_actual_name != NULL)
43816ebc 8284 actual_name = built_actual_name.get ();
63d06c5c 8285
72bf9492
DJ
8286 if (actual_name == NULL)
8287 actual_name = pdi->name;
8288
76e288d1
TT
8289 partial_symbol psymbol;
8290 memset (&psymbol, 0, sizeof (psymbol));
8291 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8292 psymbol.ginfo.section = -1;
8293
8294 /* The code below indicates that the psymbol should be installed by
8295 setting this. */
8296 gdb::optional<psymbol_placement> where;
8297
c906108c
SS
8298 switch (pdi->tag)
8299 {
b1dc1806 8300 case DW_TAG_inlined_subroutine:
c906108c 8301 case DW_TAG_subprogram:
79748972
TT
8302 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8303 - baseaddr);
0a4b0913
AB
8304 if (pdi->is_external
8305 || cu->language == language_ada
8306 || (cu->language == language_fortran
8307 && pdi->die_parent != NULL
8308 && pdi->die_parent->tag == DW_TAG_subprogram))
8309 {
8310 /* Normally, only "external" DIEs are part of the global scope.
8311 But in Ada and Fortran, we want to be able to access nested
8312 procedures globally. So all Ada and Fortran subprograms are
8313 stored in the global scope. */
76e288d1 8314 where = psymbol_placement::GLOBAL;
c906108c
SS
8315 }
8316 else
76e288d1
TT
8317 where = psymbol_placement::STATIC;
8318
8319 psymbol.domain = VAR_DOMAIN;
8320 psymbol.aclass = LOC_BLOCK;
8321 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8322 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8323
8324 if (pdi->main_subprogram && actual_name != NULL)
8325 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8326 break;
72929c62 8327 case DW_TAG_constant:
76e288d1
TT
8328 psymbol.domain = VAR_DOMAIN;
8329 psymbol.aclass = LOC_STATIC;
8330 where = (pdi->is_external
8331 ? psymbol_placement::GLOBAL
8332 : psymbol_placement::STATIC);
72929c62 8333 break;
c906108c 8334 case DW_TAG_variable:
95554aad
TT
8335 if (pdi->d.locdesc)
8336 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8337
95554aad 8338 if (pdi->d.locdesc
caac4577
JG
8339 && addr == 0
8340 && !dwarf2_per_objfile->has_section_at_zero)
8341 {
8342 /* A global or static variable may also have been stripped
8343 out by the linker if unused, in which case its address
8344 will be nullified; do not add such variables into partial
8345 symbol table then. */
8346 }
8347 else if (pdi->is_external)
c906108c
SS
8348 {
8349 /* Global Variable.
8350 Don't enter into the minimal symbol tables as there is
8351 a minimal symbol table entry from the ELF symbols already.
8352 Enter into partial symbol table if it has a location
8353 descriptor or a type.
8354 If the location descriptor is missing, new_symbol will create
8355 a LOC_UNRESOLVED symbol, the address of the variable will then
8356 be determined from the minimal symbol table whenever the variable
8357 is referenced.
8358 The address for the partial symbol table entry is not
8359 used by GDB, but it comes in handy for debugging partial symbol
8360 table building. */
8361
95554aad 8362 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8363 {
8364 psymbol.domain = VAR_DOMAIN;
8365 psymbol.aclass = LOC_STATIC;
8366 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8367 psymbol.ginfo.value.address = addr;
8368 where = psymbol_placement::GLOBAL;
8369 }
c906108c
SS
8370 }
8371 else
8372 {
ff908ebf
AW
8373 int has_loc = pdi->d.locdesc != NULL;
8374
8375 /* Static Variable. Skip symbols whose value we cannot know (those
8376 without location descriptors or constant values). */
8377 if (!has_loc && !pdi->has_const_value)
43816ebc 8378 return;
ff908ebf 8379
76e288d1
TT
8380 psymbol.domain = VAR_DOMAIN;
8381 psymbol.aclass = LOC_STATIC;
8382 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8383 if (has_loc)
8384 psymbol.ginfo.value.address = addr;
8385 where = psymbol_placement::STATIC;
c906108c
SS
8386 }
8387 break;
8388 case DW_TAG_typedef:
8389 case DW_TAG_base_type:
a02abb62 8390 case DW_TAG_subrange_type:
76e288d1
TT
8391 psymbol.domain = VAR_DOMAIN;
8392 psymbol.aclass = LOC_TYPEDEF;
8393 where = psymbol_placement::STATIC;
c906108c 8394 break;
74921315 8395 case DW_TAG_imported_declaration:
72bf9492 8396 case DW_TAG_namespace:
76e288d1
TT
8397 psymbol.domain = VAR_DOMAIN;
8398 psymbol.aclass = LOC_TYPEDEF;
8399 where = psymbol_placement::GLOBAL;
72bf9492 8400 break;
530e8392 8401 case DW_TAG_module:
a5fd13a9
BH
8402 /* With Fortran 77 there might be a "BLOCK DATA" module
8403 available without any name. If so, we skip the module as it
8404 doesn't bring any value. */
8405 if (actual_name != nullptr)
76e288d1
TT
8406 {
8407 psymbol.domain = MODULE_DOMAIN;
8408 psymbol.aclass = LOC_TYPEDEF;
8409 where = psymbol_placement::GLOBAL;
8410 }
530e8392 8411 break;
c906108c 8412 case DW_TAG_class_type:
680b30c7 8413 case DW_TAG_interface_type:
c906108c
SS
8414 case DW_TAG_structure_type:
8415 case DW_TAG_union_type:
8416 case DW_TAG_enumeration_type:
fa4028e9
JB
8417 /* Skip external references. The DWARF standard says in the section
8418 about "Structure, Union, and Class Type Entries": "An incomplete
8419 structure, union or class type is represented by a structure,
8420 union or class entry that does not have a byte size attribute
8421 and that has a DW_AT_declaration attribute." */
8422 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8423 return;
fa4028e9 8424
63d06c5c
DC
8425 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8426 static vs. global. */
76e288d1
TT
8427 psymbol.domain = STRUCT_DOMAIN;
8428 psymbol.aclass = LOC_TYPEDEF;
8429 where = (cu->language == language_cplus
8430 ? psymbol_placement::GLOBAL
8431 : psymbol_placement::STATIC);
c906108c
SS
8432 break;
8433 case DW_TAG_enumerator:
76e288d1
TT
8434 psymbol.domain = VAR_DOMAIN;
8435 psymbol.aclass = LOC_CONST;
8436 where = (cu->language == language_cplus
8437 ? psymbol_placement::GLOBAL
8438 : psymbol_placement::STATIC);
c906108c
SS
8439 break;
8440 default:
8441 break;
8442 }
76e288d1
TT
8443
8444 if (where.has_value ())
8445 {
f049a313
TT
8446 if (built_actual_name != nullptr)
8447 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8448 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8449 psymbol.ginfo.set_linkage_name (actual_name);
8450 else
8451 {
8452 psymbol.ginfo.set_demangled_name (actual_name,
8453 &objfile->objfile_obstack);
8454 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8455 }
76e288d1
TT
8456 add_psymbol_to_list (psymbol, *where, objfile);
8457 }
c906108c
SS
8458}
8459
5c4e30ca
DC
8460/* Read a partial die corresponding to a namespace; also, add a symbol
8461 corresponding to that namespace to the symbol table. NAMESPACE is
8462 the name of the enclosing namespace. */
91c24f0a 8463
72bf9492
DJ
8464static void
8465add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8466 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8467 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8468{
72bf9492 8469 /* Add a symbol for the namespace. */
e7c27a73 8470
72bf9492 8471 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8472
8473 /* Now scan partial symbols in that namespace. */
8474
91c24f0a 8475 if (pdi->has_children)
cdc07690 8476 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8477}
8478
5d7cb8df
JK
8479/* Read a partial die corresponding to a Fortran module. */
8480
8481static void
8482add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8483 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8484{
530e8392
KB
8485 /* Add a symbol for the namespace. */
8486
8487 add_partial_symbol (pdi, cu);
8488
f55ee35c 8489 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8490
8491 if (pdi->has_children)
cdc07690 8492 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8493}
8494
b1dc1806
XR
8495/* Read a partial die corresponding to a subprogram or an inlined
8496 subprogram and create a partial symbol for that subprogram.
8497 When the CU language allows it, this routine also defines a partial
8498 symbol for each nested subprogram that this subprogram contains.
8499 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8500 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8501
cdc07690
YQ
8502 PDI may also be a lexical block, in which case we simply search
8503 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8504 Again, this is only performed when the CU language allows this
8505 type of definitions. */
8506
8507static void
8508add_partial_subprogram (struct partial_die_info *pdi,
8509 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8510 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8511{
b1dc1806 8512 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8513 {
8514 if (pdi->has_pc_info)
8515 {
8516 if (pdi->lowpc < *lowpc)
8517 *lowpc = pdi->lowpc;
8518 if (pdi->highpc > *highpc)
8519 *highpc = pdi->highpc;
cdc07690 8520 if (set_addrmap)
5734ee8b 8521 {
518817b3 8522 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8523 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8524 CORE_ADDR baseaddr;
b926417a
TT
8525 CORE_ADDR this_highpc;
8526 CORE_ADDR this_lowpc;
5734ee8b 8527
b3b3bada 8528 baseaddr = objfile->text_section_offset ();
b926417a
TT
8529 this_lowpc
8530 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8531 pdi->lowpc + baseaddr)
8532 - baseaddr);
8533 this_highpc
8534 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8535 pdi->highpc + baseaddr)
8536 - baseaddr);
d320c2b5 8537 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8538 this_lowpc, this_highpc - 1,
9291a0cd 8539 cu->per_cu->v.psymtab);
5734ee8b 8540 }
481860b3
GB
8541 }
8542
8543 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8544 {
bc30ff58 8545 if (!pdi->is_declaration)
e8d05480
JB
8546 /* Ignore subprogram DIEs that do not have a name, they are
8547 illegal. Do not emit a complaint at this point, we will
8548 do so when we convert this psymtab into a symtab. */
8549 if (pdi->name)
8550 add_partial_symbol (pdi, cu);
bc30ff58
JB
8551 }
8552 }
6e70227d 8553
bc30ff58
JB
8554 if (! pdi->has_children)
8555 return;
8556
0a4b0913 8557 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8558 {
8559 pdi = pdi->die_child;
8560 while (pdi != NULL)
8561 {
52356b79 8562 pdi->fixup (cu);
bc30ff58 8563 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8564 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8565 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8566 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8567 pdi = pdi->die_sibling;
8568 }
8569 }
8570}
8571
91c24f0a
DC
8572/* Read a partial die corresponding to an enumeration type. */
8573
72bf9492
DJ
8574static void
8575add_partial_enumeration (struct partial_die_info *enum_pdi,
8576 struct dwarf2_cu *cu)
91c24f0a 8577{
72bf9492 8578 struct partial_die_info *pdi;
91c24f0a
DC
8579
8580 if (enum_pdi->name != NULL)
72bf9492
DJ
8581 add_partial_symbol (enum_pdi, cu);
8582
8583 pdi = enum_pdi->die_child;
8584 while (pdi)
91c24f0a 8585 {
72bf9492 8586 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8587 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8588 else
72bf9492
DJ
8589 add_partial_symbol (pdi, cu);
8590 pdi = pdi->die_sibling;
91c24f0a 8591 }
91c24f0a
DC
8592}
8593
6caca83c
CC
8594/* Return the initial uleb128 in the die at INFO_PTR. */
8595
8596static unsigned int
d521ce57 8597peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8598{
8599 unsigned int bytes_read;
8600
8601 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8602}
8603
685af9cd
TT
8604/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8605 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8606
4bb7a0a7
DJ
8607 Return the corresponding abbrev, or NULL if the number is zero (indicating
8608 an empty DIE). In either case *BYTES_READ will be set to the length of
8609 the initial number. */
8610
8611static struct abbrev_info *
685af9cd
TT
8612peek_die_abbrev (const die_reader_specs &reader,
8613 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8614{
685af9cd 8615 dwarf2_cu *cu = reader.cu;
518817b3 8616 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8617 unsigned int abbrev_number
8618 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8619
8620 if (abbrev_number == 0)
8621 return NULL;
8622
685af9cd 8623 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8624 if (!abbrev)
8625 {
422b9917 8626 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8627 " at offset %s [in module %s]"),
422b9917 8628 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8629 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8630 }
8631
8632 return abbrev;
8633}
8634
93311388
DE
8635/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8636 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8637 DIE. Any children of the skipped DIEs will also be skipped. */
8638
d521ce57
TT
8639static const gdb_byte *
8640skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8641{
4bb7a0a7
DJ
8642 while (1)
8643 {
685af9cd
TT
8644 unsigned int bytes_read;
8645 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8646
4bb7a0a7
DJ
8647 if (abbrev == NULL)
8648 return info_ptr + bytes_read;
8649 else
dee91e82 8650 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8651 }
8652}
8653
93311388
DE
8654/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8655 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8656 abbrev corresponding to that skipped uleb128 should be passed in
8657 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8658 children. */
8659
d521ce57
TT
8660static const gdb_byte *
8661skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8662 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8663{
8664 unsigned int bytes_read;
8665 struct attribute attr;
dee91e82
DE
8666 bfd *abfd = reader->abfd;
8667 struct dwarf2_cu *cu = reader->cu;
d521ce57 8668 const gdb_byte *buffer = reader->buffer;
f664829e 8669 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8670 unsigned int form, i;
8671
8672 for (i = 0; i < abbrev->num_attrs; i++)
8673 {
8674 /* The only abbrev we care about is DW_AT_sibling. */
8675 if (abbrev->attrs[i].name == DW_AT_sibling)
8676 {
18a8505e
AT
8677 bool ignored;
8678 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8679 &ignored);
4bb7a0a7 8680 if (attr.form == DW_FORM_ref_addr)
b98664d3 8681 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8682 else
b9502d3f 8683 {
0826b30a 8684 sect_offset off = attr.get_ref_die_offset ();
9c541725 8685 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8686
8687 if (sibling_ptr < info_ptr)
b98664d3 8688 complaint (_("DW_AT_sibling points backwards"));
22869d73 8689 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8690 reader->die_section->overflow_complaint ();
b9502d3f
WN
8691 else
8692 return sibling_ptr;
8693 }
4bb7a0a7
DJ
8694 }
8695
8696 /* If it isn't DW_AT_sibling, skip this attribute. */
8697 form = abbrev->attrs[i].form;
8698 skip_attribute:
8699 switch (form)
8700 {
4bb7a0a7 8701 case DW_FORM_ref_addr:
ae411497
TT
8702 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8703 and later it is offset sized. */
8704 if (cu->header.version == 2)
8705 info_ptr += cu->header.addr_size;
8706 else
8707 info_ptr += cu->header.offset_size;
8708 break;
36586728
TT
8709 case DW_FORM_GNU_ref_alt:
8710 info_ptr += cu->header.offset_size;
8711 break;
ae411497 8712 case DW_FORM_addr:
4bb7a0a7
DJ
8713 info_ptr += cu->header.addr_size;
8714 break;
8715 case DW_FORM_data1:
8716 case DW_FORM_ref1:
8717 case DW_FORM_flag:
8fe0f950 8718 case DW_FORM_strx1:
4bb7a0a7
DJ
8719 info_ptr += 1;
8720 break;
2dc7f7b3 8721 case DW_FORM_flag_present:
43988095 8722 case DW_FORM_implicit_const:
2dc7f7b3 8723 break;
4bb7a0a7
DJ
8724 case DW_FORM_data2:
8725 case DW_FORM_ref2:
8fe0f950 8726 case DW_FORM_strx2:
4bb7a0a7
DJ
8727 info_ptr += 2;
8728 break;
8fe0f950
AT
8729 case DW_FORM_strx3:
8730 info_ptr += 3;
8731 break;
4bb7a0a7
DJ
8732 case DW_FORM_data4:
8733 case DW_FORM_ref4:
8fe0f950 8734 case DW_FORM_strx4:
4bb7a0a7
DJ
8735 info_ptr += 4;
8736 break;
8737 case DW_FORM_data8:
8738 case DW_FORM_ref8:
55f1336d 8739 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8740 info_ptr += 8;
8741 break;
0224619f
JK
8742 case DW_FORM_data16:
8743 info_ptr += 16;
8744 break;
4bb7a0a7 8745 case DW_FORM_string:
9b1c24c8 8746 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8747 info_ptr += bytes_read;
8748 break;
2dc7f7b3 8749 case DW_FORM_sec_offset:
4bb7a0a7 8750 case DW_FORM_strp:
36586728 8751 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8752 info_ptr += cu->header.offset_size;
8753 break;
2dc7f7b3 8754 case DW_FORM_exprloc:
4bb7a0a7
DJ
8755 case DW_FORM_block:
8756 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8757 info_ptr += bytes_read;
8758 break;
8759 case DW_FORM_block1:
8760 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8761 break;
8762 case DW_FORM_block2:
8763 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8764 break;
8765 case DW_FORM_block4:
8766 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8767 break;
336d760d 8768 case DW_FORM_addrx:
cf532bd1 8769 case DW_FORM_strx:
4bb7a0a7
DJ
8770 case DW_FORM_sdata:
8771 case DW_FORM_udata:
8772 case DW_FORM_ref_udata:
3019eac3
DE
8773 case DW_FORM_GNU_addr_index:
8774 case DW_FORM_GNU_str_index:
18a8505e 8775 case DW_FORM_rnglistx:
41144253 8776 case DW_FORM_loclistx:
d521ce57 8777 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8778 break;
8779 case DW_FORM_indirect:
8780 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8781 info_ptr += bytes_read;
8782 /* We need to continue parsing from here, so just go back to
8783 the top. */
8784 goto skip_attribute;
8785
8786 default:
3e43a32a
MS
8787 error (_("Dwarf Error: Cannot handle %s "
8788 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8789 dwarf_form_name (form),
8790 bfd_get_filename (abfd));
8791 }
8792 }
8793
8794 if (abbrev->has_children)
dee91e82 8795 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8796 else
8797 return info_ptr;
8798}
8799
93311388 8800/* Locate ORIG_PDI's sibling.
dee91e82 8801 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8802
d521ce57 8803static const gdb_byte *
dee91e82
DE
8804locate_pdi_sibling (const struct die_reader_specs *reader,
8805 struct partial_die_info *orig_pdi,
d521ce57 8806 const gdb_byte *info_ptr)
91c24f0a
DC
8807{
8808 /* Do we know the sibling already? */
72bf9492 8809
91c24f0a
DC
8810 if (orig_pdi->sibling)
8811 return orig_pdi->sibling;
8812
8813 /* Are there any children to deal with? */
8814
8815 if (!orig_pdi->has_children)
8816 return info_ptr;
8817
4bb7a0a7 8818 /* Skip the children the long way. */
91c24f0a 8819
dee91e82 8820 return skip_children (reader, info_ptr);
91c24f0a
DC
8821}
8822
257e7a09 8823/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8824 not NULL. */
c906108c 8825
891813be
TT
8826void
8827dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8828{
ed2dc618
SM
8829 struct dwarf2_per_objfile *dwarf2_per_objfile
8830 = get_dwarf2_per_objfile (objfile);
8831
077cbab2
TT
8832 gdb_assert (!readin);
8833 /* If this psymtab is constructed from a debug-only objfile, the
8834 has_section_at_zero flag will not necessarily be correct. We
8835 can get the correct value for this flag by looking at the data
8836 associated with the (presumably stripped) associated objfile. */
8837 if (objfile->separate_debug_objfile_backlink)
c906108c 8838 {
077cbab2
TT
8839 struct dwarf2_per_objfile *dpo_backlink
8840 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8841
077cbab2
TT
8842 dwarf2_per_objfile->has_section_at_zero
8843 = dpo_backlink->has_section_at_zero;
8844 }
98bfdba5 8845
8566b89b 8846 expand_psymtab (objfile);
95554aad 8847
ed2dc618 8848 process_cu_includes (dwarf2_per_objfile);
c906108c 8849}
9cdd5dbd
DE
8850\f
8851/* Reading in full CUs. */
c906108c 8852
10b3939b
DJ
8853/* Add PER_CU to the queue. */
8854
8855static void
95554aad
TT
8856queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8857 enum language pretend_language)
10b3939b 8858{
10b3939b 8859 per_cu->queued = 1;
39856def 8860 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8861}
8862
89e63ee4
DE
8863/* If PER_CU is not yet queued, add it to the queue.
8864 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8865 dependency.
0907af0c 8866 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8867 meaning either PER_CU is already queued or it is already loaded.
8868
8869 N.B. There is an invariant here that if a CU is queued then it is loaded.
8870 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8871
8872static int
89e63ee4 8873maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8874 struct dwarf2_per_cu_data *per_cu,
8875 enum language pretend_language)
8876{
8877 /* We may arrive here during partial symbol reading, if we need full
8878 DIEs to process an unusual case (e.g. template arguments). Do
8879 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8880 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8881 {
8882 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8883 return 1;
8884 return 0;
8885 }
8886
8887 /* Mark the dependence relation so that we don't flush PER_CU
8888 too early. */
89e63ee4
DE
8889 if (dependent_cu != NULL)
8890 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8891
8892 /* If it's already on the queue, we have nothing to do. */
8893 if (per_cu->queued)
8894 return 0;
8895
8896 /* If the compilation unit is already loaded, just mark it as
8897 used. */
8898 if (per_cu->cu != NULL)
8899 {
8900 per_cu->cu->last_used = 0;
8901 return 0;
8902 }
8903
8904 /* Add it to the queue. */
8905 queue_comp_unit (per_cu, pretend_language);
8906
8907 return 1;
8908}
8909
10b3939b
DJ
8910/* Process the queue. */
8911
8912static void
ed2dc618 8913process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8914{
b4f54984 8915 if (dwarf_read_debug)
45cfd468
DE
8916 {
8917 fprintf_unfiltered (gdb_stdlog,
8918 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8919 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8920 }
8921
03dd20cc
DJ
8922 /* The queue starts out with one item, but following a DIE reference
8923 may load a new CU, adding it to the end of the queue. */
39856def 8924 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8925 {
39856def
TT
8926 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8927
cc12ce38 8928 if ((dwarf2_per_objfile->using_index
39856def
TT
8929 ? !item.per_cu->v.quick->compunit_symtab
8930 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8931 /* Skip dummy CUs. */
39856def 8932 && item.per_cu->cu != NULL)
f4dc4d17 8933 {
39856def 8934 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8935 unsigned int debug_print_threshold;
247f5c4f 8936 char buf[100];
f4dc4d17 8937
247f5c4f 8938 if (per_cu->is_debug_types)
f4dc4d17 8939 {
247f5c4f
DE
8940 struct signatured_type *sig_type =
8941 (struct signatured_type *) per_cu;
8942
9d8780f0 8943 sprintf (buf, "TU %s at offset %s",
73be47f5 8944 hex_string (sig_type->signature),
9d8780f0 8945 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8946 /* There can be 100s of TUs.
8947 Only print them in verbose mode. */
8948 debug_print_threshold = 2;
f4dc4d17 8949 }
247f5c4f 8950 else
73be47f5 8951 {
9d8780f0
SM
8952 sprintf (buf, "CU at offset %s",
8953 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8954 debug_print_threshold = 1;
8955 }
247f5c4f 8956
b4f54984 8957 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8958 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8959
8960 if (per_cu->is_debug_types)
39856def 8961 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8962 else
39856def 8963 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8964
b4f54984 8965 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8966 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8967 }
10b3939b 8968
39856def
TT
8969 item.per_cu->queued = 0;
8970 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8971 }
8972
b4f54984 8973 if (dwarf_read_debug)
45cfd468
DE
8974 {
8975 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8976 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8977 }
10b3939b
DJ
8978}
8979
10b3939b
DJ
8980/* Read in full symbols for PST, and anything it depends on. */
8981
8566b89b
TT
8982void
8983dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8984{
194d088f 8985 gdb_assert (!readin);
95554aad 8986
48993951 8987 expand_dependencies (objfile);
aaa75496 8988
b83470bf 8989 dw2_do_instantiate_symtab (per_cu_data, false);
5717c425 8990 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
8991}
8992
dee91e82
DE
8993/* Trivial hash function for die_info: the hash value of a DIE
8994 is its offset in .debug_info for this objfile. */
10b3939b 8995
dee91e82
DE
8996static hashval_t
8997die_hash (const void *item)
10b3939b 8998{
9a3c8263 8999 const struct die_info *die = (const struct die_info *) item;
6502dd73 9000
9c541725 9001 return to_underlying (die->sect_off);
dee91e82 9002}
63d06c5c 9003
dee91e82
DE
9004/* Trivial comparison function for die_info structures: two DIEs
9005 are equal if they have the same offset. */
98bfdba5 9006
dee91e82
DE
9007static int
9008die_eq (const void *item_lhs, const void *item_rhs)
9009{
9a3c8263
SM
9010 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9011 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9012
9c541725 9013 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9014}
c906108c 9015
c0ab21c2 9016/* Load the DIEs associated with PER_CU into memory. */
c906108c 9017
dee91e82 9018static void
c0ab21c2
TT
9019load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
9020 bool skip_partial,
9021 enum language pretend_language)
dee91e82 9022{
c0ab21c2
TT
9023 gdb_assert (! this_cu->is_debug_types);
9024
6751ebae 9025 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
9026 if (reader.dummy_p)
9027 return;
9028
9029 struct dwarf2_cu *cu = reader.cu;
9030 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9031
dee91e82
DE
9032 gdb_assert (cu->die_hash == NULL);
9033 cu->die_hash =
9034 htab_create_alloc_ex (cu->header.length / 12,
9035 die_hash,
9036 die_eq,
9037 NULL,
9038 &cu->comp_unit_obstack,
9039 hashtab_obstack_allocate,
9040 dummy_obstack_deallocate);
e142c38c 9041
3e225074 9042 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9043 reader.comp_unit_die->child
9044 = read_die_and_siblings (&reader, reader.info_ptr,
9045 &info_ptr, reader.comp_unit_die);
9046 cu->dies = reader.comp_unit_die;
dee91e82 9047 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9048
9049 /* We try not to read any attributes in this function, because not
9cdd5dbd 9050 all CUs needed for references have been loaded yet, and symbol
10b3939b 9051 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9052 or we won't be able to build types correctly.
9053 Similarly, if we do not read the producer, we can not apply
9054 producer-specific interpretation. */
c0ab21c2 9055 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9056
9057 reader.keep ();
10b3939b
DJ
9058}
9059
3da10d80
KS
9060/* Add a DIE to the delayed physname list. */
9061
9062static void
9063add_to_method_list (struct type *type, int fnfield_index, int index,
9064 const char *name, struct die_info *die,
9065 struct dwarf2_cu *cu)
9066{
9067 struct delayed_method_info mi;
9068 mi.type = type;
9069 mi.fnfield_index = fnfield_index;
9070 mi.index = index;
9071 mi.name = name;
9072 mi.die = die;
c89b44cd 9073 cu->method_list.push_back (mi);
3da10d80
KS
9074}
9075
3693fdb3
PA
9076/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9077 "const" / "volatile". If so, decrements LEN by the length of the
9078 modifier and return true. Otherwise return false. */
9079
9080template<size_t N>
9081static bool
9082check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9083{
9084 size_t mod_len = sizeof (mod) - 1;
9085 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9086 {
9087 len -= mod_len;
9088 return true;
9089 }
9090 return false;
9091}
9092
3da10d80
KS
9093/* Compute the physnames of any methods on the CU's method list.
9094
9095 The computation of method physnames is delayed in order to avoid the
9096 (bad) condition that one of the method's formal parameters is of an as yet
9097 incomplete type. */
9098
9099static void
9100compute_delayed_physnames (struct dwarf2_cu *cu)
9101{
3693fdb3 9102 /* Only C++ delays computing physnames. */
c89b44cd 9103 if (cu->method_list.empty ())
3693fdb3
PA
9104 return;
9105 gdb_assert (cu->language == language_cplus);
9106
52941706 9107 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9108 {
1d06ead6 9109 const char *physname;
3da10d80 9110 struct fn_fieldlist *fn_flp
c89b44cd
TT
9111 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9112 physname = dwarf2_physname (mi.name, mi.die, cu);
9113 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9114 = physname ? physname : "";
3693fdb3
PA
9115
9116 /* Since there's no tag to indicate whether a method is a
9117 const/volatile overload, extract that information out of the
9118 demangled name. */
9119 if (physname != NULL)
9120 {
9121 size_t len = strlen (physname);
9122
9123 while (1)
9124 {
9125 if (physname[len] == ')') /* shortcut */
9126 break;
9127 else if (check_modifier (physname, len, " const"))
c89b44cd 9128 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9129 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9130 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9131 else
9132 break;
9133 }
9134 }
3da10d80 9135 }
c89b44cd
TT
9136
9137 /* The list is no longer needed. */
9138 cu->method_list.clear ();
3da10d80
KS
9139}
9140
a766d390
DE
9141/* Go objects should be embedded in a DW_TAG_module DIE,
9142 and it's not clear if/how imported objects will appear.
9143 To keep Go support simple until that's worked out,
9144 go back through what we've read and create something usable.
9145 We could do this while processing each DIE, and feels kinda cleaner,
9146 but that way is more invasive.
9147 This is to, for example, allow the user to type "p var" or "b main"
9148 without having to specify the package name, and allow lookups
9149 of module.object to work in contexts that use the expression
9150 parser. */
9151
9152static void
9153fixup_go_packaging (struct dwarf2_cu *cu)
9154{
421d1616 9155 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9156 struct pending *list;
9157 int i;
9158
c24bdb02 9159 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9160 list != NULL;
9161 list = list->next)
a766d390
DE
9162 {
9163 for (i = 0; i < list->nsyms; ++i)
9164 {
9165 struct symbol *sym = list->symbol[i];
9166
c1b5c1eb 9167 if (sym->language () == language_go
a766d390
DE
9168 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9169 {
421d1616
TT
9170 gdb::unique_xmalloc_ptr<char> this_package_name
9171 (go_symbol_package_name (sym));
a766d390
DE
9172
9173 if (this_package_name == NULL)
9174 continue;
9175 if (package_name == NULL)
421d1616 9176 package_name = std::move (this_package_name);
a766d390
DE
9177 else
9178 {
518817b3
SM
9179 struct objfile *objfile
9180 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9181 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9182 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9183 (symbol_symtab (sym) != NULL
9184 ? symtab_to_filename_for_display
9185 (symbol_symtab (sym))
e3b94546 9186 : objfile_name (objfile)),
421d1616 9187 this_package_name.get (), package_name.get ());
a766d390
DE
9188 }
9189 }
9190 }
9191 }
9192
9193 if (package_name != NULL)
9194 {
518817b3 9195 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9196 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9197 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9198 saved_package_name);
a766d390
DE
9199 struct symbol *sym;
9200
8c14c3a3 9201 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9202 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9203 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9204 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9205 e.g., "main" finds the "main" module and not C's main(). */
9206 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9207 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9208 SYMBOL_TYPE (sym) = type;
9209
c24bdb02 9210 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9211 }
9212}
9213
c9317f21
TT
9214/* Allocate a fully-qualified name consisting of the two parts on the
9215 obstack. */
9216
9217static const char *
9218rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9219{
9220 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9221}
9222
9c6a1327
TT
9223/* A helper that allocates a variant part to attach to a Rust enum
9224 type. OBSTACK is where the results should be allocated. TYPE is
9225 the type we're processing. DISCRIMINANT_INDEX is the index of the
9226 discriminant. It must be the index of one of the fields of TYPE.
9227 DEFAULT_INDEX is the index of the default field; or -1 if there is
9228 no default. RANGES is indexed by "effective" field number (the
9229 field index, but omitting the discriminant and default fields) and
9230 must hold the discriminant values used by the variants. Note that
9231 RANGES must have a lifetime at least as long as OBSTACK -- either
9232 already allocated on it, or static. */
c9317f21 9233
9c6a1327
TT
9234static void
9235alloc_rust_variant (struct obstack *obstack, struct type *type,
9236 int discriminant_index, int default_index,
9237 gdb::array_view<discriminant_range> ranges)
9238{
9239 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9240 must be handled by the caller. */
9241 gdb_assert (discriminant_index >= 0
1f704f76 9242 && discriminant_index < type->num_fields ());
c9317f21 9243 gdb_assert (default_index == -1
1f704f76 9244 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9245
9c6a1327 9246 /* We have one variant for each non-discriminant field. */
1f704f76 9247 int n_variants = type->num_fields () - 1;
c9317f21 9248
9c6a1327
TT
9249 variant *variants = new (obstack) variant[n_variants];
9250 int var_idx = 0;
9251 int range_idx = 0;
1f704f76 9252 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9253 {
9254 if (i == discriminant_index)
9255 continue;
c9317f21 9256
9c6a1327
TT
9257 variants[var_idx].first_field = i;
9258 variants[var_idx].last_field = i + 1;
9259
9260 /* The default field does not need a range, but other fields do.
9261 We skipped the discriminant above. */
9262 if (i != default_index)
9263 {
9264 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9265 ++range_idx;
9266 }
c9317f21 9267
9c6a1327
TT
9268 ++var_idx;
9269 }
9270
9271 gdb_assert (range_idx == ranges.size ());
9272 gdb_assert (var_idx == n_variants);
9273
9274 variant_part *part = new (obstack) variant_part;
9275 part->discriminant_index = discriminant_index;
9276 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9277 discriminant_index));
9278 part->variants = gdb::array_view<variant> (variants, n_variants);
9279
9280 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9281 gdb::array_view<variant_part> *prop_value
9282 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9283
9c6a1327
TT
9284 struct dynamic_prop prop;
9285 prop.kind = PROP_VARIANT_PARTS;
9286 prop.data.variant_parts = prop_value;
9287
5c54719c 9288 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9289}
9290
9291/* Some versions of rustc emitted enums in an unusual way.
9292
9293 Ordinary enums were emitted as unions. The first element of each
9294 structure in the union was named "RUST$ENUM$DISR". This element
9295 held the discriminant.
9296
9297 These versions of Rust also implemented the "non-zero"
9298 optimization. When the enum had two values, and one is empty and
9299 the other holds a pointer that cannot be zero, the pointer is used
9300 as the discriminant, with a zero value meaning the empty variant.
9301 Here, the union's first member is of the form
9302 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9303 where the fieldnos are the indices of the fields that should be
9304 traversed in order to find the field (which may be several fields deep)
9305 and the variantname is the name of the variant of the case when the
9306 field is zero.
9307
9308 This function recognizes whether TYPE is of one of these forms,
9309 and, if so, smashes it to be a variant type. */
9310
9311static void
9312quirk_rust_enum (struct type *type, struct objfile *objfile)
9313{
78134374 9314 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9315
9316 /* We don't need to deal with empty enums. */
1f704f76 9317 if (type->num_fields () == 0)
c9317f21
TT
9318 return;
9319
9320#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9321 if (type->num_fields () == 1
c9317f21
TT
9322 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9323 {
9324 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9325
9326 /* Decode the field name to find the offset of the
9327 discriminant. */
9328 ULONGEST bit_offset = 0;
9329 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9330 while (name[0] >= '0' && name[0] <= '9')
9331 {
9332 char *tail;
9333 unsigned long index = strtoul (name, &tail, 10);
9334 name = tail;
9335 if (*name != '$'
1f704f76 9336 || index >= field_type->num_fields ()
c9317f21
TT
9337 || (TYPE_FIELD_LOC_KIND (field_type, index)
9338 != FIELD_LOC_KIND_BITPOS))
9339 {
b98664d3 9340 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9341 "[in module %s]"),
9342 TYPE_FIELD_NAME (type, 0),
9343 objfile_name (objfile));
9344 return;
9345 }
9346 ++name;
9347
9348 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9349 field_type = TYPE_FIELD_TYPE (field_type, index);
9350 }
9351
9c6a1327
TT
9352 /* Smash this type to be a structure type. We have to do this
9353 because the type has already been recorded. */
67607e24 9354 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9355 type->set_num_fields (3);
9c6a1327 9356 /* Save the field we care about. */
ceacbf6e 9357 struct field saved_field = type->field (0);
3cabb6b0
SM
9358 type->set_fields
9359 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9360
9c6a1327
TT
9361 /* Put the discriminant at index 0. */
9362 TYPE_FIELD_TYPE (type, 0) = field_type;
9363 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9364 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9365 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9366
9367 /* The order of fields doesn't really matter, so put the real
9368 field at index 1 and the data-less field at index 2. */
ceacbf6e 9369 type->field (1) = saved_field;
9c6a1327 9370 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9371 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9372 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9373 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9374 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9375
9376 const char *dataless_name
7d93a1e0 9377 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9378 name);
9379 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9380 dataless_name);
9c6a1327 9381 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9382 /* NAME points into the original discriminant name, which
9383 already has the correct lifetime. */
9c6a1327 9384 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9385 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9386
9c6a1327
TT
9387 /* Indicate that this is a variant type. */
9388 static discriminant_range ranges[1] = { { 0, 0 } };
9389 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9390 }
77c2dba3
TT
9391 /* A union with a single anonymous field is probably an old-style
9392 univariant enum. */
1f704f76 9393 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9394 {
c9317f21
TT
9395 /* Smash this type to be a structure type. We have to do this
9396 because the type has already been recorded. */
67607e24 9397 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9398
9c6a1327 9399 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9400 const char *variant_name
7d93a1e0 9401 = rust_last_path_segment (field_type->name ());
9c6a1327 9402 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9403 field_type->set_name
9404 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9405 type->name (), variant_name));
c9317f21
TT
9406 }
9407 else
9408 {
9409 struct type *disr_type = nullptr;
1f704f76 9410 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9411 {
9412 disr_type = TYPE_FIELD_TYPE (type, i);
9413
78134374 9414 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9415 {
9416 /* All fields of a true enum will be structs. */
9417 return;
9418 }
1f704f76 9419 else if (disr_type->num_fields () == 0)
c9317f21
TT
9420 {
9421 /* Could be data-less variant, so keep going. */
a037790e 9422 disr_type = nullptr;
c9317f21
TT
9423 }
9424 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9425 "RUST$ENUM$DISR") != 0)
9426 {
9427 /* Not a Rust enum. */
9428 return;
9429 }
9430 else
9431 {
9432 /* Found one. */
9433 break;
9434 }
9435 }
9436
9437 /* If we got here without a discriminant, then it's probably
9438 just a union. */
9439 if (disr_type == nullptr)
9440 return;
9441
9442 /* Smash this type to be a structure type. We have to do this
9443 because the type has already been recorded. */
67607e24 9444 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9445
9c6a1327 9446 /* Make space for the discriminant field. */
ceacbf6e 9447 struct field *disr_field = &disr_type->field (0);
9c6a1327 9448 field *new_fields
1f704f76 9449 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9450 * sizeof (struct field)));
80fc5e77 9451 memcpy (new_fields + 1, type->fields (),
1f704f76 9452 type->num_fields () * sizeof (struct field));
3cabb6b0 9453 type->set_fields (new_fields);
1f704f76 9454 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9455
9456 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9457 type->field (0) = *disr_field;
9c6a1327
TT
9458 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9459 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9460
9461 /* We need a way to find the correct discriminant given a
9462 variant name. For convenience we build a map here. */
9463 struct type *enum_type = FIELD_TYPE (*disr_field);
9464 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9465 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9466 {
9467 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9468 {
9469 const char *name
9470 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9471 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9472 }
9473 }
9474
1f704f76 9475 int n_fields = type->num_fields ();
9c6a1327
TT
9476 /* We don't need a range entry for the discriminant, but we do
9477 need one for every other field, as there is no default
9478 variant. */
9479 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9480 discriminant_range,
9481 n_fields - 1);
c9317f21
TT
9482 /* Skip the discriminant here. */
9483 for (int i = 1; i < n_fields; ++i)
9484 {
9485 /* Find the final word in the name of this variant's type.
9486 That name can be used to look up the correct
9487 discriminant. */
9488 const char *variant_name
7d93a1e0 9489 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9490
9491 auto iter = discriminant_map.find (variant_name);
9492 if (iter != discriminant_map.end ())
9c6a1327
TT
9493 {
9494 ranges[i].low = iter->second;
9495 ranges[i].high = iter->second;
9496 }
c9317f21 9497
bedda9ac 9498 /* Remove the discriminant field, if it exists. */
9c6a1327 9499 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9500 if (sub_type->num_fields () > 0)
bedda9ac 9501 {
5e33d5f4 9502 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9503 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9504 }
9c6a1327 9505 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9506 sub_type->set_name
9507 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9508 type->name (), variant_name));
c9317f21 9509 }
9c6a1327
TT
9510
9511 /* Indicate that this is a variant type. */
9512 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9513 gdb::array_view<discriminant_range> (ranges,
9514 n_fields - 1));
c9317f21
TT
9515 }
9516}
9517
9518/* Rewrite some Rust unions to be structures with variants parts. */
9519
9520static void
9521rust_union_quirks (struct dwarf2_cu *cu)
9522{
9523 gdb_assert (cu->language == language_rust);
52941706
SM
9524 for (type *type_ : cu->rust_unions)
9525 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9526 /* We don't need this any more. */
9527 cu->rust_unions.clear ();
c9317f21
TT
9528}
9529
95554aad
TT
9530/* Return the symtab for PER_CU. This works properly regardless of
9531 whether we're using the index or psymtabs. */
9532
43f3e411
DE
9533static struct compunit_symtab *
9534get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9535{
ed2dc618 9536 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9537 ? per_cu->v.quick->compunit_symtab
9538 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9539}
9540
9541/* A helper function for computing the list of all symbol tables
9542 included by PER_CU. */
9543
9544static void
4c39bc03 9545recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9546 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9547 struct dwarf2_per_cu_data *per_cu,
43f3e411 9548 struct compunit_symtab *immediate_parent)
95554aad
TT
9549{
9550 void **slot;
43f3e411 9551 struct compunit_symtab *cust;
95554aad
TT
9552
9553 slot = htab_find_slot (all_children, per_cu, INSERT);
9554 if (*slot != NULL)
9555 {
9556 /* This inclusion and its children have been processed. */
9557 return;
9558 }
9559
9560 *slot = per_cu;
9561 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9562 cust = get_compunit_symtab (per_cu);
9563 if (cust != NULL)
ec94af83
DE
9564 {
9565 /* If this is a type unit only add its symbol table if we haven't
9566 seen it yet (type unit per_cu's can share symtabs). */
9567 if (per_cu->is_debug_types)
9568 {
43f3e411 9569 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9570 if (*slot == NULL)
9571 {
43f3e411 9572 *slot = cust;
4c39bc03 9573 result->push_back (cust);
43f3e411
DE
9574 if (cust->user == NULL)
9575 cust->user = immediate_parent;
ec94af83
DE
9576 }
9577 }
9578 else
f9125b6c 9579 {
4c39bc03 9580 result->push_back (cust);
43f3e411
DE
9581 if (cust->user == NULL)
9582 cust->user = immediate_parent;
f9125b6c 9583 }
ec94af83 9584 }
95554aad 9585
ae640021
AB
9586 if (!per_cu->imported_symtabs_empty ())
9587 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9588 {
9589 recursively_compute_inclusions (result, all_children,
9590 all_type_symtabs, ptr, cust);
9591 }
95554aad
TT
9592}
9593
43f3e411 9594/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9595 PER_CU. */
9596
9597static void
43f3e411 9598compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9599{
f4dc4d17
DE
9600 gdb_assert (! per_cu->is_debug_types);
9601
ae640021 9602 if (!per_cu->imported_symtabs_empty ())
95554aad 9603 {
ae640021 9604 int len;
4c39bc03 9605 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9606 htab_t all_children, all_type_symtabs;
43f3e411 9607 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9608
9609 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9610 if (cust == NULL)
95554aad
TT
9611 return;
9612
9613 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9614 NULL, xcalloc, xfree);
ec94af83
DE
9615 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9616 NULL, xcalloc, xfree);
95554aad 9617
ae640021 9618 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9619 {
9620 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9621 all_type_symtabs, ptr, cust);
ec94af83 9622 }
95554aad 9623
ec94af83 9624 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9625 len = result_symtabs.size ();
43f3e411 9626 cust->includes
ed2dc618 9627 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9628 struct compunit_symtab *, len + 1);
4c39bc03
TT
9629 memcpy (cust->includes, result_symtabs.data (),
9630 len * sizeof (compunit_symtab *));
43f3e411 9631 cust->includes[len] = NULL;
95554aad 9632
95554aad 9633 htab_delete (all_children);
ec94af83 9634 htab_delete (all_type_symtabs);
95554aad
TT
9635 }
9636}
9637
9638/* Compute the 'includes' field for the symtabs of all the CUs we just
9639 read. */
9640
9641static void
ed2dc618 9642process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9643{
71b73764 9644 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9645 {
9646 if (! iter->is_debug_types)
43f3e411 9647 compute_compunit_symtab_includes (iter);
f4dc4d17 9648 }
95554aad 9649
c5d0225d 9650 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9651}
9652
9cdd5dbd 9653/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9654 already been loaded into memory. */
9655
9656static void
95554aad
TT
9657process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9658 enum language pretend_language)
10b3939b 9659{
10b3939b 9660 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9661 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9662 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9663 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9664 CORE_ADDR lowpc, highpc;
43f3e411 9665 struct compunit_symtab *cust;
10b3939b 9666 CORE_ADDR baseaddr;
4359dff1 9667 struct block *static_block;
3e29f34a 9668 CORE_ADDR addr;
10b3939b 9669
b3b3bada 9670 baseaddr = objfile->text_section_offset ();
10b3939b 9671
c89b44cd
TT
9672 /* Clear the list here in case something was left over. */
9673 cu->method_list.clear ();
10b3939b 9674
95554aad
TT
9675 cu->language = pretend_language;
9676 cu->language_defn = language_def (cu->language);
9677
c906108c 9678 /* Do line number decoding in read_file_scope () */
10b3939b 9679 process_die (cu->dies, cu);
c906108c 9680
a766d390
DE
9681 /* For now fudge the Go package. */
9682 if (cu->language == language_go)
9683 fixup_go_packaging (cu);
9684
5f48f8f3 9685 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9686 should be complete, and it should now be safe to compute all of the
9687 physnames. */
9688 compute_delayed_physnames (cu);
3da10d80 9689
c9317f21
TT
9690 if (cu->language == language_rust)
9691 rust_union_quirks (cu);
9692
fae299cd
DC
9693 /* Some compilers don't define a DW_AT_high_pc attribute for the
9694 compilation unit. If the DW_AT_high_pc is missing, synthesize
9695 it, by scanning the DIE's below the compilation unit. */
10b3939b 9696 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9697
3e29f34a 9698 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9699 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9700
9701 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9702 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9703 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9704 addrmap to help ensure it has an accurate map of pc values belonging to
9705 this comp unit. */
9706 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9707
c24bdb02 9708 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9709 SECT_OFF_TEXT (objfile),
9710 0);
c906108c 9711
43f3e411 9712 if (cust != NULL)
c906108c 9713 {
df15bd07 9714 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9715
8be455d7
JK
9716 /* Set symtab language to language from DW_AT_language. If the
9717 compilation is from a C file generated by language preprocessors, do
9718 not set the language if it was already deduced by start_subfile. */
43f3e411 9719 if (!(cu->language == language_c
40e3ad0e 9720 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9721 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9722
9723 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9724 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9725 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9726 there were bugs in prologue debug info, fixed later in GCC-4.5
9727 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9728
9729 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9730 needed, it would be wrong due to missing DW_AT_producer there.
9731
9732 Still one can confuse GDB by using non-standard GCC compilation
9733 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9734 */
ab260dad 9735 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9736 cust->locations_valid = 1;
e0d00bc7
JK
9737
9738 if (gcc_4_minor >= 5)
43f3e411 9739 cust->epilogue_unwind_valid = 1;
96408a79 9740
43f3e411 9741 cust->call_site_htab = cu->call_site_htab;
c906108c 9742 }
9291a0cd
TT
9743
9744 if (dwarf2_per_objfile->using_index)
43f3e411 9745 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9746 else
9747 {
891813be 9748 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9749 pst->compunit_symtab = cust;
6d94535f 9750 pst->readin = true;
9291a0cd 9751 }
c906108c 9752
95554aad 9753 /* Push it for inclusion processing later. */
c5d0225d 9754 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9755
9756 /* Not needed any more. */
c24bdb02 9757 cu->reset_builder ();
f4dc4d17 9758}
45cfd468 9759
f4dc4d17
DE
9760/* Generate full symbol information for type unit PER_CU, whose DIEs have
9761 already been loaded into memory. */
9762
9763static void
9764process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9765 enum language pretend_language)
9766{
9767 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9768 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9769 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9770 struct compunit_symtab *cust;
0186c6a7
DE
9771 struct signatured_type *sig_type;
9772
9773 gdb_assert (per_cu->is_debug_types);
9774 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9775
c89b44cd
TT
9776 /* Clear the list here in case something was left over. */
9777 cu->method_list.clear ();
f4dc4d17 9778
f4dc4d17
DE
9779 cu->language = pretend_language;
9780 cu->language_defn = language_def (cu->language);
9781
9782 /* The symbol tables are set up in read_type_unit_scope. */
9783 process_die (cu->dies, cu);
9784
9785 /* For now fudge the Go package. */
9786 if (cu->language == language_go)
9787 fixup_go_packaging (cu);
9788
5f48f8f3 9789 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9790 should be complete, and it should now be safe to compute all of the
9791 physnames. */
9792 compute_delayed_physnames (cu);
f4dc4d17 9793
c9317f21
TT
9794 if (cu->language == language_rust)
9795 rust_union_quirks (cu);
9796
f4dc4d17
DE
9797 /* TUs share symbol tables.
9798 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9799 of it with end_expandable_symtab. Otherwise, complete the addition of
9800 this TU's symbols to the existing symtab. */
43f3e411 9801 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9802 {
c24bdb02
KS
9803 buildsym_compunit *builder = cu->get_builder ();
9804 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9805 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9806
43f3e411 9807 if (cust != NULL)
f4dc4d17
DE
9808 {
9809 /* Set symtab language to language from DW_AT_language. If the
9810 compilation is from a C file generated by language preprocessors,
9811 do not set the language if it was already deduced by
9812 start_subfile. */
43f3e411
DE
9813 if (!(cu->language == language_c
9814 && COMPUNIT_FILETABS (cust)->language != language_c))
9815 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9816 }
9817 }
9818 else
9819 {
c24bdb02 9820 cu->get_builder ()->augment_type_symtab ();
43f3e411 9821 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9822 }
9823
9824 if (dwarf2_per_objfile->using_index)
43f3e411 9825 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9826 else
9827 {
891813be 9828 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9829 pst->compunit_symtab = cust;
6d94535f 9830 pst->readin = true;
45cfd468 9831 }
804d2729
TT
9832
9833 /* Not needed any more. */
c24bdb02 9834 cu->reset_builder ();
c906108c
SS
9835}
9836
95554aad
TT
9837/* Process an imported unit DIE. */
9838
9839static void
9840process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9841{
9842 struct attribute *attr;
9843
f4dc4d17
DE
9844 /* For now we don't handle imported units in type units. */
9845 if (cu->per_cu->is_debug_types)
9846 {
9847 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9848 " supported in type units [in module %s]"),
518817b3 9849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9850 }
9851
95554aad
TT
9852 attr = dwarf2_attr (die, DW_AT_import, cu);
9853 if (attr != NULL)
9854 {
0826b30a 9855 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9856 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9857 dwarf2_per_cu_data *per_cu
e3b94546 9858 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9859 cu->per_cu->dwarf2_per_objfile);
95554aad 9860
58990295
TV
9861 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9862 into another compilation unit, at root level. Regard this as a hint,
9863 and ignore it. */
9864 if (die->parent && die->parent->parent == NULL
9865 && per_cu->unit_type == DW_UT_compile
9866 && per_cu->lang == language_cplus)
9867 return;
9868
69d751e3 9869 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9870 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9871 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9872
ae640021 9873 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9874 }
9875}
9876
4c8aa72d
PA
9877/* RAII object that represents a process_die scope: i.e.,
9878 starts/finishes processing a DIE. */
9879class process_die_scope
adde2bff 9880{
4c8aa72d
PA
9881public:
9882 process_die_scope (die_info *die, dwarf2_cu *cu)
9883 : m_die (die), m_cu (cu)
9884 {
9885 /* We should only be processing DIEs not already in process. */
9886 gdb_assert (!m_die->in_process);
9887 m_die->in_process = true;
9888 }
8c3cb9fa 9889
4c8aa72d
PA
9890 ~process_die_scope ()
9891 {
9892 m_die->in_process = false;
9893
9894 /* If we're done processing the DIE for the CU that owns the line
9895 header, we don't need the line header anymore. */
9896 if (m_cu->line_header_die_owner == m_die)
9897 {
9898 delete m_cu->line_header;
9899 m_cu->line_header = NULL;
9900 m_cu->line_header_die_owner = NULL;
9901 }
9902 }
9903
9904private:
9905 die_info *m_die;
9906 dwarf2_cu *m_cu;
9907};
adde2bff 9908
c906108c
SS
9909/* Process a die and its children. */
9910
9911static void
e7c27a73 9912process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9913{
4c8aa72d 9914 process_die_scope scope (die, cu);
adde2bff 9915
c906108c
SS
9916 switch (die->tag)
9917 {
9918 case DW_TAG_padding:
9919 break;
9920 case DW_TAG_compile_unit:
95554aad 9921 case DW_TAG_partial_unit:
e7c27a73 9922 read_file_scope (die, cu);
c906108c 9923 break;
348e048f
DE
9924 case DW_TAG_type_unit:
9925 read_type_unit_scope (die, cu);
9926 break;
c906108c 9927 case DW_TAG_subprogram:
0a4b0913
AB
9928 /* Nested subprograms in Fortran get a prefix. */
9929 if (cu->language == language_fortran
9930 && die->parent != NULL
9931 && die->parent->tag == DW_TAG_subprogram)
9932 cu->processing_has_namespace_info = true;
9933 /* Fall through. */
c906108c 9934 case DW_TAG_inlined_subroutine:
edb3359d 9935 read_func_scope (die, cu);
c906108c
SS
9936 break;
9937 case DW_TAG_lexical_block:
14898363
L
9938 case DW_TAG_try_block:
9939 case DW_TAG_catch_block:
e7c27a73 9940 read_lexical_block_scope (die, cu);
c906108c 9941 break;
216f72a1 9942 case DW_TAG_call_site:
96408a79
SA
9943 case DW_TAG_GNU_call_site:
9944 read_call_site_scope (die, cu);
9945 break;
c906108c 9946 case DW_TAG_class_type:
680b30c7 9947 case DW_TAG_interface_type:
c906108c
SS
9948 case DW_TAG_structure_type:
9949 case DW_TAG_union_type:
134d01f1 9950 process_structure_scope (die, cu);
c906108c
SS
9951 break;
9952 case DW_TAG_enumeration_type:
134d01f1 9953 process_enumeration_scope (die, cu);
c906108c 9954 break;
134d01f1 9955
f792889a
DJ
9956 /* These dies have a type, but processing them does not create
9957 a symbol or recurse to process the children. Therefore we can
9958 read them on-demand through read_type_die. */
c906108c 9959 case DW_TAG_subroutine_type:
72019c9c 9960 case DW_TAG_set_type:
c906108c 9961 case DW_TAG_array_type:
c906108c 9962 case DW_TAG_pointer_type:
c906108c 9963 case DW_TAG_ptr_to_member_type:
c906108c 9964 case DW_TAG_reference_type:
4297a3f0 9965 case DW_TAG_rvalue_reference_type:
c906108c 9966 case DW_TAG_string_type:
c906108c 9967 break;
134d01f1 9968
c906108c 9969 case DW_TAG_base_type:
a02abb62 9970 case DW_TAG_subrange_type:
cb249c71 9971 case DW_TAG_typedef:
134d01f1
DJ
9972 /* Add a typedef symbol for the type definition, if it has a
9973 DW_AT_name. */
f792889a 9974 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9975 break;
c906108c 9976 case DW_TAG_common_block:
e7c27a73 9977 read_common_block (die, cu);
c906108c
SS
9978 break;
9979 case DW_TAG_common_inclusion:
9980 break;
d9fa45fe 9981 case DW_TAG_namespace:
9068261f 9982 cu->processing_has_namespace_info = true;
e7c27a73 9983 read_namespace (die, cu);
d9fa45fe 9984 break;
5d7cb8df 9985 case DW_TAG_module:
9068261f 9986 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9987 read_module (die, cu);
9988 break;
d9fa45fe 9989 case DW_TAG_imported_declaration:
9068261f 9990 cu->processing_has_namespace_info = true;
74921315
KS
9991 if (read_namespace_alias (die, cu))
9992 break;
86a73007
TT
9993 /* The declaration is not a global namespace alias. */
9994 /* Fall through. */
d9fa45fe 9995 case DW_TAG_imported_module:
9068261f 9996 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9997 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9998 || cu->language != language_fortran))
b98664d3 9999 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10000 dwarf_tag_name (die->tag));
10001 read_import_statement (die, cu);
d9fa45fe 10002 break;
95554aad
TT
10003
10004 case DW_TAG_imported_unit:
10005 process_imported_unit_die (die, cu);
10006 break;
10007
71a3c369
TT
10008 case DW_TAG_variable:
10009 read_variable (die, cu);
10010 break;
10011
c906108c 10012 default:
e7c27a73 10013 new_symbol (die, NULL, cu);
c906108c
SS
10014 break;
10015 }
10016}
ca69b9e6
DE
10017\f
10018/* DWARF name computation. */
c906108c 10019
94af9270
KS
10020/* A helper function for dwarf2_compute_name which determines whether DIE
10021 needs to have the name of the scope prepended to the name listed in the
10022 die. */
10023
10024static int
10025die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10026{
1c809c68
TT
10027 struct attribute *attr;
10028
94af9270
KS
10029 switch (die->tag)
10030 {
10031 case DW_TAG_namespace:
10032 case DW_TAG_typedef:
10033 case DW_TAG_class_type:
10034 case DW_TAG_interface_type:
10035 case DW_TAG_structure_type:
10036 case DW_TAG_union_type:
10037 case DW_TAG_enumeration_type:
10038 case DW_TAG_enumerator:
10039 case DW_TAG_subprogram:
08a76f8a 10040 case DW_TAG_inlined_subroutine:
94af9270 10041 case DW_TAG_member:
74921315 10042 case DW_TAG_imported_declaration:
94af9270
KS
10043 return 1;
10044
10045 case DW_TAG_variable:
c2b0a229 10046 case DW_TAG_constant:
94af9270
KS
10047 /* We only need to prefix "globally" visible variables. These include
10048 any variable marked with DW_AT_external or any variable that
10049 lives in a namespace. [Variables in anonymous namespaces
10050 require prefixing, but they are not DW_AT_external.] */
10051
10052 if (dwarf2_attr (die, DW_AT_specification, cu))
10053 {
10054 struct dwarf2_cu *spec_cu = cu;
9a619af0 10055
94af9270
KS
10056 return die_needs_namespace (die_specification (die, &spec_cu),
10057 spec_cu);
10058 }
10059
1c809c68 10060 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10061 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10062 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10063 return 0;
10064 /* A variable in a lexical block of some kind does not need a
10065 namespace, even though in C++ such variables may be external
10066 and have a mangled name. */
10067 if (die->parent->tag == DW_TAG_lexical_block
10068 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10069 || die->parent->tag == DW_TAG_catch_block
10070 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10071 return 0;
10072 return 1;
94af9270
KS
10073
10074 default:
10075 return 0;
10076 }
10077}
10078
73b9be8b
KS
10079/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10080 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10081 defined for the given DIE. */
10082
10083static struct attribute *
10084dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10085{
10086 struct attribute *attr;
10087
10088 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10089 if (attr == NULL)
10090 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10091
10092 return attr;
10093}
10094
10095/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10096 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10097 defined for the given DIE. */
10098
10099static const char *
10100dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10101{
10102 const char *linkage_name;
10103
10104 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10105 if (linkage_name == NULL)
10106 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10107
787de330
TT
10108 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10109 See https://github.com/rust-lang/rust/issues/32925. */
10110 if (cu->language == language_rust && linkage_name != NULL
10111 && strchr (linkage_name, '{') != NULL)
10112 linkage_name = NULL;
10113
73b9be8b
KS
10114 return linkage_name;
10115}
10116
94af9270 10117/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10118 compute the physname for the object, which include a method's:
9c37b5ae 10119 - formal parameters (C++),
a766d390 10120 - receiver type (Go),
a766d390
DE
10121
10122 The term "physname" is a bit confusing.
10123 For C++, for example, it is the demangled name.
10124 For Go, for example, it's the mangled name.
94af9270 10125
af6b7be1
JB
10126 For Ada, return the DIE's linkage name rather than the fully qualified
10127 name. PHYSNAME is ignored..
10128
45940949
TT
10129 The result is allocated on the dwarf2_per_objfile obstack and
10130 canonicalized. */
94af9270
KS
10131
10132static const char *
15d034d0
TT
10133dwarf2_compute_name (const char *name,
10134 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10135 int physname)
10136{
518817b3 10137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10138
94af9270
KS
10139 if (name == NULL)
10140 name = dwarf2_name (die, cu);
10141
2ee7123e
DE
10142 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10143 but otherwise compute it by typename_concat inside GDB.
10144 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10145 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10146 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10147 will set the demangled name to the result of dwarf2_full_name, and it is
10148 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10149 if (cu->language == language_ada
10150 || (cu->language == language_fortran && physname))
10151 {
10152 /* For Ada unit, we prefer the linkage name over the name, as
10153 the former contains the exported name, which the user expects
10154 to be able to reference. Ideally, we want the user to be able
10155 to reference this entity using either natural or linkage name,
10156 but we haven't started looking at this enhancement yet. */
73b9be8b 10157 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10158
2ee7123e
DE
10159 if (linkage_name != NULL)
10160 return linkage_name;
f55ee35c
JK
10161 }
10162
94af9270
KS
10163 /* These are the only languages we know how to qualify names in. */
10164 if (name != NULL
9c37b5ae 10165 && (cu->language == language_cplus
c44af4eb
TT
10166 || cu->language == language_fortran || cu->language == language_d
10167 || cu->language == language_rust))
94af9270
KS
10168 {
10169 if (die_needs_namespace (die, cu))
10170 {
0d5cff50 10171 const char *prefix;
34a68019 10172 const char *canonical_name = NULL;
94af9270 10173
d7e74731
PA
10174 string_file buf;
10175
94af9270 10176 prefix = determine_prefix (die, cu);
94af9270
KS
10177 if (*prefix != '\0')
10178 {
43816ebc
TT
10179 gdb::unique_xmalloc_ptr<char> prefixed_name
10180 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10181
43816ebc 10182 buf.puts (prefixed_name.get ());
94af9270
KS
10183 }
10184 else
d7e74731 10185 buf.puts (name);
94af9270 10186
98bfdba5
PA
10187 /* Template parameters may be specified in the DIE's DW_AT_name, or
10188 as children with DW_TAG_template_type_param or
10189 DW_TAG_value_type_param. If the latter, add them to the name
10190 here. If the name already has template parameters, then
10191 skip this step; some versions of GCC emit both, and
10192 it is more efficient to use the pre-computed name.
10193
10194 Something to keep in mind about this process: it is very
10195 unlikely, or in some cases downright impossible, to produce
10196 something that will match the mangled name of a function.
10197 If the definition of the function has the same debug info,
10198 we should be able to match up with it anyway. But fallbacks
10199 using the minimal symbol, for instance to find a method
10200 implemented in a stripped copy of libstdc++, will not work.
10201 If we do not have debug info for the definition, we will have to
10202 match them up some other way.
10203
10204 When we do name matching there is a related problem with function
10205 templates; two instantiated function templates are allowed to
10206 differ only by their return types, which we do not add here. */
10207
10208 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10209 {
10210 struct attribute *attr;
10211 struct die_info *child;
10212 int first = 1;
10213
10214 die->building_fullname = 1;
10215
10216 for (child = die->child; child != NULL; child = child->sibling)
10217 {
10218 struct type *type;
12df843f 10219 LONGEST value;
d521ce57 10220 const gdb_byte *bytes;
98bfdba5
PA
10221 struct dwarf2_locexpr_baton *baton;
10222 struct value *v;
10223
10224 if (child->tag != DW_TAG_template_type_param
10225 && child->tag != DW_TAG_template_value_param)
10226 continue;
10227
10228 if (first)
10229 {
d7e74731 10230 buf.puts ("<");
98bfdba5
PA
10231 first = 0;
10232 }
10233 else
d7e74731 10234 buf.puts (", ");
98bfdba5
PA
10235
10236 attr = dwarf2_attr (child, DW_AT_type, cu);
10237 if (attr == NULL)
10238 {
b98664d3 10239 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10240 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10241 continue;
10242 }
10243 type = die_type (child, cu);
10244
10245 if (child->tag == DW_TAG_template_type_param)
10246 {
c1ec8cea
TT
10247 c_print_type (type, "", &buf, -1, 0, cu->language,
10248 &type_print_raw_options);
98bfdba5
PA
10249 continue;
10250 }
10251
10252 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10253 if (attr == NULL)
10254 {
b98664d3 10255 complaint (_("template parameter missing "
3e43a32a 10256 "DW_AT_const_value"));
d7e74731 10257 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10258 continue;
10259 }
10260
10261 dwarf2_const_value_attr (attr, type, name,
10262 &cu->comp_unit_obstack, cu,
10263 &value, &bytes, &baton);
10264
10265 if (TYPE_NOSIGN (type))
10266 /* GDB prints characters as NUMBER 'CHAR'. If that's
10267 changed, this can use value_print instead. */
d7e74731 10268 c_printchar (value, type, &buf);
98bfdba5
PA
10269 else
10270 {
10271 struct value_print_options opts;
10272
10273 if (baton != NULL)
10274 v = dwarf2_evaluate_loc_desc (type, NULL,
10275 baton->data,
10276 baton->size,
10277 baton->per_cu);
10278 else if (bytes != NULL)
10279 {
10280 v = allocate_value (type);
10281 memcpy (value_contents_writeable (v), bytes,
10282 TYPE_LENGTH (type));
10283 }
10284 else
10285 v = value_from_longest (type, value);
10286
3e43a32a
MS
10287 /* Specify decimal so that we do not depend on
10288 the radix. */
98bfdba5
PA
10289 get_formatted_print_options (&opts, 'd');
10290 opts.raw = 1;
d7e74731 10291 value_print (v, &buf, &opts);
98bfdba5 10292 release_value (v);
98bfdba5
PA
10293 }
10294 }
10295
10296 die->building_fullname = 0;
10297
10298 if (!first)
10299 {
10300 /* Close the argument list, with a space if necessary
10301 (nested templates). */
d7e74731
PA
10302 if (!buf.empty () && buf.string ().back () == '>')
10303 buf.puts (" >");
98bfdba5 10304 else
d7e74731 10305 buf.puts (">");
98bfdba5
PA
10306 }
10307 }
10308
9c37b5ae 10309 /* For C++ methods, append formal parameter type
94af9270 10310 information, if PHYSNAME. */
6e70227d 10311
94af9270 10312 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10313 && cu->language == language_cplus)
94af9270
KS
10314 {
10315 struct type *type = read_type_die (die, cu);
10316
d7e74731 10317 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10318 &type_print_raw_options);
94af9270 10319
9c37b5ae 10320 if (cu->language == language_cplus)
94af9270 10321 {
60430eff
DJ
10322 /* Assume that an artificial first parameter is
10323 "this", but do not crash if it is not. RealView
10324 marks unnamed (and thus unused) parameters as
10325 artificial; there is no way to differentiate
10326 the two cases. */
1f704f76 10327 if (type->num_fields () > 0
94af9270 10328 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10329 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10330 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10331 0))))
d7e74731 10332 buf.puts (" const");
94af9270
KS
10333 }
10334 }
10335
d7e74731 10336 const std::string &intermediate_name = buf.string ();
94af9270
KS
10337
10338 if (cu->language == language_cplus)
34a68019 10339 canonical_name
322a8516 10340 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10341 objfile);
34a68019
TT
10342
10343 /* If we only computed INTERMEDIATE_NAME, or if
10344 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10345 intern it. */
322a8516 10346 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10347 name = objfile->intern (intermediate_name);
34a68019
TT
10348 else
10349 name = canonical_name;
94af9270
KS
10350 }
10351 }
10352
10353 return name;
10354}
10355
0114d602
DJ
10356/* Return the fully qualified name of DIE, based on its DW_AT_name.
10357 If scope qualifiers are appropriate they will be added. The result
34a68019 10358 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10359 not have a name. NAME may either be from a previous call to
10360 dwarf2_name or NULL.
10361
9c37b5ae 10362 The output string will be canonicalized (if C++). */
0114d602
DJ
10363
10364static const char *
15d034d0 10365dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10366{
94af9270
KS
10367 return dwarf2_compute_name (name, die, cu, 0);
10368}
0114d602 10369
94af9270
KS
10370/* Construct a physname for the given DIE in CU. NAME may either be
10371 from a previous call to dwarf2_name or NULL. The result will be
10372 allocated on the objfile_objstack or NULL if the DIE does not have a
10373 name.
0114d602 10374
9c37b5ae 10375 The output string will be canonicalized (if C++). */
0114d602 10376
94af9270 10377static const char *
15d034d0 10378dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10379{
518817b3 10380 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10381 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10382 int need_copy = 1;
10383
10384 /* In this case dwarf2_compute_name is just a shortcut not building anything
10385 on its own. */
10386 if (!die_needs_namespace (die, cu))
10387 return dwarf2_compute_name (name, die, cu, 1);
10388
906bb4c5
TT
10389 if (cu->language != language_rust)
10390 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10391
10392 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10393 has computed. */
791afaa2 10394 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10395 if (mangled != NULL)
900e11f9 10396 {
900e11f9 10397
59cc4834
JB
10398 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10399 {
10400 /* Do nothing (do not demangle the symbol name). */
10401 }
10402 else if (cu->language == language_go)
a766d390 10403 {
5e2db402
TT
10404 /* This is a lie, but we already lie to the caller new_symbol.
10405 new_symbol assumes we return the mangled name.
a766d390 10406 This just undoes that lie until things are cleaned up. */
a766d390
DE
10407 }
10408 else
10409 {
0eb876f5
JB
10410 /* Use DMGL_RET_DROP for C++ template functions to suppress
10411 their return type. It is easier for GDB users to search
10412 for such functions as `name(params)' than `long name(params)'.
10413 In such case the minimal symbol names do not match the full
10414 symbol names but for template functions there is never a need
10415 to look up their definition from their declaration so
10416 the only disadvantage remains the minimal symbol variant
10417 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10418 demangled.reset (gdb_demangle (mangled,
10419 (DMGL_PARAMS | DMGL_ANSI
10420 | DMGL_RET_DROP)));
a766d390 10421 }
900e11f9 10422 if (demangled)
791afaa2 10423 canon = demangled.get ();
900e11f9
JK
10424 else
10425 {
10426 canon = mangled;
10427 need_copy = 0;
10428 }
10429 }
10430
10431 if (canon == NULL || check_physname)
10432 {
10433 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10434
10435 if (canon != NULL && strcmp (physname, canon) != 0)
10436 {
10437 /* It may not mean a bug in GDB. The compiler could also
10438 compute DW_AT_linkage_name incorrectly. But in such case
10439 GDB would need to be bug-to-bug compatible. */
10440
b98664d3 10441 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10442 "(from linkage <%s>) - DIE at %s [in module %s]"),
10443 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10444 objfile_name (objfile));
900e11f9
JK
10445
10446 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10447 is available here - over computed PHYSNAME. It is safer
10448 against both buggy GDB and buggy compilers. */
10449
10450 retval = canon;
10451 }
10452 else
10453 {
10454 retval = physname;
10455 need_copy = 0;
10456 }
10457 }
10458 else
10459 retval = canon;
10460
10461 if (need_copy)
be1e3d3e 10462 retval = objfile->intern (retval);
900e11f9 10463
900e11f9 10464 return retval;
0114d602
DJ
10465}
10466
74921315
KS
10467/* Inspect DIE in CU for a namespace alias. If one exists, record
10468 a new symbol for it.
10469
10470 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10471
10472static int
10473read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10474{
10475 struct attribute *attr;
10476
10477 /* If the die does not have a name, this is not a namespace
10478 alias. */
10479 attr = dwarf2_attr (die, DW_AT_name, cu);
10480 if (attr != NULL)
10481 {
10482 int num;
10483 struct die_info *d = die;
10484 struct dwarf2_cu *imported_cu = cu;
10485
10486 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10487 keep inspecting DIEs until we hit the underlying import. */
10488#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10489 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10490 {
10491 attr = dwarf2_attr (d, DW_AT_import, cu);
10492 if (attr == NULL)
10493 break;
10494
10495 d = follow_die_ref (d, attr, &imported_cu);
10496 if (d->tag != DW_TAG_imported_declaration)
10497 break;
10498 }
10499
10500 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10501 {
b98664d3 10502 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10503 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10504 return 0;
10505 }
10506
10507 if (attr != NULL)
10508 {
10509 struct type *type;
0826b30a 10510 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10511
9c541725 10512 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10513 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10514 {
10515 /* This declaration is a global namespace alias. Add
10516 a symbol for it whose type is the aliased namespace. */
10517 new_symbol (die, type, cu);
10518 return 1;
10519 }
10520 }
10521 }
10522
10523 return 0;
10524}
10525
22cee43f 10526/* Return the using directives repository (global or local?) to use in the
804d2729 10527 current context for CU.
22cee43f
PMR
10528
10529 For Ada, imported declarations can materialize renamings, which *may* be
10530 global. However it is impossible (for now?) in DWARF to distinguish
10531 "external" imported declarations and "static" ones. As all imported
10532 declarations seem to be static in all other languages, make them all CU-wide
10533 global only in Ada. */
10534
10535static struct using_direct **
804d2729 10536using_directives (struct dwarf2_cu *cu)
22cee43f 10537{
c24bdb02
KS
10538 if (cu->language == language_ada
10539 && cu->get_builder ()->outermost_context_p ())
10540 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10541 else
c24bdb02 10542 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10543}
10544
27aa8d6a
SW
10545/* Read the import statement specified by the given die and record it. */
10546
10547static void
10548read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10549{
518817b3 10550 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10551 struct attribute *import_attr;
32019081 10552 struct die_info *imported_die, *child_die;
de4affc9 10553 struct dwarf2_cu *imported_cu;
27aa8d6a 10554 const char *imported_name;
794684b6 10555 const char *imported_name_prefix;
13387711
SW
10556 const char *canonical_name;
10557 const char *import_alias;
10558 const char *imported_declaration = NULL;
794684b6 10559 const char *import_prefix;
eb1e02fd 10560 std::vector<const char *> excludes;
13387711 10561
27aa8d6a
SW
10562 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10563 if (import_attr == NULL)
10564 {
b98664d3 10565 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10566 dwarf_tag_name (die->tag));
10567 return;
10568 }
10569
de4affc9
CC
10570 imported_cu = cu;
10571 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10572 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10573 if (imported_name == NULL)
10574 {
10575 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10576
10577 The import in the following code:
10578 namespace A
10579 {
10580 typedef int B;
10581 }
10582
10583 int main ()
10584 {
10585 using A::B;
10586 B b;
10587 return b;
10588 }
10589
10590 ...
10591 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10592 <52> DW_AT_decl_file : 1
10593 <53> DW_AT_decl_line : 6
10594 <54> DW_AT_import : <0x75>
10595 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10596 <59> DW_AT_name : B
10597 <5b> DW_AT_decl_file : 1
10598 <5c> DW_AT_decl_line : 2
10599 <5d> DW_AT_type : <0x6e>
10600 ...
10601 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10602 <76> DW_AT_byte_size : 4
10603 <77> DW_AT_encoding : 5 (signed)
10604
10605 imports the wrong die ( 0x75 instead of 0x58 ).
10606 This case will be ignored until the gcc bug is fixed. */
10607 return;
10608 }
10609
82856980
SW
10610 /* Figure out the local name after import. */
10611 import_alias = dwarf2_name (die, cu);
27aa8d6a 10612
794684b6
SW
10613 /* Figure out where the statement is being imported to. */
10614 import_prefix = determine_prefix (die, cu);
10615
10616 /* Figure out what the scope of the imported die is and prepend it
10617 to the name of the imported die. */
de4affc9 10618 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10619
f55ee35c
JK
10620 if (imported_die->tag != DW_TAG_namespace
10621 && imported_die->tag != DW_TAG_module)
794684b6 10622 {
13387711
SW
10623 imported_declaration = imported_name;
10624 canonical_name = imported_name_prefix;
794684b6 10625 }
13387711 10626 else if (strlen (imported_name_prefix) > 0)
12aaed36 10627 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10628 imported_name_prefix,
10629 (cu->language == language_d ? "." : "::"),
10630 imported_name, (char *) NULL);
13387711
SW
10631 else
10632 canonical_name = imported_name;
794684b6 10633
32019081
JK
10634 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10635 for (child_die = die->child; child_die && child_die->tag;
436c571c 10636 child_die = child_die->sibling)
32019081
JK
10637 {
10638 /* DWARF-4: A Fortran use statement with a “rename list” may be
10639 represented by an imported module entry with an import attribute
10640 referring to the module and owned entries corresponding to those
10641 entities that are renamed as part of being imported. */
10642
10643 if (child_die->tag != DW_TAG_imported_declaration)
10644 {
b98664d3 10645 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10646 "- DIE at %s [in module %s]"),
10647 sect_offset_str (child_die->sect_off),
10648 objfile_name (objfile));
32019081
JK
10649 continue;
10650 }
10651
10652 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10653 if (import_attr == NULL)
10654 {
b98664d3 10655 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10656 dwarf_tag_name (child_die->tag));
10657 continue;
10658 }
10659
10660 imported_cu = cu;
10661 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10662 &imported_cu);
10663 imported_name = dwarf2_name (imported_die, imported_cu);
10664 if (imported_name == NULL)
10665 {
b98664d3 10666 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10667 "imported name - DIE at %s [in module %s]"),
10668 sect_offset_str (child_die->sect_off),
10669 objfile_name (objfile));
32019081
JK
10670 continue;
10671 }
10672
eb1e02fd 10673 excludes.push_back (imported_name);
32019081
JK
10674
10675 process_die (child_die, cu);
10676 }
10677
804d2729 10678 add_using_directive (using_directives (cu),
22cee43f
PMR
10679 import_prefix,
10680 canonical_name,
10681 import_alias,
10682 imported_declaration,
10683 excludes,
10684 0,
10685 &objfile->objfile_obstack);
27aa8d6a
SW
10686}
10687
5230b05a
WT
10688/* ICC<14 does not output the required DW_AT_declaration on incomplete
10689 types, but gives them a size of zero. Starting with version 14,
10690 ICC is compatible with GCC. */
10691
9068261f 10692static bool
5230b05a
WT
10693producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10694{
10695 if (!cu->checked_producer)
10696 check_producer (cu);
10697
10698 return cu->producer_is_icc_lt_14;
10699}
10700
eb77c9df
AB
10701/* ICC generates a DW_AT_type for C void functions. This was observed on
10702 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10703 which says that void functions should not have a DW_AT_type. */
10704
10705static bool
10706producer_is_icc (struct dwarf2_cu *cu)
10707{
10708 if (!cu->checked_producer)
10709 check_producer (cu);
10710
10711 return cu->producer_is_icc;
10712}
10713
1b80a9fa
JK
10714/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10715 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10716 this, it was first present in GCC release 4.3.0. */
10717
9068261f 10718static bool
1b80a9fa
JK
10719producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10720{
10721 if (!cu->checked_producer)
10722 check_producer (cu);
10723
10724 return cu->producer_is_gcc_lt_4_3;
10725}
10726
d721ba37
PA
10727static file_and_directory
10728find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10729{
d721ba37
PA
10730 file_and_directory res;
10731
9291a0cd
TT
10732 /* Find the filename. Do not use dwarf2_name here, since the filename
10733 is not a source language identifier. */
d721ba37
PA
10734 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10735 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10736
d721ba37
PA
10737 if (res.comp_dir == NULL
10738 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10739 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10740 {
d721ba37
PA
10741 res.comp_dir_storage = ldirname (res.name);
10742 if (!res.comp_dir_storage.empty ())
10743 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10744 }
d721ba37 10745 if (res.comp_dir != NULL)
9291a0cd
TT
10746 {
10747 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10748 directory, get rid of it. */
d721ba37 10749 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10750
d721ba37
PA
10751 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10752 res.comp_dir = cp + 1;
9291a0cd
TT
10753 }
10754
d721ba37
PA
10755 if (res.name == NULL)
10756 res.name = "<unknown>";
10757
10758 return res;
9291a0cd
TT
10759}
10760
f4dc4d17
DE
10761/* Handle DW_AT_stmt_list for a compilation unit.
10762 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10763 COMP_DIR is the compilation directory. LOWPC is passed to
10764 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10765
10766static void
10767handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10768 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10769{
518817b3
SM
10770 struct dwarf2_per_objfile *dwarf2_per_objfile
10771 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10772 struct attribute *attr;
527f3840
JK
10773 struct line_header line_header_local;
10774 hashval_t line_header_local_hash;
527f3840
JK
10775 void **slot;
10776 int decode_mapping;
2ab95328 10777
f4dc4d17
DE
10778 gdb_assert (! cu->per_cu->is_debug_types);
10779
2ab95328 10780 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10781 if (attr == NULL)
10782 return;
10783
9c541725 10784 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10785
10786 /* The line header hash table is only created if needed (it exists to
10787 prevent redundant reading of the line table for partial_units).
10788 If we're given a partial_unit, we'll need it. If we're given a
10789 compile_unit, then use the line header hash table if it's already
10790 created, but don't create one just yet. */
10791
10792 if (dwarf2_per_objfile->line_header_hash == NULL
10793 && die->tag == DW_TAG_partial_unit)
2ab95328 10794 {
527f3840 10795 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10796 .reset (htab_create_alloc (127, line_header_hash_voidp,
10797 line_header_eq_voidp,
10798 free_line_header_voidp,
10799 xcalloc, xfree));
527f3840 10800 }
2ab95328 10801
9c541725 10802 line_header_local.sect_off = line_offset;
527f3840
JK
10803 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10804 line_header_local_hash = line_header_hash (&line_header_local);
10805 if (dwarf2_per_objfile->line_header_hash != NULL)
10806 {
d15acc42 10807 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10808 &line_header_local,
10809 line_header_local_hash, NO_INSERT);
10810
10811 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10812 is not present in *SLOT (since if there is something in *SLOT then
10813 it will be for a partial_unit). */
10814 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10815 {
527f3840 10816 gdb_assert (*slot != NULL);
9a3c8263 10817 cu->line_header = (struct line_header *) *slot;
527f3840 10818 return;
dee91e82 10819 }
2ab95328 10820 }
527f3840
JK
10821
10822 /* dwarf_decode_line_header does not yet provide sufficient information.
10823 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10824 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10825 if (lh == NULL)
527f3840 10826 return;
4c8aa72d
PA
10827
10828 cu->line_header = lh.release ();
10829 cu->line_header_die_owner = die;
527f3840
JK
10830
10831 if (dwarf2_per_objfile->line_header_hash == NULL)
10832 slot = NULL;
10833 else
10834 {
d15acc42 10835 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10836 &line_header_local,
10837 line_header_local_hash, INSERT);
10838 gdb_assert (slot != NULL);
10839 }
10840 if (slot != NULL && *slot == NULL)
10841 {
10842 /* This newly decoded line number information unit will be owned
10843 by line_header_hash hash table. */
10844 *slot = cu->line_header;
4c8aa72d 10845 cu->line_header_die_owner = NULL;
527f3840
JK
10846 }
10847 else
10848 {
10849 /* We cannot free any current entry in (*slot) as that struct line_header
10850 may be already used by multiple CUs. Create only temporary decoded
10851 line_header for this CU - it may happen at most once for each line
10852 number information unit. And if we're not using line_header_hash
10853 then this is what we want as well. */
10854 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10855 }
10856 decode_mapping = (die->tag != DW_TAG_partial_unit);
10857 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10858 decode_mapping);
fff8551c 10859
2ab95328
TT
10860}
10861
95554aad 10862/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10863
c906108c 10864static void
e7c27a73 10865read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10866{
518817b3
SM
10867 struct dwarf2_per_objfile *dwarf2_per_objfile
10868 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10869 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10870 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10871 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10872 CORE_ADDR highpc = ((CORE_ADDR) 0);
10873 struct attribute *attr;
c906108c 10874 struct die_info *child_die;
e142c38c 10875 CORE_ADDR baseaddr;
6e70227d 10876
380618d6 10877 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10878 baseaddr = objfile->text_section_offset ();
c906108c 10879
fae299cd 10880 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10881
10882 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10883 from finish_block. */
2acceee2 10884 if (lowpc == ((CORE_ADDR) -1))
c906108c 10885 lowpc = highpc;
3e29f34a 10886 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10887
d721ba37 10888 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10889
f4b8a18d
KW
10890 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10891 standardised yet. As a workaround for the language detection we fall
10892 back to the DW_AT_producer string. */
10893 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10894 cu->language = language_opencl;
10895
3019eac3
DE
10896 /* Similar hack for Go. */
10897 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10898 set_cu_language (DW_LANG_Go, cu);
10899
c24bdb02 10900 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10901
10902 /* Decode line number information if present. We do this before
10903 processing child DIEs, so that the line header table is available
10904 for DW_AT_decl_file. */
d721ba37 10905 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10906
10907 /* Process all dies in compilation unit. */
10908 if (die->child != NULL)
10909 {
10910 child_die = die->child;
10911 while (child_die && child_die->tag)
10912 {
10913 process_die (child_die, cu);
436c571c 10914 child_die = child_die->sibling;
3019eac3
DE
10915 }
10916 }
10917
10918 /* Decode macro information, if present. Dwarf 2 macro information
10919 refers to information in the line number info statement program
10920 header, so we can only read it if we've read the header
10921 successfully. */
0af92d60
JK
10922 attr = dwarf2_attr (die, DW_AT_macros, cu);
10923 if (attr == NULL)
10924 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10925 if (attr && cu->line_header)
10926 {
10927 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10928 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10929
43f3e411 10930 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10931 }
10932 else
10933 {
10934 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10935 if (attr && cu->line_header)
10936 {
10937 unsigned int macro_offset = DW_UNSND (attr);
10938
43f3e411 10939 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10940 }
10941 }
3019eac3
DE
10942}
10943
c24bdb02
KS
10944void
10945dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10946{
f4dc4d17
DE
10947 struct type_unit_group *tu_group;
10948 int first_time;
3019eac3 10949 struct attribute *attr;
9c541725 10950 unsigned int i;
0186c6a7 10951 struct signatured_type *sig_type;
3019eac3 10952
f4dc4d17 10953 gdb_assert (per_cu->is_debug_types);
0186c6a7 10954 sig_type = (struct signatured_type *) per_cu;
3019eac3 10955
c24bdb02 10956 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10957
f4dc4d17 10958 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10959 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10960 if (sig_type->type_unit_group == NULL)
c24bdb02 10961 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10962 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10963
10964 /* If we've already processed this stmt_list there's no real need to
10965 do it again, we could fake it and just recreate the part we need
10966 (file name,index -> symtab mapping). If data shows this optimization
10967 is useful we can do it then. */
43f3e411 10968 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10969
10970 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10971 debug info. */
fff8551c 10972 line_header_up lh;
f4dc4d17 10973 if (attr != NULL)
3019eac3 10974 {
9c541725 10975 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10976 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10977 }
10978 if (lh == NULL)
10979 {
10980 if (first_time)
c24bdb02 10981 start_symtab ("", NULL, 0);
f4dc4d17
DE
10982 else
10983 {
10984 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10985 gdb_assert (m_builder == nullptr);
804d2729 10986 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10987 m_builder.reset (new struct buildsym_compunit
10988 (COMPUNIT_OBJFILE (cust), "",
10989 COMPUNIT_DIRNAME (cust),
10990 compunit_language (cust),
10991 0, cust));
770479f2 10992 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 10993 }
f4dc4d17 10994 return;
3019eac3
DE
10995 }
10996
c24bdb02
KS
10997 line_header = lh.release ();
10998 line_header_die_owner = die;
3019eac3 10999
f4dc4d17
DE
11000 if (first_time)
11001 {
c24bdb02 11002 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11003
1fd60fc0
DE
11004 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11005 still initializing it, and our caller (a few levels up)
11006 process_full_type_unit still needs to know if this is the first
11007 time. */
11008
4ac93832
TT
11009 tu_group->symtabs
11010 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11011 struct symtab *, line_header->file_names_size ());
3019eac3 11012
7ba99d21
AT
11013 auto &file_names = line_header->file_names ();
11014 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11015 {
7ba99d21 11016 file_entry &fe = file_names[i];
c24bdb02
KS
11017 dwarf2_start_subfile (this, fe.name,
11018 fe.include_dir (line_header));
11019 buildsym_compunit *b = get_builder ();
11020 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11021 {
4c8aa72d
PA
11022 /* NOTE: start_subfile will recognize when it's been
11023 passed a file it has already seen. So we can't
11024 assume there's a simple mapping from
11025 cu->line_header->file_names to subfiles, plus
11026 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11027 b->get_current_subfile ()->symtab
11028 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11029 }
11030
c24bdb02 11031 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11032 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11033 }
11034 }
11035 else
3019eac3 11036 {
c24bdb02 11037 gdb_assert (m_builder == nullptr);
804d2729 11038 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11039 m_builder.reset (new struct buildsym_compunit
11040 (COMPUNIT_OBJFILE (cust), "",
11041 COMPUNIT_DIRNAME (cust),
11042 compunit_language (cust),
11043 0, cust));
770479f2 11044 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11045
7ba99d21
AT
11046 auto &file_names = line_header->file_names ();
11047 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11048 {
7ba99d21 11049 file_entry &fe = file_names[i];
4c8aa72d 11050 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11051 }
3019eac3
DE
11052 }
11053
f4dc4d17
DE
11054 /* The main symtab is allocated last. Type units don't have DW_AT_name
11055 so they don't have a "real" (so to speak) symtab anyway.
11056 There is later code that will assign the main symtab to all symbols
11057 that don't have one. We need to handle the case of a symbol with a
11058 missing symtab (DW_AT_decl_file) anyway. */
11059}
3019eac3 11060
f4dc4d17
DE
11061/* Process DW_TAG_type_unit.
11062 For TUs we want to skip the first top level sibling if it's not the
11063 actual type being defined by this TU. In this case the first top
11064 level sibling is there to provide context only. */
3019eac3 11065
f4dc4d17
DE
11066static void
11067read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11068{
11069 struct die_info *child_die;
3019eac3 11070
f4dc4d17
DE
11071 prepare_one_comp_unit (cu, die, language_minimal);
11072
11073 /* Initialize (or reinitialize) the machinery for building symtabs.
11074 We do this before processing child DIEs, so that the line header table
11075 is available for DW_AT_decl_file. */
c24bdb02 11076 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11077
11078 if (die->child != NULL)
11079 {
11080 child_die = die->child;
11081 while (child_die && child_die->tag)
11082 {
11083 process_die (child_die, cu);
436c571c 11084 child_die = child_die->sibling;
f4dc4d17
DE
11085 }
11086 }
3019eac3
DE
11087}
11088\f
80626a55
DE
11089/* DWO/DWP files.
11090
11091 http://gcc.gnu.org/wiki/DebugFission
11092 http://gcc.gnu.org/wiki/DebugFissionDWP
11093
11094 To simplify handling of both DWO files ("object" files with the DWARF info)
11095 and DWP files (a file with the DWOs packaged up into one file), we treat
11096 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11097
11098static hashval_t
11099hash_dwo_file (const void *item)
11100{
9a3c8263 11101 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11102 hashval_t hash;
3019eac3 11103
a2ce51a0
DE
11104 hash = htab_hash_string (dwo_file->dwo_name);
11105 if (dwo_file->comp_dir != NULL)
11106 hash += htab_hash_string (dwo_file->comp_dir);
11107 return hash;
3019eac3
DE
11108}
11109
11110static int
11111eq_dwo_file (const void *item_lhs, const void *item_rhs)
11112{
9a3c8263
SM
11113 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11114 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11115
a2ce51a0
DE
11116 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11117 return 0;
11118 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11119 return lhs->comp_dir == rhs->comp_dir;
11120 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11121}
11122
11123/* Allocate a hash table for DWO files. */
11124
51ac9db5 11125static htab_up
298e9637 11126allocate_dwo_file_hash_table ()
3019eac3 11127{
51ac9db5
SM
11128 auto delete_dwo_file = [] (void *item)
11129 {
11130 struct dwo_file *dwo_file = (struct dwo_file *) item;
11131
11132 delete dwo_file;
11133 };
11134
bc68fb19
TT
11135 return htab_up (htab_create_alloc (41,
11136 hash_dwo_file,
11137 eq_dwo_file,
11138 delete_dwo_file,
11139 xcalloc, xfree));
3019eac3
DE
11140}
11141
80626a55
DE
11142/* Lookup DWO file DWO_NAME. */
11143
11144static void **
ed2dc618
SM
11145lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11146 const char *dwo_name,
11147 const char *comp_dir)
80626a55
DE
11148{
11149 struct dwo_file find_entry;
11150 void **slot;
11151
11152 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11153 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11154
0ac5b59e
DE
11155 find_entry.dwo_name = dwo_name;
11156 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11157 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11158 INSERT);
80626a55
DE
11159
11160 return slot;
11161}
11162
3019eac3
DE
11163static hashval_t
11164hash_dwo_unit (const void *item)
11165{
9a3c8263 11166 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11167
11168 /* This drops the top 32 bits of the id, but is ok for a hash. */
11169 return dwo_unit->signature;
11170}
11171
11172static int
11173eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11174{
9a3c8263
SM
11175 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11176 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11177
11178 /* The signature is assumed to be unique within the DWO file.
11179 So while object file CU dwo_id's always have the value zero,
11180 that's OK, assuming each object file DWO file has only one CU,
11181 and that's the rule for now. */
11182 return lhs->signature == rhs->signature;
11183}
11184
11185/* Allocate a hash table for DWO CUs,TUs.
11186 There is one of these tables for each of CUs,TUs for each DWO file. */
11187
b0b6a987 11188static htab_up
298e9637 11189allocate_dwo_unit_table ()
3019eac3
DE
11190{
11191 /* Start out with a pretty small number.
11192 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11193 return htab_up (htab_create_alloc (3,
11194 hash_dwo_unit,
11195 eq_dwo_unit,
11196 NULL, xcalloc, xfree));
3019eac3
DE
11197}
11198
19c3d4c9 11199/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11200
11201static void
19c3d4c9
DE
11202create_dwo_cu_reader (const struct die_reader_specs *reader,
11203 const gdb_byte *info_ptr,
11204 struct die_info *comp_unit_die,
c0ab21c2
TT
11205 struct dwo_file *dwo_file,
11206 struct dwo_unit *dwo_unit)
3019eac3
DE
11207{
11208 struct dwarf2_cu *cu = reader->cu;
9c541725 11209 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11210 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11211
a084a2a6
AT
11212 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11213 if (!signature.has_value ())
3019eac3 11214 {
b98664d3 11215 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11216 " its dwo_id [in module %s]"),
9d8780f0 11217 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11218 return;
11219 }
11220
3019eac3 11221 dwo_unit->dwo_file = dwo_file;
a084a2a6 11222 dwo_unit->signature = *signature;
8a0459fd 11223 dwo_unit->section = section;
9c541725 11224 dwo_unit->sect_off = sect_off;
3019eac3
DE
11225 dwo_unit->length = cu->per_cu->length;
11226
b4f54984 11227 if (dwarf_read_debug)
9d8780f0
SM
11228 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11229 sect_offset_str (sect_off),
9c541725 11230 hex_string (dwo_unit->signature));
3019eac3
DE
11231}
11232
33c5cd75 11233/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11234 Note: This function processes DWO files only, not DWP files. */
3019eac3 11235
33c5cd75 11236static void
ed2dc618 11237create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11238 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11239 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11240{
11241 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11242 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11243
96b79293 11244 section.read (objfile);
33c5cd75 11245 info_ptr = section.buffer;
3019eac3
DE
11246
11247 if (info_ptr == NULL)
33c5cd75 11248 return;
3019eac3 11249
b4f54984 11250 if (dwarf_read_debug)
19c3d4c9
DE
11251 {
11252 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11253 section.get_name (),
11254 section.get_file_name ());
19c3d4c9 11255 }
3019eac3 11256
33c5cd75 11257 end_ptr = info_ptr + section.size;
3019eac3
DE
11258 while (info_ptr < end_ptr)
11259 {
11260 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11261 struct dwo_unit read_unit {};
33c5cd75
DB
11262 struct dwo_unit *dwo_unit;
11263 void **slot;
11264 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11265
11266 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11267 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11268 per_cu.is_debug_types = 0;
33c5cd75
DB
11269 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11270 per_cu.section = &section;
11271
c0ab21c2
TT
11272 cutu_reader reader (&per_cu, cu, &dwo_file);
11273 if (!reader.dummy_p)
11274 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11275 &dwo_file, &read_unit);
33c5cd75
DB
11276 info_ptr += per_cu.length;
11277
11278 // If the unit could not be parsed, skip it.
c0ab21c2 11279 if (read_unit.dwo_file == NULL)
33c5cd75 11280 continue;
3019eac3 11281
33c5cd75 11282 if (cus_htab == NULL)
298e9637 11283 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11284
45940949
TT
11285 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack,
11286 struct dwo_unit);
c0ab21c2 11287 *dwo_unit = read_unit;
b0b6a987 11288 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11289 gdb_assert (slot != NULL);
11290 if (*slot != NULL)
19c3d4c9 11291 {
33c5cd75
DB
11292 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11293 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11294
b98664d3 11295 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11296 " the entry at offset %s, signature %s"),
11297 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11298 hex_string (dwo_unit->signature));
19c3d4c9 11299 }
33c5cd75 11300 *slot = (void *)dwo_unit;
3019eac3 11301 }
3019eac3
DE
11302}
11303
80626a55
DE
11304/* DWP file .debug_{cu,tu}_index section format:
11305 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11306
d2415c6c
DE
11307 DWP Version 1:
11308
80626a55
DE
11309 Both index sections have the same format, and serve to map a 64-bit
11310 signature to a set of section numbers. Each section begins with a header,
11311 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11312 indexes, and a pool of 32-bit section numbers. The index sections will be
11313 aligned at 8-byte boundaries in the file.
11314
d2415c6c
DE
11315 The index section header consists of:
11316
11317 V, 32 bit version number
11318 -, 32 bits unused
11319 N, 32 bit number of compilation units or type units in the index
11320 M, 32 bit number of slots in the hash table
80626a55 11321
d2415c6c 11322 Numbers are recorded using the byte order of the application binary.
80626a55 11323
d2415c6c
DE
11324 The hash table begins at offset 16 in the section, and consists of an array
11325 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11326 order of the application binary). Unused slots in the hash table are 0.
11327 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11328
d2415c6c
DE
11329 The parallel table begins immediately after the hash table
11330 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11331 array of 32-bit indexes (using the byte order of the application binary),
11332 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11333 table contains a 32-bit index into the pool of section numbers. For unused
11334 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11335
73869dc2
DE
11336 The pool of section numbers begins immediately following the hash table
11337 (at offset 16 + 12 * M from the beginning of the section). The pool of
11338 section numbers consists of an array of 32-bit words (using the byte order
11339 of the application binary). Each item in the array is indexed starting
11340 from 0. The hash table entry provides the index of the first section
11341 number in the set. Additional section numbers in the set follow, and the
11342 set is terminated by a 0 entry (section number 0 is not used in ELF).
11343
11344 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11345 section must be the first entry in the set, and the .debug_abbrev.dwo must
11346 be the second entry. Other members of the set may follow in any order.
11347
11348 ---
11349
11350 DWP Version 2:
11351
11352 DWP Version 2 combines all the .debug_info, etc. sections into one,
11353 and the entries in the index tables are now offsets into these sections.
11354 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11355 section.
11356
11357 Index Section Contents:
11358 Header
11359 Hash Table of Signatures dwp_hash_table.hash_table
11360 Parallel Table of Indices dwp_hash_table.unit_table
11361 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11362 Table of Section Sizes dwp_hash_table.v2.sizes
11363
11364 The index section header consists of:
11365
11366 V, 32 bit version number
11367 L, 32 bit number of columns in the table of section offsets
11368 N, 32 bit number of compilation units or type units in the index
11369 M, 32 bit number of slots in the hash table
11370
11371 Numbers are recorded using the byte order of the application binary.
11372
11373 The hash table has the same format as version 1.
11374 The parallel table of indices has the same format as version 1,
11375 except that the entries are origin-1 indices into the table of sections
11376 offsets and the table of section sizes.
11377
11378 The table of offsets begins immediately following the parallel table
11379 (at offset 16 + 12 * M from the beginning of the section). The table is
11380 a two-dimensional array of 32-bit words (using the byte order of the
11381 application binary), with L columns and N+1 rows, in row-major order.
11382 Each row in the array is indexed starting from 0. The first row provides
11383 a key to the remaining rows: each column in this row provides an identifier
11384 for a debug section, and the offsets in the same column of subsequent rows
11385 refer to that section. The section identifiers are:
11386
11387 DW_SECT_INFO 1 .debug_info.dwo
11388 DW_SECT_TYPES 2 .debug_types.dwo
11389 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11390 DW_SECT_LINE 4 .debug_line.dwo
11391 DW_SECT_LOC 5 .debug_loc.dwo
11392 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11393 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11394 DW_SECT_MACRO 8 .debug_macro.dwo
11395
11396 The offsets provided by the CU and TU index sections are the base offsets
11397 for the contributions made by each CU or TU to the corresponding section
11398 in the package file. Each CU and TU header contains an abbrev_offset
11399 field, used to find the abbreviations table for that CU or TU within the
11400 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11401 be interpreted as relative to the base offset given in the index section.
11402 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11403 should be interpreted as relative to the base offset for .debug_line.dwo,
11404 and offsets into other debug sections obtained from DWARF attributes should
11405 also be interpreted as relative to the corresponding base offset.
11406
11407 The table of sizes begins immediately following the table of offsets.
11408 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11409 with L columns and N rows, in row-major order. Each row in the array is
11410 indexed starting from 1 (row 0 is shared by the two tables).
11411
11412 ---
11413
11414 Hash table lookup is handled the same in version 1 and 2:
11415
11416 We assume that N and M will not exceed 2^32 - 1.
11417 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11418
d2415c6c
DE
11419 Given a 64-bit compilation unit signature or a type signature S, an entry
11420 in the hash table is located as follows:
80626a55 11421
d2415c6c
DE
11422 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11423 the low-order k bits all set to 1.
80626a55 11424
d2415c6c 11425 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11426
d2415c6c
DE
11427 3) If the hash table entry at index H matches the signature, use that
11428 entry. If the hash table entry at index H is unused (all zeroes),
11429 terminate the search: the signature is not present in the table.
80626a55 11430
d2415c6c 11431 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11432
d2415c6c 11433 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11434 to stop at an unused slot or find the match. */
80626a55
DE
11435
11436/* Create a hash table to map DWO IDs to their CU/TU entry in
11437 .debug_{info,types}.dwo in DWP_FILE.
11438 Returns NULL if there isn't one.
11439 Note: This function processes DWP files only, not DWO files. */
11440
11441static struct dwp_hash_table *
ed2dc618
SM
11442create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11443 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11444{
11445 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11446 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11447 const gdb_byte *index_ptr, *index_end;
80626a55 11448 struct dwarf2_section_info *index;
73869dc2 11449 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11450 struct dwp_hash_table *htab;
11451
11452 if (is_debug_types)
11453 index = &dwp_file->sections.tu_index;
11454 else
11455 index = &dwp_file->sections.cu_index;
11456
96b79293 11457 if (index->empty ())
80626a55 11458 return NULL;
96b79293 11459 index->read (objfile);
80626a55
DE
11460
11461 index_ptr = index->buffer;
11462 index_end = index_ptr + index->size;
11463
11464 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11465 index_ptr += 4;
11466 if (version == 2)
11467 nr_columns = read_4_bytes (dbfd, index_ptr);
11468 else
11469 nr_columns = 0;
11470 index_ptr += 4;
80626a55
DE
11471 nr_units = read_4_bytes (dbfd, index_ptr);
11472 index_ptr += 4;
11473 nr_slots = read_4_bytes (dbfd, index_ptr);
11474 index_ptr += 4;
11475
73869dc2 11476 if (version != 1 && version != 2)
80626a55 11477 {
21aa081e 11478 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11479 " [in module %s]"),
21aa081e 11480 pulongest (version), dwp_file->name);
80626a55
DE
11481 }
11482 if (nr_slots != (nr_slots & -nr_slots))
11483 {
21aa081e 11484 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11485 " is not power of 2 [in module %s]"),
21aa081e 11486 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11487 }
11488
45940949 11489 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack, struct dwp_hash_table);
73869dc2
DE
11490 htab->version = version;
11491 htab->nr_columns = nr_columns;
80626a55
DE
11492 htab->nr_units = nr_units;
11493 htab->nr_slots = nr_slots;
11494 htab->hash_table = index_ptr;
11495 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11496
11497 /* Exit early if the table is empty. */
11498 if (nr_slots == 0 || nr_units == 0
11499 || (version == 2 && nr_columns == 0))
11500 {
11501 /* All must be zero. */
11502 if (nr_slots != 0 || nr_units != 0
11503 || (version == 2 && nr_columns != 0))
11504 {
b98664d3 11505 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11506 " all zero [in modules %s]"),
11507 dwp_file->name);
11508 }
11509 return htab;
11510 }
11511
11512 if (version == 1)
11513 {
11514 htab->section_pool.v1.indices =
11515 htab->unit_table + sizeof (uint32_t) * nr_slots;
11516 /* It's harder to decide whether the section is too small in v1.
11517 V1 is deprecated anyway so we punt. */
11518 }
11519 else
11520 {
11521 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11522 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11523 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11524 /* Reverse map for error checking. */
11525 int ids_seen[DW_SECT_MAX + 1];
11526 int i;
11527
11528 if (nr_columns < 2)
11529 {
11530 error (_("Dwarf Error: bad DWP hash table, too few columns"
11531 " in section table [in module %s]"),
11532 dwp_file->name);
11533 }
11534 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11535 {
11536 error (_("Dwarf Error: bad DWP hash table, too many columns"
11537 " in section table [in module %s]"),
11538 dwp_file->name);
11539 }
04fd5eed
GB
11540 memset (ids, 255, sizeof_ids);
11541 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11542 for (i = 0; i < nr_columns; ++i)
11543 {
11544 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11545
11546 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11547 {
11548 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11549 " in section table [in module %s]"),
11550 id, dwp_file->name);
11551 }
11552 if (ids_seen[id] != -1)
11553 {
11554 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11555 " id %d in section table [in module %s]"),
11556 id, dwp_file->name);
11557 }
11558 ids_seen[id] = i;
11559 ids[i] = id;
11560 }
11561 /* Must have exactly one info or types section. */
11562 if (((ids_seen[DW_SECT_INFO] != -1)
11563 + (ids_seen[DW_SECT_TYPES] != -1))
11564 != 1)
11565 {
11566 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11567 " DWO info/types section [in module %s]"),
11568 dwp_file->name);
11569 }
11570 /* Must have an abbrev section. */
11571 if (ids_seen[DW_SECT_ABBREV] == -1)
11572 {
11573 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11574 " section [in module %s]"),
11575 dwp_file->name);
11576 }
11577 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11578 htab->section_pool.v2.sizes =
11579 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11580 * nr_units * nr_columns);
11581 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11582 * nr_units * nr_columns))
11583 > index_end)
11584 {
11585 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11586 " [in module %s]"),
11587 dwp_file->name);
11588 }
11589 }
80626a55
DE
11590
11591 return htab;
11592}
11593
11594/* Update SECTIONS with the data from SECTP.
11595
11596 This function is like the other "locate" section routines that are
11597 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11598 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11599
11600 The result is non-zero for success, or zero if an error was found. */
11601
11602static int
73869dc2
DE
11603locate_v1_virtual_dwo_sections (asection *sectp,
11604 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11605{
11606 const struct dwop_section_names *names = &dwop_section_names;
11607
11608 if (section_is_p (sectp->name, &names->abbrev_dwo))
11609 {
11610 /* There can be only one. */
049412e3 11611 if (sections->abbrev.s.section != NULL)
80626a55 11612 return 0;
049412e3 11613 sections->abbrev.s.section = sectp;
fd361982 11614 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11615 }
11616 else if (section_is_p (sectp->name, &names->info_dwo)
11617 || section_is_p (sectp->name, &names->types_dwo))
11618 {
11619 /* There can be only one. */
049412e3 11620 if (sections->info_or_types.s.section != NULL)
80626a55 11621 return 0;
049412e3 11622 sections->info_or_types.s.section = sectp;
fd361982 11623 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11624 }
11625 else if (section_is_p (sectp->name, &names->line_dwo))
11626 {
11627 /* There can be only one. */
049412e3 11628 if (sections->line.s.section != NULL)
80626a55 11629 return 0;
049412e3 11630 sections->line.s.section = sectp;
fd361982 11631 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11632 }
11633 else if (section_is_p (sectp->name, &names->loc_dwo))
11634 {
11635 /* There can be only one. */
049412e3 11636 if (sections->loc.s.section != NULL)
80626a55 11637 return 0;
049412e3 11638 sections->loc.s.section = sectp;
fd361982 11639 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11640 }
11641 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11642 {
11643 /* There can be only one. */
049412e3 11644 if (sections->macinfo.s.section != NULL)
80626a55 11645 return 0;
049412e3 11646 sections->macinfo.s.section = sectp;
fd361982 11647 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11648 }
11649 else if (section_is_p (sectp->name, &names->macro_dwo))
11650 {
11651 /* There can be only one. */
049412e3 11652 if (sections->macro.s.section != NULL)
80626a55 11653 return 0;
049412e3 11654 sections->macro.s.section = sectp;
fd361982 11655 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11656 }
11657 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11658 {
11659 /* There can be only one. */
049412e3 11660 if (sections->str_offsets.s.section != NULL)
80626a55 11661 return 0;
049412e3 11662 sections->str_offsets.s.section = sectp;
fd361982 11663 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11664 }
11665 else
11666 {
11667 /* No other kind of section is valid. */
11668 return 0;
11669 }
11670
11671 return 1;
11672}
11673
73869dc2
DE
11674/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11675 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11676 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11677 This is for DWP version 1 files. */
80626a55
DE
11678
11679static struct dwo_unit *
ed2dc618
SM
11680create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11681 struct dwp_file *dwp_file,
73869dc2
DE
11682 uint32_t unit_index,
11683 const char *comp_dir,
11684 ULONGEST signature, int is_debug_types)
80626a55 11685{
73869dc2
DE
11686 const struct dwp_hash_table *dwp_htab =
11687 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11688 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11689 const char *kind = is_debug_types ? "TU" : "CU";
11690 struct dwo_file *dwo_file;
11691 struct dwo_unit *dwo_unit;
73869dc2 11692 struct virtual_v1_dwo_sections sections;
80626a55 11693 void **dwo_file_slot;
80626a55
DE
11694 int i;
11695
73869dc2
DE
11696 gdb_assert (dwp_file->version == 1);
11697
b4f54984 11698 if (dwarf_read_debug)
80626a55 11699 {
73869dc2 11700 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11701 kind,
73869dc2 11702 pulongest (unit_index), hex_string (signature),
80626a55
DE
11703 dwp_file->name);
11704 }
11705
19ac8c2e 11706 /* Fetch the sections of this DWO unit.
80626a55
DE
11707 Put a limit on the number of sections we look for so that bad data
11708 doesn't cause us to loop forever. */
11709
73869dc2 11710#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11711 (1 /* .debug_info or .debug_types */ \
11712 + 1 /* .debug_abbrev */ \
11713 + 1 /* .debug_line */ \
11714 + 1 /* .debug_loc */ \
11715 + 1 /* .debug_str_offsets */ \
19ac8c2e 11716 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11717 + 1 /* trailing zero */)
11718
11719 memset (&sections, 0, sizeof (sections));
80626a55 11720
73869dc2 11721 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11722 {
11723 asection *sectp;
11724 uint32_t section_nr =
11725 read_4_bytes (dbfd,
73869dc2
DE
11726 dwp_htab->section_pool.v1.indices
11727 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11728
11729 if (section_nr == 0)
11730 break;
11731 if (section_nr >= dwp_file->num_sections)
11732 {
11733 error (_("Dwarf Error: bad DWP hash table, section number too large"
11734 " [in module %s]"),
11735 dwp_file->name);
11736 }
11737
11738 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11739 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11740 {
11741 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11742 " [in module %s]"),
11743 dwp_file->name);
11744 }
11745 }
11746
11747 if (i < 2
96b79293
TT
11748 || sections.info_or_types.empty ()
11749 || sections.abbrev.empty ())
80626a55
DE
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11752 " [in module %s]"),
11753 dwp_file->name);
11754 }
73869dc2 11755 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11756 {
11757 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11758 " [in module %s]"),
11759 dwp_file->name);
11760 }
11761
11762 /* It's easier for the rest of the code if we fake a struct dwo_file and
11763 have dwo_unit "live" in that. At least for now.
11764
11765 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11766 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11767 file, we can combine them back into a virtual DWO file to save space
11768 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11769 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11770
791afaa2
TT
11771 std::string virtual_dwo_name =
11772 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11773 sections.abbrev.get_id (),
11774 sections.line.get_id (),
11775 sections.loc.get_id (),
11776 sections.str_offsets.get_id ());
80626a55 11777 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11778 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11779 virtual_dwo_name.c_str (),
11780 comp_dir);
80626a55
DE
11781 /* Create one if necessary. */
11782 if (*dwo_file_slot == NULL)
11783 {
b4f54984 11784 if (dwarf_read_debug)
80626a55
DE
11785 {
11786 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11787 virtual_dwo_name.c_str ());
80626a55 11788 }
51ac9db5 11789 dwo_file = new struct dwo_file;
45940949 11790 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11791 dwo_file->comp_dir = comp_dir;
80626a55
DE
11792 dwo_file->sections.abbrev = sections.abbrev;
11793 dwo_file->sections.line = sections.line;
11794 dwo_file->sections.loc = sections.loc;
11795 dwo_file->sections.macinfo = sections.macinfo;
11796 dwo_file->sections.macro = sections.macro;
11797 dwo_file->sections.str_offsets = sections.str_offsets;
11798 /* The "str" section is global to the entire DWP file. */
11799 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11800 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11801 there's no need to record it in dwo_file.
11802 Also, we can't simply record type sections in dwo_file because
11803 we record a pointer into the vector in dwo_unit. As we collect more
11804 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11805 for it, invalidating all copies of pointers into the previous
11806 contents. */
80626a55
DE
11807 *dwo_file_slot = dwo_file;
11808 }
11809 else
11810 {
b4f54984 11811 if (dwarf_read_debug)
80626a55
DE
11812 {
11813 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11814 virtual_dwo_name.c_str ());
80626a55 11815 }
9a3c8263 11816 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11817 }
80626a55 11818
45940949 11819 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack, struct dwo_unit);
80626a55
DE
11820 dwo_unit->dwo_file = dwo_file;
11821 dwo_unit->signature = signature;
8d749320 11822 dwo_unit->section =
45940949 11823 XOBNEW (&dwarf2_per_objfile->obstack, struct dwarf2_section_info);
8a0459fd 11824 *dwo_unit->section = sections.info_or_types;
57d63ce2 11825 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11826
11827 return dwo_unit;
11828}
11829
73869dc2
DE
11830/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11831 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11832 piece within that section used by a TU/CU, return a virtual section
11833 of just that piece. */
11834
11835static struct dwarf2_section_info
ed2dc618
SM
11836create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11837 struct dwarf2_section_info *section,
73869dc2
DE
11838 bfd_size_type offset, bfd_size_type size)
11839{
11840 struct dwarf2_section_info result;
11841 asection *sectp;
11842
11843 gdb_assert (section != NULL);
11844 gdb_assert (!section->is_virtual);
11845
11846 memset (&result, 0, sizeof (result));
11847 result.s.containing_section = section;
dc4ccb6f 11848 result.is_virtual = true;
73869dc2
DE
11849
11850 if (size == 0)
11851 return result;
11852
96b79293 11853 sectp = section->get_bfd_section ();
73869dc2
DE
11854
11855 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11856 bounds of the real section. This is a pretty-rare event, so just
11857 flag an error (easier) instead of a warning and trying to cope. */
11858 if (sectp == NULL
fd361982 11859 || offset + size > bfd_section_size (sectp))
73869dc2 11860 {
73869dc2
DE
11861 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11862 " in section %s [in module %s]"),
fd361982 11863 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11864 objfile_name (dwarf2_per_objfile->objfile));
11865 }
11866
11867 result.virtual_offset = offset;
11868 result.size = size;
11869 return result;
11870}
11871
11872/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11873 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11874 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11875 This is for DWP version 2 files. */
11876
11877static struct dwo_unit *
ed2dc618
SM
11878create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11879 struct dwp_file *dwp_file,
73869dc2
DE
11880 uint32_t unit_index,
11881 const char *comp_dir,
11882 ULONGEST signature, int is_debug_types)
11883{
73869dc2
DE
11884 const struct dwp_hash_table *dwp_htab =
11885 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11886 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11887 const char *kind = is_debug_types ? "TU" : "CU";
11888 struct dwo_file *dwo_file;
11889 struct dwo_unit *dwo_unit;
11890 struct virtual_v2_dwo_sections sections;
11891 void **dwo_file_slot;
73869dc2
DE
11892 int i;
11893
11894 gdb_assert (dwp_file->version == 2);
11895
b4f54984 11896 if (dwarf_read_debug)
73869dc2
DE
11897 {
11898 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11899 kind,
11900 pulongest (unit_index), hex_string (signature),
11901 dwp_file->name);
11902 }
11903
11904 /* Fetch the section offsets of this DWO unit. */
11905
11906 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11907
11908 for (i = 0; i < dwp_htab->nr_columns; ++i)
11909 {
11910 uint32_t offset = read_4_bytes (dbfd,
11911 dwp_htab->section_pool.v2.offsets
11912 + (((unit_index - 1) * dwp_htab->nr_columns
11913 + i)
11914 * sizeof (uint32_t)));
11915 uint32_t size = read_4_bytes (dbfd,
11916 dwp_htab->section_pool.v2.sizes
11917 + (((unit_index - 1) * dwp_htab->nr_columns
11918 + i)
11919 * sizeof (uint32_t)));
11920
11921 switch (dwp_htab->section_pool.v2.section_ids[i])
11922 {
11923 case DW_SECT_INFO:
11924 case DW_SECT_TYPES:
11925 sections.info_or_types_offset = offset;
11926 sections.info_or_types_size = size;
11927 break;
11928 case DW_SECT_ABBREV:
11929 sections.abbrev_offset = offset;
11930 sections.abbrev_size = size;
11931 break;
11932 case DW_SECT_LINE:
11933 sections.line_offset = offset;
11934 sections.line_size = size;
11935 break;
11936 case DW_SECT_LOC:
11937 sections.loc_offset = offset;
11938 sections.loc_size = size;
11939 break;
11940 case DW_SECT_STR_OFFSETS:
11941 sections.str_offsets_offset = offset;
11942 sections.str_offsets_size = size;
11943 break;
11944 case DW_SECT_MACINFO:
11945 sections.macinfo_offset = offset;
11946 sections.macinfo_size = size;
11947 break;
11948 case DW_SECT_MACRO:
11949 sections.macro_offset = offset;
11950 sections.macro_size = size;
11951 break;
11952 }
11953 }
11954
11955 /* It's easier for the rest of the code if we fake a struct dwo_file and
11956 have dwo_unit "live" in that. At least for now.
11957
11958 The DWP file can be made up of a random collection of CUs and TUs.
11959 However, for each CU + set of TUs that came from the same original DWO
11960 file, we can combine them back into a virtual DWO file to save space
11961 (fewer struct dwo_file objects to allocate). Remember that for really
11962 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11963
791afaa2
TT
11964 std::string virtual_dwo_name =
11965 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11966 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11967 (long) (sections.line_size ? sections.line_offset : 0),
11968 (long) (sections.loc_size ? sections.loc_offset : 0),
11969 (long) (sections.str_offsets_size
11970 ? sections.str_offsets_offset : 0));
73869dc2 11971 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11972 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11973 virtual_dwo_name.c_str (),
11974 comp_dir);
73869dc2
DE
11975 /* Create one if necessary. */
11976 if (*dwo_file_slot == NULL)
11977 {
b4f54984 11978 if (dwarf_read_debug)
73869dc2
DE
11979 {
11980 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11981 virtual_dwo_name.c_str ());
73869dc2 11982 }
51ac9db5 11983 dwo_file = new struct dwo_file;
45940949 11984 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
11985 dwo_file->comp_dir = comp_dir;
11986 dwo_file->sections.abbrev =
ed2dc618 11987 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11988 sections.abbrev_offset, sections.abbrev_size);
11989 dwo_file->sections.line =
ed2dc618 11990 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11991 sections.line_offset, sections.line_size);
11992 dwo_file->sections.loc =
ed2dc618 11993 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11994 sections.loc_offset, sections.loc_size);
11995 dwo_file->sections.macinfo =
ed2dc618 11996 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11997 sections.macinfo_offset, sections.macinfo_size);
11998 dwo_file->sections.macro =
ed2dc618 11999 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12000 sections.macro_offset, sections.macro_size);
12001 dwo_file->sections.str_offsets =
ed2dc618
SM
12002 create_dwp_v2_section (dwarf2_per_objfile,
12003 &dwp_file->sections.str_offsets,
73869dc2
DE
12004 sections.str_offsets_offset,
12005 sections.str_offsets_size);
12006 /* The "str" section is global to the entire DWP file. */
12007 dwo_file->sections.str = dwp_file->sections.str;
12008 /* The info or types section is assigned below to dwo_unit,
12009 there's no need to record it in dwo_file.
12010 Also, we can't simply record type sections in dwo_file because
12011 we record a pointer into the vector in dwo_unit. As we collect more
12012 types we'll grow the vector and eventually have to reallocate space
12013 for it, invalidating all copies of pointers into the previous
12014 contents. */
12015 *dwo_file_slot = dwo_file;
12016 }
12017 else
12018 {
b4f54984 12019 if (dwarf_read_debug)
73869dc2
DE
12020 {
12021 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12022 virtual_dwo_name.c_str ());
73869dc2 12023 }
9a3c8263 12024 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12025 }
73869dc2 12026
45940949 12027 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->obstack, struct dwo_unit);
73869dc2
DE
12028 dwo_unit->dwo_file = dwo_file;
12029 dwo_unit->signature = signature;
8d749320 12030 dwo_unit->section =
45940949 12031 XOBNEW (&dwarf2_per_objfile->obstack, struct dwarf2_section_info);
ed2dc618
SM
12032 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12033 is_debug_types
73869dc2
DE
12034 ? &dwp_file->sections.types
12035 : &dwp_file->sections.info,
12036 sections.info_or_types_offset,
12037 sections.info_or_types_size);
12038 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12039
12040 return dwo_unit;
12041}
12042
57d63ce2
DE
12043/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12044 Returns NULL if the signature isn't found. */
80626a55
DE
12045
12046static struct dwo_unit *
ed2dc618
SM
12047lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12048 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12049 ULONGEST signature, int is_debug_types)
80626a55 12050{
57d63ce2
DE
12051 const struct dwp_hash_table *dwp_htab =
12052 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12053 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12054 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12055 uint32_t hash = signature & mask;
12056 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12057 unsigned int i;
12058 void **slot;
870f88f7 12059 struct dwo_unit find_dwo_cu;
80626a55
DE
12060
12061 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12062 find_dwo_cu.signature = signature;
19ac8c2e 12063 slot = htab_find_slot (is_debug_types
48b490f2
TT
12064 ? dwp_file->loaded_tus.get ()
12065 : dwp_file->loaded_cus.get (),
19ac8c2e 12066 &find_dwo_cu, INSERT);
80626a55
DE
12067
12068 if (*slot != NULL)
9a3c8263 12069 return (struct dwo_unit *) *slot;
80626a55
DE
12070
12071 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12072 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12073 {
12074 ULONGEST signature_in_table;
12075
12076 signature_in_table =
57d63ce2 12077 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12078 if (signature_in_table == signature)
12079 {
57d63ce2
DE
12080 uint32_t unit_index =
12081 read_4_bytes (dbfd,
12082 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12083
73869dc2
DE
12084 if (dwp_file->version == 1)
12085 {
ed2dc618
SM
12086 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12087 dwp_file, unit_index,
73869dc2
DE
12088 comp_dir, signature,
12089 is_debug_types);
12090 }
12091 else
12092 {
ed2dc618
SM
12093 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12094 dwp_file, unit_index,
73869dc2
DE
12095 comp_dir, signature,
12096 is_debug_types);
12097 }
9a3c8263 12098 return (struct dwo_unit *) *slot;
80626a55
DE
12099 }
12100 if (signature_in_table == 0)
12101 return NULL;
12102 hash = (hash + hash2) & mask;
12103 }
12104
12105 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12106 " [in module %s]"),
12107 dwp_file->name);
12108}
12109
ab5088bf 12110/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12111 Open the file specified by FILE_NAME and hand it off to BFD for
12112 preliminary analysis. Return a newly initialized bfd *, which
12113 includes a canonicalized copy of FILE_NAME.
80626a55 12114 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12115 SEARCH_CWD is true if the current directory is to be searched.
12116 It will be searched before debug-file-directory.
13aaf454
DE
12117 If successful, the file is added to the bfd include table of the
12118 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12119 If unable to find/open the file, return NULL.
3019eac3
DE
12120 NOTE: This function is derived from symfile_bfd_open. */
12121
192b62ce 12122static gdb_bfd_ref_ptr
ed2dc618
SM
12123try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12124 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12125{
24b9144d 12126 int desc;
9c02c129
DE
12127 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12128 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12129 to debug_file_directory. */
e0cc99a6 12130 const char *search_path;
9c02c129
DE
12131 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12132
e0cc99a6 12133 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12134 if (search_cwd)
12135 {
12136 if (*debug_file_directory != '\0')
e0cc99a6
TT
12137 {
12138 search_path_holder.reset (concat (".", dirname_separator_string,
12139 debug_file_directory,
12140 (char *) NULL));
12141 search_path = search_path_holder.get ();
12142 }
6ac97d4c 12143 else
e0cc99a6 12144 search_path = ".";
6ac97d4c 12145 }
9c02c129 12146 else
e0cc99a6 12147 search_path = debug_file_directory;
3019eac3 12148
24b9144d 12149 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12150 if (is_dwp)
12151 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12152
12153 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12154 desc = openp (search_path, flags, file_name,
3019eac3
DE
12155 O_RDONLY | O_BINARY, &absolute_name);
12156 if (desc < 0)
12157 return NULL;
12158
e0cc99a6
TT
12159 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12160 gnutarget, desc));
9c02c129
DE
12161 if (sym_bfd == NULL)
12162 return NULL;
192b62ce 12163 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12164
192b62ce
TT
12165 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12166 return NULL;
3019eac3 12167
13aaf454
DE
12168 /* Success. Record the bfd as having been included by the objfile's bfd.
12169 This is important because things like demangled_names_hash lives in the
12170 objfile's per_bfd space and may have references to things like symbol
12171 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12172 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12173
3019eac3
DE
12174 return sym_bfd;
12175}
12176
ab5088bf 12177/* Try to open DWO file FILE_NAME.
3019eac3
DE
12178 COMP_DIR is the DW_AT_comp_dir attribute.
12179 The result is the bfd handle of the file.
12180 If there is a problem finding or opening the file, return NULL.
12181 Upon success, the canonicalized path of the file is stored in the bfd,
12182 same as symfile_bfd_open. */
12183
192b62ce 12184static gdb_bfd_ref_ptr
ed2dc618
SM
12185open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12186 const char *file_name, const char *comp_dir)
3019eac3 12187{
80626a55 12188 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12189 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12190 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12191
12192 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12193
12194 if (comp_dir != NULL)
12195 {
43816ebc
TT
12196 gdb::unique_xmalloc_ptr<char> path_to_try
12197 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12198
12199 /* NOTE: If comp_dir is a relative path, this will also try the
12200 search path, which seems useful. */
ed2dc618 12201 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12202 path_to_try.get (),
ed2dc618 12203 0 /*is_dwp*/,
192b62ce 12204 1 /*search_cwd*/));
3019eac3
DE
12205 if (abfd != NULL)
12206 return abfd;
12207 }
12208
12209 /* That didn't work, try debug-file-directory, which, despite its name,
12210 is a list of paths. */
12211
12212 if (*debug_file_directory == '\0')
12213 return NULL;
12214
ed2dc618
SM
12215 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12216 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12217}
12218
80626a55
DE
12219/* This function is mapped across the sections and remembers the offset and
12220 size of each of the DWO debugging sections we are interested in. */
12221
12222static void
12223dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12224{
9a3c8263 12225 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12226 const struct dwop_section_names *names = &dwop_section_names;
12227
12228 if (section_is_p (sectp->name, &names->abbrev_dwo))
12229 {
049412e3 12230 dwo_sections->abbrev.s.section = sectp;
fd361982 12231 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12232 }
12233 else if (section_is_p (sectp->name, &names->info_dwo))
12234 {
049412e3 12235 dwo_sections->info.s.section = sectp;
fd361982 12236 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12237 }
12238 else if (section_is_p (sectp->name, &names->line_dwo))
12239 {
049412e3 12240 dwo_sections->line.s.section = sectp;
fd361982 12241 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12242 }
12243 else if (section_is_p (sectp->name, &names->loc_dwo))
12244 {
049412e3 12245 dwo_sections->loc.s.section = sectp;
fd361982 12246 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12247 }
41144253 12248 else if (section_is_p (sectp->name, &names->loclists_dwo))
12249 {
12250 dwo_sections->loclists.s.section = sectp;
12251 dwo_sections->loclists.size = bfd_section_size (sectp);
12252 }
80626a55
DE
12253 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12254 {
049412e3 12255 dwo_sections->macinfo.s.section = sectp;
fd361982 12256 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12257 }
12258 else if (section_is_p (sectp->name, &names->macro_dwo))
12259 {
049412e3 12260 dwo_sections->macro.s.section = sectp;
fd361982 12261 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12262 }
12263 else if (section_is_p (sectp->name, &names->str_dwo))
12264 {
049412e3 12265 dwo_sections->str.s.section = sectp;
fd361982 12266 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12267 }
12268 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12269 {
049412e3 12270 dwo_sections->str_offsets.s.section = sectp;
fd361982 12271 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12272 }
12273 else if (section_is_p (sectp->name, &names->types_dwo))
12274 {
12275 struct dwarf2_section_info type_section;
12276
12277 memset (&type_section, 0, sizeof (type_section));
049412e3 12278 type_section.s.section = sectp;
fd361982 12279 type_section.size = bfd_section_size (sectp);
fd5866f6 12280 dwo_sections->types.push_back (type_section);
80626a55
DE
12281 }
12282}
12283
ab5088bf 12284/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12285 by PER_CU. This is for the non-DWP case.
80626a55 12286 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12287
12288static struct dwo_file *
0ac5b59e
DE
12289open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12290 const char *dwo_name, const char *comp_dir)
3019eac3 12291{
ed2dc618 12292 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12293
fb1eb2f9 12294 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12295 if (dbfd == NULL)
12296 {
b4f54984 12297 if (dwarf_read_debug)
80626a55
DE
12298 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12299 return NULL;
12300 }
263db9a1 12301
51ac9db5 12302 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12303 dwo_file->dwo_name = dwo_name;
12304 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12305 dwo_file->dbfd = std::move (dbfd);
3019eac3 12306
fb1eb2f9 12307 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12308 &dwo_file->sections);
3019eac3 12309
18a8505e
AT
12310 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12311 dwo_file->sections.info, dwo_file->cus);
3019eac3 12312
263db9a1 12313 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12314 dwo_file->sections.types, dwo_file->tus);
3019eac3 12315
b4f54984 12316 if (dwarf_read_debug)
80626a55
DE
12317 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12318
263db9a1 12319 return dwo_file.release ();
3019eac3
DE
12320}
12321
80626a55 12322/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12323 size of each of the DWP debugging sections common to version 1 and 2 that
12324 we are interested in. */
3019eac3 12325
80626a55 12326static void
73869dc2
DE
12327dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12328 void *dwp_file_ptr)
3019eac3 12329{
9a3c8263 12330 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12331 const struct dwop_section_names *names = &dwop_section_names;
12332 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12333
80626a55 12334 /* Record the ELF section number for later lookup: this is what the
73869dc2 12335 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12336 gdb_assert (elf_section_nr < dwp_file->num_sections);
12337 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12338
80626a55
DE
12339 /* Look for specific sections that we need. */
12340 if (section_is_p (sectp->name, &names->str_dwo))
12341 {
049412e3 12342 dwp_file->sections.str.s.section = sectp;
fd361982 12343 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12344 }
12345 else if (section_is_p (sectp->name, &names->cu_index))
12346 {
049412e3 12347 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12348 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12349 }
12350 else if (section_is_p (sectp->name, &names->tu_index))
12351 {
049412e3 12352 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12353 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12354 }
12355}
3019eac3 12356
73869dc2
DE
12357/* This function is mapped across the sections and remembers the offset and
12358 size of each of the DWP version 2 debugging sections that we are interested
12359 in. This is split into a separate function because we don't know if we
12360 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12361
12362static void
12363dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12364{
9a3c8263 12365 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12366 const struct dwop_section_names *names = &dwop_section_names;
12367 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12368
12369 /* Record the ELF section number for later lookup: this is what the
12370 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12371 gdb_assert (elf_section_nr < dwp_file->num_sections);
12372 dwp_file->elf_sections[elf_section_nr] = sectp;
12373
12374 /* Look for specific sections that we need. */
12375 if (section_is_p (sectp->name, &names->abbrev_dwo))
12376 {
049412e3 12377 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12378 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12379 }
12380 else if (section_is_p (sectp->name, &names->info_dwo))
12381 {
049412e3 12382 dwp_file->sections.info.s.section = sectp;
fd361982 12383 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12384 }
12385 else if (section_is_p (sectp->name, &names->line_dwo))
12386 {
049412e3 12387 dwp_file->sections.line.s.section = sectp;
fd361982 12388 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12389 }
12390 else if (section_is_p (sectp->name, &names->loc_dwo))
12391 {
049412e3 12392 dwp_file->sections.loc.s.section = sectp;
fd361982 12393 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12394 }
12395 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12396 {
049412e3 12397 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12398 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12399 }
12400 else if (section_is_p (sectp->name, &names->macro_dwo))
12401 {
049412e3 12402 dwp_file->sections.macro.s.section = sectp;
fd361982 12403 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12404 }
12405 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12406 {
049412e3 12407 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12408 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12409 }
12410 else if (section_is_p (sectp->name, &names->types_dwo))
12411 {
049412e3 12412 dwp_file->sections.types.s.section = sectp;
fd361982 12413 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12414 }
12415}
12416
80626a55 12417/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12418
80626a55
DE
12419static hashval_t
12420hash_dwp_loaded_cutus (const void *item)
12421{
9a3c8263 12422 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12423
80626a55
DE
12424 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12425 return dwo_unit->signature;
3019eac3
DE
12426}
12427
80626a55 12428/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12429
80626a55
DE
12430static int
12431eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12432{
9a3c8263
SM
12433 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12434 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12435
80626a55
DE
12436 return dua->signature == dub->signature;
12437}
3019eac3 12438
80626a55 12439/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12440
48b490f2 12441static htab_up
298e9637 12442allocate_dwp_loaded_cutus_table ()
80626a55 12443{
48b490f2
TT
12444 return htab_up (htab_create_alloc (3,
12445 hash_dwp_loaded_cutus,
12446 eq_dwp_loaded_cutus,
12447 NULL, xcalloc, xfree));
80626a55 12448}
3019eac3 12449
ab5088bf
DE
12450/* Try to open DWP file FILE_NAME.
12451 The result is the bfd handle of the file.
12452 If there is a problem finding or opening the file, return NULL.
12453 Upon success, the canonicalized path of the file is stored in the bfd,
12454 same as symfile_bfd_open. */
12455
192b62ce 12456static gdb_bfd_ref_ptr
ed2dc618
SM
12457open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12458 const char *file_name)
ab5088bf 12459{
ed2dc618
SM
12460 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12461 1 /*is_dwp*/,
192b62ce 12462 1 /*search_cwd*/));
6ac97d4c
DE
12463 if (abfd != NULL)
12464 return abfd;
12465
12466 /* Work around upstream bug 15652.
12467 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12468 [Whether that's a "bug" is debatable, but it is getting in our way.]
12469 We have no real idea where the dwp file is, because gdb's realpath-ing
12470 of the executable's path may have discarded the needed info.
12471 [IWBN if the dwp file name was recorded in the executable, akin to
12472 .gnu_debuglink, but that doesn't exist yet.]
12473 Strip the directory from FILE_NAME and search again. */
12474 if (*debug_file_directory != '\0')
12475 {
12476 /* Don't implicitly search the current directory here.
12477 If the user wants to search "." to handle this case,
12478 it must be added to debug-file-directory. */
ed2dc618
SM
12479 return try_open_dwop_file (dwarf2_per_objfile,
12480 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12481 0 /*search_cwd*/);
12482 }
12483
12484 return NULL;
ab5088bf
DE
12485}
12486
80626a55
DE
12487/* Initialize the use of the DWP file for the current objfile.
12488 By convention the name of the DWP file is ${objfile}.dwp.
12489 The result is NULL if it can't be found. */
a766d390 12490
400174b1 12491static std::unique_ptr<struct dwp_file>
ed2dc618 12492open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12493{
12494 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12495
82bf32bc
JK
12496 /* Try to find first .dwp for the binary file before any symbolic links
12497 resolving. */
6c447423
DE
12498
12499 /* If the objfile is a debug file, find the name of the real binary
12500 file and get the name of dwp file from there. */
d721ba37 12501 std::string dwp_name;
6c447423
DE
12502 if (objfile->separate_debug_objfile_backlink != NULL)
12503 {
12504 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12505 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12506
d721ba37 12507 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12508 }
12509 else
d721ba37
PA
12510 dwp_name = objfile->original_name;
12511
12512 dwp_name += ".dwp";
80626a55 12513
ed2dc618 12514 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12515 if (dbfd == NULL
12516 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12517 {
12518 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12519 dwp_name = objfile_name (objfile);
12520 dwp_name += ".dwp";
ed2dc618 12521 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12522 }
12523
80626a55
DE
12524 if (dbfd == NULL)
12525 {
b4f54984 12526 if (dwarf_read_debug)
d721ba37 12527 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12528 return std::unique_ptr<dwp_file> ();
3019eac3 12529 }
400174b1
TT
12530
12531 const char *name = bfd_get_filename (dbfd.get ());
12532 std::unique_ptr<struct dwp_file> dwp_file
12533 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12534
0a0f4c01 12535 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12536 dwp_file->elf_sections =
45940949 12537 OBSTACK_CALLOC (&dwarf2_per_objfile->obstack,
80626a55
DE
12538 dwp_file->num_sections, asection *);
12539
400174b1
TT
12540 bfd_map_over_sections (dwp_file->dbfd.get (),
12541 dwarf2_locate_common_dwp_sections,
12542 dwp_file.get ());
80626a55 12543
400174b1
TT
12544 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12545 0);
80626a55 12546
400174b1
TT
12547 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12548 1);
80626a55 12549
73869dc2 12550 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12551 if (dwp_file->cus && dwp_file->tus
12552 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12553 {
12554 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12555 pretty bizarre. We use pulongest here because that's the established
4d65956b 12556 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12557 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12558 " TU version %s [in DWP file %s]"),
12559 pulongest (dwp_file->cus->version),
d721ba37 12560 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12561 }
08302ed2
DE
12562
12563 if (dwp_file->cus)
12564 dwp_file->version = dwp_file->cus->version;
12565 else if (dwp_file->tus)
12566 dwp_file->version = dwp_file->tus->version;
12567 else
12568 dwp_file->version = 2;
73869dc2
DE
12569
12570 if (dwp_file->version == 2)
400174b1
TT
12571 bfd_map_over_sections (dwp_file->dbfd.get (),
12572 dwarf2_locate_v2_dwp_sections,
12573 dwp_file.get ());
73869dc2 12574
298e9637
SM
12575 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12576 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12577
b4f54984 12578 if (dwarf_read_debug)
80626a55
DE
12579 {
12580 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12581 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12582 " %s CUs, %s TUs\n",
12583 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12584 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12585 }
12586
12587 return dwp_file;
3019eac3 12588}
c906108c 12589
ab5088bf
DE
12590/* Wrapper around open_and_init_dwp_file, only open it once. */
12591
12592static struct dwp_file *
ed2dc618 12593get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12594{
12595 if (! dwarf2_per_objfile->dwp_checked)
12596 {
ed2dc618
SM
12597 dwarf2_per_objfile->dwp_file
12598 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12599 dwarf2_per_objfile->dwp_checked = 1;
12600 }
400174b1 12601 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12602}
12603
80626a55
DE
12604/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12605 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12606 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12607 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12608 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12609
12610 This is called, for example, when wanting to read a variable with a
12611 complex location. Therefore we don't want to do file i/o for every call.
12612 Therefore we don't want to look for a DWO file on every call.
12613 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12614 then we check if we've already seen DWO_NAME, and only THEN do we check
12615 for a DWO file.
12616
1c658ad5 12617 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12618 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12619
3019eac3 12620static struct dwo_unit *
80626a55
DE
12621lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12622 const char *dwo_name, const char *comp_dir,
12623 ULONGEST signature, int is_debug_types)
3019eac3 12624{
ed2dc618 12625 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12626 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12627 const char *kind = is_debug_types ? "TU" : "CU";
12628 void **dwo_file_slot;
3019eac3 12629 struct dwo_file *dwo_file;
80626a55 12630 struct dwp_file *dwp_file;
cb1df416 12631
6a506a2d
DE
12632 /* First see if there's a DWP file.
12633 If we have a DWP file but didn't find the DWO inside it, don't
12634 look for the original DWO file. It makes gdb behave differently
12635 depending on whether one is debugging in the build tree. */
cf2c3c16 12636
ed2dc618 12637 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12638 if (dwp_file != NULL)
cf2c3c16 12639 {
80626a55
DE
12640 const struct dwp_hash_table *dwp_htab =
12641 is_debug_types ? dwp_file->tus : dwp_file->cus;
12642
12643 if (dwp_htab != NULL)
12644 {
12645 struct dwo_unit *dwo_cutu =
ed2dc618 12646 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12647 signature, is_debug_types);
80626a55
DE
12648
12649 if (dwo_cutu != NULL)
12650 {
b4f54984 12651 if (dwarf_read_debug)
80626a55
DE
12652 {
12653 fprintf_unfiltered (gdb_stdlog,
12654 "Virtual DWO %s %s found: @%s\n",
12655 kind, hex_string (signature),
12656 host_address_to_string (dwo_cutu));
12657 }
12658 return dwo_cutu;
12659 }
12660 }
12661 }
6a506a2d 12662 else
80626a55 12663 {
6a506a2d 12664 /* No DWP file, look for the DWO file. */
80626a55 12665
ed2dc618
SM
12666 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12667 dwo_name, comp_dir);
6a506a2d 12668 if (*dwo_file_slot == NULL)
80626a55 12669 {
6a506a2d
DE
12670 /* Read in the file and build a table of the CUs/TUs it contains. */
12671 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12672 }
6a506a2d 12673 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12674 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12675
6a506a2d 12676 if (dwo_file != NULL)
19c3d4c9 12677 {
6a506a2d
DE
12678 struct dwo_unit *dwo_cutu = NULL;
12679
12680 if (is_debug_types && dwo_file->tus)
12681 {
12682 struct dwo_unit find_dwo_cutu;
12683
12684 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12685 find_dwo_cutu.signature = signature;
9a3c8263 12686 dwo_cutu
b0b6a987
TT
12687 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12688 &find_dwo_cutu);
6a506a2d 12689 }
33c5cd75 12690 else if (!is_debug_types && dwo_file->cus)
80626a55 12691 {
33c5cd75
DB
12692 struct dwo_unit find_dwo_cutu;
12693
12694 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12695 find_dwo_cutu.signature = signature;
b0b6a987 12696 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12697 &find_dwo_cutu);
6a506a2d
DE
12698 }
12699
12700 if (dwo_cutu != NULL)
12701 {
b4f54984 12702 if (dwarf_read_debug)
6a506a2d
DE
12703 {
12704 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12705 kind, dwo_name, hex_string (signature),
12706 host_address_to_string (dwo_cutu));
12707 }
12708 return dwo_cutu;
80626a55
DE
12709 }
12710 }
2e276125 12711 }
9cdd5dbd 12712
80626a55
DE
12713 /* We didn't find it. This could mean a dwo_id mismatch, or
12714 someone deleted the DWO/DWP file, or the search path isn't set up
12715 correctly to find the file. */
12716
b4f54984 12717 if (dwarf_read_debug)
80626a55
DE
12718 {
12719 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12720 kind, dwo_name, hex_string (signature));
12721 }
3019eac3 12722
6656a72d
DE
12723 /* This is a warning and not a complaint because it can be caused by
12724 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12725 {
12726 /* Print the name of the DWP file if we looked there, helps the user
12727 better diagnose the problem. */
791afaa2 12728 std::string dwp_text;
43942612
DE
12729
12730 if (dwp_file != NULL)
791afaa2
TT
12731 dwp_text = string_printf (" [in DWP file %s]",
12732 lbasename (dwp_file->name));
43942612 12733
9d8780f0 12734 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12735 " [in module %s]"),
12736 kind, dwo_name, hex_string (signature),
791afaa2 12737 dwp_text.c_str (),
43942612 12738 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12739 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12740 }
3019eac3 12741 return NULL;
5fb290d7
DJ
12742}
12743
80626a55
DE
12744/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12745 See lookup_dwo_cutu_unit for details. */
12746
12747static struct dwo_unit *
12748lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12749 const char *dwo_name, const char *comp_dir,
12750 ULONGEST signature)
12751{
12752 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12753}
12754
12755/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12756 See lookup_dwo_cutu_unit for details. */
12757
12758static struct dwo_unit *
12759lookup_dwo_type_unit (struct signatured_type *this_tu,
12760 const char *dwo_name, const char *comp_dir)
12761{
12762 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12763}
12764
89e63ee4
DE
12765/* Traversal function for queue_and_load_all_dwo_tus. */
12766
12767static int
12768queue_and_load_dwo_tu (void **slot, void *info)
12769{
12770 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12771 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12772 ULONGEST signature = dwo_unit->signature;
12773 struct signatured_type *sig_type =
12774 lookup_dwo_signatured_type (per_cu->cu, signature);
12775
12776 if (sig_type != NULL)
12777 {
12778 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12779
12780 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12781 a real dependency of PER_CU on SIG_TYPE. That is detected later
12782 while processing PER_CU. */
12783 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12784 load_full_type_unit (sig_cu);
ae640021 12785 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12786 }
12787
12788 return 1;
12789}
12790
12791/* Queue all TUs contained in the DWO of PER_CU to be read in.
12792 The DWO may have the only definition of the type, though it may not be
12793 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12794 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12795
12796static void
12797queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12798{
12799 struct dwo_unit *dwo_unit;
12800 struct dwo_file *dwo_file;
12801
12802 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12803 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12804 gdb_assert (per_cu->cu != NULL);
12805
12806 dwo_unit = per_cu->cu->dwo_unit;
12807 gdb_assert (dwo_unit != NULL);
12808
12809 dwo_file = dwo_unit->dwo_file;
12810 if (dwo_file->tus != NULL)
b0b6a987
TT
12811 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12812 per_cu);
89e63ee4
DE
12813}
12814
3019eac3 12815/* Read in various DIEs. */
348e048f 12816
d389af10 12817/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12818 Inherit only the children of the DW_AT_abstract_origin DIE not being
12819 already referenced by DW_AT_abstract_origin from the children of the
12820 current DIE. */
d389af10
JK
12821
12822static void
12823inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12824{
12825 struct die_info *child_die;
791afaa2 12826 sect_offset *offsetp;
d389af10
JK
12827 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12828 struct die_info *origin_die;
12829 /* Iterator of the ORIGIN_DIE children. */
12830 struct die_info *origin_child_die;
d389af10 12831 struct attribute *attr;
cd02d79d
PA
12832 struct dwarf2_cu *origin_cu;
12833 struct pending **origin_previous_list_in_scope;
d389af10
JK
12834
12835 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12836 if (!attr)
12837 return;
12838
cd02d79d
PA
12839 /* Note that following die references may follow to a die in a
12840 different cu. */
12841
12842 origin_cu = cu;
12843 origin_die = follow_die_ref (die, attr, &origin_cu);
12844
12845 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12846 symbols in. */
12847 origin_previous_list_in_scope = origin_cu->list_in_scope;
12848 origin_cu->list_in_scope = cu->list_in_scope;
12849
edb3359d
DJ
12850 if (die->tag != origin_die->tag
12851 && !(die->tag == DW_TAG_inlined_subroutine
12852 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12853 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12854 sect_offset_str (die->sect_off),
12855 sect_offset_str (origin_die->sect_off));
d389af10 12856
791afaa2 12857 std::vector<sect_offset> offsets;
d389af10 12858
3ea89b92
PMR
12859 for (child_die = die->child;
12860 child_die && child_die->tag;
436c571c 12861 child_die = child_die->sibling)
3ea89b92
PMR
12862 {
12863 struct die_info *child_origin_die;
12864 struct dwarf2_cu *child_origin_cu;
12865
12866 /* We are trying to process concrete instance entries:
216f72a1 12867 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12868 it's not relevant to our analysis here. i.e. detecting DIEs that are
12869 present in the abstract instance but not referenced in the concrete
12870 one. */
216f72a1
JK
12871 if (child_die->tag == DW_TAG_call_site
12872 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12873 continue;
12874
c38f313d
DJ
12875 /* For each CHILD_DIE, find the corresponding child of
12876 ORIGIN_DIE. If there is more than one layer of
12877 DW_AT_abstract_origin, follow them all; there shouldn't be,
12878 but GCC versions at least through 4.4 generate this (GCC PR
12879 40573). */
3ea89b92
PMR
12880 child_origin_die = child_die;
12881 child_origin_cu = cu;
c38f313d
DJ
12882 while (1)
12883 {
cd02d79d
PA
12884 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12885 child_origin_cu);
c38f313d
DJ
12886 if (attr == NULL)
12887 break;
cd02d79d
PA
12888 child_origin_die = follow_die_ref (child_origin_die, attr,
12889 &child_origin_cu);
c38f313d
DJ
12890 }
12891
d389af10
JK
12892 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12893 counterpart may exist. */
c38f313d 12894 if (child_origin_die != child_die)
d389af10 12895 {
edb3359d
DJ
12896 if (child_die->tag != child_origin_die->tag
12897 && !(child_die->tag == DW_TAG_inlined_subroutine
12898 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12899 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12900 "different tags"),
9d8780f0
SM
12901 sect_offset_str (child_die->sect_off),
12902 sect_offset_str (child_origin_die->sect_off));
c38f313d 12903 if (child_origin_die->parent != origin_die)
b98664d3 12904 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12905 "different parents"),
9d8780f0
SM
12906 sect_offset_str (child_die->sect_off),
12907 sect_offset_str (child_origin_die->sect_off));
c38f313d 12908 else
791afaa2 12909 offsets.push_back (child_origin_die->sect_off);
d389af10 12910 }
d389af10 12911 }
791afaa2
TT
12912 std::sort (offsets.begin (), offsets.end ());
12913 sect_offset *offsets_end = offsets.data () + offsets.size ();
12914 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12915 if (offsetp[-1] == *offsetp)
b98664d3 12916 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12917 "to DIE %s as their abstract origin"),
12918 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12919
791afaa2 12920 offsetp = offsets.data ();
d389af10
JK
12921 origin_child_die = origin_die->child;
12922 while (origin_child_die && origin_child_die->tag)
12923 {
12924 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12925 while (offsetp < offsets_end
9c541725 12926 && *offsetp < origin_child_die->sect_off)
d389af10 12927 offsetp++;
b64f50a1 12928 if (offsetp >= offsets_end
9c541725 12929 || *offsetp > origin_child_die->sect_off)
d389af10 12930 {
adde2bff
DE
12931 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12932 Check whether we're already processing ORIGIN_CHILD_DIE.
12933 This can happen with mutually referenced abstract_origins.
12934 PR 16581. */
12935 if (!origin_child_die->in_process)
12936 process_die (origin_child_die, origin_cu);
d389af10 12937 }
436c571c 12938 origin_child_die = origin_child_die->sibling;
d389af10 12939 }
cd02d79d 12940 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12941
12942 if (cu != origin_cu)
12943 compute_delayed_physnames (origin_cu);
d389af10
JK
12944}
12945
c906108c 12946static void
e7c27a73 12947read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12948{
518817b3 12949 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12950 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12951 struct context_stack *newobj;
c906108c
SS
12952 CORE_ADDR lowpc;
12953 CORE_ADDR highpc;
12954 struct die_info *child_die;
edb3359d 12955 struct attribute *attr, *call_line, *call_file;
15d034d0 12956 const char *name;
e142c38c 12957 CORE_ADDR baseaddr;
801e3a5b 12958 struct block *block;
edb3359d 12959 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12960 std::vector<struct symbol *> template_args;
34eaf542 12961 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12962
12963 if (inlined_func)
12964 {
12965 /* If we do not have call site information, we can't show the
12966 caller of this inlined function. That's too confusing, so
12967 only use the scope for local variables. */
12968 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12969 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12970 if (call_line == NULL || call_file == NULL)
12971 {
12972 read_lexical_block_scope (die, cu);
12973 return;
12974 }
12975 }
c906108c 12976
b3b3bada 12977 baseaddr = objfile->text_section_offset ();
e142c38c 12978
94af9270 12979 name = dwarf2_name (die, cu);
c906108c 12980
e8d05480
JB
12981 /* Ignore functions with missing or empty names. These are actually
12982 illegal according to the DWARF standard. */
12983 if (name == NULL)
12984 {
b98664d3 12985 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12986 sect_offset_str (die->sect_off));
e8d05480
JB
12987 return;
12988 }
12989
12990 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12991 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12992 <= PC_BOUNDS_INVALID)
e8d05480 12993 {
ae4d0c03
PM
12994 attr = dwarf2_attr (die, DW_AT_external, cu);
12995 if (!attr || !DW_UNSND (attr))
b98664d3 12996 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12997 "for subprogram DIE at %s"),
12998 sect_offset_str (die->sect_off));
e8d05480
JB
12999 return;
13000 }
c906108c 13001
3e29f34a
MR
13002 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13003 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13004
34eaf542
TT
13005 /* If we have any template arguments, then we must allocate a
13006 different sort of symbol. */
436c571c 13007 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13008 {
13009 if (child_die->tag == DW_TAG_template_type_param
13010 || child_die->tag == DW_TAG_template_value_param)
13011 {
8c14c3a3 13012 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13013 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13014 break;
13015 }
13016 }
13017
c24bdb02 13018 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13019 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13020 (struct symbol *) templ_func);
4c2df51b 13021
81873cc8 13022 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13023 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13024 cu->language);
13025
4cecd739
DJ
13026 /* If there is a location expression for DW_AT_frame_base, record
13027 it. */
e142c38c 13028 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13029 if (attr != nullptr)
fe978cb0 13030 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13031
63e43d3a
PMR
13032 /* If there is a location for the static link, record it. */
13033 newobj->static_link = NULL;
13034 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13035 if (attr != nullptr)
63e43d3a 13036 {
224c3ddb
SM
13037 newobj->static_link
13038 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13039 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13040 cu->per_cu->addr_type ());
63e43d3a
PMR
13041 }
13042
c24bdb02 13043 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13044
639d11d3 13045 if (die->child != NULL)
c906108c 13046 {
639d11d3 13047 child_die = die->child;
c906108c
SS
13048 while (child_die && child_die->tag)
13049 {
34eaf542
TT
13050 if (child_die->tag == DW_TAG_template_type_param
13051 || child_die->tag == DW_TAG_template_value_param)
13052 {
13053 struct symbol *arg = new_symbol (child_die, NULL, cu);
13054
f1078f66 13055 if (arg != NULL)
2f4732b0 13056 template_args.push_back (arg);
34eaf542
TT
13057 }
13058 else
13059 process_die (child_die, cu);
436c571c 13060 child_die = child_die->sibling;
c906108c
SS
13061 }
13062 }
13063
d389af10
JK
13064 inherit_abstract_dies (die, cu);
13065
4a811a97
UW
13066 /* If we have a DW_AT_specification, we might need to import using
13067 directives from the context of the specification DIE. See the
13068 comment in determine_prefix. */
13069 if (cu->language == language_cplus
13070 && dwarf2_attr (die, DW_AT_specification, cu))
13071 {
13072 struct dwarf2_cu *spec_cu = cu;
13073 struct die_info *spec_die = die_specification (die, &spec_cu);
13074
13075 while (spec_die)
13076 {
13077 child_die = spec_die->child;
13078 while (child_die && child_die->tag)
13079 {
13080 if (child_die->tag == DW_TAG_imported_module)
13081 process_die (child_die, spec_cu);
436c571c 13082 child_die = child_die->sibling;
4a811a97
UW
13083 }
13084
13085 /* In some cases, GCC generates specification DIEs that
13086 themselves contain DW_AT_specification attributes. */
13087 spec_die = die_specification (spec_die, &spec_cu);
13088 }
13089 }
13090
c24bdb02 13091 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13092 /* Make a block for the local symbols within. */
c24bdb02 13093 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13094 cstk.static_link, lowpc, highpc);
801e3a5b 13095
df8a16a1 13096 /* For C++, set the block's scope. */
45280282
IB
13097 if ((cu->language == language_cplus
13098 || cu->language == language_fortran
c44af4eb
TT
13099 || cu->language == language_d
13100 || cu->language == language_rust)
4d4ec4e5 13101 && cu->processing_has_namespace_info)
195a3f6c
TT
13102 block_set_scope (block, determine_prefix (die, cu),
13103 &objfile->objfile_obstack);
df8a16a1 13104
801e3a5b
JB
13105 /* If we have address ranges, record them. */
13106 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13107
a60f3166 13108 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13109
34eaf542 13110 /* Attach template arguments to function. */
2f4732b0 13111 if (!template_args.empty ())
34eaf542
TT
13112 {
13113 gdb_assert (templ_func != NULL);
13114
2f4732b0 13115 templ_func->n_template_arguments = template_args.size ();
34eaf542 13116 templ_func->template_arguments
8d749320
SM
13117 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13118 templ_func->n_template_arguments);
34eaf542 13119 memcpy (templ_func->template_arguments,
2f4732b0 13120 template_args.data (),
34eaf542 13121 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13122
13123 /* Make sure that the symtab is set on the new symbols. Even
13124 though they don't appear in this symtab directly, other parts
13125 of gdb assume that symbols do, and this is reasonably
13126 true. */
8634679f 13127 for (symbol *sym : template_args)
3e1d3d8c 13128 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13129 }
13130
208d8187
JB
13131 /* In C++, we can have functions nested inside functions (e.g., when
13132 a function declares a class that has methods). This means that
13133 when we finish processing a function scope, we may need to go
13134 back to building a containing block's symbol lists. */
c24bdb02
KS
13135 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13136 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13137
921e78cf
JB
13138 /* If we've finished processing a top-level function, subsequent
13139 symbols go in the file symbol list. */
c24bdb02
KS
13140 if (cu->get_builder ()->outermost_context_p ())
13141 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13142}
13143
13144/* Process all the DIES contained within a lexical block scope. Start
13145 a new scope, process the dies, and then close the scope. */
13146
13147static void
e7c27a73 13148read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13149{
518817b3 13150 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13151 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13152 CORE_ADDR lowpc, highpc;
13153 struct die_info *child_die;
e142c38c
DJ
13154 CORE_ADDR baseaddr;
13155
b3b3bada 13156 baseaddr = objfile->text_section_offset ();
c906108c
SS
13157
13158 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13159 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13160 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13161 be nasty. Might be easier to properly extend generic blocks to
af34e669 13162 describe ranges. */
e385593e
JK
13163 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13164 {
13165 case PC_BOUNDS_NOT_PRESENT:
13166 /* DW_TAG_lexical_block has no attributes, process its children as if
13167 there was no wrapping by that DW_TAG_lexical_block.
13168 GCC does no longer produces such DWARF since GCC r224161. */
13169 for (child_die = die->child;
13170 child_die != NULL && child_die->tag;
436c571c 13171 child_die = child_die->sibling)
4f7bc5ed
TT
13172 {
13173 /* We might already be processing this DIE. This can happen
13174 in an unusual circumstance -- where a subroutine A
13175 appears lexically in another subroutine B, but A actually
13176 inlines B. The recursion is broken here, rather than in
13177 inherit_abstract_dies, because it seems better to simply
13178 drop concrete children here. */
13179 if (!child_die->in_process)
13180 process_die (child_die, cu);
13181 }
e385593e
JK
13182 return;
13183 case PC_BOUNDS_INVALID:
13184 return;
13185 }
3e29f34a
MR
13186 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13187 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13188
c24bdb02 13189 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13190 if (die->child != NULL)
c906108c 13191 {
639d11d3 13192 child_die = die->child;
c906108c
SS
13193 while (child_die && child_die->tag)
13194 {
e7c27a73 13195 process_die (child_die, cu);
436c571c 13196 child_die = child_die->sibling;
c906108c
SS
13197 }
13198 }
3ea89b92 13199 inherit_abstract_dies (die, cu);
c24bdb02 13200 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13201
c24bdb02
KS
13202 if (*cu->get_builder ()->get_local_symbols () != NULL
13203 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13204 {
801e3a5b 13205 struct block *block
c24bdb02 13206 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13207 cstk.start_addr, highpc);
801e3a5b
JB
13208
13209 /* Note that recording ranges after traversing children, as we
13210 do here, means that recording a parent's ranges entails
13211 walking across all its children's ranges as they appear in
13212 the address map, which is quadratic behavior.
13213
13214 It would be nicer to record the parent's ranges before
13215 traversing its children, simply overriding whatever you find
13216 there. But since we don't even decide whether to create a
13217 block until after we've traversed its children, that's hard
13218 to do. */
13219 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13220 }
c24bdb02
KS
13221 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13222 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13223}
13224
216f72a1 13225/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13226
13227static void
13228read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13229{
518817b3 13230 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13231 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13232 CORE_ADDR pc, baseaddr;
13233 struct attribute *attr;
13234 struct call_site *call_site, call_site_local;
13235 void **slot;
13236 int nparams;
13237 struct die_info *child_die;
13238
b3b3bada 13239 baseaddr = objfile->text_section_offset ();
96408a79 13240
216f72a1
JK
13241 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13242 if (attr == NULL)
13243 {
13244 /* This was a pre-DWARF-5 GNU extension alias
13245 for DW_AT_call_return_pc. */
13246 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13247 }
96408a79
SA
13248 if (!attr)
13249 {
b98664d3 13250 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13251 "DIE %s [in module %s]"),
13252 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13253 return;
13254 }
cd6c91b4 13255 pc = attr->value_as_address () + baseaddr;
3e29f34a 13256 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13257
13258 if (cu->call_site_htab == NULL)
13259 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13260 NULL, &objfile->objfile_obstack,
13261 hashtab_obstack_allocate, NULL);
13262 call_site_local.pc = pc;
13263 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13264 if (*slot != NULL)
13265 {
b98664d3 13266 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13267 "DIE %s [in module %s]"),
13268 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13269 objfile_name (objfile));
96408a79
SA
13270 return;
13271 }
13272
13273 /* Count parameters at the caller. */
13274
13275 nparams = 0;
13276 for (child_die = die->child; child_die && child_die->tag;
436c571c 13277 child_die = child_die->sibling)
96408a79 13278 {
216f72a1
JK
13279 if (child_die->tag != DW_TAG_call_site_parameter
13280 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13281 {
b98664d3 13282 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13283 "DW_TAG_call_site child DIE %s [in module %s]"),
13284 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13285 objfile_name (objfile));
96408a79
SA
13286 continue;
13287 }
13288
13289 nparams++;
13290 }
13291
224c3ddb
SM
13292 call_site
13293 = ((struct call_site *)
13294 obstack_alloc (&objfile->objfile_obstack,
13295 sizeof (*call_site)
13296 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13297 *slot = call_site;
13298 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13299 call_site->pc = pc;
13300
216f72a1
JK
13301 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13302 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13303 {
13304 struct die_info *func_die;
13305
13306 /* Skip also over DW_TAG_inlined_subroutine. */
13307 for (func_die = die->parent;
13308 func_die && func_die->tag != DW_TAG_subprogram
13309 && func_die->tag != DW_TAG_subroutine_type;
13310 func_die = func_die->parent);
13311
216f72a1
JK
13312 /* DW_AT_call_all_calls is a superset
13313 of DW_AT_call_all_tail_calls. */
96408a79 13314 if (func_die
216f72a1 13315 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13316 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13317 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13318 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13319 {
13320 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13321 not complete. But keep CALL_SITE for look ups via call_site_htab,
13322 both the initial caller containing the real return address PC and
13323 the final callee containing the current PC of a chain of tail
13324 calls do not need to have the tail call list complete. But any
13325 function candidate for a virtual tail call frame searched via
13326 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13327 determined unambiguously. */
13328 }
13329 else
13330 {
13331 struct type *func_type = NULL;
13332
13333 if (func_die)
13334 func_type = get_die_type (func_die, cu);
13335 if (func_type != NULL)
13336 {
78134374 13337 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13338
13339 /* Enlist this call site to the function. */
13340 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13341 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13342 }
13343 else
b98664d3 13344 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13345 "DIE %s [in module %s]"),
13346 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13347 }
13348 }
13349
216f72a1
JK
13350 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13351 if (attr == NULL)
13352 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13353 if (attr == NULL)
13354 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13355 if (attr == NULL)
216f72a1
JK
13356 {
13357 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13358 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13359 }
96408a79 13360 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13361 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13362 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13363 else if (attr->form_is_block ())
96408a79
SA
13364 {
13365 struct dwarf2_locexpr_baton *dlbaton;
13366
8d749320 13367 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13368 dlbaton->data = DW_BLOCK (attr)->data;
13369 dlbaton->size = DW_BLOCK (attr)->size;
13370 dlbaton->per_cu = cu->per_cu;
13371
13372 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13373 }
cd6c91b4 13374 else if (attr->form_is_ref ())
96408a79 13375 {
96408a79
SA
13376 struct dwarf2_cu *target_cu = cu;
13377 struct die_info *target_die;
13378
ac9ec31b 13379 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13380 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13381 if (die_is_declaration (target_die, target_cu))
13382 {
7d45c7c3 13383 const char *target_physname;
9112db09
JK
13384
13385 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13386 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13387 if (target_physname == NULL)
9112db09 13388 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13389 if (target_physname == NULL)
b98664d3 13390 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13391 "physname, for referencing DIE %s [in module %s]"),
13392 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13393 else
7d455152 13394 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13395 }
13396 else
13397 {
13398 CORE_ADDR lowpc;
13399
13400 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13401 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13402 <= PC_BOUNDS_INVALID)
b98664d3 13403 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13404 "low pc, for referencing DIE %s [in module %s]"),
13405 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13406 else
3e29f34a
MR
13407 {
13408 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13409 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13410 }
96408a79
SA
13411 }
13412 }
13413 else
b98664d3 13414 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13415 "block nor reference, for DIE %s [in module %s]"),
13416 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13417
13418 call_site->per_cu = cu->per_cu;
13419
13420 for (child_die = die->child;
13421 child_die && child_die->tag;
436c571c 13422 child_die = child_die->sibling)
96408a79 13423 {
96408a79 13424 struct call_site_parameter *parameter;
1788b2d3 13425 struct attribute *loc, *origin;
96408a79 13426
216f72a1
JK
13427 if (child_die->tag != DW_TAG_call_site_parameter
13428 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13429 {
13430 /* Already printed the complaint above. */
13431 continue;
13432 }
13433
13434 gdb_assert (call_site->parameter_count < nparams);
13435 parameter = &call_site->parameter[call_site->parameter_count];
13436
1788b2d3
JK
13437 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13438 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13439 register is contained in DW_AT_call_value. */
96408a79 13440
24c5c679 13441 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13442 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13443 if (origin == NULL)
13444 {
13445 /* This was a pre-DWARF-5 GNU extension alias
13446 for DW_AT_call_parameter. */
13447 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13448 }
cd6c91b4 13449 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13450 {
1788b2d3 13451 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13452
0826b30a 13453 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13454 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13455 {
13456 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13457 binding can be done only inside one CU. Such referenced DIE
13458 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13459 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13460 "DW_TAG_call_site child DIE %s [in module %s]"),
13461 sect_offset_str (child_die->sect_off),
9c541725 13462 objfile_name (objfile));
d76b7dbc
JK
13463 continue;
13464 }
9c541725
PA
13465 parameter->u.param_cu_off
13466 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13467 }
4fc6c0d5 13468 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13469 {
b98664d3 13470 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13471 "DW_TAG_call_site child DIE %s [in module %s]"),
13472 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13473 continue;
13474 }
24c5c679 13475 else
96408a79 13476 {
24c5c679
JK
13477 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13478 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13479 if (parameter->u.dwarf_reg != -1)
13480 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13481 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13482 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13483 &parameter->u.fb_offset))
13484 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13485 else
13486 {
b98664d3 13487 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13488 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13489 "DW_TAG_call_site child DIE %s "
24c5c679 13490 "[in module %s]"),
9d8780f0 13491 sect_offset_str (child_die->sect_off),
9c541725 13492 objfile_name (objfile));
24c5c679
JK
13493 continue;
13494 }
96408a79
SA
13495 }
13496
216f72a1
JK
13497 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13498 if (attr == NULL)
13499 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13500 if (attr == NULL || !attr->form_is_block ())
96408a79 13501 {
b98664d3 13502 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13503 "DW_TAG_call_site child DIE %s [in module %s]"),
13504 sect_offset_str (child_die->sect_off),
9c541725 13505 objfile_name (objfile));
96408a79
SA
13506 continue;
13507 }
13508 parameter->value = DW_BLOCK (attr)->data;
13509 parameter->value_size = DW_BLOCK (attr)->size;
13510
13511 /* Parameters are not pre-cleared by memset above. */
13512 parameter->data_value = NULL;
13513 parameter->data_value_size = 0;
13514 call_site->parameter_count++;
13515
216f72a1
JK
13516 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13517 if (attr == NULL)
13518 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13519 if (attr != nullptr)
96408a79 13520 {
4fc6c0d5 13521 if (!attr->form_is_block ())
b98664d3 13522 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13523 "DW_TAG_call_site child DIE %s [in module %s]"),
13524 sect_offset_str (child_die->sect_off),
9c541725 13525 objfile_name (objfile));
96408a79
SA
13526 else
13527 {
13528 parameter->data_value = DW_BLOCK (attr)->data;
13529 parameter->data_value_size = DW_BLOCK (attr)->size;
13530 }
13531 }
13532 }
13533}
13534
71a3c369
TT
13535/* Helper function for read_variable. If DIE represents a virtual
13536 table, then return the type of the concrete object that is
13537 associated with the virtual table. Otherwise, return NULL. */
13538
13539static struct type *
13540rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13541{
13542 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13543 if (attr == NULL)
13544 return NULL;
13545
13546 /* Find the type DIE. */
13547 struct die_info *type_die = NULL;
13548 struct dwarf2_cu *type_cu = cu;
13549
cd6c91b4 13550 if (attr->form_is_ref ())
71a3c369
TT
13551 type_die = follow_die_ref (die, attr, &type_cu);
13552 if (type_die == NULL)
13553 return NULL;
13554
13555 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13556 return NULL;
13557 return die_containing_type (type_die, type_cu);
13558}
13559
13560/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13561
13562static void
13563read_variable (struct die_info *die, struct dwarf2_cu *cu)
13564{
13565 struct rust_vtable_symbol *storage = NULL;
13566
13567 if (cu->language == language_rust)
13568 {
13569 struct type *containing_type = rust_containing_type (die, cu);
13570
13571 if (containing_type != NULL)
13572 {
518817b3 13573 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13574
8c14c3a3 13575 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13576 storage->concrete_type = containing_type;
cf724bc9 13577 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13578 }
13579 }
13580
e4a62c65
TV
13581 struct symbol *res = new_symbol (die, NULL, cu, storage);
13582 struct attribute *abstract_origin
13583 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13584 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13585 if (res == NULL && loc && abstract_origin)
13586 {
13587 /* We have a variable without a name, but with a location and an abstract
13588 origin. This may be a concrete instance of an abstract variable
13589 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13590 later. */
13591 struct dwarf2_cu *origin_cu = cu;
13592 struct die_info *origin_die
13593 = follow_die_ref (die, abstract_origin, &origin_cu);
13594 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13595 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13596 }
71a3c369
TT
13597}
13598
43988095
JK
13599/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13600 reading .debug_rnglists.
13601 Callback's type should be:
13602 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13603 Return true if the attributes are present and valid, otherwise,
13604 return false. */
13605
13606template <typename Callback>
13607static bool
13608dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13609 Callback &&callback)
13610{
ed2dc618 13611 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13612 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13613 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13614 bfd *obfd = objfile->obfd;
43988095 13615 /* Base address selection entry. */
2b24b6e4 13616 gdb::optional<CORE_ADDR> base;
43988095 13617 const gdb_byte *buffer;
43988095
JK
13618 CORE_ADDR baseaddr;
13619 bool overflow = false;
13620
43988095
JK
13621 base = cu->base_address;
13622
96b79293 13623 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13624 if (offset >= dwarf2_per_objfile->rnglists.size)
13625 {
b98664d3 13626 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13627 offset);
13628 return false;
13629 }
13630 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13631
b3b3bada 13632 baseaddr = objfile->text_section_offset ();
43988095
JK
13633
13634 while (1)
13635 {
7814882a
JK
13636 /* Initialize it due to a false compiler warning. */
13637 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13638 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13639 + dwarf2_per_objfile->rnglists.size);
13640 unsigned int bytes_read;
13641
13642 if (buffer == buf_end)
13643 {
13644 overflow = true;
13645 break;
13646 }
13647 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13648 switch (rlet)
13649 {
13650 case DW_RLE_end_of_list:
13651 break;
13652 case DW_RLE_base_address:
13653 if (buffer + cu->header.addr_size > buf_end)
13654 {
13655 overflow = true;
13656 break;
13657 }
c8a7a66f 13658 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13659 buffer += bytes_read;
13660 break;
13661 case DW_RLE_start_length:
13662 if (buffer + cu->header.addr_size > buf_end)
13663 {
13664 overflow = true;
13665 break;
13666 }
c8a7a66f
TT
13667 range_beginning = cu->header.read_address (obfd, buffer,
13668 &bytes_read);
43988095
JK
13669 buffer += bytes_read;
13670 range_end = (range_beginning
13671 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13672 buffer += bytes_read;
13673 if (buffer > buf_end)
13674 {
13675 overflow = true;
13676 break;
13677 }
13678 break;
13679 case DW_RLE_offset_pair:
13680 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13681 buffer += bytes_read;
13682 if (buffer > buf_end)
13683 {
13684 overflow = true;
13685 break;
13686 }
13687 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13688 buffer += bytes_read;
13689 if (buffer > buf_end)
13690 {
13691 overflow = true;
13692 break;
13693 }
13694 break;
13695 case DW_RLE_start_end:
13696 if (buffer + 2 * cu->header.addr_size > buf_end)
13697 {
13698 overflow = true;
13699 break;
13700 }
c8a7a66f
TT
13701 range_beginning = cu->header.read_address (obfd, buffer,
13702 &bytes_read);
43988095 13703 buffer += bytes_read;
c8a7a66f 13704 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13705 buffer += bytes_read;
13706 break;
13707 default:
b98664d3 13708 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13709 return false;
13710 }
13711 if (rlet == DW_RLE_end_of_list || overflow)
13712 break;
13713 if (rlet == DW_RLE_base_address)
13714 continue;
13715
2b24b6e4 13716 if (!base.has_value ())
43988095
JK
13717 {
13718 /* We have no valid base address for the ranges
13719 data. */
b98664d3 13720 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13721 return false;
13722 }
13723
13724 if (range_beginning > range_end)
13725 {
13726 /* Inverted range entries are invalid. */
b98664d3 13727 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13728 return false;
13729 }
13730
13731 /* Empty range entries have no effect. */
13732 if (range_beginning == range_end)
13733 continue;
13734
2b24b6e4
TT
13735 range_beginning += *base;
13736 range_end += *base;
43988095
JK
13737
13738 /* A not-uncommon case of bad debug info.
13739 Don't pollute the addrmap with bad data. */
13740 if (range_beginning + baseaddr == 0
13741 && !dwarf2_per_objfile->has_section_at_zero)
13742 {
b98664d3 13743 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13744 " [in module %s]"), objfile_name (objfile));
13745 continue;
13746 }
13747
13748 callback (range_beginning, range_end);
13749 }
13750
13751 if (overflow)
13752 {
b98664d3 13753 complaint (_("Offset %d is not terminated "
43988095
JK
13754 "for DW_AT_ranges attribute"),
13755 offset);
13756 return false;
13757 }
13758
13759 return true;
13760}
13761
13762/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13763 Callback's type should be:
13764 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13765 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13766
43988095 13767template <typename Callback>
43039443 13768static int
5f46c5a5 13769dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13770 Callback &&callback)
43039443 13771{
ed2dc618 13772 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13773 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13774 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13775 struct comp_unit_head *cu_header = &cu->header;
13776 bfd *obfd = objfile->obfd;
13777 unsigned int addr_size = cu_header->addr_size;
13778 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13779 /* Base address selection entry. */
2b24b6e4 13780 gdb::optional<CORE_ADDR> base;
43039443 13781 unsigned int dummy;
d521ce57 13782 const gdb_byte *buffer;
ff013f42 13783 CORE_ADDR baseaddr;
43039443 13784
43988095
JK
13785 if (cu_header->version >= 5)
13786 return dwarf2_rnglists_process (offset, cu, callback);
13787
d00adf39 13788 base = cu->base_address;
43039443 13789
96b79293 13790 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13791 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13792 {
b98664d3 13793 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13794 offset);
13795 return 0;
13796 }
dce234bc 13797 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13798
b3b3bada 13799 baseaddr = objfile->text_section_offset ();
ff013f42 13800
43039443
JK
13801 while (1)
13802 {
13803 CORE_ADDR range_beginning, range_end;
13804
c8a7a66f 13805 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13806 buffer += addr_size;
c8a7a66f 13807 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13808 buffer += addr_size;
13809 offset += 2 * addr_size;
13810
13811 /* An end of list marker is a pair of zero addresses. */
13812 if (range_beginning == 0 && range_end == 0)
13813 /* Found the end of list entry. */
13814 break;
13815
13816 /* Each base address selection entry is a pair of 2 values.
13817 The first is the largest possible address, the second is
13818 the base address. Check for a base address here. */
13819 if ((range_beginning & mask) == mask)
13820 {
28d2bfb9
AB
13821 /* If we found the largest possible address, then we already
13822 have the base address in range_end. */
13823 base = range_end;
43039443
JK
13824 continue;
13825 }
13826
2b24b6e4 13827 if (!base.has_value ())
43039443
JK
13828 {
13829 /* We have no valid base address for the ranges
13830 data. */
b98664d3 13831 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13832 return 0;
13833 }
13834
9277c30c
UW
13835 if (range_beginning > range_end)
13836 {
13837 /* Inverted range entries are invalid. */
b98664d3 13838 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13839 return 0;
13840 }
13841
13842 /* Empty range entries have no effect. */
13843 if (range_beginning == range_end)
13844 continue;
13845
2b24b6e4
TT
13846 range_beginning += *base;
13847 range_end += *base;
43039443 13848
01093045
DE
13849 /* A not-uncommon case of bad debug info.
13850 Don't pollute the addrmap with bad data. */
13851 if (range_beginning + baseaddr == 0
13852 && !dwarf2_per_objfile->has_section_at_zero)
13853 {
b98664d3 13854 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13855 " [in module %s]"), objfile_name (objfile));
01093045
DE
13856 continue;
13857 }
13858
5f46c5a5
JK
13859 callback (range_beginning, range_end);
13860 }
13861
13862 return 1;
13863}
13864
13865/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13866 Return 1 if the attributes are present and valid, otherwise, return 0.
13867 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13868
13869static int
13870dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13871 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13872 dwarf2_psymtab *ranges_pst)
5f46c5a5 13873{
518817b3 13874 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13875 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13876 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13877 int low_set = 0;
13878 CORE_ADDR low = 0;
13879 CORE_ADDR high = 0;
13880 int retval;
13881
13882 retval = dwarf2_ranges_process (offset, cu,
13883 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13884 {
9277c30c 13885 if (ranges_pst != NULL)
3e29f34a
MR
13886 {
13887 CORE_ADDR lowpc;
13888 CORE_ADDR highpc;
13889
79748972
TT
13890 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13891 range_beginning + baseaddr)
13892 - baseaddr);
13893 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13894 range_end + baseaddr)
13895 - baseaddr);
d320c2b5
TT
13896 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13897 lowpc, highpc - 1, ranges_pst);
3e29f34a 13898 }
ff013f42 13899
43039443
JK
13900 /* FIXME: This is recording everything as a low-high
13901 segment of consecutive addresses. We should have a
13902 data structure for discontiguous block ranges
13903 instead. */
13904 if (! low_set)
13905 {
13906 low = range_beginning;
13907 high = range_end;
13908 low_set = 1;
13909 }
13910 else
13911 {
13912 if (range_beginning < low)
13913 low = range_beginning;
13914 if (range_end > high)
13915 high = range_end;
13916 }
5f46c5a5
JK
13917 });
13918 if (!retval)
13919 return 0;
43039443
JK
13920
13921 if (! low_set)
13922 /* If the first entry is an end-of-list marker, the range
13923 describes an empty scope, i.e. no instructions. */
13924 return 0;
13925
13926 if (low_return)
13927 *low_return = low;
13928 if (high_return)
13929 *high_return = high;
13930 return 1;
13931}
13932
3a2b436a
JK
13933/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13934 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13935 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13936
3a2b436a 13937static enum pc_bounds_kind
af34e669 13938dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13939 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13940 dwarf2_psymtab *pst)
c906108c 13941{
518817b3
SM
13942 struct dwarf2_per_objfile *dwarf2_per_objfile
13943 = cu->per_cu->dwarf2_per_objfile;
c906108c 13944 struct attribute *attr;
91da1414 13945 struct attribute *attr_high;
af34e669
DJ
13946 CORE_ADDR low = 0;
13947 CORE_ADDR high = 0;
e385593e 13948 enum pc_bounds_kind ret;
c906108c 13949
91da1414
MW
13950 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13951 if (attr_high)
af34e669 13952 {
e142c38c 13953 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13954 if (attr != nullptr)
91da1414 13955 {
cd6c91b4
TT
13956 low = attr->value_as_address ();
13957 high = attr_high->value_as_address ();
13958 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13959 high += low;
91da1414 13960 }
af34e669
DJ
13961 else
13962 /* Found high w/o low attribute. */
e385593e 13963 return PC_BOUNDS_INVALID;
af34e669
DJ
13964
13965 /* Found consecutive range of addresses. */
3a2b436a 13966 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13967 }
c906108c 13968 else
af34e669 13969 {
e142c38c 13970 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13971 if (attr != NULL)
13972 {
18a8505e 13973 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13974 We take advantage of the fact that DW_AT_ranges does not appear
13975 in DW_TAG_compile_unit of DWO files. */
13976 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13977 unsigned int ranges_offset = (DW_UNSND (attr)
13978 + (need_ranges_base
13979 ? cu->ranges_base
13980 : 0));
2e3cf129 13981
af34e669 13982 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13983 .debug_ranges section. */
2e3cf129 13984 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13985 return PC_BOUNDS_INVALID;
43039443 13986 /* Found discontinuous range of addresses. */
3a2b436a 13987 ret = PC_BOUNDS_RANGES;
af34e669 13988 }
e385593e
JK
13989 else
13990 return PC_BOUNDS_NOT_PRESENT;
af34e669 13991 }
c906108c 13992
48fbe735 13993 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13994 if (high <= low)
e385593e 13995 return PC_BOUNDS_INVALID;
c906108c
SS
13996
13997 /* When using the GNU linker, .gnu.linkonce. sections are used to
13998 eliminate duplicate copies of functions and vtables and such.
13999 The linker will arbitrarily choose one and discard the others.
14000 The AT_*_pc values for such functions refer to local labels in
14001 these sections. If the section from that file was discarded, the
14002 labels are not in the output, so the relocs get a value of 0.
14003 If this is a discarded function, mark the pc bounds as invalid,
14004 so that GDB will ignore it. */
72dca2f5 14005 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14006 return PC_BOUNDS_INVALID;
c906108c
SS
14007
14008 *lowpc = low;
96408a79
SA
14009 if (highpc)
14010 *highpc = high;
af34e669 14011 return ret;
c906108c
SS
14012}
14013
b084d499
JB
14014/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14015 its low and high PC addresses. Do nothing if these addresses could not
14016 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14017 and HIGHPC to the high address if greater than HIGHPC. */
14018
14019static void
14020dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14021 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14022 struct dwarf2_cu *cu)
14023{
14024 CORE_ADDR low, high;
14025 struct die_info *child = die->child;
14026
e385593e 14027 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14028 {
325fac50
PA
14029 *lowpc = std::min (*lowpc, low);
14030 *highpc = std::max (*highpc, high);
b084d499
JB
14031 }
14032
14033 /* If the language does not allow nested subprograms (either inside
14034 subprograms or lexical blocks), we're done. */
14035 if (cu->language != language_ada)
14036 return;
6e70227d 14037
b084d499
JB
14038 /* Check all the children of the given DIE. If it contains nested
14039 subprograms, then check their pc bounds. Likewise, we need to
14040 check lexical blocks as well, as they may also contain subprogram
14041 definitions. */
14042 while (child && child->tag)
14043 {
14044 if (child->tag == DW_TAG_subprogram
14045 || child->tag == DW_TAG_lexical_block)
14046 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14047 child = child->sibling;
b084d499
JB
14048 }
14049}
14050
fae299cd
DC
14051/* Get the low and high pc's represented by the scope DIE, and store
14052 them in *LOWPC and *HIGHPC. If the correct values can't be
14053 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14054
14055static void
14056get_scope_pc_bounds (struct die_info *die,
14057 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14058 struct dwarf2_cu *cu)
14059{
14060 CORE_ADDR best_low = (CORE_ADDR) -1;
14061 CORE_ADDR best_high = (CORE_ADDR) 0;
14062 CORE_ADDR current_low, current_high;
14063
3a2b436a 14064 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14065 >= PC_BOUNDS_RANGES)
fae299cd
DC
14066 {
14067 best_low = current_low;
14068 best_high = current_high;
14069 }
14070 else
14071 {
14072 struct die_info *child = die->child;
14073
14074 while (child && child->tag)
14075 {
14076 switch (child->tag) {
14077 case DW_TAG_subprogram:
b084d499 14078 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14079 break;
14080 case DW_TAG_namespace:
f55ee35c 14081 case DW_TAG_module:
fae299cd
DC
14082 /* FIXME: carlton/2004-01-16: Should we do this for
14083 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14084 that current GCC's always emit the DIEs corresponding
14085 to definitions of methods of classes as children of a
14086 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14087 the DIEs giving the declarations, which could be
14088 anywhere). But I don't see any reason why the
14089 standards says that they have to be there. */
14090 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14091
14092 if (current_low != ((CORE_ADDR) -1))
14093 {
325fac50
PA
14094 best_low = std::min (best_low, current_low);
14095 best_high = std::max (best_high, current_high);
fae299cd
DC
14096 }
14097 break;
14098 default:
0963b4bd 14099 /* Ignore. */
fae299cd
DC
14100 break;
14101 }
14102
436c571c 14103 child = child->sibling;
fae299cd
DC
14104 }
14105 }
14106
14107 *lowpc = best_low;
14108 *highpc = best_high;
14109}
14110
801e3a5b
JB
14111/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14112 in DIE. */
380bca97 14113
801e3a5b
JB
14114static void
14115dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14116 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14117{
518817b3 14118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14119 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14120 struct attribute *attr;
91da1414 14121 struct attribute *attr_high;
801e3a5b 14122
91da1414
MW
14123 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14124 if (attr_high)
801e3a5b 14125 {
801e3a5b 14126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14127 if (attr != nullptr)
801e3a5b 14128 {
cd6c91b4
TT
14129 CORE_ADDR low = attr->value_as_address ();
14130 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14131
cd6c91b4 14132 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14133 high += low;
9a619af0 14134
3e29f34a
MR
14135 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14136 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14137 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14138 }
14139 }
14140
14141 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14142 if (attr != nullptr)
801e3a5b 14143 {
18a8505e 14144 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14145 We take advantage of the fact that DW_AT_ranges does not appear
14146 in DW_TAG_compile_unit of DWO files. */
14147 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14148
14149 /* The value of the DW_AT_ranges attribute is the offset of the
14150 address range list in the .debug_ranges section. */
ab435259
DE
14151 unsigned long offset = (DW_UNSND (attr)
14152 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14153
2d5f09ec 14154 std::vector<blockrange> blockvec;
5f46c5a5
JK
14155 dwarf2_ranges_process (offset, cu,
14156 [&] (CORE_ADDR start, CORE_ADDR end)
14157 {
58fdfd2c
JK
14158 start += baseaddr;
14159 end += baseaddr;
5f46c5a5
JK
14160 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14161 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14162 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14163 blockvec.emplace_back (start, end);
5f46c5a5 14164 });
2d5f09ec
KB
14165
14166 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14167 }
14168}
14169
685b1105
JK
14170/* Check whether the producer field indicates either of GCC < 4.6, or the
14171 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14172
685b1105
JK
14173static void
14174check_producer (struct dwarf2_cu *cu)
60d5a603 14175{
38360086 14176 int major, minor;
60d5a603
JK
14177
14178 if (cu->producer == NULL)
14179 {
14180 /* For unknown compilers expect their behavior is DWARF version
14181 compliant.
14182
14183 GCC started to support .debug_types sections by -gdwarf-4 since
14184 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14185 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14186 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14187 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14188 }
b1ffba5a 14189 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14190 {
38360086
MW
14191 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14192 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14193 }
5230b05a 14194 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14195 {
14196 cu->producer_is_icc = true;
14197 cu->producer_is_icc_lt_14 = major < 14;
14198 }
c258c396
JD
14199 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14200 cu->producer_is_codewarrior = true;
685b1105
JK
14201 else
14202 {
14203 /* For other non-GCC compilers, expect their behavior is DWARF version
14204 compliant. */
60d5a603
JK
14205 }
14206
9068261f 14207 cu->checked_producer = true;
685b1105 14208}
ba919b58 14209
685b1105
JK
14210/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14211 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14212 during 4.6.0 experimental. */
14213
9068261f 14214static bool
685b1105
JK
14215producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14216{
14217 if (!cu->checked_producer)
14218 check_producer (cu);
14219
14220 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14221}
14222
c258c396
JD
14223
14224/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14225 with incorrect is_stmt attributes. */
14226
14227static bool
14228producer_is_codewarrior (struct dwarf2_cu *cu)
14229{
14230 if (!cu->checked_producer)
14231 check_producer (cu);
14232
14233 return cu->producer_is_codewarrior;
14234}
14235
405feb71 14236/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14237 DW_AT_accessibility. */
14238
14239static enum dwarf_access_attribute
14240dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14241{
14242 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14243 {
14244 /* The default DWARF 2 accessibility for members is public, the default
14245 accessibility for inheritance is private. */
14246
14247 if (die->tag != DW_TAG_inheritance)
14248 return DW_ACCESS_public;
14249 else
14250 return DW_ACCESS_private;
14251 }
14252 else
14253 {
14254 /* DWARF 3+ defines the default accessibility a different way. The same
14255 rules apply now for DW_TAG_inheritance as for the members and it only
14256 depends on the container kind. */
14257
14258 if (die->parent->tag == DW_TAG_class_type)
14259 return DW_ACCESS_private;
14260 else
14261 return DW_ACCESS_public;
14262 }
14263}
14264
74ac6d43
TT
14265/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14266 offset. If the attribute was not found return 0, otherwise return
14267 1. If it was found but could not properly be handled, set *OFFSET
14268 to 0. */
14269
14270static int
14271handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14272 LONGEST *offset)
14273{
14274 struct attribute *attr;
14275
14276 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14277 if (attr != NULL)
14278 {
14279 *offset = 0;
14280
14281 /* Note that we do not check for a section offset first here.
14282 This is because DW_AT_data_member_location is new in DWARF 4,
14283 so if we see it, we can assume that a constant form is really
14284 a constant and not a section offset. */
cd6c91b4 14285 if (attr->form_is_constant ())
0826b30a 14286 *offset = attr->constant_value (0);
cd6c91b4 14287 else if (attr->form_is_section_offset ())
74ac6d43 14288 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14289 else if (attr->form_is_block ())
74ac6d43
TT
14290 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14291 else
14292 dwarf2_complex_location_expr_complaint ();
14293
14294 return 1;
14295 }
14296
14297 return 0;
14298}
14299
7d79de9a
TT
14300/* Look for DW_AT_data_member_location and store the results in FIELD. */
14301
14302static void
14303handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14304 struct field *field)
14305{
14306 struct attribute *attr;
14307
14308 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14309 if (attr != NULL)
14310 {
14311 if (attr->form_is_constant ())
14312 {
14313 LONGEST offset = attr->constant_value (0);
14314 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14315 }
14316 else if (attr->form_is_section_offset ())
14317 dwarf2_complex_location_expr_complaint ();
14318 else if (attr->form_is_block ())
14319 {
14320 bool handled;
14321 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14322 if (handled)
14323 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14324 else
14325 {
14326 struct objfile *objfile
14327 = cu->per_cu->dwarf2_per_objfile->objfile;
14328 struct dwarf2_locexpr_baton *dlbaton
14329 = XOBNEW (&objfile->objfile_obstack,
14330 struct dwarf2_locexpr_baton);
14331 dlbaton->data = DW_BLOCK (attr)->data;
14332 dlbaton->size = DW_BLOCK (attr)->size;
14333 /* When using this baton, we want to compute the address
14334 of the field, not the value. This is why
14335 is_reference is set to false here. */
14336 dlbaton->is_reference = false;
14337 dlbaton->per_cu = cu->per_cu;
14338
14339 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14340 }
14341 }
14342 else
14343 dwarf2_complex_location_expr_complaint ();
14344 }
14345}
14346
c906108c
SS
14347/* Add an aggregate field to the field list. */
14348
14349static void
107d2387 14350dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14351 struct dwarf2_cu *cu)
6e70227d 14352{
518817b3 14353 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14354 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14355 struct nextfield *new_field;
14356 struct attribute *attr;
14357 struct field *fp;
15d034d0 14358 const char *fieldname = "";
c906108c 14359
7d0ccb61
DJ
14360 if (die->tag == DW_TAG_inheritance)
14361 {
be2daae6
TT
14362 fip->baseclasses.emplace_back ();
14363 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14364 }
14365 else
14366 {
be2daae6
TT
14367 fip->fields.emplace_back ();
14368 new_field = &fip->fields.back ();
7d0ccb61 14369 }
be2daae6 14370
9c6a1327
TT
14371 new_field->offset = die->sect_off;
14372
e142c38c 14373 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14374 if (attr != nullptr)
c906108c 14375 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14376 else
14377 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14378 if (new_field->accessibility != DW_ACCESS_public)
14379 fip->non_public_fields = 1;
60d5a603 14380
e142c38c 14381 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14382 if (attr != nullptr)
c906108c 14383 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14384 else
14385 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14386
14387 fp = &new_field->field;
a9a9bd0f 14388
e142c38c 14389 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14390 {
a9a9bd0f 14391 /* Data member other than a C++ static data member. */
6e70227d 14392
c906108c 14393 /* Get type of field. */
e7c27a73 14394 fp->type = die_type (die, cu);
c906108c 14395
d6a843b5 14396 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14397
c906108c 14398 /* Get bit size of field (zero if none). */
e142c38c 14399 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14400 if (attr != nullptr)
c906108c
SS
14401 {
14402 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14403 }
14404 else
14405 {
14406 FIELD_BITSIZE (*fp) = 0;
14407 }
14408
14409 /* Get bit offset of field. */
7d79de9a 14410 handle_data_member_location (die, cu, fp);
e142c38c 14411 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14412 if (attr != nullptr)
c906108c 14413 {
d5a22e77 14414 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14415 {
14416 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14417 additional bit offset from the MSB of the containing
14418 anonymous object to the MSB of the field. We don't
14419 have to do anything special since we don't need to
14420 know the size of the anonymous object. */
f41f5e61 14421 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14422 }
14423 else
14424 {
14425 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14426 MSB of the anonymous object, subtract off the number of
14427 bits from the MSB of the field to the MSB of the
14428 object, and then subtract off the number of bits of
14429 the field itself. The result is the bit offset of
14430 the LSB of the field. */
c906108c
SS
14431 int anonymous_size;
14432 int bit_offset = DW_UNSND (attr);
14433
e142c38c 14434 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14435 if (attr != nullptr)
c906108c
SS
14436 {
14437 /* The size of the anonymous object containing
14438 the bit field is explicit, so use the
14439 indicated size (in bytes). */
14440 anonymous_size = DW_UNSND (attr);
14441 }
14442 else
14443 {
14444 /* The size of the anonymous object containing
14445 the bit field must be inferred from the type
14446 attribute of the data member containing the
14447 bit field. */
14448 anonymous_size = TYPE_LENGTH (fp->type);
14449 }
f41f5e61
PA
14450 SET_FIELD_BITPOS (*fp,
14451 (FIELD_BITPOS (*fp)
14452 + anonymous_size * bits_per_byte
14453 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14454 }
14455 }
da5b30da
AA
14456 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14457 if (attr != NULL)
14458 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14459 + attr->constant_value (0)));
c906108c
SS
14460
14461 /* Get name of field. */
39cbfefa
DJ
14462 fieldname = dwarf2_name (die, cu);
14463 if (fieldname == NULL)
14464 fieldname = "";
d8151005
DJ
14465
14466 /* The name is already allocated along with this objfile, so we don't
14467 need to duplicate it for the type. */
14468 fp->name = fieldname;
c906108c
SS
14469
14470 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14471 pointer or virtual base class pointer) to private. */
e142c38c 14472 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14473 {
d48cc9dd 14474 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14475 new_field->accessibility = DW_ACCESS_private;
14476 fip->non_public_fields = 1;
14477 }
14478 }
a9a9bd0f 14479 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14480 {
a9a9bd0f
DC
14481 /* C++ static member. */
14482
14483 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14484 is a declaration, but all versions of G++ as of this writing
14485 (so through at least 3.2.1) incorrectly generate
14486 DW_TAG_variable tags. */
6e70227d 14487
ff355380 14488 const char *physname;
c906108c 14489
a9a9bd0f 14490 /* Get name of field. */
39cbfefa
DJ
14491 fieldname = dwarf2_name (die, cu);
14492 if (fieldname == NULL)
c906108c
SS
14493 return;
14494
254e6b9e 14495 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14496 if (attr
14497 /* Only create a symbol if this is an external value.
14498 new_symbol checks this and puts the value in the global symbol
14499 table, which we want. If it is not external, new_symbol
14500 will try to put the value in cu->list_in_scope which is wrong. */
14501 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14502 {
14503 /* A static const member, not much different than an enum as far as
14504 we're concerned, except that we can support more types. */
14505 new_symbol (die, NULL, cu);
14506 }
14507
2df3850c 14508 /* Get physical name. */
ff355380 14509 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14510
d8151005
DJ
14511 /* The name is already allocated along with this objfile, so we don't
14512 need to duplicate it for the type. */
14513 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14514 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14515 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14516 }
14517 else if (die->tag == DW_TAG_inheritance)
14518 {
74ac6d43 14519 /* C++ base class field. */
7d79de9a 14520 handle_data_member_location (die, cu, fp);
c906108c 14521 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14522 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14523 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14524 }
2ddeaf8a
TT
14525 else
14526 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14527}
14528
883fd55a
KS
14529/* Can the type given by DIE define another type? */
14530
14531static bool
14532type_can_define_types (const struct die_info *die)
14533{
14534 switch (die->tag)
14535 {
14536 case DW_TAG_typedef:
14537 case DW_TAG_class_type:
14538 case DW_TAG_structure_type:
14539 case DW_TAG_union_type:
14540 case DW_TAG_enumeration_type:
14541 return true;
14542
14543 default:
14544 return false;
14545 }
14546}
14547
14548/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14549
14550static void
883fd55a
KS
14551dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14552 struct dwarf2_cu *cu)
6e70227d 14553{
be2daae6
TT
14554 struct decl_field fp;
14555 memset (&fp, 0, sizeof (fp));
98751a41 14556
883fd55a 14557 gdb_assert (type_can_define_types (die));
98751a41 14558
883fd55a 14559 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14560 fp.name = dwarf2_name (die, cu);
14561 fp.type = read_type_die (die, cu);
98751a41 14562
c191a687
KS
14563 /* Save accessibility. */
14564 enum dwarf_access_attribute accessibility;
14565 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14566 if (attr != NULL)
14567 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14568 else
14569 accessibility = dwarf2_default_access_attribute (die, cu);
14570 switch (accessibility)
14571 {
14572 case DW_ACCESS_public:
14573 /* The assumed value if neither private nor protected. */
14574 break;
14575 case DW_ACCESS_private:
be2daae6 14576 fp.is_private = 1;
c191a687
KS
14577 break;
14578 case DW_ACCESS_protected:
be2daae6 14579 fp.is_protected = 1;
c191a687
KS
14580 break;
14581 default:
b98664d3 14582 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14583 }
14584
883fd55a 14585 if (die->tag == DW_TAG_typedef)
be2daae6 14586 fip->typedef_field_list.push_back (fp);
883fd55a 14587 else
be2daae6 14588 fip->nested_types_list.push_back (fp);
98751a41
JK
14589}
14590
9c6a1327
TT
14591/* A convenience typedef that's used when finding the discriminant
14592 field for a variant part. */
1b95cdb7
SM
14593typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14594 offset_map_type;
9c6a1327
TT
14595
14596/* Compute the discriminant range for a given variant. OBSTACK is
14597 where the results will be stored. VARIANT is the variant to
14598 process. IS_UNSIGNED indicates whether the discriminant is signed
14599 or unsigned. */
14600
14601static const gdb::array_view<discriminant_range>
14602convert_variant_range (struct obstack *obstack, const variant_field &variant,
14603 bool is_unsigned)
14604{
14605 std::vector<discriminant_range> ranges;
14606
14607 if (variant.default_branch)
14608 return {};
14609
14610 if (variant.discr_list_data == nullptr)
14611 {
14612 discriminant_range r
14613 = {variant.discriminant_value, variant.discriminant_value};
14614 ranges.push_back (r);
14615 }
14616 else
14617 {
14618 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14619 variant.discr_list_data->size);
14620 while (!data.empty ())
14621 {
14622 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14623 {
14624 complaint (_("invalid discriminant marker: %d"), data[0]);
14625 break;
14626 }
14627 bool is_range = data[0] == DW_DSC_range;
14628 data = data.slice (1);
14629
14630 ULONGEST low, high;
14631 unsigned int bytes_read;
14632
14633 if (data.empty ())
14634 {
14635 complaint (_("DW_AT_discr_list missing low value"));
14636 break;
14637 }
14638 if (is_unsigned)
14639 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14640 else
14641 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14642 &bytes_read);
14643 data = data.slice (bytes_read);
14644
14645 if (is_range)
14646 {
14647 if (data.empty ())
14648 {
14649 complaint (_("DW_AT_discr_list missing high value"));
14650 break;
14651 }
14652 if (is_unsigned)
14653 high = read_unsigned_leb128 (nullptr, data.data (),
14654 &bytes_read);
14655 else
14656 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14657 &bytes_read);
14658 data = data.slice (bytes_read);
14659 }
14660 else
14661 high = low;
14662
14663 ranges.push_back ({ low, high });
14664 }
14665 }
14666
14667 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14668 ranges.size ());
14669 std::copy (ranges.begin (), ranges.end (), result);
14670 return gdb::array_view<discriminant_range> (result, ranges.size ());
14671}
14672
14673static const gdb::array_view<variant_part> create_variant_parts
14674 (struct obstack *obstack,
14675 const offset_map_type &offset_map,
14676 struct field_info *fi,
14677 const std::vector<variant_part_builder> &variant_parts);
14678
14679/* Fill in a "struct variant" for a given variant field. RESULT is
14680 the variant to fill in. OBSTACK is where any needed allocations
14681 will be done. OFFSET_MAP holds the mapping from section offsets to
14682 fields for the type. FI describes the fields of the type we're
14683 processing. FIELD is the variant field we're converting. */
14684
14685static void
14686create_one_variant (variant &result, struct obstack *obstack,
14687 const offset_map_type &offset_map,
14688 struct field_info *fi, const variant_field &field)
14689{
14690 result.discriminants = convert_variant_range (obstack, field, false);
14691 result.first_field = field.first_field + fi->baseclasses.size ();
14692 result.last_field = field.last_field + fi->baseclasses.size ();
14693 result.parts = create_variant_parts (obstack, offset_map, fi,
14694 field.variant_parts);
14695}
14696
14697/* Fill in a "struct variant_part" for a given variant part. RESULT
14698 is the variant part to fill in. OBSTACK is where any needed
14699 allocations will be done. OFFSET_MAP holds the mapping from
14700 section offsets to fields for the type. FI describes the fields of
14701 the type we're processing. BUILDER is the variant part to be
14702 converted. */
14703
14704static void
14705create_one_variant_part (variant_part &result,
14706 struct obstack *obstack,
14707 const offset_map_type &offset_map,
14708 struct field_info *fi,
14709 const variant_part_builder &builder)
14710{
14711 auto iter = offset_map.find (builder.discriminant_offset);
14712 if (iter == offset_map.end ())
14713 {
14714 result.discriminant_index = -1;
14715 /* Doesn't matter. */
14716 result.is_unsigned = false;
14717 }
14718 else
14719 {
14720 result.discriminant_index = iter->second;
14721 result.is_unsigned
14722 = TYPE_UNSIGNED (FIELD_TYPE
14723 (fi->fields[result.discriminant_index].field));
14724 }
14725
14726 size_t n = builder.variants.size ();
14727 variant *output = new (obstack) variant[n];
14728 for (size_t i = 0; i < n; ++i)
14729 create_one_variant (output[i], obstack, offset_map, fi,
14730 builder.variants[i]);
14731
14732 result.variants = gdb::array_view<variant> (output, n);
14733}
14734
14735/* Create a vector of variant parts that can be attached to a type.
14736 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14737 holds the mapping from section offsets to fields for the type. FI
14738 describes the fields of the type we're processing. VARIANT_PARTS
14739 is the vector to convert. */
14740
14741static const gdb::array_view<variant_part>
14742create_variant_parts (struct obstack *obstack,
14743 const offset_map_type &offset_map,
14744 struct field_info *fi,
14745 const std::vector<variant_part_builder> &variant_parts)
14746{
14747 if (variant_parts.empty ())
14748 return {};
14749
14750 size_t n = variant_parts.size ();
14751 variant_part *result = new (obstack) variant_part[n];
14752 for (size_t i = 0; i < n; ++i)
14753 create_one_variant_part (result[i], obstack, offset_map, fi,
14754 variant_parts[i]);
14755
14756 return gdb::array_view<variant_part> (result, n);
14757}
14758
14759/* Compute the variant part vector for FIP, attaching it to TYPE when
14760 done. */
14761
14762static void
14763add_variant_property (struct field_info *fip, struct type *type,
14764 struct dwarf2_cu *cu)
14765{
14766 /* Map section offsets of fields to their field index. Note the
14767 field index here does not take the number of baseclasses into
14768 account. */
14769 offset_map_type offset_map;
14770 for (int i = 0; i < fip->fields.size (); ++i)
14771 offset_map[fip->fields[i].offset] = i;
14772
14773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14774 gdb::array_view<variant_part> parts
14775 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14776 fip->variant_parts);
14777
14778 struct dynamic_prop prop;
14779 prop.kind = PROP_VARIANT_PARTS;
14780 prop.data.variant_parts
14781 = ((gdb::array_view<variant_part> *)
14782 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14783
5c54719c 14784 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14785}
14786
c906108c
SS
14787/* Create the vector of fields, and attach it to the type. */
14788
14789static void
fba45db2 14790dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14791 struct dwarf2_cu *cu)
c906108c 14792{
317f7127 14793 int nfields = fip->nfields ();
c906108c
SS
14794
14795 /* Record the field count, allocate space for the array of fields,
14796 and create blank accessibility bitfields if necessary. */
5e33d5f4 14797 type->set_num_fields (nfields);
3cabb6b0
SM
14798 type->set_fields
14799 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14800
b4ba55a1 14801 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14802 {
14803 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14804
14805 TYPE_FIELD_PRIVATE_BITS (type) =
14806 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14807 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14808
14809 TYPE_FIELD_PROTECTED_BITS (type) =
14810 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14811 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14812
774b6a14
TT
14813 TYPE_FIELD_IGNORE_BITS (type) =
14814 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14815 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14816 }
14817
14818 /* If the type has baseclasses, allocate and clear a bit vector for
14819 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14820 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14821 {
be2daae6 14822 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14823 unsigned char *pointer;
c906108c
SS
14824
14825 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14826 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14827 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14828 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14829 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14830 }
14831
9c6a1327
TT
14832 if (!fip->variant_parts.empty ())
14833 add_variant_property (fip, type, cu);
2ddeaf8a 14834
be2daae6
TT
14835 /* Copy the saved-up fields into the field vector. */
14836 for (int i = 0; i < nfields; ++i)
c906108c 14837 {
be2daae6
TT
14838 struct nextfield &field
14839 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14840 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14841
ceacbf6e 14842 type->field (i) = field.field;
be2daae6 14843 switch (field.accessibility)
c906108c 14844 {
c5aa993b 14845 case DW_ACCESS_private:
b4ba55a1 14846 if (cu->language != language_ada)
be2daae6 14847 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14848 break;
c906108c 14849
c5aa993b 14850 case DW_ACCESS_protected:
b4ba55a1 14851 if (cu->language != language_ada)
be2daae6 14852 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14853 break;
c906108c 14854
c5aa993b
JM
14855 case DW_ACCESS_public:
14856 break;
c906108c 14857
c5aa993b
JM
14858 default:
14859 /* Unknown accessibility. Complain and treat it as public. */
14860 {
b98664d3 14861 complaint (_("unsupported accessibility %d"),
be2daae6 14862 field.accessibility);
c5aa993b
JM
14863 }
14864 break;
c906108c 14865 }
be2daae6 14866 if (i < fip->baseclasses.size ())
c906108c 14867 {
be2daae6 14868 switch (field.virtuality)
c906108c 14869 {
c5aa993b
JM
14870 case DW_VIRTUALITY_virtual:
14871 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14872 if (cu->language == language_ada)
a73c6dcd 14873 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14874 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14875 break;
c906108c
SS
14876 }
14877 }
c906108c
SS
14878 }
14879}
14880
7d27a96d
TT
14881/* Return true if this member function is a constructor, false
14882 otherwise. */
14883
14884static int
14885dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14886{
14887 const char *fieldname;
fe978cb0 14888 const char *type_name;
7d27a96d
TT
14889 int len;
14890
14891 if (die->parent == NULL)
14892 return 0;
14893
14894 if (die->parent->tag != DW_TAG_structure_type
14895 && die->parent->tag != DW_TAG_union_type
14896 && die->parent->tag != DW_TAG_class_type)
14897 return 0;
14898
14899 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14900 type_name = dwarf2_name (die->parent, cu);
14901 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14902 return 0;
14903
14904 len = strlen (fieldname);
fe978cb0
PA
14905 return (strncmp (fieldname, type_name, len) == 0
14906 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14907}
14908
e35000a7
TBA
14909/* Check if the given VALUE is a recognized enum
14910 dwarf_defaulted_attribute constant according to DWARF5 spec,
14911 Table 7.24. */
14912
14913static bool
14914is_valid_DW_AT_defaulted (ULONGEST value)
14915{
14916 switch (value)
14917 {
14918 case DW_DEFAULTED_no:
14919 case DW_DEFAULTED_in_class:
14920 case DW_DEFAULTED_out_of_class:
14921 return true;
14922 }
14923
3142e908 14924 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14925 return false;
14926}
14927
c906108c
SS
14928/* Add a member function to the proper fieldlist. */
14929
14930static void
107d2387 14931dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14932 struct type *type, struct dwarf2_cu *cu)
c906108c 14933{
518817b3 14934 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14935 struct attribute *attr;
c906108c 14936 int i;
be2daae6 14937 struct fnfieldlist *flp = nullptr;
c906108c 14938 struct fn_field *fnp;
15d034d0 14939 const char *fieldname;
f792889a 14940 struct type *this_type;
60d5a603 14941 enum dwarf_access_attribute accessibility;
c906108c 14942
b4ba55a1 14943 if (cu->language == language_ada)
a73c6dcd 14944 error (_("unexpected member function in Ada type"));
b4ba55a1 14945
2df3850c 14946 /* Get name of member function. */
39cbfefa
DJ
14947 fieldname = dwarf2_name (die, cu);
14948 if (fieldname == NULL)
2df3850c 14949 return;
c906108c 14950
c906108c 14951 /* Look up member function name in fieldlist. */
be2daae6 14952 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14953 {
27bfe10e 14954 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14955 {
14956 flp = &fip->fnfieldlists[i];
14957 break;
14958 }
c906108c
SS
14959 }
14960
be2daae6
TT
14961 /* Create a new fnfieldlist if necessary. */
14962 if (flp == nullptr)
c906108c 14963 {
be2daae6
TT
14964 fip->fnfieldlists.emplace_back ();
14965 flp = &fip->fnfieldlists.back ();
c906108c 14966 flp->name = fieldname;
be2daae6 14967 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14968 }
14969
be2daae6
TT
14970 /* Create a new member function field and add it to the vector of
14971 fnfieldlists. */
14972 flp->fnfields.emplace_back ();
14973 fnp = &flp->fnfields.back ();
3da10d80
KS
14974
14975 /* Delay processing of the physname until later. */
9c37b5ae 14976 if (cu->language == language_cplus)
be2daae6
TT
14977 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14978 die, cu);
3da10d80
KS
14979 else
14980 {
1d06ead6 14981 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14982 fnp->physname = physname ? physname : "";
14983 }
14984
c906108c 14985 fnp->type = alloc_type (objfile);
f792889a 14986 this_type = read_type_die (die, cu);
78134374 14987 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 14988 {
1f704f76 14989 int nparams = this_type->num_fields ();
c906108c 14990
f792889a 14991 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14992 of the method itself (TYPE_CODE_METHOD). */
14993 smash_to_method_type (fnp->type, type,
f792889a 14994 TYPE_TARGET_TYPE (this_type),
80fc5e77 14995 this_type->fields (),
1f704f76 14996 this_type->num_fields (),
f792889a 14997 TYPE_VARARGS (this_type));
c906108c
SS
14998
14999 /* Handle static member functions.
c5aa993b 15000 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15001 member functions. G++ helps GDB by marking the first
15002 parameter for non-static member functions (which is the this
15003 pointer) as artificial. We obtain this information from
15004 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15005 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15006 fnp->voffset = VOFFSET_STATIC;
15007 }
15008 else
b98664d3 15009 complaint (_("member function type missing for '%s'"),
3da10d80 15010 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15011
15012 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15013 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15014 fnp->fcontext = die_containing_type (die, cu);
c906108c 15015
3e43a32a
MS
15016 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15017 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15018
15019 /* Get accessibility. */
e142c38c 15020 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15021 if (attr != nullptr)
aead7601 15022 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15023 else
15024 accessibility = dwarf2_default_access_attribute (die, cu);
15025 switch (accessibility)
c906108c 15026 {
60d5a603
JK
15027 case DW_ACCESS_private:
15028 fnp->is_private = 1;
15029 break;
15030 case DW_ACCESS_protected:
15031 fnp->is_protected = 1;
15032 break;
c906108c
SS
15033 }
15034
b02dede2 15035 /* Check for artificial methods. */
e142c38c 15036 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15037 if (attr && DW_UNSND (attr) != 0)
15038 fnp->is_artificial = 1;
15039
e35000a7
TBA
15040 /* Check for defaulted methods. */
15041 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15042 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15043 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15044
15045 /* Check for deleted methods. */
15046 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15047 if (attr != nullptr && DW_UNSND (attr) != 0)
15048 fnp->is_deleted = 1;
15049
7d27a96d
TT
15050 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15051
0d564a31 15052 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15053 function. For older versions of GCC, this is an offset in the
15054 appropriate virtual table, as specified by DW_AT_containing_type.
15055 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15056 to the object address. */
15057
e142c38c 15058 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15059 if (attr != nullptr)
8e19ed76 15060 {
4fc6c0d5 15061 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15062 {
aec5aa8b
TT
15063 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15064 {
15065 /* Old-style GCC. */
15066 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15067 }
15068 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15069 || (DW_BLOCK (attr)->size > 1
15070 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15071 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15072 {
aec5aa8b
TT
15073 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15074 if ((fnp->voffset % cu->header.addr_size) != 0)
15075 dwarf2_complex_location_expr_complaint ();
15076 else
15077 fnp->voffset /= cu->header.addr_size;
15078 fnp->voffset += 2;
15079 }
15080 else
15081 dwarf2_complex_location_expr_complaint ();
15082
15083 if (!fnp->fcontext)
7e993ebf
KS
15084 {
15085 /* If there is no `this' field and no DW_AT_containing_type,
15086 we cannot actually find a base class context for the
15087 vtable! */
1f704f76 15088 if (this_type->num_fields () == 0
7e993ebf
KS
15089 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15090 {
b98664d3 15091 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15092 "function \"%s\" (offset %s)"),
15093 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15094 }
15095 else
15096 {
15097 fnp->fcontext
15098 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15099 }
15100 }
aec5aa8b 15101 }
cd6c91b4 15102 else if (attr->form_is_section_offset ())
8e19ed76 15103 {
4d3c2250 15104 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15105 }
15106 else
15107 {
4d3c2250
KB
15108 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15109 fieldname);
8e19ed76 15110 }
0d564a31 15111 }
d48cc9dd
DJ
15112 else
15113 {
15114 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15115 if (attr && DW_UNSND (attr))
15116 {
15117 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15118 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15119 "but the vtable offset is not specified"),
9d8780f0 15120 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15121 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15122 TYPE_CPLUS_DYNAMIC (type) = 1;
15123 }
15124 }
c906108c
SS
15125}
15126
15127/* Create the vector of member function fields, and attach it to the type. */
15128
15129static void
fba45db2 15130dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15131 struct dwarf2_cu *cu)
c906108c 15132{
b4ba55a1 15133 if (cu->language == language_ada)
a73c6dcd 15134 error (_("unexpected member functions in Ada type"));
b4ba55a1 15135
c906108c
SS
15136 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15137 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15138 TYPE_ALLOC (type,
15139 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15140
be2daae6 15141 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15142 {
be2daae6 15143 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15144 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15145
be2daae6
TT
15146 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15147 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15148 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15149 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15150
15151 for (int k = 0; k < nf.fnfields.size (); ++k)
15152 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15153 }
15154
be2daae6 15155 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15156}
15157
1168df01
JB
15158/* Returns non-zero if NAME is the name of a vtable member in CU's
15159 language, zero otherwise. */
15160static int
15161is_vtable_name (const char *name, struct dwarf2_cu *cu)
15162{
15163 static const char vptr[] = "_vptr";
15164
9c37b5ae
TT
15165 /* Look for the C++ form of the vtable. */
15166 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15167 return 1;
15168
15169 return 0;
15170}
15171
c0dd20ea 15172/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15173 functions, with the ABI-specified layout. If TYPE describes
15174 such a structure, smash it into a member function type.
61049d3b
DJ
15175
15176 GCC shouldn't do this; it should just output pointer to member DIEs.
15177 This is GCC PR debug/28767. */
c0dd20ea 15178
0b92b5bb
TT
15179static void
15180quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15181{
09e2d7c7 15182 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15183
15184 /* Check for a structure with no name and two children. */
1f704f76 15185 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15186 return;
c0dd20ea
DJ
15187
15188 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15189 if (TYPE_FIELD_NAME (type, 0) == NULL
15190 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15191 || TYPE_FIELD_NAME (type, 1) == NULL
15192 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15193 return;
c0dd20ea
DJ
15194
15195 /* Find the type of the method. */
0b92b5bb 15196 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15197 if (pfn_type == NULL
78134374
SM
15198 || pfn_type->code () != TYPE_CODE_PTR
15199 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15200 return;
c0dd20ea
DJ
15201
15202 /* Look for the "this" argument. */
15203 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15204 if (pfn_type->num_fields () == 0
0b92b5bb 15205 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15206 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15207 return;
c0dd20ea 15208
09e2d7c7 15209 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15210 new_type = alloc_type (objfile);
09e2d7c7 15211 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15212 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15213 TYPE_VARARGS (pfn_type));
0b92b5bb 15214 smash_to_methodptr_type (type, new_type);
c0dd20ea 15215}
1168df01 15216
2b4424c3
TT
15217/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15218 appropriate error checking and issuing complaints if there is a
15219 problem. */
15220
15221static ULONGEST
15222get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15223{
15224 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15225
15226 if (attr == nullptr)
15227 return 0;
15228
cd6c91b4 15229 if (!attr->form_is_constant ())
2b4424c3 15230 {
b98664d3 15231 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15232 " - DIE at %s [in module %s]"),
15233 sect_offset_str (die->sect_off),
15234 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15235 return 0;
15236 }
15237
15238 ULONGEST align;
15239 if (attr->form == DW_FORM_sdata)
15240 {
15241 LONGEST val = DW_SND (attr);
15242 if (val < 0)
15243 {
b98664d3 15244 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15245 " - DIE at %s [in module %s]"),
15246 sect_offset_str (die->sect_off),
15247 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15248 return 0;
15249 }
15250 align = val;
15251 }
15252 else
15253 align = DW_UNSND (attr);
15254
15255 if (align == 0)
15256 {
b98664d3 15257 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15258 " - DIE at %s [in module %s]"),
15259 sect_offset_str (die->sect_off),
15260 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15261 return 0;
15262 }
15263 if ((align & (align - 1)) != 0)
15264 {
b98664d3 15265 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15266 " - DIE at %s [in module %s]"),
15267 sect_offset_str (die->sect_off),
15268 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15269 return 0;
15270 }
15271
15272 return align;
15273}
15274
15275/* If the DIE has a DW_AT_alignment attribute, use its value to set
15276 the alignment for TYPE. */
15277
15278static void
15279maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15280 struct type *type)
15281{
15282 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15283 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15284 " - DIE at %s [in module %s]"),
15285 sect_offset_str (die->sect_off),
15286 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15287}
685b1105 15288
e35000a7
TBA
15289/* Check if the given VALUE is a valid enum dwarf_calling_convention
15290 constant for a type, according to DWARF5 spec, Table 5.5. */
15291
15292static bool
15293is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15294{
15295 switch (value)
15296 {
15297 case DW_CC_normal:
15298 case DW_CC_pass_by_reference:
15299 case DW_CC_pass_by_value:
15300 return true;
15301
15302 default:
15303 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15304 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15305 return false;
15306 }
15307}
15308
d0922fcf
TBA
15309/* Check if the given VALUE is a valid enum dwarf_calling_convention
15310 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15311 also according to GNU-specific values (see include/dwarf2.h). */
15312
15313static bool
15314is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15315{
15316 switch (value)
15317 {
15318 case DW_CC_normal:
15319 case DW_CC_program:
15320 case DW_CC_nocall:
15321 return true;
15322
15323 case DW_CC_GNU_renesas_sh:
15324 case DW_CC_GNU_borland_fastcall_i386:
15325 case DW_CC_GDB_IBM_OpenCL:
15326 return true;
15327
15328 default:
15329 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15330 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15331 return false;
15332 }
15333}
15334
c906108c 15335/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15336 (definition) to create a type for the structure or union. Fill in
15337 the type's name and general properties; the members will not be
83655187
DE
15338 processed until process_structure_scope. A symbol table entry for
15339 the type will also not be done until process_structure_scope (assuming
15340 the type has a name).
c906108c 15341
c767944b
DJ
15342 NOTE: we need to call these functions regardless of whether or not the
15343 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15344 structure or union. This gets the type entered into our set of
83655187 15345 user defined types. */
c906108c 15346
f792889a 15347static struct type *
134d01f1 15348read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15349{
518817b3 15350 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15351 struct type *type;
15352 struct attribute *attr;
15d034d0 15353 const char *name;
c906108c 15354
348e048f
DE
15355 /* If the definition of this type lives in .debug_types, read that type.
15356 Don't follow DW_AT_specification though, that will take us back up
15357 the chain and we want to go down. */
052c8bb8 15358 attr = die->attr (DW_AT_signature);
435d3d88 15359 if (attr != nullptr)
348e048f 15360 {
ac9ec31b 15361 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15362
ac9ec31b 15363 /* The type's CU may not be the same as CU.
02142a6c 15364 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15365 return set_die_type (die, type, cu);
15366 }
15367
c0dd20ea 15368 type = alloc_type (objfile);
c906108c 15369 INIT_CPLUS_SPECIFIC (type);
93311388 15370
39cbfefa
DJ
15371 name = dwarf2_name (die, cu);
15372 if (name != NULL)
c906108c 15373 {
987504bb 15374 if (cu->language == language_cplus
c44af4eb
TT
15375 || cu->language == language_d
15376 || cu->language == language_rust)
63d06c5c 15377 {
15d034d0 15378 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15379
15380 /* dwarf2_full_name might have already finished building the DIE's
15381 type. If so, there is no need to continue. */
15382 if (get_die_type (die, cu) != NULL)
15383 return get_die_type (die, cu);
15384
d0e39ea2 15385 type->set_name (full_name);
63d06c5c
DC
15386 }
15387 else
15388 {
d8151005
DJ
15389 /* The name is already allocated along with this objfile, so
15390 we don't need to duplicate it for the type. */
d0e39ea2 15391 type->set_name (name);
63d06c5c 15392 }
c906108c
SS
15393 }
15394
15395 if (die->tag == DW_TAG_structure_type)
15396 {
67607e24 15397 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15398 }
15399 else if (die->tag == DW_TAG_union_type)
15400 {
67607e24 15401 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15402 }
15403 else
15404 {
67607e24 15405 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15406 }
15407
0cc2414c
TT
15408 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15409 TYPE_DECLARED_CLASS (type) = 1;
15410
e35000a7
TBA
15411 /* Store the calling convention in the type if it's available in
15412 the die. Otherwise the calling convention remains set to
15413 the default value DW_CC_normal. */
15414 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15415 if (attr != nullptr
15416 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15417 {
15418 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15419 TYPE_CPLUS_CALLING_CONVENTION (type)
15420 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15421 }
15422
e142c38c 15423 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15424 if (attr != nullptr)
c906108c 15425 {
cd6c91b4 15426 if (attr->form_is_constant ())
155bfbd3
JB
15427 TYPE_LENGTH (type) = DW_UNSND (attr);
15428 else
15429 {
f8e89861
TT
15430 struct dynamic_prop prop;
15431 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15432 cu->per_cu->addr_type ()))
5c54719c 15433 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15434 TYPE_LENGTH (type) = 0;
15435 }
c906108c
SS
15436 }
15437 else
15438 {
15439 TYPE_LENGTH (type) = 0;
15440 }
15441
2b4424c3
TT
15442 maybe_set_alignment (cu, die, type);
15443
5230b05a 15444 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15445 {
5230b05a
WT
15446 /* ICC<14 does not output the required DW_AT_declaration on
15447 incomplete types, but gives them a size of zero. */
422b1cb0 15448 TYPE_STUB (type) = 1;
685b1105
JK
15449 }
15450 else
15451 TYPE_STUB_SUPPORTED (type) = 1;
15452
dc718098 15453 if (die_is_declaration (die, cu))
876cecd0 15454 TYPE_STUB (type) = 1;
a6c727b2
DJ
15455 else if (attr == NULL && die->child == NULL
15456 && producer_is_realview (cu->producer))
15457 /* RealView does not output the required DW_AT_declaration
15458 on incomplete types. */
15459 TYPE_STUB (type) = 1;
dc718098 15460
c906108c
SS
15461 /* We need to add the type field to the die immediately so we don't
15462 infinitely recurse when dealing with pointers to the structure
0963b4bd 15463 type within the structure itself. */
1c379e20 15464 set_die_type (die, type, cu);
c906108c 15465
7e314c57
JK
15466 /* set_die_type should be already done. */
15467 set_descriptive_type (type, die, cu);
15468
c767944b
DJ
15469 return type;
15470}
15471
9c6a1327
TT
15472static void handle_struct_member_die
15473 (struct die_info *child_die,
15474 struct type *type,
15475 struct field_info *fi,
15476 std::vector<struct symbol *> *template_args,
15477 struct dwarf2_cu *cu);
15478
15479/* A helper for handle_struct_member_die that handles
15480 DW_TAG_variant_part. */
15481
15482static void
15483handle_variant_part (struct die_info *die, struct type *type,
15484 struct field_info *fi,
15485 std::vector<struct symbol *> *template_args,
15486 struct dwarf2_cu *cu)
15487{
15488 variant_part_builder *new_part;
15489 if (fi->current_variant_part == nullptr)
15490 {
15491 fi->variant_parts.emplace_back ();
15492 new_part = &fi->variant_parts.back ();
15493 }
15494 else if (!fi->current_variant_part->processing_variant)
15495 {
15496 complaint (_("nested DW_TAG_variant_part seen "
15497 "- DIE at %s [in module %s]"),
15498 sect_offset_str (die->sect_off),
15499 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15500 return;
15501 }
15502 else
15503 {
15504 variant_field &current = fi->current_variant_part->variants.back ();
15505 current.variant_parts.emplace_back ();
15506 new_part = &current.variant_parts.back ();
15507 }
15508
15509 /* When we recurse, we want callees to add to this new variant
15510 part. */
15511 scoped_restore save_current_variant_part
15512 = make_scoped_restore (&fi->current_variant_part, new_part);
15513
15514 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15515 if (discr == NULL)
15516 {
15517 /* It's a univariant form, an extension we support. */
15518 }
15519 else if (discr->form_is_ref ())
15520 {
15521 struct dwarf2_cu *target_cu = cu;
15522 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15523
15524 new_part->discriminant_offset = target_die->sect_off;
15525 }
15526 else
15527 {
15528 complaint (_("DW_AT_discr does not have DIE reference form"
15529 " - DIE at %s [in module %s]"),
15530 sect_offset_str (die->sect_off),
15531 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15532 }
15533
15534 for (die_info *child_die = die->child;
15535 child_die != NULL;
15536 child_die = child_die->sibling)
15537 handle_struct_member_die (child_die, type, fi, template_args, cu);
15538}
15539
15540/* A helper for handle_struct_member_die that handles
15541 DW_TAG_variant. */
15542
15543static void
15544handle_variant (struct die_info *die, struct type *type,
15545 struct field_info *fi,
15546 std::vector<struct symbol *> *template_args,
15547 struct dwarf2_cu *cu)
15548{
15549 if (fi->current_variant_part == nullptr)
15550 {
15551 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15552 "- DIE at %s [in module %s]"),
15553 sect_offset_str (die->sect_off),
15554 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15555 return;
15556 }
15557 if (fi->current_variant_part->processing_variant)
15558 {
15559 complaint (_("nested DW_TAG_variant seen "
15560 "- DIE at %s [in module %s]"),
15561 sect_offset_str (die->sect_off),
15562 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15563 return;
15564 }
15565
15566 scoped_restore save_processing_variant
15567 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15568 true);
15569
15570 fi->current_variant_part->variants.emplace_back ();
15571 variant_field &variant = fi->current_variant_part->variants.back ();
15572 variant.first_field = fi->fields.size ();
15573
15574 /* In a variant we want to get the discriminant and also add a
15575 field for our sole member child. */
15576 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15577 if (discr == nullptr)
15578 {
15579 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15580 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15581 variant.default_branch = true;
15582 else
15583 variant.discr_list_data = DW_BLOCK (discr);
15584 }
15585 else
15586 variant.discriminant_value = DW_UNSND (discr);
15587
15588 for (die_info *variant_child = die->child;
15589 variant_child != NULL;
15590 variant_child = variant_child->sibling)
15591 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15592
15593 variant.last_field = fi->fields.size ();
15594}
15595
2ddeaf8a
TT
15596/* A helper for process_structure_scope that handles a single member
15597 DIE. */
15598
15599static void
15600handle_struct_member_die (struct die_info *child_die, struct type *type,
15601 struct field_info *fi,
15602 std::vector<struct symbol *> *template_args,
15603 struct dwarf2_cu *cu)
15604{
15605 if (child_die->tag == DW_TAG_member
9c6a1327 15606 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15607 {
15608 /* NOTE: carlton/2002-11-05: A C++ static data member
15609 should be a DW_TAG_member that is a declaration, but
15610 all versions of G++ as of this writing (so through at
15611 least 3.2.1) incorrectly generate DW_TAG_variable
15612 tags for them instead. */
15613 dwarf2_add_field (fi, child_die, cu);
15614 }
15615 else if (child_die->tag == DW_TAG_subprogram)
15616 {
15617 /* Rust doesn't have member functions in the C++ sense.
15618 However, it does emit ordinary functions as children
15619 of a struct DIE. */
15620 if (cu->language == language_rust)
15621 read_func_scope (child_die, cu);
15622 else
15623 {
15624 /* C++ member function. */
15625 dwarf2_add_member_fn (fi, child_die, type, cu);
15626 }
15627 }
15628 else if (child_die->tag == DW_TAG_inheritance)
15629 {
15630 /* C++ base class field. */
15631 dwarf2_add_field (fi, child_die, cu);
15632 }
15633 else if (type_can_define_types (child_die))
15634 dwarf2_add_type_defn (fi, child_die, cu);
15635 else if (child_die->tag == DW_TAG_template_type_param
15636 || child_die->tag == DW_TAG_template_value_param)
15637 {
15638 struct symbol *arg = new_symbol (child_die, NULL, cu);
15639
15640 if (arg != NULL)
15641 template_args->push_back (arg);
15642 }
9c6a1327
TT
15643 else if (child_die->tag == DW_TAG_variant_part)
15644 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15645 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15646 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15647}
15648
c767944b
DJ
15649/* Finish creating a structure or union type, including filling in
15650 its members and creating a symbol for it. */
15651
15652static void
15653process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15654{
518817b3 15655 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15656 struct die_info *child_die;
c767944b
DJ
15657 struct type *type;
15658
15659 type = get_die_type (die, cu);
15660 if (type == NULL)
15661 type = read_structure_type (die, cu);
15662
3e1d3d8c 15663 bool has_template_parameters = false;
e142c38c 15664 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15665 {
15666 struct field_info fi;
2f4732b0 15667 std::vector<struct symbol *> template_args;
c906108c 15668
639d11d3 15669 child_die = die->child;
c906108c
SS
15670
15671 while (child_die && child_die->tag)
15672 {
2ddeaf8a 15673 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15674 child_die = child_die->sibling;
c906108c
SS
15675 }
15676
34eaf542 15677 /* Attach template arguments to type. */
2f4732b0 15678 if (!template_args.empty ())
34eaf542 15679 {
3e1d3d8c 15680 has_template_parameters = true;
34eaf542 15681 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15682 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15683 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15684 = XOBNEWVEC (&objfile->objfile_obstack,
15685 struct symbol *,
15686 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15687 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15688 template_args.data (),
34eaf542
TT
15689 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15690 * sizeof (struct symbol *)));
34eaf542
TT
15691 }
15692
c906108c 15693 /* Attach fields and member functions to the type. */
317f7127 15694 if (fi.nfields () > 0)
e7c27a73 15695 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15696 if (!fi.fnfieldlists.empty ())
c906108c 15697 {
e7c27a73 15698 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15699
c5aa993b 15700 /* Get the type which refers to the base class (possibly this
c906108c 15701 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15702 class from the DW_AT_containing_type attribute. This use of
15703 DW_AT_containing_type is a GNU extension. */
c906108c 15704
e142c38c 15705 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15706 {
e7c27a73 15707 struct type *t = die_containing_type (die, cu);
c906108c 15708
ae6ae975 15709 set_type_vptr_basetype (type, t);
c906108c
SS
15710 if (type == t)
15711 {
c906108c
SS
15712 int i;
15713
15714 /* Our own class provides vtbl ptr. */
1f704f76 15715 for (i = t->num_fields () - 1;
c906108c
SS
15716 i >= TYPE_N_BASECLASSES (t);
15717 --i)
15718 {
0d5cff50 15719 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15720
1168df01 15721 if (is_vtable_name (fieldname, cu))
c906108c 15722 {
ae6ae975 15723 set_type_vptr_fieldno (type, i);
c906108c
SS
15724 break;
15725 }
15726 }
15727
15728 /* Complain if virtual function table field not found. */
15729 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15730 complaint (_("virtual function table pointer "
3e43a32a 15731 "not found when defining class '%s'"),
7d93a1e0 15732 type->name () ? type->name () : "");
c906108c
SS
15733 }
15734 else
15735 {
ae6ae975 15736 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15737 }
15738 }
f6235d4c 15739 else if (cu->producer
61012eef 15740 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15741 {
15742 /* The IBM XLC compiler does not provide direct indication
15743 of the containing type, but the vtable pointer is
15744 always named __vfp. */
15745
15746 int i;
15747
1f704f76 15748 for (i = type->num_fields () - 1;
f6235d4c
EZ
15749 i >= TYPE_N_BASECLASSES (type);
15750 --i)
15751 {
15752 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15753 {
ae6ae975
DE
15754 set_type_vptr_fieldno (type, i);
15755 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15756 break;
15757 }
15758 }
15759 }
c906108c 15760 }
98751a41
JK
15761
15762 /* Copy fi.typedef_field_list linked list elements content into the
15763 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15764 if (!fi.typedef_field_list.empty ())
98751a41 15765 {
be2daae6 15766 int count = fi.typedef_field_list.size ();
98751a41 15767
a0d7a4ff 15768 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15769 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15770 = ((struct decl_field *)
be2daae6
TT
15771 TYPE_ALLOC (type,
15772 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15773 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15774
be2daae6
TT
15775 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15776 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15777 }
c767944b 15778
883fd55a
KS
15779 /* Copy fi.nested_types_list linked list elements content into the
15780 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15781 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15782 {
be2daae6 15783 int count = fi.nested_types_list.size ();
883fd55a
KS
15784
15785 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15786 TYPE_NESTED_TYPES_ARRAY (type)
15787 = ((struct decl_field *)
be2daae6
TT
15788 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15789 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15790
be2daae6
TT
15791 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15792 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15793 }
c906108c 15794 }
63d06c5c 15795
bb5ed363 15796 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15797 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15798 cu->rust_unions.push_back (type);
0b92b5bb 15799
90aeadfc
DC
15800 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15801 snapshots) has been known to create a die giving a declaration
15802 for a class that has, as a child, a die giving a definition for a
15803 nested class. So we have to process our children even if the
15804 current die is a declaration. Normally, of course, a declaration
15805 won't have any children at all. */
134d01f1 15806
ca040673
DE
15807 child_die = die->child;
15808
90aeadfc
DC
15809 while (child_die != NULL && child_die->tag)
15810 {
15811 if (child_die->tag == DW_TAG_member
15812 || child_die->tag == DW_TAG_variable
34eaf542
TT
15813 || child_die->tag == DW_TAG_inheritance
15814 || child_die->tag == DW_TAG_template_value_param
15815 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15816 {
90aeadfc 15817 /* Do nothing. */
134d01f1 15818 }
90aeadfc
DC
15819 else
15820 process_die (child_die, cu);
134d01f1 15821
436c571c 15822 child_die = child_die->sibling;
134d01f1
DJ
15823 }
15824
fa4028e9
JB
15825 /* Do not consider external references. According to the DWARF standard,
15826 these DIEs are identified by the fact that they have no byte_size
15827 attribute, and a declaration attribute. */
15828 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15829 || !die_is_declaration (die, cu)
15830 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15831 {
15832 struct symbol *sym = new_symbol (die, type, cu);
15833
15834 if (has_template_parameters)
15835 {
a776957c
TT
15836 struct symtab *symtab;
15837 if (sym != nullptr)
15838 symtab = symbol_symtab (sym);
15839 else if (cu->line_header != nullptr)
15840 {
15841 /* Any related symtab will do. */
15842 symtab
7ba99d21 15843 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15844 }
15845 else
15846 {
15847 symtab = nullptr;
15848 complaint (_("could not find suitable "
15849 "symtab for template parameter"
15850 " - DIE at %s [in module %s]"),
15851 sect_offset_str (die->sect_off),
15852 objfile_name (objfile));
15853 }
15854
15855 if (symtab != nullptr)
15856 {
15857 /* Make sure that the symtab is set on the new symbols.
15858 Even though they don't appear in this symtab directly,
15859 other parts of gdb assume that symbols do, and this is
15860 reasonably true. */
15861 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15862 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15863 }
3e1d3d8c
TT
15864 }
15865 }
134d01f1
DJ
15866}
15867
ed6acedd
TT
15868/* Assuming DIE is an enumeration type, and TYPE is its associated
15869 type, update TYPE using some information only available in DIE's
15870 children. In particular, the fields are computed. */
55426c9d
JB
15871
15872static void
15873update_enumeration_type_from_children (struct die_info *die,
15874 struct type *type,
15875 struct dwarf2_cu *cu)
15876{
60f7655a 15877 struct die_info *child_die;
55426c9d
JB
15878 int unsigned_enum = 1;
15879 int flag_enum = 1;
55426c9d 15880
8268c778 15881 auto_obstack obstack;
ed6acedd 15882 std::vector<struct field> fields;
55426c9d 15883
60f7655a
DE
15884 for (child_die = die->child;
15885 child_die != NULL && child_die->tag;
436c571c 15886 child_die = child_die->sibling)
55426c9d
JB
15887 {
15888 struct attribute *attr;
15889 LONGEST value;
15890 const gdb_byte *bytes;
15891 struct dwarf2_locexpr_baton *baton;
15892 const char *name;
60f7655a 15893
55426c9d
JB
15894 if (child_die->tag != DW_TAG_enumerator)
15895 continue;
15896
15897 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15898 if (attr == NULL)
15899 continue;
15900
15901 name = dwarf2_name (child_die, cu);
15902 if (name == NULL)
15903 name = "<anonymous enumerator>";
15904
15905 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15906 &value, &bytes, &baton);
15907 if (value < 0)
15908 {
15909 unsigned_enum = 0;
15910 flag_enum = 0;
15911 }
55426c9d 15912 else
edd45eb0
SM
15913 {
15914 if (count_one_bits_ll (value) >= 2)
15915 flag_enum = 0;
edd45eb0 15916 }
55426c9d 15917
ed6acedd
TT
15918 fields.emplace_back ();
15919 struct field &field = fields.back ();
15920 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15921 SET_FIELD_ENUMVAL (field, value);
15922 }
15923
15924 if (!fields.empty ())
15925 {
5e33d5f4 15926 type->set_num_fields (fields.size ());
3cabb6b0
SM
15927 type->set_fields
15928 ((struct field *)
15929 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 15930 memcpy (type->fields (), fields.data (),
ed6acedd 15931 sizeof (struct field) * fields.size ());
55426c9d
JB
15932 }
15933
15934 if (unsigned_enum)
15935 TYPE_UNSIGNED (type) = 1;
15936 if (flag_enum)
15937 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15938}
15939
134d01f1
DJ
15940/* Given a DW_AT_enumeration_type die, set its type. We do not
15941 complete the type's fields yet, or create any symbols. */
c906108c 15942
f792889a 15943static struct type *
134d01f1 15944read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15945{
518817b3 15946 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15947 struct type *type;
c906108c 15948 struct attribute *attr;
0114d602 15949 const char *name;
134d01f1 15950
348e048f
DE
15951 /* If the definition of this type lives in .debug_types, read that type.
15952 Don't follow DW_AT_specification though, that will take us back up
15953 the chain and we want to go down. */
052c8bb8 15954 attr = die->attr (DW_AT_signature);
435d3d88 15955 if (attr != nullptr)
348e048f 15956 {
ac9ec31b 15957 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15958
ac9ec31b 15959 /* The type's CU may not be the same as CU.
02142a6c 15960 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15961 return set_die_type (die, type, cu);
15962 }
15963
c906108c
SS
15964 type = alloc_type (objfile);
15965
67607e24 15966 type->set_code (TYPE_CODE_ENUM);
94af9270 15967 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15968 if (name != NULL)
d0e39ea2 15969 type->set_name (name);
c906108c 15970
0626fc76
TT
15971 attr = dwarf2_attr (die, DW_AT_type, cu);
15972 if (attr != NULL)
15973 {
15974 struct type *underlying_type = die_type (die, cu);
15975
15976 TYPE_TARGET_TYPE (type) = underlying_type;
15977 }
15978
e142c38c 15979 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15980 if (attr != nullptr)
c906108c
SS
15981 {
15982 TYPE_LENGTH (type) = DW_UNSND (attr);
15983 }
15984 else
15985 {
15986 TYPE_LENGTH (type) = 0;
15987 }
15988
2b4424c3
TT
15989 maybe_set_alignment (cu, die, type);
15990
137033e9
JB
15991 /* The enumeration DIE can be incomplete. In Ada, any type can be
15992 declared as private in the package spec, and then defined only
15993 inside the package body. Such types are known as Taft Amendment
15994 Types. When another package uses such a type, an incomplete DIE
15995 may be generated by the compiler. */
02eb380e 15996 if (die_is_declaration (die, cu))
876cecd0 15997 TYPE_STUB (type) = 1;
02eb380e 15998
0626fc76
TT
15999 /* If this type has an underlying type that is not a stub, then we
16000 may use its attributes. We always use the "unsigned" attribute
16001 in this situation, because ordinarily we guess whether the type
16002 is unsigned -- but the guess can be wrong and the underlying type
16003 can tell us the reality. However, we defer to a local size
16004 attribute if one exists, because this lets the compiler override
16005 the underlying type if needed. */
16006 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16007 {
9e7c9a03
HD
16008 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16009 underlying_type = check_typedef (underlying_type);
16010 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16011 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16012 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16013 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16014 && TYPE_RAW_ALIGN (underlying_type) != 0)
16015 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16016 }
16017
3d567982
TT
16018 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16019
ed6acedd
TT
16020 set_die_type (die, type, cu);
16021
16022 /* Finish the creation of this type by using the enum's children.
16023 Note that, as usual, this must come after set_die_type to avoid
16024 infinite recursion when trying to compute the names of the
16025 enumerators. */
16026 update_enumeration_type_from_children (die, type, cu);
16027
16028 return type;
134d01f1
DJ
16029}
16030
16031/* Given a pointer to a die which begins an enumeration, process all
16032 the dies that define the members of the enumeration, and create the
16033 symbol for the enumeration type.
16034
16035 NOTE: We reverse the order of the element list. */
16036
16037static void
16038process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16039{
f792889a 16040 struct type *this_type;
134d01f1 16041
f792889a
DJ
16042 this_type = get_die_type (die, cu);
16043 if (this_type == NULL)
16044 this_type = read_enumeration_type (die, cu);
9dc481d3 16045
639d11d3 16046 if (die->child != NULL)
c906108c 16047 {
9dc481d3 16048 struct die_info *child_die;
15d034d0 16049 const char *name;
9dc481d3 16050
639d11d3 16051 child_die = die->child;
c906108c
SS
16052 while (child_die && child_die->tag)
16053 {
16054 if (child_die->tag != DW_TAG_enumerator)
16055 {
e7c27a73 16056 process_die (child_die, cu);
c906108c
SS
16057 }
16058 else
16059 {
39cbfefa
DJ
16060 name = dwarf2_name (child_die, cu);
16061 if (name)
ed6acedd 16062 new_symbol (child_die, this_type, cu);
c906108c
SS
16063 }
16064
436c571c 16065 child_die = child_die->sibling;
c906108c 16066 }
c906108c 16067 }
134d01f1 16068
6c83ed52
TT
16069 /* If we are reading an enum from a .debug_types unit, and the enum
16070 is a declaration, and the enum is not the signatured type in the
16071 unit, then we do not want to add a symbol for it. Adding a
16072 symbol would in some cases obscure the true definition of the
16073 enum, giving users an incomplete type when the definition is
16074 actually available. Note that we do not want to do this for all
16075 enums which are just declarations, because C++0x allows forward
16076 enum declarations. */
3019eac3 16077 if (cu->per_cu->is_debug_types
6c83ed52
TT
16078 && die_is_declaration (die, cu))
16079 {
52dc124a 16080 struct signatured_type *sig_type;
6c83ed52 16081
c0f78cd4 16082 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16083 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16084 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16085 return;
16086 }
16087
f792889a 16088 new_symbol (die, this_type, cu);
c906108c
SS
16089}
16090
16091/* Extract all information from a DW_TAG_array_type DIE and put it in
16092 the DIE's type field. For now, this only handles one dimensional
16093 arrays. */
16094
f792889a 16095static struct type *
e7c27a73 16096read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16097{
518817b3 16098 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16099 struct die_info *child_die;
7e314c57 16100 struct type *type;
c906108c 16101 struct type *element_type, *range_type, *index_type;
c906108c 16102 struct attribute *attr;
15d034d0 16103 const char *name;
a405673c 16104 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16105 unsigned int bit_stride = 0;
c906108c 16106
e7c27a73 16107 element_type = die_type (die, cu);
c906108c 16108
7e314c57
JK
16109 /* The die_type call above may have already set the type for this DIE. */
16110 type = get_die_type (die, cu);
16111 if (type)
16112 return type;
16113
dc53a7ad
JB
16114 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16115 if (attr != NULL)
a405673c
JB
16116 {
16117 int stride_ok;
09ba997f 16118 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16119
16120 byte_stride_prop
16121 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16122 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16123 prop_type);
a405673c
JB
16124 if (!stride_ok)
16125 {
b98664d3 16126 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16127 " - DIE at %s [in module %s]"),
16128 sect_offset_str (die->sect_off),
518817b3 16129 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16130 /* Ignore this attribute. We will likely not be able to print
16131 arrays of this type correctly, but there is little we can do
16132 to help if we cannot read the attribute's value. */
16133 byte_stride_prop = NULL;
16134 }
16135 }
dc53a7ad
JB
16136
16137 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16138 if (attr != NULL)
16139 bit_stride = DW_UNSND (attr);
16140
c906108c
SS
16141 /* Irix 6.2 native cc creates array types without children for
16142 arrays with unspecified length. */
639d11d3 16143 if (die->child == NULL)
c906108c 16144 {
46bf5051 16145 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16146 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16147 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16148 byte_stride_prop, bit_stride);
f792889a 16149 return set_die_type (die, type, cu);
c906108c
SS
16150 }
16151
791afaa2 16152 std::vector<struct type *> range_types;
639d11d3 16153 child_die = die->child;
c906108c
SS
16154 while (child_die && child_die->tag)
16155 {
16156 if (child_die->tag == DW_TAG_subrange_type)
16157 {
f792889a 16158 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16159
f792889a 16160 if (child_type != NULL)
a02abb62 16161 {
0963b4bd
MS
16162 /* The range type was succesfully read. Save it for the
16163 array type creation. */
791afaa2 16164 range_types.push_back (child_type);
a02abb62 16165 }
c906108c 16166 }
436c571c 16167 child_die = child_die->sibling;
c906108c
SS
16168 }
16169
16170 /* Dwarf2 dimensions are output from left to right, create the
16171 necessary array types in backwards order. */
7ca2d3a3 16172
c906108c 16173 type = element_type;
7ca2d3a3
DL
16174
16175 if (read_array_order (die, cu) == DW_ORD_col_major)
16176 {
16177 int i = 0;
9a619af0 16178
791afaa2 16179 while (i < range_types.size ())
dc53a7ad 16180 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16181 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16182 }
16183 else
16184 {
791afaa2 16185 size_t ndim = range_types.size ();
7ca2d3a3 16186 while (ndim-- > 0)
dc53a7ad 16187 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16188 byte_stride_prop, bit_stride);
7ca2d3a3 16189 }
c906108c 16190
f5f8a009
EZ
16191 /* Understand Dwarf2 support for vector types (like they occur on
16192 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16193 array type. This is not part of the Dwarf2/3 standard yet, but a
16194 custom vendor extension. The main difference between a regular
16195 array and the vector variant is that vectors are passed by value
16196 to functions. */
e142c38c 16197 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16198 if (attr != nullptr)
ea37ba09 16199 make_vector_type (type);
f5f8a009 16200
dbc98a8b
KW
16201 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16202 implementation may choose to implement triple vectors using this
16203 attribute. */
16204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16205 if (attr != nullptr)
dbc98a8b
KW
16206 {
16207 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16208 TYPE_LENGTH (type) = DW_UNSND (attr);
16209 else
b98664d3 16210 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16211 "than the total size of elements"));
dbc98a8b
KW
16212 }
16213
39cbfefa
DJ
16214 name = dwarf2_name (die, cu);
16215 if (name)
d0e39ea2 16216 type->set_name (name);
6e70227d 16217
2b4424c3
TT
16218 maybe_set_alignment (cu, die, type);
16219
0963b4bd 16220 /* Install the type in the die. */
7e314c57
JK
16221 set_die_type (die, type, cu);
16222
16223 /* set_die_type should be already done. */
b4ba55a1
JB
16224 set_descriptive_type (type, die, cu);
16225
7e314c57 16226 return type;
c906108c
SS
16227}
16228
7ca2d3a3 16229static enum dwarf_array_dim_ordering
6e70227d 16230read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16231{
16232 struct attribute *attr;
16233
16234 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16235
435d3d88 16236 if (attr != nullptr)
aead7601 16237 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16238
0963b4bd
MS
16239 /* GNU F77 is a special case, as at 08/2004 array type info is the
16240 opposite order to the dwarf2 specification, but data is still
16241 laid out as per normal fortran.
7ca2d3a3 16242
0963b4bd
MS
16243 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16244 version checking. */
7ca2d3a3 16245
905e0470
PM
16246 if (cu->language == language_fortran
16247 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16248 {
16249 return DW_ORD_row_major;
16250 }
16251
6e70227d 16252 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16253 {
16254 case array_column_major:
16255 return DW_ORD_col_major;
16256 case array_row_major:
16257 default:
16258 return DW_ORD_row_major;
16259 };
16260}
16261
72019c9c 16262/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16263 the DIE's type field. */
72019c9c 16264
f792889a 16265static struct type *
72019c9c
GM
16266read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16267{
7e314c57
JK
16268 struct type *domain_type, *set_type;
16269 struct attribute *attr;
f792889a 16270
7e314c57
JK
16271 domain_type = die_type (die, cu);
16272
16273 /* The die_type call above may have already set the type for this DIE. */
16274 set_type = get_die_type (die, cu);
16275 if (set_type)
16276 return set_type;
16277
16278 set_type = create_set_type (NULL, domain_type);
16279
16280 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16281 if (attr != nullptr)
d09039dd 16282 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16283
2b4424c3
TT
16284 maybe_set_alignment (cu, die, set_type);
16285
f792889a 16286 return set_die_type (die, set_type, cu);
72019c9c 16287}
7ca2d3a3 16288
0971de02
TT
16289/* A helper for read_common_block that creates a locexpr baton.
16290 SYM is the symbol which we are marking as computed.
16291 COMMON_DIE is the DIE for the common block.
16292 COMMON_LOC is the location expression attribute for the common
16293 block itself.
16294 MEMBER_LOC is the location expression attribute for the particular
16295 member of the common block that we are processing.
16296 CU is the CU from which the above come. */
16297
16298static void
16299mark_common_block_symbol_computed (struct symbol *sym,
16300 struct die_info *common_die,
16301 struct attribute *common_loc,
16302 struct attribute *member_loc,
16303 struct dwarf2_cu *cu)
16304{
518817b3
SM
16305 struct dwarf2_per_objfile *dwarf2_per_objfile
16306 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16307 struct objfile *objfile = dwarf2_per_objfile->objfile;
16308 struct dwarf2_locexpr_baton *baton;
16309 gdb_byte *ptr;
16310 unsigned int cu_off;
08feed99 16311 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16312 LONGEST offset = 0;
16313
16314 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16315 gdb_assert (common_loc->form_is_block ());
16316 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16317 || member_loc->form_is_constant ());
0971de02 16318
8d749320 16319 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16320 baton->per_cu = cu->per_cu;
16321 gdb_assert (baton->per_cu);
16322
16323 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16324
cd6c91b4 16325 if (member_loc->form_is_constant ())
0971de02 16326 {
0826b30a 16327 offset = member_loc->constant_value (0);
0971de02
TT
16328 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16329 }
16330 else
16331 baton->size += DW_BLOCK (member_loc)->size;
16332
224c3ddb 16333 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16334 baton->data = ptr;
16335
16336 *ptr++ = DW_OP_call4;
9c541725 16337 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16338 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16339 ptr += 4;
16340
cd6c91b4 16341 if (member_loc->form_is_constant ())
0971de02
TT
16342 {
16343 *ptr++ = DW_OP_addr;
16344 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16345 ptr += cu->header.addr_size;
16346 }
16347 else
16348 {
16349 /* We have to copy the data here, because DW_OP_call4 will only
16350 use a DW_AT_location attribute. */
16351 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16352 ptr += DW_BLOCK (member_loc)->size;
16353 }
16354
16355 *ptr++ = DW_OP_plus;
16356 gdb_assert (ptr - baton->data == baton->size);
16357
0971de02 16358 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16359 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16360}
16361
4357ac6c
TT
16362/* Create appropriate locally-scoped variables for all the
16363 DW_TAG_common_block entries. Also create a struct common_block
16364 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16365 is used to separate the common blocks name namespace from regular
4357ac6c 16366 variable names. */
c906108c
SS
16367
16368static void
e7c27a73 16369read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16370{
0971de02
TT
16371 struct attribute *attr;
16372
16373 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16374 if (attr != nullptr)
0971de02
TT
16375 {
16376 /* Support the .debug_loc offsets. */
4fc6c0d5 16377 if (attr->form_is_block ())
0971de02
TT
16378 {
16379 /* Ok. */
16380 }
cd6c91b4 16381 else if (attr->form_is_section_offset ())
0971de02
TT
16382 {
16383 dwarf2_complex_location_expr_complaint ();
16384 attr = NULL;
16385 }
16386 else
16387 {
16388 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16389 "common block member");
16390 attr = NULL;
16391 }
16392 }
16393
639d11d3 16394 if (die->child != NULL)
c906108c 16395 {
518817b3 16396 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16397 struct die_info *child_die;
16398 size_t n_entries = 0, size;
16399 struct common_block *common_block;
16400 struct symbol *sym;
74ac6d43 16401
4357ac6c
TT
16402 for (child_die = die->child;
16403 child_die && child_die->tag;
436c571c 16404 child_die = child_die->sibling)
4357ac6c
TT
16405 ++n_entries;
16406
16407 size = (sizeof (struct common_block)
16408 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16409 common_block
16410 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16411 size);
4357ac6c
TT
16412 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16413 common_block->n_entries = 0;
16414
16415 for (child_die = die->child;
16416 child_die && child_die->tag;
436c571c 16417 child_die = child_die->sibling)
4357ac6c
TT
16418 {
16419 /* Create the symbol in the DW_TAG_common_block block in the current
16420 symbol scope. */
e7c27a73 16421 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16422 if (sym != NULL)
16423 {
16424 struct attribute *member_loc;
16425
16426 common_block->contents[common_block->n_entries++] = sym;
16427
16428 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16429 cu);
16430 if (member_loc)
16431 {
16432 /* GDB has handled this for a long time, but it is
16433 not specified by DWARF. It seems to have been
16434 emitted by gfortran at least as recently as:
16435 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16436 complaint (_("Variable in common block has "
0971de02 16437 "DW_AT_data_member_location "
9d8780f0
SM
16438 "- DIE at %s [in module %s]"),
16439 sect_offset_str (child_die->sect_off),
518817b3 16440 objfile_name (objfile));
0971de02 16441
cd6c91b4 16442 if (member_loc->form_is_section_offset ())
0971de02 16443 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16444 else if (member_loc->form_is_constant ()
4fc6c0d5 16445 || member_loc->form_is_block ())
0971de02 16446 {
435d3d88 16447 if (attr != nullptr)
0971de02
TT
16448 mark_common_block_symbol_computed (sym, die, attr,
16449 member_loc, cu);
16450 }
16451 else
16452 dwarf2_complex_location_expr_complaint ();
16453 }
16454 }
c906108c 16455 }
4357ac6c
TT
16456
16457 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16458 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16459 }
16460}
16461
0114d602 16462/* Create a type for a C++ namespace. */
d9fa45fe 16463
0114d602
DJ
16464static struct type *
16465read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16466{
518817b3 16467 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16468 const char *previous_prefix, *name;
9219021c 16469 int is_anonymous;
0114d602
DJ
16470 struct type *type;
16471
16472 /* For extensions, reuse the type of the original namespace. */
16473 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16474 {
16475 struct die_info *ext_die;
16476 struct dwarf2_cu *ext_cu = cu;
9a619af0 16477
0114d602
DJ
16478 ext_die = dwarf2_extension (die, &ext_cu);
16479 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16480
16481 /* EXT_CU may not be the same as CU.
02142a6c 16482 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16483 return set_die_type (die, type, cu);
16484 }
9219021c 16485
e142c38c 16486 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16487
16488 /* Now build the name of the current namespace. */
16489
0114d602
DJ
16490 previous_prefix = determine_prefix (die, cu);
16491 if (previous_prefix[0] != '\0')
16492 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16493 previous_prefix, name, 0, cu);
0114d602
DJ
16494
16495 /* Create the type. */
19f392bc 16496 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16497
60531b24 16498 return set_die_type (die, type, cu);
0114d602
DJ
16499}
16500
22cee43f 16501/* Read a namespace scope. */
0114d602
DJ
16502
16503static void
16504read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16505{
518817b3 16506 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16507 int is_anonymous;
9219021c 16508
5c4e30ca
DC
16509 /* Add a symbol associated to this if we haven't seen the namespace
16510 before. Also, add a using directive if it's an anonymous
16511 namespace. */
9219021c 16512
f2f0e013 16513 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16514 {
16515 struct type *type;
16516
0114d602 16517 type = read_type_die (die, cu);
e7c27a73 16518 new_symbol (die, type, cu);
5c4e30ca 16519
e8e80198 16520 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16521 if (is_anonymous)
0114d602
DJ
16522 {
16523 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16524
eb1e02fd 16525 std::vector<const char *> excludes;
804d2729 16526 add_using_directive (using_directives (cu),
7d93a1e0 16527 previous_prefix, type->name (), NULL,
eb1e02fd 16528 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16529 }
5c4e30ca 16530 }
9219021c 16531
639d11d3 16532 if (die->child != NULL)
d9fa45fe 16533 {
639d11d3 16534 struct die_info *child_die = die->child;
6e70227d 16535
d9fa45fe
DC
16536 while (child_die && child_die->tag)
16537 {
e7c27a73 16538 process_die (child_die, cu);
436c571c 16539 child_die = child_die->sibling;
d9fa45fe
DC
16540 }
16541 }
38d518c9
EZ
16542}
16543
f55ee35c
JK
16544/* Read a Fortran module as type. This DIE can be only a declaration used for
16545 imported module. Still we need that type as local Fortran "use ... only"
16546 declaration imports depend on the created type in determine_prefix. */
16547
16548static struct type *
16549read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16550{
518817b3 16551 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16552 const char *module_name;
f55ee35c
JK
16553 struct type *type;
16554
16555 module_name = dwarf2_name (die, cu);
19f392bc 16556 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16557
f55ee35c
JK
16558 return set_die_type (die, type, cu);
16559}
16560
5d7cb8df
JK
16561/* Read a Fortran module. */
16562
16563static void
16564read_module (struct die_info *die, struct dwarf2_cu *cu)
16565{
16566 struct die_info *child_die = die->child;
530e8392
KB
16567 struct type *type;
16568
16569 type = read_type_die (die, cu);
16570 new_symbol (die, type, cu);
5d7cb8df 16571
5d7cb8df
JK
16572 while (child_die && child_die->tag)
16573 {
16574 process_die (child_die, cu);
436c571c 16575 child_die = child_die->sibling;
5d7cb8df
JK
16576 }
16577}
16578
38d518c9
EZ
16579/* Return the name of the namespace represented by DIE. Set
16580 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16581 namespace. */
16582
16583static const char *
e142c38c 16584namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16585{
16586 struct die_info *current_die;
16587 const char *name = NULL;
16588
16589 /* Loop through the extensions until we find a name. */
16590
16591 for (current_die = die;
16592 current_die != NULL;
f2f0e013 16593 current_die = dwarf2_extension (die, &cu))
38d518c9 16594 {
96553a0c
DE
16595 /* We don't use dwarf2_name here so that we can detect the absence
16596 of a name -> anonymous namespace. */
7d45c7c3 16597 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16598
38d518c9
EZ
16599 if (name != NULL)
16600 break;
16601 }
16602
16603 /* Is it an anonymous namespace? */
16604
16605 *is_anonymous = (name == NULL);
16606 if (*is_anonymous)
2b1dbab0 16607 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16608
16609 return name;
d9fa45fe
DC
16610}
16611
c906108c
SS
16612/* Extract all information from a DW_TAG_pointer_type DIE and add to
16613 the user defined type vector. */
16614
f792889a 16615static struct type *
e7c27a73 16616read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16617{
518817b3 16618 struct gdbarch *gdbarch
08feed99 16619 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16620 struct comp_unit_head *cu_header = &cu->header;
c906108c 16621 struct type *type;
8b2dbe47
KB
16622 struct attribute *attr_byte_size;
16623 struct attribute *attr_address_class;
16624 int byte_size, addr_class;
7e314c57
JK
16625 struct type *target_type;
16626
16627 target_type = die_type (die, cu);
c906108c 16628
7e314c57
JK
16629 /* The die_type call above may have already set the type for this DIE. */
16630 type = get_die_type (die, cu);
16631 if (type)
16632 return type;
16633
16634 type = lookup_pointer_type (target_type);
8b2dbe47 16635
e142c38c 16636 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16637 if (attr_byte_size)
16638 byte_size = DW_UNSND (attr_byte_size);
c906108c 16639 else
8b2dbe47
KB
16640 byte_size = cu_header->addr_size;
16641
e142c38c 16642 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16643 if (attr_address_class)
16644 addr_class = DW_UNSND (attr_address_class);
16645 else
16646 addr_class = DW_ADDR_none;
16647
2b4424c3
TT
16648 ULONGEST alignment = get_alignment (cu, die);
16649
16650 /* If the pointer size, alignment, or address class is different
16651 than the default, create a type variant marked as such and set
16652 the length accordingly. */
16653 if (TYPE_LENGTH (type) != byte_size
16654 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16655 && alignment != TYPE_RAW_ALIGN (type))
16656 || addr_class != DW_ADDR_none)
c906108c 16657 {
5e2b427d 16658 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16659 {
16660 int type_flags;
16661
849957d9 16662 type_flags = gdbarch_address_class_type_flags
5e2b427d 16663 (gdbarch, byte_size, addr_class);
876cecd0
TT
16664 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16665 == 0);
8b2dbe47
KB
16666 type = make_type_with_address_space (type, type_flags);
16667 }
16668 else if (TYPE_LENGTH (type) != byte_size)
16669 {
b98664d3 16670 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16671 }
2b4424c3
TT
16672 else if (TYPE_RAW_ALIGN (type) != alignment)
16673 {
b98664d3 16674 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16675 " - DIE at %s [in module %s]"),
16676 sect_offset_str (die->sect_off),
16677 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16678 }
6e70227d 16679 else
9a619af0
MS
16680 {
16681 /* Should we also complain about unhandled address classes? */
16682 }
c906108c 16683 }
8b2dbe47
KB
16684
16685 TYPE_LENGTH (type) = byte_size;
2b4424c3 16686 set_type_align (type, alignment);
f792889a 16687 return set_die_type (die, type, cu);
c906108c
SS
16688}
16689
16690/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16691 the user defined type vector. */
16692
f792889a 16693static struct type *
e7c27a73 16694read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16695{
16696 struct type *type;
16697 struct type *to_type;
16698 struct type *domain;
16699
e7c27a73
DJ
16700 to_type = die_type (die, cu);
16701 domain = die_containing_type (die, cu);
0d5de010 16702
7e314c57
JK
16703 /* The calls above may have already set the type for this DIE. */
16704 type = get_die_type (die, cu);
16705 if (type)
16706 return type;
16707
78134374 16708 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16709 type = lookup_methodptr_type (to_type);
78134374 16710 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16711 {
518817b3
SM
16712 struct type *new_type
16713 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16714
16715 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16716 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16717 TYPE_VARARGS (to_type));
16718 type = lookup_methodptr_type (new_type);
16719 }
0d5de010
DJ
16720 else
16721 type = lookup_memberptr_type (to_type, domain);
c906108c 16722
f792889a 16723 return set_die_type (die, type, cu);
c906108c
SS
16724}
16725
4297a3f0 16726/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16727 the user defined type vector. */
16728
f792889a 16729static struct type *
4297a3f0
AV
16730read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16731 enum type_code refcode)
c906108c 16732{
e7c27a73 16733 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16734 struct type *type, *target_type;
c906108c
SS
16735 struct attribute *attr;
16736
4297a3f0
AV
16737 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16738
7e314c57
JK
16739 target_type = die_type (die, cu);
16740
16741 /* The die_type call above may have already set the type for this DIE. */
16742 type = get_die_type (die, cu);
16743 if (type)
16744 return type;
16745
4297a3f0 16746 type = lookup_reference_type (target_type, refcode);
e142c38c 16747 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16748 if (attr != nullptr)
c906108c
SS
16749 {
16750 TYPE_LENGTH (type) = DW_UNSND (attr);
16751 }
16752 else
16753 {
107d2387 16754 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16755 }
2b4424c3 16756 maybe_set_alignment (cu, die, type);
f792889a 16757 return set_die_type (die, type, cu);
c906108c
SS
16758}
16759
cf363f18
MW
16760/* Add the given cv-qualifiers to the element type of the array. GCC
16761 outputs DWARF type qualifiers that apply to an array, not the
16762 element type. But GDB relies on the array element type to carry
16763 the cv-qualifiers. This mimics section 6.7.3 of the C99
16764 specification. */
16765
16766static struct type *
16767add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16768 struct type *base_type, int cnst, int voltl)
16769{
16770 struct type *el_type, *inner_array;
16771
16772 base_type = copy_type (base_type);
16773 inner_array = base_type;
16774
78134374 16775 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16776 {
16777 TYPE_TARGET_TYPE (inner_array) =
16778 copy_type (TYPE_TARGET_TYPE (inner_array));
16779 inner_array = TYPE_TARGET_TYPE (inner_array);
16780 }
16781
16782 el_type = TYPE_TARGET_TYPE (inner_array);
16783 cnst |= TYPE_CONST (el_type);
16784 voltl |= TYPE_VOLATILE (el_type);
16785 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16786
16787 return set_die_type (die, base_type, cu);
16788}
16789
f792889a 16790static struct type *
e7c27a73 16791read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16792{
f792889a 16793 struct type *base_type, *cv_type;
c906108c 16794
e7c27a73 16795 base_type = die_type (die, cu);
7e314c57
JK
16796
16797 /* The die_type call above may have already set the type for this DIE. */
16798 cv_type = get_die_type (die, cu);
16799 if (cv_type)
16800 return cv_type;
16801
2f608a3a
KW
16802 /* In case the const qualifier is applied to an array type, the element type
16803 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16804 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16805 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16806
f792889a
DJ
16807 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16808 return set_die_type (die, cv_type, cu);
c906108c
SS
16809}
16810
f792889a 16811static struct type *
e7c27a73 16812read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16813{
f792889a 16814 struct type *base_type, *cv_type;
c906108c 16815
e7c27a73 16816 base_type = die_type (die, cu);
7e314c57
JK
16817
16818 /* The die_type call above may have already set the type for this DIE. */
16819 cv_type = get_die_type (die, cu);
16820 if (cv_type)
16821 return cv_type;
16822
cf363f18
MW
16823 /* In case the volatile qualifier is applied to an array type, the
16824 element type is so qualified, not the array type (section 6.7.3
16825 of C99). */
78134374 16826 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16827 return add_array_cv_type (die, cu, base_type, 0, 1);
16828
f792889a
DJ
16829 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16830 return set_die_type (die, cv_type, cu);
c906108c
SS
16831}
16832
06d66ee9
TT
16833/* Handle DW_TAG_restrict_type. */
16834
16835static struct type *
16836read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16837{
16838 struct type *base_type, *cv_type;
16839
16840 base_type = die_type (die, cu);
16841
16842 /* The die_type call above may have already set the type for this DIE. */
16843 cv_type = get_die_type (die, cu);
16844 if (cv_type)
16845 return cv_type;
16846
16847 cv_type = make_restrict_type (base_type);
16848 return set_die_type (die, cv_type, cu);
16849}
16850
a2c2acaf
MW
16851/* Handle DW_TAG_atomic_type. */
16852
16853static struct type *
16854read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16855{
16856 struct type *base_type, *cv_type;
16857
16858 base_type = die_type (die, cu);
16859
16860 /* The die_type call above may have already set the type for this DIE. */
16861 cv_type = get_die_type (die, cu);
16862 if (cv_type)
16863 return cv_type;
16864
16865 cv_type = make_atomic_type (base_type);
16866 return set_die_type (die, cv_type, cu);
16867}
16868
c906108c
SS
16869/* Extract all information from a DW_TAG_string_type DIE and add to
16870 the user defined type vector. It isn't really a user defined type,
16871 but it behaves like one, with other DIE's using an AT_user_def_type
16872 attribute to reference it. */
16873
f792889a 16874static struct type *
e7c27a73 16875read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16876{
518817b3 16877 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16878 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16879 struct type *type, *range_type, *index_type, *char_type;
16880 struct attribute *attr;
216a7e6b
AB
16881 struct dynamic_prop prop;
16882 bool length_is_constant = true;
16883 LONGEST length;
16884
16885 /* There are a couple of places where bit sizes might be made use of
16886 when parsing a DW_TAG_string_type, however, no producer that we know
16887 of make use of these. Handling bit sizes that are a multiple of the
16888 byte size is easy enough, but what about other bit sizes? Lets deal
16889 with that problem when we have to. Warn about these attributes being
16890 unsupported, then parse the type and ignore them like we always
16891 have. */
16892 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16893 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16894 {
16895 static bool warning_printed = false;
16896 if (!warning_printed)
16897 {
16898 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16899 "currently supported on DW_TAG_string_type."));
16900 warning_printed = true;
16901 }
16902 }
c906108c 16903
e142c38c 16904 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16905 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16906 {
16907 /* The string length describes the location at which the length of
16908 the string can be found. The size of the length field can be
16909 specified with one of the attributes below. */
16910 struct type *prop_type;
16911 struct attribute *len
16912 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16913 if (len == nullptr)
16914 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16915 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16916 {
16917 /* Pass 0 as the default as we know this attribute is constant
16918 and the default value will not be returned. */
0826b30a 16919 LONGEST sz = len->constant_value (0);
09ba997f 16920 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16921 }
16922 else
16923 {
16924 /* If the size is not specified then we assume it is the size of
16925 an address on this target. */
09ba997f 16926 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16927 }
16928
16929 /* Convert the attribute into a dynamic property. */
16930 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16931 length = 1;
16932 else
16933 length_is_constant = false;
16934 }
16935 else if (attr != nullptr)
16936 {
16937 /* This DW_AT_string_length just contains the length with no
16938 indirection. There's no need to create a dynamic property in this
16939 case. Pass 0 for the default value as we know it will not be
16940 returned in this case. */
0826b30a 16941 length = attr->constant_value (0);
216a7e6b
AB
16942 }
16943 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16944 {
216a7e6b 16945 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16946 length = attr->constant_value (1);
c906108c
SS
16947 }
16948 else
16949 {
216a7e6b
AB
16950 /* Use 1 as a fallback length if we have nothing else. */
16951 length = 1;
c906108c 16952 }
6ccb9162 16953
46bf5051 16954 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16955 if (length_is_constant)
16956 range_type = create_static_range_type (NULL, index_type, 1, length);
16957 else
16958 {
16959 struct dynamic_prop low_bound;
16960
16961 low_bound.kind = PROP_CONST;
16962 low_bound.data.const_val = 1;
16963 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16964 }
3b7538c0
UW
16965 char_type = language_string_char_type (cu->language_defn, gdbarch);
16966 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16967
f792889a 16968 return set_die_type (die, type, cu);
c906108c
SS
16969}
16970
4d804846
JB
16971/* Assuming that DIE corresponds to a function, returns nonzero
16972 if the function is prototyped. */
16973
16974static int
16975prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16976{
16977 struct attribute *attr;
16978
16979 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16980 if (attr && (DW_UNSND (attr) != 0))
16981 return 1;
16982
16983 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16984 is only meaningful for C, but the concept also extends to other
4d804846
JB
16985 languages that allow unprototyped functions (Eg: Objective C).
16986 For all other languages, assume that functions are always
16987 prototyped. */
16988 if (cu->language != language_c
16989 && cu->language != language_objc
16990 && cu->language != language_opencl)
16991 return 1;
16992
16993 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16994 prototyped and unprototyped functions; default to prototyped,
16995 since that is more common in modern code (and RealView warns
16996 about unprototyped functions). */
16997 if (producer_is_realview (cu->producer))
16998 return 1;
16999
17000 return 0;
17001}
17002
c906108c
SS
17003/* Handle DIES due to C code like:
17004
17005 struct foo
c5aa993b
JM
17006 {
17007 int (*funcp)(int a, long l);
17008 int b;
17009 };
c906108c 17010
0963b4bd 17011 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17012
f792889a 17013static struct type *
e7c27a73 17014read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17015{
518817b3 17016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17017 struct type *type; /* Type that this function returns. */
17018 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17019 struct attribute *attr;
17020
e7c27a73 17021 type = die_type (die, cu);
7e314c57
JK
17022
17023 /* The die_type call above may have already set the type for this DIE. */
17024 ftype = get_die_type (die, cu);
17025 if (ftype)
17026 return ftype;
17027
0c8b41f1 17028 ftype = lookup_function_type (type);
c906108c 17029
4d804846 17030 if (prototyped_function_p (die, cu))
a6c727b2 17031 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17032
c055b101
CV
17033 /* Store the calling convention in the type if it's available in
17034 the subroutine die. Otherwise set the calling convention to
17035 the default value DW_CC_normal. */
17036 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17037 if (attr != nullptr
17038 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17039 TYPE_CALLING_CONVENTION (ftype)
17040 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17041 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17042 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17043 else
17044 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17045
743649fd
MW
17046 /* Record whether the function returns normally to its caller or not
17047 if the DWARF producer set that information. */
17048 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17049 if (attr && (DW_UNSND (attr) != 0))
17050 TYPE_NO_RETURN (ftype) = 1;
17051
76c10ea2
GM
17052 /* We need to add the subroutine type to the die immediately so
17053 we don't infinitely recurse when dealing with parameters
0963b4bd 17054 declared as the same subroutine type. */
76c10ea2 17055 set_die_type (die, ftype, cu);
6e70227d 17056
639d11d3 17057 if (die->child != NULL)
c906108c 17058 {
bb5ed363 17059 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17060 struct die_info *child_die;
8072405b 17061 int nparams, iparams;
c906108c
SS
17062
17063 /* Count the number of parameters.
17064 FIXME: GDB currently ignores vararg functions, but knows about
17065 vararg member functions. */
8072405b 17066 nparams = 0;
639d11d3 17067 child_die = die->child;
c906108c
SS
17068 while (child_die && child_die->tag)
17069 {
17070 if (child_die->tag == DW_TAG_formal_parameter)
17071 nparams++;
17072 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17073 TYPE_VARARGS (ftype) = 1;
436c571c 17074 child_die = child_die->sibling;
c906108c
SS
17075 }
17076
17077 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17078 ftype->set_num_fields (nparams);
3cabb6b0
SM
17079 ftype->set_fields
17080 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17081
8072405b
JK
17082 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17083 even if we error out during the parameters reading below. */
17084 for (iparams = 0; iparams < nparams; iparams++)
17085 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17086
17087 iparams = 0;
639d11d3 17088 child_die = die->child;
c906108c
SS
17089 while (child_die && child_die->tag)
17090 {
17091 if (child_die->tag == DW_TAG_formal_parameter)
17092 {
3ce3b1ba
PA
17093 struct type *arg_type;
17094
17095 /* DWARF version 2 has no clean way to discern C++
17096 static and non-static member functions. G++ helps
17097 GDB by marking the first parameter for non-static
17098 member functions (which is the this pointer) as
17099 artificial. We pass this information to
17100 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17101
17102 DWARF version 3 added DW_AT_object_pointer, which GCC
17103 4.5 does not yet generate. */
e142c38c 17104 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17105 if (attr != nullptr)
c906108c
SS
17106 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17107 else
9c37b5ae 17108 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17109 arg_type = die_type (child_die, cu);
17110
17111 /* RealView does not mark THIS as const, which the testsuite
17112 expects. GCC marks THIS as const in method definitions,
17113 but not in the class specifications (GCC PR 43053). */
17114 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17115 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17116 {
17117 int is_this = 0;
17118 struct dwarf2_cu *arg_cu = cu;
17119 const char *name = dwarf2_name (child_die, cu);
17120
17121 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17122 if (attr != nullptr)
3ce3b1ba
PA
17123 {
17124 /* If the compiler emits this, use it. */
17125 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17126 is_this = 1;
17127 }
17128 else if (name && strcmp (name, "this") == 0)
17129 /* Function definitions will have the argument names. */
17130 is_this = 1;
17131 else if (name == NULL && iparams == 0)
17132 /* Declarations may not have the names, so like
17133 elsewhere in GDB, assume an artificial first
17134 argument is "this". */
17135 is_this = 1;
17136
17137 if (is_this)
17138 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17139 arg_type, 0);
17140 }
17141
17142 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17143 iparams++;
17144 }
436c571c 17145 child_die = child_die->sibling;
c906108c
SS
17146 }
17147 }
17148
76c10ea2 17149 return ftype;
c906108c
SS
17150}
17151
f792889a 17152static struct type *
e7c27a73 17153read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17154{
518817b3 17155 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17156 const char *name = NULL;
3c8e0968 17157 struct type *this_type, *target_type;
c906108c 17158
94af9270 17159 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17160 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17161 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17162 set_die_type (die, this_type, cu);
3c8e0968
DE
17163 target_type = die_type (die, cu);
17164 if (target_type != this_type)
17165 TYPE_TARGET_TYPE (this_type) = target_type;
17166 else
17167 {
17168 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17169 spec and cause infinite loops in GDB. */
b98664d3 17170 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17171 "- DIE at %s [in module %s]"),
17172 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17173 TYPE_TARGET_TYPE (this_type) = NULL;
17174 }
e4003a34
TV
17175 if (name == NULL)
17176 {
17177 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17178 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17179 Handle these by just returning the target type, rather than
17180 constructing an anonymous typedef type and trying to handle this
17181 elsewhere. */
17182 set_die_type (die, target_type, cu);
17183 return target_type;
17184 }
f792889a 17185 return this_type;
c906108c
SS
17186}
17187
9b790ce7
UW
17188/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17189 (which may be different from NAME) to the architecture back-end to allow
17190 it to guess the correct format if necessary. */
17191
17192static struct type *
17193dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17194 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17195{
08feed99 17196 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17197 const struct floatformat **format;
17198 struct type *type;
17199
17200 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17201 if (format)
103a685e 17202 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17203 else
77b7c781 17204 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17205
17206 return type;
17207}
17208
eb77c9df
AB
17209/* Allocate an integer type of size BITS and name NAME. */
17210
17211static struct type *
17212dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17213 int bits, int unsigned_p, const char *name)
17214{
17215 struct type *type;
17216
17217 /* Versions of Intel's C Compiler generate an integer type called "void"
17218 instead of using DW_TAG_unspecified_type. This has been seen on
17219 at least versions 14, 17, and 18. */
35ee2dc2
AB
17220 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17221 && strcmp (name, "void") == 0)
eb77c9df
AB
17222 type = objfile_type (objfile)->builtin_void;
17223 else
17224 type = init_integer_type (objfile, bits, unsigned_p, name);
17225
17226 return type;
17227}
17228
8bdc1658
AB
17229/* Initialise and return a floating point type of size BITS suitable for
17230 use as a component of a complex number. The NAME_HINT is passed through
17231 when initialising the floating point type and is the name of the complex
17232 type.
17233
17234 As DWARF doesn't currently provide an explicit name for the components
17235 of a complex number, but it can be helpful to have these components
17236 named, we try to select a suitable name based on the size of the
17237 component. */
17238static struct type *
17239dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17240 struct objfile *objfile,
103a685e
TT
17241 int bits, const char *name_hint,
17242 enum bfd_endian byte_order)
8bdc1658 17243{
08feed99 17244 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17245 struct type *tt = nullptr;
17246
35add35e
AB
17247 /* Try to find a suitable floating point builtin type of size BITS.
17248 We're going to use the name of this type as the name for the complex
17249 target type that we are about to create. */
1db455a7 17250 switch (cu->language)
8bdc1658 17251 {
1db455a7
AB
17252 case language_fortran:
17253 switch (bits)
17254 {
17255 case 32:
17256 tt = builtin_f_type (gdbarch)->builtin_real;
17257 break;
17258 case 64:
17259 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17260 break;
17261 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17262 case 128:
17263 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17264 break;
17265 }
8bdc1658 17266 break;
1db455a7
AB
17267 default:
17268 switch (bits)
17269 {
17270 case 32:
17271 tt = builtin_type (gdbarch)->builtin_float;
17272 break;
17273 case 64:
17274 tt = builtin_type (gdbarch)->builtin_double;
17275 break;
17276 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17277 case 128:
17278 tt = builtin_type (gdbarch)->builtin_long_double;
17279 break;
17280 }
8bdc1658
AB
17281 break;
17282 }
17283
35add35e
AB
17284 /* If the type we found doesn't match the size we were looking for, then
17285 pretend we didn't find a type at all, the complex target type we
17286 create will then be nameless. */
a12e5744 17287 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17288 tt = nullptr;
17289
7d93a1e0 17290 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17291 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17292}
17293
c906108c
SS
17294/* Find a representation of a given base type and install
17295 it in the TYPE field of the die. */
17296
f792889a 17297static struct type *
e7c27a73 17298read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17299{
518817b3 17300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17301 struct type *type;
17302 struct attribute *attr;
19f392bc 17303 int encoding = 0, bits = 0;
15d034d0 17304 const char *name;
34877895 17305 gdbarch *arch;
c906108c 17306
e142c38c 17307 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17308 if (attr != nullptr)
34877895 17309 encoding = DW_UNSND (attr);
e142c38c 17310 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17311 if (attr != nullptr)
34877895 17312 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17313 name = dwarf2_name (die, cu);
6ccb9162 17314 if (!name)
34877895 17315 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17316
08feed99 17317 arch = objfile->arch ();
103a685e
TT
17318 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17319
34877895
PJ
17320 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17321 if (attr)
103a685e
TT
17322 {
17323 int endianity = DW_UNSND (attr);
17324
17325 switch (endianity)
17326 {
17327 case DW_END_big:
17328 byte_order = BFD_ENDIAN_BIG;
17329 break;
17330 case DW_END_little:
17331 byte_order = BFD_ENDIAN_LITTLE;
17332 break;
17333 default:
17334 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17335 break;
17336 }
17337 }
6ccb9162
UW
17338
17339 switch (encoding)
c906108c 17340 {
6ccb9162
UW
17341 case DW_ATE_address:
17342 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17343 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17344 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17345 break;
17346 case DW_ATE_boolean:
19f392bc 17347 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17348 break;
17349 case DW_ATE_complex_float:
103a685e
TT
17350 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17351 byte_order);
78134374 17352 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17353 {
17354 if (name == nullptr)
17355 {
17356 struct obstack *obstack
17357 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
7d93a1e0 17358 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17359 nullptr);
17360 }
17361 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17362 }
17363 else
17364 type = init_complex_type (name, type);
6ccb9162
UW
17365 break;
17366 case DW_ATE_decimal_float:
19f392bc 17367 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17368 break;
17369 case DW_ATE_float:
103a685e 17370 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17371 break;
17372 case DW_ATE_signed:
eb77c9df 17373 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17374 break;
17375 case DW_ATE_unsigned:
3b2b8fea
TT
17376 if (cu->language == language_fortran
17377 && name
61012eef 17378 && startswith (name, "character("))
19f392bc
UW
17379 type = init_character_type (objfile, bits, 1, name);
17380 else
eb77c9df 17381 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17382 break;
17383 case DW_ATE_signed_char:
6e70227d 17384 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17385 || cu->language == language_pascal
17386 || cu->language == language_fortran)
19f392bc
UW
17387 type = init_character_type (objfile, bits, 0, name);
17388 else
eb77c9df 17389 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17390 break;
17391 case DW_ATE_unsigned_char:
868a0084 17392 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17393 || cu->language == language_pascal
c44af4eb
TT
17394 || cu->language == language_fortran
17395 || cu->language == language_rust)
19f392bc
UW
17396 type = init_character_type (objfile, bits, 1, name);
17397 else
eb77c9df 17398 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17399 break;
75079b2b 17400 case DW_ATE_UTF:
53e710ac 17401 {
53e710ac
PA
17402 if (bits == 16)
17403 type = builtin_type (arch)->builtin_char16;
17404 else if (bits == 32)
17405 type = builtin_type (arch)->builtin_char32;
17406 else
17407 {
b98664d3 17408 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17409 bits);
eb77c9df 17410 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17411 }
17412 return set_die_type (die, type, cu);
17413 }
75079b2b
TT
17414 break;
17415
6ccb9162 17416 default:
b98664d3 17417 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17418 dwarf_type_encoding_name (encoding));
77b7c781 17419 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17420 break;
c906108c 17421 }
6ccb9162 17422
0114d602 17423 if (name && strcmp (name, "char") == 0)
876cecd0 17424 TYPE_NOSIGN (type) = 1;
0114d602 17425
2b4424c3
TT
17426 maybe_set_alignment (cu, die, type);
17427
103a685e 17428 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17429
f792889a 17430 return set_die_type (die, type, cu);
c906108c
SS
17431}
17432
80180f79
SA
17433/* Parse dwarf attribute if it's a block, reference or constant and put the
17434 resulting value of the attribute into struct bound_prop.
17435 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17436
17437static int
17438attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17439 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17440 struct type *default_type)
80180f79
SA
17441{
17442 struct dwarf2_property_baton *baton;
518817b3
SM
17443 struct obstack *obstack
17444 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17445
9a49df9d
AB
17446 gdb_assert (default_type != NULL);
17447
80180f79
SA
17448 if (attr == NULL || prop == NULL)
17449 return 0;
17450
4fc6c0d5 17451 if (attr->form_is_block ())
80180f79 17452 {
8d749320 17453 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17454 baton->property_type = default_type;
80180f79
SA
17455 baton->locexpr.per_cu = cu->per_cu;
17456 baton->locexpr.size = DW_BLOCK (attr)->size;
17457 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17458 switch (attr->name)
17459 {
17460 case DW_AT_string_length:
17461 baton->locexpr.is_reference = true;
17462 break;
17463 default:
17464 baton->locexpr.is_reference = false;
17465 break;
17466 }
80180f79
SA
17467 prop->data.baton = baton;
17468 prop->kind = PROP_LOCEXPR;
17469 gdb_assert (prop->data.baton != NULL);
17470 }
cd6c91b4 17471 else if (attr->form_is_ref ())
80180f79
SA
17472 {
17473 struct dwarf2_cu *target_cu = cu;
17474 struct die_info *target_die;
17475 struct attribute *target_attr;
17476
17477 target_die = follow_die_ref (die, attr, &target_cu);
17478 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17479 if (target_attr == NULL)
17480 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17481 target_cu);
80180f79
SA
17482 if (target_attr == NULL)
17483 return 0;
17484
df25ebbd 17485 switch (target_attr->name)
80180f79 17486 {
df25ebbd 17487 case DW_AT_location:
cd6c91b4 17488 if (target_attr->form_is_section_offset ())
df25ebbd 17489 {
8d749320 17490 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17491 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17492 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17493 prop->data.baton = baton;
17494 prop->kind = PROP_LOCLIST;
17495 gdb_assert (prop->data.baton != NULL);
17496 }
4fc6c0d5 17497 else if (target_attr->form_is_block ())
df25ebbd 17498 {
8d749320 17499 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17500 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17501 baton->locexpr.per_cu = cu->per_cu;
17502 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17503 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17504 baton->locexpr.is_reference = true;
df25ebbd
JB
17505 prop->data.baton = baton;
17506 prop->kind = PROP_LOCEXPR;
17507 gdb_assert (prop->data.baton != NULL);
17508 }
17509 else
17510 {
17511 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17512 "dynamic property");
17513 return 0;
17514 }
17515 break;
17516 case DW_AT_data_member_location:
17517 {
17518 LONGEST offset;
17519
17520 if (!handle_data_member_location (target_die, target_cu,
17521 &offset))
17522 return 0;
17523
8d749320 17524 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17525 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17526 target_cu);
df25ebbd
JB
17527 baton->offset_info.offset = offset;
17528 baton->offset_info.type = die_type (target_die, target_cu);
17529 prop->data.baton = baton;
17530 prop->kind = PROP_ADDR_OFFSET;
17531 break;
17532 }
80180f79
SA
17533 }
17534 }
cd6c91b4 17535 else if (attr->form_is_constant ())
80180f79 17536 {
0826b30a 17537 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17538 prop->kind = PROP_CONST;
17539 }
17540 else
17541 {
17542 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17543 dwarf2_name (die, cu));
17544 return 0;
17545 }
17546
17547 return 1;
17548}
17549
09ba997f 17550/* See read.h. */
9a49df9d 17551
09ba997f
TT
17552struct type *
17553dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17554{
09ba997f 17555 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17556 struct type *int_type;
17557
17558 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17559#define TRY_TYPE(F) \
17560 int_type = (unsigned_p \
17561 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17562 : objfile_type (objfile)->builtin_ ## F); \
17563 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17564 return int_type
17565
17566 TRY_TYPE (char);
17567 TRY_TYPE (short);
17568 TRY_TYPE (int);
17569 TRY_TYPE (long);
17570 TRY_TYPE (long_long);
17571
17572#undef TRY_TYPE
17573
17574 gdb_assert_not_reached ("unable to find suitable integer type");
17575}
17576
09ba997f 17577/* See read.h. */
11a8b164 17578
09ba997f
TT
17579struct type *
17580dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17581{
09ba997f
TT
17582 int addr_size = this->addr_size ();
17583 return int_type (addr_size, unsigned_p);
11a8b164
AB
17584}
17585
b86352cf
AB
17586/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17587 present (which is valid) then compute the default type based on the
17588 compilation units address size. */
17589
17590static struct type *
17591read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17592{
17593 struct type *index_type = die_type (die, cu);
17594
17595 /* Dwarf-2 specifications explicitly allows to create subrange types
17596 without specifying a base type.
17597 In that case, the base type must be set to the type of
17598 the lower bound, upper bound or count, in that order, if any of these
17599 three attributes references an object that has a type.
17600 If no base type is found, the Dwarf-2 specifications say that
17601 a signed integer type of size equal to the size of an address should
17602 be used.
17603 For the following C code: `extern char gdb_int [];'
17604 GCC produces an empty range DIE.
17605 FIXME: muller/2010-05-28: Possible references to object for low bound,
17606 high bound or count are not yet handled by this code. */
78134374 17607 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17608 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17609
17610 return index_type;
17611}
17612
a02abb62
JB
17613/* Read the given DW_AT_subrange DIE. */
17614
f792889a 17615static struct type *
a02abb62
JB
17616read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17617{
4c9ad8c2 17618 struct type *base_type, *orig_base_type;
a02abb62
JB
17619 struct type *range_type;
17620 struct attribute *attr;
729efb13 17621 struct dynamic_prop low, high;
4fae6e18 17622 int low_default_is_valid;
c451ebe5 17623 int high_bound_is_count = 0;
15d034d0 17624 const char *name;
d359392f 17625 ULONGEST negative_mask;
e77813c8 17626
b86352cf
AB
17627 orig_base_type = read_subrange_index_type (die, cu);
17628
4c9ad8c2
TT
17629 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17630 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17631 creating the range type, but we use the result of check_typedef
17632 when examining properties of the type. */
17633 base_type = check_typedef (orig_base_type);
a02abb62 17634
7e314c57
JK
17635 /* The die_type call above may have already set the type for this DIE. */
17636 range_type = get_die_type (die, cu);
17637 if (range_type)
17638 return range_type;
17639
729efb13
SA
17640 low.kind = PROP_CONST;
17641 high.kind = PROP_CONST;
17642 high.data.const_val = 0;
17643
4fae6e18
JK
17644 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17645 omitting DW_AT_lower_bound. */
17646 switch (cu->language)
6e70227d 17647 {
4fae6e18
JK
17648 case language_c:
17649 case language_cplus:
729efb13 17650 low.data.const_val = 0;
4fae6e18
JK
17651 low_default_is_valid = 1;
17652 break;
17653 case language_fortran:
729efb13 17654 low.data.const_val = 1;
4fae6e18
JK
17655 low_default_is_valid = 1;
17656 break;
17657 case language_d:
4fae6e18 17658 case language_objc:
c44af4eb 17659 case language_rust:
729efb13 17660 low.data.const_val = 0;
4fae6e18
JK
17661 low_default_is_valid = (cu->header.version >= 4);
17662 break;
17663 case language_ada:
17664 case language_m2:
17665 case language_pascal:
729efb13 17666 low.data.const_val = 1;
4fae6e18
JK
17667 low_default_is_valid = (cu->header.version >= 4);
17668 break;
17669 default:
729efb13 17670 low.data.const_val = 0;
4fae6e18
JK
17671 low_default_is_valid = 0;
17672 break;
a02abb62
JB
17673 }
17674
e142c38c 17675 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17676 if (attr != nullptr)
9a49df9d 17677 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17678 else if (!low_default_is_valid)
b98664d3 17679 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17680 "- DIE at %s [in module %s]"),
17681 sect_offset_str (die->sect_off),
518817b3 17682 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17683
506f5c41
TV
17684 struct attribute *attr_ub, *attr_count;
17685 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17686 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17687 {
506f5c41 17688 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17689 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17690 {
c451ebe5
SA
17691 /* If bounds are constant do the final calculation here. */
17692 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17693 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17694 else
17695 high_bound_is_count = 1;
c2ff108b 17696 }
506f5c41
TV
17697 else
17698 {
17699 if (attr_ub != NULL)
17700 complaint (_("Unresolved DW_AT_upper_bound "
17701 "- DIE at %s [in module %s]"),
17702 sect_offset_str (die->sect_off),
17703 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17704 if (attr_count != NULL)
17705 complaint (_("Unresolved DW_AT_count "
17706 "- DIE at %s [in module %s]"),
17707 sect_offset_str (die->sect_off),
17708 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17709 }
e77813c8 17710 }
a02abb62 17711
4e962e74
TT
17712 LONGEST bias = 0;
17713 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17714 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17715 bias = bias_attr->constant_value (0);
4e962e74 17716
dbb9c2b1
JB
17717 /* Normally, the DWARF producers are expected to use a signed
17718 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17719 But this is unfortunately not always the case, as witnessed
17720 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17721 is used instead. To work around that ambiguity, we treat
17722 the bounds as signed, and thus sign-extend their values, when
17723 the base type is signed. */
6e70227d 17724 negative_mask =
d359392f 17725 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17726 if (low.kind == PROP_CONST
17727 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17728 low.data.const_val |= negative_mask;
17729 if (high.kind == PROP_CONST
17730 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17731 high.data.const_val |= negative_mask;
43bbcdc2 17732
5bbd8269
AB
17733 /* Check for bit and byte strides. */
17734 struct dynamic_prop byte_stride_prop;
17735 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17736 if (attr_byte_stride != nullptr)
17737 {
09ba997f 17738 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17739 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17740 prop_type);
17741 }
17742
17743 struct dynamic_prop bit_stride_prop;
17744 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17745 if (attr_bit_stride != nullptr)
17746 {
17747 /* It only makes sense to have either a bit or byte stride. */
17748 if (attr_byte_stride != nullptr)
17749 {
17750 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17751 "- DIE at %s [in module %s]"),
17752 sect_offset_str (die->sect_off),
17753 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17754 attr_bit_stride = nullptr;
17755 }
17756 else
17757 {
09ba997f 17758 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17759 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17760 prop_type);
17761 }
17762 }
17763
17764 if (attr_byte_stride != nullptr
17765 || attr_bit_stride != nullptr)
17766 {
17767 bool byte_stride_p = (attr_byte_stride != nullptr);
17768 struct dynamic_prop *stride
17769 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17770
17771 range_type
17772 = create_range_type_with_stride (NULL, orig_base_type, &low,
17773 &high, bias, stride, byte_stride_p);
17774 }
17775 else
17776 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17777
c451ebe5
SA
17778 if (high_bound_is_count)
17779 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17780
c2ff108b
JK
17781 /* Ada expects an empty array on no boundary attributes. */
17782 if (attr == NULL && cu->language != language_ada)
729efb13 17783 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17784
39cbfefa
DJ
17785 name = dwarf2_name (die, cu);
17786 if (name)
d0e39ea2 17787 range_type->set_name (name);
6e70227d 17788
e142c38c 17789 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17790 if (attr != nullptr)
a02abb62
JB
17791 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17792
2b4424c3
TT
17793 maybe_set_alignment (cu, die, range_type);
17794
7e314c57
JK
17795 set_die_type (die, range_type, cu);
17796
17797 /* set_die_type should be already done. */
b4ba55a1
JB
17798 set_descriptive_type (range_type, die, cu);
17799
7e314c57 17800 return range_type;
a02abb62 17801}
6e70227d 17802
f792889a 17803static struct type *
81a17f79
JB
17804read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17805{
17806 struct type *type;
81a17f79 17807
518817b3
SM
17808 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17809 NULL);
d0e39ea2 17810 type->set_name (dwarf2_name (die, cu));
81a17f79 17811
74a2f8ff 17812 /* In Ada, an unspecified type is typically used when the description
85102364 17813 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17814 such a type, we treat it as a stub, and try to resolve it later on,
17815 when needed. */
17816 if (cu->language == language_ada)
17817 TYPE_STUB (type) = 1;
17818
f792889a 17819 return set_die_type (die, type, cu);
81a17f79 17820}
a02abb62 17821
639d11d3
DC
17822/* Read a single die and all its descendents. Set the die's sibling
17823 field to NULL; set other fields in the die correctly, and set all
17824 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17825 location of the info_ptr after reading all of those dies. PARENT
17826 is the parent of the die in question. */
17827
17828static struct die_info *
dee91e82 17829read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17830 const gdb_byte *info_ptr,
17831 const gdb_byte **new_info_ptr,
dee91e82 17832 struct die_info *parent)
639d11d3
DC
17833{
17834 struct die_info *die;
d521ce57 17835 const gdb_byte *cur_ptr;
639d11d3 17836
3e225074 17837 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17838 if (die == NULL)
17839 {
17840 *new_info_ptr = cur_ptr;
17841 return NULL;
17842 }
93311388 17843 store_in_ref_table (die, reader->cu);
639d11d3 17844
3e225074 17845 if (die->has_children)
bf6af496 17846 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17847 else
17848 {
17849 die->child = NULL;
17850 *new_info_ptr = cur_ptr;
17851 }
17852
17853 die->sibling = NULL;
17854 die->parent = parent;
17855 return die;
17856}
17857
17858/* Read a die, all of its descendents, and all of its siblings; set
17859 all of the fields of all of the dies correctly. Arguments are as
17860 in read_die_and_children. */
17861
17862static struct die_info *
bf6af496 17863read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17864 const gdb_byte *info_ptr,
17865 const gdb_byte **new_info_ptr,
bf6af496 17866 struct die_info *parent)
639d11d3
DC
17867{
17868 struct die_info *first_die, *last_sibling;
d521ce57 17869 const gdb_byte *cur_ptr;
639d11d3 17870
c906108c 17871 cur_ptr = info_ptr;
639d11d3
DC
17872 first_die = last_sibling = NULL;
17873
17874 while (1)
c906108c 17875 {
639d11d3 17876 struct die_info *die
dee91e82 17877 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17878
1d325ec1 17879 if (die == NULL)
c906108c 17880 {
639d11d3
DC
17881 *new_info_ptr = cur_ptr;
17882 return first_die;
c906108c 17883 }
1d325ec1
DJ
17884
17885 if (!first_die)
17886 first_die = die;
c906108c 17887 else
1d325ec1
DJ
17888 last_sibling->sibling = die;
17889
17890 last_sibling = die;
c906108c 17891 }
c906108c
SS
17892}
17893
bf6af496
DE
17894/* Read a die, all of its descendents, and all of its siblings; set
17895 all of the fields of all of the dies correctly. Arguments are as
17896 in read_die_and_children.
17897 This the main entry point for reading a DIE and all its children. */
17898
17899static struct die_info *
17900read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17901 const gdb_byte *info_ptr,
17902 const gdb_byte **new_info_ptr,
bf6af496
DE
17903 struct die_info *parent)
17904{
17905 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17906 new_info_ptr, parent);
17907
b4f54984 17908 if (dwarf_die_debug)
bf6af496
DE
17909 {
17910 fprintf_unfiltered (gdb_stdlog,
17911 "Read die from %s@0x%x of %s:\n",
96b79293 17912 reader->die_section->get_name (),
bf6af496
DE
17913 (unsigned) (info_ptr - reader->die_section->buffer),
17914 bfd_get_filename (reader->abfd));
b4f54984 17915 dump_die (die, dwarf_die_debug);
bf6af496
DE
17916 }
17917
17918 return die;
17919}
17920
3019eac3
DE
17921/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17922 attributes.
17923 The caller is responsible for filling in the extra attributes
17924 and updating (*DIEP)->num_attrs.
17925 Set DIEP to point to a newly allocated die with its information,
3e225074 17926 except for its child, sibling, and parent fields. */
93311388 17927
d521ce57 17928static const gdb_byte *
3019eac3 17929read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17930 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17931 int num_extra_attrs)
93311388 17932{
b64f50a1 17933 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17934 struct abbrev_info *abbrev;
17935 struct die_info *die;
17936 struct dwarf2_cu *cu = reader->cu;
17937 bfd *abfd = reader->abfd;
17938
9c541725 17939 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17940 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17941 info_ptr += bytes_read;
17942 if (!abbrev_number)
17943 {
17944 *diep = NULL;
93311388
DE
17945 return info_ptr;
17946 }
17947
685af9cd 17948 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17949 if (!abbrev)
348e048f
DE
17950 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17951 abbrev_number,
17952 bfd_get_filename (abfd));
17953
3019eac3 17954 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17955 die->sect_off = sect_off;
93311388
DE
17956 die->tag = abbrev->tag;
17957 die->abbrev = abbrev_number;
3e225074 17958 die->has_children = abbrev->has_children;
93311388 17959
3019eac3
DE
17960 /* Make the result usable.
17961 The caller needs to update num_attrs after adding the extra
17962 attributes. */
93311388
DE
17963 die->num_attrs = abbrev->num_attrs;
17964
18a8505e 17965 std::vector<int> indexes_that_need_reprocess;
93311388 17966 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17967 {
17968 bool need_reprocess;
17969 info_ptr =
17970 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17971 info_ptr, &need_reprocess);
17972 if (need_reprocess)
17973 indexes_that_need_reprocess.push_back (i);
17974 }
17975
052c8bb8 17976 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17977 if (attr != nullptr)
17978 cu->str_offsets_base = DW_UNSND (attr);
93311388 17979
41144253 17980 attr = die->attr (DW_AT_loclists_base);
17981 if (attr != nullptr)
17982 cu->loclist_base = DW_UNSND (attr);
17983
a39fdb41 17984 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17985 if (maybe_addr_base.has_value ())
17986 cu->addr_base = *maybe_addr_base;
17987 for (int index : indexes_that_need_reprocess)
17988 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17989 *diep = die;
93311388
DE
17990 return info_ptr;
17991}
17992
3019eac3
DE
17993/* Read a die and all its attributes.
17994 Set DIEP to point to a newly allocated die with its information,
3e225074 17995 except for its child, sibling, and parent fields. */
3019eac3 17996
d521ce57 17997static const gdb_byte *
3019eac3 17998read_full_die (const struct die_reader_specs *reader,
3e225074 17999 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18000{
d521ce57 18001 const gdb_byte *result;
bf6af496 18002
3e225074 18003 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18004
b4f54984 18005 if (dwarf_die_debug)
bf6af496
DE
18006 {
18007 fprintf_unfiltered (gdb_stdlog,
18008 "Read die from %s@0x%x of %s:\n",
96b79293 18009 reader->die_section->get_name (),
bf6af496
DE
18010 (unsigned) (info_ptr - reader->die_section->buffer),
18011 bfd_get_filename (reader->abfd));
b4f54984 18012 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18013 }
18014
18015 return result;
3019eac3 18016}
433df2d4 18017\f
c906108c 18018
72bf9492
DJ
18019/* Returns nonzero if TAG represents a type that we might generate a partial
18020 symbol for. */
18021
18022static int
18023is_type_tag_for_partial (int tag)
18024{
18025 switch (tag)
18026 {
18027#if 0
18028 /* Some types that would be reasonable to generate partial symbols for,
18029 that we don't at present. */
18030 case DW_TAG_array_type:
18031 case DW_TAG_file_type:
18032 case DW_TAG_ptr_to_member_type:
18033 case DW_TAG_set_type:
18034 case DW_TAG_string_type:
18035 case DW_TAG_subroutine_type:
18036#endif
18037 case DW_TAG_base_type:
18038 case DW_TAG_class_type:
680b30c7 18039 case DW_TAG_interface_type:
72bf9492
DJ
18040 case DW_TAG_enumeration_type:
18041 case DW_TAG_structure_type:
18042 case DW_TAG_subrange_type:
18043 case DW_TAG_typedef:
18044 case DW_TAG_union_type:
18045 return 1;
18046 default:
18047 return 0;
18048 }
18049}
18050
18051/* Load all DIEs that are interesting for partial symbols into memory. */
18052
18053static struct partial_die_info *
dee91e82 18054load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18055 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18056{
dee91e82 18057 struct dwarf2_cu *cu = reader->cu;
518817b3 18058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18059 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18060 unsigned int bytes_read;
5afb4e99 18061 unsigned int load_all = 0;
72bf9492
DJ
18062 int nesting_level = 1;
18063
18064 parent_die = NULL;
18065 last_die = NULL;
18066
7adf1e79
DE
18067 gdb_assert (cu->per_cu != NULL);
18068 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18069 load_all = 1;
18070
72bf9492
DJ
18071 cu->partial_dies
18072 = htab_create_alloc_ex (cu->header.length / 12,
18073 partial_die_hash,
18074 partial_die_eq,
18075 NULL,
18076 &cu->comp_unit_obstack,
18077 hashtab_obstack_allocate,
18078 dummy_obstack_deallocate);
18079
72bf9492
DJ
18080 while (1)
18081 {
685af9cd 18082 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18083
18084 /* A NULL abbrev means the end of a series of children. */
18085 if (abbrev == NULL)
18086 {
18087 if (--nesting_level == 0)
cd9983dd
YQ
18088 return first_die;
18089
72bf9492
DJ
18090 info_ptr += bytes_read;
18091 last_die = parent_die;
18092 parent_die = parent_die->die_parent;
18093 continue;
18094 }
18095
98bfdba5
PA
18096 /* Check for template arguments. We never save these; if
18097 they're seen, we just mark the parent, and go on our way. */
18098 if (parent_die != NULL
18099 && cu->language == language_cplus
18100 && (abbrev->tag == DW_TAG_template_type_param
18101 || abbrev->tag == DW_TAG_template_value_param))
18102 {
18103 parent_die->has_template_arguments = 1;
18104
18105 if (!load_all)
18106 {
18107 /* We don't need a partial DIE for the template argument. */
dee91e82 18108 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18109 continue;
18110 }
18111 }
18112
0d99eb77 18113 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18114 Skip their other children. */
18115 if (!load_all
18116 && cu->language == language_cplus
18117 && parent_die != NULL
18118 && parent_die->tag == DW_TAG_subprogram)
18119 {
dee91e82 18120 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18121 continue;
18122 }
18123
5afb4e99
DJ
18124 /* Check whether this DIE is interesting enough to save. Normally
18125 we would not be interested in members here, but there may be
18126 later variables referencing them via DW_AT_specification (for
18127 static members). */
18128 if (!load_all
18129 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18130 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18131 && abbrev->tag != DW_TAG_enumerator
18132 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18133 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18134 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18135 && abbrev->tag != DW_TAG_variable
5afb4e99 18136 && abbrev->tag != DW_TAG_namespace
f55ee35c 18137 && abbrev->tag != DW_TAG_module
95554aad 18138 && abbrev->tag != DW_TAG_member
74921315
KS
18139 && abbrev->tag != DW_TAG_imported_unit
18140 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18141 {
18142 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18143 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18144 continue;
18145 }
18146
6f06d47b
YQ
18147 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18148 abbrev);
cd9983dd 18149
48fbe735 18150 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18151
18152 /* This two-pass algorithm for processing partial symbols has a
18153 high cost in cache pressure. Thus, handle some simple cases
18154 here which cover the majority of C partial symbols. DIEs
18155 which neither have specification tags in them, nor could have
18156 specification tags elsewhere pointing at them, can simply be
18157 processed and discarded.
18158
18159 This segment is also optional; scan_partial_symbols and
18160 add_partial_symbol will handle these DIEs if we chain
18161 them in normally. When compilers which do not emit large
18162 quantities of duplicate debug information are more common,
18163 this code can probably be removed. */
18164
18165 /* Any complete simple types at the top level (pretty much all
18166 of them, for a language without namespaces), can be processed
18167 directly. */
18168 if (parent_die == NULL
cd9983dd
YQ
18169 && pdi.has_specification == 0
18170 && pdi.is_declaration == 0
18171 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18172 || pdi.tag == DW_TAG_base_type
18173 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18174 {
cd9983dd 18175 if (building_psymtab && pdi.name != NULL)
31edb802 18176 add_psymbol_to_list (pdi.name, false,
79748972 18177 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18178 psymbol_placement::STATIC,
1762568f 18179 0, cu->language, objfile);
cd9983dd 18180 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18181 continue;
18182 }
18183
d8228535
JK
18184 /* The exception for DW_TAG_typedef with has_children above is
18185 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18186 type_name_or_error will error on such types later.
d8228535
JK
18187
18188 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18189 it could not find the child DIEs referenced later, this is checked
18190 above. In correct DWARF DW_TAG_typedef should have no children. */
18191
cd9983dd 18192 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18193 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18194 "- DIE at %s [in module %s]"),
cd9983dd 18195 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18196
72bf9492
DJ
18197 /* If we're at the second level, and we're an enumerator, and
18198 our parent has no specification (meaning possibly lives in a
18199 namespace elsewhere), then we can add the partial symbol now
18200 instead of queueing it. */
cd9983dd 18201 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18202 && parent_die != NULL
18203 && parent_die->die_parent == NULL
18204 && parent_die->tag == DW_TAG_enumeration_type
18205 && parent_die->has_specification == 0)
18206 {
cd9983dd 18207 if (pdi.name == NULL)
b98664d3 18208 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18209 else if (building_psymtab)
31edb802 18210 add_psymbol_to_list (pdi.name, false,
79748972 18211 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18212 cu->language == language_cplus
75aedd27
TT
18213 ? psymbol_placement::GLOBAL
18214 : psymbol_placement::STATIC,
1762568f 18215 0, cu->language, objfile);
72bf9492 18216
cd9983dd 18217 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18218 continue;
18219 }
18220
cd9983dd 18221 struct partial_die_info *part_die
6f06d47b 18222 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18223
72bf9492
DJ
18224 /* We'll save this DIE so link it in. */
18225 part_die->die_parent = parent_die;
18226 part_die->die_sibling = NULL;
18227 part_die->die_child = NULL;
18228
18229 if (last_die && last_die == parent_die)
18230 last_die->die_child = part_die;
18231 else if (last_die)
18232 last_die->die_sibling = part_die;
18233
18234 last_die = part_die;
18235
18236 if (first_die == NULL)
18237 first_die = part_die;
18238
18239 /* Maybe add the DIE to the hash table. Not all DIEs that we
18240 find interesting need to be in the hash table, because we
18241 also have the parent/sibling/child chains; only those that we
18242 might refer to by offset later during partial symbol reading.
18243
18244 For now this means things that might have be the target of a
18245 DW_AT_specification, DW_AT_abstract_origin, or
18246 DW_AT_extension. DW_AT_extension will refer only to
18247 namespaces; DW_AT_abstract_origin refers to functions (and
18248 many things under the function DIE, but we do not recurse
18249 into function DIEs during partial symbol reading) and
18250 possibly variables as well; DW_AT_specification refers to
18251 declarations. Declarations ought to have the DW_AT_declaration
18252 flag. It happens that GCC forgets to put it in sometimes, but
18253 only for functions, not for types.
18254
18255 Adding more things than necessary to the hash table is harmless
18256 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18257 wasted time in find_partial_die, when we reread the compilation
18258 unit with load_all_dies set. */
72bf9492 18259
5afb4e99 18260 if (load_all
72929c62 18261 || abbrev->tag == DW_TAG_constant
5afb4e99 18262 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18263 || abbrev->tag == DW_TAG_variable
18264 || abbrev->tag == DW_TAG_namespace
18265 || part_die->is_declaration)
18266 {
18267 void **slot;
18268
18269 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18270 to_underlying (part_die->sect_off),
18271 INSERT);
72bf9492
DJ
18272 *slot = part_die;
18273 }
18274
72bf9492 18275 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18276 we have no reason to follow the children of structures; for other
98bfdba5
PA
18277 languages we have to, so that we can get at method physnames
18278 to infer fully qualified class names, for DW_AT_specification,
18279 and for C++ template arguments. For C++, we also look one level
18280 inside functions to find template arguments (if the name of the
18281 function does not already contain the template arguments).
bc30ff58 18282
0a4b0913
AB
18283 For Ada and Fortran, we need to scan the children of subprograms
18284 and lexical blocks as well because these languages allow the
18285 definition of nested entities that could be interesting for the
18286 debugger, such as nested subprograms for instance. */
72bf9492 18287 if (last_die->has_children
5afb4e99
DJ
18288 && (load_all
18289 || last_die->tag == DW_TAG_namespace
f55ee35c 18290 || last_die->tag == DW_TAG_module
72bf9492 18291 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18292 || (cu->language == language_cplus
18293 && last_die->tag == DW_TAG_subprogram
18294 && (last_die->name == NULL
18295 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18296 || (cu->language != language_c
18297 && (last_die->tag == DW_TAG_class_type
680b30c7 18298 || last_die->tag == DW_TAG_interface_type
72bf9492 18299 || last_die->tag == DW_TAG_structure_type
bc30ff58 18300 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18301 || ((cu->language == language_ada
18302 || cu->language == language_fortran)
bc30ff58
JB
18303 && (last_die->tag == DW_TAG_subprogram
18304 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18305 {
18306 nesting_level++;
18307 parent_die = last_die;
18308 continue;
18309 }
18310
18311 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18312 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18313
18314 /* Back to the top, do it again. */
18315 }
18316}
18317
6f06d47b
YQ
18318partial_die_info::partial_die_info (sect_offset sect_off_,
18319 struct abbrev_info *abbrev)
18320 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18321{
18322}
18323
35cc7ed7
YQ
18324/* Read a minimal amount of information into the minimal die structure.
18325 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18326
48fbe735
YQ
18327const gdb_byte *
18328partial_die_info::read (const struct die_reader_specs *reader,
18329 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18330{
dee91e82 18331 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18332 struct dwarf2_per_objfile *dwarf2_per_objfile
18333 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18334 unsigned int i;
c5aa993b 18335 int has_low_pc_attr = 0;
c906108c 18336 int has_high_pc_attr = 0;
91da1414 18337 int high_pc_relative = 0;
c906108c 18338
fd0a254f 18339 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18340 {
e7da7f8f 18341 attribute attr;
18a8505e 18342 bool need_reprocess;
e7da7f8f 18343 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18344 info_ptr, &need_reprocess);
18345 /* String and address offsets that need to do the reprocessing have
18346 already been read at this point, so there is no need to wait until
18347 the loop terminates to do the reprocessing. */
18348 if (need_reprocess)
e7da7f8f 18349 read_attribute_reprocess (reader, &attr);
c906108c 18350 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18351 partial symbol table. */
c906108c
SS
18352 switch (attr.name)
18353 {
18354 case DW_AT_name:
48fbe735 18355 switch (tag)
71c25dea
TT
18356 {
18357 case DW_TAG_compile_unit:
95554aad 18358 case DW_TAG_partial_unit:
348e048f 18359 case DW_TAG_type_unit:
71c25dea
TT
18360 /* Compilation units have a DW_AT_name that is a filename, not
18361 a source language identifier. */
18362 case DW_TAG_enumeration_type:
18363 case DW_TAG_enumerator:
18364 /* These tags always have simple identifiers already; no need
18365 to canonicalize them. */
48fbe735 18366 name = DW_STRING (&attr);
71c25dea
TT
18367 break;
18368 default:
48fbe735
YQ
18369 {
18370 struct objfile *objfile = dwarf2_per_objfile->objfile;
18371
18372 name
be1e3d3e 18373 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18374 }
71c25dea
TT
18375 break;
18376 }
c906108c 18377 break;
31ef98ae 18378 case DW_AT_linkage_name:
c906108c 18379 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18380 /* Note that both forms of linkage name might appear. We
18381 assume they will be the same, and we only store the last
18382 one we see. */
e61108c9 18383 linkage_name = attr.value_as_string ();
787de330
TT
18384 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18385 See https://github.com/rust-lang/rust/issues/32925. */
18386 if (cu->language == language_rust && linkage_name != NULL
18387 && strchr (linkage_name, '{') != NULL)
18388 linkage_name = NULL;
c906108c
SS
18389 break;
18390 case DW_AT_low_pc:
18391 has_low_pc_attr = 1;
cd6c91b4 18392 lowpc = attr.value_as_address ();
c906108c
SS
18393 break;
18394 case DW_AT_high_pc:
18395 has_high_pc_attr = 1;
cd6c91b4
TT
18396 highpc = attr.value_as_address ();
18397 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18398 high_pc_relative = 1;
c906108c
SS
18399 break;
18400 case DW_AT_location:
0963b4bd 18401 /* Support the .debug_loc offsets. */
4fc6c0d5 18402 if (attr.form_is_block ())
8e19ed76 18403 {
48fbe735 18404 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18405 }
cd6c91b4 18406 else if (attr.form_is_section_offset ())
8e19ed76 18407 {
4d3c2250 18408 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18409 }
18410 else
18411 {
4d3c2250
KB
18412 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18413 "partial symbol information");
8e19ed76 18414 }
c906108c 18415 break;
c906108c 18416 case DW_AT_external:
48fbe735 18417 is_external = DW_UNSND (&attr);
c906108c
SS
18418 break;
18419 case DW_AT_declaration:
48fbe735 18420 is_declaration = DW_UNSND (&attr);
c906108c
SS
18421 break;
18422 case DW_AT_type:
48fbe735 18423 has_type = 1;
c906108c
SS
18424 break;
18425 case DW_AT_abstract_origin:
18426 case DW_AT_specification:
72bf9492 18427 case DW_AT_extension:
48fbe735 18428 has_specification = 1;
0826b30a 18429 spec_offset = attr.get_ref_die_offset ();
48fbe735 18430 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18431 || cu->per_cu->is_dwz);
c906108c
SS
18432 break;
18433 case DW_AT_sibling:
18434 /* Ignore absolute siblings, they might point outside of
18435 the current compile unit. */
18436 if (attr.form == DW_FORM_ref_addr)
b98664d3 18437 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18438 else
b9502d3f 18439 {
48fbe735 18440 const gdb_byte *buffer = reader->buffer;
0826b30a 18441 sect_offset off = attr.get_ref_die_offset ();
9c541725 18442 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18443
18444 if (sibling_ptr < info_ptr)
b98664d3 18445 complaint (_("DW_AT_sibling points backwards"));
22869d73 18446 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18447 reader->die_section->overflow_complaint ();
b9502d3f 18448 else
48fbe735 18449 sibling = sibling_ptr;
b9502d3f 18450 }
c906108c 18451 break;
fa4028e9 18452 case DW_AT_byte_size:
48fbe735 18453 has_byte_size = 1;
fa4028e9 18454 break;
ff908ebf 18455 case DW_AT_const_value:
48fbe735 18456 has_const_value = 1;
ff908ebf 18457 break;
68511cec
CES
18458 case DW_AT_calling_convention:
18459 /* DWARF doesn't provide a way to identify a program's source-level
18460 entry point. DW_AT_calling_convention attributes are only meant
18461 to describe functions' calling conventions.
18462
18463 However, because it's a necessary piece of information in
0c1b455e
TT
18464 Fortran, and before DWARF 4 DW_CC_program was the only
18465 piece of debugging information whose definition refers to
18466 a 'main program' at all, several compilers marked Fortran
18467 main programs with DW_CC_program --- even when those
18468 functions use the standard calling conventions.
18469
18470 Although DWARF now specifies a way to provide this
18471 information, we support this practice for backward
18472 compatibility. */
68511cec 18473 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18474 && cu->language == language_fortran)
48fbe735 18475 main_subprogram = 1;
68511cec 18476 break;
481860b3
GB
18477 case DW_AT_inline:
18478 if (DW_UNSND (&attr) == DW_INL_inlined
18479 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18480 may_be_inlined = 1;
481860b3 18481 break;
95554aad
TT
18482
18483 case DW_AT_import:
48fbe735 18484 if (tag == DW_TAG_imported_unit)
36586728 18485 {
0826b30a 18486 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18487 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18488 || cu->per_cu->is_dwz);
18489 }
95554aad
TT
18490 break;
18491
0c1b455e 18492 case DW_AT_main_subprogram:
48fbe735 18493 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18494 break;
18495
05caa1d2
TT
18496 case DW_AT_ranges:
18497 {
18498 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18499 but that requires a full DIE, so instead we just
18500 reimplement it. */
18501 int need_ranges_base = tag != DW_TAG_compile_unit;
18502 unsigned int ranges_offset = (DW_UNSND (&attr)
18503 + (need_ranges_base
18504 ? cu->ranges_base
18505 : 0));
18506
18507 /* Value of the DW_AT_ranges attribute is the offset in the
18508 .debug_ranges section. */
18509 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18510 nullptr))
18511 has_pc_info = 1;
18512 }
18513 break;
18514
c906108c
SS
18515 default:
18516 break;
18517 }
18518 }
18519
10d06d82
TT
18520 /* For Ada, if both the name and the linkage name appear, we prefer
18521 the latter. This lets "catch exception" work better, regardless
18522 of the order in which the name and linkage name were emitted.
18523 Really, though, this is just a workaround for the fact that gdb
18524 doesn't store both the name and the linkage name. */
18525 if (cu->language == language_ada && linkage_name != nullptr)
18526 name = linkage_name;
18527
91da1414 18528 if (high_pc_relative)
48fbe735 18529 highpc += lowpc;
91da1414 18530
9373cf26
JK
18531 if (has_low_pc_attr && has_high_pc_attr)
18532 {
18533 /* When using the GNU linker, .gnu.linkonce. sections are used to
18534 eliminate duplicate copies of functions and vtables and such.
18535 The linker will arbitrarily choose one and discard the others.
18536 The AT_*_pc values for such functions refer to local labels in
18537 these sections. If the section from that file was discarded, the
18538 labels are not in the output, so the relocs get a value of 0.
18539 If this is a discarded function, mark the pc bounds as invalid,
18540 so that GDB will ignore it. */
48fbe735 18541 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18542 {
48fbe735 18543 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18544 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18545
b98664d3 18546 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18547 "for DIE at %s [in module %s]"),
48fbe735
YQ
18548 paddress (gdbarch, lowpc),
18549 sect_offset_str (sect_off),
9d8780f0 18550 objfile_name (objfile));
9373cf26
JK
18551 }
18552 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18553 else if (lowpc >= highpc)
9373cf26 18554 {
48fbe735 18555 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18556 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18557
b98664d3 18558 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18559 "for DIE at %s [in module %s]"),
48fbe735
YQ
18560 paddress (gdbarch, lowpc),
18561 paddress (gdbarch, highpc),
18562 sect_offset_str (sect_off),
9c541725 18563 objfile_name (objfile));
9373cf26
JK
18564 }
18565 else
48fbe735 18566 has_pc_info = 1;
9373cf26 18567 }
85cbf3d3 18568
c906108c
SS
18569 return info_ptr;
18570}
18571
72bf9492
DJ
18572/* Find a cached partial DIE at OFFSET in CU. */
18573
d590ff25
YQ
18574struct partial_die_info *
18575dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18576{
18577 struct partial_die_info *lookup_die = NULL;
6f06d47b 18578 struct partial_die_info part_die (sect_off);
72bf9492 18579
9a3c8263 18580 lookup_die = ((struct partial_die_info *)
d590ff25 18581 htab_find_with_hash (partial_dies, &part_die,
9c541725 18582 to_underlying (sect_off)));
72bf9492 18583
72bf9492
DJ
18584 return lookup_die;
18585}
18586
348e048f
DE
18587/* Find a partial DIE at OFFSET, which may or may not be in CU,
18588 except in the case of .debug_types DIEs which do not reference
18589 outside their CU (they do however referencing other types via
55f1336d 18590 DW_FORM_ref_sig8). */
72bf9492 18591
122cf0f2 18592static const struct cu_partial_die_info
9c541725 18593find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18594{
518817b3
SM
18595 struct dwarf2_per_objfile *dwarf2_per_objfile
18596 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18597 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18598 struct dwarf2_per_cu_data *per_cu = NULL;
18599 struct partial_die_info *pd = NULL;
72bf9492 18600
36586728 18601 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18602 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18603 {
d590ff25 18604 pd = cu->find_partial_die (sect_off);
5afb4e99 18605 if (pd != NULL)
fb816e8b 18606 return { cu, pd };
0d99eb77
DE
18607 /* We missed recording what we needed.
18608 Load all dies and try again. */
18609 per_cu = cu->per_cu;
5afb4e99 18610 }
0d99eb77
DE
18611 else
18612 {
18613 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18614 if (cu->per_cu->is_debug_types)
0d99eb77 18615 {
9d8780f0
SM
18616 error (_("Dwarf Error: Type Unit at offset %s contains"
18617 " external reference to offset %s [in module %s].\n"),
18618 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18619 bfd_get_filename (objfile->obfd));
18620 }
9c541725 18621 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18622 dwarf2_per_objfile);
72bf9492 18623
0d99eb77
DE
18624 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18625 load_partial_comp_unit (per_cu);
ae038cb0 18626
0d99eb77 18627 per_cu->cu->last_used = 0;
d590ff25 18628 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18629 }
5afb4e99 18630
dee91e82
DE
18631 /* If we didn't find it, and not all dies have been loaded,
18632 load them all and try again. */
18633
5afb4e99
DJ
18634 if (pd == NULL && per_cu->load_all_dies == 0)
18635 {
5afb4e99 18636 per_cu->load_all_dies = 1;
fd820528
DE
18637
18638 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18639 THIS_CU->cu may already be in use. So we can't just free it and
18640 replace its DIEs with the ones we read in. Instead, we leave those
18641 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18642 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18643 set. */
dee91e82 18644 load_partial_comp_unit (per_cu);
5afb4e99 18645
d590ff25 18646 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18647 }
18648
18649 if (pd == NULL)
18650 internal_error (__FILE__, __LINE__,
9d8780f0 18651 _("could not find partial DIE %s "
3e43a32a 18652 "in cache [from module %s]\n"),
9d8780f0 18653 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18654 return { per_cu->cu, pd };
72bf9492
DJ
18655}
18656
abc72ce4
DE
18657/* See if we can figure out if the class lives in a namespace. We do
18658 this by looking for a member function; its demangled name will
18659 contain namespace info, if there is any. */
18660
18661static void
18662guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18663 struct dwarf2_cu *cu)
18664{
18665 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18666 what template types look like, because the demangler
18667 frequently doesn't give the same name as the debug info. We
18668 could fix this by only using the demangled name to get the
18669 prefix (but see comment in read_structure_type). */
18670
18671 struct partial_die_info *real_pdi;
18672 struct partial_die_info *child_pdi;
18673
18674 /* If this DIE (this DIE's specification, if any) has a parent, then
18675 we should not do this. We'll prepend the parent's fully qualified
18676 name when we create the partial symbol. */
18677
18678 real_pdi = struct_pdi;
18679 while (real_pdi->has_specification)
fb816e8b 18680 {
122cf0f2
AB
18681 auto res = find_partial_die (real_pdi->spec_offset,
18682 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18683 real_pdi = res.pdi;
18684 cu = res.cu;
18685 }
abc72ce4
DE
18686
18687 if (real_pdi->die_parent != NULL)
18688 return;
18689
18690 for (child_pdi = struct_pdi->die_child;
18691 child_pdi != NULL;
18692 child_pdi = child_pdi->die_sibling)
18693 {
18694 if (child_pdi->tag == DW_TAG_subprogram
18695 && child_pdi->linkage_name != NULL)
18696 {
43816ebc
TT
18697 gdb::unique_xmalloc_ptr<char> actual_class_name
18698 (language_class_name_from_physname (cu->language_defn,
18699 child_pdi->linkage_name));
abc72ce4
DE
18700 if (actual_class_name != NULL)
18701 {
518817b3 18702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18703 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18704 }
18705 break;
18706 }
18707 }
18708}
18709
25c11aca
TV
18710/* Return true if a DIE with TAG may have the DW_AT_const_value
18711 attribute. */
18712
18713static bool
18714can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18715{
18716 switch (tag)
18717 {
18718 case DW_TAG_constant:
18719 case DW_TAG_enumerator:
18720 case DW_TAG_formal_parameter:
18721 case DW_TAG_template_value_param:
18722 case DW_TAG_variable:
18723 return true;
18724 }
18725
18726 return false;
18727}
18728
52356b79
YQ
18729void
18730partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18731{
abc72ce4
DE
18732 /* Once we've fixed up a die, there's no point in doing so again.
18733 This also avoids a memory leak if we were to call
18734 guess_partial_die_structure_name multiple times. */
52356b79 18735 if (fixup_called)
abc72ce4
DE
18736 return;
18737
72bf9492
DJ
18738 /* If we found a reference attribute and the DIE has no name, try
18739 to find a name in the referred to DIE. */
18740
52356b79 18741 if (name == NULL && has_specification)
72bf9492
DJ
18742 {
18743 struct partial_die_info *spec_die;
72bf9492 18744
122cf0f2 18745 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18746 spec_die = res.pdi;
18747 cu = res.cu;
72bf9492 18748
52356b79 18749 spec_die->fixup (cu);
72bf9492
DJ
18750
18751 if (spec_die->name)
18752 {
52356b79 18753 name = spec_die->name;
72bf9492
DJ
18754
18755 /* Copy DW_AT_external attribute if it is set. */
18756 if (spec_die->is_external)
52356b79 18757 is_external = spec_die->is_external;
72bf9492
DJ
18758 }
18759 }
18760
25c11aca
TV
18761 if (!has_const_value && has_specification
18762 && can_have_DW_AT_const_value_p (tag))
18763 {
18764 struct partial_die_info *spec_die;
18765
18766 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18767 spec_die = res.pdi;
18768 cu = res.cu;
18769
18770 spec_die->fixup (cu);
18771
18772 if (spec_die->has_const_value)
18773 {
18774 /* Copy DW_AT_const_value attribute if it is set. */
18775 has_const_value = spec_die->has_const_value;
18776 }
18777 }
18778
72bf9492 18779 /* Set default names for some unnamed DIEs. */
72bf9492 18780
52356b79
YQ
18781 if (name == NULL && tag == DW_TAG_namespace)
18782 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18783
abc72ce4
DE
18784 /* If there is no parent die to provide a namespace, and there are
18785 children, see if we can determine the namespace from their linkage
122d1940 18786 name. */
abc72ce4 18787 if (cu->language == language_cplus
fd5866f6 18788 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18789 && die_parent == NULL
18790 && has_children
18791 && (tag == DW_TAG_class_type
18792 || tag == DW_TAG_structure_type
18793 || tag == DW_TAG_union_type))
18794 guess_partial_die_structure_name (this, cu);
abc72ce4 18795
53832f31
TT
18796 /* GCC might emit a nameless struct or union that has a linkage
18797 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18798 if (name == NULL
18799 && (tag == DW_TAG_class_type
18800 || tag == DW_TAG_interface_type
18801 || tag == DW_TAG_structure_type
18802 || tag == DW_TAG_union_type)
18803 && linkage_name != NULL)
53832f31 18804 {
43816ebc
TT
18805 gdb::unique_xmalloc_ptr<char> demangled
18806 (gdb_demangle (linkage_name, DMGL_TYPES));
18807 if (demangled != nullptr)
53832f31 18808 {
96408a79
SA
18809 const char *base;
18810
18811 /* Strip any leading namespaces/classes, keep only the base name.
18812 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18813 base = strrchr (demangled.get (), ':');
18814 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18815 base++;
18816 else
43816ebc 18817 base = demangled.get ();
96408a79 18818
518817b3 18819 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18820 name = objfile->intern (base);
53832f31
TT
18821 }
18822 }
18823
52356b79 18824 fixup_called = 1;
72bf9492
DJ
18825}
18826
41144253 18827/* Read the .debug_loclists header contents from the given SECTION in the
18828 HEADER. */
18829static void
18830read_loclist_header (struct loclist_header *header,
18831 struct dwarf2_section_info *section)
18832{
18833 unsigned int bytes_read;
18834 bfd *abfd = section->get_bfd_owner ();
18835 const gdb_byte *info_ptr = section->buffer;
18836 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18837 info_ptr += bytes_read;
18838 header->version = read_2_bytes (abfd, info_ptr);
18839 info_ptr += 2;
18840 header->addr_size = read_1_byte (abfd, info_ptr);
18841 info_ptr += 1;
18842 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18843 info_ptr += 1;
18844 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18845}
18846
18847/* Return the DW_AT_loclists_base value for the CU. */
18848static ULONGEST
18849lookup_loclist_base (struct dwarf2_cu *cu)
18850{
18851 /* For the .dwo unit, the loclist_base points to the first offset following
18852 the header. The header consists of the following entities-
18853 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18854 bit format)
18855 2. version (2 bytes)
18856 3. address size (1 byte)
18857 4. segment selector size (1 byte)
18858 5. offset entry count (4 bytes)
18859 These sizes are derived as per the DWARFv5 standard. */
18860 if (cu->dwo_unit != nullptr)
18861 {
18862 if (cu->header.initial_length_size == 4)
18863 return LOCLIST_HEADER_SIZE32;
18864 return LOCLIST_HEADER_SIZE64;
18865 }
18866 return cu->loclist_base;
18867}
18868
18869/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18870 array of offsets in the .debug_loclists section. */
18871static CORE_ADDR
18872read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18873{
18874 struct dwarf2_per_objfile *dwarf2_per_objfile
18875 = cu->per_cu->dwarf2_per_objfile;
18876 struct objfile *objfile = dwarf2_per_objfile->objfile;
18877 bfd *abfd = objfile->obfd;
18878 ULONGEST loclist_base = lookup_loclist_base (cu);
18879 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18880
18881 section->read (objfile);
18882 if (section->buffer == NULL)
18883 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18884 "section [in module %s]"), objfile_name (objfile));
18885 struct loclist_header header;
18886 read_loclist_header (&header, section);
18887 if (loclist_index >= header.offset_entry_count)
18888 complaint (_("DW_FORM_loclistx pointing outside of "
18889 ".debug_loclists offset array [in module %s]"),
18890 objfile_name (objfile));
18891 if (loclist_base + loclist_index * cu->header.offset_size
18892 >= section->size)
18893 complaint (_("DW_FORM_loclistx pointing outside of "
18894 ".debug_loclists section [in module %s]"),
18895 objfile_name (objfile));
18896 const gdb_byte *info_ptr
18897 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18898
18899 if (cu->header.offset_size == 4)
18900 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18901 else
18902 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18903}
18904
18a8505e
AT
18905/* Process the attributes that had to be skipped in the first round. These
18906 attributes are the ones that need str_offsets_base or addr_base attributes.
18907 They could not have been processed in the first round, because at the time
18908 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18909static void
18910read_attribute_reprocess (const struct die_reader_specs *reader,
18911 struct attribute *attr)
18a8505e
AT
18912{
18913 struct dwarf2_cu *cu = reader->cu;
18914 switch (attr->form)
18915 {
18916 case DW_FORM_addrx:
18917 case DW_FORM_GNU_addr_index:
18918 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18919 break;
41144253 18920 case DW_FORM_loclistx:
18921 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18922 break;
18a8505e
AT
18923 case DW_FORM_strx:
18924 case DW_FORM_strx1:
18925 case DW_FORM_strx2:
18926 case DW_FORM_strx3:
18927 case DW_FORM_strx4:
18928 case DW_FORM_GNU_str_index:
18929 {
18930 unsigned int str_index = DW_UNSND (attr);
18931 if (reader->dwo_file != NULL)
18932 {
18933 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18934 DW_STRING_IS_CANONICAL (attr) = 0;
18935 }
18936 else
18937 {
18938 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18939 DW_STRING_IS_CANONICAL (attr) = 0;
18940 }
18941 break;
18942 }
18943 default:
18944 gdb_assert_not_reached (_("Unexpected DWARF form."));
18945 }
18946}
18947
a8329558 18948/* Read an attribute value described by an attribute form. */
c906108c 18949
d521ce57 18950static const gdb_byte *
dee91e82
DE
18951read_attribute_value (const struct die_reader_specs *reader,
18952 struct attribute *attr, unsigned form,
18a8505e
AT
18953 LONGEST implicit_const, const gdb_byte *info_ptr,
18954 bool *need_reprocess)
c906108c 18955{
dee91e82 18956 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18957 struct dwarf2_per_objfile *dwarf2_per_objfile
18958 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18959 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 18960 bfd *abfd = reader->abfd;
e7c27a73 18961 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18962 unsigned int bytes_read;
18963 struct dwarf_block *blk;
18a8505e 18964 *need_reprocess = false;
c906108c 18965
aead7601 18966 attr->form = (enum dwarf_form) form;
a8329558 18967 switch (form)
c906108c 18968 {
c906108c 18969 case DW_FORM_ref_addr:
ae411497 18970 if (cu->header.version == 2)
c8a7a66f
TT
18971 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18972 &bytes_read);
ae411497 18973 else
8266302d
TT
18974 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18975 &bytes_read);
ae411497
TT
18976 info_ptr += bytes_read;
18977 break;
36586728 18978 case DW_FORM_GNU_ref_alt:
8266302d 18979 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18980 info_ptr += bytes_read;
18981 break;
ae411497 18982 case DW_FORM_addr:
08feed99
TT
18983 {
18984 struct gdbarch *gdbarch = objfile->arch ();
18985 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18986 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18987 info_ptr += bytes_read;
18988 }
c906108c
SS
18989 break;
18990 case DW_FORM_block2:
7b5a2f43 18991 blk = dwarf_alloc_block (cu);
c906108c
SS
18992 blk->size = read_2_bytes (abfd, info_ptr);
18993 info_ptr += 2;
18994 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18995 info_ptr += blk->size;
18996 DW_BLOCK (attr) = blk;
18997 break;
18998 case DW_FORM_block4:
7b5a2f43 18999 blk = dwarf_alloc_block (cu);
c906108c
SS
19000 blk->size = read_4_bytes (abfd, info_ptr);
19001 info_ptr += 4;
19002 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19003 info_ptr += blk->size;
19004 DW_BLOCK (attr) = blk;
19005 break;
19006 case DW_FORM_data2:
19007 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19008 info_ptr += 2;
19009 break;
19010 case DW_FORM_data4:
19011 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19012 info_ptr += 4;
19013 break;
19014 case DW_FORM_data8:
19015 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19016 info_ptr += 8;
19017 break;
0224619f
JK
19018 case DW_FORM_data16:
19019 blk = dwarf_alloc_block (cu);
19020 blk->size = 16;
19021 blk->data = read_n_bytes (abfd, info_ptr, 16);
19022 info_ptr += 16;
19023 DW_BLOCK (attr) = blk;
19024 break;
2dc7f7b3 19025 case DW_FORM_sec_offset:
8266302d 19026 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19027 info_ptr += bytes_read;
19028 break;
41144253 19029 case DW_FORM_loclistx:
19030 {
19031 *need_reprocess = true;
19032 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19033 info_ptr += bytes_read;
19034 }
19035 break;
c906108c 19036 case DW_FORM_string:
9b1c24c8 19037 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19038 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19039 info_ptr += bytes_read;
19040 break;
4bdf3d34 19041 case DW_FORM_strp:
36586728
TT
19042 if (!cu->per_cu->is_dwz)
19043 {
ed2dc618
SM
19044 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19045 abfd, info_ptr, cu_header,
36586728
TT
19046 &bytes_read);
19047 DW_STRING_IS_CANONICAL (attr) = 0;
19048 info_ptr += bytes_read;
19049 break;
19050 }
19051 /* FALLTHROUGH */
43988095
JK
19052 case DW_FORM_line_strp:
19053 if (!cu->per_cu->is_dwz)
19054 {
86c0bb4c
TT
19055 DW_STRING (attr)
19056 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19057 &bytes_read);
43988095
JK
19058 DW_STRING_IS_CANONICAL (attr) = 0;
19059 info_ptr += bytes_read;
19060 break;
19061 }
19062 /* FALLTHROUGH */
36586728
TT
19063 case DW_FORM_GNU_strp_alt:
19064 {
ed2dc618 19065 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
19066 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19067 &bytes_read);
36586728 19068
0314b390 19069 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19070 DW_STRING_IS_CANONICAL (attr) = 0;
19071 info_ptr += bytes_read;
19072 }
4bdf3d34 19073 break;
2dc7f7b3 19074 case DW_FORM_exprloc:
c906108c 19075 case DW_FORM_block:
7b5a2f43 19076 blk = dwarf_alloc_block (cu);
c906108c
SS
19077 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19078 info_ptr += bytes_read;
19079 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19080 info_ptr += blk->size;
19081 DW_BLOCK (attr) = blk;
19082 break;
19083 case DW_FORM_block1:
7b5a2f43 19084 blk = dwarf_alloc_block (cu);
c906108c
SS
19085 blk->size = read_1_byte (abfd, info_ptr);
19086 info_ptr += 1;
19087 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19088 info_ptr += blk->size;
19089 DW_BLOCK (attr) = blk;
19090 break;
19091 case DW_FORM_data1:
19092 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19093 info_ptr += 1;
19094 break;
19095 case DW_FORM_flag:
19096 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19097 info_ptr += 1;
19098 break;
2dc7f7b3
TT
19099 case DW_FORM_flag_present:
19100 DW_UNSND (attr) = 1;
19101 break;
c906108c
SS
19102 case DW_FORM_sdata:
19103 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19104 info_ptr += bytes_read;
19105 break;
19106 case DW_FORM_udata:
18a8505e 19107 case DW_FORM_rnglistx:
c906108c
SS
19108 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19109 info_ptr += bytes_read;
19110 break;
19111 case DW_FORM_ref1:
9c541725 19112 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19113 + read_1_byte (abfd, info_ptr));
c906108c
SS
19114 info_ptr += 1;
19115 break;
19116 case DW_FORM_ref2:
9c541725 19117 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19118 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19119 info_ptr += 2;
19120 break;
19121 case DW_FORM_ref4:
9c541725 19122 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19123 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19124 info_ptr += 4;
19125 break;
613e1657 19126 case DW_FORM_ref8:
9c541725 19127 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19128 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19129 info_ptr += 8;
19130 break;
55f1336d 19131 case DW_FORM_ref_sig8:
ac9ec31b 19132 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19133 info_ptr += 8;
19134 break;
c906108c 19135 case DW_FORM_ref_udata:
9c541725 19136 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19137 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19138 info_ptr += bytes_read;
19139 break;
c906108c 19140 case DW_FORM_indirect:
a8329558
KW
19141 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19142 info_ptr += bytes_read;
43988095
JK
19143 if (form == DW_FORM_implicit_const)
19144 {
19145 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19146 info_ptr += bytes_read;
19147 }
19148 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19149 info_ptr, need_reprocess);
43988095
JK
19150 break;
19151 case DW_FORM_implicit_const:
19152 DW_SND (attr) = implicit_const;
a8329558 19153 break;
336d760d 19154 case DW_FORM_addrx:
3019eac3 19155 case DW_FORM_GNU_addr_index:
18a8505e
AT
19156 *need_reprocess = true;
19157 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19158 info_ptr += bytes_read;
19159 break;
cf532bd1 19160 case DW_FORM_strx:
15f18d14
AT
19161 case DW_FORM_strx1:
19162 case DW_FORM_strx2:
19163 case DW_FORM_strx3:
19164 case DW_FORM_strx4:
3019eac3 19165 case DW_FORM_GNU_str_index:
3019eac3 19166 {
15f18d14
AT
19167 ULONGEST str_index;
19168 if (form == DW_FORM_strx1)
19169 {
19170 str_index = read_1_byte (abfd, info_ptr);
19171 info_ptr += 1;
19172 }
19173 else if (form == DW_FORM_strx2)
19174 {
19175 str_index = read_2_bytes (abfd, info_ptr);
19176 info_ptr += 2;
19177 }
19178 else if (form == DW_FORM_strx3)
19179 {
19180 str_index = read_3_bytes (abfd, info_ptr);
19181 info_ptr += 3;
19182 }
19183 else if (form == DW_FORM_strx4)
19184 {
19185 str_index = read_4_bytes (abfd, info_ptr);
19186 info_ptr += 4;
19187 }
19188 else
19189 {
19190 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19191 info_ptr += bytes_read;
19192 }
18a8505e
AT
19193 *need_reprocess = true;
19194 DW_UNSND (attr) = str_index;
19195 }
3019eac3 19196 break;
c906108c 19197 default:
8a3fe4f8 19198 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19199 dwarf_form_name (form),
19200 bfd_get_filename (abfd));
c906108c 19201 }
28e94949 19202
36586728 19203 /* Super hack. */
cd6c91b4 19204 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19205 attr->form = DW_FORM_GNU_ref_alt;
19206
28e94949
JB
19207 /* We have seen instances where the compiler tried to emit a byte
19208 size attribute of -1 which ended up being encoded as an unsigned
19209 0xffffffff. Although 0xffffffff is technically a valid size value,
19210 an object of this size seems pretty unlikely so we can relatively
19211 safely treat these cases as if the size attribute was invalid and
19212 treat them as zero by default. */
19213 if (attr->name == DW_AT_byte_size
19214 && form == DW_FORM_data4
19215 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19216 {
19217 complaint
b98664d3 19218 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19219 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19220 DW_UNSND (attr) = 0;
19221 }
28e94949 19222
c906108c
SS
19223 return info_ptr;
19224}
19225
a8329558
KW
19226/* Read an attribute described by an abbreviated attribute. */
19227
d521ce57 19228static const gdb_byte *
dee91e82
DE
19229read_attribute (const struct die_reader_specs *reader,
19230 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19231 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19232{
19233 attr->name = abbrev->name;
43988095 19234 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19235 abbrev->implicit_const, info_ptr,
19236 need_reprocess);
a8329558
KW
19237}
19238
43988095
JK
19239/* Return pointer to string at .debug_str offset STR_OFFSET. */
19240
19241static const char *
ed2dc618 19242read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19243 LONGEST str_offset)
43988095 19244{
4f44ae6c
TT
19245 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19246 str_offset, "DW_FORM_strp");
c906108c
SS
19247}
19248
43988095
JK
19249/* Return pointer to string at .debug_str offset as read from BUF.
19250 BUF is assumed to be in a compilation unit described by CU_HEADER.
19251 Return *BYTES_READ_PTR count of bytes read from BUF. */
19252
d521ce57 19253static const char *
ed2dc618
SM
19254read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19255 const gdb_byte *buf,
cf2c3c16
TT
19256 const struct comp_unit_head *cu_header,
19257 unsigned int *bytes_read_ptr)
19258{
8266302d 19259 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19260
4f44ae6c 19261 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19262}
19263
86c0bb4c 19264/* See read.h. */
43988095 19265
86c0bb4c
TT
19266const char *
19267dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
19268 const struct comp_unit_head *cu_header,
19269 unsigned int *bytes_read_ptr)
19270{
86c0bb4c 19271 bfd *abfd = objfile->obfd;
8266302d 19272 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19273
86c0bb4c 19274 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19275}
19276
3019eac3 19277/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19278 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19279 ADDR_SIZE is the size of addresses from the CU header. */
19280
19281static CORE_ADDR
ed2dc618 19282read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19283 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19284 int addr_size)
3019eac3
DE
19285{
19286 struct objfile *objfile = dwarf2_per_objfile->objfile;
19287 bfd *abfd = objfile->obfd;
19288 const gdb_byte *info_ptr;
18a8505e 19289 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19290
96b79293 19291 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19292 if (dwarf2_per_objfile->addr.buffer == NULL)
19293 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19294 objfile_name (objfile));
18a8505e
AT
19295 if (addr_base_or_zero + addr_index * addr_size
19296 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19297 error (_("DW_FORM_addr_index pointing outside of "
19298 ".debug_addr section [in module %s]"),
4262abfb 19299 objfile_name (objfile));
3019eac3 19300 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19301 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19302 if (addr_size == 4)
19303 return bfd_get_32 (abfd, info_ptr);
19304 else
19305 return bfd_get_64 (abfd, info_ptr);
19306}
19307
19308/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19309
19310static CORE_ADDR
19311read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19312{
518817b3
SM
19313 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19314 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19315}
19316
19317/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19318
19319static CORE_ADDR
d521ce57 19320read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19321 unsigned int *bytes_read)
19322{
518817b3 19323 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19324 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19325
19326 return read_addr_index (cu, addr_index);
19327}
19328
450a1bfc 19329/* See read.h. */
3019eac3
DE
19330
19331CORE_ADDR
450a1bfc 19332dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19333{
ed2dc618 19334 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19335 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19336 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19337 int addr_size;
19338
3019eac3
DE
19339 /* We need addr_base and addr_size.
19340 If we don't have PER_CU->cu, we have to get it.
19341 Nasty, but the alternative is storing the needed info in PER_CU,
19342 which at this point doesn't seem justified: it's not clear how frequently
19343 it would get used and it would increase the size of every PER_CU.
19344 Entry points like dwarf2_per_cu_addr_size do a similar thing
19345 so we're not in uncharted territory here.
19346 Alas we need to be a bit more complicated as addr_base is contained
19347 in the DIE.
19348
19349 We don't need to read the entire CU(/TU).
19350 We just need the header and top level die.
a1b64ce1 19351
3019eac3 19352 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19353 For now we skip this optimization. */
3019eac3
DE
19354
19355 if (cu != NULL)
19356 {
19357 addr_base = cu->addr_base;
19358 addr_size = cu->header.addr_size;
19359 }
19360 else
19361 {
6751ebae 19362 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19363 addr_base = reader.cu->addr_base;
19364 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19365 }
19366
ed2dc618
SM
19367 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19368 addr_size);
3019eac3
DE
19369}
19370
18a8505e
AT
19371/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19372 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19373 DWO file. */
3019eac3 19374
d521ce57 19375static const char *
18a8505e
AT
19376read_str_index (struct dwarf2_cu *cu,
19377 struct dwarf2_section_info *str_section,
19378 struct dwarf2_section_info *str_offsets_section,
19379 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19380{
518817b3
SM
19381 struct dwarf2_per_objfile *dwarf2_per_objfile
19382 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19383 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19384 const char *objf_name = objfile_name (objfile);
3019eac3 19385 bfd *abfd = objfile->obfd;
d521ce57 19386 const gdb_byte *info_ptr;
3019eac3 19387 ULONGEST str_offset;
cf532bd1 19388 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19389
96b79293
TT
19390 str_section->read (objfile);
19391 str_offsets_section->read (objfile);
73869dc2 19392 if (str_section->buffer == NULL)
18a8505e 19393 error (_("%s used without %s section"
9d8780f0 19394 " in CU at offset %s [in module %s]"),
96b79293 19395 form_name, str_section->get_name (),
18a8505e 19396 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19397 if (str_offsets_section->buffer == NULL)
18a8505e 19398 error (_("%s used without %s section"
9d8780f0 19399 " in CU at offset %s [in module %s]"),
96b79293 19400 form_name, str_section->get_name (),
18a8505e 19401 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19402 info_ptr = (str_offsets_section->buffer
18a8505e 19403 + str_offsets_base
3019eac3
DE
19404 + str_index * cu->header.offset_size);
19405 if (cu->header.offset_size == 4)
19406 str_offset = bfd_get_32 (abfd, info_ptr);
19407 else
19408 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19409 if (str_offset >= str_section->size)
57d63ce2 19410 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19411 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19412 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19413 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19414}
19415
18a8505e
AT
19416/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19417
19418static const char *
19419read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19420{
19421 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19422 ? reader->cu->header.addr_size : 0;
19423 return read_str_index (reader->cu,
19424 &reader->dwo_file->sections.str,
19425 &reader->dwo_file->sections.str_offsets,
19426 str_offsets_base, str_index);
19427}
19428
19429/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19430
19431static const char *
19432read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19433{
19434 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19435 const char *objf_name = objfile_name (objfile);
19436 static const char form_name[] = "DW_FORM_GNU_str_index";
19437 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19438
19439 if (!cu->str_offsets_base.has_value ())
19440 error (_("%s used in Fission stub without %s"
19441 " in CU at offset 0x%lx [in module %s]"),
19442 form_name, str_offsets_attr_name,
19443 (long) cu->header.offset_size, objf_name);
19444
19445 return read_str_index (cu,
19446 &cu->per_cu->dwarf2_per_objfile->str,
19447 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19448 *cu->str_offsets_base, str_index);
19449}
19450
3019eac3
DE
19451/* Return the length of an LEB128 number in BUF. */
19452
19453static int
19454leb128_size (const gdb_byte *buf)
19455{
19456 const gdb_byte *begin = buf;
19457 gdb_byte byte;
19458
19459 while (1)
19460 {
19461 byte = *buf++;
19462 if ((byte & 128) == 0)
19463 return buf - begin;
19464 }
19465}
19466
c906108c 19467static void
e142c38c 19468set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19469{
19470 switch (lang)
19471 {
19472 case DW_LANG_C89:
76bee0cc 19473 case DW_LANG_C99:
0cfd832f 19474 case DW_LANG_C11:
c906108c 19475 case DW_LANG_C:
d1be3247 19476 case DW_LANG_UPC:
e142c38c 19477 cu->language = language_c;
c906108c 19478 break;
9c37b5ae 19479 case DW_LANG_Java:
c906108c 19480 case DW_LANG_C_plus_plus:
0cfd832f
MW
19481 case DW_LANG_C_plus_plus_11:
19482 case DW_LANG_C_plus_plus_14:
e142c38c 19483 cu->language = language_cplus;
c906108c 19484 break;
6aecb9c2
JB
19485 case DW_LANG_D:
19486 cu->language = language_d;
19487 break;
c906108c
SS
19488 case DW_LANG_Fortran77:
19489 case DW_LANG_Fortran90:
b21b22e0 19490 case DW_LANG_Fortran95:
f7de9aab
MW
19491 case DW_LANG_Fortran03:
19492 case DW_LANG_Fortran08:
e142c38c 19493 cu->language = language_fortran;
c906108c 19494 break;
a766d390
DE
19495 case DW_LANG_Go:
19496 cu->language = language_go;
19497 break;
c906108c 19498 case DW_LANG_Mips_Assembler:
e142c38c 19499 cu->language = language_asm;
c906108c
SS
19500 break;
19501 case DW_LANG_Ada83:
8aaf0b47 19502 case DW_LANG_Ada95:
bc5f45f8
JB
19503 cu->language = language_ada;
19504 break;
72019c9c
GM
19505 case DW_LANG_Modula2:
19506 cu->language = language_m2;
19507 break;
fe8e67fd
PM
19508 case DW_LANG_Pascal83:
19509 cu->language = language_pascal;
19510 break;
22566fbd
DJ
19511 case DW_LANG_ObjC:
19512 cu->language = language_objc;
19513 break;
c44af4eb
TT
19514 case DW_LANG_Rust:
19515 case DW_LANG_Rust_old:
19516 cu->language = language_rust;
19517 break;
c906108c
SS
19518 case DW_LANG_Cobol74:
19519 case DW_LANG_Cobol85:
c906108c 19520 default:
e142c38c 19521 cu->language = language_minimal;
c906108c
SS
19522 break;
19523 }
e142c38c 19524 cu->language_defn = language_def (cu->language);
c906108c
SS
19525}
19526
19527/* Return the named attribute or NULL if not there. */
19528
19529static struct attribute *
e142c38c 19530dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19531{
a48e046c 19532 for (;;)
c906108c 19533 {
a48e046c
TT
19534 unsigned int i;
19535 struct attribute *spec = NULL;
19536
19537 for (i = 0; i < die->num_attrs; ++i)
19538 {
19539 if (die->attrs[i].name == name)
19540 return &die->attrs[i];
19541 if (die->attrs[i].name == DW_AT_specification
19542 || die->attrs[i].name == DW_AT_abstract_origin)
19543 spec = &die->attrs[i];
19544 }
19545
19546 if (!spec)
19547 break;
c906108c 19548
f2f0e013 19549 die = follow_die_ref (die, spec, &cu);
f2f0e013 19550 }
c5aa993b 19551
c906108c
SS
19552 return NULL;
19553}
19554
7d45c7c3
KB
19555/* Return the string associated with a string-typed attribute, or NULL if it
19556 is either not found or is of an incorrect type. */
19557
19558static const char *
19559dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19560{
19561 struct attribute *attr;
19562 const char *str = NULL;
19563
19564 attr = dwarf2_attr (die, name, cu);
19565
19566 if (attr != NULL)
19567 {
e61108c9
TT
19568 str = attr->value_as_string ();
19569 if (str == nullptr)
b98664d3 19570 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19571 "DIE at %s in module %s"),
19572 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19573 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19574 }
19575
19576 return str;
19577}
19578
a084a2a6 19579/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19580 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19581static const char *
19582dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19583{
19584 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19585 if (dwo_name == nullptr)
19586 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19587 return dwo_name;
19588}
19589
05cf31d1
JB
19590/* Return non-zero iff the attribute NAME is defined for the given DIE,
19591 and holds a non-zero value. This function should only be used for
2dc7f7b3 19592 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19593
19594static int
19595dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19596{
19597 struct attribute *attr = dwarf2_attr (die, name, cu);
19598
19599 return (attr && DW_UNSND (attr));
19600}
19601
3ca72b44 19602static int
e142c38c 19603die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19604{
05cf31d1
JB
19605 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19606 which value is non-zero. However, we have to be careful with
19607 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19608 (via dwarf2_flag_true_p) follows this attribute. So we may
19609 end up accidently finding a declaration attribute that belongs
19610 to a different DIE referenced by the specification attribute,
19611 even though the given DIE does not have a declaration attribute. */
19612 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19613 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19614}
19615
63d06c5c 19616/* Return the die giving the specification for DIE, if there is
f2f0e013 19617 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19618 containing the return value on output. If there is no
19619 specification, but there is an abstract origin, that is
19620 returned. */
63d06c5c
DC
19621
19622static struct die_info *
f2f0e013 19623die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19624{
f2f0e013
DJ
19625 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19626 *spec_cu);
63d06c5c 19627
edb3359d
DJ
19628 if (spec_attr == NULL)
19629 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19630
63d06c5c
DC
19631 if (spec_attr == NULL)
19632 return NULL;
19633 else
f2f0e013 19634 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19635}
c906108c 19636
527f3840
JK
19637/* Stub for free_line_header to match void * callback types. */
19638
19639static void
19640free_line_header_voidp (void *arg)
19641{
9a3c8263 19642 struct line_header *lh = (struct line_header *) arg;
527f3840 19643
fff8551c 19644 delete lh;
527f3840
JK
19645}
19646
83769d0b 19647/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19648
19649static struct dwarf2_section_info *
19650get_debug_line_section (struct dwarf2_cu *cu)
19651{
19652 struct dwarf2_section_info *section;
518817b3
SM
19653 struct dwarf2_per_objfile *dwarf2_per_objfile
19654 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19655
19656 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19657 DWO file. */
19658 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19659 section = &cu->dwo_unit->dwo_file->sections.line;
19660 else if (cu->per_cu->is_dwz)
19661 {
ed2dc618 19662 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19663
19664 section = &dwz->line;
19665 }
19666 else
19667 section = &dwarf2_per_objfile->line;
19668
19669 return section;
19670}
19671
debd256d 19672/* Read the statement program header starting at OFFSET in
3019eac3 19673 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19674 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19675 Returns NULL if there is a problem reading the header, e.g., if it
19676 has a version we don't understand.
debd256d
JB
19677
19678 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19679 the returned object point into the dwarf line section buffer,
19680 and must not be freed. */
ae2de4f8 19681
fff8551c 19682static line_header_up
9c541725 19683dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19684{
3019eac3 19685 struct dwarf2_section_info *section;
518817b3
SM
19686 struct dwarf2_per_objfile *dwarf2_per_objfile
19687 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19688
36586728 19689 section = get_debug_line_section (cu);
96b79293 19690 section->read (dwarf2_per_objfile->objfile);
3019eac3 19691 if (section->buffer == NULL)
debd256d 19692 {
3019eac3 19693 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19694 complaint (_("missing .debug_line.dwo section"));
3019eac3 19695 else
b98664d3 19696 complaint (_("missing .debug_line section"));
debd256d
JB
19697 return 0;
19698 }
19699
0df7ad3a
TT
19700 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19701 dwarf2_per_objfile, section,
19702 &cu->header);
debd256d 19703}
c906108c 19704
c6da4cef 19705/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19706 Return the file name of the psymtab for the given file_entry.
c6da4cef 19707 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19708 If space for the result is malloc'd, *NAME_HOLDER will be set.
19709 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19710
d521ce57 19711static const char *
7ba99d21 19712psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19713 const dwarf2_psymtab *pst,
c89b44cd
TT
19714 const char *comp_dir,
19715 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19716{
d521ce57
TT
19717 const char *include_name = fe.name;
19718 const char *include_name_to_compare = include_name;
72b9f47f 19719 const char *pst_filename;
c6da4cef
DE
19720 int file_is_pst;
19721
8c43009f 19722 const char *dir_name = fe.include_dir (lh);
c6da4cef 19723
c89b44cd 19724 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19725 if (!IS_ABSOLUTE_PATH (include_name)
19726 && (dir_name != NULL || comp_dir != NULL))
19727 {
19728 /* Avoid creating a duplicate psymtab for PST.
19729 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19730 Before we do the comparison, however, we need to account
19731 for DIR_NAME and COMP_DIR.
19732 First prepend dir_name (if non-NULL). If we still don't
19733 have an absolute path prepend comp_dir (if non-NULL).
19734 However, the directory we record in the include-file's
19735 psymtab does not contain COMP_DIR (to match the
19736 corresponding symtab(s)).
19737
19738 Example:
19739
19740 bash$ cd /tmp
19741 bash$ gcc -g ./hello.c
19742 include_name = "hello.c"
19743 dir_name = "."
19744 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19745 DW_AT_name = "./hello.c"
19746
19747 */
c6da4cef
DE
19748
19749 if (dir_name != NULL)
19750 {
c89b44cd
TT
19751 name_holder->reset (concat (dir_name, SLASH_STRING,
19752 include_name, (char *) NULL));
19753 include_name = name_holder->get ();
c6da4cef 19754 include_name_to_compare = include_name;
c6da4cef
DE
19755 }
19756 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19757 {
c89b44cd
TT
19758 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19759 include_name, (char *) NULL));
19760 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19761 }
19762 }
19763
19764 pst_filename = pst->filename;
c89b44cd 19765 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19766 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19767 {
c89b44cd
TT
19768 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19769 pst_filename, (char *) NULL));
19770 pst_filename = copied_name.get ();
c6da4cef
DE
19771 }
19772
1e3fad37 19773 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19774
c6da4cef
DE
19775 if (file_is_pst)
19776 return NULL;
19777 return include_name;
19778}
19779
d9b3de22
DE
19780/* State machine to track the state of the line number program. */
19781
6f77053d 19782class lnp_state_machine
d9b3de22 19783{
6f77053d
PA
19784public:
19785 /* Initialize a machine state for the start of a line number
19786 program. */
804d2729
TT
19787 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19788 bool record_lines_p);
6f77053d 19789
8c43009f
PA
19790 file_entry *current_file ()
19791 {
19792 /* lh->file_names is 0-based, but the file name numbers in the
19793 statement program are 1-based. */
6f77053d
PA
19794 return m_line_header->file_name_at (m_file);
19795 }
19796
19797 /* Record the line in the state machine. END_SEQUENCE is true if
19798 we're processing the end of a sequence. */
19799 void record_line (bool end_sequence);
19800
7ab6656f
OJ
19801 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19802 nop-out rest of the lines in this sequence. */
6f77053d
PA
19803 void check_line_address (struct dwarf2_cu *cu,
19804 const gdb_byte *line_ptr,
7ab6656f 19805 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19806
19807 void handle_set_discriminator (unsigned int discriminator)
19808 {
19809 m_discriminator = discriminator;
19810 m_line_has_non_zero_discriminator |= discriminator != 0;
19811 }
19812
19813 /* Handle DW_LNE_set_address. */
19814 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19815 {
19816 m_op_index = 0;
19817 address += baseaddr;
19818 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19819 }
19820
19821 /* Handle DW_LNS_advance_pc. */
19822 void handle_advance_pc (CORE_ADDR adjust);
19823
19824 /* Handle a special opcode. */
19825 void handle_special_opcode (unsigned char op_code);
19826
19827 /* Handle DW_LNS_advance_line. */
19828 void handle_advance_line (int line_delta)
19829 {
19830 advance_line (line_delta);
19831 }
19832
19833 /* Handle DW_LNS_set_file. */
19834 void handle_set_file (file_name_index file);
19835
19836 /* Handle DW_LNS_negate_stmt. */
19837 void handle_negate_stmt ()
19838 {
19839 m_is_stmt = !m_is_stmt;
19840 }
19841
19842 /* Handle DW_LNS_const_add_pc. */
19843 void handle_const_add_pc ();
19844
19845 /* Handle DW_LNS_fixed_advance_pc. */
19846 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19847 {
19848 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19849 m_op_index = 0;
19850 }
19851
19852 /* Handle DW_LNS_copy. */
19853 void handle_copy ()
19854 {
19855 record_line (false);
19856 m_discriminator = 0;
19857 }
19858
19859 /* Handle DW_LNE_end_sequence. */
19860 void handle_end_sequence ()
19861 {
804d2729 19862 m_currently_recording_lines = true;
6f77053d
PA
19863 }
19864
19865private:
19866 /* Advance the line by LINE_DELTA. */
19867 void advance_line (int line_delta)
19868 {
19869 m_line += line_delta;
19870
19871 if (line_delta != 0)
19872 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19873 }
19874
804d2729
TT
19875 struct dwarf2_cu *m_cu;
19876
6f77053d
PA
19877 gdbarch *m_gdbarch;
19878
19879 /* True if we're recording lines.
19880 Otherwise we're building partial symtabs and are just interested in
19881 finding include files mentioned by the line number program. */
19882 bool m_record_lines_p;
19883
8c43009f 19884 /* The line number header. */
6f77053d 19885 line_header *m_line_header;
8c43009f 19886
6f77053d
PA
19887 /* These are part of the standard DWARF line number state machine,
19888 and initialized according to the DWARF spec. */
d9b3de22 19889
6f77053d 19890 unsigned char m_op_index = 0;
7ba99d21
AT
19891 /* The line table index of the current file. */
19892 file_name_index m_file = 1;
6f77053d
PA
19893 unsigned int m_line = 1;
19894
19895 /* These are initialized in the constructor. */
19896
19897 CORE_ADDR m_address;
19898 bool m_is_stmt;
19899 unsigned int m_discriminator;
d9b3de22
DE
19900
19901 /* Additional bits of state we need to track. */
19902
19903 /* The last file that we called dwarf2_start_subfile for.
19904 This is only used for TLLs. */
6f77053d 19905 unsigned int m_last_file = 0;
d9b3de22 19906 /* The last file a line number was recorded for. */
6f77053d 19907 struct subfile *m_last_subfile = NULL;
d9b3de22 19908
804d2729
TT
19909 /* When true, record the lines we decode. */
19910 bool m_currently_recording_lines = false;
d9b3de22
DE
19911
19912 /* The last line number that was recorded, used to coalesce
19913 consecutive entries for the same line. This can happen, for
19914 example, when discriminators are present. PR 17276. */
6f77053d
PA
19915 unsigned int m_last_line = 0;
19916 bool m_line_has_non_zero_discriminator = false;
8c43009f 19917};
d9b3de22 19918
6f77053d
PA
19919void
19920lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19921{
19922 CORE_ADDR addr_adj = (((m_op_index + adjust)
19923 / m_line_header->maximum_ops_per_instruction)
19924 * m_line_header->minimum_instruction_length);
19925 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19926 m_op_index = ((m_op_index + adjust)
19927 % m_line_header->maximum_ops_per_instruction);
19928}
d9b3de22 19929
6f77053d
PA
19930void
19931lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19932{
6f77053d 19933 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19934 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19935 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19936 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19937 / m_line_header->maximum_ops_per_instruction)
19938 * m_line_header->minimum_instruction_length);
19939 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19940 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19941 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19942
258bf0ee 19943 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19944 advance_line (line_delta);
19945 record_line (false);
19946 m_discriminator = 0;
19947}
d9b3de22 19948
6f77053d
PA
19949void
19950lnp_state_machine::handle_set_file (file_name_index file)
19951{
19952 m_file = file;
19953
19954 const file_entry *fe = current_file ();
19955 if (fe == NULL)
19956 dwarf2_debug_line_missing_file_complaint ();
19957 else if (m_record_lines_p)
19958 {
19959 const char *dir = fe->include_dir (m_line_header);
19960
c24bdb02 19961 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19962 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19963 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19964 }
19965}
19966
19967void
19968lnp_state_machine::handle_const_add_pc ()
19969{
19970 CORE_ADDR adjust
19971 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19972
19973 CORE_ADDR addr_adj
19974 = (((m_op_index + adjust)
19975 / m_line_header->maximum_ops_per_instruction)
19976 * m_line_header->minimum_instruction_length);
19977
19978 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19979 m_op_index = ((m_op_index + adjust)
19980 % m_line_header->maximum_ops_per_instruction);
19981}
d9b3de22 19982
a05a36a5
DE
19983/* Return non-zero if we should add LINE to the line number table.
19984 LINE is the line to add, LAST_LINE is the last line that was added,
19985 LAST_SUBFILE is the subfile for LAST_LINE.
19986 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19987 had a non-zero discriminator.
19988
19989 We have to be careful in the presence of discriminators.
19990 E.g., for this line:
19991
19992 for (i = 0; i < 100000; i++);
19993
19994 clang can emit four line number entries for that one line,
19995 each with a different discriminator.
19996 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19997
19998 However, we want gdb to coalesce all four entries into one.
19999 Otherwise the user could stepi into the middle of the line and
20000 gdb would get confused about whether the pc really was in the
20001 middle of the line.
20002
20003 Things are further complicated by the fact that two consecutive
20004 line number entries for the same line is a heuristic used by gcc
20005 to denote the end of the prologue. So we can't just discard duplicate
20006 entries, we have to be selective about it. The heuristic we use is
20007 that we only collapse consecutive entries for the same line if at least
20008 one of those entries has a non-zero discriminator. PR 17276.
20009
20010 Note: Addresses in the line number state machine can never go backwards
20011 within one sequence, thus this coalescing is ok. */
20012
20013static int
804d2729
TT
20014dwarf_record_line_p (struct dwarf2_cu *cu,
20015 unsigned int line, unsigned int last_line,
a05a36a5
DE
20016 int line_has_non_zero_discriminator,
20017 struct subfile *last_subfile)
20018{
c24bdb02 20019 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20020 return 1;
20021 if (line != last_line)
20022 return 1;
20023 /* Same line for the same file that we've seen already.
20024 As a last check, for pr 17276, only record the line if the line
20025 has never had a non-zero discriminator. */
20026 if (!line_has_non_zero_discriminator)
20027 return 1;
20028 return 0;
20029}
20030
804d2729
TT
20031/* Use the CU's builder to record line number LINE beginning at
20032 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20033
20034static void
d9b3de22 20035dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20036 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20037 struct dwarf2_cu *cu)
252a6764
DE
20038{
20039 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20040
27e0867f
DE
20041 if (dwarf_line_debug)
20042 {
20043 fprintf_unfiltered (gdb_stdlog,
20044 "Recording line %u, file %s, address %s\n",
20045 line, lbasename (subfile->name),
20046 paddress (gdbarch, address));
20047 }
20048
804d2729 20049 if (cu != nullptr)
8c95582d 20050 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20051}
20052
20053/* Subroutine of dwarf_decode_lines_1 to simplify it.
20054 Mark the end of a set of line number records.
d9b3de22 20055 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20056 If SUBFILE is NULL the request is ignored. */
20057
20058static void
20059dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20060 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20061{
27e0867f
DE
20062 if (subfile == NULL)
20063 return;
20064
20065 if (dwarf_line_debug)
20066 {
20067 fprintf_unfiltered (gdb_stdlog,
20068 "Finishing current line, file %s, address %s\n",
20069 lbasename (subfile->name),
20070 paddress (gdbarch, address));
20071 }
20072
8c95582d 20073 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20074}
20075
6f77053d
PA
20076void
20077lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20078{
d9b3de22
DE
20079 if (dwarf_line_debug)
20080 {
20081 fprintf_unfiltered (gdb_stdlog,
20082 "Processing actual line %u: file %u,"
94a72be7 20083 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20084 m_line, m_file,
6f77053d 20085 paddress (m_gdbarch, m_address),
94a72be7
AB
20086 m_is_stmt, m_discriminator,
20087 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20088 }
20089
6f77053d 20090 file_entry *fe = current_file ();
8c43009f
PA
20091
20092 if (fe == NULL)
d9b3de22
DE
20093 dwarf2_debug_line_missing_file_complaint ();
20094 /* For now we ignore lines not starting on an instruction boundary.
20095 But not when processing end_sequence for compatibility with the
20096 previous version of the code. */
6f77053d 20097 else if (m_op_index == 0 || end_sequence)
d9b3de22 20098 {
8c43009f 20099 fe->included_p = 1;
8c95582d 20100 if (m_record_lines_p)
d9b3de22 20101 {
c24bdb02 20102 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20103 || end_sequence)
d9b3de22 20104 {
804d2729
TT
20105 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20106 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20107 }
20108
20109 if (!end_sequence)
20110 {
8c95582d
AB
20111 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20112
804d2729 20113 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20114 m_line_has_non_zero_discriminator,
20115 m_last_subfile))
d9b3de22 20116 {
c24bdb02 20117 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20118 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20119 builder->get_current_subfile (),
8c95582d 20120 m_line, m_address, is_stmt,
804d2729 20121 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20122 }
c24bdb02 20123 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20124 m_last_line = m_line;
d9b3de22
DE
20125 }
20126 }
20127 }
20128}
20129
804d2729
TT
20130lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20131 line_header *lh, bool record_lines_p)
d9b3de22 20132{
804d2729 20133 m_cu = cu;
6f77053d
PA
20134 m_gdbarch = arch;
20135 m_record_lines_p = record_lines_p;
20136 m_line_header = lh;
d9b3de22 20137
804d2729 20138 m_currently_recording_lines = true;
d9b3de22 20139
d9b3de22
DE
20140 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20141 was a line entry for it so that the backend has a chance to adjust it
20142 and also record it in case it needs it. This is currently used by MIPS
20143 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20144 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20145 m_is_stmt = lh->default_is_stmt;
20146 m_discriminator = 0;
252a6764
DE
20147}
20148
6f77053d
PA
20149void
20150lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20151 const gdb_byte *line_ptr,
7ab6656f 20152 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20153{
7ab6656f
OJ
20154 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20155 the pc range of the CU. However, we restrict the test to only ADDRESS
20156 values of zero to preserve GDB's previous behaviour which is to handle
20157 the specific case of a function being GC'd by the linker. */
924c2928 20158
7ab6656f 20159 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20160 {
20161 /* This line table is for a function which has been
20162 GCd by the linker. Ignore it. PR gdb/12528 */
20163
518817b3 20164 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20165 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20166
b98664d3 20167 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20168 line_offset, objfile_name (objfile));
804d2729
TT
20169 m_currently_recording_lines = false;
20170 /* Note: m_currently_recording_lines is left as false until we see
20171 DW_LNE_end_sequence. */
924c2928
DE
20172 }
20173}
20174
f3f5162e 20175/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20176 Process the line number information in LH.
20177 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20178 program in order to set included_p for every referenced header. */
debd256d 20179
c906108c 20180static void
43f3e411
DE
20181dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20182 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20183{
d521ce57
TT
20184 const gdb_byte *line_ptr, *extended_end;
20185 const gdb_byte *line_end;
a8c50c1f 20186 unsigned int bytes_read, extended_len;
699ca60a 20187 unsigned char op_code, extended_op;
e142c38c 20188 CORE_ADDR baseaddr;
518817b3 20189 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20190 bfd *abfd = objfile->obfd;
08feed99 20191 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20192 /* True if we're recording line info (as opposed to building partial
20193 symtabs and just interested in finding include files mentioned by
20194 the line number program). */
20195 bool record_lines_p = !decode_for_pst_p;
e142c38c 20196
b3b3bada 20197 baseaddr = objfile->text_section_offset ();
c906108c 20198
debd256d
JB
20199 line_ptr = lh->statement_program_start;
20200 line_end = lh->statement_program_end;
c906108c
SS
20201
20202 /* Read the statement sequences until there's nothing left. */
20203 while (line_ptr < line_end)
20204 {
6f77053d
PA
20205 /* The DWARF line number program state machine. Reset the state
20206 machine at the start of each sequence. */
804d2729 20207 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20208 bool end_sequence = false;
d9b3de22 20209
8c43009f 20210 if (record_lines_p)
c906108c 20211 {
8c43009f
PA
20212 /* Start a subfile for the current file of the state
20213 machine. */
20214 const file_entry *fe = state_machine.current_file ();
20215
20216 if (fe != NULL)
804d2729 20217 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20218 }
20219
a738430d 20220 /* Decode the table. */
d9b3de22 20221 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20222 {
20223 op_code = read_1_byte (abfd, line_ptr);
20224 line_ptr += 1;
9aa1fe7e 20225
debd256d 20226 if (op_code >= lh->opcode_base)
6e70227d 20227 {
8e07a239 20228 /* Special opcode. */
6f77053d 20229 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20230 }
20231 else switch (op_code)
c906108c
SS
20232 {
20233 case DW_LNS_extended_op:
3e43a32a
MS
20234 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20235 &bytes_read);
473b7be6 20236 line_ptr += bytes_read;
a8c50c1f 20237 extended_end = line_ptr + extended_len;
c906108c
SS
20238 extended_op = read_1_byte (abfd, line_ptr);
20239 line_ptr += 1;
20240 switch (extended_op)
20241 {
20242 case DW_LNE_end_sequence:
6f77053d
PA
20243 state_machine.handle_end_sequence ();
20244 end_sequence = true;
c906108c
SS
20245 break;
20246 case DW_LNE_set_address:
d9b3de22
DE
20247 {
20248 CORE_ADDR address
c8a7a66f 20249 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20250 line_ptr += bytes_read;
6f77053d
PA
20251
20252 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20253 lowpc - baseaddr, address);
6f77053d 20254 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20255 }
c906108c
SS
20256 break;
20257 case DW_LNE_define_file:
debd256d 20258 {
d521ce57 20259 const char *cur_file;
ecfb656c
PA
20260 unsigned int mod_time, length;
20261 dir_index dindex;
6e70227d 20262
3e43a32a
MS
20263 cur_file = read_direct_string (abfd, line_ptr,
20264 &bytes_read);
debd256d 20265 line_ptr += bytes_read;
ecfb656c 20266 dindex = (dir_index)
debd256d
JB
20267 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20268 line_ptr += bytes_read;
20269 mod_time =
20270 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20271 line_ptr += bytes_read;
20272 length =
20273 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20274 line_ptr += bytes_read;
ecfb656c 20275 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20276 }
c906108c 20277 break;
d0c6ba3d 20278 case DW_LNE_set_discriminator:
6f77053d
PA
20279 {
20280 /* The discriminator is not interesting to the
20281 debugger; just ignore it. We still need to
20282 check its value though:
20283 if there are consecutive entries for the same
20284 (non-prologue) line we want to coalesce them.
20285 PR 17276. */
20286 unsigned int discr
20287 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20288 line_ptr += bytes_read;
20289
20290 state_machine.handle_set_discriminator (discr);
20291 }
d0c6ba3d 20292 break;
c906108c 20293 default:
b98664d3 20294 complaint (_("mangled .debug_line section"));
debd256d 20295 return;
c906108c 20296 }
a8c50c1f
DJ
20297 /* Make sure that we parsed the extended op correctly. If e.g.
20298 we expected a different address size than the producer used,
20299 we may have read the wrong number of bytes. */
20300 if (line_ptr != extended_end)
20301 {
b98664d3 20302 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20303 return;
20304 }
c906108c
SS
20305 break;
20306 case DW_LNS_copy:
6f77053d 20307 state_machine.handle_copy ();
c906108c
SS
20308 break;
20309 case DW_LNS_advance_pc:
2dc7f7b3
TT
20310 {
20311 CORE_ADDR adjust
20312 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20313 line_ptr += bytes_read;
6f77053d
PA
20314
20315 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20316 }
c906108c
SS
20317 break;
20318 case DW_LNS_advance_line:
a05a36a5
DE
20319 {
20320 int line_delta
20321 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20322 line_ptr += bytes_read;
6f77053d
PA
20323
20324 state_machine.handle_advance_line (line_delta);
a05a36a5 20325 }
c906108c
SS
20326 break;
20327 case DW_LNS_set_file:
d9b3de22 20328 {
6f77053d 20329 file_name_index file
ecfb656c
PA
20330 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20331 &bytes_read);
d9b3de22 20332 line_ptr += bytes_read;
8c43009f 20333
6f77053d 20334 state_machine.handle_set_file (file);
d9b3de22 20335 }
c906108c
SS
20336 break;
20337 case DW_LNS_set_column:
0ad93d4f 20338 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20339 line_ptr += bytes_read;
20340 break;
20341 case DW_LNS_negate_stmt:
6f77053d 20342 state_machine.handle_negate_stmt ();
c906108c
SS
20343 break;
20344 case DW_LNS_set_basic_block:
c906108c 20345 break;
c2c6d25f
JM
20346 /* Add to the address register of the state machine the
20347 address increment value corresponding to special opcode
a738430d
MK
20348 255. I.e., this value is scaled by the minimum
20349 instruction length since special opcode 255 would have
b021a221 20350 scaled the increment. */
c906108c 20351 case DW_LNS_const_add_pc:
6f77053d 20352 state_machine.handle_const_add_pc ();
c906108c
SS
20353 break;
20354 case DW_LNS_fixed_advance_pc:
3e29f34a 20355 {
6f77053d 20356 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20357 line_ptr += 2;
6f77053d
PA
20358
20359 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20360 }
c906108c 20361 break;
9aa1fe7e 20362 default:
a738430d
MK
20363 {
20364 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20365 int i;
a738430d 20366
debd256d 20367 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20368 {
20369 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20370 line_ptr += bytes_read;
20371 }
20372 }
c906108c
SS
20373 }
20374 }
d9b3de22
DE
20375
20376 if (!end_sequence)
20377 dwarf2_debug_line_missing_end_sequence_complaint ();
20378
20379 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20380 in which case we still finish recording the last line). */
6f77053d 20381 state_machine.record_line (true);
c906108c 20382 }
f3f5162e
DE
20383}
20384
20385/* Decode the Line Number Program (LNP) for the given line_header
20386 structure and CU. The actual information extracted and the type
20387 of structures created from the LNP depends on the value of PST.
20388
20389 1. If PST is NULL, then this procedure uses the data from the program
20390 to create all necessary symbol tables, and their linetables.
20391
20392 2. If PST is not NULL, this procedure reads the program to determine
20393 the list of files included by the unit represented by PST, and
20394 builds all the associated partial symbol tables.
20395
20396 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20397 It is used for relative paths in the line table.
20398 NOTE: When processing partial symtabs (pst != NULL),
20399 comp_dir == pst->dirname.
20400
20401 NOTE: It is important that psymtabs have the same file name (via strcmp)
20402 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20403 symtab we don't use it in the name of the psymtabs we create.
20404 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20405 A good testcase for this is mb-inline.exp.
20406
527f3840
JK
20407 LOWPC is the lowest address in CU (or 0 if not known).
20408
20409 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20410 for its PC<->lines mapping information. Otherwise only the filename
20411 table is read in. */
f3f5162e
DE
20412
20413static void
20414dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20415 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20416 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20417{
518817b3 20418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20419 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20420
527f3840
JK
20421 if (decode_mapping)
20422 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20423
20424 if (decode_for_pst_p)
20425 {
aaa75496
JB
20426 /* Now that we're done scanning the Line Header Program, we can
20427 create the psymtab of each included file. */
7ba99d21
AT
20428 for (auto &file_entry : lh->file_names ())
20429 if (file_entry.included_p == 1)
aaa75496 20430 {
c89b44cd 20431 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20432 const char *include_name =
7ba99d21
AT
20433 psymtab_include_file_name (lh, file_entry, pst,
20434 comp_dir, &name_holder);
c6da4cef 20435 if (include_name != NULL)
aaa75496
JB
20436 dwarf2_create_include_psymtab (include_name, pst, objfile);
20437 }
20438 }
cb1df416
DJ
20439 else
20440 {
20441 /* Make sure a symtab is created for every file, even files
20442 which contain only variables (i.e. no code with associated
20443 line numbers). */
c24bdb02
KS
20444 buildsym_compunit *builder = cu->get_builder ();
20445 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20446
7ba99d21 20447 for (auto &fe : lh->file_names ())
cb1df416 20448 {
804d2729 20449 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20450 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20451 {
c24bdb02 20452 builder->get_current_subfile ()->symtab
804d2729 20453 = allocate_symtab (cust,
c24bdb02 20454 builder->get_current_subfile ()->name);
43f3e411 20455 }
c24bdb02 20456 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20457 }
20458 }
c906108c
SS
20459}
20460
20461/* Start a subfile for DWARF. FILENAME is the name of the file and
20462 DIRNAME the name of the source directory which contains FILENAME
4d663531 20463 or NULL if not known.
c906108c
SS
20464 This routine tries to keep line numbers from identical absolute and
20465 relative file names in a common subfile.
20466
20467 Using the `list' example from the GDB testsuite, which resides in
20468 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20469 of /srcdir/list0.c yields the following debugging information for list0.c:
20470
c5aa993b 20471 DW_AT_name: /srcdir/list0.c
4d663531 20472 DW_AT_comp_dir: /compdir
357e46e7 20473 files.files[0].name: list0.h
c5aa993b 20474 files.files[0].dir: /srcdir
357e46e7 20475 files.files[1].name: list0.c
c5aa993b 20476 files.files[1].dir: /srcdir
c906108c
SS
20477
20478 The line number information for list0.c has to end up in a single
4f1520fb
FR
20479 subfile, so that `break /srcdir/list0.c:1' works as expected.
20480 start_subfile will ensure that this happens provided that we pass the
20481 concatenation of files.files[1].dir and files.files[1].name as the
20482 subfile's name. */
c906108c
SS
20483
20484static void
804d2729
TT
20485dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20486 const char *dirname)
c906108c 20487{
43816ebc 20488 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20489
4d663531 20490 /* In order not to lose the line information directory,
4f1520fb
FR
20491 we concatenate it to the filename when it makes sense.
20492 Note that the Dwarf3 standard says (speaking of filenames in line
20493 information): ``The directory index is ignored for file names
20494 that represent full path names''. Thus ignoring dirname in the
20495 `else' branch below isn't an issue. */
c906108c 20496
d5166ae1 20497 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20498 {
43816ebc
TT
20499 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20500 filename = copy.get ();
d521ce57 20501 }
c906108c 20502
c24bdb02 20503 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20504}
20505
804d2729
TT
20506/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20507 buildsym_compunit constructor. */
f4dc4d17 20508
c24bdb02
KS
20509struct compunit_symtab *
20510dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20511 CORE_ADDR low_pc)
f4dc4d17 20512{
c24bdb02 20513 gdb_assert (m_builder == nullptr);
43f3e411 20514
c24bdb02
KS
20515 m_builder.reset (new struct buildsym_compunit
20516 (per_cu->dwarf2_per_objfile->objfile,
20517 name, comp_dir, language, low_pc));
93b8bea4 20518
c24bdb02 20519 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20520
c24bdb02
KS
20521 get_builder ()->record_debugformat ("DWARF 2");
20522 get_builder ()->record_producer (producer);
f4dc4d17 20523
c24bdb02 20524 processing_has_namespace_info = false;
43f3e411 20525
c24bdb02 20526 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20527}
20528
4c2df51b
DJ
20529static void
20530var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20531 struct dwarf2_cu *cu)
4c2df51b 20532{
518817b3 20533 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20534 struct comp_unit_head *cu_header = &cu->header;
20535
4c2df51b
DJ
20536 /* NOTE drow/2003-01-30: There used to be a comment and some special
20537 code here to turn a symbol with DW_AT_external and a
20538 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20539 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20540 with some versions of binutils) where shared libraries could have
20541 relocations against symbols in their debug information - the
20542 minimal symbol would have the right address, but the debug info
20543 would not. It's no longer necessary, because we will explicitly
20544 apply relocations when we read in the debug information now. */
20545
20546 /* A DW_AT_location attribute with no contents indicates that a
20547 variable has been optimized away. */
4fc6c0d5 20548 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20549 {
f1e6e072 20550 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20551 return;
20552 }
20553
20554 /* Handle one degenerate form of location expression specially, to
20555 preserve GDB's previous behavior when section offsets are
336d760d
AT
20556 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20557 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20558
4fc6c0d5 20559 if (attr->form_is_block ()
3019eac3
DE
20560 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20561 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20562 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20563 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20564 && (DW_BLOCK (attr)->size
20565 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20566 {
891d2f0b 20567 unsigned int dummy;
4c2df51b 20568
3019eac3 20569 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20570 SET_SYMBOL_VALUE_ADDRESS
20571 (sym, cu->header.read_address (objfile->obfd,
20572 DW_BLOCK (attr)->data + 1,
20573 &dummy));
3019eac3 20574 else
38583298
TT
20575 SET_SYMBOL_VALUE_ADDRESS
20576 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20577 &dummy));
f1e6e072 20578 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20579 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20580 SET_SYMBOL_VALUE_ADDRESS
20581 (sym,
20582 SYMBOL_VALUE_ADDRESS (sym)
20583 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20584 return;
20585 }
20586
20587 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20588 expression evaluator, and use LOC_COMPUTED only when necessary
20589 (i.e. when the value of a register or memory location is
20590 referenced, or a thread-local block, etc.). Then again, it might
20591 not be worthwhile. I'm assuming that it isn't unless performance
20592 or memory numbers show me otherwise. */
20593
f1e6e072 20594 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20595
f1e6e072 20596 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20597 cu->has_loclist = true;
4c2df51b
DJ
20598}
20599
c906108c
SS
20600/* Given a pointer to a DWARF information entry, figure out if we need
20601 to make a symbol table entry for it, and if so, create a new entry
20602 and return a pointer to it.
20603 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20604 used the passed type.
20605 If SPACE is not NULL, use it to hold the new symbol. If it is
20606 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20607
20608static struct symbol *
5e2db402
TT
20609new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20610 struct symbol *space)
c906108c 20611{
518817b3
SM
20612 struct dwarf2_per_objfile *dwarf2_per_objfile
20613 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20614 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20615 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20616 struct symbol *sym = NULL;
15d034d0 20617 const char *name;
c906108c
SS
20618 struct attribute *attr = NULL;
20619 struct attribute *attr2 = NULL;
e142c38c 20620 CORE_ADDR baseaddr;
e37fd15a
SW
20621 struct pending **list_to_add = NULL;
20622
edb3359d 20623 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20624
b3b3bada 20625 baseaddr = objfile->text_section_offset ();
c906108c 20626
94af9270 20627 name = dwarf2_name (die, cu);
c906108c
SS
20628 if (name)
20629 {
34eaf542 20630 int suppress_add = 0;
94af9270 20631
34eaf542
TT
20632 if (space)
20633 sym = space;
20634 else
8c14c3a3 20635 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20636 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20637
20638 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20639 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20640 /* Fortran does not have mangling standard and the mangling does differ
20641 between gfortran, iFort etc. */
bcfe6157
TT
20642 const char *physname
20643 = (cu->language == language_fortran
20644 ? dwarf2_full_name (name, die, cu)
20645 : dwarf2_physname (name, die, cu));
20646 const char *linkagename = dw2_linkage_name (die, cu);
20647
20648 if (linkagename == nullptr || cu->language == language_ada)
20649 sym->set_linkage_name (physname);
20650 else
20651 {
20652 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20653 sym->set_linkage_name (linkagename);
20654 }
f55ee35c 20655
c906108c 20656 /* Default assumptions.
c5aa993b 20657 Use the passed type or decode it from the die. */
176620f1 20658 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20659 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20660 if (type != NULL)
20661 SYMBOL_TYPE (sym) = type;
20662 else
e7c27a73 20663 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20664 attr = dwarf2_attr (die,
20665 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20666 cu);
435d3d88 20667 if (attr != nullptr)
c906108c
SS
20668 {
20669 SYMBOL_LINE (sym) = DW_UNSND (attr);
20670 }
cb1df416 20671
edb3359d
DJ
20672 attr = dwarf2_attr (die,
20673 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20674 cu);
435d3d88 20675 if (attr != nullptr)
cb1df416 20676 {
ecfb656c 20677 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20678 struct file_entry *fe;
9a619af0 20679
ecfb656c
PA
20680 if (cu->line_header != NULL)
20681 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20682 else
20683 fe = NULL;
20684
20685 if (fe == NULL)
b98664d3 20686 complaint (_("file index out of range"));
8c43009f
PA
20687 else
20688 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20689 }
20690
c906108c
SS
20691 switch (die->tag)
20692 {
20693 case DW_TAG_label:
e142c38c 20694 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20695 if (attr != nullptr)
3e29f34a
MR
20696 {
20697 CORE_ADDR addr;
20698
cd6c91b4 20699 addr = attr->value_as_address ();
3e29f34a 20700 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20701 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20702 }
0f5238ed
TT
20703 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20704 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20705 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20706 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20707 break;
20708 case DW_TAG_subprogram:
20709 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20710 finish_block. */
f1e6e072 20711 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20712 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20713 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20714 || cu->language == language_ada
20715 || cu->language == language_fortran)
c906108c 20716 {
2cfa0c8d 20717 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20718 Ada and Fortran subprograms, whether marked external or
20719 not, are always stored as a global symbol, because we want
20720 to be able to access them globally. For instance, we want
20721 to be able to break on a nested subprogram without having
20722 to specify the context. */
c24bdb02 20723 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20724 }
20725 else
20726 {
e37fd15a 20727 list_to_add = cu->list_in_scope;
c906108c
SS
20728 }
20729 break;
edb3359d
DJ
20730 case DW_TAG_inlined_subroutine:
20731 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20732 finish_block. */
f1e6e072 20733 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20734 SYMBOL_INLINED (sym) = 1;
481860b3 20735 list_to_add = cu->list_in_scope;
edb3359d 20736 break;
34eaf542
TT
20737 case DW_TAG_template_value_param:
20738 suppress_add = 1;
20739 /* Fall through. */
72929c62 20740 case DW_TAG_constant:
c906108c 20741 case DW_TAG_variable:
254e6b9e 20742 case DW_TAG_member:
0963b4bd
MS
20743 /* Compilation with minimal debug info may result in
20744 variables with missing type entries. Change the
20745 misleading `void' type to something sensible. */
78134374 20746 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20747 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20748
e142c38c 20749 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20750 /* In the case of DW_TAG_member, we should only be called for
20751 static const members. */
20752 if (die->tag == DW_TAG_member)
20753 {
3863f96c
DE
20754 /* dwarf2_add_field uses die_is_declaration,
20755 so we do the same. */
254e6b9e
DE
20756 gdb_assert (die_is_declaration (die, cu));
20757 gdb_assert (attr);
20758 }
435d3d88 20759 if (attr != nullptr)
c906108c 20760 {
e7c27a73 20761 dwarf2_const_value (attr, sym, cu);
e142c38c 20762 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20763 if (!suppress_add)
34eaf542
TT
20764 {
20765 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20766 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20767 else
e37fd15a 20768 list_to_add = cu->list_in_scope;
34eaf542 20769 }
c906108c
SS
20770 break;
20771 }
e142c38c 20772 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20773 if (attr != nullptr)
c906108c 20774 {
e7c27a73 20775 var_decode_location (attr, sym, cu);
e142c38c 20776 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20777
20778 /* Fortran explicitly imports any global symbols to the local
20779 scope by DW_TAG_common_block. */
20780 if (cu->language == language_fortran && die->parent
20781 && die->parent->tag == DW_TAG_common_block)
20782 attr2 = NULL;
20783
caac4577
JG
20784 if (SYMBOL_CLASS (sym) == LOC_STATIC
20785 && SYMBOL_VALUE_ADDRESS (sym) == 0
20786 && !dwarf2_per_objfile->has_section_at_zero)
20787 {
20788 /* When a static variable is eliminated by the linker,
20789 the corresponding debug information is not stripped
20790 out, but the variable address is set to null;
20791 do not add such variables into symbol table. */
20792 }
20793 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20794 {
4b610737
TT
20795 if (SYMBOL_CLASS (sym) == LOC_STATIC
20796 && (objfile->flags & OBJF_MAINLINE) == 0
20797 && dwarf2_per_objfile->can_copy)
20798 {
20799 /* A global static variable might be subject to
20800 copy relocation. We first check for a local
20801 minsym, though, because maybe the symbol was
20802 marked hidden, in which case this would not
20803 apply. */
20804 bound_minimal_symbol found
20805 = (lookup_minimal_symbol_linkage
987012b8 20806 (sym->linkage_name (), objfile));
4b610737
TT
20807 if (found.minsym != nullptr)
20808 sym->maybe_copied = 1;
20809 }
f55ee35c 20810
1c809c68
TT
20811 /* A variable with DW_AT_external is never static,
20812 but it may be block-scoped. */
804d2729 20813 list_to_add
c24bdb02
KS
20814 = ((cu->list_in_scope
20815 == cu->get_builder ()->get_file_symbols ())
20816 ? cu->get_builder ()->get_global_symbols ()
804d2729 20817 : cu->list_in_scope);
1c809c68 20818 }
c906108c 20819 else
e37fd15a 20820 list_to_add = cu->list_in_scope;
c906108c
SS
20821 }
20822 else
20823 {
20824 /* We do not know the address of this symbol.
c5aa993b
JM
20825 If it is an external symbol and we have type information
20826 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20827 The address of the variable will then be determined from
20828 the minimal symbol table whenever the variable is
20829 referenced. */
e142c38c 20830 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20831
20832 /* Fortran explicitly imports any global symbols to the local
20833 scope by DW_TAG_common_block. */
20834 if (cu->language == language_fortran && die->parent
20835 && die->parent->tag == DW_TAG_common_block)
20836 {
20837 /* SYMBOL_CLASS doesn't matter here because
20838 read_common_block is going to reset it. */
20839 if (!suppress_add)
20840 list_to_add = cu->list_in_scope;
20841 }
20842 else if (attr2 && (DW_UNSND (attr2) != 0)
20843 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20844 {
0fe7935b
DJ
20845 /* A variable with DW_AT_external is never static, but it
20846 may be block-scoped. */
804d2729 20847 list_to_add
c24bdb02
KS
20848 = ((cu->list_in_scope
20849 == cu->get_builder ()->get_file_symbols ())
20850 ? cu->get_builder ()->get_global_symbols ()
804d2729 20851 : cu->list_in_scope);
0fe7935b 20852
f1e6e072 20853 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20854 }
442ddf59
JK
20855 else if (!die_is_declaration (die, cu))
20856 {
20857 /* Use the default LOC_OPTIMIZED_OUT class. */
20858 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20859 if (!suppress_add)
20860 list_to_add = cu->list_in_scope;
442ddf59 20861 }
c906108c
SS
20862 }
20863 break;
20864 case DW_TAG_formal_parameter:
a60f3166
TT
20865 {
20866 /* If we are inside a function, mark this as an argument. If
20867 not, we might be looking at an argument to an inlined function
20868 when we do not have enough information to show inlined frames;
20869 pretend it's a local variable in that case so that the user can
20870 still see it. */
804d2729 20871 struct context_stack *curr
c24bdb02 20872 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20873 if (curr != nullptr && curr->name != nullptr)
20874 SYMBOL_IS_ARGUMENT (sym) = 1;
20875 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20876 if (attr != nullptr)
a60f3166
TT
20877 {
20878 var_decode_location (attr, sym, cu);
20879 }
20880 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20881 if (attr != nullptr)
a60f3166
TT
20882 {
20883 dwarf2_const_value (attr, sym, cu);
20884 }
f346a30d 20885
a60f3166
TT
20886 list_to_add = cu->list_in_scope;
20887 }
c906108c
SS
20888 break;
20889 case DW_TAG_unspecified_parameters:
20890 /* From varargs functions; gdb doesn't seem to have any
20891 interest in this information, so just ignore it for now.
20892 (FIXME?) */
20893 break;
34eaf542
TT
20894 case DW_TAG_template_type_param:
20895 suppress_add = 1;
20896 /* Fall through. */
c906108c 20897 case DW_TAG_class_type:
680b30c7 20898 case DW_TAG_interface_type:
c906108c
SS
20899 case DW_TAG_structure_type:
20900 case DW_TAG_union_type:
72019c9c 20901 case DW_TAG_set_type:
c906108c 20902 case DW_TAG_enumeration_type:
f1e6e072 20903 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20904 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20905
63d06c5c 20906 {
9c37b5ae 20907 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20908 really ever be static objects: otherwise, if you try
20909 to, say, break of a class's method and you're in a file
20910 which doesn't mention that class, it won't work unless
20911 the check for all static symbols in lookup_symbol_aux
20912 saves you. See the OtherFileClass tests in
20913 gdb.c++/namespace.exp. */
20914
e37fd15a 20915 if (!suppress_add)
34eaf542 20916 {
c24bdb02 20917 buildsym_compunit *builder = cu->get_builder ();
804d2729 20918 list_to_add
c24bdb02 20919 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20920 && cu->language == language_cplus
c24bdb02 20921 ? builder->get_global_symbols ()
804d2729 20922 : cu->list_in_scope);
63d06c5c 20923
64382290 20924 /* The semantics of C++ state that "struct foo {
9c37b5ae 20925 ... }" also defines a typedef for "foo". */
64382290 20926 if (cu->language == language_cplus
45280282 20927 || cu->language == language_ada
c44af4eb
TT
20928 || cu->language == language_d
20929 || cu->language == language_rust)
64382290
TT
20930 {
20931 /* The symbol's name is already allocated along
20932 with this objfile, so we don't need to
20933 duplicate it for the type. */
7d93a1e0 20934 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20935 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20936 }
63d06c5c
DC
20937 }
20938 }
c906108c
SS
20939 break;
20940 case DW_TAG_typedef:
f1e6e072 20941 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20942 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20943 list_to_add = cu->list_in_scope;
63d06c5c 20944 break;
c906108c 20945 case DW_TAG_base_type:
a02abb62 20946 case DW_TAG_subrange_type:
f1e6e072 20947 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20948 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20949 list_to_add = cu->list_in_scope;
c906108c
SS
20950 break;
20951 case DW_TAG_enumerator:
e142c38c 20952 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20953 if (attr != nullptr)
c906108c 20954 {
e7c27a73 20955 dwarf2_const_value (attr, sym, cu);
c906108c 20956 }
63d06c5c
DC
20957 {
20958 /* NOTE: carlton/2003-11-10: See comment above in the
20959 DW_TAG_class_type, etc. block. */
20960
804d2729 20961 list_to_add
c24bdb02 20962 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20963 && cu->language == language_cplus
c24bdb02 20964 ? cu->get_builder ()->get_global_symbols ()
804d2729 20965 : cu->list_in_scope);
63d06c5c 20966 }
c906108c 20967 break;
74921315 20968 case DW_TAG_imported_declaration:
5c4e30ca 20969 case DW_TAG_namespace:
f1e6e072 20970 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20971 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20972 break;
530e8392
KB
20973 case DW_TAG_module:
20974 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20975 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20976 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20977 break;
4357ac6c 20978 case DW_TAG_common_block:
f1e6e072 20979 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20980 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20981 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20982 break;
c906108c
SS
20983 default:
20984 /* Not a tag we recognize. Hopefully we aren't processing
20985 trash data, but since we must specifically ignore things
20986 we don't recognize, there is nothing else we should do at
0963b4bd 20987 this point. */
b98664d3 20988 complaint (_("unsupported tag: '%s'"),
4d3c2250 20989 dwarf_tag_name (die->tag));
c906108c
SS
20990 break;
20991 }
df8a16a1 20992
e37fd15a
SW
20993 if (suppress_add)
20994 {
20995 sym->hash_next = objfile->template_symbols;
20996 objfile->template_symbols = sym;
20997 list_to_add = NULL;
20998 }
20999
21000 if (list_to_add != NULL)
d3cb6808 21001 add_symbol_to_list (sym, list_to_add);
e37fd15a 21002
df8a16a1
DJ
21003 /* For the benefit of old versions of GCC, check for anonymous
21004 namespaces based on the demangled name. */
4d4ec4e5 21005 if (!cu->processing_has_namespace_info
94af9270 21006 && cu->language == language_cplus)
c24bdb02 21007 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21008 }
21009 return (sym);
21010}
21011
98bfdba5
PA
21012/* Given an attr with a DW_FORM_dataN value in host byte order,
21013 zero-extend it as appropriate for the symbol's type. The DWARF
21014 standard (v4) is not entirely clear about the meaning of using
21015 DW_FORM_dataN for a constant with a signed type, where the type is
21016 wider than the data. The conclusion of a discussion on the DWARF
21017 list was that this is unspecified. We choose to always zero-extend
21018 because that is the interpretation long in use by GCC. */
c906108c 21019
98bfdba5 21020static gdb_byte *
ff39bb5e 21021dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21022 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21023{
518817b3 21024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21025 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21026 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21027 LONGEST l = DW_UNSND (attr);
21028
21029 if (bits < sizeof (*value) * 8)
21030 {
21031 l &= ((LONGEST) 1 << bits) - 1;
21032 *value = l;
21033 }
21034 else if (bits == sizeof (*value) * 8)
21035 *value = l;
21036 else
21037 {
224c3ddb 21038 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21039 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21040 return bytes;
21041 }
21042
21043 return NULL;
21044}
21045
21046/* Read a constant value from an attribute. Either set *VALUE, or if
21047 the value does not fit in *VALUE, set *BYTES - either already
21048 allocated on the objfile obstack, or newly allocated on OBSTACK,
21049 or, set *BATON, if we translated the constant to a location
21050 expression. */
21051
21052static void
ff39bb5e 21053dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21054 const char *name, struct obstack *obstack,
21055 struct dwarf2_cu *cu,
d521ce57 21056 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21057 struct dwarf2_locexpr_baton **baton)
21058{
518817b3 21059 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21060 struct comp_unit_head *cu_header = &cu->header;
c906108c 21061 struct dwarf_block *blk;
98bfdba5
PA
21062 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21063 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21064
21065 *value = 0;
21066 *bytes = NULL;
21067 *baton = NULL;
c906108c
SS
21068
21069 switch (attr->form)
21070 {
21071 case DW_FORM_addr:
336d760d 21072 case DW_FORM_addrx:
3019eac3 21073 case DW_FORM_GNU_addr_index:
ac56253d 21074 {
ac56253d
TT
21075 gdb_byte *data;
21076
98bfdba5
PA
21077 if (TYPE_LENGTH (type) != cu_header->addr_size)
21078 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21079 cu_header->addr_size,
98bfdba5 21080 TYPE_LENGTH (type));
ac56253d
TT
21081 /* Symbols of this form are reasonably rare, so we just
21082 piggyback on the existing location code rather than writing
21083 a new implementation of symbol_computed_ops. */
8d749320 21084 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21085 (*baton)->per_cu = cu->per_cu;
21086 gdb_assert ((*baton)->per_cu);
ac56253d 21087
98bfdba5 21088 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21089 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21090 (*baton)->data = data;
ac56253d
TT
21091
21092 data[0] = DW_OP_addr;
21093 store_unsigned_integer (&data[1], cu_header->addr_size,
21094 byte_order, DW_ADDR (attr));
21095 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21096 }
c906108c 21097 break;
4ac36638 21098 case DW_FORM_string:
93b5768b 21099 case DW_FORM_strp:
cf532bd1 21100 case DW_FORM_strx:
3019eac3 21101 case DW_FORM_GNU_str_index:
36586728 21102 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21103 /* DW_STRING is already allocated on the objfile obstack, point
21104 directly to it. */
d521ce57 21105 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21106 break;
c906108c
SS
21107 case DW_FORM_block1:
21108 case DW_FORM_block2:
21109 case DW_FORM_block4:
21110 case DW_FORM_block:
2dc7f7b3 21111 case DW_FORM_exprloc:
0224619f 21112 case DW_FORM_data16:
c906108c 21113 blk = DW_BLOCK (attr);
98bfdba5
PA
21114 if (TYPE_LENGTH (type) != blk->size)
21115 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21116 TYPE_LENGTH (type));
21117 *bytes = blk->data;
c906108c 21118 break;
2df3850c
JM
21119
21120 /* The DW_AT_const_value attributes are supposed to carry the
21121 symbol's value "represented as it would be on the target
21122 architecture." By the time we get here, it's already been
21123 converted to host endianness, so we just need to sign- or
21124 zero-extend it as appropriate. */
21125 case DW_FORM_data1:
3aef2284 21126 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21127 break;
c906108c 21128 case DW_FORM_data2:
3aef2284 21129 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21130 break;
c906108c 21131 case DW_FORM_data4:
3aef2284 21132 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21133 break;
c906108c 21134 case DW_FORM_data8:
3aef2284 21135 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21136 break;
21137
c906108c 21138 case DW_FORM_sdata:
663c44ac 21139 case DW_FORM_implicit_const:
98bfdba5 21140 *value = DW_SND (attr);
2df3850c
JM
21141 break;
21142
c906108c 21143 case DW_FORM_udata:
98bfdba5 21144 *value = DW_UNSND (attr);
c906108c 21145 break;
2df3850c 21146
c906108c 21147 default:
b98664d3 21148 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21149 dwarf_form_name (attr->form));
98bfdba5 21150 *value = 0;
c906108c
SS
21151 break;
21152 }
21153}
21154
2df3850c 21155
98bfdba5
PA
21156/* Copy constant value from an attribute to a symbol. */
21157
2df3850c 21158static void
ff39bb5e 21159dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21160 struct dwarf2_cu *cu)
2df3850c 21161{
518817b3 21162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21163 LONGEST value;
d521ce57 21164 const gdb_byte *bytes;
98bfdba5 21165 struct dwarf2_locexpr_baton *baton;
2df3850c 21166
98bfdba5 21167 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21168 sym->print_name (),
98bfdba5
PA
21169 &objfile->objfile_obstack, cu,
21170 &value, &bytes, &baton);
2df3850c 21171
98bfdba5
PA
21172 if (baton != NULL)
21173 {
98bfdba5 21174 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21175 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21176 }
21177 else if (bytes != NULL)
21178 {
21179 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21180 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21181 }
21182 else
21183 {
21184 SYMBOL_VALUE (sym) = value;
f1e6e072 21185 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21186 }
2df3850c
JM
21187}
21188
c906108c
SS
21189/* Return the type of the die in question using its DW_AT_type attribute. */
21190
21191static struct type *
e7c27a73 21192die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21193{
c906108c 21194 struct attribute *type_attr;
c906108c 21195
e142c38c 21196 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21197 if (!type_attr)
21198 {
518817b3 21199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21200 /* A missing DW_AT_type represents a void type. */
518817b3 21201 return objfile_type (objfile)->builtin_void;
c906108c 21202 }
348e048f 21203
673bfd45 21204 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21205}
21206
b4ba55a1
JB
21207/* True iff CU's producer generates GNAT Ada auxiliary information
21208 that allows to find parallel types through that information instead
21209 of having to do expensive parallel lookups by type name. */
21210
21211static int
21212need_gnat_info (struct dwarf2_cu *cu)
21213{
de4cb04a
JB
21214 /* Assume that the Ada compiler was GNAT, which always produces
21215 the auxiliary information. */
21216 return (cu->language == language_ada);
b4ba55a1
JB
21217}
21218
b4ba55a1
JB
21219/* Return the auxiliary type of the die in question using its
21220 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21221 attribute is not present. */
21222
21223static struct type *
21224die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21225{
b4ba55a1 21226 struct attribute *type_attr;
b4ba55a1
JB
21227
21228 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21229 if (!type_attr)
21230 return NULL;
21231
673bfd45 21232 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21233}
21234
21235/* If DIE has a descriptive_type attribute, then set the TYPE's
21236 descriptive type accordingly. */
21237
21238static void
21239set_descriptive_type (struct type *type, struct die_info *die,
21240 struct dwarf2_cu *cu)
21241{
21242 struct type *descriptive_type = die_descriptive_type (die, cu);
21243
21244 if (descriptive_type)
21245 {
21246 ALLOCATE_GNAT_AUX_TYPE (type);
21247 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21248 }
21249}
21250
c906108c
SS
21251/* Return the containing type of the die in question using its
21252 DW_AT_containing_type attribute. */
21253
21254static struct type *
e7c27a73 21255die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21256{
c906108c 21257 struct attribute *type_attr;
518817b3 21258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21259
e142c38c 21260 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21261 if (!type_attr)
21262 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21263 "[in module %s]"), objfile_name (objfile));
33ac96f0 21264
673bfd45 21265 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21266}
21267
ac9ec31b
DE
21268/* Return an error marker type to use for the ill formed type in DIE/CU. */
21269
21270static struct type *
21271build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21272{
518817b3
SM
21273 struct dwarf2_per_objfile *dwarf2_per_objfile
21274 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21275 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21276 char *saved;
ac9ec31b 21277
528e1572
SM
21278 std::string message
21279 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21280 objfile_name (objfile),
21281 sect_offset_str (cu->header.sect_off),
21282 sect_offset_str (die->sect_off));
efba19b0 21283 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21284
19f392bc 21285 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21286}
21287
673bfd45 21288/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21289 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21290 DW_AT_containing_type.
673bfd45
DE
21291 If there is no type substitute an error marker. */
21292
c906108c 21293static struct type *
ff39bb5e 21294lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21295 struct dwarf2_cu *cu)
c906108c 21296{
518817b3
SM
21297 struct dwarf2_per_objfile *dwarf2_per_objfile
21298 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21299 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21300 struct type *this_type;
21301
ac9ec31b
DE
21302 gdb_assert (attr->name == DW_AT_type
21303 || attr->name == DW_AT_GNAT_descriptive_type
21304 || attr->name == DW_AT_containing_type);
21305
673bfd45
DE
21306 /* First see if we have it cached. */
21307
36586728
TT
21308 if (attr->form == DW_FORM_GNU_ref_alt)
21309 {
21310 struct dwarf2_per_cu_data *per_cu;
0826b30a 21311 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21312
ed2dc618
SM
21313 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21314 dwarf2_per_objfile);
9c541725 21315 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21316 }
cd6c91b4 21317 else if (attr->form_is_ref ())
673bfd45 21318 {
0826b30a 21319 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21320
9c541725 21321 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21322 }
55f1336d 21323 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21324 {
ac9ec31b 21325 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21326
ac9ec31b 21327 return get_signatured_type (die, signature, cu);
673bfd45
DE
21328 }
21329 else
21330 {
b98664d3 21331 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21332 " at %s [in module %s]"),
21333 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21334 objfile_name (objfile));
ac9ec31b 21335 return build_error_marker_type (cu, die);
673bfd45
DE
21336 }
21337
21338 /* If not cached we need to read it in. */
21339
21340 if (this_type == NULL)
21341 {
ac9ec31b 21342 struct die_info *type_die = NULL;
673bfd45
DE
21343 struct dwarf2_cu *type_cu = cu;
21344
cd6c91b4 21345 if (attr->form_is_ref ())
ac9ec31b
DE
21346 type_die = follow_die_ref (die, attr, &type_cu);
21347 if (type_die == NULL)
21348 return build_error_marker_type (cu, die);
21349 /* If we find the type now, it's probably because the type came
3019eac3
DE
21350 from an inter-CU reference and the type's CU got expanded before
21351 ours. */
ac9ec31b 21352 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21353 }
21354
21355 /* If we still don't have a type use an error marker. */
21356
21357 if (this_type == NULL)
ac9ec31b 21358 return build_error_marker_type (cu, die);
673bfd45 21359
f792889a 21360 return this_type;
c906108c
SS
21361}
21362
673bfd45
DE
21363/* Return the type in DIE, CU.
21364 Returns NULL for invalid types.
21365
02142a6c 21366 This first does a lookup in die_type_hash,
673bfd45
DE
21367 and only reads the die in if necessary.
21368
21369 NOTE: This can be called when reading in partial or full symbols. */
21370
f792889a 21371static struct type *
e7c27a73 21372read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21373{
f792889a
DJ
21374 struct type *this_type;
21375
21376 this_type = get_die_type (die, cu);
21377 if (this_type)
21378 return this_type;
21379
673bfd45
DE
21380 return read_type_die_1 (die, cu);
21381}
21382
21383/* Read the type in DIE, CU.
21384 Returns NULL for invalid types. */
21385
21386static struct type *
21387read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21388{
21389 struct type *this_type = NULL;
21390
c906108c
SS
21391 switch (die->tag)
21392 {
21393 case DW_TAG_class_type:
680b30c7 21394 case DW_TAG_interface_type:
c906108c
SS
21395 case DW_TAG_structure_type:
21396 case DW_TAG_union_type:
f792889a 21397 this_type = read_structure_type (die, cu);
c906108c
SS
21398 break;
21399 case DW_TAG_enumeration_type:
f792889a 21400 this_type = read_enumeration_type (die, cu);
c906108c
SS
21401 break;
21402 case DW_TAG_subprogram:
21403 case DW_TAG_subroutine_type:
edb3359d 21404 case DW_TAG_inlined_subroutine:
f792889a 21405 this_type = read_subroutine_type (die, cu);
c906108c
SS
21406 break;
21407 case DW_TAG_array_type:
f792889a 21408 this_type = read_array_type (die, cu);
c906108c 21409 break;
72019c9c 21410 case DW_TAG_set_type:
f792889a 21411 this_type = read_set_type (die, cu);
72019c9c 21412 break;
c906108c 21413 case DW_TAG_pointer_type:
f792889a 21414 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21415 break;
21416 case DW_TAG_ptr_to_member_type:
f792889a 21417 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21418 break;
21419 case DW_TAG_reference_type:
4297a3f0
AV
21420 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21421 break;
21422 case DW_TAG_rvalue_reference_type:
21423 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21424 break;
21425 case DW_TAG_const_type:
f792889a 21426 this_type = read_tag_const_type (die, cu);
c906108c
SS
21427 break;
21428 case DW_TAG_volatile_type:
f792889a 21429 this_type = read_tag_volatile_type (die, cu);
c906108c 21430 break;
06d66ee9
TT
21431 case DW_TAG_restrict_type:
21432 this_type = read_tag_restrict_type (die, cu);
21433 break;
c906108c 21434 case DW_TAG_string_type:
f792889a 21435 this_type = read_tag_string_type (die, cu);
c906108c
SS
21436 break;
21437 case DW_TAG_typedef:
f792889a 21438 this_type = read_typedef (die, cu);
c906108c 21439 break;
a02abb62 21440 case DW_TAG_subrange_type:
f792889a 21441 this_type = read_subrange_type (die, cu);
a02abb62 21442 break;
c906108c 21443 case DW_TAG_base_type:
f792889a 21444 this_type = read_base_type (die, cu);
c906108c 21445 break;
81a17f79 21446 case DW_TAG_unspecified_type:
f792889a 21447 this_type = read_unspecified_type (die, cu);
81a17f79 21448 break;
0114d602
DJ
21449 case DW_TAG_namespace:
21450 this_type = read_namespace_type (die, cu);
21451 break;
f55ee35c
JK
21452 case DW_TAG_module:
21453 this_type = read_module_type (die, cu);
21454 break;
a2c2acaf
MW
21455 case DW_TAG_atomic_type:
21456 this_type = read_tag_atomic_type (die, cu);
21457 break;
c906108c 21458 default:
b98664d3 21459 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21460 dwarf_tag_name (die->tag));
c906108c
SS
21461 break;
21462 }
63d06c5c 21463
f792889a 21464 return this_type;
63d06c5c
DC
21465}
21466
abc72ce4
DE
21467/* See if we can figure out if the class lives in a namespace. We do
21468 this by looking for a member function; its demangled name will
21469 contain namespace info, if there is any.
21470 Return the computed name or NULL.
21471 Space for the result is allocated on the objfile's obstack.
21472 This is the full-die version of guess_partial_die_structure_name.
21473 In this case we know DIE has no useful parent. */
21474
43816ebc 21475static const char *
abc72ce4
DE
21476guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21477{
21478 struct die_info *spec_die;
21479 struct dwarf2_cu *spec_cu;
21480 struct die_info *child;
518817b3 21481 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21482
21483 spec_cu = cu;
21484 spec_die = die_specification (die, &spec_cu);
21485 if (spec_die != NULL)
21486 {
21487 die = spec_die;
21488 cu = spec_cu;
21489 }
21490
21491 for (child = die->child;
21492 child != NULL;
21493 child = child->sibling)
21494 {
21495 if (child->tag == DW_TAG_subprogram)
21496 {
73b9be8b 21497 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21498
7d45c7c3 21499 if (linkage_name != NULL)
abc72ce4 21500 {
43816ebc
TT
21501 gdb::unique_xmalloc_ptr<char> actual_name
21502 (language_class_name_from_physname (cu->language_defn,
21503 linkage_name));
21504 const char *name = NULL;
abc72ce4
DE
21505
21506 if (actual_name != NULL)
21507 {
15d034d0 21508 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21509
21510 if (die_name != NULL
43816ebc 21511 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21512 {
21513 /* Strip off the class name from the full name.
21514 We want the prefix. */
21515 int die_name_len = strlen (die_name);
43816ebc
TT
21516 int actual_name_len = strlen (actual_name.get ());
21517 const char *ptr = actual_name.get ();
abc72ce4
DE
21518
21519 /* Test for '::' as a sanity check. */
21520 if (actual_name_len > die_name_len + 2
43816ebc 21521 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21522 name = obstack_strndup (
e3b94546 21523 &objfile->per_bfd->storage_obstack,
43816ebc 21524 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21525 }
21526 }
abc72ce4
DE
21527 return name;
21528 }
21529 }
21530 }
21531
21532 return NULL;
21533}
21534
96408a79
SA
21535/* GCC might emit a nameless typedef that has a linkage name. Determine the
21536 prefix part in such case. See
21537 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21538
a121b7c1 21539static const char *
96408a79
SA
21540anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21541{
21542 struct attribute *attr;
e6a959d6 21543 const char *base;
96408a79
SA
21544
21545 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21546 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21547 return NULL;
21548
7d45c7c3 21549 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21550 return NULL;
21551
73b9be8b 21552 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21553 if (attr == NULL || DW_STRING (attr) == NULL)
21554 return NULL;
21555
21556 /* dwarf2_name had to be already called. */
21557 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21558
21559 /* Strip the base name, keep any leading namespaces/classes. */
21560 base = strrchr (DW_STRING (attr), ':');
21561 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21562 return "";
21563
518817b3 21564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21565 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21566 DW_STRING (attr),
21567 &base[-1] - DW_STRING (attr));
96408a79
SA
21568}
21569
fdde2d81 21570/* Return the name of the namespace/class that DIE is defined within,
0114d602 21571 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21572
0114d602
DJ
21573 For example, if we're within the method foo() in the following
21574 code:
21575
21576 namespace N {
21577 class C {
21578 void foo () {
21579 }
21580 };
21581 }
21582
21583 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21584
0d5cff50 21585static const char *
e142c38c 21586determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21587{
518817b3
SM
21588 struct dwarf2_per_objfile *dwarf2_per_objfile
21589 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21590 struct die_info *parent, *spec_die;
21591 struct dwarf2_cu *spec_cu;
21592 struct type *parent_type;
a121b7c1 21593 const char *retval;
63d06c5c 21594
9c37b5ae 21595 if (cu->language != language_cplus
c44af4eb
TT
21596 && cu->language != language_fortran && cu->language != language_d
21597 && cu->language != language_rust)
0114d602
DJ
21598 return "";
21599
96408a79
SA
21600 retval = anonymous_struct_prefix (die, cu);
21601 if (retval)
21602 return retval;
21603
0114d602
DJ
21604 /* We have to be careful in the presence of DW_AT_specification.
21605 For example, with GCC 3.4, given the code
21606
21607 namespace N {
21608 void foo() {
21609 // Definition of N::foo.
21610 }
21611 }
21612
21613 then we'll have a tree of DIEs like this:
21614
21615 1: DW_TAG_compile_unit
21616 2: DW_TAG_namespace // N
21617 3: DW_TAG_subprogram // declaration of N::foo
21618 4: DW_TAG_subprogram // definition of N::foo
21619 DW_AT_specification // refers to die #3
21620
21621 Thus, when processing die #4, we have to pretend that we're in
21622 the context of its DW_AT_specification, namely the contex of die
21623 #3. */
21624 spec_cu = cu;
21625 spec_die = die_specification (die, &spec_cu);
21626 if (spec_die == NULL)
21627 parent = die->parent;
21628 else
63d06c5c 21629 {
0114d602
DJ
21630 parent = spec_die->parent;
21631 cu = spec_cu;
63d06c5c 21632 }
0114d602
DJ
21633
21634 if (parent == NULL)
21635 return "";
98bfdba5
PA
21636 else if (parent->building_fullname)
21637 {
21638 const char *name;
21639 const char *parent_name;
21640
21641 /* It has been seen on RealView 2.2 built binaries,
21642 DW_TAG_template_type_param types actually _defined_ as
21643 children of the parent class:
21644
21645 enum E {};
21646 template class <class Enum> Class{};
21647 Class<enum E> class_e;
21648
21649 1: DW_TAG_class_type (Class)
21650 2: DW_TAG_enumeration_type (E)
21651 3: DW_TAG_enumerator (enum1:0)
21652 3: DW_TAG_enumerator (enum2:1)
21653 ...
21654 2: DW_TAG_template_type_param
21655 DW_AT_type DW_FORM_ref_udata (E)
21656
21657 Besides being broken debug info, it can put GDB into an
21658 infinite loop. Consider:
21659
21660 When we're building the full name for Class<E>, we'll start
21661 at Class, and go look over its template type parameters,
21662 finding E. We'll then try to build the full name of E, and
21663 reach here. We're now trying to build the full name of E,
21664 and look over the parent DIE for containing scope. In the
21665 broken case, if we followed the parent DIE of E, we'd again
21666 find Class, and once again go look at its template type
21667 arguments, etc., etc. Simply don't consider such parent die
21668 as source-level parent of this die (it can't be, the language
21669 doesn't allow it), and break the loop here. */
21670 name = dwarf2_name (die, cu);
21671 parent_name = dwarf2_name (parent, cu);
b98664d3 21672 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21673 name ? name : "<unknown>",
21674 parent_name ? parent_name : "<unknown>");
21675 return "";
21676 }
63d06c5c 21677 else
0114d602
DJ
21678 switch (parent->tag)
21679 {
63d06c5c 21680 case DW_TAG_namespace:
0114d602 21681 parent_type = read_type_die (parent, cu);
acebe513
UW
21682 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21683 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21684 Work around this problem here. */
21685 if (cu->language == language_cplus
7d93a1e0 21686 && strcmp (parent_type->name (), "::") == 0)
acebe513 21687 return "";
0114d602 21688 /* We give a name to even anonymous namespaces. */
7d93a1e0 21689 return parent_type->name ();
63d06c5c 21690 case DW_TAG_class_type:
680b30c7 21691 case DW_TAG_interface_type:
63d06c5c 21692 case DW_TAG_structure_type:
0114d602 21693 case DW_TAG_union_type:
f55ee35c 21694 case DW_TAG_module:
0114d602 21695 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21696 if (parent_type->name () != NULL)
21697 return parent_type->name ();
0114d602
DJ
21698 else
21699 /* An anonymous structure is only allowed non-static data
21700 members; no typedefs, no member functions, et cetera.
21701 So it does not need a prefix. */
21702 return "";
abc72ce4 21703 case DW_TAG_compile_unit:
95554aad 21704 case DW_TAG_partial_unit:
abc72ce4
DE
21705 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21706 if (cu->language == language_cplus
fd5866f6 21707 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21708 && die->child != NULL
21709 && (die->tag == DW_TAG_class_type
21710 || die->tag == DW_TAG_structure_type
21711 || die->tag == DW_TAG_union_type))
21712 {
43816ebc 21713 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21714 if (name != NULL)
21715 return name;
21716 }
21717 return "";
0a4b0913
AB
21718 case DW_TAG_subprogram:
21719 /* Nested subroutines in Fortran get a prefix with the name
21720 of the parent's subroutine. */
21721 if (cu->language == language_fortran)
21722 {
21723 if ((die->tag == DW_TAG_subprogram)
21724 && (dwarf2_name (parent, cu) != NULL))
21725 return dwarf2_name (parent, cu);
21726 }
21727 return determine_prefix (parent, cu);
3d567982
TT
21728 case DW_TAG_enumeration_type:
21729 parent_type = read_type_die (parent, cu);
21730 if (TYPE_DECLARED_CLASS (parent_type))
21731 {
7d93a1e0
SM
21732 if (parent_type->name () != NULL)
21733 return parent_type->name ();
3d567982
TT
21734 return "";
21735 }
21736 /* Fall through. */
63d06c5c 21737 default:
8176b9b8 21738 return determine_prefix (parent, cu);
63d06c5c 21739 }
63d06c5c
DC
21740}
21741
3e43a32a
MS
21742/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21743 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21744 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21745 an obconcat, otherwise allocate storage for the result. The CU argument is
21746 used to determine the language and hence, the appropriate separator. */
987504bb 21747
f55ee35c 21748#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21749
21750static char *
f55ee35c
JK
21751typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21752 int physname, struct dwarf2_cu *cu)
63d06c5c 21753{
f55ee35c 21754 const char *lead = "";
5c315b68 21755 const char *sep;
63d06c5c 21756
3e43a32a
MS
21757 if (suffix == NULL || suffix[0] == '\0'
21758 || prefix == NULL || prefix[0] == '\0')
987504bb 21759 sep = "";
45280282
IB
21760 else if (cu->language == language_d)
21761 {
21762 /* For D, the 'main' function could be defined in any module, but it
21763 should never be prefixed. */
21764 if (strcmp (suffix, "D main") == 0)
21765 {
21766 prefix = "";
21767 sep = "";
21768 }
21769 else
21770 sep = ".";
21771 }
f55ee35c
JK
21772 else if (cu->language == language_fortran && physname)
21773 {
21774 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21775 DW_AT_MIPS_linkage_name is preferred and used instead. */
21776
21777 lead = "__";
21778 sep = "_MOD_";
21779 }
987504bb
JJ
21780 else
21781 sep = "::";
63d06c5c 21782
6dd47d34
DE
21783 if (prefix == NULL)
21784 prefix = "";
21785 if (suffix == NULL)
21786 suffix = "";
21787
987504bb
JJ
21788 if (obs == NULL)
21789 {
3e43a32a 21790 char *retval
224c3ddb
SM
21791 = ((char *)
21792 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21793
f55ee35c
JK
21794 strcpy (retval, lead);
21795 strcat (retval, prefix);
6dd47d34
DE
21796 strcat (retval, sep);
21797 strcat (retval, suffix);
63d06c5c
DC
21798 return retval;
21799 }
987504bb
JJ
21800 else
21801 {
21802 /* We have an obstack. */
f55ee35c 21803 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21804 }
63d06c5c
DC
21805}
21806
71c25dea
TT
21807/* Get name of a die, return NULL if not found. */
21808
15d034d0
TT
21809static const char *
21810dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21811 struct objfile *objfile)
71c25dea
TT
21812{
21813 if (name && cu->language == language_cplus)
21814 {
596dc4ad
TT
21815 gdb::unique_xmalloc_ptr<char> canon_name
21816 = cp_canonicalize_string (name);
71c25dea 21817
596dc4ad
TT
21818 if (canon_name != nullptr)
21819 name = objfile->intern (canon_name.get ());
71c25dea
TT
21820 }
21821
21822 return name;
c906108c
SS
21823}
21824
96553a0c
DE
21825/* Get name of a die, return NULL if not found.
21826 Anonymous namespaces are converted to their magic string. */
9219021c 21827
15d034d0 21828static const char *
e142c38c 21829dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21830{
21831 struct attribute *attr;
518817b3 21832 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21833
e142c38c 21834 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21835 if ((!attr || !DW_STRING (attr))
96553a0c 21836 && die->tag != DW_TAG_namespace
53832f31
TT
21837 && die->tag != DW_TAG_class_type
21838 && die->tag != DW_TAG_interface_type
21839 && die->tag != DW_TAG_structure_type
21840 && die->tag != DW_TAG_union_type)
71c25dea
TT
21841 return NULL;
21842
21843 switch (die->tag)
21844 {
21845 case DW_TAG_compile_unit:
95554aad 21846 case DW_TAG_partial_unit:
71c25dea
TT
21847 /* Compilation units have a DW_AT_name that is a filename, not
21848 a source language identifier. */
21849 case DW_TAG_enumeration_type:
21850 case DW_TAG_enumerator:
21851 /* These tags always have simple identifiers already; no need
21852 to canonicalize them. */
21853 return DW_STRING (attr);
907af001 21854
96553a0c
DE
21855 case DW_TAG_namespace:
21856 if (attr != NULL && DW_STRING (attr) != NULL)
21857 return DW_STRING (attr);
21858 return CP_ANONYMOUS_NAMESPACE_STR;
21859
907af001
UW
21860 case DW_TAG_class_type:
21861 case DW_TAG_interface_type:
21862 case DW_TAG_structure_type:
21863 case DW_TAG_union_type:
21864 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21865 structures or unions. These were of the form "._%d" in GCC 4.1,
21866 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21867 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21868 if (attr && DW_STRING (attr)
61012eef
GB
21869 && (startswith (DW_STRING (attr), "._")
21870 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21871 return NULL;
53832f31
TT
21872
21873 /* GCC might emit a nameless typedef that has a linkage name. See
21874 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21875 if (!attr || DW_STRING (attr) == NULL)
21876 {
73b9be8b 21877 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21878 if (attr == NULL || DW_STRING (attr) == NULL)
21879 return NULL;
21880
df5c6c50
JK
21881 /* Avoid demangling DW_STRING (attr) the second time on a second
21882 call for the same DIE. */
21883 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21884 {
43816ebc
TT
21885 gdb::unique_xmalloc_ptr<char> demangled
21886 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21887 if (demangled == nullptr)
21888 return nullptr;
43816ebc 21889
be1e3d3e 21890 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21891 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21892 }
67430cd0
TT
21893
21894 /* Strip any leading namespaces/classes, keep only the base name.
21895 DW_AT_name for named DIEs does not contain the prefixes. */
21896 const char *base = strrchr (DW_STRING (attr), ':');
21897 if (base && base > DW_STRING (attr) && base[-1] == ':')
21898 return &base[1];
21899 else
21900 return DW_STRING (attr);
53832f31 21901 }
907af001
UW
21902 break;
21903
71c25dea 21904 default:
907af001
UW
21905 break;
21906 }
21907
21908 if (!DW_STRING_IS_CANONICAL (attr))
21909 {
be1e3d3e
TT
21910 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21911 objfile);
907af001 21912 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21913 }
907af001 21914 return DW_STRING (attr);
9219021c
DC
21915}
21916
21917/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21918 is none. *EXT_CU is the CU containing DIE on input, and the CU
21919 containing the return value on output. */
9219021c
DC
21920
21921static struct die_info *
f2f0e013 21922dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21923{
21924 struct attribute *attr;
9219021c 21925
f2f0e013 21926 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21927 if (attr == NULL)
21928 return NULL;
21929
f2f0e013 21930 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21931}
21932
f9aca02d 21933static void
d97bc12b 21934dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21935{
21936 unsigned int i;
21937
d97bc12b 21938 print_spaces (indent, f);
9d8780f0 21939 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21940 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21941 sect_offset_str (die->sect_off));
d97bc12b
DE
21942
21943 if (die->parent != NULL)
21944 {
21945 print_spaces (indent, f);
9d8780f0
SM
21946 fprintf_unfiltered (f, " parent at offset: %s\n",
21947 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21948 }
21949
21950 print_spaces (indent, f);
21951 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21952 dwarf_bool_name (die->child != NULL));
c906108c 21953
d97bc12b
DE
21954 print_spaces (indent, f);
21955 fprintf_unfiltered (f, " attributes:\n");
21956
c906108c
SS
21957 for (i = 0; i < die->num_attrs; ++i)
21958 {
d97bc12b
DE
21959 print_spaces (indent, f);
21960 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21961 dwarf_attr_name (die->attrs[i].name),
21962 dwarf_form_name (die->attrs[i].form));
d97bc12b 21963
c906108c
SS
21964 switch (die->attrs[i].form)
21965 {
c906108c 21966 case DW_FORM_addr:
336d760d 21967 case DW_FORM_addrx:
3019eac3 21968 case DW_FORM_GNU_addr_index:
d97bc12b 21969 fprintf_unfiltered (f, "address: ");
5af949e3 21970 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21971 break;
21972 case DW_FORM_block2:
21973 case DW_FORM_block4:
21974 case DW_FORM_block:
21975 case DW_FORM_block1:
56eb65bd
SP
21976 fprintf_unfiltered (f, "block: size %s",
21977 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21978 break;
2dc7f7b3 21979 case DW_FORM_exprloc:
56eb65bd
SP
21980 fprintf_unfiltered (f, "expression: size %s",
21981 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21982 break;
0224619f
JK
21983 case DW_FORM_data16:
21984 fprintf_unfiltered (f, "constant of 16 bytes");
21985 break;
4568ecf9
DE
21986 case DW_FORM_ref_addr:
21987 fprintf_unfiltered (f, "ref address: ");
21988 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21989 break;
36586728
TT
21990 case DW_FORM_GNU_ref_alt:
21991 fprintf_unfiltered (f, "alt ref address: ");
21992 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21993 break;
10b3939b
DJ
21994 case DW_FORM_ref1:
21995 case DW_FORM_ref2:
21996 case DW_FORM_ref4:
4568ecf9
DE
21997 case DW_FORM_ref8:
21998 case DW_FORM_ref_udata:
d97bc12b 21999 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22000 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22001 break;
c906108c
SS
22002 case DW_FORM_data1:
22003 case DW_FORM_data2:
22004 case DW_FORM_data4:
ce5d95e1 22005 case DW_FORM_data8:
c906108c
SS
22006 case DW_FORM_udata:
22007 case DW_FORM_sdata:
43bbcdc2
PH
22008 fprintf_unfiltered (f, "constant: %s",
22009 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22010 break;
2dc7f7b3
TT
22011 case DW_FORM_sec_offset:
22012 fprintf_unfiltered (f, "section offset: %s",
22013 pulongest (DW_UNSND (&die->attrs[i])));
22014 break;
55f1336d 22015 case DW_FORM_ref_sig8:
ac9ec31b
DE
22016 fprintf_unfiltered (f, "signature: %s",
22017 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22018 break;
c906108c 22019 case DW_FORM_string:
4bdf3d34 22020 case DW_FORM_strp:
43988095 22021 case DW_FORM_line_strp:
cf532bd1 22022 case DW_FORM_strx:
3019eac3 22023 case DW_FORM_GNU_str_index:
36586728 22024 case DW_FORM_GNU_strp_alt:
8285870a 22025 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22026 DW_STRING (&die->attrs[i])
8285870a
JK
22027 ? DW_STRING (&die->attrs[i]) : "",
22028 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22029 break;
22030 case DW_FORM_flag:
22031 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22032 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22033 else
d97bc12b 22034 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22035 break;
2dc7f7b3
TT
22036 case DW_FORM_flag_present:
22037 fprintf_unfiltered (f, "flag: TRUE");
22038 break;
a8329558 22039 case DW_FORM_indirect:
0963b4bd
MS
22040 /* The reader will have reduced the indirect form to
22041 the "base form" so this form should not occur. */
5f48f8f3 22042 fprintf_unfiltered (f,
3e43a32a 22043 "unexpected attribute form: DW_FORM_indirect");
a8329558 22044 break;
663c44ac
JK
22045 case DW_FORM_implicit_const:
22046 fprintf_unfiltered (f, "constant: %s",
22047 plongest (DW_SND (&die->attrs[i])));
22048 break;
c906108c 22049 default:
d97bc12b 22050 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22051 die->attrs[i].form);
d97bc12b 22052 break;
c906108c 22053 }
d97bc12b 22054 fprintf_unfiltered (f, "\n");
c906108c
SS
22055 }
22056}
22057
f9aca02d 22058static void
d97bc12b 22059dump_die_for_error (struct die_info *die)
c906108c 22060{
d97bc12b
DE
22061 dump_die_shallow (gdb_stderr, 0, die);
22062}
22063
22064static void
22065dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22066{
22067 int indent = level * 4;
22068
22069 gdb_assert (die != NULL);
22070
22071 if (level >= max_level)
22072 return;
22073
22074 dump_die_shallow (f, indent, die);
22075
22076 if (die->child != NULL)
c906108c 22077 {
d97bc12b
DE
22078 print_spaces (indent, f);
22079 fprintf_unfiltered (f, " Children:");
22080 if (level + 1 < max_level)
22081 {
22082 fprintf_unfiltered (f, "\n");
22083 dump_die_1 (f, level + 1, max_level, die->child);
22084 }
22085 else
22086 {
3e43a32a
MS
22087 fprintf_unfiltered (f,
22088 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22089 }
22090 }
22091
22092 if (die->sibling != NULL && level > 0)
22093 {
22094 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22095 }
22096}
22097
d97bc12b
DE
22098/* This is called from the pdie macro in gdbinit.in.
22099 It's not static so gcc will keep a copy callable from gdb. */
22100
22101void
22102dump_die (struct die_info *die, int max_level)
22103{
22104 dump_die_1 (gdb_stdlog, 0, max_level, die);
22105}
22106
f9aca02d 22107static void
51545339 22108store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22109{
51545339 22110 void **slot;
c906108c 22111
9c541725
PA
22112 slot = htab_find_slot_with_hash (cu->die_hash, die,
22113 to_underlying (die->sect_off),
b64f50a1 22114 INSERT);
51545339
DJ
22115
22116 *slot = die;
c906108c
SS
22117}
22118
348e048f
DE
22119/* Follow reference or signature attribute ATTR of SRC_DIE.
22120 On entry *REF_CU is the CU of SRC_DIE.
22121 On exit *REF_CU is the CU of the result. */
22122
22123static struct die_info *
ff39bb5e 22124follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22125 struct dwarf2_cu **ref_cu)
22126{
22127 struct die_info *die;
22128
cd6c91b4 22129 if (attr->form_is_ref ())
348e048f 22130 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22131 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22132 die = follow_die_sig (src_die, attr, ref_cu);
22133 else
22134 {
22135 dump_die_for_error (src_die);
22136 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22137 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22138 }
22139
22140 return die;
03dd20cc
DJ
22141}
22142
5c631832 22143/* Follow reference OFFSET.
673bfd45
DE
22144 On entry *REF_CU is the CU of the source die referencing OFFSET.
22145 On exit *REF_CU is the CU of the result.
22146 Returns NULL if OFFSET is invalid. */
f504f079 22147
f9aca02d 22148static struct die_info *
9c541725 22149follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22150 struct dwarf2_cu **ref_cu)
c906108c 22151{
10b3939b 22152 struct die_info temp_die;
f2f0e013 22153 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22154 struct dwarf2_per_objfile *dwarf2_per_objfile
22155 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22156
348e048f
DE
22157 gdb_assert (cu->per_cu != NULL);
22158
98bfdba5
PA
22159 target_cu = cu;
22160
3019eac3 22161 if (cu->per_cu->is_debug_types)
348e048f
DE
22162 {
22163 /* .debug_types CUs cannot reference anything outside their CU.
22164 If they need to, they have to reference a signatured type via
55f1336d 22165 DW_FORM_ref_sig8. */
4057dfde 22166 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22167 return NULL;
348e048f 22168 }
36586728 22169 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22170 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22171 {
22172 struct dwarf2_per_cu_data *per_cu;
9a619af0 22173
9c541725 22174 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22175 dwarf2_per_objfile);
03dd20cc
DJ
22176
22177 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22178 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22179 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22180
10b3939b
DJ
22181 target_cu = per_cu->cu;
22182 }
98bfdba5
PA
22183 else if (cu->dies == NULL)
22184 {
22185 /* We're loading full DIEs during partial symbol reading. */
22186 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22187 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22188 }
c906108c 22189
f2f0e013 22190 *ref_cu = target_cu;
9c541725 22191 temp_die.sect_off = sect_off;
c24bdb02
KS
22192
22193 if (target_cu != cu)
22194 target_cu->ancestor = cu;
22195
9a3c8263 22196 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22197 &temp_die,
22198 to_underlying (sect_off));
5c631832 22199}
10b3939b 22200
5c631832
JK
22201/* Follow reference attribute ATTR of SRC_DIE.
22202 On entry *REF_CU is the CU of SRC_DIE.
22203 On exit *REF_CU is the CU of the result. */
22204
22205static struct die_info *
ff39bb5e 22206follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22207 struct dwarf2_cu **ref_cu)
22208{
0826b30a 22209 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22210 struct dwarf2_cu *cu = *ref_cu;
22211 struct die_info *die;
22212
9c541725 22213 die = follow_die_offset (sect_off,
36586728
TT
22214 (attr->form == DW_FORM_GNU_ref_alt
22215 || cu->per_cu->is_dwz),
22216 ref_cu);
5c631832 22217 if (!die)
9d8780f0
SM
22218 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22219 "at %s [in module %s]"),
22220 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22221 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22222
5c631832
JK
22223 return die;
22224}
22225
d4c9a4f8 22226/* See read.h. */
5c631832
JK
22227
22228struct dwarf2_locexpr_baton
9c541725 22229dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22230 dwarf2_per_cu_data *per_cu,
8b9737bf 22231 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22232 void *baton, bool resolve_abstract_p)
5c631832 22233{
918dd910 22234 struct dwarf2_cu *cu;
5c631832
JK
22235 struct die_info *die;
22236 struct attribute *attr;
22237 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22238 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22239 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22240
918dd910 22241 if (per_cu->cu == NULL)
58f0c718 22242 load_cu (per_cu, false);
918dd910 22243 cu = per_cu->cu;
cc12ce38
DE
22244 if (cu == NULL)
22245 {
22246 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22247 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22248 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22249 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22250 }
918dd910 22251
9c541725 22252 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22253 if (!die)
9d8780f0
SM
22254 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22255 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22256
22257 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22258 if (!attr && resolve_abstract_p
3360b6e7 22259 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22260 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22261 {
22262 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22263 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22264 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22265
3360b6e7
TV
22266 for (const auto &cand_off
22267 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22268 {
3360b6e7
TV
22269 struct dwarf2_cu *cand_cu = cu;
22270 struct die_info *cand
22271 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22272 if (!cand
22273 || !cand->parent
e4a62c65
TV
22274 || cand->parent->tag != DW_TAG_subprogram)
22275 continue;
22276
22277 CORE_ADDR pc_low, pc_high;
22278 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22279 if (pc_low == ((CORE_ADDR) -1))
22280 continue;
22281 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22282 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22283 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22284 continue;
22285
22286 die = cand;
22287 attr = dwarf2_attr (die, DW_AT_location, cu);
22288 break;
22289 }
22290 }
22291
5c631832
JK
22292 if (!attr)
22293 {
e103e986
JK
22294 /* DWARF: "If there is no such attribute, then there is no effect.".
22295 DATA is ignored if SIZE is 0. */
5c631832 22296
e103e986 22297 retval.data = NULL;
5c631832
JK
22298 retval.size = 0;
22299 }
cd6c91b4 22300 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22301 {
22302 struct dwarf2_loclist_baton loclist_baton;
22303 CORE_ADDR pc = (*get_frame_pc) (baton);
22304 size_t size;
22305
22306 fill_in_loclist_baton (cu, &loclist_baton, attr);
22307
22308 retval.data = dwarf2_find_location_expression (&loclist_baton,
22309 &size, pc);
22310 retval.size = size;
22311 }
5c631832
JK
22312 else
22313 {
4fc6c0d5 22314 if (!attr->form_is_block ())
9d8780f0 22315 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22316 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22317 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22318
22319 retval.data = DW_BLOCK (attr)->data;
22320 retval.size = DW_BLOCK (attr)->size;
22321 }
22322 retval.per_cu = cu->per_cu;
918dd910 22323
ed2dc618 22324 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22325
5c631832 22326 return retval;
348e048f
DE
22327}
22328
d4c9a4f8 22329/* See read.h. */
8b9737bf
TT
22330
22331struct dwarf2_locexpr_baton
22332dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22333 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22334 CORE_ADDR (*get_frame_pc) (void *baton),
22335 void *baton)
22336{
9c541725 22337 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22338
9c541725 22339 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22340}
22341
b6807d98
TT
22342/* Write a constant of a given type as target-ordered bytes into
22343 OBSTACK. */
22344
22345static const gdb_byte *
22346write_constant_as_bytes (struct obstack *obstack,
22347 enum bfd_endian byte_order,
22348 struct type *type,
22349 ULONGEST value,
22350 LONGEST *len)
22351{
22352 gdb_byte *result;
22353
22354 *len = TYPE_LENGTH (type);
224c3ddb 22355 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22356 store_unsigned_integer (result, *len, byte_order, value);
22357
22358 return result;
22359}
22360
d4c9a4f8 22361/* See read.h. */
b6807d98
TT
22362
22363const gdb_byte *
9c541725 22364dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22365 dwarf2_per_cu_data *per_cu,
22366 obstack *obstack,
b6807d98
TT
22367 LONGEST *len)
22368{
22369 struct dwarf2_cu *cu;
22370 struct die_info *die;
22371 struct attribute *attr;
22372 const gdb_byte *result = NULL;
22373 struct type *type;
22374 LONGEST value;
22375 enum bfd_endian byte_order;
e3b94546 22376 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22377
b6807d98 22378 if (per_cu->cu == NULL)
58f0c718 22379 load_cu (per_cu, false);
b6807d98 22380 cu = per_cu->cu;
cc12ce38
DE
22381 if (cu == NULL)
22382 {
22383 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22384 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22385 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22386 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22387 }
b6807d98 22388
9c541725 22389 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22390 if (!die)
9d8780f0
SM
22391 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22392 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22393
22394 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22395 if (attr == NULL)
22396 return NULL;
22397
e3b94546 22398 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22399 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22400
22401 switch (attr->form)
22402 {
22403 case DW_FORM_addr:
336d760d 22404 case DW_FORM_addrx:
b6807d98
TT
22405 case DW_FORM_GNU_addr_index:
22406 {
22407 gdb_byte *tem;
22408
22409 *len = cu->header.addr_size;
224c3ddb 22410 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22411 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22412 result = tem;
22413 }
22414 break;
22415 case DW_FORM_string:
22416 case DW_FORM_strp:
cf532bd1 22417 case DW_FORM_strx:
b6807d98
TT
22418 case DW_FORM_GNU_str_index:
22419 case DW_FORM_GNU_strp_alt:
22420 /* DW_STRING is already allocated on the objfile obstack, point
22421 directly to it. */
22422 result = (const gdb_byte *) DW_STRING (attr);
22423 *len = strlen (DW_STRING (attr));
22424 break;
22425 case DW_FORM_block1:
22426 case DW_FORM_block2:
22427 case DW_FORM_block4:
22428 case DW_FORM_block:
22429 case DW_FORM_exprloc:
0224619f 22430 case DW_FORM_data16:
b6807d98
TT
22431 result = DW_BLOCK (attr)->data;
22432 *len = DW_BLOCK (attr)->size;
22433 break;
22434
22435 /* The DW_AT_const_value attributes are supposed to carry the
22436 symbol's value "represented as it would be on the target
22437 architecture." By the time we get here, it's already been
22438 converted to host endianness, so we just need to sign- or
22439 zero-extend it as appropriate. */
22440 case DW_FORM_data1:
22441 type = die_type (die, cu);
22442 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22443 if (result == NULL)
22444 result = write_constant_as_bytes (obstack, byte_order,
22445 type, value, len);
22446 break;
22447 case DW_FORM_data2:
22448 type = die_type (die, cu);
22449 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22450 if (result == NULL)
22451 result = write_constant_as_bytes (obstack, byte_order,
22452 type, value, len);
22453 break;
22454 case DW_FORM_data4:
22455 type = die_type (die, cu);
22456 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22457 if (result == NULL)
22458 result = write_constant_as_bytes (obstack, byte_order,
22459 type, value, len);
22460 break;
22461 case DW_FORM_data8:
22462 type = die_type (die, cu);
22463 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22464 if (result == NULL)
22465 result = write_constant_as_bytes (obstack, byte_order,
22466 type, value, len);
22467 break;
22468
22469 case DW_FORM_sdata:
663c44ac 22470 case DW_FORM_implicit_const:
b6807d98
TT
22471 type = die_type (die, cu);
22472 result = write_constant_as_bytes (obstack, byte_order,
22473 type, DW_SND (attr), len);
22474 break;
22475
22476 case DW_FORM_udata:
22477 type = die_type (die, cu);
22478 result = write_constant_as_bytes (obstack, byte_order,
22479 type, DW_UNSND (attr), len);
22480 break;
22481
22482 default:
b98664d3 22483 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22484 dwarf_form_name (attr->form));
22485 break;
22486 }
22487
22488 return result;
22489}
22490
d4c9a4f8 22491/* See read.h. */
7942e96e
AA
22492
22493struct type *
9c541725 22494dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22495 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22496{
22497 struct dwarf2_cu *cu;
22498 struct die_info *die;
22499
7942e96e 22500 if (per_cu->cu == NULL)
58f0c718 22501 load_cu (per_cu, false);
7942e96e
AA
22502 cu = per_cu->cu;
22503 if (!cu)
22504 return NULL;
22505
9c541725 22506 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22507 if (!die)
22508 return NULL;
22509
22510 return die_type (die, cu);
22511}
22512
8cb5117c 22513/* See read.h. */
8a9b8146
TT
22514
22515struct type *
b64f50a1 22516dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22517 struct dwarf2_per_cu_data *per_cu)
22518{
9c541725 22519 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22520 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22521}
22522
ac9ec31b 22523/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22524 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22525 On exit *REF_CU is the CU of the result.
22526 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22527
22528static struct die_info *
ac9ec31b
DE
22529follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22530 struct dwarf2_cu **ref_cu)
348e048f 22531{
348e048f 22532 struct die_info temp_die;
c24bdb02 22533 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22534 struct die_info *die;
22535
ac9ec31b
DE
22536 /* While it might be nice to assert sig_type->type == NULL here,
22537 we can get here for DW_AT_imported_declaration where we need
22538 the DIE not the type. */
348e048f
DE
22539
22540 /* If necessary, add it to the queue and load its DIEs. */
22541
95554aad 22542 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22543 read_signatured_type (sig_type);
348e048f 22544
348e048f 22545 sig_cu = sig_type->per_cu.cu;
69d751e3 22546 gdb_assert (sig_cu != NULL);
9c541725
PA
22547 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22548 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22549 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22550 to_underlying (temp_die.sect_off));
348e048f
DE
22551 if (die)
22552 {
ed2dc618 22553 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22554 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22555
796a7ff8
DE
22556 /* For .gdb_index version 7 keep track of included TUs.
22557 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22558 if (dwarf2_per_objfile->index_table != NULL
22559 && dwarf2_per_objfile->index_table->version <= 7)
22560 {
ae640021 22561 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22562 }
22563
348e048f 22564 *ref_cu = sig_cu;
c24bdb02
KS
22565 if (sig_cu != cu)
22566 sig_cu->ancestor = cu;
22567
348e048f
DE
22568 return die;
22569 }
22570
ac9ec31b
DE
22571 return NULL;
22572}
22573
22574/* Follow signatured type referenced by ATTR in SRC_DIE.
22575 On entry *REF_CU is the CU of SRC_DIE.
22576 On exit *REF_CU is the CU of the result.
22577 The result is the DIE of the type.
22578 If the referenced type cannot be found an error is thrown. */
22579
22580static struct die_info *
ff39bb5e 22581follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22582 struct dwarf2_cu **ref_cu)
22583{
22584 ULONGEST signature = DW_SIGNATURE (attr);
22585 struct signatured_type *sig_type;
22586 struct die_info *die;
22587
22588 gdb_assert (attr->form == DW_FORM_ref_sig8);
22589
a2ce51a0 22590 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22591 /* sig_type will be NULL if the signatured type is missing from
22592 the debug info. */
22593 if (sig_type == NULL)
22594 {
22595 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22596 " from DIE at %s [in module %s]"),
22597 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22598 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22599 }
22600
22601 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22602 if (die == NULL)
22603 {
22604 dump_die_for_error (src_die);
22605 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22606 " from DIE at %s [in module %s]"),
22607 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22608 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22609 }
22610
22611 return die;
22612}
22613
22614/* Get the type specified by SIGNATURE referenced in DIE/CU,
22615 reading in and processing the type unit if necessary. */
22616
22617static struct type *
22618get_signatured_type (struct die_info *die, ULONGEST signature,
22619 struct dwarf2_cu *cu)
22620{
518817b3
SM
22621 struct dwarf2_per_objfile *dwarf2_per_objfile
22622 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22623 struct signatured_type *sig_type;
22624 struct dwarf2_cu *type_cu;
22625 struct die_info *type_die;
22626 struct type *type;
22627
a2ce51a0 22628 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22629 /* sig_type will be NULL if the signatured type is missing from
22630 the debug info. */
22631 if (sig_type == NULL)
22632 {
b98664d3 22633 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22634 " from DIE at %s [in module %s]"),
22635 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22636 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22637 return build_error_marker_type (cu, die);
22638 }
22639
22640 /* If we already know the type we're done. */
22641 if (sig_type->type != NULL)
22642 return sig_type->type;
22643
22644 type_cu = cu;
22645 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22646 if (type_die != NULL)
22647 {
22648 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22649 is created. This is important, for example, because for c++ classes
22650 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22651 type = read_type_die (type_die, type_cu);
22652 if (type == NULL)
22653 {
b98664d3 22654 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22655 " referenced from DIE at %s [in module %s]"),
22656 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22657 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22658 type = build_error_marker_type (cu, die);
22659 }
22660 }
22661 else
22662 {
b98664d3 22663 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22664 " from DIE at %s [in module %s]"),
22665 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22666 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22667 type = build_error_marker_type (cu, die);
22668 }
22669 sig_type->type = type;
22670
22671 return type;
22672}
22673
22674/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22675 reading in and processing the type unit if necessary. */
22676
22677static struct type *
ff39bb5e 22678get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22679 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22680{
22681 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22682 if (attr->form_is_ref ())
ac9ec31b
DE
22683 {
22684 struct dwarf2_cu *type_cu = cu;
22685 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22686
22687 return read_type_die (type_die, type_cu);
22688 }
22689 else if (attr->form == DW_FORM_ref_sig8)
22690 {
22691 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22692 }
22693 else
22694 {
518817b3
SM
22695 struct dwarf2_per_objfile *dwarf2_per_objfile
22696 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22697
b98664d3 22698 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22699 " at %s [in module %s]"),
22700 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22701 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22702 return build_error_marker_type (cu, die);
22703 }
348e048f
DE
22704}
22705
e5fe5e75 22706/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22707
22708static void
e5fe5e75 22709load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22710{
52dc124a 22711 struct signatured_type *sig_type;
348e048f 22712
f4dc4d17 22713 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22714 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22715
6721b2ec
DE
22716 /* We have the per_cu, but we need the signatured_type.
22717 Fortunately this is an easy translation. */
22718 gdb_assert (per_cu->is_debug_types);
22719 sig_type = (struct signatured_type *) per_cu;
348e048f 22720
6721b2ec 22721 gdb_assert (per_cu->cu == NULL);
348e048f 22722
52dc124a 22723 read_signatured_type (sig_type);
348e048f 22724
6721b2ec 22725 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22726}
22727
3019eac3
DE
22728/* Read in a signatured type and build its CU and DIEs.
22729 If the type is a stub for the real type in a DWO file,
22730 read in the real type from the DWO file as well. */
dee91e82
DE
22731
22732static void
22733read_signatured_type (struct signatured_type *sig_type)
22734{
22735 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22736
3019eac3 22737 gdb_assert (per_cu->is_debug_types);
dee91e82 22738 gdb_assert (per_cu->cu == NULL);
348e048f 22739
6751ebae 22740 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22741
22742 if (!reader.dummy_p)
22743 {
22744 struct dwarf2_cu *cu = reader.cu;
22745 const gdb_byte *info_ptr = reader.info_ptr;
22746
22747 gdb_assert (cu->die_hash == NULL);
22748 cu->die_hash =
22749 htab_create_alloc_ex (cu->header.length / 12,
22750 die_hash,
22751 die_eq,
22752 NULL,
22753 &cu->comp_unit_obstack,
22754 hashtab_obstack_allocate,
22755 dummy_obstack_deallocate);
22756
3e225074 22757 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22758 reader.comp_unit_die->child
22759 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22760 reader.comp_unit_die);
22761 cu->dies = reader.comp_unit_die;
22762 /* comp_unit_die is not stored in die_hash, no need. */
22763
22764 /* We try not to read any attributes in this function, because
22765 not all CUs needed for references have been loaded yet, and
22766 symbol table processing isn't initialized. But we have to
22767 set the CU language, or we won't be able to build types
22768 correctly. Similarly, if we do not read the producer, we can
22769 not apply producer-specific interpretation. */
22770 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22771
22772 reader.keep ();
c0ab21c2
TT
22773 }
22774
7ee85ab1 22775 sig_type->per_cu.tu_read = 1;
c906108c
SS
22776}
22777
c906108c
SS
22778/* Decode simple location descriptions.
22779 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22780 the location and return the value. If COMPUTED is non-null, it is
22781 set to true to indicate that decoding was successful, and false
22782 otherwise. If COMPUTED is null, then this function may emit a
22783 complaint. */
c906108c
SS
22784
22785static CORE_ADDR
7d79de9a 22786decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22787{
518817b3 22788 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22789 size_t i;
22790 size_t size = blk->size;
d521ce57 22791 const gdb_byte *data = blk->data;
21ae7a4d
JK
22792 CORE_ADDR stack[64];
22793 int stacki;
22794 unsigned int bytes_read, unsnd;
22795 gdb_byte op;
c906108c 22796
7d79de9a
TT
22797 if (computed != nullptr)
22798 *computed = false;
22799
21ae7a4d
JK
22800 i = 0;
22801 stacki = 0;
22802 stack[stacki] = 0;
22803 stack[++stacki] = 0;
22804
22805 while (i < size)
22806 {
22807 op = data[i++];
22808 switch (op)
22809 {
22810 case DW_OP_lit0:
22811 case DW_OP_lit1:
22812 case DW_OP_lit2:
22813 case DW_OP_lit3:
22814 case DW_OP_lit4:
22815 case DW_OP_lit5:
22816 case DW_OP_lit6:
22817 case DW_OP_lit7:
22818 case DW_OP_lit8:
22819 case DW_OP_lit9:
22820 case DW_OP_lit10:
22821 case DW_OP_lit11:
22822 case DW_OP_lit12:
22823 case DW_OP_lit13:
22824 case DW_OP_lit14:
22825 case DW_OP_lit15:
22826 case DW_OP_lit16:
22827 case DW_OP_lit17:
22828 case DW_OP_lit18:
22829 case DW_OP_lit19:
22830 case DW_OP_lit20:
22831 case DW_OP_lit21:
22832 case DW_OP_lit22:
22833 case DW_OP_lit23:
22834 case DW_OP_lit24:
22835 case DW_OP_lit25:
22836 case DW_OP_lit26:
22837 case DW_OP_lit27:
22838 case DW_OP_lit28:
22839 case DW_OP_lit29:
22840 case DW_OP_lit30:
22841 case DW_OP_lit31:
22842 stack[++stacki] = op - DW_OP_lit0;
22843 break;
f1bea926 22844
21ae7a4d
JK
22845 case DW_OP_reg0:
22846 case DW_OP_reg1:
22847 case DW_OP_reg2:
22848 case DW_OP_reg3:
22849 case DW_OP_reg4:
22850 case DW_OP_reg5:
22851 case DW_OP_reg6:
22852 case DW_OP_reg7:
22853 case DW_OP_reg8:
22854 case DW_OP_reg9:
22855 case DW_OP_reg10:
22856 case DW_OP_reg11:
22857 case DW_OP_reg12:
22858 case DW_OP_reg13:
22859 case DW_OP_reg14:
22860 case DW_OP_reg15:
22861 case DW_OP_reg16:
22862 case DW_OP_reg17:
22863 case DW_OP_reg18:
22864 case DW_OP_reg19:
22865 case DW_OP_reg20:
22866 case DW_OP_reg21:
22867 case DW_OP_reg22:
22868 case DW_OP_reg23:
22869 case DW_OP_reg24:
22870 case DW_OP_reg25:
22871 case DW_OP_reg26:
22872 case DW_OP_reg27:
22873 case DW_OP_reg28:
22874 case DW_OP_reg29:
22875 case DW_OP_reg30:
22876 case DW_OP_reg31:
22877 stack[++stacki] = op - DW_OP_reg0;
22878 if (i < size)
7d79de9a
TT
22879 {
22880 if (computed == nullptr)
22881 dwarf2_complex_location_expr_complaint ();
22882 else
22883 return 0;
22884 }
21ae7a4d 22885 break;
c906108c 22886
21ae7a4d
JK
22887 case DW_OP_regx:
22888 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22889 i += bytes_read;
22890 stack[++stacki] = unsnd;
22891 if (i < size)
7d79de9a
TT
22892 {
22893 if (computed == nullptr)
22894 dwarf2_complex_location_expr_complaint ();
22895 else
22896 return 0;
22897 }
21ae7a4d 22898 break;
c906108c 22899
21ae7a4d 22900 case DW_OP_addr:
c8a7a66f
TT
22901 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22902 &bytes_read);
21ae7a4d
JK
22903 i += bytes_read;
22904 break;
d53d4ac5 22905
21ae7a4d
JK
22906 case DW_OP_const1u:
22907 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22908 i += 1;
22909 break;
22910
22911 case DW_OP_const1s:
22912 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22913 i += 1;
22914 break;
22915
22916 case DW_OP_const2u:
22917 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22918 i += 2;
22919 break;
22920
22921 case DW_OP_const2s:
22922 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22923 i += 2;
22924 break;
d53d4ac5 22925
21ae7a4d
JK
22926 case DW_OP_const4u:
22927 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22928 i += 4;
22929 break;
22930
22931 case DW_OP_const4s:
22932 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22933 i += 4;
22934 break;
22935
585861ea
JK
22936 case DW_OP_const8u:
22937 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22938 i += 8;
22939 break;
22940
21ae7a4d
JK
22941 case DW_OP_constu:
22942 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22943 &bytes_read);
22944 i += bytes_read;
22945 break;
22946
22947 case DW_OP_consts:
22948 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22949 i += bytes_read;
22950 break;
22951
22952 case DW_OP_dup:
22953 stack[stacki + 1] = stack[stacki];
22954 stacki++;
22955 break;
22956
22957 case DW_OP_plus:
22958 stack[stacki - 1] += stack[stacki];
22959 stacki--;
22960 break;
22961
22962 case DW_OP_plus_uconst:
22963 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22964 &bytes_read);
22965 i += bytes_read;
22966 break;
22967
22968 case DW_OP_minus:
22969 stack[stacki - 1] -= stack[stacki];
22970 stacki--;
22971 break;
22972
22973 case DW_OP_deref:
22974 /* If we're not the last op, then we definitely can't encode
22975 this using GDB's address_class enum. This is valid for partial
22976 global symbols, although the variable's address will be bogus
22977 in the psymtab. */
22978 if (i < size)
7d79de9a
TT
22979 {
22980 if (computed == nullptr)
22981 dwarf2_complex_location_expr_complaint ();
22982 else
22983 return 0;
22984 }
21ae7a4d
JK
22985 break;
22986
22987 case DW_OP_GNU_push_tls_address:
4aa4e28b 22988 case DW_OP_form_tls_address:
21ae7a4d
JK
22989 /* The top of the stack has the offset from the beginning
22990 of the thread control block at which the variable is located. */
22991 /* Nothing should follow this operator, so the top of stack would
22992 be returned. */
22993 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22994 address will be bogus in the psymtab. Make it always at least
22995 non-zero to not look as a variable garbage collected by linker
22996 which have DW_OP_addr 0. */
21ae7a4d 22997 if (i < size)
7d79de9a
TT
22998 {
22999 if (computed == nullptr)
23000 dwarf2_complex_location_expr_complaint ();
23001 else
23002 return 0;
23003 }
585861ea 23004 stack[stacki]++;
21ae7a4d
JK
23005 break;
23006
23007 case DW_OP_GNU_uninit:
7d79de9a
TT
23008 if (computed != nullptr)
23009 return 0;
21ae7a4d
JK
23010 break;
23011
336d760d 23012 case DW_OP_addrx:
3019eac3 23013 case DW_OP_GNU_addr_index:
49f6c839 23014 case DW_OP_GNU_const_index:
3019eac3
DE
23015 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23016 &bytes_read);
23017 i += bytes_read;
23018 break;
23019
21ae7a4d 23020 default:
7d79de9a
TT
23021 if (computed == nullptr)
23022 {
23023 const char *name = get_DW_OP_name (op);
21ae7a4d 23024
7d79de9a
TT
23025 if (name)
23026 complaint (_("unsupported stack op: '%s'"),
23027 name);
23028 else
23029 complaint (_("unsupported stack op: '%02x'"),
23030 op);
23031 }
21ae7a4d
JK
23032
23033 return (stack[stacki]);
d53d4ac5 23034 }
3c6e0cb3 23035
21ae7a4d
JK
23036 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23037 outside of the allocated space. Also enforce minimum>0. */
23038 if (stacki >= ARRAY_SIZE (stack) - 1)
23039 {
7d79de9a
TT
23040 if (computed == nullptr)
23041 complaint (_("location description stack overflow"));
21ae7a4d
JK
23042 return 0;
23043 }
23044
23045 if (stacki <= 0)
23046 {
7d79de9a
TT
23047 if (computed == nullptr)
23048 complaint (_("location description stack underflow"));
21ae7a4d
JK
23049 return 0;
23050 }
23051 }
7d79de9a
TT
23052
23053 if (computed != nullptr)
23054 *computed = true;
21ae7a4d 23055 return (stack[stacki]);
c906108c
SS
23056}
23057
23058/* memory allocation interface */
23059
c906108c 23060static struct dwarf_block *
7b5a2f43 23061dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23062{
8d749320 23063 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23064}
23065
c906108c 23066static struct die_info *
b60c80d6 23067dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23068{
23069 struct die_info *die;
b60c80d6
DJ
23070 size_t size = sizeof (struct die_info);
23071
23072 if (num_attrs > 1)
23073 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23074
b60c80d6 23075 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23076 memset (die, 0, sizeof (struct die_info));
23077 return (die);
23078}
2e276125
JB
23079
23080\f
a036ba48 23081
c90ec28a 23082/* Macro support. */
cf2c3c16 23083
9eac9650
TT
23084/* An overload of dwarf_decode_macros that finds the correct section
23085 and ensures it is read in before calling the other overload. */
23086
23087static void
23088dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23089 int section_is_gnu)
23090{
23091 struct dwarf2_per_objfile *dwarf2_per_objfile
23092 = cu->per_cu->dwarf2_per_objfile;
23093 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23094 const struct line_header *lh = cu->line_header;
9eac9650
TT
23095 unsigned int offset_size = cu->header.offset_size;
23096 struct dwarf2_section_info *section;
23097 const char *section_name;
23098
23099 if (cu->dwo_unit != nullptr)
23100 {
23101 if (section_is_gnu)
23102 {
23103 section = &cu->dwo_unit->dwo_file->sections.macro;
23104 section_name = ".debug_macro.dwo";
23105 }
23106 else
23107 {
23108 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23109 section_name = ".debug_macinfo.dwo";
23110 }
23111 }
23112 else
23113 {
23114 if (section_is_gnu)
23115 {
23116 section = &dwarf2_per_objfile->macro;
23117 section_name = ".debug_macro";
23118 }
23119 else
23120 {
23121 section = &dwarf2_per_objfile->macinfo;
23122 section_name = ".debug_macinfo";
23123 }
23124 }
23125
23126 section->read (objfile);
23127 if (section->buffer == nullptr)
23128 {
23129 complaint (_("missing %s section"), section_name);
23130 return;
23131 }
23132
23133 buildsym_compunit *builder = cu->get_builder ();
23134
23135 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23136 offset_size, offset, section_is_gnu);
23137}
23138
3019eac3
DE
23139/* Return the .debug_loc section to use for CU.
23140 For DWO files use .debug_loc.dwo. */
23141
23142static struct dwarf2_section_info *
23143cu_debug_loc_section (struct dwarf2_cu *cu)
23144{
518817b3
SM
23145 struct dwarf2_per_objfile *dwarf2_per_objfile
23146 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23147
3019eac3 23148 if (cu->dwo_unit)
43988095
JK
23149 {
23150 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23151
43988095
JK
23152 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23153 }
23154 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23155 : &dwarf2_per_objfile->loc);
3019eac3
DE
23156}
23157
8cf6f0b1
TT
23158/* A helper function that fills in a dwarf2_loclist_baton. */
23159
23160static void
23161fill_in_loclist_baton (struct dwarf2_cu *cu,
23162 struct dwarf2_loclist_baton *baton,
ff39bb5e 23163 const struct attribute *attr)
8cf6f0b1 23164{
518817b3
SM
23165 struct dwarf2_per_objfile *dwarf2_per_objfile
23166 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23167 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23168
96b79293 23169 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23170
23171 baton->per_cu = cu->per_cu;
23172 gdb_assert (baton->per_cu);
23173 /* We don't know how long the location list is, but make sure we
23174 don't run off the edge of the section. */
3019eac3
DE
23175 baton->size = section->size - DW_UNSND (attr);
23176 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23177 if (cu->base_address.has_value ())
23178 baton->base_address = *cu->base_address;
23179 else
23180 baton->base_address = 0;
f664829e 23181 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23182}
23183
4c2df51b 23184static void
ff39bb5e 23185dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23186 struct dwarf2_cu *cu, int is_block)
4c2df51b 23187{
518817b3
SM
23188 struct dwarf2_per_objfile *dwarf2_per_objfile
23189 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23190 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23191 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23192
cd6c91b4 23193 if (attr->form_is_section_offset ()
3019eac3 23194 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23195 the section. If so, fall through to the complaint in the
23196 other branch. */
2c7d5afc 23197 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23198 {
0d53c4c4 23199 struct dwarf2_loclist_baton *baton;
4c2df51b 23200
8d749320 23201 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23202
8cf6f0b1 23203 fill_in_loclist_baton (cu, baton, attr);
be391dca 23204
2b24b6e4 23205 if (!cu->base_address.has_value ())
b98664d3 23206 complaint (_("Location list used without "
3e43a32a 23207 "specifying the CU base address."));
4c2df51b 23208
f1e6e072
TT
23209 SYMBOL_ACLASS_INDEX (sym) = (is_block
23210 ? dwarf2_loclist_block_index
23211 : dwarf2_loclist_index);
0d53c4c4
DJ
23212 SYMBOL_LOCATION_BATON (sym) = baton;
23213 }
23214 else
23215 {
23216 struct dwarf2_locexpr_baton *baton;
23217
8d749320 23218 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23219 baton->per_cu = cu->per_cu;
23220 gdb_assert (baton->per_cu);
0d53c4c4 23221
4fc6c0d5 23222 if (attr->form_is_block ())
0d53c4c4
DJ
23223 {
23224 /* Note that we're just copying the block's data pointer
23225 here, not the actual data. We're still pointing into the
6502dd73
DJ
23226 info_buffer for SYM's objfile; right now we never release
23227 that buffer, but when we do clean up properly this may
23228 need to change. */
0d53c4c4
DJ
23229 baton->size = DW_BLOCK (attr)->size;
23230 baton->data = DW_BLOCK (attr)->data;
23231 }
23232 else
23233 {
23234 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23235 sym->natural_name ());
0d53c4c4 23236 baton->size = 0;
0d53c4c4 23237 }
6e70227d 23238
f1e6e072
TT
23239 SYMBOL_ACLASS_INDEX (sym) = (is_block
23240 ? dwarf2_locexpr_block_index
23241 : dwarf2_locexpr_index);
0d53c4c4
DJ
23242 SYMBOL_LOCATION_BATON (sym) = baton;
23243 }
4c2df51b 23244}
6502dd73 23245
09ba997f 23246/* See read.h. */
ae0d2f24
UW
23247
23248struct objfile *
09ba997f 23249dwarf2_per_cu_data::objfile () const
ae0d2f24 23250{
09ba997f 23251 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23252
23253 /* Return the master objfile, so that we can report and look up the
23254 correct file containing this variable. */
23255 if (objfile->separate_debug_objfile_backlink)
23256 objfile = objfile->separate_debug_objfile_backlink;
23257
23258 return objfile;
23259}
23260
96408a79
SA
23261/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23262 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23263 CU_HEADERP first. */
23264
23265static const struct comp_unit_head *
23266per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23267 const struct dwarf2_per_cu_data *per_cu)
96408a79 23268{
d521ce57 23269 const gdb_byte *info_ptr;
96408a79
SA
23270
23271 if (per_cu->cu)
23272 return &per_cu->cu->header;
23273
9c541725 23274 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23275
23276 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23277 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23278 rcuh_kind::COMPILE);
96408a79
SA
23279
23280 return cu_headerp;
23281}
23282
09ba997f 23283/* See read.h. */
ae0d2f24 23284
98714339 23285int
09ba997f 23286dwarf2_per_cu_data::addr_size () const
ae0d2f24 23287{
96408a79
SA
23288 struct comp_unit_head cu_header_local;
23289 const struct comp_unit_head *cu_headerp;
c471e790 23290
09ba997f 23291 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23292
23293 return cu_headerp->addr_size;
ae0d2f24
UW
23294}
23295
09ba997f 23296/* See read.h. */
9eae7c52
TT
23297
23298int
09ba997f 23299dwarf2_per_cu_data::offset_size () const
9eae7c52 23300{
96408a79
SA
23301 struct comp_unit_head cu_header_local;
23302 const struct comp_unit_head *cu_headerp;
9c6c53f7 23303
09ba997f 23304 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23305
23306 return cu_headerp->offset_size;
23307}
23308
09ba997f 23309/* See read.h. */
96408a79
SA
23310
23311int
09ba997f 23312dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23313{
23314 struct comp_unit_head cu_header_local;
23315 const struct comp_unit_head *cu_headerp;
23316
09ba997f 23317 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23318
23319 if (cu_headerp->version == 2)
23320 return cu_headerp->addr_size;
23321 else
23322 return cu_headerp->offset_size;
181cebd4
JK
23323}
23324
09ba997f 23325/* See read.h. */
9aa1f1e3
TT
23326
23327CORE_ADDR
09ba997f 23328dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23329{
09ba997f
TT
23330 struct objfile *objfile = dwarf2_per_objfile->objfile;
23331
23332 return objfile->text_section_offset ();
9aa1f1e3
TT
23333}
23334
09ba997f
TT
23335/* See read.h. */
23336
23337struct type *
23338dwarf2_per_cu_data::addr_type () const
9a49df9d 23339{
09ba997f 23340 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23341 struct type *void_type = objfile_type (objfile)->builtin_void;
23342 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23343 int addr_size = this->addr_size ();
9a49df9d
AB
23344
23345 if (TYPE_LENGTH (addr_type) == addr_size)
23346 return addr_type;
23347
09ba997f 23348 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23349 return addr_type;
23350}
23351
22b6cd70
TT
23352/* A helper function for dwarf2_find_containing_comp_unit that returns
23353 the index of the result, and that searches a vector. It will
23354 return a result even if the offset in question does not actually
23355 occur in any CU. This is separate so that it can be unit
23356 tested. */
ae038cb0 23357
22b6cd70
TT
23358static int
23359dwarf2_find_containing_comp_unit
23360 (sect_offset sect_off,
23361 unsigned int offset_in_dwz,
23362 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23363{
ae038cb0
DJ
23364 int low, high;
23365
ae038cb0 23366 low = 0;
22b6cd70 23367 high = all_comp_units.size () - 1;
ae038cb0
DJ
23368 while (high > low)
23369 {
36586728 23370 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23371 int mid = low + (high - low) / 2;
9a619af0 23372
22b6cd70 23373 mid_cu = all_comp_units[mid];
36586728 23374 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23375 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23376 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23377 high = mid;
23378 else
23379 low = mid + 1;
23380 }
23381 gdb_assert (low == high);
22b6cd70
TT
23382 return low;
23383}
23384
23385/* Locate the .debug_info compilation unit from CU's objfile which contains
23386 the DIE at OFFSET. Raises an error on failure. */
23387
23388static struct dwarf2_per_cu_data *
23389dwarf2_find_containing_comp_unit (sect_offset sect_off,
23390 unsigned int offset_in_dwz,
23391 struct dwarf2_per_objfile *dwarf2_per_objfile)
23392{
23393 int low
23394 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23395 dwarf2_per_objfile->all_comp_units);
23396 struct dwarf2_per_cu_data *this_cu
23397 = dwarf2_per_objfile->all_comp_units[low];
23398
45b8ae0c 23399 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23400 {
36586728 23401 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23402 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23403 "offset %s [in module %s]"),
23404 sect_offset_str (sect_off),
ed2dc618 23405 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23406
9c541725
PA
23407 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23408 <= sect_off);
ae038cb0
DJ
23409 return dwarf2_per_objfile->all_comp_units[low-1];
23410 }
23411 else
23412 {
b76e467d 23413 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23414 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23415 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23416 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23417 return this_cu;
23418 }
23419}
23420
22b6cd70
TT
23421#if GDB_SELF_TEST
23422
23423namespace selftests {
23424namespace find_containing_comp_unit {
23425
23426static void
23427run_test ()
23428{
23429 struct dwarf2_per_cu_data one {};
23430 struct dwarf2_per_cu_data two {};
23431 struct dwarf2_per_cu_data three {};
23432 struct dwarf2_per_cu_data four {};
23433
23434 one.length = 5;
23435 two.sect_off = sect_offset (one.length);
23436 two.length = 7;
23437
23438 three.length = 5;
23439 three.is_dwz = 1;
23440 four.sect_off = sect_offset (three.length);
23441 four.length = 7;
23442 four.is_dwz = 1;
23443
23444 std::vector<dwarf2_per_cu_data *> units;
23445 units.push_back (&one);
23446 units.push_back (&two);
23447 units.push_back (&three);
23448 units.push_back (&four);
23449
23450 int result;
23451
23452 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23453 SELF_CHECK (units[result] == &one);
23454 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23455 SELF_CHECK (units[result] == &one);
23456 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23457 SELF_CHECK (units[result] == &two);
23458
23459 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23460 SELF_CHECK (units[result] == &three);
23461 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23462 SELF_CHECK (units[result] == &three);
23463 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23464 SELF_CHECK (units[result] == &four);
23465}
23466
23467}
23468}
23469
23470#endif /* GDB_SELF_TEST */
23471
23745b47 23472/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23473
fcd3b13d
SM
23474dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23475 : per_cu (per_cu_),
9068261f
AB
23476 mark (false),
23477 has_loclist (false),
23478 checked_producer (false),
23479 producer_is_gxx_lt_4_6 (false),
23480 producer_is_gcc_lt_4_3 (false),
eb77c9df 23481 producer_is_icc (false),
9068261f 23482 producer_is_icc_lt_14 (false),
c258c396 23483 producer_is_codewarrior (false),
9068261f 23484 processing_has_namespace_info (false)
93311388 23485{
fcd3b13d
SM
23486 per_cu->cu = this;
23487}
23488
23489/* Destroy a dwarf2_cu. */
23490
23491dwarf2_cu::~dwarf2_cu ()
23492{
23493 per_cu->cu = NULL;
9816fde3
JK
23494}
23495
23496/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23497
23498static void
95554aad
TT
23499prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23500 enum language pretend_language)
9816fde3
JK
23501{
23502 struct attribute *attr;
23503
23504 /* Set the language we're debugging. */
23505 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23506 if (attr != nullptr)
9816fde3
JK
23507 set_cu_language (DW_UNSND (attr), cu);
23508 else
9cded63f 23509 {
95554aad 23510 cu->language = pretend_language;
9cded63f
TT
23511 cu->language_defn = language_def (cu->language);
23512 }
dee91e82 23513
7d45c7c3 23514 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23515}
23516
ae038cb0
DJ
23517/* Increase the age counter on each cached compilation unit, and free
23518 any that are too old. */
23519
23520static void
ed2dc618 23521age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23522{
23523 struct dwarf2_per_cu_data *per_cu, **last_chain;
23524
23525 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23526 per_cu = dwarf2_per_objfile->read_in_chain;
23527 while (per_cu != NULL)
23528 {
23529 per_cu->cu->last_used ++;
b4f54984 23530 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23531 dwarf2_mark (per_cu->cu);
23532 per_cu = per_cu->cu->read_in_chain;
23533 }
23534
23535 per_cu = dwarf2_per_objfile->read_in_chain;
23536 last_chain = &dwarf2_per_objfile->read_in_chain;
23537 while (per_cu != NULL)
23538 {
23539 struct dwarf2_per_cu_data *next_cu;
23540
23541 next_cu = per_cu->cu->read_in_chain;
23542
23543 if (!per_cu->cu->mark)
23544 {
fcd3b13d 23545 delete per_cu->cu;
ae038cb0
DJ
23546 *last_chain = next_cu;
23547 }
23548 else
23549 last_chain = &per_cu->cu->read_in_chain;
23550
23551 per_cu = next_cu;
23552 }
23553}
23554
23555/* Remove a single compilation unit from the cache. */
23556
23557static void
dee91e82 23558free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23559{
23560 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23561 struct dwarf2_per_objfile *dwarf2_per_objfile
23562 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23563
23564 per_cu = dwarf2_per_objfile->read_in_chain;
23565 last_chain = &dwarf2_per_objfile->read_in_chain;
23566 while (per_cu != NULL)
23567 {
23568 struct dwarf2_per_cu_data *next_cu;
23569
23570 next_cu = per_cu->cu->read_in_chain;
23571
dee91e82 23572 if (per_cu == target_per_cu)
ae038cb0 23573 {
fcd3b13d 23574 delete per_cu->cu;
dee91e82 23575 per_cu->cu = NULL;
ae038cb0
DJ
23576 *last_chain = next_cu;
23577 break;
23578 }
23579 else
23580 last_chain = &per_cu->cu->read_in_chain;
23581
23582 per_cu = next_cu;
23583 }
23584}
23585
dee91e82
DE
23586/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23587 We store these in a hash table separate from the DIEs, and preserve them
23588 when the DIEs are flushed out of cache.
23589
23590 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23591 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23592 or the type may come from a DWO file. Furthermore, while it's more logical
23593 to use per_cu->section+offset, with Fission the section with the data is in
23594 the DWO file but we don't know that section at the point we need it.
23595 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23596 because we can enter the lookup routine, get_die_type_at_offset, from
23597 outside this file, and thus won't necessarily have PER_CU->cu.
23598 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23599
dee91e82 23600struct dwarf2_per_cu_offset_and_type
1c379e20 23601{
dee91e82 23602 const struct dwarf2_per_cu_data *per_cu;
9c541725 23603 sect_offset sect_off;
1c379e20
DJ
23604 struct type *type;
23605};
23606
dee91e82 23607/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23608
23609static hashval_t
dee91e82 23610per_cu_offset_and_type_hash (const void *item)
1c379e20 23611{
9a3c8263
SM
23612 const struct dwarf2_per_cu_offset_and_type *ofs
23613 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23614
9c541725 23615 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23616}
23617
dee91e82 23618/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23619
23620static int
dee91e82 23621per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23622{
9a3c8263
SM
23623 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23624 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23625 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23626 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23627
dee91e82 23628 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23629 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23630}
23631
23632/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23633 table if necessary. For convenience, return TYPE.
23634
23635 The DIEs reading must have careful ordering to:
85102364 23636 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23637 reading current DIE.
23638 * Not trying to dereference contents of still incompletely read in types
23639 while reading in other DIEs.
23640 * Enable referencing still incompletely read in types just by a pointer to
23641 the type without accessing its fields.
23642
23643 Therefore caller should follow these rules:
23644 * Try to fetch any prerequisite types we may need to build this DIE type
23645 before building the type and calling set_die_type.
e71ec853 23646 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23647 possible before fetching more types to complete the current type.
23648 * Make the type as complete as possible before fetching more types. */
1c379e20 23649
f792889a 23650static struct type *
1c379e20
DJ
23651set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23652{
518817b3
SM
23653 struct dwarf2_per_objfile *dwarf2_per_objfile
23654 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23655 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23656 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23657 struct attribute *attr;
23658 struct dynamic_prop prop;
1c379e20 23659
b4ba55a1
JB
23660 /* For Ada types, make sure that the gnat-specific data is always
23661 initialized (if not already set). There are a few types where
23662 we should not be doing so, because the type-specific area is
23663 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23664 where the type-specific area is used to store the floatformat).
23665 But this is not a problem, because the gnat-specific information
23666 is actually not needed for these types. */
23667 if (need_gnat_info (cu)
78134374
SM
23668 && type->code () != TYPE_CODE_FUNC
23669 && type->code () != TYPE_CODE_FLT
23670 && type->code () != TYPE_CODE_METHODPTR
23671 && type->code () != TYPE_CODE_MEMBERPTR
23672 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23673 && !HAVE_GNAT_AUX_INFO (type))
23674 INIT_GNAT_SPECIFIC (type);
23675
3f2f83dd
KB
23676 /* Read DW_AT_allocated and set in type. */
23677 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23678 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23679 {
09ba997f 23680 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23681 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23682 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23683 }
23684 else if (attr != NULL)
23685 {
b98664d3 23686 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23687 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23688 sect_offset_str (die->sect_off));
3f2f83dd
KB
23689 }
23690
23691 /* Read DW_AT_associated and set in type. */
23692 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23693 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23694 {
09ba997f 23695 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23696 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23697 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23698 }
23699 else if (attr != NULL)
23700 {
b98664d3 23701 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23702 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23703 sect_offset_str (die->sect_off));
3f2f83dd
KB
23704 }
23705
3cdcd0ce
JB
23706 /* Read DW_AT_data_location and set in type. */
23707 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23708 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23709 cu->per_cu->addr_type ()))
5c54719c 23710 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23711
dee91e82 23712 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23713 dwarf2_per_objfile->die_type_hash
23714 = htab_up (htab_create_alloc (127,
23715 per_cu_offset_and_type_hash,
23716 per_cu_offset_and_type_eq,
23717 NULL, xcalloc, xfree));
1c379e20 23718
dee91e82 23719 ofs.per_cu = cu->per_cu;
9c541725 23720 ofs.sect_off = die->sect_off;
1c379e20 23721 ofs.type = type;
dee91e82 23722 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23723 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23724 if (*slot)
b98664d3 23725 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23726 sect_offset_str (die->sect_off));
8d749320
SM
23727 *slot = XOBNEW (&objfile->objfile_obstack,
23728 struct dwarf2_per_cu_offset_and_type);
1c379e20 23729 **slot = ofs;
f792889a 23730 return type;
1c379e20
DJ
23731}
23732
9c541725 23733/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23734 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23735
23736static struct type *
9c541725 23737get_die_type_at_offset (sect_offset sect_off,
673bfd45 23738 struct dwarf2_per_cu_data *per_cu)
1c379e20 23739{
dee91e82 23740 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23741 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23742
dee91e82 23743 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23744 return NULL;
1c379e20 23745
dee91e82 23746 ofs.per_cu = per_cu;
9c541725 23747 ofs.sect_off = sect_off;
9a3c8263 23748 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23749 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23750 if (slot)
23751 return slot->type;
23752 else
23753 return NULL;
23754}
23755
02142a6c 23756/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23757 or return NULL if DIE does not have a saved type. */
23758
23759static struct type *
23760get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23761{
9c541725 23762 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23763}
23764
10b3939b
DJ
23765/* Add a dependence relationship from CU to REF_PER_CU. */
23766
23767static void
23768dwarf2_add_dependence (struct dwarf2_cu *cu,
23769 struct dwarf2_per_cu_data *ref_per_cu)
23770{
23771 void **slot;
23772
23773 if (cu->dependencies == NULL)
23774 cu->dependencies
23775 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23776 NULL, &cu->comp_unit_obstack,
23777 hashtab_obstack_allocate,
23778 dummy_obstack_deallocate);
23779
23780 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23781 if (*slot == NULL)
23782 *slot = ref_per_cu;
23783}
1c379e20 23784
f504f079
DE
23785/* Subroutine of dwarf2_mark to pass to htab_traverse.
23786 Set the mark field in every compilation unit in the
ae038cb0
DJ
23787 cache that we must keep because we are keeping CU. */
23788
10b3939b
DJ
23789static int
23790dwarf2_mark_helper (void **slot, void *data)
23791{
23792 struct dwarf2_per_cu_data *per_cu;
23793
23794 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23795
23796 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23797 reading of the chain. As such dependencies remain valid it is not much
23798 useful to track and undo them during QUIT cleanups. */
23799 if (per_cu->cu == NULL)
23800 return 1;
23801
10b3939b
DJ
23802 if (per_cu->cu->mark)
23803 return 1;
9068261f 23804 per_cu->cu->mark = true;
10b3939b
DJ
23805
23806 if (per_cu->cu->dependencies != NULL)
23807 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23808
23809 return 1;
23810}
23811
f504f079
DE
23812/* Set the mark field in CU and in every other compilation unit in the
23813 cache that we must keep because we are keeping CU. */
23814
ae038cb0
DJ
23815static void
23816dwarf2_mark (struct dwarf2_cu *cu)
23817{
23818 if (cu->mark)
23819 return;
9068261f 23820 cu->mark = true;
10b3939b
DJ
23821 if (cu->dependencies != NULL)
23822 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23823}
23824
23825static void
23826dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23827{
23828 while (per_cu)
23829 {
9068261f 23830 per_cu->cu->mark = false;
ae038cb0
DJ
23831 per_cu = per_cu->cu->read_in_chain;
23832 }
72bf9492
DJ
23833}
23834
72bf9492
DJ
23835/* Trivial hash function for partial_die_info: the hash value of a DIE
23836 is its offset in .debug_info for this objfile. */
23837
23838static hashval_t
23839partial_die_hash (const void *item)
23840{
9a3c8263
SM
23841 const struct partial_die_info *part_die
23842 = (const struct partial_die_info *) item;
9a619af0 23843
9c541725 23844 return to_underlying (part_die->sect_off);
72bf9492
DJ
23845}
23846
23847/* Trivial comparison function for partial_die_info structures: two DIEs
23848 are equal if they have the same offset. */
23849
23850static int
23851partial_die_eq (const void *item_lhs, const void *item_rhs)
23852{
9a3c8263
SM
23853 const struct partial_die_info *part_die_lhs
23854 = (const struct partial_die_info *) item_lhs;
23855 const struct partial_die_info *part_die_rhs
23856 = (const struct partial_die_info *) item_rhs;
9a619af0 23857
9c541725 23858 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23859}
23860
3c3bb058
AB
23861struct cmd_list_element *set_dwarf_cmdlist;
23862struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23863
9291a0cd 23864static void
cd4fb1b2
SM
23865show_check_physname (struct ui_file *file, int from_tty,
23866 struct cmd_list_element *c, const char *value)
9291a0cd 23867{
cd4fb1b2
SM
23868 fprintf_filtered (file,
23869 _("Whether to check \"physname\" is %s.\n"),
23870 value);
9291a0cd
TT
23871}
23872
6c265988 23873void _initialize_dwarf2_read ();
cd4fb1b2 23874void
6c265988 23875_initialize_dwarf2_read ()
9291a0cd 23876{
0743fc83 23877 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23878Set DWARF specific variables.\n\
590042fc 23879Configure DWARF variables such as the cache size."),
0743fc83
TT
23880 &set_dwarf_cmdlist, "maintenance set dwarf ",
23881 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23882
0743fc83 23883 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23884Show DWARF specific variables.\n\
23885Show DWARF variables such as the cache size."),
0743fc83
TT
23886 &show_dwarf_cmdlist, "maintenance show dwarf ",
23887 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23888
cd4fb1b2
SM
23889 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23890 &dwarf_max_cache_age, _("\
23891Set the upper bound on the age of cached DWARF compilation units."), _("\
23892Show the upper bound on the age of cached DWARF compilation units."), _("\
23893A higher limit means that cached compilation units will be stored\n\
23894in memory longer, and more total memory will be used. Zero disables\n\
23895caching, which can slow down startup."),
23896 NULL,
23897 show_dwarf_max_cache_age,
23898 &set_dwarf_cmdlist,
23899 &show_dwarf_cmdlist);
156942c7 23900
cd4fb1b2
SM
23901 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23902Set debugging of the DWARF reader."), _("\
23903Show debugging of the DWARF reader."), _("\
23904When enabled (non-zero), debugging messages are printed during DWARF\n\
23905reading and symtab expansion. A value of 1 (one) provides basic\n\
23906information. A value greater than 1 provides more verbose information."),
23907 NULL,
23908 NULL,
23909 &setdebuglist, &showdebuglist);
9291a0cd 23910
cd4fb1b2
SM
23911 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23912Set debugging of the DWARF DIE reader."), _("\
23913Show debugging of the DWARF DIE reader."), _("\
23914When enabled (non-zero), DIEs are dumped after they are read in.\n\
23915The value is the maximum depth to print."),
23916 NULL,
23917 NULL,
23918 &setdebuglist, &showdebuglist);
9291a0cd 23919
cd4fb1b2
SM
23920 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23921Set debugging of the dwarf line reader."), _("\
23922Show debugging of the dwarf line reader."), _("\
23923When enabled (non-zero), line number entries are dumped as they are read in.\n\
23924A value of 1 (one) provides basic information.\n\
23925A value greater than 1 provides more verbose information."),
23926 NULL,
23927 NULL,
23928 &setdebuglist, &showdebuglist);
437afbb8 23929
cd4fb1b2
SM
23930 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23931Set cross-checking of \"physname\" code against demangler."), _("\
23932Show cross-checking of \"physname\" code against demangler."), _("\
23933When enabled, GDB's internal \"physname\" code is checked against\n\
23934the demangler."),
23935 NULL, show_check_physname,
23936 &setdebuglist, &showdebuglist);
900e11f9 23937
e615022a
DE
23938 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23939 no_class, &use_deprecated_index_sections, _("\
23940Set whether to use deprecated gdb_index sections."), _("\
23941Show whether to use deprecated gdb_index sections."), _("\
23942When enabled, deprecated .gdb_index sections are used anyway.\n\
23943Normally they are ignored either because of a missing feature or\n\
23944performance issue.\n\
23945Warning: This option must be enabled before gdb reads the file."),
23946 NULL,
23947 NULL,
23948 &setlist, &showlist);
23949
f1e6e072
TT
23950 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23951 &dwarf2_locexpr_funcs);
23952 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23953 &dwarf2_loclist_funcs);
23954
23955 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23956 &dwarf2_block_frame_base_locexpr_funcs);
23957 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23958 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23959
23960#if GDB_SELF_TEST
23961 selftests::register_test ("dw2_expand_symtabs_matching",
23962 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23963 selftests::register_test ("dwarf2_find_containing_comp_unit",
23964 selftests::find_containing_comp_unit::run_test);
c62446b1 23965#endif
6502dd73 23966}
This page took 5.521144 seconds and 4 git commands to generate.