Prefer existing data when evaluating DWARF expression
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
2ddeaf8a
TT
1085/* When reading a variant or variant part, we track a bit more
1086 information about the field, and store it in an object of this
1087 type. */
1088
1089struct variant_field
1090{
1091 /* If we see a DW_TAG_variant, then this will be the discriminant
1092 value. */
1093 ULONGEST discriminant_value;
1094 /* If we see a DW_TAG_variant, then this will be set if this is the
1095 default branch. */
1096 bool default_branch;
1097 /* While reading a DW_TAG_variant_part, this will be set if this
1098 field is the discriminant. */
1099 bool is_discriminant;
1100};
1101
52059ffd
TT
1102struct nextfield
1103{
be2daae6
TT
1104 int accessibility = 0;
1105 int virtuality = 0;
2ddeaf8a 1106 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1107 struct variant_field variant {};
1108 struct field field {};
52059ffd
TT
1109};
1110
1111struct fnfieldlist
1112{
be2daae6
TT
1113 const char *name = nullptr;
1114 std::vector<struct fn_field> fnfields;
52059ffd
TT
1115};
1116
c906108c
SS
1117/* The routines that read and process dies for a C struct or C++ class
1118 pass lists of data member fields and lists of member function fields
1119 in an instance of a field_info structure, as defined below. */
1120struct field_info
c5aa993b 1121 {
0963b4bd 1122 /* List of data member and baseclasses fields. */
be2daae6
TT
1123 std::vector<struct nextfield> fields;
1124 std::vector<struct nextfield> baseclasses;
c906108c 1125
85102364 1126 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1127 int non_public_fields = 0;
c906108c 1128
c5aa993b
JM
1129 /* Member function fieldlist array, contains name of possibly overloaded
1130 member function, number of overloaded member functions and a pointer
1131 to the head of the member function field chain. */
be2daae6 1132 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1133
1134 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1135 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1136 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1137
1138 /* Nested types defined by this class and the number of elements in this
1139 list. */
be2daae6 1140 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1141
1142 /* Return the total number of fields (including baseclasses). */
1143 int nfields () const
1144 {
1145 return fields.size () + baseclasses.size ();
1146 }
c5aa993b 1147 };
c906108c 1148
ae038cb0
DJ
1149/* Loaded secondary compilation units are kept in memory until they
1150 have not been referenced for the processing of this many
1151 compilation units. Set this to zero to disable caching. Cache
1152 sizes of up to at least twenty will improve startup time for
1153 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1154static int dwarf_max_cache_age = 5;
920d2a44 1155static void
b4f54984
DE
1156show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1157 struct cmd_list_element *c, const char *value)
920d2a44 1158{
3e43a32a 1159 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1160 "DWARF compilation units is %s.\n"),
920d2a44
AC
1161 value);
1162}
4390d890 1163\f
c906108c
SS
1164/* local function prototypes */
1165
918dd910
JK
1166static void dwarf2_find_base_address (struct die_info *die,
1167 struct dwarf2_cu *cu);
1168
891813be 1169static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1170 (struct dwarf2_per_cu_data *per_cu, const char *name);
1171
f1902523
JK
1172static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1173 const gdb_byte *info_ptr,
3e225074 1174 struct die_info *type_unit_die);
f1902523 1175
ed2dc618
SM
1176static void dwarf2_build_psymtabs_hard
1177 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1178
72bf9492
DJ
1179static void scan_partial_symbols (struct partial_die_info *,
1180 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1181 int, struct dwarf2_cu *);
c906108c 1182
72bf9492
DJ
1183static void add_partial_symbol (struct partial_die_info *,
1184 struct dwarf2_cu *);
63d06c5c 1185
72bf9492
DJ
1186static void add_partial_namespace (struct partial_die_info *pdi,
1187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1188 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1189
5d7cb8df 1190static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1191 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1192 struct dwarf2_cu *cu);
1193
72bf9492
DJ
1194static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1195 struct dwarf2_cu *cu);
91c24f0a 1196
bc30ff58
JB
1197static void add_partial_subprogram (struct partial_die_info *pdi,
1198 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1199 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1200
d521ce57 1201static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1202
dee91e82 1203static struct partial_die_info *load_partial_dies
d521ce57 1204 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1205
fb816e8b
TV
1206/* A pair of partial_die_info and compilation unit. */
1207struct cu_partial_die_info
1208{
1209 /* The compilation unit of the partial_die_info. */
1210 struct dwarf2_cu *cu;
1211 /* A partial_die_info. */
1212 struct partial_die_info *pdi;
122cf0f2
AB
1213
1214 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1215 : cu (cu),
1216 pdi (pdi)
405feb71 1217 { /* Nothing. */ }
122cf0f2
AB
1218
1219private:
1220 cu_partial_die_info () = delete;
fb816e8b
TV
1221};
1222
122cf0f2
AB
1223static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1224 struct dwarf2_cu *);
72bf9492 1225
d521ce57
TT
1226static const gdb_byte *read_attribute (const struct die_reader_specs *,
1227 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1228 const gdb_byte *, bool *need_reprocess);
1229
1230static void read_attribute_reprocess (const struct die_reader_specs *reader,
1231 struct attribute *attr);
1232
1233static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1234
ed2dc618
SM
1235static sect_offset read_abbrev_offset
1236 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1237 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1238
ed2dc618
SM
1239static const char *read_indirect_string
1240 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1241 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1242
ed2dc618 1243static const char *read_indirect_string_at_offset
4f44ae6c 1244 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1245
d521ce57
TT
1246static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1247 const gdb_byte *,
3019eac3
DE
1248 unsigned int *);
1249
18a8505e
AT
1250static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1251 ULONGEST str_index);
1252
1253static const char *read_stub_str_index (struct dwarf2_cu *cu,
1254 ULONGEST str_index);
3019eac3 1255
e142c38c 1256static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1257
e142c38c
DJ
1258static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1259 struct dwarf2_cu *);
c906108c 1260
7d45c7c3
KB
1261static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1262 struct dwarf2_cu *cu);
1263
a084a2a6
AT
1264static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1265
05cf31d1
JB
1266static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1267 struct dwarf2_cu *cu);
1268
e142c38c 1269static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1270
e142c38c 1271static struct die_info *die_specification (struct die_info *die,
f2f0e013 1272 struct dwarf2_cu **);
63d06c5c 1273
9c541725 1274static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1275 struct dwarf2_cu *cu);
debd256d 1276
f3f5162e 1277static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1278 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1279 CORE_ADDR, int decode_mapping);
c906108c 1280
804d2729
TT
1281static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1282 const char *);
c906108c 1283
a14ed312 1284static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1285 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1286
ff39bb5e 1287static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1288 struct dwarf2_cu *);
c906108c 1289
ff39bb5e 1290static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1291 struct type *type,
1292 const char *name,
1293 struct obstack *obstack,
12df843f 1294 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1295 const gdb_byte **bytes,
98bfdba5 1296 struct dwarf2_locexpr_baton **baton);
2df3850c 1297
e7c27a73 1298static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1299
b4ba55a1
JB
1300static int need_gnat_info (struct dwarf2_cu *);
1301
3e43a32a
MS
1302static struct type *die_descriptive_type (struct die_info *,
1303 struct dwarf2_cu *);
b4ba55a1
JB
1304
1305static void set_descriptive_type (struct type *, struct die_info *,
1306 struct dwarf2_cu *);
1307
e7c27a73
DJ
1308static struct type *die_containing_type (struct die_info *,
1309 struct dwarf2_cu *);
c906108c 1310
ff39bb5e 1311static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1312 struct dwarf2_cu *);
c906108c 1313
f792889a 1314static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1315
673bfd45
DE
1316static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1317
0d5cff50 1318static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1319
6e70227d 1320static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1321 const char *suffix, int physname,
1322 struct dwarf2_cu *cu);
63d06c5c 1323
e7c27a73 1324static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1325
348e048f
DE
1326static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1327
e7c27a73 1328static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1329
e7c27a73 1330static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1331
96408a79
SA
1332static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1333
71a3c369
TT
1334static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1335
ff013f42 1336static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1337 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1338
41144253 1339/* Return the .debug_loclists section to use for cu. */
1340static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1341
3a2b436a 1342/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1343 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1344enum pc_bounds_kind
1345{
e385593e 1346 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1347 PC_BOUNDS_NOT_PRESENT,
1348
e385593e
JK
1349 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1350 were present but they do not form a valid range of PC addresses. */
1351 PC_BOUNDS_INVALID,
1352
3a2b436a
JK
1353 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1354 PC_BOUNDS_RANGES,
1355
1356 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1357 PC_BOUNDS_HIGH_LOW,
1358};
1359
1360static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1361 CORE_ADDR *, CORE_ADDR *,
1362 struct dwarf2_cu *,
891813be 1363 dwarf2_psymtab *);
c906108c 1364
fae299cd
DC
1365static void get_scope_pc_bounds (struct die_info *,
1366 CORE_ADDR *, CORE_ADDR *,
1367 struct dwarf2_cu *);
1368
801e3a5b
JB
1369static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1370 CORE_ADDR, struct dwarf2_cu *);
1371
a14ed312 1372static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1373 struct dwarf2_cu *);
c906108c 1374
a14ed312 1375static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1376 struct type *, struct dwarf2_cu *);
c906108c 1377
a14ed312 1378static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1379 struct die_info *, struct type *,
e7c27a73 1380 struct dwarf2_cu *);
c906108c 1381
a14ed312 1382static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1383 struct type *,
1384 struct dwarf2_cu *);
c906108c 1385
134d01f1 1386static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1387
e7c27a73 1388static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1389
e7c27a73 1390static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1391
5d7cb8df
JK
1392static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1393
804d2729 1394static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1395
27aa8d6a
SW
1396static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1397
74921315
KS
1398static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1399
f55ee35c
JK
1400static struct type *read_module_type (struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
38d518c9 1403static const char *namespace_name (struct die_info *die,
e142c38c 1404 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1405
134d01f1 1406static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1407
e7c27a73 1408static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1409
6e70227d 1410static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1411 struct dwarf2_cu *);
1412
bf6af496 1413static struct die_info *read_die_and_siblings_1
d521ce57 1414 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1415 struct die_info *);
639d11d3 1416
dee91e82 1417static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1418 const gdb_byte *info_ptr,
1419 const gdb_byte **new_info_ptr,
639d11d3
DC
1420 struct die_info *parent);
1421
d521ce57
TT
1422static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1423 struct die_info **, const gdb_byte *,
3e225074 1424 int);
3019eac3 1425
d521ce57 1426static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1427 struct die_info **, const gdb_byte *);
93311388 1428
e7c27a73 1429static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1430
15d034d0 1431static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1432 struct objfile *);
71c25dea 1433
15d034d0 1434static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1435
15d034d0 1436static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1437 struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
ca69b9e6
DE
1440static const char *dwarf2_physname (const char *name, struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
e142c38c 1443static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1444 struct dwarf2_cu **);
9219021c 1445
d97bc12b
DE
1446static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1447
1448static void dump_die_for_error (struct die_info *);
1449
1450static void dump_die_1 (struct ui_file *, int level, int max_level,
1451 struct die_info *);
c906108c 1452
d97bc12b 1453/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1454
51545339 1455static void store_in_ref_table (struct die_info *,
10b3939b 1456 struct dwarf2_cu *);
c906108c 1457
348e048f 1458static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1459 const struct attribute *,
348e048f
DE
1460 struct dwarf2_cu **);
1461
10b3939b 1462static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1463 const struct attribute *,
f2f0e013 1464 struct dwarf2_cu **);
c906108c 1465
348e048f 1466static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1467 const struct attribute *,
348e048f
DE
1468 struct dwarf2_cu **);
1469
ac9ec31b
DE
1470static struct type *get_signatured_type (struct die_info *, ULONGEST,
1471 struct dwarf2_cu *);
1472
1473static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1474 const struct attribute *,
ac9ec31b
DE
1475 struct dwarf2_cu *);
1476
e5fe5e75 1477static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1478
52dc124a 1479static void read_signatured_type (struct signatured_type *);
348e048f 1480
63e43d3a
PMR
1481static int attr_to_dynamic_prop (const struct attribute *attr,
1482 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1483 struct dynamic_prop *prop, struct type *type);
63e43d3a 1484
c906108c
SS
1485/* memory allocation interface */
1486
7b5a2f43 1487static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1488
b60c80d6 1489static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1490
43f3e411 1491static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1492
8cf6f0b1
TT
1493static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1494 struct dwarf2_loclist_baton *baton,
ff39bb5e 1495 const struct attribute *attr);
8cf6f0b1 1496
ff39bb5e 1497static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1498 struct symbol *sym,
f1e6e072
TT
1499 struct dwarf2_cu *cu,
1500 int is_block);
4c2df51b 1501
d521ce57
TT
1502static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1503 const gdb_byte *info_ptr,
1504 struct abbrev_info *abbrev);
4bb7a0a7 1505
72bf9492
DJ
1506static hashval_t partial_die_hash (const void *item);
1507
1508static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1509
ae038cb0 1510static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1511 (sect_offset sect_off, unsigned int offset_in_dwz,
1512 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1513
9816fde3 1514static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1515 struct die_info *comp_unit_die,
1516 enum language pretend_language);
93311388 1517
ed2dc618 1518static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1519
dee91e82 1520static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1521
f792889a
DJ
1522static struct type *set_die_type (struct die_info *, struct type *,
1523 struct dwarf2_cu *);
1c379e20 1524
ed2dc618 1525static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1526
ed2dc618 1527static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1528
58f0c718 1529static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1530 enum language);
10b3939b 1531
95554aad
TT
1532static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1533 enum language);
10b3939b 1534
f4dc4d17
DE
1535static void process_full_type_unit (struct dwarf2_per_cu_data *,
1536 enum language);
1537
10b3939b
DJ
1538static void dwarf2_add_dependence (struct dwarf2_cu *,
1539 struct dwarf2_per_cu_data *);
1540
ae038cb0
DJ
1541static void dwarf2_mark (struct dwarf2_cu *);
1542
1543static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1544
b64f50a1 1545static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1546 struct dwarf2_per_cu_data *);
673bfd45 1547
f792889a 1548static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1549
95554aad
TT
1550static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1551 enum language pretend_language);
1552
ed2dc618 1553static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1554
b303c6f6
AB
1555/* Class, the destructor of which frees all allocated queue entries. This
1556 will only have work to do if an error was thrown while processing the
1557 dwarf. If no error was thrown then the queue entries should have all
1558 been processed, and freed, as we went along. */
1559
1560class dwarf2_queue_guard
1561{
1562public:
39856def
TT
1563 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1564 : m_per_objfile (per_objfile)
1565 {
1566 }
b303c6f6
AB
1567
1568 /* Free any entries remaining on the queue. There should only be
1569 entries left if we hit an error while processing the dwarf. */
1570 ~dwarf2_queue_guard ()
1571 {
39856def
TT
1572 /* Ensure that no memory is allocated by the queue. */
1573 std::queue<dwarf2_queue_item> empty;
1574 std::swap (m_per_objfile->queue, empty);
1575 }
b303c6f6 1576
39856def 1577 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1578
39856def
TT
1579private:
1580 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1581};
1582
39856def
TT
1583dwarf2_queue_item::~dwarf2_queue_item ()
1584{
1585 /* Anything still marked queued is likely to be in an
1586 inconsistent state, so discard it. */
1587 if (per_cu->queued)
1588 {
1589 if (per_cu->cu != NULL)
1590 free_one_cached_comp_unit (per_cu);
1591 per_cu->queued = 0;
1592 }
1593}
1594
d721ba37
PA
1595/* The return type of find_file_and_directory. Note, the enclosed
1596 string pointers are only valid while this object is valid. */
1597
1598struct file_and_directory
1599{
1600 /* The filename. This is never NULL. */
1601 const char *name;
1602
1603 /* The compilation directory. NULL if not known. If we needed to
1604 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1605 points directly to the DW_AT_comp_dir string attribute owned by
1606 the obstack that owns the DIE. */
1607 const char *comp_dir;
1608
1609 /* If we needed to build a new string for comp_dir, this is what
1610 owns the storage. */
1611 std::string comp_dir_storage;
1612};
1613
1614static file_and_directory find_file_and_directory (struct die_info *die,
1615 struct dwarf2_cu *cu);
9291a0cd 1616
298e9637 1617static htab_up allocate_signatured_type_table ();
1fd400ff 1618
298e9637 1619static htab_up allocate_dwo_unit_table ();
3019eac3 1620
57d63ce2 1621static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1622 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1623 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1624 ULONGEST signature, int is_debug_types);
a2ce51a0 1625
ed2dc618
SM
1626static struct dwp_file *get_dwp_file
1627 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1628
3019eac3 1629static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1630 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1631
1632static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1633 (struct signatured_type *, const char *, const char *);
3019eac3 1634
89e63ee4
DE
1635static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1636
263db9a1
TT
1637/* A unique pointer to a dwo_file. */
1638
51ac9db5 1639typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1640
ed2dc618 1641static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1642
1b80a9fa 1643static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1644
1645static void free_line_header_voidp (void *arg);
4390d890
DE
1646\f
1647/* Various complaints about symbol reading that don't abort the process. */
1648
4390d890
DE
1649static void
1650dwarf2_debug_line_missing_file_complaint (void)
1651{
b98664d3 1652 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1653}
1654
1655static void
1656dwarf2_debug_line_missing_end_sequence_complaint (void)
1657{
b98664d3 1658 complaint (_(".debug_line section has line "
4390d890
DE
1659 "program sequence without an end"));
1660}
1661
1662static void
1663dwarf2_complex_location_expr_complaint (void)
1664{
b98664d3 1665 complaint (_("location expression too complex"));
4390d890
DE
1666}
1667
1668static void
1669dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1670 int arg3)
1671{
b98664d3 1672 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1673 arg1, arg2, arg3);
1674}
1675
4390d890
DE
1676static void
1677dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1678{
b98664d3 1679 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1680 arg1, arg2);
1681}
527f3840
JK
1682
1683/* Hash function for line_header_hash. */
1684
1685static hashval_t
1686line_header_hash (const struct line_header *ofs)
1687{
9c541725 1688 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1689}
1690
1691/* Hash function for htab_create_alloc_ex for line_header_hash. */
1692
1693static hashval_t
1694line_header_hash_voidp (const void *item)
1695{
9a3c8263 1696 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1697
1698 return line_header_hash (ofs);
1699}
1700
1701/* Equality function for line_header_hash. */
1702
1703static int
1704line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1705{
9a3c8263
SM
1706 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1707 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1708
9c541725 1709 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1710 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1711}
1712
4390d890 1713\f
9291a0cd 1714
330cdd98
PA
1715/* See declaration. */
1716
1717dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1718 const dwarf2_debug_sections *names,
1719 bool can_copy_)
1720 : objfile (objfile_),
1721 can_copy (can_copy_)
330cdd98
PA
1722{
1723 if (names == NULL)
1724 names = &dwarf2_elf_names;
1725
1726 bfd *obfd = objfile->obfd;
1727
1728 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1729 locate_sections (obfd, sec, *names);
1730}
1731
1732dwarf2_per_objfile::~dwarf2_per_objfile ()
1733{
1734 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1735 free_cached_comp_units ();
1736
b76e467d 1737 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1738 per_cu->imported_symtabs_free ();
fc8e7e75 1739
b2bdb8cf 1740 for (signatured_type *sig_type : all_type_units)
ae640021 1741 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1742
330cdd98
PA
1743 /* Everything else should be on the objfile obstack. */
1744}
1745
1746/* See declaration. */
1747
1748void
1749dwarf2_per_objfile::free_cached_comp_units ()
1750{
1751 dwarf2_per_cu_data *per_cu = read_in_chain;
1752 dwarf2_per_cu_data **last_chain = &read_in_chain;
1753 while (per_cu != NULL)
1754 {
1755 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1756
fcd3b13d 1757 delete per_cu->cu;
330cdd98
PA
1758 *last_chain = next_cu;
1759 per_cu = next_cu;
1760 }
1761}
1762
11ed8cad
TT
1763/* A helper class that calls free_cached_comp_units on
1764 destruction. */
1765
1766class free_cached_comp_units
1767{
1768public:
1769
1770 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1771 : m_per_objfile (per_objfile)
1772 {
1773 }
1774
1775 ~free_cached_comp_units ()
1776 {
1777 m_per_objfile->free_cached_comp_units ();
1778 }
1779
1780 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1781
1782private:
1783
1784 dwarf2_per_objfile *m_per_objfile;
1785};
1786
c906108c 1787/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1788 information and return true if we have enough to do something.
1789 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1790 ELF names are used. CAN_COPY is true for formats where symbol
1791 interposition is possible and so symbol values must follow copy
1792 relocation rules. */
c906108c
SS
1793
1794int
251d32d9 1795dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1796 const struct dwarf2_debug_sections *names,
1797 bool can_copy)
c906108c 1798{
97cbe998
SDJ
1799 if (objfile->flags & OBJF_READNEVER)
1800 return 0;
1801
ed2dc618
SM
1802 struct dwarf2_per_objfile *dwarf2_per_objfile
1803 = get_dwarf2_per_objfile (objfile);
1804
1805 if (dwarf2_per_objfile == NULL)
5bfd760d 1806 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1807 names,
1808 can_copy);
5bfd760d 1809
73869dc2 1810 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1811 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1812 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1813 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1814}
1815
251d32d9
TG
1816/* When loading sections, we look either for uncompressed section or for
1817 compressed section names. */
233a11ab
CS
1818
1819static int
251d32d9
TG
1820section_is_p (const char *section_name,
1821 const struct dwarf2_section_names *names)
233a11ab 1822{
251d32d9
TG
1823 if (names->normal != NULL
1824 && strcmp (section_name, names->normal) == 0)
1825 return 1;
1826 if (names->compressed != NULL
1827 && strcmp (section_name, names->compressed) == 0)
1828 return 1;
1829 return 0;
233a11ab
CS
1830}
1831
330cdd98 1832/* See declaration. */
c906108c 1833
330cdd98
PA
1834void
1835dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1836 const dwarf2_debug_sections &names)
c906108c 1837{
fd361982 1838 flagword aflag = bfd_section_flags (sectp);
251d32d9 1839
dc7650b8
JK
1840 if ((aflag & SEC_HAS_CONTENTS) == 0)
1841 {
1842 }
950b7495
KS
1843 else if (elf_section_data (sectp)->this_hdr.sh_size
1844 > bfd_get_file_size (abfd))
1845 {
1846 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1847 warning (_("Discarding section %s which has a section size (%s"
1848 ") larger than the file size [in module %s]"),
1849 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1850 bfd_get_filename (abfd));
1851 }
330cdd98 1852 else if (section_is_p (sectp->name, &names.info))
c906108c 1853 {
330cdd98 1854 this->info.s.section = sectp;
fd361982 1855 this->info.size = bfd_section_size (sectp);
c906108c 1856 }
330cdd98 1857 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1858 {
330cdd98 1859 this->abbrev.s.section = sectp;
fd361982 1860 this->abbrev.size = bfd_section_size (sectp);
c906108c 1861 }
330cdd98 1862 else if (section_is_p (sectp->name, &names.line))
c906108c 1863 {
330cdd98 1864 this->line.s.section = sectp;
fd361982 1865 this->line.size = bfd_section_size (sectp);
c906108c 1866 }
330cdd98 1867 else if (section_is_p (sectp->name, &names.loc))
c906108c 1868 {
330cdd98 1869 this->loc.s.section = sectp;
fd361982 1870 this->loc.size = bfd_section_size (sectp);
c906108c 1871 }
330cdd98 1872 else if (section_is_p (sectp->name, &names.loclists))
43988095 1873 {
330cdd98 1874 this->loclists.s.section = sectp;
fd361982 1875 this->loclists.size = bfd_section_size (sectp);
43988095 1876 }
330cdd98 1877 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1878 {
330cdd98 1879 this->macinfo.s.section = sectp;
fd361982 1880 this->macinfo.size = bfd_section_size (sectp);
c906108c 1881 }
330cdd98 1882 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1883 {
330cdd98 1884 this->macro.s.section = sectp;
fd361982 1885 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1886 }
330cdd98 1887 else if (section_is_p (sectp->name, &names.str))
c906108c 1888 {
330cdd98 1889 this->str.s.section = sectp;
fd361982 1890 this->str.size = bfd_section_size (sectp);
c906108c 1891 }
18a8505e
AT
1892 else if (section_is_p (sectp->name, &names.str_offsets))
1893 {
1894 this->str_offsets.s.section = sectp;
1895 this->str_offsets.size = bfd_section_size (sectp);
1896 }
330cdd98 1897 else if (section_is_p (sectp->name, &names.line_str))
43988095 1898 {
330cdd98 1899 this->line_str.s.section = sectp;
fd361982 1900 this->line_str.size = bfd_section_size (sectp);
43988095 1901 }
330cdd98 1902 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1903 {
330cdd98 1904 this->addr.s.section = sectp;
fd361982 1905 this->addr.size = bfd_section_size (sectp);
3019eac3 1906 }
330cdd98 1907 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1908 {
330cdd98 1909 this->frame.s.section = sectp;
fd361982 1910 this->frame.size = bfd_section_size (sectp);
b6af0555 1911 }
330cdd98 1912 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1913 {
330cdd98 1914 this->eh_frame.s.section = sectp;
fd361982 1915 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1916 }
330cdd98 1917 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1918 {
330cdd98 1919 this->ranges.s.section = sectp;
fd361982 1920 this->ranges.size = bfd_section_size (sectp);
af34e669 1921 }
330cdd98 1922 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1923 {
330cdd98 1924 this->rnglists.s.section = sectp;
fd361982 1925 this->rnglists.size = bfd_section_size (sectp);
43988095 1926 }
330cdd98 1927 else if (section_is_p (sectp->name, &names.types))
348e048f 1928 {
8b70b953
TT
1929 struct dwarf2_section_info type_section;
1930
1931 memset (&type_section, 0, sizeof (type_section));
049412e3 1932 type_section.s.section = sectp;
fd361982 1933 type_section.size = bfd_section_size (sectp);
8b70b953 1934
fd5866f6 1935 this->types.push_back (type_section);
348e048f 1936 }
330cdd98 1937 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1938 {
330cdd98 1939 this->gdb_index.s.section = sectp;
fd361982 1940 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1941 }
927aa2e7
JK
1942 else if (section_is_p (sectp->name, &names.debug_names))
1943 {
1944 this->debug_names.s.section = sectp;
fd361982 1945 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1946 }
1947 else if (section_is_p (sectp->name, &names.debug_aranges))
1948 {
1949 this->debug_aranges.s.section = sectp;
fd361982 1950 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1951 }
dce234bc 1952
fd361982
AM
1953 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1954 && bfd_section_vma (sectp) == 0)
330cdd98 1955 this->has_section_at_zero = true;
c906108c
SS
1956}
1957
dce234bc 1958/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1959 SECTION_NAME. */
af34e669 1960
dce234bc 1961void
3017a003
TG
1962dwarf2_get_section_info (struct objfile *objfile,
1963 enum dwarf2_section_enum sect,
d521ce57 1964 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1965 bfd_size_type *sizep)
1966{
5bfd760d 1967 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1968 struct dwarf2_section_info *info;
a3b2a86b
TT
1969
1970 /* We may see an objfile without any DWARF, in which case we just
1971 return nothing. */
1972 if (data == NULL)
1973 {
1974 *sectp = NULL;
1975 *bufp = NULL;
1976 *sizep = 0;
1977 return;
1978 }
3017a003
TG
1979 switch (sect)
1980 {
1981 case DWARF2_DEBUG_FRAME:
1982 info = &data->frame;
1983 break;
1984 case DWARF2_EH_FRAME:
1985 info = &data->eh_frame;
1986 break;
1987 default:
1988 gdb_assert_not_reached ("unexpected section");
1989 }
dce234bc 1990
96b79293 1991 info->read (objfile);
dce234bc 1992
96b79293 1993 *sectp = info->get_bfd_section ();
dce234bc
PP
1994 *bufp = info->buffer;
1995 *sizep = info->size;
1996}
1997
36586728
TT
1998/* A helper function to find the sections for a .dwz file. */
1999
2000static void
2001locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2002{
9a3c8263 2003 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2004
2005 /* Note that we only support the standard ELF names, because .dwz
2006 is ELF-only (at the time of writing). */
2007 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2008 {
049412e3 2009 dwz_file->abbrev.s.section = sectp;
fd361982 2010 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2011 }
2012 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2013 {
049412e3 2014 dwz_file->info.s.section = sectp;
fd361982 2015 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2016 }
2017 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2018 {
049412e3 2019 dwz_file->str.s.section = sectp;
fd361982 2020 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2021 }
2022 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2023 {
049412e3 2024 dwz_file->line.s.section = sectp;
fd361982 2025 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2026 }
2027 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2028 {
049412e3 2029 dwz_file->macro.s.section = sectp;
fd361982 2030 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2031 }
2ec9a5e0
TT
2032 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2033 {
049412e3 2034 dwz_file->gdb_index.s.section = sectp;
fd361982 2035 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2036 }
927aa2e7
JK
2037 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2038 {
2039 dwz_file->debug_names.s.section = sectp;
fd361982 2040 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2041 }
36586728
TT
2042}
2043
c4973306 2044/* See dwarf2read.h. */
36586728 2045
c4973306 2046struct dwz_file *
ed2dc618 2047dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2048{
36586728 2049 const char *filename;
acd13123 2050 bfd_size_type buildid_len_arg;
dc294be5
TT
2051 size_t buildid_len;
2052 bfd_byte *buildid;
36586728
TT
2053
2054 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2055 return dwarf2_per_objfile->dwz_file.get ();
36586728 2056
4db1a1dc 2057 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2058 gdb::unique_xmalloc_ptr<char> data
2059 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2060 &buildid_len_arg, &buildid));
4db1a1dc
TT
2061 if (data == NULL)
2062 {
2063 if (bfd_get_error () == bfd_error_no_error)
2064 return NULL;
2065 error (_("could not read '.gnu_debugaltlink' section: %s"),
2066 bfd_errmsg (bfd_get_error ()));
2067 }
791afaa2
TT
2068
2069 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2070
acd13123
TT
2071 buildid_len = (size_t) buildid_len_arg;
2072
791afaa2 2073 filename = data.get ();
d721ba37
PA
2074
2075 std::string abs_storage;
36586728
TT
2076 if (!IS_ABSOLUTE_PATH (filename))
2077 {
14278e1f
TT
2078 gdb::unique_xmalloc_ptr<char> abs
2079 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2080
14278e1f 2081 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2082 filename = abs_storage.c_str ();
36586728
TT
2083 }
2084
dc294be5
TT
2085 /* First try the file name given in the section. If that doesn't
2086 work, try to use the build-id instead. */
192b62ce 2087 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2088 if (dwz_bfd != NULL)
36586728 2089 {
192b62ce 2090 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2091 dwz_bfd.reset (nullptr);
36586728
TT
2092 }
2093
dc294be5
TT
2094 if (dwz_bfd == NULL)
2095 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2096
0d79cdc4
AM
2097 if (dwz_bfd == nullptr)
2098 {
2099 gdb::unique_xmalloc_ptr<char> alt_filename;
2100 const char *origname = dwarf2_per_objfile->objfile->original_name;
2101
2102 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2103 buildid_len,
2104 origname,
2105 &alt_filename));
2106
2107 if (fd.get () >= 0)
2108 {
2109 /* File successfully retrieved from server. */
2110 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2111
2112 if (dwz_bfd == nullptr)
2113 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2114 alt_filename.get ());
2115 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2116 dwz_bfd.reset (nullptr);
2117 }
2118 }
2119
dc294be5
TT
2120 if (dwz_bfd == NULL)
2121 error (_("could not find '.gnu_debugaltlink' file for %s"),
2122 objfile_name (dwarf2_per_objfile->objfile));
2123
7ff8cb8c
TT
2124 std::unique_ptr<struct dwz_file> result
2125 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2126
7ff8cb8c
TT
2127 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2128 result.get ());
36586728 2129
7ff8cb8c
TT
2130 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2131 result->dwz_bfd.get ());
2132 dwarf2_per_objfile->dwz_file = std::move (result);
2133 return dwarf2_per_objfile->dwz_file.get ();
36586728 2134}
9291a0cd 2135\f
7b9f3c50
DE
2136/* DWARF quick_symbols_functions support. */
2137
2138/* TUs can share .debug_line entries, and there can be a lot more TUs than
2139 unique line tables, so we maintain a separate table of all .debug_line
2140 derived entries to support the sharing.
2141 All the quick functions need is the list of file names. We discard the
2142 line_header when we're done and don't need to record it here. */
2143struct quick_file_names
2144{
094b34ac
DE
2145 /* The data used to construct the hash key. */
2146 struct stmt_list_hash hash;
7b9f3c50
DE
2147
2148 /* The number of entries in file_names, real_names. */
2149 unsigned int num_file_names;
2150
2151 /* The file names from the line table, after being run through
2152 file_full_name. */
2153 const char **file_names;
2154
2155 /* The file names from the line table after being run through
2156 gdb_realpath. These are computed lazily. */
2157 const char **real_names;
2158};
2159
2160/* When using the index (and thus not using psymtabs), each CU has an
2161 object of this type. This is used to hold information needed by
2162 the various "quick" methods. */
2163struct dwarf2_per_cu_quick_data
2164{
2165 /* The file table. This can be NULL if there was no file table
2166 or it's currently not read in.
2167 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2168 struct quick_file_names *file_names;
2169
2170 /* The corresponding symbol table. This is NULL if symbols for this
2171 CU have not yet been read. */
43f3e411 2172 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2173
2174 /* A temporary mark bit used when iterating over all CUs in
2175 expand_symtabs_matching. */
2176 unsigned int mark : 1;
2177
2178 /* True if we've tried to read the file table and found there isn't one.
2179 There will be no point in trying to read it again next time. */
2180 unsigned int no_file_data : 1;
2181};
2182
094b34ac
DE
2183/* Utility hash function for a stmt_list_hash. */
2184
2185static hashval_t
2186hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2187{
2188 hashval_t v = 0;
2189
2190 if (stmt_list_hash->dwo_unit != NULL)
2191 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2192 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2193 return v;
2194}
2195
2196/* Utility equality function for a stmt_list_hash. */
2197
2198static int
2199eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2200 const struct stmt_list_hash *rhs)
2201{
2202 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2203 return 0;
2204 if (lhs->dwo_unit != NULL
2205 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2206 return 0;
2207
9c541725 2208 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2209}
2210
7b9f3c50
DE
2211/* Hash function for a quick_file_names. */
2212
2213static hashval_t
2214hash_file_name_entry (const void *e)
2215{
9a3c8263
SM
2216 const struct quick_file_names *file_data
2217 = (const struct quick_file_names *) e;
7b9f3c50 2218
094b34ac 2219 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2220}
2221
2222/* Equality function for a quick_file_names. */
2223
2224static int
2225eq_file_name_entry (const void *a, const void *b)
2226{
9a3c8263
SM
2227 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2228 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2229
094b34ac 2230 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2231}
2232
2233/* Delete function for a quick_file_names. */
2234
2235static void
2236delete_file_name_entry (void *e)
2237{
9a3c8263 2238 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2239 int i;
2240
2241 for (i = 0; i < file_data->num_file_names; ++i)
2242 {
2243 xfree ((void*) file_data->file_names[i]);
2244 if (file_data->real_names)
2245 xfree ((void*) file_data->real_names[i]);
2246 }
2247
2248 /* The space for the struct itself lives on objfile_obstack,
2249 so we don't free it here. */
2250}
2251
2252/* Create a quick_file_names hash table. */
2253
5895093f 2254static htab_up
7b9f3c50
DE
2255create_quick_file_names_table (unsigned int nr_initial_entries)
2256{
5895093f
TT
2257 return htab_up (htab_create_alloc (nr_initial_entries,
2258 hash_file_name_entry, eq_file_name_entry,
2259 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2260}
9291a0cd 2261
918dd910
JK
2262/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2263 have to be created afterwards. You should call age_cached_comp_units after
2264 processing PER_CU->CU. dw2_setup must have been already called. */
2265
2266static void
58f0c718 2267load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2268{
3019eac3 2269 if (per_cu->is_debug_types)
e5fe5e75 2270 load_full_type_unit (per_cu);
918dd910 2271 else
58f0c718 2272 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2273
cc12ce38
DE
2274 if (per_cu->cu == NULL)
2275 return; /* Dummy CU. */
2dc860c0
DE
2276
2277 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2278}
2279
a0f42c21 2280/* Read in the symbols for PER_CU. */
2fdf6df6 2281
9291a0cd 2282static void
58f0c718 2283dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2284{
ed2dc618 2285 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2286
f4dc4d17
DE
2287 /* Skip type_unit_groups, reading the type units they contain
2288 is handled elsewhere. */
197400e8 2289 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2290 return;
2291
b303c6f6
AB
2292 /* The destructor of dwarf2_queue_guard frees any entries left on
2293 the queue. After this point we're guaranteed to leave this function
2294 with the dwarf queue empty. */
39856def 2295 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2296
95554aad 2297 if (dwarf2_per_objfile->using_index
43f3e411 2298 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2299 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2300 {
2301 queue_comp_unit (per_cu, language_minimal);
58f0c718 2302 load_cu (per_cu, skip_partial);
89e63ee4
DE
2303
2304 /* If we just loaded a CU from a DWO, and we're working with an index
2305 that may badly handle TUs, load all the TUs in that DWO as well.
2306 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2307 if (!per_cu->is_debug_types
cc12ce38 2308 && per_cu->cu != NULL
89e63ee4
DE
2309 && per_cu->cu->dwo_unit != NULL
2310 && dwarf2_per_objfile->index_table != NULL
2311 && dwarf2_per_objfile->index_table->version <= 7
2312 /* DWP files aren't supported yet. */
ed2dc618 2313 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2314 queue_and_load_all_dwo_tus (per_cu);
95554aad 2315 }
9291a0cd 2316
ed2dc618 2317 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2318
2319 /* Age the cache, releasing compilation units that have not
2320 been used recently. */
ed2dc618 2321 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2322}
2323
2324/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2325 the objfile from which this CU came. Returns the resulting symbol
2326 table. */
2fdf6df6 2327
43f3e411 2328static struct compunit_symtab *
58f0c718 2329dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2330{
ed2dc618
SM
2331 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2332
95554aad 2333 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2334 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2335 {
11ed8cad 2336 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2337 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2338 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2339 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2340 }
f194fefb 2341
43f3e411 2342 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2343}
2344
ff4c9fec 2345/* See declaration. */
f4dc4d17 2346
ff4c9fec
SM
2347dwarf2_per_cu_data *
2348dwarf2_per_objfile::get_cutu (int index)
2349{
b76e467d 2350 if (index >= this->all_comp_units.size ())
ff4c9fec 2351 {
b76e467d 2352 index -= this->all_comp_units.size ();
b2bdb8cf 2353 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2354 return &this->all_type_units[index]->per_cu;
2355 }
f4dc4d17 2356
ff4c9fec
SM
2357 return this->all_comp_units[index];
2358}
f4dc4d17 2359
ff4c9fec 2360/* See declaration. */
2fdf6df6 2361
ff4c9fec
SM
2362dwarf2_per_cu_data *
2363dwarf2_per_objfile::get_cu (int index)
1fd400ff 2364{
b76e467d 2365 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2366
ff4c9fec 2367 return this->all_comp_units[index];
f4dc4d17
DE
2368}
2369
ff4c9fec 2370/* See declaration. */
f4dc4d17 2371
ff4c9fec
SM
2372signatured_type *
2373dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2374{
b2bdb8cf 2375 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2376
ff4c9fec 2377 return this->all_type_units[index];
1fd400ff
TT
2378}
2379
4b514bc8
JK
2380/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2381 objfile_obstack, and constructed with the specified field
2382 values. */
2383
2384static dwarf2_per_cu_data *
ed2dc618 2385create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2386 struct dwarf2_section_info *section,
2387 int is_dwz,
2388 sect_offset sect_off, ULONGEST length)
2389{
ed2dc618 2390 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2391 dwarf2_per_cu_data *the_cu
2392 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2393 struct dwarf2_per_cu_data);
2394 the_cu->sect_off = sect_off;
2395 the_cu->length = length;
e3b94546 2396 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2397 the_cu->section = section;
2398 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2399 struct dwarf2_per_cu_quick_data);
2400 the_cu->is_dwz = is_dwz;
2401 return the_cu;
2402}
2403
2ec9a5e0
TT
2404/* A helper for create_cus_from_index that handles a given list of
2405 CUs. */
2fdf6df6 2406
74a0d9f6 2407static void
12359b5e 2408create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2409 const gdb_byte *cu_list, offset_type n_elements,
2410 struct dwarf2_section_info *section,
b76e467d 2411 int is_dwz)
9291a0cd 2412{
12359b5e 2413 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2414 {
74a0d9f6 2415 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2416
2417 sect_offset sect_off
2418 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2419 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2420 cu_list += 2 * 8;
2421
b76e467d 2422 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2423 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2424 sect_off, length);
b76e467d 2425 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2426 }
9291a0cd
TT
2427}
2428
2ec9a5e0 2429/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2430 the CU objects for this objfile. */
2ec9a5e0 2431
74a0d9f6 2432static void
12359b5e 2433create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2434 const gdb_byte *cu_list, offset_type cu_list_elements,
2435 const gdb_byte *dwz_list, offset_type dwz_elements)
2436{
b76e467d
SM
2437 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2438 dwarf2_per_objfile->all_comp_units.reserve
2439 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2440
12359b5e 2441 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2442 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2443
2444 if (dwz_elements == 0)
74a0d9f6 2445 return;
2ec9a5e0 2446
12359b5e
SM
2447 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2448 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2449 &dwz->info, 1);
2ec9a5e0
TT
2450}
2451
1fd400ff 2452/* Create the signatured type hash table from the index. */
673bfd45 2453
74a0d9f6 2454static void
12359b5e
SM
2455create_signatured_type_table_from_index
2456 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2457 struct dwarf2_section_info *section,
2458 const gdb_byte *bytes,
2459 offset_type elements)
1fd400ff 2460{
12359b5e 2461 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2462
b2bdb8cf
SM
2463 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2464 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2465
298e9637 2466 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2467
12359b5e 2468 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2469 {
52dc124a 2470 struct signatured_type *sig_type;
9c541725 2471 ULONGEST signature;
1fd400ff 2472 void **slot;
9c541725 2473 cu_offset type_offset_in_tu;
1fd400ff 2474
74a0d9f6 2475 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2476 sect_offset sect_off
2477 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2478 type_offset_in_tu
2479 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2480 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2481 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2482 bytes += 3 * 8;
2483
52dc124a 2484 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2485 struct signatured_type);
52dc124a 2486 sig_type->signature = signature;
9c541725 2487 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2488 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2489 sig_type->per_cu.section = section;
9c541725 2490 sig_type->per_cu.sect_off = sect_off;
e3b94546 2491 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2492 sig_type->per_cu.v.quick
1fd400ff
TT
2493 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2494 struct dwarf2_per_cu_quick_data);
2495
b0b6a987 2496 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2497 *slot = sig_type;
1fd400ff 2498
b2bdb8cf 2499 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2500 }
2501
b0b6a987 2502 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2503}
2504
927aa2e7
JK
2505/* Create the signatured type hash table from .debug_names. */
2506
2507static void
2508create_signatured_type_table_from_debug_names
ed2dc618 2509 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2510 const mapped_debug_names &map,
2511 struct dwarf2_section_info *section,
2512 struct dwarf2_section_info *abbrev_section)
2513{
ed2dc618
SM
2514 struct objfile *objfile = dwarf2_per_objfile->objfile;
2515
96b79293
TT
2516 section->read (objfile);
2517 abbrev_section->read (objfile);
927aa2e7 2518
b2bdb8cf
SM
2519 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2520 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2521
298e9637 2522 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2523
2524 for (uint32_t i = 0; i < map.tu_count; ++i)
2525 {
2526 struct signatured_type *sig_type;
927aa2e7 2527 void **slot;
927aa2e7
JK
2528
2529 sect_offset sect_off
2530 = (sect_offset) (extract_unsigned_integer
2531 (map.tu_table_reordered + i * map.offset_size,
2532 map.offset_size,
2533 map.dwarf5_byte_order));
2534
2535 comp_unit_head cu_header;
ed2dc618
SM
2536 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2537 abbrev_section,
927aa2e7
JK
2538 section->buffer + to_underlying (sect_off),
2539 rcuh_kind::TYPE);
2540
2541 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2542 struct signatured_type);
2543 sig_type->signature = cu_header.signature;
2544 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2545 sig_type->per_cu.is_debug_types = 1;
2546 sig_type->per_cu.section = section;
2547 sig_type->per_cu.sect_off = sect_off;
e3b94546 2548 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2549 sig_type->per_cu.v.quick
2550 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2551 struct dwarf2_per_cu_quick_data);
2552
b0b6a987 2553 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2554 *slot = sig_type;
2555
b2bdb8cf 2556 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2557 }
2558
b0b6a987 2559 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2560}
2561
9291a0cd
TT
2562/* Read the address map data from the mapped index, and use it to
2563 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2564
9291a0cd 2565static void
ed2dc618
SM
2566create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2567 struct mapped_index *index)
9291a0cd 2568{
ed2dc618 2569 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2570 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2571 const gdb_byte *iter, *end;
9291a0cd 2572 struct addrmap *mutable_map;
9291a0cd
TT
2573 CORE_ADDR baseaddr;
2574
8268c778
PA
2575 auto_obstack temp_obstack;
2576
9291a0cd
TT
2577 mutable_map = addrmap_create_mutable (&temp_obstack);
2578
f00a2de2
PA
2579 iter = index->address_table.data ();
2580 end = iter + index->address_table.size ();
9291a0cd 2581
b3b3bada 2582 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2583
2584 while (iter < end)
2585 {
2586 ULONGEST hi, lo, cu_index;
2587 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2588 iter += 8;
2589 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2590 iter += 8;
2591 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2592 iter += 4;
f652bce2 2593
24a55014 2594 if (lo > hi)
f652bce2 2595 {
b98664d3 2596 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2597 hex_string (lo), hex_string (hi));
24a55014 2598 continue;
f652bce2 2599 }
24a55014 2600
b76e467d 2601 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2602 {
b98664d3 2603 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2604 (unsigned) cu_index);
24a55014 2605 continue;
f652bce2 2606 }
24a55014 2607
79748972
TT
2608 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2609 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2610 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2611 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2612 }
2613
d320c2b5 2614 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2615 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2616}
2617
927aa2e7
JK
2618/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2619 populate the objfile's psymtabs_addrmap. */
2620
2621static void
ed2dc618 2622create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2623 struct dwarf2_section_info *section)
2624{
ed2dc618 2625 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2626 bfd *abfd = objfile->obfd;
08feed99 2627 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2628 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2629
2630 auto_obstack temp_obstack;
2631 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2632
2633 std::unordered_map<sect_offset,
2634 dwarf2_per_cu_data *,
2635 gdb::hash_enum<sect_offset>>
2636 debug_info_offset_to_per_cu;
b76e467d 2637 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2638 {
927aa2e7
JK
2639 const auto insertpair
2640 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2641 if (!insertpair.second)
2642 {
2643 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2644 "debug_info_offset %s, ignoring .debug_aranges."),
2645 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2646 return;
2647 }
2648 }
2649
96b79293 2650 section->read (objfile);
927aa2e7
JK
2651
2652 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2653
2654 const gdb_byte *addr = section->buffer;
2655
2656 while (addr < section->buffer + section->size)
2657 {
2658 const gdb_byte *const entry_addr = addr;
2659 unsigned int bytes_read;
2660
2661 const LONGEST entry_length = read_initial_length (abfd, addr,
2662 &bytes_read);
2663 addr += bytes_read;
2664
2665 const gdb_byte *const entry_end = addr + entry_length;
2666 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2667 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2668 if (addr + entry_length > section->buffer + section->size)
2669 {
47e3f474 2670 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2671 "length %s exceeds section length %s, "
2672 "ignoring .debug_aranges."),
47e3f474
TV
2673 objfile_name (objfile),
2674 plongest (entry_addr - section->buffer),
927aa2e7
JK
2675 plongest (bytes_read + entry_length),
2676 pulongest (section->size));
2677 return;
2678 }
2679
2680 /* The version number. */
2681 const uint16_t version = read_2_bytes (abfd, addr);
2682 addr += 2;
2683 if (version != 2)
2684 {
47e3f474 2685 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2686 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2687 objfile_name (objfile),
2688 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2689 return;
2690 }
2691
2692 const uint64_t debug_info_offset
2693 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2694 addr += offset_size;
2695 const auto per_cu_it
2696 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2697 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2698 {
47e3f474 2699 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2700 "debug_info_offset %s does not exists, "
2701 "ignoring .debug_aranges."),
47e3f474
TV
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
927aa2e7
JK
2704 pulongest (debug_info_offset));
2705 return;
2706 }
2707 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2708
2709 const uint8_t address_size = *addr++;
2710 if (address_size < 1 || address_size > 8)
2711 {
47e3f474 2712 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2713 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2716 return;
2717 }
2718
2719 const uint8_t segment_selector_size = *addr++;
2720 if (segment_selector_size != 0)
2721 {
47e3f474 2722 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2723 "segment_selector_size %u is not supported, "
2724 "ignoring .debug_aranges."),
47e3f474
TV
2725 objfile_name (objfile),
2726 plongest (entry_addr - section->buffer),
927aa2e7
JK
2727 segment_selector_size);
2728 return;
2729 }
2730
2731 /* Must pad to an alignment boundary that is twice the address
2732 size. It is undocumented by the DWARF standard but GCC does
2733 use it. */
2734 for (size_t padding = ((-(addr - section->buffer))
2735 & (2 * address_size - 1));
2736 padding > 0; padding--)
2737 if (*addr++ != 0)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer));
927aa2e7
JK
2743 return;
2744 }
2745
2746 for (;;)
2747 {
2748 if (addr + 2 * address_size > entry_end)
2749 {
47e3f474 2750 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2751 "address list is not properly terminated, "
2752 "ignoring .debug_aranges."),
47e3f474
TV
2753 objfile_name (objfile),
2754 plongest (entry_addr - section->buffer));
927aa2e7
JK
2755 return;
2756 }
2757 ULONGEST start = extract_unsigned_integer (addr, address_size,
2758 dwarf5_byte_order);
2759 addr += address_size;
2760 ULONGEST length = extract_unsigned_integer (addr, address_size,
2761 dwarf5_byte_order);
2762 addr += address_size;
2763 if (start == 0 && length == 0)
2764 break;
2765 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2766 {
2767 /* Symbol was eliminated due to a COMDAT group. */
2768 continue;
2769 }
2770 ULONGEST end = start + length;
79748972
TT
2771 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2772 - baseaddr);
2773 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2774 - baseaddr);
927aa2e7
JK
2775 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2776 }
2777 }
2778
d320c2b5 2779 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2780 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2781}
2782
9291a0cd
TT
2783/* Find a slot in the mapped index INDEX for the object named NAME.
2784 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2785 constant pool and return true. If NAME cannot be found, return
2786 false. */
2fdf6df6 2787
109483d9 2788static bool
9291a0cd
TT
2789find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2790 offset_type **vec_out)
2791{
0cf03b49 2792 offset_type hash;
9291a0cd 2793 offset_type slot, step;
559a7a62 2794 int (*cmp) (const char *, const char *);
9291a0cd 2795
791afaa2 2796 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2797 if (current_language->la_language == language_cplus
45280282
IB
2798 || current_language->la_language == language_fortran
2799 || current_language->la_language == language_d)
0cf03b49
JK
2800 {
2801 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2802 not contain any. */
a8719064 2803
72998fb3 2804 if (strchr (name, '(') != NULL)
0cf03b49 2805 {
109483d9 2806 without_params = cp_remove_params (name);
0cf03b49 2807
72998fb3 2808 if (without_params != NULL)
791afaa2 2809 name = without_params.get ();
0cf03b49
JK
2810 }
2811 }
2812
559a7a62 2813 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2814 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2815 simulate our NAME being searched is also lowercased. */
2816 hash = mapped_index_string_hash ((index->version == 4
2817 && case_sensitivity == case_sensitive_off
2818 ? 5 : index->version),
2819 name);
2820
f00a2de2
PA
2821 slot = hash & (index->symbol_table.size () - 1);
2822 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2823 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2824
2825 for (;;)
2826 {
9291a0cd 2827 const char *str;
f00a2de2
PA
2828
2829 const auto &bucket = index->symbol_table[slot];
2830 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2831 return false;
9291a0cd 2832
f00a2de2 2833 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2834 if (!cmp (name, str))
9291a0cd
TT
2835 {
2836 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2837 + MAYBE_SWAP (bucket.vec));
109483d9 2838 return true;
9291a0cd
TT
2839 }
2840
f00a2de2 2841 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2842 }
2843}
2844
4485a1c1
SM
2845/* A helper function that reads the .gdb_index from BUFFER and fills
2846 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2847 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2848 ok to use deprecated sections.
2849
2850 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2851 out parameters that are filled in with information about the CU and
2852 TU lists in the section.
2853
4485a1c1 2854 Returns true if all went well, false otherwise. */
2fdf6df6 2855
d33bc52e 2856static bool
3810f182 2857read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2858 bool deprecated_ok,
2859 gdb::array_view<const gdb_byte> buffer,
2860 struct mapped_index *map,
2861 const gdb_byte **cu_list,
2862 offset_type *cu_list_elements,
2863 const gdb_byte **types_list,
2864 offset_type *types_list_elements)
2865{
2866 const gdb_byte *addr = &buffer[0];
82430852 2867
9291a0cd 2868 /* Version check. */
4485a1c1 2869 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2870 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2871 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2872 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2873 indices. */
831adc1f 2874 if (version < 4)
481860b3
GB
2875 {
2876 static int warning_printed = 0;
2877 if (!warning_printed)
2878 {
2879 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2880 filename);
481860b3
GB
2881 warning_printed = 1;
2882 }
2883 return 0;
2884 }
2885 /* Index version 4 uses a different hash function than index version
2886 5 and later.
2887
2888 Versions earlier than 6 did not emit psymbols for inlined
2889 functions. Using these files will cause GDB not to be able to
2890 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2891 indices unless the user has done
2892 "set use-deprecated-index-sections on". */
2ec9a5e0 2893 if (version < 6 && !deprecated_ok)
481860b3
GB
2894 {
2895 static int warning_printed = 0;
2896 if (!warning_printed)
2897 {
e615022a
DE
2898 warning (_("\
2899Skipping deprecated .gdb_index section in %s.\n\
2900Do \"set use-deprecated-index-sections on\" before the file is read\n\
2901to use the section anyway."),
2ec9a5e0 2902 filename);
481860b3
GB
2903 warning_printed = 1;
2904 }
2905 return 0;
2906 }
796a7ff8 2907 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2908 of the TU (for symbols coming from TUs),
2909 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2910 Plus gold-generated indices can have duplicate entries for global symbols,
2911 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2912 These are just performance bugs, and we can't distinguish gdb-generated
2913 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2914
481860b3 2915 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2916 longer backward compatible. */
796a7ff8 2917 if (version > 8)
594e8718 2918 return 0;
9291a0cd 2919
559a7a62 2920 map->version = version;
9291a0cd 2921
4485a1c1 2922 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2923
4485a1c1 2924 int i = 0;
2ec9a5e0
TT
2925 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2926 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2927 / 8);
1fd400ff
TT
2928 ++i;
2929
2ec9a5e0
TT
2930 *types_list = addr + MAYBE_SWAP (metadata[i]);
2931 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2932 - MAYBE_SWAP (metadata[i]))
2933 / 8);
987d643c 2934 ++i;
1fd400ff 2935
f00a2de2
PA
2936 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2937 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2938 map->address_table
2939 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2940 ++i;
2941
f00a2de2
PA
2942 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2943 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2944 map->symbol_table
2945 = gdb::array_view<mapped_index::symbol_table_slot>
2946 ((mapped_index::symbol_table_slot *) symbol_table,
2947 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2948
f00a2de2 2949 ++i;
f9d83a0b 2950 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2951
2ec9a5e0
TT
2952 return 1;
2953}
2954
4485a1c1
SM
2955/* Callback types for dwarf2_read_gdb_index. */
2956
2957typedef gdb::function_view
2958 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2959 get_gdb_index_contents_ftype;
2960typedef gdb::function_view
2961 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2962 get_gdb_index_contents_dwz_ftype;
2963
927aa2e7 2964/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2965 elements of all the CUs and return 1. Otherwise, return 0. */
2966
2967static int
4485a1c1
SM
2968dwarf2_read_gdb_index
2969 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2970 get_gdb_index_contents_ftype get_gdb_index_contents,
2971 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2972{
2ec9a5e0
TT
2973 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2974 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2975 struct dwz_file *dwz;
12359b5e 2976 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 2977
4485a1c1
SM
2978 gdb::array_view<const gdb_byte> main_index_contents
2979 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2980
2981 if (main_index_contents.empty ())
2982 return 0;
2983
3063847f 2984 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 2985 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
2986 use_deprecated_index_sections,
2987 main_index_contents, map.get (), &cu_list,
2988 &cu_list_elements, &types_list,
2989 &types_list_elements))
2ec9a5e0
TT
2990 return 0;
2991
0fefef59 2992 /* Don't use the index if it's empty. */
3063847f 2993 if (map->symbol_table.empty ())
0fefef59
DE
2994 return 0;
2995
2ec9a5e0
TT
2996 /* If there is a .dwz file, read it so we can get its CU list as
2997 well. */
ed2dc618 2998 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 2999 if (dwz != NULL)
2ec9a5e0 3000 {
2ec9a5e0
TT
3001 struct mapped_index dwz_map;
3002 const gdb_byte *dwz_types_ignore;
3003 offset_type dwz_types_elements_ignore;
3004
4485a1c1
SM
3005 gdb::array_view<const gdb_byte> dwz_index_content
3006 = get_gdb_index_contents_dwz (objfile, dwz);
3007
3008 if (dwz_index_content.empty ())
3009 return 0;
3010
3810f182 3011 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3012 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3013 &dwz_list, &dwz_list_elements,
3014 &dwz_types_ignore,
3015 &dwz_types_elements_ignore))
2ec9a5e0
TT
3016 {
3017 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3018 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3019 return 0;
3020 }
3021 }
3022
12359b5e
SM
3023 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3024 dwz_list, dwz_list_elements);
1fd400ff 3025
8b70b953
TT
3026 if (types_list_elements)
3027 {
8b70b953
TT
3028 /* We can only handle a single .debug_types when we have an
3029 index. */
fd5866f6 3030 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3031 return 0;
3032
fd5866f6 3033 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3034
12359b5e
SM
3035 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3036 types_list, types_list_elements);
8b70b953 3037 }
9291a0cd 3038
3063847f 3039 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3040
3063847f 3041 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3042 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3043 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3044 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3045
3046 return 1;
3047}
3048
dee91e82 3049/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3050
dee91e82
DE
3051static void
3052dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3053 const gdb_byte *info_ptr,
3e225074 3054 struct die_info *comp_unit_die)
9291a0cd 3055{
dee91e82 3056 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3057 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3058 struct dwarf2_per_objfile *dwarf2_per_objfile
3059 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3060 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3061 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3062 struct attribute *attr;
7b9f3c50
DE
3063 void **slot;
3064 struct quick_file_names *qfn;
9291a0cd 3065
0186c6a7
DE
3066 gdb_assert (! this_cu->is_debug_types);
3067
07261596
TT
3068 /* Our callers never want to match partial units -- instead they
3069 will match the enclosing full CU. */
3070 if (comp_unit_die->tag == DW_TAG_partial_unit)
3071 {
3072 this_cu->v.quick->no_file_data = 1;
3073 return;
3074 }
3075
0186c6a7 3076 lh_cu = this_cu;
7b9f3c50 3077 slot = NULL;
dee91e82 3078
fff8551c 3079 line_header_up lh;
9c541725 3080 sect_offset line_offset {};
fff8551c 3081
dee91e82 3082 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3083 if (attr != nullptr)
9291a0cd 3084 {
7b9f3c50
DE
3085 struct quick_file_names find_entry;
3086
9c541725 3087 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3088
3089 /* We may have already read in this line header (TU line header sharing).
3090 If we have we're done. */
094b34ac 3091 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3092 find_entry.hash.line_sect_off = line_offset;
5895093f 3093 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3094 &find_entry, INSERT);
3095 if (*slot != NULL)
3096 {
9a3c8263 3097 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3098 return;
7b9f3c50
DE
3099 }
3100
3019eac3 3101 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3102 }
3103 if (lh == NULL)
3104 {
094b34ac 3105 lh_cu->v.quick->no_file_data = 1;
dee91e82 3106 return;
9291a0cd
TT
3107 }
3108
8d749320 3109 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3110 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3111 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3112 gdb_assert (slot != NULL);
3113 *slot = qfn;
9291a0cd 3114
d721ba37 3115 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3116
aa391654
TT
3117 int offset = 0;
3118 if (strcmp (fnd.name, "<unknown>") != 0)
3119 ++offset;
3120
7ba99d21 3121 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3122 qfn->file_names =
aa391654
TT
3123 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3124 if (offset != 0)
3125 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3126 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3127 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3128 fnd.comp_dir).release ();
7b9f3c50 3129 qfn->real_names = NULL;
9291a0cd 3130
094b34ac 3131 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3132}
3133
3134/* A helper for the "quick" functions which attempts to read the line
3135 table for THIS_CU. */
3136
3137static struct quick_file_names *
e4a48d9d 3138dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3139{
0186c6a7
DE
3140 /* This should never be called for TUs. */
3141 gdb_assert (! this_cu->is_debug_types);
3142 /* Nor type unit groups. */
197400e8 3143 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3144
dee91e82
DE
3145 if (this_cu->v.quick->file_names != NULL)
3146 return this_cu->v.quick->file_names;
3147 /* If we know there is no line data, no point in looking again. */
3148 if (this_cu->v.quick->no_file_data)
3149 return NULL;
3150
c0ab21c2
TT
3151 cutu_reader reader (this_cu);
3152 if (!reader.dummy_p)
3e225074 3153 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3154
3155 if (this_cu->v.quick->no_file_data)
3156 return NULL;
3157 return this_cu->v.quick->file_names;
9291a0cd
TT
3158}
3159
3160/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3161 real path for a given file name from the line table. */
2fdf6df6 3162
9291a0cd 3163static const char *
7b9f3c50
DE
3164dw2_get_real_path (struct objfile *objfile,
3165 struct quick_file_names *qfn, int index)
9291a0cd 3166{
7b9f3c50
DE
3167 if (qfn->real_names == NULL)
3168 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3169 qfn->num_file_names, const char *);
9291a0cd 3170
7b9f3c50 3171 if (qfn->real_names[index] == NULL)
14278e1f 3172 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3173
7b9f3c50 3174 return qfn->real_names[index];
9291a0cd
TT
3175}
3176
3177static struct symtab *
3178dw2_find_last_source_symtab (struct objfile *objfile)
3179{
ed2dc618
SM
3180 struct dwarf2_per_objfile *dwarf2_per_objfile
3181 = get_dwarf2_per_objfile (objfile);
b76e467d 3182 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3183 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3184
43f3e411
DE
3185 if (cust == NULL)
3186 return NULL;
ed2dc618 3187
43f3e411 3188 return compunit_primary_filetab (cust);
9291a0cd
TT
3189}
3190
7b9f3c50
DE
3191/* Traversal function for dw2_forget_cached_source_info. */
3192
3193static int
3194dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3195{
7b9f3c50 3196 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3197
7b9f3c50 3198 if (file_data->real_names)
9291a0cd 3199 {
7b9f3c50 3200 int i;
9291a0cd 3201
7b9f3c50 3202 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3203 {
7b9f3c50
DE
3204 xfree ((void*) file_data->real_names[i]);
3205 file_data->real_names[i] = NULL;
9291a0cd
TT
3206 }
3207 }
7b9f3c50
DE
3208
3209 return 1;
3210}
3211
3212static void
3213dw2_forget_cached_source_info (struct objfile *objfile)
3214{
ed2dc618
SM
3215 struct dwarf2_per_objfile *dwarf2_per_objfile
3216 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3217
5895093f 3218 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3219 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3220}
3221
f8eba3c6
TT
3222/* Helper function for dw2_map_symtabs_matching_filename that expands
3223 the symtabs and calls the iterator. */
3224
3225static int
3226dw2_map_expand_apply (struct objfile *objfile,
3227 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3228 const char *name, const char *real_path,
14bc53a8 3229 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3230{
43f3e411 3231 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3232
3233 /* Don't visit already-expanded CUs. */
43f3e411 3234 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3235 return 0;
3236
3237 /* This may expand more than one symtab, and we want to iterate over
3238 all of them. */
58f0c718 3239 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3240
14bc53a8
PA
3241 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3242 last_made, callback);
f8eba3c6
TT
3243}
3244
3245/* Implementation of the map_symtabs_matching_filename method. */
3246
14bc53a8
PA
3247static bool
3248dw2_map_symtabs_matching_filename
3249 (struct objfile *objfile, const char *name, const char *real_path,
3250 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3251{
c011a4f4 3252 const char *name_basename = lbasename (name);
ed2dc618
SM
3253 struct dwarf2_per_objfile *dwarf2_per_objfile
3254 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3255
848e3e78
DE
3256 /* The rule is CUs specify all the files, including those used by
3257 any TU, so there's no need to scan TUs here. */
f4dc4d17 3258
b76e467d 3259 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3260 {
3d7bb9d9 3261 /* We only need to look at symtabs not already expanded. */
43f3e411 3262 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3263 continue;
3264
b76e467d 3265 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3266 if (file_data == NULL)
9291a0cd
TT
3267 continue;
3268
b76e467d 3269 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3270 {
7b9f3c50 3271 const char *this_name = file_data->file_names[j];
da235a7c 3272 const char *this_real_name;
9291a0cd 3273
af529f8f 3274 if (compare_filenames_for_search (this_name, name))
9291a0cd 3275 {
f5b95b50 3276 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3277 callback))
3278 return true;
288e77a7 3279 continue;
4aac40c8 3280 }
9291a0cd 3281
c011a4f4
DE
3282 /* Before we invoke realpath, which can get expensive when many
3283 files are involved, do a quick comparison of the basenames. */
3284 if (! basenames_may_differ
3285 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3286 continue;
3287
da235a7c
JK
3288 this_real_name = dw2_get_real_path (objfile, file_data, j);
3289 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3290 {
da235a7c 3291 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3292 callback))
3293 return true;
288e77a7 3294 continue;
da235a7c 3295 }
9291a0cd 3296
da235a7c
JK
3297 if (real_path != NULL)
3298 {
af529f8f
JK
3299 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3300 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3301 if (this_real_name != NULL
af529f8f 3302 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3303 {
f5b95b50 3304 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3305 callback))
3306 return true;
288e77a7 3307 continue;
9291a0cd
TT
3308 }
3309 }
3310 }
3311 }
3312
14bc53a8 3313 return false;
9291a0cd
TT
3314}
3315
da51c347
DE
3316/* Struct used to manage iterating over all CUs looking for a symbol. */
3317
3318struct dw2_symtab_iterator
9291a0cd 3319{
ed2dc618
SM
3320 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3321 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3322 /* If set, only look for symbols that match that block. Valid values are
3323 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3324 gdb::optional<block_enum> block_index;
da51c347
DE
3325 /* The kind of symbol we're looking for. */
3326 domain_enum domain;
3327 /* The list of CUs from the index entry of the symbol,
3328 or NULL if not found. */
3329 offset_type *vec;
3330 /* The next element in VEC to look at. */
3331 int next;
3332 /* The number of elements in VEC, or zero if there is no match. */
3333 int length;
8943b874
DE
3334 /* Have we seen a global version of the symbol?
3335 If so we can ignore all further global instances.
3336 This is to work around gold/15646, inefficient gold-generated
3337 indices. */
3338 int global_seen;
da51c347 3339};
9291a0cd 3340
2b79f376 3341/* Initialize the index symtab iterator ITER. */
2fdf6df6 3342
9291a0cd 3343static void
da51c347 3344dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3345 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3346 gdb::optional<block_enum> block_index,
da51c347
DE
3347 domain_enum domain,
3348 const char *name)
3349{
ed2dc618 3350 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3351 iter->block_index = block_index;
3352 iter->domain = domain;
3353 iter->next = 0;
8943b874 3354 iter->global_seen = 0;
da51c347 3355
3063847f 3356 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3357
3358 /* index is NULL if OBJF_READNOW. */
3359 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3360 iter->length = MAYBE_SWAP (*iter->vec);
3361 else
3362 {
3363 iter->vec = NULL;
3364 iter->length = 0;
3365 }
3366}
3367
3368/* Return the next matching CU or NULL if there are no more. */
3369
3370static struct dwarf2_per_cu_data *
3371dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3372{
ed2dc618
SM
3373 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3374
da51c347
DE
3375 for ( ; iter->next < iter->length; ++iter->next)
3376 {
3377 offset_type cu_index_and_attrs =
3378 MAYBE_SWAP (iter->vec[iter->next + 1]);
3379 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3380 gdb_index_symbol_kind symbol_kind =
3381 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3382 /* Only check the symbol attributes if they're present.
3383 Indices prior to version 7 don't record them,
3384 and indices >= 7 may elide them for certain symbols
3385 (gold does this). */
3386 int attrs_valid =
ed2dc618 3387 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3388 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3389
3190f0c6 3390 /* Don't crash on bad data. */
b76e467d 3391 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3392 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3393 {
b98664d3 3394 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3395 " [in module %s]"),
3396 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3397 continue;
3398 }
3399
ff4c9fec 3400 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3401
da51c347 3402 /* Skip if already read in. */
43f3e411 3403 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3404 continue;
3405
8943b874
DE
3406 /* Check static vs global. */
3407 if (attrs_valid)
3408 {
2b79f376
SM
3409 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3410
3411 if (iter->block_index.has_value ())
3412 {
3413 bool want_static = *iter->block_index == STATIC_BLOCK;
3414
3415 if (is_static != want_static)
3416 continue;
3417 }
3418
8943b874
DE
3419 /* Work around gold/15646. */
3420 if (!is_static && iter->global_seen)
3421 continue;
3422 if (!is_static)
3423 iter->global_seen = 1;
3424 }
da51c347
DE
3425
3426 /* Only check the symbol's kind if it has one. */
3427 if (attrs_valid)
3428 {
3429 switch (iter->domain)
3430 {
3431 case VAR_DOMAIN:
3432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3433 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3434 /* Some types are also in VAR_DOMAIN. */
3435 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3436 continue;
3437 break;
3438 case STRUCT_DOMAIN:
3439 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3440 continue;
3441 break;
3442 case LABEL_DOMAIN:
3443 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3444 continue;
3445 break;
59c35742
AB
3446 case MODULE_DOMAIN:
3447 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3448 continue;
3449 break;
da51c347
DE
3450 default:
3451 break;
3452 }
3453 }
3454
3455 ++iter->next;
3456 return per_cu;
3457 }
3458
3459 return NULL;
3460}
3461
43f3e411 3462static struct compunit_symtab *
c7f839cb 3463dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3464 const char *name, domain_enum domain)
9291a0cd 3465{
43f3e411 3466 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3467 struct dwarf2_per_objfile *dwarf2_per_objfile
3468 = get_dwarf2_per_objfile (objfile);
9291a0cd 3469
b5ec771e
PA
3470 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3471
ed2dc618
SM
3472 struct dw2_symtab_iterator iter;
3473 struct dwarf2_per_cu_data *per_cu;
da51c347 3474
2b79f376 3475 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3476
ed2dc618
SM
3477 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3478 {
3479 struct symbol *sym, *with_opaque = NULL;
58f0c718 3480 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3481 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3482 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3483
ed2dc618
SM
3484 sym = block_find_symbol (block, name, domain,
3485 block_find_non_opaque_type_preferred,
3486 &with_opaque);
b2e2f908 3487
ed2dc618
SM
3488 /* Some caution must be observed with overloaded functions
3489 and methods, since the index will not contain any overload
3490 information (but NAME might contain it). */
da51c347 3491
ed2dc618
SM
3492 if (sym != NULL
3493 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3494 return stab;
3495 if (with_opaque != NULL
3496 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3497 stab_best = stab;
da51c347 3498
ed2dc618 3499 /* Keep looking through other CUs. */
9291a0cd 3500 }
9291a0cd 3501
da51c347 3502 return stab_best;
9291a0cd
TT
3503}
3504
3505static void
3506dw2_print_stats (struct objfile *objfile)
3507{
ed2dc618
SM
3508 struct dwarf2_per_objfile *dwarf2_per_objfile
3509 = get_dwarf2_per_objfile (objfile);
b76e467d 3510 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3511 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3512 int count = 0;
9291a0cd 3513
ed2dc618 3514 for (int i = 0; i < total; ++i)
9291a0cd 3515 {
ff4c9fec 3516 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3517
43f3e411 3518 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3519 ++count;
3520 }
e4a48d9d 3521 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3522 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3523}
3524
779bd270
DE
3525/* This dumps minimal information about the index.
3526 It is called via "mt print objfiles".
3527 One use is to verify .gdb_index has been loaded by the
3528 gdb.dwarf2/gdb-index.exp testcase. */
3529
9291a0cd
TT
3530static void
3531dw2_dump (struct objfile *objfile)
3532{
ed2dc618
SM
3533 struct dwarf2_per_objfile *dwarf2_per_objfile
3534 = get_dwarf2_per_objfile (objfile);
3535
779bd270
DE
3536 gdb_assert (dwarf2_per_objfile->using_index);
3537 printf_filtered (".gdb_index:");
3538 if (dwarf2_per_objfile->index_table != NULL)
3539 {
3540 printf_filtered (" version %d\n",
3541 dwarf2_per_objfile->index_table->version);
3542 }
3543 else
3544 printf_filtered (" faked for \"readnow\"\n");
3545 printf_filtered ("\n");
9291a0cd
TT
3546}
3547
9291a0cd
TT
3548static void
3549dw2_expand_symtabs_for_function (struct objfile *objfile,
3550 const char *func_name)
3551{
ed2dc618
SM
3552 struct dwarf2_per_objfile *dwarf2_per_objfile
3553 = get_dwarf2_per_objfile (objfile);
da51c347 3554
ed2dc618
SM
3555 struct dw2_symtab_iterator iter;
3556 struct dwarf2_per_cu_data *per_cu;
da51c347 3557
2b79f376 3558 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3559
ed2dc618 3560 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3561 dw2_instantiate_symtab (per_cu, false);
da51c347 3562
9291a0cd
TT
3563}
3564
3565static void
3566dw2_expand_all_symtabs (struct objfile *objfile)
3567{
ed2dc618
SM
3568 struct dwarf2_per_objfile *dwarf2_per_objfile
3569 = get_dwarf2_per_objfile (objfile);
b76e467d 3570 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3571 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3572
ed2dc618 3573 for (int i = 0; i < total_units; ++i)
9291a0cd 3574 {
ff4c9fec 3575 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3576
58f0c718
TT
3577 /* We don't want to directly expand a partial CU, because if we
3578 read it with the wrong language, then assertion failures can
3579 be triggered later on. See PR symtab/23010. So, tell
3580 dw2_instantiate_symtab to skip partial CUs -- any important
3581 partial CU will be read via DW_TAG_imported_unit anyway. */
3582 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3583 }
3584}
3585
3586static void
652a8996
JK
3587dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3588 const char *fullname)
9291a0cd 3589{
ed2dc618
SM
3590 struct dwarf2_per_objfile *dwarf2_per_objfile
3591 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3592
3593 /* We don't need to consider type units here.
3594 This is only called for examining code, e.g. expand_line_sal.
3595 There can be an order of magnitude (or more) more type units
3596 than comp units, and we avoid them if we can. */
3597
b76e467d 3598 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3599 {
3d7bb9d9 3600 /* We only need to look at symtabs not already expanded. */
43f3e411 3601 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3602 continue;
3603
b76e467d 3604 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3605 if (file_data == NULL)
9291a0cd
TT
3606 continue;
3607
b76e467d 3608 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3609 {
652a8996
JK
3610 const char *this_fullname = file_data->file_names[j];
3611
3612 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3613 {
58f0c718 3614 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3615 break;
3616 }
3617 }
3618 }
3619}
3620
9291a0cd 3621static void
199b4314
TT
3622dw2_map_matching_symbols
3623 (struct objfile *objfile,
b054970d 3624 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3625 int global,
3626 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3627 symbol_compare_ftype *ordered_compare)
9291a0cd 3628{
1aa98955
TV
3629 /* Used for Ada. */
3630 struct dwarf2_per_objfile *dwarf2_per_objfile
3631 = get_dwarf2_per_objfile (objfile);
3632
3633 if (dwarf2_per_objfile->index_table != nullptr)
3634 {
3635 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3636 here though if the current language is Ada for a non-Ada objfile
3637 using GNU index. As Ada does not look for non-Ada symbols this
3638 function should just return. */
3639 return;
3640 }
3641
3642 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3643 inline psym_map_matching_symbols here, assuming all partial symtabs have
3644 been read in. */
3645 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3646
3647 for (compunit_symtab *cust : objfile->compunits ())
3648 {
3649 const struct block *block;
3650
3651 if (cust == NULL)
3652 continue;
3653 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3654 if (!iterate_over_symbols_terminated (block, name,
3655 domain, callback))
3656 return;
3657 }
9291a0cd
TT
3658}
3659
e1ef7d7a
PA
3660/* Starting from a search name, return the string that finds the upper
3661 bound of all strings that start with SEARCH_NAME in a sorted name
3662 list. Returns the empty string to indicate that the upper bound is
3663 the end of the list. */
3664
3665static std::string
3666make_sort_after_prefix_name (const char *search_name)
3667{
3668 /* When looking to complete "func", we find the upper bound of all
3669 symbols that start with "func" by looking for where we'd insert
3670 the closest string that would follow "func" in lexicographical
3671 order. Usually, that's "func"-with-last-character-incremented,
3672 i.e. "fund". Mind non-ASCII characters, though. Usually those
3673 will be UTF-8 multi-byte sequences, but we can't be certain.
3674 Especially mind the 0xff character, which is a valid character in
3675 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3676 rule out compilers allowing it in identifiers. Note that
3677 conveniently, strcmp/strcasecmp are specified to compare
3678 characters interpreted as unsigned char. So what we do is treat
3679 the whole string as a base 256 number composed of a sequence of
3680 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3681 to 0, and carries 1 to the following more-significant position.
3682 If the very first character in SEARCH_NAME ends up incremented
3683 and carries/overflows, then the upper bound is the end of the
3684 list. The string after the empty string is also the empty
3685 string.
3686
3687 Some examples of this operation:
3688
3689 SEARCH_NAME => "+1" RESULT
3690
3691 "abc" => "abd"
3692 "ab\xff" => "ac"
3693 "\xff" "a" "\xff" => "\xff" "b"
3694 "\xff" => ""
3695 "\xff\xff" => ""
3696 "" => ""
3697
3698 Then, with these symbols for example:
3699
3700 func
3701 func1
3702 fund
3703
3704 completing "func" looks for symbols between "func" and
3705 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3706 which finds "func" and "func1", but not "fund".
3707
3708 And with:
3709
3710 funcÿ (Latin1 'ÿ' [0xff])
3711 funcÿ1
3712 fund
3713
3714 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3715 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3716
3717 And with:
3718
3719 ÿÿ (Latin1 'ÿ' [0xff])
3720 ÿÿ1
3721
3722 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3723 the end of the list.
3724 */
3725 std::string after = search_name;
3726 while (!after.empty () && (unsigned char) after.back () == 0xff)
3727 after.pop_back ();
3728 if (!after.empty ())
3729 after.back () = (unsigned char) after.back () + 1;
3730 return after;
3731}
3732
5c58de74 3733/* See declaration. */
61d96d7e 3734
5c58de74
PA
3735std::pair<std::vector<name_component>::const_iterator,
3736 std::vector<name_component>::const_iterator>
44ed8f3e 3737mapped_index_base::find_name_components_bounds
3b00ef10 3738 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3739{
5c58de74
PA
3740 auto *name_cmp
3741 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3742
3b00ef10 3743 const char *lang_name
e0802d59 3744 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3745
3f563c84
PA
3746 /* Comparison function object for lower_bound that matches against a
3747 given symbol name. */
3748 auto lookup_compare_lower = [&] (const name_component &elem,
3749 const char *name)
3750 {
5c58de74 3751 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3752 const char *elem_name = elem_qualified + elem.name_offset;
3753 return name_cmp (elem_name, name) < 0;
3754 };
3755
3756 /* Comparison function object for upper_bound that matches against a
3757 given symbol name. */
3758 auto lookup_compare_upper = [&] (const char *name,
3759 const name_component &elem)
3760 {
5c58de74 3761 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3762 const char *elem_name = elem_qualified + elem.name_offset;
3763 return name_cmp (name, elem_name) < 0;
3764 };
3765
5c58de74
PA
3766 auto begin = this->name_components.begin ();
3767 auto end = this->name_components.end ();
3f563c84
PA
3768
3769 /* Find the lower bound. */
3770 auto lower = [&] ()
3771 {
3b00ef10 3772 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3773 return begin;
3774 else
3b00ef10 3775 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3776 } ();
3777
3778 /* Find the upper bound. */
3779 auto upper = [&] ()
3780 {
5c58de74 3781 if (lookup_name_without_params.completion_mode ())
3f563c84 3782 {
e1ef7d7a
PA
3783 /* In completion mode, we want UPPER to point past all
3784 symbols names that have the same prefix. I.e., with
3785 these symbols, and completing "func":
3786
3787 function << lower bound
3788 function1
3789 other_function << upper bound
3790
3791 We find the upper bound by looking for the insertion
3792 point of "func"-with-last-character-incremented,
3793 i.e. "fund". */
3b00ef10 3794 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3795 if (after.empty ())
3f563c84 3796 return end;
e6b2f5ef
PA
3797 return std::lower_bound (lower, end, after.c_str (),
3798 lookup_compare_lower);
3f563c84
PA
3799 }
3800 else
3b00ef10 3801 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3802 } ();
3803
5c58de74
PA
3804 return {lower, upper};
3805}
3806
3807/* See declaration. */
3808
3809void
44ed8f3e 3810mapped_index_base::build_name_components ()
5c58de74
PA
3811{
3812 if (!this->name_components.empty ())
3813 return;
3814
3815 this->name_components_casing = case_sensitivity;
3816 auto *name_cmp
3817 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3818
3819 /* The code below only knows how to break apart components of C++
3820 symbol names (and other languages that use '::' as
3b00ef10 3821 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3822 auto count = this->symbol_name_count ();
3823 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3824 {
44ed8f3e 3825 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3826 continue;
3827
3828 const char *name = this->symbol_name_at (idx);
3829
3830 /* Add each name component to the name component table. */
3831 unsigned int previous_len = 0;
3b00ef10
TT
3832
3833 if (strstr (name, "::") != nullptr)
3834 {
3835 for (unsigned int current_len = cp_find_first_component (name);
3836 name[current_len] != '\0';
3837 current_len += cp_find_first_component (name + current_len))
3838 {
3839 gdb_assert (name[current_len] == ':');
3840 this->name_components.push_back ({previous_len, idx});
3841 /* Skip the '::'. */
3842 current_len += 2;
3843 previous_len = current_len;
3844 }
3845 }
3846 else
5c58de74 3847 {
3b00ef10
TT
3848 /* Handle the Ada encoded (aka mangled) form here. */
3849 for (const char *iter = strstr (name, "__");
3850 iter != nullptr;
3851 iter = strstr (iter, "__"))
3852 {
3853 this->name_components.push_back ({previous_len, idx});
3854 iter += 2;
3855 previous_len = iter - name;
3856 }
5c58de74 3857 }
3b00ef10 3858
5c58de74
PA
3859 this->name_components.push_back ({previous_len, idx});
3860 }
3861
3862 /* Sort name_components elements by name. */
3863 auto name_comp_compare = [&] (const name_component &left,
3864 const name_component &right)
3865 {
3866 const char *left_qualified = this->symbol_name_at (left.idx);
3867 const char *right_qualified = this->symbol_name_at (right.idx);
3868
3869 const char *left_name = left_qualified + left.name_offset;
3870 const char *right_name = right_qualified + right.name_offset;
3871
3872 return name_cmp (left_name, right_name) < 0;
3873 };
3874
3875 std::sort (this->name_components.begin (),
3876 this->name_components.end (),
3877 name_comp_compare);
3878}
3879
3880/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3881 mapped_index_base instead of the containing objfile. This is split
3882 to a separate function in order to be able to unit test the
3883 name_components matching using a mock mapped_index_base. For each
5c58de74 3884 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3885 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3886
3887static void
3888dw2_expand_symtabs_matching_symbol
44ed8f3e 3889 (mapped_index_base &index,
5c58de74
PA
3890 const lookup_name_info &lookup_name_in,
3891 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3892 enum search_domain kind,
3b00ef10 3893 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3894{
3895 lookup_name_info lookup_name_without_params
3896 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3897
3898 /* Build the symbol name component sorted vector, if we haven't
3899 yet. */
3900 index.build_name_components ();
3901
3f563c84
PA
3902 /* The same symbol may appear more than once in the range though.
3903 E.g., if we're looking for symbols that complete "w", and we have
3904 a symbol named "w1::w2", we'll find the two name components for
3905 that same symbol in the range. To be sure we only call the
3906 callback once per symbol, we first collect the symbol name
3907 indexes that matched in a temporary vector and ignore
3908 duplicates. */
3909 std::vector<offset_type> matches;
3f563c84 3910
3b00ef10
TT
3911 struct name_and_matcher
3912 {
3913 symbol_name_matcher_ftype *matcher;
ecc6c606 3914 const char *name;
3b00ef10
TT
3915
3916 bool operator== (const name_and_matcher &other) const
3f563c84 3917 {
ecc6c606 3918 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3919 }
3920 };
3921
3922 /* A vector holding all the different symbol name matchers, for all
3923 languages. */
3924 std::vector<name_and_matcher> matchers;
3925
3926 for (int i = 0; i < nr_languages; i++)
3927 {
3928 enum language lang_e = (enum language) i;
3929
3930 const language_defn *lang = language_def (lang_e);
3931 symbol_name_matcher_ftype *name_matcher
3932 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3933
3b00ef10
TT
3934 name_and_matcher key {
3935 name_matcher,
3936 lookup_name_without_params.language_lookup_name (lang_e)
3937 };
3938
3939 /* Don't insert the same comparison routine more than once.
3940 Note that we do this linear walk. This is not a problem in
3941 practice because the number of supported languages is
3942 low. */
3943 if (std::find (matchers.begin (), matchers.end (), key)
3944 != matchers.end ())
9291a0cd 3945 continue;
3b00ef10
TT
3946 matchers.push_back (std::move (key));
3947
3948 auto bounds
3949 = index.find_name_components_bounds (lookup_name_without_params,
3950 lang_e);
3951
3952 /* Now for each symbol name in range, check to see if we have a name
3953 match, and if so, call the MATCH_CALLBACK callback. */
3954
3955 for (; bounds.first != bounds.second; ++bounds.first)
3956 {
3957 const char *qualified = index.symbol_name_at (bounds.first->idx);
3958
3959 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3960 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3961 continue;
9291a0cd 3962
3b00ef10
TT
3963 matches.push_back (bounds.first->idx);
3964 }
3f563c84
PA
3965 }
3966
3967 std::sort (matches.begin (), matches.end ());
3968
3969 /* Finally call the callback, once per match. */
3970 ULONGEST prev = -1;
3971 for (offset_type idx : matches)
3972 {
3973 if (prev != idx)
3974 {
3b00ef10
TT
3975 if (!match_callback (idx))
3976 break;
3f563c84
PA
3977 prev = idx;
3978 }
3979 }
3980
3981 /* Above we use a type wider than idx's for 'prev', since 0 and
3982 (offset_type)-1 are both possible values. */
3983 static_assert (sizeof (prev) > sizeof (offset_type), "");
3984}
3985
c62446b1
PA
3986#if GDB_SELF_TEST
3987
3988namespace selftests { namespace dw2_expand_symtabs_matching {
3989
a3c5fafd
PA
3990/* A mock .gdb_index/.debug_names-like name index table, enough to
3991 exercise dw2_expand_symtabs_matching_symbol, which works with the
3992 mapped_index_base interface. Builds an index from the symbol list
3993 passed as parameter to the constructor. */
3994class mock_mapped_index : public mapped_index_base
c62446b1
PA
3995{
3996public:
a3c5fafd
PA
3997 mock_mapped_index (gdb::array_view<const char *> symbols)
3998 : m_symbol_table (symbols)
c62446b1
PA
3999 {}
4000
a3c5fafd 4001 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4002
a3c5fafd 4003 /* Return the number of names in the symbol table. */
632e107b 4004 size_t symbol_name_count () const override
c62446b1 4005 {
a3c5fafd 4006 return m_symbol_table.size ();
c62446b1
PA
4007 }
4008
a3c5fafd 4009 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4010 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4011 {
4012 return m_symbol_table[idx];
4013 }
c62446b1 4014
a3c5fafd
PA
4015private:
4016 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4017};
4018
4019/* Convenience function that converts a NULL pointer to a "<null>"
4020 string, to pass to print routines. */
4021
4022static const char *
4023string_or_null (const char *str)
4024{
4025 return str != NULL ? str : "<null>";
4026}
4027
4028/* Check if a lookup_name_info built from
4029 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4030 index. EXPECTED_LIST is the list of expected matches, in expected
4031 matching order. If no match expected, then an empty list is
4032 specified. Returns true on success. On failure prints a warning
4033 indicating the file:line that failed, and returns false. */
4034
4035static bool
4036check_match (const char *file, int line,
4037 mock_mapped_index &mock_index,
4038 const char *name, symbol_name_match_type match_type,
4039 bool completion_mode,
4040 std::initializer_list<const char *> expected_list)
4041{
4042 lookup_name_info lookup_name (name, match_type, completion_mode);
4043
4044 bool matched = true;
4045
4046 auto mismatch = [&] (const char *expected_str,
4047 const char *got)
4048 {
4049 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4050 "expected=\"%s\", got=\"%s\"\n"),
4051 file, line,
4052 (match_type == symbol_name_match_type::FULL
4053 ? "FULL" : "WILD"),
4054 name, string_or_null (expected_str), string_or_null (got));
4055 matched = false;
4056 };
4057
4058 auto expected_it = expected_list.begin ();
4059 auto expected_end = expected_list.end ();
4060
a3c5fafd 4061 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4062 NULL, ALL_DOMAIN,
4063 [&] (offset_type idx)
4064 {
a3c5fafd 4065 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4066 const char *expected_str
4067 = expected_it == expected_end ? NULL : *expected_it++;
4068
4069 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4070 mismatch (expected_str, matched_name);
3b00ef10 4071 return true;
c62446b1
PA
4072 });
4073
4074 const char *expected_str
4075 = expected_it == expected_end ? NULL : *expected_it++;
4076 if (expected_str != NULL)
4077 mismatch (expected_str, NULL);
4078
4079 return matched;
4080}
4081
4082/* The symbols added to the mock mapped_index for testing (in
4083 canonical form). */
4084static const char *test_symbols[] = {
4085 "function",
4086 "std::bar",
4087 "std::zfunction",
4088 "std::zfunction2",
4089 "w1::w2",
4090 "ns::foo<char*>",
4091 "ns::foo<int>",
4092 "ns::foo<long>",
a20714ff
PA
4093 "ns2::tmpl<int>::foo2",
4094 "(anonymous namespace)::A::B::C",
c62446b1 4095
e1ef7d7a
PA
4096 /* These are used to check that the increment-last-char in the
4097 matching algorithm for completion doesn't match "t1_fund" when
4098 completing "t1_func". */
4099 "t1_func",
4100 "t1_func1",
4101 "t1_fund",
4102 "t1_fund1",
4103
4104 /* A UTF-8 name with multi-byte sequences to make sure that
4105 cp-name-parser understands this as a single identifier ("função"
4106 is "function" in PT). */
4107 u8"u8função",
4108
4109 /* \377 (0xff) is Latin1 'ÿ'. */
4110 "yfunc\377",
4111
4112 /* \377 (0xff) is Latin1 'ÿ'. */
4113 "\377",
4114 "\377\377123",
4115
c62446b1
PA
4116 /* A name with all sorts of complications. Starts with "z" to make
4117 it easier for the completion tests below. */
4118#define Z_SYM_NAME \
4119 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4120 "::tuple<(anonymous namespace)::ui*, " \
4121 "std::default_delete<(anonymous namespace)::ui>, void>"
4122
4123 Z_SYM_NAME
4124};
4125
a3c5fafd
PA
4126/* Returns true if the mapped_index_base::find_name_component_bounds
4127 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4128 in completion mode. */
5c58de74
PA
4129
4130static bool
a3c5fafd 4131check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4132 const char *search_name,
4133 gdb::array_view<const char *> expected_syms)
4134{
4135 lookup_name_info lookup_name (search_name,
4136 symbol_name_match_type::FULL, true);
4137
3b00ef10
TT
4138 auto bounds = index.find_name_components_bounds (lookup_name,
4139 language_cplus);
5c58de74
PA
4140
4141 size_t distance = std::distance (bounds.first, bounds.second);
4142 if (distance != expected_syms.size ())
4143 return false;
4144
4145 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4146 {
4147 auto nc_elem = bounds.first + exp_elem;
4148 const char *qualified = index.symbol_name_at (nc_elem->idx);
4149 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4150 return false;
4151 }
4152
4153 return true;
4154}
4155
4156/* Test the lower-level mapped_index::find_name_component_bounds
4157 method. */
4158
c62446b1 4159static void
5c58de74
PA
4160test_mapped_index_find_name_component_bounds ()
4161{
4162 mock_mapped_index mock_index (test_symbols);
4163
a3c5fafd 4164 mock_index.build_name_components ();
5c58de74
PA
4165
4166 /* Test the lower-level mapped_index::find_name_component_bounds
4167 method in completion mode. */
4168 {
4169 static const char *expected_syms[] = {
4170 "t1_func",
4171 "t1_func1",
5c58de74
PA
4172 };
4173
a3c5fafd 4174 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4175 "t1_func", expected_syms));
4176 }
4177
4178 /* Check that the increment-last-char in the name matching algorithm
4179 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4180 {
4181 static const char *expected_syms1[] = {
4182 "\377",
4183 "\377\377123",
4184 };
a3c5fafd 4185 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4186 "\377", expected_syms1));
4187
4188 static const char *expected_syms2[] = {
4189 "\377\377123",
4190 };
a3c5fafd 4191 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4192 "\377\377", expected_syms2));
4193 }
4194}
4195
4196/* Test dw2_expand_symtabs_matching_symbol. */
4197
4198static void
4199test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4200{
4201 mock_mapped_index mock_index (test_symbols);
4202
4203 /* We let all tests run until the end even if some fails, for debug
4204 convenience. */
4205 bool any_mismatch = false;
4206
4207 /* Create the expected symbols list (an initializer_list). Needed
4208 because lists have commas, and we need to pass them to CHECK,
4209 which is a macro. */
4210#define EXPECT(...) { __VA_ARGS__ }
4211
4212 /* Wrapper for check_match that passes down the current
4213 __FILE__/__LINE__. */
4214#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4215 any_mismatch |= !check_match (__FILE__, __LINE__, \
4216 mock_index, \
4217 NAME, MATCH_TYPE, COMPLETION_MODE, \
4218 EXPECTED_LIST)
4219
4220 /* Identity checks. */
4221 for (const char *sym : test_symbols)
4222 {
4223 /* Should be able to match all existing symbols. */
4224 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4225 EXPECT (sym));
4226
4227 /* Should be able to match all existing symbols with
4228 parameters. */
4229 std::string with_params = std::string (sym) + "(int)";
4230 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4231 EXPECT (sym));
4232
4233 /* Should be able to match all existing symbols with
4234 parameters and qualifiers. */
4235 with_params = std::string (sym) + " ( int ) const";
4236 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4237 EXPECT (sym));
4238
4239 /* This should really find sym, but cp-name-parser.y doesn't
4240 know about lvalue/rvalue qualifiers yet. */
4241 with_params = std::string (sym) + " ( int ) &&";
4242 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4243 {});
4244 }
4245
e1ef7d7a
PA
4246 /* Check that the name matching algorithm for completion doesn't get
4247 confused with Latin1 'ÿ' / 0xff. */
4248 {
4249 static const char str[] = "\377";
4250 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4251 EXPECT ("\377", "\377\377123"));
4252 }
4253
4254 /* Check that the increment-last-char in the matching algorithm for
4255 completion doesn't match "t1_fund" when completing "t1_func". */
4256 {
4257 static const char str[] = "t1_func";
4258 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4259 EXPECT ("t1_func", "t1_func1"));
4260 }
4261
c62446b1
PA
4262 /* Check that completion mode works at each prefix of the expected
4263 symbol name. */
4264 {
4265 static const char str[] = "function(int)";
4266 size_t len = strlen (str);
4267 std::string lookup;
4268
4269 for (size_t i = 1; i < len; i++)
4270 {
4271 lookup.assign (str, i);
4272 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4273 EXPECT ("function"));
4274 }
4275 }
4276
4277 /* While "w" is a prefix of both components, the match function
4278 should still only be called once. */
4279 {
4280 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4281 EXPECT ("w1::w2"));
a20714ff
PA
4282 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4283 EXPECT ("w1::w2"));
c62446b1
PA
4284 }
4285
4286 /* Same, with a "complicated" symbol. */
4287 {
4288 static const char str[] = Z_SYM_NAME;
4289 size_t len = strlen (str);
4290 std::string lookup;
4291
4292 for (size_t i = 1; i < len; i++)
4293 {
4294 lookup.assign (str, i);
4295 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4296 EXPECT (Z_SYM_NAME));
4297 }
4298 }
4299
4300 /* In FULL mode, an incomplete symbol doesn't match. */
4301 {
4302 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4303 {});
4304 }
4305
4306 /* A complete symbol with parameters matches any overload, since the
4307 index has no overload info. */
4308 {
4309 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4310 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4311 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4312 EXPECT ("std::zfunction", "std::zfunction2"));
4313 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4314 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4315 }
4316
4317 /* Check that whitespace is ignored appropriately. A symbol with a
4318 template argument list. */
4319 {
4320 static const char expected[] = "ns::foo<int>";
4321 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4322 EXPECT (expected));
a20714ff
PA
4323 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4324 EXPECT (expected));
c62446b1
PA
4325 }
4326
4327 /* Check that whitespace is ignored appropriately. A symbol with a
4328 template argument list that includes a pointer. */
4329 {
4330 static const char expected[] = "ns::foo<char*>";
4331 /* Try both completion and non-completion modes. */
4332 static const bool completion_mode[2] = {false, true};
4333 for (size_t i = 0; i < 2; i++)
4334 {
4335 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4336 completion_mode[i], EXPECT (expected));
a20714ff
PA
4337 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4338 completion_mode[i], EXPECT (expected));
c62446b1
PA
4339
4340 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4341 completion_mode[i], EXPECT (expected));
a20714ff
PA
4342 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4343 completion_mode[i], EXPECT (expected));
c62446b1
PA
4344 }
4345 }
4346
4347 {
4348 /* Check method qualifiers are ignored. */
4349 static const char expected[] = "ns::foo<char*>";
4350 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4351 symbol_name_match_type::FULL, true, EXPECT (expected));
4352 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4353 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4354 CHECK_MATCH ("foo < char * > ( int ) const",
4355 symbol_name_match_type::WILD, true, EXPECT (expected));
4356 CHECK_MATCH ("foo < char * > ( int ) &&",
4357 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4358 }
4359
4360 /* Test lookup names that don't match anything. */
4361 {
a20714ff
PA
4362 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4363 {});
4364
c62446b1
PA
4365 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4366 {});
4367 }
4368
a20714ff
PA
4369 /* Some wild matching tests, exercising "(anonymous namespace)",
4370 which should not be confused with a parameter list. */
4371 {
4372 static const char *syms[] = {
4373 "A::B::C",
4374 "B::C",
4375 "C",
4376 "A :: B :: C ( int )",
4377 "B :: C ( int )",
4378 "C ( int )",
4379 };
4380
4381 for (const char *s : syms)
4382 {
4383 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4384 EXPECT ("(anonymous namespace)::A::B::C"));
4385 }
4386 }
4387
4388 {
4389 static const char expected[] = "ns2::tmpl<int>::foo2";
4390 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4391 EXPECT (expected));
4392 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4393 EXPECT (expected));
4394 }
4395
c62446b1
PA
4396 SELF_CHECK (!any_mismatch);
4397
4398#undef EXPECT
4399#undef CHECK_MATCH
4400}
4401
5c58de74
PA
4402static void
4403run_test ()
4404{
4405 test_mapped_index_find_name_component_bounds ();
4406 test_dw2_expand_symtabs_matching_symbol ();
4407}
4408
c62446b1
PA
4409}} // namespace selftests::dw2_expand_symtabs_matching
4410
4411#endif /* GDB_SELF_TEST */
4412
4b514bc8
JK
4413/* If FILE_MATCHER is NULL or if PER_CU has
4414 dwarf2_per_cu_quick_data::MARK set (see
4415 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4416 EXPANSION_NOTIFY on it. */
4417
4418static void
4419dw2_expand_symtabs_matching_one
4420 (struct dwarf2_per_cu_data *per_cu,
4421 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4422 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4423{
4424 if (file_matcher == NULL || per_cu->v.quick->mark)
4425 {
4426 bool symtab_was_null
4427 = (per_cu->v.quick->compunit_symtab == NULL);
4428
58f0c718 4429 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4430
4431 if (expansion_notify != NULL
4432 && symtab_was_null
4433 && per_cu->v.quick->compunit_symtab != NULL)
4434 expansion_notify (per_cu->v.quick->compunit_symtab);
4435 }
4436}
4437
3f563c84
PA
4438/* Helper for dw2_expand_matching symtabs. Called on each symbol
4439 matched, to expand corresponding CUs that were marked. IDX is the
4440 index of the symbol name that matched. */
4441
4442static void
4443dw2_expand_marked_cus
ed2dc618 4444 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4445 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4446 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4447 search_domain kind)
4448{
3f563c84
PA
4449 offset_type *vec, vec_len, vec_idx;
4450 bool global_seen = false;
ed2dc618 4451 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4452
61920122 4453 vec = (offset_type *) (index.constant_pool
f00a2de2 4454 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4455 vec_len = MAYBE_SWAP (vec[0]);
4456 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4457 {
61920122
PA
4458 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4459 /* This value is only valid for index versions >= 7. */
4460 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4461 gdb_index_symbol_kind symbol_kind =
4462 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4463 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4464 /* Only check the symbol attributes if they're present.
4465 Indices prior to version 7 don't record them,
4466 and indices >= 7 may elide them for certain symbols
4467 (gold does this). */
4468 int attrs_valid =
4469 (index.version >= 7
4470 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4471
4472 /* Work around gold/15646. */
4473 if (attrs_valid)
9291a0cd 4474 {
61920122
PA
4475 if (!is_static && global_seen)
4476 continue;
4477 if (!is_static)
4478 global_seen = true;
4479 }
3190f0c6 4480
61920122
PA
4481 /* Only check the symbol's kind if it has one. */
4482 if (attrs_valid)
4483 {
4484 switch (kind)
8943b874 4485 {
61920122
PA
4486 case VARIABLES_DOMAIN:
4487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4488 continue;
4489 break;
4490 case FUNCTIONS_DOMAIN:
4491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4492 continue;
61920122
PA
4493 break;
4494 case TYPES_DOMAIN:
4495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4496 continue;
4497 break;
59c35742
AB
4498 case MODULES_DOMAIN:
4499 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4500 continue;
4501 break;
61920122
PA
4502 default:
4503 break;
8943b874 4504 }
61920122 4505 }
8943b874 4506
61920122 4507 /* Don't crash on bad data. */
b76e467d 4508 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4509 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4510 {
b98664d3 4511 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4512 " [in module %s]"),
4513 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4514 continue;
4515 }
4516
ff4c9fec 4517 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4518 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4519 expansion_notify);
61920122
PA
4520 }
4521}
4522
4b514bc8
JK
4523/* If FILE_MATCHER is non-NULL, set all the
4524 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4525 that match FILE_MATCHER. */
4526
61920122 4527static void
4b514bc8 4528dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4529 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4530 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4531{
4b514bc8 4532 if (file_matcher == NULL)
61920122
PA
4533 return;
4534
4b514bc8
JK
4535 objfile *const objfile = dwarf2_per_objfile->objfile;
4536
4537 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4538 htab_eq_pointer,
4539 NULL, xcalloc, xfree));
4540 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4541 htab_eq_pointer,
4542 NULL, xcalloc, xfree));
61920122 4543
4b514bc8
JK
4544 /* The rule is CUs specify all the files, including those used by
4545 any TU, so there's no need to scan TUs here. */
61920122 4546
b76e467d 4547 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4548 {
927aa2e7
JK
4549 QUIT;
4550
4551 per_cu->v.quick->mark = 0;
4552
4553 /* We only need to look at symtabs not already expanded. */
4554 if (per_cu->v.quick->compunit_symtab)
4555 continue;
4556
b76e467d 4557 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4558 if (file_data == NULL)
4559 continue;
4560
4561 if (htab_find (visited_not_found.get (), file_data) != NULL)
4562 continue;
4563 else if (htab_find (visited_found.get (), file_data) != NULL)
4564 {
4565 per_cu->v.quick->mark = 1;
4566 continue;
4567 }
4568
b76e467d 4569 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4570 {
4571 const char *this_real_name;
4572
4573 if (file_matcher (file_data->file_names[j], false))
4574 {
4575 per_cu->v.quick->mark = 1;
4576 break;
4577 }
4578
4579 /* Before we invoke realpath, which can get expensive when many
4580 files are involved, do a quick comparison of the basenames. */
4581 if (!basenames_may_differ
4582 && !file_matcher (lbasename (file_data->file_names[j]),
4583 true))
4584 continue;
4585
4586 this_real_name = dw2_get_real_path (objfile, file_data, j);
4587 if (file_matcher (this_real_name, false))
4588 {
4589 per_cu->v.quick->mark = 1;
4590 break;
4591 }
4592 }
4593
b76e467d
SM
4594 void **slot = htab_find_slot (per_cu->v.quick->mark
4595 ? visited_found.get ()
4596 : visited_not_found.get (),
4597 file_data, INSERT);
927aa2e7
JK
4598 *slot = file_data;
4599 }
4600}
4601
4602static void
4603dw2_expand_symtabs_matching
4604 (struct objfile *objfile,
4605 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4606 const lookup_name_info *lookup_name,
927aa2e7
JK
4607 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4608 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4609 enum search_domain kind)
4610{
ed2dc618
SM
4611 struct dwarf2_per_objfile *dwarf2_per_objfile
4612 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4613
4614 /* index_table is NULL if OBJF_READNOW. */
4615 if (!dwarf2_per_objfile->index_table)
4616 return;
4617
ed2dc618 4618 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4619
c1a66c06
TV
4620 if (symbol_matcher == NULL && lookup_name == NULL)
4621 {
4622 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4623 {
4624 QUIT;
4625
4626 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4627 expansion_notify);
4628 }
4629 return;
4630 }
4631
927aa2e7
JK
4632 mapped_index &index = *dwarf2_per_objfile->index_table;
4633
c1a66c06 4634 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4635 symbol_matcher,
4636 kind, [&] (offset_type idx)
4637 {
ed2dc618 4638 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4639 expansion_notify, kind);
3b00ef10 4640 return true;
927aa2e7
JK
4641 });
4642}
4643
4644/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4645 symtab. */
4646
4647static struct compunit_symtab *
4648recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4649 CORE_ADDR pc)
4650{
4651 int i;
4652
4653 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4654 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4655 return cust;
4656
4657 if (cust->includes == NULL)
4658 return NULL;
4659
4660 for (i = 0; cust->includes[i]; ++i)
4661 {
4662 struct compunit_symtab *s = cust->includes[i];
4663
4664 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4665 if (s != NULL)
4666 return s;
4667 }
4668
4669 return NULL;
4670}
4671
4672static struct compunit_symtab *
4673dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4674 struct bound_minimal_symbol msymbol,
4675 CORE_ADDR pc,
4676 struct obj_section *section,
4677 int warn_if_readin)
4678{
4679 struct dwarf2_per_cu_data *data;
4680 struct compunit_symtab *result;
4681
d320c2b5 4682 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4683 return NULL;
4684
b3b3bada 4685 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4686 data = (struct dwarf2_per_cu_data *) addrmap_find
4687 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4688 if (!data)
4689 return NULL;
4690
4691 if (warn_if_readin && data->v.quick->compunit_symtab)
4692 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4693 paddress (objfile->arch (), pc));
927aa2e7
JK
4694
4695 result
58f0c718
TT
4696 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4697 false),
927aa2e7
JK
4698 pc);
4699 gdb_assert (result != NULL);
4700 return result;
4701}
4702
4703static void
4704dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4705 void *data, int need_fullname)
4706{
ed2dc618
SM
4707 struct dwarf2_per_objfile *dwarf2_per_objfile
4708 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4709
4710 if (!dwarf2_per_objfile->filenames_cache)
4711 {
4712 dwarf2_per_objfile->filenames_cache.emplace ();
4713
4714 htab_up visited (htab_create_alloc (10,
4715 htab_hash_pointer, htab_eq_pointer,
4716 NULL, xcalloc, xfree));
4717
4718 /* The rule is CUs specify all the files, including those used
4719 by any TU, so there's no need to scan TUs here. We can
4720 ignore file names coming from already-expanded CUs. */
4721
b76e467d 4722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4723 {
927aa2e7
JK
4724 if (per_cu->v.quick->compunit_symtab)
4725 {
4726 void **slot = htab_find_slot (visited.get (),
4727 per_cu->v.quick->file_names,
4728 INSERT);
4729
4730 *slot = per_cu->v.quick->file_names;
4731 }
4732 }
4733
b76e467d 4734 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4735 {
927aa2e7
JK
4736 /* We only need to look at symtabs not already expanded. */
4737 if (per_cu->v.quick->compunit_symtab)
4738 continue;
4739
b76e467d 4740 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4741 if (file_data == NULL)
4742 continue;
4743
b76e467d 4744 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4745 if (*slot)
4746 {
4747 /* Already visited. */
4748 continue;
4749 }
4750 *slot = file_data;
4751
4752 for (int j = 0; j < file_data->num_file_names; ++j)
4753 {
4754 const char *filename = file_data->file_names[j];
4755 dwarf2_per_objfile->filenames_cache->seen (filename);
4756 }
4757 }
4758 }
4759
4760 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4761 {
4762 gdb::unique_xmalloc_ptr<char> this_real_name;
4763
4764 if (need_fullname)
4765 this_real_name = gdb_realpath (filename);
4766 (*fun) (filename, this_real_name.get (), data);
4767 });
4768}
4769
4770static int
4771dw2_has_symbols (struct objfile *objfile)
4772{
4773 return 1;
4774}
4775
4776const struct quick_symbol_functions dwarf2_gdb_index_functions =
4777{
4778 dw2_has_symbols,
4779 dw2_find_last_source_symtab,
4780 dw2_forget_cached_source_info,
4781 dw2_map_symtabs_matching_filename,
4782 dw2_lookup_symbol,
d3214198 4783 NULL,
927aa2e7
JK
4784 dw2_print_stats,
4785 dw2_dump,
927aa2e7
JK
4786 dw2_expand_symtabs_for_function,
4787 dw2_expand_all_symtabs,
4788 dw2_expand_symtabs_with_fullname,
4789 dw2_map_matching_symbols,
4790 dw2_expand_symtabs_matching,
4791 dw2_find_pc_sect_compunit_symtab,
4792 NULL,
4793 dw2_map_symbol_filenames
4794};
4795
4796/* DWARF-5 debug_names reader. */
4797
4798/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4799static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4800
4801/* A helper function that reads the .debug_names section in SECTION
4802 and fills in MAP. FILENAME is the name of the file containing the
4803 section; it is used for error reporting.
4804
4805 Returns true if all went well, false otherwise. */
4806
4807static bool
4808read_debug_names_from_section (struct objfile *objfile,
4809 const char *filename,
4810 struct dwarf2_section_info *section,
4811 mapped_debug_names &map)
4812{
96b79293 4813 if (section->empty ())
927aa2e7
JK
4814 return false;
4815
4816 /* Older elfutils strip versions could keep the section in the main
4817 executable while splitting it for the separate debug info file. */
96b79293 4818 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4819 return false;
4820
96b79293 4821 section->read (objfile);
927aa2e7 4822
08feed99 4823 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4824
4825 const gdb_byte *addr = section->buffer;
4826
96b79293 4827 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4828
4829 unsigned int bytes_read;
4830 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4831 addr += bytes_read;
4832
4833 map.dwarf5_is_dwarf64 = bytes_read != 4;
4834 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4835 if (bytes_read + length != section->size)
4836 {
4837 /* There may be multiple per-CU indices. */
4838 warning (_("Section .debug_names in %s length %s does not match "
4839 "section length %s, ignoring .debug_names."),
4840 filename, plongest (bytes_read + length),
4841 pulongest (section->size));
4842 return false;
4843 }
4844
4845 /* The version number. */
4846 uint16_t version = read_2_bytes (abfd, addr);
4847 addr += 2;
4848 if (version != 5)
4849 {
4850 warning (_("Section .debug_names in %s has unsupported version %d, "
4851 "ignoring .debug_names."),
4852 filename, version);
4853 return false;
4854 }
4855
4856 /* Padding. */
4857 uint16_t padding = read_2_bytes (abfd, addr);
4858 addr += 2;
4859 if (padding != 0)
4860 {
4861 warning (_("Section .debug_names in %s has unsupported padding %d, "
4862 "ignoring .debug_names."),
4863 filename, padding);
4864 return false;
4865 }
4866
4867 /* comp_unit_count - The number of CUs in the CU list. */
4868 map.cu_count = read_4_bytes (abfd, addr);
4869 addr += 4;
4870
4871 /* local_type_unit_count - The number of TUs in the local TU
4872 list. */
4873 map.tu_count = read_4_bytes (abfd, addr);
4874 addr += 4;
4875
4876 /* foreign_type_unit_count - The number of TUs in the foreign TU
4877 list. */
4878 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4879 addr += 4;
4880 if (foreign_tu_count != 0)
4881 {
4882 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4883 "ignoring .debug_names."),
4884 filename, static_cast<unsigned long> (foreign_tu_count));
4885 return false;
4886 }
4887
4888 /* bucket_count - The number of hash buckets in the hash lookup
4889 table. */
4890 map.bucket_count = read_4_bytes (abfd, addr);
4891 addr += 4;
4892
4893 /* name_count - The number of unique names in the index. */
4894 map.name_count = read_4_bytes (abfd, addr);
4895 addr += 4;
4896
4897 /* abbrev_table_size - The size in bytes of the abbreviations
4898 table. */
4899 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* augmentation_string_size - The size in bytes of the augmentation
4903 string. This value is rounded up to a multiple of 4. */
4904 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4905 addr += 4;
4906 map.augmentation_is_gdb = ((augmentation_string_size
4907 == sizeof (dwarf5_augmentation))
4908 && memcmp (addr, dwarf5_augmentation,
4909 sizeof (dwarf5_augmentation)) == 0);
4910 augmentation_string_size += (-augmentation_string_size) & 3;
4911 addr += augmentation_string_size;
4912
4913 /* List of CUs */
4914 map.cu_table_reordered = addr;
4915 addr += map.cu_count * map.offset_size;
4916
4917 /* List of Local TUs */
4918 map.tu_table_reordered = addr;
4919 addr += map.tu_count * map.offset_size;
4920
4921 /* Hash Lookup Table */
4922 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4923 addr += map.bucket_count * 4;
4924 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4925 addr += map.name_count * 4;
4926
4927 /* Name Table */
4928 map.name_table_string_offs_reordered = addr;
4929 addr += map.name_count * map.offset_size;
4930 map.name_table_entry_offs_reordered = addr;
4931 addr += map.name_count * map.offset_size;
4932
4933 const gdb_byte *abbrev_table_start = addr;
4934 for (;;)
4935 {
927aa2e7
JK
4936 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938 if (index_num == 0)
4939 break;
4940
4941 const auto insertpair
4942 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4943 if (!insertpair.second)
4944 {
4945 warning (_("Section .debug_names in %s has duplicate index %s, "
4946 "ignoring .debug_names."),
4947 filename, pulongest (index_num));
4948 return false;
4949 }
4950 mapped_debug_names::index_val &indexval = insertpair.first->second;
4951 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4952 addr += bytes_read;
4953
4954 for (;;)
4955 {
4956 mapped_debug_names::index_val::attr attr;
4957 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961 if (attr.form == DW_FORM_implicit_const)
4962 {
4963 attr.implicit_const = read_signed_leb128 (abfd, addr,
4964 &bytes_read);
4965 addr += bytes_read;
4966 }
4967 if (attr.dw_idx == 0 && attr.form == 0)
4968 break;
4969 indexval.attr_vec.push_back (std::move (attr));
4970 }
4971 }
4972 if (addr != abbrev_table_start + abbrev_table_size)
4973 {
4974 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4975 "of size %s vs. written as %u, ignoring .debug_names."),
4976 filename, plongest (addr - abbrev_table_start),
4977 abbrev_table_size);
927aa2e7
JK
4978 return false;
4979 }
4980 map.entry_pool = addr;
4981
4982 return true;
4983}
4984
4985/* A helper for create_cus_from_debug_names that handles the MAP's CU
4986 list. */
4987
4988static void
ed2dc618 4989create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4990 const mapped_debug_names &map,
4991 dwarf2_section_info &section,
b76e467d 4992 bool is_dwz)
927aa2e7
JK
4993{
4994 sect_offset sect_off_prev;
4995 for (uint32_t i = 0; i <= map.cu_count; ++i)
4996 {
4997 sect_offset sect_off_next;
4998 if (i < map.cu_count)
4999 {
5000 sect_off_next
5001 = (sect_offset) (extract_unsigned_integer
5002 (map.cu_table_reordered + i * map.offset_size,
5003 map.offset_size,
5004 map.dwarf5_byte_order));
5005 }
5006 else
5007 sect_off_next = (sect_offset) section.size;
5008 if (i >= 1)
5009 {
5010 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5011 dwarf2_per_cu_data *per_cu
ed2dc618 5012 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5013 sect_off_prev, length);
b76e467d 5014 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5015 }
5016 sect_off_prev = sect_off_next;
5017 }
5018}
5019
5020/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5021 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5022
5023static void
ed2dc618 5024create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5025 const mapped_debug_names &map,
5026 const mapped_debug_names &dwz_map)
5027{
b76e467d
SM
5028 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5029 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5030
ed2dc618
SM
5031 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5032 dwarf2_per_objfile->info,
b76e467d 5033 false /* is_dwz */);
927aa2e7
JK
5034
5035 if (dwz_map.cu_count == 0)
5036 return;
5037
ed2dc618
SM
5038 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5039 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5040 true /* is_dwz */);
927aa2e7
JK
5041}
5042
5043/* Read .debug_names. If everything went ok, initialize the "quick"
5044 elements of all the CUs and return true. Otherwise, return false. */
5045
5046static bool
ed2dc618 5047dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5048{
22ca247e
TT
5049 std::unique_ptr<mapped_debug_names> map
5050 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5051 mapped_debug_names dwz_map (dwarf2_per_objfile);
5052 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5053
5054 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5055 &dwarf2_per_objfile->debug_names,
22ca247e 5056 *map))
927aa2e7
JK
5057 return false;
5058
5059 /* Don't use the index if it's empty. */
22ca247e 5060 if (map->name_count == 0)
927aa2e7
JK
5061 return false;
5062
5063 /* If there is a .dwz file, read it so we can get its CU list as
5064 well. */
ed2dc618 5065 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5066 if (dwz != NULL)
5067 {
5068 if (!read_debug_names_from_section (objfile,
00f93c44 5069 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5070 &dwz->debug_names, dwz_map))
5071 {
5072 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5073 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5074 return false;
5075 }
5076 }
5077
22ca247e 5078 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5079
22ca247e 5080 if (map->tu_count != 0)
927aa2e7
JK
5081 {
5082 /* We can only handle a single .debug_types when we have an
5083 index. */
fd5866f6 5084 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5085 return false;
5086
fd5866f6 5087 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5088
5089 create_signatured_type_table_from_debug_names
22ca247e 5090 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5091 }
5092
ed2dc618
SM
5093 create_addrmap_from_aranges (dwarf2_per_objfile,
5094 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5095
22ca247e 5096 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5097 dwarf2_per_objfile->using_index = 1;
5098 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5099 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5100
5101 return true;
5102}
5103
927aa2e7
JK
5104/* Type used to manage iterating over all CUs looking for a symbol for
5105 .debug_names. */
5106
5107class dw2_debug_names_iterator
5108{
5109public:
927aa2e7 5110 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5111 gdb::optional<block_enum> block_index,
5112 domain_enum domain,
927aa2e7 5113 const char *name)
2b79f376 5114 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5115 m_addr (find_vec_in_debug_names (map, name))
5116 {}
5117
5118 dw2_debug_names_iterator (const mapped_debug_names &map,
5119 search_domain search, uint32_t namei)
5120 : m_map (map),
5121 m_search (search),
5122 m_addr (find_vec_in_debug_names (map, namei))
5123 {}
5124
3b00ef10
TT
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
5126 block_enum block_index, domain_enum domain,
5127 uint32_t namei)
5128 : m_map (map), m_block_index (block_index), m_domain (domain),
5129 m_addr (find_vec_in_debug_names (map, namei))
5130 {}
5131
927aa2e7
JK
5132 /* Return the next matching CU or NULL if there are no more. */
5133 dwarf2_per_cu_data *next ();
5134
5135private:
5136 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5137 const char *name);
5138 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5139 uint32_t namei);
5140
5141 /* The internalized form of .debug_names. */
5142 const mapped_debug_names &m_map;
5143
2b79f376
SM
5144 /* If set, only look for symbols that match that block. Valid values are
5145 GLOBAL_BLOCK and STATIC_BLOCK. */
5146 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5147
5148 /* The kind of symbol we're looking for. */
5149 const domain_enum m_domain = UNDEF_DOMAIN;
5150 const search_domain m_search = ALL_DOMAIN;
5151
5152 /* The list of CUs from the index entry of the symbol, or NULL if
5153 not found. */
5154 const gdb_byte *m_addr;
5155};
5156
5157const char *
5158mapped_debug_names::namei_to_name (uint32_t namei) const
5159{
5160 const ULONGEST namei_string_offs
5161 = extract_unsigned_integer ((name_table_string_offs_reordered
5162 + namei * offset_size),
5163 offset_size,
5164 dwarf5_byte_order);
4f44ae6c
TT
5165 return read_indirect_string_at_offset (dwarf2_per_objfile,
5166 namei_string_offs);
927aa2e7
JK
5167}
5168
5169/* Find a slot in .debug_names for the object named NAME. If NAME is
5170 found, return pointer to its pool data. If NAME cannot be found,
5171 return NULL. */
5172
5173const gdb_byte *
5174dw2_debug_names_iterator::find_vec_in_debug_names
5175 (const mapped_debug_names &map, const char *name)
5176{
5177 int (*cmp) (const char *, const char *);
5178
54ee4252 5179 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5180 if (current_language->la_language == language_cplus
5181 || current_language->la_language == language_fortran
5182 || current_language->la_language == language_d)
5183 {
5184 /* NAME is already canonical. Drop any qualifiers as
5185 .debug_names does not contain any. */
5186
5187 if (strchr (name, '(') != NULL)
5188 {
54ee4252 5189 without_params = cp_remove_params (name);
927aa2e7 5190 if (without_params != NULL)
54ee4252 5191 name = without_params.get ();
927aa2e7
JK
5192 }
5193 }
5194
5195 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5196
5197 const uint32_t full_hash = dwarf5_djb_hash (name);
5198 uint32_t namei
5199 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5200 (map.bucket_table_reordered
5201 + (full_hash % map.bucket_count)), 4,
5202 map.dwarf5_byte_order);
5203 if (namei == 0)
5204 return NULL;
5205 --namei;
5206 if (namei >= map.name_count)
5207 {
b98664d3 5208 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5209 "[in module %s]"),
5210 namei, map.name_count,
ed2dc618 5211 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5212 return NULL;
5213 }
5214
5215 for (;;)
5216 {
5217 const uint32_t namei_full_hash
5218 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5219 (map.hash_table_reordered + namei), 4,
5220 map.dwarf5_byte_order);
5221 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5222 return NULL;
5223
5224 if (full_hash == namei_full_hash)
5225 {
5226 const char *const namei_string = map.namei_to_name (namei);
5227
5228#if 0 /* An expensive sanity check. */
5229 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5230 {
b98664d3 5231 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5232 "[in module %s]"),
5233 namei, objfile_name (dwarf2_per_objfile->objfile));
5234 return NULL;
5235 }
5236#endif
5237
5238 if (cmp (namei_string, name) == 0)
5239 {
5240 const ULONGEST namei_entry_offs
5241 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5242 + namei * map.offset_size),
5243 map.offset_size, map.dwarf5_byte_order);
5244 return map.entry_pool + namei_entry_offs;
5245 }
5246 }
5247
5248 ++namei;
5249 if (namei >= map.name_count)
5250 return NULL;
5251 }
5252}
5253
5254const gdb_byte *
5255dw2_debug_names_iterator::find_vec_in_debug_names
5256 (const mapped_debug_names &map, uint32_t namei)
5257{
5258 if (namei >= map.name_count)
5259 {
b98664d3 5260 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5261 "[in module %s]"),
5262 namei, map.name_count,
ed2dc618 5263 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5264 return NULL;
5265 }
5266
5267 const ULONGEST namei_entry_offs
5268 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5269 + namei * map.offset_size),
5270 map.offset_size, map.dwarf5_byte_order);
5271 return map.entry_pool + namei_entry_offs;
5272}
5273
5274/* See dw2_debug_names_iterator. */
5275
5276dwarf2_per_cu_data *
5277dw2_debug_names_iterator::next ()
5278{
5279 if (m_addr == NULL)
5280 return NULL;
5281
ed2dc618
SM
5282 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5283 struct objfile *objfile = dwarf2_per_objfile->objfile;
5284 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5285
5286 again:
5287
5288 unsigned int bytes_read;
5289 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5290 m_addr += bytes_read;
5291 if (abbrev == 0)
5292 return NULL;
5293
5294 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5295 if (indexval_it == m_map.abbrev_map.cend ())
5296 {
b98664d3 5297 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5298 "[in module %s]"),
ed2dc618 5299 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5300 return NULL;
5301 }
5302 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5303 enum class symbol_linkage {
5304 unknown,
5305 static_,
5306 extern_,
23c13d42 5307 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5308 dwarf2_per_cu_data *per_cu = NULL;
5309 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5310 {
5311 ULONGEST ull;
5312 switch (attr.form)
5313 {
5314 case DW_FORM_implicit_const:
5315 ull = attr.implicit_const;
5316 break;
5317 case DW_FORM_flag_present:
5318 ull = 1;
5319 break;
5320 case DW_FORM_udata:
5321 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5322 m_addr += bytes_read;
5323 break;
5324 default:
b98664d3 5325 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5326 dwarf_form_name (attr.form),
ed2dc618 5327 objfile_name (objfile));
927aa2e7
JK
5328 return NULL;
5329 }
5330 switch (attr.dw_idx)
5331 {
5332 case DW_IDX_compile_unit:
5333 /* Don't crash on bad data. */
b76e467d 5334 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5335 {
b98664d3 5336 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5337 " [in module %s]"),
5338 pulongest (ull),
5339 objfile_name (dwarf2_per_objfile->objfile));
5340 continue;
5341 }
ff4c9fec 5342 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5343 break;
8af5c486
JK
5344 case DW_IDX_type_unit:
5345 /* Don't crash on bad data. */
b2bdb8cf 5346 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5347 {
b98664d3 5348 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5349 " [in module %s]"),
5350 pulongest (ull),
5351 objfile_name (dwarf2_per_objfile->objfile));
5352 continue;
5353 }
ff4c9fec 5354 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5355 break;
927aa2e7
JK
5356 case DW_IDX_GNU_internal:
5357 if (!m_map.augmentation_is_gdb)
5358 break;
23c13d42 5359 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5360 break;
5361 case DW_IDX_GNU_external:
5362 if (!m_map.augmentation_is_gdb)
5363 break;
23c13d42 5364 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5365 break;
5366 }
5367 }
5368
5369 /* Skip if already read in. */
5370 if (per_cu->v.quick->compunit_symtab)
5371 goto again;
5372
5373 /* Check static vs global. */
23c13d42 5374 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5375 {
2b79f376 5376 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5377 const bool symbol_is_static =
5378 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5379 if (want_static != symbol_is_static)
2b79f376 5380 goto again;
927aa2e7
JK
5381 }
5382
5383 /* Match dw2_symtab_iter_next, symbol_kind
5384 and debug_names::psymbol_tag. */
5385 switch (m_domain)
5386 {
5387 case VAR_DOMAIN:
5388 switch (indexval.dwarf_tag)
5389 {
5390 case DW_TAG_variable:
5391 case DW_TAG_subprogram:
5392 /* Some types are also in VAR_DOMAIN. */
5393 case DW_TAG_typedef:
5394 case DW_TAG_structure_type:
5395 break;
5396 default:
5397 goto again;
5398 }
5399 break;
5400 case STRUCT_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_typedef:
5404 case DW_TAG_structure_type:
5405 break;
5406 default:
5407 goto again;
5408 }
5409 break;
5410 case LABEL_DOMAIN:
5411 switch (indexval.dwarf_tag)
5412 {
5413 case 0:
5414 case DW_TAG_variable:
5415 break;
5416 default:
5417 goto again;
5418 }
5419 break;
59c35742
AB
5420 case MODULE_DOMAIN:
5421 switch (indexval.dwarf_tag)
5422 {
5423 case DW_TAG_module:
5424 break;
5425 default:
5426 goto again;
5427 }
5428 break;
927aa2e7
JK
5429 default:
5430 break;
5431 }
5432
5433 /* Match dw2_expand_symtabs_matching, symbol_kind and
5434 debug_names::psymbol_tag. */
5435 switch (m_search)
4b514bc8 5436 {
927aa2e7
JK
5437 case VARIABLES_DOMAIN:
5438 switch (indexval.dwarf_tag)
4b514bc8 5439 {
927aa2e7
JK
5440 case DW_TAG_variable:
5441 break;
5442 default:
5443 goto again;
4b514bc8 5444 }
927aa2e7
JK
5445 break;
5446 case FUNCTIONS_DOMAIN:
5447 switch (indexval.dwarf_tag)
4b514bc8 5448 {
927aa2e7
JK
5449 case DW_TAG_subprogram:
5450 break;
5451 default:
5452 goto again;
4b514bc8 5453 }
927aa2e7
JK
5454 break;
5455 case TYPES_DOMAIN:
5456 switch (indexval.dwarf_tag)
5457 {
5458 case DW_TAG_typedef:
5459 case DW_TAG_structure_type:
5460 break;
5461 default:
5462 goto again;
5463 }
5464 break;
59c35742
AB
5465 case MODULES_DOMAIN:
5466 switch (indexval.dwarf_tag)
5467 {
5468 case DW_TAG_module:
5469 break;
5470 default:
5471 goto again;
5472 }
927aa2e7
JK
5473 default:
5474 break;
4b514bc8 5475 }
927aa2e7
JK
5476
5477 return per_cu;
4b514bc8 5478}
61920122 5479
927aa2e7 5480static struct compunit_symtab *
c7f839cb 5481dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5482 const char *name, domain_enum domain)
4b514bc8 5483{
ed2dc618
SM
5484 struct dwarf2_per_objfile *dwarf2_per_objfile
5485 = get_dwarf2_per_objfile (objfile);
61920122 5486
927aa2e7
JK
5487 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5488 if (!mapp)
61920122 5489 {
927aa2e7
JK
5490 /* index is NULL if OBJF_READNOW. */
5491 return NULL;
5492 }
5493 const auto &map = *mapp;
9291a0cd 5494
2b79f376 5495 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5496
927aa2e7
JK
5497 struct compunit_symtab *stab_best = NULL;
5498 struct dwarf2_per_cu_data *per_cu;
5499 while ((per_cu = iter.next ()) != NULL)
5500 {
5501 struct symbol *sym, *with_opaque = NULL;
58f0c718 5502 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5503 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5504 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5505
927aa2e7
JK
5506 sym = block_find_symbol (block, name, domain,
5507 block_find_non_opaque_type_preferred,
5508 &with_opaque);
9703b513 5509
927aa2e7
JK
5510 /* Some caution must be observed with overloaded functions and
5511 methods, since the index will not contain any overload
5512 information (but NAME might contain it). */
a3ec0bb1 5513
927aa2e7 5514 if (sym != NULL
987012b8 5515 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5516 return stab;
5517 if (with_opaque != NULL
987012b8 5518 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5519 stab_best = stab;
9703b513 5520
927aa2e7 5521 /* Keep looking through other CUs. */
9703b513
TT
5522 }
5523
927aa2e7 5524 return stab_best;
9703b513
TT
5525}
5526
927aa2e7
JK
5527/* This dumps minimal information about .debug_names. It is called
5528 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5529 uses this to verify that .debug_names has been loaded. */
9291a0cd 5530
927aa2e7
JK
5531static void
5532dw2_debug_names_dump (struct objfile *objfile)
5533{
ed2dc618
SM
5534 struct dwarf2_per_objfile *dwarf2_per_objfile
5535 = get_dwarf2_per_objfile (objfile);
5536
927aa2e7
JK
5537 gdb_assert (dwarf2_per_objfile->using_index);
5538 printf_filtered (".debug_names:");
5539 if (dwarf2_per_objfile->debug_names_table)
5540 printf_filtered (" exists\n");
5541 else
5542 printf_filtered (" faked for \"readnow\"\n");
5543 printf_filtered ("\n");
9291a0cd
TT
5544}
5545
9291a0cd 5546static void
927aa2e7
JK
5547dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5548 const char *func_name)
9291a0cd 5549{
ed2dc618
SM
5550 struct dwarf2_per_objfile *dwarf2_per_objfile
5551 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5552
927aa2e7
JK
5553 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5554 if (dwarf2_per_objfile->debug_names_table)
24c79950 5555 {
927aa2e7 5556 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5557
2b79f376 5558 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5559
927aa2e7
JK
5560 struct dwarf2_per_cu_data *per_cu;
5561 while ((per_cu = iter.next ()) != NULL)
58f0c718 5562 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5563 }
5564}
24c79950 5565
3b00ef10
TT
5566static void
5567dw2_debug_names_map_matching_symbols
5568 (struct objfile *objfile,
5569 const lookup_name_info &name, domain_enum domain,
5570 int global,
5571 gdb::function_view<symbol_found_callback_ftype> callback,
5572 symbol_compare_ftype *ordered_compare)
5573{
5574 struct dwarf2_per_objfile *dwarf2_per_objfile
5575 = get_dwarf2_per_objfile (objfile);
5576
5577 /* debug_names_table is NULL if OBJF_READNOW. */
5578 if (!dwarf2_per_objfile->debug_names_table)
5579 return;
5580
5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5582 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5583
5584 const char *match_name = name.ada ().lookup_name ().c_str ();
5585 auto matcher = [&] (const char *symname)
5586 {
5587 if (ordered_compare == nullptr)
5588 return true;
5589 return ordered_compare (symname, match_name) == 0;
5590 };
5591
5592 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5593 [&] (offset_type namei)
5594 {
5595 /* The name was matched, now expand corresponding CUs that were
5596 marked. */
5597 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5598
5599 struct dwarf2_per_cu_data *per_cu;
5600 while ((per_cu = iter.next ()) != NULL)
5601 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5602 return true;
5603 });
5604
5605 /* It's a shame we couldn't do this inside the
5606 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5607 that have already been expanded. Instead, this loop matches what
5608 the psymtab code does. */
5609 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5610 {
5611 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5612 if (cust != nullptr)
5613 {
5614 const struct block *block
5615 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5616 if (!iterate_over_symbols_terminated (block, name,
5617 domain, callback))
5618 break;
5619 }
5620 }
5621}
5622
927aa2e7
JK
5623static void
5624dw2_debug_names_expand_symtabs_matching
5625 (struct objfile *objfile,
5626 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5627 const lookup_name_info *lookup_name,
927aa2e7
JK
5628 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5629 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5630 enum search_domain kind)
5631{
ed2dc618
SM
5632 struct dwarf2_per_objfile *dwarf2_per_objfile
5633 = get_dwarf2_per_objfile (objfile);
9291a0cd 5634
927aa2e7
JK
5635 /* debug_names_table is NULL if OBJF_READNOW. */
5636 if (!dwarf2_per_objfile->debug_names_table)
5637 return;
9291a0cd 5638
ed2dc618 5639 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5640
c1a66c06
TV
5641 if (symbol_matcher == NULL && lookup_name == NULL)
5642 {
5643 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5644 {
5645 QUIT;
5646
5647 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5648 expansion_notify);
5649 }
5650 return;
5651 }
5652
44ed8f3e 5653 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5654
c1a66c06 5655 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5656 symbol_matcher,
5657 kind, [&] (offset_type namei)
927aa2e7 5658 {
927aa2e7
JK
5659 /* The name was matched, now expand corresponding CUs that were
5660 marked. */
5661 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5662
927aa2e7
JK
5663 struct dwarf2_per_cu_data *per_cu;
5664 while ((per_cu = iter.next ()) != NULL)
5665 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5666 expansion_notify);
3b00ef10 5667 return true;
44ed8f3e 5668 });
9291a0cd
TT
5669}
5670
927aa2e7 5671const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5672{
5673 dw2_has_symbols,
5674 dw2_find_last_source_symtab,
5675 dw2_forget_cached_source_info,
f8eba3c6 5676 dw2_map_symtabs_matching_filename,
927aa2e7 5677 dw2_debug_names_lookup_symbol,
d3214198 5678 NULL,
9291a0cd 5679 dw2_print_stats,
927aa2e7 5680 dw2_debug_names_dump,
927aa2e7 5681 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5682 dw2_expand_all_symtabs,
652a8996 5683 dw2_expand_symtabs_with_fullname,
3b00ef10 5684 dw2_debug_names_map_matching_symbols,
927aa2e7 5685 dw2_debug_names_expand_symtabs_matching,
43f3e411 5686 dw2_find_pc_sect_compunit_symtab,
71a3c369 5687 NULL,
9291a0cd
TT
5688 dw2_map_symbol_filenames
5689};
5690
4485a1c1
SM
5691/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5692 to either a dwarf2_per_objfile or dwz_file object. */
5693
5694template <typename T>
5695static gdb::array_view<const gdb_byte>
5696get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5697{
5698 dwarf2_section_info *section = &section_owner->gdb_index;
5699
96b79293 5700 if (section->empty ())
4485a1c1
SM
5701 return {};
5702
5703 /* Older elfutils strip versions could keep the section in the main
5704 executable while splitting it for the separate debug info file. */
96b79293 5705 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5706 return {};
5707
96b79293 5708 section->read (obj);
4485a1c1 5709
8bebfcda
PA
5710 /* dwarf2_section_info::size is a bfd_size_type, while
5711 gdb::array_view works with size_t. On 32-bit hosts, with
5712 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5713 is 32-bit. So we need an explicit narrowing conversion here.
5714 This is fine, because it's impossible to allocate or mmap an
5715 array/buffer larger than what size_t can represent. */
5716 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5717}
5718
87d6a7aa
SM
5719/* Lookup the index cache for the contents of the index associated to
5720 DWARF2_OBJ. */
5721
5722static gdb::array_view<const gdb_byte>
5723get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5724{
5725 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5726 if (build_id == nullptr)
5727 return {};
5728
5729 return global_index_cache.lookup_gdb_index (build_id,
5730 &dwarf2_obj->index_cache_res);
5731}
5732
5733/* Same as the above, but for DWZ. */
5734
5735static gdb::array_view<const gdb_byte>
5736get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5737{
5738 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5739 if (build_id == nullptr)
5740 return {};
5741
5742 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5743}
5744
3c0aa29a 5745/* See symfile.h. */
9291a0cd 5746
3c0aa29a
PA
5747bool
5748dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5749{
ed2dc618
SM
5750 struct dwarf2_per_objfile *dwarf2_per_objfile
5751 = get_dwarf2_per_objfile (objfile);
5752
9291a0cd
TT
5753 /* If we're about to read full symbols, don't bother with the
5754 indices. In this case we also don't care if some other debug
5755 format is making psymtabs, because they are all about to be
5756 expanded anyway. */
5757 if ((objfile->flags & OBJF_READNOW))
5758 {
9291a0cd 5759 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5760 create_all_comp_units (dwarf2_per_objfile);
5761 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5762 dwarf2_per_objfile->quick_file_names_table
5763 = create_quick_file_names_table
5764 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5765
b76e467d 5766 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5767 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5768 {
ff4c9fec 5769 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5770
e254ef6a
DE
5771 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5772 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5773 }
5774
5775 /* Return 1 so that gdb sees the "quick" functions. However,
5776 these functions will be no-ops because we will have expanded
5777 all symtabs. */
3c0aa29a
PA
5778 *index_kind = dw_index_kind::GDB_INDEX;
5779 return true;
9291a0cd
TT
5780 }
5781
ed2dc618 5782 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5783 {
5784 *index_kind = dw_index_kind::DEBUG_NAMES;
5785 return true;
5786 }
927aa2e7 5787
4485a1c1
SM
5788 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5789 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5790 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5791 {
5792 *index_kind = dw_index_kind::GDB_INDEX;
5793 return true;
5794 }
9291a0cd 5795
87d6a7aa
SM
5796 /* ... otherwise, try to find the index in the index cache. */
5797 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5798 get_gdb_index_contents_from_cache,
5799 get_gdb_index_contents_from_cache_dwz))
5800 {
5801 global_index_cache.hit ();
5802 *index_kind = dw_index_kind::GDB_INDEX;
5803 return true;
5804 }
5805
5806 global_index_cache.miss ();
3c0aa29a 5807 return false;
9291a0cd
TT
5808}
5809
5810\f
5811
dce234bc
PP
5812/* Build a partial symbol table. */
5813
5814void
f29dff0a 5815dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5816{
ed2dc618
SM
5817 struct dwarf2_per_objfile *dwarf2_per_objfile
5818 = get_dwarf2_per_objfile (objfile);
c9bf0622 5819
6eee24ce 5820 init_psymbol_list (objfile, 1024);
c906108c 5821
a70b8144 5822 try
c9bf0622
TT
5823 {
5824 /* This isn't really ideal: all the data we allocate on the
5825 objfile's obstack is still uselessly kept around. However,
5826 freeing it seems unsafe. */
906768f9 5827 psymtab_discarder psymtabs (objfile);
ed2dc618 5828 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5829 psymtabs.keep ();
87d6a7aa
SM
5830
5831 /* (maybe) store an index in the cache. */
5832 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5833 }
230d2906 5834 catch (const gdb_exception_error &except)
492d29ea
PA
5835 {
5836 exception_print (gdb_stderr, except);
5837 }
c906108c 5838}
c906108c 5839
3b80fe9b
DE
5840/* Find the base address of the compilation unit for range lists and
5841 location lists. It will normally be specified by DW_AT_low_pc.
5842 In DWARF-3 draft 4, the base address could be overridden by
5843 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5844 compilation units with discontinuous ranges. */
5845
5846static void
5847dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5848{
5849 struct attribute *attr;
5850
2b24b6e4 5851 cu->base_address.reset ();
3b80fe9b
DE
5852
5853 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5854 if (attr != nullptr)
2b24b6e4 5855 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5856 else
5857 {
5858 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5859 if (attr != nullptr)
2b24b6e4 5860 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5861 }
5862}
5863
36586728
TT
5864/* Helper function that returns the proper abbrev section for
5865 THIS_CU. */
5866
5867static struct dwarf2_section_info *
5868get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5869{
5870 struct dwarf2_section_info *abbrev;
ed2dc618 5871 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5872
5873 if (this_cu->is_dwz)
ed2dc618 5874 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5875 else
5876 abbrev = &dwarf2_per_objfile->abbrev;
5877
5878 return abbrev;
5879}
5880
f4dc4d17
DE
5881/* Fetch the abbreviation table offset from a comp or type unit header. */
5882
5883static sect_offset
ed2dc618
SM
5884read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5885 struct dwarf2_section_info *section,
9c541725 5886 sect_offset sect_off)
f4dc4d17 5887{
96b79293 5888 bfd *abfd = section->get_bfd_owner ();
d521ce57 5889 const gdb_byte *info_ptr;
ac298888 5890 unsigned int initial_length_size, offset_size;
43988095 5891 uint16_t version;
f4dc4d17 5892
96b79293 5893 section->read (dwarf2_per_objfile->objfile);
9c541725 5894 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5895 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5896 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5897 info_ptr += initial_length_size;
5898
5899 version = read_2_bytes (abfd, info_ptr);
5900 info_ptr += 2;
5901 if (version >= 5)
5902 {
5903 /* Skip unit type and address size. */
5904 info_ptr += 2;
5905 }
5906
24aa364d 5907 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5908}
5909
b83470bf
TT
5910/* A partial symtab that is used only for include files. */
5911struct dwarf2_include_psymtab : public partial_symtab
5912{
5913 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5914 : partial_symtab (filename, objfile)
5915 {
5916 }
5917
5918 void read_symtab (struct objfile *objfile) override
5919 {
194d088f
TV
5920 /* It's an include file, no symbols to read for it.
5921 Everything is in the includer symtab. */
5922
5923 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5924 expansion of the includer psymtab. We use the dependencies[0] field to
5925 model the includer. But if we go the regular route of calling
5926 expand_psymtab here, and having expand_psymtab call expand_dependencies
5927 to expand the includer, we'll only use expand_psymtab on the includer
5928 (making it a non-toplevel psymtab), while if we expand the includer via
5929 another path, we'll use read_symtab (making it a toplevel psymtab).
5930 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5931 psymtab, and trigger read_symtab on the includer here directly. */
5932 includer ()->read_symtab (objfile);
b83470bf
TT
5933 }
5934
5935 void expand_psymtab (struct objfile *objfile) override
5936 {
194d088f
TV
5937 /* This is not called by read_symtab, and should not be called by any
5938 expand_dependencies. */
5939 gdb_assert (false);
b83470bf
TT
5940 }
5941
5942 bool readin_p () const override
5943 {
194d088f 5944 return includer ()->readin_p ();
b83470bf
TT
5945 }
5946
5947 struct compunit_symtab *get_compunit_symtab () const override
5948 {
5949 return nullptr;
5950 }
5951
5952private:
194d088f
TV
5953 partial_symtab *includer () const
5954 {
5955 /* An include psymtab has exactly one dependency: the psymtab that
5956 includes it. */
5957 gdb_assert (this->number_of_dependencies == 1);
5958 return this->dependencies[0];
5959 }
b83470bf
TT
5960};
5961
aaa75496
JB
5962/* Allocate a new partial symtab for file named NAME and mark this new
5963 partial symtab as being an include of PST. */
5964
5965static void
891813be 5966dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5967 struct objfile *objfile)
5968{
b83470bf 5969 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5970
fbd9ab74
JK
5971 if (!IS_ABSOLUTE_PATH (subpst->filename))
5972 {
5973 /* It shares objfile->objfile_obstack. */
5974 subpst->dirname = pst->dirname;
5975 }
5976
a9342b62 5977 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5978 subpst->dependencies[0] = pst;
5979 subpst->number_of_dependencies = 1;
aaa75496
JB
5980}
5981
5982/* Read the Line Number Program data and extract the list of files
5983 included by the source file represented by PST. Build an include
d85a05f0 5984 partial symtab for each of these included files. */
aaa75496
JB
5985
5986static void
5987dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5988 struct die_info *die,
891813be 5989 dwarf2_psymtab *pst)
aaa75496 5990{
fff8551c 5991 line_header_up lh;
d85a05f0 5992 struct attribute *attr;
aaa75496 5993
d85a05f0 5994 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5995 if (attr != nullptr)
9c541725 5996 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5997 if (lh == NULL)
5998 return; /* No linetable, so no includes. */
5999
79748972
TT
6000 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6001 that we pass in the raw text_low here; that is ok because we're
6002 only decoding the line table to make include partial symtabs, and
6003 so the addresses aren't really used. */
4ae976d1 6004 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6005 pst->raw_text_low (), 1);
aaa75496
JB
6006}
6007
348e048f 6008static hashval_t
52dc124a 6009hash_signatured_type (const void *item)
348e048f 6010{
9a3c8263
SM
6011 const struct signatured_type *sig_type
6012 = (const struct signatured_type *) item;
9a619af0 6013
348e048f 6014 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6015 return sig_type->signature;
348e048f
DE
6016}
6017
6018static int
52dc124a 6019eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6020{
9a3c8263
SM
6021 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6022 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6023
348e048f
DE
6024 return lhs->signature == rhs->signature;
6025}
6026
1fd400ff
TT
6027/* Allocate a hash table for signatured types. */
6028
b0b6a987 6029static htab_up
298e9637 6030allocate_signatured_type_table ()
1fd400ff 6031{
b0b6a987
TT
6032 return htab_up (htab_create_alloc (41,
6033 hash_signatured_type,
6034 eq_signatured_type,
6035 NULL, xcalloc, xfree));
1fd400ff
TT
6036}
6037
d467dd73 6038/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6039
6040static int
d467dd73 6041add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6042{
9a3c8263 6043 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6044 std::vector<signatured_type *> *all_type_units
6045 = (std::vector<signatured_type *> *) datum;
1fd400ff 6046
b2bdb8cf 6047 all_type_units->push_back (sigt);
1fd400ff
TT
6048
6049 return 1;
6050}
6051
78d4d2c5 6052/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6053 and fill them into TYPES_HTAB. It will process only type units,
6054 therefore DW_UT_type. */
c88ee1f0 6055
78d4d2c5 6056static void
ed2dc618
SM
6057create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwo_file *dwo_file,
b0b6a987 6059 dwarf2_section_info *section, htab_up &types_htab,
43988095 6060 rcuh_kind section_kind)
348e048f 6061{
3019eac3 6062 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6063 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6064 bfd *abfd;
6065 const gdb_byte *info_ptr, *end_ptr;
348e048f 6066
4bdcc0c1
DE
6067 abbrev_section = (dwo_file != NULL
6068 ? &dwo_file->sections.abbrev
6069 : &dwarf2_per_objfile->abbrev);
6070
b4f54984 6071 if (dwarf_read_debug)
43988095 6072 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6073 section->get_name (),
6074 abbrev_section->get_file_name ());
09406207 6075
96b79293 6076 section->read (objfile);
78d4d2c5 6077 info_ptr = section->buffer;
348e048f 6078
78d4d2c5
JK
6079 if (info_ptr == NULL)
6080 return;
348e048f 6081
78d4d2c5
JK
6082 /* We can't set abfd until now because the section may be empty or
6083 not present, in which case the bfd is unknown. */
96b79293 6084 abfd = section->get_bfd_owner ();
348e048f 6085
c0ab21c2
TT
6086 /* We don't use cutu_reader here because we don't need to read
6087 any dies: the signature is in the header. */
3019eac3 6088
78d4d2c5
JK
6089 end_ptr = info_ptr + section->size;
6090 while (info_ptr < end_ptr)
6091 {
78d4d2c5
JK
6092 struct signatured_type *sig_type;
6093 struct dwo_unit *dwo_tu;
6094 void **slot;
6095 const gdb_byte *ptr = info_ptr;
6096 struct comp_unit_head header;
6097 unsigned int length;
8b70b953 6098
9c541725 6099 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6100
a49dd8dd
JK
6101 /* Initialize it due to a false compiler warning. */
6102 header.signature = -1;
9c541725 6103 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6104
78d4d2c5
JK
6105 /* We need to read the type's signature in order to build the hash
6106 table, but we don't need anything else just yet. */
348e048f 6107
ed2dc618 6108 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6109 abbrev_section, ptr, section_kind);
348e048f 6110
4057dfde 6111 length = header.get_length ();
6caca83c 6112
78d4d2c5
JK
6113 /* Skip dummy type units. */
6114 if (ptr >= info_ptr + length
43988095
JK
6115 || peek_abbrev_code (abfd, ptr) == 0
6116 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6117 {
6118 info_ptr += length;
6119 continue;
6120 }
dee91e82 6121
78d4d2c5
JK
6122 if (types_htab == NULL)
6123 {
6124 if (dwo_file)
298e9637 6125 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6126 else
298e9637 6127 types_htab = allocate_signatured_type_table ();
78d4d2c5 6128 }
8b70b953 6129
78d4d2c5
JK
6130 if (dwo_file)
6131 {
6132 sig_type = NULL;
6133 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6134 struct dwo_unit);
6135 dwo_tu->dwo_file = dwo_file;
43988095 6136 dwo_tu->signature = header.signature;
9c541725 6137 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6138 dwo_tu->section = section;
9c541725 6139 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6140 dwo_tu->length = length;
6141 }
6142 else
6143 {
6144 /* N.B.: type_offset is not usable if this type uses a DWO file.
6145 The real type_offset is in the DWO file. */
6146 dwo_tu = NULL;
6147 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6148 struct signatured_type);
43988095 6149 sig_type->signature = header.signature;
9c541725 6150 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6151 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6152 sig_type->per_cu.is_debug_types = 1;
6153 sig_type->per_cu.section = section;
9c541725 6154 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6155 sig_type->per_cu.length = length;
6156 }
6157
b0b6a987 6158 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6159 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6160 INSERT);
6161 gdb_assert (slot != NULL);
6162 if (*slot != NULL)
6163 {
9c541725 6164 sect_offset dup_sect_off;
0349ea22 6165
3019eac3
DE
6166 if (dwo_file)
6167 {
78d4d2c5
JK
6168 const struct dwo_unit *dup_tu
6169 = (const struct dwo_unit *) *slot;
6170
9c541725 6171 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6172 }
6173 else
6174 {
78d4d2c5
JK
6175 const struct signatured_type *dup_tu
6176 = (const struct signatured_type *) *slot;
6177
9c541725 6178 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6179 }
8b70b953 6180
b98664d3 6181 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6182 " the entry at offset %s, signature %s"),
6183 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6184 hex_string (header.signature));
78d4d2c5
JK
6185 }
6186 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6187
78d4d2c5 6188 if (dwarf_read_debug > 1)
9d8780f0
SM
6189 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6190 sect_offset_str (sect_off),
43988095 6191 hex_string (header.signature));
3019eac3 6192
78d4d2c5
JK
6193 info_ptr += length;
6194 }
6195}
3019eac3 6196
78d4d2c5
JK
6197/* Create the hash table of all entries in the .debug_types
6198 (or .debug_types.dwo) section(s).
6199 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6200 otherwise it is NULL.
b3c8eb43 6201
78d4d2c5 6202 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6203
78d4d2c5 6204 Note: This function processes DWO files only, not DWP files. */
348e048f 6205
78d4d2c5 6206static void
ed2dc618
SM
6207create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6208 struct dwo_file *dwo_file,
fd5866f6 6209 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6210 htab_up &types_htab)
78d4d2c5 6211{
fd5866f6
SM
6212 for (dwarf2_section_info &section : type_sections)
6213 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6214 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6215}
6216
6217/* Create the hash table of all entries in the .debug_types section,
6218 and initialize all_type_units.
6219 The result is zero if there is an error (e.g. missing .debug_types section),
6220 otherwise non-zero. */
6221
6222static int
ed2dc618 6223create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6224{
b0b6a987 6225 htab_up types_htab;
3019eac3 6226
ed2dc618
SM
6227 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6228 &dwarf2_per_objfile->info, types_htab,
43988095 6229 rcuh_kind::COMPILE);
ed2dc618
SM
6230 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6231 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6232 if (types_htab == NULL)
6233 {
6234 dwarf2_per_objfile->signatured_types = NULL;
6235 return 0;
6236 }
6237
b0b6a987 6238 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6239
b2bdb8cf 6240 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6241 dwarf2_per_objfile->all_type_units.reserve
6242 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6243
b0b6a987
TT
6244 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6245 add_signatured_type_cu_to_table,
b2bdb8cf 6246 &dwarf2_per_objfile->all_type_units);
1fd400ff 6247
348e048f
DE
6248 return 1;
6249}
6250
6aa5f3a6
DE
6251/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6252 If SLOT is non-NULL, it is the entry to use in the hash table.
6253 Otherwise we find one. */
6254
6255static struct signatured_type *
ed2dc618
SM
6256add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6257 void **slot)
6aa5f3a6
DE
6258{
6259 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6260
b2bdb8cf
SM
6261 if (dwarf2_per_objfile->all_type_units.size ()
6262 == dwarf2_per_objfile->all_type_units.capacity ())
6263 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6264
b2bdb8cf
SM
6265 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6266 struct signatured_type);
6267
6268 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6269 sig_type->signature = sig;
6270 sig_type->per_cu.is_debug_types = 1;
6271 if (dwarf2_per_objfile->using_index)
6272 {
6273 sig_type->per_cu.v.quick =
6274 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6275 struct dwarf2_per_cu_quick_data);
6276 }
6277
6278 if (slot == NULL)
6279 {
b0b6a987 6280 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6281 sig_type, INSERT);
6282 }
6283 gdb_assert (*slot == NULL);
6284 *slot = sig_type;
6285 /* The rest of sig_type must be filled in by the caller. */
6286 return sig_type;
6287}
6288
a2ce51a0
DE
6289/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6290 Fill in SIG_ENTRY with DWO_ENTRY. */
6291
6292static void
ed2dc618 6293fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6294 struct signatured_type *sig_entry,
6295 struct dwo_unit *dwo_entry)
6296{
7ee85ab1 6297 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6298 gdb_assert (! sig_entry->per_cu.queued);
6299 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6300 if (dwarf2_per_objfile->using_index)
6301 {
6302 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6303 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6304 }
6305 else
6306 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6307 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6308 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6309 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6310 gdb_assert (sig_entry->dwo_unit == NULL);
6311
6312 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6313 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6314 sig_entry->per_cu.length = dwo_entry->length;
6315 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6316 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6317 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6318 sig_entry->dwo_unit = dwo_entry;
6319}
6320
6321/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6322 If we haven't read the TU yet, create the signatured_type data structure
6323 for a TU to be read in directly from a DWO file, bypassing the stub.
6324 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6325 using .gdb_index, then when reading a CU we want to stay in the DWO file
6326 containing that CU. Otherwise we could end up reading several other DWO
6327 files (due to comdat folding) to process the transitive closure of all the
6328 mentioned TUs, and that can be slow. The current DWO file will have every
6329 type signature that it needs.
a2ce51a0
DE
6330 We only do this for .gdb_index because in the psymtab case we already have
6331 to read all the DWOs to build the type unit groups. */
6332
6333static struct signatured_type *
6334lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6335{
518817b3
SM
6336 struct dwarf2_per_objfile *dwarf2_per_objfile
6337 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6338 struct dwo_file *dwo_file;
6339 struct dwo_unit find_dwo_entry, *dwo_entry;
6340 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6341 void **slot;
a2ce51a0
DE
6342
6343 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6344
6aa5f3a6
DE
6345 /* If TU skeletons have been removed then we may not have read in any
6346 TUs yet. */
6347 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6348 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6349
6350 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6351 Use the global signatured_types array to do our own comdat-folding
6352 of types. If this is the first time we're reading this TU, and
6353 the TU has an entry in .gdb_index, replace the recorded data from
6354 .gdb_index with this TU. */
a2ce51a0 6355
a2ce51a0 6356 find_sig_entry.signature = sig;
b0b6a987 6357 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6358 &find_sig_entry, INSERT);
9a3c8263 6359 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6360
6361 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6362 read. Don't reassign the global entry to point to this DWO if that's
6363 the case. Also note that if the TU is already being read, it may not
6364 have come from a DWO, the program may be a mix of Fission-compiled
6365 code and non-Fission-compiled code. */
6366
6367 /* Have we already tried to read this TU?
6368 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6369 needn't exist in the global table yet). */
6370 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6371 return sig_entry;
6372
6aa5f3a6
DE
6373 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6374 dwo_unit of the TU itself. */
6375 dwo_file = cu->dwo_unit->dwo_file;
6376
a2ce51a0
DE
6377 /* Ok, this is the first time we're reading this TU. */
6378 if (dwo_file->tus == NULL)
6379 return NULL;
6380 find_dwo_entry.signature = sig;
b0b6a987
TT
6381 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6382 &find_dwo_entry);
a2ce51a0
DE
6383 if (dwo_entry == NULL)
6384 return NULL;
6385
6aa5f3a6
DE
6386 /* If the global table doesn't have an entry for this TU, add one. */
6387 if (sig_entry == NULL)
ed2dc618 6388 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6389
ed2dc618 6390 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6391 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6392 return sig_entry;
6393}
6394
a2ce51a0
DE
6395/* Subroutine of lookup_signatured_type.
6396 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6397 then try the DWP file. If the TU stub (skeleton) has been removed then
6398 it won't be in .gdb_index. */
a2ce51a0
DE
6399
6400static struct signatured_type *
6401lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6402{
518817b3
SM
6403 struct dwarf2_per_objfile *dwarf2_per_objfile
6404 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6405 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6406 struct dwo_unit *dwo_entry;
6407 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6408 void **slot;
a2ce51a0
DE
6409
6410 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6411 gdb_assert (dwp_file != NULL);
6412
6aa5f3a6
DE
6413 /* If TU skeletons have been removed then we may not have read in any
6414 TUs yet. */
6415 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6416 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6417
6aa5f3a6 6418 find_sig_entry.signature = sig;
b0b6a987 6419 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6420 &find_sig_entry, INSERT);
9a3c8263 6421 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6422
6423 /* Have we already tried to read this TU?
6424 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6425 needn't exist in the global table yet). */
6426 if (sig_entry != NULL)
6427 return sig_entry;
6428
a2ce51a0
DE
6429 if (dwp_file->tus == NULL)
6430 return NULL;
ed2dc618 6431 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6432 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6433 if (dwo_entry == NULL)
6434 return NULL;
6435
ed2dc618
SM
6436 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6437 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6438
a2ce51a0
DE
6439 return sig_entry;
6440}
6441
380bca97 6442/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6443 Returns NULL if signature SIG is not present in the table.
6444 It is up to the caller to complain about this. */
348e048f
DE
6445
6446static struct signatured_type *
a2ce51a0 6447lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6448{
518817b3
SM
6449 struct dwarf2_per_objfile *dwarf2_per_objfile
6450 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6451
a2ce51a0
DE
6452 if (cu->dwo_unit
6453 && dwarf2_per_objfile->using_index)
6454 {
6455 /* We're in a DWO/DWP file, and we're using .gdb_index.
6456 These cases require special processing. */
ed2dc618 6457 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6458 return lookup_dwo_signatured_type (cu, sig);
6459 else
6460 return lookup_dwp_signatured_type (cu, sig);
6461 }
6462 else
6463 {
6464 struct signatured_type find_entry, *entry;
348e048f 6465
a2ce51a0
DE
6466 if (dwarf2_per_objfile->signatured_types == NULL)
6467 return NULL;
6468 find_entry.signature = sig;
9a3c8263 6469 entry = ((struct signatured_type *)
b0b6a987
TT
6470 htab_find (dwarf2_per_objfile->signatured_types.get (),
6471 &find_entry));
a2ce51a0
DE
6472 return entry;
6473 }
348e048f 6474}
18a8505e 6475
42e7ad6c 6476/* Low level DIE reading support. */
348e048f 6477
d85a05f0
DJ
6478/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6479
6480static void
6481init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6482 struct dwarf2_cu *cu,
3019eac3 6483 struct dwarf2_section_info *section,
685af9cd
TT
6484 struct dwo_file *dwo_file,
6485 struct abbrev_table *abbrev_table)
d85a05f0 6486{
fceca515 6487 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6488 reader->abfd = section->get_bfd_owner ();
d85a05f0 6489 reader->cu = cu;
3019eac3 6490 reader->dwo_file = dwo_file;
dee91e82
DE
6491 reader->die_section = section;
6492 reader->buffer = section->buffer;
f664829e 6493 reader->buffer_end = section->buffer + section->size;
685af9cd 6494 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6495}
6496
c0ab21c2 6497/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6498 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6499 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6500 already.
6501
6502 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6503 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6504 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6505 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6506 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6507 STUB_COMP_DIR may be non-NULL.
3e225074 6508 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6509 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6510 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6511 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6512 kept around for at least as long as *RESULT_READER.
6513
b0c7bfa9
DE
6514 The result is non-zero if a valid (non-dummy) DIE was found. */
6515
6516static int
6517read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6518 struct dwo_unit *dwo_unit,
b0c7bfa9 6519 struct die_info *stub_comp_unit_die,
a2ce51a0 6520 const char *stub_comp_dir,
b0c7bfa9 6521 struct die_reader_specs *result_reader,
d521ce57 6522 const gdb_byte **result_info_ptr,
b0c7bfa9 6523 struct die_info **result_comp_unit_die,
685af9cd 6524 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6525{
ed2dc618 6526 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6527 struct objfile *objfile = dwarf2_per_objfile->objfile;
6528 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6529 bfd *abfd;
d521ce57 6530 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6531 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6532 int i,num_extra_attrs;
6533 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6534 struct die_info *comp_unit_die;
6535
b0aeadb3
DE
6536 /* At most one of these may be provided. */
6537 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6538
b0c7bfa9
DE
6539 /* These attributes aren't processed until later:
6540 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6541 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6542 referenced later. However, these attributes are found in the stub
6543 which we won't have later. In order to not impose this complication
6544 on the rest of the code, we read them here and copy them to the
6545 DWO CU/TU die. */
b0c7bfa9
DE
6546
6547 stmt_list = NULL;
6548 low_pc = NULL;
6549 high_pc = NULL;
6550 ranges = NULL;
6551 comp_dir = NULL;
6552
6553 if (stub_comp_unit_die != NULL)
6554 {
6555 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6556 DWO file. */
6557 if (! this_cu->is_debug_types)
6558 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6559 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6560 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6561 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6562 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6563
a39fdb41 6564 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6565
18a8505e
AT
6566 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6567 here (if needed). We need the value before we can process
6568 DW_AT_ranges. */
a39fdb41 6569 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6570 }
a2ce51a0
DE
6571 else if (stub_comp_dir != NULL)
6572 {
6573 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6574 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6575 comp_dir->name = DW_AT_comp_dir;
6576 comp_dir->form = DW_FORM_string;
6577 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6578 DW_STRING (comp_dir) = stub_comp_dir;
6579 }
b0c7bfa9
DE
6580
6581 /* Set up for reading the DWO CU/TU. */
6582 cu->dwo_unit = dwo_unit;
685af9cd 6583 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6584 section->read (objfile);
6585 abfd = section->get_bfd_owner ();
9c541725
PA
6586 begin_info_ptr = info_ptr = (section->buffer
6587 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6588 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6589
6590 if (this_cu->is_debug_types)
6591 {
b0c7bfa9
DE
6592 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6593
ed2dc618
SM
6594 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6595 &cu->header, section,
b0c7bfa9 6596 dwo_abbrev_section,
43988095 6597 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6598 /* This is not an assert because it can be caused by bad debug info. */
43988095 6599 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6600 {
6601 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6602 " TU at offset %s [in module %s]"),
a2ce51a0 6603 hex_string (sig_type->signature),
43988095 6604 hex_string (cu->header.signature),
9d8780f0 6605 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6606 bfd_get_filename (abfd));
6607 }
9c541725 6608 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6609 /* For DWOs coming from DWP files, we don't know the CU length
6610 nor the type's offset in the TU until now. */
4057dfde 6611 dwo_unit->length = cu->header.get_length ();
9c541725 6612 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6613
6614 /* Establish the type offset that can be used to lookup the type.
6615 For DWO files, we don't know it until now. */
9c541725
PA
6616 sig_type->type_offset_in_section
6617 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6618 }
6619 else
6620 {
ed2dc618
SM
6621 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6622 &cu->header, section,
b0c7bfa9 6623 dwo_abbrev_section,
43988095 6624 info_ptr, rcuh_kind::COMPILE);
9c541725 6625 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6626 /* For DWOs coming from DWP files, we don't know the CU length
6627 until now. */
4057dfde 6628 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6629 }
6630
685af9cd 6631 *result_dwo_abbrev_table
86de1d91
TT
6632 = abbrev_table::read (objfile, dwo_abbrev_section,
6633 cu->header.abbrev_sect_off);
685af9cd
TT
6634 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6635 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6636
6637 /* Read in the die, but leave space to copy over the attributes
6638 from the stub. This has the benefit of simplifying the rest of
6639 the code - all the work to maintain the illusion of a single
6640 DW_TAG_{compile,type}_unit DIE is done here. */
6641 num_extra_attrs = ((stmt_list != NULL)
6642 + (low_pc != NULL)
6643 + (high_pc != NULL)
6644 + (ranges != NULL)
6645 + (comp_dir != NULL));
6646 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6647 num_extra_attrs);
b0c7bfa9
DE
6648
6649 /* Copy over the attributes from the stub to the DIE we just read in. */
6650 comp_unit_die = *result_comp_unit_die;
6651 i = comp_unit_die->num_attrs;
6652 if (stmt_list != NULL)
6653 comp_unit_die->attrs[i++] = *stmt_list;
6654 if (low_pc != NULL)
6655 comp_unit_die->attrs[i++] = *low_pc;
6656 if (high_pc != NULL)
6657 comp_unit_die->attrs[i++] = *high_pc;
6658 if (ranges != NULL)
6659 comp_unit_die->attrs[i++] = *ranges;
6660 if (comp_dir != NULL)
6661 comp_unit_die->attrs[i++] = *comp_dir;
6662 comp_unit_die->num_attrs += num_extra_attrs;
6663
b4f54984 6664 if (dwarf_die_debug)
bf6af496
DE
6665 {
6666 fprintf_unfiltered (gdb_stdlog,
6667 "Read die from %s@0x%x of %s:\n",
96b79293 6668 section->get_name (),
bf6af496
DE
6669 (unsigned) (begin_info_ptr - section->buffer),
6670 bfd_get_filename (abfd));
b4f54984 6671 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6672 }
6673
b0c7bfa9
DE
6674 /* Skip dummy compilation units. */
6675 if (info_ptr >= begin_info_ptr + dwo_unit->length
6676 || peek_abbrev_code (abfd, info_ptr) == 0)
6677 return 0;
6678
6679 *result_info_ptr = info_ptr;
6680 return 1;
6681}
6682
a084a2a6
AT
6683/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6684 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6685 signature is part of the header. */
6686static gdb::optional<ULONGEST>
6687lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6688{
6689 if (cu->header.version >= 5)
6690 return cu->header.signature;
6691 struct attribute *attr;
6692 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6693 if (attr == nullptr)
6694 return gdb::optional<ULONGEST> ();
6695 return DW_UNSND (attr);
6696}
6697
c0ab21c2 6698/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6699 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6700 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6701
6702static struct dwo_unit *
6703lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6704 struct die_info *comp_unit_die,
6705 const char *dwo_name)
b0c7bfa9
DE
6706{
6707 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6708 struct dwo_unit *dwo_unit;
c0ab21c2 6709 const char *comp_dir;
b0c7bfa9 6710
a2ce51a0
DE
6711 gdb_assert (cu != NULL);
6712
b0c7bfa9 6713 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6714 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6715 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6716
6717 if (this_cu->is_debug_types)
6718 {
6719 struct signatured_type *sig_type;
6720
6721 /* Since this_cu is the first member of struct signatured_type,
6722 we can go from a pointer to one to a pointer to the other. */
6723 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6724 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6725 }
6726 else
6727 {
a084a2a6
AT
6728 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6729 if (!signature.has_value ())
b0c7bfa9
DE
6730 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6731 " [in module %s]"),
e3b94546 6732 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6733 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6734 *signature);
b0c7bfa9
DE
6735 }
6736
b0c7bfa9
DE
6737 return dwo_unit;
6738}
6739
c0ab21c2 6740/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6741 See it for a description of the parameters.
fcd3b13d 6742 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6743
c0ab21c2
TT
6744void
6745cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6746 int use_existing_cu)
a2ce51a0 6747{
a2ce51a0 6748 struct signatured_type *sig_type;
a2ce51a0
DE
6749
6750 /* Verify we can do the following downcast, and that we have the
6751 data we need. */
6752 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6753 sig_type = (struct signatured_type *) this_cu;
6754 gdb_assert (sig_type->dwo_unit != NULL);
6755
6aa5f3a6
DE
6756 if (use_existing_cu && this_cu->cu != NULL)
6757 {
6758 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6759 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6760 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6761 }
6762 else
6763 {
6764 /* If !use_existing_cu, this_cu->cu must be NULL. */
6765 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6766 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6767 }
6768
6769 /* A future optimization, if needed, would be to use an existing
6770 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6771 could share abbrev tables. */
a2ce51a0
DE
6772
6773 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6774 NULL /* stub_comp_unit_die */,
6775 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6776 this, &info_ptr,
3e225074 6777 &comp_unit_die,
c0ab21c2 6778 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6779 {
6780 /* Dummy die. */
c0ab21c2 6781 dummy_p = true;
a2ce51a0 6782 }
a2ce51a0
DE
6783}
6784
fd820528 6785/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6786 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6787
f4dc4d17
DE
6788 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6789 Otherwise the table specified in the comp unit header is read in and used.
6790 This is an optimization for when we already have the abbrev table.
6791
dee91e82 6792 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6793 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6794
c0ab21c2
TT
6795cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6796 struct abbrev_table *abbrev_table,
6751ebae 6797 int use_existing_cu,
c0ab21c2
TT
6798 bool skip_partial)
6799 : die_reader_specs {},
6751ebae 6800 m_this_cu (this_cu)
c906108c 6801{
ed2dc618 6802 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6803 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6804 struct dwarf2_section_info *section = this_cu->section;
96b79293 6805 bfd *abfd = section->get_bfd_owner ();
dee91e82 6806 struct dwarf2_cu *cu;
c0ab21c2 6807 const gdb_byte *begin_info_ptr;
dee91e82 6808 struct signatured_type *sig_type = NULL;
4bdcc0c1 6809 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6810 /* Non-zero if CU currently points to a DWO file and we need to
6811 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6812 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6813 int rereading_dwo_cu = 0;
c906108c 6814
b4f54984 6815 if (dwarf_die_debug)
9d8780f0 6816 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6817 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6818 sect_offset_str (this_cu->sect_off));
09406207 6819
a2ce51a0
DE
6820 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6821 file (instead of going through the stub), short-circuit all of this. */
6822 if (this_cu->reading_dwo_directly)
6823 {
6824 /* Narrow down the scope of possibilities to have to understand. */
6825 gdb_assert (this_cu->is_debug_types);
6826 gdb_assert (abbrev_table == NULL);
6751ebae 6827 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6828 return;
6829 }
6830
dee91e82 6831 /* This is cheap if the section is already read in. */
96b79293 6832 section->read (objfile);
dee91e82 6833
9c541725 6834 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6835
6836 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6837
6838 if (use_existing_cu && this_cu->cu != NULL)
6839 {
6840 cu = this_cu->cu;
42e7ad6c
DE
6841 /* If this CU is from a DWO file we need to start over, we need to
6842 refetch the attributes from the skeleton CU.
6843 This could be optimized by retrieving those attributes from when we
6844 were here the first time: the previous comp_unit_die was stored in
6845 comp_unit_obstack. But there's no data yet that we need this
6846 optimization. */
6847 if (cu->dwo_unit != NULL)
6848 rereading_dwo_cu = 1;
dee91e82
DE
6849 }
6850 else
6851 {
6852 /* If !use_existing_cu, this_cu->cu must be NULL. */
6853 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6854 m_new_cu.reset (new dwarf2_cu (this_cu));
6855 cu = m_new_cu.get ();
42e7ad6c 6856 }
dee91e82 6857
b0c7bfa9 6858 /* Get the header. */
9c541725 6859 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6860 {
6861 /* We already have the header, there's no need to read it in again. */
9c541725 6862 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6863 }
6864 else
6865 {
3019eac3 6866 if (this_cu->is_debug_types)
dee91e82 6867 {
ed2dc618
SM
6868 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6869 &cu->header, section,
4bdcc0c1 6870 abbrev_section, info_ptr,
43988095 6871 rcuh_kind::TYPE);
dee91e82 6872
42e7ad6c
DE
6873 /* Since per_cu is the first member of struct signatured_type,
6874 we can go from a pointer to one to a pointer to the other. */
6875 sig_type = (struct signatured_type *) this_cu;
43988095 6876 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6877 gdb_assert (sig_type->type_offset_in_tu
6878 == cu->header.type_cu_offset_in_tu);
6879 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6880
42e7ad6c
DE
6881 /* LENGTH has not been set yet for type units if we're
6882 using .gdb_index. */
4057dfde 6883 this_cu->length = cu->header.get_length ();
3019eac3
DE
6884
6885 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6886 sig_type->type_offset_in_section =
6887 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6888
6889 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6890 }
6891 else
6892 {
ed2dc618
SM
6893 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6894 &cu->header, section,
4bdcc0c1 6895 abbrev_section,
43988095
JK
6896 info_ptr,
6897 rcuh_kind::COMPILE);
dee91e82 6898
9c541725 6899 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6900 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6901 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6902 }
6903 }
10b3939b 6904
6caca83c 6905 /* Skip dummy compilation units. */
dee91e82 6906 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6907 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6908 {
6909 dummy_p = true;
6910 return;
6911 }
6caca83c 6912
433df2d4
DE
6913 /* If we don't have them yet, read the abbrevs for this compilation unit.
6914 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6915 done. */
f4dc4d17 6916 if (abbrev_table != NULL)
685af9cd
TT
6917 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6918 else
f4dc4d17 6919 {
c0ab21c2 6920 m_abbrev_table_holder
86de1d91
TT
6921 = abbrev_table::read (objfile, abbrev_section,
6922 cu->header.abbrev_sect_off);
c0ab21c2 6923 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6924 }
af703f96 6925
dee91e82 6926 /* Read the top level CU/TU die. */
c0ab21c2 6927 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6928 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6929
58f0c718 6930 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6931 {
6932 dummy_p = true;
6933 return;
6934 }
58f0c718 6935
b0c7bfa9 6936 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6937 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6938 table from the DWO file and pass the ownership over to us. It will be
6939 referenced from READER, so we must make sure to free it after we're done
6940 with READER.
6941
b0c7bfa9
DE
6942 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6943 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6944 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6945 if (dwo_name != nullptr)
3019eac3 6946 {
3019eac3 6947 struct dwo_unit *dwo_unit;
b0c7bfa9 6948 struct die_info *dwo_comp_unit_die;
3019eac3 6949
3e225074 6950 if (comp_unit_die->has_children)
6a506a2d 6951 {
b98664d3 6952 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6953 " has children (offset %s) [in module %s]"),
6954 sect_offset_str (this_cu->sect_off),
6955 bfd_get_filename (abfd));
6a506a2d 6956 }
c0ab21c2 6957 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6958 if (dwo_unit != NULL)
3019eac3 6959 {
6a506a2d 6960 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6961 comp_unit_die, NULL,
c0ab21c2 6962 this, &info_ptr,
3e225074 6963 &dwo_comp_unit_die,
c0ab21c2 6964 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6965 {
6966 /* Dummy die. */
c0ab21c2 6967 dummy_p = true;
6a506a2d
DE
6968 return;
6969 }
6970 comp_unit_die = dwo_comp_unit_die;
6971 }
6972 else
6973 {
6974 /* Yikes, we couldn't find the rest of the DIE, we only have
6975 the stub. A complaint has already been logged. There's
6976 not much more we can do except pass on the stub DIE to
6977 die_reader_func. We don't want to throw an error on bad
6978 debug info. */
3019eac3
DE
6979 }
6980 }
c0ab21c2 6981}
3019eac3 6982
6751ebae
TT
6983void
6984cutu_reader::keep ()
c0ab21c2 6985{
b0c7bfa9 6986 /* Done, clean up. */
6751ebae
TT
6987 gdb_assert (!dummy_p);
6988 if (m_new_cu != NULL)
348e048f 6989 {
c0ab21c2
TT
6990 struct dwarf2_per_objfile *dwarf2_per_objfile
6991 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6992 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6993 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6994 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6995 /* The chain owns it now. */
c0ab21c2 6996 m_new_cu.release ();
348e048f 6997 }
dee91e82
DE
6998}
6999
18a8505e
AT
7000/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7001 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7002 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7003
7004 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7005 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7006
7007 We fill in THIS_CU->length.
7008
dee91e82 7009 THIS_CU->cu is always freed when done.
3019eac3 7010 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7011 to care whether it refers to the "main" CU or the DWO CU.
7012
7013 When parent_cu is passed, it is used to provide a default value for
7014 str_offsets_base and addr_base from the parent. */
dee91e82 7015
c0ab21c2
TT
7016cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7017 struct dwarf2_cu *parent_cu,
7018 struct dwo_file *dwo_file)
7019 : die_reader_specs {},
7020 m_this_cu (this_cu)
dee91e82 7021{
ed2dc618 7022 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7023 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7024 struct dwarf2_section_info *section = this_cu->section;
96b79293 7025 bfd *abfd = section->get_bfd_owner ();
33e80786 7026 struct dwarf2_section_info *abbrev_section;
d521ce57 7027 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7028
b4f54984 7029 if (dwarf_die_debug)
9d8780f0 7030 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7031 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7032 sect_offset_str (this_cu->sect_off));
09406207 7033
dee91e82
DE
7034 gdb_assert (this_cu->cu == NULL);
7035
33e80786
DE
7036 abbrev_section = (dwo_file != NULL
7037 ? &dwo_file->sections.abbrev
7038 : get_abbrev_section_for_cu (this_cu));
7039
dee91e82 7040 /* This is cheap if the section is already read in. */
96b79293 7041 section->read (objfile);
dee91e82 7042
c0ab21c2 7043 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7044
9c541725 7045 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7046 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7047 &m_new_cu->header, section,
4bdcc0c1 7048 abbrev_section, info_ptr,
43988095
JK
7049 (this_cu->is_debug_types
7050 ? rcuh_kind::TYPE
7051 : rcuh_kind::COMPILE));
dee91e82 7052
18a8505e
AT
7053 if (parent_cu != nullptr)
7054 {
c0ab21c2
TT
7055 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7056 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7057 }
4057dfde 7058 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7059
7060 /* Skip dummy compilation units. */
7061 if (info_ptr >= begin_info_ptr + this_cu->length
7062 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7063 {
7064 dummy_p = true;
7065 return;
7066 }
72bf9492 7067
c0ab21c2 7068 m_abbrev_table_holder
86de1d91
TT
7069 = abbrev_table::read (objfile, abbrev_section,
7070 m_new_cu->header.abbrev_sect_off);
dee91e82 7071
c0ab21c2
TT
7072 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7073 m_abbrev_table_holder.get ());
3e225074 7074 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7075}
7076
0018ea6f
DE
7077\f
7078/* Type Unit Groups.
dee91e82 7079
0018ea6f
DE
7080 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7081 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7082 so that all types coming from the same compilation (.o file) are grouped
7083 together. A future step could be to put the types in the same symtab as
7084 the CU the types ultimately came from. */
ff013f42 7085
f4dc4d17
DE
7086static hashval_t
7087hash_type_unit_group (const void *item)
7088{
9a3c8263
SM
7089 const struct type_unit_group *tu_group
7090 = (const struct type_unit_group *) item;
f4dc4d17 7091
094b34ac 7092 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7093}
348e048f
DE
7094
7095static int
f4dc4d17 7096eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7097{
9a3c8263
SM
7098 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7099 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7100
094b34ac 7101 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7102}
348e048f 7103
f4dc4d17
DE
7104/* Allocate a hash table for type unit groups. */
7105
eaa5fa8b 7106static htab_up
298e9637 7107allocate_type_unit_groups_table ()
f4dc4d17 7108{
eaa5fa8b
TT
7109 return htab_up (htab_create_alloc (3,
7110 hash_type_unit_group,
7111 eq_type_unit_group,
7112 NULL, xcalloc, xfree));
f4dc4d17 7113}
dee91e82 7114
f4dc4d17
DE
7115/* Type units that don't have DW_AT_stmt_list are grouped into their own
7116 partial symtabs. We combine several TUs per psymtab to not let the size
7117 of any one psymtab grow too big. */
7118#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7119#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7120
094b34ac 7121/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7122 Create the type_unit_group object used to hold one or more TUs. */
7123
7124static struct type_unit_group *
094b34ac 7125create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7126{
518817b3
SM
7127 struct dwarf2_per_objfile *dwarf2_per_objfile
7128 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7129 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7130 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7131 struct type_unit_group *tu_group;
f4dc4d17
DE
7132
7133 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7134 struct type_unit_group);
094b34ac 7135 per_cu = &tu_group->per_cu;
518817b3 7136 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7137
094b34ac
DE
7138 if (dwarf2_per_objfile->using_index)
7139 {
7140 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7141 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7142 }
7143 else
7144 {
9c541725 7145 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7146 dwarf2_psymtab *pst;
528e1572 7147 std::string name;
094b34ac
DE
7148
7149 /* Give the symtab a useful name for debug purposes. */
7150 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7151 name = string_printf ("<type_units_%d>",
7152 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7153 else
528e1572 7154 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7155
528e1572 7156 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7157 pst->anonymous = true;
094b34ac 7158 }
f4dc4d17 7159
094b34ac 7160 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7161 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7162
7163 return tu_group;
7164}
7165
094b34ac
DE
7166/* Look up the type_unit_group for type unit CU, and create it if necessary.
7167 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7168
7169static struct type_unit_group *
ff39bb5e 7170get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7171{
518817b3
SM
7172 struct dwarf2_per_objfile *dwarf2_per_objfile
7173 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7174 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7175 struct type_unit_group *tu_group;
7176 void **slot;
7177 unsigned int line_offset;
7178 struct type_unit_group type_unit_group_for_lookup;
7179
7180 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7181 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7182
7183 /* Do we need to create a new group, or can we use an existing one? */
7184
7185 if (stmt_list)
7186 {
7187 line_offset = DW_UNSND (stmt_list);
7188 ++tu_stats->nr_symtab_sharers;
7189 }
7190 else
7191 {
7192 /* Ugh, no stmt_list. Rare, but we have to handle it.
7193 We can do various things here like create one group per TU or
7194 spread them over multiple groups to split up the expansion work.
7195 To avoid worst case scenarios (too many groups or too large groups)
7196 we, umm, group them in bunches. */
7197 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7198 | (tu_stats->nr_stmt_less_type_units
7199 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7200 ++tu_stats->nr_stmt_less_type_units;
7201 }
7202
094b34ac 7203 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7204 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7205 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7206 &type_unit_group_for_lookup, INSERT);
7207 if (*slot != NULL)
7208 {
9a3c8263 7209 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7210 gdb_assert (tu_group != NULL);
7211 }
7212 else
7213 {
9c541725 7214 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7215 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7216 *slot = tu_group;
7217 ++tu_stats->nr_symtabs;
7218 }
7219
7220 return tu_group;
7221}
0018ea6f
DE
7222\f
7223/* Partial symbol tables. */
7224
7225/* Create a psymtab named NAME and assign it to PER_CU.
7226
7227 The caller must fill in the following details:
7228 dirname, textlow, texthigh. */
7229
891813be 7230static dwarf2_psymtab *
0018ea6f
DE
7231create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7232{
e3b94546 7233 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7234 dwarf2_psymtab *pst;
0018ea6f 7235
9f4e76a4 7236 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7237
6d94535f 7238 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7239
7240 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7241 per_cu->v.psymtab = pst;
7242
7243 return pst;
7244}
7245
c0ab21c2 7246/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7247
7248static void
7249process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7250 const gdb_byte *info_ptr,
0018ea6f 7251 struct die_info *comp_unit_die,
c0ab21c2 7252 enum language pretend_language)
0018ea6f
DE
7253{
7254 struct dwarf2_cu *cu = reader->cu;
518817b3 7255 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7256 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7257 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7258 CORE_ADDR baseaddr;
7259 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7260 dwarf2_psymtab *pst;
3a2b436a 7261 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7262 const char *filename;
0018ea6f 7263
0018ea6f
DE
7264 gdb_assert (! per_cu->is_debug_types);
7265
c0ab21c2 7266 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7267
0018ea6f 7268 /* Allocate a new partial symbol table structure. */
2e927613
TV
7269 gdb::unique_xmalloc_ptr<char> debug_filename;
7270 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7271 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7272 if (filename == NULL)
0018ea6f 7273 filename = "";
2e927613
TV
7274 else if (strcmp (filename, artificial) == 0)
7275 {
7276 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7277 sect_offset_str (per_cu->sect_off),
7278 (char *) NULL));
2e927613
TV
7279 filename = debug_filename.get ();
7280 }
0018ea6f
DE
7281
7282 pst = create_partial_symtab (per_cu, filename);
7283
7284 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7285 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7286
b3b3bada 7287 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7288
7289 dwarf2_find_base_address (comp_unit_die, cu);
7290
7291 /* Possibly set the default values of LOWPC and HIGHPC from
7292 `DW_AT_ranges'. */
3a2b436a
JK
7293 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7294 &best_highpc, cu, pst);
7295 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7296 {
7297 CORE_ADDR low
7298 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7299 - baseaddr);
7300 CORE_ADDR high
7301 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7302 - baseaddr - 1);
7303 /* Store the contiguous range if it is not empty; it can be
7304 empty for CUs with no code. */
d320c2b5
TT
7305 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7306 low, high, pst);
79748972 7307 }
0018ea6f
DE
7308
7309 /* Check if comp unit has_children.
7310 If so, read the rest of the partial symbols from this comp unit.
7311 If not, there's no more debug_info for this comp unit. */
3e225074 7312 if (comp_unit_die->has_children)
0018ea6f
DE
7313 {
7314 struct partial_die_info *first_die;
7315 CORE_ADDR lowpc, highpc;
7316
7317 lowpc = ((CORE_ADDR) -1);
7318 highpc = ((CORE_ADDR) 0);
7319
7320 first_die = load_partial_dies (reader, info_ptr, 1);
7321
7322 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7323 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7324
7325 /* If we didn't find a lowpc, set it to highpc to avoid
7326 complaints from `maint check'. */
7327 if (lowpc == ((CORE_ADDR) -1))
7328 lowpc = highpc;
7329
7330 /* If the compilation unit didn't have an explicit address range,
7331 then use the information extracted from its child dies. */
e385593e 7332 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7333 {
7334 best_lowpc = lowpc;
7335 best_highpc = highpc;
7336 }
7337 }
4ae976d1 7338 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7339 best_lowpc + baseaddr)
7340 - baseaddr);
4ae976d1 7341 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7342 best_highpc + baseaddr)
7343 - baseaddr);
0018ea6f 7344
8763cede 7345 end_psymtab_common (objfile, pst);
0018ea6f 7346
ae640021 7347 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7348 {
7349 int i;
ae640021 7350 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7351
7352 /* Fill in 'dependencies' here; we fill in 'users' in a
7353 post-pass. */
7354 pst->number_of_dependencies = len;
a9342b62
TT
7355 pst->dependencies
7356 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7357 for (i = 0; i < len; ++i)
7358 {
7359 pst->dependencies[i]
7360 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7361 }
0018ea6f 7362
ae640021 7363 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7364 }
7365
7366 /* Get the list of files included in the current compilation unit,
7367 and build a psymtab for each of them. */
7368 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7369
b4f54984 7370 if (dwarf_read_debug)
b926417a
TT
7371 fprintf_unfiltered (gdb_stdlog,
7372 "Psymtab for %s unit @%s: %s - %s"
7373 ", %d global, %d static syms\n",
7374 per_cu->is_debug_types ? "type" : "comp",
7375 sect_offset_str (per_cu->sect_off),
7376 paddress (gdbarch, pst->text_low (objfile)),
7377 paddress (gdbarch, pst->text_high (objfile)),
7378 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7379}
7380
7381/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7382 Process compilation unit THIS_CU for a psymtab. */
7383
7384static void
7385process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7386 bool want_partial_unit,
b93601f3 7387 enum language pretend_language)
0018ea6f
DE
7388{
7389 /* If this compilation unit was already read in, free the
7390 cached copy in order to read it in again. This is
7391 necessary because we skipped some symbols when we first
7392 read in the compilation unit (see load_partial_dies).
7393 This problem could be avoided, but the benefit is unclear. */
7394 if (this_cu->cu != NULL)
7395 free_one_cached_comp_unit (this_cu);
7396
6751ebae 7397 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7398
58990295
TV
7399 switch (reader.comp_unit_die->tag)
7400 {
7401 case DW_TAG_compile_unit:
7402 this_cu->unit_type = DW_UT_compile;
7403 break;
7404 case DW_TAG_partial_unit:
7405 this_cu->unit_type = DW_UT_partial;
7406 break;
7407 default:
7408 abort ();
7409 }
7410
c0ab21c2 7411 if (reader.dummy_p)
f1902523 7412 {
c0ab21c2 7413 /* Nothing. */
f1902523 7414 }
c0ab21c2 7415 else if (this_cu->is_debug_types)
3e225074
TT
7416 build_type_psymtabs_reader (&reader, reader.info_ptr,
7417 reader.comp_unit_die);
135f5437
TT
7418 else if (want_partial_unit
7419 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7420 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7421 reader.comp_unit_die,
c0ab21c2 7422 pretend_language);
0018ea6f 7423
58990295
TV
7424 this_cu->lang = this_cu->cu->language;
7425
0018ea6f 7426 /* Age out any secondary CUs. */
ed2dc618 7427 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7428}
f4dc4d17
DE
7429
7430/* Reader function for build_type_psymtabs. */
7431
7432static void
7433build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7434 const gdb_byte *info_ptr,
3e225074 7435 struct die_info *type_unit_die)
f4dc4d17 7436{
ed2dc618 7437 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7438 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7439 struct objfile *objfile = dwarf2_per_objfile->objfile;
7440 struct dwarf2_cu *cu = reader->cu;
7441 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7442 struct signatured_type *sig_type;
f4dc4d17
DE
7443 struct type_unit_group *tu_group;
7444 struct attribute *attr;
7445 struct partial_die_info *first_die;
7446 CORE_ADDR lowpc, highpc;
891813be 7447 dwarf2_psymtab *pst;
f4dc4d17 7448
0186c6a7
DE
7449 gdb_assert (per_cu->is_debug_types);
7450 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7451
3e225074 7452 if (! type_unit_die->has_children)
f4dc4d17
DE
7453 return;
7454
052c8bb8 7455 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7456 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7457
df07e2c7 7458 if (tu_group->tus == nullptr)
a8b3b8e9 7459 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7460 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7461
7462 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7463 pst = create_partial_symtab (per_cu, "");
6d94535f 7464 pst->anonymous = true;
f4dc4d17
DE
7465
7466 first_die = load_partial_dies (reader, info_ptr, 1);
7467
7468 lowpc = (CORE_ADDR) -1;
7469 highpc = (CORE_ADDR) 0;
7470 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7471
8763cede 7472 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7473}
7474
73051182
DE
7475/* Struct used to sort TUs by their abbreviation table offset. */
7476
7477struct tu_abbrev_offset
7478{
b2bdb8cf
SM
7479 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7480 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7481 {}
7482
7483 signatured_type *sig_type;
73051182
DE
7484 sect_offset abbrev_offset;
7485};
7486
484cf504 7487/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7488
484cf504
TT
7489static bool
7490sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7491 const struct tu_abbrev_offset &b)
73051182 7492{
484cf504 7493 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7494}
7495
7496/* Efficiently read all the type units.
7497 This does the bulk of the work for build_type_psymtabs.
7498
7499 The efficiency is because we sort TUs by the abbrev table they use and
7500 only read each abbrev table once. In one program there are 200K TUs
7501 sharing 8K abbrev tables.
7502
7503 The main purpose of this function is to support building the
7504 dwarf2_per_objfile->type_unit_groups table.
7505 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7506 can collapse the search space by grouping them by stmt_list.
7507 The savings can be significant, in the same program from above the 200K TUs
7508 share 8K stmt_list tables.
7509
7510 FUNC is expected to call get_type_unit_group, which will create the
7511 struct type_unit_group if necessary and add it to
7512 dwarf2_per_objfile->type_unit_groups. */
7513
7514static void
ed2dc618 7515build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7516{
73051182 7517 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7518 abbrev_table_up abbrev_table;
73051182 7519 sect_offset abbrev_offset;
73051182
DE
7520
7521 /* It's up to the caller to not call us multiple times. */
7522 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7523
b2bdb8cf 7524 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7525 return;
7526
7527 /* TUs typically share abbrev tables, and there can be way more TUs than
7528 abbrev tables. Sort by abbrev table to reduce the number of times we
7529 read each abbrev table in.
7530 Alternatives are to punt or to maintain a cache of abbrev tables.
7531 This is simpler and efficient enough for now.
7532
7533 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7534 symtab to use). Typically TUs with the same abbrev offset have the same
7535 stmt_list value too so in practice this should work well.
7536
7537 The basic algorithm here is:
7538
7539 sort TUs by abbrev table
7540 for each TU with same abbrev table:
7541 read abbrev table if first user
7542 read TU top level DIE
7543 [IWBN if DWO skeletons had DW_AT_stmt_list]
7544 call FUNC */
7545
b4f54984 7546 if (dwarf_read_debug)
73051182
DE
7547 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7548
7549 /* Sort in a separate table to maintain the order of all_type_units
7550 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7551 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7552 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7553
7554 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7555 sorted_by_abbrev.emplace_back
7556 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7557 sig_type->per_cu.section,
7558 sig_type->per_cu.sect_off));
73051182 7559
484cf504
TT
7560 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7561 sort_tu_by_abbrev_offset);
73051182 7562
9c541725 7563 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7564
b2bdb8cf 7565 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7566 {
73051182
DE
7567 /* Switch to the next abbrev table if necessary. */
7568 if (abbrev_table == NULL
b2bdb8cf 7569 || tu.abbrev_offset != abbrev_offset)
73051182 7570 {
b2bdb8cf 7571 abbrev_offset = tu.abbrev_offset;
73051182 7572 abbrev_table =
86de1d91
TT
7573 abbrev_table::read (dwarf2_per_objfile->objfile,
7574 &dwarf2_per_objfile->abbrev,
7575 abbrev_offset);
73051182
DE
7576 ++tu_stats->nr_uniq_abbrev_tables;
7577 }
7578
c0ab21c2 7579 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7580 0, false);
c0ab21c2
TT
7581 if (!reader.dummy_p)
7582 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7583 reader.comp_unit_die);
73051182 7584 }
6aa5f3a6 7585}
73051182 7586
6aa5f3a6
DE
7587/* Print collected type unit statistics. */
7588
7589static void
ed2dc618 7590print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7591{
7592 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7593
7594 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7595 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7596 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7597 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7598 tu_stats->nr_uniq_abbrev_tables);
7599 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7600 tu_stats->nr_symtabs);
7601 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7602 tu_stats->nr_symtab_sharers);
7603 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7604 tu_stats->nr_stmt_less_type_units);
7605 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7606 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7607}
7608
f4dc4d17
DE
7609/* Traversal function for build_type_psymtabs. */
7610
7611static int
7612build_type_psymtab_dependencies (void **slot, void *info)
7613{
ed2dc618
SM
7614 struct dwarf2_per_objfile *dwarf2_per_objfile
7615 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7616 struct objfile *objfile = dwarf2_per_objfile->objfile;
7617 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7618 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7619 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7620 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7621 int i;
7622
7623 gdb_assert (len > 0);
197400e8 7624 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7625
7626 pst->number_of_dependencies = len;
a9342b62 7627 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7628 for (i = 0; i < len; ++i)
f4dc4d17 7629 {
df07e2c7 7630 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7631 gdb_assert (iter->per_cu.is_debug_types);
7632 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7633 iter->type_unit_group = tu_group;
f4dc4d17
DE
7634 }
7635
df07e2c7
AB
7636 delete tu_group->tus;
7637 tu_group->tus = nullptr;
348e048f
DE
7638
7639 return 1;
7640}
7641
7642/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7643 Build partial symbol tables for the .debug_types comp-units. */
7644
7645static void
ed2dc618 7646build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7647{
ed2dc618 7648 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7649 return;
7650
ed2dc618 7651 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7652}
f4dc4d17 7653
6aa5f3a6
DE
7654/* Traversal function for process_skeletonless_type_unit.
7655 Read a TU in a DWO file and build partial symbols for it. */
7656
7657static int
7658process_skeletonless_type_unit (void **slot, void *info)
7659{
7660 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7661 struct dwarf2_per_objfile *dwarf2_per_objfile
7662 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7663 struct signatured_type find_entry, *entry;
7664
7665 /* If this TU doesn't exist in the global table, add it and read it in. */
7666
7667 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7668 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7669
7670 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7671 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7672 &find_entry, INSERT);
6aa5f3a6
DE
7673 /* If we've already seen this type there's nothing to do. What's happening
7674 is we're doing our own version of comdat-folding here. */
7675 if (*slot != NULL)
7676 return 1;
7677
7678 /* This does the job that create_all_type_units would have done for
7679 this TU. */
ed2dc618
SM
7680 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7681 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7682 *slot = entry;
7683
7684 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7685 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7686 if (!reader.dummy_p)
7687 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7688 reader.comp_unit_die);
6aa5f3a6
DE
7689
7690 return 1;
7691}
7692
7693/* Traversal function for process_skeletonless_type_units. */
7694
7695static int
7696process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7697{
7698 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7699
7700 if (dwo_file->tus != NULL)
b0b6a987
TT
7701 htab_traverse_noresize (dwo_file->tus.get (),
7702 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7703
7704 return 1;
7705}
7706
7707/* Scan all TUs of DWO files, verifying we've processed them.
7708 This is needed in case a TU was emitted without its skeleton.
7709 Note: This can't be done until we know what all the DWO files are. */
7710
7711static void
ed2dc618 7712process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7713{
7714 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7715 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7716 && dwarf2_per_objfile->dwo_files != NULL)
7717 {
51ac9db5 7718 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7719 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7720 dwarf2_per_objfile);
6aa5f3a6 7721 }
348e048f
DE
7722}
7723
ed2dc618 7724/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7725
7726static void
ed2dc618 7727set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7728{
b76e467d 7729 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7730 {
891813be 7731 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7732
36586728
TT
7733 if (pst == NULL)
7734 continue;
7735
b76e467d 7736 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7737 {
7738 /* Set the 'user' field only if it is not already set. */
7739 if (pst->dependencies[j]->user == NULL)
7740 pst->dependencies[j]->user = pst;
7741 }
7742 }
7743}
7744
93311388
DE
7745/* Build the partial symbol table by doing a quick pass through the
7746 .debug_info and .debug_abbrev sections. */
72bf9492 7747
93311388 7748static void
ed2dc618 7749dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7750{
ed2dc618 7751 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7752
b4f54984 7753 if (dwarf_read_debug)
45cfd468
DE
7754 {
7755 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7756 objfile_name (objfile));
45cfd468
DE
7757 }
7758
76935768
TT
7759 scoped_restore restore_reading_psyms
7760 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7761 true);
98bfdba5 7762
96b79293 7763 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7764
93311388
DE
7765 /* Any cached compilation units will be linked by the per-objfile
7766 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7767 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7768
ed2dc618 7769 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7770
ed2dc618 7771 create_all_comp_units (dwarf2_per_objfile);
c906108c 7772
60606b2c
TT
7773 /* Create a temporary address map on a temporary obstack. We later
7774 copy this to the final obstack. */
8268c778 7775 auto_obstack temp_obstack;
791afaa2
TT
7776
7777 scoped_restore save_psymtabs_addrmap
d320c2b5 7778 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7779 addrmap_create_mutable (&temp_obstack));
72bf9492 7780
b76e467d 7781 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3d5afab3
TV
7782 {
7783 if (per_cu->v.psymtab != NULL)
7784 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7785 continue;
7786 process_psymtab_comp_unit (per_cu, false, language_minimal);
7787 }
ff013f42 7788
6aa5f3a6 7789 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7790 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7791
7792 /* Now that all TUs have been processed we can fill in the dependencies. */
7793 if (dwarf2_per_objfile->type_unit_groups != NULL)
7794 {
eaa5fa8b 7795 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7796 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7797 }
7798
b4f54984 7799 if (dwarf_read_debug)
ed2dc618 7800 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7801
ed2dc618 7802 set_partial_user (dwarf2_per_objfile);
95554aad 7803
d320c2b5
TT
7804 objfile->partial_symtabs->psymtabs_addrmap
7805 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7806 objfile->partial_symtabs->obstack ());
791afaa2
TT
7807 /* At this point we want to keep the address map. */
7808 save_psymtabs_addrmap.release ();
ff013f42 7809
b4f54984 7810 if (dwarf_read_debug)
45cfd468 7811 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7812 objfile_name (objfile));
ae038cb0
DJ
7813}
7814
dee91e82
DE
7815/* Load the partial DIEs for a secondary CU into memory.
7816 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7817
dee91e82
DE
7818static void
7819load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7820{
6751ebae 7821 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7822
7823 if (!reader.dummy_p)
7824 {
7825 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7826 language_minimal);
7827
7828 /* Check if comp unit has_children.
7829 If so, read the rest of the partial symbols from this comp unit.
7830 If not, there's no more debug_info for this comp unit. */
3e225074 7831 if (reader.comp_unit_die->has_children)
c0ab21c2 7832 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7833
7834 reader.keep ();
c0ab21c2 7835 }
ae038cb0
DJ
7836}
7837
ae038cb0 7838static void
ed2dc618 7839read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7840 struct dwarf2_section_info *section,
f1902523 7841 struct dwarf2_section_info *abbrev_section,
b76e467d 7842 unsigned int is_dwz)
ae038cb0 7843{
d521ce57 7844 const gdb_byte *info_ptr;
ed2dc618 7845 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7846
b4f54984 7847 if (dwarf_read_debug)
bf6af496 7848 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7849 section->get_name (),
7850 section->get_file_name ());
bf6af496 7851
96b79293 7852 section->read (objfile);
ae038cb0 7853
36586728 7854 info_ptr = section->buffer;
6e70227d 7855
36586728 7856 while (info_ptr < section->buffer + section->size)
ae038cb0 7857 {
ae038cb0 7858 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7859
9c541725 7860 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7861
f1902523 7862 comp_unit_head cu_header;
ed2dc618
SM
7863 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7864 abbrev_section, info_ptr,
7865 rcuh_kind::COMPILE);
ae038cb0
DJ
7866
7867 /* Save the compilation unit for later lookup. */
f1902523
JK
7868 if (cu_header.unit_type != DW_UT_type)
7869 {
7870 this_cu = XOBNEW (&objfile->objfile_obstack,
7871 struct dwarf2_per_cu_data);
7872 memset (this_cu, 0, sizeof (*this_cu));
7873 }
7874 else
7875 {
7876 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7877 struct signatured_type);
7878 memset (sig_type, 0, sizeof (*sig_type));
7879 sig_type->signature = cu_header.signature;
7880 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7881 this_cu = &sig_type->per_cu;
7882 }
7883 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7884 this_cu->sect_off = sect_off;
f1902523 7885 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7886 this_cu->is_dwz = is_dwz;
e3b94546 7887 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7888 this_cu->section = section;
ae038cb0 7889
b76e467d 7890 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7891
7892 info_ptr = info_ptr + this_cu->length;
7893 }
36586728
TT
7894}
7895
7896/* Create a list of all compilation units in OBJFILE.
7897 This is only done for -readnow and building partial symtabs. */
7898
7899static void
ed2dc618 7900create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7901{
b76e467d 7902 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7903 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7904 &dwarf2_per_objfile->abbrev, 0);
36586728 7905
b76e467d 7906 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7907 if (dwz != NULL)
ed2dc618 7908 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7909 1);
c906108c
SS
7910}
7911
5734ee8b 7912/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7913 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7914 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7915 DW_AT_ranges). See the comments of add_partial_subprogram on how
7916 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7917
72bf9492
DJ
7918static void
7919scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7920 CORE_ADDR *highpc, int set_addrmap,
7921 struct dwarf2_cu *cu)
c906108c 7922{
72bf9492 7923 struct partial_die_info *pdi;
c906108c 7924
91c24f0a
DC
7925 /* Now, march along the PDI's, descending into ones which have
7926 interesting children but skipping the children of the other ones,
7927 until we reach the end of the compilation unit. */
c906108c 7928
72bf9492 7929 pdi = first_die;
91c24f0a 7930
72bf9492
DJ
7931 while (pdi != NULL)
7932 {
52356b79 7933 pdi->fixup (cu);
c906108c 7934
f55ee35c 7935 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7936 children, so we need to look at them. Ditto for anonymous
7937 enums. */
933c6fe4 7938
72bf9492 7939 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7940 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7941 || pdi->tag == DW_TAG_imported_unit
7942 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7943 {
72bf9492 7944 switch (pdi->tag)
c906108c
SS
7945 {
7946 case DW_TAG_subprogram:
b1dc1806 7947 case DW_TAG_inlined_subroutine:
cdc07690 7948 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7949 break;
72929c62 7950 case DW_TAG_constant:
c906108c
SS
7951 case DW_TAG_variable:
7952 case DW_TAG_typedef:
91c24f0a 7953 case DW_TAG_union_type:
317d2668
TV
7954 if (!pdi->is_declaration
7955 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7956 {
72bf9492 7957 add_partial_symbol (pdi, cu);
63d06c5c
DC
7958 }
7959 break;
c906108c 7960 case DW_TAG_class_type:
680b30c7 7961 case DW_TAG_interface_type:
c906108c 7962 case DW_TAG_structure_type:
72bf9492 7963 if (!pdi->is_declaration)
c906108c 7964 {
72bf9492 7965 add_partial_symbol (pdi, cu);
c906108c 7966 }
b7fee5a3
KS
7967 if ((cu->language == language_rust
7968 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7969 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7970 set_addrmap, cu);
c906108c 7971 break;
91c24f0a 7972 case DW_TAG_enumeration_type:
72bf9492
DJ
7973 if (!pdi->is_declaration)
7974 add_partial_enumeration (pdi, cu);
c906108c
SS
7975 break;
7976 case DW_TAG_base_type:
a02abb62 7977 case DW_TAG_subrange_type:
c906108c 7978 /* File scope base type definitions are added to the partial
c5aa993b 7979 symbol table. */
72bf9492 7980 add_partial_symbol (pdi, cu);
c906108c 7981 break;
d9fa45fe 7982 case DW_TAG_namespace:
cdc07690 7983 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7984 break;
5d7cb8df 7985 case DW_TAG_module:
59c35742
AB
7986 if (!pdi->is_declaration)
7987 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7988 break;
95554aad
TT
7989 case DW_TAG_imported_unit:
7990 {
7991 struct dwarf2_per_cu_data *per_cu;
7992
f4dc4d17
DE
7993 /* For now we don't handle imported units in type units. */
7994 if (cu->per_cu->is_debug_types)
7995 {
7996 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7997 " supported in type units [in module %s]"),
518817b3 7998 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7999 }
8000
e3b94546
SM
8001 per_cu = dwarf2_find_containing_comp_unit
8002 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8003 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8004
8005 /* Go read the partial unit, if needed. */
8006 if (per_cu->v.psymtab == NULL)
135f5437 8007 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8008
ae640021 8009 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8010 }
8011 break;
74921315
KS
8012 case DW_TAG_imported_declaration:
8013 add_partial_symbol (pdi, cu);
8014 break;
c906108c
SS
8015 default:
8016 break;
8017 }
8018 }
8019
72bf9492
DJ
8020 /* If the die has a sibling, skip to the sibling. */
8021
8022 pdi = pdi->die_sibling;
8023 }
8024}
8025
8026/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8027
72bf9492 8028 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8029 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8030 Enumerators are an exception; they use the scope of their parent
8031 enumeration type, i.e. the name of the enumeration type is not
8032 prepended to the enumerator.
91c24f0a 8033
72bf9492
DJ
8034 There are two complexities. One is DW_AT_specification; in this
8035 case "parent" means the parent of the target of the specification,
8036 instead of the direct parent of the DIE. The other is compilers
8037 which do not emit DW_TAG_namespace; in this case we try to guess
8038 the fully qualified name of structure types from their members'
8039 linkage names. This must be done using the DIE's children rather
8040 than the children of any DW_AT_specification target. We only need
8041 to do this for structures at the top level, i.e. if the target of
8042 any DW_AT_specification (if any; otherwise the DIE itself) does not
8043 have a parent. */
8044
8045/* Compute the scope prefix associated with PDI's parent, in
8046 compilation unit CU. The result will be allocated on CU's
8047 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8048 field. NULL is returned if no prefix is necessary. */
15d034d0 8049static const char *
72bf9492
DJ
8050partial_die_parent_scope (struct partial_die_info *pdi,
8051 struct dwarf2_cu *cu)
8052{
15d034d0 8053 const char *grandparent_scope;
72bf9492 8054 struct partial_die_info *parent, *real_pdi;
91c24f0a 8055
72bf9492
DJ
8056 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8057 then this means the parent of the specification DIE. */
8058
8059 real_pdi = pdi;
72bf9492 8060 while (real_pdi->has_specification)
fb816e8b 8061 {
122cf0f2
AB
8062 auto res = find_partial_die (real_pdi->spec_offset,
8063 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8064 real_pdi = res.pdi;
8065 cu = res.cu;
8066 }
72bf9492
DJ
8067
8068 parent = real_pdi->die_parent;
8069 if (parent == NULL)
8070 return NULL;
8071
8072 if (parent->scope_set)
8073 return parent->scope;
8074
52356b79 8075 parent->fixup (cu);
72bf9492 8076
10b3939b 8077 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8078
acebe513
UW
8079 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8080 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8081 Work around this problem here. */
8082 if (cu->language == language_cplus
6e70227d 8083 && parent->tag == DW_TAG_namespace
acebe513
UW
8084 && strcmp (parent->name, "::") == 0
8085 && grandparent_scope == NULL)
8086 {
8087 parent->scope = NULL;
8088 parent->scope_set = 1;
8089 return NULL;
8090 }
8091
0a4b0913 8092 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8093 if (pdi->tag == DW_TAG_enumerator)
8094 /* Enumerators should not get the name of the enumeration as a prefix. */
8095 parent->scope = grandparent_scope;
8096 else if (parent->tag == DW_TAG_namespace
f55ee35c 8097 || parent->tag == DW_TAG_module
72bf9492
DJ
8098 || parent->tag == DW_TAG_structure_type
8099 || parent->tag == DW_TAG_class_type
680b30c7 8100 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8101 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8102 || parent->tag == DW_TAG_enumeration_type
8103 || (cu->language == language_fortran
8104 && parent->tag == DW_TAG_subprogram
8105 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8106 {
8107 if (grandparent_scope == NULL)
8108 parent->scope = parent->name;
8109 else
3e43a32a
MS
8110 parent->scope = typename_concat (&cu->comp_unit_obstack,
8111 grandparent_scope,
f55ee35c 8112 parent->name, 0, cu);
72bf9492 8113 }
72bf9492
DJ
8114 else
8115 {
8116 /* FIXME drow/2004-04-01: What should we be doing with
8117 function-local names? For partial symbols, we should probably be
8118 ignoring them. */
fa9c3fa0
TT
8119 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8120 dwarf_tag_name (parent->tag),
8121 sect_offset_str (pdi->sect_off));
72bf9492 8122 parent->scope = grandparent_scope;
c906108c
SS
8123 }
8124
72bf9492
DJ
8125 parent->scope_set = 1;
8126 return parent->scope;
8127}
8128
8129/* Return the fully scoped name associated with PDI, from compilation unit
8130 CU. The result will be allocated with malloc. */
4568ecf9 8131
43816ebc 8132static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8133partial_die_full_name (struct partial_die_info *pdi,
8134 struct dwarf2_cu *cu)
8135{
15d034d0 8136 const char *parent_scope;
72bf9492 8137
98bfdba5
PA
8138 /* If this is a template instantiation, we can not work out the
8139 template arguments from partial DIEs. So, unfortunately, we have
8140 to go through the full DIEs. At least any work we do building
8141 types here will be reused if full symbols are loaded later. */
8142 if (pdi->has_template_arguments)
8143 {
52356b79 8144 pdi->fixup (cu);
98bfdba5
PA
8145
8146 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8147 {
8148 struct die_info *die;
8149 struct attribute attr;
8150 struct dwarf2_cu *ref_cu = cu;
8151
b64f50a1 8152 /* DW_FORM_ref_addr is using section offset. */
b4069958 8153 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8154 attr.form = DW_FORM_ref_addr;
9c541725 8155 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8156 die = follow_die_ref (NULL, &attr, &ref_cu);
8157
43816ebc 8158 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8159 }
8160 }
8161
72bf9492
DJ
8162 parent_scope = partial_die_parent_scope (pdi, cu);
8163 if (parent_scope == NULL)
8164 return NULL;
8165 else
43816ebc
TT
8166 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8167 pdi->name, 0, cu));
c906108c
SS
8168}
8169
8170static void
72bf9492 8171add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8172{
518817b3
SM
8173 struct dwarf2_per_objfile *dwarf2_per_objfile
8174 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8175 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8176 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8177 CORE_ADDR addr = 0;
15d034d0 8178 const char *actual_name = NULL;
e142c38c
DJ
8179 CORE_ADDR baseaddr;
8180
b3b3bada 8181 baseaddr = objfile->text_section_offset ();
c906108c 8182
43816ebc
TT
8183 gdb::unique_xmalloc_ptr<char> built_actual_name
8184 = partial_die_full_name (pdi, cu);
15d034d0 8185 if (built_actual_name != NULL)
43816ebc 8186 actual_name = built_actual_name.get ();
63d06c5c 8187
72bf9492
DJ
8188 if (actual_name == NULL)
8189 actual_name = pdi->name;
8190
c906108c
SS
8191 switch (pdi->tag)
8192 {
b1dc1806 8193 case DW_TAG_inlined_subroutine:
c906108c 8194 case DW_TAG_subprogram:
79748972
TT
8195 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8196 - baseaddr);
0a4b0913
AB
8197 if (pdi->is_external
8198 || cu->language == language_ada
8199 || (cu->language == language_fortran
8200 && pdi->die_parent != NULL
8201 && pdi->die_parent->tag == DW_TAG_subprogram))
8202 {
8203 /* Normally, only "external" DIEs are part of the global scope.
8204 But in Ada and Fortran, we want to be able to access nested
8205 procedures globally. So all Ada and Fortran subprograms are
8206 stored in the global scope. */
31edb802 8207 add_psymbol_to_list (actual_name,
15d034d0 8208 built_actual_name != NULL,
f47fb265 8209 VAR_DOMAIN, LOC_BLOCK,
79748972 8210 SECT_OFF_TEXT (objfile),
75aedd27 8211 psymbol_placement::GLOBAL,
79748972
TT
8212 addr,
8213 cu->language, objfile);
c906108c
SS
8214 }
8215 else
8216 {
31edb802 8217 add_psymbol_to_list (actual_name,
15d034d0 8218 built_actual_name != NULL,
f47fb265 8219 VAR_DOMAIN, LOC_BLOCK,
79748972 8220 SECT_OFF_TEXT (objfile),
75aedd27 8221 psymbol_placement::STATIC,
1762568f 8222 addr, cu->language, objfile);
c906108c 8223 }
0c1b455e
TT
8224
8225 if (pdi->main_subprogram && actual_name != NULL)
8226 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8227 break;
72929c62 8228 case DW_TAG_constant:
31edb802 8229 add_psymbol_to_list (actual_name,
75aedd27
TT
8230 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8231 -1, (pdi->is_external
8232 ? psymbol_placement::GLOBAL
8233 : psymbol_placement::STATIC),
8234 0, cu->language, objfile);
72929c62 8235 break;
c906108c 8236 case DW_TAG_variable:
95554aad
TT
8237 if (pdi->d.locdesc)
8238 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8239
95554aad 8240 if (pdi->d.locdesc
caac4577
JG
8241 && addr == 0
8242 && !dwarf2_per_objfile->has_section_at_zero)
8243 {
8244 /* A global or static variable may also have been stripped
8245 out by the linker if unused, in which case its address
8246 will be nullified; do not add such variables into partial
8247 symbol table then. */
8248 }
8249 else if (pdi->is_external)
c906108c
SS
8250 {
8251 /* Global Variable.
8252 Don't enter into the minimal symbol tables as there is
8253 a minimal symbol table entry from the ELF symbols already.
8254 Enter into partial symbol table if it has a location
8255 descriptor or a type.
8256 If the location descriptor is missing, new_symbol will create
8257 a LOC_UNRESOLVED symbol, the address of the variable will then
8258 be determined from the minimal symbol table whenever the variable
8259 is referenced.
8260 The address for the partial symbol table entry is not
8261 used by GDB, but it comes in handy for debugging partial symbol
8262 table building. */
8263
95554aad 8264 if (pdi->d.locdesc || pdi->has_type)
31edb802 8265 add_psymbol_to_list (actual_name,
15d034d0 8266 built_actual_name != NULL,
f47fb265 8267 VAR_DOMAIN, LOC_STATIC,
79748972 8268 SECT_OFF_TEXT (objfile),
75aedd27 8269 psymbol_placement::GLOBAL,
79748972 8270 addr, cu->language, objfile);
c906108c
SS
8271 }
8272 else
8273 {
ff908ebf
AW
8274 int has_loc = pdi->d.locdesc != NULL;
8275
8276 /* Static Variable. Skip symbols whose value we cannot know (those
8277 without location descriptors or constant values). */
8278 if (!has_loc && !pdi->has_const_value)
43816ebc 8279 return;
ff908ebf 8280
31edb802 8281 add_psymbol_to_list (actual_name,
15d034d0 8282 built_actual_name != NULL,
f47fb265 8283 VAR_DOMAIN, LOC_STATIC,
79748972 8284 SECT_OFF_TEXT (objfile),
75aedd27 8285 psymbol_placement::STATIC,
79748972 8286 has_loc ? addr : 0,
f47fb265 8287 cu->language, objfile);
c906108c
SS
8288 }
8289 break;
8290 case DW_TAG_typedef:
8291 case DW_TAG_base_type:
a02abb62 8292 case DW_TAG_subrange_type:
31edb802 8293 add_psymbol_to_list (actual_name,
15d034d0 8294 built_actual_name != NULL,
79748972 8295 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8296 psymbol_placement::STATIC,
1762568f 8297 0, cu->language, objfile);
c906108c 8298 break;
74921315 8299 case DW_TAG_imported_declaration:
72bf9492 8300 case DW_TAG_namespace:
31edb802 8301 add_psymbol_to_list (actual_name,
15d034d0 8302 built_actual_name != NULL,
79748972 8303 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8304 psymbol_placement::GLOBAL,
1762568f 8305 0, cu->language, objfile);
72bf9492 8306 break;
530e8392 8307 case DW_TAG_module:
a5fd13a9
BH
8308 /* With Fortran 77 there might be a "BLOCK DATA" module
8309 available without any name. If so, we skip the module as it
8310 doesn't bring any value. */
8311 if (actual_name != nullptr)
31edb802 8312 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8313 built_actual_name != NULL,
8314 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8315 psymbol_placement::GLOBAL,
8316 0, cu->language, objfile);
530e8392 8317 break;
c906108c 8318 case DW_TAG_class_type:
680b30c7 8319 case DW_TAG_interface_type:
c906108c
SS
8320 case DW_TAG_structure_type:
8321 case DW_TAG_union_type:
8322 case DW_TAG_enumeration_type:
fa4028e9
JB
8323 /* Skip external references. The DWARF standard says in the section
8324 about "Structure, Union, and Class Type Entries": "An incomplete
8325 structure, union or class type is represented by a structure,
8326 union or class entry that does not have a byte size attribute
8327 and that has a DW_AT_declaration attribute." */
8328 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8329 return;
fa4028e9 8330
63d06c5c
DC
8331 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8332 static vs. global. */
31edb802 8333 add_psymbol_to_list (actual_name,
15d034d0 8334 built_actual_name != NULL,
79748972 8335 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8336 cu->language == language_cplus
75aedd27
TT
8337 ? psymbol_placement::GLOBAL
8338 : psymbol_placement::STATIC,
1762568f 8339 0, cu->language, objfile);
c906108c 8340
c906108c
SS
8341 break;
8342 case DW_TAG_enumerator:
31edb802 8343 add_psymbol_to_list (actual_name,
15d034d0 8344 built_actual_name != NULL,
79748972 8345 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8346 cu->language == language_cplus
75aedd27
TT
8347 ? psymbol_placement::GLOBAL
8348 : psymbol_placement::STATIC,
1762568f 8349 0, cu->language, objfile);
c906108c
SS
8350 break;
8351 default:
8352 break;
8353 }
8354}
8355
5c4e30ca
DC
8356/* Read a partial die corresponding to a namespace; also, add a symbol
8357 corresponding to that namespace to the symbol table. NAMESPACE is
8358 the name of the enclosing namespace. */
91c24f0a 8359
72bf9492
DJ
8360static void
8361add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8362 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8363 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8364{
72bf9492 8365 /* Add a symbol for the namespace. */
e7c27a73 8366
72bf9492 8367 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8368
8369 /* Now scan partial symbols in that namespace. */
8370
91c24f0a 8371 if (pdi->has_children)
cdc07690 8372 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8373}
8374
5d7cb8df
JK
8375/* Read a partial die corresponding to a Fortran module. */
8376
8377static void
8378add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8379 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8380{
530e8392
KB
8381 /* Add a symbol for the namespace. */
8382
8383 add_partial_symbol (pdi, cu);
8384
f55ee35c 8385 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8386
8387 if (pdi->has_children)
cdc07690 8388 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8389}
8390
b1dc1806
XR
8391/* Read a partial die corresponding to a subprogram or an inlined
8392 subprogram and create a partial symbol for that subprogram.
8393 When the CU language allows it, this routine also defines a partial
8394 symbol for each nested subprogram that this subprogram contains.
8395 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8396 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8397
cdc07690
YQ
8398 PDI may also be a lexical block, in which case we simply search
8399 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8400 Again, this is only performed when the CU language allows this
8401 type of definitions. */
8402
8403static void
8404add_partial_subprogram (struct partial_die_info *pdi,
8405 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8406 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8407{
b1dc1806 8408 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8409 {
8410 if (pdi->has_pc_info)
8411 {
8412 if (pdi->lowpc < *lowpc)
8413 *lowpc = pdi->lowpc;
8414 if (pdi->highpc > *highpc)
8415 *highpc = pdi->highpc;
cdc07690 8416 if (set_addrmap)
5734ee8b 8417 {
518817b3 8418 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8419 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8420 CORE_ADDR baseaddr;
b926417a
TT
8421 CORE_ADDR this_highpc;
8422 CORE_ADDR this_lowpc;
5734ee8b 8423
b3b3bada 8424 baseaddr = objfile->text_section_offset ();
b926417a
TT
8425 this_lowpc
8426 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8427 pdi->lowpc + baseaddr)
8428 - baseaddr);
8429 this_highpc
8430 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8431 pdi->highpc + baseaddr)
8432 - baseaddr);
d320c2b5 8433 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8434 this_lowpc, this_highpc - 1,
9291a0cd 8435 cu->per_cu->v.psymtab);
5734ee8b 8436 }
481860b3
GB
8437 }
8438
8439 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8440 {
bc30ff58 8441 if (!pdi->is_declaration)
e8d05480
JB
8442 /* Ignore subprogram DIEs that do not have a name, they are
8443 illegal. Do not emit a complaint at this point, we will
8444 do so when we convert this psymtab into a symtab. */
8445 if (pdi->name)
8446 add_partial_symbol (pdi, cu);
bc30ff58
JB
8447 }
8448 }
6e70227d 8449
bc30ff58
JB
8450 if (! pdi->has_children)
8451 return;
8452
0a4b0913 8453 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8454 {
8455 pdi = pdi->die_child;
8456 while (pdi != NULL)
8457 {
52356b79 8458 pdi->fixup (cu);
bc30ff58 8459 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8460 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8461 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8462 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8463 pdi = pdi->die_sibling;
8464 }
8465 }
8466}
8467
91c24f0a
DC
8468/* Read a partial die corresponding to an enumeration type. */
8469
72bf9492
DJ
8470static void
8471add_partial_enumeration (struct partial_die_info *enum_pdi,
8472 struct dwarf2_cu *cu)
91c24f0a 8473{
72bf9492 8474 struct partial_die_info *pdi;
91c24f0a
DC
8475
8476 if (enum_pdi->name != NULL)
72bf9492
DJ
8477 add_partial_symbol (enum_pdi, cu);
8478
8479 pdi = enum_pdi->die_child;
8480 while (pdi)
91c24f0a 8481 {
72bf9492 8482 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8483 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8484 else
72bf9492
DJ
8485 add_partial_symbol (pdi, cu);
8486 pdi = pdi->die_sibling;
91c24f0a 8487 }
91c24f0a
DC
8488}
8489
6caca83c
CC
8490/* Return the initial uleb128 in the die at INFO_PTR. */
8491
8492static unsigned int
d521ce57 8493peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8494{
8495 unsigned int bytes_read;
8496
8497 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8498}
8499
685af9cd
TT
8500/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8501 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8502
4bb7a0a7
DJ
8503 Return the corresponding abbrev, or NULL if the number is zero (indicating
8504 an empty DIE). In either case *BYTES_READ will be set to the length of
8505 the initial number. */
8506
8507static struct abbrev_info *
685af9cd
TT
8508peek_die_abbrev (const die_reader_specs &reader,
8509 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8510{
685af9cd 8511 dwarf2_cu *cu = reader.cu;
518817b3 8512 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8513 unsigned int abbrev_number
8514 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8515
8516 if (abbrev_number == 0)
8517 return NULL;
8518
685af9cd 8519 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8520 if (!abbrev)
8521 {
422b9917 8522 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8523 " at offset %s [in module %s]"),
422b9917 8524 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8525 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8526 }
8527
8528 return abbrev;
8529}
8530
93311388
DE
8531/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8532 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8533 DIE. Any children of the skipped DIEs will also be skipped. */
8534
d521ce57
TT
8535static const gdb_byte *
8536skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8537{
4bb7a0a7
DJ
8538 while (1)
8539 {
685af9cd
TT
8540 unsigned int bytes_read;
8541 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8542
4bb7a0a7
DJ
8543 if (abbrev == NULL)
8544 return info_ptr + bytes_read;
8545 else
dee91e82 8546 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8547 }
8548}
8549
93311388
DE
8550/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8551 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8552 abbrev corresponding to that skipped uleb128 should be passed in
8553 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8554 children. */
8555
d521ce57
TT
8556static const gdb_byte *
8557skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8558 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8559{
8560 unsigned int bytes_read;
8561 struct attribute attr;
dee91e82
DE
8562 bfd *abfd = reader->abfd;
8563 struct dwarf2_cu *cu = reader->cu;
d521ce57 8564 const gdb_byte *buffer = reader->buffer;
f664829e 8565 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8566 unsigned int form, i;
8567
8568 for (i = 0; i < abbrev->num_attrs; i++)
8569 {
8570 /* The only abbrev we care about is DW_AT_sibling. */
8571 if (abbrev->attrs[i].name == DW_AT_sibling)
8572 {
18a8505e
AT
8573 bool ignored;
8574 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8575 &ignored);
4bb7a0a7 8576 if (attr.form == DW_FORM_ref_addr)
b98664d3 8577 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8578 else
b9502d3f 8579 {
0826b30a 8580 sect_offset off = attr.get_ref_die_offset ();
9c541725 8581 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8582
8583 if (sibling_ptr < info_ptr)
b98664d3 8584 complaint (_("DW_AT_sibling points backwards"));
22869d73 8585 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8586 reader->die_section->overflow_complaint ();
b9502d3f
WN
8587 else
8588 return sibling_ptr;
8589 }
4bb7a0a7
DJ
8590 }
8591
8592 /* If it isn't DW_AT_sibling, skip this attribute. */
8593 form = abbrev->attrs[i].form;
8594 skip_attribute:
8595 switch (form)
8596 {
4bb7a0a7 8597 case DW_FORM_ref_addr:
ae411497
TT
8598 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8599 and later it is offset sized. */
8600 if (cu->header.version == 2)
8601 info_ptr += cu->header.addr_size;
8602 else
8603 info_ptr += cu->header.offset_size;
8604 break;
36586728
TT
8605 case DW_FORM_GNU_ref_alt:
8606 info_ptr += cu->header.offset_size;
8607 break;
ae411497 8608 case DW_FORM_addr:
4bb7a0a7
DJ
8609 info_ptr += cu->header.addr_size;
8610 break;
8611 case DW_FORM_data1:
8612 case DW_FORM_ref1:
8613 case DW_FORM_flag:
8fe0f950 8614 case DW_FORM_strx1:
4bb7a0a7
DJ
8615 info_ptr += 1;
8616 break;
2dc7f7b3 8617 case DW_FORM_flag_present:
43988095 8618 case DW_FORM_implicit_const:
2dc7f7b3 8619 break;
4bb7a0a7
DJ
8620 case DW_FORM_data2:
8621 case DW_FORM_ref2:
8fe0f950 8622 case DW_FORM_strx2:
4bb7a0a7
DJ
8623 info_ptr += 2;
8624 break;
8fe0f950
AT
8625 case DW_FORM_strx3:
8626 info_ptr += 3;
8627 break;
4bb7a0a7
DJ
8628 case DW_FORM_data4:
8629 case DW_FORM_ref4:
8fe0f950 8630 case DW_FORM_strx4:
4bb7a0a7
DJ
8631 info_ptr += 4;
8632 break;
8633 case DW_FORM_data8:
8634 case DW_FORM_ref8:
55f1336d 8635 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8636 info_ptr += 8;
8637 break;
0224619f
JK
8638 case DW_FORM_data16:
8639 info_ptr += 16;
8640 break;
4bb7a0a7 8641 case DW_FORM_string:
9b1c24c8 8642 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8643 info_ptr += bytes_read;
8644 break;
2dc7f7b3 8645 case DW_FORM_sec_offset:
4bb7a0a7 8646 case DW_FORM_strp:
36586728 8647 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8648 info_ptr += cu->header.offset_size;
8649 break;
2dc7f7b3 8650 case DW_FORM_exprloc:
4bb7a0a7
DJ
8651 case DW_FORM_block:
8652 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8653 info_ptr += bytes_read;
8654 break;
8655 case DW_FORM_block1:
8656 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8657 break;
8658 case DW_FORM_block2:
8659 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8660 break;
8661 case DW_FORM_block4:
8662 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8663 break;
336d760d 8664 case DW_FORM_addrx:
cf532bd1 8665 case DW_FORM_strx:
4bb7a0a7
DJ
8666 case DW_FORM_sdata:
8667 case DW_FORM_udata:
8668 case DW_FORM_ref_udata:
3019eac3
DE
8669 case DW_FORM_GNU_addr_index:
8670 case DW_FORM_GNU_str_index:
18a8505e 8671 case DW_FORM_rnglistx:
41144253 8672 case DW_FORM_loclistx:
d521ce57 8673 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8674 break;
8675 case DW_FORM_indirect:
8676 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8677 info_ptr += bytes_read;
8678 /* We need to continue parsing from here, so just go back to
8679 the top. */
8680 goto skip_attribute;
8681
8682 default:
3e43a32a
MS
8683 error (_("Dwarf Error: Cannot handle %s "
8684 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8685 dwarf_form_name (form),
8686 bfd_get_filename (abfd));
8687 }
8688 }
8689
8690 if (abbrev->has_children)
dee91e82 8691 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8692 else
8693 return info_ptr;
8694}
8695
93311388 8696/* Locate ORIG_PDI's sibling.
dee91e82 8697 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8698
d521ce57 8699static const gdb_byte *
dee91e82
DE
8700locate_pdi_sibling (const struct die_reader_specs *reader,
8701 struct partial_die_info *orig_pdi,
d521ce57 8702 const gdb_byte *info_ptr)
91c24f0a
DC
8703{
8704 /* Do we know the sibling already? */
72bf9492 8705
91c24f0a
DC
8706 if (orig_pdi->sibling)
8707 return orig_pdi->sibling;
8708
8709 /* Are there any children to deal with? */
8710
8711 if (!orig_pdi->has_children)
8712 return info_ptr;
8713
4bb7a0a7 8714 /* Skip the children the long way. */
91c24f0a 8715
dee91e82 8716 return skip_children (reader, info_ptr);
91c24f0a
DC
8717}
8718
257e7a09 8719/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8720 not NULL. */
c906108c 8721
891813be
TT
8722void
8723dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8724{
ed2dc618
SM
8725 struct dwarf2_per_objfile *dwarf2_per_objfile
8726 = get_dwarf2_per_objfile (objfile);
8727
077cbab2
TT
8728 gdb_assert (!readin);
8729 /* If this psymtab is constructed from a debug-only objfile, the
8730 has_section_at_zero flag will not necessarily be correct. We
8731 can get the correct value for this flag by looking at the data
8732 associated with the (presumably stripped) associated objfile. */
8733 if (objfile->separate_debug_objfile_backlink)
c906108c 8734 {
077cbab2
TT
8735 struct dwarf2_per_objfile *dpo_backlink
8736 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8737
077cbab2
TT
8738 dwarf2_per_objfile->has_section_at_zero
8739 = dpo_backlink->has_section_at_zero;
8740 }
98bfdba5 8741
8566b89b 8742 expand_psymtab (objfile);
95554aad 8743
ed2dc618 8744 process_cu_includes (dwarf2_per_objfile);
c906108c 8745}
9cdd5dbd
DE
8746\f
8747/* Reading in full CUs. */
c906108c 8748
10b3939b
DJ
8749/* Add PER_CU to the queue. */
8750
8751static void
95554aad
TT
8752queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8753 enum language pretend_language)
10b3939b 8754{
10b3939b 8755 per_cu->queued = 1;
39856def 8756 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8757}
8758
89e63ee4
DE
8759/* If PER_CU is not yet queued, add it to the queue.
8760 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8761 dependency.
0907af0c 8762 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8763 meaning either PER_CU is already queued or it is already loaded.
8764
8765 N.B. There is an invariant here that if a CU is queued then it is loaded.
8766 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8767
8768static int
89e63ee4 8769maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8770 struct dwarf2_per_cu_data *per_cu,
8771 enum language pretend_language)
8772{
8773 /* We may arrive here during partial symbol reading, if we need full
8774 DIEs to process an unusual case (e.g. template arguments). Do
8775 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8776 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8777 {
8778 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8779 return 1;
8780 return 0;
8781 }
8782
8783 /* Mark the dependence relation so that we don't flush PER_CU
8784 too early. */
89e63ee4
DE
8785 if (dependent_cu != NULL)
8786 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8787
8788 /* If it's already on the queue, we have nothing to do. */
8789 if (per_cu->queued)
8790 return 0;
8791
8792 /* If the compilation unit is already loaded, just mark it as
8793 used. */
8794 if (per_cu->cu != NULL)
8795 {
8796 per_cu->cu->last_used = 0;
8797 return 0;
8798 }
8799
8800 /* Add it to the queue. */
8801 queue_comp_unit (per_cu, pretend_language);
8802
8803 return 1;
8804}
8805
10b3939b
DJ
8806/* Process the queue. */
8807
8808static void
ed2dc618 8809process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8810{
b4f54984 8811 if (dwarf_read_debug)
45cfd468
DE
8812 {
8813 fprintf_unfiltered (gdb_stdlog,
8814 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8815 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8816 }
8817
03dd20cc
DJ
8818 /* The queue starts out with one item, but following a DIE reference
8819 may load a new CU, adding it to the end of the queue. */
39856def 8820 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8821 {
39856def
TT
8822 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8823
cc12ce38 8824 if ((dwarf2_per_objfile->using_index
39856def
TT
8825 ? !item.per_cu->v.quick->compunit_symtab
8826 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8827 /* Skip dummy CUs. */
39856def 8828 && item.per_cu->cu != NULL)
f4dc4d17 8829 {
39856def 8830 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8831 unsigned int debug_print_threshold;
247f5c4f 8832 char buf[100];
f4dc4d17 8833
247f5c4f 8834 if (per_cu->is_debug_types)
f4dc4d17 8835 {
247f5c4f
DE
8836 struct signatured_type *sig_type =
8837 (struct signatured_type *) per_cu;
8838
9d8780f0 8839 sprintf (buf, "TU %s at offset %s",
73be47f5 8840 hex_string (sig_type->signature),
9d8780f0 8841 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8842 /* There can be 100s of TUs.
8843 Only print them in verbose mode. */
8844 debug_print_threshold = 2;
f4dc4d17 8845 }
247f5c4f 8846 else
73be47f5 8847 {
9d8780f0
SM
8848 sprintf (buf, "CU at offset %s",
8849 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8850 debug_print_threshold = 1;
8851 }
247f5c4f 8852
b4f54984 8853 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8854 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8855
8856 if (per_cu->is_debug_types)
39856def 8857 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8858 else
39856def 8859 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8860
b4f54984 8861 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8862 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8863 }
10b3939b 8864
39856def
TT
8865 item.per_cu->queued = 0;
8866 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8867 }
8868
b4f54984 8869 if (dwarf_read_debug)
45cfd468
DE
8870 {
8871 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8872 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8873 }
10b3939b
DJ
8874}
8875
10b3939b
DJ
8876/* Read in full symbols for PST, and anything it depends on. */
8877
8566b89b
TT
8878void
8879dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8880{
194d088f 8881 gdb_assert (!readin);
95554aad 8882
48993951 8883 expand_dependencies (objfile);
aaa75496 8884
b83470bf
TT
8885 dw2_do_instantiate_symtab (per_cu_data, false);
8886 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8887}
8888
dee91e82
DE
8889/* Trivial hash function for die_info: the hash value of a DIE
8890 is its offset in .debug_info for this objfile. */
10b3939b 8891
dee91e82
DE
8892static hashval_t
8893die_hash (const void *item)
10b3939b 8894{
9a3c8263 8895 const struct die_info *die = (const struct die_info *) item;
6502dd73 8896
9c541725 8897 return to_underlying (die->sect_off);
dee91e82 8898}
63d06c5c 8899
dee91e82
DE
8900/* Trivial comparison function for die_info structures: two DIEs
8901 are equal if they have the same offset. */
98bfdba5 8902
dee91e82
DE
8903static int
8904die_eq (const void *item_lhs, const void *item_rhs)
8905{
9a3c8263
SM
8906 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8907 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8908
9c541725 8909 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8910}
c906108c 8911
c0ab21c2 8912/* Load the DIEs associated with PER_CU into memory. */
c906108c 8913
dee91e82 8914static void
c0ab21c2
TT
8915load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8916 bool skip_partial,
8917 enum language pretend_language)
dee91e82 8918{
c0ab21c2
TT
8919 gdb_assert (! this_cu->is_debug_types);
8920
6751ebae 8921 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8922 if (reader.dummy_p)
8923 return;
8924
8925 struct dwarf2_cu *cu = reader.cu;
8926 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8927
dee91e82
DE
8928 gdb_assert (cu->die_hash == NULL);
8929 cu->die_hash =
8930 htab_create_alloc_ex (cu->header.length / 12,
8931 die_hash,
8932 die_eq,
8933 NULL,
8934 &cu->comp_unit_obstack,
8935 hashtab_obstack_allocate,
8936 dummy_obstack_deallocate);
e142c38c 8937
3e225074 8938 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8939 reader.comp_unit_die->child
8940 = read_die_and_siblings (&reader, reader.info_ptr,
8941 &info_ptr, reader.comp_unit_die);
8942 cu->dies = reader.comp_unit_die;
dee91e82 8943 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8944
8945 /* We try not to read any attributes in this function, because not
9cdd5dbd 8946 all CUs needed for references have been loaded yet, and symbol
10b3939b 8947 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8948 or we won't be able to build types correctly.
8949 Similarly, if we do not read the producer, we can not apply
8950 producer-specific interpretation. */
c0ab21c2 8951 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8952
8953 reader.keep ();
10b3939b
DJ
8954}
8955
3da10d80
KS
8956/* Add a DIE to the delayed physname list. */
8957
8958static void
8959add_to_method_list (struct type *type, int fnfield_index, int index,
8960 const char *name, struct die_info *die,
8961 struct dwarf2_cu *cu)
8962{
8963 struct delayed_method_info mi;
8964 mi.type = type;
8965 mi.fnfield_index = fnfield_index;
8966 mi.index = index;
8967 mi.name = name;
8968 mi.die = die;
c89b44cd 8969 cu->method_list.push_back (mi);
3da10d80
KS
8970}
8971
3693fdb3
PA
8972/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8973 "const" / "volatile". If so, decrements LEN by the length of the
8974 modifier and return true. Otherwise return false. */
8975
8976template<size_t N>
8977static bool
8978check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8979{
8980 size_t mod_len = sizeof (mod) - 1;
8981 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8982 {
8983 len -= mod_len;
8984 return true;
8985 }
8986 return false;
8987}
8988
3da10d80
KS
8989/* Compute the physnames of any methods on the CU's method list.
8990
8991 The computation of method physnames is delayed in order to avoid the
8992 (bad) condition that one of the method's formal parameters is of an as yet
8993 incomplete type. */
8994
8995static void
8996compute_delayed_physnames (struct dwarf2_cu *cu)
8997{
3693fdb3 8998 /* Only C++ delays computing physnames. */
c89b44cd 8999 if (cu->method_list.empty ())
3693fdb3
PA
9000 return;
9001 gdb_assert (cu->language == language_cplus);
9002
52941706 9003 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9004 {
1d06ead6 9005 const char *physname;
3da10d80 9006 struct fn_fieldlist *fn_flp
c89b44cd
TT
9007 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9008 physname = dwarf2_physname (mi.name, mi.die, cu);
9009 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9010 = physname ? physname : "";
3693fdb3
PA
9011
9012 /* Since there's no tag to indicate whether a method is a
9013 const/volatile overload, extract that information out of the
9014 demangled name. */
9015 if (physname != NULL)
9016 {
9017 size_t len = strlen (physname);
9018
9019 while (1)
9020 {
9021 if (physname[len] == ')') /* shortcut */
9022 break;
9023 else if (check_modifier (physname, len, " const"))
c89b44cd 9024 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9025 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9026 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9027 else
9028 break;
9029 }
9030 }
3da10d80 9031 }
c89b44cd
TT
9032
9033 /* The list is no longer needed. */
9034 cu->method_list.clear ();
3da10d80
KS
9035}
9036
a766d390
DE
9037/* Go objects should be embedded in a DW_TAG_module DIE,
9038 and it's not clear if/how imported objects will appear.
9039 To keep Go support simple until that's worked out,
9040 go back through what we've read and create something usable.
9041 We could do this while processing each DIE, and feels kinda cleaner,
9042 but that way is more invasive.
9043 This is to, for example, allow the user to type "p var" or "b main"
9044 without having to specify the package name, and allow lookups
9045 of module.object to work in contexts that use the expression
9046 parser. */
9047
9048static void
9049fixup_go_packaging (struct dwarf2_cu *cu)
9050{
421d1616 9051 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9052 struct pending *list;
9053 int i;
9054
c24bdb02 9055 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9056 list != NULL;
9057 list = list->next)
a766d390
DE
9058 {
9059 for (i = 0; i < list->nsyms; ++i)
9060 {
9061 struct symbol *sym = list->symbol[i];
9062
c1b5c1eb 9063 if (sym->language () == language_go
a766d390
DE
9064 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9065 {
421d1616
TT
9066 gdb::unique_xmalloc_ptr<char> this_package_name
9067 (go_symbol_package_name (sym));
a766d390
DE
9068
9069 if (this_package_name == NULL)
9070 continue;
9071 if (package_name == NULL)
421d1616 9072 package_name = std::move (this_package_name);
a766d390
DE
9073 else
9074 {
518817b3
SM
9075 struct objfile *objfile
9076 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9077 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9078 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9079 (symbol_symtab (sym) != NULL
9080 ? symtab_to_filename_for_display
9081 (symbol_symtab (sym))
e3b94546 9082 : objfile_name (objfile)),
421d1616 9083 this_package_name.get (), package_name.get ());
a766d390
DE
9084 }
9085 }
9086 }
9087 }
9088
9089 if (package_name != NULL)
9090 {
518817b3 9091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9092 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9093 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9094 saved_package_name);
a766d390
DE
9095 struct symbol *sym;
9096
e623cf5d 9097 sym = allocate_symbol (objfile);
d3ecddab 9098 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9099 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9100 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9101 e.g., "main" finds the "main" module and not C's main(). */
9102 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9103 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9104 SYMBOL_TYPE (sym) = type;
9105
c24bdb02 9106 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9107 }
9108}
9109
c9317f21
TT
9110/* Allocate a fully-qualified name consisting of the two parts on the
9111 obstack. */
9112
9113static const char *
9114rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9115{
9116 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9117}
9118
9119/* A helper that allocates a struct discriminant_info to attach to a
9120 union type. */
9121
9122static struct discriminant_info *
9123alloc_discriminant_info (struct type *type, int discriminant_index,
9124 int default_index)
9125{
9126 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9127 gdb_assert (discriminant_index == -1
9128 || (discriminant_index >= 0
9129 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9130 gdb_assert (default_index == -1
c7b15a66 9131 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9132
9133 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9134
9135 struct discriminant_info *disc
9136 = ((struct discriminant_info *)
9137 TYPE_ZALLOC (type,
9138 offsetof (struct discriminant_info, discriminants)
9139 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9140 disc->default_index = default_index;
9141 disc->discriminant_index = discriminant_index;
9142
9143 struct dynamic_prop prop;
9144 prop.kind = PROP_UNDEFINED;
9145 prop.data.baton = disc;
9146
9147 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9148
9149 return disc;
9150}
9151
9152/* Some versions of rustc emitted enums in an unusual way.
9153
9154 Ordinary enums were emitted as unions. The first element of each
9155 structure in the union was named "RUST$ENUM$DISR". This element
9156 held the discriminant.
9157
9158 These versions of Rust also implemented the "non-zero"
9159 optimization. When the enum had two values, and one is empty and
9160 the other holds a pointer that cannot be zero, the pointer is used
9161 as the discriminant, with a zero value meaning the empty variant.
9162 Here, the union's first member is of the form
9163 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9164 where the fieldnos are the indices of the fields that should be
9165 traversed in order to find the field (which may be several fields deep)
9166 and the variantname is the name of the variant of the case when the
9167 field is zero.
9168
9169 This function recognizes whether TYPE is of one of these forms,
9170 and, if so, smashes it to be a variant type. */
9171
9172static void
9173quirk_rust_enum (struct type *type, struct objfile *objfile)
9174{
9175 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9176
9177 /* We don't need to deal with empty enums. */
9178 if (TYPE_NFIELDS (type) == 0)
9179 return;
9180
9181#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9182 if (TYPE_NFIELDS (type) == 1
9183 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9184 {
9185 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9186
9187 /* Decode the field name to find the offset of the
9188 discriminant. */
9189 ULONGEST bit_offset = 0;
9190 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9191 while (name[0] >= '0' && name[0] <= '9')
9192 {
9193 char *tail;
9194 unsigned long index = strtoul (name, &tail, 10);
9195 name = tail;
9196 if (*name != '$'
9197 || index >= TYPE_NFIELDS (field_type)
9198 || (TYPE_FIELD_LOC_KIND (field_type, index)
9199 != FIELD_LOC_KIND_BITPOS))
9200 {
b98664d3 9201 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9202 "[in module %s]"),
9203 TYPE_FIELD_NAME (type, 0),
9204 objfile_name (objfile));
9205 return;
9206 }
9207 ++name;
9208
9209 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9210 field_type = TYPE_FIELD_TYPE (field_type, index);
9211 }
9212
9213 /* Make a union to hold the variants. */
9214 struct type *union_type = alloc_type (objfile);
9215 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9216 TYPE_NFIELDS (union_type) = 3;
9217 TYPE_FIELDS (union_type)
9218 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9219 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9220 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9221
9222 /* Put the discriminant must at index 0. */
9223 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9224 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9225 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9226 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9227
9228 /* The order of fields doesn't really matter, so put the real
9229 field at index 1 and the data-less field at index 2. */
9230 struct discriminant_info *disc
9231 = alloc_discriminant_info (union_type, 0, 1);
9232 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9233 TYPE_FIELD_NAME (union_type, 1)
9234 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9235 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9236 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9237 TYPE_FIELD_NAME (union_type, 1));
9238
9239 const char *dataless_name
9240 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9241 name);
9242 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9243 dataless_name);
9244 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9245 /* NAME points into the original discriminant name, which
9246 already has the correct lifetime. */
9247 TYPE_FIELD_NAME (union_type, 2) = name;
9248 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9249 disc->discriminants[2] = 0;
9250
9251 /* Smash this type to be a structure type. We have to do this
9252 because the type has already been recorded. */
9253 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9254 TYPE_NFIELDS (type) = 1;
9255 TYPE_FIELDS (type)
9256 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9257
9258 /* Install the variant part. */
9259 TYPE_FIELD_TYPE (type, 0) = union_type;
9260 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9261 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9262 }
77c2dba3
TT
9263 /* A union with a single anonymous field is probably an old-style
9264 univariant enum. */
9265 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9266 {
c9317f21
TT
9267 /* Smash this type to be a structure type. We have to do this
9268 because the type has already been recorded. */
9269 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9270
9271 /* Make a union to hold the variants. */
9272 struct type *union_type = alloc_type (objfile);
9273 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9274 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9275 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9276 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9277 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9278
9279 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9280 const char *variant_name
9281 = rust_last_path_segment (TYPE_NAME (field_type));
9282 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9283 TYPE_NAME (field_type)
9284 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9285 TYPE_NAME (type), variant_name);
c9317f21
TT
9286
9287 /* Install the union in the outer struct type. */
9288 TYPE_NFIELDS (type) = 1;
9289 TYPE_FIELDS (type)
9290 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9291 TYPE_FIELD_TYPE (type, 0) = union_type;
9292 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9293 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9294
9295 alloc_discriminant_info (union_type, -1, 0);
9296 }
9297 else
9298 {
9299 struct type *disr_type = nullptr;
9300 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9301 {
9302 disr_type = TYPE_FIELD_TYPE (type, i);
9303
a037790e
TT
9304 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9305 {
9306 /* All fields of a true enum will be structs. */
9307 return;
9308 }
9309 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9310 {
9311 /* Could be data-less variant, so keep going. */
a037790e 9312 disr_type = nullptr;
c9317f21
TT
9313 }
9314 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9315 "RUST$ENUM$DISR") != 0)
9316 {
9317 /* Not a Rust enum. */
9318 return;
9319 }
9320 else
9321 {
9322 /* Found one. */
9323 break;
9324 }
9325 }
9326
9327 /* If we got here without a discriminant, then it's probably
9328 just a union. */
9329 if (disr_type == nullptr)
9330 return;
9331
9332 /* Smash this type to be a structure type. We have to do this
9333 because the type has already been recorded. */
9334 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9335
9336 /* Make a union to hold the variants. */
9337 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9338 struct type *union_type = alloc_type (objfile);
9339 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9340 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9341 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9342 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9343 TYPE_FIELDS (union_type)
9344 = (struct field *) TYPE_ZALLOC (union_type,
9345 (TYPE_NFIELDS (union_type)
9346 * sizeof (struct field)));
9347
9348 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9349 TYPE_NFIELDS (type) * sizeof (struct field));
9350
9351 /* Install the discriminant at index 0 in the union. */
9352 TYPE_FIELD (union_type, 0) = *disr_field;
9353 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9354 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9355
9356 /* Install the union in the outer struct type. */
9357 TYPE_FIELD_TYPE (type, 0) = union_type;
9358 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9359 TYPE_NFIELDS (type) = 1;
9360
9361 /* Set the size and offset of the union type. */
9362 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9363
9364 /* We need a way to find the correct discriminant given a
9365 variant name. For convenience we build a map here. */
9366 struct type *enum_type = FIELD_TYPE (*disr_field);
9367 std::unordered_map<std::string, ULONGEST> discriminant_map;
9368 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9369 {
9370 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9371 {
9372 const char *name
9373 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9374 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9375 }
9376 }
9377
9378 int n_fields = TYPE_NFIELDS (union_type);
9379 struct discriminant_info *disc
9380 = alloc_discriminant_info (union_type, 0, -1);
9381 /* Skip the discriminant here. */
9382 for (int i = 1; i < n_fields; ++i)
9383 {
9384 /* Find the final word in the name of this variant's type.
9385 That name can be used to look up the correct
9386 discriminant. */
9387 const char *variant_name
9388 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9389 i)));
9390
9391 auto iter = discriminant_map.find (variant_name);
9392 if (iter != discriminant_map.end ())
9393 disc->discriminants[i] = iter->second;
9394
bedda9ac 9395 /* Remove the discriminant field, if it exists. */
c9317f21 9396 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9397 if (TYPE_NFIELDS (sub_type) > 0)
9398 {
9399 --TYPE_NFIELDS (sub_type);
9400 ++TYPE_FIELDS (sub_type);
9401 }
c9317f21
TT
9402 TYPE_FIELD_NAME (union_type, i) = variant_name;
9403 TYPE_NAME (sub_type)
9404 = rust_fully_qualify (&objfile->objfile_obstack,
9405 TYPE_NAME (type), variant_name);
9406 }
9407 }
9408}
9409
9410/* Rewrite some Rust unions to be structures with variants parts. */
9411
9412static void
9413rust_union_quirks (struct dwarf2_cu *cu)
9414{
9415 gdb_assert (cu->language == language_rust);
52941706
SM
9416 for (type *type_ : cu->rust_unions)
9417 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9418 /* We don't need this any more. */
9419 cu->rust_unions.clear ();
c9317f21
TT
9420}
9421
95554aad
TT
9422/* Return the symtab for PER_CU. This works properly regardless of
9423 whether we're using the index or psymtabs. */
9424
43f3e411
DE
9425static struct compunit_symtab *
9426get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9427{
ed2dc618 9428 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9429 ? per_cu->v.quick->compunit_symtab
9430 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9431}
9432
9433/* A helper function for computing the list of all symbol tables
9434 included by PER_CU. */
9435
9436static void
4c39bc03 9437recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9438 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9439 struct dwarf2_per_cu_data *per_cu,
43f3e411 9440 struct compunit_symtab *immediate_parent)
95554aad
TT
9441{
9442 void **slot;
43f3e411 9443 struct compunit_symtab *cust;
95554aad
TT
9444
9445 slot = htab_find_slot (all_children, per_cu, INSERT);
9446 if (*slot != NULL)
9447 {
9448 /* This inclusion and its children have been processed. */
9449 return;
9450 }
9451
9452 *slot = per_cu;
9453 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9454 cust = get_compunit_symtab (per_cu);
9455 if (cust != NULL)
ec94af83
DE
9456 {
9457 /* If this is a type unit only add its symbol table if we haven't
9458 seen it yet (type unit per_cu's can share symtabs). */
9459 if (per_cu->is_debug_types)
9460 {
43f3e411 9461 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9462 if (*slot == NULL)
9463 {
43f3e411 9464 *slot = cust;
4c39bc03 9465 result->push_back (cust);
43f3e411
DE
9466 if (cust->user == NULL)
9467 cust->user = immediate_parent;
ec94af83
DE
9468 }
9469 }
9470 else
f9125b6c 9471 {
4c39bc03 9472 result->push_back (cust);
43f3e411
DE
9473 if (cust->user == NULL)
9474 cust->user = immediate_parent;
f9125b6c 9475 }
ec94af83 9476 }
95554aad 9477
ae640021
AB
9478 if (!per_cu->imported_symtabs_empty ())
9479 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9480 {
9481 recursively_compute_inclusions (result, all_children,
9482 all_type_symtabs, ptr, cust);
9483 }
95554aad
TT
9484}
9485
43f3e411 9486/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9487 PER_CU. */
9488
9489static void
43f3e411 9490compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9491{
f4dc4d17
DE
9492 gdb_assert (! per_cu->is_debug_types);
9493
ae640021 9494 if (!per_cu->imported_symtabs_empty ())
95554aad 9495 {
ae640021 9496 int len;
4c39bc03 9497 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9498 htab_t all_children, all_type_symtabs;
43f3e411 9499 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9500
9501 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9502 if (cust == NULL)
95554aad
TT
9503 return;
9504
9505 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9506 NULL, xcalloc, xfree);
ec94af83
DE
9507 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9508 NULL, xcalloc, xfree);
95554aad 9509
ae640021 9510 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9511 {
9512 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9513 all_type_symtabs, ptr, cust);
ec94af83 9514 }
95554aad 9515
ec94af83 9516 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9517 len = result_symtabs.size ();
43f3e411 9518 cust->includes
ed2dc618 9519 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9520 struct compunit_symtab *, len + 1);
4c39bc03
TT
9521 memcpy (cust->includes, result_symtabs.data (),
9522 len * sizeof (compunit_symtab *));
43f3e411 9523 cust->includes[len] = NULL;
95554aad 9524
95554aad 9525 htab_delete (all_children);
ec94af83 9526 htab_delete (all_type_symtabs);
95554aad
TT
9527 }
9528}
9529
9530/* Compute the 'includes' field for the symtabs of all the CUs we just
9531 read. */
9532
9533static void
ed2dc618 9534process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9535{
71b73764 9536 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9537 {
9538 if (! iter->is_debug_types)
43f3e411 9539 compute_compunit_symtab_includes (iter);
f4dc4d17 9540 }
95554aad 9541
c5d0225d 9542 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9543}
9544
9cdd5dbd 9545/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9546 already been loaded into memory. */
9547
9548static void
95554aad
TT
9549process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9550 enum language pretend_language)
10b3939b 9551{
10b3939b 9552 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9553 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9554 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9555 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9556 CORE_ADDR lowpc, highpc;
43f3e411 9557 struct compunit_symtab *cust;
10b3939b 9558 CORE_ADDR baseaddr;
4359dff1 9559 struct block *static_block;
3e29f34a 9560 CORE_ADDR addr;
10b3939b 9561
b3b3bada 9562 baseaddr = objfile->text_section_offset ();
10b3939b 9563
c89b44cd
TT
9564 /* Clear the list here in case something was left over. */
9565 cu->method_list.clear ();
10b3939b 9566
95554aad
TT
9567 cu->language = pretend_language;
9568 cu->language_defn = language_def (cu->language);
9569
c906108c 9570 /* Do line number decoding in read_file_scope () */
10b3939b 9571 process_die (cu->dies, cu);
c906108c 9572
a766d390
DE
9573 /* For now fudge the Go package. */
9574 if (cu->language == language_go)
9575 fixup_go_packaging (cu);
9576
5f48f8f3 9577 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9578 should be complete, and it should now be safe to compute all of the
9579 physnames. */
9580 compute_delayed_physnames (cu);
3da10d80 9581
c9317f21
TT
9582 if (cu->language == language_rust)
9583 rust_union_quirks (cu);
9584
fae299cd
DC
9585 /* Some compilers don't define a DW_AT_high_pc attribute for the
9586 compilation unit. If the DW_AT_high_pc is missing, synthesize
9587 it, by scanning the DIE's below the compilation unit. */
10b3939b 9588 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9589
3e29f34a 9590 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9591 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9592
9593 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9594 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9595 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9596 addrmap to help ensure it has an accurate map of pc values belonging to
9597 this comp unit. */
9598 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9599
c24bdb02 9600 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9601 SECT_OFF_TEXT (objfile),
9602 0);
c906108c 9603
43f3e411 9604 if (cust != NULL)
c906108c 9605 {
df15bd07 9606 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9607
8be455d7
JK
9608 /* Set symtab language to language from DW_AT_language. If the
9609 compilation is from a C file generated by language preprocessors, do
9610 not set the language if it was already deduced by start_subfile. */
43f3e411 9611 if (!(cu->language == language_c
40e3ad0e 9612 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9613 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9614
9615 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9616 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9617 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9618 there were bugs in prologue debug info, fixed later in GCC-4.5
9619 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9620
9621 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9622 needed, it would be wrong due to missing DW_AT_producer there.
9623
9624 Still one can confuse GDB by using non-standard GCC compilation
9625 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9626 */
ab260dad 9627 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9628 cust->locations_valid = 1;
e0d00bc7
JK
9629
9630 if (gcc_4_minor >= 5)
43f3e411 9631 cust->epilogue_unwind_valid = 1;
96408a79 9632
43f3e411 9633 cust->call_site_htab = cu->call_site_htab;
c906108c 9634 }
9291a0cd
TT
9635
9636 if (dwarf2_per_objfile->using_index)
43f3e411 9637 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9638 else
9639 {
891813be 9640 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9641 pst->compunit_symtab = cust;
6d94535f 9642 pst->readin = true;
9291a0cd 9643 }
c906108c 9644
95554aad 9645 /* Push it for inclusion processing later. */
c5d0225d 9646 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9647
9648 /* Not needed any more. */
c24bdb02 9649 cu->reset_builder ();
f4dc4d17 9650}
45cfd468 9651
f4dc4d17
DE
9652/* Generate full symbol information for type unit PER_CU, whose DIEs have
9653 already been loaded into memory. */
9654
9655static void
9656process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9657 enum language pretend_language)
9658{
9659 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9660 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9661 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9662 struct compunit_symtab *cust;
0186c6a7
DE
9663 struct signatured_type *sig_type;
9664
9665 gdb_assert (per_cu->is_debug_types);
9666 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9667
c89b44cd
TT
9668 /* Clear the list here in case something was left over. */
9669 cu->method_list.clear ();
f4dc4d17 9670
f4dc4d17
DE
9671 cu->language = pretend_language;
9672 cu->language_defn = language_def (cu->language);
9673
9674 /* The symbol tables are set up in read_type_unit_scope. */
9675 process_die (cu->dies, cu);
9676
9677 /* For now fudge the Go package. */
9678 if (cu->language == language_go)
9679 fixup_go_packaging (cu);
9680
5f48f8f3 9681 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9682 should be complete, and it should now be safe to compute all of the
9683 physnames. */
9684 compute_delayed_physnames (cu);
f4dc4d17 9685
c9317f21
TT
9686 if (cu->language == language_rust)
9687 rust_union_quirks (cu);
9688
f4dc4d17
DE
9689 /* TUs share symbol tables.
9690 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9691 of it with end_expandable_symtab. Otherwise, complete the addition of
9692 this TU's symbols to the existing symtab. */
43f3e411 9693 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9694 {
c24bdb02
KS
9695 buildsym_compunit *builder = cu->get_builder ();
9696 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9697 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9698
43f3e411 9699 if (cust != NULL)
f4dc4d17
DE
9700 {
9701 /* Set symtab language to language from DW_AT_language. If the
9702 compilation is from a C file generated by language preprocessors,
9703 do not set the language if it was already deduced by
9704 start_subfile. */
43f3e411
DE
9705 if (!(cu->language == language_c
9706 && COMPUNIT_FILETABS (cust)->language != language_c))
9707 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9708 }
9709 }
9710 else
9711 {
c24bdb02 9712 cu->get_builder ()->augment_type_symtab ();
43f3e411 9713 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9714 }
9715
9716 if (dwarf2_per_objfile->using_index)
43f3e411 9717 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9718 else
9719 {
891813be 9720 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9721 pst->compunit_symtab = cust;
6d94535f 9722 pst->readin = true;
45cfd468 9723 }
804d2729
TT
9724
9725 /* Not needed any more. */
c24bdb02 9726 cu->reset_builder ();
c906108c
SS
9727}
9728
95554aad
TT
9729/* Process an imported unit DIE. */
9730
9731static void
9732process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9733{
9734 struct attribute *attr;
9735
f4dc4d17
DE
9736 /* For now we don't handle imported units in type units. */
9737 if (cu->per_cu->is_debug_types)
9738 {
9739 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9740 " supported in type units [in module %s]"),
518817b3 9741 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9742 }
9743
95554aad
TT
9744 attr = dwarf2_attr (die, DW_AT_import, cu);
9745 if (attr != NULL)
9746 {
0826b30a 9747 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9748 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9749 dwarf2_per_cu_data *per_cu
e3b94546 9750 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9751 cu->per_cu->dwarf2_per_objfile);
95554aad 9752
58990295
TV
9753 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9754 into another compilation unit, at root level. Regard this as a hint,
9755 and ignore it. */
9756 if (die->parent && die->parent->parent == NULL
9757 && per_cu->unit_type == DW_UT_compile
9758 && per_cu->lang == language_cplus)
9759 return;
9760
69d751e3 9761 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9762 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9763 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9764
ae640021 9765 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9766 }
9767}
9768
4c8aa72d
PA
9769/* RAII object that represents a process_die scope: i.e.,
9770 starts/finishes processing a DIE. */
9771class process_die_scope
adde2bff 9772{
4c8aa72d
PA
9773public:
9774 process_die_scope (die_info *die, dwarf2_cu *cu)
9775 : m_die (die), m_cu (cu)
9776 {
9777 /* We should only be processing DIEs not already in process. */
9778 gdb_assert (!m_die->in_process);
9779 m_die->in_process = true;
9780 }
8c3cb9fa 9781
4c8aa72d
PA
9782 ~process_die_scope ()
9783 {
9784 m_die->in_process = false;
9785
9786 /* If we're done processing the DIE for the CU that owns the line
9787 header, we don't need the line header anymore. */
9788 if (m_cu->line_header_die_owner == m_die)
9789 {
9790 delete m_cu->line_header;
9791 m_cu->line_header = NULL;
9792 m_cu->line_header_die_owner = NULL;
9793 }
9794 }
9795
9796private:
9797 die_info *m_die;
9798 dwarf2_cu *m_cu;
9799};
adde2bff 9800
c906108c
SS
9801/* Process a die and its children. */
9802
9803static void
e7c27a73 9804process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9805{
4c8aa72d 9806 process_die_scope scope (die, cu);
adde2bff 9807
c906108c
SS
9808 switch (die->tag)
9809 {
9810 case DW_TAG_padding:
9811 break;
9812 case DW_TAG_compile_unit:
95554aad 9813 case DW_TAG_partial_unit:
e7c27a73 9814 read_file_scope (die, cu);
c906108c 9815 break;
348e048f
DE
9816 case DW_TAG_type_unit:
9817 read_type_unit_scope (die, cu);
9818 break;
c906108c 9819 case DW_TAG_subprogram:
0a4b0913
AB
9820 /* Nested subprograms in Fortran get a prefix. */
9821 if (cu->language == language_fortran
9822 && die->parent != NULL
9823 && die->parent->tag == DW_TAG_subprogram)
9824 cu->processing_has_namespace_info = true;
9825 /* Fall through. */
c906108c 9826 case DW_TAG_inlined_subroutine:
edb3359d 9827 read_func_scope (die, cu);
c906108c
SS
9828 break;
9829 case DW_TAG_lexical_block:
14898363
L
9830 case DW_TAG_try_block:
9831 case DW_TAG_catch_block:
e7c27a73 9832 read_lexical_block_scope (die, cu);
c906108c 9833 break;
216f72a1 9834 case DW_TAG_call_site:
96408a79
SA
9835 case DW_TAG_GNU_call_site:
9836 read_call_site_scope (die, cu);
9837 break;
c906108c 9838 case DW_TAG_class_type:
680b30c7 9839 case DW_TAG_interface_type:
c906108c
SS
9840 case DW_TAG_structure_type:
9841 case DW_TAG_union_type:
134d01f1 9842 process_structure_scope (die, cu);
c906108c
SS
9843 break;
9844 case DW_TAG_enumeration_type:
134d01f1 9845 process_enumeration_scope (die, cu);
c906108c 9846 break;
134d01f1 9847
f792889a
DJ
9848 /* These dies have a type, but processing them does not create
9849 a symbol or recurse to process the children. Therefore we can
9850 read them on-demand through read_type_die. */
c906108c 9851 case DW_TAG_subroutine_type:
72019c9c 9852 case DW_TAG_set_type:
c906108c 9853 case DW_TAG_array_type:
c906108c 9854 case DW_TAG_pointer_type:
c906108c 9855 case DW_TAG_ptr_to_member_type:
c906108c 9856 case DW_TAG_reference_type:
4297a3f0 9857 case DW_TAG_rvalue_reference_type:
c906108c 9858 case DW_TAG_string_type:
c906108c 9859 break;
134d01f1 9860
c906108c 9861 case DW_TAG_base_type:
a02abb62 9862 case DW_TAG_subrange_type:
cb249c71 9863 case DW_TAG_typedef:
134d01f1
DJ
9864 /* Add a typedef symbol for the type definition, if it has a
9865 DW_AT_name. */
f792889a 9866 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9867 break;
c906108c 9868 case DW_TAG_common_block:
e7c27a73 9869 read_common_block (die, cu);
c906108c
SS
9870 break;
9871 case DW_TAG_common_inclusion:
9872 break;
d9fa45fe 9873 case DW_TAG_namespace:
9068261f 9874 cu->processing_has_namespace_info = true;
e7c27a73 9875 read_namespace (die, cu);
d9fa45fe 9876 break;
5d7cb8df 9877 case DW_TAG_module:
9068261f 9878 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9879 read_module (die, cu);
9880 break;
d9fa45fe 9881 case DW_TAG_imported_declaration:
9068261f 9882 cu->processing_has_namespace_info = true;
74921315
KS
9883 if (read_namespace_alias (die, cu))
9884 break;
86a73007
TT
9885 /* The declaration is not a global namespace alias. */
9886 /* Fall through. */
d9fa45fe 9887 case DW_TAG_imported_module:
9068261f 9888 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9889 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9890 || cu->language != language_fortran))
b98664d3 9891 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9892 dwarf_tag_name (die->tag));
9893 read_import_statement (die, cu);
d9fa45fe 9894 break;
95554aad
TT
9895
9896 case DW_TAG_imported_unit:
9897 process_imported_unit_die (die, cu);
9898 break;
9899
71a3c369
TT
9900 case DW_TAG_variable:
9901 read_variable (die, cu);
9902 break;
9903
c906108c 9904 default:
e7c27a73 9905 new_symbol (die, NULL, cu);
c906108c
SS
9906 break;
9907 }
9908}
ca69b9e6
DE
9909\f
9910/* DWARF name computation. */
c906108c 9911
94af9270
KS
9912/* A helper function for dwarf2_compute_name which determines whether DIE
9913 needs to have the name of the scope prepended to the name listed in the
9914 die. */
9915
9916static int
9917die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9918{
1c809c68
TT
9919 struct attribute *attr;
9920
94af9270
KS
9921 switch (die->tag)
9922 {
9923 case DW_TAG_namespace:
9924 case DW_TAG_typedef:
9925 case DW_TAG_class_type:
9926 case DW_TAG_interface_type:
9927 case DW_TAG_structure_type:
9928 case DW_TAG_union_type:
9929 case DW_TAG_enumeration_type:
9930 case DW_TAG_enumerator:
9931 case DW_TAG_subprogram:
08a76f8a 9932 case DW_TAG_inlined_subroutine:
94af9270 9933 case DW_TAG_member:
74921315 9934 case DW_TAG_imported_declaration:
94af9270
KS
9935 return 1;
9936
9937 case DW_TAG_variable:
c2b0a229 9938 case DW_TAG_constant:
94af9270
KS
9939 /* We only need to prefix "globally" visible variables. These include
9940 any variable marked with DW_AT_external or any variable that
9941 lives in a namespace. [Variables in anonymous namespaces
9942 require prefixing, but they are not DW_AT_external.] */
9943
9944 if (dwarf2_attr (die, DW_AT_specification, cu))
9945 {
9946 struct dwarf2_cu *spec_cu = cu;
9a619af0 9947
94af9270
KS
9948 return die_needs_namespace (die_specification (die, &spec_cu),
9949 spec_cu);
9950 }
9951
1c809c68 9952 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9953 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9954 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9955 return 0;
9956 /* A variable in a lexical block of some kind does not need a
9957 namespace, even though in C++ such variables may be external
9958 and have a mangled name. */
9959 if (die->parent->tag == DW_TAG_lexical_block
9960 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9961 || die->parent->tag == DW_TAG_catch_block
9962 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9963 return 0;
9964 return 1;
94af9270
KS
9965
9966 default:
9967 return 0;
9968 }
9969}
9970
73b9be8b
KS
9971/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9972 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9973 defined for the given DIE. */
9974
9975static struct attribute *
9976dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9977{
9978 struct attribute *attr;
9979
9980 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9981 if (attr == NULL)
9982 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9983
9984 return attr;
9985}
9986
9987/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9988 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9989 defined for the given DIE. */
9990
9991static const char *
9992dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9993{
9994 const char *linkage_name;
9995
9996 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9997 if (linkage_name == NULL)
9998 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9999
10000 return linkage_name;
10001}
10002
94af9270 10003/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10004 compute the physname for the object, which include a method's:
9c37b5ae 10005 - formal parameters (C++),
a766d390 10006 - receiver type (Go),
a766d390
DE
10007
10008 The term "physname" is a bit confusing.
10009 For C++, for example, it is the demangled name.
10010 For Go, for example, it's the mangled name.
94af9270 10011
af6b7be1
JB
10012 For Ada, return the DIE's linkage name rather than the fully qualified
10013 name. PHYSNAME is ignored..
10014
94af9270
KS
10015 The result is allocated on the objfile_obstack and canonicalized. */
10016
10017static const char *
15d034d0
TT
10018dwarf2_compute_name (const char *name,
10019 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10020 int physname)
10021{
518817b3 10022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10023
94af9270
KS
10024 if (name == NULL)
10025 name = dwarf2_name (die, cu);
10026
2ee7123e
DE
10027 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10028 but otherwise compute it by typename_concat inside GDB.
10029 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10030 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10031 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10032 will set the demangled name to the result of dwarf2_full_name, and it is
10033 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10034 if (cu->language == language_ada
10035 || (cu->language == language_fortran && physname))
10036 {
10037 /* For Ada unit, we prefer the linkage name over the name, as
10038 the former contains the exported name, which the user expects
10039 to be able to reference. Ideally, we want the user to be able
10040 to reference this entity using either natural or linkage name,
10041 but we haven't started looking at this enhancement yet. */
73b9be8b 10042 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10043
2ee7123e
DE
10044 if (linkage_name != NULL)
10045 return linkage_name;
f55ee35c
JK
10046 }
10047
94af9270
KS
10048 /* These are the only languages we know how to qualify names in. */
10049 if (name != NULL
9c37b5ae 10050 && (cu->language == language_cplus
c44af4eb
TT
10051 || cu->language == language_fortran || cu->language == language_d
10052 || cu->language == language_rust))
94af9270
KS
10053 {
10054 if (die_needs_namespace (die, cu))
10055 {
0d5cff50 10056 const char *prefix;
34a68019 10057 const char *canonical_name = NULL;
94af9270 10058
d7e74731
PA
10059 string_file buf;
10060
94af9270 10061 prefix = determine_prefix (die, cu);
94af9270
KS
10062 if (*prefix != '\0')
10063 {
43816ebc
TT
10064 gdb::unique_xmalloc_ptr<char> prefixed_name
10065 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10066
43816ebc 10067 buf.puts (prefixed_name.get ());
94af9270
KS
10068 }
10069 else
d7e74731 10070 buf.puts (name);
94af9270 10071
98bfdba5
PA
10072 /* Template parameters may be specified in the DIE's DW_AT_name, or
10073 as children with DW_TAG_template_type_param or
10074 DW_TAG_value_type_param. If the latter, add them to the name
10075 here. If the name already has template parameters, then
10076 skip this step; some versions of GCC emit both, and
10077 it is more efficient to use the pre-computed name.
10078
10079 Something to keep in mind about this process: it is very
10080 unlikely, or in some cases downright impossible, to produce
10081 something that will match the mangled name of a function.
10082 If the definition of the function has the same debug info,
10083 we should be able to match up with it anyway. But fallbacks
10084 using the minimal symbol, for instance to find a method
10085 implemented in a stripped copy of libstdc++, will not work.
10086 If we do not have debug info for the definition, we will have to
10087 match them up some other way.
10088
10089 When we do name matching there is a related problem with function
10090 templates; two instantiated function templates are allowed to
10091 differ only by their return types, which we do not add here. */
10092
10093 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10094 {
10095 struct attribute *attr;
10096 struct die_info *child;
10097 int first = 1;
10098
10099 die->building_fullname = 1;
10100
10101 for (child = die->child; child != NULL; child = child->sibling)
10102 {
10103 struct type *type;
12df843f 10104 LONGEST value;
d521ce57 10105 const gdb_byte *bytes;
98bfdba5
PA
10106 struct dwarf2_locexpr_baton *baton;
10107 struct value *v;
10108
10109 if (child->tag != DW_TAG_template_type_param
10110 && child->tag != DW_TAG_template_value_param)
10111 continue;
10112
10113 if (first)
10114 {
d7e74731 10115 buf.puts ("<");
98bfdba5
PA
10116 first = 0;
10117 }
10118 else
d7e74731 10119 buf.puts (", ");
98bfdba5
PA
10120
10121 attr = dwarf2_attr (child, DW_AT_type, cu);
10122 if (attr == NULL)
10123 {
b98664d3 10124 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10125 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10126 continue;
10127 }
10128 type = die_type (child, cu);
10129
10130 if (child->tag == DW_TAG_template_type_param)
10131 {
c1ec8cea
TT
10132 c_print_type (type, "", &buf, -1, 0, cu->language,
10133 &type_print_raw_options);
98bfdba5
PA
10134 continue;
10135 }
10136
10137 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10138 if (attr == NULL)
10139 {
b98664d3 10140 complaint (_("template parameter missing "
3e43a32a 10141 "DW_AT_const_value"));
d7e74731 10142 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10143 continue;
10144 }
10145
10146 dwarf2_const_value_attr (attr, type, name,
10147 &cu->comp_unit_obstack, cu,
10148 &value, &bytes, &baton);
10149
10150 if (TYPE_NOSIGN (type))
10151 /* GDB prints characters as NUMBER 'CHAR'. If that's
10152 changed, this can use value_print instead. */
d7e74731 10153 c_printchar (value, type, &buf);
98bfdba5
PA
10154 else
10155 {
10156 struct value_print_options opts;
10157
10158 if (baton != NULL)
10159 v = dwarf2_evaluate_loc_desc (type, NULL,
10160 baton->data,
10161 baton->size,
10162 baton->per_cu);
10163 else if (bytes != NULL)
10164 {
10165 v = allocate_value (type);
10166 memcpy (value_contents_writeable (v), bytes,
10167 TYPE_LENGTH (type));
10168 }
10169 else
10170 v = value_from_longest (type, value);
10171
3e43a32a
MS
10172 /* Specify decimal so that we do not depend on
10173 the radix. */
98bfdba5
PA
10174 get_formatted_print_options (&opts, 'd');
10175 opts.raw = 1;
d7e74731 10176 value_print (v, &buf, &opts);
98bfdba5 10177 release_value (v);
98bfdba5
PA
10178 }
10179 }
10180
10181 die->building_fullname = 0;
10182
10183 if (!first)
10184 {
10185 /* Close the argument list, with a space if necessary
10186 (nested templates). */
d7e74731
PA
10187 if (!buf.empty () && buf.string ().back () == '>')
10188 buf.puts (" >");
98bfdba5 10189 else
d7e74731 10190 buf.puts (">");
98bfdba5
PA
10191 }
10192 }
10193
9c37b5ae 10194 /* For C++ methods, append formal parameter type
94af9270 10195 information, if PHYSNAME. */
6e70227d 10196
94af9270 10197 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10198 && cu->language == language_cplus)
94af9270
KS
10199 {
10200 struct type *type = read_type_die (die, cu);
10201
d7e74731 10202 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10203 &type_print_raw_options);
94af9270 10204
9c37b5ae 10205 if (cu->language == language_cplus)
94af9270 10206 {
60430eff
DJ
10207 /* Assume that an artificial first parameter is
10208 "this", but do not crash if it is not. RealView
10209 marks unnamed (and thus unused) parameters as
10210 artificial; there is no way to differentiate
10211 the two cases. */
94af9270
KS
10212 if (TYPE_NFIELDS (type) > 0
10213 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10214 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10215 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10216 0))))
d7e74731 10217 buf.puts (" const");
94af9270
KS
10218 }
10219 }
10220
d7e74731 10221 const std::string &intermediate_name = buf.string ();
94af9270
KS
10222
10223 if (cu->language == language_cplus)
34a68019 10224 canonical_name
322a8516 10225 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10226 objfile);
34a68019
TT
10227
10228 /* If we only computed INTERMEDIATE_NAME, or if
10229 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10230 intern it. */
322a8516 10231 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10232 name = objfile->intern (intermediate_name);
34a68019
TT
10233 else
10234 name = canonical_name;
94af9270
KS
10235 }
10236 }
10237
10238 return name;
10239}
10240
0114d602
DJ
10241/* Return the fully qualified name of DIE, based on its DW_AT_name.
10242 If scope qualifiers are appropriate they will be added. The result
34a68019 10243 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10244 not have a name. NAME may either be from a previous call to
10245 dwarf2_name or NULL.
10246
9c37b5ae 10247 The output string will be canonicalized (if C++). */
0114d602
DJ
10248
10249static const char *
15d034d0 10250dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10251{
94af9270
KS
10252 return dwarf2_compute_name (name, die, cu, 0);
10253}
0114d602 10254
94af9270
KS
10255/* Construct a physname for the given DIE in CU. NAME may either be
10256 from a previous call to dwarf2_name or NULL. The result will be
10257 allocated on the objfile_objstack or NULL if the DIE does not have a
10258 name.
0114d602 10259
9c37b5ae 10260 The output string will be canonicalized (if C++). */
0114d602 10261
94af9270 10262static const char *
15d034d0 10263dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10264{
518817b3 10265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10266 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10267 int need_copy = 1;
10268
10269 /* In this case dwarf2_compute_name is just a shortcut not building anything
10270 on its own. */
10271 if (!die_needs_namespace (die, cu))
10272 return dwarf2_compute_name (name, die, cu, 1);
10273
73b9be8b 10274 mangled = dw2_linkage_name (die, cu);
900e11f9 10275
e98c9e7c
TT
10276 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10277 See https://github.com/rust-lang/rust/issues/32925. */
10278 if (cu->language == language_rust && mangled != NULL
10279 && strchr (mangled, '{') != NULL)
10280 mangled = NULL;
10281
900e11f9
JK
10282 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10283 has computed. */
791afaa2 10284 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10285 if (mangled != NULL)
900e11f9 10286 {
900e11f9 10287
59cc4834
JB
10288 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10289 {
10290 /* Do nothing (do not demangle the symbol name). */
10291 }
10292 else if (cu->language == language_go)
a766d390 10293 {
5e2db402
TT
10294 /* This is a lie, but we already lie to the caller new_symbol.
10295 new_symbol assumes we return the mangled name.
a766d390 10296 This just undoes that lie until things are cleaned up. */
a766d390
DE
10297 }
10298 else
10299 {
0eb876f5
JB
10300 /* Use DMGL_RET_DROP for C++ template functions to suppress
10301 their return type. It is easier for GDB users to search
10302 for such functions as `name(params)' than `long name(params)'.
10303 In such case the minimal symbol names do not match the full
10304 symbol names but for template functions there is never a need
10305 to look up their definition from their declaration so
10306 the only disadvantage remains the minimal symbol variant
10307 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10308 demangled.reset (gdb_demangle (mangled,
10309 (DMGL_PARAMS | DMGL_ANSI
10310 | DMGL_RET_DROP)));
a766d390 10311 }
900e11f9 10312 if (demangled)
791afaa2 10313 canon = demangled.get ();
900e11f9
JK
10314 else
10315 {
10316 canon = mangled;
10317 need_copy = 0;
10318 }
10319 }
10320
10321 if (canon == NULL || check_physname)
10322 {
10323 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10324
10325 if (canon != NULL && strcmp (physname, canon) != 0)
10326 {
10327 /* It may not mean a bug in GDB. The compiler could also
10328 compute DW_AT_linkage_name incorrectly. But in such case
10329 GDB would need to be bug-to-bug compatible. */
10330
b98664d3 10331 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10332 "(from linkage <%s>) - DIE at %s [in module %s]"),
10333 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10334 objfile_name (objfile));
900e11f9
JK
10335
10336 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10337 is available here - over computed PHYSNAME. It is safer
10338 against both buggy GDB and buggy compilers. */
10339
10340 retval = canon;
10341 }
10342 else
10343 {
10344 retval = physname;
10345 need_copy = 0;
10346 }
10347 }
10348 else
10349 retval = canon;
10350
10351 if (need_copy)
be1e3d3e 10352 retval = objfile->intern (retval);
900e11f9 10353
900e11f9 10354 return retval;
0114d602
DJ
10355}
10356
74921315
KS
10357/* Inspect DIE in CU for a namespace alias. If one exists, record
10358 a new symbol for it.
10359
10360 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10361
10362static int
10363read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10364{
10365 struct attribute *attr;
10366
10367 /* If the die does not have a name, this is not a namespace
10368 alias. */
10369 attr = dwarf2_attr (die, DW_AT_name, cu);
10370 if (attr != NULL)
10371 {
10372 int num;
10373 struct die_info *d = die;
10374 struct dwarf2_cu *imported_cu = cu;
10375
10376 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10377 keep inspecting DIEs until we hit the underlying import. */
10378#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10379 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10380 {
10381 attr = dwarf2_attr (d, DW_AT_import, cu);
10382 if (attr == NULL)
10383 break;
10384
10385 d = follow_die_ref (d, attr, &imported_cu);
10386 if (d->tag != DW_TAG_imported_declaration)
10387 break;
10388 }
10389
10390 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10391 {
b98664d3 10392 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10393 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10394 return 0;
10395 }
10396
10397 if (attr != NULL)
10398 {
10399 struct type *type;
0826b30a 10400 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10401
9c541725 10402 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10403 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10404 {
10405 /* This declaration is a global namespace alias. Add
10406 a symbol for it whose type is the aliased namespace. */
10407 new_symbol (die, type, cu);
10408 return 1;
10409 }
10410 }
10411 }
10412
10413 return 0;
10414}
10415
22cee43f 10416/* Return the using directives repository (global or local?) to use in the
804d2729 10417 current context for CU.
22cee43f
PMR
10418
10419 For Ada, imported declarations can materialize renamings, which *may* be
10420 global. However it is impossible (for now?) in DWARF to distinguish
10421 "external" imported declarations and "static" ones. As all imported
10422 declarations seem to be static in all other languages, make them all CU-wide
10423 global only in Ada. */
10424
10425static struct using_direct **
804d2729 10426using_directives (struct dwarf2_cu *cu)
22cee43f 10427{
c24bdb02
KS
10428 if (cu->language == language_ada
10429 && cu->get_builder ()->outermost_context_p ())
10430 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10431 else
c24bdb02 10432 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10433}
10434
27aa8d6a
SW
10435/* Read the import statement specified by the given die and record it. */
10436
10437static void
10438read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10439{
518817b3 10440 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10441 struct attribute *import_attr;
32019081 10442 struct die_info *imported_die, *child_die;
de4affc9 10443 struct dwarf2_cu *imported_cu;
27aa8d6a 10444 const char *imported_name;
794684b6 10445 const char *imported_name_prefix;
13387711
SW
10446 const char *canonical_name;
10447 const char *import_alias;
10448 const char *imported_declaration = NULL;
794684b6 10449 const char *import_prefix;
eb1e02fd 10450 std::vector<const char *> excludes;
13387711 10451
27aa8d6a
SW
10452 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10453 if (import_attr == NULL)
10454 {
b98664d3 10455 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10456 dwarf_tag_name (die->tag));
10457 return;
10458 }
10459
de4affc9
CC
10460 imported_cu = cu;
10461 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10462 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10463 if (imported_name == NULL)
10464 {
10465 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10466
10467 The import in the following code:
10468 namespace A
10469 {
10470 typedef int B;
10471 }
10472
10473 int main ()
10474 {
10475 using A::B;
10476 B b;
10477 return b;
10478 }
10479
10480 ...
10481 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10482 <52> DW_AT_decl_file : 1
10483 <53> DW_AT_decl_line : 6
10484 <54> DW_AT_import : <0x75>
10485 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10486 <59> DW_AT_name : B
10487 <5b> DW_AT_decl_file : 1
10488 <5c> DW_AT_decl_line : 2
10489 <5d> DW_AT_type : <0x6e>
10490 ...
10491 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10492 <76> DW_AT_byte_size : 4
10493 <77> DW_AT_encoding : 5 (signed)
10494
10495 imports the wrong die ( 0x75 instead of 0x58 ).
10496 This case will be ignored until the gcc bug is fixed. */
10497 return;
10498 }
10499
82856980
SW
10500 /* Figure out the local name after import. */
10501 import_alias = dwarf2_name (die, cu);
27aa8d6a 10502
794684b6
SW
10503 /* Figure out where the statement is being imported to. */
10504 import_prefix = determine_prefix (die, cu);
10505
10506 /* Figure out what the scope of the imported die is and prepend it
10507 to the name of the imported die. */
de4affc9 10508 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10509
f55ee35c
JK
10510 if (imported_die->tag != DW_TAG_namespace
10511 && imported_die->tag != DW_TAG_module)
794684b6 10512 {
13387711
SW
10513 imported_declaration = imported_name;
10514 canonical_name = imported_name_prefix;
794684b6 10515 }
13387711 10516 else if (strlen (imported_name_prefix) > 0)
12aaed36 10517 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10518 imported_name_prefix,
10519 (cu->language == language_d ? "." : "::"),
10520 imported_name, (char *) NULL);
13387711
SW
10521 else
10522 canonical_name = imported_name;
794684b6 10523
32019081
JK
10524 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10525 for (child_die = die->child; child_die && child_die->tag;
436c571c 10526 child_die = child_die->sibling)
32019081
JK
10527 {
10528 /* DWARF-4: A Fortran use statement with a “rename list” may be
10529 represented by an imported module entry with an import attribute
10530 referring to the module and owned entries corresponding to those
10531 entities that are renamed as part of being imported. */
10532
10533 if (child_die->tag != DW_TAG_imported_declaration)
10534 {
b98664d3 10535 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10536 "- DIE at %s [in module %s]"),
10537 sect_offset_str (child_die->sect_off),
10538 objfile_name (objfile));
32019081
JK
10539 continue;
10540 }
10541
10542 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10543 if (import_attr == NULL)
10544 {
b98664d3 10545 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10546 dwarf_tag_name (child_die->tag));
10547 continue;
10548 }
10549
10550 imported_cu = cu;
10551 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10552 &imported_cu);
10553 imported_name = dwarf2_name (imported_die, imported_cu);
10554 if (imported_name == NULL)
10555 {
b98664d3 10556 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10557 "imported name - DIE at %s [in module %s]"),
10558 sect_offset_str (child_die->sect_off),
10559 objfile_name (objfile));
32019081
JK
10560 continue;
10561 }
10562
eb1e02fd 10563 excludes.push_back (imported_name);
32019081
JK
10564
10565 process_die (child_die, cu);
10566 }
10567
804d2729 10568 add_using_directive (using_directives (cu),
22cee43f
PMR
10569 import_prefix,
10570 canonical_name,
10571 import_alias,
10572 imported_declaration,
10573 excludes,
10574 0,
10575 &objfile->objfile_obstack);
27aa8d6a
SW
10576}
10577
5230b05a
WT
10578/* ICC<14 does not output the required DW_AT_declaration on incomplete
10579 types, but gives them a size of zero. Starting with version 14,
10580 ICC is compatible with GCC. */
10581
9068261f 10582static bool
5230b05a
WT
10583producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10584{
10585 if (!cu->checked_producer)
10586 check_producer (cu);
10587
10588 return cu->producer_is_icc_lt_14;
10589}
10590
eb77c9df
AB
10591/* ICC generates a DW_AT_type for C void functions. This was observed on
10592 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10593 which says that void functions should not have a DW_AT_type. */
10594
10595static bool
10596producer_is_icc (struct dwarf2_cu *cu)
10597{
10598 if (!cu->checked_producer)
10599 check_producer (cu);
10600
10601 return cu->producer_is_icc;
10602}
10603
1b80a9fa
JK
10604/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10605 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10606 this, it was first present in GCC release 4.3.0. */
10607
9068261f 10608static bool
1b80a9fa
JK
10609producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10610{
10611 if (!cu->checked_producer)
10612 check_producer (cu);
10613
10614 return cu->producer_is_gcc_lt_4_3;
10615}
10616
d721ba37
PA
10617static file_and_directory
10618find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10619{
d721ba37
PA
10620 file_and_directory res;
10621
9291a0cd
TT
10622 /* Find the filename. Do not use dwarf2_name here, since the filename
10623 is not a source language identifier. */
d721ba37
PA
10624 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10625 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10626
d721ba37
PA
10627 if (res.comp_dir == NULL
10628 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10629 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10630 {
d721ba37
PA
10631 res.comp_dir_storage = ldirname (res.name);
10632 if (!res.comp_dir_storage.empty ())
10633 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10634 }
d721ba37 10635 if (res.comp_dir != NULL)
9291a0cd
TT
10636 {
10637 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10638 directory, get rid of it. */
d721ba37 10639 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10640
d721ba37
PA
10641 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10642 res.comp_dir = cp + 1;
9291a0cd
TT
10643 }
10644
d721ba37
PA
10645 if (res.name == NULL)
10646 res.name = "<unknown>";
10647
10648 return res;
9291a0cd
TT
10649}
10650
f4dc4d17
DE
10651/* Handle DW_AT_stmt_list for a compilation unit.
10652 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10653 COMP_DIR is the compilation directory. LOWPC is passed to
10654 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10655
10656static void
10657handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10658 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10659{
518817b3
SM
10660 struct dwarf2_per_objfile *dwarf2_per_objfile
10661 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10662 struct attribute *attr;
527f3840
JK
10663 struct line_header line_header_local;
10664 hashval_t line_header_local_hash;
527f3840
JK
10665 void **slot;
10666 int decode_mapping;
2ab95328 10667
f4dc4d17
DE
10668 gdb_assert (! cu->per_cu->is_debug_types);
10669
2ab95328 10670 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10671 if (attr == NULL)
10672 return;
10673
9c541725 10674 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10675
10676 /* The line header hash table is only created if needed (it exists to
10677 prevent redundant reading of the line table for partial_units).
10678 If we're given a partial_unit, we'll need it. If we're given a
10679 compile_unit, then use the line header hash table if it's already
10680 created, but don't create one just yet. */
10681
10682 if (dwarf2_per_objfile->line_header_hash == NULL
10683 && die->tag == DW_TAG_partial_unit)
2ab95328 10684 {
527f3840 10685 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10686 .reset (htab_create_alloc (127, line_header_hash_voidp,
10687 line_header_eq_voidp,
10688 free_line_header_voidp,
10689 xcalloc, xfree));
527f3840 10690 }
2ab95328 10691
9c541725 10692 line_header_local.sect_off = line_offset;
527f3840
JK
10693 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10694 line_header_local_hash = line_header_hash (&line_header_local);
10695 if (dwarf2_per_objfile->line_header_hash != NULL)
10696 {
d15acc42 10697 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10698 &line_header_local,
10699 line_header_local_hash, NO_INSERT);
10700
10701 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10702 is not present in *SLOT (since if there is something in *SLOT then
10703 it will be for a partial_unit). */
10704 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10705 {
527f3840 10706 gdb_assert (*slot != NULL);
9a3c8263 10707 cu->line_header = (struct line_header *) *slot;
527f3840 10708 return;
dee91e82 10709 }
2ab95328 10710 }
527f3840
JK
10711
10712 /* dwarf_decode_line_header does not yet provide sufficient information.
10713 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10714 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10715 if (lh == NULL)
527f3840 10716 return;
4c8aa72d
PA
10717
10718 cu->line_header = lh.release ();
10719 cu->line_header_die_owner = die;
527f3840
JK
10720
10721 if (dwarf2_per_objfile->line_header_hash == NULL)
10722 slot = NULL;
10723 else
10724 {
d15acc42 10725 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10726 &line_header_local,
10727 line_header_local_hash, INSERT);
10728 gdb_assert (slot != NULL);
10729 }
10730 if (slot != NULL && *slot == NULL)
10731 {
10732 /* This newly decoded line number information unit will be owned
10733 by line_header_hash hash table. */
10734 *slot = cu->line_header;
4c8aa72d 10735 cu->line_header_die_owner = NULL;
527f3840
JK
10736 }
10737 else
10738 {
10739 /* We cannot free any current entry in (*slot) as that struct line_header
10740 may be already used by multiple CUs. Create only temporary decoded
10741 line_header for this CU - it may happen at most once for each line
10742 number information unit. And if we're not using line_header_hash
10743 then this is what we want as well. */
10744 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10745 }
10746 decode_mapping = (die->tag != DW_TAG_partial_unit);
10747 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10748 decode_mapping);
fff8551c 10749
2ab95328
TT
10750}
10751
95554aad 10752/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10753
c906108c 10754static void
e7c27a73 10755read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10756{
518817b3
SM
10757 struct dwarf2_per_objfile *dwarf2_per_objfile
10758 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10759 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10760 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10761 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10762 CORE_ADDR highpc = ((CORE_ADDR) 0);
10763 struct attribute *attr;
c906108c 10764 struct die_info *child_die;
e142c38c 10765 CORE_ADDR baseaddr;
6e70227d 10766
380618d6 10767 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10768 baseaddr = objfile->text_section_offset ();
c906108c 10769
fae299cd 10770 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10771
10772 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10773 from finish_block. */
2acceee2 10774 if (lowpc == ((CORE_ADDR) -1))
c906108c 10775 lowpc = highpc;
3e29f34a 10776 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10777
d721ba37 10778 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10779
f4b8a18d
KW
10780 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10781 standardised yet. As a workaround for the language detection we fall
10782 back to the DW_AT_producer string. */
10783 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10784 cu->language = language_opencl;
10785
3019eac3
DE
10786 /* Similar hack for Go. */
10787 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10788 set_cu_language (DW_LANG_Go, cu);
10789
c24bdb02 10790 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10791
10792 /* Decode line number information if present. We do this before
10793 processing child DIEs, so that the line header table is available
10794 for DW_AT_decl_file. */
d721ba37 10795 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10796
10797 /* Process all dies in compilation unit. */
10798 if (die->child != NULL)
10799 {
10800 child_die = die->child;
10801 while (child_die && child_die->tag)
10802 {
10803 process_die (child_die, cu);
436c571c 10804 child_die = child_die->sibling;
3019eac3
DE
10805 }
10806 }
10807
10808 /* Decode macro information, if present. Dwarf 2 macro information
10809 refers to information in the line number info statement program
10810 header, so we can only read it if we've read the header
10811 successfully. */
0af92d60
JK
10812 attr = dwarf2_attr (die, DW_AT_macros, cu);
10813 if (attr == NULL)
10814 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10815 if (attr && cu->line_header)
10816 {
10817 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10818 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10819
43f3e411 10820 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10821 }
10822 else
10823 {
10824 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10825 if (attr && cu->line_header)
10826 {
10827 unsigned int macro_offset = DW_UNSND (attr);
10828
43f3e411 10829 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10830 }
10831 }
3019eac3
DE
10832}
10833
c24bdb02
KS
10834void
10835dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10836{
f4dc4d17
DE
10837 struct type_unit_group *tu_group;
10838 int first_time;
3019eac3 10839 struct attribute *attr;
9c541725 10840 unsigned int i;
0186c6a7 10841 struct signatured_type *sig_type;
3019eac3 10842
f4dc4d17 10843 gdb_assert (per_cu->is_debug_types);
0186c6a7 10844 sig_type = (struct signatured_type *) per_cu;
3019eac3 10845
c24bdb02 10846 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10847
f4dc4d17 10848 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10849 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10850 if (sig_type->type_unit_group == NULL)
c24bdb02 10851 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10852 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10853
10854 /* If we've already processed this stmt_list there's no real need to
10855 do it again, we could fake it and just recreate the part we need
10856 (file name,index -> symtab mapping). If data shows this optimization
10857 is useful we can do it then. */
43f3e411 10858 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10859
10860 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10861 debug info. */
fff8551c 10862 line_header_up lh;
f4dc4d17 10863 if (attr != NULL)
3019eac3 10864 {
9c541725 10865 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10866 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10867 }
10868 if (lh == NULL)
10869 {
10870 if (first_time)
c24bdb02 10871 start_symtab ("", NULL, 0);
f4dc4d17
DE
10872 else
10873 {
10874 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10875 gdb_assert (m_builder == nullptr);
804d2729 10876 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10877 m_builder.reset (new struct buildsym_compunit
10878 (COMPUNIT_OBJFILE (cust), "",
10879 COMPUNIT_DIRNAME (cust),
10880 compunit_language (cust),
10881 0, cust));
f4dc4d17 10882 }
f4dc4d17 10883 return;
3019eac3
DE
10884 }
10885
c24bdb02
KS
10886 line_header = lh.release ();
10887 line_header_die_owner = die;
3019eac3 10888
f4dc4d17
DE
10889 if (first_time)
10890 {
c24bdb02 10891 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10892
1fd60fc0
DE
10893 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10894 still initializing it, and our caller (a few levels up)
10895 process_full_type_unit still needs to know if this is the first
10896 time. */
10897
4ac93832
TT
10898 tu_group->symtabs
10899 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10900 struct symtab *, line_header->file_names_size ());
3019eac3 10901
7ba99d21
AT
10902 auto &file_names = line_header->file_names ();
10903 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10904 {
7ba99d21 10905 file_entry &fe = file_names[i];
c24bdb02
KS
10906 dwarf2_start_subfile (this, fe.name,
10907 fe.include_dir (line_header));
10908 buildsym_compunit *b = get_builder ();
10909 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10910 {
4c8aa72d
PA
10911 /* NOTE: start_subfile will recognize when it's been
10912 passed a file it has already seen. So we can't
10913 assume there's a simple mapping from
10914 cu->line_header->file_names to subfiles, plus
10915 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10916 b->get_current_subfile ()->symtab
10917 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10918 }
10919
c24bdb02 10920 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10921 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10922 }
10923 }
10924 else
3019eac3 10925 {
c24bdb02 10926 gdb_assert (m_builder == nullptr);
804d2729 10927 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10928 m_builder.reset (new struct buildsym_compunit
10929 (COMPUNIT_OBJFILE (cust), "",
10930 COMPUNIT_DIRNAME (cust),
10931 compunit_language (cust),
10932 0, cust));
f4dc4d17 10933
7ba99d21
AT
10934 auto &file_names = line_header->file_names ();
10935 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10936 {
7ba99d21 10937 file_entry &fe = file_names[i];
4c8aa72d 10938 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10939 }
3019eac3
DE
10940 }
10941
f4dc4d17
DE
10942 /* The main symtab is allocated last. Type units don't have DW_AT_name
10943 so they don't have a "real" (so to speak) symtab anyway.
10944 There is later code that will assign the main symtab to all symbols
10945 that don't have one. We need to handle the case of a symbol with a
10946 missing symtab (DW_AT_decl_file) anyway. */
10947}
3019eac3 10948
f4dc4d17
DE
10949/* Process DW_TAG_type_unit.
10950 For TUs we want to skip the first top level sibling if it's not the
10951 actual type being defined by this TU. In this case the first top
10952 level sibling is there to provide context only. */
3019eac3 10953
f4dc4d17
DE
10954static void
10955read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10956{
10957 struct die_info *child_die;
3019eac3 10958
f4dc4d17
DE
10959 prepare_one_comp_unit (cu, die, language_minimal);
10960
10961 /* Initialize (or reinitialize) the machinery for building symtabs.
10962 We do this before processing child DIEs, so that the line header table
10963 is available for DW_AT_decl_file. */
c24bdb02 10964 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10965
10966 if (die->child != NULL)
10967 {
10968 child_die = die->child;
10969 while (child_die && child_die->tag)
10970 {
10971 process_die (child_die, cu);
436c571c 10972 child_die = child_die->sibling;
f4dc4d17
DE
10973 }
10974 }
3019eac3
DE
10975}
10976\f
80626a55
DE
10977/* DWO/DWP files.
10978
10979 http://gcc.gnu.org/wiki/DebugFission
10980 http://gcc.gnu.org/wiki/DebugFissionDWP
10981
10982 To simplify handling of both DWO files ("object" files with the DWARF info)
10983 and DWP files (a file with the DWOs packaged up into one file), we treat
10984 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10985
10986static hashval_t
10987hash_dwo_file (const void *item)
10988{
9a3c8263 10989 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10990 hashval_t hash;
3019eac3 10991
a2ce51a0
DE
10992 hash = htab_hash_string (dwo_file->dwo_name);
10993 if (dwo_file->comp_dir != NULL)
10994 hash += htab_hash_string (dwo_file->comp_dir);
10995 return hash;
3019eac3
DE
10996}
10997
10998static int
10999eq_dwo_file (const void *item_lhs, const void *item_rhs)
11000{
9a3c8263
SM
11001 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11002 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11003
a2ce51a0
DE
11004 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11005 return 0;
11006 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11007 return lhs->comp_dir == rhs->comp_dir;
11008 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11009}
11010
11011/* Allocate a hash table for DWO files. */
11012
51ac9db5 11013static htab_up
298e9637 11014allocate_dwo_file_hash_table ()
3019eac3 11015{
51ac9db5
SM
11016 auto delete_dwo_file = [] (void *item)
11017 {
11018 struct dwo_file *dwo_file = (struct dwo_file *) item;
11019
11020 delete dwo_file;
11021 };
11022
bc68fb19
TT
11023 return htab_up (htab_create_alloc (41,
11024 hash_dwo_file,
11025 eq_dwo_file,
11026 delete_dwo_file,
11027 xcalloc, xfree));
3019eac3
DE
11028}
11029
80626a55
DE
11030/* Lookup DWO file DWO_NAME. */
11031
11032static void **
ed2dc618
SM
11033lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11034 const char *dwo_name,
11035 const char *comp_dir)
80626a55
DE
11036{
11037 struct dwo_file find_entry;
11038 void **slot;
11039
11040 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11041 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11042
0ac5b59e
DE
11043 find_entry.dwo_name = dwo_name;
11044 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11045 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11046 INSERT);
80626a55
DE
11047
11048 return slot;
11049}
11050
3019eac3
DE
11051static hashval_t
11052hash_dwo_unit (const void *item)
11053{
9a3c8263 11054 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11055
11056 /* This drops the top 32 bits of the id, but is ok for a hash. */
11057 return dwo_unit->signature;
11058}
11059
11060static int
11061eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11062{
9a3c8263
SM
11063 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11064 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11065
11066 /* The signature is assumed to be unique within the DWO file.
11067 So while object file CU dwo_id's always have the value zero,
11068 that's OK, assuming each object file DWO file has only one CU,
11069 and that's the rule for now. */
11070 return lhs->signature == rhs->signature;
11071}
11072
11073/* Allocate a hash table for DWO CUs,TUs.
11074 There is one of these tables for each of CUs,TUs for each DWO file. */
11075
b0b6a987 11076static htab_up
298e9637 11077allocate_dwo_unit_table ()
3019eac3
DE
11078{
11079 /* Start out with a pretty small number.
11080 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11081 return htab_up (htab_create_alloc (3,
11082 hash_dwo_unit,
11083 eq_dwo_unit,
11084 NULL, xcalloc, xfree));
3019eac3
DE
11085}
11086
19c3d4c9 11087/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11088
11089static void
19c3d4c9
DE
11090create_dwo_cu_reader (const struct die_reader_specs *reader,
11091 const gdb_byte *info_ptr,
11092 struct die_info *comp_unit_die,
c0ab21c2
TT
11093 struct dwo_file *dwo_file,
11094 struct dwo_unit *dwo_unit)
3019eac3
DE
11095{
11096 struct dwarf2_cu *cu = reader->cu;
9c541725 11097 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11098 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11099
a084a2a6
AT
11100 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11101 if (!signature.has_value ())
3019eac3 11102 {
b98664d3 11103 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11104 " its dwo_id [in module %s]"),
9d8780f0 11105 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11106 return;
11107 }
11108
3019eac3 11109 dwo_unit->dwo_file = dwo_file;
a084a2a6 11110 dwo_unit->signature = *signature;
8a0459fd 11111 dwo_unit->section = section;
9c541725 11112 dwo_unit->sect_off = sect_off;
3019eac3
DE
11113 dwo_unit->length = cu->per_cu->length;
11114
b4f54984 11115 if (dwarf_read_debug)
9d8780f0
SM
11116 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11117 sect_offset_str (sect_off),
9c541725 11118 hex_string (dwo_unit->signature));
3019eac3
DE
11119}
11120
33c5cd75 11121/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11122 Note: This function processes DWO files only, not DWP files. */
3019eac3 11123
33c5cd75 11124static void
ed2dc618 11125create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11126 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11127 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11128{
11129 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11130 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11131
96b79293 11132 section.read (objfile);
33c5cd75 11133 info_ptr = section.buffer;
3019eac3
DE
11134
11135 if (info_ptr == NULL)
33c5cd75 11136 return;
3019eac3 11137
b4f54984 11138 if (dwarf_read_debug)
19c3d4c9
DE
11139 {
11140 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11141 section.get_name (),
11142 section.get_file_name ());
19c3d4c9 11143 }
3019eac3 11144
33c5cd75 11145 end_ptr = info_ptr + section.size;
3019eac3
DE
11146 while (info_ptr < end_ptr)
11147 {
11148 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11149 struct dwo_unit read_unit {};
33c5cd75
DB
11150 struct dwo_unit *dwo_unit;
11151 void **slot;
11152 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11153
11154 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11155 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11156 per_cu.is_debug_types = 0;
33c5cd75
DB
11157 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11158 per_cu.section = &section;
11159
c0ab21c2
TT
11160 cutu_reader reader (&per_cu, cu, &dwo_file);
11161 if (!reader.dummy_p)
11162 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11163 &dwo_file, &read_unit);
33c5cd75
DB
11164 info_ptr += per_cu.length;
11165
11166 // If the unit could not be parsed, skip it.
c0ab21c2 11167 if (read_unit.dwo_file == NULL)
33c5cd75 11168 continue;
3019eac3 11169
33c5cd75 11170 if (cus_htab == NULL)
298e9637 11171 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11172
33c5cd75 11173 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11174 *dwo_unit = read_unit;
b0b6a987 11175 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11176 gdb_assert (slot != NULL);
11177 if (*slot != NULL)
19c3d4c9 11178 {
33c5cd75
DB
11179 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11180 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11181
b98664d3 11182 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11183 " the entry at offset %s, signature %s"),
11184 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11185 hex_string (dwo_unit->signature));
19c3d4c9 11186 }
33c5cd75 11187 *slot = (void *)dwo_unit;
3019eac3 11188 }
3019eac3
DE
11189}
11190
80626a55
DE
11191/* DWP file .debug_{cu,tu}_index section format:
11192 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11193
d2415c6c
DE
11194 DWP Version 1:
11195
80626a55
DE
11196 Both index sections have the same format, and serve to map a 64-bit
11197 signature to a set of section numbers. Each section begins with a header,
11198 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11199 indexes, and a pool of 32-bit section numbers. The index sections will be
11200 aligned at 8-byte boundaries in the file.
11201
d2415c6c
DE
11202 The index section header consists of:
11203
11204 V, 32 bit version number
11205 -, 32 bits unused
11206 N, 32 bit number of compilation units or type units in the index
11207 M, 32 bit number of slots in the hash table
80626a55 11208
d2415c6c 11209 Numbers are recorded using the byte order of the application binary.
80626a55 11210
d2415c6c
DE
11211 The hash table begins at offset 16 in the section, and consists of an array
11212 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11213 order of the application binary). Unused slots in the hash table are 0.
11214 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11215
d2415c6c
DE
11216 The parallel table begins immediately after the hash table
11217 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11218 array of 32-bit indexes (using the byte order of the application binary),
11219 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11220 table contains a 32-bit index into the pool of section numbers. For unused
11221 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11222
73869dc2
DE
11223 The pool of section numbers begins immediately following the hash table
11224 (at offset 16 + 12 * M from the beginning of the section). The pool of
11225 section numbers consists of an array of 32-bit words (using the byte order
11226 of the application binary). Each item in the array is indexed starting
11227 from 0. The hash table entry provides the index of the first section
11228 number in the set. Additional section numbers in the set follow, and the
11229 set is terminated by a 0 entry (section number 0 is not used in ELF).
11230
11231 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11232 section must be the first entry in the set, and the .debug_abbrev.dwo must
11233 be the second entry. Other members of the set may follow in any order.
11234
11235 ---
11236
11237 DWP Version 2:
11238
11239 DWP Version 2 combines all the .debug_info, etc. sections into one,
11240 and the entries in the index tables are now offsets into these sections.
11241 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11242 section.
11243
11244 Index Section Contents:
11245 Header
11246 Hash Table of Signatures dwp_hash_table.hash_table
11247 Parallel Table of Indices dwp_hash_table.unit_table
11248 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11249 Table of Section Sizes dwp_hash_table.v2.sizes
11250
11251 The index section header consists of:
11252
11253 V, 32 bit version number
11254 L, 32 bit number of columns in the table of section offsets
11255 N, 32 bit number of compilation units or type units in the index
11256 M, 32 bit number of slots in the hash table
11257
11258 Numbers are recorded using the byte order of the application binary.
11259
11260 The hash table has the same format as version 1.
11261 The parallel table of indices has the same format as version 1,
11262 except that the entries are origin-1 indices into the table of sections
11263 offsets and the table of section sizes.
11264
11265 The table of offsets begins immediately following the parallel table
11266 (at offset 16 + 12 * M from the beginning of the section). The table is
11267 a two-dimensional array of 32-bit words (using the byte order of the
11268 application binary), with L columns and N+1 rows, in row-major order.
11269 Each row in the array is indexed starting from 0. The first row provides
11270 a key to the remaining rows: each column in this row provides an identifier
11271 for a debug section, and the offsets in the same column of subsequent rows
11272 refer to that section. The section identifiers are:
11273
11274 DW_SECT_INFO 1 .debug_info.dwo
11275 DW_SECT_TYPES 2 .debug_types.dwo
11276 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11277 DW_SECT_LINE 4 .debug_line.dwo
11278 DW_SECT_LOC 5 .debug_loc.dwo
11279 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11280 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11281 DW_SECT_MACRO 8 .debug_macro.dwo
11282
11283 The offsets provided by the CU and TU index sections are the base offsets
11284 for the contributions made by each CU or TU to the corresponding section
11285 in the package file. Each CU and TU header contains an abbrev_offset
11286 field, used to find the abbreviations table for that CU or TU within the
11287 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11288 be interpreted as relative to the base offset given in the index section.
11289 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11290 should be interpreted as relative to the base offset for .debug_line.dwo,
11291 and offsets into other debug sections obtained from DWARF attributes should
11292 also be interpreted as relative to the corresponding base offset.
11293
11294 The table of sizes begins immediately following the table of offsets.
11295 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11296 with L columns and N rows, in row-major order. Each row in the array is
11297 indexed starting from 1 (row 0 is shared by the two tables).
11298
11299 ---
11300
11301 Hash table lookup is handled the same in version 1 and 2:
11302
11303 We assume that N and M will not exceed 2^32 - 1.
11304 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11305
d2415c6c
DE
11306 Given a 64-bit compilation unit signature or a type signature S, an entry
11307 in the hash table is located as follows:
80626a55 11308
d2415c6c
DE
11309 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11310 the low-order k bits all set to 1.
80626a55 11311
d2415c6c 11312 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11313
d2415c6c
DE
11314 3) If the hash table entry at index H matches the signature, use that
11315 entry. If the hash table entry at index H is unused (all zeroes),
11316 terminate the search: the signature is not present in the table.
80626a55 11317
d2415c6c 11318 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11319
d2415c6c 11320 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11321 to stop at an unused slot or find the match. */
80626a55
DE
11322
11323/* Create a hash table to map DWO IDs to their CU/TU entry in
11324 .debug_{info,types}.dwo in DWP_FILE.
11325 Returns NULL if there isn't one.
11326 Note: This function processes DWP files only, not DWO files. */
11327
11328static struct dwp_hash_table *
ed2dc618
SM
11329create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11330 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11331{
11332 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11333 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11334 const gdb_byte *index_ptr, *index_end;
80626a55 11335 struct dwarf2_section_info *index;
73869dc2 11336 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11337 struct dwp_hash_table *htab;
11338
11339 if (is_debug_types)
11340 index = &dwp_file->sections.tu_index;
11341 else
11342 index = &dwp_file->sections.cu_index;
11343
96b79293 11344 if (index->empty ())
80626a55 11345 return NULL;
96b79293 11346 index->read (objfile);
80626a55
DE
11347
11348 index_ptr = index->buffer;
11349 index_end = index_ptr + index->size;
11350
11351 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11352 index_ptr += 4;
11353 if (version == 2)
11354 nr_columns = read_4_bytes (dbfd, index_ptr);
11355 else
11356 nr_columns = 0;
11357 index_ptr += 4;
80626a55
DE
11358 nr_units = read_4_bytes (dbfd, index_ptr);
11359 index_ptr += 4;
11360 nr_slots = read_4_bytes (dbfd, index_ptr);
11361 index_ptr += 4;
11362
73869dc2 11363 if (version != 1 && version != 2)
80626a55 11364 {
21aa081e 11365 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11366 " [in module %s]"),
21aa081e 11367 pulongest (version), dwp_file->name);
80626a55
DE
11368 }
11369 if (nr_slots != (nr_slots & -nr_slots))
11370 {
21aa081e 11371 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11372 " is not power of 2 [in module %s]"),
21aa081e 11373 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11374 }
11375
11376 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11377 htab->version = version;
11378 htab->nr_columns = nr_columns;
80626a55
DE
11379 htab->nr_units = nr_units;
11380 htab->nr_slots = nr_slots;
11381 htab->hash_table = index_ptr;
11382 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11383
11384 /* Exit early if the table is empty. */
11385 if (nr_slots == 0 || nr_units == 0
11386 || (version == 2 && nr_columns == 0))
11387 {
11388 /* All must be zero. */
11389 if (nr_slots != 0 || nr_units != 0
11390 || (version == 2 && nr_columns != 0))
11391 {
b98664d3 11392 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11393 " all zero [in modules %s]"),
11394 dwp_file->name);
11395 }
11396 return htab;
11397 }
11398
11399 if (version == 1)
11400 {
11401 htab->section_pool.v1.indices =
11402 htab->unit_table + sizeof (uint32_t) * nr_slots;
11403 /* It's harder to decide whether the section is too small in v1.
11404 V1 is deprecated anyway so we punt. */
11405 }
11406 else
11407 {
11408 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11409 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11410 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11411 /* Reverse map for error checking. */
11412 int ids_seen[DW_SECT_MAX + 1];
11413 int i;
11414
11415 if (nr_columns < 2)
11416 {
11417 error (_("Dwarf Error: bad DWP hash table, too few columns"
11418 " in section table [in module %s]"),
11419 dwp_file->name);
11420 }
11421 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11422 {
11423 error (_("Dwarf Error: bad DWP hash table, too many columns"
11424 " in section table [in module %s]"),
11425 dwp_file->name);
11426 }
04fd5eed
GB
11427 memset (ids, 255, sizeof_ids);
11428 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11429 for (i = 0; i < nr_columns; ++i)
11430 {
11431 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11432
11433 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11434 {
11435 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11436 " in section table [in module %s]"),
11437 id, dwp_file->name);
11438 }
11439 if (ids_seen[id] != -1)
11440 {
11441 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11442 " id %d in section table [in module %s]"),
11443 id, dwp_file->name);
11444 }
11445 ids_seen[id] = i;
11446 ids[i] = id;
11447 }
11448 /* Must have exactly one info or types section. */
11449 if (((ids_seen[DW_SECT_INFO] != -1)
11450 + (ids_seen[DW_SECT_TYPES] != -1))
11451 != 1)
11452 {
11453 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11454 " DWO info/types section [in module %s]"),
11455 dwp_file->name);
11456 }
11457 /* Must have an abbrev section. */
11458 if (ids_seen[DW_SECT_ABBREV] == -1)
11459 {
11460 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11461 " section [in module %s]"),
11462 dwp_file->name);
11463 }
11464 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11465 htab->section_pool.v2.sizes =
11466 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11467 * nr_units * nr_columns);
11468 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11469 * nr_units * nr_columns))
11470 > index_end)
11471 {
11472 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11473 " [in module %s]"),
11474 dwp_file->name);
11475 }
11476 }
80626a55
DE
11477
11478 return htab;
11479}
11480
11481/* Update SECTIONS with the data from SECTP.
11482
11483 This function is like the other "locate" section routines that are
11484 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11485 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11486
11487 The result is non-zero for success, or zero if an error was found. */
11488
11489static int
73869dc2
DE
11490locate_v1_virtual_dwo_sections (asection *sectp,
11491 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11492{
11493 const struct dwop_section_names *names = &dwop_section_names;
11494
11495 if (section_is_p (sectp->name, &names->abbrev_dwo))
11496 {
11497 /* There can be only one. */
049412e3 11498 if (sections->abbrev.s.section != NULL)
80626a55 11499 return 0;
049412e3 11500 sections->abbrev.s.section = sectp;
fd361982 11501 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11502 }
11503 else if (section_is_p (sectp->name, &names->info_dwo)
11504 || section_is_p (sectp->name, &names->types_dwo))
11505 {
11506 /* There can be only one. */
049412e3 11507 if (sections->info_or_types.s.section != NULL)
80626a55 11508 return 0;
049412e3 11509 sections->info_or_types.s.section = sectp;
fd361982 11510 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11511 }
11512 else if (section_is_p (sectp->name, &names->line_dwo))
11513 {
11514 /* There can be only one. */
049412e3 11515 if (sections->line.s.section != NULL)
80626a55 11516 return 0;
049412e3 11517 sections->line.s.section = sectp;
fd361982 11518 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11519 }
11520 else if (section_is_p (sectp->name, &names->loc_dwo))
11521 {
11522 /* There can be only one. */
049412e3 11523 if (sections->loc.s.section != NULL)
80626a55 11524 return 0;
049412e3 11525 sections->loc.s.section = sectp;
fd361982 11526 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11527 }
11528 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11529 {
11530 /* There can be only one. */
049412e3 11531 if (sections->macinfo.s.section != NULL)
80626a55 11532 return 0;
049412e3 11533 sections->macinfo.s.section = sectp;
fd361982 11534 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11535 }
11536 else if (section_is_p (sectp->name, &names->macro_dwo))
11537 {
11538 /* There can be only one. */
049412e3 11539 if (sections->macro.s.section != NULL)
80626a55 11540 return 0;
049412e3 11541 sections->macro.s.section = sectp;
fd361982 11542 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11543 }
11544 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11545 {
11546 /* There can be only one. */
049412e3 11547 if (sections->str_offsets.s.section != NULL)
80626a55 11548 return 0;
049412e3 11549 sections->str_offsets.s.section = sectp;
fd361982 11550 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11551 }
11552 else
11553 {
11554 /* No other kind of section is valid. */
11555 return 0;
11556 }
11557
11558 return 1;
11559}
11560
73869dc2
DE
11561/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11562 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11563 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11564 This is for DWP version 1 files. */
80626a55
DE
11565
11566static struct dwo_unit *
ed2dc618
SM
11567create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11568 struct dwp_file *dwp_file,
73869dc2
DE
11569 uint32_t unit_index,
11570 const char *comp_dir,
11571 ULONGEST signature, int is_debug_types)
80626a55
DE
11572{
11573 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11574 const struct dwp_hash_table *dwp_htab =
11575 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11576 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11577 const char *kind = is_debug_types ? "TU" : "CU";
11578 struct dwo_file *dwo_file;
11579 struct dwo_unit *dwo_unit;
73869dc2 11580 struct virtual_v1_dwo_sections sections;
80626a55 11581 void **dwo_file_slot;
80626a55
DE
11582 int i;
11583
73869dc2
DE
11584 gdb_assert (dwp_file->version == 1);
11585
b4f54984 11586 if (dwarf_read_debug)
80626a55 11587 {
73869dc2 11588 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11589 kind,
73869dc2 11590 pulongest (unit_index), hex_string (signature),
80626a55
DE
11591 dwp_file->name);
11592 }
11593
19ac8c2e 11594 /* Fetch the sections of this DWO unit.
80626a55
DE
11595 Put a limit on the number of sections we look for so that bad data
11596 doesn't cause us to loop forever. */
11597
73869dc2 11598#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11599 (1 /* .debug_info or .debug_types */ \
11600 + 1 /* .debug_abbrev */ \
11601 + 1 /* .debug_line */ \
11602 + 1 /* .debug_loc */ \
11603 + 1 /* .debug_str_offsets */ \
19ac8c2e 11604 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11605 + 1 /* trailing zero */)
11606
11607 memset (&sections, 0, sizeof (sections));
80626a55 11608
73869dc2 11609 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11610 {
11611 asection *sectp;
11612 uint32_t section_nr =
11613 read_4_bytes (dbfd,
73869dc2
DE
11614 dwp_htab->section_pool.v1.indices
11615 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11616
11617 if (section_nr == 0)
11618 break;
11619 if (section_nr >= dwp_file->num_sections)
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, section number too large"
11622 " [in module %s]"),
11623 dwp_file->name);
11624 }
11625
11626 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11627 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11628 {
11629 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11630 " [in module %s]"),
11631 dwp_file->name);
11632 }
11633 }
11634
11635 if (i < 2
96b79293
TT
11636 || sections.info_or_types.empty ()
11637 || sections.abbrev.empty ())
80626a55
DE
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11640 " [in module %s]"),
11641 dwp_file->name);
11642 }
73869dc2 11643 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11644 {
11645 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11646 " [in module %s]"),
11647 dwp_file->name);
11648 }
11649
11650 /* It's easier for the rest of the code if we fake a struct dwo_file and
11651 have dwo_unit "live" in that. At least for now.
11652
11653 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11654 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11655 file, we can combine them back into a virtual DWO file to save space
11656 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11657 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11658
791afaa2
TT
11659 std::string virtual_dwo_name =
11660 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11661 sections.abbrev.get_id (),
11662 sections.line.get_id (),
11663 sections.loc.get_id (),
11664 sections.str_offsets.get_id ());
80626a55 11665 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11666 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11667 virtual_dwo_name.c_str (),
11668 comp_dir);
80626a55
DE
11669 /* Create one if necessary. */
11670 if (*dwo_file_slot == NULL)
11671 {
b4f54984 11672 if (dwarf_read_debug)
80626a55
DE
11673 {
11674 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11675 virtual_dwo_name.c_str ());
80626a55 11676 }
51ac9db5 11677 dwo_file = new struct dwo_file;
be1e3d3e 11678 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11679 dwo_file->comp_dir = comp_dir;
80626a55
DE
11680 dwo_file->sections.abbrev = sections.abbrev;
11681 dwo_file->sections.line = sections.line;
11682 dwo_file->sections.loc = sections.loc;
11683 dwo_file->sections.macinfo = sections.macinfo;
11684 dwo_file->sections.macro = sections.macro;
11685 dwo_file->sections.str_offsets = sections.str_offsets;
11686 /* The "str" section is global to the entire DWP file. */
11687 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11688 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11689 there's no need to record it in dwo_file.
11690 Also, we can't simply record type sections in dwo_file because
11691 we record a pointer into the vector in dwo_unit. As we collect more
11692 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11693 for it, invalidating all copies of pointers into the previous
11694 contents. */
80626a55
DE
11695 *dwo_file_slot = dwo_file;
11696 }
11697 else
11698 {
b4f54984 11699 if (dwarf_read_debug)
80626a55
DE
11700 {
11701 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11702 virtual_dwo_name.c_str ());
80626a55 11703 }
9a3c8263 11704 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11705 }
80626a55
DE
11706
11707 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11708 dwo_unit->dwo_file = dwo_file;
11709 dwo_unit->signature = signature;
8d749320
SM
11710 dwo_unit->section =
11711 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11712 *dwo_unit->section = sections.info_or_types;
57d63ce2 11713 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11714
11715 return dwo_unit;
11716}
11717
73869dc2
DE
11718/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11719 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11720 piece within that section used by a TU/CU, return a virtual section
11721 of just that piece. */
11722
11723static struct dwarf2_section_info
ed2dc618
SM
11724create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11725 struct dwarf2_section_info *section,
73869dc2
DE
11726 bfd_size_type offset, bfd_size_type size)
11727{
11728 struct dwarf2_section_info result;
11729 asection *sectp;
11730
11731 gdb_assert (section != NULL);
11732 gdb_assert (!section->is_virtual);
11733
11734 memset (&result, 0, sizeof (result));
11735 result.s.containing_section = section;
dc4ccb6f 11736 result.is_virtual = true;
73869dc2
DE
11737
11738 if (size == 0)
11739 return result;
11740
96b79293 11741 sectp = section->get_bfd_section ();
73869dc2
DE
11742
11743 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11744 bounds of the real section. This is a pretty-rare event, so just
11745 flag an error (easier) instead of a warning and trying to cope. */
11746 if (sectp == NULL
fd361982 11747 || offset + size > bfd_section_size (sectp))
73869dc2 11748 {
73869dc2
DE
11749 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11750 " in section %s [in module %s]"),
fd361982 11751 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11752 objfile_name (dwarf2_per_objfile->objfile));
11753 }
11754
11755 result.virtual_offset = offset;
11756 result.size = size;
11757 return result;
11758}
11759
11760/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11761 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11762 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11763 This is for DWP version 2 files. */
11764
11765static struct dwo_unit *
ed2dc618
SM
11766create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11767 struct dwp_file *dwp_file,
73869dc2
DE
11768 uint32_t unit_index,
11769 const char *comp_dir,
11770 ULONGEST signature, int is_debug_types)
11771{
11772 struct objfile *objfile = dwarf2_per_objfile->objfile;
11773 const struct dwp_hash_table *dwp_htab =
11774 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11775 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11776 const char *kind = is_debug_types ? "TU" : "CU";
11777 struct dwo_file *dwo_file;
11778 struct dwo_unit *dwo_unit;
11779 struct virtual_v2_dwo_sections sections;
11780 void **dwo_file_slot;
73869dc2
DE
11781 int i;
11782
11783 gdb_assert (dwp_file->version == 2);
11784
b4f54984 11785 if (dwarf_read_debug)
73869dc2
DE
11786 {
11787 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11788 kind,
11789 pulongest (unit_index), hex_string (signature),
11790 dwp_file->name);
11791 }
11792
11793 /* Fetch the section offsets of this DWO unit. */
11794
11795 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11796
11797 for (i = 0; i < dwp_htab->nr_columns; ++i)
11798 {
11799 uint32_t offset = read_4_bytes (dbfd,
11800 dwp_htab->section_pool.v2.offsets
11801 + (((unit_index - 1) * dwp_htab->nr_columns
11802 + i)
11803 * sizeof (uint32_t)));
11804 uint32_t size = read_4_bytes (dbfd,
11805 dwp_htab->section_pool.v2.sizes
11806 + (((unit_index - 1) * dwp_htab->nr_columns
11807 + i)
11808 * sizeof (uint32_t)));
11809
11810 switch (dwp_htab->section_pool.v2.section_ids[i])
11811 {
11812 case DW_SECT_INFO:
11813 case DW_SECT_TYPES:
11814 sections.info_or_types_offset = offset;
11815 sections.info_or_types_size = size;
11816 break;
11817 case DW_SECT_ABBREV:
11818 sections.abbrev_offset = offset;
11819 sections.abbrev_size = size;
11820 break;
11821 case DW_SECT_LINE:
11822 sections.line_offset = offset;
11823 sections.line_size = size;
11824 break;
11825 case DW_SECT_LOC:
11826 sections.loc_offset = offset;
11827 sections.loc_size = size;
11828 break;
11829 case DW_SECT_STR_OFFSETS:
11830 sections.str_offsets_offset = offset;
11831 sections.str_offsets_size = size;
11832 break;
11833 case DW_SECT_MACINFO:
11834 sections.macinfo_offset = offset;
11835 sections.macinfo_size = size;
11836 break;
11837 case DW_SECT_MACRO:
11838 sections.macro_offset = offset;
11839 sections.macro_size = size;
11840 break;
11841 }
11842 }
11843
11844 /* It's easier for the rest of the code if we fake a struct dwo_file and
11845 have dwo_unit "live" in that. At least for now.
11846
11847 The DWP file can be made up of a random collection of CUs and TUs.
11848 However, for each CU + set of TUs that came from the same original DWO
11849 file, we can combine them back into a virtual DWO file to save space
11850 (fewer struct dwo_file objects to allocate). Remember that for really
11851 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11852
791afaa2
TT
11853 std::string virtual_dwo_name =
11854 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11855 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11856 (long) (sections.line_size ? sections.line_offset : 0),
11857 (long) (sections.loc_size ? sections.loc_offset : 0),
11858 (long) (sections.str_offsets_size
11859 ? sections.str_offsets_offset : 0));
73869dc2 11860 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11861 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11862 virtual_dwo_name.c_str (),
11863 comp_dir);
73869dc2
DE
11864 /* Create one if necessary. */
11865 if (*dwo_file_slot == NULL)
11866 {
b4f54984 11867 if (dwarf_read_debug)
73869dc2
DE
11868 {
11869 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11870 virtual_dwo_name.c_str ());
73869dc2 11871 }
51ac9db5 11872 dwo_file = new struct dwo_file;
be1e3d3e 11873 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11874 dwo_file->comp_dir = comp_dir;
11875 dwo_file->sections.abbrev =
ed2dc618 11876 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11877 sections.abbrev_offset, sections.abbrev_size);
11878 dwo_file->sections.line =
ed2dc618 11879 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11880 sections.line_offset, sections.line_size);
11881 dwo_file->sections.loc =
ed2dc618 11882 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11883 sections.loc_offset, sections.loc_size);
11884 dwo_file->sections.macinfo =
ed2dc618 11885 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11886 sections.macinfo_offset, sections.macinfo_size);
11887 dwo_file->sections.macro =
ed2dc618 11888 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11889 sections.macro_offset, sections.macro_size);
11890 dwo_file->sections.str_offsets =
ed2dc618
SM
11891 create_dwp_v2_section (dwarf2_per_objfile,
11892 &dwp_file->sections.str_offsets,
73869dc2
DE
11893 sections.str_offsets_offset,
11894 sections.str_offsets_size);
11895 /* The "str" section is global to the entire DWP file. */
11896 dwo_file->sections.str = dwp_file->sections.str;
11897 /* The info or types section is assigned below to dwo_unit,
11898 there's no need to record it in dwo_file.
11899 Also, we can't simply record type sections in dwo_file because
11900 we record a pointer into the vector in dwo_unit. As we collect more
11901 types we'll grow the vector and eventually have to reallocate space
11902 for it, invalidating all copies of pointers into the previous
11903 contents. */
11904 *dwo_file_slot = dwo_file;
11905 }
11906 else
11907 {
b4f54984 11908 if (dwarf_read_debug)
73869dc2
DE
11909 {
11910 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11911 virtual_dwo_name.c_str ());
73869dc2 11912 }
9a3c8263 11913 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11914 }
73869dc2
DE
11915
11916 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11917 dwo_unit->dwo_file = dwo_file;
11918 dwo_unit->signature = signature;
8d749320
SM
11919 dwo_unit->section =
11920 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11921 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11922 is_debug_types
73869dc2
DE
11923 ? &dwp_file->sections.types
11924 : &dwp_file->sections.info,
11925 sections.info_or_types_offset,
11926 sections.info_or_types_size);
11927 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11928
11929 return dwo_unit;
11930}
11931
57d63ce2
DE
11932/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11933 Returns NULL if the signature isn't found. */
80626a55
DE
11934
11935static struct dwo_unit *
ed2dc618
SM
11936lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11937 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11938 ULONGEST signature, int is_debug_types)
80626a55 11939{
57d63ce2
DE
11940 const struct dwp_hash_table *dwp_htab =
11941 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11942 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11943 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11944 uint32_t hash = signature & mask;
11945 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11946 unsigned int i;
11947 void **slot;
870f88f7 11948 struct dwo_unit find_dwo_cu;
80626a55
DE
11949
11950 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11951 find_dwo_cu.signature = signature;
19ac8c2e 11952 slot = htab_find_slot (is_debug_types
48b490f2
TT
11953 ? dwp_file->loaded_tus.get ()
11954 : dwp_file->loaded_cus.get (),
19ac8c2e 11955 &find_dwo_cu, INSERT);
80626a55
DE
11956
11957 if (*slot != NULL)
9a3c8263 11958 return (struct dwo_unit *) *slot;
80626a55
DE
11959
11960 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11961 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11962 {
11963 ULONGEST signature_in_table;
11964
11965 signature_in_table =
57d63ce2 11966 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11967 if (signature_in_table == signature)
11968 {
57d63ce2
DE
11969 uint32_t unit_index =
11970 read_4_bytes (dbfd,
11971 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11972
73869dc2
DE
11973 if (dwp_file->version == 1)
11974 {
ed2dc618
SM
11975 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11976 dwp_file, unit_index,
73869dc2
DE
11977 comp_dir, signature,
11978 is_debug_types);
11979 }
11980 else
11981 {
ed2dc618
SM
11982 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11983 dwp_file, unit_index,
73869dc2
DE
11984 comp_dir, signature,
11985 is_debug_types);
11986 }
9a3c8263 11987 return (struct dwo_unit *) *slot;
80626a55
DE
11988 }
11989 if (signature_in_table == 0)
11990 return NULL;
11991 hash = (hash + hash2) & mask;
11992 }
11993
11994 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11995 " [in module %s]"),
11996 dwp_file->name);
11997}
11998
ab5088bf 11999/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12000 Open the file specified by FILE_NAME and hand it off to BFD for
12001 preliminary analysis. Return a newly initialized bfd *, which
12002 includes a canonicalized copy of FILE_NAME.
80626a55 12003 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12004 SEARCH_CWD is true if the current directory is to be searched.
12005 It will be searched before debug-file-directory.
13aaf454
DE
12006 If successful, the file is added to the bfd include table of the
12007 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12008 If unable to find/open the file, return NULL.
3019eac3
DE
12009 NOTE: This function is derived from symfile_bfd_open. */
12010
192b62ce 12011static gdb_bfd_ref_ptr
ed2dc618
SM
12012try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12013 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12014{
24b9144d 12015 int desc;
9c02c129
DE
12016 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12017 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12018 to debug_file_directory. */
e0cc99a6 12019 const char *search_path;
9c02c129
DE
12020 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12021
e0cc99a6 12022 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12023 if (search_cwd)
12024 {
12025 if (*debug_file_directory != '\0')
e0cc99a6
TT
12026 {
12027 search_path_holder.reset (concat (".", dirname_separator_string,
12028 debug_file_directory,
12029 (char *) NULL));
12030 search_path = search_path_holder.get ();
12031 }
6ac97d4c 12032 else
e0cc99a6 12033 search_path = ".";
6ac97d4c 12034 }
9c02c129 12035 else
e0cc99a6 12036 search_path = debug_file_directory;
3019eac3 12037
24b9144d 12038 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12039 if (is_dwp)
12040 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12041
12042 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12043 desc = openp (search_path, flags, file_name,
3019eac3
DE
12044 O_RDONLY | O_BINARY, &absolute_name);
12045 if (desc < 0)
12046 return NULL;
12047
e0cc99a6
TT
12048 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12049 gnutarget, desc));
9c02c129
DE
12050 if (sym_bfd == NULL)
12051 return NULL;
192b62ce 12052 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12053
192b62ce
TT
12054 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12055 return NULL;
3019eac3 12056
13aaf454
DE
12057 /* Success. Record the bfd as having been included by the objfile's bfd.
12058 This is important because things like demangled_names_hash lives in the
12059 objfile's per_bfd space and may have references to things like symbol
12060 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12061 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12062
3019eac3
DE
12063 return sym_bfd;
12064}
12065
ab5088bf 12066/* Try to open DWO file FILE_NAME.
3019eac3
DE
12067 COMP_DIR is the DW_AT_comp_dir attribute.
12068 The result is the bfd handle of the file.
12069 If there is a problem finding or opening the file, return NULL.
12070 Upon success, the canonicalized path of the file is stored in the bfd,
12071 same as symfile_bfd_open. */
12072
192b62ce 12073static gdb_bfd_ref_ptr
ed2dc618
SM
12074open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12075 const char *file_name, const char *comp_dir)
3019eac3 12076{
80626a55 12077 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12078 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12079 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12080
12081 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12082
12083 if (comp_dir != NULL)
12084 {
43816ebc
TT
12085 gdb::unique_xmalloc_ptr<char> path_to_try
12086 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12087
12088 /* NOTE: If comp_dir is a relative path, this will also try the
12089 search path, which seems useful. */
ed2dc618 12090 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12091 path_to_try.get (),
ed2dc618 12092 0 /*is_dwp*/,
192b62ce 12093 1 /*search_cwd*/));
3019eac3
DE
12094 if (abfd != NULL)
12095 return abfd;
12096 }
12097
12098 /* That didn't work, try debug-file-directory, which, despite its name,
12099 is a list of paths. */
12100
12101 if (*debug_file_directory == '\0')
12102 return NULL;
12103
ed2dc618
SM
12104 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12105 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12106}
12107
80626a55
DE
12108/* This function is mapped across the sections and remembers the offset and
12109 size of each of the DWO debugging sections we are interested in. */
12110
12111static void
12112dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12113{
9a3c8263 12114 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12115 const struct dwop_section_names *names = &dwop_section_names;
12116
12117 if (section_is_p (sectp->name, &names->abbrev_dwo))
12118 {
049412e3 12119 dwo_sections->abbrev.s.section = sectp;
fd361982 12120 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12121 }
12122 else if (section_is_p (sectp->name, &names->info_dwo))
12123 {
049412e3 12124 dwo_sections->info.s.section = sectp;
fd361982 12125 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12126 }
12127 else if (section_is_p (sectp->name, &names->line_dwo))
12128 {
049412e3 12129 dwo_sections->line.s.section = sectp;
fd361982 12130 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12131 }
12132 else if (section_is_p (sectp->name, &names->loc_dwo))
12133 {
049412e3 12134 dwo_sections->loc.s.section = sectp;
fd361982 12135 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12136 }
41144253 12137 else if (section_is_p (sectp->name, &names->loclists_dwo))
12138 {
12139 dwo_sections->loclists.s.section = sectp;
12140 dwo_sections->loclists.size = bfd_section_size (sectp);
12141 }
80626a55
DE
12142 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12143 {
049412e3 12144 dwo_sections->macinfo.s.section = sectp;
fd361982 12145 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12146 }
12147 else if (section_is_p (sectp->name, &names->macro_dwo))
12148 {
049412e3 12149 dwo_sections->macro.s.section = sectp;
fd361982 12150 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12151 }
12152 else if (section_is_p (sectp->name, &names->str_dwo))
12153 {
049412e3 12154 dwo_sections->str.s.section = sectp;
fd361982 12155 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12156 }
12157 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12158 {
049412e3 12159 dwo_sections->str_offsets.s.section = sectp;
fd361982 12160 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12161 }
12162 else if (section_is_p (sectp->name, &names->types_dwo))
12163 {
12164 struct dwarf2_section_info type_section;
12165
12166 memset (&type_section, 0, sizeof (type_section));
049412e3 12167 type_section.s.section = sectp;
fd361982 12168 type_section.size = bfd_section_size (sectp);
fd5866f6 12169 dwo_sections->types.push_back (type_section);
80626a55
DE
12170 }
12171}
12172
ab5088bf 12173/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12174 by PER_CU. This is for the non-DWP case.
80626a55 12175 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12176
12177static struct dwo_file *
0ac5b59e
DE
12178open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12179 const char *dwo_name, const char *comp_dir)
3019eac3 12180{
ed2dc618 12181 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12182
fb1eb2f9 12183 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12184 if (dbfd == NULL)
12185 {
b4f54984 12186 if (dwarf_read_debug)
80626a55
DE
12187 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12188 return NULL;
12189 }
263db9a1 12190
51ac9db5 12191 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12192 dwo_file->dwo_name = dwo_name;
12193 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12194 dwo_file->dbfd = std::move (dbfd);
3019eac3 12195
fb1eb2f9 12196 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12197 &dwo_file->sections);
3019eac3 12198
18a8505e
AT
12199 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12200 dwo_file->sections.info, dwo_file->cus);
3019eac3 12201
263db9a1 12202 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12203 dwo_file->sections.types, dwo_file->tus);
3019eac3 12204
b4f54984 12205 if (dwarf_read_debug)
80626a55
DE
12206 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12207
263db9a1 12208 return dwo_file.release ();
3019eac3
DE
12209}
12210
80626a55 12211/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12212 size of each of the DWP debugging sections common to version 1 and 2 that
12213 we are interested in. */
3019eac3 12214
80626a55 12215static void
73869dc2
DE
12216dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12217 void *dwp_file_ptr)
3019eac3 12218{
9a3c8263 12219 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12220 const struct dwop_section_names *names = &dwop_section_names;
12221 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12222
80626a55 12223 /* Record the ELF section number for later lookup: this is what the
73869dc2 12224 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12225 gdb_assert (elf_section_nr < dwp_file->num_sections);
12226 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12227
80626a55
DE
12228 /* Look for specific sections that we need. */
12229 if (section_is_p (sectp->name, &names->str_dwo))
12230 {
049412e3 12231 dwp_file->sections.str.s.section = sectp;
fd361982 12232 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12233 }
12234 else if (section_is_p (sectp->name, &names->cu_index))
12235 {
049412e3 12236 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12237 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12238 }
12239 else if (section_is_p (sectp->name, &names->tu_index))
12240 {
049412e3 12241 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12242 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12243 }
12244}
3019eac3 12245
73869dc2
DE
12246/* This function is mapped across the sections and remembers the offset and
12247 size of each of the DWP version 2 debugging sections that we are interested
12248 in. This is split into a separate function because we don't know if we
12249 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12250
12251static void
12252dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12253{
9a3c8263 12254 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12255 const struct dwop_section_names *names = &dwop_section_names;
12256 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12257
12258 /* Record the ELF section number for later lookup: this is what the
12259 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12260 gdb_assert (elf_section_nr < dwp_file->num_sections);
12261 dwp_file->elf_sections[elf_section_nr] = sectp;
12262
12263 /* Look for specific sections that we need. */
12264 if (section_is_p (sectp->name, &names->abbrev_dwo))
12265 {
049412e3 12266 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12267 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12268 }
12269 else if (section_is_p (sectp->name, &names->info_dwo))
12270 {
049412e3 12271 dwp_file->sections.info.s.section = sectp;
fd361982 12272 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12273 }
12274 else if (section_is_p (sectp->name, &names->line_dwo))
12275 {
049412e3 12276 dwp_file->sections.line.s.section = sectp;
fd361982 12277 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12278 }
12279 else if (section_is_p (sectp->name, &names->loc_dwo))
12280 {
049412e3 12281 dwp_file->sections.loc.s.section = sectp;
fd361982 12282 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12283 }
12284 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12285 {
049412e3 12286 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12287 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12288 }
12289 else if (section_is_p (sectp->name, &names->macro_dwo))
12290 {
049412e3 12291 dwp_file->sections.macro.s.section = sectp;
fd361982 12292 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12293 }
12294 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12295 {
049412e3 12296 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12297 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12298 }
12299 else if (section_is_p (sectp->name, &names->types_dwo))
12300 {
049412e3 12301 dwp_file->sections.types.s.section = sectp;
fd361982 12302 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12303 }
12304}
12305
80626a55 12306/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12307
80626a55
DE
12308static hashval_t
12309hash_dwp_loaded_cutus (const void *item)
12310{
9a3c8263 12311 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12312
80626a55
DE
12313 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12314 return dwo_unit->signature;
3019eac3
DE
12315}
12316
80626a55 12317/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12318
80626a55
DE
12319static int
12320eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12321{
9a3c8263
SM
12322 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12323 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12324
80626a55
DE
12325 return dua->signature == dub->signature;
12326}
3019eac3 12327
80626a55 12328/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12329
48b490f2 12330static htab_up
298e9637 12331allocate_dwp_loaded_cutus_table ()
80626a55 12332{
48b490f2
TT
12333 return htab_up (htab_create_alloc (3,
12334 hash_dwp_loaded_cutus,
12335 eq_dwp_loaded_cutus,
12336 NULL, xcalloc, xfree));
80626a55 12337}
3019eac3 12338
ab5088bf
DE
12339/* Try to open DWP file FILE_NAME.
12340 The result is the bfd handle of the file.
12341 If there is a problem finding or opening the file, return NULL.
12342 Upon success, the canonicalized path of the file is stored in the bfd,
12343 same as symfile_bfd_open. */
12344
192b62ce 12345static gdb_bfd_ref_ptr
ed2dc618
SM
12346open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12347 const char *file_name)
ab5088bf 12348{
ed2dc618
SM
12349 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12350 1 /*is_dwp*/,
192b62ce 12351 1 /*search_cwd*/));
6ac97d4c
DE
12352 if (abfd != NULL)
12353 return abfd;
12354
12355 /* Work around upstream bug 15652.
12356 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12357 [Whether that's a "bug" is debatable, but it is getting in our way.]
12358 We have no real idea where the dwp file is, because gdb's realpath-ing
12359 of the executable's path may have discarded the needed info.
12360 [IWBN if the dwp file name was recorded in the executable, akin to
12361 .gnu_debuglink, but that doesn't exist yet.]
12362 Strip the directory from FILE_NAME and search again. */
12363 if (*debug_file_directory != '\0')
12364 {
12365 /* Don't implicitly search the current directory here.
12366 If the user wants to search "." to handle this case,
12367 it must be added to debug-file-directory. */
ed2dc618
SM
12368 return try_open_dwop_file (dwarf2_per_objfile,
12369 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12370 0 /*search_cwd*/);
12371 }
12372
12373 return NULL;
ab5088bf
DE
12374}
12375
80626a55
DE
12376/* Initialize the use of the DWP file for the current objfile.
12377 By convention the name of the DWP file is ${objfile}.dwp.
12378 The result is NULL if it can't be found. */
a766d390 12379
400174b1 12380static std::unique_ptr<struct dwp_file>
ed2dc618 12381open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12382{
12383 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12384
82bf32bc
JK
12385 /* Try to find first .dwp for the binary file before any symbolic links
12386 resolving. */
6c447423
DE
12387
12388 /* If the objfile is a debug file, find the name of the real binary
12389 file and get the name of dwp file from there. */
d721ba37 12390 std::string dwp_name;
6c447423
DE
12391 if (objfile->separate_debug_objfile_backlink != NULL)
12392 {
12393 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12394 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12395
d721ba37 12396 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12397 }
12398 else
d721ba37
PA
12399 dwp_name = objfile->original_name;
12400
12401 dwp_name += ".dwp";
80626a55 12402
ed2dc618 12403 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12404 if (dbfd == NULL
12405 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12406 {
12407 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12408 dwp_name = objfile_name (objfile);
12409 dwp_name += ".dwp";
ed2dc618 12410 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12411 }
12412
80626a55
DE
12413 if (dbfd == NULL)
12414 {
b4f54984 12415 if (dwarf_read_debug)
d721ba37 12416 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12417 return std::unique_ptr<dwp_file> ();
3019eac3 12418 }
400174b1
TT
12419
12420 const char *name = bfd_get_filename (dbfd.get ());
12421 std::unique_ptr<struct dwp_file> dwp_file
12422 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12423
0a0f4c01 12424 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12425 dwp_file->elf_sections =
12426 OBSTACK_CALLOC (&objfile->objfile_obstack,
12427 dwp_file->num_sections, asection *);
12428
400174b1
TT
12429 bfd_map_over_sections (dwp_file->dbfd.get (),
12430 dwarf2_locate_common_dwp_sections,
12431 dwp_file.get ());
80626a55 12432
400174b1
TT
12433 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12434 0);
80626a55 12435
400174b1
TT
12436 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12437 1);
80626a55 12438
73869dc2 12439 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12440 if (dwp_file->cus && dwp_file->tus
12441 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12442 {
12443 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12444 pretty bizarre. We use pulongest here because that's the established
4d65956b 12445 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12446 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12447 " TU version %s [in DWP file %s]"),
12448 pulongest (dwp_file->cus->version),
d721ba37 12449 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12450 }
08302ed2
DE
12451
12452 if (dwp_file->cus)
12453 dwp_file->version = dwp_file->cus->version;
12454 else if (dwp_file->tus)
12455 dwp_file->version = dwp_file->tus->version;
12456 else
12457 dwp_file->version = 2;
73869dc2
DE
12458
12459 if (dwp_file->version == 2)
400174b1
TT
12460 bfd_map_over_sections (dwp_file->dbfd.get (),
12461 dwarf2_locate_v2_dwp_sections,
12462 dwp_file.get ());
73869dc2 12463
298e9637
SM
12464 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12465 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12466
b4f54984 12467 if (dwarf_read_debug)
80626a55
DE
12468 {
12469 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12470 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12471 " %s CUs, %s TUs\n",
12472 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12473 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12474 }
12475
12476 return dwp_file;
3019eac3 12477}
c906108c 12478
ab5088bf
DE
12479/* Wrapper around open_and_init_dwp_file, only open it once. */
12480
12481static struct dwp_file *
ed2dc618 12482get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12483{
12484 if (! dwarf2_per_objfile->dwp_checked)
12485 {
ed2dc618
SM
12486 dwarf2_per_objfile->dwp_file
12487 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12488 dwarf2_per_objfile->dwp_checked = 1;
12489 }
400174b1 12490 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12491}
12492
80626a55
DE
12493/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12494 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12495 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12496 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12497 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12498
12499 This is called, for example, when wanting to read a variable with a
12500 complex location. Therefore we don't want to do file i/o for every call.
12501 Therefore we don't want to look for a DWO file on every call.
12502 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12503 then we check if we've already seen DWO_NAME, and only THEN do we check
12504 for a DWO file.
12505
1c658ad5 12506 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12507 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12508
3019eac3 12509static struct dwo_unit *
80626a55
DE
12510lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12511 const char *dwo_name, const char *comp_dir,
12512 ULONGEST signature, int is_debug_types)
3019eac3 12513{
ed2dc618 12514 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12515 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12516 const char *kind = is_debug_types ? "TU" : "CU";
12517 void **dwo_file_slot;
3019eac3 12518 struct dwo_file *dwo_file;
80626a55 12519 struct dwp_file *dwp_file;
cb1df416 12520
6a506a2d
DE
12521 /* First see if there's a DWP file.
12522 If we have a DWP file but didn't find the DWO inside it, don't
12523 look for the original DWO file. It makes gdb behave differently
12524 depending on whether one is debugging in the build tree. */
cf2c3c16 12525
ed2dc618 12526 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12527 if (dwp_file != NULL)
cf2c3c16 12528 {
80626a55
DE
12529 const struct dwp_hash_table *dwp_htab =
12530 is_debug_types ? dwp_file->tus : dwp_file->cus;
12531
12532 if (dwp_htab != NULL)
12533 {
12534 struct dwo_unit *dwo_cutu =
ed2dc618 12535 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12536 signature, is_debug_types);
80626a55
DE
12537
12538 if (dwo_cutu != NULL)
12539 {
b4f54984 12540 if (dwarf_read_debug)
80626a55
DE
12541 {
12542 fprintf_unfiltered (gdb_stdlog,
12543 "Virtual DWO %s %s found: @%s\n",
12544 kind, hex_string (signature),
12545 host_address_to_string (dwo_cutu));
12546 }
12547 return dwo_cutu;
12548 }
12549 }
12550 }
6a506a2d 12551 else
80626a55 12552 {
6a506a2d 12553 /* No DWP file, look for the DWO file. */
80626a55 12554
ed2dc618
SM
12555 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12556 dwo_name, comp_dir);
6a506a2d 12557 if (*dwo_file_slot == NULL)
80626a55 12558 {
6a506a2d
DE
12559 /* Read in the file and build a table of the CUs/TUs it contains. */
12560 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12561 }
6a506a2d 12562 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12563 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12564
6a506a2d 12565 if (dwo_file != NULL)
19c3d4c9 12566 {
6a506a2d
DE
12567 struct dwo_unit *dwo_cutu = NULL;
12568
12569 if (is_debug_types && dwo_file->tus)
12570 {
12571 struct dwo_unit find_dwo_cutu;
12572
12573 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12574 find_dwo_cutu.signature = signature;
9a3c8263 12575 dwo_cutu
b0b6a987
TT
12576 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12577 &find_dwo_cutu);
6a506a2d 12578 }
33c5cd75 12579 else if (!is_debug_types && dwo_file->cus)
80626a55 12580 {
33c5cd75
DB
12581 struct dwo_unit find_dwo_cutu;
12582
12583 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12584 find_dwo_cutu.signature = signature;
b0b6a987 12585 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12586 &find_dwo_cutu);
6a506a2d
DE
12587 }
12588
12589 if (dwo_cutu != NULL)
12590 {
b4f54984 12591 if (dwarf_read_debug)
6a506a2d
DE
12592 {
12593 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12594 kind, dwo_name, hex_string (signature),
12595 host_address_to_string (dwo_cutu));
12596 }
12597 return dwo_cutu;
80626a55
DE
12598 }
12599 }
2e276125 12600 }
9cdd5dbd 12601
80626a55
DE
12602 /* We didn't find it. This could mean a dwo_id mismatch, or
12603 someone deleted the DWO/DWP file, or the search path isn't set up
12604 correctly to find the file. */
12605
b4f54984 12606 if (dwarf_read_debug)
80626a55
DE
12607 {
12608 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12609 kind, dwo_name, hex_string (signature));
12610 }
3019eac3 12611
6656a72d
DE
12612 /* This is a warning and not a complaint because it can be caused by
12613 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12614 {
12615 /* Print the name of the DWP file if we looked there, helps the user
12616 better diagnose the problem. */
791afaa2 12617 std::string dwp_text;
43942612
DE
12618
12619 if (dwp_file != NULL)
791afaa2
TT
12620 dwp_text = string_printf (" [in DWP file %s]",
12621 lbasename (dwp_file->name));
43942612 12622
9d8780f0 12623 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12624 " [in module %s]"),
12625 kind, dwo_name, hex_string (signature),
791afaa2 12626 dwp_text.c_str (),
43942612 12627 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12628 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12629 }
3019eac3 12630 return NULL;
5fb290d7
DJ
12631}
12632
80626a55
DE
12633/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12634 See lookup_dwo_cutu_unit for details. */
12635
12636static struct dwo_unit *
12637lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12638 const char *dwo_name, const char *comp_dir,
12639 ULONGEST signature)
12640{
12641 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12642}
12643
12644/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12645 See lookup_dwo_cutu_unit for details. */
12646
12647static struct dwo_unit *
12648lookup_dwo_type_unit (struct signatured_type *this_tu,
12649 const char *dwo_name, const char *comp_dir)
12650{
12651 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12652}
12653
89e63ee4
DE
12654/* Traversal function for queue_and_load_all_dwo_tus. */
12655
12656static int
12657queue_and_load_dwo_tu (void **slot, void *info)
12658{
12659 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12660 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12661 ULONGEST signature = dwo_unit->signature;
12662 struct signatured_type *sig_type =
12663 lookup_dwo_signatured_type (per_cu->cu, signature);
12664
12665 if (sig_type != NULL)
12666 {
12667 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12668
12669 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12670 a real dependency of PER_CU on SIG_TYPE. That is detected later
12671 while processing PER_CU. */
12672 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12673 load_full_type_unit (sig_cu);
ae640021 12674 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12675 }
12676
12677 return 1;
12678}
12679
12680/* Queue all TUs contained in the DWO of PER_CU to be read in.
12681 The DWO may have the only definition of the type, though it may not be
12682 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12683 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12684
12685static void
12686queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12687{
12688 struct dwo_unit *dwo_unit;
12689 struct dwo_file *dwo_file;
12690
12691 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12692 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12693 gdb_assert (per_cu->cu != NULL);
12694
12695 dwo_unit = per_cu->cu->dwo_unit;
12696 gdb_assert (dwo_unit != NULL);
12697
12698 dwo_file = dwo_unit->dwo_file;
12699 if (dwo_file->tus != NULL)
b0b6a987
TT
12700 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12701 per_cu);
89e63ee4
DE
12702}
12703
3019eac3 12704/* Read in various DIEs. */
348e048f 12705
d389af10 12706/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12707 Inherit only the children of the DW_AT_abstract_origin DIE not being
12708 already referenced by DW_AT_abstract_origin from the children of the
12709 current DIE. */
d389af10
JK
12710
12711static void
12712inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12713{
12714 struct die_info *child_die;
791afaa2 12715 sect_offset *offsetp;
d389af10
JK
12716 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12717 struct die_info *origin_die;
12718 /* Iterator of the ORIGIN_DIE children. */
12719 struct die_info *origin_child_die;
d389af10 12720 struct attribute *attr;
cd02d79d
PA
12721 struct dwarf2_cu *origin_cu;
12722 struct pending **origin_previous_list_in_scope;
d389af10
JK
12723
12724 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12725 if (!attr)
12726 return;
12727
cd02d79d
PA
12728 /* Note that following die references may follow to a die in a
12729 different cu. */
12730
12731 origin_cu = cu;
12732 origin_die = follow_die_ref (die, attr, &origin_cu);
12733
12734 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12735 symbols in. */
12736 origin_previous_list_in_scope = origin_cu->list_in_scope;
12737 origin_cu->list_in_scope = cu->list_in_scope;
12738
edb3359d
DJ
12739 if (die->tag != origin_die->tag
12740 && !(die->tag == DW_TAG_inlined_subroutine
12741 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12742 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12743 sect_offset_str (die->sect_off),
12744 sect_offset_str (origin_die->sect_off));
d389af10 12745
791afaa2 12746 std::vector<sect_offset> offsets;
d389af10 12747
3ea89b92
PMR
12748 for (child_die = die->child;
12749 child_die && child_die->tag;
436c571c 12750 child_die = child_die->sibling)
3ea89b92
PMR
12751 {
12752 struct die_info *child_origin_die;
12753 struct dwarf2_cu *child_origin_cu;
12754
12755 /* We are trying to process concrete instance entries:
216f72a1 12756 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12757 it's not relevant to our analysis here. i.e. detecting DIEs that are
12758 present in the abstract instance but not referenced in the concrete
12759 one. */
216f72a1
JK
12760 if (child_die->tag == DW_TAG_call_site
12761 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12762 continue;
12763
c38f313d
DJ
12764 /* For each CHILD_DIE, find the corresponding child of
12765 ORIGIN_DIE. If there is more than one layer of
12766 DW_AT_abstract_origin, follow them all; there shouldn't be,
12767 but GCC versions at least through 4.4 generate this (GCC PR
12768 40573). */
3ea89b92
PMR
12769 child_origin_die = child_die;
12770 child_origin_cu = cu;
c38f313d
DJ
12771 while (1)
12772 {
cd02d79d
PA
12773 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12774 child_origin_cu);
c38f313d
DJ
12775 if (attr == NULL)
12776 break;
cd02d79d
PA
12777 child_origin_die = follow_die_ref (child_origin_die, attr,
12778 &child_origin_cu);
c38f313d
DJ
12779 }
12780
d389af10
JK
12781 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12782 counterpart may exist. */
c38f313d 12783 if (child_origin_die != child_die)
d389af10 12784 {
edb3359d
DJ
12785 if (child_die->tag != child_origin_die->tag
12786 && !(child_die->tag == DW_TAG_inlined_subroutine
12787 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12788 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12789 "different tags"),
9d8780f0
SM
12790 sect_offset_str (child_die->sect_off),
12791 sect_offset_str (child_origin_die->sect_off));
c38f313d 12792 if (child_origin_die->parent != origin_die)
b98664d3 12793 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12794 "different parents"),
9d8780f0
SM
12795 sect_offset_str (child_die->sect_off),
12796 sect_offset_str (child_origin_die->sect_off));
c38f313d 12797 else
791afaa2 12798 offsets.push_back (child_origin_die->sect_off);
d389af10 12799 }
d389af10 12800 }
791afaa2
TT
12801 std::sort (offsets.begin (), offsets.end ());
12802 sect_offset *offsets_end = offsets.data () + offsets.size ();
12803 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12804 if (offsetp[-1] == *offsetp)
b98664d3 12805 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12806 "to DIE %s as their abstract origin"),
12807 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12808
791afaa2 12809 offsetp = offsets.data ();
d389af10
JK
12810 origin_child_die = origin_die->child;
12811 while (origin_child_die && origin_child_die->tag)
12812 {
12813 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12814 while (offsetp < offsets_end
9c541725 12815 && *offsetp < origin_child_die->sect_off)
d389af10 12816 offsetp++;
b64f50a1 12817 if (offsetp >= offsets_end
9c541725 12818 || *offsetp > origin_child_die->sect_off)
d389af10 12819 {
adde2bff
DE
12820 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12821 Check whether we're already processing ORIGIN_CHILD_DIE.
12822 This can happen with mutually referenced abstract_origins.
12823 PR 16581. */
12824 if (!origin_child_die->in_process)
12825 process_die (origin_child_die, origin_cu);
d389af10 12826 }
436c571c 12827 origin_child_die = origin_child_die->sibling;
d389af10 12828 }
cd02d79d 12829 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12830
12831 if (cu != origin_cu)
12832 compute_delayed_physnames (origin_cu);
d389af10
JK
12833}
12834
c906108c 12835static void
e7c27a73 12836read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12837{
518817b3 12838 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12839 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12840 struct context_stack *newobj;
c906108c
SS
12841 CORE_ADDR lowpc;
12842 CORE_ADDR highpc;
12843 struct die_info *child_die;
edb3359d 12844 struct attribute *attr, *call_line, *call_file;
15d034d0 12845 const char *name;
e142c38c 12846 CORE_ADDR baseaddr;
801e3a5b 12847 struct block *block;
edb3359d 12848 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12849 std::vector<struct symbol *> template_args;
34eaf542 12850 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12851
12852 if (inlined_func)
12853 {
12854 /* If we do not have call site information, we can't show the
12855 caller of this inlined function. That's too confusing, so
12856 only use the scope for local variables. */
12857 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12858 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12859 if (call_line == NULL || call_file == NULL)
12860 {
12861 read_lexical_block_scope (die, cu);
12862 return;
12863 }
12864 }
c906108c 12865
b3b3bada 12866 baseaddr = objfile->text_section_offset ();
e142c38c 12867
94af9270 12868 name = dwarf2_name (die, cu);
c906108c 12869
e8d05480
JB
12870 /* Ignore functions with missing or empty names. These are actually
12871 illegal according to the DWARF standard. */
12872 if (name == NULL)
12873 {
b98664d3 12874 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12875 sect_offset_str (die->sect_off));
e8d05480
JB
12876 return;
12877 }
12878
12879 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12880 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12881 <= PC_BOUNDS_INVALID)
e8d05480 12882 {
ae4d0c03
PM
12883 attr = dwarf2_attr (die, DW_AT_external, cu);
12884 if (!attr || !DW_UNSND (attr))
b98664d3 12885 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12886 "for subprogram DIE at %s"),
12887 sect_offset_str (die->sect_off));
e8d05480
JB
12888 return;
12889 }
c906108c 12890
3e29f34a
MR
12891 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12892 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12893
34eaf542
TT
12894 /* If we have any template arguments, then we must allocate a
12895 different sort of symbol. */
436c571c 12896 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12897 {
12898 if (child_die->tag == DW_TAG_template_type_param
12899 || child_die->tag == DW_TAG_template_value_param)
12900 {
e623cf5d 12901 templ_func = allocate_template_symbol (objfile);
cf724bc9 12902 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12903 break;
12904 }
12905 }
12906
c24bdb02 12907 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12908 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12909 (struct symbol *) templ_func);
4c2df51b 12910
81873cc8 12911 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12912 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12913 cu->language);
12914
4cecd739
DJ
12915 /* If there is a location expression for DW_AT_frame_base, record
12916 it. */
e142c38c 12917 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12918 if (attr != nullptr)
fe978cb0 12919 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12920
63e43d3a
PMR
12921 /* If there is a location for the static link, record it. */
12922 newobj->static_link = NULL;
12923 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12924 if (attr != nullptr)
63e43d3a 12925 {
224c3ddb
SM
12926 newobj->static_link
12927 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12928 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12929 cu->per_cu->addr_type ());
63e43d3a
PMR
12930 }
12931
c24bdb02 12932 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12933
639d11d3 12934 if (die->child != NULL)
c906108c 12935 {
639d11d3 12936 child_die = die->child;
c906108c
SS
12937 while (child_die && child_die->tag)
12938 {
34eaf542
TT
12939 if (child_die->tag == DW_TAG_template_type_param
12940 || child_die->tag == DW_TAG_template_value_param)
12941 {
12942 struct symbol *arg = new_symbol (child_die, NULL, cu);
12943
f1078f66 12944 if (arg != NULL)
2f4732b0 12945 template_args.push_back (arg);
34eaf542
TT
12946 }
12947 else
12948 process_die (child_die, cu);
436c571c 12949 child_die = child_die->sibling;
c906108c
SS
12950 }
12951 }
12952
d389af10
JK
12953 inherit_abstract_dies (die, cu);
12954
4a811a97
UW
12955 /* If we have a DW_AT_specification, we might need to import using
12956 directives from the context of the specification DIE. See the
12957 comment in determine_prefix. */
12958 if (cu->language == language_cplus
12959 && dwarf2_attr (die, DW_AT_specification, cu))
12960 {
12961 struct dwarf2_cu *spec_cu = cu;
12962 struct die_info *spec_die = die_specification (die, &spec_cu);
12963
12964 while (spec_die)
12965 {
12966 child_die = spec_die->child;
12967 while (child_die && child_die->tag)
12968 {
12969 if (child_die->tag == DW_TAG_imported_module)
12970 process_die (child_die, spec_cu);
436c571c 12971 child_die = child_die->sibling;
4a811a97
UW
12972 }
12973
12974 /* In some cases, GCC generates specification DIEs that
12975 themselves contain DW_AT_specification attributes. */
12976 spec_die = die_specification (spec_die, &spec_cu);
12977 }
12978 }
12979
c24bdb02 12980 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12981 /* Make a block for the local symbols within. */
c24bdb02 12982 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12983 cstk.static_link, lowpc, highpc);
801e3a5b 12984
df8a16a1 12985 /* For C++, set the block's scope. */
45280282
IB
12986 if ((cu->language == language_cplus
12987 || cu->language == language_fortran
c44af4eb
TT
12988 || cu->language == language_d
12989 || cu->language == language_rust)
4d4ec4e5 12990 && cu->processing_has_namespace_info)
195a3f6c
TT
12991 block_set_scope (block, determine_prefix (die, cu),
12992 &objfile->objfile_obstack);
df8a16a1 12993
801e3a5b
JB
12994 /* If we have address ranges, record them. */
12995 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12996
a60f3166 12997 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12998
34eaf542 12999 /* Attach template arguments to function. */
2f4732b0 13000 if (!template_args.empty ())
34eaf542
TT
13001 {
13002 gdb_assert (templ_func != NULL);
13003
2f4732b0 13004 templ_func->n_template_arguments = template_args.size ();
34eaf542 13005 templ_func->template_arguments
8d749320
SM
13006 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13007 templ_func->n_template_arguments);
34eaf542 13008 memcpy (templ_func->template_arguments,
2f4732b0 13009 template_args.data (),
34eaf542 13010 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13011
13012 /* Make sure that the symtab is set on the new symbols. Even
13013 though they don't appear in this symtab directly, other parts
13014 of gdb assume that symbols do, and this is reasonably
13015 true. */
8634679f 13016 for (symbol *sym : template_args)
3e1d3d8c 13017 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13018 }
13019
208d8187
JB
13020 /* In C++, we can have functions nested inside functions (e.g., when
13021 a function declares a class that has methods). This means that
13022 when we finish processing a function scope, we may need to go
13023 back to building a containing block's symbol lists. */
c24bdb02
KS
13024 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13025 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13026
921e78cf
JB
13027 /* If we've finished processing a top-level function, subsequent
13028 symbols go in the file symbol list. */
c24bdb02
KS
13029 if (cu->get_builder ()->outermost_context_p ())
13030 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13031}
13032
13033/* Process all the DIES contained within a lexical block scope. Start
13034 a new scope, process the dies, and then close the scope. */
13035
13036static void
e7c27a73 13037read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13038{
518817b3 13039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13040 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13041 CORE_ADDR lowpc, highpc;
13042 struct die_info *child_die;
e142c38c
DJ
13043 CORE_ADDR baseaddr;
13044
b3b3bada 13045 baseaddr = objfile->text_section_offset ();
c906108c
SS
13046
13047 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13048 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13049 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13050 be nasty. Might be easier to properly extend generic blocks to
af34e669 13051 describe ranges. */
e385593e
JK
13052 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13053 {
13054 case PC_BOUNDS_NOT_PRESENT:
13055 /* DW_TAG_lexical_block has no attributes, process its children as if
13056 there was no wrapping by that DW_TAG_lexical_block.
13057 GCC does no longer produces such DWARF since GCC r224161. */
13058 for (child_die = die->child;
13059 child_die != NULL && child_die->tag;
436c571c 13060 child_die = child_die->sibling)
e385593e
JK
13061 process_die (child_die, cu);
13062 return;
13063 case PC_BOUNDS_INVALID:
13064 return;
13065 }
3e29f34a
MR
13066 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13067 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13068
c24bdb02 13069 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13070 if (die->child != NULL)
c906108c 13071 {
639d11d3 13072 child_die = die->child;
c906108c
SS
13073 while (child_die && child_die->tag)
13074 {
e7c27a73 13075 process_die (child_die, cu);
436c571c 13076 child_die = child_die->sibling;
c906108c
SS
13077 }
13078 }
3ea89b92 13079 inherit_abstract_dies (die, cu);
c24bdb02 13080 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13081
c24bdb02
KS
13082 if (*cu->get_builder ()->get_local_symbols () != NULL
13083 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13084 {
801e3a5b 13085 struct block *block
c24bdb02 13086 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13087 cstk.start_addr, highpc);
801e3a5b
JB
13088
13089 /* Note that recording ranges after traversing children, as we
13090 do here, means that recording a parent's ranges entails
13091 walking across all its children's ranges as they appear in
13092 the address map, which is quadratic behavior.
13093
13094 It would be nicer to record the parent's ranges before
13095 traversing its children, simply overriding whatever you find
13096 there. But since we don't even decide whether to create a
13097 block until after we've traversed its children, that's hard
13098 to do. */
13099 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13100 }
c24bdb02
KS
13101 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13102 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13103}
13104
216f72a1 13105/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13106
13107static void
13108read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13109{
518817b3 13110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13111 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13112 CORE_ADDR pc, baseaddr;
13113 struct attribute *attr;
13114 struct call_site *call_site, call_site_local;
13115 void **slot;
13116 int nparams;
13117 struct die_info *child_die;
13118
b3b3bada 13119 baseaddr = objfile->text_section_offset ();
96408a79 13120
216f72a1
JK
13121 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13122 if (attr == NULL)
13123 {
13124 /* This was a pre-DWARF-5 GNU extension alias
13125 for DW_AT_call_return_pc. */
13126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13127 }
96408a79
SA
13128 if (!attr)
13129 {
b98664d3 13130 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13131 "DIE %s [in module %s]"),
13132 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13133 return;
13134 }
cd6c91b4 13135 pc = attr->value_as_address () + baseaddr;
3e29f34a 13136 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13137
13138 if (cu->call_site_htab == NULL)
13139 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13140 NULL, &objfile->objfile_obstack,
13141 hashtab_obstack_allocate, NULL);
13142 call_site_local.pc = pc;
13143 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13144 if (*slot != NULL)
13145 {
b98664d3 13146 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13147 "DIE %s [in module %s]"),
13148 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13149 objfile_name (objfile));
96408a79
SA
13150 return;
13151 }
13152
13153 /* Count parameters at the caller. */
13154
13155 nparams = 0;
13156 for (child_die = die->child; child_die && child_die->tag;
436c571c 13157 child_die = child_die->sibling)
96408a79 13158 {
216f72a1
JK
13159 if (child_die->tag != DW_TAG_call_site_parameter
13160 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13161 {
b98664d3 13162 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13163 "DW_TAG_call_site child DIE %s [in module %s]"),
13164 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13165 objfile_name (objfile));
96408a79
SA
13166 continue;
13167 }
13168
13169 nparams++;
13170 }
13171
224c3ddb
SM
13172 call_site
13173 = ((struct call_site *)
13174 obstack_alloc (&objfile->objfile_obstack,
13175 sizeof (*call_site)
13176 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13177 *slot = call_site;
13178 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13179 call_site->pc = pc;
13180
216f72a1
JK
13181 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13182 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13183 {
13184 struct die_info *func_die;
13185
13186 /* Skip also over DW_TAG_inlined_subroutine. */
13187 for (func_die = die->parent;
13188 func_die && func_die->tag != DW_TAG_subprogram
13189 && func_die->tag != DW_TAG_subroutine_type;
13190 func_die = func_die->parent);
13191
216f72a1
JK
13192 /* DW_AT_call_all_calls is a superset
13193 of DW_AT_call_all_tail_calls. */
96408a79 13194 if (func_die
216f72a1 13195 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13196 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13197 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13198 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13199 {
13200 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13201 not complete. But keep CALL_SITE for look ups via call_site_htab,
13202 both the initial caller containing the real return address PC and
13203 the final callee containing the current PC of a chain of tail
13204 calls do not need to have the tail call list complete. But any
13205 function candidate for a virtual tail call frame searched via
13206 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13207 determined unambiguously. */
13208 }
13209 else
13210 {
13211 struct type *func_type = NULL;
13212
13213 if (func_die)
13214 func_type = get_die_type (func_die, cu);
13215 if (func_type != NULL)
13216 {
13217 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13218
13219 /* Enlist this call site to the function. */
13220 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13221 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13222 }
13223 else
b98664d3 13224 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13225 "DIE %s [in module %s]"),
13226 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13227 }
13228 }
13229
216f72a1
JK
13230 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13231 if (attr == NULL)
13232 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13233 if (attr == NULL)
13234 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13235 if (attr == NULL)
216f72a1
JK
13236 {
13237 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13238 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13239 }
96408a79 13240 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13241 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13242 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13243 else if (attr->form_is_block ())
96408a79
SA
13244 {
13245 struct dwarf2_locexpr_baton *dlbaton;
13246
8d749320 13247 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13248 dlbaton->data = DW_BLOCK (attr)->data;
13249 dlbaton->size = DW_BLOCK (attr)->size;
13250 dlbaton->per_cu = cu->per_cu;
13251
13252 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13253 }
cd6c91b4 13254 else if (attr->form_is_ref ())
96408a79 13255 {
96408a79
SA
13256 struct dwarf2_cu *target_cu = cu;
13257 struct die_info *target_die;
13258
ac9ec31b 13259 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13260 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13261 if (die_is_declaration (target_die, target_cu))
13262 {
7d45c7c3 13263 const char *target_physname;
9112db09
JK
13264
13265 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13266 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13267 if (target_physname == NULL)
9112db09 13268 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13269 if (target_physname == NULL)
b98664d3 13270 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13271 "physname, for referencing DIE %s [in module %s]"),
13272 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13273 else
7d455152 13274 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13275 }
13276 else
13277 {
13278 CORE_ADDR lowpc;
13279
13280 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13281 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13282 <= PC_BOUNDS_INVALID)
b98664d3 13283 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13284 "low pc, for referencing DIE %s [in module %s]"),
13285 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13286 else
3e29f34a
MR
13287 {
13288 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13289 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13290 }
96408a79
SA
13291 }
13292 }
13293 else
b98664d3 13294 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13295 "block nor reference, for DIE %s [in module %s]"),
13296 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13297
13298 call_site->per_cu = cu->per_cu;
13299
13300 for (child_die = die->child;
13301 child_die && child_die->tag;
436c571c 13302 child_die = child_die->sibling)
96408a79 13303 {
96408a79 13304 struct call_site_parameter *parameter;
1788b2d3 13305 struct attribute *loc, *origin;
96408a79 13306
216f72a1
JK
13307 if (child_die->tag != DW_TAG_call_site_parameter
13308 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13309 {
13310 /* Already printed the complaint above. */
13311 continue;
13312 }
13313
13314 gdb_assert (call_site->parameter_count < nparams);
13315 parameter = &call_site->parameter[call_site->parameter_count];
13316
1788b2d3
JK
13317 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13318 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13319 register is contained in DW_AT_call_value. */
96408a79 13320
24c5c679 13321 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13322 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13323 if (origin == NULL)
13324 {
13325 /* This was a pre-DWARF-5 GNU extension alias
13326 for DW_AT_call_parameter. */
13327 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13328 }
cd6c91b4 13329 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13330 {
1788b2d3 13331 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13332
0826b30a 13333 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13334 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13335 {
13336 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13337 binding can be done only inside one CU. Such referenced DIE
13338 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13339 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13340 "DW_TAG_call_site child DIE %s [in module %s]"),
13341 sect_offset_str (child_die->sect_off),
9c541725 13342 objfile_name (objfile));
d76b7dbc
JK
13343 continue;
13344 }
9c541725
PA
13345 parameter->u.param_cu_off
13346 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13347 }
4fc6c0d5 13348 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13349 {
b98664d3 13350 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13351 "DW_TAG_call_site child DIE %s [in module %s]"),
13352 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13353 continue;
13354 }
24c5c679 13355 else
96408a79 13356 {
24c5c679
JK
13357 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13358 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13359 if (parameter->u.dwarf_reg != -1)
13360 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13361 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13362 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13363 &parameter->u.fb_offset))
13364 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13365 else
13366 {
b98664d3 13367 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13368 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13369 "DW_TAG_call_site child DIE %s "
24c5c679 13370 "[in module %s]"),
9d8780f0 13371 sect_offset_str (child_die->sect_off),
9c541725 13372 objfile_name (objfile));
24c5c679
JK
13373 continue;
13374 }
96408a79
SA
13375 }
13376
216f72a1
JK
13377 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13378 if (attr == NULL)
13379 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13380 if (attr == NULL || !attr->form_is_block ())
96408a79 13381 {
b98664d3 13382 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13383 "DW_TAG_call_site child DIE %s [in module %s]"),
13384 sect_offset_str (child_die->sect_off),
9c541725 13385 objfile_name (objfile));
96408a79
SA
13386 continue;
13387 }
13388 parameter->value = DW_BLOCK (attr)->data;
13389 parameter->value_size = DW_BLOCK (attr)->size;
13390
13391 /* Parameters are not pre-cleared by memset above. */
13392 parameter->data_value = NULL;
13393 parameter->data_value_size = 0;
13394 call_site->parameter_count++;
13395
216f72a1
JK
13396 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13397 if (attr == NULL)
13398 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13399 if (attr != nullptr)
96408a79 13400 {
4fc6c0d5 13401 if (!attr->form_is_block ())
b98664d3 13402 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13403 "DW_TAG_call_site child DIE %s [in module %s]"),
13404 sect_offset_str (child_die->sect_off),
9c541725 13405 objfile_name (objfile));
96408a79
SA
13406 else
13407 {
13408 parameter->data_value = DW_BLOCK (attr)->data;
13409 parameter->data_value_size = DW_BLOCK (attr)->size;
13410 }
13411 }
13412 }
13413}
13414
71a3c369
TT
13415/* Helper function for read_variable. If DIE represents a virtual
13416 table, then return the type of the concrete object that is
13417 associated with the virtual table. Otherwise, return NULL. */
13418
13419static struct type *
13420rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13421{
13422 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13423 if (attr == NULL)
13424 return NULL;
13425
13426 /* Find the type DIE. */
13427 struct die_info *type_die = NULL;
13428 struct dwarf2_cu *type_cu = cu;
13429
cd6c91b4 13430 if (attr->form_is_ref ())
71a3c369
TT
13431 type_die = follow_die_ref (die, attr, &type_cu);
13432 if (type_die == NULL)
13433 return NULL;
13434
13435 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13436 return NULL;
13437 return die_containing_type (type_die, type_cu);
13438}
13439
13440/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13441
13442static void
13443read_variable (struct die_info *die, struct dwarf2_cu *cu)
13444{
13445 struct rust_vtable_symbol *storage = NULL;
13446
13447 if (cu->language == language_rust)
13448 {
13449 struct type *containing_type = rust_containing_type (die, cu);
13450
13451 if (containing_type != NULL)
13452 {
518817b3 13453 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13454
468c0cbb 13455 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13456 initialize_objfile_symbol (storage);
13457 storage->concrete_type = containing_type;
cf724bc9 13458 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13459 }
13460 }
13461
e4a62c65
TV
13462 struct symbol *res = new_symbol (die, NULL, cu, storage);
13463 struct attribute *abstract_origin
13464 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13465 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13466 if (res == NULL && loc && abstract_origin)
13467 {
13468 /* We have a variable without a name, but with a location and an abstract
13469 origin. This may be a concrete instance of an abstract variable
13470 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13471 later. */
13472 struct dwarf2_cu *origin_cu = cu;
13473 struct die_info *origin_die
13474 = follow_die_ref (die, abstract_origin, &origin_cu);
13475 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13476 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13477 }
71a3c369
TT
13478}
13479
43988095
JK
13480/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13481 reading .debug_rnglists.
13482 Callback's type should be:
13483 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13484 Return true if the attributes are present and valid, otherwise,
13485 return false. */
13486
13487template <typename Callback>
13488static bool
13489dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13490 Callback &&callback)
13491{
ed2dc618 13492 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13493 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13494 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13495 bfd *obfd = objfile->obfd;
43988095 13496 /* Base address selection entry. */
2b24b6e4 13497 gdb::optional<CORE_ADDR> base;
43988095 13498 const gdb_byte *buffer;
43988095
JK
13499 CORE_ADDR baseaddr;
13500 bool overflow = false;
13501
43988095
JK
13502 base = cu->base_address;
13503
96b79293 13504 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13505 if (offset >= dwarf2_per_objfile->rnglists.size)
13506 {
b98664d3 13507 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13508 offset);
13509 return false;
13510 }
13511 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13512
b3b3bada 13513 baseaddr = objfile->text_section_offset ();
43988095
JK
13514
13515 while (1)
13516 {
7814882a
JK
13517 /* Initialize it due to a false compiler warning. */
13518 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13519 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13520 + dwarf2_per_objfile->rnglists.size);
13521 unsigned int bytes_read;
13522
13523 if (buffer == buf_end)
13524 {
13525 overflow = true;
13526 break;
13527 }
13528 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13529 switch (rlet)
13530 {
13531 case DW_RLE_end_of_list:
13532 break;
13533 case DW_RLE_base_address:
13534 if (buffer + cu->header.addr_size > buf_end)
13535 {
13536 overflow = true;
13537 break;
13538 }
c8a7a66f 13539 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13540 buffer += bytes_read;
13541 break;
13542 case DW_RLE_start_length:
13543 if (buffer + cu->header.addr_size > buf_end)
13544 {
13545 overflow = true;
13546 break;
13547 }
c8a7a66f
TT
13548 range_beginning = cu->header.read_address (obfd, buffer,
13549 &bytes_read);
43988095
JK
13550 buffer += bytes_read;
13551 range_end = (range_beginning
13552 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13553 buffer += bytes_read;
13554 if (buffer > buf_end)
13555 {
13556 overflow = true;
13557 break;
13558 }
13559 break;
13560 case DW_RLE_offset_pair:
13561 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13562 buffer += bytes_read;
13563 if (buffer > buf_end)
13564 {
13565 overflow = true;
13566 break;
13567 }
13568 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13569 buffer += bytes_read;
13570 if (buffer > buf_end)
13571 {
13572 overflow = true;
13573 break;
13574 }
13575 break;
13576 case DW_RLE_start_end:
13577 if (buffer + 2 * cu->header.addr_size > buf_end)
13578 {
13579 overflow = true;
13580 break;
13581 }
c8a7a66f
TT
13582 range_beginning = cu->header.read_address (obfd, buffer,
13583 &bytes_read);
43988095 13584 buffer += bytes_read;
c8a7a66f 13585 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13586 buffer += bytes_read;
13587 break;
13588 default:
b98664d3 13589 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13590 return false;
13591 }
13592 if (rlet == DW_RLE_end_of_list || overflow)
13593 break;
13594 if (rlet == DW_RLE_base_address)
13595 continue;
13596
2b24b6e4 13597 if (!base.has_value ())
43988095
JK
13598 {
13599 /* We have no valid base address for the ranges
13600 data. */
b98664d3 13601 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13602 return false;
13603 }
13604
13605 if (range_beginning > range_end)
13606 {
13607 /* Inverted range entries are invalid. */
b98664d3 13608 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13609 return false;
13610 }
13611
13612 /* Empty range entries have no effect. */
13613 if (range_beginning == range_end)
13614 continue;
13615
2b24b6e4
TT
13616 range_beginning += *base;
13617 range_end += *base;
43988095
JK
13618
13619 /* A not-uncommon case of bad debug info.
13620 Don't pollute the addrmap with bad data. */
13621 if (range_beginning + baseaddr == 0
13622 && !dwarf2_per_objfile->has_section_at_zero)
13623 {
b98664d3 13624 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13625 " [in module %s]"), objfile_name (objfile));
13626 continue;
13627 }
13628
13629 callback (range_beginning, range_end);
13630 }
13631
13632 if (overflow)
13633 {
b98664d3 13634 complaint (_("Offset %d is not terminated "
43988095
JK
13635 "for DW_AT_ranges attribute"),
13636 offset);
13637 return false;
13638 }
13639
13640 return true;
13641}
13642
13643/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13644 Callback's type should be:
13645 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13646 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13647
43988095 13648template <typename Callback>
43039443 13649static int
5f46c5a5 13650dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13651 Callback &&callback)
43039443 13652{
ed2dc618 13653 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13654 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13655 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13656 struct comp_unit_head *cu_header = &cu->header;
13657 bfd *obfd = objfile->obfd;
13658 unsigned int addr_size = cu_header->addr_size;
13659 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13660 /* Base address selection entry. */
2b24b6e4 13661 gdb::optional<CORE_ADDR> base;
43039443 13662 unsigned int dummy;
d521ce57 13663 const gdb_byte *buffer;
ff013f42 13664 CORE_ADDR baseaddr;
43039443 13665
43988095
JK
13666 if (cu_header->version >= 5)
13667 return dwarf2_rnglists_process (offset, cu, callback);
13668
d00adf39 13669 base = cu->base_address;
43039443 13670
96b79293 13671 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13672 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13673 {
b98664d3 13674 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13675 offset);
13676 return 0;
13677 }
dce234bc 13678 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13679
b3b3bada 13680 baseaddr = objfile->text_section_offset ();
ff013f42 13681
43039443
JK
13682 while (1)
13683 {
13684 CORE_ADDR range_beginning, range_end;
13685
c8a7a66f 13686 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13687 buffer += addr_size;
c8a7a66f 13688 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13689 buffer += addr_size;
13690 offset += 2 * addr_size;
13691
13692 /* An end of list marker is a pair of zero addresses. */
13693 if (range_beginning == 0 && range_end == 0)
13694 /* Found the end of list entry. */
13695 break;
13696
13697 /* Each base address selection entry is a pair of 2 values.
13698 The first is the largest possible address, the second is
13699 the base address. Check for a base address here. */
13700 if ((range_beginning & mask) == mask)
13701 {
28d2bfb9
AB
13702 /* If we found the largest possible address, then we already
13703 have the base address in range_end. */
13704 base = range_end;
43039443
JK
13705 continue;
13706 }
13707
2b24b6e4 13708 if (!base.has_value ())
43039443
JK
13709 {
13710 /* We have no valid base address for the ranges
13711 data. */
b98664d3 13712 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13713 return 0;
13714 }
13715
9277c30c
UW
13716 if (range_beginning > range_end)
13717 {
13718 /* Inverted range entries are invalid. */
b98664d3 13719 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13720 return 0;
13721 }
13722
13723 /* Empty range entries have no effect. */
13724 if (range_beginning == range_end)
13725 continue;
13726
2b24b6e4
TT
13727 range_beginning += *base;
13728 range_end += *base;
43039443 13729
01093045
DE
13730 /* A not-uncommon case of bad debug info.
13731 Don't pollute the addrmap with bad data. */
13732 if (range_beginning + baseaddr == 0
13733 && !dwarf2_per_objfile->has_section_at_zero)
13734 {
b98664d3 13735 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13736 " [in module %s]"), objfile_name (objfile));
01093045
DE
13737 continue;
13738 }
13739
5f46c5a5
JK
13740 callback (range_beginning, range_end);
13741 }
13742
13743 return 1;
13744}
13745
13746/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13747 Return 1 if the attributes are present and valid, otherwise, return 0.
13748 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13749
13750static int
13751dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13752 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13753 dwarf2_psymtab *ranges_pst)
5f46c5a5 13754{
518817b3 13755 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13756 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13757 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13758 int low_set = 0;
13759 CORE_ADDR low = 0;
13760 CORE_ADDR high = 0;
13761 int retval;
13762
13763 retval = dwarf2_ranges_process (offset, cu,
13764 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13765 {
9277c30c 13766 if (ranges_pst != NULL)
3e29f34a
MR
13767 {
13768 CORE_ADDR lowpc;
13769 CORE_ADDR highpc;
13770
79748972
TT
13771 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13772 range_beginning + baseaddr)
13773 - baseaddr);
13774 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13775 range_end + baseaddr)
13776 - baseaddr);
d320c2b5
TT
13777 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13778 lowpc, highpc - 1, ranges_pst);
3e29f34a 13779 }
ff013f42 13780
43039443
JK
13781 /* FIXME: This is recording everything as a low-high
13782 segment of consecutive addresses. We should have a
13783 data structure for discontiguous block ranges
13784 instead. */
13785 if (! low_set)
13786 {
13787 low = range_beginning;
13788 high = range_end;
13789 low_set = 1;
13790 }
13791 else
13792 {
13793 if (range_beginning < low)
13794 low = range_beginning;
13795 if (range_end > high)
13796 high = range_end;
13797 }
5f46c5a5
JK
13798 });
13799 if (!retval)
13800 return 0;
43039443
JK
13801
13802 if (! low_set)
13803 /* If the first entry is an end-of-list marker, the range
13804 describes an empty scope, i.e. no instructions. */
13805 return 0;
13806
13807 if (low_return)
13808 *low_return = low;
13809 if (high_return)
13810 *high_return = high;
13811 return 1;
13812}
13813
3a2b436a
JK
13814/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13815 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13816 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13817
3a2b436a 13818static enum pc_bounds_kind
af34e669 13819dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13820 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13821 dwarf2_psymtab *pst)
c906108c 13822{
518817b3
SM
13823 struct dwarf2_per_objfile *dwarf2_per_objfile
13824 = cu->per_cu->dwarf2_per_objfile;
c906108c 13825 struct attribute *attr;
91da1414 13826 struct attribute *attr_high;
af34e669
DJ
13827 CORE_ADDR low = 0;
13828 CORE_ADDR high = 0;
e385593e 13829 enum pc_bounds_kind ret;
c906108c 13830
91da1414
MW
13831 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13832 if (attr_high)
af34e669 13833 {
e142c38c 13834 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13835 if (attr != nullptr)
91da1414 13836 {
cd6c91b4
TT
13837 low = attr->value_as_address ();
13838 high = attr_high->value_as_address ();
13839 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13840 high += low;
91da1414 13841 }
af34e669
DJ
13842 else
13843 /* Found high w/o low attribute. */
e385593e 13844 return PC_BOUNDS_INVALID;
af34e669
DJ
13845
13846 /* Found consecutive range of addresses. */
3a2b436a 13847 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13848 }
c906108c 13849 else
af34e669 13850 {
e142c38c 13851 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13852 if (attr != NULL)
13853 {
18a8505e 13854 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13855 We take advantage of the fact that DW_AT_ranges does not appear
13856 in DW_TAG_compile_unit of DWO files. */
13857 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13858 unsigned int ranges_offset = (DW_UNSND (attr)
13859 + (need_ranges_base
13860 ? cu->ranges_base
13861 : 0));
2e3cf129 13862
af34e669 13863 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13864 .debug_ranges section. */
2e3cf129 13865 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13866 return PC_BOUNDS_INVALID;
43039443 13867 /* Found discontinuous range of addresses. */
3a2b436a 13868 ret = PC_BOUNDS_RANGES;
af34e669 13869 }
e385593e
JK
13870 else
13871 return PC_BOUNDS_NOT_PRESENT;
af34e669 13872 }
c906108c 13873
48fbe735 13874 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13875 if (high <= low)
e385593e 13876 return PC_BOUNDS_INVALID;
c906108c
SS
13877
13878 /* When using the GNU linker, .gnu.linkonce. sections are used to
13879 eliminate duplicate copies of functions and vtables and such.
13880 The linker will arbitrarily choose one and discard the others.
13881 The AT_*_pc values for such functions refer to local labels in
13882 these sections. If the section from that file was discarded, the
13883 labels are not in the output, so the relocs get a value of 0.
13884 If this is a discarded function, mark the pc bounds as invalid,
13885 so that GDB will ignore it. */
72dca2f5 13886 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13887 return PC_BOUNDS_INVALID;
c906108c
SS
13888
13889 *lowpc = low;
96408a79
SA
13890 if (highpc)
13891 *highpc = high;
af34e669 13892 return ret;
c906108c
SS
13893}
13894
b084d499
JB
13895/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13896 its low and high PC addresses. Do nothing if these addresses could not
13897 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13898 and HIGHPC to the high address if greater than HIGHPC. */
13899
13900static void
13901dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13902 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13903 struct dwarf2_cu *cu)
13904{
13905 CORE_ADDR low, high;
13906 struct die_info *child = die->child;
13907
e385593e 13908 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13909 {
325fac50
PA
13910 *lowpc = std::min (*lowpc, low);
13911 *highpc = std::max (*highpc, high);
b084d499
JB
13912 }
13913
13914 /* If the language does not allow nested subprograms (either inside
13915 subprograms or lexical blocks), we're done. */
13916 if (cu->language != language_ada)
13917 return;
6e70227d 13918
b084d499
JB
13919 /* Check all the children of the given DIE. If it contains nested
13920 subprograms, then check their pc bounds. Likewise, we need to
13921 check lexical blocks as well, as they may also contain subprogram
13922 definitions. */
13923 while (child && child->tag)
13924 {
13925 if (child->tag == DW_TAG_subprogram
13926 || child->tag == DW_TAG_lexical_block)
13927 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13928 child = child->sibling;
b084d499
JB
13929 }
13930}
13931
fae299cd
DC
13932/* Get the low and high pc's represented by the scope DIE, and store
13933 them in *LOWPC and *HIGHPC. If the correct values can't be
13934 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13935
13936static void
13937get_scope_pc_bounds (struct die_info *die,
13938 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13939 struct dwarf2_cu *cu)
13940{
13941 CORE_ADDR best_low = (CORE_ADDR) -1;
13942 CORE_ADDR best_high = (CORE_ADDR) 0;
13943 CORE_ADDR current_low, current_high;
13944
3a2b436a 13945 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13946 >= PC_BOUNDS_RANGES)
fae299cd
DC
13947 {
13948 best_low = current_low;
13949 best_high = current_high;
13950 }
13951 else
13952 {
13953 struct die_info *child = die->child;
13954
13955 while (child && child->tag)
13956 {
13957 switch (child->tag) {
13958 case DW_TAG_subprogram:
b084d499 13959 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13960 break;
13961 case DW_TAG_namespace:
f55ee35c 13962 case DW_TAG_module:
fae299cd
DC
13963 /* FIXME: carlton/2004-01-16: Should we do this for
13964 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13965 that current GCC's always emit the DIEs corresponding
13966 to definitions of methods of classes as children of a
13967 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13968 the DIEs giving the declarations, which could be
13969 anywhere). But I don't see any reason why the
13970 standards says that they have to be there. */
13971 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13972
13973 if (current_low != ((CORE_ADDR) -1))
13974 {
325fac50
PA
13975 best_low = std::min (best_low, current_low);
13976 best_high = std::max (best_high, current_high);
fae299cd
DC
13977 }
13978 break;
13979 default:
0963b4bd 13980 /* Ignore. */
fae299cd
DC
13981 break;
13982 }
13983
436c571c 13984 child = child->sibling;
fae299cd
DC
13985 }
13986 }
13987
13988 *lowpc = best_low;
13989 *highpc = best_high;
13990}
13991
801e3a5b
JB
13992/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13993 in DIE. */
380bca97 13994
801e3a5b
JB
13995static void
13996dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13997 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13998{
518817b3 13999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14000 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14001 struct attribute *attr;
91da1414 14002 struct attribute *attr_high;
801e3a5b 14003
91da1414
MW
14004 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14005 if (attr_high)
801e3a5b 14006 {
801e3a5b 14007 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14008 if (attr != nullptr)
801e3a5b 14009 {
cd6c91b4
TT
14010 CORE_ADDR low = attr->value_as_address ();
14011 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14012
cd6c91b4 14013 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14014 high += low;
9a619af0 14015
3e29f34a
MR
14016 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14017 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14018 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14019 }
14020 }
14021
14022 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14023 if (attr != nullptr)
801e3a5b 14024 {
18a8505e 14025 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14026 We take advantage of the fact that DW_AT_ranges does not appear
14027 in DW_TAG_compile_unit of DWO files. */
14028 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14029
14030 /* The value of the DW_AT_ranges attribute is the offset of the
14031 address range list in the .debug_ranges section. */
ab435259
DE
14032 unsigned long offset = (DW_UNSND (attr)
14033 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14034
2d5f09ec 14035 std::vector<blockrange> blockvec;
5f46c5a5
JK
14036 dwarf2_ranges_process (offset, cu,
14037 [&] (CORE_ADDR start, CORE_ADDR end)
14038 {
58fdfd2c
JK
14039 start += baseaddr;
14040 end += baseaddr;
5f46c5a5
JK
14041 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14042 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14043 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14044 blockvec.emplace_back (start, end);
5f46c5a5 14045 });
2d5f09ec
KB
14046
14047 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14048 }
14049}
14050
685b1105
JK
14051/* Check whether the producer field indicates either of GCC < 4.6, or the
14052 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14053
685b1105
JK
14054static void
14055check_producer (struct dwarf2_cu *cu)
60d5a603 14056{
38360086 14057 int major, minor;
60d5a603
JK
14058
14059 if (cu->producer == NULL)
14060 {
14061 /* For unknown compilers expect their behavior is DWARF version
14062 compliant.
14063
14064 GCC started to support .debug_types sections by -gdwarf-4 since
14065 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14066 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14067 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14068 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14069 }
b1ffba5a 14070 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14071 {
38360086
MW
14072 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14073 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14074 }
5230b05a 14075 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14076 {
14077 cu->producer_is_icc = true;
14078 cu->producer_is_icc_lt_14 = major < 14;
14079 }
c258c396
JD
14080 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14081 cu->producer_is_codewarrior = true;
685b1105
JK
14082 else
14083 {
14084 /* For other non-GCC compilers, expect their behavior is DWARF version
14085 compliant. */
60d5a603
JK
14086 }
14087
9068261f 14088 cu->checked_producer = true;
685b1105 14089}
ba919b58 14090
685b1105
JK
14091/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14092 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14093 during 4.6.0 experimental. */
14094
9068261f 14095static bool
685b1105
JK
14096producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14097{
14098 if (!cu->checked_producer)
14099 check_producer (cu);
14100
14101 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14102}
14103
c258c396
JD
14104
14105/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14106 with incorrect is_stmt attributes. */
14107
14108static bool
14109producer_is_codewarrior (struct dwarf2_cu *cu)
14110{
14111 if (!cu->checked_producer)
14112 check_producer (cu);
14113
14114 return cu->producer_is_codewarrior;
14115}
14116
405feb71 14117/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14118 DW_AT_accessibility. */
14119
14120static enum dwarf_access_attribute
14121dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14122{
14123 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14124 {
14125 /* The default DWARF 2 accessibility for members is public, the default
14126 accessibility for inheritance is private. */
14127
14128 if (die->tag != DW_TAG_inheritance)
14129 return DW_ACCESS_public;
14130 else
14131 return DW_ACCESS_private;
14132 }
14133 else
14134 {
14135 /* DWARF 3+ defines the default accessibility a different way. The same
14136 rules apply now for DW_TAG_inheritance as for the members and it only
14137 depends on the container kind. */
14138
14139 if (die->parent->tag == DW_TAG_class_type)
14140 return DW_ACCESS_private;
14141 else
14142 return DW_ACCESS_public;
14143 }
14144}
14145
74ac6d43
TT
14146/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14147 offset. If the attribute was not found return 0, otherwise return
14148 1. If it was found but could not properly be handled, set *OFFSET
14149 to 0. */
14150
14151static int
14152handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14153 LONGEST *offset)
14154{
14155 struct attribute *attr;
14156
14157 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14158 if (attr != NULL)
14159 {
14160 *offset = 0;
14161
14162 /* Note that we do not check for a section offset first here.
14163 This is because DW_AT_data_member_location is new in DWARF 4,
14164 so if we see it, we can assume that a constant form is really
14165 a constant and not a section offset. */
cd6c91b4 14166 if (attr->form_is_constant ())
0826b30a 14167 *offset = attr->constant_value (0);
cd6c91b4 14168 else if (attr->form_is_section_offset ())
74ac6d43 14169 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14170 else if (attr->form_is_block ())
74ac6d43
TT
14171 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14172 else
14173 dwarf2_complex_location_expr_complaint ();
14174
14175 return 1;
14176 }
14177
14178 return 0;
14179}
14180
c906108c
SS
14181/* Add an aggregate field to the field list. */
14182
14183static void
107d2387 14184dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14185 struct dwarf2_cu *cu)
6e70227d 14186{
518817b3 14187 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14188 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14189 struct nextfield *new_field;
14190 struct attribute *attr;
14191 struct field *fp;
15d034d0 14192 const char *fieldname = "";
c906108c 14193
7d0ccb61
DJ
14194 if (die->tag == DW_TAG_inheritance)
14195 {
be2daae6
TT
14196 fip->baseclasses.emplace_back ();
14197 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14198 }
14199 else
14200 {
be2daae6
TT
14201 fip->fields.emplace_back ();
14202 new_field = &fip->fields.back ();
7d0ccb61 14203 }
be2daae6 14204
e142c38c 14205 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14206 if (attr != nullptr)
c906108c 14207 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14208 else
14209 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14210 if (new_field->accessibility != DW_ACCESS_public)
14211 fip->non_public_fields = 1;
60d5a603 14212
e142c38c 14213 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14214 if (attr != nullptr)
c906108c 14215 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14216 else
14217 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14218
14219 fp = &new_field->field;
a9a9bd0f 14220
e142c38c 14221 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14222 {
74ac6d43
TT
14223 LONGEST offset;
14224
a9a9bd0f 14225 /* Data member other than a C++ static data member. */
6e70227d 14226
c906108c 14227 /* Get type of field. */
e7c27a73 14228 fp->type = die_type (die, cu);
c906108c 14229
d6a843b5 14230 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14231
c906108c 14232 /* Get bit size of field (zero if none). */
e142c38c 14233 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14234 if (attr != nullptr)
c906108c
SS
14235 {
14236 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14237 }
14238 else
14239 {
14240 FIELD_BITSIZE (*fp) = 0;
14241 }
14242
14243 /* Get bit offset of field. */
74ac6d43
TT
14244 if (handle_data_member_location (die, cu, &offset))
14245 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14246 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14247 if (attr != nullptr)
c906108c 14248 {
d5a22e77 14249 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14250 {
14251 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14252 additional bit offset from the MSB of the containing
14253 anonymous object to the MSB of the field. We don't
14254 have to do anything special since we don't need to
14255 know the size of the anonymous object. */
f41f5e61 14256 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14257 }
14258 else
14259 {
14260 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14261 MSB of the anonymous object, subtract off the number of
14262 bits from the MSB of the field to the MSB of the
14263 object, and then subtract off the number of bits of
14264 the field itself. The result is the bit offset of
14265 the LSB of the field. */
c906108c
SS
14266 int anonymous_size;
14267 int bit_offset = DW_UNSND (attr);
14268
e142c38c 14269 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14270 if (attr != nullptr)
c906108c
SS
14271 {
14272 /* The size of the anonymous object containing
14273 the bit field is explicit, so use the
14274 indicated size (in bytes). */
14275 anonymous_size = DW_UNSND (attr);
14276 }
14277 else
14278 {
14279 /* The size of the anonymous object containing
14280 the bit field must be inferred from the type
14281 attribute of the data member containing the
14282 bit field. */
14283 anonymous_size = TYPE_LENGTH (fp->type);
14284 }
f41f5e61
PA
14285 SET_FIELD_BITPOS (*fp,
14286 (FIELD_BITPOS (*fp)
14287 + anonymous_size * bits_per_byte
14288 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14289 }
14290 }
da5b30da
AA
14291 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14292 if (attr != NULL)
14293 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14294 + attr->constant_value (0)));
c906108c
SS
14295
14296 /* Get name of field. */
39cbfefa
DJ
14297 fieldname = dwarf2_name (die, cu);
14298 if (fieldname == NULL)
14299 fieldname = "";
d8151005
DJ
14300
14301 /* The name is already allocated along with this objfile, so we don't
14302 need to duplicate it for the type. */
14303 fp->name = fieldname;
c906108c
SS
14304
14305 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14306 pointer or virtual base class pointer) to private. */
e142c38c 14307 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14308 {
d48cc9dd 14309 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14310 new_field->accessibility = DW_ACCESS_private;
14311 fip->non_public_fields = 1;
14312 }
14313 }
a9a9bd0f 14314 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14315 {
a9a9bd0f
DC
14316 /* C++ static member. */
14317
14318 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14319 is a declaration, but all versions of G++ as of this writing
14320 (so through at least 3.2.1) incorrectly generate
14321 DW_TAG_variable tags. */
6e70227d 14322
ff355380 14323 const char *physname;
c906108c 14324
a9a9bd0f 14325 /* Get name of field. */
39cbfefa
DJ
14326 fieldname = dwarf2_name (die, cu);
14327 if (fieldname == NULL)
c906108c
SS
14328 return;
14329
254e6b9e 14330 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14331 if (attr
14332 /* Only create a symbol if this is an external value.
14333 new_symbol checks this and puts the value in the global symbol
14334 table, which we want. If it is not external, new_symbol
14335 will try to put the value in cu->list_in_scope which is wrong. */
14336 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14337 {
14338 /* A static const member, not much different than an enum as far as
14339 we're concerned, except that we can support more types. */
14340 new_symbol (die, NULL, cu);
14341 }
14342
2df3850c 14343 /* Get physical name. */
ff355380 14344 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14345
d8151005
DJ
14346 /* The name is already allocated along with this objfile, so we don't
14347 need to duplicate it for the type. */
14348 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14349 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14350 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14351 }
14352 else if (die->tag == DW_TAG_inheritance)
14353 {
74ac6d43 14354 LONGEST offset;
d4b96c9a 14355
74ac6d43
TT
14356 /* C++ base class field. */
14357 if (handle_data_member_location (die, cu, &offset))
14358 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14359 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14360 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14361 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14362 }
2ddeaf8a
TT
14363 else if (die->tag == DW_TAG_variant_part)
14364 {
14365 /* process_structure_scope will treat this DIE as a union. */
14366 process_structure_scope (die, cu);
14367
14368 /* The variant part is relative to the start of the enclosing
14369 structure. */
14370 SET_FIELD_BITPOS (*fp, 0);
14371 fp->type = get_die_type (die, cu);
14372 fp->artificial = 1;
14373 fp->name = "<<variant>>";
c8c81635
TT
14374
14375 /* Normally a DW_TAG_variant_part won't have a size, but our
14376 representation requires one, so set it to the maximum of the
489dbda6
TT
14377 child sizes, being sure to account for the offset at which
14378 each child is seen. */
c8c81635
TT
14379 if (TYPE_LENGTH (fp->type) == 0)
14380 {
14381 unsigned max = 0;
14382 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14383 {
14384 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14385 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14386 if (len > max)
14387 max = len;
14388 }
c8c81635
TT
14389 TYPE_LENGTH (fp->type) = max;
14390 }
2ddeaf8a
TT
14391 }
14392 else
14393 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14394}
14395
883fd55a
KS
14396/* Can the type given by DIE define another type? */
14397
14398static bool
14399type_can_define_types (const struct die_info *die)
14400{
14401 switch (die->tag)
14402 {
14403 case DW_TAG_typedef:
14404 case DW_TAG_class_type:
14405 case DW_TAG_structure_type:
14406 case DW_TAG_union_type:
14407 case DW_TAG_enumeration_type:
14408 return true;
14409
14410 default:
14411 return false;
14412 }
14413}
14414
14415/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14416
14417static void
883fd55a
KS
14418dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14419 struct dwarf2_cu *cu)
6e70227d 14420{
be2daae6
TT
14421 struct decl_field fp;
14422 memset (&fp, 0, sizeof (fp));
98751a41 14423
883fd55a 14424 gdb_assert (type_can_define_types (die));
98751a41 14425
883fd55a 14426 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14427 fp.name = dwarf2_name (die, cu);
14428 fp.type = read_type_die (die, cu);
98751a41 14429
c191a687
KS
14430 /* Save accessibility. */
14431 enum dwarf_access_attribute accessibility;
14432 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14433 if (attr != NULL)
14434 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14435 else
14436 accessibility = dwarf2_default_access_attribute (die, cu);
14437 switch (accessibility)
14438 {
14439 case DW_ACCESS_public:
14440 /* The assumed value if neither private nor protected. */
14441 break;
14442 case DW_ACCESS_private:
be2daae6 14443 fp.is_private = 1;
c191a687
KS
14444 break;
14445 case DW_ACCESS_protected:
be2daae6 14446 fp.is_protected = 1;
c191a687
KS
14447 break;
14448 default:
b98664d3 14449 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14450 }
14451
883fd55a 14452 if (die->tag == DW_TAG_typedef)
be2daae6 14453 fip->typedef_field_list.push_back (fp);
883fd55a 14454 else
be2daae6 14455 fip->nested_types_list.push_back (fp);
98751a41
JK
14456}
14457
c906108c
SS
14458/* Create the vector of fields, and attach it to the type. */
14459
14460static void
fba45db2 14461dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14462 struct dwarf2_cu *cu)
c906108c 14463{
317f7127 14464 int nfields = fip->nfields ();
c906108c
SS
14465
14466 /* Record the field count, allocate space for the array of fields,
14467 and create blank accessibility bitfields if necessary. */
14468 TYPE_NFIELDS (type) = nfields;
14469 TYPE_FIELDS (type) = (struct field *)
be2daae6 14470 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14471
b4ba55a1 14472 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14473 {
14474 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14475
14476 TYPE_FIELD_PRIVATE_BITS (type) =
14477 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14478 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14479
14480 TYPE_FIELD_PROTECTED_BITS (type) =
14481 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14482 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14483
774b6a14
TT
14484 TYPE_FIELD_IGNORE_BITS (type) =
14485 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14486 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14487 }
14488
14489 /* If the type has baseclasses, allocate and clear a bit vector for
14490 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14491 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14492 {
be2daae6 14493 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14494 unsigned char *pointer;
c906108c
SS
14495
14496 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14497 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14498 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14499 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14500 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14501 }
14502
2ddeaf8a
TT
14503 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14504 {
14505 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14506
be2daae6 14507 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14508 {
be2daae6
TT
14509 struct nextfield &field = fip->fields[index];
14510
14511 if (field.variant.is_discriminant)
2ddeaf8a 14512 di->discriminant_index = index;
be2daae6 14513 else if (field.variant.default_branch)
2ddeaf8a
TT
14514 di->default_index = index;
14515 else
be2daae6 14516 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14517 }
14518 }
14519
be2daae6
TT
14520 /* Copy the saved-up fields into the field vector. */
14521 for (int i = 0; i < nfields; ++i)
c906108c 14522 {
be2daae6
TT
14523 struct nextfield &field
14524 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14525 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14526
be2daae6
TT
14527 TYPE_FIELD (type, i) = field.field;
14528 switch (field.accessibility)
c906108c 14529 {
c5aa993b 14530 case DW_ACCESS_private:
b4ba55a1 14531 if (cu->language != language_ada)
be2daae6 14532 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14533 break;
c906108c 14534
c5aa993b 14535 case DW_ACCESS_protected:
b4ba55a1 14536 if (cu->language != language_ada)
be2daae6 14537 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14538 break;
c906108c 14539
c5aa993b
JM
14540 case DW_ACCESS_public:
14541 break;
c906108c 14542
c5aa993b
JM
14543 default:
14544 /* Unknown accessibility. Complain and treat it as public. */
14545 {
b98664d3 14546 complaint (_("unsupported accessibility %d"),
be2daae6 14547 field.accessibility);
c5aa993b
JM
14548 }
14549 break;
c906108c 14550 }
be2daae6 14551 if (i < fip->baseclasses.size ())
c906108c 14552 {
be2daae6 14553 switch (field.virtuality)
c906108c 14554 {
c5aa993b
JM
14555 case DW_VIRTUALITY_virtual:
14556 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14557 if (cu->language == language_ada)
a73c6dcd 14558 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14559 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14560 break;
c906108c
SS
14561 }
14562 }
c906108c
SS
14563 }
14564}
14565
7d27a96d
TT
14566/* Return true if this member function is a constructor, false
14567 otherwise. */
14568
14569static int
14570dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14571{
14572 const char *fieldname;
fe978cb0 14573 const char *type_name;
7d27a96d
TT
14574 int len;
14575
14576 if (die->parent == NULL)
14577 return 0;
14578
14579 if (die->parent->tag != DW_TAG_structure_type
14580 && die->parent->tag != DW_TAG_union_type
14581 && die->parent->tag != DW_TAG_class_type)
14582 return 0;
14583
14584 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14585 type_name = dwarf2_name (die->parent, cu);
14586 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14587 return 0;
14588
14589 len = strlen (fieldname);
fe978cb0
PA
14590 return (strncmp (fieldname, type_name, len) == 0
14591 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14592}
14593
e35000a7
TBA
14594/* Check if the given VALUE is a recognized enum
14595 dwarf_defaulted_attribute constant according to DWARF5 spec,
14596 Table 7.24. */
14597
14598static bool
14599is_valid_DW_AT_defaulted (ULONGEST value)
14600{
14601 switch (value)
14602 {
14603 case DW_DEFAULTED_no:
14604 case DW_DEFAULTED_in_class:
14605 case DW_DEFAULTED_out_of_class:
14606 return true;
14607 }
14608
3142e908 14609 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14610 return false;
14611}
14612
c906108c
SS
14613/* Add a member function to the proper fieldlist. */
14614
14615static void
107d2387 14616dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14617 struct type *type, struct dwarf2_cu *cu)
c906108c 14618{
518817b3 14619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14620 struct attribute *attr;
c906108c 14621 int i;
be2daae6 14622 struct fnfieldlist *flp = nullptr;
c906108c 14623 struct fn_field *fnp;
15d034d0 14624 const char *fieldname;
f792889a 14625 struct type *this_type;
60d5a603 14626 enum dwarf_access_attribute accessibility;
c906108c 14627
b4ba55a1 14628 if (cu->language == language_ada)
a73c6dcd 14629 error (_("unexpected member function in Ada type"));
b4ba55a1 14630
2df3850c 14631 /* Get name of member function. */
39cbfefa
DJ
14632 fieldname = dwarf2_name (die, cu);
14633 if (fieldname == NULL)
2df3850c 14634 return;
c906108c 14635
c906108c 14636 /* Look up member function name in fieldlist. */
be2daae6 14637 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14638 {
27bfe10e 14639 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14640 {
14641 flp = &fip->fnfieldlists[i];
14642 break;
14643 }
c906108c
SS
14644 }
14645
be2daae6
TT
14646 /* Create a new fnfieldlist if necessary. */
14647 if (flp == nullptr)
c906108c 14648 {
be2daae6
TT
14649 fip->fnfieldlists.emplace_back ();
14650 flp = &fip->fnfieldlists.back ();
c906108c 14651 flp->name = fieldname;
be2daae6 14652 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14653 }
14654
be2daae6
TT
14655 /* Create a new member function field and add it to the vector of
14656 fnfieldlists. */
14657 flp->fnfields.emplace_back ();
14658 fnp = &flp->fnfields.back ();
3da10d80
KS
14659
14660 /* Delay processing of the physname until later. */
9c37b5ae 14661 if (cu->language == language_cplus)
be2daae6
TT
14662 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14663 die, cu);
3da10d80
KS
14664 else
14665 {
1d06ead6 14666 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14667 fnp->physname = physname ? physname : "";
14668 }
14669
c906108c 14670 fnp->type = alloc_type (objfile);
f792889a
DJ
14671 this_type = read_type_die (die, cu);
14672 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14673 {
f792889a 14674 int nparams = TYPE_NFIELDS (this_type);
c906108c 14675
f792889a 14676 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14677 of the method itself (TYPE_CODE_METHOD). */
14678 smash_to_method_type (fnp->type, type,
f792889a
DJ
14679 TYPE_TARGET_TYPE (this_type),
14680 TYPE_FIELDS (this_type),
14681 TYPE_NFIELDS (this_type),
14682 TYPE_VARARGS (this_type));
c906108c
SS
14683
14684 /* Handle static member functions.
c5aa993b 14685 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14686 member functions. G++ helps GDB by marking the first
14687 parameter for non-static member functions (which is the this
14688 pointer) as artificial. We obtain this information from
14689 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14690 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14691 fnp->voffset = VOFFSET_STATIC;
14692 }
14693 else
b98664d3 14694 complaint (_("member function type missing for '%s'"),
3da10d80 14695 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14696
14697 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14698 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14699 fnp->fcontext = die_containing_type (die, cu);
c906108c 14700
3e43a32a
MS
14701 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14702 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14703
14704 /* Get accessibility. */
e142c38c 14705 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14706 if (attr != nullptr)
aead7601 14707 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14708 else
14709 accessibility = dwarf2_default_access_attribute (die, cu);
14710 switch (accessibility)
c906108c 14711 {
60d5a603
JK
14712 case DW_ACCESS_private:
14713 fnp->is_private = 1;
14714 break;
14715 case DW_ACCESS_protected:
14716 fnp->is_protected = 1;
14717 break;
c906108c
SS
14718 }
14719
b02dede2 14720 /* Check for artificial methods. */
e142c38c 14721 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14722 if (attr && DW_UNSND (attr) != 0)
14723 fnp->is_artificial = 1;
14724
e35000a7
TBA
14725 /* Check for defaulted methods. */
14726 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14727 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14728 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14729
14730 /* Check for deleted methods. */
14731 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14732 if (attr != nullptr && DW_UNSND (attr) != 0)
14733 fnp->is_deleted = 1;
14734
7d27a96d
TT
14735 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14736
0d564a31 14737 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14738 function. For older versions of GCC, this is an offset in the
14739 appropriate virtual table, as specified by DW_AT_containing_type.
14740 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14741 to the object address. */
14742
e142c38c 14743 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14744 if (attr != nullptr)
8e19ed76 14745 {
4fc6c0d5 14746 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14747 {
aec5aa8b
TT
14748 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14749 {
14750 /* Old-style GCC. */
14751 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14752 }
14753 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14754 || (DW_BLOCK (attr)->size > 1
14755 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14756 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14757 {
aec5aa8b
TT
14758 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14759 if ((fnp->voffset % cu->header.addr_size) != 0)
14760 dwarf2_complex_location_expr_complaint ();
14761 else
14762 fnp->voffset /= cu->header.addr_size;
14763 fnp->voffset += 2;
14764 }
14765 else
14766 dwarf2_complex_location_expr_complaint ();
14767
14768 if (!fnp->fcontext)
7e993ebf
KS
14769 {
14770 /* If there is no `this' field and no DW_AT_containing_type,
14771 we cannot actually find a base class context for the
14772 vtable! */
14773 if (TYPE_NFIELDS (this_type) == 0
14774 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14775 {
b98664d3 14776 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14777 "function \"%s\" (offset %s)"),
14778 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14779 }
14780 else
14781 {
14782 fnp->fcontext
14783 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14784 }
14785 }
aec5aa8b 14786 }
cd6c91b4 14787 else if (attr->form_is_section_offset ())
8e19ed76 14788 {
4d3c2250 14789 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14790 }
14791 else
14792 {
4d3c2250
KB
14793 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14794 fieldname);
8e19ed76 14795 }
0d564a31 14796 }
d48cc9dd
DJ
14797 else
14798 {
14799 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14800 if (attr && DW_UNSND (attr))
14801 {
14802 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14803 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14804 "but the vtable offset is not specified"),
9d8780f0 14805 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14806 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14807 TYPE_CPLUS_DYNAMIC (type) = 1;
14808 }
14809 }
c906108c
SS
14810}
14811
14812/* Create the vector of member function fields, and attach it to the type. */
14813
14814static void
fba45db2 14815dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14816 struct dwarf2_cu *cu)
c906108c 14817{
b4ba55a1 14818 if (cu->language == language_ada)
a73c6dcd 14819 error (_("unexpected member functions in Ada type"));
b4ba55a1 14820
c906108c
SS
14821 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14822 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14823 TYPE_ALLOC (type,
14824 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14825
be2daae6 14826 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14827 {
be2daae6 14828 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14829 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14830
be2daae6
TT
14831 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14832 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14833 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14834 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14835
14836 for (int k = 0; k < nf.fnfields.size (); ++k)
14837 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14838 }
14839
be2daae6 14840 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14841}
14842
1168df01
JB
14843/* Returns non-zero if NAME is the name of a vtable member in CU's
14844 language, zero otherwise. */
14845static int
14846is_vtable_name (const char *name, struct dwarf2_cu *cu)
14847{
14848 static const char vptr[] = "_vptr";
14849
9c37b5ae
TT
14850 /* Look for the C++ form of the vtable. */
14851 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14852 return 1;
14853
14854 return 0;
14855}
14856
c0dd20ea 14857/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14858 functions, with the ABI-specified layout. If TYPE describes
14859 such a structure, smash it into a member function type.
61049d3b
DJ
14860
14861 GCC shouldn't do this; it should just output pointer to member DIEs.
14862 This is GCC PR debug/28767. */
c0dd20ea 14863
0b92b5bb
TT
14864static void
14865quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14866{
09e2d7c7 14867 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14868
14869 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14870 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14871 return;
c0dd20ea
DJ
14872
14873 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14874 if (TYPE_FIELD_NAME (type, 0) == NULL
14875 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14876 || TYPE_FIELD_NAME (type, 1) == NULL
14877 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14878 return;
c0dd20ea
DJ
14879
14880 /* Find the type of the method. */
0b92b5bb 14881 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14882 if (pfn_type == NULL
14883 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14884 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14885 return;
c0dd20ea
DJ
14886
14887 /* Look for the "this" argument. */
14888 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14889 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14890 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14891 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14892 return;
c0dd20ea 14893
09e2d7c7 14894 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14895 new_type = alloc_type (objfile);
09e2d7c7 14896 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14897 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14898 TYPE_VARARGS (pfn_type));
0b92b5bb 14899 smash_to_methodptr_type (type, new_type);
c0dd20ea 14900}
1168df01 14901
2b4424c3
TT
14902/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14903 appropriate error checking and issuing complaints if there is a
14904 problem. */
14905
14906static ULONGEST
14907get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14908{
14909 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14910
14911 if (attr == nullptr)
14912 return 0;
14913
cd6c91b4 14914 if (!attr->form_is_constant ())
2b4424c3 14915 {
b98664d3 14916 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14917 " - DIE at %s [in module %s]"),
14918 sect_offset_str (die->sect_off),
14919 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14920 return 0;
14921 }
14922
14923 ULONGEST align;
14924 if (attr->form == DW_FORM_sdata)
14925 {
14926 LONGEST val = DW_SND (attr);
14927 if (val < 0)
14928 {
b98664d3 14929 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14930 " - DIE at %s [in module %s]"),
14931 sect_offset_str (die->sect_off),
14932 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14933 return 0;
14934 }
14935 align = val;
14936 }
14937 else
14938 align = DW_UNSND (attr);
14939
14940 if (align == 0)
14941 {
b98664d3 14942 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14943 " - DIE at %s [in module %s]"),
14944 sect_offset_str (die->sect_off),
14945 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14946 return 0;
14947 }
14948 if ((align & (align - 1)) != 0)
14949 {
b98664d3 14950 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14951 " - DIE at %s [in module %s]"),
14952 sect_offset_str (die->sect_off),
14953 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14954 return 0;
14955 }
14956
14957 return align;
14958}
14959
14960/* If the DIE has a DW_AT_alignment attribute, use its value to set
14961 the alignment for TYPE. */
14962
14963static void
14964maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14965 struct type *type)
14966{
14967 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14968 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14969 " - DIE at %s [in module %s]"),
14970 sect_offset_str (die->sect_off),
14971 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14972}
685b1105 14973
e35000a7
TBA
14974/* Check if the given VALUE is a valid enum dwarf_calling_convention
14975 constant for a type, according to DWARF5 spec, Table 5.5. */
14976
14977static bool
14978is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14979{
14980 switch (value)
14981 {
14982 case DW_CC_normal:
14983 case DW_CC_pass_by_reference:
14984 case DW_CC_pass_by_value:
14985 return true;
14986
14987 default:
14988 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14989 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14990 return false;
14991 }
14992}
14993
d0922fcf
TBA
14994/* Check if the given VALUE is a valid enum dwarf_calling_convention
14995 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14996 also according to GNU-specific values (see include/dwarf2.h). */
14997
14998static bool
14999is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15000{
15001 switch (value)
15002 {
15003 case DW_CC_normal:
15004 case DW_CC_program:
15005 case DW_CC_nocall:
15006 return true;
15007
15008 case DW_CC_GNU_renesas_sh:
15009 case DW_CC_GNU_borland_fastcall_i386:
15010 case DW_CC_GDB_IBM_OpenCL:
15011 return true;
15012
15013 default:
15014 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15015 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15016 return false;
15017 }
15018}
15019
c906108c 15020/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15021 (definition) to create a type for the structure or union. Fill in
15022 the type's name and general properties; the members will not be
83655187
DE
15023 processed until process_structure_scope. A symbol table entry for
15024 the type will also not be done until process_structure_scope (assuming
15025 the type has a name).
c906108c 15026
c767944b
DJ
15027 NOTE: we need to call these functions regardless of whether or not the
15028 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15029 structure or union. This gets the type entered into our set of
83655187 15030 user defined types. */
c906108c 15031
f792889a 15032static struct type *
134d01f1 15033read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15034{
518817b3 15035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15036 struct type *type;
15037 struct attribute *attr;
15d034d0 15038 const char *name;
c906108c 15039
348e048f
DE
15040 /* If the definition of this type lives in .debug_types, read that type.
15041 Don't follow DW_AT_specification though, that will take us back up
15042 the chain and we want to go down. */
052c8bb8 15043 attr = die->attr (DW_AT_signature);
435d3d88 15044 if (attr != nullptr)
348e048f 15045 {
ac9ec31b 15046 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15047
ac9ec31b 15048 /* The type's CU may not be the same as CU.
02142a6c 15049 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15050 return set_die_type (die, type, cu);
15051 }
15052
c0dd20ea 15053 type = alloc_type (objfile);
c906108c 15054 INIT_CPLUS_SPECIFIC (type);
93311388 15055
39cbfefa
DJ
15056 name = dwarf2_name (die, cu);
15057 if (name != NULL)
c906108c 15058 {
987504bb 15059 if (cu->language == language_cplus
c44af4eb
TT
15060 || cu->language == language_d
15061 || cu->language == language_rust)
63d06c5c 15062 {
15d034d0 15063 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15064
15065 /* dwarf2_full_name might have already finished building the DIE's
15066 type. If so, there is no need to continue. */
15067 if (get_die_type (die, cu) != NULL)
15068 return get_die_type (die, cu);
15069
e86ca25f 15070 TYPE_NAME (type) = full_name;
63d06c5c
DC
15071 }
15072 else
15073 {
d8151005
DJ
15074 /* The name is already allocated along with this objfile, so
15075 we don't need to duplicate it for the type. */
e86ca25f 15076 TYPE_NAME (type) = name;
63d06c5c 15077 }
c906108c
SS
15078 }
15079
15080 if (die->tag == DW_TAG_structure_type)
15081 {
15082 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15083 }
15084 else if (die->tag == DW_TAG_union_type)
15085 {
15086 TYPE_CODE (type) = TYPE_CODE_UNION;
15087 }
2ddeaf8a
TT
15088 else if (die->tag == DW_TAG_variant_part)
15089 {
15090 TYPE_CODE (type) = TYPE_CODE_UNION;
15091 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15092 }
c906108c
SS
15093 else
15094 {
4753d33b 15095 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15096 }
15097
0cc2414c
TT
15098 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15099 TYPE_DECLARED_CLASS (type) = 1;
15100
e35000a7
TBA
15101 /* Store the calling convention in the type if it's available in
15102 the die. Otherwise the calling convention remains set to
15103 the default value DW_CC_normal. */
15104 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15105 if (attr != nullptr
15106 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15107 {
15108 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15109 TYPE_CPLUS_CALLING_CONVENTION (type)
15110 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15111 }
15112
e142c38c 15113 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15114 if (attr != nullptr)
c906108c 15115 {
cd6c91b4 15116 if (attr->form_is_constant ())
155bfbd3
JB
15117 TYPE_LENGTH (type) = DW_UNSND (attr);
15118 else
15119 {
15120 /* For the moment, dynamic type sizes are not supported
15121 by GDB's struct type. The actual size is determined
15122 on-demand when resolving the type of a given object,
15123 so set the type's length to zero for now. Otherwise,
15124 we record an expression as the length, and that expression
15125 could lead to a very large value, which could eventually
15126 lead to us trying to allocate that much memory when creating
15127 a value of that type. */
15128 TYPE_LENGTH (type) = 0;
15129 }
c906108c
SS
15130 }
15131 else
15132 {
15133 TYPE_LENGTH (type) = 0;
15134 }
15135
2b4424c3
TT
15136 maybe_set_alignment (cu, die, type);
15137
5230b05a 15138 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15139 {
5230b05a
WT
15140 /* ICC<14 does not output the required DW_AT_declaration on
15141 incomplete types, but gives them a size of zero. */
422b1cb0 15142 TYPE_STUB (type) = 1;
685b1105
JK
15143 }
15144 else
15145 TYPE_STUB_SUPPORTED (type) = 1;
15146
dc718098 15147 if (die_is_declaration (die, cu))
876cecd0 15148 TYPE_STUB (type) = 1;
a6c727b2
DJ
15149 else if (attr == NULL && die->child == NULL
15150 && producer_is_realview (cu->producer))
15151 /* RealView does not output the required DW_AT_declaration
15152 on incomplete types. */
15153 TYPE_STUB (type) = 1;
dc718098 15154
c906108c
SS
15155 /* We need to add the type field to the die immediately so we don't
15156 infinitely recurse when dealing with pointers to the structure
0963b4bd 15157 type within the structure itself. */
1c379e20 15158 set_die_type (die, type, cu);
c906108c 15159
7e314c57
JK
15160 /* set_die_type should be already done. */
15161 set_descriptive_type (type, die, cu);
15162
c767944b
DJ
15163 return type;
15164}
15165
2ddeaf8a
TT
15166/* A helper for process_structure_scope that handles a single member
15167 DIE. */
15168
15169static void
15170handle_struct_member_die (struct die_info *child_die, struct type *type,
15171 struct field_info *fi,
15172 std::vector<struct symbol *> *template_args,
15173 struct dwarf2_cu *cu)
15174{
15175 if (child_die->tag == DW_TAG_member
15176 || child_die->tag == DW_TAG_variable
15177 || child_die->tag == DW_TAG_variant_part)
15178 {
15179 /* NOTE: carlton/2002-11-05: A C++ static data member
15180 should be a DW_TAG_member that is a declaration, but
15181 all versions of G++ as of this writing (so through at
15182 least 3.2.1) incorrectly generate DW_TAG_variable
15183 tags for them instead. */
15184 dwarf2_add_field (fi, child_die, cu);
15185 }
15186 else if (child_die->tag == DW_TAG_subprogram)
15187 {
15188 /* Rust doesn't have member functions in the C++ sense.
15189 However, it does emit ordinary functions as children
15190 of a struct DIE. */
15191 if (cu->language == language_rust)
15192 read_func_scope (child_die, cu);
15193 else
15194 {
15195 /* C++ member function. */
15196 dwarf2_add_member_fn (fi, child_die, type, cu);
15197 }
15198 }
15199 else if (child_die->tag == DW_TAG_inheritance)
15200 {
15201 /* C++ base class field. */
15202 dwarf2_add_field (fi, child_die, cu);
15203 }
15204 else if (type_can_define_types (child_die))
15205 dwarf2_add_type_defn (fi, child_die, cu);
15206 else if (child_die->tag == DW_TAG_template_type_param
15207 || child_die->tag == DW_TAG_template_value_param)
15208 {
15209 struct symbol *arg = new_symbol (child_die, NULL, cu);
15210
15211 if (arg != NULL)
15212 template_args->push_back (arg);
15213 }
15214 else if (child_die->tag == DW_TAG_variant)
15215 {
15216 /* In a variant we want to get the discriminant and also add a
15217 field for our sole member child. */
15218 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15219
bde09ab7 15220 for (die_info *variant_child = child_die->child;
2ddeaf8a 15221 variant_child != NULL;
436c571c 15222 variant_child = variant_child->sibling)
2ddeaf8a
TT
15223 {
15224 if (variant_child->tag == DW_TAG_member)
15225 {
15226 handle_struct_member_die (variant_child, type, fi,
15227 template_args, cu);
15228 /* Only handle the one. */
15229 break;
15230 }
15231 }
15232
15233 /* We don't handle this but we might as well report it if we see
15234 it. */
15235 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15236 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15237 " - DIE at %s [in module %s]"),
15238 sect_offset_str (child_die->sect_off),
15239 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15240
15241 /* The first field was just added, so we can stash the
15242 discriminant there. */
be2daae6 15243 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15244 if (discr == NULL)
be2daae6 15245 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15246 else
be2daae6 15247 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15248 }
15249}
15250
c767944b
DJ
15251/* Finish creating a structure or union type, including filling in
15252 its members and creating a symbol for it. */
15253
15254static void
15255process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15256{
518817b3 15257 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15258 struct die_info *child_die;
c767944b
DJ
15259 struct type *type;
15260
15261 type = get_die_type (die, cu);
15262 if (type == NULL)
15263 type = read_structure_type (die, cu);
15264
2ddeaf8a
TT
15265 /* When reading a DW_TAG_variant_part, we need to notice when we
15266 read the discriminant member, so we can record it later in the
15267 discriminant_info. */
15268 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15269 sect_offset discr_offset {};
3e1d3d8c 15270 bool has_template_parameters = false;
2ddeaf8a
TT
15271
15272 if (is_variant_part)
15273 {
15274 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15275 if (discr == NULL)
15276 {
15277 /* Maybe it's a univariant form, an extension we support.
15278 In this case arrange not to check the offset. */
15279 is_variant_part = false;
15280 }
cd6c91b4 15281 else if (discr->form_is_ref ())
2ddeaf8a
TT
15282 {
15283 struct dwarf2_cu *target_cu = cu;
15284 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15285
15286 discr_offset = target_die->sect_off;
15287 }
15288 else
15289 {
b98664d3 15290 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15291 " - DIE at %s [in module %s]"),
15292 sect_offset_str (die->sect_off),
15293 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15294 is_variant_part = false;
15295 }
15296 }
15297
e142c38c 15298 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15299 {
15300 struct field_info fi;
2f4732b0 15301 std::vector<struct symbol *> template_args;
c906108c 15302
639d11d3 15303 child_die = die->child;
c906108c
SS
15304
15305 while (child_die && child_die->tag)
15306 {
2ddeaf8a 15307 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15308
2ddeaf8a 15309 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15310 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15311
436c571c 15312 child_die = child_die->sibling;
c906108c
SS
15313 }
15314
34eaf542 15315 /* Attach template arguments to type. */
2f4732b0 15316 if (!template_args.empty ())
34eaf542 15317 {
3e1d3d8c 15318 has_template_parameters = true;
34eaf542 15319 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15320 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15321 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15322 = XOBNEWVEC (&objfile->objfile_obstack,
15323 struct symbol *,
15324 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15325 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15326 template_args.data (),
34eaf542
TT
15327 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15328 * sizeof (struct symbol *)));
34eaf542
TT
15329 }
15330
c906108c 15331 /* Attach fields and member functions to the type. */
317f7127 15332 if (fi.nfields () > 0)
e7c27a73 15333 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15334 if (!fi.fnfieldlists.empty ())
c906108c 15335 {
e7c27a73 15336 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15337
c5aa993b 15338 /* Get the type which refers to the base class (possibly this
c906108c 15339 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15340 class from the DW_AT_containing_type attribute. This use of
15341 DW_AT_containing_type is a GNU extension. */
c906108c 15342
e142c38c 15343 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15344 {
e7c27a73 15345 struct type *t = die_containing_type (die, cu);
c906108c 15346
ae6ae975 15347 set_type_vptr_basetype (type, t);
c906108c
SS
15348 if (type == t)
15349 {
c906108c
SS
15350 int i;
15351
15352 /* Our own class provides vtbl ptr. */
15353 for (i = TYPE_NFIELDS (t) - 1;
15354 i >= TYPE_N_BASECLASSES (t);
15355 --i)
15356 {
0d5cff50 15357 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15358
1168df01 15359 if (is_vtable_name (fieldname, cu))
c906108c 15360 {
ae6ae975 15361 set_type_vptr_fieldno (type, i);
c906108c
SS
15362 break;
15363 }
15364 }
15365
15366 /* Complain if virtual function table field not found. */
15367 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15368 complaint (_("virtual function table pointer "
3e43a32a 15369 "not found when defining class '%s'"),
e86ca25f 15370 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15371 }
15372 else
15373 {
ae6ae975 15374 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15375 }
15376 }
f6235d4c 15377 else if (cu->producer
61012eef 15378 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15379 {
15380 /* The IBM XLC compiler does not provide direct indication
15381 of the containing type, but the vtable pointer is
15382 always named __vfp. */
15383
15384 int i;
15385
15386 for (i = TYPE_NFIELDS (type) - 1;
15387 i >= TYPE_N_BASECLASSES (type);
15388 --i)
15389 {
15390 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15391 {
ae6ae975
DE
15392 set_type_vptr_fieldno (type, i);
15393 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15394 break;
15395 }
15396 }
15397 }
c906108c 15398 }
98751a41
JK
15399
15400 /* Copy fi.typedef_field_list linked list elements content into the
15401 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15402 if (!fi.typedef_field_list.empty ())
98751a41 15403 {
be2daae6 15404 int count = fi.typedef_field_list.size ();
98751a41 15405
a0d7a4ff 15406 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15407 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15408 = ((struct decl_field *)
be2daae6
TT
15409 TYPE_ALLOC (type,
15410 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15411 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15412
be2daae6
TT
15413 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15414 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15415 }
c767944b 15416
883fd55a
KS
15417 /* Copy fi.nested_types_list linked list elements content into the
15418 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15419 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15420 {
be2daae6 15421 int count = fi.nested_types_list.size ();
883fd55a
KS
15422
15423 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15424 TYPE_NESTED_TYPES_ARRAY (type)
15425 = ((struct decl_field *)
be2daae6
TT
15426 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15427 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15428
be2daae6
TT
15429 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15430 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15431 }
c906108c 15432 }
63d06c5c 15433
bb5ed363 15434 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15435 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15436 cu->rust_unions.push_back (type);
0b92b5bb 15437
90aeadfc
DC
15438 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15439 snapshots) has been known to create a die giving a declaration
15440 for a class that has, as a child, a die giving a definition for a
15441 nested class. So we have to process our children even if the
15442 current die is a declaration. Normally, of course, a declaration
15443 won't have any children at all. */
134d01f1 15444
ca040673
DE
15445 child_die = die->child;
15446
90aeadfc
DC
15447 while (child_die != NULL && child_die->tag)
15448 {
15449 if (child_die->tag == DW_TAG_member
15450 || child_die->tag == DW_TAG_variable
34eaf542
TT
15451 || child_die->tag == DW_TAG_inheritance
15452 || child_die->tag == DW_TAG_template_value_param
15453 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15454 {
90aeadfc 15455 /* Do nothing. */
134d01f1 15456 }
90aeadfc
DC
15457 else
15458 process_die (child_die, cu);
134d01f1 15459
436c571c 15460 child_die = child_die->sibling;
134d01f1
DJ
15461 }
15462
fa4028e9
JB
15463 /* Do not consider external references. According to the DWARF standard,
15464 these DIEs are identified by the fact that they have no byte_size
15465 attribute, and a declaration attribute. */
15466 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15467 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15468 {
15469 struct symbol *sym = new_symbol (die, type, cu);
15470
15471 if (has_template_parameters)
15472 {
a776957c
TT
15473 struct symtab *symtab;
15474 if (sym != nullptr)
15475 symtab = symbol_symtab (sym);
15476 else if (cu->line_header != nullptr)
15477 {
15478 /* Any related symtab will do. */
15479 symtab
7ba99d21 15480 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15481 }
15482 else
15483 {
15484 symtab = nullptr;
15485 complaint (_("could not find suitable "
15486 "symtab for template parameter"
15487 " - DIE at %s [in module %s]"),
15488 sect_offset_str (die->sect_off),
15489 objfile_name (objfile));
15490 }
15491
15492 if (symtab != nullptr)
15493 {
15494 /* Make sure that the symtab is set on the new symbols.
15495 Even though they don't appear in this symtab directly,
15496 other parts of gdb assume that symbols do, and this is
15497 reasonably true. */
15498 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15499 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15500 }
3e1d3d8c
TT
15501 }
15502 }
134d01f1
DJ
15503}
15504
55426c9d
JB
15505/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15506 update TYPE using some information only available in DIE's children. */
15507
15508static void
15509update_enumeration_type_from_children (struct die_info *die,
15510 struct type *type,
15511 struct dwarf2_cu *cu)
15512{
60f7655a 15513 struct die_info *child_die;
55426c9d
JB
15514 int unsigned_enum = 1;
15515 int flag_enum = 1;
55426c9d 15516
8268c778 15517 auto_obstack obstack;
55426c9d 15518
60f7655a
DE
15519 for (child_die = die->child;
15520 child_die != NULL && child_die->tag;
436c571c 15521 child_die = child_die->sibling)
55426c9d
JB
15522 {
15523 struct attribute *attr;
15524 LONGEST value;
15525 const gdb_byte *bytes;
15526 struct dwarf2_locexpr_baton *baton;
15527 const char *name;
60f7655a 15528
55426c9d
JB
15529 if (child_die->tag != DW_TAG_enumerator)
15530 continue;
15531
15532 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15533 if (attr == NULL)
15534 continue;
15535
15536 name = dwarf2_name (child_die, cu);
15537 if (name == NULL)
15538 name = "<anonymous enumerator>";
15539
15540 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15541 &value, &bytes, &baton);
15542 if (value < 0)
15543 {
15544 unsigned_enum = 0;
15545 flag_enum = 0;
15546 }
55426c9d 15547 else
edd45eb0
SM
15548 {
15549 if (count_one_bits_ll (value) >= 2)
15550 flag_enum = 0;
edd45eb0 15551 }
55426c9d
JB
15552
15553 /* If we already know that the enum type is neither unsigned, nor
15554 a flag type, no need to look at the rest of the enumerates. */
15555 if (!unsigned_enum && !flag_enum)
15556 break;
55426c9d
JB
15557 }
15558
15559 if (unsigned_enum)
15560 TYPE_UNSIGNED (type) = 1;
15561 if (flag_enum)
15562 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15563}
15564
134d01f1
DJ
15565/* Given a DW_AT_enumeration_type die, set its type. We do not
15566 complete the type's fields yet, or create any symbols. */
c906108c 15567
f792889a 15568static struct type *
134d01f1 15569read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15570{
518817b3 15571 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15572 struct type *type;
c906108c 15573 struct attribute *attr;
0114d602 15574 const char *name;
134d01f1 15575
348e048f
DE
15576 /* If the definition of this type lives in .debug_types, read that type.
15577 Don't follow DW_AT_specification though, that will take us back up
15578 the chain and we want to go down. */
052c8bb8 15579 attr = die->attr (DW_AT_signature);
435d3d88 15580 if (attr != nullptr)
348e048f 15581 {
ac9ec31b 15582 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15583
ac9ec31b 15584 /* The type's CU may not be the same as CU.
02142a6c 15585 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15586 return set_die_type (die, type, cu);
15587 }
15588
c906108c
SS
15589 type = alloc_type (objfile);
15590
15591 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15592 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15593 if (name != NULL)
e86ca25f 15594 TYPE_NAME (type) = name;
c906108c 15595
0626fc76
TT
15596 attr = dwarf2_attr (die, DW_AT_type, cu);
15597 if (attr != NULL)
15598 {
15599 struct type *underlying_type = die_type (die, cu);
15600
15601 TYPE_TARGET_TYPE (type) = underlying_type;
15602 }
15603
e142c38c 15604 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15605 if (attr != nullptr)
c906108c
SS
15606 {
15607 TYPE_LENGTH (type) = DW_UNSND (attr);
15608 }
15609 else
15610 {
15611 TYPE_LENGTH (type) = 0;
15612 }
15613
2b4424c3
TT
15614 maybe_set_alignment (cu, die, type);
15615
137033e9
JB
15616 /* The enumeration DIE can be incomplete. In Ada, any type can be
15617 declared as private in the package spec, and then defined only
15618 inside the package body. Such types are known as Taft Amendment
15619 Types. When another package uses such a type, an incomplete DIE
15620 may be generated by the compiler. */
02eb380e 15621 if (die_is_declaration (die, cu))
876cecd0 15622 TYPE_STUB (type) = 1;
02eb380e 15623
0626fc76
TT
15624 /* Finish the creation of this type by using the enum's children.
15625 We must call this even when the underlying type has been provided
15626 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15627 update_enumeration_type_from_children (die, type, cu);
15628
0626fc76
TT
15629 /* If this type has an underlying type that is not a stub, then we
15630 may use its attributes. We always use the "unsigned" attribute
15631 in this situation, because ordinarily we guess whether the type
15632 is unsigned -- but the guess can be wrong and the underlying type
15633 can tell us the reality. However, we defer to a local size
15634 attribute if one exists, because this lets the compiler override
15635 the underlying type if needed. */
15636 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15637 {
9e7c9a03
HD
15638 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15639 underlying_type = check_typedef (underlying_type);
15640 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 15641 if (TYPE_LENGTH (type) == 0)
9e7c9a03 15642 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 15643 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
15644 && TYPE_RAW_ALIGN (underlying_type) != 0)
15645 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
15646 }
15647
3d567982
TT
15648 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15649
f792889a 15650 return set_die_type (die, type, cu);
134d01f1
DJ
15651}
15652
15653/* Given a pointer to a die which begins an enumeration, process all
15654 the dies that define the members of the enumeration, and create the
15655 symbol for the enumeration type.
15656
15657 NOTE: We reverse the order of the element list. */
15658
15659static void
15660process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15661{
f792889a 15662 struct type *this_type;
134d01f1 15663
f792889a
DJ
15664 this_type = get_die_type (die, cu);
15665 if (this_type == NULL)
15666 this_type = read_enumeration_type (die, cu);
9dc481d3 15667
639d11d3 15668 if (die->child != NULL)
c906108c 15669 {
9dc481d3
DE
15670 struct die_info *child_die;
15671 struct symbol *sym;
43816ebc 15672 std::vector<struct field> fields;
15d034d0 15673 const char *name;
9dc481d3 15674
639d11d3 15675 child_die = die->child;
c906108c
SS
15676 while (child_die && child_die->tag)
15677 {
15678 if (child_die->tag != DW_TAG_enumerator)
15679 {
e7c27a73 15680 process_die (child_die, cu);
c906108c
SS
15681 }
15682 else
15683 {
39cbfefa
DJ
15684 name = dwarf2_name (child_die, cu);
15685 if (name)
c906108c 15686 {
f792889a 15687 sym = new_symbol (child_die, this_type, cu);
c906108c 15688
43816ebc
TT
15689 fields.emplace_back ();
15690 struct field &field = fields.back ();
c906108c 15691
43816ebc
TT
15692 FIELD_NAME (field) = sym->linkage_name ();
15693 FIELD_TYPE (field) = NULL;
15694 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15695 FIELD_BITSIZE (field) = 0;
c906108c
SS
15696 }
15697 }
15698
436c571c 15699 child_die = child_die->sibling;
c906108c
SS
15700 }
15701
43816ebc 15702 if (!fields.empty ())
c906108c 15703 {
43816ebc 15704 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15705 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15706 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15707 memcpy (TYPE_FIELDS (this_type), fields.data (),
15708 sizeof (struct field) * fields.size ());
c906108c 15709 }
c906108c 15710 }
134d01f1 15711
6c83ed52
TT
15712 /* If we are reading an enum from a .debug_types unit, and the enum
15713 is a declaration, and the enum is not the signatured type in the
15714 unit, then we do not want to add a symbol for it. Adding a
15715 symbol would in some cases obscure the true definition of the
15716 enum, giving users an incomplete type when the definition is
15717 actually available. Note that we do not want to do this for all
15718 enums which are just declarations, because C++0x allows forward
15719 enum declarations. */
3019eac3 15720 if (cu->per_cu->is_debug_types
6c83ed52
TT
15721 && die_is_declaration (die, cu))
15722 {
52dc124a 15723 struct signatured_type *sig_type;
6c83ed52 15724
c0f78cd4 15725 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15726 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15727 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15728 return;
15729 }
15730
f792889a 15731 new_symbol (die, this_type, cu);
c906108c
SS
15732}
15733
15734/* Extract all information from a DW_TAG_array_type DIE and put it in
15735 the DIE's type field. For now, this only handles one dimensional
15736 arrays. */
15737
f792889a 15738static struct type *
e7c27a73 15739read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15740{
518817b3 15741 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15742 struct die_info *child_die;
7e314c57 15743 struct type *type;
c906108c 15744 struct type *element_type, *range_type, *index_type;
c906108c 15745 struct attribute *attr;
15d034d0 15746 const char *name;
a405673c 15747 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15748 unsigned int bit_stride = 0;
c906108c 15749
e7c27a73 15750 element_type = die_type (die, cu);
c906108c 15751
7e314c57
JK
15752 /* The die_type call above may have already set the type for this DIE. */
15753 type = get_die_type (die, cu);
15754 if (type)
15755 return type;
15756
dc53a7ad
JB
15757 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15758 if (attr != NULL)
a405673c
JB
15759 {
15760 int stride_ok;
09ba997f 15761 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15762
15763 byte_stride_prop
15764 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15765 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15766 prop_type);
a405673c
JB
15767 if (!stride_ok)
15768 {
b98664d3 15769 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15770 " - DIE at %s [in module %s]"),
15771 sect_offset_str (die->sect_off),
518817b3 15772 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15773 /* Ignore this attribute. We will likely not be able to print
15774 arrays of this type correctly, but there is little we can do
15775 to help if we cannot read the attribute's value. */
15776 byte_stride_prop = NULL;
15777 }
15778 }
dc53a7ad
JB
15779
15780 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15781 if (attr != NULL)
15782 bit_stride = DW_UNSND (attr);
15783
c906108c
SS
15784 /* Irix 6.2 native cc creates array types without children for
15785 arrays with unspecified length. */
639d11d3 15786 if (die->child == NULL)
c906108c 15787 {
46bf5051 15788 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15789 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15790 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15791 byte_stride_prop, bit_stride);
f792889a 15792 return set_die_type (die, type, cu);
c906108c
SS
15793 }
15794
791afaa2 15795 std::vector<struct type *> range_types;
639d11d3 15796 child_die = die->child;
c906108c
SS
15797 while (child_die && child_die->tag)
15798 {
15799 if (child_die->tag == DW_TAG_subrange_type)
15800 {
f792889a 15801 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15802
f792889a 15803 if (child_type != NULL)
a02abb62 15804 {
0963b4bd
MS
15805 /* The range type was succesfully read. Save it for the
15806 array type creation. */
791afaa2 15807 range_types.push_back (child_type);
a02abb62 15808 }
c906108c 15809 }
436c571c 15810 child_die = child_die->sibling;
c906108c
SS
15811 }
15812
15813 /* Dwarf2 dimensions are output from left to right, create the
15814 necessary array types in backwards order. */
7ca2d3a3 15815
c906108c 15816 type = element_type;
7ca2d3a3
DL
15817
15818 if (read_array_order (die, cu) == DW_ORD_col_major)
15819 {
15820 int i = 0;
9a619af0 15821
791afaa2 15822 while (i < range_types.size ())
dc53a7ad 15823 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15824 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15825 }
15826 else
15827 {
791afaa2 15828 size_t ndim = range_types.size ();
7ca2d3a3 15829 while (ndim-- > 0)
dc53a7ad 15830 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15831 byte_stride_prop, bit_stride);
7ca2d3a3 15832 }
c906108c 15833
f5f8a009
EZ
15834 /* Understand Dwarf2 support for vector types (like they occur on
15835 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15836 array type. This is not part of the Dwarf2/3 standard yet, but a
15837 custom vendor extension. The main difference between a regular
15838 array and the vector variant is that vectors are passed by value
15839 to functions. */
e142c38c 15840 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15841 if (attr != nullptr)
ea37ba09 15842 make_vector_type (type);
f5f8a009 15843
dbc98a8b
KW
15844 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15845 implementation may choose to implement triple vectors using this
15846 attribute. */
15847 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15848 if (attr != nullptr)
dbc98a8b
KW
15849 {
15850 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15851 TYPE_LENGTH (type) = DW_UNSND (attr);
15852 else
b98664d3 15853 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15854 "than the total size of elements"));
dbc98a8b
KW
15855 }
15856
39cbfefa
DJ
15857 name = dwarf2_name (die, cu);
15858 if (name)
15859 TYPE_NAME (type) = name;
6e70227d 15860
2b4424c3
TT
15861 maybe_set_alignment (cu, die, type);
15862
0963b4bd 15863 /* Install the type in the die. */
7e314c57
JK
15864 set_die_type (die, type, cu);
15865
15866 /* set_die_type should be already done. */
b4ba55a1
JB
15867 set_descriptive_type (type, die, cu);
15868
7e314c57 15869 return type;
c906108c
SS
15870}
15871
7ca2d3a3 15872static enum dwarf_array_dim_ordering
6e70227d 15873read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15874{
15875 struct attribute *attr;
15876
15877 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15878
435d3d88 15879 if (attr != nullptr)
aead7601 15880 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15881
0963b4bd
MS
15882 /* GNU F77 is a special case, as at 08/2004 array type info is the
15883 opposite order to the dwarf2 specification, but data is still
15884 laid out as per normal fortran.
7ca2d3a3 15885
0963b4bd
MS
15886 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15887 version checking. */
7ca2d3a3 15888
905e0470
PM
15889 if (cu->language == language_fortran
15890 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15891 {
15892 return DW_ORD_row_major;
15893 }
15894
6e70227d 15895 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15896 {
15897 case array_column_major:
15898 return DW_ORD_col_major;
15899 case array_row_major:
15900 default:
15901 return DW_ORD_row_major;
15902 };
15903}
15904
72019c9c 15905/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15906 the DIE's type field. */
72019c9c 15907
f792889a 15908static struct type *
72019c9c
GM
15909read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15910{
7e314c57
JK
15911 struct type *domain_type, *set_type;
15912 struct attribute *attr;
f792889a 15913
7e314c57
JK
15914 domain_type = die_type (die, cu);
15915
15916 /* The die_type call above may have already set the type for this DIE. */
15917 set_type = get_die_type (die, cu);
15918 if (set_type)
15919 return set_type;
15920
15921 set_type = create_set_type (NULL, domain_type);
15922
15923 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15924 if (attr != nullptr)
d09039dd 15925 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15926
2b4424c3
TT
15927 maybe_set_alignment (cu, die, set_type);
15928
f792889a 15929 return set_die_type (die, set_type, cu);
72019c9c 15930}
7ca2d3a3 15931
0971de02
TT
15932/* A helper for read_common_block that creates a locexpr baton.
15933 SYM is the symbol which we are marking as computed.
15934 COMMON_DIE is the DIE for the common block.
15935 COMMON_LOC is the location expression attribute for the common
15936 block itself.
15937 MEMBER_LOC is the location expression attribute for the particular
15938 member of the common block that we are processing.
15939 CU is the CU from which the above come. */
15940
15941static void
15942mark_common_block_symbol_computed (struct symbol *sym,
15943 struct die_info *common_die,
15944 struct attribute *common_loc,
15945 struct attribute *member_loc,
15946 struct dwarf2_cu *cu)
15947{
518817b3
SM
15948 struct dwarf2_per_objfile *dwarf2_per_objfile
15949 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15950 struct objfile *objfile = dwarf2_per_objfile->objfile;
15951 struct dwarf2_locexpr_baton *baton;
15952 gdb_byte *ptr;
15953 unsigned int cu_off;
08feed99 15954 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
15955 LONGEST offset = 0;
15956
15957 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15958 gdb_assert (common_loc->form_is_block ());
15959 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15960 || member_loc->form_is_constant ());
0971de02 15961
8d749320 15962 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15963 baton->per_cu = cu->per_cu;
15964 gdb_assert (baton->per_cu);
15965
15966 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15967
cd6c91b4 15968 if (member_loc->form_is_constant ())
0971de02 15969 {
0826b30a 15970 offset = member_loc->constant_value (0);
0971de02
TT
15971 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15972 }
15973 else
15974 baton->size += DW_BLOCK (member_loc)->size;
15975
224c3ddb 15976 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15977 baton->data = ptr;
15978
15979 *ptr++ = DW_OP_call4;
9c541725 15980 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15981 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15982 ptr += 4;
15983
cd6c91b4 15984 if (member_loc->form_is_constant ())
0971de02
TT
15985 {
15986 *ptr++ = DW_OP_addr;
15987 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15988 ptr += cu->header.addr_size;
15989 }
15990 else
15991 {
15992 /* We have to copy the data here, because DW_OP_call4 will only
15993 use a DW_AT_location attribute. */
15994 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15995 ptr += DW_BLOCK (member_loc)->size;
15996 }
15997
15998 *ptr++ = DW_OP_plus;
15999 gdb_assert (ptr - baton->data == baton->size);
16000
0971de02 16001 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16002 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16003}
16004
4357ac6c
TT
16005/* Create appropriate locally-scoped variables for all the
16006 DW_TAG_common_block entries. Also create a struct common_block
16007 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16008 is used to separate the common blocks name namespace from regular
4357ac6c 16009 variable names. */
c906108c
SS
16010
16011static void
e7c27a73 16012read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16013{
0971de02
TT
16014 struct attribute *attr;
16015
16016 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16017 if (attr != nullptr)
0971de02
TT
16018 {
16019 /* Support the .debug_loc offsets. */
4fc6c0d5 16020 if (attr->form_is_block ())
0971de02
TT
16021 {
16022 /* Ok. */
16023 }
cd6c91b4 16024 else if (attr->form_is_section_offset ())
0971de02
TT
16025 {
16026 dwarf2_complex_location_expr_complaint ();
16027 attr = NULL;
16028 }
16029 else
16030 {
16031 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16032 "common block member");
16033 attr = NULL;
16034 }
16035 }
16036
639d11d3 16037 if (die->child != NULL)
c906108c 16038 {
518817b3 16039 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16040 struct die_info *child_die;
16041 size_t n_entries = 0, size;
16042 struct common_block *common_block;
16043 struct symbol *sym;
74ac6d43 16044
4357ac6c
TT
16045 for (child_die = die->child;
16046 child_die && child_die->tag;
436c571c 16047 child_die = child_die->sibling)
4357ac6c
TT
16048 ++n_entries;
16049
16050 size = (sizeof (struct common_block)
16051 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16052 common_block
16053 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16054 size);
4357ac6c
TT
16055 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16056 common_block->n_entries = 0;
16057
16058 for (child_die = die->child;
16059 child_die && child_die->tag;
436c571c 16060 child_die = child_die->sibling)
4357ac6c
TT
16061 {
16062 /* Create the symbol in the DW_TAG_common_block block in the current
16063 symbol scope. */
e7c27a73 16064 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16065 if (sym != NULL)
16066 {
16067 struct attribute *member_loc;
16068
16069 common_block->contents[common_block->n_entries++] = sym;
16070
16071 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16072 cu);
16073 if (member_loc)
16074 {
16075 /* GDB has handled this for a long time, but it is
16076 not specified by DWARF. It seems to have been
16077 emitted by gfortran at least as recently as:
16078 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16079 complaint (_("Variable in common block has "
0971de02 16080 "DW_AT_data_member_location "
9d8780f0
SM
16081 "- DIE at %s [in module %s]"),
16082 sect_offset_str (child_die->sect_off),
518817b3 16083 objfile_name (objfile));
0971de02 16084
cd6c91b4 16085 if (member_loc->form_is_section_offset ())
0971de02 16086 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16087 else if (member_loc->form_is_constant ()
4fc6c0d5 16088 || member_loc->form_is_block ())
0971de02 16089 {
435d3d88 16090 if (attr != nullptr)
0971de02
TT
16091 mark_common_block_symbol_computed (sym, die, attr,
16092 member_loc, cu);
16093 }
16094 else
16095 dwarf2_complex_location_expr_complaint ();
16096 }
16097 }
c906108c 16098 }
4357ac6c
TT
16099
16100 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16101 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16102 }
16103}
16104
0114d602 16105/* Create a type for a C++ namespace. */
d9fa45fe 16106
0114d602
DJ
16107static struct type *
16108read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16109{
518817b3 16110 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16111 const char *previous_prefix, *name;
9219021c 16112 int is_anonymous;
0114d602
DJ
16113 struct type *type;
16114
16115 /* For extensions, reuse the type of the original namespace. */
16116 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16117 {
16118 struct die_info *ext_die;
16119 struct dwarf2_cu *ext_cu = cu;
9a619af0 16120
0114d602
DJ
16121 ext_die = dwarf2_extension (die, &ext_cu);
16122 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16123
16124 /* EXT_CU may not be the same as CU.
02142a6c 16125 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16126 return set_die_type (die, type, cu);
16127 }
9219021c 16128
e142c38c 16129 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16130
16131 /* Now build the name of the current namespace. */
16132
0114d602
DJ
16133 previous_prefix = determine_prefix (die, cu);
16134 if (previous_prefix[0] != '\0')
16135 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16136 previous_prefix, name, 0, cu);
0114d602
DJ
16137
16138 /* Create the type. */
19f392bc 16139 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16140
60531b24 16141 return set_die_type (die, type, cu);
0114d602
DJ
16142}
16143
22cee43f 16144/* Read a namespace scope. */
0114d602
DJ
16145
16146static void
16147read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16148{
518817b3 16149 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16150 int is_anonymous;
9219021c 16151
5c4e30ca
DC
16152 /* Add a symbol associated to this if we haven't seen the namespace
16153 before. Also, add a using directive if it's an anonymous
16154 namespace. */
9219021c 16155
f2f0e013 16156 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16157 {
16158 struct type *type;
16159
0114d602 16160 type = read_type_die (die, cu);
e7c27a73 16161 new_symbol (die, type, cu);
5c4e30ca 16162
e8e80198 16163 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16164 if (is_anonymous)
0114d602
DJ
16165 {
16166 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16167
eb1e02fd 16168 std::vector<const char *> excludes;
804d2729 16169 add_using_directive (using_directives (cu),
22cee43f 16170 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16171 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16172 }
5c4e30ca 16173 }
9219021c 16174
639d11d3 16175 if (die->child != NULL)
d9fa45fe 16176 {
639d11d3 16177 struct die_info *child_die = die->child;
6e70227d 16178
d9fa45fe
DC
16179 while (child_die && child_die->tag)
16180 {
e7c27a73 16181 process_die (child_die, cu);
436c571c 16182 child_die = child_die->sibling;
d9fa45fe
DC
16183 }
16184 }
38d518c9
EZ
16185}
16186
f55ee35c
JK
16187/* Read a Fortran module as type. This DIE can be only a declaration used for
16188 imported module. Still we need that type as local Fortran "use ... only"
16189 declaration imports depend on the created type in determine_prefix. */
16190
16191static struct type *
16192read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16193{
518817b3 16194 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16195 const char *module_name;
f55ee35c
JK
16196 struct type *type;
16197
16198 module_name = dwarf2_name (die, cu);
19f392bc 16199 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16200
f55ee35c
JK
16201 return set_die_type (die, type, cu);
16202}
16203
5d7cb8df
JK
16204/* Read a Fortran module. */
16205
16206static void
16207read_module (struct die_info *die, struct dwarf2_cu *cu)
16208{
16209 struct die_info *child_die = die->child;
530e8392
KB
16210 struct type *type;
16211
16212 type = read_type_die (die, cu);
16213 new_symbol (die, type, cu);
5d7cb8df 16214
5d7cb8df
JK
16215 while (child_die && child_die->tag)
16216 {
16217 process_die (child_die, cu);
436c571c 16218 child_die = child_die->sibling;
5d7cb8df
JK
16219 }
16220}
16221
38d518c9
EZ
16222/* Return the name of the namespace represented by DIE. Set
16223 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16224 namespace. */
16225
16226static const char *
e142c38c 16227namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16228{
16229 struct die_info *current_die;
16230 const char *name = NULL;
16231
16232 /* Loop through the extensions until we find a name. */
16233
16234 for (current_die = die;
16235 current_die != NULL;
f2f0e013 16236 current_die = dwarf2_extension (die, &cu))
38d518c9 16237 {
96553a0c
DE
16238 /* We don't use dwarf2_name here so that we can detect the absence
16239 of a name -> anonymous namespace. */
7d45c7c3 16240 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16241
38d518c9
EZ
16242 if (name != NULL)
16243 break;
16244 }
16245
16246 /* Is it an anonymous namespace? */
16247
16248 *is_anonymous = (name == NULL);
16249 if (*is_anonymous)
2b1dbab0 16250 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16251
16252 return name;
d9fa45fe
DC
16253}
16254
c906108c
SS
16255/* Extract all information from a DW_TAG_pointer_type DIE and add to
16256 the user defined type vector. */
16257
f792889a 16258static struct type *
e7c27a73 16259read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16260{
518817b3 16261 struct gdbarch *gdbarch
08feed99 16262 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16263 struct comp_unit_head *cu_header = &cu->header;
c906108c 16264 struct type *type;
8b2dbe47
KB
16265 struct attribute *attr_byte_size;
16266 struct attribute *attr_address_class;
16267 int byte_size, addr_class;
7e314c57
JK
16268 struct type *target_type;
16269
16270 target_type = die_type (die, cu);
c906108c 16271
7e314c57
JK
16272 /* The die_type call above may have already set the type for this DIE. */
16273 type = get_die_type (die, cu);
16274 if (type)
16275 return type;
16276
16277 type = lookup_pointer_type (target_type);
8b2dbe47 16278
e142c38c 16279 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16280 if (attr_byte_size)
16281 byte_size = DW_UNSND (attr_byte_size);
c906108c 16282 else
8b2dbe47
KB
16283 byte_size = cu_header->addr_size;
16284
e142c38c 16285 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16286 if (attr_address_class)
16287 addr_class = DW_UNSND (attr_address_class);
16288 else
16289 addr_class = DW_ADDR_none;
16290
2b4424c3
TT
16291 ULONGEST alignment = get_alignment (cu, die);
16292
16293 /* If the pointer size, alignment, or address class is different
16294 than the default, create a type variant marked as such and set
16295 the length accordingly. */
16296 if (TYPE_LENGTH (type) != byte_size
16297 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16298 && alignment != TYPE_RAW_ALIGN (type))
16299 || addr_class != DW_ADDR_none)
c906108c 16300 {
5e2b427d 16301 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16302 {
16303 int type_flags;
16304
849957d9 16305 type_flags = gdbarch_address_class_type_flags
5e2b427d 16306 (gdbarch, byte_size, addr_class);
876cecd0
TT
16307 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16308 == 0);
8b2dbe47
KB
16309 type = make_type_with_address_space (type, type_flags);
16310 }
16311 else if (TYPE_LENGTH (type) != byte_size)
16312 {
b98664d3 16313 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16314 }
2b4424c3
TT
16315 else if (TYPE_RAW_ALIGN (type) != alignment)
16316 {
b98664d3 16317 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16318 " - DIE at %s [in module %s]"),
16319 sect_offset_str (die->sect_off),
16320 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16321 }
6e70227d 16322 else
9a619af0
MS
16323 {
16324 /* Should we also complain about unhandled address classes? */
16325 }
c906108c 16326 }
8b2dbe47
KB
16327
16328 TYPE_LENGTH (type) = byte_size;
2b4424c3 16329 set_type_align (type, alignment);
f792889a 16330 return set_die_type (die, type, cu);
c906108c
SS
16331}
16332
16333/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16334 the user defined type vector. */
16335
f792889a 16336static struct type *
e7c27a73 16337read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16338{
16339 struct type *type;
16340 struct type *to_type;
16341 struct type *domain;
16342
e7c27a73
DJ
16343 to_type = die_type (die, cu);
16344 domain = die_containing_type (die, cu);
0d5de010 16345
7e314c57
JK
16346 /* The calls above may have already set the type for this DIE. */
16347 type = get_die_type (die, cu);
16348 if (type)
16349 return type;
16350
0d5de010
DJ
16351 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16352 type = lookup_methodptr_type (to_type);
7078baeb
TT
16353 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16354 {
518817b3
SM
16355 struct type *new_type
16356 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16357
16358 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16359 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16360 TYPE_VARARGS (to_type));
16361 type = lookup_methodptr_type (new_type);
16362 }
0d5de010
DJ
16363 else
16364 type = lookup_memberptr_type (to_type, domain);
c906108c 16365
f792889a 16366 return set_die_type (die, type, cu);
c906108c
SS
16367}
16368
4297a3f0 16369/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16370 the user defined type vector. */
16371
f792889a 16372static struct type *
4297a3f0
AV
16373read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16374 enum type_code refcode)
c906108c 16375{
e7c27a73 16376 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16377 struct type *type, *target_type;
c906108c
SS
16378 struct attribute *attr;
16379
4297a3f0
AV
16380 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16381
7e314c57
JK
16382 target_type = die_type (die, cu);
16383
16384 /* The die_type call above may have already set the type for this DIE. */
16385 type = get_die_type (die, cu);
16386 if (type)
16387 return type;
16388
4297a3f0 16389 type = lookup_reference_type (target_type, refcode);
e142c38c 16390 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16391 if (attr != nullptr)
c906108c
SS
16392 {
16393 TYPE_LENGTH (type) = DW_UNSND (attr);
16394 }
16395 else
16396 {
107d2387 16397 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16398 }
2b4424c3 16399 maybe_set_alignment (cu, die, type);
f792889a 16400 return set_die_type (die, type, cu);
c906108c
SS
16401}
16402
cf363f18
MW
16403/* Add the given cv-qualifiers to the element type of the array. GCC
16404 outputs DWARF type qualifiers that apply to an array, not the
16405 element type. But GDB relies on the array element type to carry
16406 the cv-qualifiers. This mimics section 6.7.3 of the C99
16407 specification. */
16408
16409static struct type *
16410add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16411 struct type *base_type, int cnst, int voltl)
16412{
16413 struct type *el_type, *inner_array;
16414
16415 base_type = copy_type (base_type);
16416 inner_array = base_type;
16417
16418 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16419 {
16420 TYPE_TARGET_TYPE (inner_array) =
16421 copy_type (TYPE_TARGET_TYPE (inner_array));
16422 inner_array = TYPE_TARGET_TYPE (inner_array);
16423 }
16424
16425 el_type = TYPE_TARGET_TYPE (inner_array);
16426 cnst |= TYPE_CONST (el_type);
16427 voltl |= TYPE_VOLATILE (el_type);
16428 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16429
16430 return set_die_type (die, base_type, cu);
16431}
16432
f792889a 16433static struct type *
e7c27a73 16434read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16435{
f792889a 16436 struct type *base_type, *cv_type;
c906108c 16437
e7c27a73 16438 base_type = die_type (die, cu);
7e314c57
JK
16439
16440 /* The die_type call above may have already set the type for this DIE. */
16441 cv_type = get_die_type (die, cu);
16442 if (cv_type)
16443 return cv_type;
16444
2f608a3a
KW
16445 /* In case the const qualifier is applied to an array type, the element type
16446 is so qualified, not the array type (section 6.7.3 of C99). */
16447 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16448 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16449
f792889a
DJ
16450 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16451 return set_die_type (die, cv_type, cu);
c906108c
SS
16452}
16453
f792889a 16454static struct type *
e7c27a73 16455read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16456{
f792889a 16457 struct type *base_type, *cv_type;
c906108c 16458
e7c27a73 16459 base_type = die_type (die, cu);
7e314c57
JK
16460
16461 /* The die_type call above may have already set the type for this DIE. */
16462 cv_type = get_die_type (die, cu);
16463 if (cv_type)
16464 return cv_type;
16465
cf363f18
MW
16466 /* In case the volatile qualifier is applied to an array type, the
16467 element type is so qualified, not the array type (section 6.7.3
16468 of C99). */
16469 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16470 return add_array_cv_type (die, cu, base_type, 0, 1);
16471
f792889a
DJ
16472 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16473 return set_die_type (die, cv_type, cu);
c906108c
SS
16474}
16475
06d66ee9
TT
16476/* Handle DW_TAG_restrict_type. */
16477
16478static struct type *
16479read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16480{
16481 struct type *base_type, *cv_type;
16482
16483 base_type = die_type (die, cu);
16484
16485 /* The die_type call above may have already set the type for this DIE. */
16486 cv_type = get_die_type (die, cu);
16487 if (cv_type)
16488 return cv_type;
16489
16490 cv_type = make_restrict_type (base_type);
16491 return set_die_type (die, cv_type, cu);
16492}
16493
a2c2acaf
MW
16494/* Handle DW_TAG_atomic_type. */
16495
16496static struct type *
16497read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16498{
16499 struct type *base_type, *cv_type;
16500
16501 base_type = die_type (die, cu);
16502
16503 /* The die_type call above may have already set the type for this DIE. */
16504 cv_type = get_die_type (die, cu);
16505 if (cv_type)
16506 return cv_type;
16507
16508 cv_type = make_atomic_type (base_type);
16509 return set_die_type (die, cv_type, cu);
16510}
16511
c906108c
SS
16512/* Extract all information from a DW_TAG_string_type DIE and add to
16513 the user defined type vector. It isn't really a user defined type,
16514 but it behaves like one, with other DIE's using an AT_user_def_type
16515 attribute to reference it. */
16516
f792889a 16517static struct type *
e7c27a73 16518read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16519{
518817b3 16520 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16521 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16522 struct type *type, *range_type, *index_type, *char_type;
16523 struct attribute *attr;
216a7e6b
AB
16524 struct dynamic_prop prop;
16525 bool length_is_constant = true;
16526 LONGEST length;
16527
16528 /* There are a couple of places where bit sizes might be made use of
16529 when parsing a DW_TAG_string_type, however, no producer that we know
16530 of make use of these. Handling bit sizes that are a multiple of the
16531 byte size is easy enough, but what about other bit sizes? Lets deal
16532 with that problem when we have to. Warn about these attributes being
16533 unsupported, then parse the type and ignore them like we always
16534 have. */
16535 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16536 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16537 {
16538 static bool warning_printed = false;
16539 if (!warning_printed)
16540 {
16541 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16542 "currently supported on DW_TAG_string_type."));
16543 warning_printed = true;
16544 }
16545 }
c906108c 16546
e142c38c 16547 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16548 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16549 {
16550 /* The string length describes the location at which the length of
16551 the string can be found. The size of the length field can be
16552 specified with one of the attributes below. */
16553 struct type *prop_type;
16554 struct attribute *len
16555 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16556 if (len == nullptr)
16557 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16558 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16559 {
16560 /* Pass 0 as the default as we know this attribute is constant
16561 and the default value will not be returned. */
0826b30a 16562 LONGEST sz = len->constant_value (0);
09ba997f 16563 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16564 }
16565 else
16566 {
16567 /* If the size is not specified then we assume it is the size of
16568 an address on this target. */
09ba997f 16569 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16570 }
16571
16572 /* Convert the attribute into a dynamic property. */
16573 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16574 length = 1;
16575 else
16576 length_is_constant = false;
16577 }
16578 else if (attr != nullptr)
16579 {
16580 /* This DW_AT_string_length just contains the length with no
16581 indirection. There's no need to create a dynamic property in this
16582 case. Pass 0 for the default value as we know it will not be
16583 returned in this case. */
0826b30a 16584 length = attr->constant_value (0);
216a7e6b
AB
16585 }
16586 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16587 {
216a7e6b 16588 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16589 length = attr->constant_value (1);
c906108c
SS
16590 }
16591 else
16592 {
216a7e6b
AB
16593 /* Use 1 as a fallback length if we have nothing else. */
16594 length = 1;
c906108c 16595 }
6ccb9162 16596
46bf5051 16597 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16598 if (length_is_constant)
16599 range_type = create_static_range_type (NULL, index_type, 1, length);
16600 else
16601 {
16602 struct dynamic_prop low_bound;
16603
16604 low_bound.kind = PROP_CONST;
16605 low_bound.data.const_val = 1;
16606 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16607 }
3b7538c0
UW
16608 char_type = language_string_char_type (cu->language_defn, gdbarch);
16609 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16610
f792889a 16611 return set_die_type (die, type, cu);
c906108c
SS
16612}
16613
4d804846
JB
16614/* Assuming that DIE corresponds to a function, returns nonzero
16615 if the function is prototyped. */
16616
16617static int
16618prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16619{
16620 struct attribute *attr;
16621
16622 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16623 if (attr && (DW_UNSND (attr) != 0))
16624 return 1;
16625
16626 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16627 is only meaningful for C, but the concept also extends to other
4d804846
JB
16628 languages that allow unprototyped functions (Eg: Objective C).
16629 For all other languages, assume that functions are always
16630 prototyped. */
16631 if (cu->language != language_c
16632 && cu->language != language_objc
16633 && cu->language != language_opencl)
16634 return 1;
16635
16636 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16637 prototyped and unprototyped functions; default to prototyped,
16638 since that is more common in modern code (and RealView warns
16639 about unprototyped functions). */
16640 if (producer_is_realview (cu->producer))
16641 return 1;
16642
16643 return 0;
16644}
16645
c906108c
SS
16646/* Handle DIES due to C code like:
16647
16648 struct foo
c5aa993b
JM
16649 {
16650 int (*funcp)(int a, long l);
16651 int b;
16652 };
c906108c 16653
0963b4bd 16654 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16655
f792889a 16656static struct type *
e7c27a73 16657read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16658{
518817b3 16659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16660 struct type *type; /* Type that this function returns. */
16661 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16662 struct attribute *attr;
16663
e7c27a73 16664 type = die_type (die, cu);
7e314c57
JK
16665
16666 /* The die_type call above may have already set the type for this DIE. */
16667 ftype = get_die_type (die, cu);
16668 if (ftype)
16669 return ftype;
16670
0c8b41f1 16671 ftype = lookup_function_type (type);
c906108c 16672
4d804846 16673 if (prototyped_function_p (die, cu))
a6c727b2 16674 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16675
c055b101
CV
16676 /* Store the calling convention in the type if it's available in
16677 the subroutine die. Otherwise set the calling convention to
16678 the default value DW_CC_normal. */
16679 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16680 if (attr != nullptr
16681 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16682 TYPE_CALLING_CONVENTION (ftype)
16683 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16684 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16685 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16686 else
16687 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16688
743649fd
MW
16689 /* Record whether the function returns normally to its caller or not
16690 if the DWARF producer set that information. */
16691 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16692 if (attr && (DW_UNSND (attr) != 0))
16693 TYPE_NO_RETURN (ftype) = 1;
16694
76c10ea2
GM
16695 /* We need to add the subroutine type to the die immediately so
16696 we don't infinitely recurse when dealing with parameters
0963b4bd 16697 declared as the same subroutine type. */
76c10ea2 16698 set_die_type (die, ftype, cu);
6e70227d 16699
639d11d3 16700 if (die->child != NULL)
c906108c 16701 {
bb5ed363 16702 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16703 struct die_info *child_die;
8072405b 16704 int nparams, iparams;
c906108c
SS
16705
16706 /* Count the number of parameters.
16707 FIXME: GDB currently ignores vararg functions, but knows about
16708 vararg member functions. */
8072405b 16709 nparams = 0;
639d11d3 16710 child_die = die->child;
c906108c
SS
16711 while (child_die && child_die->tag)
16712 {
16713 if (child_die->tag == DW_TAG_formal_parameter)
16714 nparams++;
16715 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16716 TYPE_VARARGS (ftype) = 1;
436c571c 16717 child_die = child_die->sibling;
c906108c
SS
16718 }
16719
16720 /* Allocate storage for parameters and fill them in. */
16721 TYPE_NFIELDS (ftype) = nparams;
16722 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16723 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16724
8072405b
JK
16725 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16726 even if we error out during the parameters reading below. */
16727 for (iparams = 0; iparams < nparams; iparams++)
16728 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16729
16730 iparams = 0;
639d11d3 16731 child_die = die->child;
c906108c
SS
16732 while (child_die && child_die->tag)
16733 {
16734 if (child_die->tag == DW_TAG_formal_parameter)
16735 {
3ce3b1ba
PA
16736 struct type *arg_type;
16737
16738 /* DWARF version 2 has no clean way to discern C++
16739 static and non-static member functions. G++ helps
16740 GDB by marking the first parameter for non-static
16741 member functions (which is the this pointer) as
16742 artificial. We pass this information to
16743 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16744
16745 DWARF version 3 added DW_AT_object_pointer, which GCC
16746 4.5 does not yet generate. */
e142c38c 16747 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16748 if (attr != nullptr)
c906108c
SS
16749 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16750 else
9c37b5ae 16751 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16752 arg_type = die_type (child_die, cu);
16753
16754 /* RealView does not mark THIS as const, which the testsuite
16755 expects. GCC marks THIS as const in method definitions,
16756 but not in the class specifications (GCC PR 43053). */
16757 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16758 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16759 {
16760 int is_this = 0;
16761 struct dwarf2_cu *arg_cu = cu;
16762 const char *name = dwarf2_name (child_die, cu);
16763
16764 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16765 if (attr != nullptr)
3ce3b1ba
PA
16766 {
16767 /* If the compiler emits this, use it. */
16768 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16769 is_this = 1;
16770 }
16771 else if (name && strcmp (name, "this") == 0)
16772 /* Function definitions will have the argument names. */
16773 is_this = 1;
16774 else if (name == NULL && iparams == 0)
16775 /* Declarations may not have the names, so like
16776 elsewhere in GDB, assume an artificial first
16777 argument is "this". */
16778 is_this = 1;
16779
16780 if (is_this)
16781 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16782 arg_type, 0);
16783 }
16784
16785 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16786 iparams++;
16787 }
436c571c 16788 child_die = child_die->sibling;
c906108c
SS
16789 }
16790 }
16791
76c10ea2 16792 return ftype;
c906108c
SS
16793}
16794
f792889a 16795static struct type *
e7c27a73 16796read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16797{
518817b3 16798 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16799 const char *name = NULL;
3c8e0968 16800 struct type *this_type, *target_type;
c906108c 16801
94af9270 16802 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16803 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16804 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16805 set_die_type (die, this_type, cu);
3c8e0968
DE
16806 target_type = die_type (die, cu);
16807 if (target_type != this_type)
16808 TYPE_TARGET_TYPE (this_type) = target_type;
16809 else
16810 {
16811 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16812 spec and cause infinite loops in GDB. */
b98664d3 16813 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16814 "- DIE at %s [in module %s]"),
16815 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16816 TYPE_TARGET_TYPE (this_type) = NULL;
16817 }
e4003a34
TV
16818 if (name == NULL)
16819 {
16820 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16821 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16822 Handle these by just returning the target type, rather than
16823 constructing an anonymous typedef type and trying to handle this
16824 elsewhere. */
16825 set_die_type (die, target_type, cu);
16826 return target_type;
16827 }
f792889a 16828 return this_type;
c906108c
SS
16829}
16830
9b790ce7
UW
16831/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16832 (which may be different from NAME) to the architecture back-end to allow
16833 it to guess the correct format if necessary. */
16834
16835static struct type *
16836dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16837 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 16838{
08feed99 16839 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
16840 const struct floatformat **format;
16841 struct type *type;
16842
16843 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16844 if (format)
103a685e 16845 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16846 else
77b7c781 16847 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16848
16849 return type;
16850}
16851
eb77c9df
AB
16852/* Allocate an integer type of size BITS and name NAME. */
16853
16854static struct type *
16855dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16856 int bits, int unsigned_p, const char *name)
16857{
16858 struct type *type;
16859
16860 /* Versions of Intel's C Compiler generate an integer type called "void"
16861 instead of using DW_TAG_unspecified_type. This has been seen on
16862 at least versions 14, 17, and 18. */
35ee2dc2
AB
16863 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16864 && strcmp (name, "void") == 0)
eb77c9df
AB
16865 type = objfile_type (objfile)->builtin_void;
16866 else
16867 type = init_integer_type (objfile, bits, unsigned_p, name);
16868
16869 return type;
16870}
16871
8bdc1658
AB
16872/* Initialise and return a floating point type of size BITS suitable for
16873 use as a component of a complex number. The NAME_HINT is passed through
16874 when initialising the floating point type and is the name of the complex
16875 type.
16876
16877 As DWARF doesn't currently provide an explicit name for the components
16878 of a complex number, but it can be helpful to have these components
16879 named, we try to select a suitable name based on the size of the
16880 component. */
16881static struct type *
16882dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16883 struct objfile *objfile,
103a685e
TT
16884 int bits, const char *name_hint,
16885 enum bfd_endian byte_order)
8bdc1658 16886{
08feed99 16887 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
16888 struct type *tt = nullptr;
16889
35add35e
AB
16890 /* Try to find a suitable floating point builtin type of size BITS.
16891 We're going to use the name of this type as the name for the complex
16892 target type that we are about to create. */
1db455a7 16893 switch (cu->language)
8bdc1658 16894 {
1db455a7
AB
16895 case language_fortran:
16896 switch (bits)
16897 {
16898 case 32:
16899 tt = builtin_f_type (gdbarch)->builtin_real;
16900 break;
16901 case 64:
16902 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16903 break;
16904 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16905 case 128:
16906 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16907 break;
16908 }
8bdc1658 16909 break;
1db455a7
AB
16910 default:
16911 switch (bits)
16912 {
16913 case 32:
16914 tt = builtin_type (gdbarch)->builtin_float;
16915 break;
16916 case 64:
16917 tt = builtin_type (gdbarch)->builtin_double;
16918 break;
16919 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16920 case 128:
16921 tt = builtin_type (gdbarch)->builtin_long_double;
16922 break;
16923 }
8bdc1658
AB
16924 break;
16925 }
16926
35add35e
AB
16927 /* If the type we found doesn't match the size we were looking for, then
16928 pretend we didn't find a type at all, the complex target type we
16929 create will then be nameless. */
a12e5744 16930 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16931 tt = nullptr;
16932
8bdc1658 16933 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16934 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16935}
16936
c906108c
SS
16937/* Find a representation of a given base type and install
16938 it in the TYPE field of the die. */
16939
f792889a 16940static struct type *
e7c27a73 16941read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16942{
518817b3 16943 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16944 struct type *type;
16945 struct attribute *attr;
19f392bc 16946 int encoding = 0, bits = 0;
15d034d0 16947 const char *name;
34877895 16948 gdbarch *arch;
c906108c 16949
e142c38c 16950 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16951 if (attr != nullptr)
34877895 16952 encoding = DW_UNSND (attr);
e142c38c 16953 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16954 if (attr != nullptr)
34877895 16955 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16956 name = dwarf2_name (die, cu);
6ccb9162 16957 if (!name)
34877895 16958 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 16959
08feed99 16960 arch = objfile->arch ();
103a685e
TT
16961 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16962
34877895
PJ
16963 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16964 if (attr)
103a685e
TT
16965 {
16966 int endianity = DW_UNSND (attr);
16967
16968 switch (endianity)
16969 {
16970 case DW_END_big:
16971 byte_order = BFD_ENDIAN_BIG;
16972 break;
16973 case DW_END_little:
16974 byte_order = BFD_ENDIAN_LITTLE;
16975 break;
16976 default:
16977 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16978 break;
16979 }
16980 }
6ccb9162
UW
16981
16982 switch (encoding)
c906108c 16983 {
6ccb9162
UW
16984 case DW_ATE_address:
16985 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16986 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16987 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16988 break;
16989 case DW_ATE_boolean:
19f392bc 16990 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16991 break;
16992 case DW_ATE_complex_float:
103a685e
TT
16993 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16994 byte_order);
93689ce9
TT
16995 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16996 {
16997 if (name == nullptr)
16998 {
16999 struct obstack *obstack
17000 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17001 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17002 nullptr);
17003 }
17004 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17005 }
17006 else
17007 type = init_complex_type (name, type);
6ccb9162
UW
17008 break;
17009 case DW_ATE_decimal_float:
19f392bc 17010 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17011 break;
17012 case DW_ATE_float:
103a685e 17013 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17014 break;
17015 case DW_ATE_signed:
eb77c9df 17016 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17017 break;
17018 case DW_ATE_unsigned:
3b2b8fea
TT
17019 if (cu->language == language_fortran
17020 && name
61012eef 17021 && startswith (name, "character("))
19f392bc
UW
17022 type = init_character_type (objfile, bits, 1, name);
17023 else
eb77c9df 17024 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17025 break;
17026 case DW_ATE_signed_char:
6e70227d 17027 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17028 || cu->language == language_pascal
17029 || cu->language == language_fortran)
19f392bc
UW
17030 type = init_character_type (objfile, bits, 0, name);
17031 else
eb77c9df 17032 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17033 break;
17034 case DW_ATE_unsigned_char:
868a0084 17035 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17036 || cu->language == language_pascal
c44af4eb
TT
17037 || cu->language == language_fortran
17038 || cu->language == language_rust)
19f392bc
UW
17039 type = init_character_type (objfile, bits, 1, name);
17040 else
eb77c9df 17041 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17042 break;
75079b2b 17043 case DW_ATE_UTF:
53e710ac 17044 {
53e710ac
PA
17045 if (bits == 16)
17046 type = builtin_type (arch)->builtin_char16;
17047 else if (bits == 32)
17048 type = builtin_type (arch)->builtin_char32;
17049 else
17050 {
b98664d3 17051 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17052 bits);
eb77c9df 17053 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17054 }
17055 return set_die_type (die, type, cu);
17056 }
75079b2b
TT
17057 break;
17058
6ccb9162 17059 default:
b98664d3 17060 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17061 dwarf_type_encoding_name (encoding));
77b7c781 17062 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17063 break;
c906108c 17064 }
6ccb9162 17065
0114d602 17066 if (name && strcmp (name, "char") == 0)
876cecd0 17067 TYPE_NOSIGN (type) = 1;
0114d602 17068
2b4424c3
TT
17069 maybe_set_alignment (cu, die, type);
17070
103a685e 17071 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17072
f792889a 17073 return set_die_type (die, type, cu);
c906108c
SS
17074}
17075
80180f79
SA
17076/* Parse dwarf attribute if it's a block, reference or constant and put the
17077 resulting value of the attribute into struct bound_prop.
17078 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17079
17080static int
17081attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17082 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17083 struct type *default_type)
80180f79
SA
17084{
17085 struct dwarf2_property_baton *baton;
518817b3
SM
17086 struct obstack *obstack
17087 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17088
9a49df9d
AB
17089 gdb_assert (default_type != NULL);
17090
80180f79
SA
17091 if (attr == NULL || prop == NULL)
17092 return 0;
17093
4fc6c0d5 17094 if (attr->form_is_block ())
80180f79 17095 {
8d749320 17096 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17097 baton->property_type = default_type;
80180f79
SA
17098 baton->locexpr.per_cu = cu->per_cu;
17099 baton->locexpr.size = DW_BLOCK (attr)->size;
17100 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17101 switch (attr->name)
17102 {
17103 case DW_AT_string_length:
17104 baton->locexpr.is_reference = true;
17105 break;
17106 default:
17107 baton->locexpr.is_reference = false;
17108 break;
17109 }
80180f79
SA
17110 prop->data.baton = baton;
17111 prop->kind = PROP_LOCEXPR;
17112 gdb_assert (prop->data.baton != NULL);
17113 }
cd6c91b4 17114 else if (attr->form_is_ref ())
80180f79
SA
17115 {
17116 struct dwarf2_cu *target_cu = cu;
17117 struct die_info *target_die;
17118 struct attribute *target_attr;
17119
17120 target_die = follow_die_ref (die, attr, &target_cu);
17121 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17122 if (target_attr == NULL)
17123 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17124 target_cu);
80180f79
SA
17125 if (target_attr == NULL)
17126 return 0;
17127
df25ebbd 17128 switch (target_attr->name)
80180f79 17129 {
df25ebbd 17130 case DW_AT_location:
cd6c91b4 17131 if (target_attr->form_is_section_offset ())
df25ebbd 17132 {
8d749320 17133 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17134 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17135 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17136 prop->data.baton = baton;
17137 prop->kind = PROP_LOCLIST;
17138 gdb_assert (prop->data.baton != NULL);
17139 }
4fc6c0d5 17140 else if (target_attr->form_is_block ())
df25ebbd 17141 {
8d749320 17142 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17143 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17144 baton->locexpr.per_cu = cu->per_cu;
17145 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17146 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17147 baton->locexpr.is_reference = true;
df25ebbd
JB
17148 prop->data.baton = baton;
17149 prop->kind = PROP_LOCEXPR;
17150 gdb_assert (prop->data.baton != NULL);
17151 }
17152 else
17153 {
17154 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17155 "dynamic property");
17156 return 0;
17157 }
17158 break;
17159 case DW_AT_data_member_location:
17160 {
17161 LONGEST offset;
17162
17163 if (!handle_data_member_location (target_die, target_cu,
17164 &offset))
17165 return 0;
17166
8d749320 17167 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17168 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17169 target_cu);
df25ebbd
JB
17170 baton->offset_info.offset = offset;
17171 baton->offset_info.type = die_type (target_die, target_cu);
17172 prop->data.baton = baton;
17173 prop->kind = PROP_ADDR_OFFSET;
17174 break;
17175 }
80180f79
SA
17176 }
17177 }
cd6c91b4 17178 else if (attr->form_is_constant ())
80180f79 17179 {
0826b30a 17180 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17181 prop->kind = PROP_CONST;
17182 }
17183 else
17184 {
17185 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17186 dwarf2_name (die, cu));
17187 return 0;
17188 }
17189
17190 return 1;
17191}
17192
09ba997f 17193/* See read.h. */
9a49df9d 17194
09ba997f
TT
17195struct type *
17196dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17197{
09ba997f 17198 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17199 struct type *int_type;
17200
17201 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17202#define TRY_TYPE(F) \
17203 int_type = (unsigned_p \
17204 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17205 : objfile_type (objfile)->builtin_ ## F); \
17206 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17207 return int_type
17208
17209 TRY_TYPE (char);
17210 TRY_TYPE (short);
17211 TRY_TYPE (int);
17212 TRY_TYPE (long);
17213 TRY_TYPE (long_long);
17214
17215#undef TRY_TYPE
17216
17217 gdb_assert_not_reached ("unable to find suitable integer type");
17218}
17219
09ba997f 17220/* See read.h. */
11a8b164 17221
09ba997f
TT
17222struct type *
17223dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17224{
09ba997f
TT
17225 int addr_size = this->addr_size ();
17226 return int_type (addr_size, unsigned_p);
11a8b164
AB
17227}
17228
b86352cf
AB
17229/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17230 present (which is valid) then compute the default type based on the
17231 compilation units address size. */
17232
17233static struct type *
17234read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17235{
17236 struct type *index_type = die_type (die, cu);
17237
17238 /* Dwarf-2 specifications explicitly allows to create subrange types
17239 without specifying a base type.
17240 In that case, the base type must be set to the type of
17241 the lower bound, upper bound or count, in that order, if any of these
17242 three attributes references an object that has a type.
17243 If no base type is found, the Dwarf-2 specifications say that
17244 a signed integer type of size equal to the size of an address should
17245 be used.
17246 For the following C code: `extern char gdb_int [];'
17247 GCC produces an empty range DIE.
17248 FIXME: muller/2010-05-28: Possible references to object for low bound,
17249 high bound or count are not yet handled by this code. */
17250 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17251 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17252
17253 return index_type;
17254}
17255
a02abb62
JB
17256/* Read the given DW_AT_subrange DIE. */
17257
f792889a 17258static struct type *
a02abb62
JB
17259read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17260{
4c9ad8c2 17261 struct type *base_type, *orig_base_type;
a02abb62
JB
17262 struct type *range_type;
17263 struct attribute *attr;
729efb13 17264 struct dynamic_prop low, high;
4fae6e18 17265 int low_default_is_valid;
c451ebe5 17266 int high_bound_is_count = 0;
15d034d0 17267 const char *name;
d359392f 17268 ULONGEST negative_mask;
e77813c8 17269
b86352cf
AB
17270 orig_base_type = read_subrange_index_type (die, cu);
17271
4c9ad8c2
TT
17272 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17273 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17274 creating the range type, but we use the result of check_typedef
17275 when examining properties of the type. */
17276 base_type = check_typedef (orig_base_type);
a02abb62 17277
7e314c57
JK
17278 /* The die_type call above may have already set the type for this DIE. */
17279 range_type = get_die_type (die, cu);
17280 if (range_type)
17281 return range_type;
17282
729efb13
SA
17283 low.kind = PROP_CONST;
17284 high.kind = PROP_CONST;
17285 high.data.const_val = 0;
17286
4fae6e18
JK
17287 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17288 omitting DW_AT_lower_bound. */
17289 switch (cu->language)
6e70227d 17290 {
4fae6e18
JK
17291 case language_c:
17292 case language_cplus:
729efb13 17293 low.data.const_val = 0;
4fae6e18
JK
17294 low_default_is_valid = 1;
17295 break;
17296 case language_fortran:
729efb13 17297 low.data.const_val = 1;
4fae6e18
JK
17298 low_default_is_valid = 1;
17299 break;
17300 case language_d:
4fae6e18 17301 case language_objc:
c44af4eb 17302 case language_rust:
729efb13 17303 low.data.const_val = 0;
4fae6e18
JK
17304 low_default_is_valid = (cu->header.version >= 4);
17305 break;
17306 case language_ada:
17307 case language_m2:
17308 case language_pascal:
729efb13 17309 low.data.const_val = 1;
4fae6e18
JK
17310 low_default_is_valid = (cu->header.version >= 4);
17311 break;
17312 default:
729efb13 17313 low.data.const_val = 0;
4fae6e18
JK
17314 low_default_is_valid = 0;
17315 break;
a02abb62
JB
17316 }
17317
e142c38c 17318 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17319 if (attr != nullptr)
9a49df9d 17320 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17321 else if (!low_default_is_valid)
b98664d3 17322 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17323 "- DIE at %s [in module %s]"),
17324 sect_offset_str (die->sect_off),
518817b3 17325 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17326
506f5c41
TV
17327 struct attribute *attr_ub, *attr_count;
17328 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17329 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17330 {
506f5c41 17331 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17332 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17333 {
c451ebe5
SA
17334 /* If bounds are constant do the final calculation here. */
17335 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17336 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17337 else
17338 high_bound_is_count = 1;
c2ff108b 17339 }
506f5c41
TV
17340 else
17341 {
17342 if (attr_ub != NULL)
17343 complaint (_("Unresolved DW_AT_upper_bound "
17344 "- DIE at %s [in module %s]"),
17345 sect_offset_str (die->sect_off),
17346 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17347 if (attr_count != NULL)
17348 complaint (_("Unresolved DW_AT_count "
17349 "- DIE at %s [in module %s]"),
17350 sect_offset_str (die->sect_off),
17351 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17352 }
e77813c8 17353 }
a02abb62 17354
4e962e74
TT
17355 LONGEST bias = 0;
17356 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17357 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17358 bias = bias_attr->constant_value (0);
4e962e74 17359
dbb9c2b1
JB
17360 /* Normally, the DWARF producers are expected to use a signed
17361 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17362 But this is unfortunately not always the case, as witnessed
17363 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17364 is used instead. To work around that ambiguity, we treat
17365 the bounds as signed, and thus sign-extend their values, when
17366 the base type is signed. */
6e70227d 17367 negative_mask =
d359392f 17368 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17369 if (low.kind == PROP_CONST
17370 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17371 low.data.const_val |= negative_mask;
17372 if (high.kind == PROP_CONST
17373 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17374 high.data.const_val |= negative_mask;
43bbcdc2 17375
5bbd8269
AB
17376 /* Check for bit and byte strides. */
17377 struct dynamic_prop byte_stride_prop;
17378 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17379 if (attr_byte_stride != nullptr)
17380 {
09ba997f 17381 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17382 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17383 prop_type);
17384 }
17385
17386 struct dynamic_prop bit_stride_prop;
17387 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17388 if (attr_bit_stride != nullptr)
17389 {
17390 /* It only makes sense to have either a bit or byte stride. */
17391 if (attr_byte_stride != nullptr)
17392 {
17393 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17394 "- DIE at %s [in module %s]"),
17395 sect_offset_str (die->sect_off),
17396 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17397 attr_bit_stride = nullptr;
17398 }
17399 else
17400 {
09ba997f 17401 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17402 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17403 prop_type);
17404 }
17405 }
17406
17407 if (attr_byte_stride != nullptr
17408 || attr_bit_stride != nullptr)
17409 {
17410 bool byte_stride_p = (attr_byte_stride != nullptr);
17411 struct dynamic_prop *stride
17412 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17413
17414 range_type
17415 = create_range_type_with_stride (NULL, orig_base_type, &low,
17416 &high, bias, stride, byte_stride_p);
17417 }
17418 else
17419 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17420
c451ebe5
SA
17421 if (high_bound_is_count)
17422 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17423
c2ff108b
JK
17424 /* Ada expects an empty array on no boundary attributes. */
17425 if (attr == NULL && cu->language != language_ada)
729efb13 17426 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17427
39cbfefa
DJ
17428 name = dwarf2_name (die, cu);
17429 if (name)
17430 TYPE_NAME (range_type) = name;
6e70227d 17431
e142c38c 17432 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17433 if (attr != nullptr)
a02abb62
JB
17434 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17435
2b4424c3
TT
17436 maybe_set_alignment (cu, die, range_type);
17437
7e314c57
JK
17438 set_die_type (die, range_type, cu);
17439
17440 /* set_die_type should be already done. */
b4ba55a1
JB
17441 set_descriptive_type (range_type, die, cu);
17442
7e314c57 17443 return range_type;
a02abb62 17444}
6e70227d 17445
f792889a 17446static struct type *
81a17f79
JB
17447read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17448{
17449 struct type *type;
81a17f79 17450
518817b3
SM
17451 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17452 NULL);
0114d602 17453 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17454
74a2f8ff 17455 /* In Ada, an unspecified type is typically used when the description
85102364 17456 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17457 such a type, we treat it as a stub, and try to resolve it later on,
17458 when needed. */
17459 if (cu->language == language_ada)
17460 TYPE_STUB (type) = 1;
17461
f792889a 17462 return set_die_type (die, type, cu);
81a17f79 17463}
a02abb62 17464
639d11d3
DC
17465/* Read a single die and all its descendents. Set the die's sibling
17466 field to NULL; set other fields in the die correctly, and set all
17467 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17468 location of the info_ptr after reading all of those dies. PARENT
17469 is the parent of the die in question. */
17470
17471static struct die_info *
dee91e82 17472read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17473 const gdb_byte *info_ptr,
17474 const gdb_byte **new_info_ptr,
dee91e82 17475 struct die_info *parent)
639d11d3
DC
17476{
17477 struct die_info *die;
d521ce57 17478 const gdb_byte *cur_ptr;
639d11d3 17479
3e225074 17480 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17481 if (die == NULL)
17482 {
17483 *new_info_ptr = cur_ptr;
17484 return NULL;
17485 }
93311388 17486 store_in_ref_table (die, reader->cu);
639d11d3 17487
3e225074 17488 if (die->has_children)
bf6af496 17489 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17490 else
17491 {
17492 die->child = NULL;
17493 *new_info_ptr = cur_ptr;
17494 }
17495
17496 die->sibling = NULL;
17497 die->parent = parent;
17498 return die;
17499}
17500
17501/* Read a die, all of its descendents, and all of its siblings; set
17502 all of the fields of all of the dies correctly. Arguments are as
17503 in read_die_and_children. */
17504
17505static struct die_info *
bf6af496 17506read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17507 const gdb_byte *info_ptr,
17508 const gdb_byte **new_info_ptr,
bf6af496 17509 struct die_info *parent)
639d11d3
DC
17510{
17511 struct die_info *first_die, *last_sibling;
d521ce57 17512 const gdb_byte *cur_ptr;
639d11d3 17513
c906108c 17514 cur_ptr = info_ptr;
639d11d3
DC
17515 first_die = last_sibling = NULL;
17516
17517 while (1)
c906108c 17518 {
639d11d3 17519 struct die_info *die
dee91e82 17520 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17521
1d325ec1 17522 if (die == NULL)
c906108c 17523 {
639d11d3
DC
17524 *new_info_ptr = cur_ptr;
17525 return first_die;
c906108c 17526 }
1d325ec1
DJ
17527
17528 if (!first_die)
17529 first_die = die;
c906108c 17530 else
1d325ec1
DJ
17531 last_sibling->sibling = die;
17532
17533 last_sibling = die;
c906108c 17534 }
c906108c
SS
17535}
17536
bf6af496
DE
17537/* Read a die, all of its descendents, and all of its siblings; set
17538 all of the fields of all of the dies correctly. Arguments are as
17539 in read_die_and_children.
17540 This the main entry point for reading a DIE and all its children. */
17541
17542static struct die_info *
17543read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17544 const gdb_byte *info_ptr,
17545 const gdb_byte **new_info_ptr,
bf6af496
DE
17546 struct die_info *parent)
17547{
17548 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17549 new_info_ptr, parent);
17550
b4f54984 17551 if (dwarf_die_debug)
bf6af496
DE
17552 {
17553 fprintf_unfiltered (gdb_stdlog,
17554 "Read die from %s@0x%x of %s:\n",
96b79293 17555 reader->die_section->get_name (),
bf6af496
DE
17556 (unsigned) (info_ptr - reader->die_section->buffer),
17557 bfd_get_filename (reader->abfd));
b4f54984 17558 dump_die (die, dwarf_die_debug);
bf6af496
DE
17559 }
17560
17561 return die;
17562}
17563
3019eac3
DE
17564/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17565 attributes.
17566 The caller is responsible for filling in the extra attributes
17567 and updating (*DIEP)->num_attrs.
17568 Set DIEP to point to a newly allocated die with its information,
3e225074 17569 except for its child, sibling, and parent fields. */
93311388 17570
d521ce57 17571static const gdb_byte *
3019eac3 17572read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17573 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17574 int num_extra_attrs)
93311388 17575{
b64f50a1 17576 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17577 struct abbrev_info *abbrev;
17578 struct die_info *die;
17579 struct dwarf2_cu *cu = reader->cu;
17580 bfd *abfd = reader->abfd;
17581
9c541725 17582 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17583 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17584 info_ptr += bytes_read;
17585 if (!abbrev_number)
17586 {
17587 *diep = NULL;
93311388
DE
17588 return info_ptr;
17589 }
17590
685af9cd 17591 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17592 if (!abbrev)
348e048f
DE
17593 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17594 abbrev_number,
17595 bfd_get_filename (abfd));
17596
3019eac3 17597 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17598 die->sect_off = sect_off;
93311388
DE
17599 die->tag = abbrev->tag;
17600 die->abbrev = abbrev_number;
3e225074 17601 die->has_children = abbrev->has_children;
93311388 17602
3019eac3
DE
17603 /* Make the result usable.
17604 The caller needs to update num_attrs after adding the extra
17605 attributes. */
93311388
DE
17606 die->num_attrs = abbrev->num_attrs;
17607
18a8505e 17608 std::vector<int> indexes_that_need_reprocess;
93311388 17609 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17610 {
17611 bool need_reprocess;
17612 info_ptr =
17613 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17614 info_ptr, &need_reprocess);
17615 if (need_reprocess)
17616 indexes_that_need_reprocess.push_back (i);
17617 }
17618
052c8bb8 17619 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17620 if (attr != nullptr)
17621 cu->str_offsets_base = DW_UNSND (attr);
93311388 17622
41144253 17623 attr = die->attr (DW_AT_loclists_base);
17624 if (attr != nullptr)
17625 cu->loclist_base = DW_UNSND (attr);
17626
a39fdb41 17627 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17628 if (maybe_addr_base.has_value ())
17629 cu->addr_base = *maybe_addr_base;
17630 for (int index : indexes_that_need_reprocess)
17631 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17632 *diep = die;
93311388
DE
17633 return info_ptr;
17634}
17635
3019eac3
DE
17636/* Read a die and all its attributes.
17637 Set DIEP to point to a newly allocated die with its information,
3e225074 17638 except for its child, sibling, and parent fields. */
3019eac3 17639
d521ce57 17640static const gdb_byte *
3019eac3 17641read_full_die (const struct die_reader_specs *reader,
3e225074 17642 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17643{
d521ce57 17644 const gdb_byte *result;
bf6af496 17645
3e225074 17646 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17647
b4f54984 17648 if (dwarf_die_debug)
bf6af496
DE
17649 {
17650 fprintf_unfiltered (gdb_stdlog,
17651 "Read die from %s@0x%x of %s:\n",
96b79293 17652 reader->die_section->get_name (),
bf6af496
DE
17653 (unsigned) (info_ptr - reader->die_section->buffer),
17654 bfd_get_filename (reader->abfd));
b4f54984 17655 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17656 }
17657
17658 return result;
3019eac3 17659}
433df2d4 17660\f
c906108c 17661
72bf9492
DJ
17662/* Returns nonzero if TAG represents a type that we might generate a partial
17663 symbol for. */
17664
17665static int
17666is_type_tag_for_partial (int tag)
17667{
17668 switch (tag)
17669 {
17670#if 0
17671 /* Some types that would be reasonable to generate partial symbols for,
17672 that we don't at present. */
17673 case DW_TAG_array_type:
17674 case DW_TAG_file_type:
17675 case DW_TAG_ptr_to_member_type:
17676 case DW_TAG_set_type:
17677 case DW_TAG_string_type:
17678 case DW_TAG_subroutine_type:
17679#endif
17680 case DW_TAG_base_type:
17681 case DW_TAG_class_type:
680b30c7 17682 case DW_TAG_interface_type:
72bf9492
DJ
17683 case DW_TAG_enumeration_type:
17684 case DW_TAG_structure_type:
17685 case DW_TAG_subrange_type:
17686 case DW_TAG_typedef:
17687 case DW_TAG_union_type:
17688 return 1;
17689 default:
17690 return 0;
17691 }
17692}
17693
17694/* Load all DIEs that are interesting for partial symbols into memory. */
17695
17696static struct partial_die_info *
dee91e82 17697load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17698 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17699{
dee91e82 17700 struct dwarf2_cu *cu = reader->cu;
518817b3 17701 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17702 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17703 unsigned int bytes_read;
5afb4e99 17704 unsigned int load_all = 0;
72bf9492
DJ
17705 int nesting_level = 1;
17706
17707 parent_die = NULL;
17708 last_die = NULL;
17709
7adf1e79
DE
17710 gdb_assert (cu->per_cu != NULL);
17711 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17712 load_all = 1;
17713
72bf9492
DJ
17714 cu->partial_dies
17715 = htab_create_alloc_ex (cu->header.length / 12,
17716 partial_die_hash,
17717 partial_die_eq,
17718 NULL,
17719 &cu->comp_unit_obstack,
17720 hashtab_obstack_allocate,
17721 dummy_obstack_deallocate);
17722
72bf9492
DJ
17723 while (1)
17724 {
685af9cd 17725 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17726
17727 /* A NULL abbrev means the end of a series of children. */
17728 if (abbrev == NULL)
17729 {
17730 if (--nesting_level == 0)
cd9983dd
YQ
17731 return first_die;
17732
72bf9492
DJ
17733 info_ptr += bytes_read;
17734 last_die = parent_die;
17735 parent_die = parent_die->die_parent;
17736 continue;
17737 }
17738
98bfdba5
PA
17739 /* Check for template arguments. We never save these; if
17740 they're seen, we just mark the parent, and go on our way. */
17741 if (parent_die != NULL
17742 && cu->language == language_cplus
17743 && (abbrev->tag == DW_TAG_template_type_param
17744 || abbrev->tag == DW_TAG_template_value_param))
17745 {
17746 parent_die->has_template_arguments = 1;
17747
17748 if (!load_all)
17749 {
17750 /* We don't need a partial DIE for the template argument. */
dee91e82 17751 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17752 continue;
17753 }
17754 }
17755
0d99eb77 17756 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17757 Skip their other children. */
17758 if (!load_all
17759 && cu->language == language_cplus
17760 && parent_die != NULL
17761 && parent_die->tag == DW_TAG_subprogram)
17762 {
dee91e82 17763 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17764 continue;
17765 }
17766
5afb4e99
DJ
17767 /* Check whether this DIE is interesting enough to save. Normally
17768 we would not be interested in members here, but there may be
17769 later variables referencing them via DW_AT_specification (for
17770 static members). */
17771 if (!load_all
17772 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17773 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17774 && abbrev->tag != DW_TAG_enumerator
17775 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17776 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17777 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17778 && abbrev->tag != DW_TAG_variable
5afb4e99 17779 && abbrev->tag != DW_TAG_namespace
f55ee35c 17780 && abbrev->tag != DW_TAG_module
95554aad 17781 && abbrev->tag != DW_TAG_member
74921315
KS
17782 && abbrev->tag != DW_TAG_imported_unit
17783 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17784 {
17785 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17786 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17787 continue;
17788 }
17789
6f06d47b
YQ
17790 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17791 abbrev);
cd9983dd 17792
48fbe735 17793 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17794
17795 /* This two-pass algorithm for processing partial symbols has a
17796 high cost in cache pressure. Thus, handle some simple cases
17797 here which cover the majority of C partial symbols. DIEs
17798 which neither have specification tags in them, nor could have
17799 specification tags elsewhere pointing at them, can simply be
17800 processed and discarded.
17801
17802 This segment is also optional; scan_partial_symbols and
17803 add_partial_symbol will handle these DIEs if we chain
17804 them in normally. When compilers which do not emit large
17805 quantities of duplicate debug information are more common,
17806 this code can probably be removed. */
17807
17808 /* Any complete simple types at the top level (pretty much all
17809 of them, for a language without namespaces), can be processed
17810 directly. */
17811 if (parent_die == NULL
cd9983dd
YQ
17812 && pdi.has_specification == 0
17813 && pdi.is_declaration == 0
17814 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17815 || pdi.tag == DW_TAG_base_type
17816 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17817 {
cd9983dd 17818 if (building_psymtab && pdi.name != NULL)
31edb802 17819 add_psymbol_to_list (pdi.name, false,
79748972 17820 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17821 psymbol_placement::STATIC,
1762568f 17822 0, cu->language, objfile);
cd9983dd 17823 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17824 continue;
17825 }
17826
d8228535
JK
17827 /* The exception for DW_TAG_typedef with has_children above is
17828 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17829 type_name_or_error will error on such types later.
d8228535
JK
17830
17831 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17832 it could not find the child DIEs referenced later, this is checked
17833 above. In correct DWARF DW_TAG_typedef should have no children. */
17834
cd9983dd 17835 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17836 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17837 "- DIE at %s [in module %s]"),
cd9983dd 17838 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17839
72bf9492
DJ
17840 /* If we're at the second level, and we're an enumerator, and
17841 our parent has no specification (meaning possibly lives in a
17842 namespace elsewhere), then we can add the partial symbol now
17843 instead of queueing it. */
cd9983dd 17844 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17845 && parent_die != NULL
17846 && parent_die->die_parent == NULL
17847 && parent_die->tag == DW_TAG_enumeration_type
17848 && parent_die->has_specification == 0)
17849 {
cd9983dd 17850 if (pdi.name == NULL)
b98664d3 17851 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17852 else if (building_psymtab)
31edb802 17853 add_psymbol_to_list (pdi.name, false,
79748972 17854 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17855 cu->language == language_cplus
75aedd27
TT
17856 ? psymbol_placement::GLOBAL
17857 : psymbol_placement::STATIC,
1762568f 17858 0, cu->language, objfile);
72bf9492 17859
cd9983dd 17860 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17861 continue;
17862 }
17863
cd9983dd 17864 struct partial_die_info *part_die
6f06d47b 17865 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17866
72bf9492
DJ
17867 /* We'll save this DIE so link it in. */
17868 part_die->die_parent = parent_die;
17869 part_die->die_sibling = NULL;
17870 part_die->die_child = NULL;
17871
17872 if (last_die && last_die == parent_die)
17873 last_die->die_child = part_die;
17874 else if (last_die)
17875 last_die->die_sibling = part_die;
17876
17877 last_die = part_die;
17878
17879 if (first_die == NULL)
17880 first_die = part_die;
17881
17882 /* Maybe add the DIE to the hash table. Not all DIEs that we
17883 find interesting need to be in the hash table, because we
17884 also have the parent/sibling/child chains; only those that we
17885 might refer to by offset later during partial symbol reading.
17886
17887 For now this means things that might have be the target of a
17888 DW_AT_specification, DW_AT_abstract_origin, or
17889 DW_AT_extension. DW_AT_extension will refer only to
17890 namespaces; DW_AT_abstract_origin refers to functions (and
17891 many things under the function DIE, but we do not recurse
17892 into function DIEs during partial symbol reading) and
17893 possibly variables as well; DW_AT_specification refers to
17894 declarations. Declarations ought to have the DW_AT_declaration
17895 flag. It happens that GCC forgets to put it in sometimes, but
17896 only for functions, not for types.
17897
17898 Adding more things than necessary to the hash table is harmless
17899 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17900 wasted time in find_partial_die, when we reread the compilation
17901 unit with load_all_dies set. */
72bf9492 17902
5afb4e99 17903 if (load_all
72929c62 17904 || abbrev->tag == DW_TAG_constant
5afb4e99 17905 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17906 || abbrev->tag == DW_TAG_variable
17907 || abbrev->tag == DW_TAG_namespace
17908 || part_die->is_declaration)
17909 {
17910 void **slot;
17911
17912 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17913 to_underlying (part_die->sect_off),
17914 INSERT);
72bf9492
DJ
17915 *slot = part_die;
17916 }
17917
72bf9492 17918 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17919 we have no reason to follow the children of structures; for other
98bfdba5
PA
17920 languages we have to, so that we can get at method physnames
17921 to infer fully qualified class names, for DW_AT_specification,
17922 and for C++ template arguments. For C++, we also look one level
17923 inside functions to find template arguments (if the name of the
17924 function does not already contain the template arguments).
bc30ff58 17925
0a4b0913
AB
17926 For Ada and Fortran, we need to scan the children of subprograms
17927 and lexical blocks as well because these languages allow the
17928 definition of nested entities that could be interesting for the
17929 debugger, such as nested subprograms for instance. */
72bf9492 17930 if (last_die->has_children
5afb4e99
DJ
17931 && (load_all
17932 || last_die->tag == DW_TAG_namespace
f55ee35c 17933 || last_die->tag == DW_TAG_module
72bf9492 17934 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17935 || (cu->language == language_cplus
17936 && last_die->tag == DW_TAG_subprogram
17937 && (last_die->name == NULL
17938 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17939 || (cu->language != language_c
17940 && (last_die->tag == DW_TAG_class_type
680b30c7 17941 || last_die->tag == DW_TAG_interface_type
72bf9492 17942 || last_die->tag == DW_TAG_structure_type
bc30ff58 17943 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17944 || ((cu->language == language_ada
17945 || cu->language == language_fortran)
bc30ff58
JB
17946 && (last_die->tag == DW_TAG_subprogram
17947 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17948 {
17949 nesting_level++;
17950 parent_die = last_die;
17951 continue;
17952 }
17953
17954 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17955 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17956
17957 /* Back to the top, do it again. */
17958 }
17959}
17960
6f06d47b
YQ
17961partial_die_info::partial_die_info (sect_offset sect_off_,
17962 struct abbrev_info *abbrev)
17963 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17964{
17965}
17966
35cc7ed7
YQ
17967/* Read a minimal amount of information into the minimal die structure.
17968 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17969
48fbe735
YQ
17970const gdb_byte *
17971partial_die_info::read (const struct die_reader_specs *reader,
17972 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17973{
dee91e82 17974 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17975 struct dwarf2_per_objfile *dwarf2_per_objfile
17976 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17977 unsigned int i;
c5aa993b 17978 int has_low_pc_attr = 0;
c906108c 17979 int has_high_pc_attr = 0;
91da1414 17980 int high_pc_relative = 0;
c906108c 17981
fd0a254f 17982 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17983 {
e7da7f8f 17984 attribute attr;
18a8505e 17985 bool need_reprocess;
e7da7f8f 17986 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
17987 info_ptr, &need_reprocess);
17988 /* String and address offsets that need to do the reprocessing have
17989 already been read at this point, so there is no need to wait until
17990 the loop terminates to do the reprocessing. */
17991 if (need_reprocess)
e7da7f8f 17992 read_attribute_reprocess (reader, &attr);
c906108c 17993 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17994 partial symbol table. */
c906108c
SS
17995 switch (attr.name)
17996 {
17997 case DW_AT_name:
48fbe735 17998 switch (tag)
71c25dea
TT
17999 {
18000 case DW_TAG_compile_unit:
95554aad 18001 case DW_TAG_partial_unit:
348e048f 18002 case DW_TAG_type_unit:
71c25dea
TT
18003 /* Compilation units have a DW_AT_name that is a filename, not
18004 a source language identifier. */
18005 case DW_TAG_enumeration_type:
18006 case DW_TAG_enumerator:
18007 /* These tags always have simple identifiers already; no need
18008 to canonicalize them. */
48fbe735 18009 name = DW_STRING (&attr);
71c25dea
TT
18010 break;
18011 default:
48fbe735
YQ
18012 {
18013 struct objfile *objfile = dwarf2_per_objfile->objfile;
18014
18015 name
be1e3d3e 18016 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18017 }
71c25dea
TT
18018 break;
18019 }
c906108c 18020 break;
31ef98ae 18021 case DW_AT_linkage_name:
c906108c 18022 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18023 /* Note that both forms of linkage name might appear. We
18024 assume they will be the same, and we only store the last
18025 one we see. */
48fbe735 18026 linkage_name = DW_STRING (&attr);
c906108c
SS
18027 break;
18028 case DW_AT_low_pc:
18029 has_low_pc_attr = 1;
cd6c91b4 18030 lowpc = attr.value_as_address ();
c906108c
SS
18031 break;
18032 case DW_AT_high_pc:
18033 has_high_pc_attr = 1;
cd6c91b4
TT
18034 highpc = attr.value_as_address ();
18035 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18036 high_pc_relative = 1;
c906108c
SS
18037 break;
18038 case DW_AT_location:
0963b4bd 18039 /* Support the .debug_loc offsets. */
4fc6c0d5 18040 if (attr.form_is_block ())
8e19ed76 18041 {
48fbe735 18042 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18043 }
cd6c91b4 18044 else if (attr.form_is_section_offset ())
8e19ed76 18045 {
4d3c2250 18046 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18047 }
18048 else
18049 {
4d3c2250
KB
18050 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18051 "partial symbol information");
8e19ed76 18052 }
c906108c 18053 break;
c906108c 18054 case DW_AT_external:
48fbe735 18055 is_external = DW_UNSND (&attr);
c906108c
SS
18056 break;
18057 case DW_AT_declaration:
48fbe735 18058 is_declaration = DW_UNSND (&attr);
c906108c
SS
18059 break;
18060 case DW_AT_type:
48fbe735 18061 has_type = 1;
c906108c
SS
18062 break;
18063 case DW_AT_abstract_origin:
18064 case DW_AT_specification:
72bf9492 18065 case DW_AT_extension:
48fbe735 18066 has_specification = 1;
0826b30a 18067 spec_offset = attr.get_ref_die_offset ();
48fbe735 18068 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18069 || cu->per_cu->is_dwz);
c906108c
SS
18070 break;
18071 case DW_AT_sibling:
18072 /* Ignore absolute siblings, they might point outside of
18073 the current compile unit. */
18074 if (attr.form == DW_FORM_ref_addr)
b98664d3 18075 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18076 else
b9502d3f 18077 {
48fbe735 18078 const gdb_byte *buffer = reader->buffer;
0826b30a 18079 sect_offset off = attr.get_ref_die_offset ();
9c541725 18080 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18081
18082 if (sibling_ptr < info_ptr)
b98664d3 18083 complaint (_("DW_AT_sibling points backwards"));
22869d73 18084 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18085 reader->die_section->overflow_complaint ();
b9502d3f 18086 else
48fbe735 18087 sibling = sibling_ptr;
b9502d3f 18088 }
c906108c 18089 break;
fa4028e9 18090 case DW_AT_byte_size:
48fbe735 18091 has_byte_size = 1;
fa4028e9 18092 break;
ff908ebf 18093 case DW_AT_const_value:
48fbe735 18094 has_const_value = 1;
ff908ebf 18095 break;
68511cec
CES
18096 case DW_AT_calling_convention:
18097 /* DWARF doesn't provide a way to identify a program's source-level
18098 entry point. DW_AT_calling_convention attributes are only meant
18099 to describe functions' calling conventions.
18100
18101 However, because it's a necessary piece of information in
0c1b455e
TT
18102 Fortran, and before DWARF 4 DW_CC_program was the only
18103 piece of debugging information whose definition refers to
18104 a 'main program' at all, several compilers marked Fortran
18105 main programs with DW_CC_program --- even when those
18106 functions use the standard calling conventions.
18107
18108 Although DWARF now specifies a way to provide this
18109 information, we support this practice for backward
18110 compatibility. */
68511cec 18111 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18112 && cu->language == language_fortran)
48fbe735 18113 main_subprogram = 1;
68511cec 18114 break;
481860b3
GB
18115 case DW_AT_inline:
18116 if (DW_UNSND (&attr) == DW_INL_inlined
18117 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18118 may_be_inlined = 1;
481860b3 18119 break;
95554aad
TT
18120
18121 case DW_AT_import:
48fbe735 18122 if (tag == DW_TAG_imported_unit)
36586728 18123 {
0826b30a 18124 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18125 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18126 || cu->per_cu->is_dwz);
18127 }
95554aad
TT
18128 break;
18129
0c1b455e 18130 case DW_AT_main_subprogram:
48fbe735 18131 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18132 break;
18133
05caa1d2
TT
18134 case DW_AT_ranges:
18135 {
18136 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18137 but that requires a full DIE, so instead we just
18138 reimplement it. */
18139 int need_ranges_base = tag != DW_TAG_compile_unit;
18140 unsigned int ranges_offset = (DW_UNSND (&attr)
18141 + (need_ranges_base
18142 ? cu->ranges_base
18143 : 0));
18144
18145 /* Value of the DW_AT_ranges attribute is the offset in the
18146 .debug_ranges section. */
18147 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18148 nullptr))
18149 has_pc_info = 1;
18150 }
18151 break;
18152
c906108c
SS
18153 default:
18154 break;
18155 }
18156 }
18157
10d06d82
TT
18158 /* For Ada, if both the name and the linkage name appear, we prefer
18159 the latter. This lets "catch exception" work better, regardless
18160 of the order in which the name and linkage name were emitted.
18161 Really, though, this is just a workaround for the fact that gdb
18162 doesn't store both the name and the linkage name. */
18163 if (cu->language == language_ada && linkage_name != nullptr)
18164 name = linkage_name;
18165
91da1414 18166 if (high_pc_relative)
48fbe735 18167 highpc += lowpc;
91da1414 18168
9373cf26
JK
18169 if (has_low_pc_attr && has_high_pc_attr)
18170 {
18171 /* When using the GNU linker, .gnu.linkonce. sections are used to
18172 eliminate duplicate copies of functions and vtables and such.
18173 The linker will arbitrarily choose one and discard the others.
18174 The AT_*_pc values for such functions refer to local labels in
18175 these sections. If the section from that file was discarded, the
18176 labels are not in the output, so the relocs get a value of 0.
18177 If this is a discarded function, mark the pc bounds as invalid,
18178 so that GDB will ignore it. */
48fbe735 18179 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18180 {
48fbe735 18181 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18182 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18183
b98664d3 18184 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18185 "for DIE at %s [in module %s]"),
48fbe735
YQ
18186 paddress (gdbarch, lowpc),
18187 sect_offset_str (sect_off),
9d8780f0 18188 objfile_name (objfile));
9373cf26
JK
18189 }
18190 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18191 else if (lowpc >= highpc)
9373cf26 18192 {
48fbe735 18193 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18194 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18195
b98664d3 18196 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18197 "for DIE at %s [in module %s]"),
48fbe735
YQ
18198 paddress (gdbarch, lowpc),
18199 paddress (gdbarch, highpc),
18200 sect_offset_str (sect_off),
9c541725 18201 objfile_name (objfile));
9373cf26
JK
18202 }
18203 else
48fbe735 18204 has_pc_info = 1;
9373cf26 18205 }
85cbf3d3 18206
c906108c
SS
18207 return info_ptr;
18208}
18209
72bf9492
DJ
18210/* Find a cached partial DIE at OFFSET in CU. */
18211
d590ff25
YQ
18212struct partial_die_info *
18213dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18214{
18215 struct partial_die_info *lookup_die = NULL;
6f06d47b 18216 struct partial_die_info part_die (sect_off);
72bf9492 18217
9a3c8263 18218 lookup_die = ((struct partial_die_info *)
d590ff25 18219 htab_find_with_hash (partial_dies, &part_die,
9c541725 18220 to_underlying (sect_off)));
72bf9492 18221
72bf9492
DJ
18222 return lookup_die;
18223}
18224
348e048f
DE
18225/* Find a partial DIE at OFFSET, which may or may not be in CU,
18226 except in the case of .debug_types DIEs which do not reference
18227 outside their CU (they do however referencing other types via
55f1336d 18228 DW_FORM_ref_sig8). */
72bf9492 18229
122cf0f2 18230static const struct cu_partial_die_info
9c541725 18231find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18232{
518817b3
SM
18233 struct dwarf2_per_objfile *dwarf2_per_objfile
18234 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18235 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18236 struct dwarf2_per_cu_data *per_cu = NULL;
18237 struct partial_die_info *pd = NULL;
72bf9492 18238
36586728 18239 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18240 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18241 {
d590ff25 18242 pd = cu->find_partial_die (sect_off);
5afb4e99 18243 if (pd != NULL)
fb816e8b 18244 return { cu, pd };
0d99eb77
DE
18245 /* We missed recording what we needed.
18246 Load all dies and try again. */
18247 per_cu = cu->per_cu;
5afb4e99 18248 }
0d99eb77
DE
18249 else
18250 {
18251 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18252 if (cu->per_cu->is_debug_types)
0d99eb77 18253 {
9d8780f0
SM
18254 error (_("Dwarf Error: Type Unit at offset %s contains"
18255 " external reference to offset %s [in module %s].\n"),
18256 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18257 bfd_get_filename (objfile->obfd));
18258 }
9c541725 18259 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18260 dwarf2_per_objfile);
72bf9492 18261
0d99eb77
DE
18262 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18263 load_partial_comp_unit (per_cu);
ae038cb0 18264
0d99eb77 18265 per_cu->cu->last_used = 0;
d590ff25 18266 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18267 }
5afb4e99 18268
dee91e82
DE
18269 /* If we didn't find it, and not all dies have been loaded,
18270 load them all and try again. */
18271
5afb4e99
DJ
18272 if (pd == NULL && per_cu->load_all_dies == 0)
18273 {
5afb4e99 18274 per_cu->load_all_dies = 1;
fd820528
DE
18275
18276 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18277 THIS_CU->cu may already be in use. So we can't just free it and
18278 replace its DIEs with the ones we read in. Instead, we leave those
18279 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18280 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18281 set. */
dee91e82 18282 load_partial_comp_unit (per_cu);
5afb4e99 18283
d590ff25 18284 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18285 }
18286
18287 if (pd == NULL)
18288 internal_error (__FILE__, __LINE__,
9d8780f0 18289 _("could not find partial DIE %s "
3e43a32a 18290 "in cache [from module %s]\n"),
9d8780f0 18291 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18292 return { per_cu->cu, pd };
72bf9492
DJ
18293}
18294
abc72ce4
DE
18295/* See if we can figure out if the class lives in a namespace. We do
18296 this by looking for a member function; its demangled name will
18297 contain namespace info, if there is any. */
18298
18299static void
18300guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18301 struct dwarf2_cu *cu)
18302{
18303 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18304 what template types look like, because the demangler
18305 frequently doesn't give the same name as the debug info. We
18306 could fix this by only using the demangled name to get the
18307 prefix (but see comment in read_structure_type). */
18308
18309 struct partial_die_info *real_pdi;
18310 struct partial_die_info *child_pdi;
18311
18312 /* If this DIE (this DIE's specification, if any) has a parent, then
18313 we should not do this. We'll prepend the parent's fully qualified
18314 name when we create the partial symbol. */
18315
18316 real_pdi = struct_pdi;
18317 while (real_pdi->has_specification)
fb816e8b 18318 {
122cf0f2
AB
18319 auto res = find_partial_die (real_pdi->spec_offset,
18320 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18321 real_pdi = res.pdi;
18322 cu = res.cu;
18323 }
abc72ce4
DE
18324
18325 if (real_pdi->die_parent != NULL)
18326 return;
18327
18328 for (child_pdi = struct_pdi->die_child;
18329 child_pdi != NULL;
18330 child_pdi = child_pdi->die_sibling)
18331 {
18332 if (child_pdi->tag == DW_TAG_subprogram
18333 && child_pdi->linkage_name != NULL)
18334 {
43816ebc
TT
18335 gdb::unique_xmalloc_ptr<char> actual_class_name
18336 (language_class_name_from_physname (cu->language_defn,
18337 child_pdi->linkage_name));
abc72ce4
DE
18338 if (actual_class_name != NULL)
18339 {
518817b3 18340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18341 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18342 }
18343 break;
18344 }
18345 }
18346}
18347
25c11aca
TV
18348/* Return true if a DIE with TAG may have the DW_AT_const_value
18349 attribute. */
18350
18351static bool
18352can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18353{
18354 switch (tag)
18355 {
18356 case DW_TAG_constant:
18357 case DW_TAG_enumerator:
18358 case DW_TAG_formal_parameter:
18359 case DW_TAG_template_value_param:
18360 case DW_TAG_variable:
18361 return true;
18362 }
18363
18364 return false;
18365}
18366
52356b79
YQ
18367void
18368partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18369{
abc72ce4
DE
18370 /* Once we've fixed up a die, there's no point in doing so again.
18371 This also avoids a memory leak if we were to call
18372 guess_partial_die_structure_name multiple times. */
52356b79 18373 if (fixup_called)
abc72ce4
DE
18374 return;
18375
72bf9492
DJ
18376 /* If we found a reference attribute and the DIE has no name, try
18377 to find a name in the referred to DIE. */
18378
52356b79 18379 if (name == NULL && has_specification)
72bf9492
DJ
18380 {
18381 struct partial_die_info *spec_die;
72bf9492 18382
122cf0f2 18383 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18384 spec_die = res.pdi;
18385 cu = res.cu;
72bf9492 18386
52356b79 18387 spec_die->fixup (cu);
72bf9492
DJ
18388
18389 if (spec_die->name)
18390 {
52356b79 18391 name = spec_die->name;
72bf9492
DJ
18392
18393 /* Copy DW_AT_external attribute if it is set. */
18394 if (spec_die->is_external)
52356b79 18395 is_external = spec_die->is_external;
72bf9492
DJ
18396 }
18397 }
18398
25c11aca
TV
18399 if (!has_const_value && has_specification
18400 && can_have_DW_AT_const_value_p (tag))
18401 {
18402 struct partial_die_info *spec_die;
18403
18404 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18405 spec_die = res.pdi;
18406 cu = res.cu;
18407
18408 spec_die->fixup (cu);
18409
18410 if (spec_die->has_const_value)
18411 {
18412 /* Copy DW_AT_const_value attribute if it is set. */
18413 has_const_value = spec_die->has_const_value;
18414 }
18415 }
18416
72bf9492 18417 /* Set default names for some unnamed DIEs. */
72bf9492 18418
52356b79
YQ
18419 if (name == NULL && tag == DW_TAG_namespace)
18420 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18421
abc72ce4
DE
18422 /* If there is no parent die to provide a namespace, and there are
18423 children, see if we can determine the namespace from their linkage
122d1940 18424 name. */
abc72ce4 18425 if (cu->language == language_cplus
fd5866f6 18426 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18427 && die_parent == NULL
18428 && has_children
18429 && (tag == DW_TAG_class_type
18430 || tag == DW_TAG_structure_type
18431 || tag == DW_TAG_union_type))
18432 guess_partial_die_structure_name (this, cu);
abc72ce4 18433
53832f31
TT
18434 /* GCC might emit a nameless struct or union that has a linkage
18435 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18436 if (name == NULL
18437 && (tag == DW_TAG_class_type
18438 || tag == DW_TAG_interface_type
18439 || tag == DW_TAG_structure_type
18440 || tag == DW_TAG_union_type)
18441 && linkage_name != NULL)
53832f31 18442 {
43816ebc
TT
18443 gdb::unique_xmalloc_ptr<char> demangled
18444 (gdb_demangle (linkage_name, DMGL_TYPES));
18445 if (demangled != nullptr)
53832f31 18446 {
96408a79
SA
18447 const char *base;
18448
18449 /* Strip any leading namespaces/classes, keep only the base name.
18450 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18451 base = strrchr (demangled.get (), ':');
18452 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18453 base++;
18454 else
43816ebc 18455 base = demangled.get ();
96408a79 18456
518817b3 18457 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18458 name = objfile->intern (base);
53832f31
TT
18459 }
18460 }
18461
52356b79 18462 fixup_called = 1;
72bf9492
DJ
18463}
18464
41144253 18465/* Read the .debug_loclists header contents from the given SECTION in the
18466 HEADER. */
18467static void
18468read_loclist_header (struct loclist_header *header,
18469 struct dwarf2_section_info *section)
18470{
18471 unsigned int bytes_read;
18472 bfd *abfd = section->get_bfd_owner ();
18473 const gdb_byte *info_ptr = section->buffer;
18474 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18475 info_ptr += bytes_read;
18476 header->version = read_2_bytes (abfd, info_ptr);
18477 info_ptr += 2;
18478 header->addr_size = read_1_byte (abfd, info_ptr);
18479 info_ptr += 1;
18480 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18481 info_ptr += 1;
18482 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18483}
18484
18485/* Return the DW_AT_loclists_base value for the CU. */
18486static ULONGEST
18487lookup_loclist_base (struct dwarf2_cu *cu)
18488{
18489 /* For the .dwo unit, the loclist_base points to the first offset following
18490 the header. The header consists of the following entities-
18491 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18492 bit format)
18493 2. version (2 bytes)
18494 3. address size (1 byte)
18495 4. segment selector size (1 byte)
18496 5. offset entry count (4 bytes)
18497 These sizes are derived as per the DWARFv5 standard. */
18498 if (cu->dwo_unit != nullptr)
18499 {
18500 if (cu->header.initial_length_size == 4)
18501 return LOCLIST_HEADER_SIZE32;
18502 return LOCLIST_HEADER_SIZE64;
18503 }
18504 return cu->loclist_base;
18505}
18506
18507/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18508 array of offsets in the .debug_loclists section. */
18509static CORE_ADDR
18510read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18511{
18512 struct dwarf2_per_objfile *dwarf2_per_objfile
18513 = cu->per_cu->dwarf2_per_objfile;
18514 struct objfile *objfile = dwarf2_per_objfile->objfile;
18515 bfd *abfd = objfile->obfd;
18516 ULONGEST loclist_base = lookup_loclist_base (cu);
18517 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18518
18519 section->read (objfile);
18520 if (section->buffer == NULL)
18521 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18522 "section [in module %s]"), objfile_name (objfile));
18523 struct loclist_header header;
18524 read_loclist_header (&header, section);
18525 if (loclist_index >= header.offset_entry_count)
18526 complaint (_("DW_FORM_loclistx pointing outside of "
18527 ".debug_loclists offset array [in module %s]"),
18528 objfile_name (objfile));
18529 if (loclist_base + loclist_index * cu->header.offset_size
18530 >= section->size)
18531 complaint (_("DW_FORM_loclistx pointing outside of "
18532 ".debug_loclists section [in module %s]"),
18533 objfile_name (objfile));
18534 const gdb_byte *info_ptr
18535 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18536
18537 if (cu->header.offset_size == 4)
18538 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18539 else
18540 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18541}
18542
18a8505e
AT
18543/* Process the attributes that had to be skipped in the first round. These
18544 attributes are the ones that need str_offsets_base or addr_base attributes.
18545 They could not have been processed in the first round, because at the time
18546 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18547static void
18548read_attribute_reprocess (const struct die_reader_specs *reader,
18549 struct attribute *attr)
18a8505e
AT
18550{
18551 struct dwarf2_cu *cu = reader->cu;
18552 switch (attr->form)
18553 {
18554 case DW_FORM_addrx:
18555 case DW_FORM_GNU_addr_index:
18556 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18557 break;
41144253 18558 case DW_FORM_loclistx:
18559 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18560 break;
18a8505e
AT
18561 case DW_FORM_strx:
18562 case DW_FORM_strx1:
18563 case DW_FORM_strx2:
18564 case DW_FORM_strx3:
18565 case DW_FORM_strx4:
18566 case DW_FORM_GNU_str_index:
18567 {
18568 unsigned int str_index = DW_UNSND (attr);
18569 if (reader->dwo_file != NULL)
18570 {
18571 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18572 DW_STRING_IS_CANONICAL (attr) = 0;
18573 }
18574 else
18575 {
18576 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18577 DW_STRING_IS_CANONICAL (attr) = 0;
18578 }
18579 break;
18580 }
18581 default:
18582 gdb_assert_not_reached (_("Unexpected DWARF form."));
18583 }
18584}
18585
a8329558 18586/* Read an attribute value described by an attribute form. */
c906108c 18587
d521ce57 18588static const gdb_byte *
dee91e82
DE
18589read_attribute_value (const struct die_reader_specs *reader,
18590 struct attribute *attr, unsigned form,
18a8505e
AT
18591 LONGEST implicit_const, const gdb_byte *info_ptr,
18592 bool *need_reprocess)
c906108c 18593{
dee91e82 18594 struct dwarf2_cu *cu = reader->cu;
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;
dee91e82 18598 bfd *abfd = reader->abfd;
e7c27a73 18599 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18600 unsigned int bytes_read;
18601 struct dwarf_block *blk;
18a8505e 18602 *need_reprocess = false;
c906108c 18603
aead7601 18604 attr->form = (enum dwarf_form) form;
a8329558 18605 switch (form)
c906108c 18606 {
c906108c 18607 case DW_FORM_ref_addr:
ae411497 18608 if (cu->header.version == 2)
c8a7a66f
TT
18609 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18610 &bytes_read);
ae411497 18611 else
8266302d
TT
18612 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18613 &bytes_read);
ae411497
TT
18614 info_ptr += bytes_read;
18615 break;
36586728 18616 case DW_FORM_GNU_ref_alt:
8266302d 18617 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18618 info_ptr += bytes_read;
18619 break;
ae411497 18620 case DW_FORM_addr:
08feed99
TT
18621 {
18622 struct gdbarch *gdbarch = objfile->arch ();
18623 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18624 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18625 info_ptr += bytes_read;
18626 }
c906108c
SS
18627 break;
18628 case DW_FORM_block2:
7b5a2f43 18629 blk = dwarf_alloc_block (cu);
c906108c
SS
18630 blk->size = read_2_bytes (abfd, info_ptr);
18631 info_ptr += 2;
18632 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18633 info_ptr += blk->size;
18634 DW_BLOCK (attr) = blk;
18635 break;
18636 case DW_FORM_block4:
7b5a2f43 18637 blk = dwarf_alloc_block (cu);
c906108c
SS
18638 blk->size = read_4_bytes (abfd, info_ptr);
18639 info_ptr += 4;
18640 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18641 info_ptr += blk->size;
18642 DW_BLOCK (attr) = blk;
18643 break;
18644 case DW_FORM_data2:
18645 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18646 info_ptr += 2;
18647 break;
18648 case DW_FORM_data4:
18649 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18650 info_ptr += 4;
18651 break;
18652 case DW_FORM_data8:
18653 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18654 info_ptr += 8;
18655 break;
0224619f
JK
18656 case DW_FORM_data16:
18657 blk = dwarf_alloc_block (cu);
18658 blk->size = 16;
18659 blk->data = read_n_bytes (abfd, info_ptr, 16);
18660 info_ptr += 16;
18661 DW_BLOCK (attr) = blk;
18662 break;
2dc7f7b3 18663 case DW_FORM_sec_offset:
8266302d 18664 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18665 info_ptr += bytes_read;
18666 break;
41144253 18667 case DW_FORM_loclistx:
18668 {
18669 *need_reprocess = true;
18670 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18671 info_ptr += bytes_read;
18672 }
18673 break;
c906108c 18674 case DW_FORM_string:
9b1c24c8 18675 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18676 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18677 info_ptr += bytes_read;
18678 break;
4bdf3d34 18679 case DW_FORM_strp:
36586728
TT
18680 if (!cu->per_cu->is_dwz)
18681 {
ed2dc618
SM
18682 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18683 abfd, info_ptr, cu_header,
36586728
TT
18684 &bytes_read);
18685 DW_STRING_IS_CANONICAL (attr) = 0;
18686 info_ptr += bytes_read;
18687 break;
18688 }
18689 /* FALLTHROUGH */
43988095
JK
18690 case DW_FORM_line_strp:
18691 if (!cu->per_cu->is_dwz)
18692 {
86c0bb4c
TT
18693 DW_STRING (attr)
18694 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18695 &bytes_read);
43988095
JK
18696 DW_STRING_IS_CANONICAL (attr) = 0;
18697 info_ptr += bytes_read;
18698 break;
18699 }
18700 /* FALLTHROUGH */
36586728
TT
18701 case DW_FORM_GNU_strp_alt:
18702 {
ed2dc618 18703 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18704 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18705 &bytes_read);
36586728 18706
0314b390 18707 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18708 DW_STRING_IS_CANONICAL (attr) = 0;
18709 info_ptr += bytes_read;
18710 }
4bdf3d34 18711 break;
2dc7f7b3 18712 case DW_FORM_exprloc:
c906108c 18713 case DW_FORM_block:
7b5a2f43 18714 blk = dwarf_alloc_block (cu);
c906108c
SS
18715 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18716 info_ptr += bytes_read;
18717 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18718 info_ptr += blk->size;
18719 DW_BLOCK (attr) = blk;
18720 break;
18721 case DW_FORM_block1:
7b5a2f43 18722 blk = dwarf_alloc_block (cu);
c906108c
SS
18723 blk->size = read_1_byte (abfd, info_ptr);
18724 info_ptr += 1;
18725 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18726 info_ptr += blk->size;
18727 DW_BLOCK (attr) = blk;
18728 break;
18729 case DW_FORM_data1:
18730 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18731 info_ptr += 1;
18732 break;
18733 case DW_FORM_flag:
18734 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18735 info_ptr += 1;
18736 break;
2dc7f7b3
TT
18737 case DW_FORM_flag_present:
18738 DW_UNSND (attr) = 1;
18739 break;
c906108c
SS
18740 case DW_FORM_sdata:
18741 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18742 info_ptr += bytes_read;
18743 break;
18744 case DW_FORM_udata:
18a8505e 18745 case DW_FORM_rnglistx:
c906108c
SS
18746 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18747 info_ptr += bytes_read;
18748 break;
18749 case DW_FORM_ref1:
9c541725 18750 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18751 + read_1_byte (abfd, info_ptr));
c906108c
SS
18752 info_ptr += 1;
18753 break;
18754 case DW_FORM_ref2:
9c541725 18755 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18756 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18757 info_ptr += 2;
18758 break;
18759 case DW_FORM_ref4:
9c541725 18760 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18761 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18762 info_ptr += 4;
18763 break;
613e1657 18764 case DW_FORM_ref8:
9c541725 18765 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18766 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18767 info_ptr += 8;
18768 break;
55f1336d 18769 case DW_FORM_ref_sig8:
ac9ec31b 18770 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18771 info_ptr += 8;
18772 break;
c906108c 18773 case DW_FORM_ref_udata:
9c541725 18774 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18775 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18776 info_ptr += bytes_read;
18777 break;
c906108c 18778 case DW_FORM_indirect:
a8329558
KW
18779 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18780 info_ptr += bytes_read;
43988095
JK
18781 if (form == DW_FORM_implicit_const)
18782 {
18783 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18784 info_ptr += bytes_read;
18785 }
18786 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18787 info_ptr, need_reprocess);
43988095
JK
18788 break;
18789 case DW_FORM_implicit_const:
18790 DW_SND (attr) = implicit_const;
a8329558 18791 break;
336d760d 18792 case DW_FORM_addrx:
3019eac3 18793 case DW_FORM_GNU_addr_index:
18a8505e
AT
18794 *need_reprocess = true;
18795 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18796 info_ptr += bytes_read;
18797 break;
cf532bd1 18798 case DW_FORM_strx:
15f18d14
AT
18799 case DW_FORM_strx1:
18800 case DW_FORM_strx2:
18801 case DW_FORM_strx3:
18802 case DW_FORM_strx4:
3019eac3 18803 case DW_FORM_GNU_str_index:
3019eac3 18804 {
15f18d14
AT
18805 ULONGEST str_index;
18806 if (form == DW_FORM_strx1)
18807 {
18808 str_index = read_1_byte (abfd, info_ptr);
18809 info_ptr += 1;
18810 }
18811 else if (form == DW_FORM_strx2)
18812 {
18813 str_index = read_2_bytes (abfd, info_ptr);
18814 info_ptr += 2;
18815 }
18816 else if (form == DW_FORM_strx3)
18817 {
18818 str_index = read_3_bytes (abfd, info_ptr);
18819 info_ptr += 3;
18820 }
18821 else if (form == DW_FORM_strx4)
18822 {
18823 str_index = read_4_bytes (abfd, info_ptr);
18824 info_ptr += 4;
18825 }
18826 else
18827 {
18828 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18829 info_ptr += bytes_read;
18830 }
18a8505e
AT
18831 *need_reprocess = true;
18832 DW_UNSND (attr) = str_index;
18833 }
3019eac3 18834 break;
c906108c 18835 default:
8a3fe4f8 18836 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18837 dwarf_form_name (form),
18838 bfd_get_filename (abfd));
c906108c 18839 }
28e94949 18840
36586728 18841 /* Super hack. */
cd6c91b4 18842 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18843 attr->form = DW_FORM_GNU_ref_alt;
18844
28e94949
JB
18845 /* We have seen instances where the compiler tried to emit a byte
18846 size attribute of -1 which ended up being encoded as an unsigned
18847 0xffffffff. Although 0xffffffff is technically a valid size value,
18848 an object of this size seems pretty unlikely so we can relatively
18849 safely treat these cases as if the size attribute was invalid and
18850 treat them as zero by default. */
18851 if (attr->name == DW_AT_byte_size
18852 && form == DW_FORM_data4
18853 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18854 {
18855 complaint
b98664d3 18856 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18857 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18858 DW_UNSND (attr) = 0;
18859 }
28e94949 18860
c906108c
SS
18861 return info_ptr;
18862}
18863
a8329558
KW
18864/* Read an attribute described by an abbreviated attribute. */
18865
d521ce57 18866static const gdb_byte *
dee91e82
DE
18867read_attribute (const struct die_reader_specs *reader,
18868 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18869 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18870{
18871 attr->name = abbrev->name;
43988095 18872 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18873 abbrev->implicit_const, info_ptr,
18874 need_reprocess);
a8329558
KW
18875}
18876
43988095
JK
18877/* Return pointer to string at .debug_str offset STR_OFFSET. */
18878
18879static const char *
ed2dc618 18880read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18881 LONGEST str_offset)
43988095 18882{
4f44ae6c
TT
18883 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18884 str_offset, "DW_FORM_strp");
c906108c
SS
18885}
18886
43988095
JK
18887/* Return pointer to string at .debug_str offset as read from BUF.
18888 BUF is assumed to be in a compilation unit described by CU_HEADER.
18889 Return *BYTES_READ_PTR count of bytes read from BUF. */
18890
d521ce57 18891static const char *
ed2dc618
SM
18892read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18893 const gdb_byte *buf,
cf2c3c16
TT
18894 const struct comp_unit_head *cu_header,
18895 unsigned int *bytes_read_ptr)
18896{
8266302d 18897 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18898
4f44ae6c 18899 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18900}
18901
86c0bb4c 18902/* See read.h. */
43988095 18903
86c0bb4c
TT
18904const char *
18905dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18906 const struct comp_unit_head *cu_header,
18907 unsigned int *bytes_read_ptr)
18908{
86c0bb4c 18909 bfd *abfd = objfile->obfd;
8266302d 18910 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18911
86c0bb4c 18912 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18913}
18914
3019eac3 18915/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18916 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18917 ADDR_SIZE is the size of addresses from the CU header. */
18918
18919static CORE_ADDR
ed2dc618 18920read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18921 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18922 int addr_size)
3019eac3
DE
18923{
18924 struct objfile *objfile = dwarf2_per_objfile->objfile;
18925 bfd *abfd = objfile->obfd;
18926 const gdb_byte *info_ptr;
18a8505e 18927 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18928
96b79293 18929 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18930 if (dwarf2_per_objfile->addr.buffer == NULL)
18931 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18932 objfile_name (objfile));
18a8505e
AT
18933 if (addr_base_or_zero + addr_index * addr_size
18934 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18935 error (_("DW_FORM_addr_index pointing outside of "
18936 ".debug_addr section [in module %s]"),
4262abfb 18937 objfile_name (objfile));
3019eac3 18938 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18939 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18940 if (addr_size == 4)
18941 return bfd_get_32 (abfd, info_ptr);
18942 else
18943 return bfd_get_64 (abfd, info_ptr);
18944}
18945
18946/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18947
18948static CORE_ADDR
18949read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18950{
518817b3
SM
18951 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18952 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18953}
18954
18955/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18956
18957static CORE_ADDR
d521ce57 18958read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18959 unsigned int *bytes_read)
18960{
518817b3 18961 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18962 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18963
18964 return read_addr_index (cu, addr_index);
18965}
18966
450a1bfc 18967/* See read.h. */
3019eac3
DE
18968
18969CORE_ADDR
450a1bfc 18970dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18971{
ed2dc618 18972 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18973 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18974 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18975 int addr_size;
18976
3019eac3
DE
18977 /* We need addr_base and addr_size.
18978 If we don't have PER_CU->cu, we have to get it.
18979 Nasty, but the alternative is storing the needed info in PER_CU,
18980 which at this point doesn't seem justified: it's not clear how frequently
18981 it would get used and it would increase the size of every PER_CU.
18982 Entry points like dwarf2_per_cu_addr_size do a similar thing
18983 so we're not in uncharted territory here.
18984 Alas we need to be a bit more complicated as addr_base is contained
18985 in the DIE.
18986
18987 We don't need to read the entire CU(/TU).
18988 We just need the header and top level die.
a1b64ce1 18989
3019eac3 18990 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18991 For now we skip this optimization. */
3019eac3
DE
18992
18993 if (cu != NULL)
18994 {
18995 addr_base = cu->addr_base;
18996 addr_size = cu->header.addr_size;
18997 }
18998 else
18999 {
6751ebae 19000 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19001 addr_base = reader.cu->addr_base;
19002 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19003 }
19004
ed2dc618
SM
19005 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19006 addr_size);
3019eac3
DE
19007}
19008
18a8505e
AT
19009/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19010 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19011 DWO file. */
3019eac3 19012
d521ce57 19013static const char *
18a8505e
AT
19014read_str_index (struct dwarf2_cu *cu,
19015 struct dwarf2_section_info *str_section,
19016 struct dwarf2_section_info *str_offsets_section,
19017 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19018{
518817b3
SM
19019 struct dwarf2_per_objfile *dwarf2_per_objfile
19020 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19021 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19022 const char *objf_name = objfile_name (objfile);
3019eac3 19023 bfd *abfd = objfile->obfd;
d521ce57 19024 const gdb_byte *info_ptr;
3019eac3 19025 ULONGEST str_offset;
cf532bd1 19026 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19027
96b79293
TT
19028 str_section->read (objfile);
19029 str_offsets_section->read (objfile);
73869dc2 19030 if (str_section->buffer == NULL)
18a8505e 19031 error (_("%s used without %s section"
9d8780f0 19032 " in CU at offset %s [in module %s]"),
96b79293 19033 form_name, str_section->get_name (),
18a8505e 19034 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19035 if (str_offsets_section->buffer == NULL)
18a8505e 19036 error (_("%s used without %s section"
9d8780f0 19037 " in CU at offset %s [in module %s]"),
96b79293 19038 form_name, str_section->get_name (),
18a8505e 19039 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19040 info_ptr = (str_offsets_section->buffer
18a8505e 19041 + str_offsets_base
3019eac3
DE
19042 + str_index * cu->header.offset_size);
19043 if (cu->header.offset_size == 4)
19044 str_offset = bfd_get_32 (abfd, info_ptr);
19045 else
19046 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19047 if (str_offset >= str_section->size)
57d63ce2 19048 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19049 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19050 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19051 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19052}
19053
18a8505e
AT
19054/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19055
19056static const char *
19057read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19058{
19059 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19060 ? reader->cu->header.addr_size : 0;
19061 return read_str_index (reader->cu,
19062 &reader->dwo_file->sections.str,
19063 &reader->dwo_file->sections.str_offsets,
19064 str_offsets_base, str_index);
19065}
19066
19067/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19068
19069static const char *
19070read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19071{
19072 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19073 const char *objf_name = objfile_name (objfile);
19074 static const char form_name[] = "DW_FORM_GNU_str_index";
19075 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19076
19077 if (!cu->str_offsets_base.has_value ())
19078 error (_("%s used in Fission stub without %s"
19079 " in CU at offset 0x%lx [in module %s]"),
19080 form_name, str_offsets_attr_name,
19081 (long) cu->header.offset_size, objf_name);
19082
19083 return read_str_index (cu,
19084 &cu->per_cu->dwarf2_per_objfile->str,
19085 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19086 *cu->str_offsets_base, str_index);
19087}
19088
3019eac3
DE
19089/* Return the length of an LEB128 number in BUF. */
19090
19091static int
19092leb128_size (const gdb_byte *buf)
19093{
19094 const gdb_byte *begin = buf;
19095 gdb_byte byte;
19096
19097 while (1)
19098 {
19099 byte = *buf++;
19100 if ((byte & 128) == 0)
19101 return buf - begin;
19102 }
19103}
19104
c906108c 19105static void
e142c38c 19106set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19107{
19108 switch (lang)
19109 {
19110 case DW_LANG_C89:
76bee0cc 19111 case DW_LANG_C99:
0cfd832f 19112 case DW_LANG_C11:
c906108c 19113 case DW_LANG_C:
d1be3247 19114 case DW_LANG_UPC:
e142c38c 19115 cu->language = language_c;
c906108c 19116 break;
9c37b5ae 19117 case DW_LANG_Java:
c906108c 19118 case DW_LANG_C_plus_plus:
0cfd832f
MW
19119 case DW_LANG_C_plus_plus_11:
19120 case DW_LANG_C_plus_plus_14:
e142c38c 19121 cu->language = language_cplus;
c906108c 19122 break;
6aecb9c2
JB
19123 case DW_LANG_D:
19124 cu->language = language_d;
19125 break;
c906108c
SS
19126 case DW_LANG_Fortran77:
19127 case DW_LANG_Fortran90:
b21b22e0 19128 case DW_LANG_Fortran95:
f7de9aab
MW
19129 case DW_LANG_Fortran03:
19130 case DW_LANG_Fortran08:
e142c38c 19131 cu->language = language_fortran;
c906108c 19132 break;
a766d390
DE
19133 case DW_LANG_Go:
19134 cu->language = language_go;
19135 break;
c906108c 19136 case DW_LANG_Mips_Assembler:
e142c38c 19137 cu->language = language_asm;
c906108c
SS
19138 break;
19139 case DW_LANG_Ada83:
8aaf0b47 19140 case DW_LANG_Ada95:
bc5f45f8
JB
19141 cu->language = language_ada;
19142 break;
72019c9c
GM
19143 case DW_LANG_Modula2:
19144 cu->language = language_m2;
19145 break;
fe8e67fd
PM
19146 case DW_LANG_Pascal83:
19147 cu->language = language_pascal;
19148 break;
22566fbd
DJ
19149 case DW_LANG_ObjC:
19150 cu->language = language_objc;
19151 break;
c44af4eb
TT
19152 case DW_LANG_Rust:
19153 case DW_LANG_Rust_old:
19154 cu->language = language_rust;
19155 break;
c906108c
SS
19156 case DW_LANG_Cobol74:
19157 case DW_LANG_Cobol85:
c906108c 19158 default:
e142c38c 19159 cu->language = language_minimal;
c906108c
SS
19160 break;
19161 }
e142c38c 19162 cu->language_defn = language_def (cu->language);
c906108c
SS
19163}
19164
19165/* Return the named attribute or NULL if not there. */
19166
19167static struct attribute *
e142c38c 19168dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19169{
a48e046c 19170 for (;;)
c906108c 19171 {
a48e046c
TT
19172 unsigned int i;
19173 struct attribute *spec = NULL;
19174
19175 for (i = 0; i < die->num_attrs; ++i)
19176 {
19177 if (die->attrs[i].name == name)
19178 return &die->attrs[i];
19179 if (die->attrs[i].name == DW_AT_specification
19180 || die->attrs[i].name == DW_AT_abstract_origin)
19181 spec = &die->attrs[i];
19182 }
19183
19184 if (!spec)
19185 break;
c906108c 19186
f2f0e013 19187 die = follow_die_ref (die, spec, &cu);
f2f0e013 19188 }
c5aa993b 19189
c906108c
SS
19190 return NULL;
19191}
19192
7d45c7c3
KB
19193/* Return the string associated with a string-typed attribute, or NULL if it
19194 is either not found or is of an incorrect type. */
19195
19196static const char *
19197dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19198{
19199 struct attribute *attr;
19200 const char *str = NULL;
19201
19202 attr = dwarf2_attr (die, name, cu);
19203
19204 if (attr != NULL)
19205 {
43988095 19206 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19207 || attr->form == DW_FORM_string
cf532bd1 19208 || attr->form == DW_FORM_strx
8fe0f950
AT
19209 || attr->form == DW_FORM_strx1
19210 || attr->form == DW_FORM_strx2
19211 || attr->form == DW_FORM_strx3
19212 || attr->form == DW_FORM_strx4
b3340438 19213 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19214 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19215 str = DW_STRING (attr);
19216 else
b98664d3 19217 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19218 "DIE at %s in module %s"),
19219 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19220 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19221 }
19222
19223 return str;
19224}
19225
a084a2a6 19226/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19227 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19228static const char *
19229dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19230{
19231 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19232 if (dwo_name == nullptr)
19233 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19234 return dwo_name;
19235}
19236
05cf31d1
JB
19237/* Return non-zero iff the attribute NAME is defined for the given DIE,
19238 and holds a non-zero value. This function should only be used for
2dc7f7b3 19239 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19240
19241static int
19242dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19243{
19244 struct attribute *attr = dwarf2_attr (die, name, cu);
19245
19246 return (attr && DW_UNSND (attr));
19247}
19248
3ca72b44 19249static int
e142c38c 19250die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19251{
05cf31d1
JB
19252 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19253 which value is non-zero. However, we have to be careful with
19254 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19255 (via dwarf2_flag_true_p) follows this attribute. So we may
19256 end up accidently finding a declaration attribute that belongs
19257 to a different DIE referenced by the specification attribute,
19258 even though the given DIE does not have a declaration attribute. */
19259 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19260 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19261}
19262
63d06c5c 19263/* Return the die giving the specification for DIE, if there is
f2f0e013 19264 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19265 containing the return value on output. If there is no
19266 specification, but there is an abstract origin, that is
19267 returned. */
63d06c5c
DC
19268
19269static struct die_info *
f2f0e013 19270die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19271{
f2f0e013
DJ
19272 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19273 *spec_cu);
63d06c5c 19274
edb3359d
DJ
19275 if (spec_attr == NULL)
19276 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19277
63d06c5c
DC
19278 if (spec_attr == NULL)
19279 return NULL;
19280 else
f2f0e013 19281 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19282}
c906108c 19283
527f3840
JK
19284/* Stub for free_line_header to match void * callback types. */
19285
19286static void
19287free_line_header_voidp (void *arg)
19288{
9a3c8263 19289 struct line_header *lh = (struct line_header *) arg;
527f3840 19290
fff8551c 19291 delete lh;
527f3840
JK
19292}
19293
83769d0b 19294/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19295
19296static struct dwarf2_section_info *
19297get_debug_line_section (struct dwarf2_cu *cu)
19298{
19299 struct dwarf2_section_info *section;
518817b3
SM
19300 struct dwarf2_per_objfile *dwarf2_per_objfile
19301 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19302
19303 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19304 DWO file. */
19305 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19306 section = &cu->dwo_unit->dwo_file->sections.line;
19307 else if (cu->per_cu->is_dwz)
19308 {
ed2dc618 19309 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19310
19311 section = &dwz->line;
19312 }
19313 else
19314 section = &dwarf2_per_objfile->line;
19315
19316 return section;
19317}
19318
debd256d 19319/* Read the statement program header starting at OFFSET in
3019eac3 19320 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19321 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19322 Returns NULL if there is a problem reading the header, e.g., if it
19323 has a version we don't understand.
debd256d
JB
19324
19325 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19326 the returned object point into the dwarf line section buffer,
19327 and must not be freed. */
ae2de4f8 19328
fff8551c 19329static line_header_up
9c541725 19330dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19331{
3019eac3 19332 struct dwarf2_section_info *section;
518817b3
SM
19333 struct dwarf2_per_objfile *dwarf2_per_objfile
19334 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19335
36586728 19336 section = get_debug_line_section (cu);
96b79293 19337 section->read (dwarf2_per_objfile->objfile);
3019eac3 19338 if (section->buffer == NULL)
debd256d 19339 {
3019eac3 19340 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19341 complaint (_("missing .debug_line.dwo section"));
3019eac3 19342 else
b98664d3 19343 complaint (_("missing .debug_line section"));
debd256d
JB
19344 return 0;
19345 }
19346
0df7ad3a
TT
19347 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19348 dwarf2_per_objfile, section,
19349 &cu->header);
debd256d 19350}
c906108c 19351
c6da4cef 19352/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19353 Return the file name of the psymtab for the given file_entry.
c6da4cef 19354 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19355 If space for the result is malloc'd, *NAME_HOLDER will be set.
19356 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19357
d521ce57 19358static const char *
7ba99d21 19359psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19360 const dwarf2_psymtab *pst,
c89b44cd
TT
19361 const char *comp_dir,
19362 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19363{
d521ce57
TT
19364 const char *include_name = fe.name;
19365 const char *include_name_to_compare = include_name;
72b9f47f 19366 const char *pst_filename;
c6da4cef
DE
19367 int file_is_pst;
19368
8c43009f 19369 const char *dir_name = fe.include_dir (lh);
c6da4cef 19370
c89b44cd 19371 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19372 if (!IS_ABSOLUTE_PATH (include_name)
19373 && (dir_name != NULL || comp_dir != NULL))
19374 {
19375 /* Avoid creating a duplicate psymtab for PST.
19376 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19377 Before we do the comparison, however, we need to account
19378 for DIR_NAME and COMP_DIR.
19379 First prepend dir_name (if non-NULL). If we still don't
19380 have an absolute path prepend comp_dir (if non-NULL).
19381 However, the directory we record in the include-file's
19382 psymtab does not contain COMP_DIR (to match the
19383 corresponding symtab(s)).
19384
19385 Example:
19386
19387 bash$ cd /tmp
19388 bash$ gcc -g ./hello.c
19389 include_name = "hello.c"
19390 dir_name = "."
19391 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19392 DW_AT_name = "./hello.c"
19393
19394 */
c6da4cef
DE
19395
19396 if (dir_name != NULL)
19397 {
c89b44cd
TT
19398 name_holder->reset (concat (dir_name, SLASH_STRING,
19399 include_name, (char *) NULL));
19400 include_name = name_holder->get ();
c6da4cef 19401 include_name_to_compare = include_name;
c6da4cef
DE
19402 }
19403 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19404 {
c89b44cd
TT
19405 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19406 include_name, (char *) NULL));
19407 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19408 }
19409 }
19410
19411 pst_filename = pst->filename;
c89b44cd 19412 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19413 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19414 {
c89b44cd
TT
19415 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19416 pst_filename, (char *) NULL));
19417 pst_filename = copied_name.get ();
c6da4cef
DE
19418 }
19419
1e3fad37 19420 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19421
c6da4cef
DE
19422 if (file_is_pst)
19423 return NULL;
19424 return include_name;
19425}
19426
d9b3de22
DE
19427/* State machine to track the state of the line number program. */
19428
6f77053d 19429class lnp_state_machine
d9b3de22 19430{
6f77053d
PA
19431public:
19432 /* Initialize a machine state for the start of a line number
19433 program. */
804d2729
TT
19434 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19435 bool record_lines_p);
6f77053d 19436
8c43009f
PA
19437 file_entry *current_file ()
19438 {
19439 /* lh->file_names is 0-based, but the file name numbers in the
19440 statement program are 1-based. */
6f77053d
PA
19441 return m_line_header->file_name_at (m_file);
19442 }
19443
19444 /* Record the line in the state machine. END_SEQUENCE is true if
19445 we're processing the end of a sequence. */
19446 void record_line (bool end_sequence);
19447
7ab6656f
OJ
19448 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19449 nop-out rest of the lines in this sequence. */
6f77053d
PA
19450 void check_line_address (struct dwarf2_cu *cu,
19451 const gdb_byte *line_ptr,
7ab6656f 19452 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19453
19454 void handle_set_discriminator (unsigned int discriminator)
19455 {
19456 m_discriminator = discriminator;
19457 m_line_has_non_zero_discriminator |= discriminator != 0;
19458 }
19459
19460 /* Handle DW_LNE_set_address. */
19461 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19462 {
19463 m_op_index = 0;
19464 address += baseaddr;
19465 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19466 }
19467
19468 /* Handle DW_LNS_advance_pc. */
19469 void handle_advance_pc (CORE_ADDR adjust);
19470
19471 /* Handle a special opcode. */
19472 void handle_special_opcode (unsigned char op_code);
19473
19474 /* Handle DW_LNS_advance_line. */
19475 void handle_advance_line (int line_delta)
19476 {
19477 advance_line (line_delta);
19478 }
19479
19480 /* Handle DW_LNS_set_file. */
19481 void handle_set_file (file_name_index file);
19482
19483 /* Handle DW_LNS_negate_stmt. */
19484 void handle_negate_stmt ()
19485 {
19486 m_is_stmt = !m_is_stmt;
19487 }
19488
19489 /* Handle DW_LNS_const_add_pc. */
19490 void handle_const_add_pc ();
19491
19492 /* Handle DW_LNS_fixed_advance_pc. */
19493 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19494 {
19495 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19496 m_op_index = 0;
19497 }
19498
19499 /* Handle DW_LNS_copy. */
19500 void handle_copy ()
19501 {
19502 record_line (false);
19503 m_discriminator = 0;
19504 }
19505
19506 /* Handle DW_LNE_end_sequence. */
19507 void handle_end_sequence ()
19508 {
804d2729 19509 m_currently_recording_lines = true;
6f77053d
PA
19510 }
19511
19512private:
19513 /* Advance the line by LINE_DELTA. */
19514 void advance_line (int line_delta)
19515 {
19516 m_line += line_delta;
19517
19518 if (line_delta != 0)
19519 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19520 }
19521
804d2729
TT
19522 struct dwarf2_cu *m_cu;
19523
6f77053d
PA
19524 gdbarch *m_gdbarch;
19525
19526 /* True if we're recording lines.
19527 Otherwise we're building partial symtabs and are just interested in
19528 finding include files mentioned by the line number program. */
19529 bool m_record_lines_p;
19530
8c43009f 19531 /* The line number header. */
6f77053d 19532 line_header *m_line_header;
8c43009f 19533
6f77053d
PA
19534 /* These are part of the standard DWARF line number state machine,
19535 and initialized according to the DWARF spec. */
d9b3de22 19536
6f77053d 19537 unsigned char m_op_index = 0;
7ba99d21
AT
19538 /* The line table index of the current file. */
19539 file_name_index m_file = 1;
6f77053d
PA
19540 unsigned int m_line = 1;
19541
19542 /* These are initialized in the constructor. */
19543
19544 CORE_ADDR m_address;
19545 bool m_is_stmt;
19546 unsigned int m_discriminator;
d9b3de22
DE
19547
19548 /* Additional bits of state we need to track. */
19549
19550 /* The last file that we called dwarf2_start_subfile for.
19551 This is only used for TLLs. */
6f77053d 19552 unsigned int m_last_file = 0;
d9b3de22 19553 /* The last file a line number was recorded for. */
6f77053d 19554 struct subfile *m_last_subfile = NULL;
d9b3de22 19555
804d2729
TT
19556 /* When true, record the lines we decode. */
19557 bool m_currently_recording_lines = false;
d9b3de22
DE
19558
19559 /* The last line number that was recorded, used to coalesce
19560 consecutive entries for the same line. This can happen, for
19561 example, when discriminators are present. PR 17276. */
6f77053d
PA
19562 unsigned int m_last_line = 0;
19563 bool m_line_has_non_zero_discriminator = false;
8c43009f 19564};
d9b3de22 19565
6f77053d
PA
19566void
19567lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19568{
19569 CORE_ADDR addr_adj = (((m_op_index + adjust)
19570 / m_line_header->maximum_ops_per_instruction)
19571 * m_line_header->minimum_instruction_length);
19572 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19573 m_op_index = ((m_op_index + adjust)
19574 % m_line_header->maximum_ops_per_instruction);
19575}
d9b3de22 19576
6f77053d
PA
19577void
19578lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19579{
6f77053d 19580 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19581 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19582 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19583 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19584 / m_line_header->maximum_ops_per_instruction)
19585 * m_line_header->minimum_instruction_length);
19586 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19587 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19588 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19589
258bf0ee 19590 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19591 advance_line (line_delta);
19592 record_line (false);
19593 m_discriminator = 0;
19594}
d9b3de22 19595
6f77053d
PA
19596void
19597lnp_state_machine::handle_set_file (file_name_index file)
19598{
19599 m_file = file;
19600
19601 const file_entry *fe = current_file ();
19602 if (fe == NULL)
19603 dwarf2_debug_line_missing_file_complaint ();
19604 else if (m_record_lines_p)
19605 {
19606 const char *dir = fe->include_dir (m_line_header);
19607
c24bdb02 19608 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19609 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19610 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19611 }
19612}
19613
19614void
19615lnp_state_machine::handle_const_add_pc ()
19616{
19617 CORE_ADDR adjust
19618 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19619
19620 CORE_ADDR addr_adj
19621 = (((m_op_index + adjust)
19622 / m_line_header->maximum_ops_per_instruction)
19623 * m_line_header->minimum_instruction_length);
19624
19625 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19626 m_op_index = ((m_op_index + adjust)
19627 % m_line_header->maximum_ops_per_instruction);
19628}
d9b3de22 19629
a05a36a5
DE
19630/* Return non-zero if we should add LINE to the line number table.
19631 LINE is the line to add, LAST_LINE is the last line that was added,
19632 LAST_SUBFILE is the subfile for LAST_LINE.
19633 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19634 had a non-zero discriminator.
19635
19636 We have to be careful in the presence of discriminators.
19637 E.g., for this line:
19638
19639 for (i = 0; i < 100000; i++);
19640
19641 clang can emit four line number entries for that one line,
19642 each with a different discriminator.
19643 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19644
19645 However, we want gdb to coalesce all four entries into one.
19646 Otherwise the user could stepi into the middle of the line and
19647 gdb would get confused about whether the pc really was in the
19648 middle of the line.
19649
19650 Things are further complicated by the fact that two consecutive
19651 line number entries for the same line is a heuristic used by gcc
19652 to denote the end of the prologue. So we can't just discard duplicate
19653 entries, we have to be selective about it. The heuristic we use is
19654 that we only collapse consecutive entries for the same line if at least
19655 one of those entries has a non-zero discriminator. PR 17276.
19656
19657 Note: Addresses in the line number state machine can never go backwards
19658 within one sequence, thus this coalescing is ok. */
19659
19660static int
804d2729
TT
19661dwarf_record_line_p (struct dwarf2_cu *cu,
19662 unsigned int line, unsigned int last_line,
a05a36a5
DE
19663 int line_has_non_zero_discriminator,
19664 struct subfile *last_subfile)
19665{
c24bdb02 19666 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19667 return 1;
19668 if (line != last_line)
19669 return 1;
19670 /* Same line for the same file that we've seen already.
19671 As a last check, for pr 17276, only record the line if the line
19672 has never had a non-zero discriminator. */
19673 if (!line_has_non_zero_discriminator)
19674 return 1;
19675 return 0;
19676}
19677
804d2729
TT
19678/* Use the CU's builder to record line number LINE beginning at
19679 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19680
19681static void
d9b3de22 19682dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19683 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19684 struct dwarf2_cu *cu)
252a6764
DE
19685{
19686 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19687
27e0867f
DE
19688 if (dwarf_line_debug)
19689 {
19690 fprintf_unfiltered (gdb_stdlog,
19691 "Recording line %u, file %s, address %s\n",
19692 line, lbasename (subfile->name),
19693 paddress (gdbarch, address));
19694 }
19695
804d2729 19696 if (cu != nullptr)
8c95582d 19697 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19698}
19699
19700/* Subroutine of dwarf_decode_lines_1 to simplify it.
19701 Mark the end of a set of line number records.
d9b3de22 19702 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19703 If SUBFILE is NULL the request is ignored. */
19704
19705static void
19706dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19707 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19708{
27e0867f
DE
19709 if (subfile == NULL)
19710 return;
19711
19712 if (dwarf_line_debug)
19713 {
19714 fprintf_unfiltered (gdb_stdlog,
19715 "Finishing current line, file %s, address %s\n",
19716 lbasename (subfile->name),
19717 paddress (gdbarch, address));
19718 }
19719
8c95582d 19720 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19721}
19722
6f77053d
PA
19723void
19724lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19725{
d9b3de22
DE
19726 if (dwarf_line_debug)
19727 {
19728 fprintf_unfiltered (gdb_stdlog,
19729 "Processing actual line %u: file %u,"
94a72be7 19730 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19731 m_line, m_file,
6f77053d 19732 paddress (m_gdbarch, m_address),
94a72be7
AB
19733 m_is_stmt, m_discriminator,
19734 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19735 }
19736
6f77053d 19737 file_entry *fe = current_file ();
8c43009f
PA
19738
19739 if (fe == NULL)
d9b3de22
DE
19740 dwarf2_debug_line_missing_file_complaint ();
19741 /* For now we ignore lines not starting on an instruction boundary.
19742 But not when processing end_sequence for compatibility with the
19743 previous version of the code. */
6f77053d 19744 else if (m_op_index == 0 || end_sequence)
d9b3de22 19745 {
8c43009f 19746 fe->included_p = 1;
8c95582d 19747 if (m_record_lines_p)
d9b3de22 19748 {
c24bdb02 19749 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19750 || end_sequence)
d9b3de22 19751 {
804d2729
TT
19752 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19753 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19754 }
19755
19756 if (!end_sequence)
19757 {
8c95582d
AB
19758 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19759
804d2729 19760 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19761 m_line_has_non_zero_discriminator,
19762 m_last_subfile))
d9b3de22 19763 {
c24bdb02 19764 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19765 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19766 builder->get_current_subfile (),
8c95582d 19767 m_line, m_address, is_stmt,
804d2729 19768 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19769 }
c24bdb02 19770 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19771 m_last_line = m_line;
d9b3de22
DE
19772 }
19773 }
19774 }
19775}
19776
804d2729
TT
19777lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19778 line_header *lh, bool record_lines_p)
d9b3de22 19779{
804d2729 19780 m_cu = cu;
6f77053d
PA
19781 m_gdbarch = arch;
19782 m_record_lines_p = record_lines_p;
19783 m_line_header = lh;
d9b3de22 19784
804d2729 19785 m_currently_recording_lines = true;
d9b3de22 19786
d9b3de22
DE
19787 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19788 was a line entry for it so that the backend has a chance to adjust it
19789 and also record it in case it needs it. This is currently used by MIPS
19790 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19791 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19792 m_is_stmt = lh->default_is_stmt;
19793 m_discriminator = 0;
252a6764
DE
19794}
19795
6f77053d
PA
19796void
19797lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19798 const gdb_byte *line_ptr,
7ab6656f 19799 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19800{
7ab6656f
OJ
19801 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19802 the pc range of the CU. However, we restrict the test to only ADDRESS
19803 values of zero to preserve GDB's previous behaviour which is to handle
19804 the specific case of a function being GC'd by the linker. */
924c2928 19805
7ab6656f 19806 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19807 {
19808 /* This line table is for a function which has been
19809 GCd by the linker. Ignore it. PR gdb/12528 */
19810
518817b3 19811 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
19812 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19813
b98664d3 19814 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 19815 line_offset, objfile_name (objfile));
804d2729
TT
19816 m_currently_recording_lines = false;
19817 /* Note: m_currently_recording_lines is left as false until we see
19818 DW_LNE_end_sequence. */
924c2928
DE
19819 }
19820}
19821
f3f5162e 19822/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19823 Process the line number information in LH.
19824 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19825 program in order to set included_p for every referenced header. */
debd256d 19826
c906108c 19827static void
43f3e411
DE
19828dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19829 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19830{
d521ce57
TT
19831 const gdb_byte *line_ptr, *extended_end;
19832 const gdb_byte *line_end;
a8c50c1f 19833 unsigned int bytes_read, extended_len;
699ca60a 19834 unsigned char op_code, extended_op;
e142c38c 19835 CORE_ADDR baseaddr;
518817b3 19836 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19837 bfd *abfd = objfile->obfd;
08feed99 19838 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
19839 /* True if we're recording line info (as opposed to building partial
19840 symtabs and just interested in finding include files mentioned by
19841 the line number program). */
19842 bool record_lines_p = !decode_for_pst_p;
e142c38c 19843
b3b3bada 19844 baseaddr = objfile->text_section_offset ();
c906108c 19845
debd256d
JB
19846 line_ptr = lh->statement_program_start;
19847 line_end = lh->statement_program_end;
c906108c
SS
19848
19849 /* Read the statement sequences until there's nothing left. */
19850 while (line_ptr < line_end)
19851 {
6f77053d
PA
19852 /* The DWARF line number program state machine. Reset the state
19853 machine at the start of each sequence. */
804d2729 19854 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 19855 bool end_sequence = false;
d9b3de22 19856
8c43009f 19857 if (record_lines_p)
c906108c 19858 {
8c43009f
PA
19859 /* Start a subfile for the current file of the state
19860 machine. */
19861 const file_entry *fe = state_machine.current_file ();
19862
19863 if (fe != NULL)
804d2729 19864 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
19865 }
19866
a738430d 19867 /* Decode the table. */
d9b3de22 19868 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19869 {
19870 op_code = read_1_byte (abfd, line_ptr);
19871 line_ptr += 1;
9aa1fe7e 19872
debd256d 19873 if (op_code >= lh->opcode_base)
6e70227d 19874 {
8e07a239 19875 /* Special opcode. */
6f77053d 19876 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19877 }
19878 else switch (op_code)
c906108c
SS
19879 {
19880 case DW_LNS_extended_op:
3e43a32a
MS
19881 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19882 &bytes_read);
473b7be6 19883 line_ptr += bytes_read;
a8c50c1f 19884 extended_end = line_ptr + extended_len;
c906108c
SS
19885 extended_op = read_1_byte (abfd, line_ptr);
19886 line_ptr += 1;
19887 switch (extended_op)
19888 {
19889 case DW_LNE_end_sequence:
6f77053d
PA
19890 state_machine.handle_end_sequence ();
19891 end_sequence = true;
c906108c
SS
19892 break;
19893 case DW_LNE_set_address:
d9b3de22
DE
19894 {
19895 CORE_ADDR address
c8a7a66f 19896 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 19897 line_ptr += bytes_read;
6f77053d
PA
19898
19899 state_machine.check_line_address (cu, line_ptr,
7ab6656f 19900 lowpc - baseaddr, address);
6f77053d 19901 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19902 }
c906108c
SS
19903 break;
19904 case DW_LNE_define_file:
debd256d 19905 {
d521ce57 19906 const char *cur_file;
ecfb656c
PA
19907 unsigned int mod_time, length;
19908 dir_index dindex;
6e70227d 19909
3e43a32a
MS
19910 cur_file = read_direct_string (abfd, line_ptr,
19911 &bytes_read);
debd256d 19912 line_ptr += bytes_read;
ecfb656c 19913 dindex = (dir_index)
debd256d
JB
19914 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19915 line_ptr += bytes_read;
19916 mod_time =
19917 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19918 line_ptr += bytes_read;
19919 length =
19920 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19921 line_ptr += bytes_read;
ecfb656c 19922 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19923 }
c906108c 19924 break;
d0c6ba3d 19925 case DW_LNE_set_discriminator:
6f77053d
PA
19926 {
19927 /* The discriminator is not interesting to the
19928 debugger; just ignore it. We still need to
19929 check its value though:
19930 if there are consecutive entries for the same
19931 (non-prologue) line we want to coalesce them.
19932 PR 17276. */
19933 unsigned int discr
19934 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19935 line_ptr += bytes_read;
19936
19937 state_machine.handle_set_discriminator (discr);
19938 }
d0c6ba3d 19939 break;
c906108c 19940 default:
b98664d3 19941 complaint (_("mangled .debug_line section"));
debd256d 19942 return;
c906108c 19943 }
a8c50c1f
DJ
19944 /* Make sure that we parsed the extended op correctly. If e.g.
19945 we expected a different address size than the producer used,
19946 we may have read the wrong number of bytes. */
19947 if (line_ptr != extended_end)
19948 {
b98664d3 19949 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
19950 return;
19951 }
c906108c
SS
19952 break;
19953 case DW_LNS_copy:
6f77053d 19954 state_machine.handle_copy ();
c906108c
SS
19955 break;
19956 case DW_LNS_advance_pc:
2dc7f7b3
TT
19957 {
19958 CORE_ADDR adjust
19959 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19960 line_ptr += bytes_read;
6f77053d
PA
19961
19962 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19963 }
c906108c
SS
19964 break;
19965 case DW_LNS_advance_line:
a05a36a5
DE
19966 {
19967 int line_delta
19968 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19969 line_ptr += bytes_read;
6f77053d
PA
19970
19971 state_machine.handle_advance_line (line_delta);
a05a36a5 19972 }
c906108c
SS
19973 break;
19974 case DW_LNS_set_file:
d9b3de22 19975 {
6f77053d 19976 file_name_index file
ecfb656c
PA
19977 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19978 &bytes_read);
d9b3de22 19979 line_ptr += bytes_read;
8c43009f 19980
6f77053d 19981 state_machine.handle_set_file (file);
d9b3de22 19982 }
c906108c
SS
19983 break;
19984 case DW_LNS_set_column:
0ad93d4f 19985 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19986 line_ptr += bytes_read;
19987 break;
19988 case DW_LNS_negate_stmt:
6f77053d 19989 state_machine.handle_negate_stmt ();
c906108c
SS
19990 break;
19991 case DW_LNS_set_basic_block:
c906108c 19992 break;
c2c6d25f
JM
19993 /* Add to the address register of the state machine the
19994 address increment value corresponding to special opcode
a738430d
MK
19995 255. I.e., this value is scaled by the minimum
19996 instruction length since special opcode 255 would have
b021a221 19997 scaled the increment. */
c906108c 19998 case DW_LNS_const_add_pc:
6f77053d 19999 state_machine.handle_const_add_pc ();
c906108c
SS
20000 break;
20001 case DW_LNS_fixed_advance_pc:
3e29f34a 20002 {
6f77053d 20003 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20004 line_ptr += 2;
6f77053d
PA
20005
20006 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20007 }
c906108c 20008 break;
9aa1fe7e 20009 default:
a738430d
MK
20010 {
20011 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20012 int i;
a738430d 20013
debd256d 20014 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20015 {
20016 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20017 line_ptr += bytes_read;
20018 }
20019 }
c906108c
SS
20020 }
20021 }
d9b3de22
DE
20022
20023 if (!end_sequence)
20024 dwarf2_debug_line_missing_end_sequence_complaint ();
20025
20026 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20027 in which case we still finish recording the last line). */
6f77053d 20028 state_machine.record_line (true);
c906108c 20029 }
f3f5162e
DE
20030}
20031
20032/* Decode the Line Number Program (LNP) for the given line_header
20033 structure and CU. The actual information extracted and the type
20034 of structures created from the LNP depends on the value of PST.
20035
20036 1. If PST is NULL, then this procedure uses the data from the program
20037 to create all necessary symbol tables, and their linetables.
20038
20039 2. If PST is not NULL, this procedure reads the program to determine
20040 the list of files included by the unit represented by PST, and
20041 builds all the associated partial symbol tables.
20042
20043 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20044 It is used for relative paths in the line table.
20045 NOTE: When processing partial symtabs (pst != NULL),
20046 comp_dir == pst->dirname.
20047
20048 NOTE: It is important that psymtabs have the same file name (via strcmp)
20049 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20050 symtab we don't use it in the name of the psymtabs we create.
20051 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20052 A good testcase for this is mb-inline.exp.
20053
527f3840
JK
20054 LOWPC is the lowest address in CU (or 0 if not known).
20055
20056 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20057 for its PC<->lines mapping information. Otherwise only the filename
20058 table is read in. */
f3f5162e
DE
20059
20060static void
20061dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20062 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20063 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20064{
518817b3 20065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20066 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20067
527f3840
JK
20068 if (decode_mapping)
20069 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20070
20071 if (decode_for_pst_p)
20072 {
aaa75496
JB
20073 /* Now that we're done scanning the Line Header Program, we can
20074 create the psymtab of each included file. */
7ba99d21
AT
20075 for (auto &file_entry : lh->file_names ())
20076 if (file_entry.included_p == 1)
aaa75496 20077 {
c89b44cd 20078 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20079 const char *include_name =
7ba99d21
AT
20080 psymtab_include_file_name (lh, file_entry, pst,
20081 comp_dir, &name_holder);
c6da4cef 20082 if (include_name != NULL)
aaa75496
JB
20083 dwarf2_create_include_psymtab (include_name, pst, objfile);
20084 }
20085 }
cb1df416
DJ
20086 else
20087 {
20088 /* Make sure a symtab is created for every file, even files
20089 which contain only variables (i.e. no code with associated
20090 line numbers). */
c24bdb02
KS
20091 buildsym_compunit *builder = cu->get_builder ();
20092 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20093
7ba99d21 20094 for (auto &fe : lh->file_names ())
cb1df416 20095 {
804d2729 20096 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20097 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20098 {
c24bdb02 20099 builder->get_current_subfile ()->symtab
804d2729 20100 = allocate_symtab (cust,
c24bdb02 20101 builder->get_current_subfile ()->name);
43f3e411 20102 }
c24bdb02 20103 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20104 }
20105 }
c906108c
SS
20106}
20107
20108/* Start a subfile for DWARF. FILENAME is the name of the file and
20109 DIRNAME the name of the source directory which contains FILENAME
4d663531 20110 or NULL if not known.
c906108c
SS
20111 This routine tries to keep line numbers from identical absolute and
20112 relative file names in a common subfile.
20113
20114 Using the `list' example from the GDB testsuite, which resides in
20115 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20116 of /srcdir/list0.c yields the following debugging information for list0.c:
20117
c5aa993b 20118 DW_AT_name: /srcdir/list0.c
4d663531 20119 DW_AT_comp_dir: /compdir
357e46e7 20120 files.files[0].name: list0.h
c5aa993b 20121 files.files[0].dir: /srcdir
357e46e7 20122 files.files[1].name: list0.c
c5aa993b 20123 files.files[1].dir: /srcdir
c906108c
SS
20124
20125 The line number information for list0.c has to end up in a single
4f1520fb
FR
20126 subfile, so that `break /srcdir/list0.c:1' works as expected.
20127 start_subfile will ensure that this happens provided that we pass the
20128 concatenation of files.files[1].dir and files.files[1].name as the
20129 subfile's name. */
c906108c
SS
20130
20131static void
804d2729
TT
20132dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20133 const char *dirname)
c906108c 20134{
43816ebc 20135 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20136
4d663531 20137 /* In order not to lose the line information directory,
4f1520fb
FR
20138 we concatenate it to the filename when it makes sense.
20139 Note that the Dwarf3 standard says (speaking of filenames in line
20140 information): ``The directory index is ignored for file names
20141 that represent full path names''. Thus ignoring dirname in the
20142 `else' branch below isn't an issue. */
c906108c 20143
d5166ae1 20144 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20145 {
43816ebc
TT
20146 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20147 filename = copy.get ();
d521ce57 20148 }
c906108c 20149
c24bdb02 20150 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20151}
20152
804d2729
TT
20153/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20154 buildsym_compunit constructor. */
f4dc4d17 20155
c24bdb02
KS
20156struct compunit_symtab *
20157dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20158 CORE_ADDR low_pc)
f4dc4d17 20159{
c24bdb02 20160 gdb_assert (m_builder == nullptr);
43f3e411 20161
c24bdb02
KS
20162 m_builder.reset (new struct buildsym_compunit
20163 (per_cu->dwarf2_per_objfile->objfile,
20164 name, comp_dir, language, low_pc));
93b8bea4 20165
c24bdb02 20166 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20167
c24bdb02
KS
20168 get_builder ()->record_debugformat ("DWARF 2");
20169 get_builder ()->record_producer (producer);
f4dc4d17 20170
c24bdb02 20171 processing_has_namespace_info = false;
43f3e411 20172
c24bdb02 20173 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20174}
20175
4c2df51b
DJ
20176static void
20177var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20178 struct dwarf2_cu *cu)
4c2df51b 20179{
518817b3 20180 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20181 struct comp_unit_head *cu_header = &cu->header;
20182
4c2df51b
DJ
20183 /* NOTE drow/2003-01-30: There used to be a comment and some special
20184 code here to turn a symbol with DW_AT_external and a
20185 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20186 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20187 with some versions of binutils) where shared libraries could have
20188 relocations against symbols in their debug information - the
20189 minimal symbol would have the right address, but the debug info
20190 would not. It's no longer necessary, because we will explicitly
20191 apply relocations when we read in the debug information now. */
20192
20193 /* A DW_AT_location attribute with no contents indicates that a
20194 variable has been optimized away. */
4fc6c0d5 20195 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20196 {
f1e6e072 20197 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20198 return;
20199 }
20200
20201 /* Handle one degenerate form of location expression specially, to
20202 preserve GDB's previous behavior when section offsets are
336d760d
AT
20203 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20204 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20205
4fc6c0d5 20206 if (attr->form_is_block ()
3019eac3
DE
20207 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20208 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20209 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20210 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20211 && (DW_BLOCK (attr)->size
20212 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20213 {
891d2f0b 20214 unsigned int dummy;
4c2df51b 20215
3019eac3 20216 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20217 SET_SYMBOL_VALUE_ADDRESS
20218 (sym, cu->header.read_address (objfile->obfd,
20219 DW_BLOCK (attr)->data + 1,
20220 &dummy));
3019eac3 20221 else
38583298
TT
20222 SET_SYMBOL_VALUE_ADDRESS
20223 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20224 &dummy));
f1e6e072 20225 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20226 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20227 SET_SYMBOL_VALUE_ADDRESS
20228 (sym,
20229 SYMBOL_VALUE_ADDRESS (sym)
20230 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20231 return;
20232 }
20233
20234 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20235 expression evaluator, and use LOC_COMPUTED only when necessary
20236 (i.e. when the value of a register or memory location is
20237 referenced, or a thread-local block, etc.). Then again, it might
20238 not be worthwhile. I'm assuming that it isn't unless performance
20239 or memory numbers show me otherwise. */
20240
f1e6e072 20241 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20242
f1e6e072 20243 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20244 cu->has_loclist = true;
4c2df51b
DJ
20245}
20246
c906108c
SS
20247/* Given a pointer to a DWARF information entry, figure out if we need
20248 to make a symbol table entry for it, and if so, create a new entry
20249 and return a pointer to it.
20250 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20251 used the passed type.
20252 If SPACE is not NULL, use it to hold the new symbol. If it is
20253 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20254
20255static struct symbol *
5e2db402
TT
20256new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20257 struct symbol *space)
c906108c 20258{
518817b3
SM
20259 struct dwarf2_per_objfile *dwarf2_per_objfile
20260 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20261 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20262 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20263 struct symbol *sym = NULL;
15d034d0 20264 const char *name;
c906108c
SS
20265 struct attribute *attr = NULL;
20266 struct attribute *attr2 = NULL;
e142c38c 20267 CORE_ADDR baseaddr;
e37fd15a
SW
20268 struct pending **list_to_add = NULL;
20269
edb3359d 20270 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20271
b3b3bada 20272 baseaddr = objfile->text_section_offset ();
c906108c 20273
94af9270 20274 name = dwarf2_name (die, cu);
c906108c
SS
20275 if (name)
20276 {
94af9270 20277 const char *linkagename;
34eaf542 20278 int suppress_add = 0;
94af9270 20279
34eaf542
TT
20280 if (space)
20281 sym = space;
20282 else
e623cf5d 20283 sym = allocate_symbol (objfile);
c906108c 20284 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20285
20286 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20287 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20288 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20289 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20290
f55ee35c
JK
20291 /* Fortran does not have mangling standard and the mangling does differ
20292 between gfortran, iFort etc. */
20293 if (cu->language == language_fortran
468c0cbb
CB
20294 && symbol_get_demangled_name (sym) == NULL)
20295 symbol_set_demangled_name (sym,
cfc594ee 20296 dwarf2_full_name (name, die, cu),
29df156d 20297 NULL);
f55ee35c 20298
c906108c 20299 /* Default assumptions.
c5aa993b 20300 Use the passed type or decode it from the die. */
176620f1 20301 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20302 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20303 if (type != NULL)
20304 SYMBOL_TYPE (sym) = type;
20305 else
e7c27a73 20306 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20307 attr = dwarf2_attr (die,
20308 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20309 cu);
435d3d88 20310 if (attr != nullptr)
c906108c
SS
20311 {
20312 SYMBOL_LINE (sym) = DW_UNSND (attr);
20313 }
cb1df416 20314
edb3359d
DJ
20315 attr = dwarf2_attr (die,
20316 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20317 cu);
435d3d88 20318 if (attr != nullptr)
cb1df416 20319 {
ecfb656c 20320 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20321 struct file_entry *fe;
9a619af0 20322
ecfb656c
PA
20323 if (cu->line_header != NULL)
20324 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20325 else
20326 fe = NULL;
20327
20328 if (fe == NULL)
b98664d3 20329 complaint (_("file index out of range"));
8c43009f
PA
20330 else
20331 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20332 }
20333
c906108c
SS
20334 switch (die->tag)
20335 {
20336 case DW_TAG_label:
e142c38c 20337 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20338 if (attr != nullptr)
3e29f34a
MR
20339 {
20340 CORE_ADDR addr;
20341
cd6c91b4 20342 addr = attr->value_as_address ();
3e29f34a 20343 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20344 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20345 }
0f5238ed
TT
20346 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20347 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20348 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20349 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20350 break;
20351 case DW_TAG_subprogram:
20352 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20353 finish_block. */
f1e6e072 20354 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20355 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20356 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20357 || cu->language == language_ada
20358 || cu->language == language_fortran)
c906108c 20359 {
2cfa0c8d 20360 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20361 Ada and Fortran subprograms, whether marked external or
20362 not, are always stored as a global symbol, because we want
20363 to be able to access them globally. For instance, we want
20364 to be able to break on a nested subprogram without having
20365 to specify the context. */
c24bdb02 20366 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20367 }
20368 else
20369 {
e37fd15a 20370 list_to_add = cu->list_in_scope;
c906108c
SS
20371 }
20372 break;
edb3359d
DJ
20373 case DW_TAG_inlined_subroutine:
20374 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20375 finish_block. */
f1e6e072 20376 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20377 SYMBOL_INLINED (sym) = 1;
481860b3 20378 list_to_add = cu->list_in_scope;
edb3359d 20379 break;
34eaf542
TT
20380 case DW_TAG_template_value_param:
20381 suppress_add = 1;
20382 /* Fall through. */
72929c62 20383 case DW_TAG_constant:
c906108c 20384 case DW_TAG_variable:
254e6b9e 20385 case DW_TAG_member:
0963b4bd
MS
20386 /* Compilation with minimal debug info may result in
20387 variables with missing type entries. Change the
20388 misleading `void' type to something sensible. */
c906108c 20389 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20390 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20391
e142c38c 20392 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20393 /* In the case of DW_TAG_member, we should only be called for
20394 static const members. */
20395 if (die->tag == DW_TAG_member)
20396 {
3863f96c
DE
20397 /* dwarf2_add_field uses die_is_declaration,
20398 so we do the same. */
254e6b9e
DE
20399 gdb_assert (die_is_declaration (die, cu));
20400 gdb_assert (attr);
20401 }
435d3d88 20402 if (attr != nullptr)
c906108c 20403 {
e7c27a73 20404 dwarf2_const_value (attr, sym, cu);
e142c38c 20405 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20406 if (!suppress_add)
34eaf542
TT
20407 {
20408 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20409 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20410 else
e37fd15a 20411 list_to_add = cu->list_in_scope;
34eaf542 20412 }
c906108c
SS
20413 break;
20414 }
e142c38c 20415 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20416 if (attr != nullptr)
c906108c 20417 {
e7c27a73 20418 var_decode_location (attr, sym, cu);
e142c38c 20419 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20420
20421 /* Fortran explicitly imports any global symbols to the local
20422 scope by DW_TAG_common_block. */
20423 if (cu->language == language_fortran && die->parent
20424 && die->parent->tag == DW_TAG_common_block)
20425 attr2 = NULL;
20426
caac4577
JG
20427 if (SYMBOL_CLASS (sym) == LOC_STATIC
20428 && SYMBOL_VALUE_ADDRESS (sym) == 0
20429 && !dwarf2_per_objfile->has_section_at_zero)
20430 {
20431 /* When a static variable is eliminated by the linker,
20432 the corresponding debug information is not stripped
20433 out, but the variable address is set to null;
20434 do not add such variables into symbol table. */
20435 }
20436 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20437 {
4b610737
TT
20438 if (SYMBOL_CLASS (sym) == LOC_STATIC
20439 && (objfile->flags & OBJF_MAINLINE) == 0
20440 && dwarf2_per_objfile->can_copy)
20441 {
20442 /* A global static variable might be subject to
20443 copy relocation. We first check for a local
20444 minsym, though, because maybe the symbol was
20445 marked hidden, in which case this would not
20446 apply. */
20447 bound_minimal_symbol found
20448 = (lookup_minimal_symbol_linkage
987012b8 20449 (sym->linkage_name (), objfile));
4b610737
TT
20450 if (found.minsym != nullptr)
20451 sym->maybe_copied = 1;
20452 }
f55ee35c 20453
1c809c68
TT
20454 /* A variable with DW_AT_external is never static,
20455 but it may be block-scoped. */
804d2729 20456 list_to_add
c24bdb02
KS
20457 = ((cu->list_in_scope
20458 == cu->get_builder ()->get_file_symbols ())
20459 ? cu->get_builder ()->get_global_symbols ()
804d2729 20460 : cu->list_in_scope);
1c809c68 20461 }
c906108c 20462 else
e37fd15a 20463 list_to_add = cu->list_in_scope;
c906108c
SS
20464 }
20465 else
20466 {
20467 /* We do not know the address of this symbol.
c5aa993b
JM
20468 If it is an external symbol and we have type information
20469 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20470 The address of the variable will then be determined from
20471 the minimal symbol table whenever the variable is
20472 referenced. */
e142c38c 20473 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20474
20475 /* Fortran explicitly imports any global symbols to the local
20476 scope by DW_TAG_common_block. */
20477 if (cu->language == language_fortran && die->parent
20478 && die->parent->tag == DW_TAG_common_block)
20479 {
20480 /* SYMBOL_CLASS doesn't matter here because
20481 read_common_block is going to reset it. */
20482 if (!suppress_add)
20483 list_to_add = cu->list_in_scope;
20484 }
20485 else if (attr2 && (DW_UNSND (attr2) != 0)
20486 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20487 {
0fe7935b
DJ
20488 /* A variable with DW_AT_external is never static, but it
20489 may be block-scoped. */
804d2729 20490 list_to_add
c24bdb02
KS
20491 = ((cu->list_in_scope
20492 == cu->get_builder ()->get_file_symbols ())
20493 ? cu->get_builder ()->get_global_symbols ()
804d2729 20494 : cu->list_in_scope);
0fe7935b 20495
f1e6e072 20496 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20497 }
442ddf59
JK
20498 else if (!die_is_declaration (die, cu))
20499 {
20500 /* Use the default LOC_OPTIMIZED_OUT class. */
20501 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20502 if (!suppress_add)
20503 list_to_add = cu->list_in_scope;
442ddf59 20504 }
c906108c
SS
20505 }
20506 break;
20507 case DW_TAG_formal_parameter:
a60f3166
TT
20508 {
20509 /* If we are inside a function, mark this as an argument. If
20510 not, we might be looking at an argument to an inlined function
20511 when we do not have enough information to show inlined frames;
20512 pretend it's a local variable in that case so that the user can
20513 still see it. */
804d2729 20514 struct context_stack *curr
c24bdb02 20515 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20516 if (curr != nullptr && curr->name != nullptr)
20517 SYMBOL_IS_ARGUMENT (sym) = 1;
20518 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20519 if (attr != nullptr)
a60f3166
TT
20520 {
20521 var_decode_location (attr, sym, cu);
20522 }
20523 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20524 if (attr != nullptr)
a60f3166
TT
20525 {
20526 dwarf2_const_value (attr, sym, cu);
20527 }
f346a30d 20528
a60f3166
TT
20529 list_to_add = cu->list_in_scope;
20530 }
c906108c
SS
20531 break;
20532 case DW_TAG_unspecified_parameters:
20533 /* From varargs functions; gdb doesn't seem to have any
20534 interest in this information, so just ignore it for now.
20535 (FIXME?) */
20536 break;
34eaf542
TT
20537 case DW_TAG_template_type_param:
20538 suppress_add = 1;
20539 /* Fall through. */
c906108c 20540 case DW_TAG_class_type:
680b30c7 20541 case DW_TAG_interface_type:
c906108c
SS
20542 case DW_TAG_structure_type:
20543 case DW_TAG_union_type:
72019c9c 20544 case DW_TAG_set_type:
c906108c 20545 case DW_TAG_enumeration_type:
f1e6e072 20546 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20547 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20548
63d06c5c 20549 {
9c37b5ae 20550 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20551 really ever be static objects: otherwise, if you try
20552 to, say, break of a class's method and you're in a file
20553 which doesn't mention that class, it won't work unless
20554 the check for all static symbols in lookup_symbol_aux
20555 saves you. See the OtherFileClass tests in
20556 gdb.c++/namespace.exp. */
20557
e37fd15a 20558 if (!suppress_add)
34eaf542 20559 {
c24bdb02 20560 buildsym_compunit *builder = cu->get_builder ();
804d2729 20561 list_to_add
c24bdb02 20562 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20563 && cu->language == language_cplus
c24bdb02 20564 ? builder->get_global_symbols ()
804d2729 20565 : cu->list_in_scope);
63d06c5c 20566
64382290 20567 /* The semantics of C++ state that "struct foo {
9c37b5ae 20568 ... }" also defines a typedef for "foo". */
64382290 20569 if (cu->language == language_cplus
45280282 20570 || cu->language == language_ada
c44af4eb
TT
20571 || cu->language == language_d
20572 || cu->language == language_rust)
64382290
TT
20573 {
20574 /* The symbol's name is already allocated along
20575 with this objfile, so we don't need to
20576 duplicate it for the type. */
20577 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20578 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20579 }
63d06c5c
DC
20580 }
20581 }
c906108c
SS
20582 break;
20583 case DW_TAG_typedef:
f1e6e072 20584 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20585 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20586 list_to_add = cu->list_in_scope;
63d06c5c 20587 break;
c906108c 20588 case DW_TAG_base_type:
a02abb62 20589 case DW_TAG_subrange_type:
f1e6e072 20590 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20591 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20592 list_to_add = cu->list_in_scope;
c906108c
SS
20593 break;
20594 case DW_TAG_enumerator:
e142c38c 20595 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20596 if (attr != nullptr)
c906108c 20597 {
e7c27a73 20598 dwarf2_const_value (attr, sym, cu);
c906108c 20599 }
63d06c5c
DC
20600 {
20601 /* NOTE: carlton/2003-11-10: See comment above in the
20602 DW_TAG_class_type, etc. block. */
20603
804d2729 20604 list_to_add
c24bdb02 20605 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20606 && cu->language == language_cplus
c24bdb02 20607 ? cu->get_builder ()->get_global_symbols ()
804d2729 20608 : cu->list_in_scope);
63d06c5c 20609 }
c906108c 20610 break;
74921315 20611 case DW_TAG_imported_declaration:
5c4e30ca 20612 case DW_TAG_namespace:
f1e6e072 20613 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20614 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20615 break;
530e8392
KB
20616 case DW_TAG_module:
20617 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20618 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20619 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20620 break;
4357ac6c 20621 case DW_TAG_common_block:
f1e6e072 20622 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20623 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20624 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20625 break;
c906108c
SS
20626 default:
20627 /* Not a tag we recognize. Hopefully we aren't processing
20628 trash data, but since we must specifically ignore things
20629 we don't recognize, there is nothing else we should do at
0963b4bd 20630 this point. */
b98664d3 20631 complaint (_("unsupported tag: '%s'"),
4d3c2250 20632 dwarf_tag_name (die->tag));
c906108c
SS
20633 break;
20634 }
df8a16a1 20635
e37fd15a
SW
20636 if (suppress_add)
20637 {
20638 sym->hash_next = objfile->template_symbols;
20639 objfile->template_symbols = sym;
20640 list_to_add = NULL;
20641 }
20642
20643 if (list_to_add != NULL)
d3cb6808 20644 add_symbol_to_list (sym, list_to_add);
e37fd15a 20645
df8a16a1
DJ
20646 /* For the benefit of old versions of GCC, check for anonymous
20647 namespaces based on the demangled name. */
4d4ec4e5 20648 if (!cu->processing_has_namespace_info
94af9270 20649 && cu->language == language_cplus)
c24bdb02 20650 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20651 }
20652 return (sym);
20653}
20654
98bfdba5
PA
20655/* Given an attr with a DW_FORM_dataN value in host byte order,
20656 zero-extend it as appropriate for the symbol's type. The DWARF
20657 standard (v4) is not entirely clear about the meaning of using
20658 DW_FORM_dataN for a constant with a signed type, where the type is
20659 wider than the data. The conclusion of a discussion on the DWARF
20660 list was that this is unspecified. We choose to always zero-extend
20661 because that is the interpretation long in use by GCC. */
c906108c 20662
98bfdba5 20663static gdb_byte *
ff39bb5e 20664dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20665 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20666{
518817b3 20667 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20668 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20669 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20670 LONGEST l = DW_UNSND (attr);
20671
20672 if (bits < sizeof (*value) * 8)
20673 {
20674 l &= ((LONGEST) 1 << bits) - 1;
20675 *value = l;
20676 }
20677 else if (bits == sizeof (*value) * 8)
20678 *value = l;
20679 else
20680 {
224c3ddb 20681 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20682 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20683 return bytes;
20684 }
20685
20686 return NULL;
20687}
20688
20689/* Read a constant value from an attribute. Either set *VALUE, or if
20690 the value does not fit in *VALUE, set *BYTES - either already
20691 allocated on the objfile obstack, or newly allocated on OBSTACK,
20692 or, set *BATON, if we translated the constant to a location
20693 expression. */
20694
20695static void
ff39bb5e 20696dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20697 const char *name, struct obstack *obstack,
20698 struct dwarf2_cu *cu,
d521ce57 20699 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20700 struct dwarf2_locexpr_baton **baton)
20701{
518817b3 20702 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20703 struct comp_unit_head *cu_header = &cu->header;
c906108c 20704 struct dwarf_block *blk;
98bfdba5
PA
20705 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20706 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20707
20708 *value = 0;
20709 *bytes = NULL;
20710 *baton = NULL;
c906108c
SS
20711
20712 switch (attr->form)
20713 {
20714 case DW_FORM_addr:
336d760d 20715 case DW_FORM_addrx:
3019eac3 20716 case DW_FORM_GNU_addr_index:
ac56253d 20717 {
ac56253d
TT
20718 gdb_byte *data;
20719
98bfdba5
PA
20720 if (TYPE_LENGTH (type) != cu_header->addr_size)
20721 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20722 cu_header->addr_size,
98bfdba5 20723 TYPE_LENGTH (type));
ac56253d
TT
20724 /* Symbols of this form are reasonably rare, so we just
20725 piggyback on the existing location code rather than writing
20726 a new implementation of symbol_computed_ops. */
8d749320 20727 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20728 (*baton)->per_cu = cu->per_cu;
20729 gdb_assert ((*baton)->per_cu);
ac56253d 20730
98bfdba5 20731 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20732 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20733 (*baton)->data = data;
ac56253d
TT
20734
20735 data[0] = DW_OP_addr;
20736 store_unsigned_integer (&data[1], cu_header->addr_size,
20737 byte_order, DW_ADDR (attr));
20738 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20739 }
c906108c 20740 break;
4ac36638 20741 case DW_FORM_string:
93b5768b 20742 case DW_FORM_strp:
cf532bd1 20743 case DW_FORM_strx:
3019eac3 20744 case DW_FORM_GNU_str_index:
36586728 20745 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20746 /* DW_STRING is already allocated on the objfile obstack, point
20747 directly to it. */
d521ce57 20748 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20749 break;
c906108c
SS
20750 case DW_FORM_block1:
20751 case DW_FORM_block2:
20752 case DW_FORM_block4:
20753 case DW_FORM_block:
2dc7f7b3 20754 case DW_FORM_exprloc:
0224619f 20755 case DW_FORM_data16:
c906108c 20756 blk = DW_BLOCK (attr);
98bfdba5
PA
20757 if (TYPE_LENGTH (type) != blk->size)
20758 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20759 TYPE_LENGTH (type));
20760 *bytes = blk->data;
c906108c 20761 break;
2df3850c
JM
20762
20763 /* The DW_AT_const_value attributes are supposed to carry the
20764 symbol's value "represented as it would be on the target
20765 architecture." By the time we get here, it's already been
20766 converted to host endianness, so we just need to sign- or
20767 zero-extend it as appropriate. */
20768 case DW_FORM_data1:
3aef2284 20769 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20770 break;
c906108c 20771 case DW_FORM_data2:
3aef2284 20772 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20773 break;
c906108c 20774 case DW_FORM_data4:
3aef2284 20775 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20776 break;
c906108c 20777 case DW_FORM_data8:
3aef2284 20778 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20779 break;
20780
c906108c 20781 case DW_FORM_sdata:
663c44ac 20782 case DW_FORM_implicit_const:
98bfdba5 20783 *value = DW_SND (attr);
2df3850c
JM
20784 break;
20785
c906108c 20786 case DW_FORM_udata:
98bfdba5 20787 *value = DW_UNSND (attr);
c906108c 20788 break;
2df3850c 20789
c906108c 20790 default:
b98664d3 20791 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20792 dwarf_form_name (attr->form));
98bfdba5 20793 *value = 0;
c906108c
SS
20794 break;
20795 }
20796}
20797
2df3850c 20798
98bfdba5
PA
20799/* Copy constant value from an attribute to a symbol. */
20800
2df3850c 20801static void
ff39bb5e 20802dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20803 struct dwarf2_cu *cu)
2df3850c 20804{
518817b3 20805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20806 LONGEST value;
d521ce57 20807 const gdb_byte *bytes;
98bfdba5 20808 struct dwarf2_locexpr_baton *baton;
2df3850c 20809
98bfdba5 20810 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 20811 sym->print_name (),
98bfdba5
PA
20812 &objfile->objfile_obstack, cu,
20813 &value, &bytes, &baton);
2df3850c 20814
98bfdba5
PA
20815 if (baton != NULL)
20816 {
98bfdba5 20817 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20818 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20819 }
20820 else if (bytes != NULL)
20821 {
20822 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20823 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20824 }
20825 else
20826 {
20827 SYMBOL_VALUE (sym) = value;
f1e6e072 20828 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20829 }
2df3850c
JM
20830}
20831
c906108c
SS
20832/* Return the type of the die in question using its DW_AT_type attribute. */
20833
20834static struct type *
e7c27a73 20835die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20836{
c906108c 20837 struct attribute *type_attr;
c906108c 20838
e142c38c 20839 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20840 if (!type_attr)
20841 {
518817b3 20842 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20843 /* A missing DW_AT_type represents a void type. */
518817b3 20844 return objfile_type (objfile)->builtin_void;
c906108c 20845 }
348e048f 20846
673bfd45 20847 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20848}
20849
b4ba55a1
JB
20850/* True iff CU's producer generates GNAT Ada auxiliary information
20851 that allows to find parallel types through that information instead
20852 of having to do expensive parallel lookups by type name. */
20853
20854static int
20855need_gnat_info (struct dwarf2_cu *cu)
20856{
de4cb04a
JB
20857 /* Assume that the Ada compiler was GNAT, which always produces
20858 the auxiliary information. */
20859 return (cu->language == language_ada);
b4ba55a1
JB
20860}
20861
b4ba55a1
JB
20862/* Return the auxiliary type of the die in question using its
20863 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20864 attribute is not present. */
20865
20866static struct type *
20867die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20868{
b4ba55a1 20869 struct attribute *type_attr;
b4ba55a1
JB
20870
20871 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20872 if (!type_attr)
20873 return NULL;
20874
673bfd45 20875 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20876}
20877
20878/* If DIE has a descriptive_type attribute, then set the TYPE's
20879 descriptive type accordingly. */
20880
20881static void
20882set_descriptive_type (struct type *type, struct die_info *die,
20883 struct dwarf2_cu *cu)
20884{
20885 struct type *descriptive_type = die_descriptive_type (die, cu);
20886
20887 if (descriptive_type)
20888 {
20889 ALLOCATE_GNAT_AUX_TYPE (type);
20890 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20891 }
20892}
20893
c906108c
SS
20894/* Return the containing type of the die in question using its
20895 DW_AT_containing_type attribute. */
20896
20897static struct type *
e7c27a73 20898die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20899{
c906108c 20900 struct attribute *type_attr;
518817b3 20901 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20902
e142c38c 20903 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20904 if (!type_attr)
20905 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 20906 "[in module %s]"), objfile_name (objfile));
33ac96f0 20907
673bfd45 20908 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20909}
20910
ac9ec31b
DE
20911/* Return an error marker type to use for the ill formed type in DIE/CU. */
20912
20913static struct type *
20914build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20915{
518817b3
SM
20916 struct dwarf2_per_objfile *dwarf2_per_objfile
20917 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 20918 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 20919 char *saved;
ac9ec31b 20920
528e1572
SM
20921 std::string message
20922 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20923 objfile_name (objfile),
20924 sect_offset_str (cu->header.sect_off),
20925 sect_offset_str (die->sect_off));
efba19b0 20926 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 20927
19f392bc 20928 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20929}
20930
673bfd45 20931/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20932 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20933 DW_AT_containing_type.
673bfd45
DE
20934 If there is no type substitute an error marker. */
20935
c906108c 20936static struct type *
ff39bb5e 20937lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20938 struct dwarf2_cu *cu)
c906108c 20939{
518817b3
SM
20940 struct dwarf2_per_objfile *dwarf2_per_objfile
20941 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20942 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
20943 struct type *this_type;
20944
ac9ec31b
DE
20945 gdb_assert (attr->name == DW_AT_type
20946 || attr->name == DW_AT_GNAT_descriptive_type
20947 || attr->name == DW_AT_containing_type);
20948
673bfd45
DE
20949 /* First see if we have it cached. */
20950
36586728
TT
20951 if (attr->form == DW_FORM_GNU_ref_alt)
20952 {
20953 struct dwarf2_per_cu_data *per_cu;
0826b30a 20954 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 20955
ed2dc618
SM
20956 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20957 dwarf2_per_objfile);
9c541725 20958 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20959 }
cd6c91b4 20960 else if (attr->form_is_ref ())
673bfd45 20961 {
0826b30a 20962 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 20963
9c541725 20964 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20965 }
55f1336d 20966 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20967 {
ac9ec31b 20968 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20969
ac9ec31b 20970 return get_signatured_type (die, signature, cu);
673bfd45
DE
20971 }
20972 else
20973 {
b98664d3 20974 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
20975 " at %s [in module %s]"),
20976 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 20977 objfile_name (objfile));
ac9ec31b 20978 return build_error_marker_type (cu, die);
673bfd45
DE
20979 }
20980
20981 /* If not cached we need to read it in. */
20982
20983 if (this_type == NULL)
20984 {
ac9ec31b 20985 struct die_info *type_die = NULL;
673bfd45
DE
20986 struct dwarf2_cu *type_cu = cu;
20987
cd6c91b4 20988 if (attr->form_is_ref ())
ac9ec31b
DE
20989 type_die = follow_die_ref (die, attr, &type_cu);
20990 if (type_die == NULL)
20991 return build_error_marker_type (cu, die);
20992 /* If we find the type now, it's probably because the type came
3019eac3
DE
20993 from an inter-CU reference and the type's CU got expanded before
20994 ours. */
ac9ec31b 20995 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20996 }
20997
20998 /* If we still don't have a type use an error marker. */
20999
21000 if (this_type == NULL)
ac9ec31b 21001 return build_error_marker_type (cu, die);
673bfd45 21002
f792889a 21003 return this_type;
c906108c
SS
21004}
21005
673bfd45
DE
21006/* Return the type in DIE, CU.
21007 Returns NULL for invalid types.
21008
02142a6c 21009 This first does a lookup in die_type_hash,
673bfd45
DE
21010 and only reads the die in if necessary.
21011
21012 NOTE: This can be called when reading in partial or full symbols. */
21013
f792889a 21014static struct type *
e7c27a73 21015read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21016{
f792889a
DJ
21017 struct type *this_type;
21018
21019 this_type = get_die_type (die, cu);
21020 if (this_type)
21021 return this_type;
21022
673bfd45
DE
21023 return read_type_die_1 (die, cu);
21024}
21025
21026/* Read the type in DIE, CU.
21027 Returns NULL for invalid types. */
21028
21029static struct type *
21030read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21031{
21032 struct type *this_type = NULL;
21033
c906108c
SS
21034 switch (die->tag)
21035 {
21036 case DW_TAG_class_type:
680b30c7 21037 case DW_TAG_interface_type:
c906108c
SS
21038 case DW_TAG_structure_type:
21039 case DW_TAG_union_type:
f792889a 21040 this_type = read_structure_type (die, cu);
c906108c
SS
21041 break;
21042 case DW_TAG_enumeration_type:
f792889a 21043 this_type = read_enumeration_type (die, cu);
c906108c
SS
21044 break;
21045 case DW_TAG_subprogram:
21046 case DW_TAG_subroutine_type:
edb3359d 21047 case DW_TAG_inlined_subroutine:
f792889a 21048 this_type = read_subroutine_type (die, cu);
c906108c
SS
21049 break;
21050 case DW_TAG_array_type:
f792889a 21051 this_type = read_array_type (die, cu);
c906108c 21052 break;
72019c9c 21053 case DW_TAG_set_type:
f792889a 21054 this_type = read_set_type (die, cu);
72019c9c 21055 break;
c906108c 21056 case DW_TAG_pointer_type:
f792889a 21057 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21058 break;
21059 case DW_TAG_ptr_to_member_type:
f792889a 21060 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21061 break;
21062 case DW_TAG_reference_type:
4297a3f0
AV
21063 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21064 break;
21065 case DW_TAG_rvalue_reference_type:
21066 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21067 break;
21068 case DW_TAG_const_type:
f792889a 21069 this_type = read_tag_const_type (die, cu);
c906108c
SS
21070 break;
21071 case DW_TAG_volatile_type:
f792889a 21072 this_type = read_tag_volatile_type (die, cu);
c906108c 21073 break;
06d66ee9
TT
21074 case DW_TAG_restrict_type:
21075 this_type = read_tag_restrict_type (die, cu);
21076 break;
c906108c 21077 case DW_TAG_string_type:
f792889a 21078 this_type = read_tag_string_type (die, cu);
c906108c
SS
21079 break;
21080 case DW_TAG_typedef:
f792889a 21081 this_type = read_typedef (die, cu);
c906108c 21082 break;
a02abb62 21083 case DW_TAG_subrange_type:
f792889a 21084 this_type = read_subrange_type (die, cu);
a02abb62 21085 break;
c906108c 21086 case DW_TAG_base_type:
f792889a 21087 this_type = read_base_type (die, cu);
c906108c 21088 break;
81a17f79 21089 case DW_TAG_unspecified_type:
f792889a 21090 this_type = read_unspecified_type (die, cu);
81a17f79 21091 break;
0114d602
DJ
21092 case DW_TAG_namespace:
21093 this_type = read_namespace_type (die, cu);
21094 break;
f55ee35c
JK
21095 case DW_TAG_module:
21096 this_type = read_module_type (die, cu);
21097 break;
a2c2acaf
MW
21098 case DW_TAG_atomic_type:
21099 this_type = read_tag_atomic_type (die, cu);
21100 break;
c906108c 21101 default:
b98664d3 21102 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21103 dwarf_tag_name (die->tag));
c906108c
SS
21104 break;
21105 }
63d06c5c 21106
f792889a 21107 return this_type;
63d06c5c
DC
21108}
21109
abc72ce4
DE
21110/* See if we can figure out if the class lives in a namespace. We do
21111 this by looking for a member function; its demangled name will
21112 contain namespace info, if there is any.
21113 Return the computed name or NULL.
21114 Space for the result is allocated on the objfile's obstack.
21115 This is the full-die version of guess_partial_die_structure_name.
21116 In this case we know DIE has no useful parent. */
21117
43816ebc 21118static const char *
abc72ce4
DE
21119guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21120{
21121 struct die_info *spec_die;
21122 struct dwarf2_cu *spec_cu;
21123 struct die_info *child;
518817b3 21124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21125
21126 spec_cu = cu;
21127 spec_die = die_specification (die, &spec_cu);
21128 if (spec_die != NULL)
21129 {
21130 die = spec_die;
21131 cu = spec_cu;
21132 }
21133
21134 for (child = die->child;
21135 child != NULL;
21136 child = child->sibling)
21137 {
21138 if (child->tag == DW_TAG_subprogram)
21139 {
73b9be8b 21140 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21141
7d45c7c3 21142 if (linkage_name != NULL)
abc72ce4 21143 {
43816ebc
TT
21144 gdb::unique_xmalloc_ptr<char> actual_name
21145 (language_class_name_from_physname (cu->language_defn,
21146 linkage_name));
21147 const char *name = NULL;
abc72ce4
DE
21148
21149 if (actual_name != NULL)
21150 {
15d034d0 21151 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21152
21153 if (die_name != NULL
43816ebc 21154 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21155 {
21156 /* Strip off the class name from the full name.
21157 We want the prefix. */
21158 int die_name_len = strlen (die_name);
43816ebc
TT
21159 int actual_name_len = strlen (actual_name.get ());
21160 const char *ptr = actual_name.get ();
abc72ce4
DE
21161
21162 /* Test for '::' as a sanity check. */
21163 if (actual_name_len > die_name_len + 2
43816ebc 21164 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21165 name = obstack_strndup (
e3b94546 21166 &objfile->per_bfd->storage_obstack,
43816ebc 21167 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21168 }
21169 }
abc72ce4
DE
21170 return name;
21171 }
21172 }
21173 }
21174
21175 return NULL;
21176}
21177
96408a79
SA
21178/* GCC might emit a nameless typedef that has a linkage name. Determine the
21179 prefix part in such case. See
21180 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21181
a121b7c1 21182static const char *
96408a79
SA
21183anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21184{
21185 struct attribute *attr;
e6a959d6 21186 const char *base;
96408a79
SA
21187
21188 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21189 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21190 return NULL;
21191
7d45c7c3 21192 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21193 return NULL;
21194
73b9be8b 21195 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21196 if (attr == NULL || DW_STRING (attr) == NULL)
21197 return NULL;
21198
21199 /* dwarf2_name had to be already called. */
21200 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21201
21202 /* Strip the base name, keep any leading namespaces/classes. */
21203 base = strrchr (DW_STRING (attr), ':');
21204 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21205 return "";
21206
518817b3 21207 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21208 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21209 DW_STRING (attr),
21210 &base[-1] - DW_STRING (attr));
96408a79
SA
21211}
21212
fdde2d81 21213/* Return the name of the namespace/class that DIE is defined within,
0114d602 21214 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21215
0114d602
DJ
21216 For example, if we're within the method foo() in the following
21217 code:
21218
21219 namespace N {
21220 class C {
21221 void foo () {
21222 }
21223 };
21224 }
21225
21226 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21227
0d5cff50 21228static const char *
e142c38c 21229determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21230{
518817b3
SM
21231 struct dwarf2_per_objfile *dwarf2_per_objfile
21232 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21233 struct die_info *parent, *spec_die;
21234 struct dwarf2_cu *spec_cu;
21235 struct type *parent_type;
a121b7c1 21236 const char *retval;
63d06c5c 21237
9c37b5ae 21238 if (cu->language != language_cplus
c44af4eb
TT
21239 && cu->language != language_fortran && cu->language != language_d
21240 && cu->language != language_rust)
0114d602
DJ
21241 return "";
21242
96408a79
SA
21243 retval = anonymous_struct_prefix (die, cu);
21244 if (retval)
21245 return retval;
21246
0114d602
DJ
21247 /* We have to be careful in the presence of DW_AT_specification.
21248 For example, with GCC 3.4, given the code
21249
21250 namespace N {
21251 void foo() {
21252 // Definition of N::foo.
21253 }
21254 }
21255
21256 then we'll have a tree of DIEs like this:
21257
21258 1: DW_TAG_compile_unit
21259 2: DW_TAG_namespace // N
21260 3: DW_TAG_subprogram // declaration of N::foo
21261 4: DW_TAG_subprogram // definition of N::foo
21262 DW_AT_specification // refers to die #3
21263
21264 Thus, when processing die #4, we have to pretend that we're in
21265 the context of its DW_AT_specification, namely the contex of die
21266 #3. */
21267 spec_cu = cu;
21268 spec_die = die_specification (die, &spec_cu);
21269 if (spec_die == NULL)
21270 parent = die->parent;
21271 else
63d06c5c 21272 {
0114d602
DJ
21273 parent = spec_die->parent;
21274 cu = spec_cu;
63d06c5c 21275 }
0114d602
DJ
21276
21277 if (parent == NULL)
21278 return "";
98bfdba5
PA
21279 else if (parent->building_fullname)
21280 {
21281 const char *name;
21282 const char *parent_name;
21283
21284 /* It has been seen on RealView 2.2 built binaries,
21285 DW_TAG_template_type_param types actually _defined_ as
21286 children of the parent class:
21287
21288 enum E {};
21289 template class <class Enum> Class{};
21290 Class<enum E> class_e;
21291
21292 1: DW_TAG_class_type (Class)
21293 2: DW_TAG_enumeration_type (E)
21294 3: DW_TAG_enumerator (enum1:0)
21295 3: DW_TAG_enumerator (enum2:1)
21296 ...
21297 2: DW_TAG_template_type_param
21298 DW_AT_type DW_FORM_ref_udata (E)
21299
21300 Besides being broken debug info, it can put GDB into an
21301 infinite loop. Consider:
21302
21303 When we're building the full name for Class<E>, we'll start
21304 at Class, and go look over its template type parameters,
21305 finding E. We'll then try to build the full name of E, and
21306 reach here. We're now trying to build the full name of E,
21307 and look over the parent DIE for containing scope. In the
21308 broken case, if we followed the parent DIE of E, we'd again
21309 find Class, and once again go look at its template type
21310 arguments, etc., etc. Simply don't consider such parent die
21311 as source-level parent of this die (it can't be, the language
21312 doesn't allow it), and break the loop here. */
21313 name = dwarf2_name (die, cu);
21314 parent_name = dwarf2_name (parent, cu);
b98664d3 21315 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21316 name ? name : "<unknown>",
21317 parent_name ? parent_name : "<unknown>");
21318 return "";
21319 }
63d06c5c 21320 else
0114d602
DJ
21321 switch (parent->tag)
21322 {
63d06c5c 21323 case DW_TAG_namespace:
0114d602 21324 parent_type = read_type_die (parent, cu);
acebe513
UW
21325 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21326 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21327 Work around this problem here. */
21328 if (cu->language == language_cplus
e86ca25f 21329 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21330 return "";
0114d602 21331 /* We give a name to even anonymous namespaces. */
e86ca25f 21332 return TYPE_NAME (parent_type);
63d06c5c 21333 case DW_TAG_class_type:
680b30c7 21334 case DW_TAG_interface_type:
63d06c5c 21335 case DW_TAG_structure_type:
0114d602 21336 case DW_TAG_union_type:
f55ee35c 21337 case DW_TAG_module:
0114d602 21338 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21339 if (TYPE_NAME (parent_type) != NULL)
21340 return TYPE_NAME (parent_type);
0114d602
DJ
21341 else
21342 /* An anonymous structure is only allowed non-static data
21343 members; no typedefs, no member functions, et cetera.
21344 So it does not need a prefix. */
21345 return "";
abc72ce4 21346 case DW_TAG_compile_unit:
95554aad 21347 case DW_TAG_partial_unit:
abc72ce4
DE
21348 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21349 if (cu->language == language_cplus
fd5866f6 21350 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21351 && die->child != NULL
21352 && (die->tag == DW_TAG_class_type
21353 || die->tag == DW_TAG_structure_type
21354 || die->tag == DW_TAG_union_type))
21355 {
43816ebc 21356 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21357 if (name != NULL)
21358 return name;
21359 }
21360 return "";
0a4b0913
AB
21361 case DW_TAG_subprogram:
21362 /* Nested subroutines in Fortran get a prefix with the name
21363 of the parent's subroutine. */
21364 if (cu->language == language_fortran)
21365 {
21366 if ((die->tag == DW_TAG_subprogram)
21367 && (dwarf2_name (parent, cu) != NULL))
21368 return dwarf2_name (parent, cu);
21369 }
21370 return determine_prefix (parent, cu);
3d567982
TT
21371 case DW_TAG_enumeration_type:
21372 parent_type = read_type_die (parent, cu);
21373 if (TYPE_DECLARED_CLASS (parent_type))
21374 {
e86ca25f
TT
21375 if (TYPE_NAME (parent_type) != NULL)
21376 return TYPE_NAME (parent_type);
3d567982
TT
21377 return "";
21378 }
21379 /* Fall through. */
63d06c5c 21380 default:
8176b9b8 21381 return determine_prefix (parent, cu);
63d06c5c 21382 }
63d06c5c
DC
21383}
21384
3e43a32a
MS
21385/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21386 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21387 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21388 an obconcat, otherwise allocate storage for the result. The CU argument is
21389 used to determine the language and hence, the appropriate separator. */
987504bb 21390
f55ee35c 21391#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21392
21393static char *
f55ee35c
JK
21394typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21395 int physname, struct dwarf2_cu *cu)
63d06c5c 21396{
f55ee35c 21397 const char *lead = "";
5c315b68 21398 const char *sep;
63d06c5c 21399
3e43a32a
MS
21400 if (suffix == NULL || suffix[0] == '\0'
21401 || prefix == NULL || prefix[0] == '\0')
987504bb 21402 sep = "";
45280282
IB
21403 else if (cu->language == language_d)
21404 {
21405 /* For D, the 'main' function could be defined in any module, but it
21406 should never be prefixed. */
21407 if (strcmp (suffix, "D main") == 0)
21408 {
21409 prefix = "";
21410 sep = "";
21411 }
21412 else
21413 sep = ".";
21414 }
f55ee35c
JK
21415 else if (cu->language == language_fortran && physname)
21416 {
21417 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21418 DW_AT_MIPS_linkage_name is preferred and used instead. */
21419
21420 lead = "__";
21421 sep = "_MOD_";
21422 }
987504bb
JJ
21423 else
21424 sep = "::";
63d06c5c 21425
6dd47d34
DE
21426 if (prefix == NULL)
21427 prefix = "";
21428 if (suffix == NULL)
21429 suffix = "";
21430
987504bb
JJ
21431 if (obs == NULL)
21432 {
3e43a32a 21433 char *retval
224c3ddb
SM
21434 = ((char *)
21435 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21436
f55ee35c
JK
21437 strcpy (retval, lead);
21438 strcat (retval, prefix);
6dd47d34
DE
21439 strcat (retval, sep);
21440 strcat (retval, suffix);
63d06c5c
DC
21441 return retval;
21442 }
987504bb
JJ
21443 else
21444 {
21445 /* We have an obstack. */
f55ee35c 21446 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21447 }
63d06c5c
DC
21448}
21449
71c25dea
TT
21450/* Get name of a die, return NULL if not found. */
21451
15d034d0
TT
21452static const char *
21453dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21454 struct objfile *objfile)
71c25dea
TT
21455{
21456 if (name && cu->language == language_cplus)
21457 {
2f408ecb 21458 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21459
2f408ecb 21460 if (!canon_name.empty ())
71c25dea 21461 {
2f408ecb 21462 if (canon_name != name)
be1e3d3e 21463 name = objfile->intern (canon_name);
71c25dea
TT
21464 }
21465 }
21466
21467 return name;
c906108c
SS
21468}
21469
96553a0c
DE
21470/* Get name of a die, return NULL if not found.
21471 Anonymous namespaces are converted to their magic string. */
9219021c 21472
15d034d0 21473static const char *
e142c38c 21474dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21475{
21476 struct attribute *attr;
518817b3 21477 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21478
e142c38c 21479 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21480 if ((!attr || !DW_STRING (attr))
96553a0c 21481 && die->tag != DW_TAG_namespace
53832f31
TT
21482 && die->tag != DW_TAG_class_type
21483 && die->tag != DW_TAG_interface_type
21484 && die->tag != DW_TAG_structure_type
21485 && die->tag != DW_TAG_union_type)
71c25dea
TT
21486 return NULL;
21487
21488 switch (die->tag)
21489 {
21490 case DW_TAG_compile_unit:
95554aad 21491 case DW_TAG_partial_unit:
71c25dea
TT
21492 /* Compilation units have a DW_AT_name that is a filename, not
21493 a source language identifier. */
21494 case DW_TAG_enumeration_type:
21495 case DW_TAG_enumerator:
21496 /* These tags always have simple identifiers already; no need
21497 to canonicalize them. */
21498 return DW_STRING (attr);
907af001 21499
96553a0c
DE
21500 case DW_TAG_namespace:
21501 if (attr != NULL && DW_STRING (attr) != NULL)
21502 return DW_STRING (attr);
21503 return CP_ANONYMOUS_NAMESPACE_STR;
21504
907af001
UW
21505 case DW_TAG_class_type:
21506 case DW_TAG_interface_type:
21507 case DW_TAG_structure_type:
21508 case DW_TAG_union_type:
21509 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21510 structures or unions. These were of the form "._%d" in GCC 4.1,
21511 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21512 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21513 if (attr && DW_STRING (attr)
61012eef
GB
21514 && (startswith (DW_STRING (attr), "._")
21515 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21516 return NULL;
53832f31
TT
21517
21518 /* GCC might emit a nameless typedef that has a linkage name. See
21519 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21520 if (!attr || DW_STRING (attr) == NULL)
21521 {
73b9be8b 21522 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21523 if (attr == NULL || DW_STRING (attr) == NULL)
21524 return NULL;
21525
df5c6c50
JK
21526 /* Avoid demangling DW_STRING (attr) the second time on a second
21527 call for the same DIE. */
21528 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21529 {
43816ebc
TT
21530 gdb::unique_xmalloc_ptr<char> demangled
21531 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21532 if (demangled == nullptr)
21533 return nullptr;
43816ebc 21534
be1e3d3e 21535 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21536 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21537 }
67430cd0
TT
21538
21539 /* Strip any leading namespaces/classes, keep only the base name.
21540 DW_AT_name for named DIEs does not contain the prefixes. */
21541 const char *base = strrchr (DW_STRING (attr), ':');
21542 if (base && base > DW_STRING (attr) && base[-1] == ':')
21543 return &base[1];
21544 else
21545 return DW_STRING (attr);
53832f31 21546 }
907af001
UW
21547 break;
21548
71c25dea 21549 default:
907af001
UW
21550 break;
21551 }
21552
21553 if (!DW_STRING_IS_CANONICAL (attr))
21554 {
be1e3d3e
TT
21555 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21556 objfile);
907af001 21557 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21558 }
907af001 21559 return DW_STRING (attr);
9219021c
DC
21560}
21561
21562/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21563 is none. *EXT_CU is the CU containing DIE on input, and the CU
21564 containing the return value on output. */
9219021c
DC
21565
21566static struct die_info *
f2f0e013 21567dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21568{
21569 struct attribute *attr;
9219021c 21570
f2f0e013 21571 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21572 if (attr == NULL)
21573 return NULL;
21574
f2f0e013 21575 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21576}
21577
f9aca02d 21578static void
d97bc12b 21579dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21580{
21581 unsigned int i;
21582
d97bc12b 21583 print_spaces (indent, f);
9d8780f0 21584 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21585 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21586 sect_offset_str (die->sect_off));
d97bc12b
DE
21587
21588 if (die->parent != NULL)
21589 {
21590 print_spaces (indent, f);
9d8780f0
SM
21591 fprintf_unfiltered (f, " parent at offset: %s\n",
21592 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21593 }
21594
21595 print_spaces (indent, f);
21596 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21597 dwarf_bool_name (die->child != NULL));
c906108c 21598
d97bc12b
DE
21599 print_spaces (indent, f);
21600 fprintf_unfiltered (f, " attributes:\n");
21601
c906108c
SS
21602 for (i = 0; i < die->num_attrs; ++i)
21603 {
d97bc12b
DE
21604 print_spaces (indent, f);
21605 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21606 dwarf_attr_name (die->attrs[i].name),
21607 dwarf_form_name (die->attrs[i].form));
d97bc12b 21608
c906108c
SS
21609 switch (die->attrs[i].form)
21610 {
c906108c 21611 case DW_FORM_addr:
336d760d 21612 case DW_FORM_addrx:
3019eac3 21613 case DW_FORM_GNU_addr_index:
d97bc12b 21614 fprintf_unfiltered (f, "address: ");
5af949e3 21615 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21616 break;
21617 case DW_FORM_block2:
21618 case DW_FORM_block4:
21619 case DW_FORM_block:
21620 case DW_FORM_block1:
56eb65bd
SP
21621 fprintf_unfiltered (f, "block: size %s",
21622 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21623 break;
2dc7f7b3 21624 case DW_FORM_exprloc:
56eb65bd
SP
21625 fprintf_unfiltered (f, "expression: size %s",
21626 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21627 break;
0224619f
JK
21628 case DW_FORM_data16:
21629 fprintf_unfiltered (f, "constant of 16 bytes");
21630 break;
4568ecf9
DE
21631 case DW_FORM_ref_addr:
21632 fprintf_unfiltered (f, "ref address: ");
21633 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21634 break;
36586728
TT
21635 case DW_FORM_GNU_ref_alt:
21636 fprintf_unfiltered (f, "alt ref address: ");
21637 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21638 break;
10b3939b
DJ
21639 case DW_FORM_ref1:
21640 case DW_FORM_ref2:
21641 case DW_FORM_ref4:
4568ecf9
DE
21642 case DW_FORM_ref8:
21643 case DW_FORM_ref_udata:
d97bc12b 21644 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21645 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21646 break;
c906108c
SS
21647 case DW_FORM_data1:
21648 case DW_FORM_data2:
21649 case DW_FORM_data4:
ce5d95e1 21650 case DW_FORM_data8:
c906108c
SS
21651 case DW_FORM_udata:
21652 case DW_FORM_sdata:
43bbcdc2
PH
21653 fprintf_unfiltered (f, "constant: %s",
21654 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21655 break;
2dc7f7b3
TT
21656 case DW_FORM_sec_offset:
21657 fprintf_unfiltered (f, "section offset: %s",
21658 pulongest (DW_UNSND (&die->attrs[i])));
21659 break;
55f1336d 21660 case DW_FORM_ref_sig8:
ac9ec31b
DE
21661 fprintf_unfiltered (f, "signature: %s",
21662 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21663 break;
c906108c 21664 case DW_FORM_string:
4bdf3d34 21665 case DW_FORM_strp:
43988095 21666 case DW_FORM_line_strp:
cf532bd1 21667 case DW_FORM_strx:
3019eac3 21668 case DW_FORM_GNU_str_index:
36586728 21669 case DW_FORM_GNU_strp_alt:
8285870a 21670 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21671 DW_STRING (&die->attrs[i])
8285870a
JK
21672 ? DW_STRING (&die->attrs[i]) : "",
21673 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21674 break;
21675 case DW_FORM_flag:
21676 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21677 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21678 else
d97bc12b 21679 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21680 break;
2dc7f7b3
TT
21681 case DW_FORM_flag_present:
21682 fprintf_unfiltered (f, "flag: TRUE");
21683 break;
a8329558 21684 case DW_FORM_indirect:
0963b4bd
MS
21685 /* The reader will have reduced the indirect form to
21686 the "base form" so this form should not occur. */
5f48f8f3 21687 fprintf_unfiltered (f,
3e43a32a 21688 "unexpected attribute form: DW_FORM_indirect");
a8329558 21689 break;
663c44ac
JK
21690 case DW_FORM_implicit_const:
21691 fprintf_unfiltered (f, "constant: %s",
21692 plongest (DW_SND (&die->attrs[i])));
21693 break;
c906108c 21694 default:
d97bc12b 21695 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21696 die->attrs[i].form);
d97bc12b 21697 break;
c906108c 21698 }
d97bc12b 21699 fprintf_unfiltered (f, "\n");
c906108c
SS
21700 }
21701}
21702
f9aca02d 21703static void
d97bc12b 21704dump_die_for_error (struct die_info *die)
c906108c 21705{
d97bc12b
DE
21706 dump_die_shallow (gdb_stderr, 0, die);
21707}
21708
21709static void
21710dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21711{
21712 int indent = level * 4;
21713
21714 gdb_assert (die != NULL);
21715
21716 if (level >= max_level)
21717 return;
21718
21719 dump_die_shallow (f, indent, die);
21720
21721 if (die->child != NULL)
c906108c 21722 {
d97bc12b
DE
21723 print_spaces (indent, f);
21724 fprintf_unfiltered (f, " Children:");
21725 if (level + 1 < max_level)
21726 {
21727 fprintf_unfiltered (f, "\n");
21728 dump_die_1 (f, level + 1, max_level, die->child);
21729 }
21730 else
21731 {
3e43a32a
MS
21732 fprintf_unfiltered (f,
21733 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21734 }
21735 }
21736
21737 if (die->sibling != NULL && level > 0)
21738 {
21739 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21740 }
21741}
21742
d97bc12b
DE
21743/* This is called from the pdie macro in gdbinit.in.
21744 It's not static so gcc will keep a copy callable from gdb. */
21745
21746void
21747dump_die (struct die_info *die, int max_level)
21748{
21749 dump_die_1 (gdb_stdlog, 0, max_level, die);
21750}
21751
f9aca02d 21752static void
51545339 21753store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21754{
51545339 21755 void **slot;
c906108c 21756
9c541725
PA
21757 slot = htab_find_slot_with_hash (cu->die_hash, die,
21758 to_underlying (die->sect_off),
b64f50a1 21759 INSERT);
51545339
DJ
21760
21761 *slot = die;
c906108c
SS
21762}
21763
348e048f
DE
21764/* Follow reference or signature attribute ATTR of SRC_DIE.
21765 On entry *REF_CU is the CU of SRC_DIE.
21766 On exit *REF_CU is the CU of the result. */
21767
21768static struct die_info *
ff39bb5e 21769follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21770 struct dwarf2_cu **ref_cu)
21771{
21772 struct die_info *die;
21773
cd6c91b4 21774 if (attr->form_is_ref ())
348e048f 21775 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21776 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21777 die = follow_die_sig (src_die, attr, ref_cu);
21778 else
21779 {
21780 dump_die_for_error (src_die);
21781 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 21782 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
21783 }
21784
21785 return die;
03dd20cc
DJ
21786}
21787
5c631832 21788/* Follow reference OFFSET.
673bfd45
DE
21789 On entry *REF_CU is the CU of the source die referencing OFFSET.
21790 On exit *REF_CU is the CU of the result.
21791 Returns NULL if OFFSET is invalid. */
f504f079 21792
f9aca02d 21793static struct die_info *
9c541725 21794follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21795 struct dwarf2_cu **ref_cu)
c906108c 21796{
10b3939b 21797 struct die_info temp_die;
f2f0e013 21798 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
21799 struct dwarf2_per_objfile *dwarf2_per_objfile
21800 = cu->per_cu->dwarf2_per_objfile;
10b3939b 21801
348e048f
DE
21802 gdb_assert (cu->per_cu != NULL);
21803
98bfdba5
PA
21804 target_cu = cu;
21805
3019eac3 21806 if (cu->per_cu->is_debug_types)
348e048f
DE
21807 {
21808 /* .debug_types CUs cannot reference anything outside their CU.
21809 If they need to, they have to reference a signatured type via
55f1336d 21810 DW_FORM_ref_sig8. */
4057dfde 21811 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 21812 return NULL;
348e048f 21813 }
36586728 21814 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 21815 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
21816 {
21817 struct dwarf2_per_cu_data *per_cu;
9a619af0 21818
9c541725 21819 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 21820 dwarf2_per_objfile);
03dd20cc
DJ
21821
21822 /* If necessary, add it to the queue and load its DIEs. */
95554aad 21823 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 21824 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 21825
10b3939b
DJ
21826 target_cu = per_cu->cu;
21827 }
98bfdba5
PA
21828 else if (cu->dies == NULL)
21829 {
21830 /* We're loading full DIEs during partial symbol reading. */
21831 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 21832 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 21833 }
c906108c 21834
f2f0e013 21835 *ref_cu = target_cu;
9c541725 21836 temp_die.sect_off = sect_off;
c24bdb02
KS
21837
21838 if (target_cu != cu)
21839 target_cu->ancestor = cu;
21840
9a3c8263 21841 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21842 &temp_die,
21843 to_underlying (sect_off));
5c631832 21844}
10b3939b 21845
5c631832
JK
21846/* Follow reference attribute ATTR of SRC_DIE.
21847 On entry *REF_CU is the CU of SRC_DIE.
21848 On exit *REF_CU is the CU of the result. */
21849
21850static struct die_info *
ff39bb5e 21851follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21852 struct dwarf2_cu **ref_cu)
21853{
0826b30a 21854 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
21855 struct dwarf2_cu *cu = *ref_cu;
21856 struct die_info *die;
21857
9c541725 21858 die = follow_die_offset (sect_off,
36586728
TT
21859 (attr->form == DW_FORM_GNU_ref_alt
21860 || cu->per_cu->is_dwz),
21861 ref_cu);
5c631832 21862 if (!die)
9d8780f0
SM
21863 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21864 "at %s [in module %s]"),
21865 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 21866 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 21867
5c631832
JK
21868 return die;
21869}
21870
d4c9a4f8 21871/* See read.h. */
5c631832
JK
21872
21873struct dwarf2_locexpr_baton
9c541725 21874dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 21875 dwarf2_per_cu_data *per_cu,
8b9737bf 21876 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 21877 void *baton, bool resolve_abstract_p)
5c631832 21878{
918dd910 21879 struct dwarf2_cu *cu;
5c631832
JK
21880 struct die_info *die;
21881 struct attribute *attr;
21882 struct dwarf2_locexpr_baton retval;
12359b5e
SM
21883 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21884 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 21885
918dd910 21886 if (per_cu->cu == NULL)
58f0c718 21887 load_cu (per_cu, false);
918dd910 21888 cu = per_cu->cu;
cc12ce38
DE
21889 if (cu == NULL)
21890 {
21891 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21892 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21893 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21894 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21895 }
918dd910 21896
9c541725 21897 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 21898 if (!die)
9d8780f0
SM
21899 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21900 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21901
21902 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 21903 if (!attr && resolve_abstract_p
3360b6e7 21904 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
21905 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21906 {
21907 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 21908 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 21909 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 21910
3360b6e7
TV
21911 for (const auto &cand_off
21912 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 21913 {
3360b6e7
TV
21914 struct dwarf2_cu *cand_cu = cu;
21915 struct die_info *cand
21916 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21917 if (!cand
21918 || !cand->parent
e4a62c65
TV
21919 || cand->parent->tag != DW_TAG_subprogram)
21920 continue;
21921
21922 CORE_ADDR pc_low, pc_high;
21923 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
21924 if (pc_low == ((CORE_ADDR) -1))
21925 continue;
21926 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21927 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21928 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
21929 continue;
21930
21931 die = cand;
21932 attr = dwarf2_attr (die, DW_AT_location, cu);
21933 break;
21934 }
21935 }
21936
5c631832
JK
21937 if (!attr)
21938 {
e103e986
JK
21939 /* DWARF: "If there is no such attribute, then there is no effect.".
21940 DATA is ignored if SIZE is 0. */
5c631832 21941
e103e986 21942 retval.data = NULL;
5c631832
JK
21943 retval.size = 0;
21944 }
cd6c91b4 21945 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
21946 {
21947 struct dwarf2_loclist_baton loclist_baton;
21948 CORE_ADDR pc = (*get_frame_pc) (baton);
21949 size_t size;
21950
21951 fill_in_loclist_baton (cu, &loclist_baton, attr);
21952
21953 retval.data = dwarf2_find_location_expression (&loclist_baton,
21954 &size, pc);
21955 retval.size = size;
21956 }
5c631832
JK
21957 else
21958 {
4fc6c0d5 21959 if (!attr->form_is_block ())
9d8780f0 21960 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 21961 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 21962 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21963
21964 retval.data = DW_BLOCK (attr)->data;
21965 retval.size = DW_BLOCK (attr)->size;
21966 }
21967 retval.per_cu = cu->per_cu;
918dd910 21968
ed2dc618 21969 age_cached_comp_units (dwarf2_per_objfile);
918dd910 21970
5c631832 21971 return retval;
348e048f
DE
21972}
21973
d4c9a4f8 21974/* See read.h. */
8b9737bf
TT
21975
21976struct dwarf2_locexpr_baton
21977dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 21978 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
21979 CORE_ADDR (*get_frame_pc) (void *baton),
21980 void *baton)
21981{
9c541725 21982 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21983
9c541725 21984 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21985}
21986
b6807d98
TT
21987/* Write a constant of a given type as target-ordered bytes into
21988 OBSTACK. */
21989
21990static const gdb_byte *
21991write_constant_as_bytes (struct obstack *obstack,
21992 enum bfd_endian byte_order,
21993 struct type *type,
21994 ULONGEST value,
21995 LONGEST *len)
21996{
21997 gdb_byte *result;
21998
21999 *len = TYPE_LENGTH (type);
224c3ddb 22000 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22001 store_unsigned_integer (result, *len, byte_order, value);
22002
22003 return result;
22004}
22005
d4c9a4f8 22006/* See read.h. */
b6807d98
TT
22007
22008const gdb_byte *
9c541725 22009dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22010 dwarf2_per_cu_data *per_cu,
22011 obstack *obstack,
b6807d98
TT
22012 LONGEST *len)
22013{
22014 struct dwarf2_cu *cu;
22015 struct die_info *die;
22016 struct attribute *attr;
22017 const gdb_byte *result = NULL;
22018 struct type *type;
22019 LONGEST value;
22020 enum bfd_endian byte_order;
e3b94546 22021 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22022
b6807d98 22023 if (per_cu->cu == NULL)
58f0c718 22024 load_cu (per_cu, false);
b6807d98 22025 cu = per_cu->cu;
cc12ce38
DE
22026 if (cu == NULL)
22027 {
22028 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22029 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22030 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22031 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22032 }
b6807d98 22033
9c541725 22034 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22035 if (!die)
9d8780f0
SM
22036 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22037 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22038
22039 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22040 if (attr == NULL)
22041 return NULL;
22042
e3b94546 22043 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22044 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22045
22046 switch (attr->form)
22047 {
22048 case DW_FORM_addr:
336d760d 22049 case DW_FORM_addrx:
b6807d98
TT
22050 case DW_FORM_GNU_addr_index:
22051 {
22052 gdb_byte *tem;
22053
22054 *len = cu->header.addr_size;
224c3ddb 22055 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22056 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22057 result = tem;
22058 }
22059 break;
22060 case DW_FORM_string:
22061 case DW_FORM_strp:
cf532bd1 22062 case DW_FORM_strx:
b6807d98
TT
22063 case DW_FORM_GNU_str_index:
22064 case DW_FORM_GNU_strp_alt:
22065 /* DW_STRING is already allocated on the objfile obstack, point
22066 directly to it. */
22067 result = (const gdb_byte *) DW_STRING (attr);
22068 *len = strlen (DW_STRING (attr));
22069 break;
22070 case DW_FORM_block1:
22071 case DW_FORM_block2:
22072 case DW_FORM_block4:
22073 case DW_FORM_block:
22074 case DW_FORM_exprloc:
0224619f 22075 case DW_FORM_data16:
b6807d98
TT
22076 result = DW_BLOCK (attr)->data;
22077 *len = DW_BLOCK (attr)->size;
22078 break;
22079
22080 /* The DW_AT_const_value attributes are supposed to carry the
22081 symbol's value "represented as it would be on the target
22082 architecture." By the time we get here, it's already been
22083 converted to host endianness, so we just need to sign- or
22084 zero-extend it as appropriate. */
22085 case DW_FORM_data1:
22086 type = die_type (die, cu);
22087 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22088 if (result == NULL)
22089 result = write_constant_as_bytes (obstack, byte_order,
22090 type, value, len);
22091 break;
22092 case DW_FORM_data2:
22093 type = die_type (die, cu);
22094 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22095 if (result == NULL)
22096 result = write_constant_as_bytes (obstack, byte_order,
22097 type, value, len);
22098 break;
22099 case DW_FORM_data4:
22100 type = die_type (die, cu);
22101 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22102 if (result == NULL)
22103 result = write_constant_as_bytes (obstack, byte_order,
22104 type, value, len);
22105 break;
22106 case DW_FORM_data8:
22107 type = die_type (die, cu);
22108 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22109 if (result == NULL)
22110 result = write_constant_as_bytes (obstack, byte_order,
22111 type, value, len);
22112 break;
22113
22114 case DW_FORM_sdata:
663c44ac 22115 case DW_FORM_implicit_const:
b6807d98
TT
22116 type = die_type (die, cu);
22117 result = write_constant_as_bytes (obstack, byte_order,
22118 type, DW_SND (attr), len);
22119 break;
22120
22121 case DW_FORM_udata:
22122 type = die_type (die, cu);
22123 result = write_constant_as_bytes (obstack, byte_order,
22124 type, DW_UNSND (attr), len);
22125 break;
22126
22127 default:
b98664d3 22128 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22129 dwarf_form_name (attr->form));
22130 break;
22131 }
22132
22133 return result;
22134}
22135
d4c9a4f8 22136/* See read.h. */
7942e96e
AA
22137
22138struct type *
9c541725 22139dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22140 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22141{
22142 struct dwarf2_cu *cu;
22143 struct die_info *die;
22144
7942e96e 22145 if (per_cu->cu == NULL)
58f0c718 22146 load_cu (per_cu, false);
7942e96e
AA
22147 cu = per_cu->cu;
22148 if (!cu)
22149 return NULL;
22150
9c541725 22151 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22152 if (!die)
22153 return NULL;
22154
22155 return die_type (die, cu);
22156}
22157
8cb5117c 22158/* See read.h. */
8a9b8146
TT
22159
22160struct type *
b64f50a1 22161dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22162 struct dwarf2_per_cu_data *per_cu)
22163{
9c541725 22164 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22165 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22166}
22167
ac9ec31b 22168/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22169 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22170 On exit *REF_CU is the CU of the result.
22171 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22172
22173static struct die_info *
ac9ec31b
DE
22174follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22175 struct dwarf2_cu **ref_cu)
348e048f 22176{
348e048f 22177 struct die_info temp_die;
c24bdb02 22178 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22179 struct die_info *die;
22180
ac9ec31b
DE
22181 /* While it might be nice to assert sig_type->type == NULL here,
22182 we can get here for DW_AT_imported_declaration where we need
22183 the DIE not the type. */
348e048f
DE
22184
22185 /* If necessary, add it to the queue and load its DIEs. */
22186
95554aad 22187 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22188 read_signatured_type (sig_type);
348e048f 22189
348e048f 22190 sig_cu = sig_type->per_cu.cu;
69d751e3 22191 gdb_assert (sig_cu != NULL);
9c541725
PA
22192 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22193 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22194 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22195 to_underlying (temp_die.sect_off));
348e048f
DE
22196 if (die)
22197 {
ed2dc618 22198 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22199 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22200
796a7ff8
DE
22201 /* For .gdb_index version 7 keep track of included TUs.
22202 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22203 if (dwarf2_per_objfile->index_table != NULL
22204 && dwarf2_per_objfile->index_table->version <= 7)
22205 {
ae640021 22206 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22207 }
22208
348e048f 22209 *ref_cu = sig_cu;
c24bdb02
KS
22210 if (sig_cu != cu)
22211 sig_cu->ancestor = cu;
22212
348e048f
DE
22213 return die;
22214 }
22215
ac9ec31b
DE
22216 return NULL;
22217}
22218
22219/* Follow signatured type referenced by ATTR in SRC_DIE.
22220 On entry *REF_CU is the CU of SRC_DIE.
22221 On exit *REF_CU is the CU of the result.
22222 The result is the DIE of the type.
22223 If the referenced type cannot be found an error is thrown. */
22224
22225static struct die_info *
ff39bb5e 22226follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22227 struct dwarf2_cu **ref_cu)
22228{
22229 ULONGEST signature = DW_SIGNATURE (attr);
22230 struct signatured_type *sig_type;
22231 struct die_info *die;
22232
22233 gdb_assert (attr->form == DW_FORM_ref_sig8);
22234
a2ce51a0 22235 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22236 /* sig_type will be NULL if the signatured type is missing from
22237 the debug info. */
22238 if (sig_type == NULL)
22239 {
22240 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22241 " from DIE at %s [in module %s]"),
22242 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22243 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22244 }
22245
22246 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22247 if (die == NULL)
22248 {
22249 dump_die_for_error (src_die);
22250 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22251 " from DIE at %s [in module %s]"),
22252 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22253 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22254 }
22255
22256 return die;
22257}
22258
22259/* Get the type specified by SIGNATURE referenced in DIE/CU,
22260 reading in and processing the type unit if necessary. */
22261
22262static struct type *
22263get_signatured_type (struct die_info *die, ULONGEST signature,
22264 struct dwarf2_cu *cu)
22265{
518817b3
SM
22266 struct dwarf2_per_objfile *dwarf2_per_objfile
22267 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22268 struct signatured_type *sig_type;
22269 struct dwarf2_cu *type_cu;
22270 struct die_info *type_die;
22271 struct type *type;
22272
a2ce51a0 22273 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22274 /* sig_type will be NULL if the signatured type is missing from
22275 the debug info. */
22276 if (sig_type == NULL)
22277 {
b98664d3 22278 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22279 " from DIE at %s [in module %s]"),
22280 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22281 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22282 return build_error_marker_type (cu, die);
22283 }
22284
22285 /* If we already know the type we're done. */
22286 if (sig_type->type != NULL)
22287 return sig_type->type;
22288
22289 type_cu = cu;
22290 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22291 if (type_die != NULL)
22292 {
22293 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22294 is created. This is important, for example, because for c++ classes
22295 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22296 type = read_type_die (type_die, type_cu);
22297 if (type == NULL)
22298 {
b98664d3 22299 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22300 " referenced from DIE at %s [in module %s]"),
22301 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22302 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22303 type = build_error_marker_type (cu, die);
22304 }
22305 }
22306 else
22307 {
b98664d3 22308 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22309 " from DIE at %s [in module %s]"),
22310 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22311 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22312 type = build_error_marker_type (cu, die);
22313 }
22314 sig_type->type = type;
22315
22316 return type;
22317}
22318
22319/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22320 reading in and processing the type unit if necessary. */
22321
22322static struct type *
ff39bb5e 22323get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22324 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22325{
22326 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22327 if (attr->form_is_ref ())
ac9ec31b
DE
22328 {
22329 struct dwarf2_cu *type_cu = cu;
22330 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22331
22332 return read_type_die (type_die, type_cu);
22333 }
22334 else if (attr->form == DW_FORM_ref_sig8)
22335 {
22336 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22337 }
22338 else
22339 {
518817b3
SM
22340 struct dwarf2_per_objfile *dwarf2_per_objfile
22341 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22342
b98664d3 22343 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22344 " at %s [in module %s]"),
22345 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22346 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22347 return build_error_marker_type (cu, die);
22348 }
348e048f
DE
22349}
22350
e5fe5e75 22351/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22352
22353static void
e5fe5e75 22354load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22355{
52dc124a 22356 struct signatured_type *sig_type;
348e048f 22357
f4dc4d17 22358 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22359 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22360
6721b2ec
DE
22361 /* We have the per_cu, but we need the signatured_type.
22362 Fortunately this is an easy translation. */
22363 gdb_assert (per_cu->is_debug_types);
22364 sig_type = (struct signatured_type *) per_cu;
348e048f 22365
6721b2ec 22366 gdb_assert (per_cu->cu == NULL);
348e048f 22367
52dc124a 22368 read_signatured_type (sig_type);
348e048f 22369
6721b2ec 22370 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22371}
22372
3019eac3
DE
22373/* Read in a signatured type and build its CU and DIEs.
22374 If the type is a stub for the real type in a DWO file,
22375 read in the real type from the DWO file as well. */
dee91e82
DE
22376
22377static void
22378read_signatured_type (struct signatured_type *sig_type)
22379{
22380 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22381
3019eac3 22382 gdb_assert (per_cu->is_debug_types);
dee91e82 22383 gdb_assert (per_cu->cu == NULL);
348e048f 22384
6751ebae 22385 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22386
22387 if (!reader.dummy_p)
22388 {
22389 struct dwarf2_cu *cu = reader.cu;
22390 const gdb_byte *info_ptr = reader.info_ptr;
22391
22392 gdb_assert (cu->die_hash == NULL);
22393 cu->die_hash =
22394 htab_create_alloc_ex (cu->header.length / 12,
22395 die_hash,
22396 die_eq,
22397 NULL,
22398 &cu->comp_unit_obstack,
22399 hashtab_obstack_allocate,
22400 dummy_obstack_deallocate);
22401
3e225074 22402 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22403 reader.comp_unit_die->child
22404 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22405 reader.comp_unit_die);
22406 cu->dies = reader.comp_unit_die;
22407 /* comp_unit_die is not stored in die_hash, no need. */
22408
22409 /* We try not to read any attributes in this function, because
22410 not all CUs needed for references have been loaded yet, and
22411 symbol table processing isn't initialized. But we have to
22412 set the CU language, or we won't be able to build types
22413 correctly. Similarly, if we do not read the producer, we can
22414 not apply producer-specific interpretation. */
22415 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22416
22417 reader.keep ();
c0ab21c2
TT
22418 }
22419
7ee85ab1 22420 sig_type->per_cu.tu_read = 1;
c906108c
SS
22421}
22422
c906108c
SS
22423/* Decode simple location descriptions.
22424 Given a pointer to a dwarf block that defines a location, compute
22425 the location and return the value.
22426
4cecd739
DJ
22427 NOTE drow/2003-11-18: This function is called in two situations
22428 now: for the address of static or global variables (partial symbols
22429 only) and for offsets into structures which are expected to be
22430 (more or less) constant. The partial symbol case should go away,
22431 and only the constant case should remain. That will let this
22432 function complain more accurately. A few special modes are allowed
22433 without complaint for global variables (for instance, global
22434 register values and thread-local values).
c906108c
SS
22435
22436 A location description containing no operations indicates that the
4cecd739 22437 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22438 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22439 callers will only want a very basic result and this can become a
21ae7a4d
JK
22440 complaint.
22441
22442 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22443
22444static CORE_ADDR
e7c27a73 22445decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22446{
518817b3 22447 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22448 size_t i;
22449 size_t size = blk->size;
d521ce57 22450 const gdb_byte *data = blk->data;
21ae7a4d
JK
22451 CORE_ADDR stack[64];
22452 int stacki;
22453 unsigned int bytes_read, unsnd;
22454 gdb_byte op;
c906108c 22455
21ae7a4d
JK
22456 i = 0;
22457 stacki = 0;
22458 stack[stacki] = 0;
22459 stack[++stacki] = 0;
22460
22461 while (i < size)
22462 {
22463 op = data[i++];
22464 switch (op)
22465 {
22466 case DW_OP_lit0:
22467 case DW_OP_lit1:
22468 case DW_OP_lit2:
22469 case DW_OP_lit3:
22470 case DW_OP_lit4:
22471 case DW_OP_lit5:
22472 case DW_OP_lit6:
22473 case DW_OP_lit7:
22474 case DW_OP_lit8:
22475 case DW_OP_lit9:
22476 case DW_OP_lit10:
22477 case DW_OP_lit11:
22478 case DW_OP_lit12:
22479 case DW_OP_lit13:
22480 case DW_OP_lit14:
22481 case DW_OP_lit15:
22482 case DW_OP_lit16:
22483 case DW_OP_lit17:
22484 case DW_OP_lit18:
22485 case DW_OP_lit19:
22486 case DW_OP_lit20:
22487 case DW_OP_lit21:
22488 case DW_OP_lit22:
22489 case DW_OP_lit23:
22490 case DW_OP_lit24:
22491 case DW_OP_lit25:
22492 case DW_OP_lit26:
22493 case DW_OP_lit27:
22494 case DW_OP_lit28:
22495 case DW_OP_lit29:
22496 case DW_OP_lit30:
22497 case DW_OP_lit31:
22498 stack[++stacki] = op - DW_OP_lit0;
22499 break;
f1bea926 22500
21ae7a4d
JK
22501 case DW_OP_reg0:
22502 case DW_OP_reg1:
22503 case DW_OP_reg2:
22504 case DW_OP_reg3:
22505 case DW_OP_reg4:
22506 case DW_OP_reg5:
22507 case DW_OP_reg6:
22508 case DW_OP_reg7:
22509 case DW_OP_reg8:
22510 case DW_OP_reg9:
22511 case DW_OP_reg10:
22512 case DW_OP_reg11:
22513 case DW_OP_reg12:
22514 case DW_OP_reg13:
22515 case DW_OP_reg14:
22516 case DW_OP_reg15:
22517 case DW_OP_reg16:
22518 case DW_OP_reg17:
22519 case DW_OP_reg18:
22520 case DW_OP_reg19:
22521 case DW_OP_reg20:
22522 case DW_OP_reg21:
22523 case DW_OP_reg22:
22524 case DW_OP_reg23:
22525 case DW_OP_reg24:
22526 case DW_OP_reg25:
22527 case DW_OP_reg26:
22528 case DW_OP_reg27:
22529 case DW_OP_reg28:
22530 case DW_OP_reg29:
22531 case DW_OP_reg30:
22532 case DW_OP_reg31:
22533 stack[++stacki] = op - DW_OP_reg0;
22534 if (i < size)
22535 dwarf2_complex_location_expr_complaint ();
22536 break;
c906108c 22537
21ae7a4d
JK
22538 case DW_OP_regx:
22539 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22540 i += bytes_read;
22541 stack[++stacki] = unsnd;
22542 if (i < size)
22543 dwarf2_complex_location_expr_complaint ();
22544 break;
c906108c 22545
21ae7a4d 22546 case DW_OP_addr:
c8a7a66f
TT
22547 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22548 &bytes_read);
21ae7a4d
JK
22549 i += bytes_read;
22550 break;
d53d4ac5 22551
21ae7a4d
JK
22552 case DW_OP_const1u:
22553 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22554 i += 1;
22555 break;
22556
22557 case DW_OP_const1s:
22558 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22559 i += 1;
22560 break;
22561
22562 case DW_OP_const2u:
22563 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22564 i += 2;
22565 break;
22566
22567 case DW_OP_const2s:
22568 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22569 i += 2;
22570 break;
d53d4ac5 22571
21ae7a4d
JK
22572 case DW_OP_const4u:
22573 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22574 i += 4;
22575 break;
22576
22577 case DW_OP_const4s:
22578 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22579 i += 4;
22580 break;
22581
585861ea
JK
22582 case DW_OP_const8u:
22583 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22584 i += 8;
22585 break;
22586
21ae7a4d
JK
22587 case DW_OP_constu:
22588 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22589 &bytes_read);
22590 i += bytes_read;
22591 break;
22592
22593 case DW_OP_consts:
22594 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22595 i += bytes_read;
22596 break;
22597
22598 case DW_OP_dup:
22599 stack[stacki + 1] = stack[stacki];
22600 stacki++;
22601 break;
22602
22603 case DW_OP_plus:
22604 stack[stacki - 1] += stack[stacki];
22605 stacki--;
22606 break;
22607
22608 case DW_OP_plus_uconst:
22609 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22610 &bytes_read);
22611 i += bytes_read;
22612 break;
22613
22614 case DW_OP_minus:
22615 stack[stacki - 1] -= stack[stacki];
22616 stacki--;
22617 break;
22618
22619 case DW_OP_deref:
22620 /* If we're not the last op, then we definitely can't encode
22621 this using GDB's address_class enum. This is valid for partial
22622 global symbols, although the variable's address will be bogus
22623 in the psymtab. */
22624 if (i < size)
22625 dwarf2_complex_location_expr_complaint ();
22626 break;
22627
22628 case DW_OP_GNU_push_tls_address:
4aa4e28b 22629 case DW_OP_form_tls_address:
21ae7a4d
JK
22630 /* The top of the stack has the offset from the beginning
22631 of the thread control block at which the variable is located. */
22632 /* Nothing should follow this operator, so the top of stack would
22633 be returned. */
22634 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22635 address will be bogus in the psymtab. Make it always at least
22636 non-zero to not look as a variable garbage collected by linker
22637 which have DW_OP_addr 0. */
21ae7a4d
JK
22638 if (i < size)
22639 dwarf2_complex_location_expr_complaint ();
585861ea 22640 stack[stacki]++;
21ae7a4d
JK
22641 break;
22642
22643 case DW_OP_GNU_uninit:
22644 break;
22645
336d760d 22646 case DW_OP_addrx:
3019eac3 22647 case DW_OP_GNU_addr_index:
49f6c839 22648 case DW_OP_GNU_const_index:
3019eac3
DE
22649 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22650 &bytes_read);
22651 i += bytes_read;
22652 break;
22653
21ae7a4d
JK
22654 default:
22655 {
f39c6ffd 22656 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22657
22658 if (name)
b98664d3 22659 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22660 name);
22661 else
b98664d3 22662 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22663 op);
22664 }
22665
22666 return (stack[stacki]);
d53d4ac5 22667 }
3c6e0cb3 22668
21ae7a4d
JK
22669 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22670 outside of the allocated space. Also enforce minimum>0. */
22671 if (stacki >= ARRAY_SIZE (stack) - 1)
22672 {
b98664d3 22673 complaint (_("location description stack overflow"));
21ae7a4d
JK
22674 return 0;
22675 }
22676
22677 if (stacki <= 0)
22678 {
b98664d3 22679 complaint (_("location description stack underflow"));
21ae7a4d
JK
22680 return 0;
22681 }
22682 }
22683 return (stack[stacki]);
c906108c
SS
22684}
22685
22686/* memory allocation interface */
22687
c906108c 22688static struct dwarf_block *
7b5a2f43 22689dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22690{
8d749320 22691 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22692}
22693
c906108c 22694static struct die_info *
b60c80d6 22695dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22696{
22697 struct die_info *die;
b60c80d6
DJ
22698 size_t size = sizeof (struct die_info);
22699
22700 if (num_attrs > 1)
22701 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22702
b60c80d6 22703 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22704 memset (die, 0, sizeof (struct die_info));
22705 return (die);
22706}
2e276125
JB
22707
22708\f
a036ba48 22709
c90ec28a 22710/* Macro support. */
cf2c3c16 22711
9eac9650
TT
22712/* An overload of dwarf_decode_macros that finds the correct section
22713 and ensures it is read in before calling the other overload. */
22714
22715static void
22716dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22717 int section_is_gnu)
22718{
22719 struct dwarf2_per_objfile *dwarf2_per_objfile
22720 = cu->per_cu->dwarf2_per_objfile;
22721 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 22722 const struct line_header *lh = cu->line_header;
9eac9650
TT
22723 unsigned int offset_size = cu->header.offset_size;
22724 struct dwarf2_section_info *section;
22725 const char *section_name;
22726
22727 if (cu->dwo_unit != nullptr)
22728 {
22729 if (section_is_gnu)
22730 {
22731 section = &cu->dwo_unit->dwo_file->sections.macro;
22732 section_name = ".debug_macro.dwo";
22733 }
22734 else
22735 {
22736 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22737 section_name = ".debug_macinfo.dwo";
22738 }
22739 }
22740 else
22741 {
22742 if (section_is_gnu)
22743 {
22744 section = &dwarf2_per_objfile->macro;
22745 section_name = ".debug_macro";
22746 }
22747 else
22748 {
22749 section = &dwarf2_per_objfile->macinfo;
22750 section_name = ".debug_macinfo";
22751 }
22752 }
22753
22754 section->read (objfile);
22755 if (section->buffer == nullptr)
22756 {
22757 complaint (_("missing %s section"), section_name);
22758 return;
22759 }
22760
22761 buildsym_compunit *builder = cu->get_builder ();
22762
22763 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22764 offset_size, offset, section_is_gnu);
22765}
22766
3019eac3
DE
22767/* Return the .debug_loc section to use for CU.
22768 For DWO files use .debug_loc.dwo. */
22769
22770static struct dwarf2_section_info *
22771cu_debug_loc_section (struct dwarf2_cu *cu)
22772{
518817b3
SM
22773 struct dwarf2_per_objfile *dwarf2_per_objfile
22774 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22775
3019eac3 22776 if (cu->dwo_unit)
43988095
JK
22777 {
22778 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 22779
43988095
JK
22780 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22781 }
22782 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22783 : &dwarf2_per_objfile->loc);
3019eac3
DE
22784}
22785
8cf6f0b1
TT
22786/* A helper function that fills in a dwarf2_loclist_baton. */
22787
22788static void
22789fill_in_loclist_baton (struct dwarf2_cu *cu,
22790 struct dwarf2_loclist_baton *baton,
ff39bb5e 22791 const struct attribute *attr)
8cf6f0b1 22792{
518817b3
SM
22793 struct dwarf2_per_objfile *dwarf2_per_objfile
22794 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
22795 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22796
96b79293 22797 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
22798
22799 baton->per_cu = cu->per_cu;
22800 gdb_assert (baton->per_cu);
22801 /* We don't know how long the location list is, but make sure we
22802 don't run off the edge of the section. */
3019eac3
DE
22803 baton->size = section->size - DW_UNSND (attr);
22804 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
22805 if (cu->base_address.has_value ())
22806 baton->base_address = *cu->base_address;
22807 else
22808 baton->base_address = 0;
f664829e 22809 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22810}
22811
4c2df51b 22812static void
ff39bb5e 22813dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22814 struct dwarf2_cu *cu, int is_block)
4c2df51b 22815{
518817b3
SM
22816 struct dwarf2_per_objfile *dwarf2_per_objfile
22817 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 22818 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22819 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22820
cd6c91b4 22821 if (attr->form_is_section_offset ()
3019eac3 22822 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22823 the section. If so, fall through to the complaint in the
22824 other branch. */
2c7d5afc 22825 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 22826 {
0d53c4c4 22827 struct dwarf2_loclist_baton *baton;
4c2df51b 22828
8d749320 22829 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22830
8cf6f0b1 22831 fill_in_loclist_baton (cu, baton, attr);
be391dca 22832
2b24b6e4 22833 if (!cu->base_address.has_value ())
b98664d3 22834 complaint (_("Location list used without "
3e43a32a 22835 "specifying the CU base address."));
4c2df51b 22836
f1e6e072
TT
22837 SYMBOL_ACLASS_INDEX (sym) = (is_block
22838 ? dwarf2_loclist_block_index
22839 : dwarf2_loclist_index);
0d53c4c4
DJ
22840 SYMBOL_LOCATION_BATON (sym) = baton;
22841 }
22842 else
22843 {
22844 struct dwarf2_locexpr_baton *baton;
22845
8d749320 22846 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22847 baton->per_cu = cu->per_cu;
22848 gdb_assert (baton->per_cu);
0d53c4c4 22849
4fc6c0d5 22850 if (attr->form_is_block ())
0d53c4c4
DJ
22851 {
22852 /* Note that we're just copying the block's data pointer
22853 here, not the actual data. We're still pointing into the
6502dd73
DJ
22854 info_buffer for SYM's objfile; right now we never release
22855 that buffer, but when we do clean up properly this may
22856 need to change. */
0d53c4c4
DJ
22857 baton->size = DW_BLOCK (attr)->size;
22858 baton->data = DW_BLOCK (attr)->data;
22859 }
22860 else
22861 {
22862 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 22863 sym->natural_name ());
0d53c4c4 22864 baton->size = 0;
0d53c4c4 22865 }
6e70227d 22866
f1e6e072
TT
22867 SYMBOL_ACLASS_INDEX (sym) = (is_block
22868 ? dwarf2_locexpr_block_index
22869 : dwarf2_locexpr_index);
0d53c4c4
DJ
22870 SYMBOL_LOCATION_BATON (sym) = baton;
22871 }
4c2df51b 22872}
6502dd73 22873
09ba997f 22874/* See read.h. */
ae0d2f24
UW
22875
22876struct objfile *
09ba997f 22877dwarf2_per_cu_data::objfile () const
ae0d2f24 22878{
09ba997f 22879 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
22880
22881 /* Return the master objfile, so that we can report and look up the
22882 correct file containing this variable. */
22883 if (objfile->separate_debug_objfile_backlink)
22884 objfile = objfile->separate_debug_objfile_backlink;
22885
22886 return objfile;
22887}
22888
96408a79
SA
22889/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22890 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22891 CU_HEADERP first. */
22892
22893static const struct comp_unit_head *
22894per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 22895 const struct dwarf2_per_cu_data *per_cu)
96408a79 22896{
d521ce57 22897 const gdb_byte *info_ptr;
96408a79
SA
22898
22899 if (per_cu->cu)
22900 return &per_cu->cu->header;
22901
9c541725 22902 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22903
22904 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22905 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22906 rcuh_kind::COMPILE);
96408a79
SA
22907
22908 return cu_headerp;
22909}
22910
09ba997f 22911/* See read.h. */
ae0d2f24 22912
98714339 22913int
09ba997f 22914dwarf2_per_cu_data::addr_size () const
ae0d2f24 22915{
96408a79
SA
22916 struct comp_unit_head cu_header_local;
22917 const struct comp_unit_head *cu_headerp;
c471e790 22918
09ba997f 22919 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22920
22921 return cu_headerp->addr_size;
ae0d2f24
UW
22922}
22923
09ba997f 22924/* See read.h. */
9eae7c52
TT
22925
22926int
09ba997f 22927dwarf2_per_cu_data::offset_size () const
9eae7c52 22928{
96408a79
SA
22929 struct comp_unit_head cu_header_local;
22930 const struct comp_unit_head *cu_headerp;
9c6c53f7 22931
09ba997f 22932 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22933
22934 return cu_headerp->offset_size;
22935}
22936
09ba997f 22937/* See read.h. */
96408a79
SA
22938
22939int
09ba997f 22940dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
22941{
22942 struct comp_unit_head cu_header_local;
22943 const struct comp_unit_head *cu_headerp;
22944
09ba997f 22945 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22946
22947 if (cu_headerp->version == 2)
22948 return cu_headerp->addr_size;
22949 else
22950 return cu_headerp->offset_size;
181cebd4
JK
22951}
22952
09ba997f 22953/* See read.h. */
9aa1f1e3
TT
22954
22955CORE_ADDR
09ba997f 22956dwarf2_per_cu_data::text_offset () const
9aa1f1e3 22957{
09ba997f
TT
22958 struct objfile *objfile = dwarf2_per_objfile->objfile;
22959
22960 return objfile->text_section_offset ();
9aa1f1e3
TT
22961}
22962
09ba997f
TT
22963/* See read.h. */
22964
22965struct type *
22966dwarf2_per_cu_data::addr_type () const
9a49df9d 22967{
09ba997f 22968 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
22969 struct type *void_type = objfile_type (objfile)->builtin_void;
22970 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 22971 int addr_size = this->addr_size ();
9a49df9d
AB
22972
22973 if (TYPE_LENGTH (addr_type) == addr_size)
22974 return addr_type;
22975
09ba997f 22976 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
22977 return addr_type;
22978}
22979
22b6cd70
TT
22980/* A helper function for dwarf2_find_containing_comp_unit that returns
22981 the index of the result, and that searches a vector. It will
22982 return a result even if the offset in question does not actually
22983 occur in any CU. This is separate so that it can be unit
22984 tested. */
ae038cb0 22985
22b6cd70
TT
22986static int
22987dwarf2_find_containing_comp_unit
22988 (sect_offset sect_off,
22989 unsigned int offset_in_dwz,
22990 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 22991{
ae038cb0
DJ
22992 int low, high;
22993
ae038cb0 22994 low = 0;
22b6cd70 22995 high = all_comp_units.size () - 1;
ae038cb0
DJ
22996 while (high > low)
22997 {
36586728 22998 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22999 int mid = low + (high - low) / 2;
9a619af0 23000
22b6cd70 23001 mid_cu = all_comp_units[mid];
36586728 23002 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23003 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23004 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23005 high = mid;
23006 else
23007 low = mid + 1;
23008 }
23009 gdb_assert (low == high);
22b6cd70
TT
23010 return low;
23011}
23012
23013/* Locate the .debug_info compilation unit from CU's objfile which contains
23014 the DIE at OFFSET. Raises an error on failure. */
23015
23016static struct dwarf2_per_cu_data *
23017dwarf2_find_containing_comp_unit (sect_offset sect_off,
23018 unsigned int offset_in_dwz,
23019 struct dwarf2_per_objfile *dwarf2_per_objfile)
23020{
23021 int low
23022 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23023 dwarf2_per_objfile->all_comp_units);
23024 struct dwarf2_per_cu_data *this_cu
23025 = dwarf2_per_objfile->all_comp_units[low];
23026
45b8ae0c 23027 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23028 {
36586728 23029 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23030 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23031 "offset %s [in module %s]"),
23032 sect_offset_str (sect_off),
ed2dc618 23033 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23034
9c541725
PA
23035 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23036 <= sect_off);
ae038cb0
DJ
23037 return dwarf2_per_objfile->all_comp_units[low-1];
23038 }
23039 else
23040 {
b76e467d 23041 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23042 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23043 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23044 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23045 return this_cu;
23046 }
23047}
23048
22b6cd70
TT
23049#if GDB_SELF_TEST
23050
23051namespace selftests {
23052namespace find_containing_comp_unit {
23053
23054static void
23055run_test ()
23056{
23057 struct dwarf2_per_cu_data one {};
23058 struct dwarf2_per_cu_data two {};
23059 struct dwarf2_per_cu_data three {};
23060 struct dwarf2_per_cu_data four {};
23061
23062 one.length = 5;
23063 two.sect_off = sect_offset (one.length);
23064 two.length = 7;
23065
23066 three.length = 5;
23067 three.is_dwz = 1;
23068 four.sect_off = sect_offset (three.length);
23069 four.length = 7;
23070 four.is_dwz = 1;
23071
23072 std::vector<dwarf2_per_cu_data *> units;
23073 units.push_back (&one);
23074 units.push_back (&two);
23075 units.push_back (&three);
23076 units.push_back (&four);
23077
23078 int result;
23079
23080 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23081 SELF_CHECK (units[result] == &one);
23082 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23083 SELF_CHECK (units[result] == &one);
23084 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23085 SELF_CHECK (units[result] == &two);
23086
23087 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23088 SELF_CHECK (units[result] == &three);
23089 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23090 SELF_CHECK (units[result] == &three);
23091 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23092 SELF_CHECK (units[result] == &four);
23093}
23094
23095}
23096}
23097
23098#endif /* GDB_SELF_TEST */
23099
23745b47 23100/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23101
fcd3b13d
SM
23102dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23103 : per_cu (per_cu_),
9068261f
AB
23104 mark (false),
23105 has_loclist (false),
23106 checked_producer (false),
23107 producer_is_gxx_lt_4_6 (false),
23108 producer_is_gcc_lt_4_3 (false),
eb77c9df 23109 producer_is_icc (false),
9068261f 23110 producer_is_icc_lt_14 (false),
c258c396 23111 producer_is_codewarrior (false),
9068261f 23112 processing_has_namespace_info (false)
93311388 23113{
fcd3b13d
SM
23114 per_cu->cu = this;
23115}
23116
23117/* Destroy a dwarf2_cu. */
23118
23119dwarf2_cu::~dwarf2_cu ()
23120{
23121 per_cu->cu = NULL;
9816fde3
JK
23122}
23123
23124/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23125
23126static void
95554aad
TT
23127prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23128 enum language pretend_language)
9816fde3
JK
23129{
23130 struct attribute *attr;
23131
23132 /* Set the language we're debugging. */
23133 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23134 if (attr != nullptr)
9816fde3
JK
23135 set_cu_language (DW_UNSND (attr), cu);
23136 else
9cded63f 23137 {
95554aad 23138 cu->language = pretend_language;
9cded63f
TT
23139 cu->language_defn = language_def (cu->language);
23140 }
dee91e82 23141
7d45c7c3 23142 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23143}
23144
ae038cb0
DJ
23145/* Increase the age counter on each cached compilation unit, and free
23146 any that are too old. */
23147
23148static void
ed2dc618 23149age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23150{
23151 struct dwarf2_per_cu_data *per_cu, **last_chain;
23152
23153 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23154 per_cu = dwarf2_per_objfile->read_in_chain;
23155 while (per_cu != NULL)
23156 {
23157 per_cu->cu->last_used ++;
b4f54984 23158 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23159 dwarf2_mark (per_cu->cu);
23160 per_cu = per_cu->cu->read_in_chain;
23161 }
23162
23163 per_cu = dwarf2_per_objfile->read_in_chain;
23164 last_chain = &dwarf2_per_objfile->read_in_chain;
23165 while (per_cu != NULL)
23166 {
23167 struct dwarf2_per_cu_data *next_cu;
23168
23169 next_cu = per_cu->cu->read_in_chain;
23170
23171 if (!per_cu->cu->mark)
23172 {
fcd3b13d 23173 delete per_cu->cu;
ae038cb0
DJ
23174 *last_chain = next_cu;
23175 }
23176 else
23177 last_chain = &per_cu->cu->read_in_chain;
23178
23179 per_cu = next_cu;
23180 }
23181}
23182
23183/* Remove a single compilation unit from the cache. */
23184
23185static void
dee91e82 23186free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23187{
23188 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23189 struct dwarf2_per_objfile *dwarf2_per_objfile
23190 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23191
23192 per_cu = dwarf2_per_objfile->read_in_chain;
23193 last_chain = &dwarf2_per_objfile->read_in_chain;
23194 while (per_cu != NULL)
23195 {
23196 struct dwarf2_per_cu_data *next_cu;
23197
23198 next_cu = per_cu->cu->read_in_chain;
23199
dee91e82 23200 if (per_cu == target_per_cu)
ae038cb0 23201 {
fcd3b13d 23202 delete per_cu->cu;
dee91e82 23203 per_cu->cu = NULL;
ae038cb0
DJ
23204 *last_chain = next_cu;
23205 break;
23206 }
23207 else
23208 last_chain = &per_cu->cu->read_in_chain;
23209
23210 per_cu = next_cu;
23211 }
23212}
23213
dee91e82
DE
23214/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23215 We store these in a hash table separate from the DIEs, and preserve them
23216 when the DIEs are flushed out of cache.
23217
23218 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23219 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23220 or the type may come from a DWO file. Furthermore, while it's more logical
23221 to use per_cu->section+offset, with Fission the section with the data is in
23222 the DWO file but we don't know that section at the point we need it.
23223 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23224 because we can enter the lookup routine, get_die_type_at_offset, from
23225 outside this file, and thus won't necessarily have PER_CU->cu.
23226 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23227
dee91e82 23228struct dwarf2_per_cu_offset_and_type
1c379e20 23229{
dee91e82 23230 const struct dwarf2_per_cu_data *per_cu;
9c541725 23231 sect_offset sect_off;
1c379e20
DJ
23232 struct type *type;
23233};
23234
dee91e82 23235/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23236
23237static hashval_t
dee91e82 23238per_cu_offset_and_type_hash (const void *item)
1c379e20 23239{
9a3c8263
SM
23240 const struct dwarf2_per_cu_offset_and_type *ofs
23241 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23242
9c541725 23243 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23244}
23245
dee91e82 23246/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23247
23248static int
dee91e82 23249per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23250{
9a3c8263
SM
23251 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23252 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23253 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23254 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23255
dee91e82 23256 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23257 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23258}
23259
23260/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23261 table if necessary. For convenience, return TYPE.
23262
23263 The DIEs reading must have careful ordering to:
85102364 23264 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23265 reading current DIE.
23266 * Not trying to dereference contents of still incompletely read in types
23267 while reading in other DIEs.
23268 * Enable referencing still incompletely read in types just by a pointer to
23269 the type without accessing its fields.
23270
23271 Therefore caller should follow these rules:
23272 * Try to fetch any prerequisite types we may need to build this DIE type
23273 before building the type and calling set_die_type.
e71ec853 23274 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23275 possible before fetching more types to complete the current type.
23276 * Make the type as complete as possible before fetching more types. */
1c379e20 23277
f792889a 23278static struct type *
1c379e20
DJ
23279set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23280{
518817b3
SM
23281 struct dwarf2_per_objfile *dwarf2_per_objfile
23282 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23283 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23284 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23285 struct attribute *attr;
23286 struct dynamic_prop prop;
1c379e20 23287
b4ba55a1
JB
23288 /* For Ada types, make sure that the gnat-specific data is always
23289 initialized (if not already set). There are a few types where
23290 we should not be doing so, because the type-specific area is
23291 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23292 where the type-specific area is used to store the floatformat).
23293 But this is not a problem, because the gnat-specific information
23294 is actually not needed for these types. */
23295 if (need_gnat_info (cu)
23296 && TYPE_CODE (type) != TYPE_CODE_FUNC
23297 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23298 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23299 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23300 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23301 && !HAVE_GNAT_AUX_INFO (type))
23302 INIT_GNAT_SPECIFIC (type);
23303
3f2f83dd
KB
23304 /* Read DW_AT_allocated and set in type. */
23305 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23306 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23307 {
09ba997f 23308 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23309 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23310 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23311 }
23312 else if (attr != NULL)
23313 {
b98664d3 23314 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23315 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23316 sect_offset_str (die->sect_off));
3f2f83dd
KB
23317 }
23318
23319 /* Read DW_AT_associated and set in type. */
23320 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23321 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23322 {
09ba997f 23323 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23324 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23325 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23326 }
23327 else if (attr != NULL)
23328 {
b98664d3 23329 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23330 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23331 sect_offset_str (die->sect_off));
3f2f83dd
KB
23332 }
23333
3cdcd0ce
JB
23334 /* Read DW_AT_data_location and set in type. */
23335 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23336 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23337 cu->per_cu->addr_type ()))
50a82047 23338 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23339
dee91e82 23340 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23341 dwarf2_per_objfile->die_type_hash
23342 = htab_up (htab_create_alloc (127,
23343 per_cu_offset_and_type_hash,
23344 per_cu_offset_and_type_eq,
23345 NULL, xcalloc, xfree));
1c379e20 23346
dee91e82 23347 ofs.per_cu = cu->per_cu;
9c541725 23348 ofs.sect_off = die->sect_off;
1c379e20 23349 ofs.type = type;
dee91e82 23350 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23351 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23352 if (*slot)
b98664d3 23353 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23354 sect_offset_str (die->sect_off));
8d749320
SM
23355 *slot = XOBNEW (&objfile->objfile_obstack,
23356 struct dwarf2_per_cu_offset_and_type);
1c379e20 23357 **slot = ofs;
f792889a 23358 return type;
1c379e20
DJ
23359}
23360
9c541725 23361/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23362 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23363
23364static struct type *
9c541725 23365get_die_type_at_offset (sect_offset sect_off,
673bfd45 23366 struct dwarf2_per_cu_data *per_cu)
1c379e20 23367{
dee91e82 23368 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23369 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23370
dee91e82 23371 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23372 return NULL;
1c379e20 23373
dee91e82 23374 ofs.per_cu = per_cu;
9c541725 23375 ofs.sect_off = sect_off;
9a3c8263 23376 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23377 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23378 if (slot)
23379 return slot->type;
23380 else
23381 return NULL;
23382}
23383
02142a6c 23384/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23385 or return NULL if DIE does not have a saved type. */
23386
23387static struct type *
23388get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23389{
9c541725 23390 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23391}
23392
10b3939b
DJ
23393/* Add a dependence relationship from CU to REF_PER_CU. */
23394
23395static void
23396dwarf2_add_dependence (struct dwarf2_cu *cu,
23397 struct dwarf2_per_cu_data *ref_per_cu)
23398{
23399 void **slot;
23400
23401 if (cu->dependencies == NULL)
23402 cu->dependencies
23403 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23404 NULL, &cu->comp_unit_obstack,
23405 hashtab_obstack_allocate,
23406 dummy_obstack_deallocate);
23407
23408 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23409 if (*slot == NULL)
23410 *slot = ref_per_cu;
23411}
1c379e20 23412
f504f079
DE
23413/* Subroutine of dwarf2_mark to pass to htab_traverse.
23414 Set the mark field in every compilation unit in the
ae038cb0
DJ
23415 cache that we must keep because we are keeping CU. */
23416
10b3939b
DJ
23417static int
23418dwarf2_mark_helper (void **slot, void *data)
23419{
23420 struct dwarf2_per_cu_data *per_cu;
23421
23422 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23423
23424 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23425 reading of the chain. As such dependencies remain valid it is not much
23426 useful to track and undo them during QUIT cleanups. */
23427 if (per_cu->cu == NULL)
23428 return 1;
23429
10b3939b
DJ
23430 if (per_cu->cu->mark)
23431 return 1;
9068261f 23432 per_cu->cu->mark = true;
10b3939b
DJ
23433
23434 if (per_cu->cu->dependencies != NULL)
23435 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23436
23437 return 1;
23438}
23439
f504f079
DE
23440/* Set the mark field in CU and in every other compilation unit in the
23441 cache that we must keep because we are keeping CU. */
23442
ae038cb0
DJ
23443static void
23444dwarf2_mark (struct dwarf2_cu *cu)
23445{
23446 if (cu->mark)
23447 return;
9068261f 23448 cu->mark = true;
10b3939b
DJ
23449 if (cu->dependencies != NULL)
23450 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23451}
23452
23453static void
23454dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23455{
23456 while (per_cu)
23457 {
9068261f 23458 per_cu->cu->mark = false;
ae038cb0
DJ
23459 per_cu = per_cu->cu->read_in_chain;
23460 }
72bf9492
DJ
23461}
23462
72bf9492
DJ
23463/* Trivial hash function for partial_die_info: the hash value of a DIE
23464 is its offset in .debug_info for this objfile. */
23465
23466static hashval_t
23467partial_die_hash (const void *item)
23468{
9a3c8263
SM
23469 const struct partial_die_info *part_die
23470 = (const struct partial_die_info *) item;
9a619af0 23471
9c541725 23472 return to_underlying (part_die->sect_off);
72bf9492
DJ
23473}
23474
23475/* Trivial comparison function for partial_die_info structures: two DIEs
23476 are equal if they have the same offset. */
23477
23478static int
23479partial_die_eq (const void *item_lhs, const void *item_rhs)
23480{
9a3c8263
SM
23481 const struct partial_die_info *part_die_lhs
23482 = (const struct partial_die_info *) item_lhs;
23483 const struct partial_die_info *part_die_rhs
23484 = (const struct partial_die_info *) item_rhs;
9a619af0 23485
9c541725 23486 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23487}
23488
3c3bb058
AB
23489struct cmd_list_element *set_dwarf_cmdlist;
23490struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23491
9291a0cd 23492static void
cd4fb1b2
SM
23493show_check_physname (struct ui_file *file, int from_tty,
23494 struct cmd_list_element *c, const char *value)
9291a0cd 23495{
cd4fb1b2
SM
23496 fprintf_filtered (file,
23497 _("Whether to check \"physname\" is %s.\n"),
23498 value);
9291a0cd
TT
23499}
23500
6c265988 23501void _initialize_dwarf2_read ();
cd4fb1b2 23502void
6c265988 23503_initialize_dwarf2_read ()
9291a0cd 23504{
0743fc83 23505 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23506Set DWARF specific variables.\n\
590042fc 23507Configure DWARF variables such as the cache size."),
0743fc83
TT
23508 &set_dwarf_cmdlist, "maintenance set dwarf ",
23509 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23510
0743fc83 23511 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23512Show DWARF specific variables.\n\
23513Show DWARF variables such as the cache size."),
0743fc83
TT
23514 &show_dwarf_cmdlist, "maintenance show dwarf ",
23515 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23516
cd4fb1b2
SM
23517 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23518 &dwarf_max_cache_age, _("\
23519Set the upper bound on the age of cached DWARF compilation units."), _("\
23520Show the upper bound on the age of cached DWARF compilation units."), _("\
23521A higher limit means that cached compilation units will be stored\n\
23522in memory longer, and more total memory will be used. Zero disables\n\
23523caching, which can slow down startup."),
23524 NULL,
23525 show_dwarf_max_cache_age,
23526 &set_dwarf_cmdlist,
23527 &show_dwarf_cmdlist);
156942c7 23528
cd4fb1b2
SM
23529 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23530Set debugging of the DWARF reader."), _("\
23531Show debugging of the DWARF reader."), _("\
23532When enabled (non-zero), debugging messages are printed during DWARF\n\
23533reading and symtab expansion. A value of 1 (one) provides basic\n\
23534information. A value greater than 1 provides more verbose information."),
23535 NULL,
23536 NULL,
23537 &setdebuglist, &showdebuglist);
9291a0cd 23538
cd4fb1b2
SM
23539 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23540Set debugging of the DWARF DIE reader."), _("\
23541Show debugging of the DWARF DIE reader."), _("\
23542When enabled (non-zero), DIEs are dumped after they are read in.\n\
23543The value is the maximum depth to print."),
23544 NULL,
23545 NULL,
23546 &setdebuglist, &showdebuglist);
9291a0cd 23547
cd4fb1b2
SM
23548 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23549Set debugging of the dwarf line reader."), _("\
23550Show debugging of the dwarf line reader."), _("\
23551When enabled (non-zero), line number entries are dumped as they are read in.\n\
23552A value of 1 (one) provides basic information.\n\
23553A value greater than 1 provides more verbose information."),
23554 NULL,
23555 NULL,
23556 &setdebuglist, &showdebuglist);
437afbb8 23557
cd4fb1b2
SM
23558 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23559Set cross-checking of \"physname\" code against demangler."), _("\
23560Show cross-checking of \"physname\" code against demangler."), _("\
23561When enabled, GDB's internal \"physname\" code is checked against\n\
23562the demangler."),
23563 NULL, show_check_physname,
23564 &setdebuglist, &showdebuglist);
900e11f9 23565
e615022a
DE
23566 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23567 no_class, &use_deprecated_index_sections, _("\
23568Set whether to use deprecated gdb_index sections."), _("\
23569Show whether to use deprecated gdb_index sections."), _("\
23570When enabled, deprecated .gdb_index sections are used anyway.\n\
23571Normally they are ignored either because of a missing feature or\n\
23572performance issue.\n\
23573Warning: This option must be enabled before gdb reads the file."),
23574 NULL,
23575 NULL,
23576 &setlist, &showlist);
23577
f1e6e072
TT
23578 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23579 &dwarf2_locexpr_funcs);
23580 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23581 &dwarf2_loclist_funcs);
23582
23583 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23584 &dwarf2_block_frame_base_locexpr_funcs);
23585 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23586 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23587
23588#if GDB_SELF_TEST
23589 selftests::register_test ("dw2_expand_symtabs_matching",
23590 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23591 selftests::register_test ("dwarf2_find_containing_comp_unit",
23592 selftests::find_containing_comp_unit::run_test);
c62446b1 23593#endif
6502dd73 23594}
This page took 5.389428 seconds and 4 git commands to generate.