Add dwarf2_per_objfile parameter to cutu_reader's constructors
[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{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
d00adf39 424 /* The header of the compilation unit. */
fcd3b13d 425 struct comp_unit_head header {};
e142c38c 426
d00adf39 427 /* Base address of this compilation unit. */
2b24b6e4 428 gdb::optional<CORE_ADDR> base_address;
d00adf39 429
e142c38c 430 /* The language we are debugging. */
fcd3b13d
SM
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
e142c38c 433
fcd3b13d 434 const char *producer = nullptr;
b0f35d58 435
c24bdb02 436private:
804d2729
TT
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
c24bdb02 439 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 440
c24bdb02 441public:
e142c38c
DJ
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
fcd3b13d 451 struct pending **list_in_scope = nullptr;
e142c38c 452
b64f50a1
JK
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 455 htab_t partial_dies = nullptr;
72bf9492
DJ
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
fcd3b13d 459 auto_obstack comp_unit_obstack;
72bf9492 460
ae038cb0
DJ
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
fcd3b13d 465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 466
69d751e3 467 /* Backlink to our per_cu entry. */
ae038cb0
DJ
468 struct dwarf2_per_cu_data *per_cu;
469
9e021579
SM
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
ae038cb0 473 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 474 int last_used = 0;
ae038cb0 475
b64f50a1
JK
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
fcd3b13d 478 htab_t die_hash = nullptr;
10b3939b
DJ
479
480 /* Full DIEs if read in. */
fcd3b13d 481 struct die_info *dies = nullptr;
10b3939b
DJ
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
fcd3b13d 486 htab_t dependencies = nullptr;
10b3939b 487
cb1df416 488 /* Header data from the line table, during full symbol processing. */
fcd3b13d 489 struct line_header *line_header = nullptr;
4c8aa72d 490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
fcd3b13d 495 die_info *line_header_die_owner = nullptr;
cb1df416 496
3da10d80
KS
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
c89b44cd 499 std::vector<delayed_method_info> method_list;
3da10d80 500
96408a79 501 /* To be copied to symtab->call_site_htab. */
fcd3b13d 502 htab_t call_site_htab = nullptr;
96408a79 503
034e5797
DE
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
18a8505e 508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
fcd3b13d 513 struct dwo_unit *dwo_unit = nullptr;
3019eac3 514
18a8505e 515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 517 gdb::optional<ULONGEST> addr_base;
3019eac3 518
18a8505e 519 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 520 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 521 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 528 ULONGEST ranges_base = 0;
2e3cf129 529
41144253 530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
c9317f21
TT
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
18a8505e
AT
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
ae038cb0 547 /* Mark used when releasing cached dies. */
9068261f 548 bool mark : 1;
ae038cb0 549
8be455d7
JK
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 554 bool has_loclist : 1;
ba919b58 555
9068261f 556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
9068261f
AB
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 563 bool producer_is_icc : 1;
9068261f 564 bool producer_is_icc_lt_14 : 1;
c258c396 565 bool producer_is_codewarrior : 1;
4d4ec4e5 566
9068261f 567 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
9068261f 571 bool processing_has_namespace_info : 1;
d590ff25
YQ
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
e7c27a73
DJ
592};
593
094b34ac
DE
594/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597struct stmt_list_hash
598{
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 603 sect_offset line_sect_off;
094b34ac
DE
604};
605
5989a64e 606/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
f4dc4d17
DE
607 an object of this type. */
608
609struct type_unit_group
610{
0186c6a7 611 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
197400e8 614 a "per_cu" handle on the symtab. */
094b34ac
DE
615 struct dwarf2_per_cu_data per_cu;
616
0186c6a7
DE
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
a8b3b8e9 620 std::vector<signatured_type *> *tus;
f4dc4d17 621
43f3e411 622 /* The compunit symtab.
094b34ac 623 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
f4dc4d17 626
094b34ac
DE
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
f4dc4d17 629
f4dc4d17
DE
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639};
640
73869dc2 641/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
642
643struct dwo_sections
644{
645 struct dwarf2_section_info abbrev;
3019eac3
DE
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
43988095 648 struct dwarf2_section_info loclists;
09262596
DE
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
3019eac3
DE
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
80626a55
DE
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
fd5866f6 655 std::vector<dwarf2_section_info> types;
3019eac3
DE
656};
657
c88ee1f0 658/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
659
660struct dwo_unit
661{
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 671 struct dwarf2_section_info *section;
3019eac3 672
9c541725
PA
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
3019eac3
DE
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679};
680
73869dc2
DE
681/* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685enum dwp_v2_section_ids
686{
687 DW_SECT_MIN = 1
688};
689
80626a55 690/* Data for one DWO file.
57d63ce2
DE
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
3019eac3
DE
700
701struct dwo_file
702{
51ac9db5
SM
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
18a8505e 706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
51ac9db5 710 const char *dwo_name = nullptr;
0ac5b59e
DE
711
712 /* The DW_AT_comp_dir attribute. */
51ac9db5 713 const char *comp_dir = nullptr;
3019eac3 714
80626a55
DE
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 717 gdb_bfd_ref_ptr dbfd;
3019eac3 718
73869dc2
DE
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
51ac9db5 722 struct dwo_sections sections {};
3019eac3 723
33c5cd75
DB
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 728 htab_up cus;
3019eac3
DE
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
b0b6a987 732 htab_up tus;
3019eac3
DE
733};
734
80626a55
DE
735/* These sections are what may appear in a DWP file. */
736
737struct dwp_sections
738{
73869dc2 739 /* These are used by both DWP version 1 and 2. */
80626a55
DE
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
73869dc2
DE
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
80626a55
DE
760};
761
73869dc2
DE
762/* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 764
73869dc2 765struct virtual_v1_dwo_sections
80626a55
DE
766{
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 774 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
775 struct dwarf2_section_info info_or_types;
776};
777
73869dc2
DE
778/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783struct virtual_v2_dwo_sections
784{
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807};
808
80626a55
DE
809/* Contents of DWP hash tables. */
810
811struct dwp_hash_table
812{
73869dc2 813 uint32_t version, nr_columns;
80626a55 814 uint32_t nr_units, nr_slots;
73869dc2
DE
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826#define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
80626a55
DE
838};
839
840/* Data for one DWP file. */
841
842struct dwp_file
843{
400174b1
TT
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
80626a55
DE
850 /* Name of the file. */
851 const char *name;
852
73869dc2 853 /* File format version. */
400174b1 854 int version = 0;
73869dc2 855
93417882 856 /* The bfd. */
400174b1 857 gdb_bfd_ref_ptr dbfd;
80626a55
DE
858
859 /* Section info for this file. */
400174b1 860 struct dwp_sections sections {};
80626a55 861
57d63ce2 862 /* Table of CUs in the file. */
400174b1 863 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
864
865 /* Table of TUs in the file. */
400174b1 866 const struct dwp_hash_table *tus = nullptr;
80626a55 867
19ac8c2e 868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
869 htab_up loaded_cus;
870 htab_up loaded_tus;
80626a55 871
73869dc2
DE
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
400174b1
TT
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
80626a55
DE
876};
877
0963b4bd
MS
878/* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
dee91e82 881 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
882
883struct die_reader_specs
884{
a32a8923 885 /* The bfd of die_section. */
93311388
DE
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
80626a55 891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
892 struct dwo_file *dwo_file;
893
dee91e82 894 /* The section the die comes from.
3019eac3 895 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
d521ce57 899 const gdb_byte *buffer;
f664829e
DE
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
a2ce51a0 903
685af9cd
TT
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
93311388
DE
906};
907
c0ab21c2
TT
908/* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911class cutu_reader : public die_reader_specs
912{
913public:
914
ab432490
SM
915 cutu_reader (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
c0ab21c2 917 struct abbrev_table *abbrev_table,
6751ebae 918 int use_existing_cu,
c0ab21c2
TT
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 922 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
c0ab21c2
TT
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
930 bool dummy_p = false;
931
6751ebae
TT
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
c0ab21c2 936private:
9e021579
SM
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
6751ebae 939 int use_existing_cu);
c0ab21c2
TT
940
941 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949};
dee91e82 950
c906108c 951/* When we construct a partial symbol table entry we only
0963b4bd 952 need this much information. */
6f06d47b 953struct partial_die_info : public allocate_on_obstack
c906108c 954 {
6f06d47b
YQ
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
52356b79
YQ
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
48fbe735
YQ
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
72bf9492 972 /* Offset of this DIE. */
6f06d47b 973 const sect_offset sect_off;
72bf9492
DJ
974
975 /* DWARF-2 tag for this DIE. */
6f06d47b 976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 977
72bf9492 978 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
979 const unsigned int has_children : 1;
980
72bf9492
DJ
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
481860b3 986 unsigned int may_be_inlined : 1;
72bf9492 987
0c1b455e
TT
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
72bf9492
DJ
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
fa4028e9
JB
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
ff908ebf
AW
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
98bfdba5
PA
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
52356b79 1004 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1005 unsigned int fixup_called : 1;
1006
36586728
TT
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
72bf9492 1013 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1014 sometimes a default name for unnamed DIEs. */
6f06d47b 1015 const char *name = nullptr;
72bf9492 1016
abc72ce4 1017 /* The linkage name, if present. */
6f06d47b 1018 const char *linkage_name = nullptr;
abc72ce4 1019
72bf9492
DJ
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
6f06d47b 1023 const char *scope = nullptr;
72bf9492 1024
95554aad
TT
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1032 sect_offset sect_off;
6f06d47b 1033 } d {};
72bf9492
DJ
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
72bf9492 1038
93311388 1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1040 DW_AT_sibling, if any. */
48fbe735
YQ
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1043 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
6f06d47b 1048 sect_offset spec_offset {};
72bf9492
DJ
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
6f06d47b
YQ
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
c906108c
SS
1085 };
1086
c906108c
SS
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
9c6a1327
TT
1092struct variant_part_builder;
1093
1094/* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
2ddeaf8a
TT
1096
1097struct variant_field
1098{
9c6a1327
TT
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
2ddeaf8a
TT
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
9c6a1327
TT
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114};
1115
1116/* This represents a DW_TAG_variant_part. */
1117
1118struct variant_part_builder
1119{
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
2ddeaf8a
TT
1128};
1129
52059ffd
TT
1130struct nextfield
1131{
be2daae6
TT
1132 int accessibility = 0;
1133 int virtuality = 0;
9c6a1327
TT
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
be2daae6 1138 struct field field {};
52059ffd
TT
1139};
1140
1141struct fnfieldlist
1142{
be2daae6
TT
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
52059ffd
TT
1145};
1146
c906108c
SS
1147/* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150struct field_info
c5aa993b 1151 {
0963b4bd 1152 /* List of data member and baseclasses fields. */
be2daae6
TT
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
c906108c 1155
85102364 1156 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1157 int non_public_fields = 0;
c906108c 1158
c5aa993b
JM
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
be2daae6 1162 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1166 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
be2daae6 1170 std::vector<struct decl_field> nested_types_list;
317f7127 1171
9c6a1327
TT
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
317f7127
TT
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
c5aa993b 1184 };
c906108c 1185
ae038cb0
DJ
1186/* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1191static int dwarf_max_cache_age = 5;
920d2a44 1192static void
b4f54984
DE
1193show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
920d2a44 1195{
3e43a32a 1196 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1197 "DWARF compilation units is %s.\n"),
920d2a44
AC
1198 value);
1199}
4390d890 1200\f
c906108c
SS
1201/* local function prototypes */
1202
918dd910
JK
1203static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
891813be 1206static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1207 (struct dwarf2_per_cu_data *per_cu, const char *name);
1208
f1902523
JK
1209static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1210 const gdb_byte *info_ptr,
3e225074 1211 struct die_info *type_unit_die);
f1902523 1212
ed2dc618
SM
1213static void dwarf2_build_psymtabs_hard
1214 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1215
72bf9492
DJ
1216static void scan_partial_symbols (struct partial_die_info *,
1217 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1218 int, struct dwarf2_cu *);
c906108c 1219
72bf9492
DJ
1220static void add_partial_symbol (struct partial_die_info *,
1221 struct dwarf2_cu *);
63d06c5c 1222
72bf9492
DJ
1223static void add_partial_namespace (struct partial_die_info *pdi,
1224 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1225 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1226
5d7cb8df 1227static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1228 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1229 struct dwarf2_cu *cu);
1230
72bf9492
DJ
1231static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1232 struct dwarf2_cu *cu);
91c24f0a 1233
bc30ff58
JB
1234static void add_partial_subprogram (struct partial_die_info *pdi,
1235 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1236 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1237
d521ce57 1238static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1239
dee91e82 1240static struct partial_die_info *load_partial_dies
d521ce57 1241 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1242
fb816e8b
TV
1243/* A pair of partial_die_info and compilation unit. */
1244struct cu_partial_die_info
1245{
1246 /* The compilation unit of the partial_die_info. */
1247 struct dwarf2_cu *cu;
1248 /* A partial_die_info. */
1249 struct partial_die_info *pdi;
122cf0f2
AB
1250
1251 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1252 : cu (cu),
1253 pdi (pdi)
405feb71 1254 { /* Nothing. */ }
122cf0f2
AB
1255
1256private:
1257 cu_partial_die_info () = delete;
fb816e8b
TV
1258};
1259
122cf0f2
AB
1260static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1261 struct dwarf2_cu *);
72bf9492 1262
d521ce57
TT
1263static const gdb_byte *read_attribute (const struct die_reader_specs *,
1264 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1265 const gdb_byte *, bool *need_reprocess);
1266
1267static void read_attribute_reprocess (const struct die_reader_specs *reader,
1268 struct attribute *attr);
1269
1270static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1271
ed2dc618
SM
1272static sect_offset read_abbrev_offset
1273 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1274 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1275
ed2dc618
SM
1276static const char *read_indirect_string
1277 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1278 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1279
ed2dc618 1280static const char *read_indirect_string_at_offset
4f44ae6c 1281 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1282
d521ce57
TT
1283static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1284 const gdb_byte *,
3019eac3
DE
1285 unsigned int *);
1286
18a8505e
AT
1287static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1288 ULONGEST str_index);
1289
1290static const char *read_stub_str_index (struct dwarf2_cu *cu,
1291 ULONGEST str_index);
3019eac3 1292
e142c38c 1293static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1294
e142c38c
DJ
1295static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1296 struct dwarf2_cu *);
c906108c 1297
7d45c7c3
KB
1298static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1299 struct dwarf2_cu *cu);
1300
a084a2a6
AT
1301static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1302
05cf31d1
JB
1303static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1304 struct dwarf2_cu *cu);
1305
e142c38c 1306static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1307
e142c38c 1308static struct die_info *die_specification (struct die_info *die,
f2f0e013 1309 struct dwarf2_cu **);
63d06c5c 1310
9c541725 1311static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1312 struct dwarf2_cu *cu);
debd256d 1313
f3f5162e 1314static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1315 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1316 CORE_ADDR, int decode_mapping);
c906108c 1317
804d2729
TT
1318static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1319 const char *);
c906108c 1320
a14ed312 1321static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1322 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1323
ff39bb5e 1324static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1325 struct dwarf2_cu *);
c906108c 1326
ff39bb5e 1327static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1328 struct type *type,
1329 const char *name,
1330 struct obstack *obstack,
12df843f 1331 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1332 const gdb_byte **bytes,
98bfdba5 1333 struct dwarf2_locexpr_baton **baton);
2df3850c 1334
e7c27a73 1335static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1336
b4ba55a1
JB
1337static int need_gnat_info (struct dwarf2_cu *);
1338
3e43a32a
MS
1339static struct type *die_descriptive_type (struct die_info *,
1340 struct dwarf2_cu *);
b4ba55a1
JB
1341
1342static void set_descriptive_type (struct type *, struct die_info *,
1343 struct dwarf2_cu *);
1344
e7c27a73
DJ
1345static struct type *die_containing_type (struct die_info *,
1346 struct dwarf2_cu *);
c906108c 1347
ff39bb5e 1348static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1349 struct dwarf2_cu *);
c906108c 1350
f792889a 1351static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1352
673bfd45
DE
1353static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1354
0d5cff50 1355static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1356
6e70227d 1357static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1358 const char *suffix, int physname,
1359 struct dwarf2_cu *cu);
63d06c5c 1360
e7c27a73 1361static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1362
348e048f
DE
1363static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1364
e7c27a73 1365static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1366
e7c27a73 1367static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1368
96408a79
SA
1369static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1370
71a3c369
TT
1371static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1372
ff013f42 1373static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1374 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1375
41144253 1376/* Return the .debug_loclists section to use for cu. */
1377static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1378
3a2b436a 1379/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1380 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1381enum pc_bounds_kind
1382{
e385593e 1383 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1384 PC_BOUNDS_NOT_PRESENT,
1385
e385593e
JK
1386 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1387 were present but they do not form a valid range of PC addresses. */
1388 PC_BOUNDS_INVALID,
1389
3a2b436a
JK
1390 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1391 PC_BOUNDS_RANGES,
1392
1393 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1394 PC_BOUNDS_HIGH_LOW,
1395};
1396
1397static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1398 CORE_ADDR *, CORE_ADDR *,
1399 struct dwarf2_cu *,
891813be 1400 dwarf2_psymtab *);
c906108c 1401
fae299cd
DC
1402static void get_scope_pc_bounds (struct die_info *,
1403 CORE_ADDR *, CORE_ADDR *,
1404 struct dwarf2_cu *);
1405
801e3a5b
JB
1406static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1407 CORE_ADDR, struct dwarf2_cu *);
1408
a14ed312 1409static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1410 struct dwarf2_cu *);
c906108c 1411
a14ed312 1412static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1413 struct type *, struct dwarf2_cu *);
c906108c 1414
a14ed312 1415static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1416 struct die_info *, struct type *,
e7c27a73 1417 struct dwarf2_cu *);
c906108c 1418
a14ed312 1419static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1420 struct type *,
1421 struct dwarf2_cu *);
c906108c 1422
134d01f1 1423static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1424
e7c27a73 1425static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1426
e7c27a73 1427static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1428
5d7cb8df
JK
1429static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1430
804d2729 1431static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1432
27aa8d6a
SW
1433static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1434
74921315
KS
1435static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1436
f55ee35c
JK
1437static struct type *read_module_type (struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
38d518c9 1440static const char *namespace_name (struct die_info *die,
e142c38c 1441 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1442
134d01f1 1443static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1444
7d79de9a
TT
1445static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1446 bool * = nullptr);
c906108c 1447
6e70227d 1448static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1449 struct dwarf2_cu *);
1450
bf6af496 1451static struct die_info *read_die_and_siblings_1
d521ce57 1452 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1453 struct die_info *);
639d11d3 1454
dee91e82 1455static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1456 const gdb_byte *info_ptr,
1457 const gdb_byte **new_info_ptr,
639d11d3
DC
1458 struct die_info *parent);
1459
d521ce57
TT
1460static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1461 struct die_info **, const gdb_byte *,
3e225074 1462 int);
3019eac3 1463
d521ce57 1464static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1465 struct die_info **, const gdb_byte *);
93311388 1466
e7c27a73 1467static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1468
15d034d0 1469static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1470 struct objfile *);
71c25dea 1471
15d034d0 1472static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1473
15d034d0 1474static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1475 struct die_info *die,
1476 struct dwarf2_cu *cu);
1477
ca69b9e6
DE
1478static const char *dwarf2_physname (const char *name, struct die_info *die,
1479 struct dwarf2_cu *cu);
1480
e142c38c 1481static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1482 struct dwarf2_cu **);
9219021c 1483
d97bc12b
DE
1484static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1485
1486static void dump_die_for_error (struct die_info *);
1487
1488static void dump_die_1 (struct ui_file *, int level, int max_level,
1489 struct die_info *);
c906108c 1490
d97bc12b 1491/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1492
51545339 1493static void store_in_ref_table (struct die_info *,
10b3939b 1494 struct dwarf2_cu *);
c906108c 1495
348e048f 1496static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1497 const struct attribute *,
348e048f
DE
1498 struct dwarf2_cu **);
1499
10b3939b 1500static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1501 const struct attribute *,
f2f0e013 1502 struct dwarf2_cu **);
c906108c 1503
348e048f 1504static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1505 const struct attribute *,
348e048f
DE
1506 struct dwarf2_cu **);
1507
ac9ec31b
DE
1508static struct type *get_signatured_type (struct die_info *, ULONGEST,
1509 struct dwarf2_cu *);
1510
1511static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1512 const struct attribute *,
ac9ec31b
DE
1513 struct dwarf2_cu *);
1514
ab432490
SM
1515static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1516 dwarf2_per_objfile *per_objfile);
348e048f 1517
ab432490
SM
1518static void read_signatured_type (signatured_type *sig_type,
1519 dwarf2_per_objfile *per_objfile);
348e048f 1520
63e43d3a
PMR
1521static int attr_to_dynamic_prop (const struct attribute *attr,
1522 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1523 struct dynamic_prop *prop, struct type *type);
63e43d3a 1524
c906108c
SS
1525/* memory allocation interface */
1526
7b5a2f43 1527static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1528
b60c80d6 1529static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1530
43f3e411 1531static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1532
8cf6f0b1
TT
1533static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1534 struct dwarf2_loclist_baton *baton,
ff39bb5e 1535 const struct attribute *attr);
8cf6f0b1 1536
ff39bb5e 1537static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1538 struct symbol *sym,
f1e6e072
TT
1539 struct dwarf2_cu *cu,
1540 int is_block);
4c2df51b 1541
d521ce57
TT
1542static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1543 const gdb_byte *info_ptr,
1544 struct abbrev_info *abbrev);
4bb7a0a7 1545
72bf9492
DJ
1546static hashval_t partial_die_hash (const void *item);
1547
1548static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1549
ae038cb0 1550static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1551 (sect_offset sect_off, unsigned int offset_in_dwz,
1552 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1553
9816fde3 1554static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1555 struct die_info *comp_unit_die,
1556 enum language pretend_language);
93311388 1557
ed2dc618 1558static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1559
dee91e82 1560static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1561
f792889a
DJ
1562static struct type *set_die_type (struct die_info *, struct type *,
1563 struct dwarf2_cu *);
1c379e20 1564
ed2dc618 1565static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1566
ed2dc618 1567static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1568
ab432490
SM
1569static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1570 dwarf2_per_objfile *per_objfile,
1571 bool skip_partial,
1572 enum language pretend_language);
10b3939b 1573
95554aad
TT
1574static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1575 enum language);
10b3939b 1576
f4dc4d17
DE
1577static void process_full_type_unit (struct dwarf2_per_cu_data *,
1578 enum language);
1579
10b3939b
DJ
1580static void dwarf2_add_dependence (struct dwarf2_cu *,
1581 struct dwarf2_per_cu_data *);
1582
ae038cb0
DJ
1583static void dwarf2_mark (struct dwarf2_cu *);
1584
1585static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1586
b64f50a1 1587static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1588 struct dwarf2_per_cu_data *);
673bfd45 1589
f792889a 1590static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1591
95554aad
TT
1592static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1593 enum language pretend_language);
1594
ed2dc618 1595static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1596
b303c6f6
AB
1597/* Class, the destructor of which frees all allocated queue entries. This
1598 will only have work to do if an error was thrown while processing the
1599 dwarf. If no error was thrown then the queue entries should have all
1600 been processed, and freed, as we went along. */
1601
1602class dwarf2_queue_guard
1603{
1604public:
39856def
TT
1605 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1606 : m_per_objfile (per_objfile)
1607 {
1608 }
b303c6f6
AB
1609
1610 /* Free any entries remaining on the queue. There should only be
1611 entries left if we hit an error while processing the dwarf. */
1612 ~dwarf2_queue_guard ()
1613 {
39856def
TT
1614 /* Ensure that no memory is allocated by the queue. */
1615 std::queue<dwarf2_queue_item> empty;
5989a64e 1616 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1617 }
b303c6f6 1618
39856def 1619 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1620
39856def
TT
1621private:
1622 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1623};
1624
39856def
TT
1625dwarf2_queue_item::~dwarf2_queue_item ()
1626{
1627 /* Anything still marked queued is likely to be in an
1628 inconsistent state, so discard it. */
1629 if (per_cu->queued)
1630 {
1631 if (per_cu->cu != NULL)
1632 free_one_cached_comp_unit (per_cu);
1633 per_cu->queued = 0;
1634 }
1635}
1636
d721ba37
PA
1637/* The return type of find_file_and_directory. Note, the enclosed
1638 string pointers are only valid while this object is valid. */
1639
1640struct file_and_directory
1641{
1642 /* The filename. This is never NULL. */
1643 const char *name;
1644
1645 /* The compilation directory. NULL if not known. If we needed to
1646 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1647 points directly to the DW_AT_comp_dir string attribute owned by
1648 the obstack that owns the DIE. */
1649 const char *comp_dir;
1650
1651 /* If we needed to build a new string for comp_dir, this is what
1652 owns the storage. */
1653 std::string comp_dir_storage;
1654};
1655
1656static file_and_directory find_file_and_directory (struct die_info *die,
1657 struct dwarf2_cu *cu);
9291a0cd 1658
298e9637 1659static htab_up allocate_signatured_type_table ();
1fd400ff 1660
298e9637 1661static htab_up allocate_dwo_unit_table ();
3019eac3 1662
57d63ce2 1663static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1664 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1665 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1666 ULONGEST signature, int is_debug_types);
a2ce51a0 1667
ed2dc618
SM
1668static struct dwp_file *get_dwp_file
1669 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1670
3019eac3 1671static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1672 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1673
1674static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1675 (struct signatured_type *, const char *, const char *);
3019eac3 1676
89e63ee4
DE
1677static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1678
263db9a1
TT
1679/* A unique pointer to a dwo_file. */
1680
51ac9db5 1681typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1682
ed2dc618 1683static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1684
1b80a9fa 1685static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1686
1687static void free_line_header_voidp (void *arg);
4390d890
DE
1688\f
1689/* Various complaints about symbol reading that don't abort the process. */
1690
4390d890
DE
1691static void
1692dwarf2_debug_line_missing_file_complaint (void)
1693{
b98664d3 1694 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1695}
1696
1697static void
1698dwarf2_debug_line_missing_end_sequence_complaint (void)
1699{
b98664d3 1700 complaint (_(".debug_line section has line "
4390d890
DE
1701 "program sequence without an end"));
1702}
1703
1704static void
1705dwarf2_complex_location_expr_complaint (void)
1706{
b98664d3 1707 complaint (_("location expression too complex"));
4390d890
DE
1708}
1709
1710static void
1711dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1712 int arg3)
1713{
b98664d3 1714 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1715 arg1, arg2, arg3);
1716}
1717
4390d890
DE
1718static void
1719dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1720{
b98664d3 1721 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1722 arg1, arg2);
1723}
527f3840
JK
1724
1725/* Hash function for line_header_hash. */
1726
1727static hashval_t
1728line_header_hash (const struct line_header *ofs)
1729{
9c541725 1730 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1731}
1732
1733/* Hash function for htab_create_alloc_ex for line_header_hash. */
1734
1735static hashval_t
1736line_header_hash_voidp (const void *item)
1737{
9a3c8263 1738 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1739
1740 return line_header_hash (ofs);
1741}
1742
1743/* Equality function for line_header_hash. */
1744
1745static int
1746line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1747{
9a3c8263
SM
1748 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1749 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1750
9c541725 1751 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1752 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1753}
1754
4390d890 1755\f
9291a0cd 1756
330cdd98
PA
1757/* See declaration. */
1758
5989a64e
SM
1759dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1760 bool can_copy_)
c3699833
SM
1761 : obfd (obfd),
1762 can_copy (can_copy_)
330cdd98
PA
1763{
1764 if (names == NULL)
1765 names = &dwarf2_elf_names;
1766
330cdd98
PA
1767 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1768 locate_sections (obfd, sec, *names);
1769}
1770
5989a64e 1771dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1772{
1773 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1774 free_cached_comp_units ();
1775
b76e467d 1776 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1777 per_cu->imported_symtabs_free ();
fc8e7e75 1778
b2bdb8cf 1779 for (signatured_type *sig_type : all_type_units)
ae640021 1780 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1781
5989a64e 1782 /* Everything else should be on this->obstack. */
330cdd98
PA
1783}
1784
1785/* See declaration. */
1786
1787void
5989a64e 1788dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1789{
1790 dwarf2_per_cu_data *per_cu = read_in_chain;
1791 dwarf2_per_cu_data **last_chain = &read_in_chain;
1792 while (per_cu != NULL)
1793 {
1794 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1795
fcd3b13d 1796 delete per_cu->cu;
330cdd98
PA
1797 *last_chain = next_cu;
1798 per_cu = next_cu;
1799 }
1800}
1801
11ed8cad
TT
1802/* A helper class that calls free_cached_comp_units on
1803 destruction. */
1804
1805class free_cached_comp_units
1806{
1807public:
1808
1809 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1810 : m_per_objfile (per_objfile)
1811 {
1812 }
1813
1814 ~free_cached_comp_units ()
1815 {
5989a64e 1816 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1817 }
1818
1819 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1820
1821private:
1822
1823 dwarf2_per_objfile *m_per_objfile;
1824};
1825
af758d11
SM
1826/* See read.h. */
1827
1828bool
1829dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1830{
1831 gdb_assert (per_cu->index < this->m_symtabs.size ());
1832
1833 return this->m_symtabs[per_cu->index] != nullptr;
1834}
1835
1836/* See read.h. */
1837
1838compunit_symtab *
1839dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1840{
1841 gdb_assert (per_cu->index < this->m_symtabs.size ());
1842
1843 return this->m_symtabs[per_cu->index];
1844}
1845
1846/* See read.h. */
1847
1848void
1849dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1850 compunit_symtab *symtab)
1851{
1852 gdb_assert (per_cu->index < this->m_symtabs.size ());
1853 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1854
1855 this->m_symtabs[per_cu->index] = symtab;
1856}
1857
c906108c 1858/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1859 information and return true if we have enough to do something.
1860 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1861 ELF names are used. CAN_COPY is true for formats where symbol
1862 interposition is possible and so symbol values must follow copy
1863 relocation rules. */
c906108c
SS
1864
1865int
251d32d9 1866dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1867 const struct dwarf2_debug_sections *names,
1868 bool can_copy)
c906108c 1869{
97cbe998
SDJ
1870 if (objfile->flags & OBJF_READNEVER)
1871 return 0;
1872
ed2dc618
SM
1873 struct dwarf2_per_objfile *dwarf2_per_objfile
1874 = get_dwarf2_per_objfile (objfile);
1875
1876 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1877 {
1878 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1879 sharing yet). */
1880 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1881
1882 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1883 }
5bfd760d 1884
5989a64e
SM
1885 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1886 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1887 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1888 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1889}
1890
251d32d9
TG
1891/* When loading sections, we look either for uncompressed section or for
1892 compressed section names. */
233a11ab
CS
1893
1894static int
251d32d9
TG
1895section_is_p (const char *section_name,
1896 const struct dwarf2_section_names *names)
233a11ab 1897{
251d32d9
TG
1898 if (names->normal != NULL
1899 && strcmp (section_name, names->normal) == 0)
1900 return 1;
1901 if (names->compressed != NULL
1902 && strcmp (section_name, names->compressed) == 0)
1903 return 1;
1904 return 0;
233a11ab
CS
1905}
1906
330cdd98 1907/* See declaration. */
c906108c 1908
330cdd98 1909void
5989a64e
SM
1910dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1911 const dwarf2_debug_sections &names)
c906108c 1912{
fd361982 1913 flagword aflag = bfd_section_flags (sectp);
251d32d9 1914
dc7650b8
JK
1915 if ((aflag & SEC_HAS_CONTENTS) == 0)
1916 {
1917 }
950b7495
KS
1918 else if (elf_section_data (sectp)->this_hdr.sh_size
1919 > bfd_get_file_size (abfd))
1920 {
1921 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1922 warning (_("Discarding section %s which has a section size (%s"
1923 ") larger than the file size [in module %s]"),
1924 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1925 bfd_get_filename (abfd));
1926 }
330cdd98 1927 else if (section_is_p (sectp->name, &names.info))
c906108c 1928 {
330cdd98 1929 this->info.s.section = sectp;
fd361982 1930 this->info.size = bfd_section_size (sectp);
c906108c 1931 }
330cdd98 1932 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1933 {
330cdd98 1934 this->abbrev.s.section = sectp;
fd361982 1935 this->abbrev.size = bfd_section_size (sectp);
c906108c 1936 }
330cdd98 1937 else if (section_is_p (sectp->name, &names.line))
c906108c 1938 {
330cdd98 1939 this->line.s.section = sectp;
fd361982 1940 this->line.size = bfd_section_size (sectp);
c906108c 1941 }
330cdd98 1942 else if (section_is_p (sectp->name, &names.loc))
c906108c 1943 {
330cdd98 1944 this->loc.s.section = sectp;
fd361982 1945 this->loc.size = bfd_section_size (sectp);
c906108c 1946 }
330cdd98 1947 else if (section_is_p (sectp->name, &names.loclists))
43988095 1948 {
330cdd98 1949 this->loclists.s.section = sectp;
fd361982 1950 this->loclists.size = bfd_section_size (sectp);
43988095 1951 }
330cdd98 1952 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1953 {
330cdd98 1954 this->macinfo.s.section = sectp;
fd361982 1955 this->macinfo.size = bfd_section_size (sectp);
c906108c 1956 }
330cdd98 1957 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1958 {
330cdd98 1959 this->macro.s.section = sectp;
fd361982 1960 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1961 }
330cdd98 1962 else if (section_is_p (sectp->name, &names.str))
c906108c 1963 {
330cdd98 1964 this->str.s.section = sectp;
fd361982 1965 this->str.size = bfd_section_size (sectp);
c906108c 1966 }
18a8505e
AT
1967 else if (section_is_p (sectp->name, &names.str_offsets))
1968 {
1969 this->str_offsets.s.section = sectp;
1970 this->str_offsets.size = bfd_section_size (sectp);
1971 }
330cdd98 1972 else if (section_is_p (sectp->name, &names.line_str))
43988095 1973 {
330cdd98 1974 this->line_str.s.section = sectp;
fd361982 1975 this->line_str.size = bfd_section_size (sectp);
43988095 1976 }
330cdd98 1977 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1978 {
330cdd98 1979 this->addr.s.section = sectp;
fd361982 1980 this->addr.size = bfd_section_size (sectp);
3019eac3 1981 }
330cdd98 1982 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1983 {
330cdd98 1984 this->frame.s.section = sectp;
fd361982 1985 this->frame.size = bfd_section_size (sectp);
b6af0555 1986 }
330cdd98 1987 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1988 {
330cdd98 1989 this->eh_frame.s.section = sectp;
fd361982 1990 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1991 }
330cdd98 1992 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1993 {
330cdd98 1994 this->ranges.s.section = sectp;
fd361982 1995 this->ranges.size = bfd_section_size (sectp);
af34e669 1996 }
330cdd98 1997 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1998 {
330cdd98 1999 this->rnglists.s.section = sectp;
fd361982 2000 this->rnglists.size = bfd_section_size (sectp);
43988095 2001 }
330cdd98 2002 else if (section_is_p (sectp->name, &names.types))
348e048f 2003 {
8b70b953
TT
2004 struct dwarf2_section_info type_section;
2005
2006 memset (&type_section, 0, sizeof (type_section));
049412e3 2007 type_section.s.section = sectp;
fd361982 2008 type_section.size = bfd_section_size (sectp);
8b70b953 2009
fd5866f6 2010 this->types.push_back (type_section);
348e048f 2011 }
330cdd98 2012 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2013 {
330cdd98 2014 this->gdb_index.s.section = sectp;
fd361982 2015 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2016 }
927aa2e7
JK
2017 else if (section_is_p (sectp->name, &names.debug_names))
2018 {
2019 this->debug_names.s.section = sectp;
fd361982 2020 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2021 }
2022 else if (section_is_p (sectp->name, &names.debug_aranges))
2023 {
2024 this->debug_aranges.s.section = sectp;
fd361982 2025 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2026 }
dce234bc 2027
fd361982
AM
2028 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2029 && bfd_section_vma (sectp) == 0)
330cdd98 2030 this->has_section_at_zero = true;
c906108c
SS
2031}
2032
dce234bc 2033/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2034 SECTION_NAME. */
af34e669 2035
dce234bc 2036void
3017a003
TG
2037dwarf2_get_section_info (struct objfile *objfile,
2038 enum dwarf2_section_enum sect,
d521ce57 2039 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2040 bfd_size_type *sizep)
2041{
5bfd760d 2042 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2043 struct dwarf2_section_info *info;
a3b2a86b
TT
2044
2045 /* We may see an objfile without any DWARF, in which case we just
2046 return nothing. */
2047 if (data == NULL)
2048 {
2049 *sectp = NULL;
2050 *bufp = NULL;
2051 *sizep = 0;
2052 return;
2053 }
3017a003
TG
2054 switch (sect)
2055 {
2056 case DWARF2_DEBUG_FRAME:
5989a64e 2057 info = &data->per_bfd->frame;
3017a003
TG
2058 break;
2059 case DWARF2_EH_FRAME:
5989a64e 2060 info = &data->per_bfd->eh_frame;
3017a003
TG
2061 break;
2062 default:
2063 gdb_assert_not_reached ("unexpected section");
2064 }
dce234bc 2065
96b79293 2066 info->read (objfile);
dce234bc 2067
96b79293 2068 *sectp = info->get_bfd_section ();
dce234bc
PP
2069 *bufp = info->buffer;
2070 *sizep = info->size;
2071}
2072
36586728
TT
2073/* A helper function to find the sections for a .dwz file. */
2074
2075static void
2076locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2077{
9a3c8263 2078 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2079
2080 /* Note that we only support the standard ELF names, because .dwz
2081 is ELF-only (at the time of writing). */
2082 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2083 {
049412e3 2084 dwz_file->abbrev.s.section = sectp;
fd361982 2085 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2086 }
2087 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2088 {
049412e3 2089 dwz_file->info.s.section = sectp;
fd361982 2090 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2091 }
2092 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2093 {
049412e3 2094 dwz_file->str.s.section = sectp;
fd361982 2095 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2096 }
2097 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2098 {
049412e3 2099 dwz_file->line.s.section = sectp;
fd361982 2100 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2101 }
2102 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2103 {
049412e3 2104 dwz_file->macro.s.section = sectp;
fd361982 2105 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2106 }
2ec9a5e0
TT
2107 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2108 {
049412e3 2109 dwz_file->gdb_index.s.section = sectp;
fd361982 2110 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2111 }
927aa2e7
JK
2112 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2113 {
2114 dwz_file->debug_names.s.section = sectp;
fd361982 2115 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2116 }
36586728
TT
2117}
2118
c4973306 2119/* See dwarf2read.h. */
36586728 2120
c4973306 2121struct dwz_file *
c3699833 2122dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2123{
36586728 2124 const char *filename;
acd13123 2125 bfd_size_type buildid_len_arg;
dc294be5
TT
2126 size_t buildid_len;
2127 bfd_byte *buildid;
36586728 2128
c3699833
SM
2129 if (per_bfd->dwz_file != NULL)
2130 return per_bfd->dwz_file.get ();
36586728 2131
4db1a1dc 2132 bfd_set_error (bfd_error_no_error);
791afaa2 2133 gdb::unique_xmalloc_ptr<char> data
c3699833 2134 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2135 &buildid_len_arg, &buildid));
4db1a1dc
TT
2136 if (data == NULL)
2137 {
2138 if (bfd_get_error () == bfd_error_no_error)
2139 return NULL;
2140 error (_("could not read '.gnu_debugaltlink' section: %s"),
2141 bfd_errmsg (bfd_get_error ()));
2142 }
791afaa2
TT
2143
2144 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2145
acd13123
TT
2146 buildid_len = (size_t) buildid_len_arg;
2147
791afaa2 2148 filename = data.get ();
d721ba37
PA
2149
2150 std::string abs_storage;
36586728
TT
2151 if (!IS_ABSOLUTE_PATH (filename))
2152 {
14278e1f 2153 gdb::unique_xmalloc_ptr<char> abs
c3699833 2154 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2155
14278e1f 2156 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2157 filename = abs_storage.c_str ();
36586728
TT
2158 }
2159
dc294be5
TT
2160 /* First try the file name given in the section. If that doesn't
2161 work, try to use the build-id instead. */
ad80db5b 2162 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2163 if (dwz_bfd != NULL)
36586728 2164 {
192b62ce 2165 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2166 dwz_bfd.reset (nullptr);
36586728
TT
2167 }
2168
dc294be5
TT
2169 if (dwz_bfd == NULL)
2170 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2171
0d79cdc4
AM
2172 if (dwz_bfd == nullptr)
2173 {
2174 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2175 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2176
2177 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2178 buildid_len,
2179 origname,
2180 &alt_filename));
2181
2182 if (fd.get () >= 0)
2183 {
2184 /* File successfully retrieved from server. */
ad80db5b 2185 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2186
2187 if (dwz_bfd == nullptr)
2188 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2189 alt_filename.get ());
2190 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2191 dwz_bfd.reset (nullptr);
2192 }
2193 }
2194
dc294be5
TT
2195 if (dwz_bfd == NULL)
2196 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2197 bfd_get_filename (per_bfd->obfd));
dc294be5 2198
7ff8cb8c
TT
2199 std::unique_ptr<struct dwz_file> result
2200 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2201
7ff8cb8c
TT
2202 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2203 result.get ());
36586728 2204
c3699833
SM
2205 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2206 per_bfd->dwz_file = std::move (result);
2207 return per_bfd->dwz_file.get ();
36586728 2208}
9291a0cd 2209\f
7b9f3c50
DE
2210/* DWARF quick_symbols_functions support. */
2211
2212/* TUs can share .debug_line entries, and there can be a lot more TUs than
2213 unique line tables, so we maintain a separate table of all .debug_line
2214 derived entries to support the sharing.
2215 All the quick functions need is the list of file names. We discard the
2216 line_header when we're done and don't need to record it here. */
2217struct quick_file_names
2218{
094b34ac
DE
2219 /* The data used to construct the hash key. */
2220 struct stmt_list_hash hash;
7b9f3c50
DE
2221
2222 /* The number of entries in file_names, real_names. */
2223 unsigned int num_file_names;
2224
2225 /* The file names from the line table, after being run through
2226 file_full_name. */
2227 const char **file_names;
2228
2229 /* The file names from the line table after being run through
2230 gdb_realpath. These are computed lazily. */
2231 const char **real_names;
2232};
2233
2234/* When using the index (and thus not using psymtabs), each CU has an
2235 object of this type. This is used to hold information needed by
2236 the various "quick" methods. */
2237struct dwarf2_per_cu_quick_data
2238{
2239 /* The file table. This can be NULL if there was no file table
2240 or it's currently not read in.
5989a64e 2241 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2242 struct quick_file_names *file_names;
2243
7b9f3c50
DE
2244 /* A temporary mark bit used when iterating over all CUs in
2245 expand_symtabs_matching. */
2246 unsigned int mark : 1;
2247
2248 /* True if we've tried to read the file table and found there isn't one.
2249 There will be no point in trying to read it again next time. */
2250 unsigned int no_file_data : 1;
2251};
2252
094b34ac
DE
2253/* Utility hash function for a stmt_list_hash. */
2254
2255static hashval_t
2256hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2257{
2258 hashval_t v = 0;
2259
2260 if (stmt_list_hash->dwo_unit != NULL)
2261 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2262 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2263 return v;
2264}
2265
2266/* Utility equality function for a stmt_list_hash. */
2267
2268static int
2269eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2270 const struct stmt_list_hash *rhs)
2271{
2272 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2273 return 0;
2274 if (lhs->dwo_unit != NULL
2275 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2276 return 0;
2277
9c541725 2278 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2279}
2280
7b9f3c50
DE
2281/* Hash function for a quick_file_names. */
2282
2283static hashval_t
2284hash_file_name_entry (const void *e)
2285{
9a3c8263
SM
2286 const struct quick_file_names *file_data
2287 = (const struct quick_file_names *) e;
7b9f3c50 2288
094b34ac 2289 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2290}
2291
2292/* Equality function for a quick_file_names. */
2293
2294static int
2295eq_file_name_entry (const void *a, const void *b)
2296{
9a3c8263
SM
2297 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2298 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2299
094b34ac 2300 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2301}
2302
2303/* Delete function for a quick_file_names. */
2304
2305static void
2306delete_file_name_entry (void *e)
2307{
9a3c8263 2308 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2309 int i;
2310
2311 for (i = 0; i < file_data->num_file_names; ++i)
2312 {
2313 xfree ((void*) file_data->file_names[i]);
2314 if (file_data->real_names)
2315 xfree ((void*) file_data->real_names[i]);
2316 }
2317
45940949
TT
2318 /* The space for the struct itself lives on the obstack, so we don't
2319 free it here. */
7b9f3c50
DE
2320}
2321
2322/* Create a quick_file_names hash table. */
2323
5895093f 2324static htab_up
7b9f3c50
DE
2325create_quick_file_names_table (unsigned int nr_initial_entries)
2326{
5895093f
TT
2327 return htab_up (htab_create_alloc (nr_initial_entries,
2328 hash_file_name_entry, eq_file_name_entry,
2329 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2330}
9291a0cd 2331
ab432490
SM
2332/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2333 function is unrelated to symtabs, symtab would have to be created afterwards.
2334 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2335
2336static void
ab432490
SM
2337load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2338 bool skip_partial)
918dd910 2339{
3019eac3 2340 if (per_cu->is_debug_types)
ab432490 2341 load_full_type_unit (per_cu, per_objfile);
918dd910 2342 else
ab432490 2343 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2344
cc12ce38
DE
2345 if (per_cu->cu == NULL)
2346 return; /* Dummy CU. */
2dc860c0
DE
2347
2348 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2349}
2350
97a1449a 2351/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2352
9291a0cd 2353static void
97a1449a
SM
2354dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2355 dwarf2_per_objfile *dwarf2_per_objfile,
2356 bool skip_partial)
9291a0cd 2357{
f4dc4d17
DE
2358 /* Skip type_unit_groups, reading the type units they contain
2359 is handled elsewhere. */
197400e8 2360 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2361 return;
2362
b303c6f6
AB
2363 /* The destructor of dwarf2_queue_guard frees any entries left on
2364 the queue. After this point we're guaranteed to leave this function
2365 with the dwarf queue empty. */
39856def 2366 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2367
af758d11 2368 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2369 {
2370 queue_comp_unit (per_cu, language_minimal);
ab432490 2371 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2372
2373 /* If we just loaded a CU from a DWO, and we're working with an index
2374 that may badly handle TUs, load all the TUs in that DWO as well.
2375 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2376 if (!per_cu->is_debug_types
cc12ce38 2377 && per_cu->cu != NULL
89e63ee4 2378 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2379 && dwarf2_per_objfile->per_bfd->index_table != NULL
2380 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2381 /* DWP files aren't supported yet. */
ed2dc618 2382 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2383 queue_and_load_all_dwo_tus (per_cu);
95554aad 2384 }
9291a0cd 2385
ed2dc618 2386 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2387
2388 /* Age the cache, releasing compilation units that have not
2389 been used recently. */
ed2dc618 2390 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2391}
2392
97a1449a
SM
2393/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2394 the per-objfile for which this symtab is instantiated.
2395
2396 Returns the resulting symbol table. */
2fdf6df6 2397
43f3e411 2398static struct compunit_symtab *
97a1449a
SM
2399dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2400 dwarf2_per_objfile *dwarf2_per_objfile,
2401 bool skip_partial)
9291a0cd 2402{
5989a64e 2403 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2404
2405 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2406 {
11ed8cad 2407 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2408 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2409 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2410 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2411 }
f194fefb 2412
af758d11 2413 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2414}
2415
ff4c9fec 2416/* See declaration. */
f4dc4d17 2417
ff4c9fec 2418dwarf2_per_cu_data *
5989a64e 2419dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2420{
b76e467d 2421 if (index >= this->all_comp_units.size ())
ff4c9fec 2422 {
b76e467d 2423 index -= this->all_comp_units.size ();
b2bdb8cf 2424 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2425 return &this->all_type_units[index]->per_cu;
2426 }
f4dc4d17 2427
ff4c9fec
SM
2428 return this->all_comp_units[index];
2429}
f4dc4d17 2430
ff4c9fec 2431/* See declaration. */
2fdf6df6 2432
ff4c9fec 2433dwarf2_per_cu_data *
5989a64e 2434dwarf2_per_bfd::get_cu (int index)
1fd400ff 2435{
b76e467d 2436 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2437
ff4c9fec 2438 return this->all_comp_units[index];
f4dc4d17
DE
2439}
2440
ff4c9fec 2441/* See declaration. */
f4dc4d17 2442
ff4c9fec 2443signatured_type *
5989a64e 2444dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2445{
b2bdb8cf 2446 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2447
ff4c9fec 2448 return this->all_type_units[index];
1fd400ff
TT
2449}
2450
d3473f0c
TT
2451/* See read.h. */
2452
2453dwarf2_per_cu_data *
5989a64e 2454dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2455{
2456 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2457 result->per_bfd = this;
d3473f0c
TT
2458 result->index = m_num_psymtabs++;
2459 return result;
2460}
2461
2462/* See read.h. */
2463
2464signatured_type *
5989a64e 2465dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2466{
2467 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2468 result->per_cu.per_bfd = this;
d3473f0c
TT
2469 result->per_cu.index = m_num_psymtabs++;
2470 return result;
2471}
2472
45940949
TT
2473/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2474 obstack, and constructed with the specified field values. */
4b514bc8
JK
2475
2476static dwarf2_per_cu_data *
ed2dc618 2477create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2478 struct dwarf2_section_info *section,
2479 int is_dwz,
2480 sect_offset sect_off, ULONGEST length)
2481{
5989a64e 2482 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2483 the_cu->sect_off = sect_off;
2484 the_cu->length = length;
e3b94546 2485 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2486 the_cu->section = section;
5989a64e 2487 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2488 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2489 the_cu->is_dwz = is_dwz;
2490 return the_cu;
2491}
2492
2ec9a5e0
TT
2493/* A helper for create_cus_from_index that handles a given list of
2494 CUs. */
2fdf6df6 2495
74a0d9f6 2496static void
12359b5e 2497create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2498 const gdb_byte *cu_list, offset_type n_elements,
2499 struct dwarf2_section_info *section,
b76e467d 2500 int is_dwz)
9291a0cd 2501{
12359b5e 2502 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2503 {
74a0d9f6 2504 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2505
2506 sect_offset sect_off
2507 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2508 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2509 cu_list += 2 * 8;
2510
b76e467d 2511 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2512 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2513 sect_off, length);
5989a64e 2514 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2515 }
9291a0cd
TT
2516}
2517
2ec9a5e0 2518/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2519 the CU objects for this objfile. */
2ec9a5e0 2520
74a0d9f6 2521static void
12359b5e 2522create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2523 const gdb_byte *cu_list, offset_type cu_list_elements,
2524 const gdb_byte *dwz_list, offset_type dwz_elements)
2525{
5989a64e
SM
2526 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2527 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2528 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2529
12359b5e 2530 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2531 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2532
2533 if (dwz_elements == 0)
74a0d9f6 2534 return;
2ec9a5e0 2535
c3699833 2536 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2537 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2538 &dwz->info, 1);
2ec9a5e0
TT
2539}
2540
1fd400ff 2541/* Create the signatured type hash table from the index. */
673bfd45 2542
74a0d9f6 2543static void
12359b5e
SM
2544create_signatured_type_table_from_index
2545 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2546 struct dwarf2_section_info *section,
2547 const gdb_byte *bytes,
2548 offset_type elements)
1fd400ff 2549{
5989a64e
SM
2550 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2551 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2552
298e9637 2553 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2554
12359b5e 2555 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2556 {
52dc124a 2557 struct signatured_type *sig_type;
9c541725 2558 ULONGEST signature;
1fd400ff 2559 void **slot;
9c541725 2560 cu_offset type_offset_in_tu;
1fd400ff 2561
74a0d9f6 2562 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2563 sect_offset sect_off
2564 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2565 type_offset_in_tu
2566 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2567 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2568 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2569 bytes += 3 * 8;
2570
5989a64e 2571 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2572 sig_type->signature = signature;
9c541725 2573 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2574 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2575 sig_type->per_cu.section = section;
9c541725 2576 sig_type->per_cu.sect_off = sect_off;
e3b94546 2577 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2578 sig_type->per_cu.v.quick
5989a64e 2579 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2580 struct dwarf2_per_cu_quick_data);
2581
b0b6a987 2582 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2583 *slot = sig_type;
1fd400ff 2584
5989a64e 2585 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2586 }
2587
5989a64e 2588 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2589}
2590
927aa2e7
JK
2591/* Create the signatured type hash table from .debug_names. */
2592
2593static void
2594create_signatured_type_table_from_debug_names
ed2dc618 2595 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2596 const mapped_debug_names &map,
2597 struct dwarf2_section_info *section,
2598 struct dwarf2_section_info *abbrev_section)
2599{
ed2dc618
SM
2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
2601
96b79293
TT
2602 section->read (objfile);
2603 abbrev_section->read (objfile);
927aa2e7 2604
5989a64e
SM
2605 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2606 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2607
298e9637 2608 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2609
2610 for (uint32_t i = 0; i < map.tu_count; ++i)
2611 {
2612 struct signatured_type *sig_type;
927aa2e7 2613 void **slot;
927aa2e7
JK
2614
2615 sect_offset sect_off
2616 = (sect_offset) (extract_unsigned_integer
2617 (map.tu_table_reordered + i * map.offset_size,
2618 map.offset_size,
2619 map.dwarf5_byte_order));
2620
2621 comp_unit_head cu_header;
ed2dc618
SM
2622 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2623 abbrev_section,
927aa2e7
JK
2624 section->buffer + to_underlying (sect_off),
2625 rcuh_kind::TYPE);
2626
5989a64e 2627 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2628 sig_type->signature = cu_header.signature;
2629 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2630 sig_type->per_cu.is_debug_types = 1;
2631 sig_type->per_cu.section = section;
2632 sig_type->per_cu.sect_off = sect_off;
e3b94546 2633 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2634 sig_type->per_cu.v.quick
5989a64e 2635 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2636 struct dwarf2_per_cu_quick_data);
2637
b0b6a987 2638 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2639 *slot = sig_type;
2640
5989a64e 2641 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2642 }
2643
5989a64e 2644 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2645}
2646
9291a0cd
TT
2647/* Read the address map data from the mapped index, and use it to
2648 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2649
9291a0cd 2650static void
ed2dc618
SM
2651create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2652 struct mapped_index *index)
9291a0cd 2653{
ed2dc618 2654 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2655 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2656 const gdb_byte *iter, *end;
9291a0cd 2657 struct addrmap *mutable_map;
9291a0cd
TT
2658 CORE_ADDR baseaddr;
2659
8268c778
PA
2660 auto_obstack temp_obstack;
2661
9291a0cd
TT
2662 mutable_map = addrmap_create_mutable (&temp_obstack);
2663
f00a2de2
PA
2664 iter = index->address_table.data ();
2665 end = iter + index->address_table.size ();
9291a0cd 2666
b3b3bada 2667 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2668
2669 while (iter < end)
2670 {
2671 ULONGEST hi, lo, cu_index;
2672 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2673 iter += 8;
2674 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2675 iter += 8;
2676 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2677 iter += 4;
f652bce2 2678
24a55014 2679 if (lo > hi)
f652bce2 2680 {
b98664d3 2681 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2682 hex_string (lo), hex_string (hi));
24a55014 2683 continue;
f652bce2 2684 }
24a55014 2685
5989a64e 2686 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2687 {
b98664d3 2688 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2689 (unsigned) cu_index);
24a55014 2690 continue;
f652bce2 2691 }
24a55014 2692
79748972
TT
2693 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2694 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2695 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2696 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2697 }
2698
d320c2b5 2699 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2700 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2701}
2702
927aa2e7
JK
2703/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2704 populate the objfile's psymtabs_addrmap. */
2705
2706static void
ed2dc618 2707create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2708 struct dwarf2_section_info *section)
2709{
ed2dc618 2710 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2711 bfd *abfd = objfile->obfd;
08feed99 2712 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2713 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2714
2715 auto_obstack temp_obstack;
2716 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2717
2718 std::unordered_map<sect_offset,
2719 dwarf2_per_cu_data *,
2720 gdb::hash_enum<sect_offset>>
2721 debug_info_offset_to_per_cu;
5989a64e 2722 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2723 {
927aa2e7
JK
2724 const auto insertpair
2725 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2726 if (!insertpair.second)
2727 {
2728 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2729 "debug_info_offset %s, ignoring .debug_aranges."),
2730 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2731 return;
2732 }
2733 }
2734
96b79293 2735 section->read (objfile);
927aa2e7
JK
2736
2737 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2738
2739 const gdb_byte *addr = section->buffer;
2740
2741 while (addr < section->buffer + section->size)
2742 {
2743 const gdb_byte *const entry_addr = addr;
2744 unsigned int bytes_read;
2745
2746 const LONGEST entry_length = read_initial_length (abfd, addr,
2747 &bytes_read);
2748 addr += bytes_read;
2749
2750 const gdb_byte *const entry_end = addr + entry_length;
2751 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2752 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2753 if (addr + entry_length > section->buffer + section->size)
2754 {
47e3f474 2755 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2756 "length %s exceeds section length %s, "
2757 "ignoring .debug_aranges."),
47e3f474
TV
2758 objfile_name (objfile),
2759 plongest (entry_addr - section->buffer),
927aa2e7
JK
2760 plongest (bytes_read + entry_length),
2761 pulongest (section->size));
2762 return;
2763 }
2764
2765 /* The version number. */
2766 const uint16_t version = read_2_bytes (abfd, addr);
2767 addr += 2;
2768 if (version != 2)
2769 {
47e3f474 2770 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2771 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2774 return;
2775 }
2776
2777 const uint64_t debug_info_offset
2778 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2779 addr += offset_size;
2780 const auto per_cu_it
2781 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2782 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2783 {
47e3f474 2784 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2785 "debug_info_offset %s does not exists, "
2786 "ignoring .debug_aranges."),
47e3f474
TV
2787 objfile_name (objfile),
2788 plongest (entry_addr - section->buffer),
927aa2e7
JK
2789 pulongest (debug_info_offset));
2790 return;
2791 }
2792 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2793
2794 const uint8_t address_size = *addr++;
2795 if (address_size < 1 || address_size > 8)
2796 {
47e3f474 2797 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2798 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2799 objfile_name (objfile),
2800 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2801 return;
2802 }
2803
2804 const uint8_t segment_selector_size = *addr++;
2805 if (segment_selector_size != 0)
2806 {
47e3f474 2807 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2808 "segment_selector_size %u is not supported, "
2809 "ignoring .debug_aranges."),
47e3f474
TV
2810 objfile_name (objfile),
2811 plongest (entry_addr - section->buffer),
927aa2e7
JK
2812 segment_selector_size);
2813 return;
2814 }
2815
2816 /* Must pad to an alignment boundary that is twice the address
2817 size. It is undocumented by the DWARF standard but GCC does
2818 use it. */
2819 for (size_t padding = ((-(addr - section->buffer))
2820 & (2 * address_size - 1));
2821 padding > 0; padding--)
2822 if (*addr++ != 0)
2823 {
47e3f474 2824 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2825 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2826 objfile_name (objfile),
2827 plongest (entry_addr - section->buffer));
927aa2e7
JK
2828 return;
2829 }
2830
2831 for (;;)
2832 {
2833 if (addr + 2 * address_size > entry_end)
2834 {
47e3f474 2835 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2836 "address list is not properly terminated, "
2837 "ignoring .debug_aranges."),
47e3f474
TV
2838 objfile_name (objfile),
2839 plongest (entry_addr - section->buffer));
927aa2e7
JK
2840 return;
2841 }
2842 ULONGEST start = extract_unsigned_integer (addr, address_size,
2843 dwarf5_byte_order);
2844 addr += address_size;
2845 ULONGEST length = extract_unsigned_integer (addr, address_size,
2846 dwarf5_byte_order);
2847 addr += address_size;
2848 if (start == 0 && length == 0)
2849 break;
5989a64e 2850 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2851 {
2852 /* Symbol was eliminated due to a COMDAT group. */
2853 continue;
2854 }
2855 ULONGEST end = start + length;
79748972
TT
2856 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2857 - baseaddr);
2858 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2859 - baseaddr);
927aa2e7
JK
2860 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2861 }
2862 }
2863
d320c2b5 2864 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2865 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2866}
2867
9291a0cd
TT
2868/* Find a slot in the mapped index INDEX for the object named NAME.
2869 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2870 constant pool and return true. If NAME cannot be found, return
2871 false. */
2fdf6df6 2872
109483d9 2873static bool
9291a0cd
TT
2874find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2875 offset_type **vec_out)
2876{
0cf03b49 2877 offset_type hash;
9291a0cd 2878 offset_type slot, step;
559a7a62 2879 int (*cmp) (const char *, const char *);
9291a0cd 2880
791afaa2 2881 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2882 if (current_language->la_language == language_cplus
45280282
IB
2883 || current_language->la_language == language_fortran
2884 || current_language->la_language == language_d)
0cf03b49
JK
2885 {
2886 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2887 not contain any. */
a8719064 2888
72998fb3 2889 if (strchr (name, '(') != NULL)
0cf03b49 2890 {
109483d9 2891 without_params = cp_remove_params (name);
0cf03b49 2892
72998fb3 2893 if (without_params != NULL)
791afaa2 2894 name = without_params.get ();
0cf03b49
JK
2895 }
2896 }
2897
559a7a62 2898 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2899 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2900 simulate our NAME being searched is also lowercased. */
2901 hash = mapped_index_string_hash ((index->version == 4
2902 && case_sensitivity == case_sensitive_off
2903 ? 5 : index->version),
2904 name);
2905
f00a2de2
PA
2906 slot = hash & (index->symbol_table.size () - 1);
2907 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2908 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2909
2910 for (;;)
2911 {
9291a0cd 2912 const char *str;
f00a2de2
PA
2913
2914 const auto &bucket = index->symbol_table[slot];
2915 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2916 return false;
9291a0cd 2917
f00a2de2 2918 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2919 if (!cmp (name, str))
9291a0cd
TT
2920 {
2921 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2922 + MAYBE_SWAP (bucket.vec));
109483d9 2923 return true;
9291a0cd
TT
2924 }
2925
f00a2de2 2926 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2927 }
2928}
2929
4485a1c1
SM
2930/* A helper function that reads the .gdb_index from BUFFER and fills
2931 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2932 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2933 ok to use deprecated sections.
2934
2935 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2936 out parameters that are filled in with information about the CU and
2937 TU lists in the section.
2938
4485a1c1 2939 Returns true if all went well, false otherwise. */
2fdf6df6 2940
d33bc52e 2941static bool
3810f182 2942read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2943 bool deprecated_ok,
2944 gdb::array_view<const gdb_byte> buffer,
2945 struct mapped_index *map,
2946 const gdb_byte **cu_list,
2947 offset_type *cu_list_elements,
2948 const gdb_byte **types_list,
2949 offset_type *types_list_elements)
2950{
2951 const gdb_byte *addr = &buffer[0];
82430852 2952
9291a0cd 2953 /* Version check. */
4485a1c1 2954 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2955 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2956 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2957 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2958 indices. */
831adc1f 2959 if (version < 4)
481860b3
GB
2960 {
2961 static int warning_printed = 0;
2962 if (!warning_printed)
2963 {
2964 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2965 filename);
481860b3
GB
2966 warning_printed = 1;
2967 }
2968 return 0;
2969 }
2970 /* Index version 4 uses a different hash function than index version
2971 5 and later.
2972
2973 Versions earlier than 6 did not emit psymbols for inlined
2974 functions. Using these files will cause GDB not to be able to
2975 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2976 indices unless the user has done
2977 "set use-deprecated-index-sections on". */
2ec9a5e0 2978 if (version < 6 && !deprecated_ok)
481860b3
GB
2979 {
2980 static int warning_printed = 0;
2981 if (!warning_printed)
2982 {
e615022a
DE
2983 warning (_("\
2984Skipping deprecated .gdb_index section in %s.\n\
2985Do \"set use-deprecated-index-sections on\" before the file is read\n\
2986to use the section anyway."),
2ec9a5e0 2987 filename);
481860b3
GB
2988 warning_printed = 1;
2989 }
2990 return 0;
2991 }
796a7ff8 2992 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2993 of the TU (for symbols coming from TUs),
2994 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2995 Plus gold-generated indices can have duplicate entries for global symbols,
2996 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2997 These are just performance bugs, and we can't distinguish gdb-generated
2998 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2999
481860b3 3000 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3001 longer backward compatible. */
796a7ff8 3002 if (version > 8)
594e8718 3003 return 0;
9291a0cd 3004
559a7a62 3005 map->version = version;
9291a0cd 3006
4485a1c1 3007 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3008
4485a1c1 3009 int i = 0;
2ec9a5e0
TT
3010 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3011 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3012 / 8);
1fd400ff
TT
3013 ++i;
3014
2ec9a5e0
TT
3015 *types_list = addr + MAYBE_SWAP (metadata[i]);
3016 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3017 - MAYBE_SWAP (metadata[i]))
3018 / 8);
987d643c 3019 ++i;
1fd400ff 3020
f00a2de2
PA
3021 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3022 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3023 map->address_table
3024 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3025 ++i;
3026
f00a2de2
PA
3027 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3028 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3029 map->symbol_table
3030 = gdb::array_view<mapped_index::symbol_table_slot>
3031 ((mapped_index::symbol_table_slot *) symbol_table,
3032 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3033
f00a2de2 3034 ++i;
f9d83a0b 3035 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3036
2ec9a5e0
TT
3037 return 1;
3038}
3039
4485a1c1
SM
3040/* Callback types for dwarf2_read_gdb_index. */
3041
3042typedef gdb::function_view
5989a64e 3043 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3044 get_gdb_index_contents_ftype;
3045typedef gdb::function_view
3046 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3047 get_gdb_index_contents_dwz_ftype;
3048
927aa2e7 3049/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3050 elements of all the CUs and return 1. Otherwise, return 0. */
3051
3052static int
4485a1c1
SM
3053dwarf2_read_gdb_index
3054 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3055 get_gdb_index_contents_ftype get_gdb_index_contents,
3056 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3057{
2ec9a5e0
TT
3058 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3059 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3060 struct dwz_file *dwz;
12359b5e 3061 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3062
4485a1c1 3063 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3064 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3065
3066 if (main_index_contents.empty ())
3067 return 0;
3068
3063847f 3069 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3070 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3071 use_deprecated_index_sections,
3072 main_index_contents, map.get (), &cu_list,
3073 &cu_list_elements, &types_list,
3074 &types_list_elements))
2ec9a5e0
TT
3075 return 0;
3076
0fefef59 3077 /* Don't use the index if it's empty. */
3063847f 3078 if (map->symbol_table.empty ())
0fefef59
DE
3079 return 0;
3080
2ec9a5e0
TT
3081 /* If there is a .dwz file, read it so we can get its CU list as
3082 well. */
c3699833 3083 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3084 if (dwz != NULL)
2ec9a5e0 3085 {
2ec9a5e0
TT
3086 struct mapped_index dwz_map;
3087 const gdb_byte *dwz_types_ignore;
3088 offset_type dwz_types_elements_ignore;
3089
4485a1c1
SM
3090 gdb::array_view<const gdb_byte> dwz_index_content
3091 = get_gdb_index_contents_dwz (objfile, dwz);
3092
3093 if (dwz_index_content.empty ())
3094 return 0;
3095
3810f182 3096 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3097 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3098 &dwz_list, &dwz_list_elements,
3099 &dwz_types_ignore,
3100 &dwz_types_elements_ignore))
2ec9a5e0
TT
3101 {
3102 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3103 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3104 return 0;
3105 }
3106 }
3107
12359b5e
SM
3108 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3109 dwz_list, dwz_list_elements);
1fd400ff 3110
8b70b953
TT
3111 if (types_list_elements)
3112 {
8b70b953
TT
3113 /* We can only handle a single .debug_types when we have an
3114 index. */
5989a64e 3115 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3116 return 0;
3117
5989a64e 3118 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3119
12359b5e
SM
3120 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3121 types_list, types_list_elements);
8b70b953 3122 }
9291a0cd 3123
3063847f 3124 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3125
5989a64e
SM
3126 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3127 dwarf2_per_objfile->per_bfd->using_index = 1;
3128 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3129 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3130
3131 return 1;
3132}
3133
dee91e82 3134/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3135
dee91e82
DE
3136static void
3137dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3138 const gdb_byte *info_ptr,
3e225074 3139 struct die_info *comp_unit_die)
9291a0cd 3140{
dee91e82 3141 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3142 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3143 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3144 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3145 struct attribute *attr;
7b9f3c50
DE
3146 void **slot;
3147 struct quick_file_names *qfn;
9291a0cd 3148
0186c6a7
DE
3149 gdb_assert (! this_cu->is_debug_types);
3150
07261596
TT
3151 /* Our callers never want to match partial units -- instead they
3152 will match the enclosing full CU. */
3153 if (comp_unit_die->tag == DW_TAG_partial_unit)
3154 {
3155 this_cu->v.quick->no_file_data = 1;
3156 return;
3157 }
3158
0186c6a7 3159 lh_cu = this_cu;
7b9f3c50 3160 slot = NULL;
dee91e82 3161
fff8551c 3162 line_header_up lh;
9c541725 3163 sect_offset line_offset {};
fff8551c 3164
dee91e82 3165 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3166 if (attr != nullptr)
9291a0cd 3167 {
7b9f3c50
DE
3168 struct quick_file_names find_entry;
3169
9c541725 3170 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3171
3172 /* We may have already read in this line header (TU line header sharing).
3173 If we have we're done. */
094b34ac 3174 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3175 find_entry.hash.line_sect_off = line_offset;
5989a64e 3176 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3177 &find_entry, INSERT);
3178 if (*slot != NULL)
3179 {
9a3c8263 3180 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3181 return;
7b9f3c50
DE
3182 }
3183
3019eac3 3184 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3185 }
3186 if (lh == NULL)
3187 {
094b34ac 3188 lh_cu->v.quick->no_file_data = 1;
dee91e82 3189 return;
9291a0cd
TT
3190 }
3191
5989a64e 3192 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3193 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3194 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3195 gdb_assert (slot != NULL);
3196 *slot = qfn;
9291a0cd 3197
d721ba37 3198 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3199
aa391654
TT
3200 int offset = 0;
3201 if (strcmp (fnd.name, "<unknown>") != 0)
3202 ++offset;
3203
7ba99d21 3204 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3205 qfn->file_names =
5989a64e 3206 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3207 qfn->num_file_names);
aa391654
TT
3208 if (offset != 0)
3209 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3210 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3211 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3212 fnd.comp_dir).release ();
7b9f3c50 3213 qfn->real_names = NULL;
9291a0cd 3214
094b34ac 3215 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3216}
3217
3218/* A helper for the "quick" functions which attempts to read the line
3219 table for THIS_CU. */
3220
3221static struct quick_file_names *
ab432490
SM
3222dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3223 dwarf2_per_objfile *per_objfile)
dee91e82 3224{
0186c6a7
DE
3225 /* This should never be called for TUs. */
3226 gdb_assert (! this_cu->is_debug_types);
3227 /* Nor type unit groups. */
197400e8 3228 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3229
dee91e82
DE
3230 if (this_cu->v.quick->file_names != NULL)
3231 return this_cu->v.quick->file_names;
3232 /* If we know there is no line data, no point in looking again. */
3233 if (this_cu->v.quick->no_file_data)
3234 return NULL;
3235
ab432490 3236 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3237 if (!reader.dummy_p)
3e225074 3238 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3239
3240 if (this_cu->v.quick->no_file_data)
3241 return NULL;
3242 return this_cu->v.quick->file_names;
9291a0cd
TT
3243}
3244
3245/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3246 real path for a given file name from the line table. */
2fdf6df6 3247
9291a0cd 3248static const char *
45940949 3249dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3250 struct quick_file_names *qfn, int index)
9291a0cd 3251{
7b9f3c50 3252 if (qfn->real_names == NULL)
5989a64e 3253 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3254 qfn->num_file_names, const char *);
9291a0cd 3255
7b9f3c50 3256 if (qfn->real_names[index] == NULL)
14278e1f 3257 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3258
7b9f3c50 3259 return qfn->real_names[index];
9291a0cd
TT
3260}
3261
3262static struct symtab *
3263dw2_find_last_source_symtab (struct objfile *objfile)
3264{
ed2dc618
SM
3265 struct dwarf2_per_objfile *dwarf2_per_objfile
3266 = get_dwarf2_per_objfile (objfile);
5989a64e 3267 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3268 compunit_symtab *cust
3269 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3270
43f3e411
DE
3271 if (cust == NULL)
3272 return NULL;
ed2dc618 3273
43f3e411 3274 return compunit_primary_filetab (cust);
9291a0cd
TT
3275}
3276
7b9f3c50
DE
3277/* Traversal function for dw2_forget_cached_source_info. */
3278
3279static int
3280dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3281{
7b9f3c50 3282 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3283
7b9f3c50 3284 if (file_data->real_names)
9291a0cd 3285 {
7b9f3c50 3286 int i;
9291a0cd 3287
7b9f3c50 3288 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3289 {
7b9f3c50
DE
3290 xfree ((void*) file_data->real_names[i]);
3291 file_data->real_names[i] = NULL;
9291a0cd
TT
3292 }
3293 }
7b9f3c50
DE
3294
3295 return 1;
3296}
3297
3298static void
3299dw2_forget_cached_source_info (struct objfile *objfile)
3300{
ed2dc618
SM
3301 struct dwarf2_per_objfile *dwarf2_per_objfile
3302 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3303
5989a64e 3304 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3305 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3306}
3307
f8eba3c6
TT
3308/* Helper function for dw2_map_symtabs_matching_filename that expands
3309 the symtabs and calls the iterator. */
3310
3311static int
3312dw2_map_expand_apply (struct objfile *objfile,
3313 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3314 const char *name, const char *real_path,
14bc53a8 3315 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3316{
43f3e411 3317 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3318
3319 /* Don't visit already-expanded CUs. */
af758d11
SM
3320 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3321 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3322 return 0;
3323
3324 /* This may expand more than one symtab, and we want to iterate over
3325 all of them. */
97a1449a 3326 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3327
14bc53a8
PA
3328 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3329 last_made, callback);
f8eba3c6
TT
3330}
3331
3332/* Implementation of the map_symtabs_matching_filename method. */
3333
14bc53a8
PA
3334static bool
3335dw2_map_symtabs_matching_filename
3336 (struct objfile *objfile, const char *name, const char *real_path,
3337 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3338{
c011a4f4 3339 const char *name_basename = lbasename (name);
ed2dc618
SM
3340 struct dwarf2_per_objfile *dwarf2_per_objfile
3341 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3342
848e3e78
DE
3343 /* The rule is CUs specify all the files, including those used by
3344 any TU, so there's no need to scan TUs here. */
f4dc4d17 3345
5989a64e 3346 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3347 {
3d7bb9d9 3348 /* We only need to look at symtabs not already expanded. */
af758d11 3349 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3350 continue;
3351
ab432490
SM
3352 quick_file_names *file_data
3353 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3354 if (file_data == NULL)
9291a0cd
TT
3355 continue;
3356
b76e467d 3357 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3358 {
7b9f3c50 3359 const char *this_name = file_data->file_names[j];
da235a7c 3360 const char *this_real_name;
9291a0cd 3361
af529f8f 3362 if (compare_filenames_for_search (this_name, name))
9291a0cd 3363 {
f5b95b50 3364 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3365 callback))
3366 return true;
288e77a7 3367 continue;
4aac40c8 3368 }
9291a0cd 3369
c011a4f4
DE
3370 /* Before we invoke realpath, which can get expensive when many
3371 files are involved, do a quick comparison of the basenames. */
3372 if (! basenames_may_differ
3373 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3374 continue;
3375
45940949
TT
3376 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3377 file_data, j);
da235a7c 3378 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3379 {
da235a7c 3380 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3381 callback))
3382 return true;
288e77a7 3383 continue;
da235a7c 3384 }
9291a0cd 3385
da235a7c
JK
3386 if (real_path != NULL)
3387 {
af529f8f
JK
3388 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3389 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3390 if (this_real_name != NULL
af529f8f 3391 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3392 {
f5b95b50 3393 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3394 callback))
3395 return true;
288e77a7 3396 continue;
9291a0cd
TT
3397 }
3398 }
3399 }
3400 }
3401
14bc53a8 3402 return false;
9291a0cd
TT
3403}
3404
da51c347
DE
3405/* Struct used to manage iterating over all CUs looking for a symbol. */
3406
3407struct dw2_symtab_iterator
9291a0cd 3408{
ed2dc618
SM
3409 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3410 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3411 /* If set, only look for symbols that match that block. Valid values are
3412 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3413 gdb::optional<block_enum> block_index;
da51c347
DE
3414 /* The kind of symbol we're looking for. */
3415 domain_enum domain;
3416 /* The list of CUs from the index entry of the symbol,
3417 or NULL if not found. */
3418 offset_type *vec;
3419 /* The next element in VEC to look at. */
3420 int next;
3421 /* The number of elements in VEC, or zero if there is no match. */
3422 int length;
8943b874
DE
3423 /* Have we seen a global version of the symbol?
3424 If so we can ignore all further global instances.
3425 This is to work around gold/15646, inefficient gold-generated
3426 indices. */
3427 int global_seen;
da51c347 3428};
9291a0cd 3429
2b79f376 3430/* Initialize the index symtab iterator ITER. */
2fdf6df6 3431
9291a0cd 3432static void
da51c347 3433dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3434 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3435 gdb::optional<block_enum> block_index,
da51c347
DE
3436 domain_enum domain,
3437 const char *name)
3438{
ed2dc618 3439 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3440 iter->block_index = block_index;
3441 iter->domain = domain;
3442 iter->next = 0;
8943b874 3443 iter->global_seen = 0;
da51c347 3444
5989a64e 3445 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3446
3447 /* index is NULL if OBJF_READNOW. */
3448 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3449 iter->length = MAYBE_SWAP (*iter->vec);
3450 else
3451 {
3452 iter->vec = NULL;
3453 iter->length = 0;
3454 }
3455}
3456
3457/* Return the next matching CU or NULL if there are no more. */
3458
3459static struct dwarf2_per_cu_data *
3460dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3461{
ed2dc618
SM
3462 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3463
da51c347
DE
3464 for ( ; iter->next < iter->length; ++iter->next)
3465 {
3466 offset_type cu_index_and_attrs =
3467 MAYBE_SWAP (iter->vec[iter->next + 1]);
3468 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3469 gdb_index_symbol_kind symbol_kind =
3470 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3471 /* Only check the symbol attributes if they're present.
3472 Indices prior to version 7 don't record them,
3473 and indices >= 7 may elide them for certain symbols
3474 (gold does this). */
3475 int attrs_valid =
5989a64e 3476 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3477 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3478
3190f0c6 3479 /* Don't crash on bad data. */
5989a64e
SM
3480 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3481 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3482 {
b98664d3 3483 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3484 " [in module %s]"),
3485 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3486 continue;
3487 }
3488
5989a64e 3489 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3490
da51c347 3491 /* Skip if already read in. */
af758d11 3492 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3493 continue;
3494
8943b874
DE
3495 /* Check static vs global. */
3496 if (attrs_valid)
3497 {
2b79f376
SM
3498 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3499
3500 if (iter->block_index.has_value ())
3501 {
3502 bool want_static = *iter->block_index == STATIC_BLOCK;
3503
3504 if (is_static != want_static)
3505 continue;
3506 }
3507
8943b874
DE
3508 /* Work around gold/15646. */
3509 if (!is_static && iter->global_seen)
3510 continue;
3511 if (!is_static)
3512 iter->global_seen = 1;
3513 }
da51c347
DE
3514
3515 /* Only check the symbol's kind if it has one. */
3516 if (attrs_valid)
3517 {
3518 switch (iter->domain)
3519 {
3520 case VAR_DOMAIN:
3521 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3522 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3523 /* Some types are also in VAR_DOMAIN. */
3524 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3525 continue;
3526 break;
3527 case STRUCT_DOMAIN:
3528 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3529 continue;
3530 break;
3531 case LABEL_DOMAIN:
3532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3533 continue;
3534 break;
59c35742
AB
3535 case MODULE_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3537 continue;
3538 break;
da51c347
DE
3539 default:
3540 break;
3541 }
3542 }
3543
3544 ++iter->next;
3545 return per_cu;
3546 }
3547
3548 return NULL;
3549}
3550
43f3e411 3551static struct compunit_symtab *
c7f839cb 3552dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3553 const char *name, domain_enum domain)
9291a0cd 3554{
43f3e411 3555 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
9291a0cd 3558
b5ec771e
PA
3559 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3560
ed2dc618
SM
3561 struct dw2_symtab_iterator iter;
3562 struct dwarf2_per_cu_data *per_cu;
da51c347 3563
2b79f376 3564 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3565
ed2dc618
SM
3566 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3567 {
3568 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3569 struct compunit_symtab *stab
3570 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3571 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3572 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3573
ed2dc618
SM
3574 sym = block_find_symbol (block, name, domain,
3575 block_find_non_opaque_type_preferred,
3576 &with_opaque);
b2e2f908 3577
ed2dc618
SM
3578 /* Some caution must be observed with overloaded functions
3579 and methods, since the index will not contain any overload
3580 information (but NAME might contain it). */
da51c347 3581
ed2dc618
SM
3582 if (sym != NULL
3583 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3584 return stab;
3585 if (with_opaque != NULL
3586 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3587 stab_best = stab;
da51c347 3588
ed2dc618 3589 /* Keep looking through other CUs. */
9291a0cd 3590 }
9291a0cd 3591
da51c347 3592 return stab_best;
9291a0cd
TT
3593}
3594
3595static void
3596dw2_print_stats (struct objfile *objfile)
3597{
ed2dc618
SM
3598 struct dwarf2_per_objfile *dwarf2_per_objfile
3599 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3600 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3601 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3602 int count = 0;
9291a0cd 3603
ed2dc618 3604 for (int i = 0; i < total; ++i)
9291a0cd 3605 {
5989a64e 3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3607
af758d11 3608 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3609 ++count;
3610 }
e4a48d9d 3611 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3612 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3613}
3614
779bd270
DE
3615/* This dumps minimal information about the index.
3616 It is called via "mt print objfiles".
3617 One use is to verify .gdb_index has been loaded by the
3618 gdb.dwarf2/gdb-index.exp testcase. */
3619
9291a0cd
TT
3620static void
3621dw2_dump (struct objfile *objfile)
3622{
ed2dc618
SM
3623 struct dwarf2_per_objfile *dwarf2_per_objfile
3624 = get_dwarf2_per_objfile (objfile);
3625
5989a64e 3626 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3627 printf_filtered (".gdb_index:");
5989a64e 3628 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3629 {
3630 printf_filtered (" version %d\n",
5989a64e 3631 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3632 }
3633 else
3634 printf_filtered (" faked for \"readnow\"\n");
3635 printf_filtered ("\n");
9291a0cd
TT
3636}
3637
9291a0cd
TT
3638static void
3639dw2_expand_symtabs_for_function (struct objfile *objfile,
3640 const char *func_name)
3641{
ed2dc618
SM
3642 struct dwarf2_per_objfile *dwarf2_per_objfile
3643 = get_dwarf2_per_objfile (objfile);
da51c347 3644
ed2dc618
SM
3645 struct dw2_symtab_iterator iter;
3646 struct dwarf2_per_cu_data *per_cu;
da51c347 3647
2b79f376 3648 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3649
ed2dc618 3650 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3651 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3652
9291a0cd
TT
3653}
3654
3655static void
3656dw2_expand_all_symtabs (struct objfile *objfile)
3657{
ed2dc618
SM
3658 struct dwarf2_per_objfile *dwarf2_per_objfile
3659 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3660 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3661 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3662
ed2dc618 3663 for (int i = 0; i < total_units; ++i)
9291a0cd 3664 {
5989a64e 3665 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3666
58f0c718
TT
3667 /* We don't want to directly expand a partial CU, because if we
3668 read it with the wrong language, then assertion failures can
3669 be triggered later on. See PR symtab/23010. So, tell
3670 dw2_instantiate_symtab to skip partial CUs -- any important
3671 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3672 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3673 }
3674}
3675
3676static void
652a8996
JK
3677dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3678 const char *fullname)
9291a0cd 3679{
ed2dc618
SM
3680 struct dwarf2_per_objfile *dwarf2_per_objfile
3681 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3682
3683 /* We don't need to consider type units here.
3684 This is only called for examining code, e.g. expand_line_sal.
3685 There can be an order of magnitude (or more) more type units
3686 than comp units, and we avoid them if we can. */
3687
5989a64e 3688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3689 {
3d7bb9d9 3690 /* We only need to look at symtabs not already expanded. */
af758d11 3691 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3692 continue;
3693
ab432490
SM
3694 quick_file_names *file_data
3695 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3696 if (file_data == NULL)
9291a0cd
TT
3697 continue;
3698
b76e467d 3699 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3700 {
652a8996
JK
3701 const char *this_fullname = file_data->file_names[j];
3702
3703 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3704 {
97a1449a 3705 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3706 break;
3707 }
3708 }
3709 }
3710}
3711
9a0bacfb
TV
3712static void
3713dw2_expand_symtabs_matching_symbol
3714 (mapped_index_base &index,
3715 const lookup_name_info &lookup_name_in,
3716 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3717 enum search_domain kind,
3718 gdb::function_view<bool (offset_type)> match_callback);
3719
3720static void
3721dw2_expand_symtabs_matching_one
97a1449a
SM
3722 (dwarf2_per_cu_data *per_cu,
3723 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3724 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3725 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3726
9291a0cd 3727static void
199b4314
TT
3728dw2_map_matching_symbols
3729 (struct objfile *objfile,
b054970d 3730 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3731 int global,
3732 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3733 symbol_compare_ftype *ordered_compare)
9291a0cd 3734{
1aa98955
TV
3735 /* Used for Ada. */
3736 struct dwarf2_per_objfile *dwarf2_per_objfile
3737 = get_dwarf2_per_objfile (objfile);
3738
9a0bacfb
TV
3739 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3740
5989a64e 3741 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3742 {
3743 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3744 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3745 using GNU index. */
5989a64e 3746 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3747
9a0bacfb
TV
3748 const char *match_name = name.ada ().lookup_name ().c_str ();
3749 auto matcher = [&] (const char *symname)
3750 {
3751 if (ordered_compare == nullptr)
3752 return true;
3753 return ordered_compare (symname, match_name) == 0;
3754 };
3755
3756 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3757 [&] (offset_type namei)
3758 {
3759 struct dw2_symtab_iterator iter;
3760 struct dwarf2_per_cu_data *per_cu;
3761
3762 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3763 match_name);
3764 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3765 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3766 nullptr);
9a0bacfb
TV
3767 return true;
3768 });
3769 }
3770 else
3771 {
3772 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3773 proceed assuming all symtabs have been read in. */
3774 }
1aa98955
TV
3775
3776 for (compunit_symtab *cust : objfile->compunits ())
3777 {
3778 const struct block *block;
3779
3780 if (cust == NULL)
3781 continue;
3782 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3783 if (!iterate_over_symbols_terminated (block, name,
3784 domain, callback))
3785 return;
3786 }
9291a0cd
TT
3787}
3788
e1ef7d7a
PA
3789/* Starting from a search name, return the string that finds the upper
3790 bound of all strings that start with SEARCH_NAME in a sorted name
3791 list. Returns the empty string to indicate that the upper bound is
3792 the end of the list. */
3793
3794static std::string
3795make_sort_after_prefix_name (const char *search_name)
3796{
3797 /* When looking to complete "func", we find the upper bound of all
3798 symbols that start with "func" by looking for where we'd insert
3799 the closest string that would follow "func" in lexicographical
3800 order. Usually, that's "func"-with-last-character-incremented,
3801 i.e. "fund". Mind non-ASCII characters, though. Usually those
3802 will be UTF-8 multi-byte sequences, but we can't be certain.
3803 Especially mind the 0xff character, which is a valid character in
3804 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3805 rule out compilers allowing it in identifiers. Note that
3806 conveniently, strcmp/strcasecmp are specified to compare
3807 characters interpreted as unsigned char. So what we do is treat
3808 the whole string as a base 256 number composed of a sequence of
3809 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3810 to 0, and carries 1 to the following more-significant position.
3811 If the very first character in SEARCH_NAME ends up incremented
3812 and carries/overflows, then the upper bound is the end of the
3813 list. The string after the empty string is also the empty
3814 string.
3815
3816 Some examples of this operation:
3817
3818 SEARCH_NAME => "+1" RESULT
3819
3820 "abc" => "abd"
3821 "ab\xff" => "ac"
3822 "\xff" "a" "\xff" => "\xff" "b"
3823 "\xff" => ""
3824 "\xff\xff" => ""
3825 "" => ""
3826
3827 Then, with these symbols for example:
3828
3829 func
3830 func1
3831 fund
3832
3833 completing "func" looks for symbols between "func" and
3834 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3835 which finds "func" and "func1", but not "fund".
3836
3837 And with:
3838
3839 funcÿ (Latin1 'ÿ' [0xff])
3840 funcÿ1
3841 fund
3842
3843 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3844 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3845
3846 And with:
3847
3848 ÿÿ (Latin1 'ÿ' [0xff])
3849 ÿÿ1
3850
3851 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3852 the end of the list.
3853 */
3854 std::string after = search_name;
3855 while (!after.empty () && (unsigned char) after.back () == 0xff)
3856 after.pop_back ();
3857 if (!after.empty ())
3858 after.back () = (unsigned char) after.back () + 1;
3859 return after;
3860}
3861
5c58de74 3862/* See declaration. */
61d96d7e 3863
5c58de74
PA
3864std::pair<std::vector<name_component>::const_iterator,
3865 std::vector<name_component>::const_iterator>
44ed8f3e 3866mapped_index_base::find_name_components_bounds
3b00ef10 3867 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3868{
5c58de74
PA
3869 auto *name_cmp
3870 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3871
3b00ef10 3872 const char *lang_name
e0802d59 3873 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3874
3f563c84
PA
3875 /* Comparison function object for lower_bound that matches against a
3876 given symbol name. */
3877 auto lookup_compare_lower = [&] (const name_component &elem,
3878 const char *name)
3879 {
5c58de74 3880 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3881 const char *elem_name = elem_qualified + elem.name_offset;
3882 return name_cmp (elem_name, name) < 0;
3883 };
3884
3885 /* Comparison function object for upper_bound that matches against a
3886 given symbol name. */
3887 auto lookup_compare_upper = [&] (const char *name,
3888 const name_component &elem)
3889 {
5c58de74 3890 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3891 const char *elem_name = elem_qualified + elem.name_offset;
3892 return name_cmp (name, elem_name) < 0;
3893 };
3894
5c58de74
PA
3895 auto begin = this->name_components.begin ();
3896 auto end = this->name_components.end ();
3f563c84
PA
3897
3898 /* Find the lower bound. */
3899 auto lower = [&] ()
3900 {
3b00ef10 3901 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3902 return begin;
3903 else
3b00ef10 3904 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3905 } ();
3906
3907 /* Find the upper bound. */
3908 auto upper = [&] ()
3909 {
5c58de74 3910 if (lookup_name_without_params.completion_mode ())
3f563c84 3911 {
e1ef7d7a
PA
3912 /* In completion mode, we want UPPER to point past all
3913 symbols names that have the same prefix. I.e., with
3914 these symbols, and completing "func":
3915
3916 function << lower bound
3917 function1
3918 other_function << upper bound
3919
3920 We find the upper bound by looking for the insertion
3921 point of "func"-with-last-character-incremented,
3922 i.e. "fund". */
3b00ef10 3923 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3924 if (after.empty ())
3f563c84 3925 return end;
e6b2f5ef
PA
3926 return std::lower_bound (lower, end, after.c_str (),
3927 lookup_compare_lower);
3f563c84
PA
3928 }
3929 else
3b00ef10 3930 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3931 } ();
3932
5c58de74
PA
3933 return {lower, upper};
3934}
3935
3936/* See declaration. */
3937
3938void
44ed8f3e 3939mapped_index_base::build_name_components ()
5c58de74
PA
3940{
3941 if (!this->name_components.empty ())
3942 return;
3943
3944 this->name_components_casing = case_sensitivity;
3945 auto *name_cmp
3946 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3947
3948 /* The code below only knows how to break apart components of C++
3949 symbol names (and other languages that use '::' as
3b00ef10 3950 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3951 auto count = this->symbol_name_count ();
3952 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3953 {
44ed8f3e 3954 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3955 continue;
3956
3957 const char *name = this->symbol_name_at (idx);
3958
3959 /* Add each name component to the name component table. */
3960 unsigned int previous_len = 0;
3b00ef10
TT
3961
3962 if (strstr (name, "::") != nullptr)
3963 {
3964 for (unsigned int current_len = cp_find_first_component (name);
3965 name[current_len] != '\0';
3966 current_len += cp_find_first_component (name + current_len))
3967 {
3968 gdb_assert (name[current_len] == ':');
3969 this->name_components.push_back ({previous_len, idx});
3970 /* Skip the '::'. */
3971 current_len += 2;
3972 previous_len = current_len;
3973 }
3974 }
3975 else
5c58de74 3976 {
3b00ef10
TT
3977 /* Handle the Ada encoded (aka mangled) form here. */
3978 for (const char *iter = strstr (name, "__");
3979 iter != nullptr;
3980 iter = strstr (iter, "__"))
3981 {
3982 this->name_components.push_back ({previous_len, idx});
3983 iter += 2;
3984 previous_len = iter - name;
3985 }
5c58de74 3986 }
3b00ef10 3987
5c58de74
PA
3988 this->name_components.push_back ({previous_len, idx});
3989 }
3990
3991 /* Sort name_components elements by name. */
3992 auto name_comp_compare = [&] (const name_component &left,
3993 const name_component &right)
3994 {
3995 const char *left_qualified = this->symbol_name_at (left.idx);
3996 const char *right_qualified = this->symbol_name_at (right.idx);
3997
3998 const char *left_name = left_qualified + left.name_offset;
3999 const char *right_name = right_qualified + right.name_offset;
4000
4001 return name_cmp (left_name, right_name) < 0;
4002 };
4003
4004 std::sort (this->name_components.begin (),
4005 this->name_components.end (),
4006 name_comp_compare);
4007}
4008
4009/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4010 mapped_index_base instead of the containing objfile. This is split
4011 to a separate function in order to be able to unit test the
4012 name_components matching using a mock mapped_index_base. For each
5c58de74 4013 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4014 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4015
4016static void
4017dw2_expand_symtabs_matching_symbol
44ed8f3e 4018 (mapped_index_base &index,
5c58de74
PA
4019 const lookup_name_info &lookup_name_in,
4020 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4021 enum search_domain kind,
3b00ef10 4022 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4023{
4024 lookup_name_info lookup_name_without_params
4025 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4026
4027 /* Build the symbol name component sorted vector, if we haven't
4028 yet. */
4029 index.build_name_components ();
4030
3f563c84
PA
4031 /* The same symbol may appear more than once in the range though.
4032 E.g., if we're looking for symbols that complete "w", and we have
4033 a symbol named "w1::w2", we'll find the two name components for
4034 that same symbol in the range. To be sure we only call the
4035 callback once per symbol, we first collect the symbol name
4036 indexes that matched in a temporary vector and ignore
4037 duplicates. */
4038 std::vector<offset_type> matches;
3f563c84 4039
3b00ef10
TT
4040 struct name_and_matcher
4041 {
4042 symbol_name_matcher_ftype *matcher;
ecc6c606 4043 const char *name;
3b00ef10
TT
4044
4045 bool operator== (const name_and_matcher &other) const
3f563c84 4046 {
ecc6c606 4047 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4048 }
4049 };
4050
4051 /* A vector holding all the different symbol name matchers, for all
4052 languages. */
4053 std::vector<name_and_matcher> matchers;
4054
4055 for (int i = 0; i < nr_languages; i++)
4056 {
4057 enum language lang_e = (enum language) i;
4058
4059 const language_defn *lang = language_def (lang_e);
4060 symbol_name_matcher_ftype *name_matcher
4061 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4062
3b00ef10
TT
4063 name_and_matcher key {
4064 name_matcher,
4065 lookup_name_without_params.language_lookup_name (lang_e)
4066 };
4067
4068 /* Don't insert the same comparison routine more than once.
4069 Note that we do this linear walk. This is not a problem in
4070 practice because the number of supported languages is
4071 low. */
4072 if (std::find (matchers.begin (), matchers.end (), key)
4073 != matchers.end ())
9291a0cd 4074 continue;
3b00ef10
TT
4075 matchers.push_back (std::move (key));
4076
4077 auto bounds
4078 = index.find_name_components_bounds (lookup_name_without_params,
4079 lang_e);
4080
4081 /* Now for each symbol name in range, check to see if we have a name
4082 match, and if so, call the MATCH_CALLBACK callback. */
4083
4084 for (; bounds.first != bounds.second; ++bounds.first)
4085 {
4086 const char *qualified = index.symbol_name_at (bounds.first->idx);
4087
4088 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4089 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4090 continue;
9291a0cd 4091
3b00ef10
TT
4092 matches.push_back (bounds.first->idx);
4093 }
3f563c84
PA
4094 }
4095
4096 std::sort (matches.begin (), matches.end ());
4097
4098 /* Finally call the callback, once per match. */
4099 ULONGEST prev = -1;
4100 for (offset_type idx : matches)
4101 {
4102 if (prev != idx)
4103 {
3b00ef10
TT
4104 if (!match_callback (idx))
4105 break;
3f563c84
PA
4106 prev = idx;
4107 }
4108 }
4109
4110 /* Above we use a type wider than idx's for 'prev', since 0 and
4111 (offset_type)-1 are both possible values. */
4112 static_assert (sizeof (prev) > sizeof (offset_type), "");
4113}
4114
c62446b1
PA
4115#if GDB_SELF_TEST
4116
4117namespace selftests { namespace dw2_expand_symtabs_matching {
4118
a3c5fafd
PA
4119/* A mock .gdb_index/.debug_names-like name index table, enough to
4120 exercise dw2_expand_symtabs_matching_symbol, which works with the
4121 mapped_index_base interface. Builds an index from the symbol list
4122 passed as parameter to the constructor. */
4123class mock_mapped_index : public mapped_index_base
c62446b1
PA
4124{
4125public:
a3c5fafd
PA
4126 mock_mapped_index (gdb::array_view<const char *> symbols)
4127 : m_symbol_table (symbols)
c62446b1
PA
4128 {}
4129
a3c5fafd 4130 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4131
a3c5fafd 4132 /* Return the number of names in the symbol table. */
632e107b 4133 size_t symbol_name_count () const override
c62446b1 4134 {
a3c5fafd 4135 return m_symbol_table.size ();
c62446b1
PA
4136 }
4137
a3c5fafd 4138 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4139 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4140 {
4141 return m_symbol_table[idx];
4142 }
c62446b1 4143
a3c5fafd
PA
4144private:
4145 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4146};
4147
4148/* Convenience function that converts a NULL pointer to a "<null>"
4149 string, to pass to print routines. */
4150
4151static const char *
4152string_or_null (const char *str)
4153{
4154 return str != NULL ? str : "<null>";
4155}
4156
4157/* Check if a lookup_name_info built from
4158 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4159 index. EXPECTED_LIST is the list of expected matches, in expected
4160 matching order. If no match expected, then an empty list is
4161 specified. Returns true on success. On failure prints a warning
4162 indicating the file:line that failed, and returns false. */
4163
4164static bool
4165check_match (const char *file, int line,
4166 mock_mapped_index &mock_index,
4167 const char *name, symbol_name_match_type match_type,
4168 bool completion_mode,
4169 std::initializer_list<const char *> expected_list)
4170{
4171 lookup_name_info lookup_name (name, match_type, completion_mode);
4172
4173 bool matched = true;
4174
4175 auto mismatch = [&] (const char *expected_str,
4176 const char *got)
4177 {
4178 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4179 "expected=\"%s\", got=\"%s\"\n"),
4180 file, line,
4181 (match_type == symbol_name_match_type::FULL
4182 ? "FULL" : "WILD"),
4183 name, string_or_null (expected_str), string_or_null (got));
4184 matched = false;
4185 };
4186
4187 auto expected_it = expected_list.begin ();
4188 auto expected_end = expected_list.end ();
4189
a3c5fafd 4190 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4191 NULL, ALL_DOMAIN,
4192 [&] (offset_type idx)
4193 {
a3c5fafd 4194 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4195 const char *expected_str
4196 = expected_it == expected_end ? NULL : *expected_it++;
4197
4198 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4199 mismatch (expected_str, matched_name);
3b00ef10 4200 return true;
c62446b1
PA
4201 });
4202
4203 const char *expected_str
4204 = expected_it == expected_end ? NULL : *expected_it++;
4205 if (expected_str != NULL)
4206 mismatch (expected_str, NULL);
4207
4208 return matched;
4209}
4210
4211/* The symbols added to the mock mapped_index for testing (in
4212 canonical form). */
4213static const char *test_symbols[] = {
4214 "function",
4215 "std::bar",
4216 "std::zfunction",
4217 "std::zfunction2",
4218 "w1::w2",
4219 "ns::foo<char*>",
4220 "ns::foo<int>",
4221 "ns::foo<long>",
a20714ff
PA
4222 "ns2::tmpl<int>::foo2",
4223 "(anonymous namespace)::A::B::C",
c62446b1 4224
e1ef7d7a
PA
4225 /* These are used to check that the increment-last-char in the
4226 matching algorithm for completion doesn't match "t1_fund" when
4227 completing "t1_func". */
4228 "t1_func",
4229 "t1_func1",
4230 "t1_fund",
4231 "t1_fund1",
4232
4233 /* A UTF-8 name with multi-byte sequences to make sure that
4234 cp-name-parser understands this as a single identifier ("função"
4235 is "function" in PT). */
4236 u8"u8função",
4237
4238 /* \377 (0xff) is Latin1 'ÿ'. */
4239 "yfunc\377",
4240
4241 /* \377 (0xff) is Latin1 'ÿ'. */
4242 "\377",
4243 "\377\377123",
4244
c62446b1
PA
4245 /* A name with all sorts of complications. Starts with "z" to make
4246 it easier for the completion tests below. */
4247#define Z_SYM_NAME \
4248 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4249 "::tuple<(anonymous namespace)::ui*, " \
4250 "std::default_delete<(anonymous namespace)::ui>, void>"
4251
4252 Z_SYM_NAME
4253};
4254
a3c5fafd
PA
4255/* Returns true if the mapped_index_base::find_name_component_bounds
4256 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4257 in completion mode. */
5c58de74
PA
4258
4259static bool
a3c5fafd 4260check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4261 const char *search_name,
4262 gdb::array_view<const char *> expected_syms)
4263{
4264 lookup_name_info lookup_name (search_name,
4265 symbol_name_match_type::FULL, true);
4266
3b00ef10
TT
4267 auto bounds = index.find_name_components_bounds (lookup_name,
4268 language_cplus);
5c58de74
PA
4269
4270 size_t distance = std::distance (bounds.first, bounds.second);
4271 if (distance != expected_syms.size ())
4272 return false;
4273
4274 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4275 {
4276 auto nc_elem = bounds.first + exp_elem;
4277 const char *qualified = index.symbol_name_at (nc_elem->idx);
4278 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4279 return false;
4280 }
4281
4282 return true;
4283}
4284
4285/* Test the lower-level mapped_index::find_name_component_bounds
4286 method. */
4287
c62446b1 4288static void
5c58de74
PA
4289test_mapped_index_find_name_component_bounds ()
4290{
4291 mock_mapped_index mock_index (test_symbols);
4292
a3c5fafd 4293 mock_index.build_name_components ();
5c58de74
PA
4294
4295 /* Test the lower-level mapped_index::find_name_component_bounds
4296 method in completion mode. */
4297 {
4298 static const char *expected_syms[] = {
4299 "t1_func",
4300 "t1_func1",
5c58de74
PA
4301 };
4302
a3c5fafd 4303 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4304 "t1_func", expected_syms));
4305 }
4306
4307 /* Check that the increment-last-char in the name matching algorithm
4308 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4309 {
4310 static const char *expected_syms1[] = {
4311 "\377",
4312 "\377\377123",
4313 };
a3c5fafd 4314 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4315 "\377", expected_syms1));
4316
4317 static const char *expected_syms2[] = {
4318 "\377\377123",
4319 };
a3c5fafd 4320 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4321 "\377\377", expected_syms2));
4322 }
4323}
4324
4325/* Test dw2_expand_symtabs_matching_symbol. */
4326
4327static void
4328test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4329{
4330 mock_mapped_index mock_index (test_symbols);
4331
4332 /* We let all tests run until the end even if some fails, for debug
4333 convenience. */
4334 bool any_mismatch = false;
4335
4336 /* Create the expected symbols list (an initializer_list). Needed
4337 because lists have commas, and we need to pass them to CHECK,
4338 which is a macro. */
4339#define EXPECT(...) { __VA_ARGS__ }
4340
4341 /* Wrapper for check_match that passes down the current
4342 __FILE__/__LINE__. */
4343#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4344 any_mismatch |= !check_match (__FILE__, __LINE__, \
4345 mock_index, \
4346 NAME, MATCH_TYPE, COMPLETION_MODE, \
4347 EXPECTED_LIST)
4348
4349 /* Identity checks. */
4350 for (const char *sym : test_symbols)
4351 {
4352 /* Should be able to match all existing symbols. */
4353 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4354 EXPECT (sym));
4355
4356 /* Should be able to match all existing symbols with
4357 parameters. */
4358 std::string with_params = std::string (sym) + "(int)";
4359 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4360 EXPECT (sym));
4361
4362 /* Should be able to match all existing symbols with
4363 parameters and qualifiers. */
4364 with_params = std::string (sym) + " ( int ) const";
4365 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4366 EXPECT (sym));
4367
4368 /* This should really find sym, but cp-name-parser.y doesn't
4369 know about lvalue/rvalue qualifiers yet. */
4370 with_params = std::string (sym) + " ( int ) &&";
4371 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4372 {});
4373 }
4374
e1ef7d7a
PA
4375 /* Check that the name matching algorithm for completion doesn't get
4376 confused with Latin1 'ÿ' / 0xff. */
4377 {
4378 static const char str[] = "\377";
4379 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4380 EXPECT ("\377", "\377\377123"));
4381 }
4382
4383 /* Check that the increment-last-char in the matching algorithm for
4384 completion doesn't match "t1_fund" when completing "t1_func". */
4385 {
4386 static const char str[] = "t1_func";
4387 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4388 EXPECT ("t1_func", "t1_func1"));
4389 }
4390
c62446b1
PA
4391 /* Check that completion mode works at each prefix of the expected
4392 symbol name. */
4393 {
4394 static const char str[] = "function(int)";
4395 size_t len = strlen (str);
4396 std::string lookup;
4397
4398 for (size_t i = 1; i < len; i++)
4399 {
4400 lookup.assign (str, i);
4401 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4402 EXPECT ("function"));
4403 }
4404 }
4405
4406 /* While "w" is a prefix of both components, the match function
4407 should still only be called once. */
4408 {
4409 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4410 EXPECT ("w1::w2"));
a20714ff
PA
4411 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4412 EXPECT ("w1::w2"));
c62446b1
PA
4413 }
4414
4415 /* Same, with a "complicated" symbol. */
4416 {
4417 static const char str[] = Z_SYM_NAME;
4418 size_t len = strlen (str);
4419 std::string lookup;
4420
4421 for (size_t i = 1; i < len; i++)
4422 {
4423 lookup.assign (str, i);
4424 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4425 EXPECT (Z_SYM_NAME));
4426 }
4427 }
4428
4429 /* In FULL mode, an incomplete symbol doesn't match. */
4430 {
4431 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4432 {});
4433 }
4434
4435 /* A complete symbol with parameters matches any overload, since the
4436 index has no overload info. */
4437 {
4438 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4439 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4440 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4441 EXPECT ("std::zfunction", "std::zfunction2"));
4442 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4443 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4444 }
4445
4446 /* Check that whitespace is ignored appropriately. A symbol with a
4447 template argument list. */
4448 {
4449 static const char expected[] = "ns::foo<int>";
4450 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4451 EXPECT (expected));
a20714ff
PA
4452 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4453 EXPECT (expected));
c62446b1
PA
4454 }
4455
4456 /* Check that whitespace is ignored appropriately. A symbol with a
4457 template argument list that includes a pointer. */
4458 {
4459 static const char expected[] = "ns::foo<char*>";
4460 /* Try both completion and non-completion modes. */
4461 static const bool completion_mode[2] = {false, true};
4462 for (size_t i = 0; i < 2; i++)
4463 {
4464 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4465 completion_mode[i], EXPECT (expected));
a20714ff
PA
4466 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4467 completion_mode[i], EXPECT (expected));
c62446b1
PA
4468
4469 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4470 completion_mode[i], EXPECT (expected));
a20714ff
PA
4471 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4472 completion_mode[i], EXPECT (expected));
c62446b1
PA
4473 }
4474 }
4475
4476 {
4477 /* Check method qualifiers are ignored. */
4478 static const char expected[] = "ns::foo<char*>";
4479 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4480 symbol_name_match_type::FULL, true, EXPECT (expected));
4481 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4482 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4483 CHECK_MATCH ("foo < char * > ( int ) const",
4484 symbol_name_match_type::WILD, true, EXPECT (expected));
4485 CHECK_MATCH ("foo < char * > ( int ) &&",
4486 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4487 }
4488
4489 /* Test lookup names that don't match anything. */
4490 {
a20714ff
PA
4491 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4492 {});
4493
c62446b1
PA
4494 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4495 {});
4496 }
4497
a20714ff
PA
4498 /* Some wild matching tests, exercising "(anonymous namespace)",
4499 which should not be confused with a parameter list. */
4500 {
4501 static const char *syms[] = {
4502 "A::B::C",
4503 "B::C",
4504 "C",
4505 "A :: B :: C ( int )",
4506 "B :: C ( int )",
4507 "C ( int )",
4508 };
4509
4510 for (const char *s : syms)
4511 {
4512 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4513 EXPECT ("(anonymous namespace)::A::B::C"));
4514 }
4515 }
4516
4517 {
4518 static const char expected[] = "ns2::tmpl<int>::foo2";
4519 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4520 EXPECT (expected));
4521 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4522 EXPECT (expected));
4523 }
4524
c62446b1
PA
4525 SELF_CHECK (!any_mismatch);
4526
4527#undef EXPECT
4528#undef CHECK_MATCH
4529}
4530
5c58de74
PA
4531static void
4532run_test ()
4533{
4534 test_mapped_index_find_name_component_bounds ();
4535 test_dw2_expand_symtabs_matching_symbol ();
4536}
4537
c62446b1
PA
4538}} // namespace selftests::dw2_expand_symtabs_matching
4539
4540#endif /* GDB_SELF_TEST */
4541
4b514bc8
JK
4542/* If FILE_MATCHER is NULL or if PER_CU has
4543 dwarf2_per_cu_quick_data::MARK set (see
4544 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4545 EXPANSION_NOTIFY on it. */
4546
4547static void
4548dw2_expand_symtabs_matching_one
97a1449a
SM
4549 (dwarf2_per_cu_data *per_cu,
4550 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4551 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4552 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4553{
4554 if (file_matcher == NULL || per_cu->v.quick->mark)
4555 {
af758d11 4556 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4557
97a1449a
SM
4558 compunit_symtab *symtab
4559 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4560 gdb_assert (symtab != nullptr);
4b514bc8 4561
af758d11
SM
4562 if (expansion_notify != NULL && symtab_was_null)
4563 expansion_notify (symtab);
4b514bc8
JK
4564 }
4565}
4566
3f563c84
PA
4567/* Helper for dw2_expand_matching symtabs. Called on each symbol
4568 matched, to expand corresponding CUs that were marked. IDX is the
4569 index of the symbol name that matched. */
4570
4571static void
4572dw2_expand_marked_cus
97a1449a 4573 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4575 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4576 search_domain kind)
4577{
3f563c84
PA
4578 offset_type *vec, vec_len, vec_idx;
4579 bool global_seen = false;
5989a64e 4580 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4581
61920122 4582 vec = (offset_type *) (index.constant_pool
f00a2de2 4583 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4584 vec_len = MAYBE_SWAP (vec[0]);
4585 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4586 {
61920122
PA
4587 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4588 /* This value is only valid for index versions >= 7. */
4589 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4590 gdb_index_symbol_kind symbol_kind =
4591 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4592 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4593 /* Only check the symbol attributes if they're present.
4594 Indices prior to version 7 don't record them,
4595 and indices >= 7 may elide them for certain symbols
4596 (gold does this). */
4597 int attrs_valid =
4598 (index.version >= 7
4599 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4600
4601 /* Work around gold/15646. */
4602 if (attrs_valid)
9291a0cd 4603 {
61920122
PA
4604 if (!is_static && global_seen)
4605 continue;
4606 if (!is_static)
4607 global_seen = true;
4608 }
3190f0c6 4609
61920122
PA
4610 /* Only check the symbol's kind if it has one. */
4611 if (attrs_valid)
4612 {
4613 switch (kind)
8943b874 4614 {
61920122
PA
4615 case VARIABLES_DOMAIN:
4616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4617 continue;
4618 break;
4619 case FUNCTIONS_DOMAIN:
4620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4621 continue;
61920122
PA
4622 break;
4623 case TYPES_DOMAIN:
4624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4625 continue;
4626 break;
59c35742
AB
4627 case MODULES_DOMAIN:
4628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4629 continue;
4630 break;
61920122
PA
4631 default:
4632 break;
8943b874 4633 }
61920122 4634 }
8943b874 4635
61920122 4636 /* Don't crash on bad data. */
5989a64e
SM
4637 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4638 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4639 {
b98664d3 4640 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4641 " [in module %s]"),
4642 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4643 continue;
4644 }
4645
5989a64e 4646 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4647 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4648 expansion_notify);
61920122
PA
4649 }
4650}
4651
4b514bc8
JK
4652/* If FILE_MATCHER is non-NULL, set all the
4653 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4654 that match FILE_MATCHER. */
4655
61920122 4656static void
4b514bc8 4657dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4658 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4659 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4660{
4b514bc8 4661 if (file_matcher == NULL)
61920122
PA
4662 return;
4663
4b514bc8
JK
4664 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4665 htab_eq_pointer,
4666 NULL, xcalloc, xfree));
4667 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4668 htab_eq_pointer,
4669 NULL, xcalloc, xfree));
61920122 4670
4b514bc8
JK
4671 /* The rule is CUs specify all the files, including those used by
4672 any TU, so there's no need to scan TUs here. */
61920122 4673
5989a64e 4674 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4675 {
927aa2e7
JK
4676 QUIT;
4677
4678 per_cu->v.quick->mark = 0;
4679
4680 /* We only need to look at symtabs not already expanded. */
af758d11 4681 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4682 continue;
4683
ab432490
SM
4684 quick_file_names *file_data
4685 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4686 if (file_data == NULL)
4687 continue;
4688
4689 if (htab_find (visited_not_found.get (), file_data) != NULL)
4690 continue;
4691 else if (htab_find (visited_found.get (), file_data) != NULL)
4692 {
4693 per_cu->v.quick->mark = 1;
4694 continue;
4695 }
4696
b76e467d 4697 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4698 {
4699 const char *this_real_name;
4700
4701 if (file_matcher (file_data->file_names[j], false))
4702 {
4703 per_cu->v.quick->mark = 1;
4704 break;
4705 }
4706
4707 /* Before we invoke realpath, which can get expensive when many
4708 files are involved, do a quick comparison of the basenames. */
4709 if (!basenames_may_differ
4710 && !file_matcher (lbasename (file_data->file_names[j]),
4711 true))
4712 continue;
4713
45940949
TT
4714 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4715 file_data, j);
927aa2e7
JK
4716 if (file_matcher (this_real_name, false))
4717 {
4718 per_cu->v.quick->mark = 1;
4719 break;
4720 }
4721 }
4722
b76e467d
SM
4723 void **slot = htab_find_slot (per_cu->v.quick->mark
4724 ? visited_found.get ()
4725 : visited_not_found.get (),
4726 file_data, INSERT);
927aa2e7
JK
4727 *slot = file_data;
4728 }
4729}
4730
4731static void
4732dw2_expand_symtabs_matching
4733 (struct objfile *objfile,
4734 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4735 const lookup_name_info *lookup_name,
927aa2e7
JK
4736 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4737 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4738 enum search_domain kind)
4739{
ed2dc618
SM
4740 struct dwarf2_per_objfile *dwarf2_per_objfile
4741 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4742
4743 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4744 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4745 return;
4746
ed2dc618 4747 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4748
c1a66c06
TV
4749 if (symbol_matcher == NULL && lookup_name == NULL)
4750 {
5989a64e 4751 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4752 {
4753 QUIT;
4754
97a1449a
SM
4755 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4756 file_matcher, expansion_notify);
c1a66c06
TV
4757 }
4758 return;
4759 }
4760
5989a64e 4761 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4762
c1a66c06 4763 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4764 symbol_matcher,
4765 kind, [&] (offset_type idx)
4766 {
ed2dc618 4767 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4768 expansion_notify, kind);
3b00ef10 4769 return true;
927aa2e7
JK
4770 });
4771}
4772
4773/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4774 symtab. */
4775
4776static struct compunit_symtab *
4777recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4778 CORE_ADDR pc)
4779{
4780 int i;
4781
4782 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4783 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4784 return cust;
4785
4786 if (cust->includes == NULL)
4787 return NULL;
4788
4789 for (i = 0; cust->includes[i]; ++i)
4790 {
4791 struct compunit_symtab *s = cust->includes[i];
4792
4793 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4794 if (s != NULL)
4795 return s;
4796 }
4797
4798 return NULL;
4799}
4800
4801static struct compunit_symtab *
4802dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4803 struct bound_minimal_symbol msymbol,
4804 CORE_ADDR pc,
4805 struct obj_section *section,
4806 int warn_if_readin)
4807{
4808 struct dwarf2_per_cu_data *data;
4809 struct compunit_symtab *result;
4810
d320c2b5 4811 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4812 return NULL;
4813
b3b3bada 4814 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4815 data = (struct dwarf2_per_cu_data *) addrmap_find
4816 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4817 if (!data)
4818 return NULL;
4819
af758d11
SM
4820 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4821 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4822 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4823 paddress (objfile->arch (), pc));
927aa2e7 4824
97a1449a
SM
4825 result = recursively_find_pc_sect_compunit_symtab
4826 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4827
927aa2e7
JK
4828 gdb_assert (result != NULL);
4829 return result;
4830}
4831
4832static void
4833dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4834 void *data, int need_fullname)
4835{
ed2dc618
SM
4836 struct dwarf2_per_objfile *dwarf2_per_objfile
4837 = get_dwarf2_per_objfile (objfile);
927aa2e7 4838
5989a64e 4839 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4840 {
5989a64e 4841 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4842
4843 htab_up visited (htab_create_alloc (10,
4844 htab_hash_pointer, htab_eq_pointer,
4845 NULL, xcalloc, xfree));
4846
4847 /* The rule is CUs specify all the files, including those used
4848 by any TU, so there's no need to scan TUs here. We can
4849 ignore file names coming from already-expanded CUs. */
4850
5989a64e 4851 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4852 {
af758d11 4853 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4854 {
4855 void **slot = htab_find_slot (visited.get (),
4856 per_cu->v.quick->file_names,
4857 INSERT);
4858
4859 *slot = per_cu->v.quick->file_names;
4860 }
4861 }
4862
5989a64e 4863 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4864 {
927aa2e7 4865 /* We only need to look at symtabs not already expanded. */
af758d11 4866 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4867 continue;
4868
ab432490
SM
4869 quick_file_names *file_data
4870 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4871 if (file_data == NULL)
4872 continue;
4873
b76e467d 4874 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4875 if (*slot)
4876 {
4877 /* Already visited. */
4878 continue;
4879 }
4880 *slot = file_data;
4881
4882 for (int j = 0; j < file_data->num_file_names; ++j)
4883 {
4884 const char *filename = file_data->file_names[j];
5989a64e 4885 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4886 }
4887 }
4888 }
4889
5989a64e 4890 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4891 {
4892 gdb::unique_xmalloc_ptr<char> this_real_name;
4893
4894 if (need_fullname)
4895 this_real_name = gdb_realpath (filename);
4896 (*fun) (filename, this_real_name.get (), data);
4897 });
4898}
4899
4900static int
4901dw2_has_symbols (struct objfile *objfile)
4902{
4903 return 1;
4904}
4905
4906const struct quick_symbol_functions dwarf2_gdb_index_functions =
4907{
4908 dw2_has_symbols,
4909 dw2_find_last_source_symtab,
4910 dw2_forget_cached_source_info,
4911 dw2_map_symtabs_matching_filename,
4912 dw2_lookup_symbol,
d3214198 4913 NULL,
927aa2e7
JK
4914 dw2_print_stats,
4915 dw2_dump,
927aa2e7
JK
4916 dw2_expand_symtabs_for_function,
4917 dw2_expand_all_symtabs,
4918 dw2_expand_symtabs_with_fullname,
4919 dw2_map_matching_symbols,
4920 dw2_expand_symtabs_matching,
4921 dw2_find_pc_sect_compunit_symtab,
4922 NULL,
4923 dw2_map_symbol_filenames
4924};
4925
4926/* DWARF-5 debug_names reader. */
4927
4928/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4929static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4930
4931/* A helper function that reads the .debug_names section in SECTION
4932 and fills in MAP. FILENAME is the name of the file containing the
4933 section; it is used for error reporting.
4934
4935 Returns true if all went well, false otherwise. */
4936
4937static bool
4938read_debug_names_from_section (struct objfile *objfile,
4939 const char *filename,
4940 struct dwarf2_section_info *section,
4941 mapped_debug_names &map)
4942{
96b79293 4943 if (section->empty ())
927aa2e7
JK
4944 return false;
4945
4946 /* Older elfutils strip versions could keep the section in the main
4947 executable while splitting it for the separate debug info file. */
96b79293 4948 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4949 return false;
4950
96b79293 4951 section->read (objfile);
927aa2e7 4952
08feed99 4953 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4954
4955 const gdb_byte *addr = section->buffer;
4956
96b79293 4957 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4958
4959 unsigned int bytes_read;
4960 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4961 addr += bytes_read;
4962
4963 map.dwarf5_is_dwarf64 = bytes_read != 4;
4964 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4965 if (bytes_read + length != section->size)
4966 {
4967 /* There may be multiple per-CU indices. */
4968 warning (_("Section .debug_names in %s length %s does not match "
4969 "section length %s, ignoring .debug_names."),
4970 filename, plongest (bytes_read + length),
4971 pulongest (section->size));
4972 return false;
4973 }
4974
4975 /* The version number. */
4976 uint16_t version = read_2_bytes (abfd, addr);
4977 addr += 2;
4978 if (version != 5)
4979 {
4980 warning (_("Section .debug_names in %s has unsupported version %d, "
4981 "ignoring .debug_names."),
4982 filename, version);
4983 return false;
4984 }
4985
4986 /* Padding. */
4987 uint16_t padding = read_2_bytes (abfd, addr);
4988 addr += 2;
4989 if (padding != 0)
4990 {
4991 warning (_("Section .debug_names in %s has unsupported padding %d, "
4992 "ignoring .debug_names."),
4993 filename, padding);
4994 return false;
4995 }
4996
4997 /* comp_unit_count - The number of CUs in the CU list. */
4998 map.cu_count = read_4_bytes (abfd, addr);
4999 addr += 4;
5000
5001 /* local_type_unit_count - The number of TUs in the local TU
5002 list. */
5003 map.tu_count = read_4_bytes (abfd, addr);
5004 addr += 4;
5005
5006 /* foreign_type_unit_count - The number of TUs in the foreign TU
5007 list. */
5008 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5009 addr += 4;
5010 if (foreign_tu_count != 0)
5011 {
5012 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5013 "ignoring .debug_names."),
5014 filename, static_cast<unsigned long> (foreign_tu_count));
5015 return false;
5016 }
5017
5018 /* bucket_count - The number of hash buckets in the hash lookup
5019 table. */
5020 map.bucket_count = read_4_bytes (abfd, addr);
5021 addr += 4;
5022
5023 /* name_count - The number of unique names in the index. */
5024 map.name_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* abbrev_table_size - The size in bytes of the abbreviations
5028 table. */
5029 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5030 addr += 4;
5031
5032 /* augmentation_string_size - The size in bytes of the augmentation
5033 string. This value is rounded up to a multiple of 4. */
5034 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5035 addr += 4;
5036 map.augmentation_is_gdb = ((augmentation_string_size
5037 == sizeof (dwarf5_augmentation))
5038 && memcmp (addr, dwarf5_augmentation,
5039 sizeof (dwarf5_augmentation)) == 0);
5040 augmentation_string_size += (-augmentation_string_size) & 3;
5041 addr += augmentation_string_size;
5042
5043 /* List of CUs */
5044 map.cu_table_reordered = addr;
5045 addr += map.cu_count * map.offset_size;
5046
5047 /* List of Local TUs */
5048 map.tu_table_reordered = addr;
5049 addr += map.tu_count * map.offset_size;
5050
5051 /* Hash Lookup Table */
5052 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5053 addr += map.bucket_count * 4;
5054 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5055 addr += map.name_count * 4;
5056
5057 /* Name Table */
5058 map.name_table_string_offs_reordered = addr;
5059 addr += map.name_count * map.offset_size;
5060 map.name_table_entry_offs_reordered = addr;
5061 addr += map.name_count * map.offset_size;
5062
5063 const gdb_byte *abbrev_table_start = addr;
5064 for (;;)
5065 {
927aa2e7
JK
5066 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5067 addr += bytes_read;
5068 if (index_num == 0)
5069 break;
5070
5071 const auto insertpair
5072 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5073 if (!insertpair.second)
5074 {
5075 warning (_("Section .debug_names in %s has duplicate index %s, "
5076 "ignoring .debug_names."),
5077 filename, pulongest (index_num));
5078 return false;
5079 }
5080 mapped_debug_names::index_val &indexval = insertpair.first->second;
5081 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5082 addr += bytes_read;
5083
5084 for (;;)
5085 {
5086 mapped_debug_names::index_val::attr attr;
5087 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5088 addr += bytes_read;
5089 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5090 addr += bytes_read;
5091 if (attr.form == DW_FORM_implicit_const)
5092 {
5093 attr.implicit_const = read_signed_leb128 (abfd, addr,
5094 &bytes_read);
5095 addr += bytes_read;
5096 }
5097 if (attr.dw_idx == 0 && attr.form == 0)
5098 break;
5099 indexval.attr_vec.push_back (std::move (attr));
5100 }
5101 }
5102 if (addr != abbrev_table_start + abbrev_table_size)
5103 {
5104 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5105 "of size %s vs. written as %u, ignoring .debug_names."),
5106 filename, plongest (addr - abbrev_table_start),
5107 abbrev_table_size);
927aa2e7
JK
5108 return false;
5109 }
5110 map.entry_pool = addr;
5111
5112 return true;
5113}
5114
5115/* A helper for create_cus_from_debug_names that handles the MAP's CU
5116 list. */
5117
5118static void
ed2dc618 5119create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5120 const mapped_debug_names &map,
5121 dwarf2_section_info &section,
b76e467d 5122 bool is_dwz)
927aa2e7 5123{
3ee6bb11
TV
5124 if (!map.augmentation_is_gdb)
5125 {
5126 for (uint32_t i = 0; i < map.cu_count; ++i)
5127 {
5128 sect_offset sect_off
5129 = (sect_offset) (extract_unsigned_integer
5130 (map.cu_table_reordered + i * map.offset_size,
5131 map.offset_size,
5132 map.dwarf5_byte_order));
5133 /* We don't know the length of the CU, because the CU list in a
5134 .debug_names index can be incomplete, so we can't use the start of
5135 the next CU as end of this CU. We create the CUs here with length 0,
5136 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5137 dwarf2_per_cu_data *per_cu
5138 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5139 sect_off, 0);
5989a64e 5140 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5141 }
5142 }
5143
927aa2e7
JK
5144 sect_offset sect_off_prev;
5145 for (uint32_t i = 0; i <= map.cu_count; ++i)
5146 {
5147 sect_offset sect_off_next;
5148 if (i < map.cu_count)
5149 {
5150 sect_off_next
5151 = (sect_offset) (extract_unsigned_integer
5152 (map.cu_table_reordered + i * map.offset_size,
5153 map.offset_size,
5154 map.dwarf5_byte_order));
5155 }
5156 else
5157 sect_off_next = (sect_offset) section.size;
5158 if (i >= 1)
5159 {
5160 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5161 dwarf2_per_cu_data *per_cu
ed2dc618 5162 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5163 sect_off_prev, length);
5989a64e 5164 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5165 }
5166 sect_off_prev = sect_off_next;
5167 }
5168}
5169
5170/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5171 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5172
5173static void
ed2dc618 5174create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5175 const mapped_debug_names &map,
5176 const mapped_debug_names &dwz_map)
5177{
5989a64e
SM
5178 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5179 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5180
ed2dc618 5181 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5182 dwarf2_per_objfile->per_bfd->info,
b76e467d 5183 false /* is_dwz */);
927aa2e7
JK
5184
5185 if (dwz_map.cu_count == 0)
5186 return;
5187
c3699833 5188 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5189 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5190 true /* is_dwz */);
927aa2e7
JK
5191}
5192
5193/* Read .debug_names. If everything went ok, initialize the "quick"
5194 elements of all the CUs and return true. Otherwise, return false. */
5195
5196static bool
ed2dc618 5197dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5198{
22ca247e
TT
5199 std::unique_ptr<mapped_debug_names> map
5200 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5201 mapped_debug_names dwz_map (dwarf2_per_objfile);
5202 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5203
5204 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5205 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5206 *map))
927aa2e7
JK
5207 return false;
5208
5209 /* Don't use the index if it's empty. */
22ca247e 5210 if (map->name_count == 0)
927aa2e7
JK
5211 return false;
5212
5213 /* If there is a .dwz file, read it so we can get its CU list as
5214 well. */
c3699833 5215 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5216 if (dwz != NULL)
5217 {
5218 if (!read_debug_names_from_section (objfile,
00f93c44 5219 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5220 &dwz->debug_names, dwz_map))
5221 {
5222 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5223 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5224 return false;
5225 }
5226 }
5227
22ca247e 5228 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5229
22ca247e 5230 if (map->tu_count != 0)
927aa2e7
JK
5231 {
5232 /* We can only handle a single .debug_types when we have an
5233 index. */
5989a64e 5234 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5235 return false;
5236
5989a64e 5237 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5238
5239 create_signatured_type_table_from_debug_names
5989a64e 5240 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5241 }
5242
ed2dc618 5243 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5244 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5245
5989a64e
SM
5246 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5247 dwarf2_per_objfile->per_bfd->using_index = 1;
5248 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5249 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5250
5251 return true;
5252}
5253
927aa2e7
JK
5254/* Type used to manage iterating over all CUs looking for a symbol for
5255 .debug_names. */
5256
5257class dw2_debug_names_iterator
5258{
5259public:
927aa2e7 5260 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5261 gdb::optional<block_enum> block_index,
5262 domain_enum domain,
927aa2e7 5263 const char *name)
2b79f376 5264 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5265 m_addr (find_vec_in_debug_names (map, name))
5266 {}
5267
5268 dw2_debug_names_iterator (const mapped_debug_names &map,
5269 search_domain search, uint32_t namei)
5270 : m_map (map),
5271 m_search (search),
5272 m_addr (find_vec_in_debug_names (map, namei))
5273 {}
5274
3b00ef10
TT
5275 dw2_debug_names_iterator (const mapped_debug_names &map,
5276 block_enum block_index, domain_enum domain,
5277 uint32_t namei)
5278 : m_map (map), m_block_index (block_index), m_domain (domain),
5279 m_addr (find_vec_in_debug_names (map, namei))
5280 {}
5281
927aa2e7
JK
5282 /* Return the next matching CU or NULL if there are no more. */
5283 dwarf2_per_cu_data *next ();
5284
5285private:
5286 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5287 const char *name);
5288 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5289 uint32_t namei);
5290
5291 /* The internalized form of .debug_names. */
5292 const mapped_debug_names &m_map;
5293
2b79f376
SM
5294 /* If set, only look for symbols that match that block. Valid values are
5295 GLOBAL_BLOCK and STATIC_BLOCK. */
5296 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5297
5298 /* The kind of symbol we're looking for. */
5299 const domain_enum m_domain = UNDEF_DOMAIN;
5300 const search_domain m_search = ALL_DOMAIN;
5301
5302 /* The list of CUs from the index entry of the symbol, or NULL if
5303 not found. */
5304 const gdb_byte *m_addr;
5305};
5306
5307const char *
5308mapped_debug_names::namei_to_name (uint32_t namei) const
5309{
5310 const ULONGEST namei_string_offs
5311 = extract_unsigned_integer ((name_table_string_offs_reordered
5312 + namei * offset_size),
5313 offset_size,
5314 dwarf5_byte_order);
4f44ae6c
TT
5315 return read_indirect_string_at_offset (dwarf2_per_objfile,
5316 namei_string_offs);
927aa2e7
JK
5317}
5318
5319/* Find a slot in .debug_names for the object named NAME. If NAME is
5320 found, return pointer to its pool data. If NAME cannot be found,
5321 return NULL. */
5322
5323const gdb_byte *
5324dw2_debug_names_iterator::find_vec_in_debug_names
5325 (const mapped_debug_names &map, const char *name)
5326{
5327 int (*cmp) (const char *, const char *);
5328
54ee4252 5329 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5330 if (current_language->la_language == language_cplus
5331 || current_language->la_language == language_fortran
5332 || current_language->la_language == language_d)
5333 {
5334 /* NAME is already canonical. Drop any qualifiers as
5335 .debug_names does not contain any. */
5336
5337 if (strchr (name, '(') != NULL)
5338 {
54ee4252 5339 without_params = cp_remove_params (name);
927aa2e7 5340 if (without_params != NULL)
54ee4252 5341 name = without_params.get ();
927aa2e7
JK
5342 }
5343 }
5344
5345 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5346
5347 const uint32_t full_hash = dwarf5_djb_hash (name);
5348 uint32_t namei
5349 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5350 (map.bucket_table_reordered
5351 + (full_hash % map.bucket_count)), 4,
5352 map.dwarf5_byte_order);
5353 if (namei == 0)
5354 return NULL;
5355 --namei;
5356 if (namei >= map.name_count)
5357 {
b98664d3 5358 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5359 "[in module %s]"),
5360 namei, map.name_count,
ed2dc618 5361 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5362 return NULL;
5363 }
5364
5365 for (;;)
5366 {
5367 const uint32_t namei_full_hash
5368 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5369 (map.hash_table_reordered + namei), 4,
5370 map.dwarf5_byte_order);
5371 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5372 return NULL;
5373
5374 if (full_hash == namei_full_hash)
5375 {
5376 const char *const namei_string = map.namei_to_name (namei);
5377
5378#if 0 /* An expensive sanity check. */
5379 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5380 {
b98664d3 5381 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5382 "[in module %s]"),
5383 namei, objfile_name (dwarf2_per_objfile->objfile));
5384 return NULL;
5385 }
5386#endif
5387
5388 if (cmp (namei_string, name) == 0)
5389 {
5390 const ULONGEST namei_entry_offs
5391 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5392 + namei * map.offset_size),
5393 map.offset_size, map.dwarf5_byte_order);
5394 return map.entry_pool + namei_entry_offs;
5395 }
5396 }
5397
5398 ++namei;
5399 if (namei >= map.name_count)
5400 return NULL;
5401 }
5402}
5403
5404const gdb_byte *
5405dw2_debug_names_iterator::find_vec_in_debug_names
5406 (const mapped_debug_names &map, uint32_t namei)
5407{
5408 if (namei >= map.name_count)
5409 {
b98664d3 5410 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5411 "[in module %s]"),
5412 namei, map.name_count,
ed2dc618 5413 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5414 return NULL;
5415 }
5416
5417 const ULONGEST namei_entry_offs
5418 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5419 + namei * map.offset_size),
5420 map.offset_size, map.dwarf5_byte_order);
5421 return map.entry_pool + namei_entry_offs;
5422}
5423
5424/* See dw2_debug_names_iterator. */
5425
5426dwarf2_per_cu_data *
5427dw2_debug_names_iterator::next ()
5428{
5429 if (m_addr == NULL)
5430 return NULL;
5431
ed2dc618
SM
5432 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5433 struct objfile *objfile = dwarf2_per_objfile->objfile;
5434 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5435
5436 again:
5437
5438 unsigned int bytes_read;
5439 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5440 m_addr += bytes_read;
5441 if (abbrev == 0)
5442 return NULL;
5443
5444 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5445 if (indexval_it == m_map.abbrev_map.cend ())
5446 {
b98664d3 5447 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5448 "[in module %s]"),
ed2dc618 5449 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5450 return NULL;
5451 }
5452 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5453 enum class symbol_linkage {
5454 unknown,
5455 static_,
5456 extern_,
23c13d42 5457 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5458 dwarf2_per_cu_data *per_cu = NULL;
5459 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5460 {
5461 ULONGEST ull;
5462 switch (attr.form)
5463 {
5464 case DW_FORM_implicit_const:
5465 ull = attr.implicit_const;
5466 break;
5467 case DW_FORM_flag_present:
5468 ull = 1;
5469 break;
5470 case DW_FORM_udata:
5471 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5472 m_addr += bytes_read;
5473 break;
6dc55ce9 5474 case DW_FORM_ref4:
5475 ull = read_4_bytes (abfd, m_addr);
5476 m_addr += 4;
5477 break;
5478 case DW_FORM_ref8:
5479 ull = read_8_bytes (abfd, m_addr);
5480 m_addr += 8;
5481 break;
5482 case DW_FORM_ref_sig8:
5483 ull = read_8_bytes (abfd, m_addr);
5484 m_addr += 8;
5485 break;
927aa2e7 5486 default:
b98664d3 5487 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5488 dwarf_form_name (attr.form),
ed2dc618 5489 objfile_name (objfile));
927aa2e7
JK
5490 return NULL;
5491 }
5492 switch (attr.dw_idx)
5493 {
5494 case DW_IDX_compile_unit:
5495 /* Don't crash on bad data. */
5989a64e 5496 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5497 {
b98664d3 5498 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5499 " [in module %s]"),
5500 pulongest (ull),
5501 objfile_name (dwarf2_per_objfile->objfile));
5502 continue;
5503 }
5989a64e 5504 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5505 break;
8af5c486
JK
5506 case DW_IDX_type_unit:
5507 /* Don't crash on bad data. */
5989a64e 5508 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5509 {
b98664d3 5510 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5511 " [in module %s]"),
5512 pulongest (ull),
5513 objfile_name (dwarf2_per_objfile->objfile));
5514 continue;
5515 }
5989a64e 5516 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5517 break;
6dc55ce9 5518 case DW_IDX_die_offset:
5519 /* In a per-CU index (as opposed to a per-module index), index
5520 entries without CU attribute implicitly refer to the single CU. */
5521 if (per_cu == NULL)
5989a64e 5522 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5523 break;
927aa2e7
JK
5524 case DW_IDX_GNU_internal:
5525 if (!m_map.augmentation_is_gdb)
5526 break;
23c13d42 5527 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5528 break;
5529 case DW_IDX_GNU_external:
5530 if (!m_map.augmentation_is_gdb)
5531 break;
23c13d42 5532 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5533 break;
5534 }
5535 }
5536
5537 /* Skip if already read in. */
af758d11 5538 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5539 goto again;
5540
5541 /* Check static vs global. */
23c13d42 5542 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5543 {
2b79f376 5544 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5545 const bool symbol_is_static =
5546 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5547 if (want_static != symbol_is_static)
2b79f376 5548 goto again;
927aa2e7
JK
5549 }
5550
5551 /* Match dw2_symtab_iter_next, symbol_kind
5552 and debug_names::psymbol_tag. */
5553 switch (m_domain)
5554 {
5555 case VAR_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case DW_TAG_variable:
5559 case DW_TAG_subprogram:
5560 /* Some types are also in VAR_DOMAIN. */
5561 case DW_TAG_typedef:
5562 case DW_TAG_structure_type:
5563 break;
5564 default:
5565 goto again;
5566 }
5567 break;
5568 case STRUCT_DOMAIN:
5569 switch (indexval.dwarf_tag)
5570 {
5571 case DW_TAG_typedef:
5572 case DW_TAG_structure_type:
5573 break;
5574 default:
5575 goto again;
5576 }
5577 break;
5578 case LABEL_DOMAIN:
5579 switch (indexval.dwarf_tag)
5580 {
5581 case 0:
5582 case DW_TAG_variable:
5583 break;
5584 default:
5585 goto again;
5586 }
5587 break;
59c35742
AB
5588 case MODULE_DOMAIN:
5589 switch (indexval.dwarf_tag)
5590 {
5591 case DW_TAG_module:
5592 break;
5593 default:
5594 goto again;
5595 }
5596 break;
927aa2e7
JK
5597 default:
5598 break;
5599 }
5600
5601 /* Match dw2_expand_symtabs_matching, symbol_kind and
5602 debug_names::psymbol_tag. */
5603 switch (m_search)
4b514bc8 5604 {
927aa2e7
JK
5605 case VARIABLES_DOMAIN:
5606 switch (indexval.dwarf_tag)
4b514bc8 5607 {
927aa2e7
JK
5608 case DW_TAG_variable:
5609 break;
5610 default:
5611 goto again;
4b514bc8 5612 }
927aa2e7
JK
5613 break;
5614 case FUNCTIONS_DOMAIN:
5615 switch (indexval.dwarf_tag)
4b514bc8 5616 {
927aa2e7
JK
5617 case DW_TAG_subprogram:
5618 break;
5619 default:
5620 goto again;
4b514bc8 5621 }
927aa2e7
JK
5622 break;
5623 case TYPES_DOMAIN:
5624 switch (indexval.dwarf_tag)
5625 {
5626 case DW_TAG_typedef:
5627 case DW_TAG_structure_type:
5628 break;
5629 default:
5630 goto again;
5631 }
5632 break;
59c35742
AB
5633 case MODULES_DOMAIN:
5634 switch (indexval.dwarf_tag)
5635 {
5636 case DW_TAG_module:
5637 break;
5638 default:
5639 goto again;
5640 }
927aa2e7
JK
5641 default:
5642 break;
4b514bc8 5643 }
927aa2e7
JK
5644
5645 return per_cu;
4b514bc8 5646}
61920122 5647
927aa2e7 5648static struct compunit_symtab *
c7f839cb 5649dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5650 const char *name, domain_enum domain)
4b514bc8 5651{
ed2dc618
SM
5652 struct dwarf2_per_objfile *dwarf2_per_objfile
5653 = get_dwarf2_per_objfile (objfile);
61920122 5654
5989a64e 5655 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5656 if (!mapp)
61920122 5657 {
927aa2e7
JK
5658 /* index is NULL if OBJF_READNOW. */
5659 return NULL;
5660 }
5661 const auto &map = *mapp;
9291a0cd 5662
2b79f376 5663 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5664
927aa2e7
JK
5665 struct compunit_symtab *stab_best = NULL;
5666 struct dwarf2_per_cu_data *per_cu;
5667 while ((per_cu = iter.next ()) != NULL)
5668 {
5669 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5670 compunit_symtab *stab
5671 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5672 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5673 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5674
927aa2e7
JK
5675 sym = block_find_symbol (block, name, domain,
5676 block_find_non_opaque_type_preferred,
5677 &with_opaque);
9703b513 5678
927aa2e7
JK
5679 /* Some caution must be observed with overloaded functions and
5680 methods, since the index will not contain any overload
5681 information (but NAME might contain it). */
a3ec0bb1 5682
927aa2e7 5683 if (sym != NULL
987012b8 5684 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5685 return stab;
5686 if (with_opaque != NULL
987012b8 5687 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5688 stab_best = stab;
9703b513 5689
927aa2e7 5690 /* Keep looking through other CUs. */
9703b513
TT
5691 }
5692
927aa2e7 5693 return stab_best;
9703b513
TT
5694}
5695
927aa2e7
JK
5696/* This dumps minimal information about .debug_names. It is called
5697 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5698 uses this to verify that .debug_names has been loaded. */
9291a0cd 5699
927aa2e7
JK
5700static void
5701dw2_debug_names_dump (struct objfile *objfile)
5702{
ed2dc618
SM
5703 struct dwarf2_per_objfile *dwarf2_per_objfile
5704 = get_dwarf2_per_objfile (objfile);
5705
5989a64e 5706 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5707 printf_filtered (".debug_names:");
5989a64e 5708 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5709 printf_filtered (" exists\n");
5710 else
5711 printf_filtered (" faked for \"readnow\"\n");
5712 printf_filtered ("\n");
9291a0cd
TT
5713}
5714
9291a0cd 5715static void
927aa2e7
JK
5716dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5717 const char *func_name)
9291a0cd 5718{
ed2dc618
SM
5719 struct dwarf2_per_objfile *dwarf2_per_objfile
5720 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5721
5989a64e
SM
5722 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5723 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5724 {
5989a64e 5725 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5726
2b79f376 5727 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5728
927aa2e7
JK
5729 struct dwarf2_per_cu_data *per_cu;
5730 while ((per_cu = iter.next ()) != NULL)
97a1449a 5731 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5732 }
5733}
24c79950 5734
3b00ef10
TT
5735static void
5736dw2_debug_names_map_matching_symbols
5737 (struct objfile *objfile,
5738 const lookup_name_info &name, domain_enum domain,
5739 int global,
5740 gdb::function_view<symbol_found_callback_ftype> callback,
5741 symbol_compare_ftype *ordered_compare)
5742{
5743 struct dwarf2_per_objfile *dwarf2_per_objfile
5744 = get_dwarf2_per_objfile (objfile);
5745
5746 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5747 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5748 return;
5749
5989a64e 5750 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5751 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5752
5753 const char *match_name = name.ada ().lookup_name ().c_str ();
5754 auto matcher = [&] (const char *symname)
5755 {
5756 if (ordered_compare == nullptr)
5757 return true;
5758 return ordered_compare (symname, match_name) == 0;
5759 };
5760
5761 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5762 [&] (offset_type namei)
5763 {
5764 /* The name was matched, now expand corresponding CUs that were
5765 marked. */
5766 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5767
5768 struct dwarf2_per_cu_data *per_cu;
5769 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5770 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5771 nullptr);
3b00ef10
TT
5772 return true;
5773 });
5774
5775 /* It's a shame we couldn't do this inside the
5776 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5777 that have already been expanded. Instead, this loop matches what
5778 the psymtab code does. */
5989a64e 5779 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5780 {
af758d11
SM
5781 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5782 if (symtab != nullptr)
3b00ef10
TT
5783 {
5784 const struct block *block
af758d11 5785 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5786 if (!iterate_over_symbols_terminated (block, name,
5787 domain, callback))
5788 break;
5789 }
5790 }
5791}
5792
927aa2e7
JK
5793static void
5794dw2_debug_names_expand_symtabs_matching
5795 (struct objfile *objfile,
5796 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5797 const lookup_name_info *lookup_name,
927aa2e7
JK
5798 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5799 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5800 enum search_domain kind)
5801{
ed2dc618
SM
5802 struct dwarf2_per_objfile *dwarf2_per_objfile
5803 = get_dwarf2_per_objfile (objfile);
9291a0cd 5804
927aa2e7 5805 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5806 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5807 return;
9291a0cd 5808
ed2dc618 5809 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5810
c1a66c06
TV
5811 if (symbol_matcher == NULL && lookup_name == NULL)
5812 {
5989a64e 5813 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5814 {
5815 QUIT;
5816
97a1449a
SM
5817 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5818 file_matcher, expansion_notify);
c1a66c06
TV
5819 }
5820 return;
5821 }
5822
5989a64e 5823 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5824
c1a66c06 5825 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5826 symbol_matcher,
5827 kind, [&] (offset_type namei)
927aa2e7 5828 {
927aa2e7
JK
5829 /* The name was matched, now expand corresponding CUs that were
5830 marked. */
5831 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5832
927aa2e7
JK
5833 struct dwarf2_per_cu_data *per_cu;
5834 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5835 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5836 file_matcher, expansion_notify);
3b00ef10 5837 return true;
44ed8f3e 5838 });
9291a0cd
TT
5839}
5840
927aa2e7 5841const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5842{
5843 dw2_has_symbols,
5844 dw2_find_last_source_symtab,
5845 dw2_forget_cached_source_info,
f8eba3c6 5846 dw2_map_symtabs_matching_filename,
927aa2e7 5847 dw2_debug_names_lookup_symbol,
d3214198 5848 NULL,
9291a0cd 5849 dw2_print_stats,
927aa2e7 5850 dw2_debug_names_dump,
927aa2e7 5851 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5852 dw2_expand_all_symtabs,
652a8996 5853 dw2_expand_symtabs_with_fullname,
3b00ef10 5854 dw2_debug_names_map_matching_symbols,
927aa2e7 5855 dw2_debug_names_expand_symtabs_matching,
43f3e411 5856 dw2_find_pc_sect_compunit_symtab,
71a3c369 5857 NULL,
9291a0cd
TT
5858 dw2_map_symbol_filenames
5859};
5860
4485a1c1 5861/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5862 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5863
5864template <typename T>
5865static gdb::array_view<const gdb_byte>
5866get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5867{
5868 dwarf2_section_info *section = &section_owner->gdb_index;
5869
96b79293 5870 if (section->empty ())
4485a1c1
SM
5871 return {};
5872
5873 /* Older elfutils strip versions could keep the section in the main
5874 executable while splitting it for the separate debug info file. */
96b79293 5875 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5876 return {};
5877
96b79293 5878 section->read (obj);
4485a1c1 5879
8bebfcda
PA
5880 /* dwarf2_section_info::size is a bfd_size_type, while
5881 gdb::array_view works with size_t. On 32-bit hosts, with
5882 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5883 is 32-bit. So we need an explicit narrowing conversion here.
5884 This is fine, because it's impossible to allocate or mmap an
5885 array/buffer larger than what size_t can represent. */
5886 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5887}
5888
87d6a7aa
SM
5889/* Lookup the index cache for the contents of the index associated to
5890 DWARF2_OBJ. */
5891
5892static gdb::array_view<const gdb_byte>
5989a64e 5893get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5894{
5895 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5896 if (build_id == nullptr)
5897 return {};
5898
5899 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5900 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5901}
5902
5903/* Same as the above, but for DWZ. */
5904
5905static gdb::array_view<const gdb_byte>
5906get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5907{
5908 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5909 if (build_id == nullptr)
5910 return {};
5911
5912 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5913}
5914
3c0aa29a 5915/* See symfile.h. */
9291a0cd 5916
3c0aa29a
PA
5917bool
5918dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5919{
ed2dc618
SM
5920 struct dwarf2_per_objfile *dwarf2_per_objfile
5921 = get_dwarf2_per_objfile (objfile);
5922
9291a0cd
TT
5923 /* If we're about to read full symbols, don't bother with the
5924 indices. In this case we also don't care if some other debug
5925 format is making psymtabs, because they are all about to be
5926 expanded anyway. */
5927 if ((objfile->flags & OBJF_READNOW))
5928 {
5989a64e 5929 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5930 create_all_comp_units (dwarf2_per_objfile);
5931 create_all_type_units (dwarf2_per_objfile);
5989a64e 5932 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5933 = create_quick_file_names_table
5989a64e 5934 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5935 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5936
5989a64e
SM
5937 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5938 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5939 {
5989a64e 5940 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5941
5989a64e 5942 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5943 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5944 }
5945
5946 /* Return 1 so that gdb sees the "quick" functions. However,
5947 these functions will be no-ops because we will have expanded
5948 all symtabs. */
3c0aa29a
PA
5949 *index_kind = dw_index_kind::GDB_INDEX;
5950 return true;
9291a0cd
TT
5951 }
5952
ed2dc618 5953 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5954 {
5955 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5956 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5957 return true;
5958 }
927aa2e7 5959
4485a1c1 5960 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5961 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5962 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5963 {
5964 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5965 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5966 return true;
5967 }
9291a0cd 5968
87d6a7aa
SM
5969 /* ... otherwise, try to find the index in the index cache. */
5970 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5971 get_gdb_index_contents_from_cache,
5972 get_gdb_index_contents_from_cache_dwz))
5973 {
5974 global_index_cache.hit ();
5975 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5976 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5977 return true;
5978 }
5979
5980 global_index_cache.miss ();
3c0aa29a 5981 return false;
9291a0cd
TT
5982}
5983
5984\f
5985
dce234bc
PP
5986/* Build a partial symbol table. */
5987
5988void
f29dff0a 5989dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5990{
ed2dc618
SM
5991 struct dwarf2_per_objfile *dwarf2_per_objfile
5992 = get_dwarf2_per_objfile (objfile);
c9bf0622 5993
6eee24ce 5994 init_psymbol_list (objfile, 1024);
c906108c 5995
a70b8144 5996 try
c9bf0622
TT
5997 {
5998 /* This isn't really ideal: all the data we allocate on the
5999 objfile's obstack is still uselessly kept around. However,
6000 freeing it seems unsafe. */
906768f9 6001 psymtab_discarder psymtabs (objfile);
ed2dc618 6002 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6003 psymtabs.keep ();
87d6a7aa 6004
af758d11
SM
6005 dwarf2_per_objfile->resize_symtabs ();
6006
87d6a7aa
SM
6007 /* (maybe) store an index in the cache. */
6008 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6009 }
230d2906 6010 catch (const gdb_exception_error &except)
492d29ea
PA
6011 {
6012 exception_print (gdb_stderr, except);
6013 }
c906108c 6014}
c906108c 6015
3b80fe9b
DE
6016/* Find the base address of the compilation unit for range lists and
6017 location lists. It will normally be specified by DW_AT_low_pc.
6018 In DWARF-3 draft 4, the base address could be overridden by
6019 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6020 compilation units with discontinuous ranges. */
6021
6022static void
6023dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6024{
6025 struct attribute *attr;
6026
2b24b6e4 6027 cu->base_address.reset ();
3b80fe9b
DE
6028
6029 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6030 if (attr != nullptr)
2b24b6e4 6031 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6032 else
6033 {
6034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6035 if (attr != nullptr)
2b24b6e4 6036 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6037 }
6038}
6039
36586728
TT
6040/* Helper function that returns the proper abbrev section for
6041 THIS_CU. */
6042
6043static struct dwarf2_section_info *
6044get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6045{
6046 struct dwarf2_section_info *abbrev;
c3699833 6047 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6048
6049 if (this_cu->is_dwz)
c3699833 6050 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6051 else
c3699833 6052 abbrev = &per_bfd->abbrev;
36586728
TT
6053
6054 return abbrev;
6055}
6056
f4dc4d17
DE
6057/* Fetch the abbreviation table offset from a comp or type unit header. */
6058
6059static sect_offset
ed2dc618
SM
6060read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6061 struct dwarf2_section_info *section,
9c541725 6062 sect_offset sect_off)
f4dc4d17 6063{
96b79293 6064 bfd *abfd = section->get_bfd_owner ();
d521ce57 6065 const gdb_byte *info_ptr;
ac298888 6066 unsigned int initial_length_size, offset_size;
43988095 6067 uint16_t version;
f4dc4d17 6068
96b79293 6069 section->read (dwarf2_per_objfile->objfile);
9c541725 6070 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6071 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6072 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6073 info_ptr += initial_length_size;
6074
6075 version = read_2_bytes (abfd, info_ptr);
6076 info_ptr += 2;
6077 if (version >= 5)
6078 {
6079 /* Skip unit type and address size. */
6080 info_ptr += 2;
6081 }
6082
24aa364d 6083 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6084}
6085
b83470bf
TT
6086/* A partial symtab that is used only for include files. */
6087struct dwarf2_include_psymtab : public partial_symtab
6088{
6089 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6090 : partial_symtab (filename, objfile)
6091 {
6092 }
6093
6094 void read_symtab (struct objfile *objfile) override
6095 {
194d088f
TV
6096 /* It's an include file, no symbols to read for it.
6097 Everything is in the includer symtab. */
6098
6099 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6100 expansion of the includer psymtab. We use the dependencies[0] field to
6101 model the includer. But if we go the regular route of calling
6102 expand_psymtab here, and having expand_psymtab call expand_dependencies
6103 to expand the includer, we'll only use expand_psymtab on the includer
6104 (making it a non-toplevel psymtab), while if we expand the includer via
6105 another path, we'll use read_symtab (making it a toplevel psymtab).
6106 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6107 psymtab, and trigger read_symtab on the includer here directly. */
6108 includer ()->read_symtab (objfile);
b83470bf
TT
6109 }
6110
6111 void expand_psymtab (struct objfile *objfile) override
6112 {
194d088f
TV
6113 /* This is not called by read_symtab, and should not be called by any
6114 expand_dependencies. */
6115 gdb_assert (false);
b83470bf
TT
6116 }
6117
5717c425 6118 bool readin_p (struct objfile *objfile) const override
b83470bf 6119 {
5717c425 6120 return includer ()->readin_p (objfile);
b83470bf
TT
6121 }
6122
5717c425 6123 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6124 {
6125 return nullptr;
6126 }
6127
6128private:
194d088f
TV
6129 partial_symtab *includer () const
6130 {
6131 /* An include psymtab has exactly one dependency: the psymtab that
6132 includes it. */
6133 gdb_assert (this->number_of_dependencies == 1);
6134 return this->dependencies[0];
6135 }
b83470bf
TT
6136};
6137
aaa75496
JB
6138/* Allocate a new partial symtab for file named NAME and mark this new
6139 partial symtab as being an include of PST. */
6140
6141static void
891813be 6142dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6143 struct objfile *objfile)
6144{
b83470bf 6145 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6146
fbd9ab74 6147 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6148 subpst->dirname = pst->dirname;
fbd9ab74 6149
a9342b62 6150 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6151 subpst->dependencies[0] = pst;
6152 subpst->number_of_dependencies = 1;
aaa75496
JB
6153}
6154
6155/* Read the Line Number Program data and extract the list of files
6156 included by the source file represented by PST. Build an include
d85a05f0 6157 partial symtab for each of these included files. */
aaa75496
JB
6158
6159static void
6160dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6161 struct die_info *die,
891813be 6162 dwarf2_psymtab *pst)
aaa75496 6163{
fff8551c 6164 line_header_up lh;
d85a05f0 6165 struct attribute *attr;
aaa75496 6166
d85a05f0 6167 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6168 if (attr != nullptr)
9c541725 6169 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6170 if (lh == NULL)
6171 return; /* No linetable, so no includes. */
6172
79748972
TT
6173 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6174 that we pass in the raw text_low here; that is ok because we're
6175 only decoding the line table to make include partial symtabs, and
6176 so the addresses aren't really used. */
4ae976d1 6177 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6178 pst->raw_text_low (), 1);
aaa75496
JB
6179}
6180
348e048f 6181static hashval_t
52dc124a 6182hash_signatured_type (const void *item)
348e048f 6183{
9a3c8263
SM
6184 const struct signatured_type *sig_type
6185 = (const struct signatured_type *) item;
9a619af0 6186
348e048f 6187 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6188 return sig_type->signature;
348e048f
DE
6189}
6190
6191static int
52dc124a 6192eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6193{
9a3c8263
SM
6194 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6195 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6196
348e048f
DE
6197 return lhs->signature == rhs->signature;
6198}
6199
1fd400ff
TT
6200/* Allocate a hash table for signatured types. */
6201
b0b6a987 6202static htab_up
298e9637 6203allocate_signatured_type_table ()
1fd400ff 6204{
b0b6a987
TT
6205 return htab_up (htab_create_alloc (41,
6206 hash_signatured_type,
6207 eq_signatured_type,
6208 NULL, xcalloc, xfree));
1fd400ff
TT
6209}
6210
d467dd73 6211/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6212
6213static int
d467dd73 6214add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6215{
9a3c8263 6216 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6217 std::vector<signatured_type *> *all_type_units
6218 = (std::vector<signatured_type *> *) datum;
1fd400ff 6219
b2bdb8cf 6220 all_type_units->push_back (sigt);
1fd400ff
TT
6221
6222 return 1;
6223}
6224
78d4d2c5 6225/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6226 and fill them into TYPES_HTAB. It will process only type units,
6227 therefore DW_UT_type. */
c88ee1f0 6228
78d4d2c5 6229static void
ed2dc618
SM
6230create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6231 struct dwo_file *dwo_file,
b0b6a987 6232 dwarf2_section_info *section, htab_up &types_htab,
43988095 6233 rcuh_kind section_kind)
348e048f 6234{
3019eac3 6235 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6236 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6237 bfd *abfd;
6238 const gdb_byte *info_ptr, *end_ptr;
348e048f 6239
4bdcc0c1
DE
6240 abbrev_section = (dwo_file != NULL
6241 ? &dwo_file->sections.abbrev
5989a64e 6242 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6243
b4f54984 6244 if (dwarf_read_debug)
43988095 6245 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6246 section->get_name (),
6247 abbrev_section->get_file_name ());
09406207 6248
96b79293 6249 section->read (objfile);
78d4d2c5 6250 info_ptr = section->buffer;
348e048f 6251
78d4d2c5
JK
6252 if (info_ptr == NULL)
6253 return;
348e048f 6254
78d4d2c5
JK
6255 /* We can't set abfd until now because the section may be empty or
6256 not present, in which case the bfd is unknown. */
96b79293 6257 abfd = section->get_bfd_owner ();
348e048f 6258
c0ab21c2
TT
6259 /* We don't use cutu_reader here because we don't need to read
6260 any dies: the signature is in the header. */
3019eac3 6261
78d4d2c5
JK
6262 end_ptr = info_ptr + section->size;
6263 while (info_ptr < end_ptr)
6264 {
78d4d2c5
JK
6265 struct signatured_type *sig_type;
6266 struct dwo_unit *dwo_tu;
6267 void **slot;
6268 const gdb_byte *ptr = info_ptr;
6269 struct comp_unit_head header;
6270 unsigned int length;
8b70b953 6271
9c541725 6272 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6273
a49dd8dd
JK
6274 /* Initialize it due to a false compiler warning. */
6275 header.signature = -1;
9c541725 6276 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6277
78d4d2c5
JK
6278 /* We need to read the type's signature in order to build the hash
6279 table, but we don't need anything else just yet. */
348e048f 6280
ed2dc618 6281 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6282 abbrev_section, ptr, section_kind);
348e048f 6283
4057dfde 6284 length = header.get_length ();
6caca83c 6285
78d4d2c5
JK
6286 /* Skip dummy type units. */
6287 if (ptr >= info_ptr + length
43988095
JK
6288 || peek_abbrev_code (abfd, ptr) == 0
6289 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6290 {
6291 info_ptr += length;
6292 continue;
6293 }
dee91e82 6294
78d4d2c5
JK
6295 if (types_htab == NULL)
6296 {
6297 if (dwo_file)
298e9637 6298 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6299 else
298e9637 6300 types_htab = allocate_signatured_type_table ();
78d4d2c5 6301 }
8b70b953 6302
78d4d2c5
JK
6303 if (dwo_file)
6304 {
6305 sig_type = NULL;
5989a64e 6306 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6307 struct dwo_unit);
6308 dwo_tu->dwo_file = dwo_file;
43988095 6309 dwo_tu->signature = header.signature;
9c541725 6310 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6311 dwo_tu->section = section;
9c541725 6312 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6313 dwo_tu->length = length;
6314 }
6315 else
6316 {
6317 /* N.B.: type_offset is not usable if this type uses a DWO file.
6318 The real type_offset is in the DWO file. */
6319 dwo_tu = NULL;
5989a64e 6320 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6321 sig_type->signature = header.signature;
9c541725 6322 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6323 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
9c541725 6326 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6327 sig_type->per_cu.length = length;
6328 }
6329
b0b6a987 6330 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
9c541725 6336 sect_offset dup_sect_off;
0349ea22 6337
3019eac3
DE
6338 if (dwo_file)
6339 {
78d4d2c5
JK
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
9c541725 6343 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6344 }
6345 else
6346 {
78d4d2c5
JK
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
9c541725 6350 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6351 }
8b70b953 6352
b98664d3 6353 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6356 hex_string (header.signature));
78d4d2c5
JK
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6359
78d4d2c5 6360 if (dwarf_read_debug > 1)
9d8780f0
SM
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
43988095 6363 hex_string (header.signature));
3019eac3 6364
78d4d2c5
JK
6365 info_ptr += length;
6366 }
6367}
3019eac3 6368
78d4d2c5
JK
6369/* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
b3c8eb43 6373
78d4d2c5 6374 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6375
78d4d2c5 6376 Note: This function processes DWO files only, not DWP files. */
348e048f 6377
78d4d2c5 6378static void
ed2dc618
SM
6379create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
fd5866f6 6381 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6382 htab_up &types_htab)
78d4d2c5 6383{
fd5866f6
SM
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6386 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6387}
6388
6389/* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394static int
ed2dc618 6395create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6396{
b0b6a987 6397 htab_up types_htab;
3019eac3 6398
ed2dc618 6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6401 rcuh_kind::COMPILE);
ed2dc618 6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6403 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6404 if (types_htab == NULL)
6405 {
5989a64e 6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6407 return 0;
6408 }
6409
5989a64e 6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6411
5989a64e
SM
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6415
5989a64e 6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6417 add_signatured_type_cu_to_table,
5989a64e 6418 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6419
348e048f
DE
6420 return 1;
6421}
6422
5989a64e 6423/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427static struct signatured_type *
ed2dc618
SM
6428add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6aa5f3a6 6430{
5989a64e
SM
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6434
5989a64e 6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6436
af758d11
SM
6437 dwarf2_per_objfile->resize_symtabs ();
6438
5989a64e 6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
5989a64e 6442 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6443 {
6444 sig_type->per_cu.v.quick =
5989a64e 6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
5989a64e 6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458}
6459
a2ce51a0
DE
6460/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463static void
ed2dc618 6464fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467{
1859c670
SM
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
7ee85ab1 6470 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6473 if (per_bfd->using_index)
6aa5f3a6
DE
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6482 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6489 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 6490 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6491 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6492 sig_entry->dwo_unit = dwo_entry;
6493}
6494
6495/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6496 If we haven't read the TU yet, create the signatured_type data structure
6497 for a TU to be read in directly from a DWO file, bypassing the stub.
6498 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6499 using .gdb_index, then when reading a CU we want to stay in the DWO file
6500 containing that CU. Otherwise we could end up reading several other DWO
6501 files (due to comdat folding) to process the transitive closure of all the
6502 mentioned TUs, and that can be slow. The current DWO file will have every
6503 type signature that it needs.
a2ce51a0
DE
6504 We only do this for .gdb_index because in the psymtab case we already have
6505 to read all the DWOs to build the type unit groups. */
6506
6507static struct signatured_type *
6508lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6509{
5e22e966 6510 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6511 struct dwo_file *dwo_file;
6512 struct dwo_unit find_dwo_entry, *dwo_entry;
6513 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6514 void **slot;
a2ce51a0 6515
5989a64e 6516 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6517
6aa5f3a6
DE
6518 /* If TU skeletons have been removed then we may not have read in any
6519 TUs yet. */
5989a64e
SM
6520 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6521 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6522
6523 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6524 Use the global signatured_types array to do our own comdat-folding
6525 of types. If this is the first time we're reading this TU, and
6526 the TU has an entry in .gdb_index, replace the recorded data from
6527 .gdb_index with this TU. */
a2ce51a0 6528
a2ce51a0 6529 find_sig_entry.signature = sig;
5989a64e 6530 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6531 &find_sig_entry, INSERT);
9a3c8263 6532 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6533
6534 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6535 read. Don't reassign the global entry to point to this DWO if that's
6536 the case. Also note that if the TU is already being read, it may not
6537 have come from a DWO, the program may be a mix of Fission-compiled
6538 code and non-Fission-compiled code. */
6539
6540 /* Have we already tried to read this TU?
6541 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6542 needn't exist in the global table yet). */
6543 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6544 return sig_entry;
6545
6aa5f3a6
DE
6546 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6547 dwo_unit of the TU itself. */
6548 dwo_file = cu->dwo_unit->dwo_file;
6549
a2ce51a0
DE
6550 /* Ok, this is the first time we're reading this TU. */
6551 if (dwo_file->tus == NULL)
6552 return NULL;
6553 find_dwo_entry.signature = sig;
b0b6a987
TT
6554 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6555 &find_dwo_entry);
a2ce51a0
DE
6556 if (dwo_entry == NULL)
6557 return NULL;
6558
6aa5f3a6
DE
6559 /* If the global table doesn't have an entry for this TU, add one. */
6560 if (sig_entry == NULL)
ed2dc618 6561 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6562
ed2dc618 6563 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6564 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6565 return sig_entry;
6566}
6567
a2ce51a0
DE
6568/* Subroutine of lookup_signatured_type.
6569 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6570 then try the DWP file. If the TU stub (skeleton) has been removed then
6571 it won't be in .gdb_index. */
a2ce51a0
DE
6572
6573static struct signatured_type *
6574lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6575{
5e22e966 6576 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6577 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6578 struct dwo_unit *dwo_entry;
6579 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6580 void **slot;
a2ce51a0 6581
5989a64e 6582 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6583 gdb_assert (dwp_file != NULL);
6584
6aa5f3a6
DE
6585 /* If TU skeletons have been removed then we may not have read in any
6586 TUs yet. */
5989a64e
SM
6587 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6588 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6589
6aa5f3a6 6590 find_sig_entry.signature = sig;
5989a64e 6591 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6592 &find_sig_entry, INSERT);
9a3c8263 6593 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6594
6595 /* Have we already tried to read this TU?
6596 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6597 needn't exist in the global table yet). */
6598 if (sig_entry != NULL)
6599 return sig_entry;
6600
a2ce51a0
DE
6601 if (dwp_file->tus == NULL)
6602 return NULL;
ed2dc618 6603 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6604 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6605 if (dwo_entry == NULL)
6606 return NULL;
6607
ed2dc618
SM
6608 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6609 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6610
a2ce51a0
DE
6611 return sig_entry;
6612}
6613
380bca97 6614/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6615 Returns NULL if signature SIG is not present in the table.
6616 It is up to the caller to complain about this. */
348e048f
DE
6617
6618static struct signatured_type *
a2ce51a0 6619lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6620{
5e22e966 6621 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6622
a2ce51a0 6623 if (cu->dwo_unit
5989a64e 6624 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6625 {
6626 /* We're in a DWO/DWP file, and we're using .gdb_index.
6627 These cases require special processing. */
ed2dc618 6628 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6629 return lookup_dwo_signatured_type (cu, sig);
6630 else
6631 return lookup_dwp_signatured_type (cu, sig);
6632 }
6633 else
6634 {
6635 struct signatured_type find_entry, *entry;
348e048f 6636
5989a64e 6637 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6638 return NULL;
6639 find_entry.signature = sig;
9a3c8263 6640 entry = ((struct signatured_type *)
5989a64e 6641 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6642 &find_entry));
a2ce51a0
DE
6643 return entry;
6644 }
348e048f 6645}
18a8505e 6646
42e7ad6c 6647/* Low level DIE reading support. */
348e048f 6648
d85a05f0
DJ
6649/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6650
6651static void
6652init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6653 struct dwarf2_cu *cu,
3019eac3 6654 struct dwarf2_section_info *section,
685af9cd
TT
6655 struct dwo_file *dwo_file,
6656 struct abbrev_table *abbrev_table)
d85a05f0 6657{
fceca515 6658 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6659 reader->abfd = section->get_bfd_owner ();
d85a05f0 6660 reader->cu = cu;
3019eac3 6661 reader->dwo_file = dwo_file;
dee91e82
DE
6662 reader->die_section = section;
6663 reader->buffer = section->buffer;
f664829e 6664 reader->buffer_end = section->buffer + section->size;
685af9cd 6665 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6666}
6667
c0ab21c2 6668/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6669 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6670 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6671 already.
6672
6673 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6674 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6675 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6676 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6677 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6678 STUB_COMP_DIR may be non-NULL.
3e225074 6679 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6680 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6681 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6682 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6683 kept around for at least as long as *RESULT_READER.
6684
b0c7bfa9
DE
6685 The result is non-zero if a valid (non-dummy) DIE was found. */
6686
6687static int
6688read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6689 struct dwo_unit *dwo_unit,
b0c7bfa9 6690 struct die_info *stub_comp_unit_die,
a2ce51a0 6691 const char *stub_comp_dir,
b0c7bfa9 6692 struct die_reader_specs *result_reader,
d521ce57 6693 const gdb_byte **result_info_ptr,
b0c7bfa9 6694 struct die_info **result_comp_unit_die,
685af9cd 6695 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6696{
ed2dc618 6697 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
6699 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6700 bfd *abfd;
d521ce57 6701 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6702 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6703 int i,num_extra_attrs;
6704 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6705 struct die_info *comp_unit_die;
6706
b0aeadb3
DE
6707 /* At most one of these may be provided. */
6708 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6709
b0c7bfa9
DE
6710 /* These attributes aren't processed until later:
6711 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6712 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6713 referenced later. However, these attributes are found in the stub
6714 which we won't have later. In order to not impose this complication
6715 on the rest of the code, we read them here and copy them to the
6716 DWO CU/TU die. */
b0c7bfa9
DE
6717
6718 stmt_list = NULL;
6719 low_pc = NULL;
6720 high_pc = NULL;
6721 ranges = NULL;
6722 comp_dir = NULL;
6723
6724 if (stub_comp_unit_die != NULL)
6725 {
6726 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6727 DWO file. */
6728 if (! this_cu->is_debug_types)
6729 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6730 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6731 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6732 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6733 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6734
a39fdb41 6735 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6736
18a8505e
AT
6737 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6738 here (if needed). We need the value before we can process
6739 DW_AT_ranges. */
a39fdb41 6740 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6741 }
a2ce51a0
DE
6742 else if (stub_comp_dir != NULL)
6743 {
6744 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6745 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6746 comp_dir->name = DW_AT_comp_dir;
6747 comp_dir->form = DW_FORM_string;
6748 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6749 DW_STRING (comp_dir) = stub_comp_dir;
6750 }
b0c7bfa9
DE
6751
6752 /* Set up for reading the DWO CU/TU. */
6753 cu->dwo_unit = dwo_unit;
685af9cd 6754 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6755 section->read (objfile);
6756 abfd = section->get_bfd_owner ();
9c541725
PA
6757 begin_info_ptr = info_ptr = (section->buffer
6758 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6759 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6760
6761 if (this_cu->is_debug_types)
6762 {
b0c7bfa9
DE
6763 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6764
ed2dc618
SM
6765 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6766 &cu->header, section,
b0c7bfa9 6767 dwo_abbrev_section,
43988095 6768 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6769 /* This is not an assert because it can be caused by bad debug info. */
43988095 6770 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6771 {
6772 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6773 " TU at offset %s [in module %s]"),
a2ce51a0 6774 hex_string (sig_type->signature),
43988095 6775 hex_string (cu->header.signature),
9d8780f0 6776 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6777 bfd_get_filename (abfd));
6778 }
9c541725 6779 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6780 /* For DWOs coming from DWP files, we don't know the CU length
6781 nor the type's offset in the TU until now. */
4057dfde 6782 dwo_unit->length = cu->header.get_length ();
9c541725 6783 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6784
6785 /* Establish the type offset that can be used to lookup the type.
6786 For DWO files, we don't know it until now. */
9c541725
PA
6787 sig_type->type_offset_in_section
6788 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6789 }
6790 else
6791 {
ed2dc618
SM
6792 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6793 &cu->header, section,
b0c7bfa9 6794 dwo_abbrev_section,
43988095 6795 info_ptr, rcuh_kind::COMPILE);
9c541725 6796 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6797 /* For DWOs coming from DWP files, we don't know the CU length
6798 until now. */
4057dfde 6799 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6800 }
6801
685af9cd 6802 *result_dwo_abbrev_table
86de1d91
TT
6803 = abbrev_table::read (objfile, dwo_abbrev_section,
6804 cu->header.abbrev_sect_off);
685af9cd
TT
6805 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6806 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6807
6808 /* Read in the die, but leave space to copy over the attributes
6809 from the stub. This has the benefit of simplifying the rest of
6810 the code - all the work to maintain the illusion of a single
6811 DW_TAG_{compile,type}_unit DIE is done here. */
6812 num_extra_attrs = ((stmt_list != NULL)
6813 + (low_pc != NULL)
6814 + (high_pc != NULL)
6815 + (ranges != NULL)
6816 + (comp_dir != NULL));
6817 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6818 num_extra_attrs);
b0c7bfa9
DE
6819
6820 /* Copy over the attributes from the stub to the DIE we just read in. */
6821 comp_unit_die = *result_comp_unit_die;
6822 i = comp_unit_die->num_attrs;
6823 if (stmt_list != NULL)
6824 comp_unit_die->attrs[i++] = *stmt_list;
6825 if (low_pc != NULL)
6826 comp_unit_die->attrs[i++] = *low_pc;
6827 if (high_pc != NULL)
6828 comp_unit_die->attrs[i++] = *high_pc;
6829 if (ranges != NULL)
6830 comp_unit_die->attrs[i++] = *ranges;
6831 if (comp_dir != NULL)
6832 comp_unit_die->attrs[i++] = *comp_dir;
6833 comp_unit_die->num_attrs += num_extra_attrs;
6834
b4f54984 6835 if (dwarf_die_debug)
bf6af496
DE
6836 {
6837 fprintf_unfiltered (gdb_stdlog,
6838 "Read die from %s@0x%x of %s:\n",
96b79293 6839 section->get_name (),
bf6af496
DE
6840 (unsigned) (begin_info_ptr - section->buffer),
6841 bfd_get_filename (abfd));
b4f54984 6842 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6843 }
6844
b0c7bfa9
DE
6845 /* Skip dummy compilation units. */
6846 if (info_ptr >= begin_info_ptr + dwo_unit->length
6847 || peek_abbrev_code (abfd, info_ptr) == 0)
6848 return 0;
6849
6850 *result_info_ptr = info_ptr;
6851 return 1;
6852}
6853
a084a2a6
AT
6854/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6855 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6856 signature is part of the header. */
6857static gdb::optional<ULONGEST>
6858lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6859{
6860 if (cu->header.version >= 5)
6861 return cu->header.signature;
6862 struct attribute *attr;
6863 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6864 if (attr == nullptr)
6865 return gdb::optional<ULONGEST> ();
6866 return DW_UNSND (attr);
6867}
6868
c0ab21c2 6869/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6870 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6871 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6872
6873static struct dwo_unit *
6874lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6875 struct die_info *comp_unit_die,
6876 const char *dwo_name)
b0c7bfa9
DE
6877{
6878 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6879 struct dwo_unit *dwo_unit;
c0ab21c2 6880 const char *comp_dir;
b0c7bfa9 6881
a2ce51a0
DE
6882 gdb_assert (cu != NULL);
6883
b0c7bfa9 6884 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6885 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6886 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6887
6888 if (this_cu->is_debug_types)
6889 {
6890 struct signatured_type *sig_type;
6891
6892 /* Since this_cu is the first member of struct signatured_type,
6893 we can go from a pointer to one to a pointer to the other. */
6894 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6895 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6896 }
6897 else
6898 {
a084a2a6
AT
6899 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6900 if (!signature.has_value ())
b0c7bfa9
DE
6901 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6902 " [in module %s]"),
313bad1b 6903 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
b0c7bfa9 6904 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6905 *signature);
b0c7bfa9
DE
6906 }
6907
b0c7bfa9
DE
6908 return dwo_unit;
6909}
6910
c0ab21c2 6911/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6912 See it for a description of the parameters.
fcd3b13d 6913 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6914
c0ab21c2 6915void
9e021579
SM
6916cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6917 dwarf2_per_objfile *per_objfile,
6751ebae 6918 int use_existing_cu)
a2ce51a0 6919{
a2ce51a0 6920 struct signatured_type *sig_type;
a2ce51a0
DE
6921
6922 /* Verify we can do the following downcast, and that we have the
6923 data we need. */
6924 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6925 sig_type = (struct signatured_type *) this_cu;
6926 gdb_assert (sig_type->dwo_unit != NULL);
6927
6aa5f3a6
DE
6928 if (use_existing_cu && this_cu->cu != NULL)
6929 {
6930 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6931 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6932 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6933 }
6934 else
6935 {
6936 /* If !use_existing_cu, this_cu->cu must be NULL. */
6937 gdb_assert (this_cu->cu == NULL);
9e021579 6938 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6939 }
6940
6941 /* A future optimization, if needed, would be to use an existing
6942 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6943 could share abbrev tables. */
a2ce51a0
DE
6944
6945 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6946 NULL /* stub_comp_unit_die */,
6947 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6948 this, &info_ptr,
3e225074 6949 &comp_unit_die,
c0ab21c2 6950 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6951 {
6952 /* Dummy die. */
c0ab21c2 6953 dummy_p = true;
a2ce51a0 6954 }
a2ce51a0
DE
6955}
6956
fd820528 6957/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6958 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6959
f4dc4d17
DE
6960 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6961 Otherwise the table specified in the comp unit header is read in and used.
6962 This is an optimization for when we already have the abbrev table.
6963
dee91e82 6964 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6965 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6966
ab432490
SM
6967cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6968 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6969 struct abbrev_table *abbrev_table,
6751ebae 6970 int use_existing_cu,
c0ab21c2
TT
6971 bool skip_partial)
6972 : die_reader_specs {},
6751ebae 6973 m_this_cu (this_cu)
c906108c 6974{
dee91e82 6975 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6976 struct dwarf2_section_info *section = this_cu->section;
96b79293 6977 bfd *abfd = section->get_bfd_owner ();
dee91e82 6978 struct dwarf2_cu *cu;
c0ab21c2 6979 const gdb_byte *begin_info_ptr;
dee91e82 6980 struct signatured_type *sig_type = NULL;
4bdcc0c1 6981 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6982 /* Non-zero if CU currently points to a DWO file and we need to
6983 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6984 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6985 int rereading_dwo_cu = 0;
c906108c 6986
b4f54984 6987 if (dwarf_die_debug)
9d8780f0 6988 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6989 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6990 sect_offset_str (this_cu->sect_off));
09406207 6991
a2ce51a0
DE
6992 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6993 file (instead of going through the stub), short-circuit all of this. */
6994 if (this_cu->reading_dwo_directly)
6995 {
6996 /* Narrow down the scope of possibilities to have to understand. */
6997 gdb_assert (this_cu->is_debug_types);
6998 gdb_assert (abbrev_table == NULL);
9e021579 6999 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
7000 return;
7001 }
7002
dee91e82 7003 /* This is cheap if the section is already read in. */
96b79293 7004 section->read (objfile);
dee91e82 7005
9c541725 7006 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7007
7008 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7009
7010 if (use_existing_cu && this_cu->cu != NULL)
7011 {
7012 cu = this_cu->cu;
42e7ad6c
DE
7013 /* If this CU is from a DWO file we need to start over, we need to
7014 refetch the attributes from the skeleton CU.
7015 This could be optimized by retrieving those attributes from when we
7016 were here the first time: the previous comp_unit_die was stored in
7017 comp_unit_obstack. But there's no data yet that we need this
7018 optimization. */
7019 if (cu->dwo_unit != NULL)
7020 rereading_dwo_cu = 1;
dee91e82
DE
7021 }
7022 else
7023 {
7024 /* If !use_existing_cu, this_cu->cu must be NULL. */
7025 gdb_assert (this_cu->cu == NULL);
9e021579 7026 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7027 cu = m_new_cu.get ();
42e7ad6c 7028 }
dee91e82 7029
b0c7bfa9 7030 /* Get the header. */
9c541725 7031 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7032 {
7033 /* We already have the header, there's no need to read it in again. */
9c541725 7034 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7035 }
7036 else
7037 {
3019eac3 7038 if (this_cu->is_debug_types)
dee91e82 7039 {
ed2dc618
SM
7040 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7041 &cu->header, section,
4bdcc0c1 7042 abbrev_section, info_ptr,
43988095 7043 rcuh_kind::TYPE);
dee91e82 7044
42e7ad6c
DE
7045 /* Since per_cu is the first member of struct signatured_type,
7046 we can go from a pointer to one to a pointer to the other. */
7047 sig_type = (struct signatured_type *) this_cu;
43988095 7048 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7049 gdb_assert (sig_type->type_offset_in_tu
7050 == cu->header.type_cu_offset_in_tu);
7051 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7052
42e7ad6c
DE
7053 /* LENGTH has not been set yet for type units if we're
7054 using .gdb_index. */
4057dfde 7055 this_cu->length = cu->header.get_length ();
3019eac3
DE
7056
7057 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7058 sig_type->type_offset_in_section =
7059 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7060
7061 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7062 }
7063 else
7064 {
ed2dc618
SM
7065 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7066 &cu->header, section,
4bdcc0c1 7067 abbrev_section,
43988095
JK
7068 info_ptr,
7069 rcuh_kind::COMPILE);
dee91e82 7070
9c541725 7071 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7072 if (this_cu->length == 0)
7073 this_cu->length = cu->header.get_length ();
7074 else
7075 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7076 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7077 }
7078 }
10b3939b 7079
6caca83c 7080 /* Skip dummy compilation units. */
dee91e82 7081 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7082 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7083 {
7084 dummy_p = true;
7085 return;
7086 }
6caca83c 7087
433df2d4
DE
7088 /* If we don't have them yet, read the abbrevs for this compilation unit.
7089 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7090 done. */
f4dc4d17 7091 if (abbrev_table != NULL)
685af9cd
TT
7092 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7093 else
f4dc4d17 7094 {
c0ab21c2 7095 m_abbrev_table_holder
86de1d91
TT
7096 = abbrev_table::read (objfile, abbrev_section,
7097 cu->header.abbrev_sect_off);
c0ab21c2 7098 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7099 }
af703f96 7100
dee91e82 7101 /* Read the top level CU/TU die. */
c0ab21c2 7102 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7103 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7104
58f0c718 7105 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7106 {
7107 dummy_p = true;
7108 return;
7109 }
58f0c718 7110
b0c7bfa9 7111 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7112 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7113 table from the DWO file and pass the ownership over to us. It will be
7114 referenced from READER, so we must make sure to free it after we're done
7115 with READER.
7116
b0c7bfa9
DE
7117 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7118 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7119 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7120 if (dwo_name != nullptr)
3019eac3 7121 {
3019eac3 7122 struct dwo_unit *dwo_unit;
b0c7bfa9 7123 struct die_info *dwo_comp_unit_die;
3019eac3 7124
3e225074 7125 if (comp_unit_die->has_children)
6a506a2d 7126 {
b98664d3 7127 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7128 " has children (offset %s) [in module %s]"),
7129 sect_offset_str (this_cu->sect_off),
7130 bfd_get_filename (abfd));
6a506a2d 7131 }
c0ab21c2 7132 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7133 if (dwo_unit != NULL)
3019eac3 7134 {
6a506a2d 7135 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7136 comp_unit_die, NULL,
c0ab21c2 7137 this, &info_ptr,
3e225074 7138 &dwo_comp_unit_die,
c0ab21c2 7139 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7140 {
7141 /* Dummy die. */
c0ab21c2 7142 dummy_p = true;
6a506a2d
DE
7143 return;
7144 }
7145 comp_unit_die = dwo_comp_unit_die;
7146 }
7147 else
7148 {
7149 /* Yikes, we couldn't find the rest of the DIE, we only have
7150 the stub. A complaint has already been logged. There's
7151 not much more we can do except pass on the stub DIE to
7152 die_reader_func. We don't want to throw an error on bad
7153 debug info. */
3019eac3
DE
7154 }
7155 }
c0ab21c2 7156}
3019eac3 7157
6751ebae
TT
7158void
7159cutu_reader::keep ()
c0ab21c2 7160{
b0c7bfa9 7161 /* Done, clean up. */
6751ebae
TT
7162 gdb_assert (!dummy_p);
7163 if (m_new_cu != NULL)
348e048f 7164 {
c0ab21c2
TT
7165 struct dwarf2_per_objfile *dwarf2_per_objfile
7166 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7167 /* Link this CU into read_in_chain. */
5989a64e
SM
7168 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7169 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7170 /* The chain owns it now. */
c0ab21c2 7171 m_new_cu.release ();
348e048f 7172 }
dee91e82
DE
7173}
7174
18a8505e
AT
7175/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7176 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7177 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7178
7179 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7180 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7181
7182 We fill in THIS_CU->length.
7183
dee91e82 7184 THIS_CU->cu is always freed when done.
3019eac3 7185 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7186 to care whether it refers to the "main" CU or the DWO CU.
7187
7188 When parent_cu is passed, it is used to provide a default value for
7189 str_offsets_base and addr_base from the parent. */
dee91e82 7190
ab432490
SM
7191cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7192 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7193 struct dwarf2_cu *parent_cu,
7194 struct dwo_file *dwo_file)
7195 : die_reader_specs {},
7196 m_this_cu (this_cu)
dee91e82
DE
7197{
7198 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7199 struct dwarf2_section_info *section = this_cu->section;
96b79293 7200 bfd *abfd = section->get_bfd_owner ();
33e80786 7201 struct dwarf2_section_info *abbrev_section;
d521ce57 7202 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7203
b4f54984 7204 if (dwarf_die_debug)
9d8780f0 7205 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7206 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7207 sect_offset_str (this_cu->sect_off));
09406207 7208
dee91e82
DE
7209 gdb_assert (this_cu->cu == NULL);
7210
33e80786
DE
7211 abbrev_section = (dwo_file != NULL
7212 ? &dwo_file->sections.abbrev
7213 : get_abbrev_section_for_cu (this_cu));
7214
dee91e82 7215 /* This is cheap if the section is already read in. */
96b79293 7216 section->read (objfile);
dee91e82 7217
9e021579 7218 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7219
9c541725 7220 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7221 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7222 &m_new_cu->header, section,
4bdcc0c1 7223 abbrev_section, info_ptr,
43988095
JK
7224 (this_cu->is_debug_types
7225 ? rcuh_kind::TYPE
7226 : rcuh_kind::COMPILE));
dee91e82 7227
18a8505e
AT
7228 if (parent_cu != nullptr)
7229 {
c0ab21c2
TT
7230 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7231 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7232 }
4057dfde 7233 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7234
7235 /* Skip dummy compilation units. */
7236 if (info_ptr >= begin_info_ptr + this_cu->length
7237 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7238 {
7239 dummy_p = true;
7240 return;
7241 }
72bf9492 7242
c0ab21c2 7243 m_abbrev_table_holder
86de1d91
TT
7244 = abbrev_table::read (objfile, abbrev_section,
7245 m_new_cu->header.abbrev_sect_off);
dee91e82 7246
c0ab21c2
TT
7247 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7248 m_abbrev_table_holder.get ());
3e225074 7249 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7250}
7251
0018ea6f
DE
7252\f
7253/* Type Unit Groups.
dee91e82 7254
0018ea6f
DE
7255 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7256 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7257 so that all types coming from the same compilation (.o file) are grouped
7258 together. A future step could be to put the types in the same symtab as
7259 the CU the types ultimately came from. */
ff013f42 7260
f4dc4d17
DE
7261static hashval_t
7262hash_type_unit_group (const void *item)
7263{
9a3c8263
SM
7264 const struct type_unit_group *tu_group
7265 = (const struct type_unit_group *) item;
f4dc4d17 7266
094b34ac 7267 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7268}
348e048f
DE
7269
7270static int
f4dc4d17 7271eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7272{
9a3c8263
SM
7273 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7274 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7275
094b34ac 7276 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7277}
348e048f 7278
f4dc4d17
DE
7279/* Allocate a hash table for type unit groups. */
7280
eaa5fa8b 7281static htab_up
298e9637 7282allocate_type_unit_groups_table ()
f4dc4d17 7283{
eaa5fa8b
TT
7284 return htab_up (htab_create_alloc (3,
7285 hash_type_unit_group,
7286 eq_type_unit_group,
7287 NULL, xcalloc, xfree));
f4dc4d17 7288}
dee91e82 7289
f4dc4d17
DE
7290/* Type units that don't have DW_AT_stmt_list are grouped into their own
7291 partial symtabs. We combine several TUs per psymtab to not let the size
7292 of any one psymtab grow too big. */
7293#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7294#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7295
094b34ac 7296/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7297 Create the type_unit_group object used to hold one or more TUs. */
7298
7299static struct type_unit_group *
094b34ac 7300create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7301{
5e22e966 7302 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7303 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7304 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7305 struct type_unit_group *tu_group;
f4dc4d17 7306
5989a64e 7307 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7308 struct type_unit_group);
094b34ac 7309 per_cu = &tu_group->per_cu;
518817b3 7310 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 7311 per_cu->per_bfd = per_bfd;
f4dc4d17 7312
1859c670 7313 if (per_bfd->using_index)
094b34ac 7314 {
1859c670 7315 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7316 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7317 }
7318 else
7319 {
9c541725 7320 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7321 dwarf2_psymtab *pst;
528e1572 7322 std::string name;
094b34ac
DE
7323
7324 /* Give the symtab a useful name for debug purposes. */
7325 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7326 name = string_printf ("<type_units_%d>",
7327 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7328 else
528e1572 7329 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7330
528e1572 7331 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7332 pst->anonymous = true;
094b34ac 7333 }
f4dc4d17 7334
094b34ac 7335 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7336 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7337
7338 return tu_group;
7339}
7340
094b34ac
DE
7341/* Look up the type_unit_group for type unit CU, and create it if necessary.
7342 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7343
7344static struct type_unit_group *
ff39bb5e 7345get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7346{
5e22e966 7347 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7348 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7349 struct type_unit_group *tu_group;
7350 void **slot;
7351 unsigned int line_offset;
7352 struct type_unit_group type_unit_group_for_lookup;
7353
5989a64e
SM
7354 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7355 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7356
7357 /* Do we need to create a new group, or can we use an existing one? */
7358
7359 if (stmt_list)
7360 {
7361 line_offset = DW_UNSND (stmt_list);
7362 ++tu_stats->nr_symtab_sharers;
7363 }
7364 else
7365 {
7366 /* Ugh, no stmt_list. Rare, but we have to handle it.
7367 We can do various things here like create one group per TU or
7368 spread them over multiple groups to split up the expansion work.
7369 To avoid worst case scenarios (too many groups or too large groups)
7370 we, umm, group them in bunches. */
7371 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7372 | (tu_stats->nr_stmt_less_type_units
7373 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7374 ++tu_stats->nr_stmt_less_type_units;
7375 }
7376
094b34ac 7377 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7378 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7379 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7380 &type_unit_group_for_lookup, INSERT);
7381 if (*slot != NULL)
7382 {
9a3c8263 7383 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7384 gdb_assert (tu_group != NULL);
7385 }
7386 else
7387 {
9c541725 7388 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7389 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7390 *slot = tu_group;
7391 ++tu_stats->nr_symtabs;
7392 }
7393
7394 return tu_group;
7395}
0018ea6f
DE
7396\f
7397/* Partial symbol tables. */
7398
7399/* Create a psymtab named NAME and assign it to PER_CU.
7400
7401 The caller must fill in the following details:
7402 dirname, textlow, texthigh. */
7403
891813be 7404static dwarf2_psymtab *
0018ea6f
DE
7405create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7406{
e3b94546 7407 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7408 dwarf2_psymtab *pst;
0018ea6f 7409
9f4e76a4 7410 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7411
6d94535f 7412 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7413
7414 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7415 per_cu->v.psymtab = pst;
7416
7417 return pst;
7418}
7419
c0ab21c2 7420/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7421
7422static void
7423process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7424 const gdb_byte *info_ptr,
0018ea6f 7425 struct die_info *comp_unit_die,
c0ab21c2 7426 enum language pretend_language)
0018ea6f
DE
7427{
7428 struct dwarf2_cu *cu = reader->cu;
5e22e966 7429 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 7430 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7431 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7432 CORE_ADDR baseaddr;
7433 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7434 dwarf2_psymtab *pst;
3a2b436a 7435 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7436 const char *filename;
0018ea6f 7437
0018ea6f
DE
7438 gdb_assert (! per_cu->is_debug_types);
7439
c0ab21c2 7440 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7441
0018ea6f 7442 /* Allocate a new partial symbol table structure. */
2e927613
TV
7443 gdb::unique_xmalloc_ptr<char> debug_filename;
7444 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7445 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7446 if (filename == NULL)
0018ea6f 7447 filename = "";
2e927613
TV
7448 else if (strcmp (filename, artificial) == 0)
7449 {
7450 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7451 sect_offset_str (per_cu->sect_off),
7452 (char *) NULL));
2e927613
TV
7453 filename = debug_filename.get ();
7454 }
0018ea6f
DE
7455
7456 pst = create_partial_symtab (per_cu, filename);
7457
7458 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7459 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7460
b3b3bada 7461 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7462
7463 dwarf2_find_base_address (comp_unit_die, cu);
7464
7465 /* Possibly set the default values of LOWPC and HIGHPC from
7466 `DW_AT_ranges'. */
3a2b436a
JK
7467 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7468 &best_highpc, cu, pst);
7469 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7470 {
7471 CORE_ADDR low
7472 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7473 - baseaddr);
7474 CORE_ADDR high
7475 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7476 - baseaddr - 1);
7477 /* Store the contiguous range if it is not empty; it can be
7478 empty for CUs with no code. */
d320c2b5
TT
7479 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7480 low, high, pst);
79748972 7481 }
0018ea6f
DE
7482
7483 /* Check if comp unit has_children.
7484 If so, read the rest of the partial symbols from this comp unit.
7485 If not, there's no more debug_info for this comp unit. */
3e225074 7486 if (comp_unit_die->has_children)
0018ea6f
DE
7487 {
7488 struct partial_die_info *first_die;
7489 CORE_ADDR lowpc, highpc;
7490
7491 lowpc = ((CORE_ADDR) -1);
7492 highpc = ((CORE_ADDR) 0);
7493
7494 first_die = load_partial_dies (reader, info_ptr, 1);
7495
7496 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7497 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7498
7499 /* If we didn't find a lowpc, set it to highpc to avoid
7500 complaints from `maint check'. */
7501 if (lowpc == ((CORE_ADDR) -1))
7502 lowpc = highpc;
7503
7504 /* If the compilation unit didn't have an explicit address range,
7505 then use the information extracted from its child dies. */
e385593e 7506 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7507 {
7508 best_lowpc = lowpc;
7509 best_highpc = highpc;
7510 }
7511 }
4ae976d1 7512 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7513 best_lowpc + baseaddr)
7514 - baseaddr);
4ae976d1 7515 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7516 best_highpc + baseaddr)
7517 - baseaddr);
0018ea6f 7518
8763cede 7519 end_psymtab_common (objfile, pst);
0018ea6f 7520
ae640021 7521 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7522 {
7523 int i;
ae640021 7524 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7525
7526 /* Fill in 'dependencies' here; we fill in 'users' in a
7527 post-pass. */
7528 pst->number_of_dependencies = len;
a9342b62
TT
7529 pst->dependencies
7530 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7531 for (i = 0; i < len; ++i)
7532 {
7533 pst->dependencies[i]
7534 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7535 }
0018ea6f 7536
ae640021 7537 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7538 }
7539
7540 /* Get the list of files included in the current compilation unit,
7541 and build a psymtab for each of them. */
7542 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7543
b4f54984 7544 if (dwarf_read_debug)
b926417a
TT
7545 fprintf_unfiltered (gdb_stdlog,
7546 "Psymtab for %s unit @%s: %s - %s"
7547 ", %d global, %d static syms\n",
7548 per_cu->is_debug_types ? "type" : "comp",
7549 sect_offset_str (per_cu->sect_off),
7550 paddress (gdbarch, pst->text_low (objfile)),
7551 paddress (gdbarch, pst->text_high (objfile)),
7552 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7553}
7554
7555/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7556 Process compilation unit THIS_CU for a psymtab. */
7557
7558static void
ab432490
SM
7559process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7560 dwarf2_per_objfile *per_objfile,
135f5437 7561 bool want_partial_unit,
b93601f3 7562 enum language pretend_language)
0018ea6f
DE
7563{
7564 /* If this compilation unit was already read in, free the
7565 cached copy in order to read it in again. This is
7566 necessary because we skipped some symbols when we first
7567 read in the compilation unit (see load_partial_dies).
7568 This problem could be avoided, but the benefit is unclear. */
7569 if (this_cu->cu != NULL)
7570 free_one_cached_comp_unit (this_cu);
7571
ab432490 7572 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7573
58990295
TV
7574 switch (reader.comp_unit_die->tag)
7575 {
7576 case DW_TAG_compile_unit:
7577 this_cu->unit_type = DW_UT_compile;
7578 break;
7579 case DW_TAG_partial_unit:
7580 this_cu->unit_type = DW_UT_partial;
7581 break;
7582 default:
7583 abort ();
7584 }
7585
c0ab21c2 7586 if (reader.dummy_p)
f1902523 7587 {
c0ab21c2 7588 /* Nothing. */
f1902523 7589 }
c0ab21c2 7590 else if (this_cu->is_debug_types)
3e225074
TT
7591 build_type_psymtabs_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die);
135f5437
TT
7593 else if (want_partial_unit
7594 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7595 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7596 reader.comp_unit_die,
c0ab21c2 7597 pretend_language);
0018ea6f 7598
58990295
TV
7599 this_cu->lang = this_cu->cu->language;
7600
0018ea6f 7601 /* Age out any secondary CUs. */
ed2dc618 7602 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7603}
f4dc4d17
DE
7604
7605/* Reader function for build_type_psymtabs. */
7606
7607static void
7608build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7609 const gdb_byte *info_ptr,
3e225074 7610 struct die_info *type_unit_die)
f4dc4d17 7611{
5e22e966 7612 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7613 struct objfile *objfile = dwarf2_per_objfile->objfile;
7614 struct dwarf2_cu *cu = reader->cu;
7615 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7616 struct signatured_type *sig_type;
f4dc4d17
DE
7617 struct type_unit_group *tu_group;
7618 struct attribute *attr;
7619 struct partial_die_info *first_die;
7620 CORE_ADDR lowpc, highpc;
891813be 7621 dwarf2_psymtab *pst;
f4dc4d17 7622
0186c6a7
DE
7623 gdb_assert (per_cu->is_debug_types);
7624 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7625
3e225074 7626 if (! type_unit_die->has_children)
f4dc4d17
DE
7627 return;
7628
052c8bb8 7629 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7630 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7631
df07e2c7 7632 if (tu_group->tus == nullptr)
a8b3b8e9 7633 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7634 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7635
7636 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7637 pst = create_partial_symtab (per_cu, "");
6d94535f 7638 pst->anonymous = true;
f4dc4d17
DE
7639
7640 first_die = load_partial_dies (reader, info_ptr, 1);
7641
7642 lowpc = (CORE_ADDR) -1;
7643 highpc = (CORE_ADDR) 0;
7644 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7645
8763cede 7646 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7647}
7648
73051182
DE
7649/* Struct used to sort TUs by their abbreviation table offset. */
7650
7651struct tu_abbrev_offset
7652{
b2bdb8cf
SM
7653 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7654 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7655 {}
7656
7657 signatured_type *sig_type;
73051182
DE
7658 sect_offset abbrev_offset;
7659};
7660
484cf504 7661/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7662
484cf504
TT
7663static bool
7664sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7665 const struct tu_abbrev_offset &b)
73051182 7666{
484cf504 7667 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7668}
7669
7670/* Efficiently read all the type units.
7671 This does the bulk of the work for build_type_psymtabs.
7672
7673 The efficiency is because we sort TUs by the abbrev table they use and
7674 only read each abbrev table once. In one program there are 200K TUs
7675 sharing 8K abbrev tables.
7676
7677 The main purpose of this function is to support building the
5989a64e 7678 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7679 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7680 can collapse the search space by grouping them by stmt_list.
7681 The savings can be significant, in the same program from above the 200K TUs
7682 share 8K stmt_list tables.
7683
7684 FUNC is expected to call get_type_unit_group, which will create the
7685 struct type_unit_group if necessary and add it to
5989a64e 7686 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7687
7688static void
ed2dc618 7689build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7690{
5989a64e 7691 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7692 abbrev_table_up abbrev_table;
73051182 7693 sect_offset abbrev_offset;
73051182
DE
7694
7695 /* It's up to the caller to not call us multiple times. */
5989a64e 7696 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7697
5989a64e 7698 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7699 return;
7700
7701 /* TUs typically share abbrev tables, and there can be way more TUs than
7702 abbrev tables. Sort by abbrev table to reduce the number of times we
7703 read each abbrev table in.
7704 Alternatives are to punt or to maintain a cache of abbrev tables.
7705 This is simpler and efficient enough for now.
7706
7707 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7708 symtab to use). Typically TUs with the same abbrev offset have the same
7709 stmt_list value too so in practice this should work well.
7710
7711 The basic algorithm here is:
7712
7713 sort TUs by abbrev table
7714 for each TU with same abbrev table:
7715 read abbrev table if first user
7716 read TU top level DIE
7717 [IWBN if DWO skeletons had DW_AT_stmt_list]
7718 call FUNC */
7719
b4f54984 7720 if (dwarf_read_debug)
73051182
DE
7721 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7722
7723 /* Sort in a separate table to maintain the order of all_type_units
7724 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7725 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7726 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7727
5989a64e 7728 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7729 sorted_by_abbrev.emplace_back
7730 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7731 sig_type->per_cu.section,
7732 sig_type->per_cu.sect_off));
73051182 7733
484cf504
TT
7734 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7735 sort_tu_by_abbrev_offset);
73051182 7736
9c541725 7737 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7738
b2bdb8cf 7739 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7740 {
73051182
DE
7741 /* Switch to the next abbrev table if necessary. */
7742 if (abbrev_table == NULL
b2bdb8cf 7743 || tu.abbrev_offset != abbrev_offset)
73051182 7744 {
b2bdb8cf 7745 abbrev_offset = tu.abbrev_offset;
73051182 7746 abbrev_table =
86de1d91 7747 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7748 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7749 abbrev_offset);
73051182
DE
7750 ++tu_stats->nr_uniq_abbrev_tables;
7751 }
7752
ab432490
SM
7753 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7754 abbrev_table.get (), 0, false);
c0ab21c2
TT
7755 if (!reader.dummy_p)
7756 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7757 reader.comp_unit_die);
73051182 7758 }
6aa5f3a6 7759}
73051182 7760
6aa5f3a6
DE
7761/* Print collected type unit statistics. */
7762
7763static void
ed2dc618 7764print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7765{
5989a64e 7766 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7767
7768 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7769 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7770 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7771 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7772 tu_stats->nr_uniq_abbrev_tables);
7773 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7774 tu_stats->nr_symtabs);
7775 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7776 tu_stats->nr_symtab_sharers);
7777 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7778 tu_stats->nr_stmt_less_type_units);
7779 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7780 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7781}
7782
f4dc4d17
DE
7783/* Traversal function for build_type_psymtabs. */
7784
7785static int
7786build_type_psymtab_dependencies (void **slot, void *info)
7787{
ed2dc618
SM
7788 struct dwarf2_per_objfile *dwarf2_per_objfile
7789 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7790 struct objfile *objfile = dwarf2_per_objfile->objfile;
7791 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7792 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7793 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7794 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7795 int i;
7796
7797 gdb_assert (len > 0);
197400e8 7798 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7799
7800 pst->number_of_dependencies = len;
a9342b62 7801 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7802 for (i = 0; i < len; ++i)
f4dc4d17 7803 {
df07e2c7 7804 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7805 gdb_assert (iter->per_cu.is_debug_types);
7806 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7807 iter->type_unit_group = tu_group;
f4dc4d17
DE
7808 }
7809
df07e2c7
AB
7810 delete tu_group->tus;
7811 tu_group->tus = nullptr;
348e048f
DE
7812
7813 return 1;
7814}
7815
7816/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7817 Build partial symbol tables for the .debug_types comp-units. */
7818
7819static void
ed2dc618 7820build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7821{
ed2dc618 7822 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7823 return;
7824
ed2dc618 7825 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7826}
f4dc4d17 7827
6aa5f3a6
DE
7828/* Traversal function for process_skeletonless_type_unit.
7829 Read a TU in a DWO file and build partial symbols for it. */
7830
7831static int
7832process_skeletonless_type_unit (void **slot, void *info)
7833{
7834 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7835 struct dwarf2_per_objfile *dwarf2_per_objfile
7836 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7837 struct signatured_type find_entry, *entry;
7838
7839 /* If this TU doesn't exist in the global table, add it and read it in. */
7840
5989a64e
SM
7841 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7842 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7843
7844 find_entry.signature = dwo_unit->signature;
5989a64e 7845 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7846 &find_entry, INSERT);
6aa5f3a6
DE
7847 /* If we've already seen this type there's nothing to do. What's happening
7848 is we're doing our own version of comdat-folding here. */
7849 if (*slot != NULL)
7850 return 1;
7851
7852 /* This does the job that create_all_type_units would have done for
7853 this TU. */
ed2dc618
SM
7854 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7855 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7856 *slot = entry;
7857
7858 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7859 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7860 if (!reader.dummy_p)
7861 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7862 reader.comp_unit_die);
6aa5f3a6
DE
7863
7864 return 1;
7865}
7866
7867/* Traversal function for process_skeletonless_type_units. */
7868
7869static int
7870process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7871{
7872 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7873
7874 if (dwo_file->tus != NULL)
b0b6a987
TT
7875 htab_traverse_noresize (dwo_file->tus.get (),
7876 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7877
7878 return 1;
7879}
7880
7881/* Scan all TUs of DWO files, verifying we've processed them.
7882 This is needed in case a TU was emitted without its skeleton.
7883 Note: This can't be done until we know what all the DWO files are. */
7884
7885static void
ed2dc618 7886process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7887{
7888 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7889 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7890 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7891 {
5989a64e 7892 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7893 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7894 dwarf2_per_objfile);
6aa5f3a6 7895 }
348e048f
DE
7896}
7897
ed2dc618 7898/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7899
7900static void
ed2dc618 7901set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7902{
5989a64e 7903 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7904 {
891813be 7905 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7906
36586728
TT
7907 if (pst == NULL)
7908 continue;
7909
b76e467d 7910 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7911 {
7912 /* Set the 'user' field only if it is not already set. */
7913 if (pst->dependencies[j]->user == NULL)
7914 pst->dependencies[j]->user = pst;
7915 }
7916 }
7917}
7918
93311388
DE
7919/* Build the partial symbol table by doing a quick pass through the
7920 .debug_info and .debug_abbrev sections. */
72bf9492 7921
93311388 7922static void
ed2dc618 7923dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7924{
ed2dc618 7925 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7926
b4f54984 7927 if (dwarf_read_debug)
45cfd468
DE
7928 {
7929 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7930 objfile_name (objfile));
45cfd468
DE
7931 }
7932
76935768 7933 scoped_restore restore_reading_psyms
5989a64e 7934 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7935 true);
98bfdba5 7936
5989a64e 7937 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7938
93311388
DE
7939 /* Any cached compilation units will be linked by the per-objfile
7940 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7941 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7942
ed2dc618 7943 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7944
ed2dc618 7945 create_all_comp_units (dwarf2_per_objfile);
c906108c 7946
60606b2c
TT
7947 /* Create a temporary address map on a temporary obstack. We later
7948 copy this to the final obstack. */
8268c778 7949 auto_obstack temp_obstack;
791afaa2
TT
7950
7951 scoped_restore save_psymtabs_addrmap
d320c2b5 7952 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7953 addrmap_create_mutable (&temp_obstack));
72bf9492 7954
5989a64e 7955 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7956 {
7957 if (per_cu->v.psymtab != NULL)
7958 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7959 continue;
ab432490
SM
7960 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7961 language_minimal);
3d5afab3 7962 }
ff013f42 7963
6aa5f3a6 7964 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7965 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7966
7967 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7968 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7969 {
5989a64e 7970 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7971 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7972 }
7973
b4f54984 7974 if (dwarf_read_debug)
ed2dc618 7975 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7976
ed2dc618 7977 set_partial_user (dwarf2_per_objfile);
95554aad 7978
d320c2b5
TT
7979 objfile->partial_symtabs->psymtabs_addrmap
7980 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7981 objfile->partial_symtabs->obstack ());
791afaa2
TT
7982 /* At this point we want to keep the address map. */
7983 save_psymtabs_addrmap.release ();
ff013f42 7984
b4f54984 7985 if (dwarf_read_debug)
45cfd468 7986 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7987 objfile_name (objfile));
ae038cb0
DJ
7988}
7989
dee91e82
DE
7990/* Load the partial DIEs for a secondary CU into memory.
7991 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7992
dee91e82 7993static void
ab432490
SM
7994load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7995 dwarf2_per_objfile *per_objfile)
dee91e82 7996{
ab432490 7997 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
7998
7999 if (!reader.dummy_p)
8000 {
8001 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8002 language_minimal);
8003
8004 /* Check if comp unit has_children.
8005 If so, read the rest of the partial symbols from this comp unit.
8006 If not, there's no more debug_info for this comp unit. */
3e225074 8007 if (reader.comp_unit_die->has_children)
c0ab21c2 8008 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8009
8010 reader.keep ();
c0ab21c2 8011 }
ae038cb0
DJ
8012}
8013
ae038cb0 8014static void
ed2dc618 8015read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8016 struct dwarf2_section_info *section,
f1902523 8017 struct dwarf2_section_info *abbrev_section,
b76e467d 8018 unsigned int is_dwz)
ae038cb0 8019{
d521ce57 8020 const gdb_byte *info_ptr;
ed2dc618 8021 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8022
b4f54984 8023 if (dwarf_read_debug)
bf6af496 8024 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8025 section->get_name (),
8026 section->get_file_name ());
bf6af496 8027
96b79293 8028 section->read (objfile);
ae038cb0 8029
36586728 8030 info_ptr = section->buffer;
6e70227d 8031
36586728 8032 while (info_ptr < section->buffer + section->size)
ae038cb0 8033 {
ae038cb0 8034 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8035
9c541725 8036 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8037
f1902523 8038 comp_unit_head cu_header;
ed2dc618
SM
8039 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8040 abbrev_section, info_ptr,
8041 rcuh_kind::COMPILE);
ae038cb0
DJ
8042
8043 /* Save the compilation unit for later lookup. */
f1902523 8044 if (cu_header.unit_type != DW_UT_type)
5989a64e 8045 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8046 else
8047 {
5989a64e 8048 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8049 sig_type->signature = cu_header.signature;
8050 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8051 this_cu = &sig_type->per_cu;
8052 }
8053 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8054 this_cu->sect_off = sect_off;
f1902523 8055 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8056 this_cu->is_dwz = is_dwz;
e3b94546 8057 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8058 this_cu->section = section;
ae038cb0 8059
5989a64e 8060 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8061
8062 info_ptr = info_ptr + this_cu->length;
8063 }
36586728
TT
8064}
8065
8066/* Create a list of all compilation units in OBJFILE.
8067 This is only done for -readnow and building partial symtabs. */
8068
8069static void
ed2dc618 8070create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8071{
5989a64e
SM
8072 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8073 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8074 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8075
c3699833 8076 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8077 if (dwz != NULL)
ed2dc618 8078 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8079 1);
c906108c
SS
8080}
8081
5734ee8b 8082/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8083 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8084 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8085 DW_AT_ranges). See the comments of add_partial_subprogram on how
8086 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8087
72bf9492
DJ
8088static void
8089scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8090 CORE_ADDR *highpc, int set_addrmap,
8091 struct dwarf2_cu *cu)
c906108c 8092{
72bf9492 8093 struct partial_die_info *pdi;
c906108c 8094
91c24f0a
DC
8095 /* Now, march along the PDI's, descending into ones which have
8096 interesting children but skipping the children of the other ones,
8097 until we reach the end of the compilation unit. */
c906108c 8098
72bf9492 8099 pdi = first_die;
91c24f0a 8100
72bf9492
DJ
8101 while (pdi != NULL)
8102 {
52356b79 8103 pdi->fixup (cu);
c906108c 8104
f55ee35c 8105 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8106 children, so we need to look at them. Ditto for anonymous
8107 enums. */
933c6fe4 8108
72bf9492 8109 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8110 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8111 || pdi->tag == DW_TAG_imported_unit
8112 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8113 {
72bf9492 8114 switch (pdi->tag)
c906108c
SS
8115 {
8116 case DW_TAG_subprogram:
b1dc1806 8117 case DW_TAG_inlined_subroutine:
cdc07690 8118 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8119 break;
72929c62 8120 case DW_TAG_constant:
c906108c
SS
8121 case DW_TAG_variable:
8122 case DW_TAG_typedef:
91c24f0a 8123 case DW_TAG_union_type:
317d2668
TV
8124 if (!pdi->is_declaration
8125 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8126 {
72bf9492 8127 add_partial_symbol (pdi, cu);
63d06c5c
DC
8128 }
8129 break;
c906108c 8130 case DW_TAG_class_type:
680b30c7 8131 case DW_TAG_interface_type:
c906108c 8132 case DW_TAG_structure_type:
72bf9492 8133 if (!pdi->is_declaration)
c906108c 8134 {
72bf9492 8135 add_partial_symbol (pdi, cu);
c906108c 8136 }
b7fee5a3
KS
8137 if ((cu->language == language_rust
8138 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8139 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8140 set_addrmap, cu);
c906108c 8141 break;
91c24f0a 8142 case DW_TAG_enumeration_type:
72bf9492
DJ
8143 if (!pdi->is_declaration)
8144 add_partial_enumeration (pdi, cu);
c906108c
SS
8145 break;
8146 case DW_TAG_base_type:
a02abb62 8147 case DW_TAG_subrange_type:
c906108c 8148 /* File scope base type definitions are added to the partial
c5aa993b 8149 symbol table. */
72bf9492 8150 add_partial_symbol (pdi, cu);
c906108c 8151 break;
d9fa45fe 8152 case DW_TAG_namespace:
cdc07690 8153 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8154 break;
5d7cb8df 8155 case DW_TAG_module:
59c35742
AB
8156 if (!pdi->is_declaration)
8157 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8158 break;
95554aad
TT
8159 case DW_TAG_imported_unit:
8160 {
8161 struct dwarf2_per_cu_data *per_cu;
8162
f4dc4d17
DE
8163 /* For now we don't handle imported units in type units. */
8164 if (cu->per_cu->is_debug_types)
8165 {
8166 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8167 " supported in type units [in module %s]"),
5e22e966 8168 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8169 }
8170
e3b94546 8171 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8172 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8173
8174 /* Go read the partial unit, if needed. */
8175 if (per_cu->v.psymtab == NULL)
ab432490
SM
8176 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8177 cu->language);
95554aad 8178
ae640021 8179 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8180 }
8181 break;
74921315
KS
8182 case DW_TAG_imported_declaration:
8183 add_partial_symbol (pdi, cu);
8184 break;
c906108c
SS
8185 default:
8186 break;
8187 }
8188 }
8189
72bf9492
DJ
8190 /* If the die has a sibling, skip to the sibling. */
8191
8192 pdi = pdi->die_sibling;
8193 }
8194}
8195
8196/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8197
72bf9492 8198 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8199 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8200 Enumerators are an exception; they use the scope of their parent
8201 enumeration type, i.e. the name of the enumeration type is not
8202 prepended to the enumerator.
91c24f0a 8203
72bf9492
DJ
8204 There are two complexities. One is DW_AT_specification; in this
8205 case "parent" means the parent of the target of the specification,
8206 instead of the direct parent of the DIE. The other is compilers
8207 which do not emit DW_TAG_namespace; in this case we try to guess
8208 the fully qualified name of structure types from their members'
8209 linkage names. This must be done using the DIE's children rather
8210 than the children of any DW_AT_specification target. We only need
8211 to do this for structures at the top level, i.e. if the target of
8212 any DW_AT_specification (if any; otherwise the DIE itself) does not
8213 have a parent. */
8214
8215/* Compute the scope prefix associated with PDI's parent, in
8216 compilation unit CU. The result will be allocated on CU's
8217 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8218 field. NULL is returned if no prefix is necessary. */
15d034d0 8219static const char *
72bf9492
DJ
8220partial_die_parent_scope (struct partial_die_info *pdi,
8221 struct dwarf2_cu *cu)
8222{
15d034d0 8223 const char *grandparent_scope;
72bf9492 8224 struct partial_die_info *parent, *real_pdi;
91c24f0a 8225
72bf9492
DJ
8226 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8227 then this means the parent of the specification DIE. */
8228
8229 real_pdi = pdi;
72bf9492 8230 while (real_pdi->has_specification)
fb816e8b 8231 {
122cf0f2
AB
8232 auto res = find_partial_die (real_pdi->spec_offset,
8233 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8234 real_pdi = res.pdi;
8235 cu = res.cu;
8236 }
72bf9492
DJ
8237
8238 parent = real_pdi->die_parent;
8239 if (parent == NULL)
8240 return NULL;
8241
8242 if (parent->scope_set)
8243 return parent->scope;
8244
52356b79 8245 parent->fixup (cu);
72bf9492 8246
10b3939b 8247 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8248
acebe513
UW
8249 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8250 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8251 Work around this problem here. */
8252 if (cu->language == language_cplus
6e70227d 8253 && parent->tag == DW_TAG_namespace
acebe513
UW
8254 && strcmp (parent->name, "::") == 0
8255 && grandparent_scope == NULL)
8256 {
8257 parent->scope = NULL;
8258 parent->scope_set = 1;
8259 return NULL;
8260 }
8261
0a4b0913 8262 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8263 if (pdi->tag == DW_TAG_enumerator)
8264 /* Enumerators should not get the name of the enumeration as a prefix. */
8265 parent->scope = grandparent_scope;
8266 else if (parent->tag == DW_TAG_namespace
f55ee35c 8267 || parent->tag == DW_TAG_module
72bf9492
DJ
8268 || parent->tag == DW_TAG_structure_type
8269 || parent->tag == DW_TAG_class_type
680b30c7 8270 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8271 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8272 || parent->tag == DW_TAG_enumeration_type
8273 || (cu->language == language_fortran
8274 && parent->tag == DW_TAG_subprogram
8275 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8276 {
8277 if (grandparent_scope == NULL)
8278 parent->scope = parent->name;
8279 else
3e43a32a
MS
8280 parent->scope = typename_concat (&cu->comp_unit_obstack,
8281 grandparent_scope,
f55ee35c 8282 parent->name, 0, cu);
72bf9492 8283 }
72bf9492
DJ
8284 else
8285 {
8286 /* FIXME drow/2004-04-01: What should we be doing with
8287 function-local names? For partial symbols, we should probably be
8288 ignoring them. */
fa9c3fa0
TT
8289 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8290 dwarf_tag_name (parent->tag),
8291 sect_offset_str (pdi->sect_off));
72bf9492 8292 parent->scope = grandparent_scope;
c906108c
SS
8293 }
8294
72bf9492
DJ
8295 parent->scope_set = 1;
8296 return parent->scope;
8297}
8298
8299/* Return the fully scoped name associated with PDI, from compilation unit
8300 CU. The result will be allocated with malloc. */
4568ecf9 8301
43816ebc 8302static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8303partial_die_full_name (struct partial_die_info *pdi,
8304 struct dwarf2_cu *cu)
8305{
15d034d0 8306 const char *parent_scope;
72bf9492 8307
98bfdba5
PA
8308 /* If this is a template instantiation, we can not work out the
8309 template arguments from partial DIEs. So, unfortunately, we have
8310 to go through the full DIEs. At least any work we do building
8311 types here will be reused if full symbols are loaded later. */
8312 if (pdi->has_template_arguments)
8313 {
52356b79 8314 pdi->fixup (cu);
98bfdba5
PA
8315
8316 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8317 {
8318 struct die_info *die;
8319 struct attribute attr;
8320 struct dwarf2_cu *ref_cu = cu;
8321
b64f50a1 8322 /* DW_FORM_ref_addr is using section offset. */
b4069958 8323 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8324 attr.form = DW_FORM_ref_addr;
9c541725 8325 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8326 die = follow_die_ref (NULL, &attr, &ref_cu);
8327
43816ebc 8328 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8329 }
8330 }
8331
72bf9492
DJ
8332 parent_scope = partial_die_parent_scope (pdi, cu);
8333 if (parent_scope == NULL)
8334 return NULL;
8335 else
43816ebc
TT
8336 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8337 pdi->name, 0, cu));
c906108c
SS
8338}
8339
8340static void
72bf9492 8341add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8342{
5e22e966 8343 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8344 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8345 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8346 CORE_ADDR addr = 0;
15d034d0 8347 const char *actual_name = NULL;
e142c38c
DJ
8348 CORE_ADDR baseaddr;
8349
b3b3bada 8350 baseaddr = objfile->text_section_offset ();
c906108c 8351
43816ebc
TT
8352 gdb::unique_xmalloc_ptr<char> built_actual_name
8353 = partial_die_full_name (pdi, cu);
15d034d0 8354 if (built_actual_name != NULL)
43816ebc 8355 actual_name = built_actual_name.get ();
63d06c5c 8356
72bf9492
DJ
8357 if (actual_name == NULL)
8358 actual_name = pdi->name;
8359
76e288d1
TT
8360 partial_symbol psymbol;
8361 memset (&psymbol, 0, sizeof (psymbol));
8362 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8363 psymbol.ginfo.section = -1;
8364
8365 /* The code below indicates that the psymbol should be installed by
8366 setting this. */
8367 gdb::optional<psymbol_placement> where;
8368
c906108c
SS
8369 switch (pdi->tag)
8370 {
b1dc1806 8371 case DW_TAG_inlined_subroutine:
c906108c 8372 case DW_TAG_subprogram:
79748972
TT
8373 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8374 - baseaddr);
0a4b0913
AB
8375 if (pdi->is_external
8376 || cu->language == language_ada
8377 || (cu->language == language_fortran
8378 && pdi->die_parent != NULL
8379 && pdi->die_parent->tag == DW_TAG_subprogram))
8380 {
8381 /* Normally, only "external" DIEs are part of the global scope.
8382 But in Ada and Fortran, we want to be able to access nested
8383 procedures globally. So all Ada and Fortran subprograms are
8384 stored in the global scope. */
76e288d1 8385 where = psymbol_placement::GLOBAL;
c906108c
SS
8386 }
8387 else
76e288d1
TT
8388 where = psymbol_placement::STATIC;
8389
8390 psymbol.domain = VAR_DOMAIN;
8391 psymbol.aclass = LOC_BLOCK;
8392 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8393 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8394
8395 if (pdi->main_subprogram && actual_name != NULL)
8396 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8397 break;
72929c62 8398 case DW_TAG_constant:
76e288d1
TT
8399 psymbol.domain = VAR_DOMAIN;
8400 psymbol.aclass = LOC_STATIC;
8401 where = (pdi->is_external
8402 ? psymbol_placement::GLOBAL
8403 : psymbol_placement::STATIC);
72929c62 8404 break;
c906108c 8405 case DW_TAG_variable:
95554aad
TT
8406 if (pdi->d.locdesc)
8407 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8408
95554aad 8409 if (pdi->d.locdesc
caac4577 8410 && addr == 0
5989a64e 8411 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8412 {
8413 /* A global or static variable may also have been stripped
8414 out by the linker if unused, in which case its address
8415 will be nullified; do not add such variables into partial
8416 symbol table then. */
8417 }
8418 else if (pdi->is_external)
c906108c
SS
8419 {
8420 /* Global Variable.
8421 Don't enter into the minimal symbol tables as there is
8422 a minimal symbol table entry from the ELF symbols already.
8423 Enter into partial symbol table if it has a location
8424 descriptor or a type.
8425 If the location descriptor is missing, new_symbol will create
8426 a LOC_UNRESOLVED symbol, the address of the variable will then
8427 be determined from the minimal symbol table whenever the variable
8428 is referenced.
8429 The address for the partial symbol table entry is not
8430 used by GDB, but it comes in handy for debugging partial symbol
8431 table building. */
8432
95554aad 8433 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8434 {
8435 psymbol.domain = VAR_DOMAIN;
8436 psymbol.aclass = LOC_STATIC;
8437 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8438 psymbol.ginfo.value.address = addr;
8439 where = psymbol_placement::GLOBAL;
8440 }
c906108c
SS
8441 }
8442 else
8443 {
ff908ebf
AW
8444 int has_loc = pdi->d.locdesc != NULL;
8445
8446 /* Static Variable. Skip symbols whose value we cannot know (those
8447 without location descriptors or constant values). */
8448 if (!has_loc && !pdi->has_const_value)
43816ebc 8449 return;
ff908ebf 8450
76e288d1
TT
8451 psymbol.domain = VAR_DOMAIN;
8452 psymbol.aclass = LOC_STATIC;
8453 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8454 if (has_loc)
8455 psymbol.ginfo.value.address = addr;
8456 where = psymbol_placement::STATIC;
c906108c
SS
8457 }
8458 break;
8459 case DW_TAG_typedef:
8460 case DW_TAG_base_type:
a02abb62 8461 case DW_TAG_subrange_type:
76e288d1
TT
8462 psymbol.domain = VAR_DOMAIN;
8463 psymbol.aclass = LOC_TYPEDEF;
8464 where = psymbol_placement::STATIC;
c906108c 8465 break;
74921315 8466 case DW_TAG_imported_declaration:
72bf9492 8467 case DW_TAG_namespace:
76e288d1
TT
8468 psymbol.domain = VAR_DOMAIN;
8469 psymbol.aclass = LOC_TYPEDEF;
8470 where = psymbol_placement::GLOBAL;
72bf9492 8471 break;
530e8392 8472 case DW_TAG_module:
a5fd13a9
BH
8473 /* With Fortran 77 there might be a "BLOCK DATA" module
8474 available without any name. If so, we skip the module as it
8475 doesn't bring any value. */
8476 if (actual_name != nullptr)
76e288d1
TT
8477 {
8478 psymbol.domain = MODULE_DOMAIN;
8479 psymbol.aclass = LOC_TYPEDEF;
8480 where = psymbol_placement::GLOBAL;
8481 }
530e8392 8482 break;
c906108c 8483 case DW_TAG_class_type:
680b30c7 8484 case DW_TAG_interface_type:
c906108c
SS
8485 case DW_TAG_structure_type:
8486 case DW_TAG_union_type:
8487 case DW_TAG_enumeration_type:
fa4028e9
JB
8488 /* Skip external references. The DWARF standard says in the section
8489 about "Structure, Union, and Class Type Entries": "An incomplete
8490 structure, union or class type is represented by a structure,
8491 union or class entry that does not have a byte size attribute
8492 and that has a DW_AT_declaration attribute." */
8493 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8494 return;
fa4028e9 8495
63d06c5c
DC
8496 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8497 static vs. global. */
76e288d1
TT
8498 psymbol.domain = STRUCT_DOMAIN;
8499 psymbol.aclass = LOC_TYPEDEF;
8500 where = (cu->language == language_cplus
8501 ? psymbol_placement::GLOBAL
8502 : psymbol_placement::STATIC);
c906108c
SS
8503 break;
8504 case DW_TAG_enumerator:
76e288d1
TT
8505 psymbol.domain = VAR_DOMAIN;
8506 psymbol.aclass = LOC_CONST;
8507 where = (cu->language == language_cplus
8508 ? psymbol_placement::GLOBAL
8509 : psymbol_placement::STATIC);
c906108c
SS
8510 break;
8511 default:
8512 break;
8513 }
76e288d1
TT
8514
8515 if (where.has_value ())
8516 {
f049a313
TT
8517 if (built_actual_name != nullptr)
8518 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8519 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8520 psymbol.ginfo.set_linkage_name (actual_name);
8521 else
8522 {
8523 psymbol.ginfo.set_demangled_name (actual_name,
8524 &objfile->objfile_obstack);
8525 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8526 }
76e288d1
TT
8527 add_psymbol_to_list (psymbol, *where, objfile);
8528 }
c906108c
SS
8529}
8530
5c4e30ca
DC
8531/* Read a partial die corresponding to a namespace; also, add a symbol
8532 corresponding to that namespace to the symbol table. NAMESPACE is
8533 the name of the enclosing namespace. */
91c24f0a 8534
72bf9492
DJ
8535static void
8536add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8537 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8538 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8539{
72bf9492 8540 /* Add a symbol for the namespace. */
e7c27a73 8541
72bf9492 8542 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8543
8544 /* Now scan partial symbols in that namespace. */
8545
91c24f0a 8546 if (pdi->has_children)
cdc07690 8547 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8548}
8549
5d7cb8df
JK
8550/* Read a partial die corresponding to a Fortran module. */
8551
8552static void
8553add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8554 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8555{
530e8392
KB
8556 /* Add a symbol for the namespace. */
8557
8558 add_partial_symbol (pdi, cu);
8559
f55ee35c 8560 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8561
8562 if (pdi->has_children)
cdc07690 8563 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8564}
8565
b1dc1806
XR
8566/* Read a partial die corresponding to a subprogram or an inlined
8567 subprogram and create a partial symbol for that subprogram.
8568 When the CU language allows it, this routine also defines a partial
8569 symbol for each nested subprogram that this subprogram contains.
8570 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8571 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8572
cdc07690
YQ
8573 PDI may also be a lexical block, in which case we simply search
8574 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8575 Again, this is only performed when the CU language allows this
8576 type of definitions. */
8577
8578static void
8579add_partial_subprogram (struct partial_die_info *pdi,
8580 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8581 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8582{
b1dc1806 8583 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8584 {
8585 if (pdi->has_pc_info)
8586 {
8587 if (pdi->lowpc < *lowpc)
8588 *lowpc = pdi->lowpc;
8589 if (pdi->highpc > *highpc)
8590 *highpc = pdi->highpc;
cdc07690 8591 if (set_addrmap)
5734ee8b 8592 {
5e22e966 8593 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8594 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8595 CORE_ADDR baseaddr;
b926417a
TT
8596 CORE_ADDR this_highpc;
8597 CORE_ADDR this_lowpc;
5734ee8b 8598
b3b3bada 8599 baseaddr = objfile->text_section_offset ();
b926417a
TT
8600 this_lowpc
8601 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8602 pdi->lowpc + baseaddr)
8603 - baseaddr);
8604 this_highpc
8605 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8606 pdi->highpc + baseaddr)
8607 - baseaddr);
d320c2b5 8608 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8609 this_lowpc, this_highpc - 1,
9291a0cd 8610 cu->per_cu->v.psymtab);
5734ee8b 8611 }
481860b3
GB
8612 }
8613
8614 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8615 {
bc30ff58 8616 if (!pdi->is_declaration)
e8d05480
JB
8617 /* Ignore subprogram DIEs that do not have a name, they are
8618 illegal. Do not emit a complaint at this point, we will
8619 do so when we convert this psymtab into a symtab. */
8620 if (pdi->name)
8621 add_partial_symbol (pdi, cu);
bc30ff58
JB
8622 }
8623 }
6e70227d 8624
bc30ff58
JB
8625 if (! pdi->has_children)
8626 return;
8627
0a4b0913 8628 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8629 {
8630 pdi = pdi->die_child;
8631 while (pdi != NULL)
8632 {
52356b79 8633 pdi->fixup (cu);
bc30ff58 8634 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8635 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8636 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8637 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8638 pdi = pdi->die_sibling;
8639 }
8640 }
8641}
8642
91c24f0a
DC
8643/* Read a partial die corresponding to an enumeration type. */
8644
72bf9492
DJ
8645static void
8646add_partial_enumeration (struct partial_die_info *enum_pdi,
8647 struct dwarf2_cu *cu)
91c24f0a 8648{
72bf9492 8649 struct partial_die_info *pdi;
91c24f0a
DC
8650
8651 if (enum_pdi->name != NULL)
72bf9492
DJ
8652 add_partial_symbol (enum_pdi, cu);
8653
8654 pdi = enum_pdi->die_child;
8655 while (pdi)
91c24f0a 8656 {
72bf9492 8657 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8658 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8659 else
72bf9492
DJ
8660 add_partial_symbol (pdi, cu);
8661 pdi = pdi->die_sibling;
91c24f0a 8662 }
91c24f0a
DC
8663}
8664
6caca83c
CC
8665/* Return the initial uleb128 in the die at INFO_PTR. */
8666
8667static unsigned int
d521ce57 8668peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8669{
8670 unsigned int bytes_read;
8671
8672 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8673}
8674
685af9cd
TT
8675/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8676 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8677
4bb7a0a7
DJ
8678 Return the corresponding abbrev, or NULL if the number is zero (indicating
8679 an empty DIE). In either case *BYTES_READ will be set to the length of
8680 the initial number. */
8681
8682static struct abbrev_info *
685af9cd
TT
8683peek_die_abbrev (const die_reader_specs &reader,
8684 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8685{
685af9cd 8686 dwarf2_cu *cu = reader.cu;
5e22e966 8687 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8688 unsigned int abbrev_number
8689 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8690
8691 if (abbrev_number == 0)
8692 return NULL;
8693
685af9cd 8694 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8695 if (!abbrev)
8696 {
422b9917 8697 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8698 " at offset %s [in module %s]"),
422b9917 8699 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8700 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8701 }
8702
8703 return abbrev;
8704}
8705
93311388
DE
8706/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8707 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8708 DIE. Any children of the skipped DIEs will also be skipped. */
8709
d521ce57
TT
8710static const gdb_byte *
8711skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8712{
4bb7a0a7
DJ
8713 while (1)
8714 {
685af9cd
TT
8715 unsigned int bytes_read;
8716 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8717
4bb7a0a7
DJ
8718 if (abbrev == NULL)
8719 return info_ptr + bytes_read;
8720 else
dee91e82 8721 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8722 }
8723}
8724
93311388
DE
8725/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8726 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8727 abbrev corresponding to that skipped uleb128 should be passed in
8728 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8729 children. */
8730
d521ce57
TT
8731static const gdb_byte *
8732skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8733 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8734{
8735 unsigned int bytes_read;
8736 struct attribute attr;
dee91e82
DE
8737 bfd *abfd = reader->abfd;
8738 struct dwarf2_cu *cu = reader->cu;
d521ce57 8739 const gdb_byte *buffer = reader->buffer;
f664829e 8740 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8741 unsigned int form, i;
8742
8743 for (i = 0; i < abbrev->num_attrs; i++)
8744 {
8745 /* The only abbrev we care about is DW_AT_sibling. */
8746 if (abbrev->attrs[i].name == DW_AT_sibling)
8747 {
18a8505e
AT
8748 bool ignored;
8749 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8750 &ignored);
4bb7a0a7 8751 if (attr.form == DW_FORM_ref_addr)
b98664d3 8752 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8753 else
b9502d3f 8754 {
0826b30a 8755 sect_offset off = attr.get_ref_die_offset ();
9c541725 8756 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8757
8758 if (sibling_ptr < info_ptr)
b98664d3 8759 complaint (_("DW_AT_sibling points backwards"));
22869d73 8760 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8761 reader->die_section->overflow_complaint ();
b9502d3f
WN
8762 else
8763 return sibling_ptr;
8764 }
4bb7a0a7
DJ
8765 }
8766
8767 /* If it isn't DW_AT_sibling, skip this attribute. */
8768 form = abbrev->attrs[i].form;
8769 skip_attribute:
8770 switch (form)
8771 {
4bb7a0a7 8772 case DW_FORM_ref_addr:
ae411497
TT
8773 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8774 and later it is offset sized. */
8775 if (cu->header.version == 2)
8776 info_ptr += cu->header.addr_size;
8777 else
8778 info_ptr += cu->header.offset_size;
8779 break;
36586728
TT
8780 case DW_FORM_GNU_ref_alt:
8781 info_ptr += cu->header.offset_size;
8782 break;
ae411497 8783 case DW_FORM_addr:
4bb7a0a7
DJ
8784 info_ptr += cu->header.addr_size;
8785 break;
8786 case DW_FORM_data1:
8787 case DW_FORM_ref1:
8788 case DW_FORM_flag:
8fe0f950 8789 case DW_FORM_strx1:
4bb7a0a7
DJ
8790 info_ptr += 1;
8791 break;
2dc7f7b3 8792 case DW_FORM_flag_present:
43988095 8793 case DW_FORM_implicit_const:
2dc7f7b3 8794 break;
4bb7a0a7
DJ
8795 case DW_FORM_data2:
8796 case DW_FORM_ref2:
8fe0f950 8797 case DW_FORM_strx2:
4bb7a0a7
DJ
8798 info_ptr += 2;
8799 break;
8fe0f950
AT
8800 case DW_FORM_strx3:
8801 info_ptr += 3;
8802 break;
4bb7a0a7
DJ
8803 case DW_FORM_data4:
8804 case DW_FORM_ref4:
8fe0f950 8805 case DW_FORM_strx4:
4bb7a0a7
DJ
8806 info_ptr += 4;
8807 break;
8808 case DW_FORM_data8:
8809 case DW_FORM_ref8:
55f1336d 8810 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8811 info_ptr += 8;
8812 break;
0224619f
JK
8813 case DW_FORM_data16:
8814 info_ptr += 16;
8815 break;
4bb7a0a7 8816 case DW_FORM_string:
9b1c24c8 8817 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8818 info_ptr += bytes_read;
8819 break;
2dc7f7b3 8820 case DW_FORM_sec_offset:
4bb7a0a7 8821 case DW_FORM_strp:
36586728 8822 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8823 info_ptr += cu->header.offset_size;
8824 break;
2dc7f7b3 8825 case DW_FORM_exprloc:
4bb7a0a7
DJ
8826 case DW_FORM_block:
8827 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8828 info_ptr += bytes_read;
8829 break;
8830 case DW_FORM_block1:
8831 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8832 break;
8833 case DW_FORM_block2:
8834 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8835 break;
8836 case DW_FORM_block4:
8837 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8838 break;
336d760d 8839 case DW_FORM_addrx:
cf532bd1 8840 case DW_FORM_strx:
4bb7a0a7
DJ
8841 case DW_FORM_sdata:
8842 case DW_FORM_udata:
8843 case DW_FORM_ref_udata:
3019eac3
DE
8844 case DW_FORM_GNU_addr_index:
8845 case DW_FORM_GNU_str_index:
18a8505e 8846 case DW_FORM_rnglistx:
41144253 8847 case DW_FORM_loclistx:
d521ce57 8848 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8849 break;
8850 case DW_FORM_indirect:
8851 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8852 info_ptr += bytes_read;
8853 /* We need to continue parsing from here, so just go back to
8854 the top. */
8855 goto skip_attribute;
8856
8857 default:
3e43a32a
MS
8858 error (_("Dwarf Error: Cannot handle %s "
8859 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8860 dwarf_form_name (form),
8861 bfd_get_filename (abfd));
8862 }
8863 }
8864
8865 if (abbrev->has_children)
dee91e82 8866 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8867 else
8868 return info_ptr;
8869}
8870
93311388 8871/* Locate ORIG_PDI's sibling.
dee91e82 8872 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8873
d521ce57 8874static const gdb_byte *
dee91e82
DE
8875locate_pdi_sibling (const struct die_reader_specs *reader,
8876 struct partial_die_info *orig_pdi,
d521ce57 8877 const gdb_byte *info_ptr)
91c24f0a
DC
8878{
8879 /* Do we know the sibling already? */
72bf9492 8880
91c24f0a
DC
8881 if (orig_pdi->sibling)
8882 return orig_pdi->sibling;
8883
8884 /* Are there any children to deal with? */
8885
8886 if (!orig_pdi->has_children)
8887 return info_ptr;
8888
4bb7a0a7 8889 /* Skip the children the long way. */
91c24f0a 8890
dee91e82 8891 return skip_children (reader, info_ptr);
91c24f0a
DC
8892}
8893
257e7a09 8894/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8895 not NULL. */
c906108c 8896
891813be
TT
8897void
8898dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8899{
ed2dc618
SM
8900 struct dwarf2_per_objfile *dwarf2_per_objfile
8901 = get_dwarf2_per_objfile (objfile);
8902
af758d11
SM
8903 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8904
077cbab2
TT
8905 /* If this psymtab is constructed from a debug-only objfile, the
8906 has_section_at_zero flag will not necessarily be correct. We
8907 can get the correct value for this flag by looking at the data
8908 associated with the (presumably stripped) associated objfile. */
8909 if (objfile->separate_debug_objfile_backlink)
c906108c 8910 {
077cbab2
TT
8911 struct dwarf2_per_objfile *dpo_backlink
8912 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8913
5989a64e
SM
8914 dwarf2_per_objfile->per_bfd->has_section_at_zero
8915 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8916 }
98bfdba5 8917
8566b89b 8918 expand_psymtab (objfile);
95554aad 8919
ed2dc618 8920 process_cu_includes (dwarf2_per_objfile);
c906108c 8921}
9cdd5dbd
DE
8922\f
8923/* Reading in full CUs. */
c906108c 8924
10b3939b
DJ
8925/* Add PER_CU to the queue. */
8926
8927static void
95554aad
TT
8928queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8929 enum language pretend_language)
10b3939b 8930{
10b3939b 8931 per_cu->queued = 1;
1859c670 8932 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8933}
8934
89e63ee4
DE
8935/* If PER_CU is not yet queued, add it to the queue.
8936 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8937 dependency.
0907af0c 8938 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8939 meaning either PER_CU is already queued or it is already loaded.
8940
8941 N.B. There is an invariant here that if a CU is queued then it is loaded.
8942 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8943
8944static int
89e63ee4 8945maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8946 struct dwarf2_per_cu_data *per_cu,
8947 enum language pretend_language)
8948{
8949 /* We may arrive here during partial symbol reading, if we need full
8950 DIEs to process an unusual case (e.g. template arguments). Do
8951 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8952 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8953 {
8954 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8955 return 1;
8956 return 0;
8957 }
8958
8959 /* Mark the dependence relation so that we don't flush PER_CU
8960 too early. */
89e63ee4
DE
8961 if (dependent_cu != NULL)
8962 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8963
8964 /* If it's already on the queue, we have nothing to do. */
8965 if (per_cu->queued)
8966 return 0;
8967
8968 /* If the compilation unit is already loaded, just mark it as
8969 used. */
8970 if (per_cu->cu != NULL)
8971 {
8972 per_cu->cu->last_used = 0;
8973 return 0;
8974 }
8975
8976 /* Add it to the queue. */
8977 queue_comp_unit (per_cu, pretend_language);
8978
8979 return 1;
8980}
8981
10b3939b
DJ
8982/* Process the queue. */
8983
8984static void
ed2dc618 8985process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8986{
b4f54984 8987 if (dwarf_read_debug)
45cfd468
DE
8988 {
8989 fprintf_unfiltered (gdb_stdlog,
8990 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8991 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8992 }
8993
03dd20cc
DJ
8994 /* The queue starts out with one item, but following a DIE reference
8995 may load a new CU, adding it to the end of the queue. */
5989a64e 8996 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8997 {
5989a64e 8998 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8999
af758d11 9000 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 9001 /* Skip dummy CUs. */
39856def 9002 && item.per_cu->cu != NULL)
f4dc4d17 9003 {
39856def 9004 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9005 unsigned int debug_print_threshold;
247f5c4f 9006 char buf[100];
f4dc4d17 9007
247f5c4f 9008 if (per_cu->is_debug_types)
f4dc4d17 9009 {
247f5c4f
DE
9010 struct signatured_type *sig_type =
9011 (struct signatured_type *) per_cu;
9012
9d8780f0 9013 sprintf (buf, "TU %s at offset %s",
73be47f5 9014 hex_string (sig_type->signature),
9d8780f0 9015 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9016 /* There can be 100s of TUs.
9017 Only print them in verbose mode. */
9018 debug_print_threshold = 2;
f4dc4d17 9019 }
247f5c4f 9020 else
73be47f5 9021 {
9d8780f0
SM
9022 sprintf (buf, "CU at offset %s",
9023 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9024 debug_print_threshold = 1;
9025 }
247f5c4f 9026
b4f54984 9027 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9028 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9029
9030 if (per_cu->is_debug_types)
39856def 9031 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9032 else
39856def 9033 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9034
b4f54984 9035 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9036 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9037 }
10b3939b 9038
39856def 9039 item.per_cu->queued = 0;
5989a64e 9040 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9041 }
9042
b4f54984 9043 if (dwarf_read_debug)
45cfd468
DE
9044 {
9045 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9046 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9047 }
10b3939b
DJ
9048}
9049
10b3939b
DJ
9050/* Read in full symbols for PST, and anything it depends on. */
9051
8566b89b
TT
9052void
9053dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9054{
af758d11 9055 gdb_assert (!readin_p (objfile));
95554aad 9056
48993951 9057 expand_dependencies (objfile);
aaa75496 9058
97a1449a
SM
9059 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9060 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9061 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9062}
9063
af758d11
SM
9064/* See psympriv.h. */
9065
9066bool
9067dwarf2_psymtab::readin_p (struct objfile *objfile) const
9068{
9069 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9070 return per_objfile->symtab_set_p (per_cu_data);
9071}
9072
9073/* See psympriv.h. */
9074
9075compunit_symtab *
9076dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9077{
9078 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9079 return per_objfile->get_symtab (per_cu_data);
9080}
9081
dee91e82
DE
9082/* Trivial hash function for die_info: the hash value of a DIE
9083 is its offset in .debug_info for this objfile. */
10b3939b 9084
dee91e82
DE
9085static hashval_t
9086die_hash (const void *item)
10b3939b 9087{
9a3c8263 9088 const struct die_info *die = (const struct die_info *) item;
6502dd73 9089
9c541725 9090 return to_underlying (die->sect_off);
dee91e82 9091}
63d06c5c 9092
dee91e82
DE
9093/* Trivial comparison function for die_info structures: two DIEs
9094 are equal if they have the same offset. */
98bfdba5 9095
dee91e82
DE
9096static int
9097die_eq (const void *item_lhs, const void *item_rhs)
9098{
9a3c8263
SM
9099 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9100 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9101
9c541725 9102 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9103}
c906108c 9104
c0ab21c2 9105/* Load the DIEs associated with PER_CU into memory. */
c906108c 9106
dee91e82 9107static void
ab432490
SM
9108load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9109 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9110 bool skip_partial,
9111 enum language pretend_language)
dee91e82 9112{
c0ab21c2
TT
9113 gdb_assert (! this_cu->is_debug_types);
9114
ab432490 9115 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9116 if (reader.dummy_p)
9117 return;
9118
9119 struct dwarf2_cu *cu = reader.cu;
9120 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9121
dee91e82
DE
9122 gdb_assert (cu->die_hash == NULL);
9123 cu->die_hash =
9124 htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
e142c38c 9131
3e225074 9132 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9133 reader.comp_unit_die->child
9134 = read_die_and_siblings (&reader, reader.info_ptr,
9135 &info_ptr, reader.comp_unit_die);
9136 cu->dies = reader.comp_unit_die;
dee91e82 9137 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9138
9139 /* We try not to read any attributes in this function, because not
9cdd5dbd 9140 all CUs needed for references have been loaded yet, and symbol
10b3939b 9141 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9142 or we won't be able to build types correctly.
9143 Similarly, if we do not read the producer, we can not apply
9144 producer-specific interpretation. */
c0ab21c2 9145 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9146
9147 reader.keep ();
10b3939b
DJ
9148}
9149
3da10d80
KS
9150/* Add a DIE to the delayed physname list. */
9151
9152static void
9153add_to_method_list (struct type *type, int fnfield_index, int index,
9154 const char *name, struct die_info *die,
9155 struct dwarf2_cu *cu)
9156{
9157 struct delayed_method_info mi;
9158 mi.type = type;
9159 mi.fnfield_index = fnfield_index;
9160 mi.index = index;
9161 mi.name = name;
9162 mi.die = die;
c89b44cd 9163 cu->method_list.push_back (mi);
3da10d80
KS
9164}
9165
3693fdb3
PA
9166/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9167 "const" / "volatile". If so, decrements LEN by the length of the
9168 modifier and return true. Otherwise return false. */
9169
9170template<size_t N>
9171static bool
9172check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9173{
9174 size_t mod_len = sizeof (mod) - 1;
9175 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9176 {
9177 len -= mod_len;
9178 return true;
9179 }
9180 return false;
9181}
9182
3da10d80
KS
9183/* Compute the physnames of any methods on the CU's method list.
9184
9185 The computation of method physnames is delayed in order to avoid the
9186 (bad) condition that one of the method's formal parameters is of an as yet
9187 incomplete type. */
9188
9189static void
9190compute_delayed_physnames (struct dwarf2_cu *cu)
9191{
3693fdb3 9192 /* Only C++ delays computing physnames. */
c89b44cd 9193 if (cu->method_list.empty ())
3693fdb3
PA
9194 return;
9195 gdb_assert (cu->language == language_cplus);
9196
52941706 9197 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9198 {
1d06ead6 9199 const char *physname;
3da10d80 9200 struct fn_fieldlist *fn_flp
c89b44cd
TT
9201 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9202 physname = dwarf2_physname (mi.name, mi.die, cu);
9203 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9204 = physname ? physname : "";
3693fdb3
PA
9205
9206 /* Since there's no tag to indicate whether a method is a
9207 const/volatile overload, extract that information out of the
9208 demangled name. */
9209 if (physname != NULL)
9210 {
9211 size_t len = strlen (physname);
9212
9213 while (1)
9214 {
9215 if (physname[len] == ')') /* shortcut */
9216 break;
9217 else if (check_modifier (physname, len, " const"))
c89b44cd 9218 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9219 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9220 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9221 else
9222 break;
9223 }
9224 }
3da10d80 9225 }
c89b44cd
TT
9226
9227 /* The list is no longer needed. */
9228 cu->method_list.clear ();
3da10d80
KS
9229}
9230
a766d390
DE
9231/* Go objects should be embedded in a DW_TAG_module DIE,
9232 and it's not clear if/how imported objects will appear.
9233 To keep Go support simple until that's worked out,
9234 go back through what we've read and create something usable.
9235 We could do this while processing each DIE, and feels kinda cleaner,
9236 but that way is more invasive.
9237 This is to, for example, allow the user to type "p var" or "b main"
9238 without having to specify the package name, and allow lookups
9239 of module.object to work in contexts that use the expression
9240 parser. */
9241
9242static void
9243fixup_go_packaging (struct dwarf2_cu *cu)
9244{
421d1616 9245 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9246 struct pending *list;
9247 int i;
9248
c24bdb02 9249 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9250 list != NULL;
9251 list = list->next)
a766d390
DE
9252 {
9253 for (i = 0; i < list->nsyms; ++i)
9254 {
9255 struct symbol *sym = list->symbol[i];
9256
c1b5c1eb 9257 if (sym->language () == language_go
a766d390
DE
9258 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9259 {
421d1616
TT
9260 gdb::unique_xmalloc_ptr<char> this_package_name
9261 (go_symbol_package_name (sym));
a766d390
DE
9262
9263 if (this_package_name == NULL)
9264 continue;
9265 if (package_name == NULL)
421d1616 9266 package_name = std::move (this_package_name);
a766d390
DE
9267 else
9268 {
5e22e966 9269 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9270 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9271 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9272 (symbol_symtab (sym) != NULL
9273 ? symtab_to_filename_for_display
9274 (symbol_symtab (sym))
e3b94546 9275 : objfile_name (objfile)),
421d1616 9276 this_package_name.get (), package_name.get ());
a766d390
DE
9277 }
9278 }
9279 }
9280 }
9281
9282 if (package_name != NULL)
9283 {
5e22e966 9284 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9285 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9286 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9287 saved_package_name);
a766d390
DE
9288 struct symbol *sym;
9289
8c14c3a3 9290 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9291 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9292 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9293 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9294 e.g., "main" finds the "main" module and not C's main(). */
9295 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9296 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9297 SYMBOL_TYPE (sym) = type;
9298
c24bdb02 9299 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9300 }
9301}
9302
c9317f21
TT
9303/* Allocate a fully-qualified name consisting of the two parts on the
9304 obstack. */
9305
9306static const char *
9307rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9308{
9309 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9310}
9311
9c6a1327
TT
9312/* A helper that allocates a variant part to attach to a Rust enum
9313 type. OBSTACK is where the results should be allocated. TYPE is
9314 the type we're processing. DISCRIMINANT_INDEX is the index of the
9315 discriminant. It must be the index of one of the fields of TYPE.
9316 DEFAULT_INDEX is the index of the default field; or -1 if there is
9317 no default. RANGES is indexed by "effective" field number (the
9318 field index, but omitting the discriminant and default fields) and
9319 must hold the discriminant values used by the variants. Note that
9320 RANGES must have a lifetime at least as long as OBSTACK -- either
9321 already allocated on it, or static. */
c9317f21 9322
9c6a1327
TT
9323static void
9324alloc_rust_variant (struct obstack *obstack, struct type *type,
9325 int discriminant_index, int default_index,
9326 gdb::array_view<discriminant_range> ranges)
9327{
9328 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9329 must be handled by the caller. */
9330 gdb_assert (discriminant_index >= 0
1f704f76 9331 && discriminant_index < type->num_fields ());
c9317f21 9332 gdb_assert (default_index == -1
1f704f76 9333 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9334
9c6a1327 9335 /* We have one variant for each non-discriminant field. */
1f704f76 9336 int n_variants = type->num_fields () - 1;
c9317f21 9337
9c6a1327
TT
9338 variant *variants = new (obstack) variant[n_variants];
9339 int var_idx = 0;
9340 int range_idx = 0;
1f704f76 9341 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9342 {
9343 if (i == discriminant_index)
9344 continue;
c9317f21 9345
9c6a1327
TT
9346 variants[var_idx].first_field = i;
9347 variants[var_idx].last_field = i + 1;
9348
9349 /* The default field does not need a range, but other fields do.
9350 We skipped the discriminant above. */
9351 if (i != default_index)
9352 {
9353 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9354 ++range_idx;
9355 }
c9317f21 9356
9c6a1327
TT
9357 ++var_idx;
9358 }
9359
9360 gdb_assert (range_idx == ranges.size ());
9361 gdb_assert (var_idx == n_variants);
9362
9363 variant_part *part = new (obstack) variant_part;
9364 part->discriminant_index = discriminant_index;
9365 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9366 discriminant_index));
9367 part->variants = gdb::array_view<variant> (variants, n_variants);
9368
9369 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9370 gdb::array_view<variant_part> *prop_value
9371 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9372
9c6a1327
TT
9373 struct dynamic_prop prop;
9374 prop.kind = PROP_VARIANT_PARTS;
9375 prop.data.variant_parts = prop_value;
9376
5c54719c 9377 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9378}
9379
9380/* Some versions of rustc emitted enums in an unusual way.
9381
9382 Ordinary enums were emitted as unions. The first element of each
9383 structure in the union was named "RUST$ENUM$DISR". This element
9384 held the discriminant.
9385
9386 These versions of Rust also implemented the "non-zero"
9387 optimization. When the enum had two values, and one is empty and
9388 the other holds a pointer that cannot be zero, the pointer is used
9389 as the discriminant, with a zero value meaning the empty variant.
9390 Here, the union's first member is of the form
9391 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9392 where the fieldnos are the indices of the fields that should be
9393 traversed in order to find the field (which may be several fields deep)
9394 and the variantname is the name of the variant of the case when the
9395 field is zero.
9396
9397 This function recognizes whether TYPE is of one of these forms,
9398 and, if so, smashes it to be a variant type. */
9399
9400static void
9401quirk_rust_enum (struct type *type, struct objfile *objfile)
9402{
78134374 9403 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9404
9405 /* We don't need to deal with empty enums. */
1f704f76 9406 if (type->num_fields () == 0)
c9317f21
TT
9407 return;
9408
9409#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9410 if (type->num_fields () == 1
c9317f21
TT
9411 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9412 {
9413 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9414
9415 /* Decode the field name to find the offset of the
9416 discriminant. */
9417 ULONGEST bit_offset = 0;
9418 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9419 while (name[0] >= '0' && name[0] <= '9')
9420 {
9421 char *tail;
9422 unsigned long index = strtoul (name, &tail, 10);
9423 name = tail;
9424 if (*name != '$'
1f704f76 9425 || index >= field_type->num_fields ()
c9317f21
TT
9426 || (TYPE_FIELD_LOC_KIND (field_type, index)
9427 != FIELD_LOC_KIND_BITPOS))
9428 {
b98664d3 9429 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9430 "[in module %s]"),
9431 TYPE_FIELD_NAME (type, 0),
9432 objfile_name (objfile));
9433 return;
9434 }
9435 ++name;
9436
9437 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9438 field_type = TYPE_FIELD_TYPE (field_type, index);
9439 }
9440
9c6a1327
TT
9441 /* Smash this type to be a structure type. We have to do this
9442 because the type has already been recorded. */
67607e24 9443 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9444 type->set_num_fields (3);
9c6a1327 9445 /* Save the field we care about. */
ceacbf6e 9446 struct field saved_field = type->field (0);
3cabb6b0
SM
9447 type->set_fields
9448 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9449
9c6a1327
TT
9450 /* Put the discriminant at index 0. */
9451 TYPE_FIELD_TYPE (type, 0) = field_type;
9452 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9453 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9454 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9455
9456 /* The order of fields doesn't really matter, so put the real
9457 field at index 1 and the data-less field at index 2. */
ceacbf6e 9458 type->field (1) = saved_field;
9c6a1327 9459 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9460 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9461 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9462 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9463 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9464
9465 const char *dataless_name
7d93a1e0 9466 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9467 name);
9468 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9469 dataless_name);
9c6a1327 9470 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9471 /* NAME points into the original discriminant name, which
9472 already has the correct lifetime. */
9c6a1327 9473 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9474 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9475
9c6a1327
TT
9476 /* Indicate that this is a variant type. */
9477 static discriminant_range ranges[1] = { { 0, 0 } };
9478 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9479 }
77c2dba3
TT
9480 /* A union with a single anonymous field is probably an old-style
9481 univariant enum. */
1f704f76 9482 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9483 {
c9317f21
TT
9484 /* Smash this type to be a structure type. We have to do this
9485 because the type has already been recorded. */
67607e24 9486 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9487
9c6a1327 9488 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9489 const char *variant_name
7d93a1e0 9490 = rust_last_path_segment (field_type->name ());
9c6a1327 9491 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9492 field_type->set_name
9493 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9494 type->name (), variant_name));
c9317f21
TT
9495 }
9496 else
9497 {
9498 struct type *disr_type = nullptr;
1f704f76 9499 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9500 {
9501 disr_type = TYPE_FIELD_TYPE (type, i);
9502
78134374 9503 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9504 {
9505 /* All fields of a true enum will be structs. */
9506 return;
9507 }
1f704f76 9508 else if (disr_type->num_fields () == 0)
c9317f21
TT
9509 {
9510 /* Could be data-less variant, so keep going. */
a037790e 9511 disr_type = nullptr;
c9317f21
TT
9512 }
9513 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9514 "RUST$ENUM$DISR") != 0)
9515 {
9516 /* Not a Rust enum. */
9517 return;
9518 }
9519 else
9520 {
9521 /* Found one. */
9522 break;
9523 }
9524 }
9525
9526 /* If we got here without a discriminant, then it's probably
9527 just a union. */
9528 if (disr_type == nullptr)
9529 return;
9530
9531 /* Smash this type to be a structure type. We have to do this
9532 because the type has already been recorded. */
67607e24 9533 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9534
9c6a1327 9535 /* Make space for the discriminant field. */
ceacbf6e 9536 struct field *disr_field = &disr_type->field (0);
9c6a1327 9537 field *new_fields
1f704f76 9538 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9539 * sizeof (struct field)));
80fc5e77 9540 memcpy (new_fields + 1, type->fields (),
1f704f76 9541 type->num_fields () * sizeof (struct field));
3cabb6b0 9542 type->set_fields (new_fields);
1f704f76 9543 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9544
9545 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9546 type->field (0) = *disr_field;
9c6a1327
TT
9547 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9548 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9549
9550 /* We need a way to find the correct discriminant given a
9551 variant name. For convenience we build a map here. */
9552 struct type *enum_type = FIELD_TYPE (*disr_field);
9553 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9554 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9555 {
9556 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9557 {
9558 const char *name
9559 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9560 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9561 }
9562 }
9563
1f704f76 9564 int n_fields = type->num_fields ();
9c6a1327
TT
9565 /* We don't need a range entry for the discriminant, but we do
9566 need one for every other field, as there is no default
9567 variant. */
9568 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9569 discriminant_range,
9570 n_fields - 1);
c9317f21
TT
9571 /* Skip the discriminant here. */
9572 for (int i = 1; i < n_fields; ++i)
9573 {
9574 /* Find the final word in the name of this variant's type.
9575 That name can be used to look up the correct
9576 discriminant. */
9577 const char *variant_name
7d93a1e0 9578 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9579
9580 auto iter = discriminant_map.find (variant_name);
9581 if (iter != discriminant_map.end ())
9c6a1327
TT
9582 {
9583 ranges[i].low = iter->second;
9584 ranges[i].high = iter->second;
9585 }
c9317f21 9586
bedda9ac 9587 /* Remove the discriminant field, if it exists. */
9c6a1327 9588 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9589 if (sub_type->num_fields () > 0)
bedda9ac 9590 {
5e33d5f4 9591 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9592 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9593 }
9c6a1327 9594 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9595 sub_type->set_name
9596 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9597 type->name (), variant_name));
c9317f21 9598 }
9c6a1327
TT
9599
9600 /* Indicate that this is a variant type. */
9601 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9602 gdb::array_view<discriminant_range> (ranges,
9603 n_fields - 1));
c9317f21
TT
9604 }
9605}
9606
9607/* Rewrite some Rust unions to be structures with variants parts. */
9608
9609static void
9610rust_union_quirks (struct dwarf2_cu *cu)
9611{
9612 gdb_assert (cu->language == language_rust);
52941706 9613 for (type *type_ : cu->rust_unions)
5e22e966 9614 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9615 /* We don't need this any more. */
9616 cu->rust_unions.clear ();
c9317f21
TT
9617}
9618
95554aad
TT
9619/* A helper function for computing the list of all symbol tables
9620 included by PER_CU. */
9621
9622static void
4c39bc03 9623recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9624 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9625 struct dwarf2_per_cu_data *per_cu,
43f3e411 9626 struct compunit_symtab *immediate_parent)
95554aad 9627{
af758d11 9628 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9629 if (*slot != NULL)
9630 {
9631 /* This inclusion and its children have been processed. */
9632 return;
9633 }
9634
9635 *slot = per_cu;
af758d11 9636
95554aad 9637 /* Only add a CU if it has a symbol table. */
af758d11 9638 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
43f3e411 9639 if (cust != NULL)
ec94af83
DE
9640 {
9641 /* If this is a type unit only add its symbol table if we haven't
9642 seen it yet (type unit per_cu's can share symtabs). */
9643 if (per_cu->is_debug_types)
9644 {
43f3e411 9645 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9646 if (*slot == NULL)
9647 {
43f3e411 9648 *slot = cust;
4c39bc03 9649 result->push_back (cust);
43f3e411
DE
9650 if (cust->user == NULL)
9651 cust->user = immediate_parent;
ec94af83
DE
9652 }
9653 }
9654 else
f9125b6c 9655 {
4c39bc03 9656 result->push_back (cust);
43f3e411
DE
9657 if (cust->user == NULL)
9658 cust->user = immediate_parent;
f9125b6c 9659 }
ec94af83 9660 }
95554aad 9661
ae640021
AB
9662 if (!per_cu->imported_symtabs_empty ())
9663 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9664 {
9665 recursively_compute_inclusions (result, all_children,
9666 all_type_symtabs, ptr, cust);
9667 }
95554aad
TT
9668}
9669
43f3e411 9670/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9671 PER_CU. */
9672
9673static void
43f3e411 9674compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9675{
f4dc4d17
DE
9676 gdb_assert (! per_cu->is_debug_types);
9677
ae640021 9678 if (!per_cu->imported_symtabs_empty ())
95554aad 9679 {
ae640021 9680 int len;
4c39bc03 9681 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9682 htab_t all_children, all_type_symtabs;
af758d11 9683 compunit_symtab *cust = per_cu->dwarf2_per_objfile->get_symtab (per_cu);
95554aad
TT
9684
9685 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9686 if (cust == NULL)
95554aad
TT
9687 return;
9688
9689 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9690 NULL, xcalloc, xfree);
ec94af83
DE
9691 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9692 NULL, xcalloc, xfree);
95554aad 9693
ae640021 9694 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9695 {
9696 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9697 all_type_symtabs, ptr, cust);
ec94af83 9698 }
95554aad 9699
ec94af83 9700 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9701 len = result_symtabs.size ();
43f3e411 9702 cust->includes
ed2dc618 9703 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9704 struct compunit_symtab *, len + 1);
4c39bc03
TT
9705 memcpy (cust->includes, result_symtabs.data (),
9706 len * sizeof (compunit_symtab *));
43f3e411 9707 cust->includes[len] = NULL;
95554aad 9708
95554aad 9709 htab_delete (all_children);
ec94af83 9710 htab_delete (all_type_symtabs);
95554aad
TT
9711 }
9712}
9713
9714/* Compute the 'includes' field for the symtabs of all the CUs we just
9715 read. */
9716
9717static void
ed2dc618 9718process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9719{
5989a64e 9720 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9721 {
9722 if (! iter->is_debug_types)
43f3e411 9723 compute_compunit_symtab_includes (iter);
f4dc4d17 9724 }
95554aad 9725
5989a64e 9726 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9727}
9728
9cdd5dbd 9729/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9730 already been loaded into memory. */
9731
9732static void
95554aad
TT
9733process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9734 enum language pretend_language)
10b3939b 9735{
10b3939b 9736 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9737 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9738 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9739 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9740 CORE_ADDR lowpc, highpc;
43f3e411 9741 struct compunit_symtab *cust;
10b3939b 9742 CORE_ADDR baseaddr;
4359dff1 9743 struct block *static_block;
3e29f34a 9744 CORE_ADDR addr;
10b3939b 9745
b3b3bada 9746 baseaddr = objfile->text_section_offset ();
10b3939b 9747
c89b44cd
TT
9748 /* Clear the list here in case something was left over. */
9749 cu->method_list.clear ();
10b3939b 9750
95554aad
TT
9751 cu->language = pretend_language;
9752 cu->language_defn = language_def (cu->language);
9753
c906108c 9754 /* Do line number decoding in read_file_scope () */
10b3939b 9755 process_die (cu->dies, cu);
c906108c 9756
a766d390
DE
9757 /* For now fudge the Go package. */
9758 if (cu->language == language_go)
9759 fixup_go_packaging (cu);
9760
5f48f8f3 9761 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9762 should be complete, and it should now be safe to compute all of the
9763 physnames. */
9764 compute_delayed_physnames (cu);
3da10d80 9765
c9317f21
TT
9766 if (cu->language == language_rust)
9767 rust_union_quirks (cu);
9768
fae299cd
DC
9769 /* Some compilers don't define a DW_AT_high_pc attribute for the
9770 compilation unit. If the DW_AT_high_pc is missing, synthesize
9771 it, by scanning the DIE's below the compilation unit. */
10b3939b 9772 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9773
3e29f34a 9774 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9775 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9776
9777 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9778 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9779 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9780 addrmap to help ensure it has an accurate map of pc values belonging to
9781 this comp unit. */
9782 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9783
c24bdb02 9784 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9785 SECT_OFF_TEXT (objfile),
9786 0);
c906108c 9787
43f3e411 9788 if (cust != NULL)
c906108c 9789 {
df15bd07 9790 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9791
8be455d7
JK
9792 /* Set symtab language to language from DW_AT_language. If the
9793 compilation is from a C file generated by language preprocessors, do
9794 not set the language if it was already deduced by start_subfile. */
43f3e411 9795 if (!(cu->language == language_c
40e3ad0e 9796 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9797 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9798
9799 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9800 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9801 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9802 there were bugs in prologue debug info, fixed later in GCC-4.5
9803 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9804
9805 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9806 needed, it would be wrong due to missing DW_AT_producer there.
9807
9808 Still one can confuse GDB by using non-standard GCC compilation
9809 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9810 */
ab260dad 9811 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9812 cust->locations_valid = 1;
e0d00bc7
JK
9813
9814 if (gcc_4_minor >= 5)
43f3e411 9815 cust->epilogue_unwind_valid = 1;
96408a79 9816
43f3e411 9817 cust->call_site_htab = cu->call_site_htab;
c906108c 9818 }
9291a0cd 9819
af758d11 9820 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9821
95554aad 9822 /* Push it for inclusion processing later. */
5989a64e 9823 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9824
9825 /* Not needed any more. */
c24bdb02 9826 cu->reset_builder ();
f4dc4d17 9827}
45cfd468 9828
f4dc4d17
DE
9829/* Generate full symbol information for type unit PER_CU, whose DIEs have
9830 already been loaded into memory. */
9831
9832static void
9833process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9834 enum language pretend_language)
9835{
9836 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9837 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9838 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9839 struct compunit_symtab *cust;
0186c6a7
DE
9840 struct signatured_type *sig_type;
9841
9842 gdb_assert (per_cu->is_debug_types);
9843 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9844
c89b44cd
TT
9845 /* Clear the list here in case something was left over. */
9846 cu->method_list.clear ();
f4dc4d17 9847
f4dc4d17
DE
9848 cu->language = pretend_language;
9849 cu->language_defn = language_def (cu->language);
9850
9851 /* The symbol tables are set up in read_type_unit_scope. */
9852 process_die (cu->dies, cu);
9853
9854 /* For now fudge the Go package. */
9855 if (cu->language == language_go)
9856 fixup_go_packaging (cu);
9857
5f48f8f3 9858 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9859 should be complete, and it should now be safe to compute all of the
9860 physnames. */
9861 compute_delayed_physnames (cu);
f4dc4d17 9862
c9317f21
TT
9863 if (cu->language == language_rust)
9864 rust_union_quirks (cu);
9865
f4dc4d17
DE
9866 /* TUs share symbol tables.
9867 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9868 of it with end_expandable_symtab. Otherwise, complete the addition of
9869 this TU's symbols to the existing symtab. */
43f3e411 9870 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9871 {
c24bdb02
KS
9872 buildsym_compunit *builder = cu->get_builder ();
9873 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9874 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9875
43f3e411 9876 if (cust != NULL)
f4dc4d17
DE
9877 {
9878 /* Set symtab language to language from DW_AT_language. If the
9879 compilation is from a C file generated by language preprocessors,
9880 do not set the language if it was already deduced by
9881 start_subfile. */
43f3e411
DE
9882 if (!(cu->language == language_c
9883 && COMPUNIT_FILETABS (cust)->language != language_c))
9884 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9885 }
9886 }
9887 else
9888 {
c24bdb02 9889 cu->get_builder ()->augment_type_symtab ();
43f3e411 9890 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9891 }
9892
af758d11 9893 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9894
9895 /* Not needed any more. */
c24bdb02 9896 cu->reset_builder ();
c906108c
SS
9897}
9898
95554aad
TT
9899/* Process an imported unit DIE. */
9900
9901static void
9902process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9903{
9904 struct attribute *attr;
9905
f4dc4d17
DE
9906 /* For now we don't handle imported units in type units. */
9907 if (cu->per_cu->is_debug_types)
9908 {
9909 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9910 " supported in type units [in module %s]"),
5e22e966 9911 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9912 }
9913
95554aad
TT
9914 attr = dwarf2_attr (die, DW_AT_import, cu);
9915 if (attr != NULL)
9916 {
0826b30a 9917 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9918 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9919 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9920 dwarf2_per_cu_data *per_cu
ab432490 9921 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9922
58990295
TV
9923 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9924 into another compilation unit, at root level. Regard this as a hint,
9925 and ignore it. */
9926 if (die->parent && die->parent->parent == NULL
9927 && per_cu->unit_type == DW_UT_compile
9928 && per_cu->lang == language_cplus)
9929 return;
9930
69d751e3 9931 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9932 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 9933 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9934
ae640021 9935 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9936 }
9937}
9938
4c8aa72d
PA
9939/* RAII object that represents a process_die scope: i.e.,
9940 starts/finishes processing a DIE. */
9941class process_die_scope
adde2bff 9942{
4c8aa72d
PA
9943public:
9944 process_die_scope (die_info *die, dwarf2_cu *cu)
9945 : m_die (die), m_cu (cu)
9946 {
9947 /* We should only be processing DIEs not already in process. */
9948 gdb_assert (!m_die->in_process);
9949 m_die->in_process = true;
9950 }
8c3cb9fa 9951
4c8aa72d
PA
9952 ~process_die_scope ()
9953 {
9954 m_die->in_process = false;
9955
9956 /* If we're done processing the DIE for the CU that owns the line
9957 header, we don't need the line header anymore. */
9958 if (m_cu->line_header_die_owner == m_die)
9959 {
9960 delete m_cu->line_header;
9961 m_cu->line_header = NULL;
9962 m_cu->line_header_die_owner = NULL;
9963 }
9964 }
9965
9966private:
9967 die_info *m_die;
9968 dwarf2_cu *m_cu;
9969};
adde2bff 9970
c906108c
SS
9971/* Process a die and its children. */
9972
9973static void
e7c27a73 9974process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9975{
4c8aa72d 9976 process_die_scope scope (die, cu);
adde2bff 9977
c906108c
SS
9978 switch (die->tag)
9979 {
9980 case DW_TAG_padding:
9981 break;
9982 case DW_TAG_compile_unit:
95554aad 9983 case DW_TAG_partial_unit:
e7c27a73 9984 read_file_scope (die, cu);
c906108c 9985 break;
348e048f
DE
9986 case DW_TAG_type_unit:
9987 read_type_unit_scope (die, cu);
9988 break;
c906108c 9989 case DW_TAG_subprogram:
0a4b0913
AB
9990 /* Nested subprograms in Fortran get a prefix. */
9991 if (cu->language == language_fortran
9992 && die->parent != NULL
9993 && die->parent->tag == DW_TAG_subprogram)
9994 cu->processing_has_namespace_info = true;
9995 /* Fall through. */
c906108c 9996 case DW_TAG_inlined_subroutine:
edb3359d 9997 read_func_scope (die, cu);
c906108c
SS
9998 break;
9999 case DW_TAG_lexical_block:
14898363
L
10000 case DW_TAG_try_block:
10001 case DW_TAG_catch_block:
e7c27a73 10002 read_lexical_block_scope (die, cu);
c906108c 10003 break;
216f72a1 10004 case DW_TAG_call_site:
96408a79
SA
10005 case DW_TAG_GNU_call_site:
10006 read_call_site_scope (die, cu);
10007 break;
c906108c 10008 case DW_TAG_class_type:
680b30c7 10009 case DW_TAG_interface_type:
c906108c
SS
10010 case DW_TAG_structure_type:
10011 case DW_TAG_union_type:
134d01f1 10012 process_structure_scope (die, cu);
c906108c
SS
10013 break;
10014 case DW_TAG_enumeration_type:
134d01f1 10015 process_enumeration_scope (die, cu);
c906108c 10016 break;
134d01f1 10017
f792889a
DJ
10018 /* These dies have a type, but processing them does not create
10019 a symbol or recurse to process the children. Therefore we can
10020 read them on-demand through read_type_die. */
c906108c 10021 case DW_TAG_subroutine_type:
72019c9c 10022 case DW_TAG_set_type:
c906108c 10023 case DW_TAG_array_type:
c906108c 10024 case DW_TAG_pointer_type:
c906108c 10025 case DW_TAG_ptr_to_member_type:
c906108c 10026 case DW_TAG_reference_type:
4297a3f0 10027 case DW_TAG_rvalue_reference_type:
c906108c 10028 case DW_TAG_string_type:
c906108c 10029 break;
134d01f1 10030
c906108c 10031 case DW_TAG_base_type:
a02abb62 10032 case DW_TAG_subrange_type:
cb249c71 10033 case DW_TAG_typedef:
134d01f1
DJ
10034 /* Add a typedef symbol for the type definition, if it has a
10035 DW_AT_name. */
f792889a 10036 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10037 break;
c906108c 10038 case DW_TAG_common_block:
e7c27a73 10039 read_common_block (die, cu);
c906108c
SS
10040 break;
10041 case DW_TAG_common_inclusion:
10042 break;
d9fa45fe 10043 case DW_TAG_namespace:
9068261f 10044 cu->processing_has_namespace_info = true;
e7c27a73 10045 read_namespace (die, cu);
d9fa45fe 10046 break;
5d7cb8df 10047 case DW_TAG_module:
9068261f 10048 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10049 read_module (die, cu);
10050 break;
d9fa45fe 10051 case DW_TAG_imported_declaration:
9068261f 10052 cu->processing_has_namespace_info = true;
74921315
KS
10053 if (read_namespace_alias (die, cu))
10054 break;
86a73007
TT
10055 /* The declaration is not a global namespace alias. */
10056 /* Fall through. */
d9fa45fe 10057 case DW_TAG_imported_module:
9068261f 10058 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10059 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10060 || cu->language != language_fortran))
b98664d3 10061 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10062 dwarf_tag_name (die->tag));
10063 read_import_statement (die, cu);
d9fa45fe 10064 break;
95554aad
TT
10065
10066 case DW_TAG_imported_unit:
10067 process_imported_unit_die (die, cu);
10068 break;
10069
71a3c369
TT
10070 case DW_TAG_variable:
10071 read_variable (die, cu);
10072 break;
10073
c906108c 10074 default:
e7c27a73 10075 new_symbol (die, NULL, cu);
c906108c
SS
10076 break;
10077 }
10078}
ca69b9e6
DE
10079\f
10080/* DWARF name computation. */
c906108c 10081
94af9270
KS
10082/* A helper function for dwarf2_compute_name which determines whether DIE
10083 needs to have the name of the scope prepended to the name listed in the
10084 die. */
10085
10086static int
10087die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10088{
1c809c68
TT
10089 struct attribute *attr;
10090
94af9270
KS
10091 switch (die->tag)
10092 {
10093 case DW_TAG_namespace:
10094 case DW_TAG_typedef:
10095 case DW_TAG_class_type:
10096 case DW_TAG_interface_type:
10097 case DW_TAG_structure_type:
10098 case DW_TAG_union_type:
10099 case DW_TAG_enumeration_type:
10100 case DW_TAG_enumerator:
10101 case DW_TAG_subprogram:
08a76f8a 10102 case DW_TAG_inlined_subroutine:
94af9270 10103 case DW_TAG_member:
74921315 10104 case DW_TAG_imported_declaration:
94af9270
KS
10105 return 1;
10106
10107 case DW_TAG_variable:
c2b0a229 10108 case DW_TAG_constant:
94af9270
KS
10109 /* We only need to prefix "globally" visible variables. These include
10110 any variable marked with DW_AT_external or any variable that
10111 lives in a namespace. [Variables in anonymous namespaces
10112 require prefixing, but they are not DW_AT_external.] */
10113
10114 if (dwarf2_attr (die, DW_AT_specification, cu))
10115 {
10116 struct dwarf2_cu *spec_cu = cu;
9a619af0 10117
94af9270
KS
10118 return die_needs_namespace (die_specification (die, &spec_cu),
10119 spec_cu);
10120 }
10121
1c809c68 10122 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10123 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10124 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10125 return 0;
10126 /* A variable in a lexical block of some kind does not need a
10127 namespace, even though in C++ such variables may be external
10128 and have a mangled name. */
10129 if (die->parent->tag == DW_TAG_lexical_block
10130 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10131 || die->parent->tag == DW_TAG_catch_block
10132 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10133 return 0;
10134 return 1;
94af9270
KS
10135
10136 default:
10137 return 0;
10138 }
10139}
10140
73b9be8b
KS
10141/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10142 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10143 defined for the given DIE. */
10144
10145static struct attribute *
10146dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10147{
10148 struct attribute *attr;
10149
10150 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10151 if (attr == NULL)
10152 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10153
10154 return attr;
10155}
10156
10157/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10158 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10159 defined for the given DIE. */
10160
10161static const char *
10162dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10163{
10164 const char *linkage_name;
10165
10166 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10167 if (linkage_name == NULL)
10168 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10169
787de330
TT
10170 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10171 See https://github.com/rust-lang/rust/issues/32925. */
10172 if (cu->language == language_rust && linkage_name != NULL
10173 && strchr (linkage_name, '{') != NULL)
10174 linkage_name = NULL;
10175
73b9be8b
KS
10176 return linkage_name;
10177}
10178
94af9270 10179/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10180 compute the physname for the object, which include a method's:
9c37b5ae 10181 - formal parameters (C++),
a766d390 10182 - receiver type (Go),
a766d390
DE
10183
10184 The term "physname" is a bit confusing.
10185 For C++, for example, it is the demangled name.
10186 For Go, for example, it's the mangled name.
94af9270 10187
af6b7be1
JB
10188 For Ada, return the DIE's linkage name rather than the fully qualified
10189 name. PHYSNAME is ignored..
10190
5989a64e 10191 The result is allocated on the objfile->per_bfd's obstack and
45940949 10192 canonicalized. */
94af9270
KS
10193
10194static const char *
15d034d0
TT
10195dwarf2_compute_name (const char *name,
10196 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10197 int physname)
10198{
5e22e966 10199 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10200
94af9270
KS
10201 if (name == NULL)
10202 name = dwarf2_name (die, cu);
10203
2ee7123e
DE
10204 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10205 but otherwise compute it by typename_concat inside GDB.
10206 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10207 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10208 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10209 will set the demangled name to the result of dwarf2_full_name, and it is
10210 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10211 if (cu->language == language_ada
10212 || (cu->language == language_fortran && physname))
10213 {
10214 /* For Ada unit, we prefer the linkage name over the name, as
10215 the former contains the exported name, which the user expects
10216 to be able to reference. Ideally, we want the user to be able
10217 to reference this entity using either natural or linkage name,
10218 but we haven't started looking at this enhancement yet. */
73b9be8b 10219 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10220
2ee7123e
DE
10221 if (linkage_name != NULL)
10222 return linkage_name;
f55ee35c
JK
10223 }
10224
94af9270
KS
10225 /* These are the only languages we know how to qualify names in. */
10226 if (name != NULL
9c37b5ae 10227 && (cu->language == language_cplus
c44af4eb
TT
10228 || cu->language == language_fortran || cu->language == language_d
10229 || cu->language == language_rust))
94af9270
KS
10230 {
10231 if (die_needs_namespace (die, cu))
10232 {
0d5cff50 10233 const char *prefix;
34a68019 10234 const char *canonical_name = NULL;
94af9270 10235
d7e74731
PA
10236 string_file buf;
10237
94af9270 10238 prefix = determine_prefix (die, cu);
94af9270
KS
10239 if (*prefix != '\0')
10240 {
43816ebc
TT
10241 gdb::unique_xmalloc_ptr<char> prefixed_name
10242 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10243
43816ebc 10244 buf.puts (prefixed_name.get ());
94af9270
KS
10245 }
10246 else
d7e74731 10247 buf.puts (name);
94af9270 10248
98bfdba5
PA
10249 /* Template parameters may be specified in the DIE's DW_AT_name, or
10250 as children with DW_TAG_template_type_param or
10251 DW_TAG_value_type_param. If the latter, add them to the name
10252 here. If the name already has template parameters, then
10253 skip this step; some versions of GCC emit both, and
10254 it is more efficient to use the pre-computed name.
10255
10256 Something to keep in mind about this process: it is very
10257 unlikely, or in some cases downright impossible, to produce
10258 something that will match the mangled name of a function.
10259 If the definition of the function has the same debug info,
10260 we should be able to match up with it anyway. But fallbacks
10261 using the minimal symbol, for instance to find a method
10262 implemented in a stripped copy of libstdc++, will not work.
10263 If we do not have debug info for the definition, we will have to
10264 match them up some other way.
10265
10266 When we do name matching there is a related problem with function
10267 templates; two instantiated function templates are allowed to
10268 differ only by their return types, which we do not add here. */
10269
10270 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10271 {
10272 struct attribute *attr;
10273 struct die_info *child;
10274 int first = 1;
10275
10276 die->building_fullname = 1;
10277
10278 for (child = die->child; child != NULL; child = child->sibling)
10279 {
10280 struct type *type;
12df843f 10281 LONGEST value;
d521ce57 10282 const gdb_byte *bytes;
98bfdba5
PA
10283 struct dwarf2_locexpr_baton *baton;
10284 struct value *v;
10285
10286 if (child->tag != DW_TAG_template_type_param
10287 && child->tag != DW_TAG_template_value_param)
10288 continue;
10289
10290 if (first)
10291 {
d7e74731 10292 buf.puts ("<");
98bfdba5
PA
10293 first = 0;
10294 }
10295 else
d7e74731 10296 buf.puts (", ");
98bfdba5
PA
10297
10298 attr = dwarf2_attr (child, DW_AT_type, cu);
10299 if (attr == NULL)
10300 {
b98664d3 10301 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10302 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10303 continue;
10304 }
10305 type = die_type (child, cu);
10306
10307 if (child->tag == DW_TAG_template_type_param)
10308 {
c1ec8cea
TT
10309 c_print_type (type, "", &buf, -1, 0, cu->language,
10310 &type_print_raw_options);
98bfdba5
PA
10311 continue;
10312 }
10313
10314 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10315 if (attr == NULL)
10316 {
b98664d3 10317 complaint (_("template parameter missing "
3e43a32a 10318 "DW_AT_const_value"));
d7e74731 10319 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10320 continue;
10321 }
10322
10323 dwarf2_const_value_attr (attr, type, name,
10324 &cu->comp_unit_obstack, cu,
10325 &value, &bytes, &baton);
10326
10327 if (TYPE_NOSIGN (type))
10328 /* GDB prints characters as NUMBER 'CHAR'. If that's
10329 changed, this can use value_print instead. */
d7e74731 10330 c_printchar (value, type, &buf);
98bfdba5
PA
10331 else
10332 {
10333 struct value_print_options opts;
10334
10335 if (baton != NULL)
10336 v = dwarf2_evaluate_loc_desc (type, NULL,
10337 baton->data,
10338 baton->size,
10339 baton->per_cu);
10340 else if (bytes != NULL)
10341 {
10342 v = allocate_value (type);
10343 memcpy (value_contents_writeable (v), bytes,
10344 TYPE_LENGTH (type));
10345 }
10346 else
10347 v = value_from_longest (type, value);
10348
3e43a32a
MS
10349 /* Specify decimal so that we do not depend on
10350 the radix. */
98bfdba5
PA
10351 get_formatted_print_options (&opts, 'd');
10352 opts.raw = 1;
d7e74731 10353 value_print (v, &buf, &opts);
98bfdba5 10354 release_value (v);
98bfdba5
PA
10355 }
10356 }
10357
10358 die->building_fullname = 0;
10359
10360 if (!first)
10361 {
10362 /* Close the argument list, with a space if necessary
10363 (nested templates). */
d7e74731
PA
10364 if (!buf.empty () && buf.string ().back () == '>')
10365 buf.puts (" >");
98bfdba5 10366 else
d7e74731 10367 buf.puts (">");
98bfdba5
PA
10368 }
10369 }
10370
9c37b5ae 10371 /* For C++ methods, append formal parameter type
94af9270 10372 information, if PHYSNAME. */
6e70227d 10373
94af9270 10374 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10375 && cu->language == language_cplus)
94af9270
KS
10376 {
10377 struct type *type = read_type_die (die, cu);
10378
d7e74731 10379 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10380 &type_print_raw_options);
94af9270 10381
9c37b5ae 10382 if (cu->language == language_cplus)
94af9270 10383 {
60430eff
DJ
10384 /* Assume that an artificial first parameter is
10385 "this", but do not crash if it is not. RealView
10386 marks unnamed (and thus unused) parameters as
10387 artificial; there is no way to differentiate
10388 the two cases. */
1f704f76 10389 if (type->num_fields () > 0
94af9270 10390 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10391 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10392 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10393 0))))
d7e74731 10394 buf.puts (" const");
94af9270
KS
10395 }
10396 }
10397
d7e74731 10398 const std::string &intermediate_name = buf.string ();
94af9270
KS
10399
10400 if (cu->language == language_cplus)
34a68019 10401 canonical_name
322a8516 10402 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10403 objfile);
34a68019
TT
10404
10405 /* If we only computed INTERMEDIATE_NAME, or if
10406 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10407 intern it. */
322a8516 10408 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10409 name = objfile->intern (intermediate_name);
34a68019
TT
10410 else
10411 name = canonical_name;
94af9270
KS
10412 }
10413 }
10414
10415 return name;
10416}
10417
0114d602
DJ
10418/* Return the fully qualified name of DIE, based on its DW_AT_name.
10419 If scope qualifiers are appropriate they will be added. The result
34a68019 10420 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10421 not have a name. NAME may either be from a previous call to
10422 dwarf2_name or NULL.
10423
9c37b5ae 10424 The output string will be canonicalized (if C++). */
0114d602
DJ
10425
10426static const char *
15d034d0 10427dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10428{
94af9270
KS
10429 return dwarf2_compute_name (name, die, cu, 0);
10430}
0114d602 10431
94af9270
KS
10432/* Construct a physname for the given DIE in CU. NAME may either be
10433 from a previous call to dwarf2_name or NULL. The result will be
10434 allocated on the objfile_objstack or NULL if the DIE does not have a
10435 name.
0114d602 10436
9c37b5ae 10437 The output string will be canonicalized (if C++). */
0114d602 10438
94af9270 10439static const char *
15d034d0 10440dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10441{
5e22e966 10442 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10443 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10444 int need_copy = 1;
10445
10446 /* In this case dwarf2_compute_name is just a shortcut not building anything
10447 on its own. */
10448 if (!die_needs_namespace (die, cu))
10449 return dwarf2_compute_name (name, die, cu, 1);
10450
906bb4c5
TT
10451 if (cu->language != language_rust)
10452 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10453
10454 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10455 has computed. */
791afaa2 10456 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10457 if (mangled != NULL)
900e11f9 10458 {
900e11f9 10459
59cc4834
JB
10460 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10461 {
10462 /* Do nothing (do not demangle the symbol name). */
10463 }
10464 else if (cu->language == language_go)
a766d390 10465 {
5e2db402
TT
10466 /* This is a lie, but we already lie to the caller new_symbol.
10467 new_symbol assumes we return the mangled name.
a766d390 10468 This just undoes that lie until things are cleaned up. */
a766d390
DE
10469 }
10470 else
10471 {
0eb876f5
JB
10472 /* Use DMGL_RET_DROP for C++ template functions to suppress
10473 their return type. It is easier for GDB users to search
10474 for such functions as `name(params)' than `long name(params)'.
10475 In such case the minimal symbol names do not match the full
10476 symbol names but for template functions there is never a need
10477 to look up their definition from their declaration so
10478 the only disadvantage remains the minimal symbol variant
10479 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10480 demangled.reset (gdb_demangle (mangled,
10481 (DMGL_PARAMS | DMGL_ANSI
10482 | DMGL_RET_DROP)));
a766d390 10483 }
900e11f9 10484 if (demangled)
791afaa2 10485 canon = demangled.get ();
900e11f9
JK
10486 else
10487 {
10488 canon = mangled;
10489 need_copy = 0;
10490 }
10491 }
10492
10493 if (canon == NULL || check_physname)
10494 {
10495 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10496
10497 if (canon != NULL && strcmp (physname, canon) != 0)
10498 {
10499 /* It may not mean a bug in GDB. The compiler could also
10500 compute DW_AT_linkage_name incorrectly. But in such case
10501 GDB would need to be bug-to-bug compatible. */
10502
b98664d3 10503 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10504 "(from linkage <%s>) - DIE at %s [in module %s]"),
10505 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10506 objfile_name (objfile));
900e11f9
JK
10507
10508 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10509 is available here - over computed PHYSNAME. It is safer
10510 against both buggy GDB and buggy compilers. */
10511
10512 retval = canon;
10513 }
10514 else
10515 {
10516 retval = physname;
10517 need_copy = 0;
10518 }
10519 }
10520 else
10521 retval = canon;
10522
10523 if (need_copy)
be1e3d3e 10524 retval = objfile->intern (retval);
900e11f9 10525
900e11f9 10526 return retval;
0114d602
DJ
10527}
10528
74921315
KS
10529/* Inspect DIE in CU for a namespace alias. If one exists, record
10530 a new symbol for it.
10531
10532 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10533
10534static int
10535read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10536{
10537 struct attribute *attr;
10538
10539 /* If the die does not have a name, this is not a namespace
10540 alias. */
10541 attr = dwarf2_attr (die, DW_AT_name, cu);
10542 if (attr != NULL)
10543 {
10544 int num;
10545 struct die_info *d = die;
10546 struct dwarf2_cu *imported_cu = cu;
10547
10548 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10549 keep inspecting DIEs until we hit the underlying import. */
10550#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10551 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10552 {
10553 attr = dwarf2_attr (d, DW_AT_import, cu);
10554 if (attr == NULL)
10555 break;
10556
10557 d = follow_die_ref (d, attr, &imported_cu);
10558 if (d->tag != DW_TAG_imported_declaration)
10559 break;
10560 }
10561
10562 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10563 {
b98664d3 10564 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10565 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10566 return 0;
10567 }
10568
10569 if (attr != NULL)
10570 {
10571 struct type *type;
0826b30a 10572 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10573
9c541725 10574 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10575 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10576 {
10577 /* This declaration is a global namespace alias. Add
10578 a symbol for it whose type is the aliased namespace. */
10579 new_symbol (die, type, cu);
10580 return 1;
10581 }
10582 }
10583 }
10584
10585 return 0;
10586}
10587
22cee43f 10588/* Return the using directives repository (global or local?) to use in the
804d2729 10589 current context for CU.
22cee43f
PMR
10590
10591 For Ada, imported declarations can materialize renamings, which *may* be
10592 global. However it is impossible (for now?) in DWARF to distinguish
10593 "external" imported declarations and "static" ones. As all imported
10594 declarations seem to be static in all other languages, make them all CU-wide
10595 global only in Ada. */
10596
10597static struct using_direct **
804d2729 10598using_directives (struct dwarf2_cu *cu)
22cee43f 10599{
c24bdb02
KS
10600 if (cu->language == language_ada
10601 && cu->get_builder ()->outermost_context_p ())
10602 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10603 else
c24bdb02 10604 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10605}
10606
27aa8d6a
SW
10607/* Read the import statement specified by the given die and record it. */
10608
10609static void
10610read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10611{
5e22e966 10612 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10613 struct attribute *import_attr;
32019081 10614 struct die_info *imported_die, *child_die;
de4affc9 10615 struct dwarf2_cu *imported_cu;
27aa8d6a 10616 const char *imported_name;
794684b6 10617 const char *imported_name_prefix;
13387711
SW
10618 const char *canonical_name;
10619 const char *import_alias;
10620 const char *imported_declaration = NULL;
794684b6 10621 const char *import_prefix;
eb1e02fd 10622 std::vector<const char *> excludes;
13387711 10623
27aa8d6a
SW
10624 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10625 if (import_attr == NULL)
10626 {
b98664d3 10627 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10628 dwarf_tag_name (die->tag));
10629 return;
10630 }
10631
de4affc9
CC
10632 imported_cu = cu;
10633 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10634 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10635 if (imported_name == NULL)
10636 {
10637 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10638
10639 The import in the following code:
10640 namespace A
10641 {
10642 typedef int B;
10643 }
10644
10645 int main ()
10646 {
10647 using A::B;
10648 B b;
10649 return b;
10650 }
10651
10652 ...
10653 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10654 <52> DW_AT_decl_file : 1
10655 <53> DW_AT_decl_line : 6
10656 <54> DW_AT_import : <0x75>
10657 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10658 <59> DW_AT_name : B
10659 <5b> DW_AT_decl_file : 1
10660 <5c> DW_AT_decl_line : 2
10661 <5d> DW_AT_type : <0x6e>
10662 ...
10663 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10664 <76> DW_AT_byte_size : 4
10665 <77> DW_AT_encoding : 5 (signed)
10666
10667 imports the wrong die ( 0x75 instead of 0x58 ).
10668 This case will be ignored until the gcc bug is fixed. */
10669 return;
10670 }
10671
82856980
SW
10672 /* Figure out the local name after import. */
10673 import_alias = dwarf2_name (die, cu);
27aa8d6a 10674
794684b6
SW
10675 /* Figure out where the statement is being imported to. */
10676 import_prefix = determine_prefix (die, cu);
10677
10678 /* Figure out what the scope of the imported die is and prepend it
10679 to the name of the imported die. */
de4affc9 10680 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10681
f55ee35c
JK
10682 if (imported_die->tag != DW_TAG_namespace
10683 && imported_die->tag != DW_TAG_module)
794684b6 10684 {
13387711
SW
10685 imported_declaration = imported_name;
10686 canonical_name = imported_name_prefix;
794684b6 10687 }
13387711 10688 else if (strlen (imported_name_prefix) > 0)
12aaed36 10689 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10690 imported_name_prefix,
10691 (cu->language == language_d ? "." : "::"),
10692 imported_name, (char *) NULL);
13387711
SW
10693 else
10694 canonical_name = imported_name;
794684b6 10695
32019081
JK
10696 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10697 for (child_die = die->child; child_die && child_die->tag;
436c571c 10698 child_die = child_die->sibling)
32019081
JK
10699 {
10700 /* DWARF-4: A Fortran use statement with a “rename list” may be
10701 represented by an imported module entry with an import attribute
10702 referring to the module and owned entries corresponding to those
10703 entities that are renamed as part of being imported. */
10704
10705 if (child_die->tag != DW_TAG_imported_declaration)
10706 {
b98664d3 10707 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10708 "- DIE at %s [in module %s]"),
10709 sect_offset_str (child_die->sect_off),
10710 objfile_name (objfile));
32019081
JK
10711 continue;
10712 }
10713
10714 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10715 if (import_attr == NULL)
10716 {
b98664d3 10717 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10718 dwarf_tag_name (child_die->tag));
10719 continue;
10720 }
10721
10722 imported_cu = cu;
10723 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10724 &imported_cu);
10725 imported_name = dwarf2_name (imported_die, imported_cu);
10726 if (imported_name == NULL)
10727 {
b98664d3 10728 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10729 "imported name - DIE at %s [in module %s]"),
10730 sect_offset_str (child_die->sect_off),
10731 objfile_name (objfile));
32019081
JK
10732 continue;
10733 }
10734
eb1e02fd 10735 excludes.push_back (imported_name);
32019081
JK
10736
10737 process_die (child_die, cu);
10738 }
10739
804d2729 10740 add_using_directive (using_directives (cu),
22cee43f
PMR
10741 import_prefix,
10742 canonical_name,
10743 import_alias,
10744 imported_declaration,
10745 excludes,
10746 0,
10747 &objfile->objfile_obstack);
27aa8d6a
SW
10748}
10749
5230b05a
WT
10750/* ICC<14 does not output the required DW_AT_declaration on incomplete
10751 types, but gives them a size of zero. Starting with version 14,
10752 ICC is compatible with GCC. */
10753
9068261f 10754static bool
5230b05a
WT
10755producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10756{
10757 if (!cu->checked_producer)
10758 check_producer (cu);
10759
10760 return cu->producer_is_icc_lt_14;
10761}
10762
eb77c9df
AB
10763/* ICC generates a DW_AT_type for C void functions. This was observed on
10764 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10765 which says that void functions should not have a DW_AT_type. */
10766
10767static bool
10768producer_is_icc (struct dwarf2_cu *cu)
10769{
10770 if (!cu->checked_producer)
10771 check_producer (cu);
10772
10773 return cu->producer_is_icc;
10774}
10775
1b80a9fa
JK
10776/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10777 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10778 this, it was first present in GCC release 4.3.0. */
10779
9068261f 10780static bool
1b80a9fa
JK
10781producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10782{
10783 if (!cu->checked_producer)
10784 check_producer (cu);
10785
10786 return cu->producer_is_gcc_lt_4_3;
10787}
10788
d721ba37
PA
10789static file_and_directory
10790find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10791{
d721ba37
PA
10792 file_and_directory res;
10793
9291a0cd
TT
10794 /* Find the filename. Do not use dwarf2_name here, since the filename
10795 is not a source language identifier. */
d721ba37
PA
10796 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10797 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10798
d721ba37
PA
10799 if (res.comp_dir == NULL
10800 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10801 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10802 {
d721ba37
PA
10803 res.comp_dir_storage = ldirname (res.name);
10804 if (!res.comp_dir_storage.empty ())
10805 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10806 }
d721ba37 10807 if (res.comp_dir != NULL)
9291a0cd
TT
10808 {
10809 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10810 directory, get rid of it. */
d721ba37 10811 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10812
d721ba37
PA
10813 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10814 res.comp_dir = cp + 1;
9291a0cd
TT
10815 }
10816
d721ba37
PA
10817 if (res.name == NULL)
10818 res.name = "<unknown>";
10819
10820 return res;
9291a0cd
TT
10821}
10822
f4dc4d17
DE
10823/* Handle DW_AT_stmt_list for a compilation unit.
10824 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10825 COMP_DIR is the compilation directory. LOWPC is passed to
10826 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10827
10828static void
10829handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10830 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10831{
5e22e966 10832 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10833 struct attribute *attr;
527f3840
JK
10834 struct line_header line_header_local;
10835 hashval_t line_header_local_hash;
527f3840
JK
10836 void **slot;
10837 int decode_mapping;
2ab95328 10838
f4dc4d17
DE
10839 gdb_assert (! cu->per_cu->is_debug_types);
10840
2ab95328 10841 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10842 if (attr == NULL)
10843 return;
10844
9c541725 10845 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10846
10847 /* The line header hash table is only created if needed (it exists to
10848 prevent redundant reading of the line table for partial_units).
10849 If we're given a partial_unit, we'll need it. If we're given a
10850 compile_unit, then use the line header hash table if it's already
10851 created, but don't create one just yet. */
10852
5989a64e 10853 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10854 && die->tag == DW_TAG_partial_unit)
2ab95328 10855 {
5989a64e 10856 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10857 .reset (htab_create_alloc (127, line_header_hash_voidp,
10858 line_header_eq_voidp,
10859 free_line_header_voidp,
10860 xcalloc, xfree));
527f3840 10861 }
2ab95328 10862
9c541725 10863 line_header_local.sect_off = line_offset;
527f3840
JK
10864 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10865 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10866 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10867 {
5989a64e 10868 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10869 &line_header_local,
10870 line_header_local_hash, NO_INSERT);
10871
10872 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10873 is not present in *SLOT (since if there is something in *SLOT then
10874 it will be for a partial_unit). */
10875 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10876 {
527f3840 10877 gdb_assert (*slot != NULL);
9a3c8263 10878 cu->line_header = (struct line_header *) *slot;
527f3840 10879 return;
dee91e82 10880 }
2ab95328 10881 }
527f3840
JK
10882
10883 /* dwarf_decode_line_header does not yet provide sufficient information.
10884 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10885 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10886 if (lh == NULL)
527f3840 10887 return;
4c8aa72d
PA
10888
10889 cu->line_header = lh.release ();
10890 cu->line_header_die_owner = die;
527f3840 10891
5989a64e 10892 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10893 slot = NULL;
10894 else
10895 {
5989a64e 10896 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10897 &line_header_local,
10898 line_header_local_hash, INSERT);
10899 gdb_assert (slot != NULL);
10900 }
10901 if (slot != NULL && *slot == NULL)
10902 {
10903 /* This newly decoded line number information unit will be owned
10904 by line_header_hash hash table. */
10905 *slot = cu->line_header;
4c8aa72d 10906 cu->line_header_die_owner = NULL;
527f3840
JK
10907 }
10908 else
10909 {
10910 /* We cannot free any current entry in (*slot) as that struct line_header
10911 may be already used by multiple CUs. Create only temporary decoded
10912 line_header for this CU - it may happen at most once for each line
10913 number information unit. And if we're not using line_header_hash
10914 then this is what we want as well. */
10915 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10916 }
10917 decode_mapping = (die->tag != DW_TAG_partial_unit);
10918 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10919 decode_mapping);
fff8551c 10920
2ab95328
TT
10921}
10922
95554aad 10923/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10924
c906108c 10925static void
e7c27a73 10926read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10927{
5e22e966 10928 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10929 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10930 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10931 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10932 CORE_ADDR highpc = ((CORE_ADDR) 0);
10933 struct attribute *attr;
c906108c 10934 struct die_info *child_die;
e142c38c 10935 CORE_ADDR baseaddr;
6e70227d 10936
380618d6 10937 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10938 baseaddr = objfile->text_section_offset ();
c906108c 10939
fae299cd 10940 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10941
10942 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10943 from finish_block. */
2acceee2 10944 if (lowpc == ((CORE_ADDR) -1))
c906108c 10945 lowpc = highpc;
3e29f34a 10946 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10947
d721ba37 10948 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10949
f4b8a18d
KW
10950 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10951 standardised yet. As a workaround for the language detection we fall
10952 back to the DW_AT_producer string. */
10953 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10954 cu->language = language_opencl;
10955
3019eac3
DE
10956 /* Similar hack for Go. */
10957 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10958 set_cu_language (DW_LANG_Go, cu);
10959
c24bdb02 10960 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10961
10962 /* Decode line number information if present. We do this before
10963 processing child DIEs, so that the line header table is available
10964 for DW_AT_decl_file. */
d721ba37 10965 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10966
10967 /* Process all dies in compilation unit. */
10968 if (die->child != NULL)
10969 {
10970 child_die = die->child;
10971 while (child_die && child_die->tag)
10972 {
10973 process_die (child_die, cu);
436c571c 10974 child_die = child_die->sibling;
3019eac3
DE
10975 }
10976 }
10977
10978 /* Decode macro information, if present. Dwarf 2 macro information
10979 refers to information in the line number info statement program
10980 header, so we can only read it if we've read the header
10981 successfully. */
0af92d60
JK
10982 attr = dwarf2_attr (die, DW_AT_macros, cu);
10983 if (attr == NULL)
10984 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10985 if (attr && cu->line_header)
10986 {
10987 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10988 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10989
43f3e411 10990 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10991 }
10992 else
10993 {
10994 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10995 if (attr && cu->line_header)
10996 {
10997 unsigned int macro_offset = DW_UNSND (attr);
10998
43f3e411 10999 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11000 }
11001 }
3019eac3
DE
11002}
11003
c24bdb02
KS
11004void
11005dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11006{
f4dc4d17
DE
11007 struct type_unit_group *tu_group;
11008 int first_time;
3019eac3 11009 struct attribute *attr;
9c541725 11010 unsigned int i;
0186c6a7 11011 struct signatured_type *sig_type;
3019eac3 11012
f4dc4d17 11013 gdb_assert (per_cu->is_debug_types);
0186c6a7 11014 sig_type = (struct signatured_type *) per_cu;
3019eac3 11015
c24bdb02 11016 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11017
f4dc4d17 11018 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11019 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11020 if (sig_type->type_unit_group == NULL)
c24bdb02 11021 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11022 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11023
11024 /* If we've already processed this stmt_list there's no real need to
11025 do it again, we could fake it and just recreate the part we need
11026 (file name,index -> symtab mapping). If data shows this optimization
11027 is useful we can do it then. */
43f3e411 11028 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11029
11030 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11031 debug info. */
fff8551c 11032 line_header_up lh;
f4dc4d17 11033 if (attr != NULL)
3019eac3 11034 {
9c541725 11035 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11036 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11037 }
11038 if (lh == NULL)
11039 {
11040 if (first_time)
c24bdb02 11041 start_symtab ("", NULL, 0);
f4dc4d17
DE
11042 else
11043 {
11044 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11045 gdb_assert (m_builder == nullptr);
804d2729 11046 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11047 m_builder.reset (new struct buildsym_compunit
11048 (COMPUNIT_OBJFILE (cust), "",
11049 COMPUNIT_DIRNAME (cust),
11050 compunit_language (cust),
11051 0, cust));
770479f2 11052 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11053 }
f4dc4d17 11054 return;
3019eac3
DE
11055 }
11056
c24bdb02
KS
11057 line_header = lh.release ();
11058 line_header_die_owner = die;
3019eac3 11059
f4dc4d17
DE
11060 if (first_time)
11061 {
c24bdb02 11062 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11063
1fd60fc0
DE
11064 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11065 still initializing it, and our caller (a few levels up)
11066 process_full_type_unit still needs to know if this is the first
11067 time. */
11068
4ac93832
TT
11069 tu_group->symtabs
11070 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11071 struct symtab *, line_header->file_names_size ());
3019eac3 11072
7ba99d21
AT
11073 auto &file_names = line_header->file_names ();
11074 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11075 {
7ba99d21 11076 file_entry &fe = file_names[i];
c24bdb02
KS
11077 dwarf2_start_subfile (this, fe.name,
11078 fe.include_dir (line_header));
11079 buildsym_compunit *b = get_builder ();
11080 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11081 {
4c8aa72d
PA
11082 /* NOTE: start_subfile will recognize when it's been
11083 passed a file it has already seen. So we can't
11084 assume there's a simple mapping from
11085 cu->line_header->file_names to subfiles, plus
11086 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11087 b->get_current_subfile ()->symtab
11088 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11089 }
11090
c24bdb02 11091 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11092 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11093 }
11094 }
11095 else
3019eac3 11096 {
c24bdb02 11097 gdb_assert (m_builder == nullptr);
804d2729 11098 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11099 m_builder.reset (new struct buildsym_compunit
11100 (COMPUNIT_OBJFILE (cust), "",
11101 COMPUNIT_DIRNAME (cust),
11102 compunit_language (cust),
11103 0, cust));
770479f2 11104 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11105
7ba99d21
AT
11106 auto &file_names = line_header->file_names ();
11107 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11108 {
7ba99d21 11109 file_entry &fe = file_names[i];
4c8aa72d 11110 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11111 }
3019eac3
DE
11112 }
11113
f4dc4d17
DE
11114 /* The main symtab is allocated last. Type units don't have DW_AT_name
11115 so they don't have a "real" (so to speak) symtab anyway.
11116 There is later code that will assign the main symtab to all symbols
11117 that don't have one. We need to handle the case of a symbol with a
11118 missing symtab (DW_AT_decl_file) anyway. */
11119}
3019eac3 11120
f4dc4d17
DE
11121/* Process DW_TAG_type_unit.
11122 For TUs we want to skip the first top level sibling if it's not the
11123 actual type being defined by this TU. In this case the first top
11124 level sibling is there to provide context only. */
3019eac3 11125
f4dc4d17
DE
11126static void
11127read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11128{
11129 struct die_info *child_die;
3019eac3 11130
f4dc4d17
DE
11131 prepare_one_comp_unit (cu, die, language_minimal);
11132
11133 /* Initialize (or reinitialize) the machinery for building symtabs.
11134 We do this before processing child DIEs, so that the line header table
11135 is available for DW_AT_decl_file. */
c24bdb02 11136 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11137
11138 if (die->child != NULL)
11139 {
11140 child_die = die->child;
11141 while (child_die && child_die->tag)
11142 {
11143 process_die (child_die, cu);
436c571c 11144 child_die = child_die->sibling;
f4dc4d17
DE
11145 }
11146 }
3019eac3
DE
11147}
11148\f
80626a55
DE
11149/* DWO/DWP files.
11150
11151 http://gcc.gnu.org/wiki/DebugFission
11152 http://gcc.gnu.org/wiki/DebugFissionDWP
11153
11154 To simplify handling of both DWO files ("object" files with the DWARF info)
11155 and DWP files (a file with the DWOs packaged up into one file), we treat
11156 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11157
11158static hashval_t
11159hash_dwo_file (const void *item)
11160{
9a3c8263 11161 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11162 hashval_t hash;
3019eac3 11163
a2ce51a0
DE
11164 hash = htab_hash_string (dwo_file->dwo_name);
11165 if (dwo_file->comp_dir != NULL)
11166 hash += htab_hash_string (dwo_file->comp_dir);
11167 return hash;
3019eac3
DE
11168}
11169
11170static int
11171eq_dwo_file (const void *item_lhs, const void *item_rhs)
11172{
9a3c8263
SM
11173 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11174 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11175
a2ce51a0
DE
11176 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11177 return 0;
11178 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11179 return lhs->comp_dir == rhs->comp_dir;
11180 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11181}
11182
11183/* Allocate a hash table for DWO files. */
11184
51ac9db5 11185static htab_up
298e9637 11186allocate_dwo_file_hash_table ()
3019eac3 11187{
51ac9db5
SM
11188 auto delete_dwo_file = [] (void *item)
11189 {
11190 struct dwo_file *dwo_file = (struct dwo_file *) item;
11191
11192 delete dwo_file;
11193 };
11194
bc68fb19
TT
11195 return htab_up (htab_create_alloc (41,
11196 hash_dwo_file,
11197 eq_dwo_file,
11198 delete_dwo_file,
11199 xcalloc, xfree));
3019eac3
DE
11200}
11201
80626a55
DE
11202/* Lookup DWO file DWO_NAME. */
11203
11204static void **
ed2dc618
SM
11205lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11206 const char *dwo_name,
11207 const char *comp_dir)
80626a55
DE
11208{
11209 struct dwo_file find_entry;
11210 void **slot;
11211
5989a64e
SM
11212 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11213 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11214
0ac5b59e
DE
11215 find_entry.dwo_name = dwo_name;
11216 find_entry.comp_dir = comp_dir;
5989a64e 11217 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11218 INSERT);
80626a55
DE
11219
11220 return slot;
11221}
11222
3019eac3
DE
11223static hashval_t
11224hash_dwo_unit (const void *item)
11225{
9a3c8263 11226 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11227
11228 /* This drops the top 32 bits of the id, but is ok for a hash. */
11229 return dwo_unit->signature;
11230}
11231
11232static int
11233eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11234{
9a3c8263
SM
11235 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11236 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11237
11238 /* The signature is assumed to be unique within the DWO file.
11239 So while object file CU dwo_id's always have the value zero,
11240 that's OK, assuming each object file DWO file has only one CU,
11241 and that's the rule for now. */
11242 return lhs->signature == rhs->signature;
11243}
11244
11245/* Allocate a hash table for DWO CUs,TUs.
11246 There is one of these tables for each of CUs,TUs for each DWO file. */
11247
b0b6a987 11248static htab_up
298e9637 11249allocate_dwo_unit_table ()
3019eac3
DE
11250{
11251 /* Start out with a pretty small number.
11252 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11253 return htab_up (htab_create_alloc (3,
11254 hash_dwo_unit,
11255 eq_dwo_unit,
11256 NULL, xcalloc, xfree));
3019eac3
DE
11257}
11258
19c3d4c9 11259/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11260
11261static void
19c3d4c9
DE
11262create_dwo_cu_reader (const struct die_reader_specs *reader,
11263 const gdb_byte *info_ptr,
11264 struct die_info *comp_unit_die,
c0ab21c2
TT
11265 struct dwo_file *dwo_file,
11266 struct dwo_unit *dwo_unit)
3019eac3
DE
11267{
11268 struct dwarf2_cu *cu = reader->cu;
9c541725 11269 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11270 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11271
a084a2a6
AT
11272 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11273 if (!signature.has_value ())
3019eac3 11274 {
b98664d3 11275 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11276 " its dwo_id [in module %s]"),
9d8780f0 11277 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11278 return;
11279 }
11280
3019eac3 11281 dwo_unit->dwo_file = dwo_file;
a084a2a6 11282 dwo_unit->signature = *signature;
8a0459fd 11283 dwo_unit->section = section;
9c541725 11284 dwo_unit->sect_off = sect_off;
3019eac3
DE
11285 dwo_unit->length = cu->per_cu->length;
11286
b4f54984 11287 if (dwarf_read_debug)
9d8780f0
SM
11288 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11289 sect_offset_str (sect_off),
9c541725 11290 hex_string (dwo_unit->signature));
3019eac3
DE
11291}
11292
33c5cd75 11293/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11294 Note: This function processes DWO files only, not DWP files. */
3019eac3 11295
33c5cd75 11296static void
ed2dc618 11297create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11298 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11299 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11300{
11301 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11302 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11303 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11304
96b79293 11305 section.read (objfile);
33c5cd75 11306 info_ptr = section.buffer;
3019eac3
DE
11307
11308 if (info_ptr == NULL)
33c5cd75 11309 return;
3019eac3 11310
b4f54984 11311 if (dwarf_read_debug)
19c3d4c9
DE
11312 {
11313 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11314 section.get_name (),
11315 section.get_file_name ());
19c3d4c9 11316 }
3019eac3 11317
33c5cd75 11318 end_ptr = info_ptr + section.size;
3019eac3
DE
11319 while (info_ptr < end_ptr)
11320 {
11321 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11322 struct dwo_unit read_unit {};
33c5cd75
DB
11323 struct dwo_unit *dwo_unit;
11324 void **slot;
11325 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11326
11327 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11328 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 11329 per_cu.per_bfd = per_bfd;
3019eac3 11330 per_cu.is_debug_types = 0;
33c5cd75
DB
11331 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11332 per_cu.section = &section;
11333
ab432490 11334 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11335 if (!reader.dummy_p)
11336 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11337 &dwo_file, &read_unit);
33c5cd75
DB
11338 info_ptr += per_cu.length;
11339
11340 // If the unit could not be parsed, skip it.
c0ab21c2 11341 if (read_unit.dwo_file == NULL)
33c5cd75 11342 continue;
3019eac3 11343
33c5cd75 11344 if (cus_htab == NULL)
298e9637 11345 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11346
1859c670 11347 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11348 struct dwo_unit);
c0ab21c2 11349 *dwo_unit = read_unit;
b0b6a987 11350 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11351 gdb_assert (slot != NULL);
11352 if (*slot != NULL)
19c3d4c9 11353 {
33c5cd75
DB
11354 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11355 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11356
b98664d3 11357 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11358 " the entry at offset %s, signature %s"),
11359 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11360 hex_string (dwo_unit->signature));
19c3d4c9 11361 }
33c5cd75 11362 *slot = (void *)dwo_unit;
3019eac3 11363 }
3019eac3
DE
11364}
11365
80626a55
DE
11366/* DWP file .debug_{cu,tu}_index section format:
11367 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11368
d2415c6c
DE
11369 DWP Version 1:
11370
80626a55
DE
11371 Both index sections have the same format, and serve to map a 64-bit
11372 signature to a set of section numbers. Each section begins with a header,
11373 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11374 indexes, and a pool of 32-bit section numbers. The index sections will be
11375 aligned at 8-byte boundaries in the file.
11376
d2415c6c
DE
11377 The index section header consists of:
11378
11379 V, 32 bit version number
11380 -, 32 bits unused
11381 N, 32 bit number of compilation units or type units in the index
11382 M, 32 bit number of slots in the hash table
80626a55 11383
d2415c6c 11384 Numbers are recorded using the byte order of the application binary.
80626a55 11385
d2415c6c
DE
11386 The hash table begins at offset 16 in the section, and consists of an array
11387 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11388 order of the application binary). Unused slots in the hash table are 0.
11389 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11390
d2415c6c
DE
11391 The parallel table begins immediately after the hash table
11392 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11393 array of 32-bit indexes (using the byte order of the application binary),
11394 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11395 table contains a 32-bit index into the pool of section numbers. For unused
11396 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11397
73869dc2
DE
11398 The pool of section numbers begins immediately following the hash table
11399 (at offset 16 + 12 * M from the beginning of the section). The pool of
11400 section numbers consists of an array of 32-bit words (using the byte order
11401 of the application binary). Each item in the array is indexed starting
11402 from 0. The hash table entry provides the index of the first section
11403 number in the set. Additional section numbers in the set follow, and the
11404 set is terminated by a 0 entry (section number 0 is not used in ELF).
11405
11406 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11407 section must be the first entry in the set, and the .debug_abbrev.dwo must
11408 be the second entry. Other members of the set may follow in any order.
11409
11410 ---
11411
11412 DWP Version 2:
11413
11414 DWP Version 2 combines all the .debug_info, etc. sections into one,
11415 and the entries in the index tables are now offsets into these sections.
11416 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11417 section.
11418
11419 Index Section Contents:
11420 Header
11421 Hash Table of Signatures dwp_hash_table.hash_table
11422 Parallel Table of Indices dwp_hash_table.unit_table
11423 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11424 Table of Section Sizes dwp_hash_table.v2.sizes
11425
11426 The index section header consists of:
11427
11428 V, 32 bit version number
11429 L, 32 bit number of columns in the table of section offsets
11430 N, 32 bit number of compilation units or type units in the index
11431 M, 32 bit number of slots in the hash table
11432
11433 Numbers are recorded using the byte order of the application binary.
11434
11435 The hash table has the same format as version 1.
11436 The parallel table of indices has the same format as version 1,
11437 except that the entries are origin-1 indices into the table of sections
11438 offsets and the table of section sizes.
11439
11440 The table of offsets begins immediately following the parallel table
11441 (at offset 16 + 12 * M from the beginning of the section). The table is
11442 a two-dimensional array of 32-bit words (using the byte order of the
11443 application binary), with L columns and N+1 rows, in row-major order.
11444 Each row in the array is indexed starting from 0. The first row provides
11445 a key to the remaining rows: each column in this row provides an identifier
11446 for a debug section, and the offsets in the same column of subsequent rows
11447 refer to that section. The section identifiers are:
11448
11449 DW_SECT_INFO 1 .debug_info.dwo
11450 DW_SECT_TYPES 2 .debug_types.dwo
11451 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11452 DW_SECT_LINE 4 .debug_line.dwo
11453 DW_SECT_LOC 5 .debug_loc.dwo
11454 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11455 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11456 DW_SECT_MACRO 8 .debug_macro.dwo
11457
11458 The offsets provided by the CU and TU index sections are the base offsets
11459 for the contributions made by each CU or TU to the corresponding section
11460 in the package file. Each CU and TU header contains an abbrev_offset
11461 field, used to find the abbreviations table for that CU or TU within the
11462 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11463 be interpreted as relative to the base offset given in the index section.
11464 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11465 should be interpreted as relative to the base offset for .debug_line.dwo,
11466 and offsets into other debug sections obtained from DWARF attributes should
11467 also be interpreted as relative to the corresponding base offset.
11468
11469 The table of sizes begins immediately following the table of offsets.
11470 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11471 with L columns and N rows, in row-major order. Each row in the array is
11472 indexed starting from 1 (row 0 is shared by the two tables).
11473
11474 ---
11475
11476 Hash table lookup is handled the same in version 1 and 2:
11477
11478 We assume that N and M will not exceed 2^32 - 1.
11479 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11480
d2415c6c
DE
11481 Given a 64-bit compilation unit signature or a type signature S, an entry
11482 in the hash table is located as follows:
80626a55 11483
d2415c6c
DE
11484 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11485 the low-order k bits all set to 1.
80626a55 11486
d2415c6c 11487 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11488
d2415c6c
DE
11489 3) If the hash table entry at index H matches the signature, use that
11490 entry. If the hash table entry at index H is unused (all zeroes),
11491 terminate the search: the signature is not present in the table.
80626a55 11492
d2415c6c 11493 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11494
d2415c6c 11495 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11496 to stop at an unused slot or find the match. */
80626a55
DE
11497
11498/* Create a hash table to map DWO IDs to their CU/TU entry in
11499 .debug_{info,types}.dwo in DWP_FILE.
11500 Returns NULL if there isn't one.
11501 Note: This function processes DWP files only, not DWO files. */
11502
11503static struct dwp_hash_table *
ed2dc618
SM
11504create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11505 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11506{
11507 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11508 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11509 const gdb_byte *index_ptr, *index_end;
80626a55 11510 struct dwarf2_section_info *index;
73869dc2 11511 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11512 struct dwp_hash_table *htab;
11513
11514 if (is_debug_types)
11515 index = &dwp_file->sections.tu_index;
11516 else
11517 index = &dwp_file->sections.cu_index;
11518
96b79293 11519 if (index->empty ())
80626a55 11520 return NULL;
96b79293 11521 index->read (objfile);
80626a55
DE
11522
11523 index_ptr = index->buffer;
11524 index_end = index_ptr + index->size;
11525
11526 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11527 index_ptr += 4;
11528 if (version == 2)
11529 nr_columns = read_4_bytes (dbfd, index_ptr);
11530 else
11531 nr_columns = 0;
11532 index_ptr += 4;
80626a55
DE
11533 nr_units = read_4_bytes (dbfd, index_ptr);
11534 index_ptr += 4;
11535 nr_slots = read_4_bytes (dbfd, index_ptr);
11536 index_ptr += 4;
11537
73869dc2 11538 if (version != 1 && version != 2)
80626a55 11539 {
21aa081e 11540 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11541 " [in module %s]"),
21aa081e 11542 pulongest (version), dwp_file->name);
80626a55
DE
11543 }
11544 if (nr_slots != (nr_slots & -nr_slots))
11545 {
21aa081e 11546 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11547 " is not power of 2 [in module %s]"),
21aa081e 11548 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11549 }
11550
5989a64e 11551 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11552 htab->version = version;
11553 htab->nr_columns = nr_columns;
80626a55
DE
11554 htab->nr_units = nr_units;
11555 htab->nr_slots = nr_slots;
11556 htab->hash_table = index_ptr;
11557 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11558
11559 /* Exit early if the table is empty. */
11560 if (nr_slots == 0 || nr_units == 0
11561 || (version == 2 && nr_columns == 0))
11562 {
11563 /* All must be zero. */
11564 if (nr_slots != 0 || nr_units != 0
11565 || (version == 2 && nr_columns != 0))
11566 {
b98664d3 11567 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11568 " all zero [in modules %s]"),
11569 dwp_file->name);
11570 }
11571 return htab;
11572 }
11573
11574 if (version == 1)
11575 {
11576 htab->section_pool.v1.indices =
11577 htab->unit_table + sizeof (uint32_t) * nr_slots;
11578 /* It's harder to decide whether the section is too small in v1.
11579 V1 is deprecated anyway so we punt. */
11580 }
11581 else
11582 {
11583 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11584 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11585 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11586 /* Reverse map for error checking. */
11587 int ids_seen[DW_SECT_MAX + 1];
11588 int i;
11589
11590 if (nr_columns < 2)
11591 {
11592 error (_("Dwarf Error: bad DWP hash table, too few columns"
11593 " in section table [in module %s]"),
11594 dwp_file->name);
11595 }
11596 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, too many columns"
11599 " in section table [in module %s]"),
11600 dwp_file->name);
11601 }
04fd5eed
GB
11602 memset (ids, 255, sizeof_ids);
11603 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11604 for (i = 0; i < nr_columns; ++i)
11605 {
11606 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11607
11608 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11609 {
11610 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11611 " in section table [in module %s]"),
11612 id, dwp_file->name);
11613 }
11614 if (ids_seen[id] != -1)
11615 {
11616 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11617 " id %d in section table [in module %s]"),
11618 id, dwp_file->name);
11619 }
11620 ids_seen[id] = i;
11621 ids[i] = id;
11622 }
11623 /* Must have exactly one info or types section. */
11624 if (((ids_seen[DW_SECT_INFO] != -1)
11625 + (ids_seen[DW_SECT_TYPES] != -1))
11626 != 1)
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11629 " DWO info/types section [in module %s]"),
11630 dwp_file->name);
11631 }
11632 /* Must have an abbrev section. */
11633 if (ids_seen[DW_SECT_ABBREV] == -1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11636 " section [in module %s]"),
11637 dwp_file->name);
11638 }
11639 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11640 htab->section_pool.v2.sizes =
11641 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11642 * nr_units * nr_columns);
11643 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11644 * nr_units * nr_columns))
11645 > index_end)
11646 {
11647 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11648 " [in module %s]"),
11649 dwp_file->name);
11650 }
11651 }
80626a55
DE
11652
11653 return htab;
11654}
11655
11656/* Update SECTIONS with the data from SECTP.
11657
11658 This function is like the other "locate" section routines that are
11659 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11660 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11661
11662 The result is non-zero for success, or zero if an error was found. */
11663
11664static int
73869dc2
DE
11665locate_v1_virtual_dwo_sections (asection *sectp,
11666 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11667{
11668 const struct dwop_section_names *names = &dwop_section_names;
11669
11670 if (section_is_p (sectp->name, &names->abbrev_dwo))
11671 {
11672 /* There can be only one. */
049412e3 11673 if (sections->abbrev.s.section != NULL)
80626a55 11674 return 0;
049412e3 11675 sections->abbrev.s.section = sectp;
fd361982 11676 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11677 }
11678 else if (section_is_p (sectp->name, &names->info_dwo)
11679 || section_is_p (sectp->name, &names->types_dwo))
11680 {
11681 /* There can be only one. */
049412e3 11682 if (sections->info_or_types.s.section != NULL)
80626a55 11683 return 0;
049412e3 11684 sections->info_or_types.s.section = sectp;
fd361982 11685 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11686 }
11687 else if (section_is_p (sectp->name, &names->line_dwo))
11688 {
11689 /* There can be only one. */
049412e3 11690 if (sections->line.s.section != NULL)
80626a55 11691 return 0;
049412e3 11692 sections->line.s.section = sectp;
fd361982 11693 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11694 }
11695 else if (section_is_p (sectp->name, &names->loc_dwo))
11696 {
11697 /* There can be only one. */
049412e3 11698 if (sections->loc.s.section != NULL)
80626a55 11699 return 0;
049412e3 11700 sections->loc.s.section = sectp;
fd361982 11701 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11702 }
11703 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11704 {
11705 /* There can be only one. */
049412e3 11706 if (sections->macinfo.s.section != NULL)
80626a55 11707 return 0;
049412e3 11708 sections->macinfo.s.section = sectp;
fd361982 11709 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11710 }
11711 else if (section_is_p (sectp->name, &names->macro_dwo))
11712 {
11713 /* There can be only one. */
049412e3 11714 if (sections->macro.s.section != NULL)
80626a55 11715 return 0;
049412e3 11716 sections->macro.s.section = sectp;
fd361982 11717 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11718 }
11719 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11720 {
11721 /* There can be only one. */
049412e3 11722 if (sections->str_offsets.s.section != NULL)
80626a55 11723 return 0;
049412e3 11724 sections->str_offsets.s.section = sectp;
fd361982 11725 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11726 }
11727 else
11728 {
11729 /* No other kind of section is valid. */
11730 return 0;
11731 }
11732
11733 return 1;
11734}
11735
73869dc2
DE
11736/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11737 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11738 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11739 This is for DWP version 1 files. */
80626a55
DE
11740
11741static struct dwo_unit *
ed2dc618
SM
11742create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11743 struct dwp_file *dwp_file,
73869dc2
DE
11744 uint32_t unit_index,
11745 const char *comp_dir,
11746 ULONGEST signature, int is_debug_types)
80626a55 11747{
73869dc2
DE
11748 const struct dwp_hash_table *dwp_htab =
11749 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11750 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11751 const char *kind = is_debug_types ? "TU" : "CU";
11752 struct dwo_file *dwo_file;
11753 struct dwo_unit *dwo_unit;
73869dc2 11754 struct virtual_v1_dwo_sections sections;
80626a55 11755 void **dwo_file_slot;
80626a55
DE
11756 int i;
11757
73869dc2
DE
11758 gdb_assert (dwp_file->version == 1);
11759
b4f54984 11760 if (dwarf_read_debug)
80626a55 11761 {
73869dc2 11762 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11763 kind,
73869dc2 11764 pulongest (unit_index), hex_string (signature),
80626a55
DE
11765 dwp_file->name);
11766 }
11767
19ac8c2e 11768 /* Fetch the sections of this DWO unit.
80626a55
DE
11769 Put a limit on the number of sections we look for so that bad data
11770 doesn't cause us to loop forever. */
11771
73869dc2 11772#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11773 (1 /* .debug_info or .debug_types */ \
11774 + 1 /* .debug_abbrev */ \
11775 + 1 /* .debug_line */ \
11776 + 1 /* .debug_loc */ \
11777 + 1 /* .debug_str_offsets */ \
19ac8c2e 11778 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11779 + 1 /* trailing zero */)
11780
11781 memset (&sections, 0, sizeof (sections));
80626a55 11782
73869dc2 11783 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11784 {
11785 asection *sectp;
11786 uint32_t section_nr =
11787 read_4_bytes (dbfd,
73869dc2
DE
11788 dwp_htab->section_pool.v1.indices
11789 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11790
11791 if (section_nr == 0)
11792 break;
11793 if (section_nr >= dwp_file->num_sections)
11794 {
11795 error (_("Dwarf Error: bad DWP hash table, section number too large"
11796 " [in module %s]"),
11797 dwp_file->name);
11798 }
11799
11800 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11801 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11804 " [in module %s]"),
11805 dwp_file->name);
11806 }
11807 }
11808
11809 if (i < 2
96b79293
TT
11810 || sections.info_or_types.empty ()
11811 || sections.abbrev.empty ())
80626a55
DE
11812 {
11813 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11814 " [in module %s]"),
11815 dwp_file->name);
11816 }
73869dc2 11817 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11818 {
11819 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11820 " [in module %s]"),
11821 dwp_file->name);
11822 }
11823
11824 /* It's easier for the rest of the code if we fake a struct dwo_file and
11825 have dwo_unit "live" in that. At least for now.
11826
11827 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11828 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11829 file, we can combine them back into a virtual DWO file to save space
11830 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11831 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11832
791afaa2
TT
11833 std::string virtual_dwo_name =
11834 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11835 sections.abbrev.get_id (),
11836 sections.line.get_id (),
11837 sections.loc.get_id (),
11838 sections.str_offsets.get_id ());
80626a55 11839 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11840 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11841 virtual_dwo_name.c_str (),
11842 comp_dir);
80626a55
DE
11843 /* Create one if necessary. */
11844 if (*dwo_file_slot == NULL)
11845 {
b4f54984 11846 if (dwarf_read_debug)
80626a55
DE
11847 {
11848 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11849 virtual_dwo_name.c_str ());
80626a55 11850 }
51ac9db5 11851 dwo_file = new struct dwo_file;
45940949 11852 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11853 dwo_file->comp_dir = comp_dir;
80626a55
DE
11854 dwo_file->sections.abbrev = sections.abbrev;
11855 dwo_file->sections.line = sections.line;
11856 dwo_file->sections.loc = sections.loc;
11857 dwo_file->sections.macinfo = sections.macinfo;
11858 dwo_file->sections.macro = sections.macro;
11859 dwo_file->sections.str_offsets = sections.str_offsets;
11860 /* The "str" section is global to the entire DWP file. */
11861 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11862 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11863 there's no need to record it in dwo_file.
11864 Also, we can't simply record type sections in dwo_file because
11865 we record a pointer into the vector in dwo_unit. As we collect more
11866 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11867 for it, invalidating all copies of pointers into the previous
11868 contents. */
80626a55
DE
11869 *dwo_file_slot = dwo_file;
11870 }
11871 else
11872 {
b4f54984 11873 if (dwarf_read_debug)
80626a55
DE
11874 {
11875 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11876 virtual_dwo_name.c_str ());
80626a55 11877 }
9a3c8263 11878 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11879 }
80626a55 11880
5989a64e 11881 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11882 dwo_unit->dwo_file = dwo_file;
11883 dwo_unit->signature = signature;
8d749320 11884 dwo_unit->section =
5989a64e 11885 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11886 *dwo_unit->section = sections.info_or_types;
57d63ce2 11887 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11888
11889 return dwo_unit;
11890}
11891
73869dc2
DE
11892/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11893 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11894 piece within that section used by a TU/CU, return a virtual section
11895 of just that piece. */
11896
11897static struct dwarf2_section_info
ed2dc618
SM
11898create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11899 struct dwarf2_section_info *section,
73869dc2
DE
11900 bfd_size_type offset, bfd_size_type size)
11901{
11902 struct dwarf2_section_info result;
11903 asection *sectp;
11904
11905 gdb_assert (section != NULL);
11906 gdb_assert (!section->is_virtual);
11907
11908 memset (&result, 0, sizeof (result));
11909 result.s.containing_section = section;
dc4ccb6f 11910 result.is_virtual = true;
73869dc2
DE
11911
11912 if (size == 0)
11913 return result;
11914
96b79293 11915 sectp = section->get_bfd_section ();
73869dc2
DE
11916
11917 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11918 bounds of the real section. This is a pretty-rare event, so just
11919 flag an error (easier) instead of a warning and trying to cope. */
11920 if (sectp == NULL
fd361982 11921 || offset + size > bfd_section_size (sectp))
73869dc2 11922 {
73869dc2
DE
11923 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11924 " in section %s [in module %s]"),
fd361982 11925 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11926 objfile_name (dwarf2_per_objfile->objfile));
11927 }
11928
11929 result.virtual_offset = offset;
11930 result.size = size;
11931 return result;
11932}
11933
11934/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11935 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11936 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11937 This is for DWP version 2 files. */
11938
11939static struct dwo_unit *
ed2dc618
SM
11940create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11941 struct dwp_file *dwp_file,
73869dc2
DE
11942 uint32_t unit_index,
11943 const char *comp_dir,
11944 ULONGEST signature, int is_debug_types)
11945{
73869dc2
DE
11946 const struct dwp_hash_table *dwp_htab =
11947 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11948 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11949 const char *kind = is_debug_types ? "TU" : "CU";
11950 struct dwo_file *dwo_file;
11951 struct dwo_unit *dwo_unit;
11952 struct virtual_v2_dwo_sections sections;
11953 void **dwo_file_slot;
73869dc2
DE
11954 int i;
11955
11956 gdb_assert (dwp_file->version == 2);
11957
b4f54984 11958 if (dwarf_read_debug)
73869dc2
DE
11959 {
11960 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11961 kind,
11962 pulongest (unit_index), hex_string (signature),
11963 dwp_file->name);
11964 }
11965
11966 /* Fetch the section offsets of this DWO unit. */
11967
11968 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11969
11970 for (i = 0; i < dwp_htab->nr_columns; ++i)
11971 {
11972 uint32_t offset = read_4_bytes (dbfd,
11973 dwp_htab->section_pool.v2.offsets
11974 + (((unit_index - 1) * dwp_htab->nr_columns
11975 + i)
11976 * sizeof (uint32_t)));
11977 uint32_t size = read_4_bytes (dbfd,
11978 dwp_htab->section_pool.v2.sizes
11979 + (((unit_index - 1) * dwp_htab->nr_columns
11980 + i)
11981 * sizeof (uint32_t)));
11982
11983 switch (dwp_htab->section_pool.v2.section_ids[i])
11984 {
11985 case DW_SECT_INFO:
11986 case DW_SECT_TYPES:
11987 sections.info_or_types_offset = offset;
11988 sections.info_or_types_size = size;
11989 break;
11990 case DW_SECT_ABBREV:
11991 sections.abbrev_offset = offset;
11992 sections.abbrev_size = size;
11993 break;
11994 case DW_SECT_LINE:
11995 sections.line_offset = offset;
11996 sections.line_size = size;
11997 break;
11998 case DW_SECT_LOC:
11999 sections.loc_offset = offset;
12000 sections.loc_size = size;
12001 break;
12002 case DW_SECT_STR_OFFSETS:
12003 sections.str_offsets_offset = offset;
12004 sections.str_offsets_size = size;
12005 break;
12006 case DW_SECT_MACINFO:
12007 sections.macinfo_offset = offset;
12008 sections.macinfo_size = size;
12009 break;
12010 case DW_SECT_MACRO:
12011 sections.macro_offset = offset;
12012 sections.macro_size = size;
12013 break;
12014 }
12015 }
12016
12017 /* It's easier for the rest of the code if we fake a struct dwo_file and
12018 have dwo_unit "live" in that. At least for now.
12019
12020 The DWP file can be made up of a random collection of CUs and TUs.
12021 However, for each CU + set of TUs that came from the same original DWO
12022 file, we can combine them back into a virtual DWO file to save space
12023 (fewer struct dwo_file objects to allocate). Remember that for really
12024 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12025
791afaa2
TT
12026 std::string virtual_dwo_name =
12027 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12028 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12029 (long) (sections.line_size ? sections.line_offset : 0),
12030 (long) (sections.loc_size ? sections.loc_offset : 0),
12031 (long) (sections.str_offsets_size
12032 ? sections.str_offsets_offset : 0));
73869dc2 12033 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12034 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12035 virtual_dwo_name.c_str (),
12036 comp_dir);
73869dc2
DE
12037 /* Create one if necessary. */
12038 if (*dwo_file_slot == NULL)
12039 {
b4f54984 12040 if (dwarf_read_debug)
73869dc2
DE
12041 {
12042 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12043 virtual_dwo_name.c_str ());
73869dc2 12044 }
51ac9db5 12045 dwo_file = new struct dwo_file;
45940949 12046 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12047 dwo_file->comp_dir = comp_dir;
12048 dwo_file->sections.abbrev =
ed2dc618 12049 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12050 sections.abbrev_offset, sections.abbrev_size);
12051 dwo_file->sections.line =
ed2dc618 12052 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12053 sections.line_offset, sections.line_size);
12054 dwo_file->sections.loc =
ed2dc618 12055 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12056 sections.loc_offset, sections.loc_size);
12057 dwo_file->sections.macinfo =
ed2dc618 12058 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12059 sections.macinfo_offset, sections.macinfo_size);
12060 dwo_file->sections.macro =
ed2dc618 12061 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12062 sections.macro_offset, sections.macro_size);
12063 dwo_file->sections.str_offsets =
ed2dc618
SM
12064 create_dwp_v2_section (dwarf2_per_objfile,
12065 &dwp_file->sections.str_offsets,
73869dc2
DE
12066 sections.str_offsets_offset,
12067 sections.str_offsets_size);
12068 /* The "str" section is global to the entire DWP file. */
12069 dwo_file->sections.str = dwp_file->sections.str;
12070 /* The info or types section is assigned below to dwo_unit,
12071 there's no need to record it in dwo_file.
12072 Also, we can't simply record type sections in dwo_file because
12073 we record a pointer into the vector in dwo_unit. As we collect more
12074 types we'll grow the vector and eventually have to reallocate space
12075 for it, invalidating all copies of pointers into the previous
12076 contents. */
12077 *dwo_file_slot = dwo_file;
12078 }
12079 else
12080 {
b4f54984 12081 if (dwarf_read_debug)
73869dc2
DE
12082 {
12083 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12084 virtual_dwo_name.c_str ());
73869dc2 12085 }
9a3c8263 12086 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12087 }
73869dc2 12088
5989a64e 12089 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12090 dwo_unit->dwo_file = dwo_file;
12091 dwo_unit->signature = signature;
8d749320 12092 dwo_unit->section =
5989a64e 12093 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12094 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12095 is_debug_types
73869dc2
DE
12096 ? &dwp_file->sections.types
12097 : &dwp_file->sections.info,
12098 sections.info_or_types_offset,
12099 sections.info_or_types_size);
12100 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12101
12102 return dwo_unit;
12103}
12104
57d63ce2
DE
12105/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12106 Returns NULL if the signature isn't found. */
80626a55
DE
12107
12108static struct dwo_unit *
ed2dc618
SM
12109lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12110 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12111 ULONGEST signature, int is_debug_types)
80626a55 12112{
57d63ce2
DE
12113 const struct dwp_hash_table *dwp_htab =
12114 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12115 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12116 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12117 uint32_t hash = signature & mask;
12118 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12119 unsigned int i;
12120 void **slot;
870f88f7 12121 struct dwo_unit find_dwo_cu;
80626a55
DE
12122
12123 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12124 find_dwo_cu.signature = signature;
19ac8c2e 12125 slot = htab_find_slot (is_debug_types
48b490f2
TT
12126 ? dwp_file->loaded_tus.get ()
12127 : dwp_file->loaded_cus.get (),
19ac8c2e 12128 &find_dwo_cu, INSERT);
80626a55
DE
12129
12130 if (*slot != NULL)
9a3c8263 12131 return (struct dwo_unit *) *slot;
80626a55
DE
12132
12133 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12134 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12135 {
12136 ULONGEST signature_in_table;
12137
12138 signature_in_table =
57d63ce2 12139 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12140 if (signature_in_table == signature)
12141 {
57d63ce2
DE
12142 uint32_t unit_index =
12143 read_4_bytes (dbfd,
12144 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12145
73869dc2
DE
12146 if (dwp_file->version == 1)
12147 {
ed2dc618
SM
12148 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12149 dwp_file, unit_index,
73869dc2
DE
12150 comp_dir, signature,
12151 is_debug_types);
12152 }
12153 else
12154 {
ed2dc618
SM
12155 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12156 dwp_file, unit_index,
73869dc2
DE
12157 comp_dir, signature,
12158 is_debug_types);
12159 }
9a3c8263 12160 return (struct dwo_unit *) *slot;
80626a55
DE
12161 }
12162 if (signature_in_table == 0)
12163 return NULL;
12164 hash = (hash + hash2) & mask;
12165 }
12166
12167 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12168 " [in module %s]"),
12169 dwp_file->name);
12170}
12171
ab5088bf 12172/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12173 Open the file specified by FILE_NAME and hand it off to BFD for
12174 preliminary analysis. Return a newly initialized bfd *, which
12175 includes a canonicalized copy of FILE_NAME.
80626a55 12176 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12177 SEARCH_CWD is true if the current directory is to be searched.
12178 It will be searched before debug-file-directory.
13aaf454
DE
12179 If successful, the file is added to the bfd include table of the
12180 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12181 If unable to find/open the file, return NULL.
3019eac3
DE
12182 NOTE: This function is derived from symfile_bfd_open. */
12183
192b62ce 12184static gdb_bfd_ref_ptr
ed2dc618
SM
12185try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12186 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12187{
24b9144d 12188 int desc;
9c02c129
DE
12189 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12190 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12191 to debug_file_directory. */
e0cc99a6 12192 const char *search_path;
9c02c129
DE
12193 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12194
e0cc99a6 12195 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12196 if (search_cwd)
12197 {
12198 if (*debug_file_directory != '\0')
e0cc99a6
TT
12199 {
12200 search_path_holder.reset (concat (".", dirname_separator_string,
12201 debug_file_directory,
12202 (char *) NULL));
12203 search_path = search_path_holder.get ();
12204 }
6ac97d4c 12205 else
e0cc99a6 12206 search_path = ".";
6ac97d4c 12207 }
9c02c129 12208 else
e0cc99a6 12209 search_path = debug_file_directory;
3019eac3 12210
24b9144d 12211 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12212 if (is_dwp)
12213 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12214
12215 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12216 desc = openp (search_path, flags, file_name,
3019eac3
DE
12217 O_RDONLY | O_BINARY, &absolute_name);
12218 if (desc < 0)
12219 return NULL;
12220
e0cc99a6
TT
12221 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12222 gnutarget, desc));
9c02c129
DE
12223 if (sym_bfd == NULL)
12224 return NULL;
192b62ce 12225 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12226
192b62ce
TT
12227 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12228 return NULL;
3019eac3 12229
13aaf454
DE
12230 /* Success. Record the bfd as having been included by the objfile's bfd.
12231 This is important because things like demangled_names_hash lives in the
12232 objfile's per_bfd space and may have references to things like symbol
12233 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12234 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12235
3019eac3
DE
12236 return sym_bfd;
12237}
12238
ab5088bf 12239/* Try to open DWO file FILE_NAME.
3019eac3
DE
12240 COMP_DIR is the DW_AT_comp_dir attribute.
12241 The result is the bfd handle of the file.
12242 If there is a problem finding or opening the file, return NULL.
12243 Upon success, the canonicalized path of the file is stored in the bfd,
12244 same as symfile_bfd_open. */
12245
192b62ce 12246static gdb_bfd_ref_ptr
ed2dc618
SM
12247open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12248 const char *file_name, const char *comp_dir)
3019eac3 12249{
80626a55 12250 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12251 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12252 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12253
12254 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12255
12256 if (comp_dir != NULL)
12257 {
43816ebc
TT
12258 gdb::unique_xmalloc_ptr<char> path_to_try
12259 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12260
12261 /* NOTE: If comp_dir is a relative path, this will also try the
12262 search path, which seems useful. */
ed2dc618 12263 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12264 path_to_try.get (),
ed2dc618 12265 0 /*is_dwp*/,
192b62ce 12266 1 /*search_cwd*/));
3019eac3
DE
12267 if (abfd != NULL)
12268 return abfd;
12269 }
12270
12271 /* That didn't work, try debug-file-directory, which, despite its name,
12272 is a list of paths. */
12273
12274 if (*debug_file_directory == '\0')
12275 return NULL;
12276
ed2dc618
SM
12277 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12278 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12279}
12280
80626a55
DE
12281/* This function is mapped across the sections and remembers the offset and
12282 size of each of the DWO debugging sections we are interested in. */
12283
12284static void
12285dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12286{
9a3c8263 12287 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12288 const struct dwop_section_names *names = &dwop_section_names;
12289
12290 if (section_is_p (sectp->name, &names->abbrev_dwo))
12291 {
049412e3 12292 dwo_sections->abbrev.s.section = sectp;
fd361982 12293 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12294 }
12295 else if (section_is_p (sectp->name, &names->info_dwo))
12296 {
049412e3 12297 dwo_sections->info.s.section = sectp;
fd361982 12298 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12299 }
12300 else if (section_is_p (sectp->name, &names->line_dwo))
12301 {
049412e3 12302 dwo_sections->line.s.section = sectp;
fd361982 12303 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12304 }
12305 else if (section_is_p (sectp->name, &names->loc_dwo))
12306 {
049412e3 12307 dwo_sections->loc.s.section = sectp;
fd361982 12308 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12309 }
41144253 12310 else if (section_is_p (sectp->name, &names->loclists_dwo))
12311 {
12312 dwo_sections->loclists.s.section = sectp;
12313 dwo_sections->loclists.size = bfd_section_size (sectp);
12314 }
80626a55
DE
12315 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12316 {
049412e3 12317 dwo_sections->macinfo.s.section = sectp;
fd361982 12318 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12319 }
12320 else if (section_is_p (sectp->name, &names->macro_dwo))
12321 {
049412e3 12322 dwo_sections->macro.s.section = sectp;
fd361982 12323 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12324 }
12325 else if (section_is_p (sectp->name, &names->str_dwo))
12326 {
049412e3 12327 dwo_sections->str.s.section = sectp;
fd361982 12328 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12329 }
12330 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12331 {
049412e3 12332 dwo_sections->str_offsets.s.section = sectp;
fd361982 12333 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12334 }
12335 else if (section_is_p (sectp->name, &names->types_dwo))
12336 {
12337 struct dwarf2_section_info type_section;
12338
12339 memset (&type_section, 0, sizeof (type_section));
049412e3 12340 type_section.s.section = sectp;
fd361982 12341 type_section.size = bfd_section_size (sectp);
fd5866f6 12342 dwo_sections->types.push_back (type_section);
80626a55
DE
12343 }
12344}
12345
ab5088bf 12346/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12347 by PER_CU. This is for the non-DWP case.
80626a55 12348 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12349
12350static struct dwo_file *
0ac5b59e
DE
12351open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12352 const char *dwo_name, const char *comp_dir)
3019eac3 12353{
ed2dc618 12354 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12355
fb1eb2f9 12356 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12357 if (dbfd == NULL)
12358 {
b4f54984 12359 if (dwarf_read_debug)
80626a55
DE
12360 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12361 return NULL;
12362 }
263db9a1 12363
51ac9db5 12364 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12365 dwo_file->dwo_name = dwo_name;
12366 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12367 dwo_file->dbfd = std::move (dbfd);
3019eac3 12368
fb1eb2f9 12369 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12370 &dwo_file->sections);
3019eac3 12371
18a8505e
AT
12372 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12373 dwo_file->sections.info, dwo_file->cus);
3019eac3 12374
263db9a1 12375 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12376 dwo_file->sections.types, dwo_file->tus);
3019eac3 12377
b4f54984 12378 if (dwarf_read_debug)
80626a55
DE
12379 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12380
263db9a1 12381 return dwo_file.release ();
3019eac3
DE
12382}
12383
80626a55 12384/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12385 size of each of the DWP debugging sections common to version 1 and 2 that
12386 we are interested in. */
3019eac3 12387
80626a55 12388static void
73869dc2
DE
12389dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12390 void *dwp_file_ptr)
3019eac3 12391{
9a3c8263 12392 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12393 const struct dwop_section_names *names = &dwop_section_names;
12394 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12395
80626a55 12396 /* Record the ELF section number for later lookup: this is what the
73869dc2 12397 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12398 gdb_assert (elf_section_nr < dwp_file->num_sections);
12399 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12400
80626a55
DE
12401 /* Look for specific sections that we need. */
12402 if (section_is_p (sectp->name, &names->str_dwo))
12403 {
049412e3 12404 dwp_file->sections.str.s.section = sectp;
fd361982 12405 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12406 }
12407 else if (section_is_p (sectp->name, &names->cu_index))
12408 {
049412e3 12409 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12410 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12411 }
12412 else if (section_is_p (sectp->name, &names->tu_index))
12413 {
049412e3 12414 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12415 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12416 }
12417}
3019eac3 12418
73869dc2
DE
12419/* This function is mapped across the sections and remembers the offset and
12420 size of each of the DWP version 2 debugging sections that we are interested
12421 in. This is split into a separate function because we don't know if we
12422 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12423
12424static void
12425dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12426{
9a3c8263 12427 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12428 const struct dwop_section_names *names = &dwop_section_names;
12429 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12430
12431 /* Record the ELF section number for later lookup: this is what the
12432 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12433 gdb_assert (elf_section_nr < dwp_file->num_sections);
12434 dwp_file->elf_sections[elf_section_nr] = sectp;
12435
12436 /* Look for specific sections that we need. */
12437 if (section_is_p (sectp->name, &names->abbrev_dwo))
12438 {
049412e3 12439 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12440 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12441 }
12442 else if (section_is_p (sectp->name, &names->info_dwo))
12443 {
049412e3 12444 dwp_file->sections.info.s.section = sectp;
fd361982 12445 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12446 }
12447 else if (section_is_p (sectp->name, &names->line_dwo))
12448 {
049412e3 12449 dwp_file->sections.line.s.section = sectp;
fd361982 12450 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12451 }
12452 else if (section_is_p (sectp->name, &names->loc_dwo))
12453 {
049412e3 12454 dwp_file->sections.loc.s.section = sectp;
fd361982 12455 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12456 }
12457 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12458 {
049412e3 12459 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12460 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12461 }
12462 else if (section_is_p (sectp->name, &names->macro_dwo))
12463 {
049412e3 12464 dwp_file->sections.macro.s.section = sectp;
fd361982 12465 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12466 }
12467 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12468 {
049412e3 12469 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12470 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12471 }
12472 else if (section_is_p (sectp->name, &names->types_dwo))
12473 {
049412e3 12474 dwp_file->sections.types.s.section = sectp;
fd361982 12475 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12476 }
12477}
12478
80626a55 12479/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12480
80626a55
DE
12481static hashval_t
12482hash_dwp_loaded_cutus (const void *item)
12483{
9a3c8263 12484 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12485
80626a55
DE
12486 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12487 return dwo_unit->signature;
3019eac3
DE
12488}
12489
80626a55 12490/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12491
80626a55
DE
12492static int
12493eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12494{
9a3c8263
SM
12495 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12496 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12497
80626a55
DE
12498 return dua->signature == dub->signature;
12499}
3019eac3 12500
80626a55 12501/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12502
48b490f2 12503static htab_up
298e9637 12504allocate_dwp_loaded_cutus_table ()
80626a55 12505{
48b490f2
TT
12506 return htab_up (htab_create_alloc (3,
12507 hash_dwp_loaded_cutus,
12508 eq_dwp_loaded_cutus,
12509 NULL, xcalloc, xfree));
80626a55 12510}
3019eac3 12511
ab5088bf
DE
12512/* Try to open DWP file FILE_NAME.
12513 The result is the bfd handle of the file.
12514 If there is a problem finding or opening the file, return NULL.
12515 Upon success, the canonicalized path of the file is stored in the bfd,
12516 same as symfile_bfd_open. */
12517
192b62ce 12518static gdb_bfd_ref_ptr
ed2dc618
SM
12519open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12520 const char *file_name)
ab5088bf 12521{
ed2dc618
SM
12522 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12523 1 /*is_dwp*/,
192b62ce 12524 1 /*search_cwd*/));
6ac97d4c
DE
12525 if (abfd != NULL)
12526 return abfd;
12527
12528 /* Work around upstream bug 15652.
12529 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12530 [Whether that's a "bug" is debatable, but it is getting in our way.]
12531 We have no real idea where the dwp file is, because gdb's realpath-ing
12532 of the executable's path may have discarded the needed info.
12533 [IWBN if the dwp file name was recorded in the executable, akin to
12534 .gnu_debuglink, but that doesn't exist yet.]
12535 Strip the directory from FILE_NAME and search again. */
12536 if (*debug_file_directory != '\0')
12537 {
12538 /* Don't implicitly search the current directory here.
12539 If the user wants to search "." to handle this case,
12540 it must be added to debug-file-directory. */
ed2dc618
SM
12541 return try_open_dwop_file (dwarf2_per_objfile,
12542 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12543 0 /*search_cwd*/);
12544 }
12545
12546 return NULL;
ab5088bf
DE
12547}
12548
80626a55
DE
12549/* Initialize the use of the DWP file for the current objfile.
12550 By convention the name of the DWP file is ${objfile}.dwp.
12551 The result is NULL if it can't be found. */
a766d390 12552
400174b1 12553static std::unique_ptr<struct dwp_file>
ed2dc618 12554open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12555{
12556 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12557
82bf32bc
JK
12558 /* Try to find first .dwp for the binary file before any symbolic links
12559 resolving. */
6c447423
DE
12560
12561 /* If the objfile is a debug file, find the name of the real binary
12562 file and get the name of dwp file from there. */
d721ba37 12563 std::string dwp_name;
6c447423
DE
12564 if (objfile->separate_debug_objfile_backlink != NULL)
12565 {
12566 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12567 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12568
d721ba37 12569 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12570 }
12571 else
d721ba37
PA
12572 dwp_name = objfile->original_name;
12573
12574 dwp_name += ".dwp";
80626a55 12575
ed2dc618 12576 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12577 if (dbfd == NULL
12578 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12579 {
12580 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12581 dwp_name = objfile_name (objfile);
12582 dwp_name += ".dwp";
ed2dc618 12583 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12584 }
12585
80626a55
DE
12586 if (dbfd == NULL)
12587 {
b4f54984 12588 if (dwarf_read_debug)
d721ba37 12589 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12590 return std::unique_ptr<dwp_file> ();
3019eac3 12591 }
400174b1
TT
12592
12593 const char *name = bfd_get_filename (dbfd.get ());
12594 std::unique_ptr<struct dwp_file> dwp_file
12595 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12596
0a0f4c01 12597 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12598 dwp_file->elf_sections =
5989a64e 12599 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12600 dwp_file->num_sections, asection *);
12601
400174b1
TT
12602 bfd_map_over_sections (dwp_file->dbfd.get (),
12603 dwarf2_locate_common_dwp_sections,
12604 dwp_file.get ());
80626a55 12605
400174b1
TT
12606 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12607 0);
80626a55 12608
400174b1
TT
12609 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12610 1);
80626a55 12611
73869dc2 12612 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12613 if (dwp_file->cus && dwp_file->tus
12614 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12615 {
12616 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12617 pretty bizarre. We use pulongest here because that's the established
4d65956b 12618 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12619 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12620 " TU version %s [in DWP file %s]"),
12621 pulongest (dwp_file->cus->version),
d721ba37 12622 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12623 }
08302ed2
DE
12624
12625 if (dwp_file->cus)
12626 dwp_file->version = dwp_file->cus->version;
12627 else if (dwp_file->tus)
12628 dwp_file->version = dwp_file->tus->version;
12629 else
12630 dwp_file->version = 2;
73869dc2
DE
12631
12632 if (dwp_file->version == 2)
400174b1
TT
12633 bfd_map_over_sections (dwp_file->dbfd.get (),
12634 dwarf2_locate_v2_dwp_sections,
12635 dwp_file.get ());
73869dc2 12636
298e9637
SM
12637 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12638 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12639
b4f54984 12640 if (dwarf_read_debug)
80626a55
DE
12641 {
12642 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12643 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12644 " %s CUs, %s TUs\n",
12645 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12646 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12647 }
12648
12649 return dwp_file;
3019eac3 12650}
c906108c 12651
ab5088bf
DE
12652/* Wrapper around open_and_init_dwp_file, only open it once. */
12653
12654static struct dwp_file *
ed2dc618 12655get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12656{
5989a64e 12657 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12658 {
5989a64e 12659 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12660 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12661 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12662 }
5989a64e 12663 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12664}
12665
80626a55
DE
12666/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12667 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12668 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12669 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12670 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12671
12672 This is called, for example, when wanting to read a variable with a
12673 complex location. Therefore we don't want to do file i/o for every call.
12674 Therefore we don't want to look for a DWO file on every call.
12675 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12676 then we check if we've already seen DWO_NAME, and only THEN do we check
12677 for a DWO file.
12678
1c658ad5 12679 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12680 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12681
3019eac3 12682static struct dwo_unit *
80626a55
DE
12683lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12684 const char *dwo_name, const char *comp_dir,
12685 ULONGEST signature, int is_debug_types)
3019eac3 12686{
ed2dc618 12687 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12688 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12689 const char *kind = is_debug_types ? "TU" : "CU";
12690 void **dwo_file_slot;
3019eac3 12691 struct dwo_file *dwo_file;
80626a55 12692 struct dwp_file *dwp_file;
cb1df416 12693
6a506a2d
DE
12694 /* First see if there's a DWP file.
12695 If we have a DWP file but didn't find the DWO inside it, don't
12696 look for the original DWO file. It makes gdb behave differently
12697 depending on whether one is debugging in the build tree. */
cf2c3c16 12698
ed2dc618 12699 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12700 if (dwp_file != NULL)
cf2c3c16 12701 {
80626a55
DE
12702 const struct dwp_hash_table *dwp_htab =
12703 is_debug_types ? dwp_file->tus : dwp_file->cus;
12704
12705 if (dwp_htab != NULL)
12706 {
12707 struct dwo_unit *dwo_cutu =
ed2dc618 12708 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12709 signature, is_debug_types);
80626a55
DE
12710
12711 if (dwo_cutu != NULL)
12712 {
b4f54984 12713 if (dwarf_read_debug)
80626a55
DE
12714 {
12715 fprintf_unfiltered (gdb_stdlog,
12716 "Virtual DWO %s %s found: @%s\n",
12717 kind, hex_string (signature),
12718 host_address_to_string (dwo_cutu));
12719 }
12720 return dwo_cutu;
12721 }
12722 }
12723 }
6a506a2d 12724 else
80626a55 12725 {
6a506a2d 12726 /* No DWP file, look for the DWO file. */
80626a55 12727
ed2dc618
SM
12728 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12729 dwo_name, comp_dir);
6a506a2d 12730 if (*dwo_file_slot == NULL)
80626a55 12731 {
6a506a2d
DE
12732 /* Read in the file and build a table of the CUs/TUs it contains. */
12733 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12734 }
6a506a2d 12735 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12736 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12737
6a506a2d 12738 if (dwo_file != NULL)
19c3d4c9 12739 {
6a506a2d
DE
12740 struct dwo_unit *dwo_cutu = NULL;
12741
12742 if (is_debug_types && dwo_file->tus)
12743 {
12744 struct dwo_unit find_dwo_cutu;
12745
12746 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12747 find_dwo_cutu.signature = signature;
9a3c8263 12748 dwo_cutu
b0b6a987
TT
12749 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12750 &find_dwo_cutu);
6a506a2d 12751 }
33c5cd75 12752 else if (!is_debug_types && dwo_file->cus)
80626a55 12753 {
33c5cd75
DB
12754 struct dwo_unit find_dwo_cutu;
12755
12756 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12757 find_dwo_cutu.signature = signature;
b0b6a987 12758 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12759 &find_dwo_cutu);
6a506a2d
DE
12760 }
12761
12762 if (dwo_cutu != NULL)
12763 {
b4f54984 12764 if (dwarf_read_debug)
6a506a2d
DE
12765 {
12766 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12767 kind, dwo_name, hex_string (signature),
12768 host_address_to_string (dwo_cutu));
12769 }
12770 return dwo_cutu;
80626a55
DE
12771 }
12772 }
2e276125 12773 }
9cdd5dbd 12774
80626a55
DE
12775 /* We didn't find it. This could mean a dwo_id mismatch, or
12776 someone deleted the DWO/DWP file, or the search path isn't set up
12777 correctly to find the file. */
12778
b4f54984 12779 if (dwarf_read_debug)
80626a55
DE
12780 {
12781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12782 kind, dwo_name, hex_string (signature));
12783 }
3019eac3 12784
6656a72d
DE
12785 /* This is a warning and not a complaint because it can be caused by
12786 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12787 {
12788 /* Print the name of the DWP file if we looked there, helps the user
12789 better diagnose the problem. */
791afaa2 12790 std::string dwp_text;
43942612
DE
12791
12792 if (dwp_file != NULL)
791afaa2
TT
12793 dwp_text = string_printf (" [in DWP file %s]",
12794 lbasename (dwp_file->name));
43942612 12795
9d8780f0 12796 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12797 " [in module %s]"),
12798 kind, dwo_name, hex_string (signature),
791afaa2 12799 dwp_text.c_str (),
43942612 12800 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12801 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12802 }
3019eac3 12803 return NULL;
5fb290d7
DJ
12804}
12805
80626a55
DE
12806/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12807 See lookup_dwo_cutu_unit for details. */
12808
12809static struct dwo_unit *
12810lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12811 const char *dwo_name, const char *comp_dir,
12812 ULONGEST signature)
12813{
12814 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12815}
12816
12817/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12818 See lookup_dwo_cutu_unit for details. */
12819
12820static struct dwo_unit *
12821lookup_dwo_type_unit (struct signatured_type *this_tu,
12822 const char *dwo_name, const char *comp_dir)
12823{
12824 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12825}
12826
89e63ee4
DE
12827/* Traversal function for queue_and_load_all_dwo_tus. */
12828
12829static int
12830queue_and_load_dwo_tu (void **slot, void *info)
12831{
12832 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12833 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12834 ULONGEST signature = dwo_unit->signature;
12835 struct signatured_type *sig_type =
12836 lookup_dwo_signatured_type (per_cu->cu, signature);
12837
12838 if (sig_type != NULL)
12839 {
12840 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12841
12842 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12843 a real dependency of PER_CU on SIG_TYPE. That is detected later
12844 while processing PER_CU. */
12845 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
ab432490 12846 load_full_type_unit (sig_cu, per_cu->cu->per_objfile);
ae640021 12847 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12848 }
12849
12850 return 1;
12851}
12852
12853/* Queue all TUs contained in the DWO of PER_CU to be read in.
12854 The DWO may have the only definition of the type, though it may not be
12855 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12856 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12857
12858static void
12859queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12860{
12861 struct dwo_unit *dwo_unit;
12862 struct dwo_file *dwo_file;
12863
12864 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12865 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12866 gdb_assert (per_cu->cu != NULL);
12867
12868 dwo_unit = per_cu->cu->dwo_unit;
12869 gdb_assert (dwo_unit != NULL);
12870
12871 dwo_file = dwo_unit->dwo_file;
12872 if (dwo_file->tus != NULL)
b0b6a987
TT
12873 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12874 per_cu);
89e63ee4
DE
12875}
12876
3019eac3 12877/* Read in various DIEs. */
348e048f 12878
d389af10 12879/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12880 Inherit only the children of the DW_AT_abstract_origin DIE not being
12881 already referenced by DW_AT_abstract_origin from the children of the
12882 current DIE. */
d389af10
JK
12883
12884static void
12885inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12886{
12887 struct die_info *child_die;
791afaa2 12888 sect_offset *offsetp;
d389af10
JK
12889 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12890 struct die_info *origin_die;
12891 /* Iterator of the ORIGIN_DIE children. */
12892 struct die_info *origin_child_die;
d389af10 12893 struct attribute *attr;
cd02d79d
PA
12894 struct dwarf2_cu *origin_cu;
12895 struct pending **origin_previous_list_in_scope;
d389af10
JK
12896
12897 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12898 if (!attr)
12899 return;
12900
cd02d79d
PA
12901 /* Note that following die references may follow to a die in a
12902 different cu. */
12903
12904 origin_cu = cu;
12905 origin_die = follow_die_ref (die, attr, &origin_cu);
12906
12907 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12908 symbols in. */
12909 origin_previous_list_in_scope = origin_cu->list_in_scope;
12910 origin_cu->list_in_scope = cu->list_in_scope;
12911
edb3359d
DJ
12912 if (die->tag != origin_die->tag
12913 && !(die->tag == DW_TAG_inlined_subroutine
12914 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12915 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12916 sect_offset_str (die->sect_off),
12917 sect_offset_str (origin_die->sect_off));
d389af10 12918
791afaa2 12919 std::vector<sect_offset> offsets;
d389af10 12920
3ea89b92
PMR
12921 for (child_die = die->child;
12922 child_die && child_die->tag;
436c571c 12923 child_die = child_die->sibling)
3ea89b92
PMR
12924 {
12925 struct die_info *child_origin_die;
12926 struct dwarf2_cu *child_origin_cu;
12927
12928 /* We are trying to process concrete instance entries:
216f72a1 12929 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12930 it's not relevant to our analysis here. i.e. detecting DIEs that are
12931 present in the abstract instance but not referenced in the concrete
12932 one. */
216f72a1
JK
12933 if (child_die->tag == DW_TAG_call_site
12934 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12935 continue;
12936
c38f313d
DJ
12937 /* For each CHILD_DIE, find the corresponding child of
12938 ORIGIN_DIE. If there is more than one layer of
12939 DW_AT_abstract_origin, follow them all; there shouldn't be,
12940 but GCC versions at least through 4.4 generate this (GCC PR
12941 40573). */
3ea89b92
PMR
12942 child_origin_die = child_die;
12943 child_origin_cu = cu;
c38f313d
DJ
12944 while (1)
12945 {
cd02d79d
PA
12946 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12947 child_origin_cu);
c38f313d
DJ
12948 if (attr == NULL)
12949 break;
cd02d79d
PA
12950 child_origin_die = follow_die_ref (child_origin_die, attr,
12951 &child_origin_cu);
c38f313d
DJ
12952 }
12953
d389af10
JK
12954 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12955 counterpart may exist. */
c38f313d 12956 if (child_origin_die != child_die)
d389af10 12957 {
edb3359d
DJ
12958 if (child_die->tag != child_origin_die->tag
12959 && !(child_die->tag == DW_TAG_inlined_subroutine
12960 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12961 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12962 "different tags"),
9d8780f0
SM
12963 sect_offset_str (child_die->sect_off),
12964 sect_offset_str (child_origin_die->sect_off));
c38f313d 12965 if (child_origin_die->parent != origin_die)
b98664d3 12966 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12967 "different parents"),
9d8780f0
SM
12968 sect_offset_str (child_die->sect_off),
12969 sect_offset_str (child_origin_die->sect_off));
c38f313d 12970 else
791afaa2 12971 offsets.push_back (child_origin_die->sect_off);
d389af10 12972 }
d389af10 12973 }
791afaa2
TT
12974 std::sort (offsets.begin (), offsets.end ());
12975 sect_offset *offsets_end = offsets.data () + offsets.size ();
12976 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12977 if (offsetp[-1] == *offsetp)
b98664d3 12978 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12979 "to DIE %s as their abstract origin"),
12980 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12981
791afaa2 12982 offsetp = offsets.data ();
d389af10
JK
12983 origin_child_die = origin_die->child;
12984 while (origin_child_die && origin_child_die->tag)
12985 {
12986 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12987 while (offsetp < offsets_end
9c541725 12988 && *offsetp < origin_child_die->sect_off)
d389af10 12989 offsetp++;
b64f50a1 12990 if (offsetp >= offsets_end
9c541725 12991 || *offsetp > origin_child_die->sect_off)
d389af10 12992 {
adde2bff
DE
12993 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12994 Check whether we're already processing ORIGIN_CHILD_DIE.
12995 This can happen with mutually referenced abstract_origins.
12996 PR 16581. */
12997 if (!origin_child_die->in_process)
12998 process_die (origin_child_die, origin_cu);
d389af10 12999 }
436c571c 13000 origin_child_die = origin_child_die->sibling;
d389af10 13001 }
cd02d79d 13002 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13003
13004 if (cu != origin_cu)
13005 compute_delayed_physnames (origin_cu);
d389af10
JK
13006}
13007
c906108c 13008static void
e7c27a73 13009read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13010{
5e22e966 13011 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13012 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13013 struct context_stack *newobj;
c906108c
SS
13014 CORE_ADDR lowpc;
13015 CORE_ADDR highpc;
13016 struct die_info *child_die;
edb3359d 13017 struct attribute *attr, *call_line, *call_file;
15d034d0 13018 const char *name;
e142c38c 13019 CORE_ADDR baseaddr;
801e3a5b 13020 struct block *block;
edb3359d 13021 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13022 std::vector<struct symbol *> template_args;
34eaf542 13023 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13024
13025 if (inlined_func)
13026 {
13027 /* If we do not have call site information, we can't show the
13028 caller of this inlined function. That's too confusing, so
13029 only use the scope for local variables. */
13030 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13031 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13032 if (call_line == NULL || call_file == NULL)
13033 {
13034 read_lexical_block_scope (die, cu);
13035 return;
13036 }
13037 }
c906108c 13038
b3b3bada 13039 baseaddr = objfile->text_section_offset ();
e142c38c 13040
94af9270 13041 name = dwarf2_name (die, cu);
c906108c 13042
e8d05480
JB
13043 /* Ignore functions with missing or empty names. These are actually
13044 illegal according to the DWARF standard. */
13045 if (name == NULL)
13046 {
b98664d3 13047 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13048 sect_offset_str (die->sect_off));
e8d05480
JB
13049 return;
13050 }
13051
13052 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13053 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13054 <= PC_BOUNDS_INVALID)
e8d05480 13055 {
ae4d0c03
PM
13056 attr = dwarf2_attr (die, DW_AT_external, cu);
13057 if (!attr || !DW_UNSND (attr))
b98664d3 13058 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13059 "for subprogram DIE at %s"),
13060 sect_offset_str (die->sect_off));
e8d05480
JB
13061 return;
13062 }
c906108c 13063
3e29f34a
MR
13064 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13065 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13066
34eaf542
TT
13067 /* If we have any template arguments, then we must allocate a
13068 different sort of symbol. */
436c571c 13069 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13070 {
13071 if (child_die->tag == DW_TAG_template_type_param
13072 || child_die->tag == DW_TAG_template_value_param)
13073 {
8c14c3a3 13074 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13075 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13076 break;
13077 }
13078 }
13079
c24bdb02 13080 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13081 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13082 (struct symbol *) templ_func);
4c2df51b 13083
81873cc8 13084 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13085 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13086 cu->language);
13087
4cecd739
DJ
13088 /* If there is a location expression for DW_AT_frame_base, record
13089 it. */
e142c38c 13090 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13091 if (attr != nullptr)
fe978cb0 13092 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13093
63e43d3a
PMR
13094 /* If there is a location for the static link, record it. */
13095 newobj->static_link = NULL;
13096 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13097 if (attr != nullptr)
63e43d3a 13098 {
224c3ddb
SM
13099 newobj->static_link
13100 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13101 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13102 cu->per_cu->addr_type ());
63e43d3a
PMR
13103 }
13104
c24bdb02 13105 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13106
639d11d3 13107 if (die->child != NULL)
c906108c 13108 {
639d11d3 13109 child_die = die->child;
c906108c
SS
13110 while (child_die && child_die->tag)
13111 {
34eaf542
TT
13112 if (child_die->tag == DW_TAG_template_type_param
13113 || child_die->tag == DW_TAG_template_value_param)
13114 {
13115 struct symbol *arg = new_symbol (child_die, NULL, cu);
13116
f1078f66 13117 if (arg != NULL)
2f4732b0 13118 template_args.push_back (arg);
34eaf542
TT
13119 }
13120 else
13121 process_die (child_die, cu);
436c571c 13122 child_die = child_die->sibling;
c906108c
SS
13123 }
13124 }
13125
d389af10
JK
13126 inherit_abstract_dies (die, cu);
13127
4a811a97
UW
13128 /* If we have a DW_AT_specification, we might need to import using
13129 directives from the context of the specification DIE. See the
13130 comment in determine_prefix. */
13131 if (cu->language == language_cplus
13132 && dwarf2_attr (die, DW_AT_specification, cu))
13133 {
13134 struct dwarf2_cu *spec_cu = cu;
13135 struct die_info *spec_die = die_specification (die, &spec_cu);
13136
13137 while (spec_die)
13138 {
13139 child_die = spec_die->child;
13140 while (child_die && child_die->tag)
13141 {
13142 if (child_die->tag == DW_TAG_imported_module)
13143 process_die (child_die, spec_cu);
436c571c 13144 child_die = child_die->sibling;
4a811a97
UW
13145 }
13146
13147 /* In some cases, GCC generates specification DIEs that
13148 themselves contain DW_AT_specification attributes. */
13149 spec_die = die_specification (spec_die, &spec_cu);
13150 }
13151 }
13152
c24bdb02 13153 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13154 /* Make a block for the local symbols within. */
c24bdb02 13155 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13156 cstk.static_link, lowpc, highpc);
801e3a5b 13157
df8a16a1 13158 /* For C++, set the block's scope. */
45280282
IB
13159 if ((cu->language == language_cplus
13160 || cu->language == language_fortran
c44af4eb
TT
13161 || cu->language == language_d
13162 || cu->language == language_rust)
4d4ec4e5 13163 && cu->processing_has_namespace_info)
195a3f6c
TT
13164 block_set_scope (block, determine_prefix (die, cu),
13165 &objfile->objfile_obstack);
df8a16a1 13166
801e3a5b
JB
13167 /* If we have address ranges, record them. */
13168 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13169
a60f3166 13170 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13171
34eaf542 13172 /* Attach template arguments to function. */
2f4732b0 13173 if (!template_args.empty ())
34eaf542
TT
13174 {
13175 gdb_assert (templ_func != NULL);
13176
2f4732b0 13177 templ_func->n_template_arguments = template_args.size ();
34eaf542 13178 templ_func->template_arguments
8d749320
SM
13179 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13180 templ_func->n_template_arguments);
34eaf542 13181 memcpy (templ_func->template_arguments,
2f4732b0 13182 template_args.data (),
34eaf542 13183 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13184
13185 /* Make sure that the symtab is set on the new symbols. Even
13186 though they don't appear in this symtab directly, other parts
13187 of gdb assume that symbols do, and this is reasonably
13188 true. */
8634679f 13189 for (symbol *sym : template_args)
3e1d3d8c 13190 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13191 }
13192
208d8187
JB
13193 /* In C++, we can have functions nested inside functions (e.g., when
13194 a function declares a class that has methods). This means that
13195 when we finish processing a function scope, we may need to go
13196 back to building a containing block's symbol lists. */
c24bdb02
KS
13197 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13198 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13199
921e78cf
JB
13200 /* If we've finished processing a top-level function, subsequent
13201 symbols go in the file symbol list. */
c24bdb02
KS
13202 if (cu->get_builder ()->outermost_context_p ())
13203 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13204}
13205
13206/* Process all the DIES contained within a lexical block scope. Start
13207 a new scope, process the dies, and then close the scope. */
13208
13209static void
e7c27a73 13210read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13211{
5e22e966 13212 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13213 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13214 CORE_ADDR lowpc, highpc;
13215 struct die_info *child_die;
e142c38c
DJ
13216 CORE_ADDR baseaddr;
13217
b3b3bada 13218 baseaddr = objfile->text_section_offset ();
c906108c
SS
13219
13220 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13221 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13222 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13223 be nasty. Might be easier to properly extend generic blocks to
af34e669 13224 describe ranges. */
e385593e
JK
13225 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13226 {
13227 case PC_BOUNDS_NOT_PRESENT:
13228 /* DW_TAG_lexical_block has no attributes, process its children as if
13229 there was no wrapping by that DW_TAG_lexical_block.
13230 GCC does no longer produces such DWARF since GCC r224161. */
13231 for (child_die = die->child;
13232 child_die != NULL && child_die->tag;
436c571c 13233 child_die = child_die->sibling)
4f7bc5ed
TT
13234 {
13235 /* We might already be processing this DIE. This can happen
13236 in an unusual circumstance -- where a subroutine A
13237 appears lexically in another subroutine B, but A actually
13238 inlines B. The recursion is broken here, rather than in
13239 inherit_abstract_dies, because it seems better to simply
13240 drop concrete children here. */
13241 if (!child_die->in_process)
13242 process_die (child_die, cu);
13243 }
e385593e
JK
13244 return;
13245 case PC_BOUNDS_INVALID:
13246 return;
13247 }
3e29f34a
MR
13248 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13249 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13250
c24bdb02 13251 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13252 if (die->child != NULL)
c906108c 13253 {
639d11d3 13254 child_die = die->child;
c906108c
SS
13255 while (child_die && child_die->tag)
13256 {
e7c27a73 13257 process_die (child_die, cu);
436c571c 13258 child_die = child_die->sibling;
c906108c
SS
13259 }
13260 }
3ea89b92 13261 inherit_abstract_dies (die, cu);
c24bdb02 13262 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13263
c24bdb02
KS
13264 if (*cu->get_builder ()->get_local_symbols () != NULL
13265 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13266 {
801e3a5b 13267 struct block *block
c24bdb02 13268 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13269 cstk.start_addr, highpc);
801e3a5b
JB
13270
13271 /* Note that recording ranges after traversing children, as we
13272 do here, means that recording a parent's ranges entails
13273 walking across all its children's ranges as they appear in
13274 the address map, which is quadratic behavior.
13275
13276 It would be nicer to record the parent's ranges before
13277 traversing its children, simply overriding whatever you find
13278 there. But since we don't even decide whether to create a
13279 block until after we've traversed its children, that's hard
13280 to do. */
13281 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13282 }
c24bdb02
KS
13283 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13284 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13285}
13286
216f72a1 13287/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13288
13289static void
13290read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13291{
5e22e966 13292 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13293 struct objfile *objfile = per_objfile->objfile;
08feed99 13294 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13295 CORE_ADDR pc, baseaddr;
13296 struct attribute *attr;
13297 struct call_site *call_site, call_site_local;
13298 void **slot;
13299 int nparams;
13300 struct die_info *child_die;
13301
b3b3bada 13302 baseaddr = objfile->text_section_offset ();
96408a79 13303
216f72a1
JK
13304 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13305 if (attr == NULL)
13306 {
13307 /* This was a pre-DWARF-5 GNU extension alias
13308 for DW_AT_call_return_pc. */
13309 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13310 }
96408a79
SA
13311 if (!attr)
13312 {
b98664d3 13313 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13314 "DIE %s [in module %s]"),
13315 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13316 return;
13317 }
cd6c91b4 13318 pc = attr->value_as_address () + baseaddr;
3e29f34a 13319 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13320
13321 if (cu->call_site_htab == NULL)
13322 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13323 NULL, &objfile->objfile_obstack,
13324 hashtab_obstack_allocate, NULL);
13325 call_site_local.pc = pc;
13326 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13327 if (*slot != NULL)
13328 {
b98664d3 13329 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13330 "DIE %s [in module %s]"),
13331 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13332 objfile_name (objfile));
96408a79
SA
13333 return;
13334 }
13335
13336 /* Count parameters at the caller. */
13337
13338 nparams = 0;
13339 for (child_die = die->child; child_die && child_die->tag;
436c571c 13340 child_die = child_die->sibling)
96408a79 13341 {
216f72a1
JK
13342 if (child_die->tag != DW_TAG_call_site_parameter
13343 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13344 {
b98664d3 13345 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13346 "DW_TAG_call_site child DIE %s [in module %s]"),
13347 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13348 objfile_name (objfile));
96408a79
SA
13349 continue;
13350 }
13351
13352 nparams++;
13353 }
13354
224c3ddb
SM
13355 call_site
13356 = ((struct call_site *)
13357 obstack_alloc (&objfile->objfile_obstack,
13358 sizeof (*call_site)
13359 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13360 *slot = call_site;
13361 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13362 call_site->pc = pc;
13363
216f72a1
JK
13364 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13365 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13366 {
13367 struct die_info *func_die;
13368
13369 /* Skip also over DW_TAG_inlined_subroutine. */
13370 for (func_die = die->parent;
13371 func_die && func_die->tag != DW_TAG_subprogram
13372 && func_die->tag != DW_TAG_subroutine_type;
13373 func_die = func_die->parent);
13374
216f72a1
JK
13375 /* DW_AT_call_all_calls is a superset
13376 of DW_AT_call_all_tail_calls. */
96408a79 13377 if (func_die
216f72a1 13378 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13379 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13380 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13381 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13382 {
13383 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13384 not complete. But keep CALL_SITE for look ups via call_site_htab,
13385 both the initial caller containing the real return address PC and
13386 the final callee containing the current PC of a chain of tail
13387 calls do not need to have the tail call list complete. But any
13388 function candidate for a virtual tail call frame searched via
13389 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13390 determined unambiguously. */
13391 }
13392 else
13393 {
13394 struct type *func_type = NULL;
13395
13396 if (func_die)
13397 func_type = get_die_type (func_die, cu);
13398 if (func_type != NULL)
13399 {
78134374 13400 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13401
13402 /* Enlist this call site to the function. */
13403 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13404 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13405 }
13406 else
b98664d3 13407 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13408 "DIE %s [in module %s]"),
13409 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13410 }
13411 }
13412
216f72a1
JK
13413 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13414 if (attr == NULL)
13415 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13416 if (attr == NULL)
13417 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13418 if (attr == NULL)
216f72a1
JK
13419 {
13420 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13421 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13422 }
96408a79 13423 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13424 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13425 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13426 else if (attr->form_is_block ())
96408a79
SA
13427 {
13428 struct dwarf2_locexpr_baton *dlbaton;
13429
8d749320 13430 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13431 dlbaton->data = DW_BLOCK (attr)->data;
13432 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13433 dlbaton->per_objfile = per_objfile;
96408a79
SA
13434 dlbaton->per_cu = cu->per_cu;
13435
13436 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13437 }
cd6c91b4 13438 else if (attr->form_is_ref ())
96408a79 13439 {
96408a79
SA
13440 struct dwarf2_cu *target_cu = cu;
13441 struct die_info *target_die;
13442
ac9ec31b 13443 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13444 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13445 if (die_is_declaration (target_die, target_cu))
13446 {
7d45c7c3 13447 const char *target_physname;
9112db09
JK
13448
13449 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13450 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13451 if (target_physname == NULL)
9112db09 13452 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13453 if (target_physname == NULL)
b98664d3 13454 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13455 "physname, for referencing DIE %s [in module %s]"),
13456 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13457 else
7d455152 13458 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13459 }
13460 else
13461 {
13462 CORE_ADDR lowpc;
13463
13464 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13465 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13466 <= PC_BOUNDS_INVALID)
b98664d3 13467 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13468 "low pc, for referencing DIE %s [in module %s]"),
13469 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13470 else
3e29f34a
MR
13471 {
13472 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13473 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13474 }
96408a79
SA
13475 }
13476 }
13477 else
b98664d3 13478 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13479 "block nor reference, for DIE %s [in module %s]"),
13480 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13481
13482 call_site->per_cu = cu->per_cu;
13483
13484 for (child_die = die->child;
13485 child_die && child_die->tag;
436c571c 13486 child_die = child_die->sibling)
96408a79 13487 {
96408a79 13488 struct call_site_parameter *parameter;
1788b2d3 13489 struct attribute *loc, *origin;
96408a79 13490
216f72a1
JK
13491 if (child_die->tag != DW_TAG_call_site_parameter
13492 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13493 {
13494 /* Already printed the complaint above. */
13495 continue;
13496 }
13497
13498 gdb_assert (call_site->parameter_count < nparams);
13499 parameter = &call_site->parameter[call_site->parameter_count];
13500
1788b2d3
JK
13501 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13502 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13503 register is contained in DW_AT_call_value. */
96408a79 13504
24c5c679 13505 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13506 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13507 if (origin == NULL)
13508 {
13509 /* This was a pre-DWARF-5 GNU extension alias
13510 for DW_AT_call_parameter. */
13511 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13512 }
cd6c91b4 13513 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13514 {
1788b2d3 13515 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13516
0826b30a 13517 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13518 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13519 {
13520 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13521 binding can be done only inside one CU. Such referenced DIE
13522 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13523 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13524 "DW_TAG_call_site child DIE %s [in module %s]"),
13525 sect_offset_str (child_die->sect_off),
9c541725 13526 objfile_name (objfile));
d76b7dbc
JK
13527 continue;
13528 }
9c541725
PA
13529 parameter->u.param_cu_off
13530 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13531 }
4fc6c0d5 13532 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13533 {
b98664d3 13534 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13535 "DW_TAG_call_site child DIE %s [in module %s]"),
13536 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13537 continue;
13538 }
24c5c679 13539 else
96408a79 13540 {
24c5c679
JK
13541 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13542 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13543 if (parameter->u.dwarf_reg != -1)
13544 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13545 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13546 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13547 &parameter->u.fb_offset))
13548 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13549 else
13550 {
b98664d3 13551 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13552 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13553 "DW_TAG_call_site child DIE %s "
24c5c679 13554 "[in module %s]"),
9d8780f0 13555 sect_offset_str (child_die->sect_off),
9c541725 13556 objfile_name (objfile));
24c5c679
JK
13557 continue;
13558 }
96408a79
SA
13559 }
13560
216f72a1
JK
13561 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13562 if (attr == NULL)
13563 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13564 if (attr == NULL || !attr->form_is_block ())
96408a79 13565 {
b98664d3 13566 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13567 "DW_TAG_call_site child DIE %s [in module %s]"),
13568 sect_offset_str (child_die->sect_off),
9c541725 13569 objfile_name (objfile));
96408a79
SA
13570 continue;
13571 }
13572 parameter->value = DW_BLOCK (attr)->data;
13573 parameter->value_size = DW_BLOCK (attr)->size;
13574
13575 /* Parameters are not pre-cleared by memset above. */
13576 parameter->data_value = NULL;
13577 parameter->data_value_size = 0;
13578 call_site->parameter_count++;
13579
216f72a1
JK
13580 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13581 if (attr == NULL)
13582 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13583 if (attr != nullptr)
96408a79 13584 {
4fc6c0d5 13585 if (!attr->form_is_block ())
b98664d3 13586 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13587 "DW_TAG_call_site child DIE %s [in module %s]"),
13588 sect_offset_str (child_die->sect_off),
9c541725 13589 objfile_name (objfile));
96408a79
SA
13590 else
13591 {
13592 parameter->data_value = DW_BLOCK (attr)->data;
13593 parameter->data_value_size = DW_BLOCK (attr)->size;
13594 }
13595 }
13596 }
13597}
13598
71a3c369
TT
13599/* Helper function for read_variable. If DIE represents a virtual
13600 table, then return the type of the concrete object that is
13601 associated with the virtual table. Otherwise, return NULL. */
13602
13603static struct type *
13604rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13605{
13606 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13607 if (attr == NULL)
13608 return NULL;
13609
13610 /* Find the type DIE. */
13611 struct die_info *type_die = NULL;
13612 struct dwarf2_cu *type_cu = cu;
13613
cd6c91b4 13614 if (attr->form_is_ref ())
71a3c369
TT
13615 type_die = follow_die_ref (die, attr, &type_cu);
13616 if (type_die == NULL)
13617 return NULL;
13618
13619 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13620 return NULL;
13621 return die_containing_type (type_die, type_cu);
13622}
13623
13624/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13625
13626static void
13627read_variable (struct die_info *die, struct dwarf2_cu *cu)
13628{
13629 struct rust_vtable_symbol *storage = NULL;
13630
13631 if (cu->language == language_rust)
13632 {
13633 struct type *containing_type = rust_containing_type (die, cu);
13634
13635 if (containing_type != NULL)
13636 {
5e22e966 13637 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13638
8c14c3a3 13639 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13640 storage->concrete_type = containing_type;
cf724bc9 13641 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13642 }
13643 }
13644
e4a62c65
TV
13645 struct symbol *res = new_symbol (die, NULL, cu, storage);
13646 struct attribute *abstract_origin
13647 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13648 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13649 if (res == NULL && loc && abstract_origin)
13650 {
13651 /* We have a variable without a name, but with a location and an abstract
13652 origin. This may be a concrete instance of an abstract variable
13653 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13654 later. */
13655 struct dwarf2_cu *origin_cu = cu;
13656 struct die_info *origin_die
13657 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13658 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13659 per_objfile->per_bfd->abstract_to_concrete
13660 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13661 }
71a3c369
TT
13662}
13663
43988095
JK
13664/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13665 reading .debug_rnglists.
13666 Callback's type should be:
13667 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13668 Return true if the attributes are present and valid, otherwise,
13669 return false. */
13670
13671template <typename Callback>
13672static bool
13673dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13674 Callback &&callback)
13675{
5e22e966 13676 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13677 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13678 bfd *obfd = objfile->obfd;
43988095 13679 /* Base address selection entry. */
2b24b6e4 13680 gdb::optional<CORE_ADDR> base;
43988095 13681 const gdb_byte *buffer;
43988095
JK
13682 CORE_ADDR baseaddr;
13683 bool overflow = false;
13684
43988095
JK
13685 base = cu->base_address;
13686
5989a64e
SM
13687 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13688 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13689 {
b98664d3 13690 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13691 offset);
13692 return false;
13693 }
5989a64e 13694 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13695
b3b3bada 13696 baseaddr = objfile->text_section_offset ();
43988095
JK
13697
13698 while (1)
13699 {
7814882a
JK
13700 /* Initialize it due to a false compiler warning. */
13701 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13702 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13703 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13704 unsigned int bytes_read;
13705
13706 if (buffer == buf_end)
13707 {
13708 overflow = true;
13709 break;
13710 }
13711 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13712 switch (rlet)
13713 {
13714 case DW_RLE_end_of_list:
13715 break;
13716 case DW_RLE_base_address:
13717 if (buffer + cu->header.addr_size > buf_end)
13718 {
13719 overflow = true;
13720 break;
13721 }
c8a7a66f 13722 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13723 buffer += bytes_read;
13724 break;
13725 case DW_RLE_start_length:
13726 if (buffer + cu->header.addr_size > buf_end)
13727 {
13728 overflow = true;
13729 break;
13730 }
c8a7a66f
TT
13731 range_beginning = cu->header.read_address (obfd, buffer,
13732 &bytes_read);
43988095
JK
13733 buffer += bytes_read;
13734 range_end = (range_beginning
13735 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13736 buffer += bytes_read;
13737 if (buffer > buf_end)
13738 {
13739 overflow = true;
13740 break;
13741 }
13742 break;
13743 case DW_RLE_offset_pair:
13744 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13745 buffer += bytes_read;
13746 if (buffer > buf_end)
13747 {
13748 overflow = true;
13749 break;
13750 }
13751 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13752 buffer += bytes_read;
13753 if (buffer > buf_end)
13754 {
13755 overflow = true;
13756 break;
13757 }
13758 break;
13759 case DW_RLE_start_end:
13760 if (buffer + 2 * cu->header.addr_size > buf_end)
13761 {
13762 overflow = true;
13763 break;
13764 }
c8a7a66f
TT
13765 range_beginning = cu->header.read_address (obfd, buffer,
13766 &bytes_read);
43988095 13767 buffer += bytes_read;
c8a7a66f 13768 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13769 buffer += bytes_read;
13770 break;
13771 default:
b98664d3 13772 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13773 return false;
13774 }
13775 if (rlet == DW_RLE_end_of_list || overflow)
13776 break;
13777 if (rlet == DW_RLE_base_address)
13778 continue;
13779
2b24b6e4 13780 if (!base.has_value ())
43988095
JK
13781 {
13782 /* We have no valid base address for the ranges
13783 data. */
b98664d3 13784 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13785 return false;
13786 }
13787
13788 if (range_beginning > range_end)
13789 {
13790 /* Inverted range entries are invalid. */
b98664d3 13791 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13792 return false;
13793 }
13794
13795 /* Empty range entries have no effect. */
13796 if (range_beginning == range_end)
13797 continue;
13798
2b24b6e4
TT
13799 range_beginning += *base;
13800 range_end += *base;
43988095
JK
13801
13802 /* A not-uncommon case of bad debug info.
13803 Don't pollute the addrmap with bad data. */
13804 if (range_beginning + baseaddr == 0
5989a64e 13805 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13806 {
b98664d3 13807 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13808 " [in module %s]"), objfile_name (objfile));
13809 continue;
13810 }
13811
13812 callback (range_beginning, range_end);
13813 }
13814
13815 if (overflow)
13816 {
b98664d3 13817 complaint (_("Offset %d is not terminated "
43988095
JK
13818 "for DW_AT_ranges attribute"),
13819 offset);
13820 return false;
13821 }
13822
13823 return true;
13824}
13825
13826/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13827 Callback's type should be:
13828 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13829 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13830
43988095 13831template <typename Callback>
43039443 13832static int
5f46c5a5 13833dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13834 Callback &&callback)
43039443 13835{
5e22e966
SM
13836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13837 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13838 struct comp_unit_head *cu_header = &cu->header;
13839 bfd *obfd = objfile->obfd;
13840 unsigned int addr_size = cu_header->addr_size;
13841 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13842 /* Base address selection entry. */
2b24b6e4 13843 gdb::optional<CORE_ADDR> base;
43039443 13844 unsigned int dummy;
d521ce57 13845 const gdb_byte *buffer;
ff013f42 13846 CORE_ADDR baseaddr;
43039443 13847
43988095
JK
13848 if (cu_header->version >= 5)
13849 return dwarf2_rnglists_process (offset, cu, callback);
13850
d00adf39 13851 base = cu->base_address;
43039443 13852
5e22e966
SM
13853 per_objfile->per_bfd->ranges.read (objfile);
13854 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13855 {
b98664d3 13856 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13857 offset);
13858 return 0;
13859 }
5e22e966 13860 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13861
b3b3bada 13862 baseaddr = objfile->text_section_offset ();
ff013f42 13863
43039443
JK
13864 while (1)
13865 {
13866 CORE_ADDR range_beginning, range_end;
13867
c8a7a66f 13868 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13869 buffer += addr_size;
c8a7a66f 13870 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13871 buffer += addr_size;
13872 offset += 2 * addr_size;
13873
13874 /* An end of list marker is a pair of zero addresses. */
13875 if (range_beginning == 0 && range_end == 0)
13876 /* Found the end of list entry. */
13877 break;
13878
13879 /* Each base address selection entry is a pair of 2 values.
13880 The first is the largest possible address, the second is
13881 the base address. Check for a base address here. */
13882 if ((range_beginning & mask) == mask)
13883 {
28d2bfb9
AB
13884 /* If we found the largest possible address, then we already
13885 have the base address in range_end. */
13886 base = range_end;
43039443
JK
13887 continue;
13888 }
13889
2b24b6e4 13890 if (!base.has_value ())
43039443
JK
13891 {
13892 /* We have no valid base address for the ranges
13893 data. */
b98664d3 13894 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13895 return 0;
13896 }
13897
9277c30c
UW
13898 if (range_beginning > range_end)
13899 {
13900 /* Inverted range entries are invalid. */
b98664d3 13901 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13902 return 0;
13903 }
13904
13905 /* Empty range entries have no effect. */
13906 if (range_beginning == range_end)
13907 continue;
13908
2b24b6e4
TT
13909 range_beginning += *base;
13910 range_end += *base;
43039443 13911
01093045
DE
13912 /* A not-uncommon case of bad debug info.
13913 Don't pollute the addrmap with bad data. */
13914 if (range_beginning + baseaddr == 0
5e22e966 13915 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13916 {
b98664d3 13917 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13918 " [in module %s]"), objfile_name (objfile));
01093045
DE
13919 continue;
13920 }
13921
5f46c5a5
JK
13922 callback (range_beginning, range_end);
13923 }
13924
13925 return 1;
13926}
13927
13928/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13929 Return 1 if the attributes are present and valid, otherwise, return 0.
13930 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13931
13932static int
13933dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13934 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13935 dwarf2_psymtab *ranges_pst)
5f46c5a5 13936{
5e22e966 13937 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13938 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13939 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13940 int low_set = 0;
13941 CORE_ADDR low = 0;
13942 CORE_ADDR high = 0;
13943 int retval;
13944
13945 retval = dwarf2_ranges_process (offset, cu,
13946 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13947 {
9277c30c 13948 if (ranges_pst != NULL)
3e29f34a
MR
13949 {
13950 CORE_ADDR lowpc;
13951 CORE_ADDR highpc;
13952
79748972
TT
13953 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13954 range_beginning + baseaddr)
13955 - baseaddr);
13956 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13957 range_end + baseaddr)
13958 - baseaddr);
d320c2b5
TT
13959 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13960 lowpc, highpc - 1, ranges_pst);
3e29f34a 13961 }
ff013f42 13962
43039443
JK
13963 /* FIXME: This is recording everything as a low-high
13964 segment of consecutive addresses. We should have a
13965 data structure for discontiguous block ranges
13966 instead. */
13967 if (! low_set)
13968 {
13969 low = range_beginning;
13970 high = range_end;
13971 low_set = 1;
13972 }
13973 else
13974 {
13975 if (range_beginning < low)
13976 low = range_beginning;
13977 if (range_end > high)
13978 high = range_end;
13979 }
5f46c5a5
JK
13980 });
13981 if (!retval)
13982 return 0;
43039443
JK
13983
13984 if (! low_set)
13985 /* If the first entry is an end-of-list marker, the range
13986 describes an empty scope, i.e. no instructions. */
13987 return 0;
13988
13989 if (low_return)
13990 *low_return = low;
13991 if (high_return)
13992 *high_return = high;
13993 return 1;
13994}
13995
3a2b436a
JK
13996/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13997 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13998 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13999
3a2b436a 14000static enum pc_bounds_kind
af34e669 14001dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14002 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14003 dwarf2_psymtab *pst)
c906108c 14004{
5e22e966 14005 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14006 struct attribute *attr;
91da1414 14007 struct attribute *attr_high;
af34e669
DJ
14008 CORE_ADDR low = 0;
14009 CORE_ADDR high = 0;
e385593e 14010 enum pc_bounds_kind ret;
c906108c 14011
91da1414
MW
14012 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14013 if (attr_high)
af34e669 14014 {
e142c38c 14015 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14016 if (attr != nullptr)
91da1414 14017 {
cd6c91b4
TT
14018 low = attr->value_as_address ();
14019 high = attr_high->value_as_address ();
14020 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14021 high += low;
91da1414 14022 }
af34e669
DJ
14023 else
14024 /* Found high w/o low attribute. */
e385593e 14025 return PC_BOUNDS_INVALID;
af34e669
DJ
14026
14027 /* Found consecutive range of addresses. */
3a2b436a 14028 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14029 }
c906108c 14030 else
af34e669 14031 {
e142c38c 14032 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14033 if (attr != NULL)
14034 {
18a8505e 14035 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14036 We take advantage of the fact that DW_AT_ranges does not appear
14037 in DW_TAG_compile_unit of DWO files. */
14038 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14039 unsigned int ranges_offset = (DW_UNSND (attr)
14040 + (need_ranges_base
14041 ? cu->ranges_base
14042 : 0));
2e3cf129 14043
af34e669 14044 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14045 .debug_ranges section. */
2e3cf129 14046 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14047 return PC_BOUNDS_INVALID;
43039443 14048 /* Found discontinuous range of addresses. */
3a2b436a 14049 ret = PC_BOUNDS_RANGES;
af34e669 14050 }
e385593e
JK
14051 else
14052 return PC_BOUNDS_NOT_PRESENT;
af34e669 14053 }
c906108c 14054
48fbe735 14055 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14056 if (high <= low)
e385593e 14057 return PC_BOUNDS_INVALID;
c906108c
SS
14058
14059 /* When using the GNU linker, .gnu.linkonce. sections are used to
14060 eliminate duplicate copies of functions and vtables and such.
14061 The linker will arbitrarily choose one and discard the others.
14062 The AT_*_pc values for such functions refer to local labels in
14063 these sections. If the section from that file was discarded, the
14064 labels are not in the output, so the relocs get a value of 0.
14065 If this is a discarded function, mark the pc bounds as invalid,
14066 so that GDB will ignore it. */
5989a64e 14067 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14068 return PC_BOUNDS_INVALID;
c906108c
SS
14069
14070 *lowpc = low;
96408a79
SA
14071 if (highpc)
14072 *highpc = high;
af34e669 14073 return ret;
c906108c
SS
14074}
14075
b084d499
JB
14076/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14077 its low and high PC addresses. Do nothing if these addresses could not
14078 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14079 and HIGHPC to the high address if greater than HIGHPC. */
14080
14081static void
14082dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14083 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14084 struct dwarf2_cu *cu)
14085{
14086 CORE_ADDR low, high;
14087 struct die_info *child = die->child;
14088
e385593e 14089 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14090 {
325fac50
PA
14091 *lowpc = std::min (*lowpc, low);
14092 *highpc = std::max (*highpc, high);
b084d499
JB
14093 }
14094
14095 /* If the language does not allow nested subprograms (either inside
14096 subprograms or lexical blocks), we're done. */
14097 if (cu->language != language_ada)
14098 return;
6e70227d 14099
b084d499
JB
14100 /* Check all the children of the given DIE. If it contains nested
14101 subprograms, then check their pc bounds. Likewise, we need to
14102 check lexical blocks as well, as they may also contain subprogram
14103 definitions. */
14104 while (child && child->tag)
14105 {
14106 if (child->tag == DW_TAG_subprogram
14107 || child->tag == DW_TAG_lexical_block)
14108 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14109 child = child->sibling;
b084d499
JB
14110 }
14111}
14112
fae299cd
DC
14113/* Get the low and high pc's represented by the scope DIE, and store
14114 them in *LOWPC and *HIGHPC. If the correct values can't be
14115 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14116
14117static void
14118get_scope_pc_bounds (struct die_info *die,
14119 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14120 struct dwarf2_cu *cu)
14121{
14122 CORE_ADDR best_low = (CORE_ADDR) -1;
14123 CORE_ADDR best_high = (CORE_ADDR) 0;
14124 CORE_ADDR current_low, current_high;
14125
3a2b436a 14126 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14127 >= PC_BOUNDS_RANGES)
fae299cd
DC
14128 {
14129 best_low = current_low;
14130 best_high = current_high;
14131 }
14132 else
14133 {
14134 struct die_info *child = die->child;
14135
14136 while (child && child->tag)
14137 {
14138 switch (child->tag) {
14139 case DW_TAG_subprogram:
b084d499 14140 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14141 break;
14142 case DW_TAG_namespace:
f55ee35c 14143 case DW_TAG_module:
fae299cd
DC
14144 /* FIXME: carlton/2004-01-16: Should we do this for
14145 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14146 that current GCC's always emit the DIEs corresponding
14147 to definitions of methods of classes as children of a
14148 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14149 the DIEs giving the declarations, which could be
14150 anywhere). But I don't see any reason why the
14151 standards says that they have to be there. */
14152 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14153
14154 if (current_low != ((CORE_ADDR) -1))
14155 {
325fac50
PA
14156 best_low = std::min (best_low, current_low);
14157 best_high = std::max (best_high, current_high);
fae299cd
DC
14158 }
14159 break;
14160 default:
0963b4bd 14161 /* Ignore. */
fae299cd
DC
14162 break;
14163 }
14164
436c571c 14165 child = child->sibling;
fae299cd
DC
14166 }
14167 }
14168
14169 *lowpc = best_low;
14170 *highpc = best_high;
14171}
14172
801e3a5b
JB
14173/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14174 in DIE. */
380bca97 14175
801e3a5b
JB
14176static void
14177dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14178 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14179{
5e22e966 14180 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14181 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14182 struct attribute *attr;
91da1414 14183 struct attribute *attr_high;
801e3a5b 14184
91da1414
MW
14185 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14186 if (attr_high)
801e3a5b 14187 {
801e3a5b 14188 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14189 if (attr != nullptr)
801e3a5b 14190 {
cd6c91b4
TT
14191 CORE_ADDR low = attr->value_as_address ();
14192 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14193
cd6c91b4 14194 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14195 high += low;
9a619af0 14196
3e29f34a
MR
14197 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14198 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14199 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14200 }
14201 }
14202
14203 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14204 if (attr != nullptr)
801e3a5b 14205 {
18a8505e 14206 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14207 We take advantage of the fact that DW_AT_ranges does not appear
14208 in DW_TAG_compile_unit of DWO files. */
14209 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14210
14211 /* The value of the DW_AT_ranges attribute is the offset of the
14212 address range list in the .debug_ranges section. */
ab435259
DE
14213 unsigned long offset = (DW_UNSND (attr)
14214 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14215
2d5f09ec 14216 std::vector<blockrange> blockvec;
5f46c5a5
JK
14217 dwarf2_ranges_process (offset, cu,
14218 [&] (CORE_ADDR start, CORE_ADDR end)
14219 {
58fdfd2c
JK
14220 start += baseaddr;
14221 end += baseaddr;
5f46c5a5
JK
14222 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14223 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14224 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14225 blockvec.emplace_back (start, end);
5f46c5a5 14226 });
2d5f09ec
KB
14227
14228 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14229 }
14230}
14231
685b1105
JK
14232/* Check whether the producer field indicates either of GCC < 4.6, or the
14233 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14234
685b1105
JK
14235static void
14236check_producer (struct dwarf2_cu *cu)
60d5a603 14237{
38360086 14238 int major, minor;
60d5a603
JK
14239
14240 if (cu->producer == NULL)
14241 {
14242 /* For unknown compilers expect their behavior is DWARF version
14243 compliant.
14244
14245 GCC started to support .debug_types sections by -gdwarf-4 since
14246 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14247 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14248 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14249 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14250 }
b1ffba5a 14251 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14252 {
38360086
MW
14253 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14254 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14255 }
5230b05a 14256 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14257 {
14258 cu->producer_is_icc = true;
14259 cu->producer_is_icc_lt_14 = major < 14;
14260 }
c258c396
JD
14261 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14262 cu->producer_is_codewarrior = true;
685b1105
JK
14263 else
14264 {
14265 /* For other non-GCC compilers, expect their behavior is DWARF version
14266 compliant. */
60d5a603
JK
14267 }
14268
9068261f 14269 cu->checked_producer = true;
685b1105 14270}
ba919b58 14271
685b1105
JK
14272/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14273 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14274 during 4.6.0 experimental. */
14275
9068261f 14276static bool
685b1105
JK
14277producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14278{
14279 if (!cu->checked_producer)
14280 check_producer (cu);
14281
14282 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14283}
14284
c258c396
JD
14285
14286/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14287 with incorrect is_stmt attributes. */
14288
14289static bool
14290producer_is_codewarrior (struct dwarf2_cu *cu)
14291{
14292 if (!cu->checked_producer)
14293 check_producer (cu);
14294
14295 return cu->producer_is_codewarrior;
14296}
14297
405feb71 14298/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14299 DW_AT_accessibility. */
14300
14301static enum dwarf_access_attribute
14302dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14303{
14304 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14305 {
14306 /* The default DWARF 2 accessibility for members is public, the default
14307 accessibility for inheritance is private. */
14308
14309 if (die->tag != DW_TAG_inheritance)
14310 return DW_ACCESS_public;
14311 else
14312 return DW_ACCESS_private;
14313 }
14314 else
14315 {
14316 /* DWARF 3+ defines the default accessibility a different way. The same
14317 rules apply now for DW_TAG_inheritance as for the members and it only
14318 depends on the container kind. */
14319
14320 if (die->parent->tag == DW_TAG_class_type)
14321 return DW_ACCESS_private;
14322 else
14323 return DW_ACCESS_public;
14324 }
14325}
14326
74ac6d43
TT
14327/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14328 offset. If the attribute was not found return 0, otherwise return
14329 1. If it was found but could not properly be handled, set *OFFSET
14330 to 0. */
14331
14332static int
14333handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14334 LONGEST *offset)
14335{
14336 struct attribute *attr;
14337
14338 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14339 if (attr != NULL)
14340 {
14341 *offset = 0;
14342
14343 /* Note that we do not check for a section offset first here.
14344 This is because DW_AT_data_member_location is new in DWARF 4,
14345 so if we see it, we can assume that a constant form is really
14346 a constant and not a section offset. */
cd6c91b4 14347 if (attr->form_is_constant ())
0826b30a 14348 *offset = attr->constant_value (0);
cd6c91b4 14349 else if (attr->form_is_section_offset ())
74ac6d43 14350 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14351 else if (attr->form_is_block ())
74ac6d43
TT
14352 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14353 else
14354 dwarf2_complex_location_expr_complaint ();
14355
14356 return 1;
14357 }
14358
14359 return 0;
14360}
14361
7d79de9a
TT
14362/* Look for DW_AT_data_member_location and store the results in FIELD. */
14363
14364static void
14365handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14366 struct field *field)
14367{
14368 struct attribute *attr;
14369
14370 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14371 if (attr != NULL)
14372 {
14373 if (attr->form_is_constant ())
14374 {
14375 LONGEST offset = attr->constant_value (0);
14376 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14377 }
14378 else if (attr->form_is_section_offset ())
14379 dwarf2_complex_location_expr_complaint ();
14380 else if (attr->form_is_block ())
14381 {
14382 bool handled;
14383 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14384 if (handled)
14385 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14386 else
14387 {
5e22e966
SM
14388 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14389 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14390 struct dwarf2_locexpr_baton *dlbaton
14391 = XOBNEW (&objfile->objfile_obstack,
14392 struct dwarf2_locexpr_baton);
14393 dlbaton->data = DW_BLOCK (attr)->data;
14394 dlbaton->size = DW_BLOCK (attr)->size;
14395 /* When using this baton, we want to compute the address
14396 of the field, not the value. This is why
14397 is_reference is set to false here. */
14398 dlbaton->is_reference = false;
5e22e966 14399 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14400 dlbaton->per_cu = cu->per_cu;
14401
14402 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14403 }
14404 }
14405 else
14406 dwarf2_complex_location_expr_complaint ();
14407 }
14408}
14409
c906108c
SS
14410/* Add an aggregate field to the field list. */
14411
14412static void
107d2387 14413dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14414 struct dwarf2_cu *cu)
6e70227d 14415{
5e22e966 14416 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14417 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14418 struct nextfield *new_field;
14419 struct attribute *attr;
14420 struct field *fp;
15d034d0 14421 const char *fieldname = "";
c906108c 14422
7d0ccb61
DJ
14423 if (die->tag == DW_TAG_inheritance)
14424 {
be2daae6
TT
14425 fip->baseclasses.emplace_back ();
14426 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14427 }
14428 else
14429 {
be2daae6
TT
14430 fip->fields.emplace_back ();
14431 new_field = &fip->fields.back ();
7d0ccb61 14432 }
be2daae6 14433
9c6a1327
TT
14434 new_field->offset = die->sect_off;
14435
e142c38c 14436 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14437 if (attr != nullptr)
c906108c 14438 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14439 else
14440 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14441 if (new_field->accessibility != DW_ACCESS_public)
14442 fip->non_public_fields = 1;
60d5a603 14443
e142c38c 14444 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14445 if (attr != nullptr)
c906108c 14446 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14447 else
14448 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14449
14450 fp = &new_field->field;
a9a9bd0f 14451
e142c38c 14452 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14453 {
a9a9bd0f 14454 /* Data member other than a C++ static data member. */
6e70227d 14455
c906108c 14456 /* Get type of field. */
e7c27a73 14457 fp->type = die_type (die, cu);
c906108c 14458
d6a843b5 14459 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14460
c906108c 14461 /* Get bit size of field (zero if none). */
e142c38c 14462 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14463 if (attr != nullptr)
c906108c
SS
14464 {
14465 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14466 }
14467 else
14468 {
14469 FIELD_BITSIZE (*fp) = 0;
14470 }
14471
14472 /* Get bit offset of field. */
7d79de9a 14473 handle_data_member_location (die, cu, fp);
e142c38c 14474 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14475 if (attr != nullptr)
c906108c 14476 {
d5a22e77 14477 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14478 {
14479 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14480 additional bit offset from the MSB of the containing
14481 anonymous object to the MSB of the field. We don't
14482 have to do anything special since we don't need to
14483 know the size of the anonymous object. */
f41f5e61 14484 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14485 }
14486 else
14487 {
14488 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14489 MSB of the anonymous object, subtract off the number of
14490 bits from the MSB of the field to the MSB of the
14491 object, and then subtract off the number of bits of
14492 the field itself. The result is the bit offset of
14493 the LSB of the field. */
c906108c
SS
14494 int anonymous_size;
14495 int bit_offset = DW_UNSND (attr);
14496
e142c38c 14497 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14498 if (attr != nullptr)
c906108c
SS
14499 {
14500 /* The size of the anonymous object containing
14501 the bit field is explicit, so use the
14502 indicated size (in bytes). */
14503 anonymous_size = DW_UNSND (attr);
14504 }
14505 else
14506 {
14507 /* The size of the anonymous object containing
14508 the bit field must be inferred from the type
14509 attribute of the data member containing the
14510 bit field. */
14511 anonymous_size = TYPE_LENGTH (fp->type);
14512 }
f41f5e61
PA
14513 SET_FIELD_BITPOS (*fp,
14514 (FIELD_BITPOS (*fp)
14515 + anonymous_size * bits_per_byte
14516 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14517 }
14518 }
da5b30da
AA
14519 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14520 if (attr != NULL)
14521 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14522 + attr->constant_value (0)));
c906108c
SS
14523
14524 /* Get name of field. */
39cbfefa
DJ
14525 fieldname = dwarf2_name (die, cu);
14526 if (fieldname == NULL)
14527 fieldname = "";
d8151005
DJ
14528
14529 /* The name is already allocated along with this objfile, so we don't
14530 need to duplicate it for the type. */
14531 fp->name = fieldname;
c906108c
SS
14532
14533 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14534 pointer or virtual base class pointer) to private. */
e142c38c 14535 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14536 {
d48cc9dd 14537 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14538 new_field->accessibility = DW_ACCESS_private;
14539 fip->non_public_fields = 1;
14540 }
14541 }
a9a9bd0f 14542 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14543 {
a9a9bd0f
DC
14544 /* C++ static member. */
14545
14546 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14547 is a declaration, but all versions of G++ as of this writing
14548 (so through at least 3.2.1) incorrectly generate
14549 DW_TAG_variable tags. */
6e70227d 14550
ff355380 14551 const char *physname;
c906108c 14552
a9a9bd0f 14553 /* Get name of field. */
39cbfefa
DJ
14554 fieldname = dwarf2_name (die, cu);
14555 if (fieldname == NULL)
c906108c
SS
14556 return;
14557
254e6b9e 14558 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14559 if (attr
14560 /* Only create a symbol if this is an external value.
14561 new_symbol checks this and puts the value in the global symbol
14562 table, which we want. If it is not external, new_symbol
14563 will try to put the value in cu->list_in_scope which is wrong. */
14564 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14565 {
14566 /* A static const member, not much different than an enum as far as
14567 we're concerned, except that we can support more types. */
14568 new_symbol (die, NULL, cu);
14569 }
14570
2df3850c 14571 /* Get physical name. */
ff355380 14572 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14573
d8151005
DJ
14574 /* The name is already allocated along with this objfile, so we don't
14575 need to duplicate it for the type. */
14576 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14577 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14578 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14579 }
14580 else if (die->tag == DW_TAG_inheritance)
14581 {
74ac6d43 14582 /* C++ base class field. */
7d79de9a 14583 handle_data_member_location (die, cu, fp);
c906108c 14584 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14585 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14586 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14587 }
2ddeaf8a
TT
14588 else
14589 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14590}
14591
883fd55a
KS
14592/* Can the type given by DIE define another type? */
14593
14594static bool
14595type_can_define_types (const struct die_info *die)
14596{
14597 switch (die->tag)
14598 {
14599 case DW_TAG_typedef:
14600 case DW_TAG_class_type:
14601 case DW_TAG_structure_type:
14602 case DW_TAG_union_type:
14603 case DW_TAG_enumeration_type:
14604 return true;
14605
14606 default:
14607 return false;
14608 }
14609}
14610
14611/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14612
14613static void
883fd55a
KS
14614dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14615 struct dwarf2_cu *cu)
6e70227d 14616{
be2daae6
TT
14617 struct decl_field fp;
14618 memset (&fp, 0, sizeof (fp));
98751a41 14619
883fd55a 14620 gdb_assert (type_can_define_types (die));
98751a41 14621
883fd55a 14622 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14623 fp.name = dwarf2_name (die, cu);
14624 fp.type = read_type_die (die, cu);
98751a41 14625
c191a687
KS
14626 /* Save accessibility. */
14627 enum dwarf_access_attribute accessibility;
14628 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14629 if (attr != NULL)
14630 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14631 else
14632 accessibility = dwarf2_default_access_attribute (die, cu);
14633 switch (accessibility)
14634 {
14635 case DW_ACCESS_public:
14636 /* The assumed value if neither private nor protected. */
14637 break;
14638 case DW_ACCESS_private:
be2daae6 14639 fp.is_private = 1;
c191a687
KS
14640 break;
14641 case DW_ACCESS_protected:
be2daae6 14642 fp.is_protected = 1;
c191a687
KS
14643 break;
14644 default:
b98664d3 14645 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14646 }
14647
883fd55a 14648 if (die->tag == DW_TAG_typedef)
be2daae6 14649 fip->typedef_field_list.push_back (fp);
883fd55a 14650 else
be2daae6 14651 fip->nested_types_list.push_back (fp);
98751a41
JK
14652}
14653
9c6a1327
TT
14654/* A convenience typedef that's used when finding the discriminant
14655 field for a variant part. */
1b95cdb7
SM
14656typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14657 offset_map_type;
9c6a1327
TT
14658
14659/* Compute the discriminant range for a given variant. OBSTACK is
14660 where the results will be stored. VARIANT is the variant to
14661 process. IS_UNSIGNED indicates whether the discriminant is signed
14662 or unsigned. */
14663
14664static const gdb::array_view<discriminant_range>
14665convert_variant_range (struct obstack *obstack, const variant_field &variant,
14666 bool is_unsigned)
14667{
14668 std::vector<discriminant_range> ranges;
14669
14670 if (variant.default_branch)
14671 return {};
14672
14673 if (variant.discr_list_data == nullptr)
14674 {
14675 discriminant_range r
14676 = {variant.discriminant_value, variant.discriminant_value};
14677 ranges.push_back (r);
14678 }
14679 else
14680 {
14681 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14682 variant.discr_list_data->size);
14683 while (!data.empty ())
14684 {
14685 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14686 {
14687 complaint (_("invalid discriminant marker: %d"), data[0]);
14688 break;
14689 }
14690 bool is_range = data[0] == DW_DSC_range;
14691 data = data.slice (1);
14692
14693 ULONGEST low, high;
14694 unsigned int bytes_read;
14695
14696 if (data.empty ())
14697 {
14698 complaint (_("DW_AT_discr_list missing low value"));
14699 break;
14700 }
14701 if (is_unsigned)
14702 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14703 else
14704 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14705 &bytes_read);
14706 data = data.slice (bytes_read);
14707
14708 if (is_range)
14709 {
14710 if (data.empty ())
14711 {
14712 complaint (_("DW_AT_discr_list missing high value"));
14713 break;
14714 }
14715 if (is_unsigned)
14716 high = read_unsigned_leb128 (nullptr, data.data (),
14717 &bytes_read);
14718 else
14719 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14720 &bytes_read);
14721 data = data.slice (bytes_read);
14722 }
14723 else
14724 high = low;
14725
14726 ranges.push_back ({ low, high });
14727 }
14728 }
14729
14730 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14731 ranges.size ());
14732 std::copy (ranges.begin (), ranges.end (), result);
14733 return gdb::array_view<discriminant_range> (result, ranges.size ());
14734}
14735
14736static const gdb::array_view<variant_part> create_variant_parts
14737 (struct obstack *obstack,
14738 const offset_map_type &offset_map,
14739 struct field_info *fi,
14740 const std::vector<variant_part_builder> &variant_parts);
14741
14742/* Fill in a "struct variant" for a given variant field. RESULT is
14743 the variant to fill in. OBSTACK is where any needed allocations
14744 will be done. OFFSET_MAP holds the mapping from section offsets to
14745 fields for the type. FI describes the fields of the type we're
14746 processing. FIELD is the variant field we're converting. */
14747
14748static void
14749create_one_variant (variant &result, struct obstack *obstack,
14750 const offset_map_type &offset_map,
14751 struct field_info *fi, const variant_field &field)
14752{
14753 result.discriminants = convert_variant_range (obstack, field, false);
14754 result.first_field = field.first_field + fi->baseclasses.size ();
14755 result.last_field = field.last_field + fi->baseclasses.size ();
14756 result.parts = create_variant_parts (obstack, offset_map, fi,
14757 field.variant_parts);
14758}
14759
14760/* Fill in a "struct variant_part" for a given variant part. RESULT
14761 is the variant part to fill in. OBSTACK is where any needed
14762 allocations will be done. OFFSET_MAP holds the mapping from
14763 section offsets to fields for the type. FI describes the fields of
14764 the type we're processing. BUILDER is the variant part to be
14765 converted. */
14766
14767static void
14768create_one_variant_part (variant_part &result,
14769 struct obstack *obstack,
14770 const offset_map_type &offset_map,
14771 struct field_info *fi,
14772 const variant_part_builder &builder)
14773{
14774 auto iter = offset_map.find (builder.discriminant_offset);
14775 if (iter == offset_map.end ())
14776 {
14777 result.discriminant_index = -1;
14778 /* Doesn't matter. */
14779 result.is_unsigned = false;
14780 }
14781 else
14782 {
14783 result.discriminant_index = iter->second;
14784 result.is_unsigned
14785 = TYPE_UNSIGNED (FIELD_TYPE
14786 (fi->fields[result.discriminant_index].field));
14787 }
14788
14789 size_t n = builder.variants.size ();
14790 variant *output = new (obstack) variant[n];
14791 for (size_t i = 0; i < n; ++i)
14792 create_one_variant (output[i], obstack, offset_map, fi,
14793 builder.variants[i]);
14794
14795 result.variants = gdb::array_view<variant> (output, n);
14796}
14797
14798/* Create a vector of variant parts that can be attached to a type.
14799 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14800 holds the mapping from section offsets to fields for the type. FI
14801 describes the fields of the type we're processing. VARIANT_PARTS
14802 is the vector to convert. */
14803
14804static const gdb::array_view<variant_part>
14805create_variant_parts (struct obstack *obstack,
14806 const offset_map_type &offset_map,
14807 struct field_info *fi,
14808 const std::vector<variant_part_builder> &variant_parts)
14809{
14810 if (variant_parts.empty ())
14811 return {};
14812
14813 size_t n = variant_parts.size ();
14814 variant_part *result = new (obstack) variant_part[n];
14815 for (size_t i = 0; i < n; ++i)
14816 create_one_variant_part (result[i], obstack, offset_map, fi,
14817 variant_parts[i]);
14818
14819 return gdb::array_view<variant_part> (result, n);
14820}
14821
14822/* Compute the variant part vector for FIP, attaching it to TYPE when
14823 done. */
14824
14825static void
14826add_variant_property (struct field_info *fip, struct type *type,
14827 struct dwarf2_cu *cu)
14828{
14829 /* Map section offsets of fields to their field index. Note the
14830 field index here does not take the number of baseclasses into
14831 account. */
14832 offset_map_type offset_map;
14833 for (int i = 0; i < fip->fields.size (); ++i)
14834 offset_map[fip->fields[i].offset] = i;
14835
5e22e966 14836 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14837 gdb::array_view<variant_part> parts
14838 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14839 fip->variant_parts);
14840
14841 struct dynamic_prop prop;
14842 prop.kind = PROP_VARIANT_PARTS;
14843 prop.data.variant_parts
14844 = ((gdb::array_view<variant_part> *)
14845 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14846
5c54719c 14847 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14848}
14849
c906108c
SS
14850/* Create the vector of fields, and attach it to the type. */
14851
14852static void
fba45db2 14853dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14854 struct dwarf2_cu *cu)
c906108c 14855{
317f7127 14856 int nfields = fip->nfields ();
c906108c
SS
14857
14858 /* Record the field count, allocate space for the array of fields,
14859 and create blank accessibility bitfields if necessary. */
5e33d5f4 14860 type->set_num_fields (nfields);
3cabb6b0
SM
14861 type->set_fields
14862 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14863
b4ba55a1 14864 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14865 {
14866 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14867
14868 TYPE_FIELD_PRIVATE_BITS (type) =
14869 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14870 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14871
14872 TYPE_FIELD_PROTECTED_BITS (type) =
14873 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14874 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14875
774b6a14
TT
14876 TYPE_FIELD_IGNORE_BITS (type) =
14877 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14878 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14879 }
14880
14881 /* If the type has baseclasses, allocate and clear a bit vector for
14882 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14883 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14884 {
be2daae6 14885 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14886 unsigned char *pointer;
c906108c
SS
14887
14888 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14889 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14890 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14891 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14892 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14893 }
14894
9c6a1327
TT
14895 if (!fip->variant_parts.empty ())
14896 add_variant_property (fip, type, cu);
2ddeaf8a 14897
be2daae6
TT
14898 /* Copy the saved-up fields into the field vector. */
14899 for (int i = 0; i < nfields; ++i)
c906108c 14900 {
be2daae6
TT
14901 struct nextfield &field
14902 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14903 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14904
ceacbf6e 14905 type->field (i) = field.field;
be2daae6 14906 switch (field.accessibility)
c906108c 14907 {
c5aa993b 14908 case DW_ACCESS_private:
b4ba55a1 14909 if (cu->language != language_ada)
be2daae6 14910 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14911 break;
c906108c 14912
c5aa993b 14913 case DW_ACCESS_protected:
b4ba55a1 14914 if (cu->language != language_ada)
be2daae6 14915 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14916 break;
c906108c 14917
c5aa993b
JM
14918 case DW_ACCESS_public:
14919 break;
c906108c 14920
c5aa993b
JM
14921 default:
14922 /* Unknown accessibility. Complain and treat it as public. */
14923 {
b98664d3 14924 complaint (_("unsupported accessibility %d"),
be2daae6 14925 field.accessibility);
c5aa993b
JM
14926 }
14927 break;
c906108c 14928 }
be2daae6 14929 if (i < fip->baseclasses.size ())
c906108c 14930 {
be2daae6 14931 switch (field.virtuality)
c906108c 14932 {
c5aa993b
JM
14933 case DW_VIRTUALITY_virtual:
14934 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14935 if (cu->language == language_ada)
a73c6dcd 14936 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14937 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14938 break;
c906108c
SS
14939 }
14940 }
c906108c
SS
14941 }
14942}
14943
7d27a96d
TT
14944/* Return true if this member function is a constructor, false
14945 otherwise. */
14946
14947static int
14948dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14949{
14950 const char *fieldname;
fe978cb0 14951 const char *type_name;
7d27a96d
TT
14952 int len;
14953
14954 if (die->parent == NULL)
14955 return 0;
14956
14957 if (die->parent->tag != DW_TAG_structure_type
14958 && die->parent->tag != DW_TAG_union_type
14959 && die->parent->tag != DW_TAG_class_type)
14960 return 0;
14961
14962 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14963 type_name = dwarf2_name (die->parent, cu);
14964 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14965 return 0;
14966
14967 len = strlen (fieldname);
fe978cb0
PA
14968 return (strncmp (fieldname, type_name, len) == 0
14969 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14970}
14971
e35000a7
TBA
14972/* Check if the given VALUE is a recognized enum
14973 dwarf_defaulted_attribute constant according to DWARF5 spec,
14974 Table 7.24. */
14975
14976static bool
14977is_valid_DW_AT_defaulted (ULONGEST value)
14978{
14979 switch (value)
14980 {
14981 case DW_DEFAULTED_no:
14982 case DW_DEFAULTED_in_class:
14983 case DW_DEFAULTED_out_of_class:
14984 return true;
14985 }
14986
3142e908 14987 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14988 return false;
14989}
14990
c906108c
SS
14991/* Add a member function to the proper fieldlist. */
14992
14993static void
107d2387 14994dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14995 struct type *type, struct dwarf2_cu *cu)
c906108c 14996{
5e22e966 14997 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 14998 struct attribute *attr;
c906108c 14999 int i;
be2daae6 15000 struct fnfieldlist *flp = nullptr;
c906108c 15001 struct fn_field *fnp;
15d034d0 15002 const char *fieldname;
f792889a 15003 struct type *this_type;
60d5a603 15004 enum dwarf_access_attribute accessibility;
c906108c 15005
b4ba55a1 15006 if (cu->language == language_ada)
a73c6dcd 15007 error (_("unexpected member function in Ada type"));
b4ba55a1 15008
2df3850c 15009 /* Get name of member function. */
39cbfefa
DJ
15010 fieldname = dwarf2_name (die, cu);
15011 if (fieldname == NULL)
2df3850c 15012 return;
c906108c 15013
c906108c 15014 /* Look up member function name in fieldlist. */
be2daae6 15015 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15016 {
27bfe10e 15017 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15018 {
15019 flp = &fip->fnfieldlists[i];
15020 break;
15021 }
c906108c
SS
15022 }
15023
be2daae6
TT
15024 /* Create a new fnfieldlist if necessary. */
15025 if (flp == nullptr)
c906108c 15026 {
be2daae6
TT
15027 fip->fnfieldlists.emplace_back ();
15028 flp = &fip->fnfieldlists.back ();
c906108c 15029 flp->name = fieldname;
be2daae6 15030 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15031 }
15032
be2daae6
TT
15033 /* Create a new member function field and add it to the vector of
15034 fnfieldlists. */
15035 flp->fnfields.emplace_back ();
15036 fnp = &flp->fnfields.back ();
3da10d80
KS
15037
15038 /* Delay processing of the physname until later. */
9c37b5ae 15039 if (cu->language == language_cplus)
be2daae6
TT
15040 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15041 die, cu);
3da10d80
KS
15042 else
15043 {
1d06ead6 15044 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15045 fnp->physname = physname ? physname : "";
15046 }
15047
c906108c 15048 fnp->type = alloc_type (objfile);
f792889a 15049 this_type = read_type_die (die, cu);
78134374 15050 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15051 {
1f704f76 15052 int nparams = this_type->num_fields ();
c906108c 15053
f792889a 15054 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15055 of the method itself (TYPE_CODE_METHOD). */
15056 smash_to_method_type (fnp->type, type,
f792889a 15057 TYPE_TARGET_TYPE (this_type),
80fc5e77 15058 this_type->fields (),
1f704f76 15059 this_type->num_fields (),
f792889a 15060 TYPE_VARARGS (this_type));
c906108c
SS
15061
15062 /* Handle static member functions.
c5aa993b 15063 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15064 member functions. G++ helps GDB by marking the first
15065 parameter for non-static member functions (which is the this
15066 pointer) as artificial. We obtain this information from
15067 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15068 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15069 fnp->voffset = VOFFSET_STATIC;
15070 }
15071 else
b98664d3 15072 complaint (_("member function type missing for '%s'"),
3da10d80 15073 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15074
15075 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15076 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15077 fnp->fcontext = die_containing_type (die, cu);
c906108c 15078
3e43a32a
MS
15079 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15080 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15081
15082 /* Get accessibility. */
e142c38c 15083 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15084 if (attr != nullptr)
aead7601 15085 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15086 else
15087 accessibility = dwarf2_default_access_attribute (die, cu);
15088 switch (accessibility)
c906108c 15089 {
60d5a603
JK
15090 case DW_ACCESS_private:
15091 fnp->is_private = 1;
15092 break;
15093 case DW_ACCESS_protected:
15094 fnp->is_protected = 1;
15095 break;
c906108c
SS
15096 }
15097
b02dede2 15098 /* Check for artificial methods. */
e142c38c 15099 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15100 if (attr && DW_UNSND (attr) != 0)
15101 fnp->is_artificial = 1;
15102
e35000a7
TBA
15103 /* Check for defaulted methods. */
15104 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15105 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15106 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15107
15108 /* Check for deleted methods. */
15109 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15110 if (attr != nullptr && DW_UNSND (attr) != 0)
15111 fnp->is_deleted = 1;
15112
7d27a96d
TT
15113 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15114
0d564a31 15115 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15116 function. For older versions of GCC, this is an offset in the
15117 appropriate virtual table, as specified by DW_AT_containing_type.
15118 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15119 to the object address. */
15120
e142c38c 15121 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15122 if (attr != nullptr)
8e19ed76 15123 {
4fc6c0d5 15124 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15125 {
aec5aa8b
TT
15126 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15127 {
15128 /* Old-style GCC. */
15129 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15130 }
15131 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15132 || (DW_BLOCK (attr)->size > 1
15133 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15134 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15135 {
aec5aa8b
TT
15136 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15137 if ((fnp->voffset % cu->header.addr_size) != 0)
15138 dwarf2_complex_location_expr_complaint ();
15139 else
15140 fnp->voffset /= cu->header.addr_size;
15141 fnp->voffset += 2;
15142 }
15143 else
15144 dwarf2_complex_location_expr_complaint ();
15145
15146 if (!fnp->fcontext)
7e993ebf
KS
15147 {
15148 /* If there is no `this' field and no DW_AT_containing_type,
15149 we cannot actually find a base class context for the
15150 vtable! */
1f704f76 15151 if (this_type->num_fields () == 0
7e993ebf
KS
15152 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15153 {
b98664d3 15154 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15155 "function \"%s\" (offset %s)"),
15156 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15157 }
15158 else
15159 {
15160 fnp->fcontext
15161 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15162 }
15163 }
aec5aa8b 15164 }
cd6c91b4 15165 else if (attr->form_is_section_offset ())
8e19ed76 15166 {
4d3c2250 15167 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15168 }
15169 else
15170 {
4d3c2250
KB
15171 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15172 fieldname);
8e19ed76 15173 }
0d564a31 15174 }
d48cc9dd
DJ
15175 else
15176 {
15177 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15178 if (attr && DW_UNSND (attr))
15179 {
15180 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15181 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15182 "but the vtable offset is not specified"),
9d8780f0 15183 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15184 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15185 TYPE_CPLUS_DYNAMIC (type) = 1;
15186 }
15187 }
c906108c
SS
15188}
15189
15190/* Create the vector of member function fields, and attach it to the type. */
15191
15192static void
fba45db2 15193dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15194 struct dwarf2_cu *cu)
c906108c 15195{
b4ba55a1 15196 if (cu->language == language_ada)
a73c6dcd 15197 error (_("unexpected member functions in Ada type"));
b4ba55a1 15198
c906108c
SS
15199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15200 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15201 TYPE_ALLOC (type,
15202 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15203
be2daae6 15204 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15205 {
be2daae6 15206 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15207 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15208
be2daae6
TT
15209 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15210 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15211 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15212 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15213
15214 for (int k = 0; k < nf.fnfields.size (); ++k)
15215 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15216 }
15217
be2daae6 15218 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15219}
15220
1168df01
JB
15221/* Returns non-zero if NAME is the name of a vtable member in CU's
15222 language, zero otherwise. */
15223static int
15224is_vtable_name (const char *name, struct dwarf2_cu *cu)
15225{
15226 static const char vptr[] = "_vptr";
15227
9c37b5ae
TT
15228 /* Look for the C++ form of the vtable. */
15229 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15230 return 1;
15231
15232 return 0;
15233}
15234
c0dd20ea 15235/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15236 functions, with the ABI-specified layout. If TYPE describes
15237 such a structure, smash it into a member function type.
61049d3b
DJ
15238
15239 GCC shouldn't do this; it should just output pointer to member DIEs.
15240 This is GCC PR debug/28767. */
c0dd20ea 15241
0b92b5bb
TT
15242static void
15243quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15244{
09e2d7c7 15245 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15246
15247 /* Check for a structure with no name and two children. */
1f704f76 15248 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15249 return;
c0dd20ea
DJ
15250
15251 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15252 if (TYPE_FIELD_NAME (type, 0) == NULL
15253 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15254 || TYPE_FIELD_NAME (type, 1) == NULL
15255 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15256 return;
c0dd20ea
DJ
15257
15258 /* Find the type of the method. */
0b92b5bb 15259 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15260 if (pfn_type == NULL
78134374
SM
15261 || pfn_type->code () != TYPE_CODE_PTR
15262 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15263 return;
c0dd20ea
DJ
15264
15265 /* Look for the "this" argument. */
15266 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15267 if (pfn_type->num_fields () == 0
0b92b5bb 15268 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15269 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15270 return;
c0dd20ea 15271
09e2d7c7 15272 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15273 new_type = alloc_type (objfile);
09e2d7c7 15274 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15275 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15276 TYPE_VARARGS (pfn_type));
0b92b5bb 15277 smash_to_methodptr_type (type, new_type);
c0dd20ea 15278}
1168df01 15279
2b4424c3
TT
15280/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15281 appropriate error checking and issuing complaints if there is a
15282 problem. */
15283
15284static ULONGEST
15285get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15286{
15287 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15288
15289 if (attr == nullptr)
15290 return 0;
15291
cd6c91b4 15292 if (!attr->form_is_constant ())
2b4424c3 15293 {
b98664d3 15294 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15295 " - DIE at %s [in module %s]"),
15296 sect_offset_str (die->sect_off),
5e22e966 15297 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15298 return 0;
15299 }
15300
15301 ULONGEST align;
15302 if (attr->form == DW_FORM_sdata)
15303 {
15304 LONGEST val = DW_SND (attr);
15305 if (val < 0)
15306 {
b98664d3 15307 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15308 " - DIE at %s [in module %s]"),
15309 sect_offset_str (die->sect_off),
5e22e966 15310 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15311 return 0;
15312 }
15313 align = val;
15314 }
15315 else
15316 align = DW_UNSND (attr);
15317
15318 if (align == 0)
15319 {
b98664d3 15320 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15321 " - DIE at %s [in module %s]"),
15322 sect_offset_str (die->sect_off),
5e22e966 15323 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15324 return 0;
15325 }
15326 if ((align & (align - 1)) != 0)
15327 {
b98664d3 15328 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15329 " - DIE at %s [in module %s]"),
15330 sect_offset_str (die->sect_off),
5e22e966 15331 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15332 return 0;
15333 }
15334
15335 return align;
15336}
15337
15338/* If the DIE has a DW_AT_alignment attribute, use its value to set
15339 the alignment for TYPE. */
15340
15341static void
15342maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15343 struct type *type)
15344{
15345 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15346 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15347 " - DIE at %s [in module %s]"),
15348 sect_offset_str (die->sect_off),
5e22e966 15349 objfile_name (cu->per_objfile->objfile));
2b4424c3 15350}
685b1105 15351
e35000a7
TBA
15352/* Check if the given VALUE is a valid enum dwarf_calling_convention
15353 constant for a type, according to DWARF5 spec, Table 5.5. */
15354
15355static bool
15356is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15357{
15358 switch (value)
15359 {
15360 case DW_CC_normal:
15361 case DW_CC_pass_by_reference:
15362 case DW_CC_pass_by_value:
15363 return true;
15364
15365 default:
15366 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15367 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15368 return false;
15369 }
15370}
15371
d0922fcf
TBA
15372/* Check if the given VALUE is a valid enum dwarf_calling_convention
15373 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15374 also according to GNU-specific values (see include/dwarf2.h). */
15375
15376static bool
15377is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15378{
15379 switch (value)
15380 {
15381 case DW_CC_normal:
15382 case DW_CC_program:
15383 case DW_CC_nocall:
15384 return true;
15385
15386 case DW_CC_GNU_renesas_sh:
15387 case DW_CC_GNU_borland_fastcall_i386:
15388 case DW_CC_GDB_IBM_OpenCL:
15389 return true;
15390
15391 default:
15392 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15393 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15394 return false;
15395 }
15396}
15397
c906108c 15398/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15399 (definition) to create a type for the structure or union. Fill in
15400 the type's name and general properties; the members will not be
83655187
DE
15401 processed until process_structure_scope. A symbol table entry for
15402 the type will also not be done until process_structure_scope (assuming
15403 the type has a name).
c906108c 15404
c767944b
DJ
15405 NOTE: we need to call these functions regardless of whether or not the
15406 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15407 structure or union. This gets the type entered into our set of
83655187 15408 user defined types. */
c906108c 15409
f792889a 15410static struct type *
134d01f1 15411read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15412{
5e22e966 15413 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15414 struct type *type;
15415 struct attribute *attr;
15d034d0 15416 const char *name;
c906108c 15417
348e048f
DE
15418 /* If the definition of this type lives in .debug_types, read that type.
15419 Don't follow DW_AT_specification though, that will take us back up
15420 the chain and we want to go down. */
052c8bb8 15421 attr = die->attr (DW_AT_signature);
435d3d88 15422 if (attr != nullptr)
348e048f 15423 {
ac9ec31b 15424 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15425
ac9ec31b 15426 /* The type's CU may not be the same as CU.
02142a6c 15427 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15428 return set_die_type (die, type, cu);
15429 }
15430
c0dd20ea 15431 type = alloc_type (objfile);
c906108c 15432 INIT_CPLUS_SPECIFIC (type);
93311388 15433
39cbfefa
DJ
15434 name = dwarf2_name (die, cu);
15435 if (name != NULL)
c906108c 15436 {
987504bb 15437 if (cu->language == language_cplus
c44af4eb
TT
15438 || cu->language == language_d
15439 || cu->language == language_rust)
63d06c5c 15440 {
15d034d0 15441 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15442
15443 /* dwarf2_full_name might have already finished building the DIE's
15444 type. If so, there is no need to continue. */
15445 if (get_die_type (die, cu) != NULL)
15446 return get_die_type (die, cu);
15447
d0e39ea2 15448 type->set_name (full_name);
63d06c5c
DC
15449 }
15450 else
15451 {
d8151005
DJ
15452 /* The name is already allocated along with this objfile, so
15453 we don't need to duplicate it for the type. */
d0e39ea2 15454 type->set_name (name);
63d06c5c 15455 }
c906108c
SS
15456 }
15457
15458 if (die->tag == DW_TAG_structure_type)
15459 {
67607e24 15460 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15461 }
15462 else if (die->tag == DW_TAG_union_type)
15463 {
67607e24 15464 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15465 }
15466 else
15467 {
67607e24 15468 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15469 }
15470
0cc2414c
TT
15471 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15472 TYPE_DECLARED_CLASS (type) = 1;
15473
e35000a7
TBA
15474 /* Store the calling convention in the type if it's available in
15475 the die. Otherwise the calling convention remains set to
15476 the default value DW_CC_normal. */
15477 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15478 if (attr != nullptr
15479 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15480 {
15481 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15482 TYPE_CPLUS_CALLING_CONVENTION (type)
15483 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15484 }
15485
e142c38c 15486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15487 if (attr != nullptr)
c906108c 15488 {
cd6c91b4 15489 if (attr->form_is_constant ())
155bfbd3
JB
15490 TYPE_LENGTH (type) = DW_UNSND (attr);
15491 else
15492 {
f8e89861
TT
15493 struct dynamic_prop prop;
15494 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15495 cu->per_cu->addr_type ()))
5c54719c 15496 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15497 TYPE_LENGTH (type) = 0;
15498 }
c906108c
SS
15499 }
15500 else
15501 {
15502 TYPE_LENGTH (type) = 0;
15503 }
15504
2b4424c3
TT
15505 maybe_set_alignment (cu, die, type);
15506
5230b05a 15507 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15508 {
5230b05a
WT
15509 /* ICC<14 does not output the required DW_AT_declaration on
15510 incomplete types, but gives them a size of zero. */
422b1cb0 15511 TYPE_STUB (type) = 1;
685b1105
JK
15512 }
15513 else
15514 TYPE_STUB_SUPPORTED (type) = 1;
15515
dc718098 15516 if (die_is_declaration (die, cu))
876cecd0 15517 TYPE_STUB (type) = 1;
a6c727b2
DJ
15518 else if (attr == NULL && die->child == NULL
15519 && producer_is_realview (cu->producer))
15520 /* RealView does not output the required DW_AT_declaration
15521 on incomplete types. */
15522 TYPE_STUB (type) = 1;
dc718098 15523
c906108c
SS
15524 /* We need to add the type field to the die immediately so we don't
15525 infinitely recurse when dealing with pointers to the structure
0963b4bd 15526 type within the structure itself. */
1c379e20 15527 set_die_type (die, type, cu);
c906108c 15528
7e314c57
JK
15529 /* set_die_type should be already done. */
15530 set_descriptive_type (type, die, cu);
15531
c767944b
DJ
15532 return type;
15533}
15534
9c6a1327
TT
15535static void handle_struct_member_die
15536 (struct die_info *child_die,
15537 struct type *type,
15538 struct field_info *fi,
15539 std::vector<struct symbol *> *template_args,
15540 struct dwarf2_cu *cu);
15541
15542/* A helper for handle_struct_member_die that handles
15543 DW_TAG_variant_part. */
15544
15545static void
15546handle_variant_part (struct die_info *die, struct type *type,
15547 struct field_info *fi,
15548 std::vector<struct symbol *> *template_args,
15549 struct dwarf2_cu *cu)
15550{
15551 variant_part_builder *new_part;
15552 if (fi->current_variant_part == nullptr)
15553 {
15554 fi->variant_parts.emplace_back ();
15555 new_part = &fi->variant_parts.back ();
15556 }
15557 else if (!fi->current_variant_part->processing_variant)
15558 {
15559 complaint (_("nested DW_TAG_variant_part seen "
15560 "- DIE at %s [in module %s]"),
15561 sect_offset_str (die->sect_off),
5e22e966 15562 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15563 return;
15564 }
15565 else
15566 {
15567 variant_field &current = fi->current_variant_part->variants.back ();
15568 current.variant_parts.emplace_back ();
15569 new_part = &current.variant_parts.back ();
15570 }
15571
15572 /* When we recurse, we want callees to add to this new variant
15573 part. */
15574 scoped_restore save_current_variant_part
15575 = make_scoped_restore (&fi->current_variant_part, new_part);
15576
15577 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15578 if (discr == NULL)
15579 {
15580 /* It's a univariant form, an extension we support. */
15581 }
15582 else if (discr->form_is_ref ())
15583 {
15584 struct dwarf2_cu *target_cu = cu;
15585 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15586
15587 new_part->discriminant_offset = target_die->sect_off;
15588 }
15589 else
15590 {
15591 complaint (_("DW_AT_discr does not have DIE reference form"
15592 " - DIE at %s [in module %s]"),
15593 sect_offset_str (die->sect_off),
5e22e966 15594 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15595 }
15596
15597 for (die_info *child_die = die->child;
15598 child_die != NULL;
15599 child_die = child_die->sibling)
15600 handle_struct_member_die (child_die, type, fi, template_args, cu);
15601}
15602
15603/* A helper for handle_struct_member_die that handles
15604 DW_TAG_variant. */
15605
15606static void
15607handle_variant (struct die_info *die, struct type *type,
15608 struct field_info *fi,
15609 std::vector<struct symbol *> *template_args,
15610 struct dwarf2_cu *cu)
15611{
15612 if (fi->current_variant_part == nullptr)
15613 {
15614 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15615 "- DIE at %s [in module %s]"),
15616 sect_offset_str (die->sect_off),
5e22e966 15617 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15618 return;
15619 }
15620 if (fi->current_variant_part->processing_variant)
15621 {
15622 complaint (_("nested DW_TAG_variant seen "
15623 "- DIE at %s [in module %s]"),
15624 sect_offset_str (die->sect_off),
5e22e966 15625 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15626 return;
15627 }
15628
15629 scoped_restore save_processing_variant
15630 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15631 true);
15632
15633 fi->current_variant_part->variants.emplace_back ();
15634 variant_field &variant = fi->current_variant_part->variants.back ();
15635 variant.first_field = fi->fields.size ();
15636
15637 /* In a variant we want to get the discriminant and also add a
15638 field for our sole member child. */
15639 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15640 if (discr == nullptr)
15641 {
15642 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15643 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15644 variant.default_branch = true;
15645 else
15646 variant.discr_list_data = DW_BLOCK (discr);
15647 }
15648 else
15649 variant.discriminant_value = DW_UNSND (discr);
15650
15651 for (die_info *variant_child = die->child;
15652 variant_child != NULL;
15653 variant_child = variant_child->sibling)
15654 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15655
15656 variant.last_field = fi->fields.size ();
15657}
15658
2ddeaf8a
TT
15659/* A helper for process_structure_scope that handles a single member
15660 DIE. */
15661
15662static void
15663handle_struct_member_die (struct die_info *child_die, struct type *type,
15664 struct field_info *fi,
15665 std::vector<struct symbol *> *template_args,
15666 struct dwarf2_cu *cu)
15667{
15668 if (child_die->tag == DW_TAG_member
9c6a1327 15669 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15670 {
15671 /* NOTE: carlton/2002-11-05: A C++ static data member
15672 should be a DW_TAG_member that is a declaration, but
15673 all versions of G++ as of this writing (so through at
15674 least 3.2.1) incorrectly generate DW_TAG_variable
15675 tags for them instead. */
15676 dwarf2_add_field (fi, child_die, cu);
15677 }
15678 else if (child_die->tag == DW_TAG_subprogram)
15679 {
15680 /* Rust doesn't have member functions in the C++ sense.
15681 However, it does emit ordinary functions as children
15682 of a struct DIE. */
15683 if (cu->language == language_rust)
15684 read_func_scope (child_die, cu);
15685 else
15686 {
15687 /* C++ member function. */
15688 dwarf2_add_member_fn (fi, child_die, type, cu);
15689 }
15690 }
15691 else if (child_die->tag == DW_TAG_inheritance)
15692 {
15693 /* C++ base class field. */
15694 dwarf2_add_field (fi, child_die, cu);
15695 }
15696 else if (type_can_define_types (child_die))
15697 dwarf2_add_type_defn (fi, child_die, cu);
15698 else if (child_die->tag == DW_TAG_template_type_param
15699 || child_die->tag == DW_TAG_template_value_param)
15700 {
15701 struct symbol *arg = new_symbol (child_die, NULL, cu);
15702
15703 if (arg != NULL)
15704 template_args->push_back (arg);
15705 }
9c6a1327
TT
15706 else if (child_die->tag == DW_TAG_variant_part)
15707 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15708 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15709 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15710}
15711
c767944b
DJ
15712/* Finish creating a structure or union type, including filling in
15713 its members and creating a symbol for it. */
15714
15715static void
15716process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15717{
5e22e966 15718 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15719 struct die_info *child_die;
c767944b
DJ
15720 struct type *type;
15721
15722 type = get_die_type (die, cu);
15723 if (type == NULL)
15724 type = read_structure_type (die, cu);
15725
3e1d3d8c 15726 bool has_template_parameters = false;
e142c38c 15727 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15728 {
15729 struct field_info fi;
2f4732b0 15730 std::vector<struct symbol *> template_args;
c906108c 15731
639d11d3 15732 child_die = die->child;
c906108c
SS
15733
15734 while (child_die && child_die->tag)
15735 {
2ddeaf8a 15736 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15737 child_die = child_die->sibling;
c906108c
SS
15738 }
15739
34eaf542 15740 /* Attach template arguments to type. */
2f4732b0 15741 if (!template_args.empty ())
34eaf542 15742 {
3e1d3d8c 15743 has_template_parameters = true;
34eaf542 15744 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15745 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15746 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15747 = XOBNEWVEC (&objfile->objfile_obstack,
15748 struct symbol *,
15749 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15750 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15751 template_args.data (),
34eaf542
TT
15752 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15753 * sizeof (struct symbol *)));
34eaf542
TT
15754 }
15755
c906108c 15756 /* Attach fields and member functions to the type. */
317f7127 15757 if (fi.nfields () > 0)
e7c27a73 15758 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15759 if (!fi.fnfieldlists.empty ())
c906108c 15760 {
e7c27a73 15761 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15762
c5aa993b 15763 /* Get the type which refers to the base class (possibly this
c906108c 15764 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15765 class from the DW_AT_containing_type attribute. This use of
15766 DW_AT_containing_type is a GNU extension. */
c906108c 15767
e142c38c 15768 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15769 {
e7c27a73 15770 struct type *t = die_containing_type (die, cu);
c906108c 15771
ae6ae975 15772 set_type_vptr_basetype (type, t);
c906108c
SS
15773 if (type == t)
15774 {
c906108c
SS
15775 int i;
15776
15777 /* Our own class provides vtbl ptr. */
1f704f76 15778 for (i = t->num_fields () - 1;
c906108c
SS
15779 i >= TYPE_N_BASECLASSES (t);
15780 --i)
15781 {
0d5cff50 15782 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15783
1168df01 15784 if (is_vtable_name (fieldname, cu))
c906108c 15785 {
ae6ae975 15786 set_type_vptr_fieldno (type, i);
c906108c
SS
15787 break;
15788 }
15789 }
15790
15791 /* Complain if virtual function table field not found. */
15792 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15793 complaint (_("virtual function table pointer "
3e43a32a 15794 "not found when defining class '%s'"),
7d93a1e0 15795 type->name () ? type->name () : "");
c906108c
SS
15796 }
15797 else
15798 {
ae6ae975 15799 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15800 }
15801 }
f6235d4c 15802 else if (cu->producer
61012eef 15803 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15804 {
15805 /* The IBM XLC compiler does not provide direct indication
15806 of the containing type, but the vtable pointer is
15807 always named __vfp. */
15808
15809 int i;
15810
1f704f76 15811 for (i = type->num_fields () - 1;
f6235d4c
EZ
15812 i >= TYPE_N_BASECLASSES (type);
15813 --i)
15814 {
15815 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15816 {
ae6ae975
DE
15817 set_type_vptr_fieldno (type, i);
15818 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15819 break;
15820 }
15821 }
15822 }
c906108c 15823 }
98751a41
JK
15824
15825 /* Copy fi.typedef_field_list linked list elements content into the
15826 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15827 if (!fi.typedef_field_list.empty ())
98751a41 15828 {
be2daae6 15829 int count = fi.typedef_field_list.size ();
98751a41 15830
a0d7a4ff 15831 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15832 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15833 = ((struct decl_field *)
be2daae6
TT
15834 TYPE_ALLOC (type,
15835 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15836 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15837
be2daae6
TT
15838 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15839 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15840 }
c767944b 15841
883fd55a
KS
15842 /* Copy fi.nested_types_list linked list elements content into the
15843 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15844 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15845 {
be2daae6 15846 int count = fi.nested_types_list.size ();
883fd55a
KS
15847
15848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15849 TYPE_NESTED_TYPES_ARRAY (type)
15850 = ((struct decl_field *)
be2daae6
TT
15851 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15852 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15853
be2daae6
TT
15854 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15855 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15856 }
c906108c 15857 }
63d06c5c 15858
bb5ed363 15859 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15860 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15861 cu->rust_unions.push_back (type);
0b92b5bb 15862
90aeadfc
DC
15863 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15864 snapshots) has been known to create a die giving a declaration
15865 for a class that has, as a child, a die giving a definition for a
15866 nested class. So we have to process our children even if the
15867 current die is a declaration. Normally, of course, a declaration
15868 won't have any children at all. */
134d01f1 15869
ca040673
DE
15870 child_die = die->child;
15871
90aeadfc
DC
15872 while (child_die != NULL && child_die->tag)
15873 {
15874 if (child_die->tag == DW_TAG_member
15875 || child_die->tag == DW_TAG_variable
34eaf542
TT
15876 || child_die->tag == DW_TAG_inheritance
15877 || child_die->tag == DW_TAG_template_value_param
15878 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15879 {
90aeadfc 15880 /* Do nothing. */
134d01f1 15881 }
90aeadfc
DC
15882 else
15883 process_die (child_die, cu);
134d01f1 15884
436c571c 15885 child_die = child_die->sibling;
134d01f1
DJ
15886 }
15887
fa4028e9
JB
15888 /* Do not consider external references. According to the DWARF standard,
15889 these DIEs are identified by the fact that they have no byte_size
15890 attribute, and a declaration attribute. */
15891 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15892 || !die_is_declaration (die, cu)
15893 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15894 {
15895 struct symbol *sym = new_symbol (die, type, cu);
15896
15897 if (has_template_parameters)
15898 {
a776957c
TT
15899 struct symtab *symtab;
15900 if (sym != nullptr)
15901 symtab = symbol_symtab (sym);
15902 else if (cu->line_header != nullptr)
15903 {
15904 /* Any related symtab will do. */
15905 symtab
7ba99d21 15906 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15907 }
15908 else
15909 {
15910 symtab = nullptr;
15911 complaint (_("could not find suitable "
15912 "symtab for template parameter"
15913 " - DIE at %s [in module %s]"),
15914 sect_offset_str (die->sect_off),
15915 objfile_name (objfile));
15916 }
15917
15918 if (symtab != nullptr)
15919 {
15920 /* Make sure that the symtab is set on the new symbols.
15921 Even though they don't appear in this symtab directly,
15922 other parts of gdb assume that symbols do, and this is
15923 reasonably true. */
15924 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15925 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15926 }
3e1d3d8c
TT
15927 }
15928 }
134d01f1
DJ
15929}
15930
ed6acedd
TT
15931/* Assuming DIE is an enumeration type, and TYPE is its associated
15932 type, update TYPE using some information only available in DIE's
15933 children. In particular, the fields are computed. */
55426c9d
JB
15934
15935static void
15936update_enumeration_type_from_children (struct die_info *die,
15937 struct type *type,
15938 struct dwarf2_cu *cu)
15939{
60f7655a 15940 struct die_info *child_die;
55426c9d
JB
15941 int unsigned_enum = 1;
15942 int flag_enum = 1;
55426c9d 15943
8268c778 15944 auto_obstack obstack;
ed6acedd 15945 std::vector<struct field> fields;
55426c9d 15946
60f7655a
DE
15947 for (child_die = die->child;
15948 child_die != NULL && child_die->tag;
436c571c 15949 child_die = child_die->sibling)
55426c9d
JB
15950 {
15951 struct attribute *attr;
15952 LONGEST value;
15953 const gdb_byte *bytes;
15954 struct dwarf2_locexpr_baton *baton;
15955 const char *name;
60f7655a 15956
55426c9d
JB
15957 if (child_die->tag != DW_TAG_enumerator)
15958 continue;
15959
15960 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15961 if (attr == NULL)
15962 continue;
15963
15964 name = dwarf2_name (child_die, cu);
15965 if (name == NULL)
15966 name = "<anonymous enumerator>";
15967
15968 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15969 &value, &bytes, &baton);
15970 if (value < 0)
15971 {
15972 unsigned_enum = 0;
15973 flag_enum = 0;
15974 }
55426c9d 15975 else
edd45eb0
SM
15976 {
15977 if (count_one_bits_ll (value) >= 2)
15978 flag_enum = 0;
edd45eb0 15979 }
55426c9d 15980
ed6acedd
TT
15981 fields.emplace_back ();
15982 struct field &field = fields.back ();
15983 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15984 SET_FIELD_ENUMVAL (field, value);
15985 }
15986
15987 if (!fields.empty ())
15988 {
5e33d5f4 15989 type->set_num_fields (fields.size ());
3cabb6b0
SM
15990 type->set_fields
15991 ((struct field *)
15992 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 15993 memcpy (type->fields (), fields.data (),
ed6acedd 15994 sizeof (struct field) * fields.size ());
55426c9d
JB
15995 }
15996
15997 if (unsigned_enum)
15998 TYPE_UNSIGNED (type) = 1;
15999 if (flag_enum)
16000 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16001}
16002
134d01f1
DJ
16003/* Given a DW_AT_enumeration_type die, set its type. We do not
16004 complete the type's fields yet, or create any symbols. */
c906108c 16005
f792889a 16006static struct type *
134d01f1 16007read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16008{
5e22e966 16009 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16010 struct type *type;
c906108c 16011 struct attribute *attr;
0114d602 16012 const char *name;
134d01f1 16013
348e048f
DE
16014 /* If the definition of this type lives in .debug_types, read that type.
16015 Don't follow DW_AT_specification though, that will take us back up
16016 the chain and we want to go down. */
052c8bb8 16017 attr = die->attr (DW_AT_signature);
435d3d88 16018 if (attr != nullptr)
348e048f 16019 {
ac9ec31b 16020 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16021
ac9ec31b 16022 /* The type's CU may not be the same as CU.
02142a6c 16023 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16024 return set_die_type (die, type, cu);
16025 }
16026
c906108c
SS
16027 type = alloc_type (objfile);
16028
67607e24 16029 type->set_code (TYPE_CODE_ENUM);
94af9270 16030 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16031 if (name != NULL)
d0e39ea2 16032 type->set_name (name);
c906108c 16033
0626fc76
TT
16034 attr = dwarf2_attr (die, DW_AT_type, cu);
16035 if (attr != NULL)
16036 {
16037 struct type *underlying_type = die_type (die, cu);
16038
16039 TYPE_TARGET_TYPE (type) = underlying_type;
16040 }
16041
e142c38c 16042 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16043 if (attr != nullptr)
c906108c
SS
16044 {
16045 TYPE_LENGTH (type) = DW_UNSND (attr);
16046 }
16047 else
16048 {
16049 TYPE_LENGTH (type) = 0;
16050 }
16051
2b4424c3
TT
16052 maybe_set_alignment (cu, die, type);
16053
137033e9
JB
16054 /* The enumeration DIE can be incomplete. In Ada, any type can be
16055 declared as private in the package spec, and then defined only
16056 inside the package body. Such types are known as Taft Amendment
16057 Types. When another package uses such a type, an incomplete DIE
16058 may be generated by the compiler. */
02eb380e 16059 if (die_is_declaration (die, cu))
876cecd0 16060 TYPE_STUB (type) = 1;
02eb380e 16061
0626fc76
TT
16062 /* If this type has an underlying type that is not a stub, then we
16063 may use its attributes. We always use the "unsigned" attribute
16064 in this situation, because ordinarily we guess whether the type
16065 is unsigned -- but the guess can be wrong and the underlying type
16066 can tell us the reality. However, we defer to a local size
16067 attribute if one exists, because this lets the compiler override
16068 the underlying type if needed. */
16069 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16070 {
9e7c9a03
HD
16071 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16072 underlying_type = check_typedef (underlying_type);
16073 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16074 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16075 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16076 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16077 && TYPE_RAW_ALIGN (underlying_type) != 0)
16078 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16079 }
16080
3d567982
TT
16081 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16082
ed6acedd
TT
16083 set_die_type (die, type, cu);
16084
16085 /* Finish the creation of this type by using the enum's children.
16086 Note that, as usual, this must come after set_die_type to avoid
16087 infinite recursion when trying to compute the names of the
16088 enumerators. */
16089 update_enumeration_type_from_children (die, type, cu);
16090
16091 return type;
134d01f1
DJ
16092}
16093
16094/* Given a pointer to a die which begins an enumeration, process all
16095 the dies that define the members of the enumeration, and create the
16096 symbol for the enumeration type.
16097
16098 NOTE: We reverse the order of the element list. */
16099
16100static void
16101process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16102{
f792889a 16103 struct type *this_type;
134d01f1 16104
f792889a
DJ
16105 this_type = get_die_type (die, cu);
16106 if (this_type == NULL)
16107 this_type = read_enumeration_type (die, cu);
9dc481d3 16108
639d11d3 16109 if (die->child != NULL)
c906108c 16110 {
9dc481d3 16111 struct die_info *child_die;
15d034d0 16112 const char *name;
9dc481d3 16113
639d11d3 16114 child_die = die->child;
c906108c
SS
16115 while (child_die && child_die->tag)
16116 {
16117 if (child_die->tag != DW_TAG_enumerator)
16118 {
e7c27a73 16119 process_die (child_die, cu);
c906108c
SS
16120 }
16121 else
16122 {
39cbfefa
DJ
16123 name = dwarf2_name (child_die, cu);
16124 if (name)
ed6acedd 16125 new_symbol (child_die, this_type, cu);
c906108c
SS
16126 }
16127
436c571c 16128 child_die = child_die->sibling;
c906108c 16129 }
c906108c 16130 }
134d01f1 16131
6c83ed52
TT
16132 /* If we are reading an enum from a .debug_types unit, and the enum
16133 is a declaration, and the enum is not the signatured type in the
16134 unit, then we do not want to add a symbol for it. Adding a
16135 symbol would in some cases obscure the true definition of the
16136 enum, giving users an incomplete type when the definition is
16137 actually available. Note that we do not want to do this for all
16138 enums which are just declarations, because C++0x allows forward
16139 enum declarations. */
3019eac3 16140 if (cu->per_cu->is_debug_types
6c83ed52
TT
16141 && die_is_declaration (die, cu))
16142 {
52dc124a 16143 struct signatured_type *sig_type;
6c83ed52 16144
c0f78cd4 16145 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16146 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16147 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16148 return;
16149 }
16150
f792889a 16151 new_symbol (die, this_type, cu);
c906108c
SS
16152}
16153
16154/* Extract all information from a DW_TAG_array_type DIE and put it in
16155 the DIE's type field. For now, this only handles one dimensional
16156 arrays. */
16157
f792889a 16158static struct type *
e7c27a73 16159read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16160{
5e22e966 16161 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16162 struct die_info *child_die;
7e314c57 16163 struct type *type;
c906108c 16164 struct type *element_type, *range_type, *index_type;
c906108c 16165 struct attribute *attr;
15d034d0 16166 const char *name;
a405673c 16167 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16168 unsigned int bit_stride = 0;
c906108c 16169
e7c27a73 16170 element_type = die_type (die, cu);
c906108c 16171
7e314c57
JK
16172 /* The die_type call above may have already set the type for this DIE. */
16173 type = get_die_type (die, cu);
16174 if (type)
16175 return type;
16176
dc53a7ad
JB
16177 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16178 if (attr != NULL)
a405673c
JB
16179 {
16180 int stride_ok;
09ba997f 16181 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16182
16183 byte_stride_prop
16184 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16185 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16186 prop_type);
a405673c
JB
16187 if (!stride_ok)
16188 {
b98664d3 16189 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16190 " - DIE at %s [in module %s]"),
16191 sect_offset_str (die->sect_off),
5e22e966 16192 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16193 /* Ignore this attribute. We will likely not be able to print
16194 arrays of this type correctly, but there is little we can do
16195 to help if we cannot read the attribute's value. */
16196 byte_stride_prop = NULL;
16197 }
16198 }
dc53a7ad
JB
16199
16200 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16201 if (attr != NULL)
16202 bit_stride = DW_UNSND (attr);
16203
c906108c
SS
16204 /* Irix 6.2 native cc creates array types without children for
16205 arrays with unspecified length. */
639d11d3 16206 if (die->child == NULL)
c906108c 16207 {
46bf5051 16208 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16209 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16210 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16211 byte_stride_prop, bit_stride);
f792889a 16212 return set_die_type (die, type, cu);
c906108c
SS
16213 }
16214
791afaa2 16215 std::vector<struct type *> range_types;
639d11d3 16216 child_die = die->child;
c906108c
SS
16217 while (child_die && child_die->tag)
16218 {
16219 if (child_die->tag == DW_TAG_subrange_type)
16220 {
f792889a 16221 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16222
f792889a 16223 if (child_type != NULL)
a02abb62 16224 {
0963b4bd
MS
16225 /* The range type was succesfully read. Save it for the
16226 array type creation. */
791afaa2 16227 range_types.push_back (child_type);
a02abb62 16228 }
c906108c 16229 }
436c571c 16230 child_die = child_die->sibling;
c906108c
SS
16231 }
16232
16233 /* Dwarf2 dimensions are output from left to right, create the
16234 necessary array types in backwards order. */
7ca2d3a3 16235
c906108c 16236 type = element_type;
7ca2d3a3
DL
16237
16238 if (read_array_order (die, cu) == DW_ORD_col_major)
16239 {
16240 int i = 0;
9a619af0 16241
791afaa2 16242 while (i < range_types.size ())
dc53a7ad 16243 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16244 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16245 }
16246 else
16247 {
791afaa2 16248 size_t ndim = range_types.size ();
7ca2d3a3 16249 while (ndim-- > 0)
dc53a7ad 16250 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16251 byte_stride_prop, bit_stride);
7ca2d3a3 16252 }
c906108c 16253
f5f8a009
EZ
16254 /* Understand Dwarf2 support for vector types (like they occur on
16255 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16256 array type. This is not part of the Dwarf2/3 standard yet, but a
16257 custom vendor extension. The main difference between a regular
16258 array and the vector variant is that vectors are passed by value
16259 to functions. */
e142c38c 16260 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16261 if (attr != nullptr)
ea37ba09 16262 make_vector_type (type);
f5f8a009 16263
dbc98a8b
KW
16264 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16265 implementation may choose to implement triple vectors using this
16266 attribute. */
16267 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16268 if (attr != nullptr)
dbc98a8b
KW
16269 {
16270 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16271 TYPE_LENGTH (type) = DW_UNSND (attr);
16272 else
b98664d3 16273 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16274 "than the total size of elements"));
dbc98a8b
KW
16275 }
16276
39cbfefa
DJ
16277 name = dwarf2_name (die, cu);
16278 if (name)
d0e39ea2 16279 type->set_name (name);
6e70227d 16280
2b4424c3
TT
16281 maybe_set_alignment (cu, die, type);
16282
0963b4bd 16283 /* Install the type in the die. */
7e314c57
JK
16284 set_die_type (die, type, cu);
16285
16286 /* set_die_type should be already done. */
b4ba55a1
JB
16287 set_descriptive_type (type, die, cu);
16288
7e314c57 16289 return type;
c906108c
SS
16290}
16291
7ca2d3a3 16292static enum dwarf_array_dim_ordering
6e70227d 16293read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16294{
16295 struct attribute *attr;
16296
16297 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16298
435d3d88 16299 if (attr != nullptr)
aead7601 16300 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16301
0963b4bd
MS
16302 /* GNU F77 is a special case, as at 08/2004 array type info is the
16303 opposite order to the dwarf2 specification, but data is still
16304 laid out as per normal fortran.
7ca2d3a3 16305
0963b4bd
MS
16306 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16307 version checking. */
7ca2d3a3 16308
905e0470
PM
16309 if (cu->language == language_fortran
16310 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16311 {
16312 return DW_ORD_row_major;
16313 }
16314
6e70227d 16315 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16316 {
16317 case array_column_major:
16318 return DW_ORD_col_major;
16319 case array_row_major:
16320 default:
16321 return DW_ORD_row_major;
16322 };
16323}
16324
72019c9c 16325/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16326 the DIE's type field. */
72019c9c 16327
f792889a 16328static struct type *
72019c9c
GM
16329read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16330{
7e314c57
JK
16331 struct type *domain_type, *set_type;
16332 struct attribute *attr;
f792889a 16333
7e314c57
JK
16334 domain_type = die_type (die, cu);
16335
16336 /* The die_type call above may have already set the type for this DIE. */
16337 set_type = get_die_type (die, cu);
16338 if (set_type)
16339 return set_type;
16340
16341 set_type = create_set_type (NULL, domain_type);
16342
16343 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16344 if (attr != nullptr)
d09039dd 16345 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16346
2b4424c3
TT
16347 maybe_set_alignment (cu, die, set_type);
16348
f792889a 16349 return set_die_type (die, set_type, cu);
72019c9c 16350}
7ca2d3a3 16351
0971de02
TT
16352/* A helper for read_common_block that creates a locexpr baton.
16353 SYM is the symbol which we are marking as computed.
16354 COMMON_DIE is the DIE for the common block.
16355 COMMON_LOC is the location expression attribute for the common
16356 block itself.
16357 MEMBER_LOC is the location expression attribute for the particular
16358 member of the common block that we are processing.
16359 CU is the CU from which the above come. */
16360
16361static void
16362mark_common_block_symbol_computed (struct symbol *sym,
16363 struct die_info *common_die,
16364 struct attribute *common_loc,
16365 struct attribute *member_loc,
16366 struct dwarf2_cu *cu)
16367{
5e22e966 16368 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16369 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16370 struct dwarf2_locexpr_baton *baton;
16371 gdb_byte *ptr;
16372 unsigned int cu_off;
08feed99 16373 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16374 LONGEST offset = 0;
16375
16376 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16377 gdb_assert (common_loc->form_is_block ());
16378 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16379 || member_loc->form_is_constant ());
0971de02 16380
8d749320 16381 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16382 baton->per_objfile = per_objfile;
0971de02
TT
16383 baton->per_cu = cu->per_cu;
16384 gdb_assert (baton->per_cu);
16385
16386 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16387
cd6c91b4 16388 if (member_loc->form_is_constant ())
0971de02 16389 {
0826b30a 16390 offset = member_loc->constant_value (0);
0971de02
TT
16391 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16392 }
16393 else
16394 baton->size += DW_BLOCK (member_loc)->size;
16395
224c3ddb 16396 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16397 baton->data = ptr;
16398
16399 *ptr++ = DW_OP_call4;
9c541725 16400 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16401 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16402 ptr += 4;
16403
cd6c91b4 16404 if (member_loc->form_is_constant ())
0971de02
TT
16405 {
16406 *ptr++ = DW_OP_addr;
16407 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16408 ptr += cu->header.addr_size;
16409 }
16410 else
16411 {
16412 /* We have to copy the data here, because DW_OP_call4 will only
16413 use a DW_AT_location attribute. */
16414 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16415 ptr += DW_BLOCK (member_loc)->size;
16416 }
16417
16418 *ptr++ = DW_OP_plus;
16419 gdb_assert (ptr - baton->data == baton->size);
16420
0971de02 16421 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16422 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16423}
16424
4357ac6c
TT
16425/* Create appropriate locally-scoped variables for all the
16426 DW_TAG_common_block entries. Also create a struct common_block
16427 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16428 is used to separate the common blocks name namespace from regular
4357ac6c 16429 variable names. */
c906108c
SS
16430
16431static void
e7c27a73 16432read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16433{
0971de02
TT
16434 struct attribute *attr;
16435
16436 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16437 if (attr != nullptr)
0971de02
TT
16438 {
16439 /* Support the .debug_loc offsets. */
4fc6c0d5 16440 if (attr->form_is_block ())
0971de02
TT
16441 {
16442 /* Ok. */
16443 }
cd6c91b4 16444 else if (attr->form_is_section_offset ())
0971de02
TT
16445 {
16446 dwarf2_complex_location_expr_complaint ();
16447 attr = NULL;
16448 }
16449 else
16450 {
16451 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16452 "common block member");
16453 attr = NULL;
16454 }
16455 }
16456
639d11d3 16457 if (die->child != NULL)
c906108c 16458 {
5e22e966 16459 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16460 struct die_info *child_die;
16461 size_t n_entries = 0, size;
16462 struct common_block *common_block;
16463 struct symbol *sym;
74ac6d43 16464
4357ac6c
TT
16465 for (child_die = die->child;
16466 child_die && child_die->tag;
436c571c 16467 child_die = child_die->sibling)
4357ac6c
TT
16468 ++n_entries;
16469
16470 size = (sizeof (struct common_block)
16471 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16472 common_block
16473 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16474 size);
4357ac6c
TT
16475 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16476 common_block->n_entries = 0;
16477
16478 for (child_die = die->child;
16479 child_die && child_die->tag;
436c571c 16480 child_die = child_die->sibling)
4357ac6c
TT
16481 {
16482 /* Create the symbol in the DW_TAG_common_block block in the current
16483 symbol scope. */
e7c27a73 16484 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16485 if (sym != NULL)
16486 {
16487 struct attribute *member_loc;
16488
16489 common_block->contents[common_block->n_entries++] = sym;
16490
16491 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16492 cu);
16493 if (member_loc)
16494 {
16495 /* GDB has handled this for a long time, but it is
16496 not specified by DWARF. It seems to have been
16497 emitted by gfortran at least as recently as:
16498 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16499 complaint (_("Variable in common block has "
0971de02 16500 "DW_AT_data_member_location "
9d8780f0
SM
16501 "- DIE at %s [in module %s]"),
16502 sect_offset_str (child_die->sect_off),
518817b3 16503 objfile_name (objfile));
0971de02 16504
cd6c91b4 16505 if (member_loc->form_is_section_offset ())
0971de02 16506 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16507 else if (member_loc->form_is_constant ()
4fc6c0d5 16508 || member_loc->form_is_block ())
0971de02 16509 {
435d3d88 16510 if (attr != nullptr)
0971de02
TT
16511 mark_common_block_symbol_computed (sym, die, attr,
16512 member_loc, cu);
16513 }
16514 else
16515 dwarf2_complex_location_expr_complaint ();
16516 }
16517 }
c906108c 16518 }
4357ac6c
TT
16519
16520 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16521 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16522 }
16523}
16524
0114d602 16525/* Create a type for a C++ namespace. */
d9fa45fe 16526
0114d602
DJ
16527static struct type *
16528read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16529{
5e22e966 16530 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16531 const char *previous_prefix, *name;
9219021c 16532 int is_anonymous;
0114d602
DJ
16533 struct type *type;
16534
16535 /* For extensions, reuse the type of the original namespace. */
16536 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16537 {
16538 struct die_info *ext_die;
16539 struct dwarf2_cu *ext_cu = cu;
9a619af0 16540
0114d602
DJ
16541 ext_die = dwarf2_extension (die, &ext_cu);
16542 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16543
16544 /* EXT_CU may not be the same as CU.
02142a6c 16545 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16546 return set_die_type (die, type, cu);
16547 }
9219021c 16548
e142c38c 16549 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16550
16551 /* Now build the name of the current namespace. */
16552
0114d602
DJ
16553 previous_prefix = determine_prefix (die, cu);
16554 if (previous_prefix[0] != '\0')
16555 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16556 previous_prefix, name, 0, cu);
0114d602
DJ
16557
16558 /* Create the type. */
19f392bc 16559 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16560
60531b24 16561 return set_die_type (die, type, cu);
0114d602
DJ
16562}
16563
22cee43f 16564/* Read a namespace scope. */
0114d602
DJ
16565
16566static void
16567read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16568{
5e22e966 16569 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16570 int is_anonymous;
9219021c 16571
5c4e30ca
DC
16572 /* Add a symbol associated to this if we haven't seen the namespace
16573 before. Also, add a using directive if it's an anonymous
16574 namespace. */
9219021c 16575
f2f0e013 16576 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16577 {
16578 struct type *type;
16579
0114d602 16580 type = read_type_die (die, cu);
e7c27a73 16581 new_symbol (die, type, cu);
5c4e30ca 16582
e8e80198 16583 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16584 if (is_anonymous)
0114d602
DJ
16585 {
16586 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16587
eb1e02fd 16588 std::vector<const char *> excludes;
804d2729 16589 add_using_directive (using_directives (cu),
7d93a1e0 16590 previous_prefix, type->name (), NULL,
eb1e02fd 16591 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16592 }
5c4e30ca 16593 }
9219021c 16594
639d11d3 16595 if (die->child != NULL)
d9fa45fe 16596 {
639d11d3 16597 struct die_info *child_die = die->child;
6e70227d 16598
d9fa45fe
DC
16599 while (child_die && child_die->tag)
16600 {
e7c27a73 16601 process_die (child_die, cu);
436c571c 16602 child_die = child_die->sibling;
d9fa45fe
DC
16603 }
16604 }
38d518c9
EZ
16605}
16606
f55ee35c
JK
16607/* Read a Fortran module as type. This DIE can be only a declaration used for
16608 imported module. Still we need that type as local Fortran "use ... only"
16609 declaration imports depend on the created type in determine_prefix. */
16610
16611static struct type *
16612read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16613{
5e22e966 16614 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16615 const char *module_name;
f55ee35c
JK
16616 struct type *type;
16617
16618 module_name = dwarf2_name (die, cu);
19f392bc 16619 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16620
f55ee35c
JK
16621 return set_die_type (die, type, cu);
16622}
16623
5d7cb8df
JK
16624/* Read a Fortran module. */
16625
16626static void
16627read_module (struct die_info *die, struct dwarf2_cu *cu)
16628{
16629 struct die_info *child_die = die->child;
530e8392
KB
16630 struct type *type;
16631
16632 type = read_type_die (die, cu);
16633 new_symbol (die, type, cu);
5d7cb8df 16634
5d7cb8df
JK
16635 while (child_die && child_die->tag)
16636 {
16637 process_die (child_die, cu);
436c571c 16638 child_die = child_die->sibling;
5d7cb8df
JK
16639 }
16640}
16641
38d518c9
EZ
16642/* Return the name of the namespace represented by DIE. Set
16643 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16644 namespace. */
16645
16646static const char *
e142c38c 16647namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16648{
16649 struct die_info *current_die;
16650 const char *name = NULL;
16651
16652 /* Loop through the extensions until we find a name. */
16653
16654 for (current_die = die;
16655 current_die != NULL;
f2f0e013 16656 current_die = dwarf2_extension (die, &cu))
38d518c9 16657 {
96553a0c
DE
16658 /* We don't use dwarf2_name here so that we can detect the absence
16659 of a name -> anonymous namespace. */
7d45c7c3 16660 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16661
38d518c9
EZ
16662 if (name != NULL)
16663 break;
16664 }
16665
16666 /* Is it an anonymous namespace? */
16667
16668 *is_anonymous = (name == NULL);
16669 if (*is_anonymous)
2b1dbab0 16670 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16671
16672 return name;
d9fa45fe
DC
16673}
16674
c906108c
SS
16675/* Extract all information from a DW_TAG_pointer_type DIE and add to
16676 the user defined type vector. */
16677
f792889a 16678static struct type *
e7c27a73 16679read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16680{
5e22e966 16681 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16682 struct comp_unit_head *cu_header = &cu->header;
c906108c 16683 struct type *type;
8b2dbe47
KB
16684 struct attribute *attr_byte_size;
16685 struct attribute *attr_address_class;
16686 int byte_size, addr_class;
7e314c57
JK
16687 struct type *target_type;
16688
16689 target_type = die_type (die, cu);
c906108c 16690
7e314c57
JK
16691 /* The die_type call above may have already set the type for this DIE. */
16692 type = get_die_type (die, cu);
16693 if (type)
16694 return type;
16695
16696 type = lookup_pointer_type (target_type);
8b2dbe47 16697
e142c38c 16698 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16699 if (attr_byte_size)
16700 byte_size = DW_UNSND (attr_byte_size);
c906108c 16701 else
8b2dbe47
KB
16702 byte_size = cu_header->addr_size;
16703
e142c38c 16704 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16705 if (attr_address_class)
16706 addr_class = DW_UNSND (attr_address_class);
16707 else
16708 addr_class = DW_ADDR_none;
16709
2b4424c3
TT
16710 ULONGEST alignment = get_alignment (cu, die);
16711
16712 /* If the pointer size, alignment, or address class is different
16713 than the default, create a type variant marked as such and set
16714 the length accordingly. */
16715 if (TYPE_LENGTH (type) != byte_size
16716 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16717 && alignment != TYPE_RAW_ALIGN (type))
16718 || addr_class != DW_ADDR_none)
c906108c 16719 {
5e2b427d 16720 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16721 {
16722 int type_flags;
16723
849957d9 16724 type_flags = gdbarch_address_class_type_flags
5e2b427d 16725 (gdbarch, byte_size, addr_class);
876cecd0
TT
16726 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16727 == 0);
8b2dbe47
KB
16728 type = make_type_with_address_space (type, type_flags);
16729 }
16730 else if (TYPE_LENGTH (type) != byte_size)
16731 {
b98664d3 16732 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16733 }
2b4424c3
TT
16734 else if (TYPE_RAW_ALIGN (type) != alignment)
16735 {
b98664d3 16736 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16737 " - DIE at %s [in module %s]"),
16738 sect_offset_str (die->sect_off),
5e22e966 16739 objfile_name (cu->per_objfile->objfile));
2b4424c3 16740 }
6e70227d 16741 else
9a619af0
MS
16742 {
16743 /* Should we also complain about unhandled address classes? */
16744 }
c906108c 16745 }
8b2dbe47
KB
16746
16747 TYPE_LENGTH (type) = byte_size;
2b4424c3 16748 set_type_align (type, alignment);
f792889a 16749 return set_die_type (die, type, cu);
c906108c
SS
16750}
16751
16752/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16753 the user defined type vector. */
16754
f792889a 16755static struct type *
e7c27a73 16756read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16757{
16758 struct type *type;
16759 struct type *to_type;
16760 struct type *domain;
16761
e7c27a73
DJ
16762 to_type = die_type (die, cu);
16763 domain = die_containing_type (die, cu);
0d5de010 16764
7e314c57
JK
16765 /* The calls above may have already set the type for this DIE. */
16766 type = get_die_type (die, cu);
16767 if (type)
16768 return type;
16769
78134374 16770 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16771 type = lookup_methodptr_type (to_type);
78134374 16772 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16773 {
5e22e966 16774 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16775
16776 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16777 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16778 TYPE_VARARGS (to_type));
16779 type = lookup_methodptr_type (new_type);
16780 }
0d5de010
DJ
16781 else
16782 type = lookup_memberptr_type (to_type, domain);
c906108c 16783
f792889a 16784 return set_die_type (die, type, cu);
c906108c
SS
16785}
16786
4297a3f0 16787/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16788 the user defined type vector. */
16789
f792889a 16790static struct type *
4297a3f0
AV
16791read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16792 enum type_code refcode)
c906108c 16793{
e7c27a73 16794 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16795 struct type *type, *target_type;
c906108c
SS
16796 struct attribute *attr;
16797
4297a3f0
AV
16798 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16799
7e314c57
JK
16800 target_type = die_type (die, cu);
16801
16802 /* The die_type call above may have already set the type for this DIE. */
16803 type = get_die_type (die, cu);
16804 if (type)
16805 return type;
16806
4297a3f0 16807 type = lookup_reference_type (target_type, refcode);
e142c38c 16808 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16809 if (attr != nullptr)
c906108c
SS
16810 {
16811 TYPE_LENGTH (type) = DW_UNSND (attr);
16812 }
16813 else
16814 {
107d2387 16815 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16816 }
2b4424c3 16817 maybe_set_alignment (cu, die, type);
f792889a 16818 return set_die_type (die, type, cu);
c906108c
SS
16819}
16820
cf363f18
MW
16821/* Add the given cv-qualifiers to the element type of the array. GCC
16822 outputs DWARF type qualifiers that apply to an array, not the
16823 element type. But GDB relies on the array element type to carry
16824 the cv-qualifiers. This mimics section 6.7.3 of the C99
16825 specification. */
16826
16827static struct type *
16828add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16829 struct type *base_type, int cnst, int voltl)
16830{
16831 struct type *el_type, *inner_array;
16832
16833 base_type = copy_type (base_type);
16834 inner_array = base_type;
16835
78134374 16836 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16837 {
16838 TYPE_TARGET_TYPE (inner_array) =
16839 copy_type (TYPE_TARGET_TYPE (inner_array));
16840 inner_array = TYPE_TARGET_TYPE (inner_array);
16841 }
16842
16843 el_type = TYPE_TARGET_TYPE (inner_array);
16844 cnst |= TYPE_CONST (el_type);
16845 voltl |= TYPE_VOLATILE (el_type);
16846 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16847
16848 return set_die_type (die, base_type, cu);
16849}
16850
f792889a 16851static struct type *
e7c27a73 16852read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16853{
f792889a 16854 struct type *base_type, *cv_type;
c906108c 16855
e7c27a73 16856 base_type = die_type (die, cu);
7e314c57
JK
16857
16858 /* The die_type call above may have already set the type for this DIE. */
16859 cv_type = get_die_type (die, cu);
16860 if (cv_type)
16861 return cv_type;
16862
2f608a3a
KW
16863 /* In case the const qualifier is applied to an array type, the element type
16864 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16865 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16866 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16867
f792889a
DJ
16868 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16869 return set_die_type (die, cv_type, cu);
c906108c
SS
16870}
16871
f792889a 16872static struct type *
e7c27a73 16873read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16874{
f792889a 16875 struct type *base_type, *cv_type;
c906108c 16876
e7c27a73 16877 base_type = die_type (die, cu);
7e314c57
JK
16878
16879 /* The die_type call above may have already set the type for this DIE. */
16880 cv_type = get_die_type (die, cu);
16881 if (cv_type)
16882 return cv_type;
16883
cf363f18
MW
16884 /* In case the volatile qualifier is applied to an array type, the
16885 element type is so qualified, not the array type (section 6.7.3
16886 of C99). */
78134374 16887 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16888 return add_array_cv_type (die, cu, base_type, 0, 1);
16889
f792889a
DJ
16890 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16891 return set_die_type (die, cv_type, cu);
c906108c
SS
16892}
16893
06d66ee9
TT
16894/* Handle DW_TAG_restrict_type. */
16895
16896static struct type *
16897read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16898{
16899 struct type *base_type, *cv_type;
16900
16901 base_type = die_type (die, cu);
16902
16903 /* The die_type call above may have already set the type for this DIE. */
16904 cv_type = get_die_type (die, cu);
16905 if (cv_type)
16906 return cv_type;
16907
16908 cv_type = make_restrict_type (base_type);
16909 return set_die_type (die, cv_type, cu);
16910}
16911
a2c2acaf
MW
16912/* Handle DW_TAG_atomic_type. */
16913
16914static struct type *
16915read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16916{
16917 struct type *base_type, *cv_type;
16918
16919 base_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 cv_type = get_die_type (die, cu);
16923 if (cv_type)
16924 return cv_type;
16925
16926 cv_type = make_atomic_type (base_type);
16927 return set_die_type (die, cv_type, cu);
16928}
16929
c906108c
SS
16930/* Extract all information from a DW_TAG_string_type DIE and add to
16931 the user defined type vector. It isn't really a user defined type,
16932 but it behaves like one, with other DIE's using an AT_user_def_type
16933 attribute to reference it. */
16934
f792889a 16935static struct type *
e7c27a73 16936read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16937{
5e22e966 16938 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16939 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16940 struct type *type, *range_type, *index_type, *char_type;
16941 struct attribute *attr;
216a7e6b
AB
16942 struct dynamic_prop prop;
16943 bool length_is_constant = true;
16944 LONGEST length;
16945
16946 /* There are a couple of places where bit sizes might be made use of
16947 when parsing a DW_TAG_string_type, however, no producer that we know
16948 of make use of these. Handling bit sizes that are a multiple of the
16949 byte size is easy enough, but what about other bit sizes? Lets deal
16950 with that problem when we have to. Warn about these attributes being
16951 unsupported, then parse the type and ignore them like we always
16952 have. */
16953 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16954 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16955 {
16956 static bool warning_printed = false;
16957 if (!warning_printed)
16958 {
16959 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16960 "currently supported on DW_TAG_string_type."));
16961 warning_printed = true;
16962 }
16963 }
c906108c 16964
e142c38c 16965 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16966 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16967 {
16968 /* The string length describes the location at which the length of
16969 the string can be found. The size of the length field can be
16970 specified with one of the attributes below. */
16971 struct type *prop_type;
16972 struct attribute *len
16973 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16974 if (len == nullptr)
16975 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16976 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16977 {
16978 /* Pass 0 as the default as we know this attribute is constant
16979 and the default value will not be returned. */
0826b30a 16980 LONGEST sz = len->constant_value (0);
09ba997f 16981 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16982 }
16983 else
16984 {
16985 /* If the size is not specified then we assume it is the size of
16986 an address on this target. */
09ba997f 16987 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16988 }
16989
16990 /* Convert the attribute into a dynamic property. */
16991 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16992 length = 1;
16993 else
16994 length_is_constant = false;
16995 }
16996 else if (attr != nullptr)
16997 {
16998 /* This DW_AT_string_length just contains the length with no
16999 indirection. There's no need to create a dynamic property in this
17000 case. Pass 0 for the default value as we know it will not be
17001 returned in this case. */
0826b30a 17002 length = attr->constant_value (0);
216a7e6b
AB
17003 }
17004 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17005 {
216a7e6b 17006 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17007 length = attr->constant_value (1);
c906108c
SS
17008 }
17009 else
17010 {
216a7e6b
AB
17011 /* Use 1 as a fallback length if we have nothing else. */
17012 length = 1;
c906108c 17013 }
6ccb9162 17014
46bf5051 17015 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17016 if (length_is_constant)
17017 range_type = create_static_range_type (NULL, index_type, 1, length);
17018 else
17019 {
17020 struct dynamic_prop low_bound;
17021
17022 low_bound.kind = PROP_CONST;
17023 low_bound.data.const_val = 1;
17024 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17025 }
3b7538c0
UW
17026 char_type = language_string_char_type (cu->language_defn, gdbarch);
17027 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17028
f792889a 17029 return set_die_type (die, type, cu);
c906108c
SS
17030}
17031
4d804846
JB
17032/* Assuming that DIE corresponds to a function, returns nonzero
17033 if the function is prototyped. */
17034
17035static int
17036prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17037{
17038 struct attribute *attr;
17039
17040 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17041 if (attr && (DW_UNSND (attr) != 0))
17042 return 1;
17043
17044 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17045 is only meaningful for C, but the concept also extends to other
4d804846
JB
17046 languages that allow unprototyped functions (Eg: Objective C).
17047 For all other languages, assume that functions are always
17048 prototyped. */
17049 if (cu->language != language_c
17050 && cu->language != language_objc
17051 && cu->language != language_opencl)
17052 return 1;
17053
17054 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17055 prototyped and unprototyped functions; default to prototyped,
17056 since that is more common in modern code (and RealView warns
17057 about unprototyped functions). */
17058 if (producer_is_realview (cu->producer))
17059 return 1;
17060
17061 return 0;
17062}
17063
c906108c
SS
17064/* Handle DIES due to C code like:
17065
17066 struct foo
c5aa993b
JM
17067 {
17068 int (*funcp)(int a, long l);
17069 int b;
17070 };
c906108c 17071
0963b4bd 17072 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17073
f792889a 17074static struct type *
e7c27a73 17075read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17076{
5e22e966 17077 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17078 struct type *type; /* Type that this function returns. */
17079 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17080 struct attribute *attr;
17081
e7c27a73 17082 type = die_type (die, cu);
7e314c57
JK
17083
17084 /* The die_type call above may have already set the type for this DIE. */
17085 ftype = get_die_type (die, cu);
17086 if (ftype)
17087 return ftype;
17088
0c8b41f1 17089 ftype = lookup_function_type (type);
c906108c 17090
4d804846 17091 if (prototyped_function_p (die, cu))
a6c727b2 17092 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17093
c055b101
CV
17094 /* Store the calling convention in the type if it's available in
17095 the subroutine die. Otherwise set the calling convention to
17096 the default value DW_CC_normal. */
17097 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17098 if (attr != nullptr
17099 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17100 TYPE_CALLING_CONVENTION (ftype)
17101 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17102 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17103 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17104 else
17105 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17106
743649fd
MW
17107 /* Record whether the function returns normally to its caller or not
17108 if the DWARF producer set that information. */
17109 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17110 if (attr && (DW_UNSND (attr) != 0))
17111 TYPE_NO_RETURN (ftype) = 1;
17112
76c10ea2
GM
17113 /* We need to add the subroutine type to the die immediately so
17114 we don't infinitely recurse when dealing with parameters
0963b4bd 17115 declared as the same subroutine type. */
76c10ea2 17116 set_die_type (die, ftype, cu);
6e70227d 17117
639d11d3 17118 if (die->child != NULL)
c906108c 17119 {
bb5ed363 17120 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17121 struct die_info *child_die;
8072405b 17122 int nparams, iparams;
c906108c
SS
17123
17124 /* Count the number of parameters.
17125 FIXME: GDB currently ignores vararg functions, but knows about
17126 vararg member functions. */
8072405b 17127 nparams = 0;
639d11d3 17128 child_die = die->child;
c906108c
SS
17129 while (child_die && child_die->tag)
17130 {
17131 if (child_die->tag == DW_TAG_formal_parameter)
17132 nparams++;
17133 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17134 TYPE_VARARGS (ftype) = 1;
436c571c 17135 child_die = child_die->sibling;
c906108c
SS
17136 }
17137
17138 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17139 ftype->set_num_fields (nparams);
3cabb6b0
SM
17140 ftype->set_fields
17141 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17142
8072405b
JK
17143 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17144 even if we error out during the parameters reading below. */
17145 for (iparams = 0; iparams < nparams; iparams++)
17146 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17147
17148 iparams = 0;
639d11d3 17149 child_die = die->child;
c906108c
SS
17150 while (child_die && child_die->tag)
17151 {
17152 if (child_die->tag == DW_TAG_formal_parameter)
17153 {
3ce3b1ba
PA
17154 struct type *arg_type;
17155
17156 /* DWARF version 2 has no clean way to discern C++
17157 static and non-static member functions. G++ helps
17158 GDB by marking the first parameter for non-static
17159 member functions (which is the this pointer) as
17160 artificial. We pass this information to
17161 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17162
17163 DWARF version 3 added DW_AT_object_pointer, which GCC
17164 4.5 does not yet generate. */
e142c38c 17165 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17166 if (attr != nullptr)
c906108c
SS
17167 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17168 else
9c37b5ae 17169 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17170 arg_type = die_type (child_die, cu);
17171
17172 /* RealView does not mark THIS as const, which the testsuite
17173 expects. GCC marks THIS as const in method definitions,
17174 but not in the class specifications (GCC PR 43053). */
17175 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17176 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17177 {
17178 int is_this = 0;
17179 struct dwarf2_cu *arg_cu = cu;
17180 const char *name = dwarf2_name (child_die, cu);
17181
17182 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17183 if (attr != nullptr)
3ce3b1ba
PA
17184 {
17185 /* If the compiler emits this, use it. */
17186 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17187 is_this = 1;
17188 }
17189 else if (name && strcmp (name, "this") == 0)
17190 /* Function definitions will have the argument names. */
17191 is_this = 1;
17192 else if (name == NULL && iparams == 0)
17193 /* Declarations may not have the names, so like
17194 elsewhere in GDB, assume an artificial first
17195 argument is "this". */
17196 is_this = 1;
17197
17198 if (is_this)
17199 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17200 arg_type, 0);
17201 }
17202
17203 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17204 iparams++;
17205 }
436c571c 17206 child_die = child_die->sibling;
c906108c
SS
17207 }
17208 }
17209
76c10ea2 17210 return ftype;
c906108c
SS
17211}
17212
f792889a 17213static struct type *
e7c27a73 17214read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17215{
5e22e966 17216 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17217 const char *name = NULL;
3c8e0968 17218 struct type *this_type, *target_type;
c906108c 17219
94af9270 17220 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17221 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17222 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17223 set_die_type (die, this_type, cu);
3c8e0968
DE
17224 target_type = die_type (die, cu);
17225 if (target_type != this_type)
17226 TYPE_TARGET_TYPE (this_type) = target_type;
17227 else
17228 {
17229 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17230 spec and cause infinite loops in GDB. */
b98664d3 17231 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17232 "- DIE at %s [in module %s]"),
17233 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17234 TYPE_TARGET_TYPE (this_type) = NULL;
17235 }
e4003a34
TV
17236 if (name == NULL)
17237 {
17238 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17239 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17240 Handle these by just returning the target type, rather than
17241 constructing an anonymous typedef type and trying to handle this
17242 elsewhere. */
17243 set_die_type (die, target_type, cu);
17244 return target_type;
17245 }
f792889a 17246 return this_type;
c906108c
SS
17247}
17248
9b790ce7
UW
17249/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17250 (which may be different from NAME) to the architecture back-end to allow
17251 it to guess the correct format if necessary. */
17252
17253static struct type *
17254dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17255 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17256{
08feed99 17257 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17258 const struct floatformat **format;
17259 struct type *type;
17260
17261 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17262 if (format)
103a685e 17263 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17264 else
77b7c781 17265 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17266
17267 return type;
17268}
17269
eb77c9df
AB
17270/* Allocate an integer type of size BITS and name NAME. */
17271
17272static struct type *
17273dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17274 int bits, int unsigned_p, const char *name)
17275{
17276 struct type *type;
17277
17278 /* Versions of Intel's C Compiler generate an integer type called "void"
17279 instead of using DW_TAG_unspecified_type. This has been seen on
17280 at least versions 14, 17, and 18. */
35ee2dc2
AB
17281 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17282 && strcmp (name, "void") == 0)
eb77c9df
AB
17283 type = objfile_type (objfile)->builtin_void;
17284 else
17285 type = init_integer_type (objfile, bits, unsigned_p, name);
17286
17287 return type;
17288}
17289
8bdc1658
AB
17290/* Initialise and return a floating point type of size BITS suitable for
17291 use as a component of a complex number. The NAME_HINT is passed through
17292 when initialising the floating point type and is the name of the complex
17293 type.
17294
17295 As DWARF doesn't currently provide an explicit name for the components
17296 of a complex number, but it can be helpful to have these components
17297 named, we try to select a suitable name based on the size of the
17298 component. */
17299static struct type *
17300dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17301 struct objfile *objfile,
103a685e
TT
17302 int bits, const char *name_hint,
17303 enum bfd_endian byte_order)
8bdc1658 17304{
08feed99 17305 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17306 struct type *tt = nullptr;
17307
35add35e
AB
17308 /* Try to find a suitable floating point builtin type of size BITS.
17309 We're going to use the name of this type as the name for the complex
17310 target type that we are about to create. */
1db455a7 17311 switch (cu->language)
8bdc1658 17312 {
1db455a7
AB
17313 case language_fortran:
17314 switch (bits)
17315 {
17316 case 32:
17317 tt = builtin_f_type (gdbarch)->builtin_real;
17318 break;
17319 case 64:
17320 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17321 break;
17322 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17323 case 128:
17324 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17325 break;
17326 }
8bdc1658 17327 break;
1db455a7
AB
17328 default:
17329 switch (bits)
17330 {
17331 case 32:
17332 tt = builtin_type (gdbarch)->builtin_float;
17333 break;
17334 case 64:
17335 tt = builtin_type (gdbarch)->builtin_double;
17336 break;
17337 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17338 case 128:
17339 tt = builtin_type (gdbarch)->builtin_long_double;
17340 break;
17341 }
8bdc1658
AB
17342 break;
17343 }
17344
35add35e
AB
17345 /* If the type we found doesn't match the size we were looking for, then
17346 pretend we didn't find a type at all, the complex target type we
17347 create will then be nameless. */
a12e5744 17348 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17349 tt = nullptr;
17350
7d93a1e0 17351 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17352 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17353}
17354
c906108c
SS
17355/* Find a representation of a given base type and install
17356 it in the TYPE field of the die. */
17357
f792889a 17358static struct type *
e7c27a73 17359read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17360{
5e22e966 17361 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17362 struct type *type;
17363 struct attribute *attr;
19f392bc 17364 int encoding = 0, bits = 0;
15d034d0 17365 const char *name;
34877895 17366 gdbarch *arch;
c906108c 17367
e142c38c 17368 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17369 if (attr != nullptr)
34877895 17370 encoding = DW_UNSND (attr);
e142c38c 17371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17372 if (attr != nullptr)
34877895 17373 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17374 name = dwarf2_name (die, cu);
6ccb9162 17375 if (!name)
34877895 17376 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17377
08feed99 17378 arch = objfile->arch ();
103a685e
TT
17379 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17380
34877895
PJ
17381 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17382 if (attr)
103a685e
TT
17383 {
17384 int endianity = DW_UNSND (attr);
17385
17386 switch (endianity)
17387 {
17388 case DW_END_big:
17389 byte_order = BFD_ENDIAN_BIG;
17390 break;
17391 case DW_END_little:
17392 byte_order = BFD_ENDIAN_LITTLE;
17393 break;
17394 default:
17395 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17396 break;
17397 }
17398 }
6ccb9162
UW
17399
17400 switch (encoding)
c906108c 17401 {
6ccb9162
UW
17402 case DW_ATE_address:
17403 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17404 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17405 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17406 break;
17407 case DW_ATE_boolean:
19f392bc 17408 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17409 break;
17410 case DW_ATE_complex_float:
103a685e
TT
17411 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17412 byte_order);
78134374 17413 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17414 {
17415 if (name == nullptr)
17416 {
17417 struct obstack *obstack
5e22e966 17418 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17419 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17420 nullptr);
17421 }
17422 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17423 }
17424 else
17425 type = init_complex_type (name, type);
6ccb9162
UW
17426 break;
17427 case DW_ATE_decimal_float:
19f392bc 17428 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17429 break;
17430 case DW_ATE_float:
103a685e 17431 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17432 break;
17433 case DW_ATE_signed:
eb77c9df 17434 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17435 break;
17436 case DW_ATE_unsigned:
3b2b8fea
TT
17437 if (cu->language == language_fortran
17438 && name
61012eef 17439 && startswith (name, "character("))
19f392bc
UW
17440 type = init_character_type (objfile, bits, 1, name);
17441 else
eb77c9df 17442 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17443 break;
17444 case DW_ATE_signed_char:
6e70227d 17445 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17446 || cu->language == language_pascal
17447 || cu->language == language_fortran)
19f392bc
UW
17448 type = init_character_type (objfile, bits, 0, name);
17449 else
eb77c9df 17450 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17451 break;
17452 case DW_ATE_unsigned_char:
868a0084 17453 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17454 || cu->language == language_pascal
c44af4eb
TT
17455 || cu->language == language_fortran
17456 || cu->language == language_rust)
19f392bc
UW
17457 type = init_character_type (objfile, bits, 1, name);
17458 else
eb77c9df 17459 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17460 break;
75079b2b 17461 case DW_ATE_UTF:
53e710ac 17462 {
53e710ac
PA
17463 if (bits == 16)
17464 type = builtin_type (arch)->builtin_char16;
17465 else if (bits == 32)
17466 type = builtin_type (arch)->builtin_char32;
17467 else
17468 {
b98664d3 17469 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17470 bits);
eb77c9df 17471 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17472 }
17473 return set_die_type (die, type, cu);
17474 }
75079b2b
TT
17475 break;
17476
6ccb9162 17477 default:
b98664d3 17478 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17479 dwarf_type_encoding_name (encoding));
77b7c781 17480 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17481 break;
c906108c 17482 }
6ccb9162 17483
0114d602 17484 if (name && strcmp (name, "char") == 0)
876cecd0 17485 TYPE_NOSIGN (type) = 1;
0114d602 17486
2b4424c3
TT
17487 maybe_set_alignment (cu, die, type);
17488
103a685e 17489 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17490
f792889a 17491 return set_die_type (die, type, cu);
c906108c
SS
17492}
17493
80180f79
SA
17494/* Parse dwarf attribute if it's a block, reference or constant and put the
17495 resulting value of the attribute into struct bound_prop.
17496 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17497
17498static int
17499attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17500 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17501 struct type *default_type)
80180f79
SA
17502{
17503 struct dwarf2_property_baton *baton;
5e22e966 17504 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17505 struct objfile *objfile = per_objfile->objfile;
17506 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17507
9a49df9d
AB
17508 gdb_assert (default_type != NULL);
17509
80180f79
SA
17510 if (attr == NULL || prop == NULL)
17511 return 0;
17512
4fc6c0d5 17513 if (attr->form_is_block ())
80180f79 17514 {
8d749320 17515 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17516 baton->property_type = default_type;
80180f79 17517 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17518 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17519 baton->locexpr.size = DW_BLOCK (attr)->size;
17520 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17521 switch (attr->name)
17522 {
17523 case DW_AT_string_length:
17524 baton->locexpr.is_reference = true;
17525 break;
17526 default:
17527 baton->locexpr.is_reference = false;
17528 break;
17529 }
80180f79
SA
17530 prop->data.baton = baton;
17531 prop->kind = PROP_LOCEXPR;
17532 gdb_assert (prop->data.baton != NULL);
17533 }
cd6c91b4 17534 else if (attr->form_is_ref ())
80180f79
SA
17535 {
17536 struct dwarf2_cu *target_cu = cu;
17537 struct die_info *target_die;
17538 struct attribute *target_attr;
17539
17540 target_die = follow_die_ref (die, attr, &target_cu);
17541 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17542 if (target_attr == NULL)
17543 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17544 target_cu);
80180f79
SA
17545 if (target_attr == NULL)
17546 return 0;
17547
df25ebbd 17548 switch (target_attr->name)
80180f79 17549 {
df25ebbd 17550 case DW_AT_location:
cd6c91b4 17551 if (target_attr->form_is_section_offset ())
df25ebbd 17552 {
8d749320 17553 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17554 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17555 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17556 prop->data.baton = baton;
17557 prop->kind = PROP_LOCLIST;
17558 gdb_assert (prop->data.baton != NULL);
17559 }
4fc6c0d5 17560 else if (target_attr->form_is_block ())
df25ebbd 17561 {
8d749320 17562 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17563 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17564 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17565 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17566 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17567 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17568 baton->locexpr.is_reference = true;
df25ebbd
JB
17569 prop->data.baton = baton;
17570 prop->kind = PROP_LOCEXPR;
17571 gdb_assert (prop->data.baton != NULL);
17572 }
17573 else
17574 {
17575 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17576 "dynamic property");
17577 return 0;
17578 }
17579 break;
17580 case DW_AT_data_member_location:
17581 {
17582 LONGEST offset;
17583
17584 if (!handle_data_member_location (target_die, target_cu,
17585 &offset))
17586 return 0;
17587
8d749320 17588 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17589 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17590 target_cu);
df25ebbd
JB
17591 baton->offset_info.offset = offset;
17592 baton->offset_info.type = die_type (target_die, target_cu);
17593 prop->data.baton = baton;
17594 prop->kind = PROP_ADDR_OFFSET;
17595 break;
17596 }
80180f79
SA
17597 }
17598 }
cd6c91b4 17599 else if (attr->form_is_constant ())
80180f79 17600 {
0826b30a 17601 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17602 prop->kind = PROP_CONST;
17603 }
17604 else
17605 {
17606 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17607 dwarf2_name (die, cu));
17608 return 0;
17609 }
17610
17611 return 1;
17612}
17613
09ba997f 17614/* See read.h. */
9a49df9d 17615
09ba997f
TT
17616struct type *
17617dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17618{
09ba997f 17619 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17620 struct type *int_type;
17621
17622 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17623#define TRY_TYPE(F) \
17624 int_type = (unsigned_p \
17625 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17626 : objfile_type (objfile)->builtin_ ## F); \
17627 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17628 return int_type
17629
17630 TRY_TYPE (char);
17631 TRY_TYPE (short);
17632 TRY_TYPE (int);
17633 TRY_TYPE (long);
17634 TRY_TYPE (long_long);
17635
17636#undef TRY_TYPE
17637
17638 gdb_assert_not_reached ("unable to find suitable integer type");
17639}
17640
09ba997f 17641/* See read.h. */
11a8b164 17642
09ba997f
TT
17643struct type *
17644dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17645{
09ba997f
TT
17646 int addr_size = this->addr_size ();
17647 return int_type (addr_size, unsigned_p);
11a8b164
AB
17648}
17649
b86352cf
AB
17650/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17651 present (which is valid) then compute the default type based on the
17652 compilation units address size. */
17653
17654static struct type *
17655read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17656{
17657 struct type *index_type = die_type (die, cu);
17658
17659 /* Dwarf-2 specifications explicitly allows to create subrange types
17660 without specifying a base type.
17661 In that case, the base type must be set to the type of
17662 the lower bound, upper bound or count, in that order, if any of these
17663 three attributes references an object that has a type.
17664 If no base type is found, the Dwarf-2 specifications say that
17665 a signed integer type of size equal to the size of an address should
17666 be used.
17667 For the following C code: `extern char gdb_int [];'
17668 GCC produces an empty range DIE.
17669 FIXME: muller/2010-05-28: Possible references to object for low bound,
17670 high bound or count are not yet handled by this code. */
78134374 17671 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17672 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17673
17674 return index_type;
17675}
17676
a02abb62
JB
17677/* Read the given DW_AT_subrange DIE. */
17678
f792889a 17679static struct type *
a02abb62
JB
17680read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17681{
4c9ad8c2 17682 struct type *base_type, *orig_base_type;
a02abb62
JB
17683 struct type *range_type;
17684 struct attribute *attr;
729efb13 17685 struct dynamic_prop low, high;
4fae6e18 17686 int low_default_is_valid;
c451ebe5 17687 int high_bound_is_count = 0;
15d034d0 17688 const char *name;
d359392f 17689 ULONGEST negative_mask;
e77813c8 17690
b86352cf
AB
17691 orig_base_type = read_subrange_index_type (die, cu);
17692
4c9ad8c2
TT
17693 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17694 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17695 creating the range type, but we use the result of check_typedef
17696 when examining properties of the type. */
17697 base_type = check_typedef (orig_base_type);
a02abb62 17698
7e314c57
JK
17699 /* The die_type call above may have already set the type for this DIE. */
17700 range_type = get_die_type (die, cu);
17701 if (range_type)
17702 return range_type;
17703
729efb13
SA
17704 low.kind = PROP_CONST;
17705 high.kind = PROP_CONST;
17706 high.data.const_val = 0;
17707
4fae6e18
JK
17708 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17709 omitting DW_AT_lower_bound. */
17710 switch (cu->language)
6e70227d 17711 {
4fae6e18
JK
17712 case language_c:
17713 case language_cplus:
729efb13 17714 low.data.const_val = 0;
4fae6e18
JK
17715 low_default_is_valid = 1;
17716 break;
17717 case language_fortran:
729efb13 17718 low.data.const_val = 1;
4fae6e18
JK
17719 low_default_is_valid = 1;
17720 break;
17721 case language_d:
4fae6e18 17722 case language_objc:
c44af4eb 17723 case language_rust:
729efb13 17724 low.data.const_val = 0;
4fae6e18
JK
17725 low_default_is_valid = (cu->header.version >= 4);
17726 break;
17727 case language_ada:
17728 case language_m2:
17729 case language_pascal:
729efb13 17730 low.data.const_val = 1;
4fae6e18
JK
17731 low_default_is_valid = (cu->header.version >= 4);
17732 break;
17733 default:
729efb13 17734 low.data.const_val = 0;
4fae6e18
JK
17735 low_default_is_valid = 0;
17736 break;
a02abb62
JB
17737 }
17738
e142c38c 17739 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17740 if (attr != nullptr)
9a49df9d 17741 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17742 else if (!low_default_is_valid)
b98664d3 17743 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17744 "- DIE at %s [in module %s]"),
17745 sect_offset_str (die->sect_off),
5e22e966 17746 objfile_name (cu->per_objfile->objfile));
a02abb62 17747
506f5c41
TV
17748 struct attribute *attr_ub, *attr_count;
17749 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17750 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17751 {
506f5c41 17752 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17753 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17754 {
c451ebe5
SA
17755 /* If bounds are constant do the final calculation here. */
17756 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17757 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17758 else
17759 high_bound_is_count = 1;
c2ff108b 17760 }
506f5c41
TV
17761 else
17762 {
17763 if (attr_ub != NULL)
17764 complaint (_("Unresolved DW_AT_upper_bound "
17765 "- DIE at %s [in module %s]"),
17766 sect_offset_str (die->sect_off),
5e22e966 17767 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17768 if (attr_count != NULL)
17769 complaint (_("Unresolved DW_AT_count "
17770 "- DIE at %s [in module %s]"),
17771 sect_offset_str (die->sect_off),
5e22e966 17772 objfile_name (cu->per_objfile->objfile));
506f5c41 17773 }
e77813c8 17774 }
a02abb62 17775
4e962e74
TT
17776 LONGEST bias = 0;
17777 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17778 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17779 bias = bias_attr->constant_value (0);
4e962e74 17780
dbb9c2b1
JB
17781 /* Normally, the DWARF producers are expected to use a signed
17782 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17783 But this is unfortunately not always the case, as witnessed
17784 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17785 is used instead. To work around that ambiguity, we treat
17786 the bounds as signed, and thus sign-extend their values, when
17787 the base type is signed. */
6e70227d 17788 negative_mask =
d359392f 17789 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17790 if (low.kind == PROP_CONST
17791 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17792 low.data.const_val |= negative_mask;
17793 if (high.kind == PROP_CONST
17794 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17795 high.data.const_val |= negative_mask;
43bbcdc2 17796
5bbd8269
AB
17797 /* Check for bit and byte strides. */
17798 struct dynamic_prop byte_stride_prop;
17799 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17800 if (attr_byte_stride != nullptr)
17801 {
09ba997f 17802 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17803 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17804 prop_type);
17805 }
17806
17807 struct dynamic_prop bit_stride_prop;
17808 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17809 if (attr_bit_stride != nullptr)
17810 {
17811 /* It only makes sense to have either a bit or byte stride. */
17812 if (attr_byte_stride != nullptr)
17813 {
17814 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17815 "- DIE at %s [in module %s]"),
17816 sect_offset_str (die->sect_off),
5e22e966 17817 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17818 attr_bit_stride = nullptr;
17819 }
17820 else
17821 {
09ba997f 17822 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17823 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17824 prop_type);
17825 }
17826 }
17827
17828 if (attr_byte_stride != nullptr
17829 || attr_bit_stride != nullptr)
17830 {
17831 bool byte_stride_p = (attr_byte_stride != nullptr);
17832 struct dynamic_prop *stride
17833 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17834
17835 range_type
17836 = create_range_type_with_stride (NULL, orig_base_type, &low,
17837 &high, bias, stride, byte_stride_p);
17838 }
17839 else
17840 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17841
c451ebe5
SA
17842 if (high_bound_is_count)
17843 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17844
c2ff108b
JK
17845 /* Ada expects an empty array on no boundary attributes. */
17846 if (attr == NULL && cu->language != language_ada)
729efb13 17847 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17848
39cbfefa
DJ
17849 name = dwarf2_name (die, cu);
17850 if (name)
d0e39ea2 17851 range_type->set_name (name);
6e70227d 17852
e142c38c 17853 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17854 if (attr != nullptr)
a02abb62
JB
17855 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17856
2b4424c3
TT
17857 maybe_set_alignment (cu, die, range_type);
17858
7e314c57
JK
17859 set_die_type (die, range_type, cu);
17860
17861 /* set_die_type should be already done. */
b4ba55a1
JB
17862 set_descriptive_type (range_type, die, cu);
17863
7e314c57 17864 return range_type;
a02abb62 17865}
6e70227d 17866
f792889a 17867static struct type *
81a17f79
JB
17868read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17869{
17870 struct type *type;
81a17f79 17871
5e22e966 17872 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17873 type->set_name (dwarf2_name (die, cu));
81a17f79 17874
74a2f8ff 17875 /* In Ada, an unspecified type is typically used when the description
85102364 17876 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17877 such a type, we treat it as a stub, and try to resolve it later on,
17878 when needed. */
17879 if (cu->language == language_ada)
17880 TYPE_STUB (type) = 1;
17881
f792889a 17882 return set_die_type (die, type, cu);
81a17f79 17883}
a02abb62 17884
639d11d3
DC
17885/* Read a single die and all its descendents. Set the die's sibling
17886 field to NULL; set other fields in the die correctly, and set all
17887 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17888 location of the info_ptr after reading all of those dies. PARENT
17889 is the parent of the die in question. */
17890
17891static struct die_info *
dee91e82 17892read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17893 const gdb_byte *info_ptr,
17894 const gdb_byte **new_info_ptr,
dee91e82 17895 struct die_info *parent)
639d11d3
DC
17896{
17897 struct die_info *die;
d521ce57 17898 const gdb_byte *cur_ptr;
639d11d3 17899
3e225074 17900 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17901 if (die == NULL)
17902 {
17903 *new_info_ptr = cur_ptr;
17904 return NULL;
17905 }
93311388 17906 store_in_ref_table (die, reader->cu);
639d11d3 17907
3e225074 17908 if (die->has_children)
bf6af496 17909 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17910 else
17911 {
17912 die->child = NULL;
17913 *new_info_ptr = cur_ptr;
17914 }
17915
17916 die->sibling = NULL;
17917 die->parent = parent;
17918 return die;
17919}
17920
17921/* Read a die, all of its descendents, and all of its siblings; set
17922 all of the fields of all of the dies correctly. Arguments are as
17923 in read_die_and_children. */
17924
17925static struct die_info *
bf6af496 17926read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17927 const gdb_byte *info_ptr,
17928 const gdb_byte **new_info_ptr,
bf6af496 17929 struct die_info *parent)
639d11d3
DC
17930{
17931 struct die_info *first_die, *last_sibling;
d521ce57 17932 const gdb_byte *cur_ptr;
639d11d3 17933
c906108c 17934 cur_ptr = info_ptr;
639d11d3
DC
17935 first_die = last_sibling = NULL;
17936
17937 while (1)
c906108c 17938 {
639d11d3 17939 struct die_info *die
dee91e82 17940 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17941
1d325ec1 17942 if (die == NULL)
c906108c 17943 {
639d11d3
DC
17944 *new_info_ptr = cur_ptr;
17945 return first_die;
c906108c 17946 }
1d325ec1
DJ
17947
17948 if (!first_die)
17949 first_die = die;
c906108c 17950 else
1d325ec1
DJ
17951 last_sibling->sibling = die;
17952
17953 last_sibling = die;
c906108c 17954 }
c906108c
SS
17955}
17956
bf6af496
DE
17957/* Read a die, all of its descendents, and all of its siblings; set
17958 all of the fields of all of the dies correctly. Arguments are as
17959 in read_die_and_children.
17960 This the main entry point for reading a DIE and all its children. */
17961
17962static struct die_info *
17963read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17964 const gdb_byte *info_ptr,
17965 const gdb_byte **new_info_ptr,
bf6af496
DE
17966 struct die_info *parent)
17967{
17968 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17969 new_info_ptr, parent);
17970
b4f54984 17971 if (dwarf_die_debug)
bf6af496
DE
17972 {
17973 fprintf_unfiltered (gdb_stdlog,
17974 "Read die from %s@0x%x of %s:\n",
96b79293 17975 reader->die_section->get_name (),
bf6af496
DE
17976 (unsigned) (info_ptr - reader->die_section->buffer),
17977 bfd_get_filename (reader->abfd));
b4f54984 17978 dump_die (die, dwarf_die_debug);
bf6af496
DE
17979 }
17980
17981 return die;
17982}
17983
3019eac3
DE
17984/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17985 attributes.
17986 The caller is responsible for filling in the extra attributes
17987 and updating (*DIEP)->num_attrs.
17988 Set DIEP to point to a newly allocated die with its information,
3e225074 17989 except for its child, sibling, and parent fields. */
93311388 17990
d521ce57 17991static const gdb_byte *
3019eac3 17992read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17993 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17994 int num_extra_attrs)
93311388 17995{
b64f50a1 17996 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17997 struct abbrev_info *abbrev;
17998 struct die_info *die;
17999 struct dwarf2_cu *cu = reader->cu;
18000 bfd *abfd = reader->abfd;
18001
9c541725 18002 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18003 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18004 info_ptr += bytes_read;
18005 if (!abbrev_number)
18006 {
18007 *diep = NULL;
93311388
DE
18008 return info_ptr;
18009 }
18010
685af9cd 18011 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18012 if (!abbrev)
348e048f
DE
18013 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18014 abbrev_number,
18015 bfd_get_filename (abfd));
18016
3019eac3 18017 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18018 die->sect_off = sect_off;
93311388
DE
18019 die->tag = abbrev->tag;
18020 die->abbrev = abbrev_number;
3e225074 18021 die->has_children = abbrev->has_children;
93311388 18022
3019eac3
DE
18023 /* Make the result usable.
18024 The caller needs to update num_attrs after adding the extra
18025 attributes. */
93311388
DE
18026 die->num_attrs = abbrev->num_attrs;
18027
18a8505e 18028 std::vector<int> indexes_that_need_reprocess;
93311388 18029 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18030 {
18031 bool need_reprocess;
18032 info_ptr =
18033 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18034 info_ptr, &need_reprocess);
18035 if (need_reprocess)
18036 indexes_that_need_reprocess.push_back (i);
18037 }
18038
052c8bb8 18039 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18040 if (attr != nullptr)
18041 cu->str_offsets_base = DW_UNSND (attr);
93311388 18042
41144253 18043 attr = die->attr (DW_AT_loclists_base);
18044 if (attr != nullptr)
18045 cu->loclist_base = DW_UNSND (attr);
18046
a39fdb41 18047 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18048 if (maybe_addr_base.has_value ())
18049 cu->addr_base = *maybe_addr_base;
18050 for (int index : indexes_that_need_reprocess)
18051 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18052 *diep = die;
93311388
DE
18053 return info_ptr;
18054}
18055
3019eac3
DE
18056/* Read a die and all its attributes.
18057 Set DIEP to point to a newly allocated die with its information,
3e225074 18058 except for its child, sibling, and parent fields. */
3019eac3 18059
d521ce57 18060static const gdb_byte *
3019eac3 18061read_full_die (const struct die_reader_specs *reader,
3e225074 18062 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18063{
d521ce57 18064 const gdb_byte *result;
bf6af496 18065
3e225074 18066 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18067
b4f54984 18068 if (dwarf_die_debug)
bf6af496
DE
18069 {
18070 fprintf_unfiltered (gdb_stdlog,
18071 "Read die from %s@0x%x of %s:\n",
96b79293 18072 reader->die_section->get_name (),
bf6af496
DE
18073 (unsigned) (info_ptr - reader->die_section->buffer),
18074 bfd_get_filename (reader->abfd));
b4f54984 18075 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18076 }
18077
18078 return result;
3019eac3 18079}
433df2d4 18080\f
c906108c 18081
72bf9492
DJ
18082/* Returns nonzero if TAG represents a type that we might generate a partial
18083 symbol for. */
18084
18085static int
18086is_type_tag_for_partial (int tag)
18087{
18088 switch (tag)
18089 {
18090#if 0
18091 /* Some types that would be reasonable to generate partial symbols for,
18092 that we don't at present. */
18093 case DW_TAG_array_type:
18094 case DW_TAG_file_type:
18095 case DW_TAG_ptr_to_member_type:
18096 case DW_TAG_set_type:
18097 case DW_TAG_string_type:
18098 case DW_TAG_subroutine_type:
18099#endif
18100 case DW_TAG_base_type:
18101 case DW_TAG_class_type:
680b30c7 18102 case DW_TAG_interface_type:
72bf9492
DJ
18103 case DW_TAG_enumeration_type:
18104 case DW_TAG_structure_type:
18105 case DW_TAG_subrange_type:
18106 case DW_TAG_typedef:
18107 case DW_TAG_union_type:
18108 return 1;
18109 default:
18110 return 0;
18111 }
18112}
18113
18114/* Load all DIEs that are interesting for partial symbols into memory. */
18115
18116static struct partial_die_info *
dee91e82 18117load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18118 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18119{
dee91e82 18120 struct dwarf2_cu *cu = reader->cu;
5e22e966 18121 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18122 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18123 unsigned int bytes_read;
5afb4e99 18124 unsigned int load_all = 0;
72bf9492
DJ
18125 int nesting_level = 1;
18126
18127 parent_die = NULL;
18128 last_die = NULL;
18129
7adf1e79
DE
18130 gdb_assert (cu->per_cu != NULL);
18131 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18132 load_all = 1;
18133
72bf9492
DJ
18134 cu->partial_dies
18135 = htab_create_alloc_ex (cu->header.length / 12,
18136 partial_die_hash,
18137 partial_die_eq,
18138 NULL,
18139 &cu->comp_unit_obstack,
18140 hashtab_obstack_allocate,
18141 dummy_obstack_deallocate);
18142
72bf9492
DJ
18143 while (1)
18144 {
685af9cd 18145 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18146
18147 /* A NULL abbrev means the end of a series of children. */
18148 if (abbrev == NULL)
18149 {
18150 if (--nesting_level == 0)
cd9983dd
YQ
18151 return first_die;
18152
72bf9492
DJ
18153 info_ptr += bytes_read;
18154 last_die = parent_die;
18155 parent_die = parent_die->die_parent;
18156 continue;
18157 }
18158
98bfdba5
PA
18159 /* Check for template arguments. We never save these; if
18160 they're seen, we just mark the parent, and go on our way. */
18161 if (parent_die != NULL
18162 && cu->language == language_cplus
18163 && (abbrev->tag == DW_TAG_template_type_param
18164 || abbrev->tag == DW_TAG_template_value_param))
18165 {
18166 parent_die->has_template_arguments = 1;
18167
18168 if (!load_all)
18169 {
18170 /* We don't need a partial DIE for the template argument. */
dee91e82 18171 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18172 continue;
18173 }
18174 }
18175
0d99eb77 18176 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18177 Skip their other children. */
18178 if (!load_all
18179 && cu->language == language_cplus
18180 && parent_die != NULL
18181 && parent_die->tag == DW_TAG_subprogram)
18182 {
dee91e82 18183 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18184 continue;
18185 }
18186
5afb4e99
DJ
18187 /* Check whether this DIE is interesting enough to save. Normally
18188 we would not be interested in members here, but there may be
18189 later variables referencing them via DW_AT_specification (for
18190 static members). */
18191 if (!load_all
18192 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18193 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18194 && abbrev->tag != DW_TAG_enumerator
18195 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18196 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18197 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18198 && abbrev->tag != DW_TAG_variable
5afb4e99 18199 && abbrev->tag != DW_TAG_namespace
f55ee35c 18200 && abbrev->tag != DW_TAG_module
95554aad 18201 && abbrev->tag != DW_TAG_member
74921315
KS
18202 && abbrev->tag != DW_TAG_imported_unit
18203 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18204 {
18205 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18206 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18207 continue;
18208 }
18209
6f06d47b
YQ
18210 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18211 abbrev);
cd9983dd 18212
48fbe735 18213 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18214
18215 /* This two-pass algorithm for processing partial symbols has a
18216 high cost in cache pressure. Thus, handle some simple cases
18217 here which cover the majority of C partial symbols. DIEs
18218 which neither have specification tags in them, nor could have
18219 specification tags elsewhere pointing at them, can simply be
18220 processed and discarded.
18221
18222 This segment is also optional; scan_partial_symbols and
18223 add_partial_symbol will handle these DIEs if we chain
18224 them in normally. When compilers which do not emit large
18225 quantities of duplicate debug information are more common,
18226 this code can probably be removed. */
18227
18228 /* Any complete simple types at the top level (pretty much all
18229 of them, for a language without namespaces), can be processed
18230 directly. */
18231 if (parent_die == NULL
cd9983dd
YQ
18232 && pdi.has_specification == 0
18233 && pdi.is_declaration == 0
18234 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18235 || pdi.tag == DW_TAG_base_type
18236 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18237 {
cd9983dd 18238 if (building_psymtab && pdi.name != NULL)
31edb802 18239 add_psymbol_to_list (pdi.name, false,
79748972 18240 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18241 psymbol_placement::STATIC,
1762568f 18242 0, cu->language, objfile);
cd9983dd 18243 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18244 continue;
18245 }
18246
d8228535
JK
18247 /* The exception for DW_TAG_typedef with has_children above is
18248 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18249 type_name_or_error will error on such types later.
d8228535
JK
18250
18251 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18252 it could not find the child DIEs referenced later, this is checked
18253 above. In correct DWARF DW_TAG_typedef should have no children. */
18254
cd9983dd 18255 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18256 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18257 "- DIE at %s [in module %s]"),
cd9983dd 18258 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18259
72bf9492
DJ
18260 /* If we're at the second level, and we're an enumerator, and
18261 our parent has no specification (meaning possibly lives in a
18262 namespace elsewhere), then we can add the partial symbol now
18263 instead of queueing it. */
cd9983dd 18264 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18265 && parent_die != NULL
18266 && parent_die->die_parent == NULL
18267 && parent_die->tag == DW_TAG_enumeration_type
18268 && parent_die->has_specification == 0)
18269 {
cd9983dd 18270 if (pdi.name == NULL)
b98664d3 18271 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18272 else if (building_psymtab)
31edb802 18273 add_psymbol_to_list (pdi.name, false,
79748972 18274 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18275 cu->language == language_cplus
75aedd27
TT
18276 ? psymbol_placement::GLOBAL
18277 : psymbol_placement::STATIC,
1762568f 18278 0, cu->language, objfile);
72bf9492 18279
cd9983dd 18280 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18281 continue;
18282 }
18283
cd9983dd 18284 struct partial_die_info *part_die
6f06d47b 18285 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18286
72bf9492
DJ
18287 /* We'll save this DIE so link it in. */
18288 part_die->die_parent = parent_die;
18289 part_die->die_sibling = NULL;
18290 part_die->die_child = NULL;
18291
18292 if (last_die && last_die == parent_die)
18293 last_die->die_child = part_die;
18294 else if (last_die)
18295 last_die->die_sibling = part_die;
18296
18297 last_die = part_die;
18298
18299 if (first_die == NULL)
18300 first_die = part_die;
18301
18302 /* Maybe add the DIE to the hash table. Not all DIEs that we
18303 find interesting need to be in the hash table, because we
18304 also have the parent/sibling/child chains; only those that we
18305 might refer to by offset later during partial symbol reading.
18306
18307 For now this means things that might have be the target of a
18308 DW_AT_specification, DW_AT_abstract_origin, or
18309 DW_AT_extension. DW_AT_extension will refer only to
18310 namespaces; DW_AT_abstract_origin refers to functions (and
18311 many things under the function DIE, but we do not recurse
18312 into function DIEs during partial symbol reading) and
18313 possibly variables as well; DW_AT_specification refers to
18314 declarations. Declarations ought to have the DW_AT_declaration
18315 flag. It happens that GCC forgets to put it in sometimes, but
18316 only for functions, not for types.
18317
18318 Adding more things than necessary to the hash table is harmless
18319 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18320 wasted time in find_partial_die, when we reread the compilation
18321 unit with load_all_dies set. */
72bf9492 18322
5afb4e99 18323 if (load_all
72929c62 18324 || abbrev->tag == DW_TAG_constant
5afb4e99 18325 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18326 || abbrev->tag == DW_TAG_variable
18327 || abbrev->tag == DW_TAG_namespace
18328 || part_die->is_declaration)
18329 {
18330 void **slot;
18331
18332 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18333 to_underlying (part_die->sect_off),
18334 INSERT);
72bf9492
DJ
18335 *slot = part_die;
18336 }
18337
72bf9492 18338 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18339 we have no reason to follow the children of structures; for other
98bfdba5
PA
18340 languages we have to, so that we can get at method physnames
18341 to infer fully qualified class names, for DW_AT_specification,
18342 and for C++ template arguments. For C++, we also look one level
18343 inside functions to find template arguments (if the name of the
18344 function does not already contain the template arguments).
bc30ff58 18345
0a4b0913
AB
18346 For Ada and Fortran, we need to scan the children of subprograms
18347 and lexical blocks as well because these languages allow the
18348 definition of nested entities that could be interesting for the
18349 debugger, such as nested subprograms for instance. */
72bf9492 18350 if (last_die->has_children
5afb4e99
DJ
18351 && (load_all
18352 || last_die->tag == DW_TAG_namespace
f55ee35c 18353 || last_die->tag == DW_TAG_module
72bf9492 18354 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18355 || (cu->language == language_cplus
18356 && last_die->tag == DW_TAG_subprogram
18357 && (last_die->name == NULL
18358 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18359 || (cu->language != language_c
18360 && (last_die->tag == DW_TAG_class_type
680b30c7 18361 || last_die->tag == DW_TAG_interface_type
72bf9492 18362 || last_die->tag == DW_TAG_structure_type
bc30ff58 18363 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18364 || ((cu->language == language_ada
18365 || cu->language == language_fortran)
bc30ff58
JB
18366 && (last_die->tag == DW_TAG_subprogram
18367 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18368 {
18369 nesting_level++;
18370 parent_die = last_die;
18371 continue;
18372 }
18373
18374 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18375 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18376
18377 /* Back to the top, do it again. */
18378 }
18379}
18380
6f06d47b
YQ
18381partial_die_info::partial_die_info (sect_offset sect_off_,
18382 struct abbrev_info *abbrev)
18383 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18384{
18385}
18386
35cc7ed7
YQ
18387/* Read a minimal amount of information into the minimal die structure.
18388 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18389
48fbe735
YQ
18390const gdb_byte *
18391partial_die_info::read (const struct die_reader_specs *reader,
18392 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18393{
dee91e82 18394 struct dwarf2_cu *cu = reader->cu;
5e22e966 18395 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18396 unsigned int i;
c5aa993b 18397 int has_low_pc_attr = 0;
c906108c 18398 int has_high_pc_attr = 0;
91da1414 18399 int high_pc_relative = 0;
c906108c 18400
fd0a254f 18401 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18402 {
e7da7f8f 18403 attribute attr;
18a8505e 18404 bool need_reprocess;
e7da7f8f 18405 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18406 info_ptr, &need_reprocess);
18407 /* String and address offsets that need to do the reprocessing have
18408 already been read at this point, so there is no need to wait until
18409 the loop terminates to do the reprocessing. */
18410 if (need_reprocess)
e7da7f8f 18411 read_attribute_reprocess (reader, &attr);
c906108c 18412 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18413 partial symbol table. */
c906108c
SS
18414 switch (attr.name)
18415 {
18416 case DW_AT_name:
48fbe735 18417 switch (tag)
71c25dea
TT
18418 {
18419 case DW_TAG_compile_unit:
95554aad 18420 case DW_TAG_partial_unit:
348e048f 18421 case DW_TAG_type_unit:
71c25dea
TT
18422 /* Compilation units have a DW_AT_name that is a filename, not
18423 a source language identifier. */
18424 case DW_TAG_enumeration_type:
18425 case DW_TAG_enumerator:
18426 /* These tags always have simple identifiers already; no need
18427 to canonicalize them. */
48fbe735 18428 name = DW_STRING (&attr);
71c25dea
TT
18429 break;
18430 default:
48fbe735
YQ
18431 {
18432 struct objfile *objfile = dwarf2_per_objfile->objfile;
18433
18434 name
be1e3d3e 18435 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18436 }
71c25dea
TT
18437 break;
18438 }
c906108c 18439 break;
31ef98ae 18440 case DW_AT_linkage_name:
c906108c 18441 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18442 /* Note that both forms of linkage name might appear. We
18443 assume they will be the same, and we only store the last
18444 one we see. */
e61108c9 18445 linkage_name = attr.value_as_string ();
787de330
TT
18446 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18447 See https://github.com/rust-lang/rust/issues/32925. */
18448 if (cu->language == language_rust && linkage_name != NULL
18449 && strchr (linkage_name, '{') != NULL)
18450 linkage_name = NULL;
c906108c
SS
18451 break;
18452 case DW_AT_low_pc:
18453 has_low_pc_attr = 1;
cd6c91b4 18454 lowpc = attr.value_as_address ();
c906108c
SS
18455 break;
18456 case DW_AT_high_pc:
18457 has_high_pc_attr = 1;
cd6c91b4
TT
18458 highpc = attr.value_as_address ();
18459 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18460 high_pc_relative = 1;
c906108c
SS
18461 break;
18462 case DW_AT_location:
0963b4bd 18463 /* Support the .debug_loc offsets. */
4fc6c0d5 18464 if (attr.form_is_block ())
8e19ed76 18465 {
48fbe735 18466 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18467 }
cd6c91b4 18468 else if (attr.form_is_section_offset ())
8e19ed76 18469 {
4d3c2250 18470 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18471 }
18472 else
18473 {
4d3c2250
KB
18474 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18475 "partial symbol information");
8e19ed76 18476 }
c906108c 18477 break;
c906108c 18478 case DW_AT_external:
48fbe735 18479 is_external = DW_UNSND (&attr);
c906108c
SS
18480 break;
18481 case DW_AT_declaration:
48fbe735 18482 is_declaration = DW_UNSND (&attr);
c906108c
SS
18483 break;
18484 case DW_AT_type:
48fbe735 18485 has_type = 1;
c906108c
SS
18486 break;
18487 case DW_AT_abstract_origin:
18488 case DW_AT_specification:
72bf9492 18489 case DW_AT_extension:
48fbe735 18490 has_specification = 1;
0826b30a 18491 spec_offset = attr.get_ref_die_offset ();
48fbe735 18492 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18493 || cu->per_cu->is_dwz);
c906108c
SS
18494 break;
18495 case DW_AT_sibling:
18496 /* Ignore absolute siblings, they might point outside of
18497 the current compile unit. */
18498 if (attr.form == DW_FORM_ref_addr)
b98664d3 18499 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18500 else
b9502d3f 18501 {
48fbe735 18502 const gdb_byte *buffer = reader->buffer;
0826b30a 18503 sect_offset off = attr.get_ref_die_offset ();
9c541725 18504 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18505
18506 if (sibling_ptr < info_ptr)
b98664d3 18507 complaint (_("DW_AT_sibling points backwards"));
22869d73 18508 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18509 reader->die_section->overflow_complaint ();
b9502d3f 18510 else
48fbe735 18511 sibling = sibling_ptr;
b9502d3f 18512 }
c906108c 18513 break;
fa4028e9 18514 case DW_AT_byte_size:
48fbe735 18515 has_byte_size = 1;
fa4028e9 18516 break;
ff908ebf 18517 case DW_AT_const_value:
48fbe735 18518 has_const_value = 1;
ff908ebf 18519 break;
68511cec
CES
18520 case DW_AT_calling_convention:
18521 /* DWARF doesn't provide a way to identify a program's source-level
18522 entry point. DW_AT_calling_convention attributes are only meant
18523 to describe functions' calling conventions.
18524
18525 However, because it's a necessary piece of information in
0c1b455e
TT
18526 Fortran, and before DWARF 4 DW_CC_program was the only
18527 piece of debugging information whose definition refers to
18528 a 'main program' at all, several compilers marked Fortran
18529 main programs with DW_CC_program --- even when those
18530 functions use the standard calling conventions.
18531
18532 Although DWARF now specifies a way to provide this
18533 information, we support this practice for backward
18534 compatibility. */
68511cec 18535 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18536 && cu->language == language_fortran)
48fbe735 18537 main_subprogram = 1;
68511cec 18538 break;
481860b3
GB
18539 case DW_AT_inline:
18540 if (DW_UNSND (&attr) == DW_INL_inlined
18541 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18542 may_be_inlined = 1;
481860b3 18543 break;
95554aad
TT
18544
18545 case DW_AT_import:
48fbe735 18546 if (tag == DW_TAG_imported_unit)
36586728 18547 {
0826b30a 18548 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18549 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18550 || cu->per_cu->is_dwz);
18551 }
95554aad
TT
18552 break;
18553
0c1b455e 18554 case DW_AT_main_subprogram:
48fbe735 18555 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18556 break;
18557
05caa1d2
TT
18558 case DW_AT_ranges:
18559 {
18560 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18561 but that requires a full DIE, so instead we just
18562 reimplement it. */
18563 int need_ranges_base = tag != DW_TAG_compile_unit;
18564 unsigned int ranges_offset = (DW_UNSND (&attr)
18565 + (need_ranges_base
18566 ? cu->ranges_base
18567 : 0));
18568
18569 /* Value of the DW_AT_ranges attribute is the offset in the
18570 .debug_ranges section. */
18571 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18572 nullptr))
18573 has_pc_info = 1;
18574 }
18575 break;
18576
c906108c
SS
18577 default:
18578 break;
18579 }
18580 }
18581
10d06d82
TT
18582 /* For Ada, if both the name and the linkage name appear, we prefer
18583 the latter. This lets "catch exception" work better, regardless
18584 of the order in which the name and linkage name were emitted.
18585 Really, though, this is just a workaround for the fact that gdb
18586 doesn't store both the name and the linkage name. */
18587 if (cu->language == language_ada && linkage_name != nullptr)
18588 name = linkage_name;
18589
91da1414 18590 if (high_pc_relative)
48fbe735 18591 highpc += lowpc;
91da1414 18592
9373cf26
JK
18593 if (has_low_pc_attr && has_high_pc_attr)
18594 {
18595 /* When using the GNU linker, .gnu.linkonce. sections are used to
18596 eliminate duplicate copies of functions and vtables and such.
18597 The linker will arbitrarily choose one and discard the others.
18598 The AT_*_pc values for such functions refer to local labels in
18599 these sections. If the section from that file was discarded, the
18600 labels are not in the output, so the relocs get a value of 0.
18601 If this is a discarded function, mark the pc bounds as invalid,
18602 so that GDB will ignore it. */
5989a64e 18603 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18604 {
48fbe735 18605 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18606 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18607
b98664d3 18608 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18609 "for DIE at %s [in module %s]"),
48fbe735
YQ
18610 paddress (gdbarch, lowpc),
18611 sect_offset_str (sect_off),
9d8780f0 18612 objfile_name (objfile));
9373cf26
JK
18613 }
18614 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18615 else if (lowpc >= highpc)
9373cf26 18616 {
48fbe735 18617 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18618 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18619
b98664d3 18620 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18621 "for DIE at %s [in module %s]"),
48fbe735
YQ
18622 paddress (gdbarch, lowpc),
18623 paddress (gdbarch, highpc),
18624 sect_offset_str (sect_off),
9c541725 18625 objfile_name (objfile));
9373cf26
JK
18626 }
18627 else
48fbe735 18628 has_pc_info = 1;
9373cf26 18629 }
85cbf3d3 18630
c906108c
SS
18631 return info_ptr;
18632}
18633
72bf9492
DJ
18634/* Find a cached partial DIE at OFFSET in CU. */
18635
d590ff25
YQ
18636struct partial_die_info *
18637dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18638{
18639 struct partial_die_info *lookup_die = NULL;
6f06d47b 18640 struct partial_die_info part_die (sect_off);
72bf9492 18641
9a3c8263 18642 lookup_die = ((struct partial_die_info *)
d590ff25 18643 htab_find_with_hash (partial_dies, &part_die,
9c541725 18644 to_underlying (sect_off)));
72bf9492 18645
72bf9492
DJ
18646 return lookup_die;
18647}
18648
348e048f
DE
18649/* Find a partial DIE at OFFSET, which may or may not be in CU,
18650 except in the case of .debug_types DIEs which do not reference
18651 outside their CU (they do however referencing other types via
55f1336d 18652 DW_FORM_ref_sig8). */
72bf9492 18653
122cf0f2 18654static const struct cu_partial_die_info
9c541725 18655find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18656{
5e22e966 18657 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18658 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18659 struct dwarf2_per_cu_data *per_cu = NULL;
18660 struct partial_die_info *pd = NULL;
72bf9492 18661
36586728 18662 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18663 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18664 {
d590ff25 18665 pd = cu->find_partial_die (sect_off);
5afb4e99 18666 if (pd != NULL)
fb816e8b 18667 return { cu, pd };
0d99eb77
DE
18668 /* We missed recording what we needed.
18669 Load all dies and try again. */
18670 per_cu = cu->per_cu;
5afb4e99 18671 }
0d99eb77
DE
18672 else
18673 {
18674 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18675 if (cu->per_cu->is_debug_types)
0d99eb77 18676 {
9d8780f0
SM
18677 error (_("Dwarf Error: Type Unit at offset %s contains"
18678 " external reference to offset %s [in module %s].\n"),
18679 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18680 bfd_get_filename (objfile->obfd));
18681 }
9c541725 18682 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18683 dwarf2_per_objfile);
72bf9492 18684
0d99eb77 18685 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18686 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18687
0d99eb77 18688 per_cu->cu->last_used = 0;
d590ff25 18689 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18690 }
5afb4e99 18691
dee91e82
DE
18692 /* If we didn't find it, and not all dies have been loaded,
18693 load them all and try again. */
18694
5afb4e99
DJ
18695 if (pd == NULL && per_cu->load_all_dies == 0)
18696 {
5afb4e99 18697 per_cu->load_all_dies = 1;
fd820528
DE
18698
18699 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18700 THIS_CU->cu may already be in use. So we can't just free it and
18701 replace its DIEs with the ones we read in. Instead, we leave those
18702 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18703 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18704 set. */
ab432490 18705 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18706
d590ff25 18707 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18708 }
18709
18710 if (pd == NULL)
18711 internal_error (__FILE__, __LINE__,
9d8780f0 18712 _("could not find partial DIE %s "
3e43a32a 18713 "in cache [from module %s]\n"),
9d8780f0 18714 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18715 return { per_cu->cu, pd };
72bf9492
DJ
18716}
18717
abc72ce4
DE
18718/* See if we can figure out if the class lives in a namespace. We do
18719 this by looking for a member function; its demangled name will
18720 contain namespace info, if there is any. */
18721
18722static void
18723guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18724 struct dwarf2_cu *cu)
18725{
18726 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18727 what template types look like, because the demangler
18728 frequently doesn't give the same name as the debug info. We
18729 could fix this by only using the demangled name to get the
18730 prefix (but see comment in read_structure_type). */
18731
18732 struct partial_die_info *real_pdi;
18733 struct partial_die_info *child_pdi;
18734
18735 /* If this DIE (this DIE's specification, if any) has a parent, then
18736 we should not do this. We'll prepend the parent's fully qualified
18737 name when we create the partial symbol. */
18738
18739 real_pdi = struct_pdi;
18740 while (real_pdi->has_specification)
fb816e8b 18741 {
122cf0f2
AB
18742 auto res = find_partial_die (real_pdi->spec_offset,
18743 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18744 real_pdi = res.pdi;
18745 cu = res.cu;
18746 }
abc72ce4
DE
18747
18748 if (real_pdi->die_parent != NULL)
18749 return;
18750
18751 for (child_pdi = struct_pdi->die_child;
18752 child_pdi != NULL;
18753 child_pdi = child_pdi->die_sibling)
18754 {
18755 if (child_pdi->tag == DW_TAG_subprogram
18756 && child_pdi->linkage_name != NULL)
18757 {
43816ebc
TT
18758 gdb::unique_xmalloc_ptr<char> actual_class_name
18759 (language_class_name_from_physname (cu->language_defn,
18760 child_pdi->linkage_name));
abc72ce4
DE
18761 if (actual_class_name != NULL)
18762 {
5e22e966 18763 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18764 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18765 }
18766 break;
18767 }
18768 }
18769}
18770
25c11aca
TV
18771/* Return true if a DIE with TAG may have the DW_AT_const_value
18772 attribute. */
18773
18774static bool
18775can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18776{
18777 switch (tag)
18778 {
18779 case DW_TAG_constant:
18780 case DW_TAG_enumerator:
18781 case DW_TAG_formal_parameter:
18782 case DW_TAG_template_value_param:
18783 case DW_TAG_variable:
18784 return true;
18785 }
18786
18787 return false;
18788}
18789
52356b79
YQ
18790void
18791partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18792{
abc72ce4
DE
18793 /* Once we've fixed up a die, there's no point in doing so again.
18794 This also avoids a memory leak if we were to call
18795 guess_partial_die_structure_name multiple times. */
52356b79 18796 if (fixup_called)
abc72ce4
DE
18797 return;
18798
72bf9492
DJ
18799 /* If we found a reference attribute and the DIE has no name, try
18800 to find a name in the referred to DIE. */
18801
52356b79 18802 if (name == NULL && has_specification)
72bf9492
DJ
18803 {
18804 struct partial_die_info *spec_die;
72bf9492 18805
122cf0f2 18806 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18807 spec_die = res.pdi;
18808 cu = res.cu;
72bf9492 18809
52356b79 18810 spec_die->fixup (cu);
72bf9492
DJ
18811
18812 if (spec_die->name)
18813 {
52356b79 18814 name = spec_die->name;
72bf9492
DJ
18815
18816 /* Copy DW_AT_external attribute if it is set. */
18817 if (spec_die->is_external)
52356b79 18818 is_external = spec_die->is_external;
72bf9492
DJ
18819 }
18820 }
18821
25c11aca
TV
18822 if (!has_const_value && has_specification
18823 && can_have_DW_AT_const_value_p (tag))
18824 {
18825 struct partial_die_info *spec_die;
18826
18827 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18828 spec_die = res.pdi;
18829 cu = res.cu;
18830
18831 spec_die->fixup (cu);
18832
18833 if (spec_die->has_const_value)
18834 {
18835 /* Copy DW_AT_const_value attribute if it is set. */
18836 has_const_value = spec_die->has_const_value;
18837 }
18838 }
18839
72bf9492 18840 /* Set default names for some unnamed DIEs. */
72bf9492 18841
52356b79
YQ
18842 if (name == NULL && tag == DW_TAG_namespace)
18843 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18844
abc72ce4
DE
18845 /* If there is no parent die to provide a namespace, and there are
18846 children, see if we can determine the namespace from their linkage
122d1940 18847 name. */
abc72ce4 18848 if (cu->language == language_cplus
5e22e966 18849 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18850 && die_parent == NULL
18851 && has_children
18852 && (tag == DW_TAG_class_type
18853 || tag == DW_TAG_structure_type
18854 || tag == DW_TAG_union_type))
18855 guess_partial_die_structure_name (this, cu);
abc72ce4 18856
53832f31
TT
18857 /* GCC might emit a nameless struct or union that has a linkage
18858 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18859 if (name == NULL
18860 && (tag == DW_TAG_class_type
18861 || tag == DW_TAG_interface_type
18862 || tag == DW_TAG_structure_type
18863 || tag == DW_TAG_union_type)
18864 && linkage_name != NULL)
53832f31 18865 {
43816ebc
TT
18866 gdb::unique_xmalloc_ptr<char> demangled
18867 (gdb_demangle (linkage_name, DMGL_TYPES));
18868 if (demangled != nullptr)
53832f31 18869 {
96408a79
SA
18870 const char *base;
18871
18872 /* Strip any leading namespaces/classes, keep only the base name.
18873 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18874 base = strrchr (demangled.get (), ':');
18875 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18876 base++;
18877 else
43816ebc 18878 base = demangled.get ();
96408a79 18879
5e22e966 18880 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18881 name = objfile->intern (base);
53832f31
TT
18882 }
18883 }
18884
52356b79 18885 fixup_called = 1;
72bf9492
DJ
18886}
18887
41144253 18888/* Read the .debug_loclists header contents from the given SECTION in the
18889 HEADER. */
18890static void
18891read_loclist_header (struct loclist_header *header,
18892 struct dwarf2_section_info *section)
18893{
18894 unsigned int bytes_read;
18895 bfd *abfd = section->get_bfd_owner ();
18896 const gdb_byte *info_ptr = section->buffer;
18897 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18898 info_ptr += bytes_read;
18899 header->version = read_2_bytes (abfd, info_ptr);
18900 info_ptr += 2;
18901 header->addr_size = read_1_byte (abfd, info_ptr);
18902 info_ptr += 1;
18903 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18904 info_ptr += 1;
18905 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18906}
18907
18908/* Return the DW_AT_loclists_base value for the CU. */
18909static ULONGEST
18910lookup_loclist_base (struct dwarf2_cu *cu)
18911{
18912 /* For the .dwo unit, the loclist_base points to the first offset following
18913 the header. The header consists of the following entities-
18914 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18915 bit format)
18916 2. version (2 bytes)
18917 3. address size (1 byte)
18918 4. segment selector size (1 byte)
18919 5. offset entry count (4 bytes)
18920 These sizes are derived as per the DWARFv5 standard. */
18921 if (cu->dwo_unit != nullptr)
18922 {
18923 if (cu->header.initial_length_size == 4)
18924 return LOCLIST_HEADER_SIZE32;
18925 return LOCLIST_HEADER_SIZE64;
18926 }
18927 return cu->loclist_base;
18928}
18929
18930/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18931 array of offsets in the .debug_loclists section. */
18932static CORE_ADDR
18933read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18934{
5e22e966 18935 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18936 struct objfile *objfile = dwarf2_per_objfile->objfile;
18937 bfd *abfd = objfile->obfd;
18938 ULONGEST loclist_base = lookup_loclist_base (cu);
18939 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18940
18941 section->read (objfile);
18942 if (section->buffer == NULL)
18943 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18944 "section [in module %s]"), objfile_name (objfile));
18945 struct loclist_header header;
18946 read_loclist_header (&header, section);
18947 if (loclist_index >= header.offset_entry_count)
18948 complaint (_("DW_FORM_loclistx pointing outside of "
18949 ".debug_loclists offset array [in module %s]"),
18950 objfile_name (objfile));
18951 if (loclist_base + loclist_index * cu->header.offset_size
18952 >= section->size)
18953 complaint (_("DW_FORM_loclistx pointing outside of "
18954 ".debug_loclists section [in module %s]"),
18955 objfile_name (objfile));
18956 const gdb_byte *info_ptr
18957 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18958
18959 if (cu->header.offset_size == 4)
18960 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18961 else
18962 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18963}
18964
18a8505e
AT
18965/* Process the attributes that had to be skipped in the first round. These
18966 attributes are the ones that need str_offsets_base or addr_base attributes.
18967 They could not have been processed in the first round, because at the time
18968 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18969static void
18970read_attribute_reprocess (const struct die_reader_specs *reader,
18971 struct attribute *attr)
18a8505e
AT
18972{
18973 struct dwarf2_cu *cu = reader->cu;
18974 switch (attr->form)
18975 {
18976 case DW_FORM_addrx:
18977 case DW_FORM_GNU_addr_index:
18978 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18979 break;
41144253 18980 case DW_FORM_loclistx:
18981 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18982 break;
18a8505e
AT
18983 case DW_FORM_strx:
18984 case DW_FORM_strx1:
18985 case DW_FORM_strx2:
18986 case DW_FORM_strx3:
18987 case DW_FORM_strx4:
18988 case DW_FORM_GNU_str_index:
18989 {
18990 unsigned int str_index = DW_UNSND (attr);
18991 if (reader->dwo_file != NULL)
18992 {
18993 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18994 DW_STRING_IS_CANONICAL (attr) = 0;
18995 }
18996 else
18997 {
18998 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18999 DW_STRING_IS_CANONICAL (attr) = 0;
19000 }
19001 break;
19002 }
19003 default:
19004 gdb_assert_not_reached (_("Unexpected DWARF form."));
19005 }
19006}
19007
a8329558 19008/* Read an attribute value described by an attribute form. */
c906108c 19009
d521ce57 19010static const gdb_byte *
dee91e82
DE
19011read_attribute_value (const struct die_reader_specs *reader,
19012 struct attribute *attr, unsigned form,
18a8505e
AT
19013 LONGEST implicit_const, const gdb_byte *info_ptr,
19014 bool *need_reprocess)
c906108c 19015{
dee91e82 19016 struct dwarf2_cu *cu = reader->cu;
5e22e966 19017 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19018 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19019 bfd *abfd = reader->abfd;
e7c27a73 19020 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19021 unsigned int bytes_read;
19022 struct dwarf_block *blk;
18a8505e 19023 *need_reprocess = false;
c906108c 19024
aead7601 19025 attr->form = (enum dwarf_form) form;
a8329558 19026 switch (form)
c906108c 19027 {
c906108c 19028 case DW_FORM_ref_addr:
ae411497 19029 if (cu->header.version == 2)
c8a7a66f
TT
19030 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19031 &bytes_read);
ae411497 19032 else
8266302d
TT
19033 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19034 &bytes_read);
ae411497
TT
19035 info_ptr += bytes_read;
19036 break;
36586728 19037 case DW_FORM_GNU_ref_alt:
8266302d 19038 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19039 info_ptr += bytes_read;
19040 break;
ae411497 19041 case DW_FORM_addr:
08feed99
TT
19042 {
19043 struct gdbarch *gdbarch = objfile->arch ();
19044 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19045 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19046 info_ptr += bytes_read;
19047 }
c906108c
SS
19048 break;
19049 case DW_FORM_block2:
7b5a2f43 19050 blk = dwarf_alloc_block (cu);
c906108c
SS
19051 blk->size = read_2_bytes (abfd, info_ptr);
19052 info_ptr += 2;
19053 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19054 info_ptr += blk->size;
19055 DW_BLOCK (attr) = blk;
19056 break;
19057 case DW_FORM_block4:
7b5a2f43 19058 blk = dwarf_alloc_block (cu);
c906108c
SS
19059 blk->size = read_4_bytes (abfd, info_ptr);
19060 info_ptr += 4;
19061 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19062 info_ptr += blk->size;
19063 DW_BLOCK (attr) = blk;
19064 break;
19065 case DW_FORM_data2:
19066 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19067 info_ptr += 2;
19068 break;
19069 case DW_FORM_data4:
19070 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19071 info_ptr += 4;
19072 break;
19073 case DW_FORM_data8:
19074 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19075 info_ptr += 8;
19076 break;
0224619f
JK
19077 case DW_FORM_data16:
19078 blk = dwarf_alloc_block (cu);
19079 blk->size = 16;
19080 blk->data = read_n_bytes (abfd, info_ptr, 16);
19081 info_ptr += 16;
19082 DW_BLOCK (attr) = blk;
19083 break;
2dc7f7b3 19084 case DW_FORM_sec_offset:
8266302d 19085 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19086 info_ptr += bytes_read;
19087 break;
41144253 19088 case DW_FORM_loclistx:
19089 {
19090 *need_reprocess = true;
19091 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19092 info_ptr += bytes_read;
19093 }
19094 break;
c906108c 19095 case DW_FORM_string:
9b1c24c8 19096 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19097 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19098 info_ptr += bytes_read;
19099 break;
4bdf3d34 19100 case DW_FORM_strp:
36586728
TT
19101 if (!cu->per_cu->is_dwz)
19102 {
ed2dc618
SM
19103 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19104 abfd, info_ptr, cu_header,
36586728
TT
19105 &bytes_read);
19106 DW_STRING_IS_CANONICAL (attr) = 0;
19107 info_ptr += bytes_read;
19108 break;
19109 }
19110 /* FALLTHROUGH */
43988095
JK
19111 case DW_FORM_line_strp:
19112 if (!cu->per_cu->is_dwz)
19113 {
86c0bb4c
TT
19114 DW_STRING (attr)
19115 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19116 &bytes_read);
43988095
JK
19117 DW_STRING_IS_CANONICAL (attr) = 0;
19118 info_ptr += bytes_read;
19119 break;
19120 }
19121 /* FALLTHROUGH */
36586728
TT
19122 case DW_FORM_GNU_strp_alt:
19123 {
c3699833 19124 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19125 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19126 &bytes_read);
36586728 19127
0314b390 19128 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19129 DW_STRING_IS_CANONICAL (attr) = 0;
19130 info_ptr += bytes_read;
19131 }
4bdf3d34 19132 break;
2dc7f7b3 19133 case DW_FORM_exprloc:
c906108c 19134 case DW_FORM_block:
7b5a2f43 19135 blk = dwarf_alloc_block (cu);
c906108c
SS
19136 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19137 info_ptr += bytes_read;
19138 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19139 info_ptr += blk->size;
19140 DW_BLOCK (attr) = blk;
19141 break;
19142 case DW_FORM_block1:
7b5a2f43 19143 blk = dwarf_alloc_block (cu);
c906108c
SS
19144 blk->size = read_1_byte (abfd, info_ptr);
19145 info_ptr += 1;
19146 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19147 info_ptr += blk->size;
19148 DW_BLOCK (attr) = blk;
19149 break;
19150 case DW_FORM_data1:
19151 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19152 info_ptr += 1;
19153 break;
19154 case DW_FORM_flag:
19155 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19156 info_ptr += 1;
19157 break;
2dc7f7b3
TT
19158 case DW_FORM_flag_present:
19159 DW_UNSND (attr) = 1;
19160 break;
c906108c
SS
19161 case DW_FORM_sdata:
19162 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19163 info_ptr += bytes_read;
19164 break;
19165 case DW_FORM_udata:
18a8505e 19166 case DW_FORM_rnglistx:
c906108c
SS
19167 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19168 info_ptr += bytes_read;
19169 break;
19170 case DW_FORM_ref1:
9c541725 19171 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19172 + read_1_byte (abfd, info_ptr));
c906108c
SS
19173 info_ptr += 1;
19174 break;
19175 case DW_FORM_ref2:
9c541725 19176 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19177 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19178 info_ptr += 2;
19179 break;
19180 case DW_FORM_ref4:
9c541725 19181 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19182 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19183 info_ptr += 4;
19184 break;
613e1657 19185 case DW_FORM_ref8:
9c541725 19186 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19187 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19188 info_ptr += 8;
19189 break;
55f1336d 19190 case DW_FORM_ref_sig8:
ac9ec31b 19191 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19192 info_ptr += 8;
19193 break;
c906108c 19194 case DW_FORM_ref_udata:
9c541725 19195 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19196 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19197 info_ptr += bytes_read;
19198 break;
c906108c 19199 case DW_FORM_indirect:
a8329558
KW
19200 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19201 info_ptr += bytes_read;
43988095
JK
19202 if (form == DW_FORM_implicit_const)
19203 {
19204 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19205 info_ptr += bytes_read;
19206 }
19207 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19208 info_ptr, need_reprocess);
43988095
JK
19209 break;
19210 case DW_FORM_implicit_const:
19211 DW_SND (attr) = implicit_const;
a8329558 19212 break;
336d760d 19213 case DW_FORM_addrx:
3019eac3 19214 case DW_FORM_GNU_addr_index:
18a8505e
AT
19215 *need_reprocess = true;
19216 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19217 info_ptr += bytes_read;
19218 break;
cf532bd1 19219 case DW_FORM_strx:
15f18d14
AT
19220 case DW_FORM_strx1:
19221 case DW_FORM_strx2:
19222 case DW_FORM_strx3:
19223 case DW_FORM_strx4:
3019eac3 19224 case DW_FORM_GNU_str_index:
3019eac3 19225 {
15f18d14
AT
19226 ULONGEST str_index;
19227 if (form == DW_FORM_strx1)
19228 {
19229 str_index = read_1_byte (abfd, info_ptr);
19230 info_ptr += 1;
19231 }
19232 else if (form == DW_FORM_strx2)
19233 {
19234 str_index = read_2_bytes (abfd, info_ptr);
19235 info_ptr += 2;
19236 }
19237 else if (form == DW_FORM_strx3)
19238 {
19239 str_index = read_3_bytes (abfd, info_ptr);
19240 info_ptr += 3;
19241 }
19242 else if (form == DW_FORM_strx4)
19243 {
19244 str_index = read_4_bytes (abfd, info_ptr);
19245 info_ptr += 4;
19246 }
19247 else
19248 {
19249 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19250 info_ptr += bytes_read;
19251 }
18a8505e
AT
19252 *need_reprocess = true;
19253 DW_UNSND (attr) = str_index;
19254 }
3019eac3 19255 break;
c906108c 19256 default:
8a3fe4f8 19257 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19258 dwarf_form_name (form),
19259 bfd_get_filename (abfd));
c906108c 19260 }
28e94949 19261
36586728 19262 /* Super hack. */
cd6c91b4 19263 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19264 attr->form = DW_FORM_GNU_ref_alt;
19265
28e94949
JB
19266 /* We have seen instances where the compiler tried to emit a byte
19267 size attribute of -1 which ended up being encoded as an unsigned
19268 0xffffffff. Although 0xffffffff is technically a valid size value,
19269 an object of this size seems pretty unlikely so we can relatively
19270 safely treat these cases as if the size attribute was invalid and
19271 treat them as zero by default. */
19272 if (attr->name == DW_AT_byte_size
19273 && form == DW_FORM_data4
19274 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19275 {
19276 complaint
b98664d3 19277 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19278 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19279 DW_UNSND (attr) = 0;
19280 }
28e94949 19281
c906108c
SS
19282 return info_ptr;
19283}
19284
a8329558
KW
19285/* Read an attribute described by an abbreviated attribute. */
19286
d521ce57 19287static const gdb_byte *
dee91e82
DE
19288read_attribute (const struct die_reader_specs *reader,
19289 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19290 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19291{
19292 attr->name = abbrev->name;
43988095 19293 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19294 abbrev->implicit_const, info_ptr,
19295 need_reprocess);
a8329558
KW
19296}
19297
43988095
JK
19298/* Return pointer to string at .debug_str offset STR_OFFSET. */
19299
19300static const char *
ed2dc618 19301read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19302 LONGEST str_offset)
43988095 19303{
5989a64e
SM
19304 return dwarf2_per_objfile->per_bfd->str.read_string
19305 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19306}
19307
43988095
JK
19308/* Return pointer to string at .debug_str offset as read from BUF.
19309 BUF is assumed to be in a compilation unit described by CU_HEADER.
19310 Return *BYTES_READ_PTR count of bytes read from BUF. */
19311
d521ce57 19312static const char *
ed2dc618
SM
19313read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19314 const gdb_byte *buf,
cf2c3c16
TT
19315 const struct comp_unit_head *cu_header,
19316 unsigned int *bytes_read_ptr)
19317{
8266302d 19318 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19319
4f44ae6c 19320 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19321}
19322
86c0bb4c 19323/* See read.h. */
43988095 19324
86c0bb4c
TT
19325const char *
19326dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19327 const struct comp_unit_head *cu_header,
19328 unsigned int *bytes_read_ptr)
43988095 19329{
86c0bb4c 19330 bfd *abfd = objfile->obfd;
8266302d 19331 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19332
5989a64e 19333 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19334}
19335
3019eac3 19336/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19337 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19338 ADDR_SIZE is the size of addresses from the CU header. */
19339
19340static CORE_ADDR
ed2dc618 19341read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19342 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19343 int addr_size)
3019eac3
DE
19344{
19345 struct objfile *objfile = dwarf2_per_objfile->objfile;
19346 bfd *abfd = objfile->obfd;
19347 const gdb_byte *info_ptr;
18a8505e 19348 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19349
5989a64e
SM
19350 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19351 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19352 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19353 objfile_name (objfile));
18a8505e 19354 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19355 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19356 error (_("DW_FORM_addr_index pointing outside of "
19357 ".debug_addr section [in module %s]"),
4262abfb 19358 objfile_name (objfile));
5989a64e 19359 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19360 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19361 if (addr_size == 4)
19362 return bfd_get_32 (abfd, info_ptr);
19363 else
19364 return bfd_get_64 (abfd, info_ptr);
19365}
19366
19367/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19368
19369static CORE_ADDR
19370read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19371{
5e22e966 19372 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19373 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19374}
19375
19376/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19377
19378static CORE_ADDR
d521ce57 19379read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19380 unsigned int *bytes_read)
19381{
5e22e966 19382 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19383 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19384
19385 return read_addr_index (cu, addr_index);
19386}
19387
450a1bfc 19388/* See read.h. */
3019eac3
DE
19389
19390CORE_ADDR
450a1bfc 19391dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19392{
ed2dc618 19393 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19394 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19395 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19396 int addr_size;
19397
3019eac3
DE
19398 /* We need addr_base and addr_size.
19399 If we don't have PER_CU->cu, we have to get it.
19400 Nasty, but the alternative is storing the needed info in PER_CU,
19401 which at this point doesn't seem justified: it's not clear how frequently
19402 it would get used and it would increase the size of every PER_CU.
19403 Entry points like dwarf2_per_cu_addr_size do a similar thing
19404 so we're not in uncharted territory here.
19405 Alas we need to be a bit more complicated as addr_base is contained
19406 in the DIE.
19407
19408 We don't need to read the entire CU(/TU).
19409 We just need the header and top level die.
a1b64ce1 19410
3019eac3 19411 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19412 For now we skip this optimization. */
3019eac3
DE
19413
19414 if (cu != NULL)
19415 {
19416 addr_base = cu->addr_base;
19417 addr_size = cu->header.addr_size;
19418 }
19419 else
19420 {
ab432490 19421 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19422 addr_base = reader.cu->addr_base;
19423 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19424 }
19425
ed2dc618
SM
19426 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19427 addr_size);
3019eac3
DE
19428}
19429
18a8505e
AT
19430/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19431 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19432 DWO file. */
3019eac3 19433
d521ce57 19434static const char *
18a8505e
AT
19435read_str_index (struct dwarf2_cu *cu,
19436 struct dwarf2_section_info *str_section,
19437 struct dwarf2_section_info *str_offsets_section,
19438 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19439{
5e22e966 19440 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19441 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19442 const char *objf_name = objfile_name (objfile);
3019eac3 19443 bfd *abfd = objfile->obfd;
d521ce57 19444 const gdb_byte *info_ptr;
3019eac3 19445 ULONGEST str_offset;
cf532bd1 19446 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19447
96b79293
TT
19448 str_section->read (objfile);
19449 str_offsets_section->read (objfile);
73869dc2 19450 if (str_section->buffer == NULL)
18a8505e 19451 error (_("%s used without %s section"
9d8780f0 19452 " in CU at offset %s [in module %s]"),
96b79293 19453 form_name, str_section->get_name (),
18a8505e 19454 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19455 if (str_offsets_section->buffer == NULL)
18a8505e 19456 error (_("%s used without %s section"
9d8780f0 19457 " in CU at offset %s [in module %s]"),
96b79293 19458 form_name, str_section->get_name (),
18a8505e 19459 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19460 info_ptr = (str_offsets_section->buffer
18a8505e 19461 + str_offsets_base
3019eac3
DE
19462 + str_index * cu->header.offset_size);
19463 if (cu->header.offset_size == 4)
19464 str_offset = bfd_get_32 (abfd, info_ptr);
19465 else
19466 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19467 if (str_offset >= str_section->size)
57d63ce2 19468 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19469 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19470 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19471 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19472}
19473
18a8505e
AT
19474/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19475
19476static const char *
19477read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19478{
19479 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19480 ? reader->cu->header.addr_size : 0;
19481 return read_str_index (reader->cu,
19482 &reader->dwo_file->sections.str,
19483 &reader->dwo_file->sections.str_offsets,
19484 str_offsets_base, str_index);
19485}
19486
19487/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19488
19489static const char *
19490read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19491{
5e22e966 19492 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19493 const char *objf_name = objfile_name (objfile);
19494 static const char form_name[] = "DW_FORM_GNU_str_index";
19495 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19496
19497 if (!cu->str_offsets_base.has_value ())
19498 error (_("%s used in Fission stub without %s"
19499 " in CU at offset 0x%lx [in module %s]"),
19500 form_name, str_offsets_attr_name,
19501 (long) cu->header.offset_size, objf_name);
19502
19503 return read_str_index (cu,
5e22e966
SM
19504 &cu->per_objfile->per_bfd->str,
19505 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19506 *cu->str_offsets_base, str_index);
19507}
19508
3019eac3
DE
19509/* Return the length of an LEB128 number in BUF. */
19510
19511static int
19512leb128_size (const gdb_byte *buf)
19513{
19514 const gdb_byte *begin = buf;
19515 gdb_byte byte;
19516
19517 while (1)
19518 {
19519 byte = *buf++;
19520 if ((byte & 128) == 0)
19521 return buf - begin;
19522 }
19523}
19524
c906108c 19525static void
e142c38c 19526set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19527{
19528 switch (lang)
19529 {
19530 case DW_LANG_C89:
76bee0cc 19531 case DW_LANG_C99:
0cfd832f 19532 case DW_LANG_C11:
c906108c 19533 case DW_LANG_C:
d1be3247 19534 case DW_LANG_UPC:
e142c38c 19535 cu->language = language_c;
c906108c 19536 break;
9c37b5ae 19537 case DW_LANG_Java:
c906108c 19538 case DW_LANG_C_plus_plus:
0cfd832f
MW
19539 case DW_LANG_C_plus_plus_11:
19540 case DW_LANG_C_plus_plus_14:
e142c38c 19541 cu->language = language_cplus;
c906108c 19542 break;
6aecb9c2
JB
19543 case DW_LANG_D:
19544 cu->language = language_d;
19545 break;
c906108c
SS
19546 case DW_LANG_Fortran77:
19547 case DW_LANG_Fortran90:
b21b22e0 19548 case DW_LANG_Fortran95:
f7de9aab
MW
19549 case DW_LANG_Fortran03:
19550 case DW_LANG_Fortran08:
e142c38c 19551 cu->language = language_fortran;
c906108c 19552 break;
a766d390
DE
19553 case DW_LANG_Go:
19554 cu->language = language_go;
19555 break;
c906108c 19556 case DW_LANG_Mips_Assembler:
e142c38c 19557 cu->language = language_asm;
c906108c
SS
19558 break;
19559 case DW_LANG_Ada83:
8aaf0b47 19560 case DW_LANG_Ada95:
bc5f45f8
JB
19561 cu->language = language_ada;
19562 break;
72019c9c
GM
19563 case DW_LANG_Modula2:
19564 cu->language = language_m2;
19565 break;
fe8e67fd
PM
19566 case DW_LANG_Pascal83:
19567 cu->language = language_pascal;
19568 break;
22566fbd
DJ
19569 case DW_LANG_ObjC:
19570 cu->language = language_objc;
19571 break;
c44af4eb
TT
19572 case DW_LANG_Rust:
19573 case DW_LANG_Rust_old:
19574 cu->language = language_rust;
19575 break;
c906108c
SS
19576 case DW_LANG_Cobol74:
19577 case DW_LANG_Cobol85:
c906108c 19578 default:
e142c38c 19579 cu->language = language_minimal;
c906108c
SS
19580 break;
19581 }
e142c38c 19582 cu->language_defn = language_def (cu->language);
c906108c
SS
19583}
19584
19585/* Return the named attribute or NULL if not there. */
19586
19587static struct attribute *
e142c38c 19588dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19589{
a48e046c 19590 for (;;)
c906108c 19591 {
a48e046c
TT
19592 unsigned int i;
19593 struct attribute *spec = NULL;
19594
19595 for (i = 0; i < die->num_attrs; ++i)
19596 {
19597 if (die->attrs[i].name == name)
19598 return &die->attrs[i];
19599 if (die->attrs[i].name == DW_AT_specification
19600 || die->attrs[i].name == DW_AT_abstract_origin)
19601 spec = &die->attrs[i];
19602 }
19603
19604 if (!spec)
19605 break;
c906108c 19606
f2f0e013 19607 die = follow_die_ref (die, spec, &cu);
f2f0e013 19608 }
c5aa993b 19609
c906108c
SS
19610 return NULL;
19611}
19612
7d45c7c3
KB
19613/* Return the string associated with a string-typed attribute, or NULL if it
19614 is either not found or is of an incorrect type. */
19615
19616static const char *
19617dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19618{
19619 struct attribute *attr;
19620 const char *str = NULL;
19621
19622 attr = dwarf2_attr (die, name, cu);
19623
19624 if (attr != NULL)
19625 {
e61108c9
TT
19626 str = attr->value_as_string ();
19627 if (str == nullptr)
b98664d3 19628 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19629 "DIE at %s in module %s"),
19630 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19631 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19632 }
19633
19634 return str;
19635}
19636
a084a2a6 19637/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19638 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19639static const char *
19640dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19641{
19642 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19643 if (dwo_name == nullptr)
19644 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19645 return dwo_name;
19646}
19647
05cf31d1
JB
19648/* Return non-zero iff the attribute NAME is defined for the given DIE,
19649 and holds a non-zero value. This function should only be used for
2dc7f7b3 19650 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19651
19652static int
19653dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19654{
19655 struct attribute *attr = dwarf2_attr (die, name, cu);
19656
19657 return (attr && DW_UNSND (attr));
19658}
19659
3ca72b44 19660static int
e142c38c 19661die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19662{
05cf31d1
JB
19663 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19664 which value is non-zero. However, we have to be careful with
19665 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19666 (via dwarf2_flag_true_p) follows this attribute. So we may
19667 end up accidently finding a declaration attribute that belongs
19668 to a different DIE referenced by the specification attribute,
19669 even though the given DIE does not have a declaration attribute. */
19670 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19671 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19672}
19673
63d06c5c 19674/* Return the die giving the specification for DIE, if there is
f2f0e013 19675 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19676 containing the return value on output. If there is no
19677 specification, but there is an abstract origin, that is
19678 returned. */
63d06c5c
DC
19679
19680static struct die_info *
f2f0e013 19681die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19682{
f2f0e013
DJ
19683 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19684 *spec_cu);
63d06c5c 19685
edb3359d
DJ
19686 if (spec_attr == NULL)
19687 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19688
63d06c5c
DC
19689 if (spec_attr == NULL)
19690 return NULL;
19691 else
f2f0e013 19692 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19693}
c906108c 19694
527f3840
JK
19695/* Stub for free_line_header to match void * callback types. */
19696
19697static void
19698free_line_header_voidp (void *arg)
19699{
9a3c8263 19700 struct line_header *lh = (struct line_header *) arg;
527f3840 19701
fff8551c 19702 delete lh;
527f3840
JK
19703}
19704
83769d0b 19705/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19706
19707static struct dwarf2_section_info *
19708get_debug_line_section (struct dwarf2_cu *cu)
19709{
19710 struct dwarf2_section_info *section;
5e22e966 19711 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19712
19713 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19714 DWO file. */
19715 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19716 section = &cu->dwo_unit->dwo_file->sections.line;
19717 else if (cu->per_cu->is_dwz)
19718 {
c3699833 19719 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19720
19721 section = &dwz->line;
19722 }
19723 else
5989a64e 19724 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19725
19726 return section;
19727}
19728
debd256d 19729/* Read the statement program header starting at OFFSET in
3019eac3 19730 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19731 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19732 Returns NULL if there is a problem reading the header, e.g., if it
19733 has a version we don't understand.
debd256d
JB
19734
19735 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19736 the returned object point into the dwarf line section buffer,
19737 and must not be freed. */
ae2de4f8 19738
fff8551c 19739static line_header_up
9c541725 19740dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19741{
3019eac3 19742 struct dwarf2_section_info *section;
5e22e966 19743 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19744
36586728 19745 section = get_debug_line_section (cu);
96b79293 19746 section->read (dwarf2_per_objfile->objfile);
3019eac3 19747 if (section->buffer == NULL)
debd256d 19748 {
3019eac3 19749 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19750 complaint (_("missing .debug_line.dwo section"));
3019eac3 19751 else
b98664d3 19752 complaint (_("missing .debug_line section"));
debd256d
JB
19753 return 0;
19754 }
19755
0df7ad3a
TT
19756 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19757 dwarf2_per_objfile, section,
19758 &cu->header);
debd256d 19759}
c906108c 19760
c6da4cef 19761/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19762 Return the file name of the psymtab for the given file_entry.
c6da4cef 19763 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19764 If space for the result is malloc'd, *NAME_HOLDER will be set.
19765 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19766
d521ce57 19767static const char *
7ba99d21 19768psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19769 const dwarf2_psymtab *pst,
c89b44cd
TT
19770 const char *comp_dir,
19771 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19772{
d521ce57
TT
19773 const char *include_name = fe.name;
19774 const char *include_name_to_compare = include_name;
72b9f47f 19775 const char *pst_filename;
c6da4cef
DE
19776 int file_is_pst;
19777
8c43009f 19778 const char *dir_name = fe.include_dir (lh);
c6da4cef 19779
c89b44cd 19780 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19781 if (!IS_ABSOLUTE_PATH (include_name)
19782 && (dir_name != NULL || comp_dir != NULL))
19783 {
19784 /* Avoid creating a duplicate psymtab for PST.
19785 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19786 Before we do the comparison, however, we need to account
19787 for DIR_NAME and COMP_DIR.
19788 First prepend dir_name (if non-NULL). If we still don't
19789 have an absolute path prepend comp_dir (if non-NULL).
19790 However, the directory we record in the include-file's
19791 psymtab does not contain COMP_DIR (to match the
19792 corresponding symtab(s)).
19793
19794 Example:
19795
19796 bash$ cd /tmp
19797 bash$ gcc -g ./hello.c
19798 include_name = "hello.c"
19799 dir_name = "."
19800 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19801 DW_AT_name = "./hello.c"
19802
19803 */
c6da4cef
DE
19804
19805 if (dir_name != NULL)
19806 {
c89b44cd
TT
19807 name_holder->reset (concat (dir_name, SLASH_STRING,
19808 include_name, (char *) NULL));
19809 include_name = name_holder->get ();
c6da4cef 19810 include_name_to_compare = include_name;
c6da4cef
DE
19811 }
19812 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19813 {
c89b44cd
TT
19814 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19815 include_name, (char *) NULL));
19816 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19817 }
19818 }
19819
19820 pst_filename = pst->filename;
c89b44cd 19821 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19822 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19823 {
c89b44cd
TT
19824 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19825 pst_filename, (char *) NULL));
19826 pst_filename = copied_name.get ();
c6da4cef
DE
19827 }
19828
1e3fad37 19829 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19830
c6da4cef
DE
19831 if (file_is_pst)
19832 return NULL;
19833 return include_name;
19834}
19835
d9b3de22
DE
19836/* State machine to track the state of the line number program. */
19837
6f77053d 19838class lnp_state_machine
d9b3de22 19839{
6f77053d
PA
19840public:
19841 /* Initialize a machine state for the start of a line number
19842 program. */
804d2729
TT
19843 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19844 bool record_lines_p);
6f77053d 19845
8c43009f
PA
19846 file_entry *current_file ()
19847 {
19848 /* lh->file_names is 0-based, but the file name numbers in the
19849 statement program are 1-based. */
6f77053d
PA
19850 return m_line_header->file_name_at (m_file);
19851 }
19852
19853 /* Record the line in the state machine. END_SEQUENCE is true if
19854 we're processing the end of a sequence. */
19855 void record_line (bool end_sequence);
19856
7ab6656f
OJ
19857 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19858 nop-out rest of the lines in this sequence. */
6f77053d
PA
19859 void check_line_address (struct dwarf2_cu *cu,
19860 const gdb_byte *line_ptr,
7ab6656f 19861 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19862
19863 void handle_set_discriminator (unsigned int discriminator)
19864 {
19865 m_discriminator = discriminator;
19866 m_line_has_non_zero_discriminator |= discriminator != 0;
19867 }
19868
19869 /* Handle DW_LNE_set_address. */
19870 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19871 {
19872 m_op_index = 0;
19873 address += baseaddr;
19874 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19875 }
19876
19877 /* Handle DW_LNS_advance_pc. */
19878 void handle_advance_pc (CORE_ADDR adjust);
19879
19880 /* Handle a special opcode. */
19881 void handle_special_opcode (unsigned char op_code);
19882
19883 /* Handle DW_LNS_advance_line. */
19884 void handle_advance_line (int line_delta)
19885 {
19886 advance_line (line_delta);
19887 }
19888
19889 /* Handle DW_LNS_set_file. */
19890 void handle_set_file (file_name_index file);
19891
19892 /* Handle DW_LNS_negate_stmt. */
19893 void handle_negate_stmt ()
19894 {
19895 m_is_stmt = !m_is_stmt;
19896 }
19897
19898 /* Handle DW_LNS_const_add_pc. */
19899 void handle_const_add_pc ();
19900
19901 /* Handle DW_LNS_fixed_advance_pc. */
19902 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19903 {
19904 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19905 m_op_index = 0;
19906 }
19907
19908 /* Handle DW_LNS_copy. */
19909 void handle_copy ()
19910 {
19911 record_line (false);
19912 m_discriminator = 0;
19913 }
19914
19915 /* Handle DW_LNE_end_sequence. */
19916 void handle_end_sequence ()
19917 {
804d2729 19918 m_currently_recording_lines = true;
6f77053d
PA
19919 }
19920
19921private:
19922 /* Advance the line by LINE_DELTA. */
19923 void advance_line (int line_delta)
19924 {
19925 m_line += line_delta;
19926
19927 if (line_delta != 0)
19928 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19929 }
19930
804d2729
TT
19931 struct dwarf2_cu *m_cu;
19932
6f77053d
PA
19933 gdbarch *m_gdbarch;
19934
19935 /* True if we're recording lines.
19936 Otherwise we're building partial symtabs and are just interested in
19937 finding include files mentioned by the line number program. */
19938 bool m_record_lines_p;
19939
8c43009f 19940 /* The line number header. */
6f77053d 19941 line_header *m_line_header;
8c43009f 19942
6f77053d
PA
19943 /* These are part of the standard DWARF line number state machine,
19944 and initialized according to the DWARF spec. */
d9b3de22 19945
6f77053d 19946 unsigned char m_op_index = 0;
7ba99d21
AT
19947 /* The line table index of the current file. */
19948 file_name_index m_file = 1;
6f77053d
PA
19949 unsigned int m_line = 1;
19950
19951 /* These are initialized in the constructor. */
19952
19953 CORE_ADDR m_address;
19954 bool m_is_stmt;
19955 unsigned int m_discriminator;
d9b3de22
DE
19956
19957 /* Additional bits of state we need to track. */
19958
19959 /* The last file that we called dwarf2_start_subfile for.
19960 This is only used for TLLs. */
6f77053d 19961 unsigned int m_last_file = 0;
d9b3de22 19962 /* The last file a line number was recorded for. */
6f77053d 19963 struct subfile *m_last_subfile = NULL;
d9b3de22 19964
804d2729
TT
19965 /* When true, record the lines we decode. */
19966 bool m_currently_recording_lines = false;
d9b3de22
DE
19967
19968 /* The last line number that was recorded, used to coalesce
19969 consecutive entries for the same line. This can happen, for
19970 example, when discriminators are present. PR 17276. */
6f77053d
PA
19971 unsigned int m_last_line = 0;
19972 bool m_line_has_non_zero_discriminator = false;
8c43009f 19973};
d9b3de22 19974
6f77053d
PA
19975void
19976lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19977{
19978 CORE_ADDR addr_adj = (((m_op_index + adjust)
19979 / m_line_header->maximum_ops_per_instruction)
19980 * m_line_header->minimum_instruction_length);
19981 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19982 m_op_index = ((m_op_index + adjust)
19983 % m_line_header->maximum_ops_per_instruction);
19984}
d9b3de22 19985
6f77053d
PA
19986void
19987lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19988{
6f77053d 19989 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19990 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19991 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19992 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19993 / m_line_header->maximum_ops_per_instruction)
19994 * m_line_header->minimum_instruction_length);
19995 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19996 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19997 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19998
258bf0ee 19999 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20000 advance_line (line_delta);
20001 record_line (false);
20002 m_discriminator = 0;
20003}
d9b3de22 20004
6f77053d
PA
20005void
20006lnp_state_machine::handle_set_file (file_name_index file)
20007{
20008 m_file = file;
20009
20010 const file_entry *fe = current_file ();
20011 if (fe == NULL)
20012 dwarf2_debug_line_missing_file_complaint ();
20013 else if (m_record_lines_p)
20014 {
20015 const char *dir = fe->include_dir (m_line_header);
20016
c24bdb02 20017 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20018 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20019 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20020 }
20021}
20022
20023void
20024lnp_state_machine::handle_const_add_pc ()
20025{
20026 CORE_ADDR adjust
20027 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20028
20029 CORE_ADDR addr_adj
20030 = (((m_op_index + adjust)
20031 / m_line_header->maximum_ops_per_instruction)
20032 * m_line_header->minimum_instruction_length);
20033
20034 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20035 m_op_index = ((m_op_index + adjust)
20036 % m_line_header->maximum_ops_per_instruction);
20037}
d9b3de22 20038
a05a36a5
DE
20039/* Return non-zero if we should add LINE to the line number table.
20040 LINE is the line to add, LAST_LINE is the last line that was added,
20041 LAST_SUBFILE is the subfile for LAST_LINE.
20042 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20043 had a non-zero discriminator.
20044
20045 We have to be careful in the presence of discriminators.
20046 E.g., for this line:
20047
20048 for (i = 0; i < 100000; i++);
20049
20050 clang can emit four line number entries for that one line,
20051 each with a different discriminator.
20052 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20053
20054 However, we want gdb to coalesce all four entries into one.
20055 Otherwise the user could stepi into the middle of the line and
20056 gdb would get confused about whether the pc really was in the
20057 middle of the line.
20058
20059 Things are further complicated by the fact that two consecutive
20060 line number entries for the same line is a heuristic used by gcc
20061 to denote the end of the prologue. So we can't just discard duplicate
20062 entries, we have to be selective about it. The heuristic we use is
20063 that we only collapse consecutive entries for the same line if at least
20064 one of those entries has a non-zero discriminator. PR 17276.
20065
20066 Note: Addresses in the line number state machine can never go backwards
20067 within one sequence, thus this coalescing is ok. */
20068
20069static int
804d2729
TT
20070dwarf_record_line_p (struct dwarf2_cu *cu,
20071 unsigned int line, unsigned int last_line,
a05a36a5
DE
20072 int line_has_non_zero_discriminator,
20073 struct subfile *last_subfile)
20074{
c24bdb02 20075 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20076 return 1;
20077 if (line != last_line)
20078 return 1;
20079 /* Same line for the same file that we've seen already.
20080 As a last check, for pr 17276, only record the line if the line
20081 has never had a non-zero discriminator. */
20082 if (!line_has_non_zero_discriminator)
20083 return 1;
20084 return 0;
20085}
20086
804d2729
TT
20087/* Use the CU's builder to record line number LINE beginning at
20088 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20089
20090static void
d9b3de22 20091dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20092 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20093 struct dwarf2_cu *cu)
252a6764
DE
20094{
20095 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20096
27e0867f
DE
20097 if (dwarf_line_debug)
20098 {
20099 fprintf_unfiltered (gdb_stdlog,
20100 "Recording line %u, file %s, address %s\n",
20101 line, lbasename (subfile->name),
20102 paddress (gdbarch, address));
20103 }
20104
804d2729 20105 if (cu != nullptr)
8c95582d 20106 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20107}
20108
20109/* Subroutine of dwarf_decode_lines_1 to simplify it.
20110 Mark the end of a set of line number records.
d9b3de22 20111 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20112 If SUBFILE is NULL the request is ignored. */
20113
20114static void
20115dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20116 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20117{
27e0867f
DE
20118 if (subfile == NULL)
20119 return;
20120
20121 if (dwarf_line_debug)
20122 {
20123 fprintf_unfiltered (gdb_stdlog,
20124 "Finishing current line, file %s, address %s\n",
20125 lbasename (subfile->name),
20126 paddress (gdbarch, address));
20127 }
20128
8c95582d 20129 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20130}
20131
6f77053d
PA
20132void
20133lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20134{
d9b3de22
DE
20135 if (dwarf_line_debug)
20136 {
20137 fprintf_unfiltered (gdb_stdlog,
20138 "Processing actual line %u: file %u,"
94a72be7 20139 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20140 m_line, m_file,
6f77053d 20141 paddress (m_gdbarch, m_address),
94a72be7
AB
20142 m_is_stmt, m_discriminator,
20143 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20144 }
20145
6f77053d 20146 file_entry *fe = current_file ();
8c43009f
PA
20147
20148 if (fe == NULL)
d9b3de22
DE
20149 dwarf2_debug_line_missing_file_complaint ();
20150 /* For now we ignore lines not starting on an instruction boundary.
20151 But not when processing end_sequence for compatibility with the
20152 previous version of the code. */
6f77053d 20153 else if (m_op_index == 0 || end_sequence)
d9b3de22 20154 {
8c43009f 20155 fe->included_p = 1;
8c95582d 20156 if (m_record_lines_p)
d9b3de22 20157 {
c24bdb02 20158 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20159 || end_sequence)
d9b3de22 20160 {
804d2729
TT
20161 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20162 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20163 }
20164
20165 if (!end_sequence)
20166 {
8c95582d
AB
20167 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20168
804d2729 20169 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20170 m_line_has_non_zero_discriminator,
20171 m_last_subfile))
d9b3de22 20172 {
c24bdb02 20173 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20174 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20175 builder->get_current_subfile (),
8c95582d 20176 m_line, m_address, is_stmt,
804d2729 20177 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20178 }
c24bdb02 20179 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20180 m_last_line = m_line;
d9b3de22
DE
20181 }
20182 }
20183 }
20184}
20185
804d2729
TT
20186lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20187 line_header *lh, bool record_lines_p)
d9b3de22 20188{
804d2729 20189 m_cu = cu;
6f77053d
PA
20190 m_gdbarch = arch;
20191 m_record_lines_p = record_lines_p;
20192 m_line_header = lh;
d9b3de22 20193
804d2729 20194 m_currently_recording_lines = true;
d9b3de22 20195
d9b3de22
DE
20196 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20197 was a line entry for it so that the backend has a chance to adjust it
20198 and also record it in case it needs it. This is currently used by MIPS
20199 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20200 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20201 m_is_stmt = lh->default_is_stmt;
20202 m_discriminator = 0;
252a6764
DE
20203}
20204
6f77053d
PA
20205void
20206lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20207 const gdb_byte *line_ptr,
7ab6656f 20208 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20209{
7ab6656f
OJ
20210 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20211 the pc range of the CU. However, we restrict the test to only ADDRESS
20212 values of zero to preserve GDB's previous behaviour which is to handle
20213 the specific case of a function being GC'd by the linker. */
924c2928 20214
7ab6656f 20215 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20216 {
20217 /* This line table is for a function which has been
20218 GCd by the linker. Ignore it. PR gdb/12528 */
20219
5e22e966 20220 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20221 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20222
b98664d3 20223 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20224 line_offset, objfile_name (objfile));
804d2729
TT
20225 m_currently_recording_lines = false;
20226 /* Note: m_currently_recording_lines is left as false until we see
20227 DW_LNE_end_sequence. */
924c2928
DE
20228 }
20229}
20230
f3f5162e 20231/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20232 Process the line number information in LH.
20233 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20234 program in order to set included_p for every referenced header. */
debd256d 20235
c906108c 20236static void
43f3e411
DE
20237dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20238 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20239{
d521ce57
TT
20240 const gdb_byte *line_ptr, *extended_end;
20241 const gdb_byte *line_end;
a8c50c1f 20242 unsigned int bytes_read, extended_len;
699ca60a 20243 unsigned char op_code, extended_op;
e142c38c 20244 CORE_ADDR baseaddr;
5e22e966 20245 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20246 bfd *abfd = objfile->obfd;
08feed99 20247 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20248 /* True if we're recording line info (as opposed to building partial
20249 symtabs and just interested in finding include files mentioned by
20250 the line number program). */
20251 bool record_lines_p = !decode_for_pst_p;
e142c38c 20252
b3b3bada 20253 baseaddr = objfile->text_section_offset ();
c906108c 20254
debd256d
JB
20255 line_ptr = lh->statement_program_start;
20256 line_end = lh->statement_program_end;
c906108c
SS
20257
20258 /* Read the statement sequences until there's nothing left. */
20259 while (line_ptr < line_end)
20260 {
6f77053d
PA
20261 /* The DWARF line number program state machine. Reset the state
20262 machine at the start of each sequence. */
804d2729 20263 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20264 bool end_sequence = false;
d9b3de22 20265
8c43009f 20266 if (record_lines_p)
c906108c 20267 {
8c43009f
PA
20268 /* Start a subfile for the current file of the state
20269 machine. */
20270 const file_entry *fe = state_machine.current_file ();
20271
20272 if (fe != NULL)
804d2729 20273 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20274 }
20275
a738430d 20276 /* Decode the table. */
d9b3de22 20277 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20278 {
20279 op_code = read_1_byte (abfd, line_ptr);
20280 line_ptr += 1;
9aa1fe7e 20281
debd256d 20282 if (op_code >= lh->opcode_base)
6e70227d 20283 {
8e07a239 20284 /* Special opcode. */
6f77053d 20285 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20286 }
20287 else switch (op_code)
c906108c
SS
20288 {
20289 case DW_LNS_extended_op:
3e43a32a
MS
20290 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20291 &bytes_read);
473b7be6 20292 line_ptr += bytes_read;
a8c50c1f 20293 extended_end = line_ptr + extended_len;
c906108c
SS
20294 extended_op = read_1_byte (abfd, line_ptr);
20295 line_ptr += 1;
20296 switch (extended_op)
20297 {
20298 case DW_LNE_end_sequence:
6f77053d
PA
20299 state_machine.handle_end_sequence ();
20300 end_sequence = true;
c906108c
SS
20301 break;
20302 case DW_LNE_set_address:
d9b3de22
DE
20303 {
20304 CORE_ADDR address
c8a7a66f 20305 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20306 line_ptr += bytes_read;
6f77053d
PA
20307
20308 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20309 lowpc - baseaddr, address);
6f77053d 20310 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20311 }
c906108c
SS
20312 break;
20313 case DW_LNE_define_file:
debd256d 20314 {
d521ce57 20315 const char *cur_file;
ecfb656c
PA
20316 unsigned int mod_time, length;
20317 dir_index dindex;
6e70227d 20318
3e43a32a
MS
20319 cur_file = read_direct_string (abfd, line_ptr,
20320 &bytes_read);
debd256d 20321 line_ptr += bytes_read;
ecfb656c 20322 dindex = (dir_index)
debd256d
JB
20323 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20324 line_ptr += bytes_read;
20325 mod_time =
20326 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20327 line_ptr += bytes_read;
20328 length =
20329 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20330 line_ptr += bytes_read;
ecfb656c 20331 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20332 }
c906108c 20333 break;
d0c6ba3d 20334 case DW_LNE_set_discriminator:
6f77053d
PA
20335 {
20336 /* The discriminator is not interesting to the
20337 debugger; just ignore it. We still need to
20338 check its value though:
20339 if there are consecutive entries for the same
20340 (non-prologue) line we want to coalesce them.
20341 PR 17276. */
20342 unsigned int discr
20343 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345
20346 state_machine.handle_set_discriminator (discr);
20347 }
d0c6ba3d 20348 break;
c906108c 20349 default:
b98664d3 20350 complaint (_("mangled .debug_line section"));
debd256d 20351 return;
c906108c 20352 }
a8c50c1f
DJ
20353 /* Make sure that we parsed the extended op correctly. If e.g.
20354 we expected a different address size than the producer used,
20355 we may have read the wrong number of bytes. */
20356 if (line_ptr != extended_end)
20357 {
b98664d3 20358 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20359 return;
20360 }
c906108c
SS
20361 break;
20362 case DW_LNS_copy:
6f77053d 20363 state_machine.handle_copy ();
c906108c
SS
20364 break;
20365 case DW_LNS_advance_pc:
2dc7f7b3
TT
20366 {
20367 CORE_ADDR adjust
20368 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20369 line_ptr += bytes_read;
6f77053d
PA
20370
20371 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20372 }
c906108c
SS
20373 break;
20374 case DW_LNS_advance_line:
a05a36a5
DE
20375 {
20376 int line_delta
20377 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20378 line_ptr += bytes_read;
6f77053d
PA
20379
20380 state_machine.handle_advance_line (line_delta);
a05a36a5 20381 }
c906108c
SS
20382 break;
20383 case DW_LNS_set_file:
d9b3de22 20384 {
6f77053d 20385 file_name_index file
ecfb656c
PA
20386 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20387 &bytes_read);
d9b3de22 20388 line_ptr += bytes_read;
8c43009f 20389
6f77053d 20390 state_machine.handle_set_file (file);
d9b3de22 20391 }
c906108c
SS
20392 break;
20393 case DW_LNS_set_column:
0ad93d4f 20394 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20395 line_ptr += bytes_read;
20396 break;
20397 case DW_LNS_negate_stmt:
6f77053d 20398 state_machine.handle_negate_stmt ();
c906108c
SS
20399 break;
20400 case DW_LNS_set_basic_block:
c906108c 20401 break;
c2c6d25f
JM
20402 /* Add to the address register of the state machine the
20403 address increment value corresponding to special opcode
a738430d
MK
20404 255. I.e., this value is scaled by the minimum
20405 instruction length since special opcode 255 would have
b021a221 20406 scaled the increment. */
c906108c 20407 case DW_LNS_const_add_pc:
6f77053d 20408 state_machine.handle_const_add_pc ();
c906108c
SS
20409 break;
20410 case DW_LNS_fixed_advance_pc:
3e29f34a 20411 {
6f77053d 20412 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20413 line_ptr += 2;
6f77053d
PA
20414
20415 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20416 }
c906108c 20417 break;
9aa1fe7e 20418 default:
a738430d
MK
20419 {
20420 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20421 int i;
a738430d 20422
debd256d 20423 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20424 {
20425 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20426 line_ptr += bytes_read;
20427 }
20428 }
c906108c
SS
20429 }
20430 }
d9b3de22
DE
20431
20432 if (!end_sequence)
20433 dwarf2_debug_line_missing_end_sequence_complaint ();
20434
20435 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20436 in which case we still finish recording the last line). */
6f77053d 20437 state_machine.record_line (true);
c906108c 20438 }
f3f5162e
DE
20439}
20440
20441/* Decode the Line Number Program (LNP) for the given line_header
20442 structure and CU. The actual information extracted and the type
20443 of structures created from the LNP depends on the value of PST.
20444
20445 1. If PST is NULL, then this procedure uses the data from the program
20446 to create all necessary symbol tables, and their linetables.
20447
20448 2. If PST is not NULL, this procedure reads the program to determine
20449 the list of files included by the unit represented by PST, and
20450 builds all the associated partial symbol tables.
20451
20452 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20453 It is used for relative paths in the line table.
20454 NOTE: When processing partial symtabs (pst != NULL),
20455 comp_dir == pst->dirname.
20456
20457 NOTE: It is important that psymtabs have the same file name (via strcmp)
20458 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20459 symtab we don't use it in the name of the psymtabs we create.
20460 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20461 A good testcase for this is mb-inline.exp.
20462
527f3840
JK
20463 LOWPC is the lowest address in CU (or 0 if not known).
20464
20465 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20466 for its PC<->lines mapping information. Otherwise only the filename
20467 table is read in. */
f3f5162e
DE
20468
20469static void
20470dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20471 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20472 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20473{
5e22e966 20474 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20475 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20476
527f3840
JK
20477 if (decode_mapping)
20478 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20479
20480 if (decode_for_pst_p)
20481 {
aaa75496
JB
20482 /* Now that we're done scanning the Line Header Program, we can
20483 create the psymtab of each included file. */
7ba99d21
AT
20484 for (auto &file_entry : lh->file_names ())
20485 if (file_entry.included_p == 1)
aaa75496 20486 {
c89b44cd 20487 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20488 const char *include_name =
7ba99d21
AT
20489 psymtab_include_file_name (lh, file_entry, pst,
20490 comp_dir, &name_holder);
c6da4cef 20491 if (include_name != NULL)
aaa75496
JB
20492 dwarf2_create_include_psymtab (include_name, pst, objfile);
20493 }
20494 }
cb1df416
DJ
20495 else
20496 {
20497 /* Make sure a symtab is created for every file, even files
20498 which contain only variables (i.e. no code with associated
20499 line numbers). */
c24bdb02
KS
20500 buildsym_compunit *builder = cu->get_builder ();
20501 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20502
7ba99d21 20503 for (auto &fe : lh->file_names ())
cb1df416 20504 {
804d2729 20505 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20506 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20507 {
c24bdb02 20508 builder->get_current_subfile ()->symtab
804d2729 20509 = allocate_symtab (cust,
c24bdb02 20510 builder->get_current_subfile ()->name);
43f3e411 20511 }
c24bdb02 20512 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20513 }
20514 }
c906108c
SS
20515}
20516
20517/* Start a subfile for DWARF. FILENAME is the name of the file and
20518 DIRNAME the name of the source directory which contains FILENAME
4d663531 20519 or NULL if not known.
c906108c
SS
20520 This routine tries to keep line numbers from identical absolute and
20521 relative file names in a common subfile.
20522
20523 Using the `list' example from the GDB testsuite, which resides in
20524 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20525 of /srcdir/list0.c yields the following debugging information for list0.c:
20526
c5aa993b 20527 DW_AT_name: /srcdir/list0.c
4d663531 20528 DW_AT_comp_dir: /compdir
357e46e7 20529 files.files[0].name: list0.h
c5aa993b 20530 files.files[0].dir: /srcdir
357e46e7 20531 files.files[1].name: list0.c
c5aa993b 20532 files.files[1].dir: /srcdir
c906108c
SS
20533
20534 The line number information for list0.c has to end up in a single
4f1520fb
FR
20535 subfile, so that `break /srcdir/list0.c:1' works as expected.
20536 start_subfile will ensure that this happens provided that we pass the
20537 concatenation of files.files[1].dir and files.files[1].name as the
20538 subfile's name. */
c906108c
SS
20539
20540static void
804d2729
TT
20541dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20542 const char *dirname)
c906108c 20543{
43816ebc 20544 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20545
4d663531 20546 /* In order not to lose the line information directory,
4f1520fb
FR
20547 we concatenate it to the filename when it makes sense.
20548 Note that the Dwarf3 standard says (speaking of filenames in line
20549 information): ``The directory index is ignored for file names
20550 that represent full path names''. Thus ignoring dirname in the
20551 `else' branch below isn't an issue. */
c906108c 20552
d5166ae1 20553 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20554 {
43816ebc
TT
20555 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20556 filename = copy.get ();
d521ce57 20557 }
c906108c 20558
c24bdb02 20559 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20560}
20561
804d2729
TT
20562/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20563 buildsym_compunit constructor. */
f4dc4d17 20564
c24bdb02
KS
20565struct compunit_symtab *
20566dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20567 CORE_ADDR low_pc)
f4dc4d17 20568{
c24bdb02 20569 gdb_assert (m_builder == nullptr);
43f3e411 20570
c24bdb02
KS
20571 m_builder.reset (new struct buildsym_compunit
20572 (per_cu->dwarf2_per_objfile->objfile,
20573 name, comp_dir, language, low_pc));
93b8bea4 20574
c24bdb02 20575 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20576
c24bdb02
KS
20577 get_builder ()->record_debugformat ("DWARF 2");
20578 get_builder ()->record_producer (producer);
f4dc4d17 20579
c24bdb02 20580 processing_has_namespace_info = false;
43f3e411 20581
c24bdb02 20582 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20583}
20584
4c2df51b
DJ
20585static void
20586var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20587 struct dwarf2_cu *cu)
4c2df51b 20588{
5e22e966 20589 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20590 struct comp_unit_head *cu_header = &cu->header;
20591
4c2df51b
DJ
20592 /* NOTE drow/2003-01-30: There used to be a comment and some special
20593 code here to turn a symbol with DW_AT_external and a
20594 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20595 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20596 with some versions of binutils) where shared libraries could have
20597 relocations against symbols in their debug information - the
20598 minimal symbol would have the right address, but the debug info
20599 would not. It's no longer necessary, because we will explicitly
20600 apply relocations when we read in the debug information now. */
20601
20602 /* A DW_AT_location attribute with no contents indicates that a
20603 variable has been optimized away. */
4fc6c0d5 20604 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20605 {
f1e6e072 20606 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20607 return;
20608 }
20609
20610 /* Handle one degenerate form of location expression specially, to
20611 preserve GDB's previous behavior when section offsets are
336d760d
AT
20612 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20613 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20614
4fc6c0d5 20615 if (attr->form_is_block ()
3019eac3
DE
20616 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20617 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20618 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20619 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20620 && (DW_BLOCK (attr)->size
20621 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20622 {
891d2f0b 20623 unsigned int dummy;
4c2df51b 20624
3019eac3 20625 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20626 SET_SYMBOL_VALUE_ADDRESS
20627 (sym, cu->header.read_address (objfile->obfd,
20628 DW_BLOCK (attr)->data + 1,
20629 &dummy));
3019eac3 20630 else
38583298
TT
20631 SET_SYMBOL_VALUE_ADDRESS
20632 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20633 &dummy));
f1e6e072 20634 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20635 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20636 SET_SYMBOL_VALUE_ADDRESS
20637 (sym,
20638 SYMBOL_VALUE_ADDRESS (sym)
20639 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20640 return;
20641 }
20642
20643 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20644 expression evaluator, and use LOC_COMPUTED only when necessary
20645 (i.e. when the value of a register or memory location is
20646 referenced, or a thread-local block, etc.). Then again, it might
20647 not be worthwhile. I'm assuming that it isn't unless performance
20648 or memory numbers show me otherwise. */
20649
f1e6e072 20650 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20651
f1e6e072 20652 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20653 cu->has_loclist = true;
4c2df51b
DJ
20654}
20655
c906108c
SS
20656/* Given a pointer to a DWARF information entry, figure out if we need
20657 to make a symbol table entry for it, and if so, create a new entry
20658 and return a pointer to it.
20659 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20660 used the passed type.
20661 If SPACE is not NULL, use it to hold the new symbol. If it is
20662 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20663
20664static struct symbol *
5e2db402
TT
20665new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20666 struct symbol *space)
c906108c 20667{
5e22e966 20668 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20669 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20670 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20671 struct symbol *sym = NULL;
15d034d0 20672 const char *name;
c906108c
SS
20673 struct attribute *attr = NULL;
20674 struct attribute *attr2 = NULL;
e142c38c 20675 CORE_ADDR baseaddr;
e37fd15a
SW
20676 struct pending **list_to_add = NULL;
20677
edb3359d 20678 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20679
b3b3bada 20680 baseaddr = objfile->text_section_offset ();
c906108c 20681
94af9270 20682 name = dwarf2_name (die, cu);
c906108c
SS
20683 if (name)
20684 {
34eaf542 20685 int suppress_add = 0;
94af9270 20686
34eaf542
TT
20687 if (space)
20688 sym = space;
20689 else
8c14c3a3 20690 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20691 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20692
20693 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20694 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20695 /* Fortran does not have mangling standard and the mangling does differ
20696 between gfortran, iFort etc. */
bcfe6157
TT
20697 const char *physname
20698 = (cu->language == language_fortran
20699 ? dwarf2_full_name (name, die, cu)
20700 : dwarf2_physname (name, die, cu));
20701 const char *linkagename = dw2_linkage_name (die, cu);
20702
20703 if (linkagename == nullptr || cu->language == language_ada)
20704 sym->set_linkage_name (physname);
20705 else
20706 {
20707 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20708 sym->set_linkage_name (linkagename);
20709 }
f55ee35c 20710
c906108c 20711 /* Default assumptions.
c5aa993b 20712 Use the passed type or decode it from the die. */
176620f1 20713 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20714 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20715 if (type != NULL)
20716 SYMBOL_TYPE (sym) = type;
20717 else
e7c27a73 20718 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20719 attr = dwarf2_attr (die,
20720 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20721 cu);
435d3d88 20722 if (attr != nullptr)
c906108c
SS
20723 {
20724 SYMBOL_LINE (sym) = DW_UNSND (attr);
20725 }
cb1df416 20726
edb3359d
DJ
20727 attr = dwarf2_attr (die,
20728 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20729 cu);
435d3d88 20730 if (attr != nullptr)
cb1df416 20731 {
ecfb656c 20732 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20733 struct file_entry *fe;
9a619af0 20734
ecfb656c
PA
20735 if (cu->line_header != NULL)
20736 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20737 else
20738 fe = NULL;
20739
20740 if (fe == NULL)
b98664d3 20741 complaint (_("file index out of range"));
8c43009f
PA
20742 else
20743 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20744 }
20745
c906108c
SS
20746 switch (die->tag)
20747 {
20748 case DW_TAG_label:
e142c38c 20749 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20750 if (attr != nullptr)
3e29f34a
MR
20751 {
20752 CORE_ADDR addr;
20753
cd6c91b4 20754 addr = attr->value_as_address ();
3e29f34a 20755 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20756 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20757 }
0f5238ed
TT
20758 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20759 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20760 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20761 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20762 break;
20763 case DW_TAG_subprogram:
20764 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20765 finish_block. */
f1e6e072 20766 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20767 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20768 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20769 || cu->language == language_ada
20770 || cu->language == language_fortran)
c906108c 20771 {
2cfa0c8d 20772 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20773 Ada and Fortran subprograms, whether marked external or
20774 not, are always stored as a global symbol, because we want
20775 to be able to access them globally. For instance, we want
20776 to be able to break on a nested subprogram without having
20777 to specify the context. */
c24bdb02 20778 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20779 }
20780 else
20781 {
e37fd15a 20782 list_to_add = cu->list_in_scope;
c906108c
SS
20783 }
20784 break;
edb3359d
DJ
20785 case DW_TAG_inlined_subroutine:
20786 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20787 finish_block. */
f1e6e072 20788 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20789 SYMBOL_INLINED (sym) = 1;
481860b3 20790 list_to_add = cu->list_in_scope;
edb3359d 20791 break;
34eaf542
TT
20792 case DW_TAG_template_value_param:
20793 suppress_add = 1;
20794 /* Fall through. */
72929c62 20795 case DW_TAG_constant:
c906108c 20796 case DW_TAG_variable:
254e6b9e 20797 case DW_TAG_member:
0963b4bd
MS
20798 /* Compilation with minimal debug info may result in
20799 variables with missing type entries. Change the
20800 misleading `void' type to something sensible. */
78134374 20801 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20802 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20803
e142c38c 20804 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20805 /* In the case of DW_TAG_member, we should only be called for
20806 static const members. */
20807 if (die->tag == DW_TAG_member)
20808 {
3863f96c
DE
20809 /* dwarf2_add_field uses die_is_declaration,
20810 so we do the same. */
254e6b9e
DE
20811 gdb_assert (die_is_declaration (die, cu));
20812 gdb_assert (attr);
20813 }
435d3d88 20814 if (attr != nullptr)
c906108c 20815 {
e7c27a73 20816 dwarf2_const_value (attr, sym, cu);
e142c38c 20817 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20818 if (!suppress_add)
34eaf542
TT
20819 {
20820 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20821 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20822 else
e37fd15a 20823 list_to_add = cu->list_in_scope;
34eaf542 20824 }
c906108c
SS
20825 break;
20826 }
e142c38c 20827 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20828 if (attr != nullptr)
c906108c 20829 {
e7c27a73 20830 var_decode_location (attr, sym, cu);
e142c38c 20831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20832
20833 /* Fortran explicitly imports any global symbols to the local
20834 scope by DW_TAG_common_block. */
20835 if (cu->language == language_fortran && die->parent
20836 && die->parent->tag == DW_TAG_common_block)
20837 attr2 = NULL;
20838
caac4577
JG
20839 if (SYMBOL_CLASS (sym) == LOC_STATIC
20840 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20841 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20842 {
20843 /* When a static variable is eliminated by the linker,
20844 the corresponding debug information is not stripped
20845 out, but the variable address is set to null;
20846 do not add such variables into symbol table. */
20847 }
20848 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20849 {
4b610737
TT
20850 if (SYMBOL_CLASS (sym) == LOC_STATIC
20851 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20852 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20853 {
20854 /* A global static variable might be subject to
20855 copy relocation. We first check for a local
20856 minsym, though, because maybe the symbol was
20857 marked hidden, in which case this would not
20858 apply. */
20859 bound_minimal_symbol found
20860 = (lookup_minimal_symbol_linkage
987012b8 20861 (sym->linkage_name (), objfile));
4b610737
TT
20862 if (found.minsym != nullptr)
20863 sym->maybe_copied = 1;
20864 }
f55ee35c 20865
1c809c68
TT
20866 /* A variable with DW_AT_external is never static,
20867 but it may be block-scoped. */
804d2729 20868 list_to_add
c24bdb02
KS
20869 = ((cu->list_in_scope
20870 == cu->get_builder ()->get_file_symbols ())
20871 ? cu->get_builder ()->get_global_symbols ()
804d2729 20872 : cu->list_in_scope);
1c809c68 20873 }
c906108c 20874 else
e37fd15a 20875 list_to_add = cu->list_in_scope;
c906108c
SS
20876 }
20877 else
20878 {
20879 /* We do not know the address of this symbol.
c5aa993b
JM
20880 If it is an external symbol and we have type information
20881 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20882 The address of the variable will then be determined from
20883 the minimal symbol table whenever the variable is
20884 referenced. */
e142c38c 20885 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20886
20887 /* Fortran explicitly imports any global symbols to the local
20888 scope by DW_TAG_common_block. */
20889 if (cu->language == language_fortran && die->parent
20890 && die->parent->tag == DW_TAG_common_block)
20891 {
20892 /* SYMBOL_CLASS doesn't matter here because
20893 read_common_block is going to reset it. */
20894 if (!suppress_add)
20895 list_to_add = cu->list_in_scope;
20896 }
20897 else if (attr2 && (DW_UNSND (attr2) != 0)
20898 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20899 {
0fe7935b
DJ
20900 /* A variable with DW_AT_external is never static, but it
20901 may be block-scoped. */
804d2729 20902 list_to_add
c24bdb02
KS
20903 = ((cu->list_in_scope
20904 == cu->get_builder ()->get_file_symbols ())
20905 ? cu->get_builder ()->get_global_symbols ()
804d2729 20906 : cu->list_in_scope);
0fe7935b 20907
f1e6e072 20908 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20909 }
442ddf59
JK
20910 else if (!die_is_declaration (die, cu))
20911 {
20912 /* Use the default LOC_OPTIMIZED_OUT class. */
20913 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20914 if (!suppress_add)
20915 list_to_add = cu->list_in_scope;
442ddf59 20916 }
c906108c
SS
20917 }
20918 break;
20919 case DW_TAG_formal_parameter:
a60f3166
TT
20920 {
20921 /* If we are inside a function, mark this as an argument. If
20922 not, we might be looking at an argument to an inlined function
20923 when we do not have enough information to show inlined frames;
20924 pretend it's a local variable in that case so that the user can
20925 still see it. */
804d2729 20926 struct context_stack *curr
c24bdb02 20927 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20928 if (curr != nullptr && curr->name != nullptr)
20929 SYMBOL_IS_ARGUMENT (sym) = 1;
20930 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20931 if (attr != nullptr)
a60f3166
TT
20932 {
20933 var_decode_location (attr, sym, cu);
20934 }
20935 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20936 if (attr != nullptr)
a60f3166
TT
20937 {
20938 dwarf2_const_value (attr, sym, cu);
20939 }
f346a30d 20940
a60f3166
TT
20941 list_to_add = cu->list_in_scope;
20942 }
c906108c
SS
20943 break;
20944 case DW_TAG_unspecified_parameters:
20945 /* From varargs functions; gdb doesn't seem to have any
20946 interest in this information, so just ignore it for now.
20947 (FIXME?) */
20948 break;
34eaf542
TT
20949 case DW_TAG_template_type_param:
20950 suppress_add = 1;
20951 /* Fall through. */
c906108c 20952 case DW_TAG_class_type:
680b30c7 20953 case DW_TAG_interface_type:
c906108c
SS
20954 case DW_TAG_structure_type:
20955 case DW_TAG_union_type:
72019c9c 20956 case DW_TAG_set_type:
c906108c 20957 case DW_TAG_enumeration_type:
f1e6e072 20958 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20959 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20960
63d06c5c 20961 {
9c37b5ae 20962 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20963 really ever be static objects: otherwise, if you try
20964 to, say, break of a class's method and you're in a file
20965 which doesn't mention that class, it won't work unless
20966 the check for all static symbols in lookup_symbol_aux
20967 saves you. See the OtherFileClass tests in
20968 gdb.c++/namespace.exp. */
20969
e37fd15a 20970 if (!suppress_add)
34eaf542 20971 {
c24bdb02 20972 buildsym_compunit *builder = cu->get_builder ();
804d2729 20973 list_to_add
c24bdb02 20974 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20975 && cu->language == language_cplus
c24bdb02 20976 ? builder->get_global_symbols ()
804d2729 20977 : cu->list_in_scope);
63d06c5c 20978
64382290 20979 /* The semantics of C++ state that "struct foo {
9c37b5ae 20980 ... }" also defines a typedef for "foo". */
64382290 20981 if (cu->language == language_cplus
45280282 20982 || cu->language == language_ada
c44af4eb
TT
20983 || cu->language == language_d
20984 || cu->language == language_rust)
64382290
TT
20985 {
20986 /* The symbol's name is already allocated along
20987 with this objfile, so we don't need to
20988 duplicate it for the type. */
7d93a1e0 20989 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20990 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20991 }
63d06c5c
DC
20992 }
20993 }
c906108c
SS
20994 break;
20995 case DW_TAG_typedef:
f1e6e072 20996 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20997 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20998 list_to_add = cu->list_in_scope;
63d06c5c 20999 break;
c906108c 21000 case DW_TAG_base_type:
a02abb62 21001 case DW_TAG_subrange_type:
f1e6e072 21002 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21003 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21004 list_to_add = cu->list_in_scope;
c906108c
SS
21005 break;
21006 case DW_TAG_enumerator:
e142c38c 21007 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21008 if (attr != nullptr)
c906108c 21009 {
e7c27a73 21010 dwarf2_const_value (attr, sym, cu);
c906108c 21011 }
63d06c5c
DC
21012 {
21013 /* NOTE: carlton/2003-11-10: See comment above in the
21014 DW_TAG_class_type, etc. block. */
21015
804d2729 21016 list_to_add
c24bdb02 21017 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21018 && cu->language == language_cplus
c24bdb02 21019 ? cu->get_builder ()->get_global_symbols ()
804d2729 21020 : cu->list_in_scope);
63d06c5c 21021 }
c906108c 21022 break;
74921315 21023 case DW_TAG_imported_declaration:
5c4e30ca 21024 case DW_TAG_namespace:
f1e6e072 21025 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21026 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21027 break;
530e8392
KB
21028 case DW_TAG_module:
21029 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21030 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21031 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21032 break;
4357ac6c 21033 case DW_TAG_common_block:
f1e6e072 21034 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21035 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21036 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21037 break;
c906108c
SS
21038 default:
21039 /* Not a tag we recognize. Hopefully we aren't processing
21040 trash data, but since we must specifically ignore things
21041 we don't recognize, there is nothing else we should do at
0963b4bd 21042 this point. */
b98664d3 21043 complaint (_("unsupported tag: '%s'"),
4d3c2250 21044 dwarf_tag_name (die->tag));
c906108c
SS
21045 break;
21046 }
df8a16a1 21047
e37fd15a
SW
21048 if (suppress_add)
21049 {
21050 sym->hash_next = objfile->template_symbols;
21051 objfile->template_symbols = sym;
21052 list_to_add = NULL;
21053 }
21054
21055 if (list_to_add != NULL)
d3cb6808 21056 add_symbol_to_list (sym, list_to_add);
e37fd15a 21057
df8a16a1
DJ
21058 /* For the benefit of old versions of GCC, check for anonymous
21059 namespaces based on the demangled name. */
4d4ec4e5 21060 if (!cu->processing_has_namespace_info
94af9270 21061 && cu->language == language_cplus)
c24bdb02 21062 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21063 }
21064 return (sym);
21065}
21066
98bfdba5
PA
21067/* Given an attr with a DW_FORM_dataN value in host byte order,
21068 zero-extend it as appropriate for the symbol's type. The DWARF
21069 standard (v4) is not entirely clear about the meaning of using
21070 DW_FORM_dataN for a constant with a signed type, where the type is
21071 wider than the data. The conclusion of a discussion on the DWARF
21072 list was that this is unspecified. We choose to always zero-extend
21073 because that is the interpretation long in use by GCC. */
c906108c 21074
98bfdba5 21075static gdb_byte *
ff39bb5e 21076dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21077 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21078{
5e22e966 21079 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21080 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21081 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21082 LONGEST l = DW_UNSND (attr);
21083
21084 if (bits < sizeof (*value) * 8)
21085 {
21086 l &= ((LONGEST) 1 << bits) - 1;
21087 *value = l;
21088 }
21089 else if (bits == sizeof (*value) * 8)
21090 *value = l;
21091 else
21092 {
224c3ddb 21093 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21094 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21095 return bytes;
21096 }
21097
21098 return NULL;
21099}
21100
21101/* Read a constant value from an attribute. Either set *VALUE, or if
21102 the value does not fit in *VALUE, set *BYTES - either already
21103 allocated on the objfile obstack, or newly allocated on OBSTACK,
21104 or, set *BATON, if we translated the constant to a location
21105 expression. */
21106
21107static void
ff39bb5e 21108dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21109 const char *name, struct obstack *obstack,
21110 struct dwarf2_cu *cu,
d521ce57 21111 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21112 struct dwarf2_locexpr_baton **baton)
21113{
5e22e966 21114 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21115 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21116 struct comp_unit_head *cu_header = &cu->header;
c906108c 21117 struct dwarf_block *blk;
98bfdba5
PA
21118 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21119 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21120
21121 *value = 0;
21122 *bytes = NULL;
21123 *baton = NULL;
c906108c
SS
21124
21125 switch (attr->form)
21126 {
21127 case DW_FORM_addr:
336d760d 21128 case DW_FORM_addrx:
3019eac3 21129 case DW_FORM_GNU_addr_index:
ac56253d 21130 {
ac56253d
TT
21131 gdb_byte *data;
21132
98bfdba5
PA
21133 if (TYPE_LENGTH (type) != cu_header->addr_size)
21134 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21135 cu_header->addr_size,
98bfdba5 21136 TYPE_LENGTH (type));
ac56253d
TT
21137 /* Symbols of this form are reasonably rare, so we just
21138 piggyback on the existing location code rather than writing
21139 a new implementation of symbol_computed_ops. */
8d749320 21140 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21141 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21142 (*baton)->per_cu = cu->per_cu;
21143 gdb_assert ((*baton)->per_cu);
ac56253d 21144
98bfdba5 21145 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21146 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21147 (*baton)->data = data;
ac56253d
TT
21148
21149 data[0] = DW_OP_addr;
21150 store_unsigned_integer (&data[1], cu_header->addr_size,
21151 byte_order, DW_ADDR (attr));
21152 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21153 }
c906108c 21154 break;
4ac36638 21155 case DW_FORM_string:
93b5768b 21156 case DW_FORM_strp:
cf532bd1 21157 case DW_FORM_strx:
3019eac3 21158 case DW_FORM_GNU_str_index:
36586728 21159 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21160 /* DW_STRING is already allocated on the objfile obstack, point
21161 directly to it. */
d521ce57 21162 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21163 break;
c906108c
SS
21164 case DW_FORM_block1:
21165 case DW_FORM_block2:
21166 case DW_FORM_block4:
21167 case DW_FORM_block:
2dc7f7b3 21168 case DW_FORM_exprloc:
0224619f 21169 case DW_FORM_data16:
c906108c 21170 blk = DW_BLOCK (attr);
98bfdba5
PA
21171 if (TYPE_LENGTH (type) != blk->size)
21172 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21173 TYPE_LENGTH (type));
21174 *bytes = blk->data;
c906108c 21175 break;
2df3850c
JM
21176
21177 /* The DW_AT_const_value attributes are supposed to carry the
21178 symbol's value "represented as it would be on the target
21179 architecture." By the time we get here, it's already been
21180 converted to host endianness, so we just need to sign- or
21181 zero-extend it as appropriate. */
21182 case DW_FORM_data1:
3aef2284 21183 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21184 break;
c906108c 21185 case DW_FORM_data2:
3aef2284 21186 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21187 break;
c906108c 21188 case DW_FORM_data4:
3aef2284 21189 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21190 break;
c906108c 21191 case DW_FORM_data8:
3aef2284 21192 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21193 break;
21194
c906108c 21195 case DW_FORM_sdata:
663c44ac 21196 case DW_FORM_implicit_const:
98bfdba5 21197 *value = DW_SND (attr);
2df3850c
JM
21198 break;
21199
c906108c 21200 case DW_FORM_udata:
98bfdba5 21201 *value = DW_UNSND (attr);
c906108c 21202 break;
2df3850c 21203
c906108c 21204 default:
b98664d3 21205 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21206 dwarf_form_name (attr->form));
98bfdba5 21207 *value = 0;
c906108c
SS
21208 break;
21209 }
21210}
21211
2df3850c 21212
98bfdba5
PA
21213/* Copy constant value from an attribute to a symbol. */
21214
2df3850c 21215static void
ff39bb5e 21216dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21217 struct dwarf2_cu *cu)
2df3850c 21218{
5e22e966 21219 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21220 LONGEST value;
d521ce57 21221 const gdb_byte *bytes;
98bfdba5 21222 struct dwarf2_locexpr_baton *baton;
2df3850c 21223
98bfdba5 21224 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21225 sym->print_name (),
98bfdba5
PA
21226 &objfile->objfile_obstack, cu,
21227 &value, &bytes, &baton);
2df3850c 21228
98bfdba5
PA
21229 if (baton != NULL)
21230 {
98bfdba5 21231 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21232 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21233 }
21234 else if (bytes != NULL)
21235 {
21236 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21237 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21238 }
21239 else
21240 {
21241 SYMBOL_VALUE (sym) = value;
f1e6e072 21242 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21243 }
2df3850c
JM
21244}
21245
c906108c
SS
21246/* Return the type of the die in question using its DW_AT_type attribute. */
21247
21248static struct type *
e7c27a73 21249die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21250{
c906108c 21251 struct attribute *type_attr;
c906108c 21252
e142c38c 21253 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21254 if (!type_attr)
21255 {
5e22e966 21256 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21257 /* A missing DW_AT_type represents a void type. */
518817b3 21258 return objfile_type (objfile)->builtin_void;
c906108c 21259 }
348e048f 21260
673bfd45 21261 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21262}
21263
b4ba55a1
JB
21264/* True iff CU's producer generates GNAT Ada auxiliary information
21265 that allows to find parallel types through that information instead
21266 of having to do expensive parallel lookups by type name. */
21267
21268static int
21269need_gnat_info (struct dwarf2_cu *cu)
21270{
de4cb04a
JB
21271 /* Assume that the Ada compiler was GNAT, which always produces
21272 the auxiliary information. */
21273 return (cu->language == language_ada);
b4ba55a1
JB
21274}
21275
b4ba55a1
JB
21276/* Return the auxiliary type of the die in question using its
21277 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21278 attribute is not present. */
21279
21280static struct type *
21281die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21282{
b4ba55a1 21283 struct attribute *type_attr;
b4ba55a1
JB
21284
21285 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21286 if (!type_attr)
21287 return NULL;
21288
673bfd45 21289 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21290}
21291
21292/* If DIE has a descriptive_type attribute, then set the TYPE's
21293 descriptive type accordingly. */
21294
21295static void
21296set_descriptive_type (struct type *type, struct die_info *die,
21297 struct dwarf2_cu *cu)
21298{
21299 struct type *descriptive_type = die_descriptive_type (die, cu);
21300
21301 if (descriptive_type)
21302 {
21303 ALLOCATE_GNAT_AUX_TYPE (type);
21304 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21305 }
21306}
21307
c906108c
SS
21308/* Return the containing type of the die in question using its
21309 DW_AT_containing_type attribute. */
21310
21311static struct type *
e7c27a73 21312die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21313{
c906108c 21314 struct attribute *type_attr;
5e22e966 21315 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21316
e142c38c 21317 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21318 if (!type_attr)
21319 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21320 "[in module %s]"), objfile_name (objfile));
33ac96f0 21321
673bfd45 21322 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21323}
21324
ac9ec31b
DE
21325/* Return an error marker type to use for the ill formed type in DIE/CU. */
21326
21327static struct type *
21328build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21329{
5e22e966 21330 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21331 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21332 char *saved;
ac9ec31b 21333
528e1572
SM
21334 std::string message
21335 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21336 objfile_name (objfile),
21337 sect_offset_str (cu->header.sect_off),
21338 sect_offset_str (die->sect_off));
efba19b0 21339 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21340
19f392bc 21341 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21342}
21343
673bfd45 21344/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21345 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21346 DW_AT_containing_type.
673bfd45
DE
21347 If there is no type substitute an error marker. */
21348
c906108c 21349static struct type *
ff39bb5e 21350lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21351 struct dwarf2_cu *cu)
c906108c 21352{
5e22e966 21353 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21354 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21355 struct type *this_type;
21356
ac9ec31b
DE
21357 gdb_assert (attr->name == DW_AT_type
21358 || attr->name == DW_AT_GNAT_descriptive_type
21359 || attr->name == DW_AT_containing_type);
21360
673bfd45
DE
21361 /* First see if we have it cached. */
21362
36586728
TT
21363 if (attr->form == DW_FORM_GNU_ref_alt)
21364 {
21365 struct dwarf2_per_cu_data *per_cu;
0826b30a 21366 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21367
ed2dc618
SM
21368 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21369 dwarf2_per_objfile);
9c541725 21370 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21371 }
cd6c91b4 21372 else if (attr->form_is_ref ())
673bfd45 21373 {
0826b30a 21374 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21375
9c541725 21376 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21377 }
55f1336d 21378 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21379 {
ac9ec31b 21380 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21381
ac9ec31b 21382 return get_signatured_type (die, signature, cu);
673bfd45
DE
21383 }
21384 else
21385 {
b98664d3 21386 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21387 " at %s [in module %s]"),
21388 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21389 objfile_name (objfile));
ac9ec31b 21390 return build_error_marker_type (cu, die);
673bfd45
DE
21391 }
21392
21393 /* If not cached we need to read it in. */
21394
21395 if (this_type == NULL)
21396 {
ac9ec31b 21397 struct die_info *type_die = NULL;
673bfd45
DE
21398 struct dwarf2_cu *type_cu = cu;
21399
cd6c91b4 21400 if (attr->form_is_ref ())
ac9ec31b
DE
21401 type_die = follow_die_ref (die, attr, &type_cu);
21402 if (type_die == NULL)
21403 return build_error_marker_type (cu, die);
21404 /* If we find the type now, it's probably because the type came
3019eac3
DE
21405 from an inter-CU reference and the type's CU got expanded before
21406 ours. */
ac9ec31b 21407 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21408 }
21409
21410 /* If we still don't have a type use an error marker. */
21411
21412 if (this_type == NULL)
ac9ec31b 21413 return build_error_marker_type (cu, die);
673bfd45 21414
f792889a 21415 return this_type;
c906108c
SS
21416}
21417
673bfd45
DE
21418/* Return the type in DIE, CU.
21419 Returns NULL for invalid types.
21420
02142a6c 21421 This first does a lookup in die_type_hash,
673bfd45
DE
21422 and only reads the die in if necessary.
21423
21424 NOTE: This can be called when reading in partial or full symbols. */
21425
f792889a 21426static struct type *
e7c27a73 21427read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21428{
f792889a
DJ
21429 struct type *this_type;
21430
21431 this_type = get_die_type (die, cu);
21432 if (this_type)
21433 return this_type;
21434
673bfd45
DE
21435 return read_type_die_1 (die, cu);
21436}
21437
21438/* Read the type in DIE, CU.
21439 Returns NULL for invalid types. */
21440
21441static struct type *
21442read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21443{
21444 struct type *this_type = NULL;
21445
c906108c
SS
21446 switch (die->tag)
21447 {
21448 case DW_TAG_class_type:
680b30c7 21449 case DW_TAG_interface_type:
c906108c
SS
21450 case DW_TAG_structure_type:
21451 case DW_TAG_union_type:
f792889a 21452 this_type = read_structure_type (die, cu);
c906108c
SS
21453 break;
21454 case DW_TAG_enumeration_type:
f792889a 21455 this_type = read_enumeration_type (die, cu);
c906108c
SS
21456 break;
21457 case DW_TAG_subprogram:
21458 case DW_TAG_subroutine_type:
edb3359d 21459 case DW_TAG_inlined_subroutine:
f792889a 21460 this_type = read_subroutine_type (die, cu);
c906108c
SS
21461 break;
21462 case DW_TAG_array_type:
f792889a 21463 this_type = read_array_type (die, cu);
c906108c 21464 break;
72019c9c 21465 case DW_TAG_set_type:
f792889a 21466 this_type = read_set_type (die, cu);
72019c9c 21467 break;
c906108c 21468 case DW_TAG_pointer_type:
f792889a 21469 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21470 break;
21471 case DW_TAG_ptr_to_member_type:
f792889a 21472 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21473 break;
21474 case DW_TAG_reference_type:
4297a3f0
AV
21475 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21476 break;
21477 case DW_TAG_rvalue_reference_type:
21478 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21479 break;
21480 case DW_TAG_const_type:
f792889a 21481 this_type = read_tag_const_type (die, cu);
c906108c
SS
21482 break;
21483 case DW_TAG_volatile_type:
f792889a 21484 this_type = read_tag_volatile_type (die, cu);
c906108c 21485 break;
06d66ee9
TT
21486 case DW_TAG_restrict_type:
21487 this_type = read_tag_restrict_type (die, cu);
21488 break;
c906108c 21489 case DW_TAG_string_type:
f792889a 21490 this_type = read_tag_string_type (die, cu);
c906108c
SS
21491 break;
21492 case DW_TAG_typedef:
f792889a 21493 this_type = read_typedef (die, cu);
c906108c 21494 break;
a02abb62 21495 case DW_TAG_subrange_type:
f792889a 21496 this_type = read_subrange_type (die, cu);
a02abb62 21497 break;
c906108c 21498 case DW_TAG_base_type:
f792889a 21499 this_type = read_base_type (die, cu);
c906108c 21500 break;
81a17f79 21501 case DW_TAG_unspecified_type:
f792889a 21502 this_type = read_unspecified_type (die, cu);
81a17f79 21503 break;
0114d602
DJ
21504 case DW_TAG_namespace:
21505 this_type = read_namespace_type (die, cu);
21506 break;
f55ee35c
JK
21507 case DW_TAG_module:
21508 this_type = read_module_type (die, cu);
21509 break;
a2c2acaf
MW
21510 case DW_TAG_atomic_type:
21511 this_type = read_tag_atomic_type (die, cu);
21512 break;
c906108c 21513 default:
b98664d3 21514 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21515 dwarf_tag_name (die->tag));
c906108c
SS
21516 break;
21517 }
63d06c5c 21518
f792889a 21519 return this_type;
63d06c5c
DC
21520}
21521
abc72ce4
DE
21522/* See if we can figure out if the class lives in a namespace. We do
21523 this by looking for a member function; its demangled name will
21524 contain namespace info, if there is any.
21525 Return the computed name or NULL.
21526 Space for the result is allocated on the objfile's obstack.
21527 This is the full-die version of guess_partial_die_structure_name.
21528 In this case we know DIE has no useful parent. */
21529
43816ebc 21530static const char *
abc72ce4
DE
21531guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21532{
21533 struct die_info *spec_die;
21534 struct dwarf2_cu *spec_cu;
21535 struct die_info *child;
5e22e966 21536 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21537
21538 spec_cu = cu;
21539 spec_die = die_specification (die, &spec_cu);
21540 if (spec_die != NULL)
21541 {
21542 die = spec_die;
21543 cu = spec_cu;
21544 }
21545
21546 for (child = die->child;
21547 child != NULL;
21548 child = child->sibling)
21549 {
21550 if (child->tag == DW_TAG_subprogram)
21551 {
73b9be8b 21552 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21553
7d45c7c3 21554 if (linkage_name != NULL)
abc72ce4 21555 {
43816ebc
TT
21556 gdb::unique_xmalloc_ptr<char> actual_name
21557 (language_class_name_from_physname (cu->language_defn,
21558 linkage_name));
21559 const char *name = NULL;
abc72ce4
DE
21560
21561 if (actual_name != NULL)
21562 {
15d034d0 21563 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21564
21565 if (die_name != NULL
43816ebc 21566 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21567 {
21568 /* Strip off the class name from the full name.
21569 We want the prefix. */
21570 int die_name_len = strlen (die_name);
43816ebc
TT
21571 int actual_name_len = strlen (actual_name.get ());
21572 const char *ptr = actual_name.get ();
abc72ce4
DE
21573
21574 /* Test for '::' as a sanity check. */
21575 if (actual_name_len > die_name_len + 2
43816ebc 21576 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21577 name = obstack_strndup (
e3b94546 21578 &objfile->per_bfd->storage_obstack,
43816ebc 21579 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21580 }
21581 }
abc72ce4
DE
21582 return name;
21583 }
21584 }
21585 }
21586
21587 return NULL;
21588}
21589
96408a79
SA
21590/* GCC might emit a nameless typedef that has a linkage name. Determine the
21591 prefix part in such case. See
21592 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21593
a121b7c1 21594static const char *
96408a79
SA
21595anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21596{
21597 struct attribute *attr;
e6a959d6 21598 const char *base;
96408a79
SA
21599
21600 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21601 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21602 return NULL;
21603
7d45c7c3 21604 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21605 return NULL;
21606
73b9be8b 21607 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21608 if (attr == NULL || DW_STRING (attr) == NULL)
21609 return NULL;
21610
21611 /* dwarf2_name had to be already called. */
21612 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21613
21614 /* Strip the base name, keep any leading namespaces/classes. */
21615 base = strrchr (DW_STRING (attr), ':');
21616 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21617 return "";
21618
5e22e966 21619 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21620 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21621 DW_STRING (attr),
21622 &base[-1] - DW_STRING (attr));
96408a79
SA
21623}
21624
fdde2d81 21625/* Return the name of the namespace/class that DIE is defined within,
0114d602 21626 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21627
0114d602
DJ
21628 For example, if we're within the method foo() in the following
21629 code:
21630
21631 namespace N {
21632 class C {
21633 void foo () {
21634 }
21635 };
21636 }
21637
21638 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21639
0d5cff50 21640static const char *
e142c38c 21641determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21642{
5e22e966 21643 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21644 struct die_info *parent, *spec_die;
21645 struct dwarf2_cu *spec_cu;
21646 struct type *parent_type;
a121b7c1 21647 const char *retval;
63d06c5c 21648
9c37b5ae 21649 if (cu->language != language_cplus
c44af4eb
TT
21650 && cu->language != language_fortran && cu->language != language_d
21651 && cu->language != language_rust)
0114d602
DJ
21652 return "";
21653
96408a79
SA
21654 retval = anonymous_struct_prefix (die, cu);
21655 if (retval)
21656 return retval;
21657
0114d602
DJ
21658 /* We have to be careful in the presence of DW_AT_specification.
21659 For example, with GCC 3.4, given the code
21660
21661 namespace N {
21662 void foo() {
21663 // Definition of N::foo.
21664 }
21665 }
21666
21667 then we'll have a tree of DIEs like this:
21668
21669 1: DW_TAG_compile_unit
21670 2: DW_TAG_namespace // N
21671 3: DW_TAG_subprogram // declaration of N::foo
21672 4: DW_TAG_subprogram // definition of N::foo
21673 DW_AT_specification // refers to die #3
21674
21675 Thus, when processing die #4, we have to pretend that we're in
21676 the context of its DW_AT_specification, namely the contex of die
21677 #3. */
21678 spec_cu = cu;
21679 spec_die = die_specification (die, &spec_cu);
21680 if (spec_die == NULL)
21681 parent = die->parent;
21682 else
63d06c5c 21683 {
0114d602
DJ
21684 parent = spec_die->parent;
21685 cu = spec_cu;
63d06c5c 21686 }
0114d602
DJ
21687
21688 if (parent == NULL)
21689 return "";
98bfdba5
PA
21690 else if (parent->building_fullname)
21691 {
21692 const char *name;
21693 const char *parent_name;
21694
21695 /* It has been seen on RealView 2.2 built binaries,
21696 DW_TAG_template_type_param types actually _defined_ as
21697 children of the parent class:
21698
21699 enum E {};
21700 template class <class Enum> Class{};
21701 Class<enum E> class_e;
21702
21703 1: DW_TAG_class_type (Class)
21704 2: DW_TAG_enumeration_type (E)
21705 3: DW_TAG_enumerator (enum1:0)
21706 3: DW_TAG_enumerator (enum2:1)
21707 ...
21708 2: DW_TAG_template_type_param
21709 DW_AT_type DW_FORM_ref_udata (E)
21710
21711 Besides being broken debug info, it can put GDB into an
21712 infinite loop. Consider:
21713
21714 When we're building the full name for Class<E>, we'll start
21715 at Class, and go look over its template type parameters,
21716 finding E. We'll then try to build the full name of E, and
21717 reach here. We're now trying to build the full name of E,
21718 and look over the parent DIE for containing scope. In the
21719 broken case, if we followed the parent DIE of E, we'd again
21720 find Class, and once again go look at its template type
21721 arguments, etc., etc. Simply don't consider such parent die
21722 as source-level parent of this die (it can't be, the language
21723 doesn't allow it), and break the loop here. */
21724 name = dwarf2_name (die, cu);
21725 parent_name = dwarf2_name (parent, cu);
b98664d3 21726 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21727 name ? name : "<unknown>",
21728 parent_name ? parent_name : "<unknown>");
21729 return "";
21730 }
63d06c5c 21731 else
0114d602
DJ
21732 switch (parent->tag)
21733 {
63d06c5c 21734 case DW_TAG_namespace:
0114d602 21735 parent_type = read_type_die (parent, cu);
acebe513
UW
21736 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21737 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21738 Work around this problem here. */
21739 if (cu->language == language_cplus
7d93a1e0 21740 && strcmp (parent_type->name (), "::") == 0)
acebe513 21741 return "";
0114d602 21742 /* We give a name to even anonymous namespaces. */
7d93a1e0 21743 return parent_type->name ();
63d06c5c 21744 case DW_TAG_class_type:
680b30c7 21745 case DW_TAG_interface_type:
63d06c5c 21746 case DW_TAG_structure_type:
0114d602 21747 case DW_TAG_union_type:
f55ee35c 21748 case DW_TAG_module:
0114d602 21749 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21750 if (parent_type->name () != NULL)
21751 return parent_type->name ();
0114d602
DJ
21752 else
21753 /* An anonymous structure is only allowed non-static data
21754 members; no typedefs, no member functions, et cetera.
21755 So it does not need a prefix. */
21756 return "";
abc72ce4 21757 case DW_TAG_compile_unit:
95554aad 21758 case DW_TAG_partial_unit:
abc72ce4
DE
21759 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21760 if (cu->language == language_cplus
5989a64e 21761 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21762 && die->child != NULL
21763 && (die->tag == DW_TAG_class_type
21764 || die->tag == DW_TAG_structure_type
21765 || die->tag == DW_TAG_union_type))
21766 {
43816ebc 21767 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21768 if (name != NULL)
21769 return name;
21770 }
21771 return "";
0a4b0913
AB
21772 case DW_TAG_subprogram:
21773 /* Nested subroutines in Fortran get a prefix with the name
21774 of the parent's subroutine. */
21775 if (cu->language == language_fortran)
21776 {
21777 if ((die->tag == DW_TAG_subprogram)
21778 && (dwarf2_name (parent, cu) != NULL))
21779 return dwarf2_name (parent, cu);
21780 }
21781 return determine_prefix (parent, cu);
3d567982
TT
21782 case DW_TAG_enumeration_type:
21783 parent_type = read_type_die (parent, cu);
21784 if (TYPE_DECLARED_CLASS (parent_type))
21785 {
7d93a1e0
SM
21786 if (parent_type->name () != NULL)
21787 return parent_type->name ();
3d567982
TT
21788 return "";
21789 }
21790 /* Fall through. */
63d06c5c 21791 default:
8176b9b8 21792 return determine_prefix (parent, cu);
63d06c5c 21793 }
63d06c5c
DC
21794}
21795
3e43a32a
MS
21796/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21797 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21798 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21799 an obconcat, otherwise allocate storage for the result. The CU argument is
21800 used to determine the language and hence, the appropriate separator. */
987504bb 21801
f55ee35c 21802#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21803
21804static char *
f55ee35c
JK
21805typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21806 int physname, struct dwarf2_cu *cu)
63d06c5c 21807{
f55ee35c 21808 const char *lead = "";
5c315b68 21809 const char *sep;
63d06c5c 21810
3e43a32a
MS
21811 if (suffix == NULL || suffix[0] == '\0'
21812 || prefix == NULL || prefix[0] == '\0')
987504bb 21813 sep = "";
45280282
IB
21814 else if (cu->language == language_d)
21815 {
21816 /* For D, the 'main' function could be defined in any module, but it
21817 should never be prefixed. */
21818 if (strcmp (suffix, "D main") == 0)
21819 {
21820 prefix = "";
21821 sep = "";
21822 }
21823 else
21824 sep = ".";
21825 }
f55ee35c
JK
21826 else if (cu->language == language_fortran && physname)
21827 {
21828 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21829 DW_AT_MIPS_linkage_name is preferred and used instead. */
21830
21831 lead = "__";
21832 sep = "_MOD_";
21833 }
987504bb
JJ
21834 else
21835 sep = "::";
63d06c5c 21836
6dd47d34
DE
21837 if (prefix == NULL)
21838 prefix = "";
21839 if (suffix == NULL)
21840 suffix = "";
21841
987504bb
JJ
21842 if (obs == NULL)
21843 {
3e43a32a 21844 char *retval
224c3ddb
SM
21845 = ((char *)
21846 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21847
f55ee35c
JK
21848 strcpy (retval, lead);
21849 strcat (retval, prefix);
6dd47d34
DE
21850 strcat (retval, sep);
21851 strcat (retval, suffix);
63d06c5c
DC
21852 return retval;
21853 }
987504bb
JJ
21854 else
21855 {
21856 /* We have an obstack. */
f55ee35c 21857 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21858 }
63d06c5c
DC
21859}
21860
71c25dea
TT
21861/* Get name of a die, return NULL if not found. */
21862
15d034d0
TT
21863static const char *
21864dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21865 struct objfile *objfile)
71c25dea
TT
21866{
21867 if (name && cu->language == language_cplus)
21868 {
596dc4ad
TT
21869 gdb::unique_xmalloc_ptr<char> canon_name
21870 = cp_canonicalize_string (name);
71c25dea 21871
596dc4ad
TT
21872 if (canon_name != nullptr)
21873 name = objfile->intern (canon_name.get ());
71c25dea
TT
21874 }
21875
21876 return name;
c906108c
SS
21877}
21878
96553a0c
DE
21879/* Get name of a die, return NULL if not found.
21880 Anonymous namespaces are converted to their magic string. */
9219021c 21881
15d034d0 21882static const char *
e142c38c 21883dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21884{
21885 struct attribute *attr;
5e22e966 21886 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21887
e142c38c 21888 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21889 if ((!attr || !DW_STRING (attr))
96553a0c 21890 && die->tag != DW_TAG_namespace
53832f31
TT
21891 && die->tag != DW_TAG_class_type
21892 && die->tag != DW_TAG_interface_type
21893 && die->tag != DW_TAG_structure_type
21894 && die->tag != DW_TAG_union_type)
71c25dea
TT
21895 return NULL;
21896
21897 switch (die->tag)
21898 {
21899 case DW_TAG_compile_unit:
95554aad 21900 case DW_TAG_partial_unit:
71c25dea
TT
21901 /* Compilation units have a DW_AT_name that is a filename, not
21902 a source language identifier. */
21903 case DW_TAG_enumeration_type:
21904 case DW_TAG_enumerator:
21905 /* These tags always have simple identifiers already; no need
21906 to canonicalize them. */
21907 return DW_STRING (attr);
907af001 21908
96553a0c
DE
21909 case DW_TAG_namespace:
21910 if (attr != NULL && DW_STRING (attr) != NULL)
21911 return DW_STRING (attr);
21912 return CP_ANONYMOUS_NAMESPACE_STR;
21913
907af001
UW
21914 case DW_TAG_class_type:
21915 case DW_TAG_interface_type:
21916 case DW_TAG_structure_type:
21917 case DW_TAG_union_type:
21918 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21919 structures or unions. These were of the form "._%d" in GCC 4.1,
21920 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21921 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21922 if (attr && DW_STRING (attr)
61012eef
GB
21923 && (startswith (DW_STRING (attr), "._")
21924 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21925 return NULL;
53832f31
TT
21926
21927 /* GCC might emit a nameless typedef that has a linkage name. See
21928 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21929 if (!attr || DW_STRING (attr) == NULL)
21930 {
73b9be8b 21931 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21932 if (attr == NULL || DW_STRING (attr) == NULL)
21933 return NULL;
21934
df5c6c50
JK
21935 /* Avoid demangling DW_STRING (attr) the second time on a second
21936 call for the same DIE. */
21937 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21938 {
43816ebc
TT
21939 gdb::unique_xmalloc_ptr<char> demangled
21940 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21941 if (demangled == nullptr)
21942 return nullptr;
43816ebc 21943
be1e3d3e 21944 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21945 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21946 }
67430cd0
TT
21947
21948 /* Strip any leading namespaces/classes, keep only the base name.
21949 DW_AT_name for named DIEs does not contain the prefixes. */
21950 const char *base = strrchr (DW_STRING (attr), ':');
21951 if (base && base > DW_STRING (attr) && base[-1] == ':')
21952 return &base[1];
21953 else
21954 return DW_STRING (attr);
53832f31 21955 }
907af001
UW
21956 break;
21957
71c25dea 21958 default:
907af001
UW
21959 break;
21960 }
21961
21962 if (!DW_STRING_IS_CANONICAL (attr))
21963 {
be1e3d3e
TT
21964 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21965 objfile);
907af001 21966 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21967 }
907af001 21968 return DW_STRING (attr);
9219021c
DC
21969}
21970
21971/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21972 is none. *EXT_CU is the CU containing DIE on input, and the CU
21973 containing the return value on output. */
9219021c
DC
21974
21975static struct die_info *
f2f0e013 21976dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21977{
21978 struct attribute *attr;
9219021c 21979
f2f0e013 21980 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21981 if (attr == NULL)
21982 return NULL;
21983
f2f0e013 21984 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21985}
21986
f9aca02d 21987static void
d97bc12b 21988dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21989{
21990 unsigned int i;
21991
d97bc12b 21992 print_spaces (indent, f);
9d8780f0 21993 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21994 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21995 sect_offset_str (die->sect_off));
d97bc12b
DE
21996
21997 if (die->parent != NULL)
21998 {
21999 print_spaces (indent, f);
9d8780f0
SM
22000 fprintf_unfiltered (f, " parent at offset: %s\n",
22001 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22002 }
22003
22004 print_spaces (indent, f);
22005 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22006 dwarf_bool_name (die->child != NULL));
c906108c 22007
d97bc12b
DE
22008 print_spaces (indent, f);
22009 fprintf_unfiltered (f, " attributes:\n");
22010
c906108c
SS
22011 for (i = 0; i < die->num_attrs; ++i)
22012 {
d97bc12b
DE
22013 print_spaces (indent, f);
22014 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22015 dwarf_attr_name (die->attrs[i].name),
22016 dwarf_form_name (die->attrs[i].form));
d97bc12b 22017
c906108c
SS
22018 switch (die->attrs[i].form)
22019 {
c906108c 22020 case DW_FORM_addr:
336d760d 22021 case DW_FORM_addrx:
3019eac3 22022 case DW_FORM_GNU_addr_index:
d97bc12b 22023 fprintf_unfiltered (f, "address: ");
5af949e3 22024 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22025 break;
22026 case DW_FORM_block2:
22027 case DW_FORM_block4:
22028 case DW_FORM_block:
22029 case DW_FORM_block1:
56eb65bd
SP
22030 fprintf_unfiltered (f, "block: size %s",
22031 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22032 break;
2dc7f7b3 22033 case DW_FORM_exprloc:
56eb65bd
SP
22034 fprintf_unfiltered (f, "expression: size %s",
22035 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22036 break;
0224619f
JK
22037 case DW_FORM_data16:
22038 fprintf_unfiltered (f, "constant of 16 bytes");
22039 break;
4568ecf9
DE
22040 case DW_FORM_ref_addr:
22041 fprintf_unfiltered (f, "ref address: ");
22042 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22043 break;
36586728
TT
22044 case DW_FORM_GNU_ref_alt:
22045 fprintf_unfiltered (f, "alt ref address: ");
22046 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22047 break;
10b3939b
DJ
22048 case DW_FORM_ref1:
22049 case DW_FORM_ref2:
22050 case DW_FORM_ref4:
4568ecf9
DE
22051 case DW_FORM_ref8:
22052 case DW_FORM_ref_udata:
d97bc12b 22053 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22054 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22055 break;
c906108c
SS
22056 case DW_FORM_data1:
22057 case DW_FORM_data2:
22058 case DW_FORM_data4:
ce5d95e1 22059 case DW_FORM_data8:
c906108c
SS
22060 case DW_FORM_udata:
22061 case DW_FORM_sdata:
43bbcdc2
PH
22062 fprintf_unfiltered (f, "constant: %s",
22063 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22064 break;
2dc7f7b3
TT
22065 case DW_FORM_sec_offset:
22066 fprintf_unfiltered (f, "section offset: %s",
22067 pulongest (DW_UNSND (&die->attrs[i])));
22068 break;
55f1336d 22069 case DW_FORM_ref_sig8:
ac9ec31b
DE
22070 fprintf_unfiltered (f, "signature: %s",
22071 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22072 break;
c906108c 22073 case DW_FORM_string:
4bdf3d34 22074 case DW_FORM_strp:
43988095 22075 case DW_FORM_line_strp:
cf532bd1 22076 case DW_FORM_strx:
3019eac3 22077 case DW_FORM_GNU_str_index:
36586728 22078 case DW_FORM_GNU_strp_alt:
8285870a 22079 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22080 DW_STRING (&die->attrs[i])
8285870a
JK
22081 ? DW_STRING (&die->attrs[i]) : "",
22082 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22083 break;
22084 case DW_FORM_flag:
22085 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22086 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22087 else
d97bc12b 22088 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22089 break;
2dc7f7b3
TT
22090 case DW_FORM_flag_present:
22091 fprintf_unfiltered (f, "flag: TRUE");
22092 break;
a8329558 22093 case DW_FORM_indirect:
0963b4bd
MS
22094 /* The reader will have reduced the indirect form to
22095 the "base form" so this form should not occur. */
5f48f8f3 22096 fprintf_unfiltered (f,
3e43a32a 22097 "unexpected attribute form: DW_FORM_indirect");
a8329558 22098 break;
663c44ac
JK
22099 case DW_FORM_implicit_const:
22100 fprintf_unfiltered (f, "constant: %s",
22101 plongest (DW_SND (&die->attrs[i])));
22102 break;
c906108c 22103 default:
d97bc12b 22104 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22105 die->attrs[i].form);
d97bc12b 22106 break;
c906108c 22107 }
d97bc12b 22108 fprintf_unfiltered (f, "\n");
c906108c
SS
22109 }
22110}
22111
f9aca02d 22112static void
d97bc12b 22113dump_die_for_error (struct die_info *die)
c906108c 22114{
d97bc12b
DE
22115 dump_die_shallow (gdb_stderr, 0, die);
22116}
22117
22118static void
22119dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22120{
22121 int indent = level * 4;
22122
22123 gdb_assert (die != NULL);
22124
22125 if (level >= max_level)
22126 return;
22127
22128 dump_die_shallow (f, indent, die);
22129
22130 if (die->child != NULL)
c906108c 22131 {
d97bc12b
DE
22132 print_spaces (indent, f);
22133 fprintf_unfiltered (f, " Children:");
22134 if (level + 1 < max_level)
22135 {
22136 fprintf_unfiltered (f, "\n");
22137 dump_die_1 (f, level + 1, max_level, die->child);
22138 }
22139 else
22140 {
3e43a32a
MS
22141 fprintf_unfiltered (f,
22142 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22143 }
22144 }
22145
22146 if (die->sibling != NULL && level > 0)
22147 {
22148 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22149 }
22150}
22151
d97bc12b
DE
22152/* This is called from the pdie macro in gdbinit.in.
22153 It's not static so gcc will keep a copy callable from gdb. */
22154
22155void
22156dump_die (struct die_info *die, int max_level)
22157{
22158 dump_die_1 (gdb_stdlog, 0, max_level, die);
22159}
22160
f9aca02d 22161static void
51545339 22162store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22163{
51545339 22164 void **slot;
c906108c 22165
9c541725
PA
22166 slot = htab_find_slot_with_hash (cu->die_hash, die,
22167 to_underlying (die->sect_off),
b64f50a1 22168 INSERT);
51545339
DJ
22169
22170 *slot = die;
c906108c
SS
22171}
22172
348e048f
DE
22173/* Follow reference or signature attribute ATTR of SRC_DIE.
22174 On entry *REF_CU is the CU of SRC_DIE.
22175 On exit *REF_CU is the CU of the result. */
22176
22177static struct die_info *
ff39bb5e 22178follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22179 struct dwarf2_cu **ref_cu)
22180{
22181 struct die_info *die;
22182
cd6c91b4 22183 if (attr->form_is_ref ())
348e048f 22184 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22185 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22186 die = follow_die_sig (src_die, attr, ref_cu);
22187 else
22188 {
22189 dump_die_for_error (src_die);
22190 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22191 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22192 }
22193
22194 return die;
03dd20cc
DJ
22195}
22196
5c631832 22197/* Follow reference OFFSET.
673bfd45
DE
22198 On entry *REF_CU is the CU of the source die referencing OFFSET.
22199 On exit *REF_CU is the CU of the result.
22200 Returns NULL if OFFSET is invalid. */
f504f079 22201
f9aca02d 22202static struct die_info *
9c541725 22203follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22204 struct dwarf2_cu **ref_cu)
c906108c 22205{
10b3939b 22206 struct die_info temp_die;
f2f0e013 22207 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22208 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22209
348e048f
DE
22210 gdb_assert (cu->per_cu != NULL);
22211
98bfdba5
PA
22212 target_cu = cu;
22213
3019eac3 22214 if (cu->per_cu->is_debug_types)
348e048f
DE
22215 {
22216 /* .debug_types CUs cannot reference anything outside their CU.
22217 If they need to, they have to reference a signatured type via
55f1336d 22218 DW_FORM_ref_sig8. */
4057dfde 22219 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22220 return NULL;
348e048f 22221 }
36586728 22222 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22223 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22224 {
22225 struct dwarf2_per_cu_data *per_cu;
9a619af0 22226
9c541725 22227 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22228 dwarf2_per_objfile);
03dd20cc
DJ
22229
22230 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22231 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 22232 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22233
10b3939b
DJ
22234 target_cu = per_cu->cu;
22235 }
98bfdba5
PA
22236 else if (cu->dies == NULL)
22237 {
22238 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22239 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22240 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22241 language_minimal);
98bfdba5 22242 }
c906108c 22243
f2f0e013 22244 *ref_cu = target_cu;
9c541725 22245 temp_die.sect_off = sect_off;
c24bdb02
KS
22246
22247 if (target_cu != cu)
22248 target_cu->ancestor = cu;
22249
9a3c8263 22250 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22251 &temp_die,
22252 to_underlying (sect_off));
5c631832 22253}
10b3939b 22254
5c631832
JK
22255/* Follow reference attribute ATTR of SRC_DIE.
22256 On entry *REF_CU is the CU of SRC_DIE.
22257 On exit *REF_CU is the CU of the result. */
22258
22259static struct die_info *
ff39bb5e 22260follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22261 struct dwarf2_cu **ref_cu)
22262{
0826b30a 22263 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22264 struct dwarf2_cu *cu = *ref_cu;
22265 struct die_info *die;
22266
9c541725 22267 die = follow_die_offset (sect_off,
36586728
TT
22268 (attr->form == DW_FORM_GNU_ref_alt
22269 || cu->per_cu->is_dwz),
22270 ref_cu);
5c631832 22271 if (!die)
9d8780f0
SM
22272 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22273 "at %s [in module %s]"),
22274 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22275 objfile_name (cu->per_objfile->objfile));
348e048f 22276
5c631832
JK
22277 return die;
22278}
22279
d4c9a4f8 22280/* See read.h. */
5c631832
JK
22281
22282struct dwarf2_locexpr_baton
9c541725 22283dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22284 dwarf2_per_cu_data *per_cu,
8b9737bf 22285 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22286 void *baton, bool resolve_abstract_p)
5c631832 22287{
918dd910 22288 struct dwarf2_cu *cu;
5c631832
JK
22289 struct die_info *die;
22290 struct attribute *attr;
22291 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22292 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22293 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22294
918dd910 22295 if (per_cu->cu == NULL)
ab432490 22296 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22297 cu = per_cu->cu;
cc12ce38
DE
22298 if (cu == NULL)
22299 {
22300 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22301 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22302 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22303 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22304 }
918dd910 22305
9c541725 22306 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22307 if (!die)
9d8780f0
SM
22308 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22309 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22310
22311 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22312 if (!attr && resolve_abstract_p
5989a64e
SM
22313 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22314 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22315 {
22316 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22317 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22318 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22319
3360b6e7 22320 for (const auto &cand_off
5989a64e 22321 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22322 {
3360b6e7
TV
22323 struct dwarf2_cu *cand_cu = cu;
22324 struct die_info *cand
22325 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22326 if (!cand
22327 || !cand->parent
e4a62c65
TV
22328 || cand->parent->tag != DW_TAG_subprogram)
22329 continue;
22330
22331 CORE_ADDR pc_low, pc_high;
22332 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22333 if (pc_low == ((CORE_ADDR) -1))
22334 continue;
22335 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22336 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22337 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22338 continue;
22339
22340 die = cand;
22341 attr = dwarf2_attr (die, DW_AT_location, cu);
22342 break;
22343 }
22344 }
22345
5c631832
JK
22346 if (!attr)
22347 {
e103e986
JK
22348 /* DWARF: "If there is no such attribute, then there is no effect.".
22349 DATA is ignored if SIZE is 0. */
5c631832 22350
e103e986 22351 retval.data = NULL;
5c631832
JK
22352 retval.size = 0;
22353 }
cd6c91b4 22354 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22355 {
22356 struct dwarf2_loclist_baton loclist_baton;
22357 CORE_ADDR pc = (*get_frame_pc) (baton);
22358 size_t size;
22359
22360 fill_in_loclist_baton (cu, &loclist_baton, attr);
22361
22362 retval.data = dwarf2_find_location_expression (&loclist_baton,
22363 &size, pc);
22364 retval.size = size;
22365 }
5c631832
JK
22366 else
22367 {
4fc6c0d5 22368 if (!attr->form_is_block ())
9d8780f0 22369 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22370 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22371 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22372
22373 retval.data = DW_BLOCK (attr)->data;
22374 retval.size = DW_BLOCK (attr)->size;
22375 }
a50264ba 22376 retval.per_objfile = dwarf2_per_objfile;
5c631832 22377 retval.per_cu = cu->per_cu;
918dd910 22378
ed2dc618 22379 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22380
5c631832 22381 return retval;
348e048f
DE
22382}
22383
d4c9a4f8 22384/* See read.h. */
8b9737bf
TT
22385
22386struct dwarf2_locexpr_baton
22387dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22388 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22389 CORE_ADDR (*get_frame_pc) (void *baton),
22390 void *baton)
22391{
9c541725 22392 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22393
9c541725 22394 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22395}
22396
b6807d98
TT
22397/* Write a constant of a given type as target-ordered bytes into
22398 OBSTACK. */
22399
22400static const gdb_byte *
22401write_constant_as_bytes (struct obstack *obstack,
22402 enum bfd_endian byte_order,
22403 struct type *type,
22404 ULONGEST value,
22405 LONGEST *len)
22406{
22407 gdb_byte *result;
22408
22409 *len = TYPE_LENGTH (type);
224c3ddb 22410 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22411 store_unsigned_integer (result, *len, byte_order, value);
22412
22413 return result;
22414}
22415
d4c9a4f8 22416/* See read.h. */
b6807d98
TT
22417
22418const gdb_byte *
9c541725 22419dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22420 dwarf2_per_cu_data *per_cu,
22421 obstack *obstack,
b6807d98
TT
22422 LONGEST *len)
22423{
22424 struct dwarf2_cu *cu;
22425 struct die_info *die;
22426 struct attribute *attr;
22427 const gdb_byte *result = NULL;
22428 struct type *type;
22429 LONGEST value;
22430 enum bfd_endian byte_order;
e3b94546 22431 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22432
b6807d98 22433 if (per_cu->cu == NULL)
ab432490 22434 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
b6807d98 22435 cu = per_cu->cu;
cc12ce38
DE
22436 if (cu == NULL)
22437 {
22438 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22439 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22440 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22441 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22442 }
b6807d98 22443
9c541725 22444 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22445 if (!die)
9d8780f0
SM
22446 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22447 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22448
22449 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22450 if (attr == NULL)
22451 return NULL;
22452
e3b94546 22453 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22454 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22455
22456 switch (attr->form)
22457 {
22458 case DW_FORM_addr:
336d760d 22459 case DW_FORM_addrx:
b6807d98
TT
22460 case DW_FORM_GNU_addr_index:
22461 {
22462 gdb_byte *tem;
22463
22464 *len = cu->header.addr_size;
224c3ddb 22465 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22466 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22467 result = tem;
22468 }
22469 break;
22470 case DW_FORM_string:
22471 case DW_FORM_strp:
cf532bd1 22472 case DW_FORM_strx:
b6807d98
TT
22473 case DW_FORM_GNU_str_index:
22474 case DW_FORM_GNU_strp_alt:
22475 /* DW_STRING is already allocated on the objfile obstack, point
22476 directly to it. */
22477 result = (const gdb_byte *) DW_STRING (attr);
22478 *len = strlen (DW_STRING (attr));
22479 break;
22480 case DW_FORM_block1:
22481 case DW_FORM_block2:
22482 case DW_FORM_block4:
22483 case DW_FORM_block:
22484 case DW_FORM_exprloc:
0224619f 22485 case DW_FORM_data16:
b6807d98
TT
22486 result = DW_BLOCK (attr)->data;
22487 *len = DW_BLOCK (attr)->size;
22488 break;
22489
22490 /* The DW_AT_const_value attributes are supposed to carry the
22491 symbol's value "represented as it would be on the target
22492 architecture." By the time we get here, it's already been
22493 converted to host endianness, so we just need to sign- or
22494 zero-extend it as appropriate. */
22495 case DW_FORM_data1:
22496 type = die_type (die, cu);
22497 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22498 if (result == NULL)
22499 result = write_constant_as_bytes (obstack, byte_order,
22500 type, value, len);
22501 break;
22502 case DW_FORM_data2:
22503 type = die_type (die, cu);
22504 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22505 if (result == NULL)
22506 result = write_constant_as_bytes (obstack, byte_order,
22507 type, value, len);
22508 break;
22509 case DW_FORM_data4:
22510 type = die_type (die, cu);
22511 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22512 if (result == NULL)
22513 result = write_constant_as_bytes (obstack, byte_order,
22514 type, value, len);
22515 break;
22516 case DW_FORM_data8:
22517 type = die_type (die, cu);
22518 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22519 if (result == NULL)
22520 result = write_constant_as_bytes (obstack, byte_order,
22521 type, value, len);
22522 break;
22523
22524 case DW_FORM_sdata:
663c44ac 22525 case DW_FORM_implicit_const:
b6807d98
TT
22526 type = die_type (die, cu);
22527 result = write_constant_as_bytes (obstack, byte_order,
22528 type, DW_SND (attr), len);
22529 break;
22530
22531 case DW_FORM_udata:
22532 type = die_type (die, cu);
22533 result = write_constant_as_bytes (obstack, byte_order,
22534 type, DW_UNSND (attr), len);
22535 break;
22536
22537 default:
b98664d3 22538 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22539 dwarf_form_name (attr->form));
22540 break;
22541 }
22542
22543 return result;
22544}
22545
d4c9a4f8 22546/* See read.h. */
7942e96e
AA
22547
22548struct type *
9c541725 22549dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22550 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22551{
22552 struct dwarf2_cu *cu;
22553 struct die_info *die;
22554
7942e96e 22555 if (per_cu->cu == NULL)
ab432490 22556 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
7942e96e
AA
22557 cu = per_cu->cu;
22558 if (!cu)
22559 return NULL;
22560
9c541725 22561 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22562 if (!die)
22563 return NULL;
22564
22565 return die_type (die, cu);
22566}
22567
8cb5117c 22568/* See read.h. */
8a9b8146
TT
22569
22570struct type *
b64f50a1 22571dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22572 struct dwarf2_per_cu_data *per_cu)
22573{
9c541725 22574 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22575 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22576}
22577
ac9ec31b 22578/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22579 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22580 On exit *REF_CU is the CU of the result.
22581 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22582
22583static struct die_info *
ac9ec31b
DE
22584follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22585 struct dwarf2_cu **ref_cu)
348e048f 22586{
348e048f 22587 struct die_info temp_die;
c24bdb02 22588 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22589 struct die_info *die;
22590
ac9ec31b
DE
22591 /* While it might be nice to assert sig_type->type == NULL here,
22592 we can get here for DW_AT_imported_declaration where we need
22593 the DIE not the type. */
348e048f
DE
22594
22595 /* If necessary, add it to the queue and load its DIEs. */
22596
95554aad 22597 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
ab432490 22598 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
348e048f 22599
348e048f 22600 sig_cu = sig_type->per_cu.cu;
69d751e3 22601 gdb_assert (sig_cu != NULL);
9c541725
PA
22602 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22603 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22604 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22605 to_underlying (temp_die.sect_off));
348e048f
DE
22606 if (die)
22607 {
5e22e966 22608 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
ed2dc618 22609
796a7ff8
DE
22610 /* For .gdb_index version 7 keep track of included TUs.
22611 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22612 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22613 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22614 {
ae640021 22615 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22616 }
22617
348e048f 22618 *ref_cu = sig_cu;
c24bdb02
KS
22619 if (sig_cu != cu)
22620 sig_cu->ancestor = cu;
22621
348e048f
DE
22622 return die;
22623 }
22624
ac9ec31b
DE
22625 return NULL;
22626}
22627
22628/* Follow signatured type referenced by ATTR in SRC_DIE.
22629 On entry *REF_CU is the CU of SRC_DIE.
22630 On exit *REF_CU is the CU of the result.
22631 The result is the DIE of the type.
22632 If the referenced type cannot be found an error is thrown. */
22633
22634static struct die_info *
ff39bb5e 22635follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22636 struct dwarf2_cu **ref_cu)
22637{
22638 ULONGEST signature = DW_SIGNATURE (attr);
22639 struct signatured_type *sig_type;
22640 struct die_info *die;
22641
22642 gdb_assert (attr->form == DW_FORM_ref_sig8);
22643
a2ce51a0 22644 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22645 /* sig_type will be NULL if the signatured type is missing from
22646 the debug info. */
22647 if (sig_type == NULL)
22648 {
22649 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22650 " from DIE at %s [in module %s]"),
22651 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22652 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22653 }
22654
22655 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22656 if (die == NULL)
22657 {
22658 dump_die_for_error (src_die);
22659 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22660 " from DIE at %s [in module %s]"),
22661 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22662 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22663 }
22664
22665 return die;
22666}
22667
22668/* Get the type specified by SIGNATURE referenced in DIE/CU,
22669 reading in and processing the type unit if necessary. */
22670
22671static struct type *
22672get_signatured_type (struct die_info *die, ULONGEST signature,
22673 struct dwarf2_cu *cu)
22674{
5e22e966 22675 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22676 struct signatured_type *sig_type;
22677 struct dwarf2_cu *type_cu;
22678 struct die_info *type_die;
22679 struct type *type;
22680
a2ce51a0 22681 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22682 /* sig_type will be NULL if the signatured type is missing from
22683 the debug info. */
22684 if (sig_type == NULL)
22685 {
b98664d3 22686 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22687 " from DIE at %s [in module %s]"),
22688 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22689 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22690 return build_error_marker_type (cu, die);
22691 }
22692
22693 /* If we already know the type we're done. */
22694 if (sig_type->type != NULL)
22695 return sig_type->type;
22696
22697 type_cu = cu;
22698 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22699 if (type_die != NULL)
22700 {
22701 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22702 is created. This is important, for example, because for c++ classes
22703 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22704 type = read_type_die (type_die, type_cu);
22705 if (type == NULL)
22706 {
b98664d3 22707 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22708 " referenced from DIE at %s [in module %s]"),
22709 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22710 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22711 type = build_error_marker_type (cu, die);
22712 }
22713 }
22714 else
22715 {
b98664d3 22716 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22717 " from DIE at %s [in module %s]"),
22718 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22719 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22720 type = build_error_marker_type (cu, die);
22721 }
22722 sig_type->type = type;
22723
22724 return type;
22725}
22726
22727/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22728 reading in and processing the type unit if necessary. */
22729
22730static struct type *
ff39bb5e 22731get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22732 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22733{
22734 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22735 if (attr->form_is_ref ())
ac9ec31b
DE
22736 {
22737 struct dwarf2_cu *type_cu = cu;
22738 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22739
22740 return read_type_die (type_die, type_cu);
22741 }
22742 else if (attr->form == DW_FORM_ref_sig8)
22743 {
22744 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22745 }
22746 else
22747 {
5e22e966 22748 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22749
b98664d3 22750 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22751 " at %s [in module %s]"),
22752 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22753 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22754 return build_error_marker_type (cu, die);
22755 }
348e048f
DE
22756}
22757
e5fe5e75 22758/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22759
22760static void
ab432490
SM
22761load_full_type_unit (dwarf2_per_cu_data *per_cu,
22762 dwarf2_per_objfile *per_objfile)
348e048f 22763{
52dc124a 22764 struct signatured_type *sig_type;
348e048f 22765
f4dc4d17 22766 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22767 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22768
6721b2ec
DE
22769 /* We have the per_cu, but we need the signatured_type.
22770 Fortunately this is an easy translation. */
22771 gdb_assert (per_cu->is_debug_types);
22772 sig_type = (struct signatured_type *) per_cu;
348e048f 22773
6721b2ec 22774 gdb_assert (per_cu->cu == NULL);
348e048f 22775
ab432490 22776 read_signatured_type (sig_type, per_objfile);
348e048f 22777
6721b2ec 22778 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22779}
22780
3019eac3
DE
22781/* Read in a signatured type and build its CU and DIEs.
22782 If the type is a stub for the real type in a DWO file,
22783 read in the real type from the DWO file as well. */
dee91e82
DE
22784
22785static void
ab432490
SM
22786read_signatured_type (signatured_type *sig_type,
22787 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22788{
22789 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22790
3019eac3 22791 gdb_assert (per_cu->is_debug_types);
dee91e82 22792 gdb_assert (per_cu->cu == NULL);
348e048f 22793
ab432490 22794 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22795
22796 if (!reader.dummy_p)
22797 {
22798 struct dwarf2_cu *cu = reader.cu;
22799 const gdb_byte *info_ptr = reader.info_ptr;
22800
22801 gdb_assert (cu->die_hash == NULL);
22802 cu->die_hash =
22803 htab_create_alloc_ex (cu->header.length / 12,
22804 die_hash,
22805 die_eq,
22806 NULL,
22807 &cu->comp_unit_obstack,
22808 hashtab_obstack_allocate,
22809 dummy_obstack_deallocate);
22810
3e225074 22811 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22812 reader.comp_unit_die->child
22813 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22814 reader.comp_unit_die);
22815 cu->dies = reader.comp_unit_die;
22816 /* comp_unit_die is not stored in die_hash, no need. */
22817
22818 /* We try not to read any attributes in this function, because
22819 not all CUs needed for references have been loaded yet, and
22820 symbol table processing isn't initialized. But we have to
22821 set the CU language, or we won't be able to build types
22822 correctly. Similarly, if we do not read the producer, we can
22823 not apply producer-specific interpretation. */
22824 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22825
22826 reader.keep ();
c0ab21c2
TT
22827 }
22828
7ee85ab1 22829 sig_type->per_cu.tu_read = 1;
c906108c
SS
22830}
22831
c906108c
SS
22832/* Decode simple location descriptions.
22833 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22834 the location and return the value. If COMPUTED is non-null, it is
22835 set to true to indicate that decoding was successful, and false
22836 otherwise. If COMPUTED is null, then this function may emit a
22837 complaint. */
c906108c
SS
22838
22839static CORE_ADDR
7d79de9a 22840decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22841{
5e22e966 22842 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22843 size_t i;
22844 size_t size = blk->size;
d521ce57 22845 const gdb_byte *data = blk->data;
21ae7a4d
JK
22846 CORE_ADDR stack[64];
22847 int stacki;
22848 unsigned int bytes_read, unsnd;
22849 gdb_byte op;
c906108c 22850
7d79de9a
TT
22851 if (computed != nullptr)
22852 *computed = false;
22853
21ae7a4d
JK
22854 i = 0;
22855 stacki = 0;
22856 stack[stacki] = 0;
22857 stack[++stacki] = 0;
22858
22859 while (i < size)
22860 {
22861 op = data[i++];
22862 switch (op)
22863 {
22864 case DW_OP_lit0:
22865 case DW_OP_lit1:
22866 case DW_OP_lit2:
22867 case DW_OP_lit3:
22868 case DW_OP_lit4:
22869 case DW_OP_lit5:
22870 case DW_OP_lit6:
22871 case DW_OP_lit7:
22872 case DW_OP_lit8:
22873 case DW_OP_lit9:
22874 case DW_OP_lit10:
22875 case DW_OP_lit11:
22876 case DW_OP_lit12:
22877 case DW_OP_lit13:
22878 case DW_OP_lit14:
22879 case DW_OP_lit15:
22880 case DW_OP_lit16:
22881 case DW_OP_lit17:
22882 case DW_OP_lit18:
22883 case DW_OP_lit19:
22884 case DW_OP_lit20:
22885 case DW_OP_lit21:
22886 case DW_OP_lit22:
22887 case DW_OP_lit23:
22888 case DW_OP_lit24:
22889 case DW_OP_lit25:
22890 case DW_OP_lit26:
22891 case DW_OP_lit27:
22892 case DW_OP_lit28:
22893 case DW_OP_lit29:
22894 case DW_OP_lit30:
22895 case DW_OP_lit31:
22896 stack[++stacki] = op - DW_OP_lit0;
22897 break;
f1bea926 22898
21ae7a4d
JK
22899 case DW_OP_reg0:
22900 case DW_OP_reg1:
22901 case DW_OP_reg2:
22902 case DW_OP_reg3:
22903 case DW_OP_reg4:
22904 case DW_OP_reg5:
22905 case DW_OP_reg6:
22906 case DW_OP_reg7:
22907 case DW_OP_reg8:
22908 case DW_OP_reg9:
22909 case DW_OP_reg10:
22910 case DW_OP_reg11:
22911 case DW_OP_reg12:
22912 case DW_OP_reg13:
22913 case DW_OP_reg14:
22914 case DW_OP_reg15:
22915 case DW_OP_reg16:
22916 case DW_OP_reg17:
22917 case DW_OP_reg18:
22918 case DW_OP_reg19:
22919 case DW_OP_reg20:
22920 case DW_OP_reg21:
22921 case DW_OP_reg22:
22922 case DW_OP_reg23:
22923 case DW_OP_reg24:
22924 case DW_OP_reg25:
22925 case DW_OP_reg26:
22926 case DW_OP_reg27:
22927 case DW_OP_reg28:
22928 case DW_OP_reg29:
22929 case DW_OP_reg30:
22930 case DW_OP_reg31:
22931 stack[++stacki] = op - DW_OP_reg0;
22932 if (i < size)
7d79de9a
TT
22933 {
22934 if (computed == nullptr)
22935 dwarf2_complex_location_expr_complaint ();
22936 else
22937 return 0;
22938 }
21ae7a4d 22939 break;
c906108c 22940
21ae7a4d
JK
22941 case DW_OP_regx:
22942 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22943 i += bytes_read;
22944 stack[++stacki] = unsnd;
22945 if (i < size)
7d79de9a
TT
22946 {
22947 if (computed == nullptr)
22948 dwarf2_complex_location_expr_complaint ();
22949 else
22950 return 0;
22951 }
21ae7a4d 22952 break;
c906108c 22953
21ae7a4d 22954 case DW_OP_addr:
c8a7a66f
TT
22955 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22956 &bytes_read);
21ae7a4d
JK
22957 i += bytes_read;
22958 break;
d53d4ac5 22959
21ae7a4d
JK
22960 case DW_OP_const1u:
22961 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22962 i += 1;
22963 break;
22964
22965 case DW_OP_const1s:
22966 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22967 i += 1;
22968 break;
22969
22970 case DW_OP_const2u:
22971 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22972 i += 2;
22973 break;
22974
22975 case DW_OP_const2s:
22976 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22977 i += 2;
22978 break;
d53d4ac5 22979
21ae7a4d
JK
22980 case DW_OP_const4u:
22981 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22982 i += 4;
22983 break;
22984
22985 case DW_OP_const4s:
22986 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22987 i += 4;
22988 break;
22989
585861ea
JK
22990 case DW_OP_const8u:
22991 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22992 i += 8;
22993 break;
22994
21ae7a4d
JK
22995 case DW_OP_constu:
22996 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22997 &bytes_read);
22998 i += bytes_read;
22999 break;
23000
23001 case DW_OP_consts:
23002 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23003 i += bytes_read;
23004 break;
23005
23006 case DW_OP_dup:
23007 stack[stacki + 1] = stack[stacki];
23008 stacki++;
23009 break;
23010
23011 case DW_OP_plus:
23012 stack[stacki - 1] += stack[stacki];
23013 stacki--;
23014 break;
23015
23016 case DW_OP_plus_uconst:
23017 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23018 &bytes_read);
23019 i += bytes_read;
23020 break;
23021
23022 case DW_OP_minus:
23023 stack[stacki - 1] -= stack[stacki];
23024 stacki--;
23025 break;
23026
23027 case DW_OP_deref:
23028 /* If we're not the last op, then we definitely can't encode
23029 this using GDB's address_class enum. This is valid for partial
23030 global symbols, although the variable's address will be bogus
23031 in the psymtab. */
23032 if (i < size)
7d79de9a
TT
23033 {
23034 if (computed == nullptr)
23035 dwarf2_complex_location_expr_complaint ();
23036 else
23037 return 0;
23038 }
21ae7a4d
JK
23039 break;
23040
23041 case DW_OP_GNU_push_tls_address:
4aa4e28b 23042 case DW_OP_form_tls_address:
21ae7a4d
JK
23043 /* The top of the stack has the offset from the beginning
23044 of the thread control block at which the variable is located. */
23045 /* Nothing should follow this operator, so the top of stack would
23046 be returned. */
23047 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23048 address will be bogus in the psymtab. Make it always at least
23049 non-zero to not look as a variable garbage collected by linker
23050 which have DW_OP_addr 0. */
21ae7a4d 23051 if (i < size)
7d79de9a
TT
23052 {
23053 if (computed == nullptr)
23054 dwarf2_complex_location_expr_complaint ();
23055 else
23056 return 0;
23057 }
585861ea 23058 stack[stacki]++;
21ae7a4d
JK
23059 break;
23060
23061 case DW_OP_GNU_uninit:
7d79de9a
TT
23062 if (computed != nullptr)
23063 return 0;
21ae7a4d
JK
23064 break;
23065
336d760d 23066 case DW_OP_addrx:
3019eac3 23067 case DW_OP_GNU_addr_index:
49f6c839 23068 case DW_OP_GNU_const_index:
3019eac3
DE
23069 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23070 &bytes_read);
23071 i += bytes_read;
23072 break;
23073
21ae7a4d 23074 default:
7d79de9a
TT
23075 if (computed == nullptr)
23076 {
23077 const char *name = get_DW_OP_name (op);
21ae7a4d 23078
7d79de9a
TT
23079 if (name)
23080 complaint (_("unsupported stack op: '%s'"),
23081 name);
23082 else
23083 complaint (_("unsupported stack op: '%02x'"),
23084 op);
23085 }
21ae7a4d
JK
23086
23087 return (stack[stacki]);
d53d4ac5 23088 }
3c6e0cb3 23089
21ae7a4d
JK
23090 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23091 outside of the allocated space. Also enforce minimum>0. */
23092 if (stacki >= ARRAY_SIZE (stack) - 1)
23093 {
7d79de9a
TT
23094 if (computed == nullptr)
23095 complaint (_("location description stack overflow"));
21ae7a4d
JK
23096 return 0;
23097 }
23098
23099 if (stacki <= 0)
23100 {
7d79de9a
TT
23101 if (computed == nullptr)
23102 complaint (_("location description stack underflow"));
21ae7a4d
JK
23103 return 0;
23104 }
23105 }
7d79de9a
TT
23106
23107 if (computed != nullptr)
23108 *computed = true;
21ae7a4d 23109 return (stack[stacki]);
c906108c
SS
23110}
23111
23112/* memory allocation interface */
23113
c906108c 23114static struct dwarf_block *
7b5a2f43 23115dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23116{
8d749320 23117 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23118}
23119
c906108c 23120static struct die_info *
b60c80d6 23121dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23122{
23123 struct die_info *die;
b60c80d6
DJ
23124 size_t size = sizeof (struct die_info);
23125
23126 if (num_attrs > 1)
23127 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23128
b60c80d6 23129 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23130 memset (die, 0, sizeof (struct die_info));
23131 return (die);
23132}
2e276125
JB
23133
23134\f
a036ba48 23135
c90ec28a 23136/* Macro support. */
cf2c3c16 23137
9eac9650
TT
23138/* An overload of dwarf_decode_macros that finds the correct section
23139 and ensures it is read in before calling the other overload. */
23140
23141static void
23142dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23143 int section_is_gnu)
23144{
5e22e966 23145 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23146 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23147 const struct line_header *lh = cu->line_header;
9eac9650
TT
23148 unsigned int offset_size = cu->header.offset_size;
23149 struct dwarf2_section_info *section;
23150 const char *section_name;
23151
23152 if (cu->dwo_unit != nullptr)
23153 {
23154 if (section_is_gnu)
23155 {
23156 section = &cu->dwo_unit->dwo_file->sections.macro;
23157 section_name = ".debug_macro.dwo";
23158 }
23159 else
23160 {
23161 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23162 section_name = ".debug_macinfo.dwo";
23163 }
23164 }
23165 else
23166 {
23167 if (section_is_gnu)
23168 {
5989a64e 23169 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23170 section_name = ".debug_macro";
23171 }
23172 else
23173 {
5989a64e 23174 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23175 section_name = ".debug_macinfo";
23176 }
23177 }
23178
23179 section->read (objfile);
23180 if (section->buffer == nullptr)
23181 {
23182 complaint (_("missing %s section"), section_name);
23183 return;
23184 }
23185
23186 buildsym_compunit *builder = cu->get_builder ();
23187
23188 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23189 offset_size, offset, section_is_gnu);
23190}
23191
3019eac3
DE
23192/* Return the .debug_loc section to use for CU.
23193 For DWO files use .debug_loc.dwo. */
23194
23195static struct dwarf2_section_info *
23196cu_debug_loc_section (struct dwarf2_cu *cu)
23197{
5e22e966 23198 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23199
3019eac3 23200 if (cu->dwo_unit)
43988095
JK
23201 {
23202 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23203
43988095
JK
23204 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23205 }
5989a64e
SM
23206 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23207 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23208}
23209
8cf6f0b1
TT
23210/* A helper function that fills in a dwarf2_loclist_baton. */
23211
23212static void
23213fill_in_loclist_baton (struct dwarf2_cu *cu,
23214 struct dwarf2_loclist_baton *baton,
ff39bb5e 23215 const struct attribute *attr)
8cf6f0b1 23216{
5e22e966 23217 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23218 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23219
96b79293 23220 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23221
a50264ba 23222 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23223 baton->per_cu = cu->per_cu;
23224 gdb_assert (baton->per_cu);
23225 /* We don't know how long the location list is, but make sure we
23226 don't run off the edge of the section. */
3019eac3
DE
23227 baton->size = section->size - DW_UNSND (attr);
23228 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23229 if (cu->base_address.has_value ())
23230 baton->base_address = *cu->base_address;
23231 else
23232 baton->base_address = 0;
f664829e 23233 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23234}
23235
4c2df51b 23236static void
ff39bb5e 23237dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23238 struct dwarf2_cu *cu, int is_block)
4c2df51b 23239{
5e22e966 23240 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23241 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23242 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23243
cd6c91b4 23244 if (attr->form_is_section_offset ()
3019eac3 23245 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23246 the section. If so, fall through to the complaint in the
23247 other branch. */
2c7d5afc 23248 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23249 {
0d53c4c4 23250 struct dwarf2_loclist_baton *baton;
4c2df51b 23251
8d749320 23252 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23253
8cf6f0b1 23254 fill_in_loclist_baton (cu, baton, attr);
be391dca 23255
2b24b6e4 23256 if (!cu->base_address.has_value ())
b98664d3 23257 complaint (_("Location list used without "
3e43a32a 23258 "specifying the CU base address."));
4c2df51b 23259
f1e6e072
TT
23260 SYMBOL_ACLASS_INDEX (sym) = (is_block
23261 ? dwarf2_loclist_block_index
23262 : dwarf2_loclist_index);
0d53c4c4
DJ
23263 SYMBOL_LOCATION_BATON (sym) = baton;
23264 }
23265 else
23266 {
23267 struct dwarf2_locexpr_baton *baton;
23268
8d749320 23269 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23270 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23271 baton->per_cu = cu->per_cu;
23272 gdb_assert (baton->per_cu);
0d53c4c4 23273
4fc6c0d5 23274 if (attr->form_is_block ())
0d53c4c4
DJ
23275 {
23276 /* Note that we're just copying the block's data pointer
23277 here, not the actual data. We're still pointing into the
6502dd73
DJ
23278 info_buffer for SYM's objfile; right now we never release
23279 that buffer, but when we do clean up properly this may
23280 need to change. */
0d53c4c4
DJ
23281 baton->size = DW_BLOCK (attr)->size;
23282 baton->data = DW_BLOCK (attr)->data;
23283 }
23284 else
23285 {
23286 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23287 sym->natural_name ());
0d53c4c4 23288 baton->size = 0;
0d53c4c4 23289 }
6e70227d 23290
f1e6e072
TT
23291 SYMBOL_ACLASS_INDEX (sym) = (is_block
23292 ? dwarf2_locexpr_block_index
23293 : dwarf2_locexpr_index);
0d53c4c4
DJ
23294 SYMBOL_LOCATION_BATON (sym) = baton;
23295 }
4c2df51b 23296}
6502dd73 23297
09ba997f 23298/* See read.h. */
ae0d2f24
UW
23299
23300struct objfile *
09ba997f 23301dwarf2_per_cu_data::objfile () const
ae0d2f24 23302{
09ba997f 23303 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23304
23305 /* Return the master objfile, so that we can report and look up the
23306 correct file containing this variable. */
23307 if (objfile->separate_debug_objfile_backlink)
23308 objfile = objfile->separate_debug_objfile_backlink;
23309
23310 return objfile;
23311}
23312
96408a79
SA
23313/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23314 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23315 CU_HEADERP first. */
23316
23317static const struct comp_unit_head *
23318per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23319 const struct dwarf2_per_cu_data *per_cu)
96408a79 23320{
d521ce57 23321 const gdb_byte *info_ptr;
96408a79
SA
23322
23323 if (per_cu->cu)
23324 return &per_cu->cu->header;
23325
9c541725 23326 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23327
23328 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23329 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23330 rcuh_kind::COMPILE);
96408a79
SA
23331
23332 return cu_headerp;
23333}
23334
09ba997f 23335/* See read.h. */
ae0d2f24 23336
98714339 23337int
09ba997f 23338dwarf2_per_cu_data::addr_size () const
ae0d2f24 23339{
96408a79
SA
23340 struct comp_unit_head cu_header_local;
23341 const struct comp_unit_head *cu_headerp;
c471e790 23342
09ba997f 23343 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23344
23345 return cu_headerp->addr_size;
ae0d2f24
UW
23346}
23347
09ba997f 23348/* See read.h. */
9eae7c52
TT
23349
23350int
09ba997f 23351dwarf2_per_cu_data::offset_size () const
9eae7c52 23352{
96408a79
SA
23353 struct comp_unit_head cu_header_local;
23354 const struct comp_unit_head *cu_headerp;
9c6c53f7 23355
09ba997f 23356 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23357
23358 return cu_headerp->offset_size;
23359}
23360
09ba997f 23361/* See read.h. */
96408a79
SA
23362
23363int
09ba997f 23364dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23365{
23366 struct comp_unit_head cu_header_local;
23367 const struct comp_unit_head *cu_headerp;
23368
09ba997f 23369 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23370
23371 if (cu_headerp->version == 2)
23372 return cu_headerp->addr_size;
23373 else
23374 return cu_headerp->offset_size;
181cebd4
JK
23375}
23376
09ba997f 23377/* See read.h. */
9aa1f1e3
TT
23378
23379CORE_ADDR
09ba997f 23380dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23381{
09ba997f
TT
23382 struct objfile *objfile = dwarf2_per_objfile->objfile;
23383
23384 return objfile->text_section_offset ();
9aa1f1e3
TT
23385}
23386
09ba997f
TT
23387/* See read.h. */
23388
23389struct type *
23390dwarf2_per_cu_data::addr_type () const
9a49df9d 23391{
09ba997f 23392 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23393 struct type *void_type = objfile_type (objfile)->builtin_void;
23394 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23395 int addr_size = this->addr_size ();
9a49df9d
AB
23396
23397 if (TYPE_LENGTH (addr_type) == addr_size)
23398 return addr_type;
23399
09ba997f 23400 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23401 return addr_type;
23402}
23403
22b6cd70
TT
23404/* A helper function for dwarf2_find_containing_comp_unit that returns
23405 the index of the result, and that searches a vector. It will
23406 return a result even if the offset in question does not actually
23407 occur in any CU. This is separate so that it can be unit
23408 tested. */
ae038cb0 23409
22b6cd70
TT
23410static int
23411dwarf2_find_containing_comp_unit
23412 (sect_offset sect_off,
23413 unsigned int offset_in_dwz,
23414 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23415{
ae038cb0
DJ
23416 int low, high;
23417
ae038cb0 23418 low = 0;
22b6cd70 23419 high = all_comp_units.size () - 1;
ae038cb0
DJ
23420 while (high > low)
23421 {
36586728 23422 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23423 int mid = low + (high - low) / 2;
9a619af0 23424
22b6cd70 23425 mid_cu = all_comp_units[mid];
36586728 23426 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23427 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23428 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23429 high = mid;
23430 else
23431 low = mid + 1;
23432 }
23433 gdb_assert (low == high);
22b6cd70
TT
23434 return low;
23435}
23436
23437/* Locate the .debug_info compilation unit from CU's objfile which contains
23438 the DIE at OFFSET. Raises an error on failure. */
23439
23440static struct dwarf2_per_cu_data *
23441dwarf2_find_containing_comp_unit (sect_offset sect_off,
23442 unsigned int offset_in_dwz,
23443 struct dwarf2_per_objfile *dwarf2_per_objfile)
23444{
23445 int low
23446 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23447 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23448 struct dwarf2_per_cu_data *this_cu
5989a64e 23449 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23450
45b8ae0c 23451 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23452 {
36586728 23453 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23454 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23455 "offset %s [in module %s]"),
23456 sect_offset_str (sect_off),
ed2dc618 23457 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23458
5989a64e 23459 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23460 <= sect_off);
5989a64e 23461 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23462 }
23463 else
23464 {
5989a64e 23465 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23466 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23467 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23468 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23469 return this_cu;
23470 }
23471}
23472
22b6cd70
TT
23473#if GDB_SELF_TEST
23474
23475namespace selftests {
23476namespace find_containing_comp_unit {
23477
23478static void
23479run_test ()
23480{
23481 struct dwarf2_per_cu_data one {};
23482 struct dwarf2_per_cu_data two {};
23483 struct dwarf2_per_cu_data three {};
23484 struct dwarf2_per_cu_data four {};
23485
23486 one.length = 5;
23487 two.sect_off = sect_offset (one.length);
23488 two.length = 7;
23489
23490 three.length = 5;
23491 three.is_dwz = 1;
23492 four.sect_off = sect_offset (three.length);
23493 four.length = 7;
23494 four.is_dwz = 1;
23495
23496 std::vector<dwarf2_per_cu_data *> units;
23497 units.push_back (&one);
23498 units.push_back (&two);
23499 units.push_back (&three);
23500 units.push_back (&four);
23501
23502 int result;
23503
23504 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23505 SELF_CHECK (units[result] == &one);
23506 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23507 SELF_CHECK (units[result] == &one);
23508 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23509 SELF_CHECK (units[result] == &two);
23510
23511 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23512 SELF_CHECK (units[result] == &three);
23513 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23514 SELF_CHECK (units[result] == &three);
23515 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23516 SELF_CHECK (units[result] == &four);
23517}
23518
23519}
23520}
23521
23522#endif /* GDB_SELF_TEST */
23523
9e021579 23524/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23525
9e021579
SM
23526dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23527 dwarf2_per_objfile *per_objfile)
23528 : per_cu (per_cu),
23529 per_objfile (per_objfile),
9068261f
AB
23530 mark (false),
23531 has_loclist (false),
23532 checked_producer (false),
23533 producer_is_gxx_lt_4_6 (false),
23534 producer_is_gcc_lt_4_3 (false),
eb77c9df 23535 producer_is_icc (false),
9068261f 23536 producer_is_icc_lt_14 (false),
c258c396 23537 producer_is_codewarrior (false),
9068261f 23538 processing_has_namespace_info (false)
93311388 23539{
fcd3b13d
SM
23540 per_cu->cu = this;
23541}
23542
23543/* Destroy a dwarf2_cu. */
23544
23545dwarf2_cu::~dwarf2_cu ()
23546{
23547 per_cu->cu = NULL;
9816fde3
JK
23548}
23549
23550/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23551
23552static void
95554aad
TT
23553prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23554 enum language pretend_language)
9816fde3
JK
23555{
23556 struct attribute *attr;
23557
23558 /* Set the language we're debugging. */
23559 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23560 if (attr != nullptr)
9816fde3
JK
23561 set_cu_language (DW_UNSND (attr), cu);
23562 else
9cded63f 23563 {
95554aad 23564 cu->language = pretend_language;
9cded63f
TT
23565 cu->language_defn = language_def (cu->language);
23566 }
dee91e82 23567
7d45c7c3 23568 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23569}
23570
ae038cb0
DJ
23571/* Increase the age counter on each cached compilation unit, and free
23572 any that are too old. */
23573
23574static void
ed2dc618 23575age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23576{
23577 struct dwarf2_per_cu_data *per_cu, **last_chain;
23578
5989a64e
SM
23579 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23580 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23581 while (per_cu != NULL)
23582 {
23583 per_cu->cu->last_used ++;
b4f54984 23584 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23585 dwarf2_mark (per_cu->cu);
23586 per_cu = per_cu->cu->read_in_chain;
23587 }
23588
5989a64e
SM
23589 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23590 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23591 while (per_cu != NULL)
23592 {
23593 struct dwarf2_per_cu_data *next_cu;
23594
23595 next_cu = per_cu->cu->read_in_chain;
23596
23597 if (!per_cu->cu->mark)
23598 {
fcd3b13d 23599 delete per_cu->cu;
ae038cb0
DJ
23600 *last_chain = next_cu;
23601 }
23602 else
23603 last_chain = &per_cu->cu->read_in_chain;
23604
23605 per_cu = next_cu;
23606 }
23607}
23608
23609/* Remove a single compilation unit from the cache. */
23610
23611static void
dee91e82 23612free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23613{
23614 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23615 struct dwarf2_per_objfile *dwarf2_per_objfile
23616 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23617
5989a64e
SM
23618 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23619 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23620 while (per_cu != NULL)
23621 {
23622 struct dwarf2_per_cu_data *next_cu;
23623
23624 next_cu = per_cu->cu->read_in_chain;
23625
dee91e82 23626 if (per_cu == target_per_cu)
ae038cb0 23627 {
fcd3b13d 23628 delete per_cu->cu;
dee91e82 23629 per_cu->cu = NULL;
ae038cb0
DJ
23630 *last_chain = next_cu;
23631 break;
23632 }
23633 else
23634 last_chain = &per_cu->cu->read_in_chain;
23635
23636 per_cu = next_cu;
23637 }
23638}
23639
dee91e82
DE
23640/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23641 We store these in a hash table separate from the DIEs, and preserve them
23642 when the DIEs are flushed out of cache.
23643
23644 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23645 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23646 or the type may come from a DWO file. Furthermore, while it's more logical
23647 to use per_cu->section+offset, with Fission the section with the data is in
23648 the DWO file but we don't know that section at the point we need it.
23649 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23650 because we can enter the lookup routine, get_die_type_at_offset, from
23651 outside this file, and thus won't necessarily have PER_CU->cu.
23652 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23653
dee91e82 23654struct dwarf2_per_cu_offset_and_type
1c379e20 23655{
dee91e82 23656 const struct dwarf2_per_cu_data *per_cu;
9c541725 23657 sect_offset sect_off;
1c379e20
DJ
23658 struct type *type;
23659};
23660
dee91e82 23661/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23662
23663static hashval_t
dee91e82 23664per_cu_offset_and_type_hash (const void *item)
1c379e20 23665{
9a3c8263
SM
23666 const struct dwarf2_per_cu_offset_and_type *ofs
23667 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23668
9c541725 23669 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23670}
23671
dee91e82 23672/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23673
23674static int
dee91e82 23675per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23676{
9a3c8263
SM
23677 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23678 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23679 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23680 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23681
dee91e82 23682 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23683 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23684}
23685
23686/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23687 table if necessary. For convenience, return TYPE.
23688
23689 The DIEs reading must have careful ordering to:
85102364 23690 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23691 reading current DIE.
23692 * Not trying to dereference contents of still incompletely read in types
23693 while reading in other DIEs.
23694 * Enable referencing still incompletely read in types just by a pointer to
23695 the type without accessing its fields.
23696
23697 Therefore caller should follow these rules:
23698 * Try to fetch any prerequisite types we may need to build this DIE type
23699 before building the type and calling set_die_type.
e71ec853 23700 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23701 possible before fetching more types to complete the current type.
23702 * Make the type as complete as possible before fetching more types. */
1c379e20 23703
f792889a 23704static struct type *
1c379e20
DJ
23705set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23706{
5e22e966 23707 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23708 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23709 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23710 struct attribute *attr;
23711 struct dynamic_prop prop;
1c379e20 23712
b4ba55a1
JB
23713 /* For Ada types, make sure that the gnat-specific data is always
23714 initialized (if not already set). There are a few types where
23715 we should not be doing so, because the type-specific area is
23716 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23717 where the type-specific area is used to store the floatformat).
23718 But this is not a problem, because the gnat-specific information
23719 is actually not needed for these types. */
23720 if (need_gnat_info (cu)
78134374
SM
23721 && type->code () != TYPE_CODE_FUNC
23722 && type->code () != TYPE_CODE_FLT
23723 && type->code () != TYPE_CODE_METHODPTR
23724 && type->code () != TYPE_CODE_MEMBERPTR
23725 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23726 && !HAVE_GNAT_AUX_INFO (type))
23727 INIT_GNAT_SPECIFIC (type);
23728
3f2f83dd
KB
23729 /* Read DW_AT_allocated and set in type. */
23730 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23731 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23732 {
09ba997f 23733 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23734 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23735 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23736 }
23737 else if (attr != NULL)
23738 {
b98664d3 23739 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23740 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23741 sect_offset_str (die->sect_off));
3f2f83dd
KB
23742 }
23743
23744 /* Read DW_AT_associated and set in type. */
23745 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23746 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23747 {
09ba997f 23748 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23749 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23750 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23751 }
23752 else if (attr != NULL)
23753 {
b98664d3 23754 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23755 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23756 sect_offset_str (die->sect_off));
3f2f83dd
KB
23757 }
23758
3cdcd0ce
JB
23759 /* Read DW_AT_data_location and set in type. */
23760 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23761 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23762 cu->per_cu->addr_type ()))
5c54719c 23763 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23764
ae090bdb
SM
23765 if (dwarf2_per_objfile->die_type_hash == NULL)
23766 dwarf2_per_objfile->die_type_hash
0335378b
TT
23767 = htab_up (htab_create_alloc (127,
23768 per_cu_offset_and_type_hash,
23769 per_cu_offset_and_type_eq,
23770 NULL, xcalloc, xfree));
1c379e20 23771
dee91e82 23772 ofs.per_cu = cu->per_cu;
9c541725 23773 ofs.sect_off = die->sect_off;
1c379e20 23774 ofs.type = type;
dee91e82 23775 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23776 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23777 if (*slot)
b98664d3 23778 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23779 sect_offset_str (die->sect_off));
8d749320
SM
23780 *slot = XOBNEW (&objfile->objfile_obstack,
23781 struct dwarf2_per_cu_offset_and_type);
1c379e20 23782 **slot = ofs;
f792889a 23783 return type;
1c379e20
DJ
23784}
23785
9c541725 23786/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23787 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23788
23789static struct type *
9c541725 23790get_die_type_at_offset (sect_offset sect_off,
673bfd45 23791 struct dwarf2_per_cu_data *per_cu)
1c379e20 23792{
dee91e82 23793 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23794 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23795
ae090bdb 23796 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23797 return NULL;
1c379e20 23798
dee91e82 23799 ofs.per_cu = per_cu;
9c541725 23800 ofs.sect_off = sect_off;
9a3c8263 23801 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23802 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23803 if (slot)
23804 return slot->type;
23805 else
23806 return NULL;
23807}
23808
02142a6c 23809/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23810 or return NULL if DIE does not have a saved type. */
23811
23812static struct type *
23813get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23814{
9c541725 23815 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23816}
23817
10b3939b
DJ
23818/* Add a dependence relationship from CU to REF_PER_CU. */
23819
23820static void
23821dwarf2_add_dependence (struct dwarf2_cu *cu,
23822 struct dwarf2_per_cu_data *ref_per_cu)
23823{
23824 void **slot;
23825
23826 if (cu->dependencies == NULL)
23827 cu->dependencies
23828 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23829 NULL, &cu->comp_unit_obstack,
23830 hashtab_obstack_allocate,
23831 dummy_obstack_deallocate);
23832
23833 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23834 if (*slot == NULL)
23835 *slot = ref_per_cu;
23836}
1c379e20 23837
f504f079
DE
23838/* Subroutine of dwarf2_mark to pass to htab_traverse.
23839 Set the mark field in every compilation unit in the
ae038cb0
DJ
23840 cache that we must keep because we are keeping CU. */
23841
10b3939b
DJ
23842static int
23843dwarf2_mark_helper (void **slot, void *data)
23844{
23845 struct dwarf2_per_cu_data *per_cu;
23846
23847 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23848
23849 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23850 reading of the chain. As such dependencies remain valid it is not much
23851 useful to track and undo them during QUIT cleanups. */
23852 if (per_cu->cu == NULL)
23853 return 1;
23854
10b3939b
DJ
23855 if (per_cu->cu->mark)
23856 return 1;
9068261f 23857 per_cu->cu->mark = true;
10b3939b
DJ
23858
23859 if (per_cu->cu->dependencies != NULL)
23860 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23861
23862 return 1;
23863}
23864
f504f079
DE
23865/* Set the mark field in CU and in every other compilation unit in the
23866 cache that we must keep because we are keeping CU. */
23867
ae038cb0
DJ
23868static void
23869dwarf2_mark (struct dwarf2_cu *cu)
23870{
23871 if (cu->mark)
23872 return;
9068261f 23873 cu->mark = true;
10b3939b
DJ
23874 if (cu->dependencies != NULL)
23875 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23876}
23877
23878static void
23879dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23880{
23881 while (per_cu)
23882 {
9068261f 23883 per_cu->cu->mark = false;
ae038cb0
DJ
23884 per_cu = per_cu->cu->read_in_chain;
23885 }
72bf9492
DJ
23886}
23887
72bf9492
DJ
23888/* Trivial hash function for partial_die_info: the hash value of a DIE
23889 is its offset in .debug_info for this objfile. */
23890
23891static hashval_t
23892partial_die_hash (const void *item)
23893{
9a3c8263
SM
23894 const struct partial_die_info *part_die
23895 = (const struct partial_die_info *) item;
9a619af0 23896
9c541725 23897 return to_underlying (part_die->sect_off);
72bf9492
DJ
23898}
23899
23900/* Trivial comparison function for partial_die_info structures: two DIEs
23901 are equal if they have the same offset. */
23902
23903static int
23904partial_die_eq (const void *item_lhs, const void *item_rhs)
23905{
9a3c8263
SM
23906 const struct partial_die_info *part_die_lhs
23907 = (const struct partial_die_info *) item_lhs;
23908 const struct partial_die_info *part_die_rhs
23909 = (const struct partial_die_info *) item_rhs;
9a619af0 23910
9c541725 23911 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23912}
23913
3c3bb058
AB
23914struct cmd_list_element *set_dwarf_cmdlist;
23915struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23916
9291a0cd 23917static void
cd4fb1b2
SM
23918show_check_physname (struct ui_file *file, int from_tty,
23919 struct cmd_list_element *c, const char *value)
9291a0cd 23920{
cd4fb1b2
SM
23921 fprintf_filtered (file,
23922 _("Whether to check \"physname\" is %s.\n"),
23923 value);
9291a0cd
TT
23924}
23925
6c265988 23926void _initialize_dwarf2_read ();
cd4fb1b2 23927void
6c265988 23928_initialize_dwarf2_read ()
9291a0cd 23929{
0743fc83 23930 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23931Set DWARF specific variables.\n\
590042fc 23932Configure DWARF variables such as the cache size."),
0743fc83
TT
23933 &set_dwarf_cmdlist, "maintenance set dwarf ",
23934 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23935
0743fc83 23936 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23937Show DWARF specific variables.\n\
23938Show DWARF variables such as the cache size."),
0743fc83
TT
23939 &show_dwarf_cmdlist, "maintenance show dwarf ",
23940 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23941
cd4fb1b2
SM
23942 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23943 &dwarf_max_cache_age, _("\
23944Set the upper bound on the age of cached DWARF compilation units."), _("\
23945Show the upper bound on the age of cached DWARF compilation units."), _("\
23946A higher limit means that cached compilation units will be stored\n\
23947in memory longer, and more total memory will be used. Zero disables\n\
23948caching, which can slow down startup."),
23949 NULL,
23950 show_dwarf_max_cache_age,
23951 &set_dwarf_cmdlist,
23952 &show_dwarf_cmdlist);
156942c7 23953
cd4fb1b2
SM
23954 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23955Set debugging of the DWARF reader."), _("\
23956Show debugging of the DWARF reader."), _("\
23957When enabled (non-zero), debugging messages are printed during DWARF\n\
23958reading and symtab expansion. A value of 1 (one) provides basic\n\
23959information. A value greater than 1 provides more verbose information."),
23960 NULL,
23961 NULL,
23962 &setdebuglist, &showdebuglist);
9291a0cd 23963
cd4fb1b2
SM
23964 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23965Set debugging of the DWARF DIE reader."), _("\
23966Show debugging of the DWARF DIE reader."), _("\
23967When enabled (non-zero), DIEs are dumped after they are read in.\n\
23968The value is the maximum depth to print."),
23969 NULL,
23970 NULL,
23971 &setdebuglist, &showdebuglist);
9291a0cd 23972
cd4fb1b2
SM
23973 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23974Set debugging of the dwarf line reader."), _("\
23975Show debugging of the dwarf line reader."), _("\
23976When enabled (non-zero), line number entries are dumped as they are read in.\n\
23977A value of 1 (one) provides basic information.\n\
23978A value greater than 1 provides more verbose information."),
23979 NULL,
23980 NULL,
23981 &setdebuglist, &showdebuglist);
437afbb8 23982
cd4fb1b2
SM
23983 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23984Set cross-checking of \"physname\" code against demangler."), _("\
23985Show cross-checking of \"physname\" code against demangler."), _("\
23986When enabled, GDB's internal \"physname\" code is checked against\n\
23987the demangler."),
23988 NULL, show_check_physname,
23989 &setdebuglist, &showdebuglist);
900e11f9 23990
e615022a
DE
23991 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23992 no_class, &use_deprecated_index_sections, _("\
23993Set whether to use deprecated gdb_index sections."), _("\
23994Show whether to use deprecated gdb_index sections."), _("\
23995When enabled, deprecated .gdb_index sections are used anyway.\n\
23996Normally they are ignored either because of a missing feature or\n\
23997performance issue.\n\
23998Warning: This option must be enabled before gdb reads the file."),
23999 NULL,
24000 NULL,
24001 &setlist, &showlist);
24002
f1e6e072
TT
24003 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24004 &dwarf2_locexpr_funcs);
24005 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24006 &dwarf2_loclist_funcs);
24007
24008 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24009 &dwarf2_block_frame_base_locexpr_funcs);
24010 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24011 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24012
24013#if GDB_SELF_TEST
24014 selftests::register_test ("dw2_expand_symtabs_matching",
24015 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24016 selftests::register_test ("dwarf2_find_containing_comp_unit",
24017 selftests::find_containing_comp_unit::run_test);
c62446b1 24018#endif
6502dd73 24019}
This page took 5.797385 seconds and 4 git commands to generate.