Pass dwarf2_cu objects to dwo-related functions, instead of dwarf2_per_cu_data
[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
7aa104c4
SM
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
0018ea6f 1209
f1902523
JK
1210static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
3e225074 1212 struct die_info *type_unit_die);
f1902523 1213
ed2dc618
SM
1214static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1216
72bf9492
DJ
1217static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1219 int, struct dwarf2_cu *);
c906108c 1220
72bf9492
DJ
1221static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
63d06c5c 1223
72bf9492
DJ
1224static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1226 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1227
5d7cb8df 1228static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1229 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1230 struct dwarf2_cu *cu);
1231
72bf9492
DJ
1232static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
91c24f0a 1234
bc30ff58
JB
1235static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1237 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1238
d521ce57 1239static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1240
dee91e82 1241static struct partial_die_info *load_partial_dies
d521ce57 1242 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1243
fb816e8b
TV
1244/* A pair of partial_die_info and compilation unit. */
1245struct cu_partial_die_info
1246{
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
122cf0f2
AB
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
405feb71 1255 { /* Nothing. */ }
122cf0f2
AB
1256
1257private:
1258 cu_partial_die_info () = delete;
fb816e8b
TV
1259};
1260
122cf0f2
AB
1261static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
72bf9492 1263
d521ce57
TT
1264static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1266 const gdb_byte *, bool *need_reprocess);
1267
1268static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1272
ed2dc618
SM
1273static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1276
ed2dc618
SM
1277static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1280
ed2dc618 1281static const char *read_indirect_string_at_offset
4f44ae6c 1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1283
d521ce57
TT
1284static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
3019eac3
DE
1286 unsigned int *);
1287
18a8505e
AT
1288static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
3019eac3 1293
e142c38c 1294static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1295
e142c38c
DJ
1296static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
c906108c 1298
7d45c7c3
KB
1299static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
a084a2a6
AT
1302static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
05cf31d1
JB
1304static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
e142c38c 1307static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1308
e142c38c 1309static struct die_info *die_specification (struct die_info *die,
f2f0e013 1310 struct dwarf2_cu **);
63d06c5c 1311
9c541725 1312static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1313 struct dwarf2_cu *cu);
debd256d 1314
f3f5162e 1315static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1316 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1317 CORE_ADDR, int decode_mapping);
c906108c 1318
804d2729
TT
1319static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
c906108c 1321
a14ed312 1322static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1323 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1324
ff39bb5e 1325static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1326 struct dwarf2_cu *);
c906108c 1327
ff39bb5e 1328static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
12df843f 1332 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1333 const gdb_byte **bytes,
98bfdba5 1334 struct dwarf2_locexpr_baton **baton);
2df3850c 1335
e7c27a73 1336static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1337
b4ba55a1
JB
1338static int need_gnat_info (struct dwarf2_cu *);
1339
3e43a32a
MS
1340static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
b4ba55a1
JB
1342
1343static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
e7c27a73
DJ
1346static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
c906108c 1348
ff39bb5e 1349static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1350 struct dwarf2_cu *);
c906108c 1351
f792889a 1352static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1353
673bfd45
DE
1354static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
0d5cff50 1356static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1357
6e70227d 1358static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
63d06c5c 1361
e7c27a73 1362static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1363
348e048f
DE
1364static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
e7c27a73 1366static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1367
e7c27a73 1368static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1369
96408a79
SA
1370static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
71a3c369
TT
1372static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
ff013f42 1374static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1375 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1376
41144253 1377/* Return the .debug_loclists section to use for cu. */
1378static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
3a2b436a 1380/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1381 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1382enum pc_bounds_kind
1383{
e385593e 1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1385 PC_BOUNDS_NOT_PRESENT,
1386
e385593e
JK
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
3a2b436a
JK
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396};
1397
1398static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
891813be 1401 dwarf2_psymtab *);
c906108c 1402
fae299cd
DC
1403static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
801e3a5b
JB
1407static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
a14ed312 1410static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1411 struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1414 struct type *, struct dwarf2_cu *);
c906108c 1415
a14ed312 1416static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1417 struct die_info *, struct type *,
e7c27a73 1418 struct dwarf2_cu *);
c906108c 1419
a14ed312 1420static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1421 struct type *,
1422 struct dwarf2_cu *);
c906108c 1423
134d01f1 1424static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1425
e7c27a73 1426static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1427
e7c27a73 1428static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1429
5d7cb8df
JK
1430static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
804d2729 1432static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1433
27aa8d6a
SW
1434static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
74921315
KS
1436static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
f55ee35c
JK
1438static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
38d518c9 1441static const char *namespace_name (struct die_info *die,
e142c38c 1442 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1443
134d01f1 1444static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
7d79de9a
TT
1446static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
c906108c 1448
6e70227d 1449static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1450 struct dwarf2_cu *);
1451
bf6af496 1452static struct die_info *read_die_and_siblings_1
d521ce57 1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1454 struct die_info *);
639d11d3 1455
dee91e82 1456static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
639d11d3
DC
1459 struct die_info *parent);
1460
d521ce57
TT
1461static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
3e225074 1463 int);
3019eac3 1464
d521ce57 1465static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1466 struct die_info **, const gdb_byte *);
93311388 1467
e7c27a73 1468static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1469
15d034d0 1470static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1471 struct objfile *);
71c25dea 1472
15d034d0 1473static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1474
15d034d0 1475static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
ca69b9e6
DE
1479static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
e142c38c 1482static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1483 struct dwarf2_cu **);
9219021c 1484
d97bc12b
DE
1485static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487static void dump_die_for_error (struct die_info *);
1488
1489static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
c906108c 1491
d97bc12b 1492/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1493
51545339 1494static void store_in_ref_table (struct die_info *,
10b3939b 1495 struct dwarf2_cu *);
c906108c 1496
348e048f 1497static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
10b3939b 1501static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1502 const struct attribute *,
f2f0e013 1503 struct dwarf2_cu **);
c906108c 1504
348e048f 1505static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1506 const struct attribute *,
348e048f
DE
1507 struct dwarf2_cu **);
1508
ac9ec31b
DE
1509static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1513 const struct attribute *,
ac9ec31b
DE
1514 struct dwarf2_cu *);
1515
ab432490
SM
1516static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
348e048f 1518
ab432490
SM
1519static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
348e048f 1521
63e43d3a
PMR
1522static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1524 struct dynamic_prop *prop, struct type *type);
63e43d3a 1525
c906108c
SS
1526/* memory allocation interface */
1527
7b5a2f43 1528static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1529
b60c80d6 1530static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1531
43f3e411 1532static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1533
8cf6f0b1
TT
1534static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
ff39bb5e 1536 const struct attribute *attr);
8cf6f0b1 1537
ff39bb5e 1538static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1539 struct symbol *sym,
f1e6e072
TT
1540 struct dwarf2_cu *cu,
1541 int is_block);
4c2df51b 1542
d521ce57
TT
1543static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
4bb7a0a7 1546
72bf9492
DJ
1547static hashval_t partial_die_hash (const void *item);
1548
1549static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
ae038cb0 1551static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1554
9816fde3 1555static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
93311388 1558
ed2dc618 1559static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1560
dee91e82 1561static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1562
f792889a
DJ
1563static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1c379e20 1565
ed2dc618 1566static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1567
ed2dc618 1568static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1569
ab432490
SM
1570static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
10b3939b 1574
47b14e86
SM
1575static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1576 dwarf2_per_objfile *per_objfile,
1577 enum language pretend_language);
10b3939b 1578
47b14e86
SM
1579static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1580 dwarf2_per_objfile *per_objfile,
1581 enum language pretend_language);
f4dc4d17 1582
10b3939b
DJ
1583static void dwarf2_add_dependence (struct dwarf2_cu *,
1584 struct dwarf2_per_cu_data *);
1585
ae038cb0
DJ
1586static void dwarf2_mark (struct dwarf2_cu *);
1587
1588static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1589
b64f50a1 1590static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1591 struct dwarf2_per_cu_data *);
673bfd45 1592
f792889a 1593static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1594
95554aad
TT
1595static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1596 enum language pretend_language);
1597
ed2dc618 1598static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1599
b303c6f6
AB
1600/* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605class dwarf2_queue_guard
1606{
1607public:
39856def
TT
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
b303c6f6
AB
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
39856def
TT
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
5989a64e 1619 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1620 }
b303c6f6 1621
39856def 1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1623
39856def
TT
1624private:
1625 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1626};
1627
39856def
TT
1628dwarf2_queue_item::~dwarf2_queue_item ()
1629{
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
1635 free_one_cached_comp_unit (per_cu);
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1669 ULONGEST signature, int is_debug_types);
a2ce51a0 1670
ed2dc618
SM
1671static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1673
3019eac3 1674static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
3019eac3
DE
1677
1678static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1680
89e63ee4
DE
1681static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
263db9a1
TT
1683/* A unique pointer to a dwo_file. */
1684
51ac9db5 1685typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1686
ed2dc618 1687static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1688
1b80a9fa 1689static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1690
1691static void free_line_header_voidp (void *arg);
4390d890
DE
1692\f
1693/* Various complaints about symbol reading that don't abort the process. */
1694
4390d890
DE
1695static void
1696dwarf2_debug_line_missing_file_complaint (void)
1697{
b98664d3 1698 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1699}
1700
1701static void
1702dwarf2_debug_line_missing_end_sequence_complaint (void)
1703{
b98664d3 1704 complaint (_(".debug_line section has line "
4390d890
DE
1705 "program sequence without an end"));
1706}
1707
1708static void
1709dwarf2_complex_location_expr_complaint (void)
1710{
b98664d3 1711 complaint (_("location expression too complex"));
4390d890
DE
1712}
1713
1714static void
1715dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717{
b98664d3 1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1719 arg1, arg2, arg3);
1720}
1721
4390d890
DE
1722static void
1723dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724{
b98664d3 1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1726 arg1, arg2);
1727}
527f3840
JK
1728
1729/* Hash function for line_header_hash. */
1730
1731static hashval_t
1732line_header_hash (const struct line_header *ofs)
1733{
9c541725 1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1735}
1736
1737/* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash_voidp (const void *item)
1741{
9a3c8263 1742 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1743
1744 return line_header_hash (ofs);
1745}
1746
1747/* Equality function for line_header_hash. */
1748
1749static int
1750line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751{
9a3c8263
SM
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1754
9c541725 1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757}
1758
4390d890 1759\f
9291a0cd 1760
330cdd98
PA
1761/* See declaration. */
1762
5989a64e
SM
1763dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
c3699833
SM
1765 : obfd (obfd),
1766 can_copy (can_copy_)
330cdd98
PA
1767{
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
330cdd98
PA
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773}
1774
5989a64e 1775dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1776{
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
b76e467d 1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1781 per_cu->imported_symtabs_free ();
fc8e7e75 1782
b2bdb8cf 1783 for (signatured_type *sig_type : all_type_units)
ae640021 1784 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1785
5989a64e 1786 /* Everything else should be on this->obstack. */
330cdd98
PA
1787}
1788
1789/* See declaration. */
1790
1791void
5989a64e 1792dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1793{
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
fcd3b13d 1800 delete per_cu->cu;
330cdd98
PA
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804}
1805
11ed8cad
TT
1806/* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809class free_cached_comp_units
1810{
1811public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
5989a64e 1820 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828};
1829
af758d11
SM
1830/* See read.h. */
1831
1832bool
1833dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838}
1839
1840/* See read.h. */
1841
1842compunit_symtab *
1843dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848}
1849
1850/* See read.h. */
1851
1852void
1853dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855{
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860}
1861
c906108c 1862/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
c906108c
SS
1868
1869int
251d32d9 1870dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
c906108c 1873{
97cbe998
SDJ
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
ed2dc618
SM
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
5bfd760d 1888
5989a64e
SM
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1893}
1894
251d32d9
TG
1895/* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
233a11ab
CS
1897
1898static int
251d32d9
TG
1899section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
233a11ab 1901{
251d32d9
TG
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
233a11ab
CS
1909}
1910
330cdd98 1911/* See declaration. */
c906108c 1912
330cdd98 1913void
5989a64e
SM
1914dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
c906108c 1916{
fd361982 1917 flagword aflag = bfd_section_flags (sectp);
251d32d9 1918
dc7650b8
JK
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
950b7495
KS
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.info))
c906108c 1932 {
330cdd98 1933 this->info.s.section = sectp;
fd361982 1934 this->info.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1937 {
330cdd98 1938 this->abbrev.s.section = sectp;
fd361982 1939 this->abbrev.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.line))
c906108c 1942 {
330cdd98 1943 this->line.s.section = sectp;
fd361982 1944 this->line.size = bfd_section_size (sectp);
c906108c 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.loc))
c906108c 1947 {
330cdd98 1948 this->loc.s.section = sectp;
fd361982 1949 this->loc.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.loclists))
43988095 1952 {
330cdd98 1953 this->loclists.s.section = sectp;
fd361982 1954 this->loclists.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1957 {
330cdd98 1958 this->macinfo.s.section = sectp;
fd361982 1959 this->macinfo.size = bfd_section_size (sectp);
c906108c 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1962 {
330cdd98 1963 this->macro.s.section = sectp;
fd361982 1964 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.str))
c906108c 1967 {
330cdd98 1968 this->str.s.section = sectp;
fd361982 1969 this->str.size = bfd_section_size (sectp);
c906108c 1970 }
18a8505e
AT
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.line_str))
43988095 1977 {
330cdd98 1978 this->line_str.s.section = sectp;
fd361982 1979 this->line_str.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1982 {
330cdd98 1983 this->addr.s.section = sectp;
fd361982 1984 this->addr.size = bfd_section_size (sectp);
3019eac3 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1987 {
330cdd98 1988 this->frame.s.section = sectp;
fd361982 1989 this->frame.size = bfd_section_size (sectp);
b6af0555 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1992 {
330cdd98 1993 this->eh_frame.s.section = sectp;
fd361982 1994 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1997 {
330cdd98 1998 this->ranges.s.section = sectp;
fd361982 1999 this->ranges.size = bfd_section_size (sectp);
af34e669 2000 }
330cdd98 2001 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2002 {
330cdd98 2003 this->rnglists.s.section = sectp;
fd361982 2004 this->rnglists.size = bfd_section_size (sectp);
43988095 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.types))
348e048f 2007 {
8b70b953
TT
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
049412e3 2011 type_section.s.section = sectp;
fd361982 2012 type_section.size = bfd_section_size (sectp);
8b70b953 2013
fd5866f6 2014 this->types.push_back (type_section);
348e048f 2015 }
330cdd98 2016 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2017 {
330cdd98 2018 this->gdb_index.s.section = sectp;
fd361982 2019 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2020 }
927aa2e7
JK
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
fd361982 2024 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
fd361982 2029 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2030 }
dce234bc 2031
fd361982
AM
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
330cdd98 2034 this->has_section_at_zero = true;
c906108c
SS
2035}
2036
dce234bc 2037/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2038 SECTION_NAME. */
af34e669 2039
dce234bc 2040void
3017a003
TG
2041dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
d521ce57 2043 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2044 bfd_size_type *sizep)
2045{
5bfd760d 2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2047 struct dwarf2_section_info *info;
a3b2a86b
TT
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
3017a003
TG
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
5989a64e 2061 info = &data->per_bfd->frame;
3017a003
TG
2062 break;
2063 case DWARF2_EH_FRAME:
5989a64e 2064 info = &data->per_bfd->eh_frame;
3017a003
TG
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
dce234bc 2069
96b79293 2070 info->read (objfile);
dce234bc 2071
96b79293 2072 *sectp = info->get_bfd_section ();
dce234bc
PP
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075}
2076
36586728
TT
2077/* A helper function to find the sections for a .dwz file. */
2078
2079static void
2080locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081{
9a3c8263 2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
049412e3 2088 dwz_file->abbrev.s.section = sectp;
fd361982 2089 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
049412e3 2093 dwz_file->info.s.section = sectp;
fd361982 2094 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
049412e3 2098 dwz_file->str.s.section = sectp;
fd361982 2099 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
049412e3 2103 dwz_file->line.s.section = sectp;
fd361982 2104 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
049412e3 2108 dwz_file->macro.s.section = sectp;
fd361982 2109 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2110 }
2ec9a5e0
TT
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
049412e3 2113 dwz_file->gdb_index.s.section = sectp;
fd361982 2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2115 }
927aa2e7
JK
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
fd361982 2119 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2120 }
36586728
TT
2121}
2122
c4973306 2123/* See dwarf2read.h. */
36586728 2124
c4973306 2125struct dwz_file *
c3699833 2126dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2127{
36586728 2128 const char *filename;
acd13123 2129 bfd_size_type buildid_len_arg;
dc294be5
TT
2130 size_t buildid_len;
2131 bfd_byte *buildid;
36586728 2132
c3699833
SM
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
36586728 2135
4db1a1dc 2136 bfd_set_error (bfd_error_no_error);
791afaa2 2137 gdb::unique_xmalloc_ptr<char> data
c3699833 2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2139 &buildid_len_arg, &buildid));
4db1a1dc
TT
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
791afaa2
TT
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2149
acd13123
TT
2150 buildid_len = (size_t) buildid_len_arg;
2151
791afaa2 2152 filename = data.get ();
d721ba37
PA
2153
2154 std::string abs_storage;
36586728
TT
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
14278e1f 2157 gdb::unique_xmalloc_ptr<char> abs
c3699833 2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2159
14278e1f 2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2161 filename = abs_storage.c_str ();
36586728
TT
2162 }
2163
dc294be5
TT
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
ad80db5b 2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2167 if (dwz_bfd != NULL)
36586728 2168 {
192b62ce 2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2170 dwz_bfd.reset (nullptr);
36586728
TT
2171 }
2172
dc294be5
TT
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
0d79cdc4
AM
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2179 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
ad80db5b 2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
dc294be5
TT
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2201 bfd_get_filename (per_bfd->obfd));
dc294be5 2202
7ff8cb8c
TT
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2205
7ff8cb8c
TT
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
36586728 2208
c3699833
SM
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
36586728 2212}
9291a0cd 2213\f
7b9f3c50
DE
2214/* DWARF quick_symbols_functions support. */
2215
2216/* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221struct quick_file_names
2222{
094b34ac
DE
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
7b9f3c50
DE
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236};
2237
2238/* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241struct dwarf2_per_cu_quick_data
2242{
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
5989a64e 2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2246 struct quick_file_names *file_names;
2247
7b9f3c50
DE
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255};
2256
094b34ac
DE
2257/* Utility hash function for a stmt_list_hash. */
2258
2259static hashval_t
2260hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261{
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2266 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2267 return v;
2268}
2269
2270/* Utility equality function for a stmt_list_hash. */
2271
2272static int
2273eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275{
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
9c541725 2282 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2283}
2284
7b9f3c50
DE
2285/* Hash function for a quick_file_names. */
2286
2287static hashval_t
2288hash_file_name_entry (const void *e)
2289{
9a3c8263
SM
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
7b9f3c50 2292
094b34ac 2293 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2294}
2295
2296/* Equality function for a quick_file_names. */
2297
2298static int
2299eq_file_name_entry (const void *a, const void *b)
2300{
9a3c8263
SM
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2303
094b34ac 2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2305}
2306
2307/* Delete function for a quick_file_names. */
2308
2309static void
2310delete_file_name_entry (void *e)
2311{
9a3c8263 2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
45940949
TT
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
7b9f3c50
DE
2324}
2325
2326/* Create a quick_file_names hash table. */
2327
5895093f 2328static htab_up
7b9f3c50
DE
2329create_quick_file_names_table (unsigned int nr_initial_entries)
2330{
5895093f
TT
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2334}
9291a0cd 2335
ab432490
SM
2336/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2339
2340static void
ab432490
SM
2341load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
918dd910 2343{
3019eac3 2344 if (per_cu->is_debug_types)
ab432490 2345 load_full_type_unit (per_cu, per_objfile);
918dd910 2346 else
ab432490 2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2348
cc12ce38
DE
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2dc860c0
DE
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2353}
2354
97a1449a 2355/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2356
9291a0cd 2357static void
97a1449a
SM
2358dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
9291a0cd 2361{
f4dc4d17
DE
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
197400e8 2364 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2365 return;
2366
b303c6f6
AB
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
39856def 2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2371
af758d11 2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2373 {
2374 queue_comp_unit (per_cu, language_minimal);
ab432490 2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
cc12ce38 2381 && per_cu->cu != NULL
89e63ee4 2382 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2385 /* DWP files aren't supported yet. */
ed2dc618 2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2387 queue_and_load_all_dwo_tus (per_cu);
95554aad 2388 }
9291a0cd 2389
ed2dc618 2390 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
ed2dc618 2394 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2395}
2396
97a1449a
SM
2397/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2fdf6df6 2401
43f3e411 2402static struct compunit_symtab *
97a1449a
SM
2403dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
9291a0cd 2406{
5989a64e 2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2410 {
11ed8cad 2411 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2412 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2414 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2415 }
f194fefb 2416
af758d11 2417 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2418}
2419
ff4c9fec 2420/* See declaration. */
f4dc4d17 2421
ff4c9fec 2422dwarf2_per_cu_data *
5989a64e 2423dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2424{
b76e467d 2425 if (index >= this->all_comp_units.size ())
ff4c9fec 2426 {
b76e467d 2427 index -= this->all_comp_units.size ();
b2bdb8cf 2428 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2429 return &this->all_type_units[index]->per_cu;
2430 }
f4dc4d17 2431
ff4c9fec
SM
2432 return this->all_comp_units[index];
2433}
f4dc4d17 2434
ff4c9fec 2435/* See declaration. */
2fdf6df6 2436
ff4c9fec 2437dwarf2_per_cu_data *
5989a64e 2438dwarf2_per_bfd::get_cu (int index)
1fd400ff 2439{
b76e467d 2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2441
ff4c9fec 2442 return this->all_comp_units[index];
f4dc4d17
DE
2443}
2444
ff4c9fec 2445/* See declaration. */
f4dc4d17 2446
ff4c9fec 2447signatured_type *
5989a64e 2448dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2449{
b2bdb8cf 2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_type_units[index];
1fd400ff
TT
2453}
2454
d3473f0c
TT
2455/* See read.h. */
2456
2457dwarf2_per_cu_data *
5989a64e 2458dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2459{
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2461 result->per_bfd = this;
d3473f0c
TT
2462 result->index = m_num_psymtabs++;
2463 return result;
2464}
2465
2466/* See read.h. */
2467
2468signatured_type *
5989a64e 2469dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2470{
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2472 result->per_cu.per_bfd = this;
d3473f0c
TT
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475}
2476
45940949
TT
2477/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
4b514bc8
JK
2479
2480static dwarf2_per_cu_data *
ed2dc618 2481create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485{
5989a64e 2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
e3b94546 2489 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2490 the_cu->section = section;
5989a64e 2491 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2492 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2493 the_cu->is_dwz = is_dwz;
2494 return the_cu;
2495}
2496
2ec9a5e0
TT
2497/* A helper for create_cus_from_index that handles a given list of
2498 CUs. */
2fdf6df6 2499
74a0d9f6 2500static void
12359b5e 2501create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2502 const gdb_byte *cu_list, offset_type n_elements,
2503 struct dwarf2_section_info *section,
b76e467d 2504 int is_dwz)
9291a0cd 2505{
12359b5e 2506 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2507 {
74a0d9f6 2508 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2509
2510 sect_offset sect_off
2511 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2512 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2513 cu_list += 2 * 8;
2514
b76e467d 2515 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2516 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2517 sect_off, length);
5989a64e 2518 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2519 }
9291a0cd
TT
2520}
2521
2ec9a5e0 2522/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2523 the CU objects for this objfile. */
2ec9a5e0 2524
74a0d9f6 2525static void
12359b5e 2526create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2527 const gdb_byte *cu_list, offset_type cu_list_elements,
2528 const gdb_byte *dwz_list, offset_type dwz_elements)
2529{
5989a64e
SM
2530 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2531 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2532 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2533
12359b5e 2534 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2535 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2536
2537 if (dwz_elements == 0)
74a0d9f6 2538 return;
2ec9a5e0 2539
c3699833 2540 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2541 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2542 &dwz->info, 1);
2ec9a5e0
TT
2543}
2544
1fd400ff 2545/* Create the signatured type hash table from the index. */
673bfd45 2546
74a0d9f6 2547static void
12359b5e
SM
2548create_signatured_type_table_from_index
2549 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2550 struct dwarf2_section_info *section,
2551 const gdb_byte *bytes,
2552 offset_type elements)
1fd400ff 2553{
5989a64e
SM
2554 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2555 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2556
298e9637 2557 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2558
12359b5e 2559 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2560 {
52dc124a 2561 struct signatured_type *sig_type;
9c541725 2562 ULONGEST signature;
1fd400ff 2563 void **slot;
9c541725 2564 cu_offset type_offset_in_tu;
1fd400ff 2565
74a0d9f6 2566 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2567 sect_offset sect_off
2568 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2569 type_offset_in_tu
2570 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2571 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2572 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2573 bytes += 3 * 8;
2574
5989a64e 2575 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2576 sig_type->signature = signature;
9c541725 2577 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2578 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2579 sig_type->per_cu.section = section;
9c541725 2580 sig_type->per_cu.sect_off = sect_off;
e3b94546 2581 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2582 sig_type->per_cu.v.quick
5989a64e 2583 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2584 struct dwarf2_per_cu_quick_data);
2585
b0b6a987 2586 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2587 *slot = sig_type;
1fd400ff 2588
5989a64e 2589 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2590 }
2591
5989a64e 2592 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2593}
2594
927aa2e7
JK
2595/* Create the signatured type hash table from .debug_names. */
2596
2597static void
2598create_signatured_type_table_from_debug_names
ed2dc618 2599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2600 const mapped_debug_names &map,
2601 struct dwarf2_section_info *section,
2602 struct dwarf2_section_info *abbrev_section)
2603{
ed2dc618
SM
2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
2605
96b79293
TT
2606 section->read (objfile);
2607 abbrev_section->read (objfile);
927aa2e7 2608
5989a64e
SM
2609 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2610 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2611
298e9637 2612 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2613
2614 for (uint32_t i = 0; i < map.tu_count; ++i)
2615 {
2616 struct signatured_type *sig_type;
927aa2e7 2617 void **slot;
927aa2e7
JK
2618
2619 sect_offset sect_off
2620 = (sect_offset) (extract_unsigned_integer
2621 (map.tu_table_reordered + i * map.offset_size,
2622 map.offset_size,
2623 map.dwarf5_byte_order));
2624
2625 comp_unit_head cu_header;
ed2dc618
SM
2626 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2627 abbrev_section,
927aa2e7
JK
2628 section->buffer + to_underlying (sect_off),
2629 rcuh_kind::TYPE);
2630
5989a64e 2631 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2632 sig_type->signature = cu_header.signature;
2633 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2634 sig_type->per_cu.is_debug_types = 1;
2635 sig_type->per_cu.section = section;
2636 sig_type->per_cu.sect_off = sect_off;
e3b94546 2637 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2638 sig_type->per_cu.v.quick
5989a64e 2639 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2640 struct dwarf2_per_cu_quick_data);
2641
b0b6a987 2642 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2643 *slot = sig_type;
2644
5989a64e 2645 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2646 }
2647
5989a64e 2648 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2649}
2650
9291a0cd
TT
2651/* Read the address map data from the mapped index, and use it to
2652 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2653
9291a0cd 2654static void
ed2dc618
SM
2655create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2656 struct mapped_index *index)
9291a0cd 2657{
ed2dc618 2658 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2659 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2660 const gdb_byte *iter, *end;
9291a0cd 2661 struct addrmap *mutable_map;
9291a0cd
TT
2662 CORE_ADDR baseaddr;
2663
8268c778
PA
2664 auto_obstack temp_obstack;
2665
9291a0cd
TT
2666 mutable_map = addrmap_create_mutable (&temp_obstack);
2667
f00a2de2
PA
2668 iter = index->address_table.data ();
2669 end = iter + index->address_table.size ();
9291a0cd 2670
b3b3bada 2671 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2672
2673 while (iter < end)
2674 {
2675 ULONGEST hi, lo, cu_index;
2676 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2677 iter += 8;
2678 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2679 iter += 8;
2680 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2681 iter += 4;
f652bce2 2682
24a55014 2683 if (lo > hi)
f652bce2 2684 {
b98664d3 2685 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2686 hex_string (lo), hex_string (hi));
24a55014 2687 continue;
f652bce2 2688 }
24a55014 2689
5989a64e 2690 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2691 {
b98664d3 2692 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2693 (unsigned) cu_index);
24a55014 2694 continue;
f652bce2 2695 }
24a55014 2696
79748972
TT
2697 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2698 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2699 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2700 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2701 }
2702
d320c2b5 2703 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2704 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2705}
2706
927aa2e7
JK
2707/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2708 populate the objfile's psymtabs_addrmap. */
2709
2710static void
ed2dc618 2711create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2712 struct dwarf2_section_info *section)
2713{
ed2dc618 2714 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2715 bfd *abfd = objfile->obfd;
08feed99 2716 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2717 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2718
2719 auto_obstack temp_obstack;
2720 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2721
2722 std::unordered_map<sect_offset,
2723 dwarf2_per_cu_data *,
2724 gdb::hash_enum<sect_offset>>
2725 debug_info_offset_to_per_cu;
5989a64e 2726 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2727 {
927aa2e7
JK
2728 const auto insertpair
2729 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2730 if (!insertpair.second)
2731 {
2732 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2733 "debug_info_offset %s, ignoring .debug_aranges."),
2734 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2735 return;
2736 }
2737 }
2738
96b79293 2739 section->read (objfile);
927aa2e7
JK
2740
2741 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2742
2743 const gdb_byte *addr = section->buffer;
2744
2745 while (addr < section->buffer + section->size)
2746 {
2747 const gdb_byte *const entry_addr = addr;
2748 unsigned int bytes_read;
2749
2750 const LONGEST entry_length = read_initial_length (abfd, addr,
2751 &bytes_read);
2752 addr += bytes_read;
2753
2754 const gdb_byte *const entry_end = addr + entry_length;
2755 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2756 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2757 if (addr + entry_length > section->buffer + section->size)
2758 {
47e3f474 2759 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2760 "length %s exceeds section length %s, "
2761 "ignoring .debug_aranges."),
47e3f474
TV
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
927aa2e7
JK
2764 plongest (bytes_read + entry_length),
2765 pulongest (section->size));
2766 return;
2767 }
2768
2769 /* The version number. */
2770 const uint16_t version = read_2_bytes (abfd, addr);
2771 addr += 2;
2772 if (version != 2)
2773 {
47e3f474 2774 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2775 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2776 objfile_name (objfile),
2777 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2778 return;
2779 }
2780
2781 const uint64_t debug_info_offset
2782 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2783 addr += offset_size;
2784 const auto per_cu_it
2785 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2786 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2787 {
47e3f474 2788 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2789 "debug_info_offset %s does not exists, "
2790 "ignoring .debug_aranges."),
47e3f474
TV
2791 objfile_name (objfile),
2792 plongest (entry_addr - section->buffer),
927aa2e7
JK
2793 pulongest (debug_info_offset));
2794 return;
2795 }
2796 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2797
2798 const uint8_t address_size = *addr++;
2799 if (address_size < 1 || address_size > 8)
2800 {
47e3f474 2801 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2802 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2803 objfile_name (objfile),
2804 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2805 return;
2806 }
2807
2808 const uint8_t segment_selector_size = *addr++;
2809 if (segment_selector_size != 0)
2810 {
47e3f474 2811 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2812 "segment_selector_size %u is not supported, "
2813 "ignoring .debug_aranges."),
47e3f474
TV
2814 objfile_name (objfile),
2815 plongest (entry_addr - section->buffer),
927aa2e7
JK
2816 segment_selector_size);
2817 return;
2818 }
2819
2820 /* Must pad to an alignment boundary that is twice the address
2821 size. It is undocumented by the DWARF standard but GCC does
2822 use it. */
2823 for (size_t padding = ((-(addr - section->buffer))
2824 & (2 * address_size - 1));
2825 padding > 0; padding--)
2826 if (*addr++ != 0)
2827 {
47e3f474 2828 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2829 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2830 objfile_name (objfile),
2831 plongest (entry_addr - section->buffer));
927aa2e7
JK
2832 return;
2833 }
2834
2835 for (;;)
2836 {
2837 if (addr + 2 * address_size > entry_end)
2838 {
47e3f474 2839 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2840 "address list is not properly terminated, "
2841 "ignoring .debug_aranges."),
47e3f474
TV
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer));
927aa2e7
JK
2844 return;
2845 }
2846 ULONGEST start = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 ULONGEST length = extract_unsigned_integer (addr, address_size,
2850 dwarf5_byte_order);
2851 addr += address_size;
2852 if (start == 0 && length == 0)
2853 break;
5989a64e 2854 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2855 {
2856 /* Symbol was eliminated due to a COMDAT group. */
2857 continue;
2858 }
2859 ULONGEST end = start + length;
79748972
TT
2860 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2861 - baseaddr);
2862 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2863 - baseaddr);
927aa2e7
JK
2864 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2865 }
2866 }
2867
d320c2b5 2868 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2869 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2870}
2871
9291a0cd
TT
2872/* Find a slot in the mapped index INDEX for the object named NAME.
2873 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2874 constant pool and return true. If NAME cannot be found, return
2875 false. */
2fdf6df6 2876
109483d9 2877static bool
9291a0cd
TT
2878find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2879 offset_type **vec_out)
2880{
0cf03b49 2881 offset_type hash;
9291a0cd 2882 offset_type slot, step;
559a7a62 2883 int (*cmp) (const char *, const char *);
9291a0cd 2884
791afaa2 2885 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2886 if (current_language->la_language == language_cplus
45280282
IB
2887 || current_language->la_language == language_fortran
2888 || current_language->la_language == language_d)
0cf03b49
JK
2889 {
2890 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2891 not contain any. */
a8719064 2892
72998fb3 2893 if (strchr (name, '(') != NULL)
0cf03b49 2894 {
109483d9 2895 without_params = cp_remove_params (name);
0cf03b49 2896
72998fb3 2897 if (without_params != NULL)
791afaa2 2898 name = without_params.get ();
0cf03b49
JK
2899 }
2900 }
2901
559a7a62 2902 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2903 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2904 simulate our NAME being searched is also lowercased. */
2905 hash = mapped_index_string_hash ((index->version == 4
2906 && case_sensitivity == case_sensitive_off
2907 ? 5 : index->version),
2908 name);
2909
f00a2de2
PA
2910 slot = hash & (index->symbol_table.size () - 1);
2911 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2912 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2913
2914 for (;;)
2915 {
9291a0cd 2916 const char *str;
f00a2de2
PA
2917
2918 const auto &bucket = index->symbol_table[slot];
2919 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2920 return false;
9291a0cd 2921
f00a2de2 2922 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2923 if (!cmp (name, str))
9291a0cd
TT
2924 {
2925 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2926 + MAYBE_SWAP (bucket.vec));
109483d9 2927 return true;
9291a0cd
TT
2928 }
2929
f00a2de2 2930 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2931 }
2932}
2933
4485a1c1
SM
2934/* A helper function that reads the .gdb_index from BUFFER and fills
2935 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2936 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2937 ok to use deprecated sections.
2938
2939 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2940 out parameters that are filled in with information about the CU and
2941 TU lists in the section.
2942
4485a1c1 2943 Returns true if all went well, false otherwise. */
2fdf6df6 2944
d33bc52e 2945static bool
3810f182 2946read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2947 bool deprecated_ok,
2948 gdb::array_view<const gdb_byte> buffer,
2949 struct mapped_index *map,
2950 const gdb_byte **cu_list,
2951 offset_type *cu_list_elements,
2952 const gdb_byte **types_list,
2953 offset_type *types_list_elements)
2954{
2955 const gdb_byte *addr = &buffer[0];
82430852 2956
9291a0cd 2957 /* Version check. */
4485a1c1 2958 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2959 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2960 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2961 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2962 indices. */
831adc1f 2963 if (version < 4)
481860b3
GB
2964 {
2965 static int warning_printed = 0;
2966 if (!warning_printed)
2967 {
2968 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2969 filename);
481860b3
GB
2970 warning_printed = 1;
2971 }
2972 return 0;
2973 }
2974 /* Index version 4 uses a different hash function than index version
2975 5 and later.
2976
2977 Versions earlier than 6 did not emit psymbols for inlined
2978 functions. Using these files will cause GDB not to be able to
2979 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2980 indices unless the user has done
2981 "set use-deprecated-index-sections on". */
2ec9a5e0 2982 if (version < 6 && !deprecated_ok)
481860b3
GB
2983 {
2984 static int warning_printed = 0;
2985 if (!warning_printed)
2986 {
e615022a
DE
2987 warning (_("\
2988Skipping deprecated .gdb_index section in %s.\n\
2989Do \"set use-deprecated-index-sections on\" before the file is read\n\
2990to use the section anyway."),
2ec9a5e0 2991 filename);
481860b3
GB
2992 warning_printed = 1;
2993 }
2994 return 0;
2995 }
796a7ff8 2996 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2997 of the TU (for symbols coming from TUs),
2998 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2999 Plus gold-generated indices can have duplicate entries for global symbols,
3000 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3001 These are just performance bugs, and we can't distinguish gdb-generated
3002 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3003
481860b3 3004 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3005 longer backward compatible. */
796a7ff8 3006 if (version > 8)
594e8718 3007 return 0;
9291a0cd 3008
559a7a62 3009 map->version = version;
9291a0cd 3010
4485a1c1 3011 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3012
4485a1c1 3013 int i = 0;
2ec9a5e0
TT
3014 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3015 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3016 / 8);
1fd400ff
TT
3017 ++i;
3018
2ec9a5e0
TT
3019 *types_list = addr + MAYBE_SWAP (metadata[i]);
3020 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3021 - MAYBE_SWAP (metadata[i]))
3022 / 8);
987d643c 3023 ++i;
1fd400ff 3024
f00a2de2
PA
3025 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3026 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3027 map->address_table
3028 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3029 ++i;
3030
f00a2de2
PA
3031 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3032 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3033 map->symbol_table
3034 = gdb::array_view<mapped_index::symbol_table_slot>
3035 ((mapped_index::symbol_table_slot *) symbol_table,
3036 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3037
f00a2de2 3038 ++i;
f9d83a0b 3039 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3040
2ec9a5e0
TT
3041 return 1;
3042}
3043
4485a1c1
SM
3044/* Callback types for dwarf2_read_gdb_index. */
3045
3046typedef gdb::function_view
5989a64e 3047 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3048 get_gdb_index_contents_ftype;
3049typedef gdb::function_view
3050 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3051 get_gdb_index_contents_dwz_ftype;
3052
927aa2e7 3053/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3054 elements of all the CUs and return 1. Otherwise, return 0. */
3055
3056static int
4485a1c1
SM
3057dwarf2_read_gdb_index
3058 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3059 get_gdb_index_contents_ftype get_gdb_index_contents,
3060 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3061{
2ec9a5e0
TT
3062 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3063 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3064 struct dwz_file *dwz;
12359b5e 3065 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3066
4485a1c1 3067 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3068 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3069
3070 if (main_index_contents.empty ())
3071 return 0;
3072
3063847f 3073 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3074 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3075 use_deprecated_index_sections,
3076 main_index_contents, map.get (), &cu_list,
3077 &cu_list_elements, &types_list,
3078 &types_list_elements))
2ec9a5e0
TT
3079 return 0;
3080
0fefef59 3081 /* Don't use the index if it's empty. */
3063847f 3082 if (map->symbol_table.empty ())
0fefef59
DE
3083 return 0;
3084
2ec9a5e0
TT
3085 /* If there is a .dwz file, read it so we can get its CU list as
3086 well. */
c3699833 3087 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3088 if (dwz != NULL)
2ec9a5e0 3089 {
2ec9a5e0
TT
3090 struct mapped_index dwz_map;
3091 const gdb_byte *dwz_types_ignore;
3092 offset_type dwz_types_elements_ignore;
3093
4485a1c1
SM
3094 gdb::array_view<const gdb_byte> dwz_index_content
3095 = get_gdb_index_contents_dwz (objfile, dwz);
3096
3097 if (dwz_index_content.empty ())
3098 return 0;
3099
3810f182 3100 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3101 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3102 &dwz_list, &dwz_list_elements,
3103 &dwz_types_ignore,
3104 &dwz_types_elements_ignore))
2ec9a5e0
TT
3105 {
3106 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3107 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3108 return 0;
3109 }
3110 }
3111
12359b5e
SM
3112 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3113 dwz_list, dwz_list_elements);
1fd400ff 3114
8b70b953
TT
3115 if (types_list_elements)
3116 {
8b70b953
TT
3117 /* We can only handle a single .debug_types when we have an
3118 index. */
5989a64e 3119 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3120 return 0;
3121
5989a64e 3122 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3123
12359b5e
SM
3124 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3125 types_list, types_list_elements);
8b70b953 3126 }
9291a0cd 3127
3063847f 3128 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3129
5989a64e
SM
3130 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3131 dwarf2_per_objfile->per_bfd->using_index = 1;
3132 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3133 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3134
3135 return 1;
3136}
3137
dee91e82 3138/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3139
dee91e82
DE
3140static void
3141dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3142 const gdb_byte *info_ptr,
3e225074 3143 struct die_info *comp_unit_die)
9291a0cd 3144{
dee91e82 3145 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3146 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3147 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3148 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3149 struct attribute *attr;
7b9f3c50
DE
3150 void **slot;
3151 struct quick_file_names *qfn;
9291a0cd 3152
0186c6a7
DE
3153 gdb_assert (! this_cu->is_debug_types);
3154
07261596
TT
3155 /* Our callers never want to match partial units -- instead they
3156 will match the enclosing full CU. */
3157 if (comp_unit_die->tag == DW_TAG_partial_unit)
3158 {
3159 this_cu->v.quick->no_file_data = 1;
3160 return;
3161 }
3162
0186c6a7 3163 lh_cu = this_cu;
7b9f3c50 3164 slot = NULL;
dee91e82 3165
fff8551c 3166 line_header_up lh;
9c541725 3167 sect_offset line_offset {};
fff8551c 3168
dee91e82 3169 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3170 if (attr != nullptr)
9291a0cd 3171 {
7b9f3c50
DE
3172 struct quick_file_names find_entry;
3173
9c541725 3174 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3175
3176 /* We may have already read in this line header (TU line header sharing).
3177 If we have we're done. */
094b34ac 3178 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3179 find_entry.hash.line_sect_off = line_offset;
5989a64e 3180 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3181 &find_entry, INSERT);
3182 if (*slot != NULL)
3183 {
9a3c8263 3184 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3185 return;
7b9f3c50
DE
3186 }
3187
3019eac3 3188 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3189 }
3190 if (lh == NULL)
3191 {
094b34ac 3192 lh_cu->v.quick->no_file_data = 1;
dee91e82 3193 return;
9291a0cd
TT
3194 }
3195
5989a64e 3196 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3197 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3198 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3199 gdb_assert (slot != NULL);
3200 *slot = qfn;
9291a0cd 3201
d721ba37 3202 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3203
aa391654
TT
3204 int offset = 0;
3205 if (strcmp (fnd.name, "<unknown>") != 0)
3206 ++offset;
3207
7ba99d21 3208 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3209 qfn->file_names =
5989a64e 3210 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3211 qfn->num_file_names);
aa391654
TT
3212 if (offset != 0)
3213 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3214 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3215 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3216 fnd.comp_dir).release ();
7b9f3c50 3217 qfn->real_names = NULL;
9291a0cd 3218
094b34ac 3219 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3220}
3221
3222/* A helper for the "quick" functions which attempts to read the line
3223 table for THIS_CU. */
3224
3225static struct quick_file_names *
ab432490
SM
3226dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3227 dwarf2_per_objfile *per_objfile)
dee91e82 3228{
0186c6a7
DE
3229 /* This should never be called for TUs. */
3230 gdb_assert (! this_cu->is_debug_types);
3231 /* Nor type unit groups. */
197400e8 3232 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3233
dee91e82
DE
3234 if (this_cu->v.quick->file_names != NULL)
3235 return this_cu->v.quick->file_names;
3236 /* If we know there is no line data, no point in looking again. */
3237 if (this_cu->v.quick->no_file_data)
3238 return NULL;
3239
ab432490 3240 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3241 if (!reader.dummy_p)
3e225074 3242 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3243
3244 if (this_cu->v.quick->no_file_data)
3245 return NULL;
3246 return this_cu->v.quick->file_names;
9291a0cd
TT
3247}
3248
3249/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3250 real path for a given file name from the line table. */
2fdf6df6 3251
9291a0cd 3252static const char *
45940949 3253dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3254 struct quick_file_names *qfn, int index)
9291a0cd 3255{
7b9f3c50 3256 if (qfn->real_names == NULL)
5989a64e 3257 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3258 qfn->num_file_names, const char *);
9291a0cd 3259
7b9f3c50 3260 if (qfn->real_names[index] == NULL)
14278e1f 3261 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3262
7b9f3c50 3263 return qfn->real_names[index];
9291a0cd
TT
3264}
3265
3266static struct symtab *
3267dw2_find_last_source_symtab (struct objfile *objfile)
3268{
ed2dc618
SM
3269 struct dwarf2_per_objfile *dwarf2_per_objfile
3270 = get_dwarf2_per_objfile (objfile);
5989a64e 3271 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3272 compunit_symtab *cust
3273 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3274
43f3e411
DE
3275 if (cust == NULL)
3276 return NULL;
ed2dc618 3277
43f3e411 3278 return compunit_primary_filetab (cust);
9291a0cd
TT
3279}
3280
7b9f3c50
DE
3281/* Traversal function for dw2_forget_cached_source_info. */
3282
3283static int
3284dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3285{
7b9f3c50 3286 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3287
7b9f3c50 3288 if (file_data->real_names)
9291a0cd 3289 {
7b9f3c50 3290 int i;
9291a0cd 3291
7b9f3c50 3292 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3293 {
7b9f3c50
DE
3294 xfree ((void*) file_data->real_names[i]);
3295 file_data->real_names[i] = NULL;
9291a0cd
TT
3296 }
3297 }
7b9f3c50
DE
3298
3299 return 1;
3300}
3301
3302static void
3303dw2_forget_cached_source_info (struct objfile *objfile)
3304{
ed2dc618
SM
3305 struct dwarf2_per_objfile *dwarf2_per_objfile
3306 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3307
5989a64e 3308 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3309 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3310}
3311
f8eba3c6
TT
3312/* Helper function for dw2_map_symtabs_matching_filename that expands
3313 the symtabs and calls the iterator. */
3314
3315static int
3316dw2_map_expand_apply (struct objfile *objfile,
3317 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3318 const char *name, const char *real_path,
14bc53a8 3319 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3320{
43f3e411 3321 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3322
3323 /* Don't visit already-expanded CUs. */
af758d11
SM
3324 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3325 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3326 return 0;
3327
3328 /* This may expand more than one symtab, and we want to iterate over
3329 all of them. */
97a1449a 3330 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3331
14bc53a8
PA
3332 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3333 last_made, callback);
f8eba3c6
TT
3334}
3335
3336/* Implementation of the map_symtabs_matching_filename method. */
3337
14bc53a8
PA
3338static bool
3339dw2_map_symtabs_matching_filename
3340 (struct objfile *objfile, const char *name, const char *real_path,
3341 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3342{
c011a4f4 3343 const char *name_basename = lbasename (name);
ed2dc618
SM
3344 struct dwarf2_per_objfile *dwarf2_per_objfile
3345 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3346
848e3e78
DE
3347 /* The rule is CUs specify all the files, including those used by
3348 any TU, so there's no need to scan TUs here. */
f4dc4d17 3349
5989a64e 3350 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3351 {
3d7bb9d9 3352 /* We only need to look at symtabs not already expanded. */
af758d11 3353 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3354 continue;
3355
ab432490
SM
3356 quick_file_names *file_data
3357 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3358 if (file_data == NULL)
9291a0cd
TT
3359 continue;
3360
b76e467d 3361 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3362 {
7b9f3c50 3363 const char *this_name = file_data->file_names[j];
da235a7c 3364 const char *this_real_name;
9291a0cd 3365
af529f8f 3366 if (compare_filenames_for_search (this_name, name))
9291a0cd 3367 {
f5b95b50 3368 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3369 callback))
3370 return true;
288e77a7 3371 continue;
4aac40c8 3372 }
9291a0cd 3373
c011a4f4
DE
3374 /* Before we invoke realpath, which can get expensive when many
3375 files are involved, do a quick comparison of the basenames. */
3376 if (! basenames_may_differ
3377 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3378 continue;
3379
45940949
TT
3380 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3381 file_data, j);
da235a7c 3382 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3383 {
da235a7c 3384 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3385 callback))
3386 return true;
288e77a7 3387 continue;
da235a7c 3388 }
9291a0cd 3389
da235a7c
JK
3390 if (real_path != NULL)
3391 {
af529f8f
JK
3392 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3393 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3394 if (this_real_name != NULL
af529f8f 3395 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3396 {
f5b95b50 3397 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3398 callback))
3399 return true;
288e77a7 3400 continue;
9291a0cd
TT
3401 }
3402 }
3403 }
3404 }
3405
14bc53a8 3406 return false;
9291a0cd
TT
3407}
3408
da51c347
DE
3409/* Struct used to manage iterating over all CUs looking for a symbol. */
3410
3411struct dw2_symtab_iterator
9291a0cd 3412{
ed2dc618
SM
3413 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3414 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3415 /* If set, only look for symbols that match that block. Valid values are
3416 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3417 gdb::optional<block_enum> block_index;
da51c347
DE
3418 /* The kind of symbol we're looking for. */
3419 domain_enum domain;
3420 /* The list of CUs from the index entry of the symbol,
3421 or NULL if not found. */
3422 offset_type *vec;
3423 /* The next element in VEC to look at. */
3424 int next;
3425 /* The number of elements in VEC, or zero if there is no match. */
3426 int length;
8943b874
DE
3427 /* Have we seen a global version of the symbol?
3428 If so we can ignore all further global instances.
3429 This is to work around gold/15646, inefficient gold-generated
3430 indices. */
3431 int global_seen;
da51c347 3432};
9291a0cd 3433
2b79f376 3434/* Initialize the index symtab iterator ITER. */
2fdf6df6 3435
9291a0cd 3436static void
da51c347 3437dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3438 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3439 gdb::optional<block_enum> block_index,
da51c347
DE
3440 domain_enum domain,
3441 const char *name)
3442{
ed2dc618 3443 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3444 iter->block_index = block_index;
3445 iter->domain = domain;
3446 iter->next = 0;
8943b874 3447 iter->global_seen = 0;
da51c347 3448
5989a64e 3449 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3450
3451 /* index is NULL if OBJF_READNOW. */
3452 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3453 iter->length = MAYBE_SWAP (*iter->vec);
3454 else
3455 {
3456 iter->vec = NULL;
3457 iter->length = 0;
3458 }
3459}
3460
3461/* Return the next matching CU or NULL if there are no more. */
3462
3463static struct dwarf2_per_cu_data *
3464dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3465{
ed2dc618
SM
3466 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3467
da51c347
DE
3468 for ( ; iter->next < iter->length; ++iter->next)
3469 {
3470 offset_type cu_index_and_attrs =
3471 MAYBE_SWAP (iter->vec[iter->next + 1]);
3472 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3473 gdb_index_symbol_kind symbol_kind =
3474 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3475 /* Only check the symbol attributes if they're present.
3476 Indices prior to version 7 don't record them,
3477 and indices >= 7 may elide them for certain symbols
3478 (gold does this). */
3479 int attrs_valid =
5989a64e 3480 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3481 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3482
3190f0c6 3483 /* Don't crash on bad data. */
5989a64e
SM
3484 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3485 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3486 {
b98664d3 3487 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3488 " [in module %s]"),
3489 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3490 continue;
3491 }
3492
5989a64e 3493 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3494
da51c347 3495 /* Skip if already read in. */
af758d11 3496 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3497 continue;
3498
8943b874
DE
3499 /* Check static vs global. */
3500 if (attrs_valid)
3501 {
2b79f376
SM
3502 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3503
3504 if (iter->block_index.has_value ())
3505 {
3506 bool want_static = *iter->block_index == STATIC_BLOCK;
3507
3508 if (is_static != want_static)
3509 continue;
3510 }
3511
8943b874
DE
3512 /* Work around gold/15646. */
3513 if (!is_static && iter->global_seen)
3514 continue;
3515 if (!is_static)
3516 iter->global_seen = 1;
3517 }
da51c347
DE
3518
3519 /* Only check the symbol's kind if it has one. */
3520 if (attrs_valid)
3521 {
3522 switch (iter->domain)
3523 {
3524 case VAR_DOMAIN:
3525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3526 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3527 /* Some types are also in VAR_DOMAIN. */
3528 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3529 continue;
3530 break;
3531 case STRUCT_DOMAIN:
3532 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3533 continue;
3534 break;
3535 case LABEL_DOMAIN:
3536 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3537 continue;
3538 break;
59c35742
AB
3539 case MODULE_DOMAIN:
3540 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3541 continue;
3542 break;
da51c347
DE
3543 default:
3544 break;
3545 }
3546 }
3547
3548 ++iter->next;
3549 return per_cu;
3550 }
3551
3552 return NULL;
3553}
3554
43f3e411 3555static struct compunit_symtab *
c7f839cb 3556dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3557 const char *name, domain_enum domain)
9291a0cd 3558{
43f3e411 3559 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3560 struct dwarf2_per_objfile *dwarf2_per_objfile
3561 = get_dwarf2_per_objfile (objfile);
9291a0cd 3562
b5ec771e
PA
3563 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3564
ed2dc618
SM
3565 struct dw2_symtab_iterator iter;
3566 struct dwarf2_per_cu_data *per_cu;
da51c347 3567
2b79f376 3568 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3569
ed2dc618
SM
3570 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3571 {
3572 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3573 struct compunit_symtab *stab
3574 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3575 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3576 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3577
ed2dc618
SM
3578 sym = block_find_symbol (block, name, domain,
3579 block_find_non_opaque_type_preferred,
3580 &with_opaque);
b2e2f908 3581
ed2dc618
SM
3582 /* Some caution must be observed with overloaded functions
3583 and methods, since the index will not contain any overload
3584 information (but NAME might contain it). */
da51c347 3585
ed2dc618
SM
3586 if (sym != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3588 return stab;
3589 if (with_opaque != NULL
3590 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3591 stab_best = stab;
da51c347 3592
ed2dc618 3593 /* Keep looking through other CUs. */
9291a0cd 3594 }
9291a0cd 3595
da51c347 3596 return stab_best;
9291a0cd
TT
3597}
3598
3599static void
3600dw2_print_stats (struct objfile *objfile)
3601{
ed2dc618
SM
3602 struct dwarf2_per_objfile *dwarf2_per_objfile
3603 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3604 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3605 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3606 int count = 0;
9291a0cd 3607
ed2dc618 3608 for (int i = 0; i < total; ++i)
9291a0cd 3609 {
5989a64e 3610 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3611
af758d11 3612 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3613 ++count;
3614 }
e4a48d9d 3615 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3616 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3617}
3618
779bd270
DE
3619/* This dumps minimal information about the index.
3620 It is called via "mt print objfiles".
3621 One use is to verify .gdb_index has been loaded by the
3622 gdb.dwarf2/gdb-index.exp testcase. */
3623
9291a0cd
TT
3624static void
3625dw2_dump (struct objfile *objfile)
3626{
ed2dc618
SM
3627 struct dwarf2_per_objfile *dwarf2_per_objfile
3628 = get_dwarf2_per_objfile (objfile);
3629
5989a64e 3630 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3631 printf_filtered (".gdb_index:");
5989a64e 3632 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3633 {
3634 printf_filtered (" version %d\n",
5989a64e 3635 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3636 }
3637 else
3638 printf_filtered (" faked for \"readnow\"\n");
3639 printf_filtered ("\n");
9291a0cd
TT
3640}
3641
9291a0cd
TT
3642static void
3643dw2_expand_symtabs_for_function (struct objfile *objfile,
3644 const char *func_name)
3645{
ed2dc618
SM
3646 struct dwarf2_per_objfile *dwarf2_per_objfile
3647 = get_dwarf2_per_objfile (objfile);
da51c347 3648
ed2dc618
SM
3649 struct dw2_symtab_iterator iter;
3650 struct dwarf2_per_cu_data *per_cu;
da51c347 3651
2b79f376 3652 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3653
ed2dc618 3654 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3655 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3656
9291a0cd
TT
3657}
3658
3659static void
3660dw2_expand_all_symtabs (struct objfile *objfile)
3661{
ed2dc618
SM
3662 struct dwarf2_per_objfile *dwarf2_per_objfile
3663 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3664 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3665 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3666
ed2dc618 3667 for (int i = 0; i < total_units; ++i)
9291a0cd 3668 {
5989a64e 3669 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3670
58f0c718
TT
3671 /* We don't want to directly expand a partial CU, because if we
3672 read it with the wrong language, then assertion failures can
3673 be triggered later on. See PR symtab/23010. So, tell
3674 dw2_instantiate_symtab to skip partial CUs -- any important
3675 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3676 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3677 }
3678}
3679
3680static void
652a8996
JK
3681dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3682 const char *fullname)
9291a0cd 3683{
ed2dc618
SM
3684 struct dwarf2_per_objfile *dwarf2_per_objfile
3685 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3686
3687 /* We don't need to consider type units here.
3688 This is only called for examining code, e.g. expand_line_sal.
3689 There can be an order of magnitude (or more) more type units
3690 than comp units, and we avoid them if we can. */
3691
5989a64e 3692 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3693 {
3d7bb9d9 3694 /* We only need to look at symtabs not already expanded. */
af758d11 3695 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3696 continue;
3697
ab432490
SM
3698 quick_file_names *file_data
3699 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3700 if (file_data == NULL)
9291a0cd
TT
3701 continue;
3702
b76e467d 3703 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3704 {
652a8996
JK
3705 const char *this_fullname = file_data->file_names[j];
3706
3707 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3708 {
97a1449a 3709 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3710 break;
3711 }
3712 }
3713 }
3714}
3715
9a0bacfb
TV
3716static void
3717dw2_expand_symtabs_matching_symbol
3718 (mapped_index_base &index,
3719 const lookup_name_info &lookup_name_in,
3720 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3721 enum search_domain kind,
3722 gdb::function_view<bool (offset_type)> match_callback);
3723
3724static void
3725dw2_expand_symtabs_matching_one
97a1449a
SM
3726 (dwarf2_per_cu_data *per_cu,
3727 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3728 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3729 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3730
9291a0cd 3731static void
199b4314
TT
3732dw2_map_matching_symbols
3733 (struct objfile *objfile,
b054970d 3734 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3735 int global,
3736 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3737 symbol_compare_ftype *ordered_compare)
9291a0cd 3738{
1aa98955
TV
3739 /* Used for Ada. */
3740 struct dwarf2_per_objfile *dwarf2_per_objfile
3741 = get_dwarf2_per_objfile (objfile);
3742
9a0bacfb
TV
3743 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3744
5989a64e 3745 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3746 {
3747 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3748 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3749 using GNU index. */
5989a64e 3750 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3751
9a0bacfb
TV
3752 const char *match_name = name.ada ().lookup_name ().c_str ();
3753 auto matcher = [&] (const char *symname)
3754 {
3755 if (ordered_compare == nullptr)
3756 return true;
3757 return ordered_compare (symname, match_name) == 0;
3758 };
3759
3760 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3761 [&] (offset_type namei)
3762 {
3763 struct dw2_symtab_iterator iter;
3764 struct dwarf2_per_cu_data *per_cu;
3765
3766 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3767 match_name);
3768 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3769 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3770 nullptr);
9a0bacfb
TV
3771 return true;
3772 });
3773 }
3774 else
3775 {
3776 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3777 proceed assuming all symtabs have been read in. */
3778 }
1aa98955
TV
3779
3780 for (compunit_symtab *cust : objfile->compunits ())
3781 {
3782 const struct block *block;
3783
3784 if (cust == NULL)
3785 continue;
3786 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3787 if (!iterate_over_symbols_terminated (block, name,
3788 domain, callback))
3789 return;
3790 }
9291a0cd
TT
3791}
3792
e1ef7d7a
PA
3793/* Starting from a search name, return the string that finds the upper
3794 bound of all strings that start with SEARCH_NAME in a sorted name
3795 list. Returns the empty string to indicate that the upper bound is
3796 the end of the list. */
3797
3798static std::string
3799make_sort_after_prefix_name (const char *search_name)
3800{
3801 /* When looking to complete "func", we find the upper bound of all
3802 symbols that start with "func" by looking for where we'd insert
3803 the closest string that would follow "func" in lexicographical
3804 order. Usually, that's "func"-with-last-character-incremented,
3805 i.e. "fund". Mind non-ASCII characters, though. Usually those
3806 will be UTF-8 multi-byte sequences, but we can't be certain.
3807 Especially mind the 0xff character, which is a valid character in
3808 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3809 rule out compilers allowing it in identifiers. Note that
3810 conveniently, strcmp/strcasecmp are specified to compare
3811 characters interpreted as unsigned char. So what we do is treat
3812 the whole string as a base 256 number composed of a sequence of
3813 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3814 to 0, and carries 1 to the following more-significant position.
3815 If the very first character in SEARCH_NAME ends up incremented
3816 and carries/overflows, then the upper bound is the end of the
3817 list. The string after the empty string is also the empty
3818 string.
3819
3820 Some examples of this operation:
3821
3822 SEARCH_NAME => "+1" RESULT
3823
3824 "abc" => "abd"
3825 "ab\xff" => "ac"
3826 "\xff" "a" "\xff" => "\xff" "b"
3827 "\xff" => ""
3828 "\xff\xff" => ""
3829 "" => ""
3830
3831 Then, with these symbols for example:
3832
3833 func
3834 func1
3835 fund
3836
3837 completing "func" looks for symbols between "func" and
3838 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3839 which finds "func" and "func1", but not "fund".
3840
3841 And with:
3842
3843 funcÿ (Latin1 'ÿ' [0xff])
3844 funcÿ1
3845 fund
3846
3847 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3848 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3849
3850 And with:
3851
3852 ÿÿ (Latin1 'ÿ' [0xff])
3853 ÿÿ1
3854
3855 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3856 the end of the list.
3857 */
3858 std::string after = search_name;
3859 while (!after.empty () && (unsigned char) after.back () == 0xff)
3860 after.pop_back ();
3861 if (!after.empty ())
3862 after.back () = (unsigned char) after.back () + 1;
3863 return after;
3864}
3865
5c58de74 3866/* See declaration. */
61d96d7e 3867
5c58de74
PA
3868std::pair<std::vector<name_component>::const_iterator,
3869 std::vector<name_component>::const_iterator>
44ed8f3e 3870mapped_index_base::find_name_components_bounds
3b00ef10 3871 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3872{
5c58de74
PA
3873 auto *name_cmp
3874 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3875
3b00ef10 3876 const char *lang_name
e0802d59 3877 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3878
3f563c84
PA
3879 /* Comparison function object for lower_bound that matches against a
3880 given symbol name. */
3881 auto lookup_compare_lower = [&] (const name_component &elem,
3882 const char *name)
3883 {
5c58de74 3884 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3885 const char *elem_name = elem_qualified + elem.name_offset;
3886 return name_cmp (elem_name, name) < 0;
3887 };
3888
3889 /* Comparison function object for upper_bound that matches against a
3890 given symbol name. */
3891 auto lookup_compare_upper = [&] (const char *name,
3892 const name_component &elem)
3893 {
5c58de74 3894 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3895 const char *elem_name = elem_qualified + elem.name_offset;
3896 return name_cmp (name, elem_name) < 0;
3897 };
3898
5c58de74
PA
3899 auto begin = this->name_components.begin ();
3900 auto end = this->name_components.end ();
3f563c84
PA
3901
3902 /* Find the lower bound. */
3903 auto lower = [&] ()
3904 {
3b00ef10 3905 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3906 return begin;
3907 else
3b00ef10 3908 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3909 } ();
3910
3911 /* Find the upper bound. */
3912 auto upper = [&] ()
3913 {
5c58de74 3914 if (lookup_name_without_params.completion_mode ())
3f563c84 3915 {
e1ef7d7a
PA
3916 /* In completion mode, we want UPPER to point past all
3917 symbols names that have the same prefix. I.e., with
3918 these symbols, and completing "func":
3919
3920 function << lower bound
3921 function1
3922 other_function << upper bound
3923
3924 We find the upper bound by looking for the insertion
3925 point of "func"-with-last-character-incremented,
3926 i.e. "fund". */
3b00ef10 3927 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3928 if (after.empty ())
3f563c84 3929 return end;
e6b2f5ef
PA
3930 return std::lower_bound (lower, end, after.c_str (),
3931 lookup_compare_lower);
3f563c84
PA
3932 }
3933 else
3b00ef10 3934 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3935 } ();
3936
5c58de74
PA
3937 return {lower, upper};
3938}
3939
3940/* See declaration. */
3941
3942void
44ed8f3e 3943mapped_index_base::build_name_components ()
5c58de74
PA
3944{
3945 if (!this->name_components.empty ())
3946 return;
3947
3948 this->name_components_casing = case_sensitivity;
3949 auto *name_cmp
3950 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3951
3952 /* The code below only knows how to break apart components of C++
3953 symbol names (and other languages that use '::' as
3b00ef10 3954 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3955 auto count = this->symbol_name_count ();
3956 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3957 {
44ed8f3e 3958 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3959 continue;
3960
3961 const char *name = this->symbol_name_at (idx);
3962
3963 /* Add each name component to the name component table. */
3964 unsigned int previous_len = 0;
3b00ef10
TT
3965
3966 if (strstr (name, "::") != nullptr)
3967 {
3968 for (unsigned int current_len = cp_find_first_component (name);
3969 name[current_len] != '\0';
3970 current_len += cp_find_first_component (name + current_len))
3971 {
3972 gdb_assert (name[current_len] == ':');
3973 this->name_components.push_back ({previous_len, idx});
3974 /* Skip the '::'. */
3975 current_len += 2;
3976 previous_len = current_len;
3977 }
3978 }
3979 else
5c58de74 3980 {
3b00ef10
TT
3981 /* Handle the Ada encoded (aka mangled) form here. */
3982 for (const char *iter = strstr (name, "__");
3983 iter != nullptr;
3984 iter = strstr (iter, "__"))
3985 {
3986 this->name_components.push_back ({previous_len, idx});
3987 iter += 2;
3988 previous_len = iter - name;
3989 }
5c58de74 3990 }
3b00ef10 3991
5c58de74
PA
3992 this->name_components.push_back ({previous_len, idx});
3993 }
3994
3995 /* Sort name_components elements by name. */
3996 auto name_comp_compare = [&] (const name_component &left,
3997 const name_component &right)
3998 {
3999 const char *left_qualified = this->symbol_name_at (left.idx);
4000 const char *right_qualified = this->symbol_name_at (right.idx);
4001
4002 const char *left_name = left_qualified + left.name_offset;
4003 const char *right_name = right_qualified + right.name_offset;
4004
4005 return name_cmp (left_name, right_name) < 0;
4006 };
4007
4008 std::sort (this->name_components.begin (),
4009 this->name_components.end (),
4010 name_comp_compare);
4011}
4012
4013/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4014 mapped_index_base instead of the containing objfile. This is split
4015 to a separate function in order to be able to unit test the
4016 name_components matching using a mock mapped_index_base. For each
5c58de74 4017 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4018 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4019
4020static void
4021dw2_expand_symtabs_matching_symbol
44ed8f3e 4022 (mapped_index_base &index,
5c58de74
PA
4023 const lookup_name_info &lookup_name_in,
4024 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4025 enum search_domain kind,
3b00ef10 4026 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4027{
4028 lookup_name_info lookup_name_without_params
4029 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4030
4031 /* Build the symbol name component sorted vector, if we haven't
4032 yet. */
4033 index.build_name_components ();
4034
3f563c84
PA
4035 /* The same symbol may appear more than once in the range though.
4036 E.g., if we're looking for symbols that complete "w", and we have
4037 a symbol named "w1::w2", we'll find the two name components for
4038 that same symbol in the range. To be sure we only call the
4039 callback once per symbol, we first collect the symbol name
4040 indexes that matched in a temporary vector and ignore
4041 duplicates. */
4042 std::vector<offset_type> matches;
3f563c84 4043
3b00ef10
TT
4044 struct name_and_matcher
4045 {
4046 symbol_name_matcher_ftype *matcher;
ecc6c606 4047 const char *name;
3b00ef10
TT
4048
4049 bool operator== (const name_and_matcher &other) const
3f563c84 4050 {
ecc6c606 4051 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4052 }
4053 };
4054
4055 /* A vector holding all the different symbol name matchers, for all
4056 languages. */
4057 std::vector<name_and_matcher> matchers;
4058
4059 for (int i = 0; i < nr_languages; i++)
4060 {
4061 enum language lang_e = (enum language) i;
4062
4063 const language_defn *lang = language_def (lang_e);
4064 symbol_name_matcher_ftype *name_matcher
4065 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4066
3b00ef10
TT
4067 name_and_matcher key {
4068 name_matcher,
4069 lookup_name_without_params.language_lookup_name (lang_e)
4070 };
4071
4072 /* Don't insert the same comparison routine more than once.
4073 Note that we do this linear walk. This is not a problem in
4074 practice because the number of supported languages is
4075 low. */
4076 if (std::find (matchers.begin (), matchers.end (), key)
4077 != matchers.end ())
9291a0cd 4078 continue;
3b00ef10
TT
4079 matchers.push_back (std::move (key));
4080
4081 auto bounds
4082 = index.find_name_components_bounds (lookup_name_without_params,
4083 lang_e);
4084
4085 /* Now for each symbol name in range, check to see if we have a name
4086 match, and if so, call the MATCH_CALLBACK callback. */
4087
4088 for (; bounds.first != bounds.second; ++bounds.first)
4089 {
4090 const char *qualified = index.symbol_name_at (bounds.first->idx);
4091
4092 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4093 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4094 continue;
9291a0cd 4095
3b00ef10
TT
4096 matches.push_back (bounds.first->idx);
4097 }
3f563c84
PA
4098 }
4099
4100 std::sort (matches.begin (), matches.end ());
4101
4102 /* Finally call the callback, once per match. */
4103 ULONGEST prev = -1;
4104 for (offset_type idx : matches)
4105 {
4106 if (prev != idx)
4107 {
3b00ef10
TT
4108 if (!match_callback (idx))
4109 break;
3f563c84
PA
4110 prev = idx;
4111 }
4112 }
4113
4114 /* Above we use a type wider than idx's for 'prev', since 0 and
4115 (offset_type)-1 are both possible values. */
4116 static_assert (sizeof (prev) > sizeof (offset_type), "");
4117}
4118
c62446b1
PA
4119#if GDB_SELF_TEST
4120
4121namespace selftests { namespace dw2_expand_symtabs_matching {
4122
a3c5fafd
PA
4123/* A mock .gdb_index/.debug_names-like name index table, enough to
4124 exercise dw2_expand_symtabs_matching_symbol, which works with the
4125 mapped_index_base interface. Builds an index from the symbol list
4126 passed as parameter to the constructor. */
4127class mock_mapped_index : public mapped_index_base
c62446b1
PA
4128{
4129public:
a3c5fafd
PA
4130 mock_mapped_index (gdb::array_view<const char *> symbols)
4131 : m_symbol_table (symbols)
c62446b1
PA
4132 {}
4133
a3c5fafd 4134 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4135
a3c5fafd 4136 /* Return the number of names in the symbol table. */
632e107b 4137 size_t symbol_name_count () const override
c62446b1 4138 {
a3c5fafd 4139 return m_symbol_table.size ();
c62446b1
PA
4140 }
4141
a3c5fafd 4142 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4143 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4144 {
4145 return m_symbol_table[idx];
4146 }
c62446b1 4147
a3c5fafd
PA
4148private:
4149 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4150};
4151
4152/* Convenience function that converts a NULL pointer to a "<null>"
4153 string, to pass to print routines. */
4154
4155static const char *
4156string_or_null (const char *str)
4157{
4158 return str != NULL ? str : "<null>";
4159}
4160
4161/* Check if a lookup_name_info built from
4162 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4163 index. EXPECTED_LIST is the list of expected matches, in expected
4164 matching order. If no match expected, then an empty list is
4165 specified. Returns true on success. On failure prints a warning
4166 indicating the file:line that failed, and returns false. */
4167
4168static bool
4169check_match (const char *file, int line,
4170 mock_mapped_index &mock_index,
4171 const char *name, symbol_name_match_type match_type,
4172 bool completion_mode,
4173 std::initializer_list<const char *> expected_list)
4174{
4175 lookup_name_info lookup_name (name, match_type, completion_mode);
4176
4177 bool matched = true;
4178
4179 auto mismatch = [&] (const char *expected_str,
4180 const char *got)
4181 {
4182 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4183 "expected=\"%s\", got=\"%s\"\n"),
4184 file, line,
4185 (match_type == symbol_name_match_type::FULL
4186 ? "FULL" : "WILD"),
4187 name, string_or_null (expected_str), string_or_null (got));
4188 matched = false;
4189 };
4190
4191 auto expected_it = expected_list.begin ();
4192 auto expected_end = expected_list.end ();
4193
a3c5fafd 4194 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4195 NULL, ALL_DOMAIN,
4196 [&] (offset_type idx)
4197 {
a3c5fafd 4198 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4199 const char *expected_str
4200 = expected_it == expected_end ? NULL : *expected_it++;
4201
4202 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4203 mismatch (expected_str, matched_name);
3b00ef10 4204 return true;
c62446b1
PA
4205 });
4206
4207 const char *expected_str
4208 = expected_it == expected_end ? NULL : *expected_it++;
4209 if (expected_str != NULL)
4210 mismatch (expected_str, NULL);
4211
4212 return matched;
4213}
4214
4215/* The symbols added to the mock mapped_index for testing (in
4216 canonical form). */
4217static const char *test_symbols[] = {
4218 "function",
4219 "std::bar",
4220 "std::zfunction",
4221 "std::zfunction2",
4222 "w1::w2",
4223 "ns::foo<char*>",
4224 "ns::foo<int>",
4225 "ns::foo<long>",
a20714ff
PA
4226 "ns2::tmpl<int>::foo2",
4227 "(anonymous namespace)::A::B::C",
c62446b1 4228
e1ef7d7a
PA
4229 /* These are used to check that the increment-last-char in the
4230 matching algorithm for completion doesn't match "t1_fund" when
4231 completing "t1_func". */
4232 "t1_func",
4233 "t1_func1",
4234 "t1_fund",
4235 "t1_fund1",
4236
4237 /* A UTF-8 name with multi-byte sequences to make sure that
4238 cp-name-parser understands this as a single identifier ("função"
4239 is "function" in PT). */
4240 u8"u8função",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "yfunc\377",
4244
4245 /* \377 (0xff) is Latin1 'ÿ'. */
4246 "\377",
4247 "\377\377123",
4248
c62446b1
PA
4249 /* A name with all sorts of complications. Starts with "z" to make
4250 it easier for the completion tests below. */
4251#define Z_SYM_NAME \
4252 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4253 "::tuple<(anonymous namespace)::ui*, " \
4254 "std::default_delete<(anonymous namespace)::ui>, void>"
4255
4256 Z_SYM_NAME
4257};
4258
a3c5fafd
PA
4259/* Returns true if the mapped_index_base::find_name_component_bounds
4260 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4261 in completion mode. */
5c58de74
PA
4262
4263static bool
a3c5fafd 4264check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4265 const char *search_name,
4266 gdb::array_view<const char *> expected_syms)
4267{
4268 lookup_name_info lookup_name (search_name,
4269 symbol_name_match_type::FULL, true);
4270
3b00ef10
TT
4271 auto bounds = index.find_name_components_bounds (lookup_name,
4272 language_cplus);
5c58de74
PA
4273
4274 size_t distance = std::distance (bounds.first, bounds.second);
4275 if (distance != expected_syms.size ())
4276 return false;
4277
4278 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4279 {
4280 auto nc_elem = bounds.first + exp_elem;
4281 const char *qualified = index.symbol_name_at (nc_elem->idx);
4282 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4283 return false;
4284 }
4285
4286 return true;
4287}
4288
4289/* Test the lower-level mapped_index::find_name_component_bounds
4290 method. */
4291
c62446b1 4292static void
5c58de74
PA
4293test_mapped_index_find_name_component_bounds ()
4294{
4295 mock_mapped_index mock_index (test_symbols);
4296
a3c5fafd 4297 mock_index.build_name_components ();
5c58de74
PA
4298
4299 /* Test the lower-level mapped_index::find_name_component_bounds
4300 method in completion mode. */
4301 {
4302 static const char *expected_syms[] = {
4303 "t1_func",
4304 "t1_func1",
5c58de74
PA
4305 };
4306
a3c5fafd 4307 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4308 "t1_func", expected_syms));
4309 }
4310
4311 /* Check that the increment-last-char in the name matching algorithm
4312 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4313 {
4314 static const char *expected_syms1[] = {
4315 "\377",
4316 "\377\377123",
4317 };
a3c5fafd 4318 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4319 "\377", expected_syms1));
4320
4321 static const char *expected_syms2[] = {
4322 "\377\377123",
4323 };
a3c5fafd 4324 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4325 "\377\377", expected_syms2));
4326 }
4327}
4328
4329/* Test dw2_expand_symtabs_matching_symbol. */
4330
4331static void
4332test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4333{
4334 mock_mapped_index mock_index (test_symbols);
4335
4336 /* We let all tests run until the end even if some fails, for debug
4337 convenience. */
4338 bool any_mismatch = false;
4339
4340 /* Create the expected symbols list (an initializer_list). Needed
4341 because lists have commas, and we need to pass them to CHECK,
4342 which is a macro. */
4343#define EXPECT(...) { __VA_ARGS__ }
4344
4345 /* Wrapper for check_match that passes down the current
4346 __FILE__/__LINE__. */
4347#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4348 any_mismatch |= !check_match (__FILE__, __LINE__, \
4349 mock_index, \
4350 NAME, MATCH_TYPE, COMPLETION_MODE, \
4351 EXPECTED_LIST)
4352
4353 /* Identity checks. */
4354 for (const char *sym : test_symbols)
4355 {
4356 /* Should be able to match all existing symbols. */
4357 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4358 EXPECT (sym));
4359
4360 /* Should be able to match all existing symbols with
4361 parameters. */
4362 std::string with_params = std::string (sym) + "(int)";
4363 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4364 EXPECT (sym));
4365
4366 /* Should be able to match all existing symbols with
4367 parameters and qualifiers. */
4368 with_params = std::string (sym) + " ( int ) const";
4369 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4370 EXPECT (sym));
4371
4372 /* This should really find sym, but cp-name-parser.y doesn't
4373 know about lvalue/rvalue qualifiers yet. */
4374 with_params = std::string (sym) + " ( int ) &&";
4375 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4376 {});
4377 }
4378
e1ef7d7a
PA
4379 /* Check that the name matching algorithm for completion doesn't get
4380 confused with Latin1 'ÿ' / 0xff. */
4381 {
4382 static const char str[] = "\377";
4383 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4384 EXPECT ("\377", "\377\377123"));
4385 }
4386
4387 /* Check that the increment-last-char in the matching algorithm for
4388 completion doesn't match "t1_fund" when completing "t1_func". */
4389 {
4390 static const char str[] = "t1_func";
4391 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4392 EXPECT ("t1_func", "t1_func1"));
4393 }
4394
c62446b1
PA
4395 /* Check that completion mode works at each prefix of the expected
4396 symbol name. */
4397 {
4398 static const char str[] = "function(int)";
4399 size_t len = strlen (str);
4400 std::string lookup;
4401
4402 for (size_t i = 1; i < len; i++)
4403 {
4404 lookup.assign (str, i);
4405 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4406 EXPECT ("function"));
4407 }
4408 }
4409
4410 /* While "w" is a prefix of both components, the match function
4411 should still only be called once. */
4412 {
4413 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4414 EXPECT ("w1::w2"));
a20714ff
PA
4415 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4416 EXPECT ("w1::w2"));
c62446b1
PA
4417 }
4418
4419 /* Same, with a "complicated" symbol. */
4420 {
4421 static const char str[] = Z_SYM_NAME;
4422 size_t len = strlen (str);
4423 std::string lookup;
4424
4425 for (size_t i = 1; i < len; i++)
4426 {
4427 lookup.assign (str, i);
4428 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4429 EXPECT (Z_SYM_NAME));
4430 }
4431 }
4432
4433 /* In FULL mode, an incomplete symbol doesn't match. */
4434 {
4435 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4436 {});
4437 }
4438
4439 /* A complete symbol with parameters matches any overload, since the
4440 index has no overload info. */
4441 {
4442 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4443 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4444 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4445 EXPECT ("std::zfunction", "std::zfunction2"));
4446 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4447 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4448 }
4449
4450 /* Check that whitespace is ignored appropriately. A symbol with a
4451 template argument list. */
4452 {
4453 static const char expected[] = "ns::foo<int>";
4454 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4455 EXPECT (expected));
a20714ff
PA
4456 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4457 EXPECT (expected));
c62446b1
PA
4458 }
4459
4460 /* Check that whitespace is ignored appropriately. A symbol with a
4461 template argument list that includes a pointer. */
4462 {
4463 static const char expected[] = "ns::foo<char*>";
4464 /* Try both completion and non-completion modes. */
4465 static const bool completion_mode[2] = {false, true};
4466 for (size_t i = 0; i < 2; i++)
4467 {
4468 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4469 completion_mode[i], EXPECT (expected));
a20714ff
PA
4470 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4471 completion_mode[i], EXPECT (expected));
c62446b1
PA
4472
4473 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4474 completion_mode[i], EXPECT (expected));
a20714ff
PA
4475 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4476 completion_mode[i], EXPECT (expected));
c62446b1
PA
4477 }
4478 }
4479
4480 {
4481 /* Check method qualifiers are ignored. */
4482 static const char expected[] = "ns::foo<char*>";
4483 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4484 symbol_name_match_type::FULL, true, EXPECT (expected));
4485 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4486 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4487 CHECK_MATCH ("foo < char * > ( int ) const",
4488 symbol_name_match_type::WILD, true, EXPECT (expected));
4489 CHECK_MATCH ("foo < char * > ( int ) &&",
4490 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4491 }
4492
4493 /* Test lookup names that don't match anything. */
4494 {
a20714ff
PA
4495 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4496 {});
4497
c62446b1
PA
4498 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4499 {});
4500 }
4501
a20714ff
PA
4502 /* Some wild matching tests, exercising "(anonymous namespace)",
4503 which should not be confused with a parameter list. */
4504 {
4505 static const char *syms[] = {
4506 "A::B::C",
4507 "B::C",
4508 "C",
4509 "A :: B :: C ( int )",
4510 "B :: C ( int )",
4511 "C ( int )",
4512 };
4513
4514 for (const char *s : syms)
4515 {
4516 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4517 EXPECT ("(anonymous namespace)::A::B::C"));
4518 }
4519 }
4520
4521 {
4522 static const char expected[] = "ns2::tmpl<int>::foo2";
4523 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4524 EXPECT (expected));
4525 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4526 EXPECT (expected));
4527 }
4528
c62446b1
PA
4529 SELF_CHECK (!any_mismatch);
4530
4531#undef EXPECT
4532#undef CHECK_MATCH
4533}
4534
5c58de74
PA
4535static void
4536run_test ()
4537{
4538 test_mapped_index_find_name_component_bounds ();
4539 test_dw2_expand_symtabs_matching_symbol ();
4540}
4541
c62446b1
PA
4542}} // namespace selftests::dw2_expand_symtabs_matching
4543
4544#endif /* GDB_SELF_TEST */
4545
4b514bc8
JK
4546/* If FILE_MATCHER is NULL or if PER_CU has
4547 dwarf2_per_cu_quick_data::MARK set (see
4548 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4549 EXPANSION_NOTIFY on it. */
4550
4551static void
4552dw2_expand_symtabs_matching_one
97a1449a
SM
4553 (dwarf2_per_cu_data *per_cu,
4554 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4555 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4556 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4557{
4558 if (file_matcher == NULL || per_cu->v.quick->mark)
4559 {
af758d11 4560 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4561
97a1449a
SM
4562 compunit_symtab *symtab
4563 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4564 gdb_assert (symtab != nullptr);
4b514bc8 4565
af758d11
SM
4566 if (expansion_notify != NULL && symtab_was_null)
4567 expansion_notify (symtab);
4b514bc8
JK
4568 }
4569}
4570
3f563c84
PA
4571/* Helper for dw2_expand_matching symtabs. Called on each symbol
4572 matched, to expand corresponding CUs that were marked. IDX is the
4573 index of the symbol name that matched. */
4574
4575static void
4576dw2_expand_marked_cus
97a1449a 4577 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4579 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4580 search_domain kind)
4581{
3f563c84
PA
4582 offset_type *vec, vec_len, vec_idx;
4583 bool global_seen = false;
5989a64e 4584 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4585
61920122 4586 vec = (offset_type *) (index.constant_pool
f00a2de2 4587 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4588 vec_len = MAYBE_SWAP (vec[0]);
4589 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4590 {
61920122
PA
4591 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4592 /* This value is only valid for index versions >= 7. */
4593 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4594 gdb_index_symbol_kind symbol_kind =
4595 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4596 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4597 /* Only check the symbol attributes if they're present.
4598 Indices prior to version 7 don't record them,
4599 and indices >= 7 may elide them for certain symbols
4600 (gold does this). */
4601 int attrs_valid =
4602 (index.version >= 7
4603 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4604
4605 /* Work around gold/15646. */
4606 if (attrs_valid)
9291a0cd 4607 {
61920122
PA
4608 if (!is_static && global_seen)
4609 continue;
4610 if (!is_static)
4611 global_seen = true;
4612 }
3190f0c6 4613
61920122
PA
4614 /* Only check the symbol's kind if it has one. */
4615 if (attrs_valid)
4616 {
4617 switch (kind)
8943b874 4618 {
61920122
PA
4619 case VARIABLES_DOMAIN:
4620 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4621 continue;
4622 break;
4623 case FUNCTIONS_DOMAIN:
4624 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4625 continue;
61920122
PA
4626 break;
4627 case TYPES_DOMAIN:
4628 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4629 continue;
4630 break;
59c35742
AB
4631 case MODULES_DOMAIN:
4632 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4633 continue;
4634 break;
61920122
PA
4635 default:
4636 break;
8943b874 4637 }
61920122 4638 }
8943b874 4639
61920122 4640 /* Don't crash on bad data. */
5989a64e
SM
4641 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4642 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4643 {
b98664d3 4644 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4645 " [in module %s]"),
4646 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4647 continue;
4648 }
4649
5989a64e 4650 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4651 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4652 expansion_notify);
61920122
PA
4653 }
4654}
4655
4b514bc8
JK
4656/* If FILE_MATCHER is non-NULL, set all the
4657 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4658 that match FILE_MATCHER. */
4659
61920122 4660static void
4b514bc8 4661dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4662 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4663 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4664{
4b514bc8 4665 if (file_matcher == NULL)
61920122
PA
4666 return;
4667
4b514bc8
JK
4668 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4672 htab_eq_pointer,
4673 NULL, xcalloc, xfree));
61920122 4674
4b514bc8
JK
4675 /* The rule is CUs specify all the files, including those used by
4676 any TU, so there's no need to scan TUs here. */
61920122 4677
5989a64e 4678 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4679 {
927aa2e7
JK
4680 QUIT;
4681
4682 per_cu->v.quick->mark = 0;
4683
4684 /* We only need to look at symtabs not already expanded. */
af758d11 4685 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4686 continue;
4687
ab432490
SM
4688 quick_file_names *file_data
4689 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4690 if (file_data == NULL)
4691 continue;
4692
4693 if (htab_find (visited_not_found.get (), file_data) != NULL)
4694 continue;
4695 else if (htab_find (visited_found.get (), file_data) != NULL)
4696 {
4697 per_cu->v.quick->mark = 1;
4698 continue;
4699 }
4700
b76e467d 4701 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4702 {
4703 const char *this_real_name;
4704
4705 if (file_matcher (file_data->file_names[j], false))
4706 {
4707 per_cu->v.quick->mark = 1;
4708 break;
4709 }
4710
4711 /* Before we invoke realpath, which can get expensive when many
4712 files are involved, do a quick comparison of the basenames. */
4713 if (!basenames_may_differ
4714 && !file_matcher (lbasename (file_data->file_names[j]),
4715 true))
4716 continue;
4717
45940949
TT
4718 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4719 file_data, j);
927aa2e7
JK
4720 if (file_matcher (this_real_name, false))
4721 {
4722 per_cu->v.quick->mark = 1;
4723 break;
4724 }
4725 }
4726
b76e467d
SM
4727 void **slot = htab_find_slot (per_cu->v.quick->mark
4728 ? visited_found.get ()
4729 : visited_not_found.get (),
4730 file_data, INSERT);
927aa2e7
JK
4731 *slot = file_data;
4732 }
4733}
4734
4735static void
4736dw2_expand_symtabs_matching
4737 (struct objfile *objfile,
4738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4739 const lookup_name_info *lookup_name,
927aa2e7
JK
4740 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4741 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4742 enum search_domain kind)
4743{
ed2dc618
SM
4744 struct dwarf2_per_objfile *dwarf2_per_objfile
4745 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4746
4747 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4748 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4749 return;
4750
ed2dc618 4751 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4752
c1a66c06
TV
4753 if (symbol_matcher == NULL && lookup_name == NULL)
4754 {
5989a64e 4755 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4756 {
4757 QUIT;
4758
97a1449a
SM
4759 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4760 file_matcher, expansion_notify);
c1a66c06
TV
4761 }
4762 return;
4763 }
4764
5989a64e 4765 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4766
c1a66c06 4767 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4768 symbol_matcher,
4769 kind, [&] (offset_type idx)
4770 {
ed2dc618 4771 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4772 expansion_notify, kind);
3b00ef10 4773 return true;
927aa2e7
JK
4774 });
4775}
4776
4777/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4778 symtab. */
4779
4780static struct compunit_symtab *
4781recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4782 CORE_ADDR pc)
4783{
4784 int i;
4785
4786 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4787 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4788 return cust;
4789
4790 if (cust->includes == NULL)
4791 return NULL;
4792
4793 for (i = 0; cust->includes[i]; ++i)
4794 {
4795 struct compunit_symtab *s = cust->includes[i];
4796
4797 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4798 if (s != NULL)
4799 return s;
4800 }
4801
4802 return NULL;
4803}
4804
4805static struct compunit_symtab *
4806dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4807 struct bound_minimal_symbol msymbol,
4808 CORE_ADDR pc,
4809 struct obj_section *section,
4810 int warn_if_readin)
4811{
4812 struct dwarf2_per_cu_data *data;
4813 struct compunit_symtab *result;
4814
d320c2b5 4815 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4816 return NULL;
4817
b3b3bada 4818 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4819 data = (struct dwarf2_per_cu_data *) addrmap_find
4820 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4821 if (!data)
4822 return NULL;
4823
af758d11
SM
4824 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4825 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4826 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4827 paddress (objfile->arch (), pc));
927aa2e7 4828
97a1449a
SM
4829 result = recursively_find_pc_sect_compunit_symtab
4830 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4831
927aa2e7
JK
4832 gdb_assert (result != NULL);
4833 return result;
4834}
4835
4836static void
4837dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4838 void *data, int need_fullname)
4839{
ed2dc618
SM
4840 struct dwarf2_per_objfile *dwarf2_per_objfile
4841 = get_dwarf2_per_objfile (objfile);
927aa2e7 4842
5989a64e 4843 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4844 {
5989a64e 4845 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4846
4847 htab_up visited (htab_create_alloc (10,
4848 htab_hash_pointer, htab_eq_pointer,
4849 NULL, xcalloc, xfree));
4850
4851 /* The rule is CUs specify all the files, including those used
4852 by any TU, so there's no need to scan TUs here. We can
4853 ignore file names coming from already-expanded CUs. */
4854
5989a64e 4855 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4856 {
af758d11 4857 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4858 {
4859 void **slot = htab_find_slot (visited.get (),
4860 per_cu->v.quick->file_names,
4861 INSERT);
4862
4863 *slot = per_cu->v.quick->file_names;
4864 }
4865 }
4866
5989a64e 4867 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4868 {
927aa2e7 4869 /* We only need to look at symtabs not already expanded. */
af758d11 4870 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4871 continue;
4872
ab432490
SM
4873 quick_file_names *file_data
4874 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4875 if (file_data == NULL)
4876 continue;
4877
b76e467d 4878 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4879 if (*slot)
4880 {
4881 /* Already visited. */
4882 continue;
4883 }
4884 *slot = file_data;
4885
4886 for (int j = 0; j < file_data->num_file_names; ++j)
4887 {
4888 const char *filename = file_data->file_names[j];
5989a64e 4889 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4890 }
4891 }
4892 }
4893
5989a64e 4894 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4895 {
4896 gdb::unique_xmalloc_ptr<char> this_real_name;
4897
4898 if (need_fullname)
4899 this_real_name = gdb_realpath (filename);
4900 (*fun) (filename, this_real_name.get (), data);
4901 });
4902}
4903
4904static int
4905dw2_has_symbols (struct objfile *objfile)
4906{
4907 return 1;
4908}
4909
4910const struct quick_symbol_functions dwarf2_gdb_index_functions =
4911{
4912 dw2_has_symbols,
4913 dw2_find_last_source_symtab,
4914 dw2_forget_cached_source_info,
4915 dw2_map_symtabs_matching_filename,
4916 dw2_lookup_symbol,
d3214198 4917 NULL,
927aa2e7
JK
4918 dw2_print_stats,
4919 dw2_dump,
927aa2e7
JK
4920 dw2_expand_symtabs_for_function,
4921 dw2_expand_all_symtabs,
4922 dw2_expand_symtabs_with_fullname,
4923 dw2_map_matching_symbols,
4924 dw2_expand_symtabs_matching,
4925 dw2_find_pc_sect_compunit_symtab,
4926 NULL,
4927 dw2_map_symbol_filenames
4928};
4929
4930/* DWARF-5 debug_names reader. */
4931
4932/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4933static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4934
4935/* A helper function that reads the .debug_names section in SECTION
4936 and fills in MAP. FILENAME is the name of the file containing the
4937 section; it is used for error reporting.
4938
4939 Returns true if all went well, false otherwise. */
4940
4941static bool
4942read_debug_names_from_section (struct objfile *objfile,
4943 const char *filename,
4944 struct dwarf2_section_info *section,
4945 mapped_debug_names &map)
4946{
96b79293 4947 if (section->empty ())
927aa2e7
JK
4948 return false;
4949
4950 /* Older elfutils strip versions could keep the section in the main
4951 executable while splitting it for the separate debug info file. */
96b79293 4952 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4953 return false;
4954
96b79293 4955 section->read (objfile);
927aa2e7 4956
08feed99 4957 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4958
4959 const gdb_byte *addr = section->buffer;
4960
96b79293 4961 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4962
4963 unsigned int bytes_read;
4964 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4965 addr += bytes_read;
4966
4967 map.dwarf5_is_dwarf64 = bytes_read != 4;
4968 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4969 if (bytes_read + length != section->size)
4970 {
4971 /* There may be multiple per-CU indices. */
4972 warning (_("Section .debug_names in %s length %s does not match "
4973 "section length %s, ignoring .debug_names."),
4974 filename, plongest (bytes_read + length),
4975 pulongest (section->size));
4976 return false;
4977 }
4978
4979 /* The version number. */
4980 uint16_t version = read_2_bytes (abfd, addr);
4981 addr += 2;
4982 if (version != 5)
4983 {
4984 warning (_("Section .debug_names in %s has unsupported version %d, "
4985 "ignoring .debug_names."),
4986 filename, version);
4987 return false;
4988 }
4989
4990 /* Padding. */
4991 uint16_t padding = read_2_bytes (abfd, addr);
4992 addr += 2;
4993 if (padding != 0)
4994 {
4995 warning (_("Section .debug_names in %s has unsupported padding %d, "
4996 "ignoring .debug_names."),
4997 filename, padding);
4998 return false;
4999 }
5000
5001 /* comp_unit_count - The number of CUs in the CU list. */
5002 map.cu_count = read_4_bytes (abfd, addr);
5003 addr += 4;
5004
5005 /* local_type_unit_count - The number of TUs in the local TU
5006 list. */
5007 map.tu_count = read_4_bytes (abfd, addr);
5008 addr += 4;
5009
5010 /* foreign_type_unit_count - The number of TUs in the foreign TU
5011 list. */
5012 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014 if (foreign_tu_count != 0)
5015 {
5016 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5017 "ignoring .debug_names."),
5018 filename, static_cast<unsigned long> (foreign_tu_count));
5019 return false;
5020 }
5021
5022 /* bucket_count - The number of hash buckets in the hash lookup
5023 table. */
5024 map.bucket_count = read_4_bytes (abfd, addr);
5025 addr += 4;
5026
5027 /* name_count - The number of unique names in the index. */
5028 map.name_count = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* abbrev_table_size - The size in bytes of the abbreviations
5032 table. */
5033 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5034 addr += 4;
5035
5036 /* augmentation_string_size - The size in bytes of the augmentation
5037 string. This value is rounded up to a multiple of 4. */
5038 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5039 addr += 4;
5040 map.augmentation_is_gdb = ((augmentation_string_size
5041 == sizeof (dwarf5_augmentation))
5042 && memcmp (addr, dwarf5_augmentation,
5043 sizeof (dwarf5_augmentation)) == 0);
5044 augmentation_string_size += (-augmentation_string_size) & 3;
5045 addr += augmentation_string_size;
5046
5047 /* List of CUs */
5048 map.cu_table_reordered = addr;
5049 addr += map.cu_count * map.offset_size;
5050
5051 /* List of Local TUs */
5052 map.tu_table_reordered = addr;
5053 addr += map.tu_count * map.offset_size;
5054
5055 /* Hash Lookup Table */
5056 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5057 addr += map.bucket_count * 4;
5058 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5059 addr += map.name_count * 4;
5060
5061 /* Name Table */
5062 map.name_table_string_offs_reordered = addr;
5063 addr += map.name_count * map.offset_size;
5064 map.name_table_entry_offs_reordered = addr;
5065 addr += map.name_count * map.offset_size;
5066
5067 const gdb_byte *abbrev_table_start = addr;
5068 for (;;)
5069 {
927aa2e7
JK
5070 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5071 addr += bytes_read;
5072 if (index_num == 0)
5073 break;
5074
5075 const auto insertpair
5076 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5077 if (!insertpair.second)
5078 {
5079 warning (_("Section .debug_names in %s has duplicate index %s, "
5080 "ignoring .debug_names."),
5081 filename, pulongest (index_num));
5082 return false;
5083 }
5084 mapped_debug_names::index_val &indexval = insertpair.first->second;
5085 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5086 addr += bytes_read;
5087
5088 for (;;)
5089 {
5090 mapped_debug_names::index_val::attr attr;
5091 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5092 addr += bytes_read;
5093 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5094 addr += bytes_read;
5095 if (attr.form == DW_FORM_implicit_const)
5096 {
5097 attr.implicit_const = read_signed_leb128 (abfd, addr,
5098 &bytes_read);
5099 addr += bytes_read;
5100 }
5101 if (attr.dw_idx == 0 && attr.form == 0)
5102 break;
5103 indexval.attr_vec.push_back (std::move (attr));
5104 }
5105 }
5106 if (addr != abbrev_table_start + abbrev_table_size)
5107 {
5108 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5109 "of size %s vs. written as %u, ignoring .debug_names."),
5110 filename, plongest (addr - abbrev_table_start),
5111 abbrev_table_size);
927aa2e7
JK
5112 return false;
5113 }
5114 map.entry_pool = addr;
5115
5116 return true;
5117}
5118
5119/* A helper for create_cus_from_debug_names that handles the MAP's CU
5120 list. */
5121
5122static void
ed2dc618 5123create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5124 const mapped_debug_names &map,
5125 dwarf2_section_info &section,
b76e467d 5126 bool is_dwz)
927aa2e7 5127{
3ee6bb11
TV
5128 if (!map.augmentation_is_gdb)
5129 {
5130 for (uint32_t i = 0; i < map.cu_count; ++i)
5131 {
5132 sect_offset sect_off
5133 = (sect_offset) (extract_unsigned_integer
5134 (map.cu_table_reordered + i * map.offset_size,
5135 map.offset_size,
5136 map.dwarf5_byte_order));
5137 /* We don't know the length of the CU, because the CU list in a
5138 .debug_names index can be incomplete, so we can't use the start of
5139 the next CU as end of this CU. We create the CUs here with length 0,
5140 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5141 dwarf2_per_cu_data *per_cu
5142 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5143 sect_off, 0);
5989a64e 5144 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5145 }
5146 }
5147
927aa2e7
JK
5148 sect_offset sect_off_prev;
5149 for (uint32_t i = 0; i <= map.cu_count; ++i)
5150 {
5151 sect_offset sect_off_next;
5152 if (i < map.cu_count)
5153 {
5154 sect_off_next
5155 = (sect_offset) (extract_unsigned_integer
5156 (map.cu_table_reordered + i * map.offset_size,
5157 map.offset_size,
5158 map.dwarf5_byte_order));
5159 }
5160 else
5161 sect_off_next = (sect_offset) section.size;
5162 if (i >= 1)
5163 {
5164 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5165 dwarf2_per_cu_data *per_cu
ed2dc618 5166 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5167 sect_off_prev, length);
5989a64e 5168 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5169 }
5170 sect_off_prev = sect_off_next;
5171 }
5172}
5173
5174/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5175 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5176
5177static void
ed2dc618 5178create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5179 const mapped_debug_names &map,
5180 const mapped_debug_names &dwz_map)
5181{
5989a64e
SM
5182 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5183 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5184
ed2dc618 5185 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5186 dwarf2_per_objfile->per_bfd->info,
b76e467d 5187 false /* is_dwz */);
927aa2e7
JK
5188
5189 if (dwz_map.cu_count == 0)
5190 return;
5191
c3699833 5192 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5193 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5194 true /* is_dwz */);
927aa2e7
JK
5195}
5196
5197/* Read .debug_names. If everything went ok, initialize the "quick"
5198 elements of all the CUs and return true. Otherwise, return false. */
5199
5200static bool
ed2dc618 5201dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5202{
22ca247e
TT
5203 std::unique_ptr<mapped_debug_names> map
5204 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5205 mapped_debug_names dwz_map (dwarf2_per_objfile);
5206 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5207
5208 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5209 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5210 *map))
927aa2e7
JK
5211 return false;
5212
5213 /* Don't use the index if it's empty. */
22ca247e 5214 if (map->name_count == 0)
927aa2e7
JK
5215 return false;
5216
5217 /* If there is a .dwz file, read it so we can get its CU list as
5218 well. */
c3699833 5219 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5220 if (dwz != NULL)
5221 {
5222 if (!read_debug_names_from_section (objfile,
00f93c44 5223 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5224 &dwz->debug_names, dwz_map))
5225 {
5226 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5227 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5228 return false;
5229 }
5230 }
5231
22ca247e 5232 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5233
22ca247e 5234 if (map->tu_count != 0)
927aa2e7
JK
5235 {
5236 /* We can only handle a single .debug_types when we have an
5237 index. */
5989a64e 5238 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5239 return false;
5240
5989a64e 5241 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5242
5243 create_signatured_type_table_from_debug_names
5989a64e 5244 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5245 }
5246
ed2dc618 5247 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5248 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5249
5989a64e
SM
5250 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5251 dwarf2_per_objfile->per_bfd->using_index = 1;
5252 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5253 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5254
5255 return true;
5256}
5257
927aa2e7
JK
5258/* Type used to manage iterating over all CUs looking for a symbol for
5259 .debug_names. */
5260
5261class dw2_debug_names_iterator
5262{
5263public:
927aa2e7 5264 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5265 gdb::optional<block_enum> block_index,
5266 domain_enum domain,
927aa2e7 5267 const char *name)
2b79f376 5268 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5269 m_addr (find_vec_in_debug_names (map, name))
5270 {}
5271
5272 dw2_debug_names_iterator (const mapped_debug_names &map,
5273 search_domain search, uint32_t namei)
5274 : m_map (map),
5275 m_search (search),
5276 m_addr (find_vec_in_debug_names (map, namei))
5277 {}
5278
3b00ef10
TT
5279 dw2_debug_names_iterator (const mapped_debug_names &map,
5280 block_enum block_index, domain_enum domain,
5281 uint32_t namei)
5282 : m_map (map), m_block_index (block_index), m_domain (domain),
5283 m_addr (find_vec_in_debug_names (map, namei))
5284 {}
5285
927aa2e7
JK
5286 /* Return the next matching CU or NULL if there are no more. */
5287 dwarf2_per_cu_data *next ();
5288
5289private:
5290 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5291 const char *name);
5292 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5293 uint32_t namei);
5294
5295 /* The internalized form of .debug_names. */
5296 const mapped_debug_names &m_map;
5297
2b79f376
SM
5298 /* If set, only look for symbols that match that block. Valid values are
5299 GLOBAL_BLOCK and STATIC_BLOCK. */
5300 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5301
5302 /* The kind of symbol we're looking for. */
5303 const domain_enum m_domain = UNDEF_DOMAIN;
5304 const search_domain m_search = ALL_DOMAIN;
5305
5306 /* The list of CUs from the index entry of the symbol, or NULL if
5307 not found. */
5308 const gdb_byte *m_addr;
5309};
5310
5311const char *
5312mapped_debug_names::namei_to_name (uint32_t namei) const
5313{
5314 const ULONGEST namei_string_offs
5315 = extract_unsigned_integer ((name_table_string_offs_reordered
5316 + namei * offset_size),
5317 offset_size,
5318 dwarf5_byte_order);
4f44ae6c
TT
5319 return read_indirect_string_at_offset (dwarf2_per_objfile,
5320 namei_string_offs);
927aa2e7
JK
5321}
5322
5323/* Find a slot in .debug_names for the object named NAME. If NAME is
5324 found, return pointer to its pool data. If NAME cannot be found,
5325 return NULL. */
5326
5327const gdb_byte *
5328dw2_debug_names_iterator::find_vec_in_debug_names
5329 (const mapped_debug_names &map, const char *name)
5330{
5331 int (*cmp) (const char *, const char *);
5332
54ee4252 5333 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5334 if (current_language->la_language == language_cplus
5335 || current_language->la_language == language_fortran
5336 || current_language->la_language == language_d)
5337 {
5338 /* NAME is already canonical. Drop any qualifiers as
5339 .debug_names does not contain any. */
5340
5341 if (strchr (name, '(') != NULL)
5342 {
54ee4252 5343 without_params = cp_remove_params (name);
927aa2e7 5344 if (without_params != NULL)
54ee4252 5345 name = without_params.get ();
927aa2e7
JK
5346 }
5347 }
5348
5349 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5350
5351 const uint32_t full_hash = dwarf5_djb_hash (name);
5352 uint32_t namei
5353 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5354 (map.bucket_table_reordered
5355 + (full_hash % map.bucket_count)), 4,
5356 map.dwarf5_byte_order);
5357 if (namei == 0)
5358 return NULL;
5359 --namei;
5360 if (namei >= map.name_count)
5361 {
b98664d3 5362 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5363 "[in module %s]"),
5364 namei, map.name_count,
ed2dc618 5365 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5366 return NULL;
5367 }
5368
5369 for (;;)
5370 {
5371 const uint32_t namei_full_hash
5372 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5373 (map.hash_table_reordered + namei), 4,
5374 map.dwarf5_byte_order);
5375 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5376 return NULL;
5377
5378 if (full_hash == namei_full_hash)
5379 {
5380 const char *const namei_string = map.namei_to_name (namei);
5381
5382#if 0 /* An expensive sanity check. */
5383 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5384 {
b98664d3 5385 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5386 "[in module %s]"),
5387 namei, objfile_name (dwarf2_per_objfile->objfile));
5388 return NULL;
5389 }
5390#endif
5391
5392 if (cmp (namei_string, name) == 0)
5393 {
5394 const ULONGEST namei_entry_offs
5395 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5396 + namei * map.offset_size),
5397 map.offset_size, map.dwarf5_byte_order);
5398 return map.entry_pool + namei_entry_offs;
5399 }
5400 }
5401
5402 ++namei;
5403 if (namei >= map.name_count)
5404 return NULL;
5405 }
5406}
5407
5408const gdb_byte *
5409dw2_debug_names_iterator::find_vec_in_debug_names
5410 (const mapped_debug_names &map, uint32_t namei)
5411{
5412 if (namei >= map.name_count)
5413 {
b98664d3 5414 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5415 "[in module %s]"),
5416 namei, map.name_count,
ed2dc618 5417 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5418 return NULL;
5419 }
5420
5421 const ULONGEST namei_entry_offs
5422 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5423 + namei * map.offset_size),
5424 map.offset_size, map.dwarf5_byte_order);
5425 return map.entry_pool + namei_entry_offs;
5426}
5427
5428/* See dw2_debug_names_iterator. */
5429
5430dwarf2_per_cu_data *
5431dw2_debug_names_iterator::next ()
5432{
5433 if (m_addr == NULL)
5434 return NULL;
5435
ed2dc618
SM
5436 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5437 struct objfile *objfile = dwarf2_per_objfile->objfile;
5438 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5439
5440 again:
5441
5442 unsigned int bytes_read;
5443 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5444 m_addr += bytes_read;
5445 if (abbrev == 0)
5446 return NULL;
5447
5448 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5449 if (indexval_it == m_map.abbrev_map.cend ())
5450 {
b98664d3 5451 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5452 "[in module %s]"),
ed2dc618 5453 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5454 return NULL;
5455 }
5456 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5457 enum class symbol_linkage {
5458 unknown,
5459 static_,
5460 extern_,
23c13d42 5461 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5462 dwarf2_per_cu_data *per_cu = NULL;
5463 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5464 {
5465 ULONGEST ull;
5466 switch (attr.form)
5467 {
5468 case DW_FORM_implicit_const:
5469 ull = attr.implicit_const;
5470 break;
5471 case DW_FORM_flag_present:
5472 ull = 1;
5473 break;
5474 case DW_FORM_udata:
5475 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5476 m_addr += bytes_read;
5477 break;
6dc55ce9 5478 case DW_FORM_ref4:
5479 ull = read_4_bytes (abfd, m_addr);
5480 m_addr += 4;
5481 break;
5482 case DW_FORM_ref8:
5483 ull = read_8_bytes (abfd, m_addr);
5484 m_addr += 8;
5485 break;
5486 case DW_FORM_ref_sig8:
5487 ull = read_8_bytes (abfd, m_addr);
5488 m_addr += 8;
5489 break;
927aa2e7 5490 default:
b98664d3 5491 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5492 dwarf_form_name (attr.form),
ed2dc618 5493 objfile_name (objfile));
927aa2e7
JK
5494 return NULL;
5495 }
5496 switch (attr.dw_idx)
5497 {
5498 case DW_IDX_compile_unit:
5499 /* Don't crash on bad data. */
5989a64e 5500 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5501 {
b98664d3 5502 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5503 " [in module %s]"),
5504 pulongest (ull),
5505 objfile_name (dwarf2_per_objfile->objfile));
5506 continue;
5507 }
5989a64e 5508 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5509 break;
8af5c486
JK
5510 case DW_IDX_type_unit:
5511 /* Don't crash on bad data. */
5989a64e 5512 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5513 {
b98664d3 5514 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5515 " [in module %s]"),
5516 pulongest (ull),
5517 objfile_name (dwarf2_per_objfile->objfile));
5518 continue;
5519 }
5989a64e 5520 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5521 break;
6dc55ce9 5522 case DW_IDX_die_offset:
5523 /* In a per-CU index (as opposed to a per-module index), index
5524 entries without CU attribute implicitly refer to the single CU. */
5525 if (per_cu == NULL)
5989a64e 5526 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5527 break;
927aa2e7
JK
5528 case DW_IDX_GNU_internal:
5529 if (!m_map.augmentation_is_gdb)
5530 break;
23c13d42 5531 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5532 break;
5533 case DW_IDX_GNU_external:
5534 if (!m_map.augmentation_is_gdb)
5535 break;
23c13d42 5536 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5537 break;
5538 }
5539 }
5540
5541 /* Skip if already read in. */
af758d11 5542 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5543 goto again;
5544
5545 /* Check static vs global. */
23c13d42 5546 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5547 {
2b79f376 5548 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5549 const bool symbol_is_static =
5550 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5551 if (want_static != symbol_is_static)
2b79f376 5552 goto again;
927aa2e7
JK
5553 }
5554
5555 /* Match dw2_symtab_iter_next, symbol_kind
5556 and debug_names::psymbol_tag. */
5557 switch (m_domain)
5558 {
5559 case VAR_DOMAIN:
5560 switch (indexval.dwarf_tag)
5561 {
5562 case DW_TAG_variable:
5563 case DW_TAG_subprogram:
5564 /* Some types are also in VAR_DOMAIN. */
5565 case DW_TAG_typedef:
5566 case DW_TAG_structure_type:
5567 break;
5568 default:
5569 goto again;
5570 }
5571 break;
5572 case STRUCT_DOMAIN:
5573 switch (indexval.dwarf_tag)
5574 {
5575 case DW_TAG_typedef:
5576 case DW_TAG_structure_type:
5577 break;
5578 default:
5579 goto again;
5580 }
5581 break;
5582 case LABEL_DOMAIN:
5583 switch (indexval.dwarf_tag)
5584 {
5585 case 0:
5586 case DW_TAG_variable:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
59c35742
AB
5592 case MODULE_DOMAIN:
5593 switch (indexval.dwarf_tag)
5594 {
5595 case DW_TAG_module:
5596 break;
5597 default:
5598 goto again;
5599 }
5600 break;
927aa2e7
JK
5601 default:
5602 break;
5603 }
5604
5605 /* Match dw2_expand_symtabs_matching, symbol_kind and
5606 debug_names::psymbol_tag. */
5607 switch (m_search)
4b514bc8 5608 {
927aa2e7
JK
5609 case VARIABLES_DOMAIN:
5610 switch (indexval.dwarf_tag)
4b514bc8 5611 {
927aa2e7
JK
5612 case DW_TAG_variable:
5613 break;
5614 default:
5615 goto again;
4b514bc8 5616 }
927aa2e7
JK
5617 break;
5618 case FUNCTIONS_DOMAIN:
5619 switch (indexval.dwarf_tag)
4b514bc8 5620 {
927aa2e7
JK
5621 case DW_TAG_subprogram:
5622 break;
5623 default:
5624 goto again;
4b514bc8 5625 }
927aa2e7
JK
5626 break;
5627 case TYPES_DOMAIN:
5628 switch (indexval.dwarf_tag)
5629 {
5630 case DW_TAG_typedef:
5631 case DW_TAG_structure_type:
5632 break;
5633 default:
5634 goto again;
5635 }
5636 break;
59c35742
AB
5637 case MODULES_DOMAIN:
5638 switch (indexval.dwarf_tag)
5639 {
5640 case DW_TAG_module:
5641 break;
5642 default:
5643 goto again;
5644 }
927aa2e7
JK
5645 default:
5646 break;
4b514bc8 5647 }
927aa2e7
JK
5648
5649 return per_cu;
4b514bc8 5650}
61920122 5651
927aa2e7 5652static struct compunit_symtab *
c7f839cb 5653dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5654 const char *name, domain_enum domain)
4b514bc8 5655{
ed2dc618
SM
5656 struct dwarf2_per_objfile *dwarf2_per_objfile
5657 = get_dwarf2_per_objfile (objfile);
61920122 5658
5989a64e 5659 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5660 if (!mapp)
61920122 5661 {
927aa2e7
JK
5662 /* index is NULL if OBJF_READNOW. */
5663 return NULL;
5664 }
5665 const auto &map = *mapp;
9291a0cd 5666
2b79f376 5667 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5668
927aa2e7
JK
5669 struct compunit_symtab *stab_best = NULL;
5670 struct dwarf2_per_cu_data *per_cu;
5671 while ((per_cu = iter.next ()) != NULL)
5672 {
5673 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5674 compunit_symtab *stab
5675 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5676 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5677 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5678
927aa2e7
JK
5679 sym = block_find_symbol (block, name, domain,
5680 block_find_non_opaque_type_preferred,
5681 &with_opaque);
9703b513 5682
927aa2e7
JK
5683 /* Some caution must be observed with overloaded functions and
5684 methods, since the index will not contain any overload
5685 information (but NAME might contain it). */
a3ec0bb1 5686
927aa2e7 5687 if (sym != NULL
987012b8 5688 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5689 return stab;
5690 if (with_opaque != NULL
987012b8 5691 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5692 stab_best = stab;
9703b513 5693
927aa2e7 5694 /* Keep looking through other CUs. */
9703b513
TT
5695 }
5696
927aa2e7 5697 return stab_best;
9703b513
TT
5698}
5699
927aa2e7
JK
5700/* This dumps minimal information about .debug_names. It is called
5701 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5702 uses this to verify that .debug_names has been loaded. */
9291a0cd 5703
927aa2e7
JK
5704static void
5705dw2_debug_names_dump (struct objfile *objfile)
5706{
ed2dc618
SM
5707 struct dwarf2_per_objfile *dwarf2_per_objfile
5708 = get_dwarf2_per_objfile (objfile);
5709
5989a64e 5710 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5711 printf_filtered (".debug_names:");
5989a64e 5712 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5713 printf_filtered (" exists\n");
5714 else
5715 printf_filtered (" faked for \"readnow\"\n");
5716 printf_filtered ("\n");
9291a0cd
TT
5717}
5718
9291a0cd 5719static void
927aa2e7
JK
5720dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5721 const char *func_name)
9291a0cd 5722{
ed2dc618
SM
5723 struct dwarf2_per_objfile *dwarf2_per_objfile
5724 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5725
5989a64e
SM
5726 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5727 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5728 {
5989a64e 5729 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5730
2b79f376 5731 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5732
927aa2e7
JK
5733 struct dwarf2_per_cu_data *per_cu;
5734 while ((per_cu = iter.next ()) != NULL)
97a1449a 5735 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5736 }
5737}
24c79950 5738
3b00ef10
TT
5739static void
5740dw2_debug_names_map_matching_symbols
5741 (struct objfile *objfile,
5742 const lookup_name_info &name, domain_enum domain,
5743 int global,
5744 gdb::function_view<symbol_found_callback_ftype> callback,
5745 symbol_compare_ftype *ordered_compare)
5746{
5747 struct dwarf2_per_objfile *dwarf2_per_objfile
5748 = get_dwarf2_per_objfile (objfile);
5749
5750 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5751 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5752 return;
5753
5989a64e 5754 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5755 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5756
5757 const char *match_name = name.ada ().lookup_name ().c_str ();
5758 auto matcher = [&] (const char *symname)
5759 {
5760 if (ordered_compare == nullptr)
5761 return true;
5762 return ordered_compare (symname, match_name) == 0;
5763 };
5764
5765 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5766 [&] (offset_type namei)
5767 {
5768 /* The name was matched, now expand corresponding CUs that were
5769 marked. */
5770 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5771
5772 struct dwarf2_per_cu_data *per_cu;
5773 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5774 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5775 nullptr);
3b00ef10
TT
5776 return true;
5777 });
5778
5779 /* It's a shame we couldn't do this inside the
5780 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5781 that have already been expanded. Instead, this loop matches what
5782 the psymtab code does. */
5989a64e 5783 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5784 {
af758d11
SM
5785 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5786 if (symtab != nullptr)
3b00ef10
TT
5787 {
5788 const struct block *block
af758d11 5789 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5790 if (!iterate_over_symbols_terminated (block, name,
5791 domain, callback))
5792 break;
5793 }
5794 }
5795}
5796
927aa2e7
JK
5797static void
5798dw2_debug_names_expand_symtabs_matching
5799 (struct objfile *objfile,
5800 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5801 const lookup_name_info *lookup_name,
927aa2e7
JK
5802 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5803 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5804 enum search_domain kind)
5805{
ed2dc618
SM
5806 struct dwarf2_per_objfile *dwarf2_per_objfile
5807 = get_dwarf2_per_objfile (objfile);
9291a0cd 5808
927aa2e7 5809 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5810 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5811 return;
9291a0cd 5812
ed2dc618 5813 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5814
c1a66c06
TV
5815 if (symbol_matcher == NULL && lookup_name == NULL)
5816 {
5989a64e 5817 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5818 {
5819 QUIT;
5820
97a1449a
SM
5821 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5822 file_matcher, expansion_notify);
c1a66c06
TV
5823 }
5824 return;
5825 }
5826
5989a64e 5827 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5828
c1a66c06 5829 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5830 symbol_matcher,
5831 kind, [&] (offset_type namei)
927aa2e7 5832 {
927aa2e7
JK
5833 /* The name was matched, now expand corresponding CUs that were
5834 marked. */
5835 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5836
927aa2e7
JK
5837 struct dwarf2_per_cu_data *per_cu;
5838 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5839 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5840 file_matcher, expansion_notify);
3b00ef10 5841 return true;
44ed8f3e 5842 });
9291a0cd
TT
5843}
5844
927aa2e7 5845const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5846{
5847 dw2_has_symbols,
5848 dw2_find_last_source_symtab,
5849 dw2_forget_cached_source_info,
f8eba3c6 5850 dw2_map_symtabs_matching_filename,
927aa2e7 5851 dw2_debug_names_lookup_symbol,
d3214198 5852 NULL,
9291a0cd 5853 dw2_print_stats,
927aa2e7 5854 dw2_debug_names_dump,
927aa2e7 5855 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5856 dw2_expand_all_symtabs,
652a8996 5857 dw2_expand_symtabs_with_fullname,
3b00ef10 5858 dw2_debug_names_map_matching_symbols,
927aa2e7 5859 dw2_debug_names_expand_symtabs_matching,
43f3e411 5860 dw2_find_pc_sect_compunit_symtab,
71a3c369 5861 NULL,
9291a0cd
TT
5862 dw2_map_symbol_filenames
5863};
5864
4485a1c1 5865/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5866 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5867
5868template <typename T>
5869static gdb::array_view<const gdb_byte>
5870get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5871{
5872 dwarf2_section_info *section = &section_owner->gdb_index;
5873
96b79293 5874 if (section->empty ())
4485a1c1
SM
5875 return {};
5876
5877 /* Older elfutils strip versions could keep the section in the main
5878 executable while splitting it for the separate debug info file. */
96b79293 5879 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5880 return {};
5881
96b79293 5882 section->read (obj);
4485a1c1 5883
8bebfcda
PA
5884 /* dwarf2_section_info::size is a bfd_size_type, while
5885 gdb::array_view works with size_t. On 32-bit hosts, with
5886 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5887 is 32-bit. So we need an explicit narrowing conversion here.
5888 This is fine, because it's impossible to allocate or mmap an
5889 array/buffer larger than what size_t can represent. */
5890 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5891}
5892
87d6a7aa
SM
5893/* Lookup the index cache for the contents of the index associated to
5894 DWARF2_OBJ. */
5895
5896static gdb::array_view<const gdb_byte>
5989a64e 5897get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5898{
5899 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5900 if (build_id == nullptr)
5901 return {};
5902
5903 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5904 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5905}
5906
5907/* Same as the above, but for DWZ. */
5908
5909static gdb::array_view<const gdb_byte>
5910get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5911{
5912 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5913 if (build_id == nullptr)
5914 return {};
5915
5916 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5917}
5918
3c0aa29a 5919/* See symfile.h. */
9291a0cd 5920
3c0aa29a
PA
5921bool
5922dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5923{
ed2dc618
SM
5924 struct dwarf2_per_objfile *dwarf2_per_objfile
5925 = get_dwarf2_per_objfile (objfile);
5926
9291a0cd
TT
5927 /* If we're about to read full symbols, don't bother with the
5928 indices. In this case we also don't care if some other debug
5929 format is making psymtabs, because they are all about to be
5930 expanded anyway. */
5931 if ((objfile->flags & OBJF_READNOW))
5932 {
5989a64e 5933 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5934 create_all_comp_units (dwarf2_per_objfile);
5935 create_all_type_units (dwarf2_per_objfile);
5989a64e 5936 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5937 = create_quick_file_names_table
5989a64e 5938 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5939 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5940
5989a64e
SM
5941 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5942 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5943 {
5989a64e 5944 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5945
5989a64e 5946 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5947 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5948 }
5949
5950 /* Return 1 so that gdb sees the "quick" functions. However,
5951 these functions will be no-ops because we will have expanded
5952 all symtabs. */
3c0aa29a
PA
5953 *index_kind = dw_index_kind::GDB_INDEX;
5954 return true;
9291a0cd
TT
5955 }
5956
ed2dc618 5957 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5958 {
5959 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5960 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5961 return true;
5962 }
927aa2e7 5963
4485a1c1 5964 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5965 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5966 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5967 {
5968 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5969 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5970 return true;
5971 }
9291a0cd 5972
87d6a7aa
SM
5973 /* ... otherwise, try to find the index in the index cache. */
5974 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5975 get_gdb_index_contents_from_cache,
5976 get_gdb_index_contents_from_cache_dwz))
5977 {
5978 global_index_cache.hit ();
5979 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5980 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5981 return true;
5982 }
5983
5984 global_index_cache.miss ();
3c0aa29a 5985 return false;
9291a0cd
TT
5986}
5987
5988\f
5989
dce234bc
PP
5990/* Build a partial symbol table. */
5991
5992void
f29dff0a 5993dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5994{
ed2dc618
SM
5995 struct dwarf2_per_objfile *dwarf2_per_objfile
5996 = get_dwarf2_per_objfile (objfile);
c9bf0622 5997
6eee24ce 5998 init_psymbol_list (objfile, 1024);
c906108c 5999
a70b8144 6000 try
c9bf0622
TT
6001 {
6002 /* This isn't really ideal: all the data we allocate on the
6003 objfile's obstack is still uselessly kept around. However,
6004 freeing it seems unsafe. */
906768f9 6005 psymtab_discarder psymtabs (objfile);
ed2dc618 6006 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6007 psymtabs.keep ();
87d6a7aa 6008
af758d11
SM
6009 dwarf2_per_objfile->resize_symtabs ();
6010
87d6a7aa
SM
6011 /* (maybe) store an index in the cache. */
6012 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6013 }
230d2906 6014 catch (const gdb_exception_error &except)
492d29ea
PA
6015 {
6016 exception_print (gdb_stderr, except);
6017 }
c906108c 6018}
c906108c 6019
3b80fe9b
DE
6020/* Find the base address of the compilation unit for range lists and
6021 location lists. It will normally be specified by DW_AT_low_pc.
6022 In DWARF-3 draft 4, the base address could be overridden by
6023 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6024 compilation units with discontinuous ranges. */
6025
6026static void
6027dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6028{
6029 struct attribute *attr;
6030
2b24b6e4 6031 cu->base_address.reset ();
3b80fe9b
DE
6032
6033 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6034 if (attr != nullptr)
2b24b6e4 6035 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6036 else
6037 {
6038 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6039 if (attr != nullptr)
2b24b6e4 6040 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6041 }
6042}
6043
36586728
TT
6044/* Helper function that returns the proper abbrev section for
6045 THIS_CU. */
6046
6047static struct dwarf2_section_info *
6048get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6049{
6050 struct dwarf2_section_info *abbrev;
c3699833 6051 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6052
6053 if (this_cu->is_dwz)
c3699833 6054 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6055 else
c3699833 6056 abbrev = &per_bfd->abbrev;
36586728
TT
6057
6058 return abbrev;
6059}
6060
f4dc4d17
DE
6061/* Fetch the abbreviation table offset from a comp or type unit header. */
6062
6063static sect_offset
ed2dc618
SM
6064read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6065 struct dwarf2_section_info *section,
9c541725 6066 sect_offset sect_off)
f4dc4d17 6067{
96b79293 6068 bfd *abfd = section->get_bfd_owner ();
d521ce57 6069 const gdb_byte *info_ptr;
ac298888 6070 unsigned int initial_length_size, offset_size;
43988095 6071 uint16_t version;
f4dc4d17 6072
96b79293 6073 section->read (dwarf2_per_objfile->objfile);
9c541725 6074 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6075 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6076 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6077 info_ptr += initial_length_size;
6078
6079 version = read_2_bytes (abfd, info_ptr);
6080 info_ptr += 2;
6081 if (version >= 5)
6082 {
6083 /* Skip unit type and address size. */
6084 info_ptr += 2;
6085 }
6086
24aa364d 6087 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6088}
6089
b83470bf
TT
6090/* A partial symtab that is used only for include files. */
6091struct dwarf2_include_psymtab : public partial_symtab
6092{
6093 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6094 : partial_symtab (filename, objfile)
6095 {
6096 }
6097
6098 void read_symtab (struct objfile *objfile) override
6099 {
194d088f
TV
6100 /* It's an include file, no symbols to read for it.
6101 Everything is in the includer symtab. */
6102
6103 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6104 expansion of the includer psymtab. We use the dependencies[0] field to
6105 model the includer. But if we go the regular route of calling
6106 expand_psymtab here, and having expand_psymtab call expand_dependencies
6107 to expand the includer, we'll only use expand_psymtab on the includer
6108 (making it a non-toplevel psymtab), while if we expand the includer via
6109 another path, we'll use read_symtab (making it a toplevel psymtab).
6110 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6111 psymtab, and trigger read_symtab on the includer here directly. */
6112 includer ()->read_symtab (objfile);
b83470bf
TT
6113 }
6114
6115 void expand_psymtab (struct objfile *objfile) override
6116 {
194d088f
TV
6117 /* This is not called by read_symtab, and should not be called by any
6118 expand_dependencies. */
6119 gdb_assert (false);
b83470bf
TT
6120 }
6121
5717c425 6122 bool readin_p (struct objfile *objfile) const override
b83470bf 6123 {
5717c425 6124 return includer ()->readin_p (objfile);
b83470bf
TT
6125 }
6126
5717c425 6127 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6128 {
6129 return nullptr;
6130 }
6131
6132private:
194d088f
TV
6133 partial_symtab *includer () const
6134 {
6135 /* An include psymtab has exactly one dependency: the psymtab that
6136 includes it. */
6137 gdb_assert (this->number_of_dependencies == 1);
6138 return this->dependencies[0];
6139 }
b83470bf
TT
6140};
6141
aaa75496
JB
6142/* Allocate a new partial symtab for file named NAME and mark this new
6143 partial symtab as being an include of PST. */
6144
6145static void
891813be 6146dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6147 struct objfile *objfile)
6148{
b83470bf 6149 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6150
fbd9ab74 6151 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6152 subpst->dirname = pst->dirname;
fbd9ab74 6153
a9342b62 6154 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6155 subpst->dependencies[0] = pst;
6156 subpst->number_of_dependencies = 1;
aaa75496
JB
6157}
6158
6159/* Read the Line Number Program data and extract the list of files
6160 included by the source file represented by PST. Build an include
d85a05f0 6161 partial symtab for each of these included files. */
aaa75496
JB
6162
6163static void
6164dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6165 struct die_info *die,
891813be 6166 dwarf2_psymtab *pst)
aaa75496 6167{
fff8551c 6168 line_header_up lh;
d85a05f0 6169 struct attribute *attr;
aaa75496 6170
d85a05f0 6171 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6172 if (attr != nullptr)
9c541725 6173 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6174 if (lh == NULL)
6175 return; /* No linetable, so no includes. */
6176
79748972
TT
6177 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6178 that we pass in the raw text_low here; that is ok because we're
6179 only decoding the line table to make include partial symtabs, and
6180 so the addresses aren't really used. */
4ae976d1 6181 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6182 pst->raw_text_low (), 1);
aaa75496
JB
6183}
6184
348e048f 6185static hashval_t
52dc124a 6186hash_signatured_type (const void *item)
348e048f 6187{
9a3c8263
SM
6188 const struct signatured_type *sig_type
6189 = (const struct signatured_type *) item;
9a619af0 6190
348e048f 6191 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6192 return sig_type->signature;
348e048f
DE
6193}
6194
6195static int
52dc124a 6196eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6197{
9a3c8263
SM
6198 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6199 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6200
348e048f
DE
6201 return lhs->signature == rhs->signature;
6202}
6203
1fd400ff
TT
6204/* Allocate a hash table for signatured types. */
6205
b0b6a987 6206static htab_up
298e9637 6207allocate_signatured_type_table ()
1fd400ff 6208{
b0b6a987
TT
6209 return htab_up (htab_create_alloc (41,
6210 hash_signatured_type,
6211 eq_signatured_type,
6212 NULL, xcalloc, xfree));
1fd400ff
TT
6213}
6214
d467dd73 6215/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6216
6217static int
d467dd73 6218add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6219{
9a3c8263 6220 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6221 std::vector<signatured_type *> *all_type_units
6222 = (std::vector<signatured_type *> *) datum;
1fd400ff 6223
b2bdb8cf 6224 all_type_units->push_back (sigt);
1fd400ff
TT
6225
6226 return 1;
6227}
6228
78d4d2c5 6229/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6230 and fill them into TYPES_HTAB. It will process only type units,
6231 therefore DW_UT_type. */
c88ee1f0 6232
78d4d2c5 6233static void
ed2dc618
SM
6234create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6235 struct dwo_file *dwo_file,
b0b6a987 6236 dwarf2_section_info *section, htab_up &types_htab,
43988095 6237 rcuh_kind section_kind)
348e048f 6238{
3019eac3 6239 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6240 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6241 bfd *abfd;
6242 const gdb_byte *info_ptr, *end_ptr;
348e048f 6243
4bdcc0c1
DE
6244 abbrev_section = (dwo_file != NULL
6245 ? &dwo_file->sections.abbrev
5989a64e 6246 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6247
b4f54984 6248 if (dwarf_read_debug)
43988095 6249 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6250 section->get_name (),
6251 abbrev_section->get_file_name ());
09406207 6252
96b79293 6253 section->read (objfile);
78d4d2c5 6254 info_ptr = section->buffer;
348e048f 6255
78d4d2c5
JK
6256 if (info_ptr == NULL)
6257 return;
348e048f 6258
78d4d2c5
JK
6259 /* We can't set abfd until now because the section may be empty or
6260 not present, in which case the bfd is unknown. */
96b79293 6261 abfd = section->get_bfd_owner ();
348e048f 6262
c0ab21c2
TT
6263 /* We don't use cutu_reader here because we don't need to read
6264 any dies: the signature is in the header. */
3019eac3 6265
78d4d2c5
JK
6266 end_ptr = info_ptr + section->size;
6267 while (info_ptr < end_ptr)
6268 {
78d4d2c5
JK
6269 struct signatured_type *sig_type;
6270 struct dwo_unit *dwo_tu;
6271 void **slot;
6272 const gdb_byte *ptr = info_ptr;
6273 struct comp_unit_head header;
6274 unsigned int length;
8b70b953 6275
9c541725 6276 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6277
a49dd8dd
JK
6278 /* Initialize it due to a false compiler warning. */
6279 header.signature = -1;
9c541725 6280 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6281
78d4d2c5
JK
6282 /* We need to read the type's signature in order to build the hash
6283 table, but we don't need anything else just yet. */
348e048f 6284
ed2dc618 6285 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6286 abbrev_section, ptr, section_kind);
348e048f 6287
4057dfde 6288 length = header.get_length ();
6caca83c 6289
78d4d2c5
JK
6290 /* Skip dummy type units. */
6291 if (ptr >= info_ptr + length
43988095
JK
6292 || peek_abbrev_code (abfd, ptr) == 0
6293 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6294 {
6295 info_ptr += length;
6296 continue;
6297 }
dee91e82 6298
78d4d2c5
JK
6299 if (types_htab == NULL)
6300 {
6301 if (dwo_file)
298e9637 6302 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6303 else
298e9637 6304 types_htab = allocate_signatured_type_table ();
78d4d2c5 6305 }
8b70b953 6306
78d4d2c5
JK
6307 if (dwo_file)
6308 {
6309 sig_type = NULL;
5989a64e 6310 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6311 struct dwo_unit);
6312 dwo_tu->dwo_file = dwo_file;
43988095 6313 dwo_tu->signature = header.signature;
9c541725 6314 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6315 dwo_tu->section = section;
9c541725 6316 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6317 dwo_tu->length = length;
6318 }
6319 else
6320 {
6321 /* N.B.: type_offset is not usable if this type uses a DWO file.
6322 The real type_offset is in the DWO file. */
6323 dwo_tu = NULL;
5989a64e 6324 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6325 sig_type->signature = header.signature;
9c541725 6326 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6327 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6328 sig_type->per_cu.is_debug_types = 1;
6329 sig_type->per_cu.section = section;
9c541725 6330 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6331 sig_type->per_cu.length = length;
6332 }
6333
b0b6a987 6334 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6335 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6336 INSERT);
6337 gdb_assert (slot != NULL);
6338 if (*slot != NULL)
6339 {
9c541725 6340 sect_offset dup_sect_off;
0349ea22 6341
3019eac3
DE
6342 if (dwo_file)
6343 {
78d4d2c5
JK
6344 const struct dwo_unit *dup_tu
6345 = (const struct dwo_unit *) *slot;
6346
9c541725 6347 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6348 }
6349 else
6350 {
78d4d2c5
JK
6351 const struct signatured_type *dup_tu
6352 = (const struct signatured_type *) *slot;
6353
9c541725 6354 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6355 }
8b70b953 6356
b98664d3 6357 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6358 " the entry at offset %s, signature %s"),
6359 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6360 hex_string (header.signature));
78d4d2c5
JK
6361 }
6362 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6363
78d4d2c5 6364 if (dwarf_read_debug > 1)
9d8780f0
SM
6365 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6366 sect_offset_str (sect_off),
43988095 6367 hex_string (header.signature));
3019eac3 6368
78d4d2c5
JK
6369 info_ptr += length;
6370 }
6371}
3019eac3 6372
78d4d2c5
JK
6373/* Create the hash table of all entries in the .debug_types
6374 (or .debug_types.dwo) section(s).
6375 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6376 otherwise it is NULL.
b3c8eb43 6377
78d4d2c5 6378 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6379
78d4d2c5 6380 Note: This function processes DWO files only, not DWP files. */
348e048f 6381
78d4d2c5 6382static void
ed2dc618
SM
6383create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6384 struct dwo_file *dwo_file,
fd5866f6 6385 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6386 htab_up &types_htab)
78d4d2c5 6387{
fd5866f6
SM
6388 for (dwarf2_section_info &section : type_sections)
6389 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6390 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6391}
6392
6393/* Create the hash table of all entries in the .debug_types section,
6394 and initialize all_type_units.
6395 The result is zero if there is an error (e.g. missing .debug_types section),
6396 otherwise non-zero. */
6397
6398static int
ed2dc618 6399create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6400{
b0b6a987 6401 htab_up types_htab;
3019eac3 6402
ed2dc618 6403 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6404 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6405 rcuh_kind::COMPILE);
ed2dc618 6406 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6407 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6408 if (types_htab == NULL)
6409 {
5989a64e 6410 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6411 return 0;
6412 }
6413
5989a64e 6414 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6415
5989a64e
SM
6416 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6417 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6418 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6419
5989a64e 6420 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6421 add_signatured_type_cu_to_table,
5989a64e 6422 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6423
348e048f
DE
6424 return 1;
6425}
6426
5989a64e 6427/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6428 If SLOT is non-NULL, it is the entry to use in the hash table.
6429 Otherwise we find one. */
6430
6431static struct signatured_type *
ed2dc618
SM
6432add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6433 void **slot)
6aa5f3a6 6434{
5989a64e
SM
6435 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6436 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6437 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6438
5989a64e 6439 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6440
af758d11
SM
6441 dwarf2_per_objfile->resize_symtabs ();
6442
5989a64e 6443 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6444 sig_type->signature = sig;
6445 sig_type->per_cu.is_debug_types = 1;
5989a64e 6446 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6447 {
6448 sig_type->per_cu.v.quick =
5989a64e 6449 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6450 struct dwarf2_per_cu_quick_data);
6451 }
6452
6453 if (slot == NULL)
6454 {
5989a64e 6455 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6456 sig_type, INSERT);
6457 }
6458 gdb_assert (*slot == NULL);
6459 *slot = sig_type;
6460 /* The rest of sig_type must be filled in by the caller. */
6461 return sig_type;
6462}
6463
a2ce51a0
DE
6464/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6465 Fill in SIG_ENTRY with DWO_ENTRY. */
6466
6467static void
ed2dc618 6468fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6469 struct signatured_type *sig_entry,
6470 struct dwo_unit *dwo_entry)
6471{
1859c670
SM
6472 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6473
7ee85ab1 6474 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6475 gdb_assert (! sig_entry->per_cu.queued);
6476 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6477 if (per_bfd->using_index)
6aa5f3a6
DE
6478 {
6479 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6480 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6481 }
6482 else
6483 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6484 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6485 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6486 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6487 gdb_assert (sig_entry->dwo_unit == NULL);
6488
6489 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6490 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6491 sig_entry->per_cu.length = dwo_entry->length;
6492 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6493 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 6494 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6495 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6496 sig_entry->dwo_unit = dwo_entry;
6497}
6498
6499/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6500 If we haven't read the TU yet, create the signatured_type data structure
6501 for a TU to be read in directly from a DWO file, bypassing the stub.
6502 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6503 using .gdb_index, then when reading a CU we want to stay in the DWO file
6504 containing that CU. Otherwise we could end up reading several other DWO
6505 files (due to comdat folding) to process the transitive closure of all the
6506 mentioned TUs, and that can be slow. The current DWO file will have every
6507 type signature that it needs.
a2ce51a0
DE
6508 We only do this for .gdb_index because in the psymtab case we already have
6509 to read all the DWOs to build the type unit groups. */
6510
6511static struct signatured_type *
6512lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6513{
5e22e966 6514 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6515 struct dwo_file *dwo_file;
6516 struct dwo_unit find_dwo_entry, *dwo_entry;
6517 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6518 void **slot;
a2ce51a0 6519
5989a64e 6520 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6521
6aa5f3a6
DE
6522 /* If TU skeletons have been removed then we may not have read in any
6523 TUs yet. */
5989a64e
SM
6524 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6525 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6526
6527 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6528 Use the global signatured_types array to do our own comdat-folding
6529 of types. If this is the first time we're reading this TU, and
6530 the TU has an entry in .gdb_index, replace the recorded data from
6531 .gdb_index with this TU. */
a2ce51a0 6532
a2ce51a0 6533 find_sig_entry.signature = sig;
5989a64e 6534 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6535 &find_sig_entry, INSERT);
9a3c8263 6536 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6537
6538 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6539 read. Don't reassign the global entry to point to this DWO if that's
6540 the case. Also note that if the TU is already being read, it may not
6541 have come from a DWO, the program may be a mix of Fission-compiled
6542 code and non-Fission-compiled code. */
6543
6544 /* Have we already tried to read this TU?
6545 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6546 needn't exist in the global table yet). */
6547 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6548 return sig_entry;
6549
6aa5f3a6
DE
6550 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6551 dwo_unit of the TU itself. */
6552 dwo_file = cu->dwo_unit->dwo_file;
6553
a2ce51a0
DE
6554 /* Ok, this is the first time we're reading this TU. */
6555 if (dwo_file->tus == NULL)
6556 return NULL;
6557 find_dwo_entry.signature = sig;
b0b6a987
TT
6558 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6559 &find_dwo_entry);
a2ce51a0
DE
6560 if (dwo_entry == NULL)
6561 return NULL;
6562
6aa5f3a6
DE
6563 /* If the global table doesn't have an entry for this TU, add one. */
6564 if (sig_entry == NULL)
ed2dc618 6565 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6566
ed2dc618 6567 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6568 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6569 return sig_entry;
6570}
6571
a2ce51a0
DE
6572/* Subroutine of lookup_signatured_type.
6573 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6574 then try the DWP file. If the TU stub (skeleton) has been removed then
6575 it won't be in .gdb_index. */
a2ce51a0
DE
6576
6577static struct signatured_type *
6578lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6579{
5e22e966 6580 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6581 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6582 struct dwo_unit *dwo_entry;
6583 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6584 void **slot;
a2ce51a0 6585
5989a64e 6586 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6587 gdb_assert (dwp_file != NULL);
6588
6aa5f3a6
DE
6589 /* If TU skeletons have been removed then we may not have read in any
6590 TUs yet. */
5989a64e
SM
6591 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6592 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6593
6aa5f3a6 6594 find_sig_entry.signature = sig;
5989a64e 6595 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6596 &find_sig_entry, INSERT);
9a3c8263 6597 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6598
6599 /* Have we already tried to read this TU?
6600 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6601 needn't exist in the global table yet). */
6602 if (sig_entry != NULL)
6603 return sig_entry;
6604
a2ce51a0
DE
6605 if (dwp_file->tus == NULL)
6606 return NULL;
ed2dc618 6607 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6608 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6609 if (dwo_entry == NULL)
6610 return NULL;
6611
ed2dc618
SM
6612 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6613 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6614
a2ce51a0
DE
6615 return sig_entry;
6616}
6617
380bca97 6618/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6619 Returns NULL if signature SIG is not present in the table.
6620 It is up to the caller to complain about this. */
348e048f
DE
6621
6622static struct signatured_type *
a2ce51a0 6623lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6624{
5e22e966 6625 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6626
a2ce51a0 6627 if (cu->dwo_unit
5989a64e 6628 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6629 {
6630 /* We're in a DWO/DWP file, and we're using .gdb_index.
6631 These cases require special processing. */
ed2dc618 6632 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6633 return lookup_dwo_signatured_type (cu, sig);
6634 else
6635 return lookup_dwp_signatured_type (cu, sig);
6636 }
6637 else
6638 {
6639 struct signatured_type find_entry, *entry;
348e048f 6640
5989a64e 6641 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6642 return NULL;
6643 find_entry.signature = sig;
9a3c8263 6644 entry = ((struct signatured_type *)
5989a64e 6645 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6646 &find_entry));
a2ce51a0
DE
6647 return entry;
6648 }
348e048f 6649}
18a8505e 6650
42e7ad6c 6651/* Low level DIE reading support. */
348e048f 6652
d85a05f0
DJ
6653/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6654
6655static void
6656init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6657 struct dwarf2_cu *cu,
3019eac3 6658 struct dwarf2_section_info *section,
685af9cd
TT
6659 struct dwo_file *dwo_file,
6660 struct abbrev_table *abbrev_table)
d85a05f0 6661{
fceca515 6662 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6663 reader->abfd = section->get_bfd_owner ();
d85a05f0 6664 reader->cu = cu;
3019eac3 6665 reader->dwo_file = dwo_file;
dee91e82
DE
6666 reader->die_section = section;
6667 reader->buffer = section->buffer;
f664829e 6668 reader->buffer_end = section->buffer + section->size;
685af9cd 6669 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6670}
6671
c0ab21c2 6672/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6673 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6674 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6675 already.
6676
6677 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6678 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6679 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6680 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6681 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6682 STUB_COMP_DIR may be non-NULL.
3e225074 6683 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6684 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6685 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6686 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6687 kept around for at least as long as *RESULT_READER.
6688
b0c7bfa9
DE
6689 The result is non-zero if a valid (non-dummy) DIE was found. */
6690
6691static int
4ab09049 6692read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6693 struct dwo_unit *dwo_unit,
b0c7bfa9 6694 struct die_info *stub_comp_unit_die,
a2ce51a0 6695 const char *stub_comp_dir,
b0c7bfa9 6696 struct die_reader_specs *result_reader,
d521ce57 6697 const gdb_byte **result_info_ptr,
b0c7bfa9 6698 struct die_info **result_comp_unit_die,
685af9cd 6699 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6700{
4ab09049
SM
6701 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6702 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6703 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6704 bfd *abfd;
d521ce57 6705 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6706 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6707 int i,num_extra_attrs;
6708 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6709 struct die_info *comp_unit_die;
6710
b0aeadb3
DE
6711 /* At most one of these may be provided. */
6712 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6713
b0c7bfa9
DE
6714 /* These attributes aren't processed until later:
6715 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6716 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6717 referenced later. However, these attributes are found in the stub
6718 which we won't have later. In order to not impose this complication
6719 on the rest of the code, we read them here and copy them to the
6720 DWO CU/TU die. */
b0c7bfa9
DE
6721
6722 stmt_list = NULL;
6723 low_pc = NULL;
6724 high_pc = NULL;
6725 ranges = NULL;
6726 comp_dir = NULL;
6727
6728 if (stub_comp_unit_die != NULL)
6729 {
6730 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6731 DWO file. */
4ab09049 6732 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6733 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6734 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6735 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6736 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6737 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6738
a39fdb41 6739 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6740
18a8505e
AT
6741 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6742 here (if needed). We need the value before we can process
6743 DW_AT_ranges. */
a39fdb41 6744 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6745 }
a2ce51a0
DE
6746 else if (stub_comp_dir != NULL)
6747 {
6748 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6749 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6750 comp_dir->name = DW_AT_comp_dir;
6751 comp_dir->form = DW_FORM_string;
6752 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6753 DW_STRING (comp_dir) = stub_comp_dir;
6754 }
b0c7bfa9
DE
6755
6756 /* Set up for reading the DWO CU/TU. */
6757 cu->dwo_unit = dwo_unit;
685af9cd 6758 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6759 section->read (objfile);
6760 abfd = section->get_bfd_owner ();
9c541725
PA
6761 begin_info_ptr = info_ptr = (section->buffer
6762 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6763 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6764
4ab09049 6765 if (per_cu->is_debug_types)
b0c7bfa9 6766 {
4ab09049 6767 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6768
ed2dc618
SM
6769 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6770 &cu->header, section,
b0c7bfa9 6771 dwo_abbrev_section,
43988095 6772 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6773 /* This is not an assert because it can be caused by bad debug info. */
43988095 6774 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6775 {
6776 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6777 " TU at offset %s [in module %s]"),
a2ce51a0 6778 hex_string (sig_type->signature),
43988095 6779 hex_string (cu->header.signature),
9d8780f0 6780 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6781 bfd_get_filename (abfd));
6782 }
9c541725 6783 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6784 /* For DWOs coming from DWP files, we don't know the CU length
6785 nor the type's offset in the TU until now. */
4057dfde 6786 dwo_unit->length = cu->header.get_length ();
9c541725 6787 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6788
6789 /* Establish the type offset that can be used to lookup the type.
6790 For DWO files, we don't know it until now. */
9c541725
PA
6791 sig_type->type_offset_in_section
6792 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6793 }
6794 else
6795 {
ed2dc618
SM
6796 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6797 &cu->header, section,
b0c7bfa9 6798 dwo_abbrev_section,
43988095 6799 info_ptr, rcuh_kind::COMPILE);
9c541725 6800 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6801 /* For DWOs coming from DWP files, we don't know the CU length
6802 until now. */
4057dfde 6803 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6804 }
6805
685af9cd 6806 *result_dwo_abbrev_table
86de1d91
TT
6807 = abbrev_table::read (objfile, dwo_abbrev_section,
6808 cu->header.abbrev_sect_off);
685af9cd
TT
6809 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6810 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6811
6812 /* Read in the die, but leave space to copy over the attributes
6813 from the stub. This has the benefit of simplifying the rest of
6814 the code - all the work to maintain the illusion of a single
6815 DW_TAG_{compile,type}_unit DIE is done here. */
6816 num_extra_attrs = ((stmt_list != NULL)
6817 + (low_pc != NULL)
6818 + (high_pc != NULL)
6819 + (ranges != NULL)
6820 + (comp_dir != NULL));
6821 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6822 num_extra_attrs);
b0c7bfa9
DE
6823
6824 /* Copy over the attributes from the stub to the DIE we just read in. */
6825 comp_unit_die = *result_comp_unit_die;
6826 i = comp_unit_die->num_attrs;
6827 if (stmt_list != NULL)
6828 comp_unit_die->attrs[i++] = *stmt_list;
6829 if (low_pc != NULL)
6830 comp_unit_die->attrs[i++] = *low_pc;
6831 if (high_pc != NULL)
6832 comp_unit_die->attrs[i++] = *high_pc;
6833 if (ranges != NULL)
6834 comp_unit_die->attrs[i++] = *ranges;
6835 if (comp_dir != NULL)
6836 comp_unit_die->attrs[i++] = *comp_dir;
6837 comp_unit_die->num_attrs += num_extra_attrs;
6838
b4f54984 6839 if (dwarf_die_debug)
bf6af496
DE
6840 {
6841 fprintf_unfiltered (gdb_stdlog,
6842 "Read die from %s@0x%x of %s:\n",
96b79293 6843 section->get_name (),
bf6af496
DE
6844 (unsigned) (begin_info_ptr - section->buffer),
6845 bfd_get_filename (abfd));
b4f54984 6846 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6847 }
6848
b0c7bfa9
DE
6849 /* Skip dummy compilation units. */
6850 if (info_ptr >= begin_info_ptr + dwo_unit->length
6851 || peek_abbrev_code (abfd, info_ptr) == 0)
6852 return 0;
6853
6854 *result_info_ptr = info_ptr;
6855 return 1;
6856}
6857
a084a2a6
AT
6858/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6859 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6860 signature is part of the header. */
6861static gdb::optional<ULONGEST>
6862lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6863{
6864 if (cu->header.version >= 5)
6865 return cu->header.signature;
6866 struct attribute *attr;
6867 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6868 if (attr == nullptr)
6869 return gdb::optional<ULONGEST> ();
6870 return DW_UNSND (attr);
6871}
6872
c0ab21c2 6873/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6874 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6875 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6876
6877static struct dwo_unit *
4ab09049 6878lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6879{
4ab09049 6880 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6881 struct dwo_unit *dwo_unit;
c0ab21c2 6882 const char *comp_dir;
b0c7bfa9 6883
a2ce51a0
DE
6884 gdb_assert (cu != NULL);
6885
b0c7bfa9 6886 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6887 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6888 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6889
4ab09049
SM
6890 if (per_cu->is_debug_types)
6891 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6892 else
6893 {
a084a2a6 6894 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6895
a084a2a6 6896 if (!signature.has_value ())
b0c7bfa9
DE
6897 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6898 " [in module %s]"),
4ab09049
SM
6899 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6900
6901 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6902 }
6903
b0c7bfa9
DE
6904 return dwo_unit;
6905}
6906
c0ab21c2 6907/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6908 See it for a description of the parameters.
fcd3b13d 6909 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6910
c0ab21c2 6911void
9e021579
SM
6912cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6913 dwarf2_per_objfile *per_objfile,
6751ebae 6914 int use_existing_cu)
a2ce51a0 6915{
a2ce51a0 6916 struct signatured_type *sig_type;
a2ce51a0
DE
6917
6918 /* Verify we can do the following downcast, and that we have the
6919 data we need. */
6920 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6921 sig_type = (struct signatured_type *) this_cu;
6922 gdb_assert (sig_type->dwo_unit != NULL);
6923
6aa5f3a6
DE
6924 if (use_existing_cu && this_cu->cu != NULL)
6925 {
6926 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6927 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6928 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6929 }
6930 else
6931 {
6932 /* If !use_existing_cu, this_cu->cu must be NULL. */
6933 gdb_assert (this_cu->cu == NULL);
9e021579 6934 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6935 }
6936
6937 /* A future optimization, if needed, would be to use an existing
6938 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6939 could share abbrev tables. */
a2ce51a0 6940
4ab09049 6941 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6942 NULL /* stub_comp_unit_die */,
6943 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6944 this, &info_ptr,
3e225074 6945 &comp_unit_die,
c0ab21c2 6946 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6947 {
6948 /* Dummy die. */
c0ab21c2 6949 dummy_p = true;
a2ce51a0 6950 }
a2ce51a0
DE
6951}
6952
fd820528 6953/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6954 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6955
f4dc4d17
DE
6956 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6957 Otherwise the table specified in the comp unit header is read in and used.
6958 This is an optimization for when we already have the abbrev table.
6959
dee91e82 6960 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6961 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6962
ab432490
SM
6963cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6964 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6965 struct abbrev_table *abbrev_table,
6751ebae 6966 int use_existing_cu,
c0ab21c2
TT
6967 bool skip_partial)
6968 : die_reader_specs {},
6751ebae 6969 m_this_cu (this_cu)
c906108c 6970{
dee91e82 6971 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6972 struct dwarf2_section_info *section = this_cu->section;
96b79293 6973 bfd *abfd = section->get_bfd_owner ();
dee91e82 6974 struct dwarf2_cu *cu;
c0ab21c2 6975 const gdb_byte *begin_info_ptr;
dee91e82 6976 struct signatured_type *sig_type = NULL;
4bdcc0c1 6977 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6978 /* Non-zero if CU currently points to a DWO file and we need to
6979 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6980 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6981 int rereading_dwo_cu = 0;
c906108c 6982
b4f54984 6983 if (dwarf_die_debug)
9d8780f0 6984 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6985 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6986 sect_offset_str (this_cu->sect_off));
09406207 6987
a2ce51a0
DE
6988 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6989 file (instead of going through the stub), short-circuit all of this. */
6990 if (this_cu->reading_dwo_directly)
6991 {
6992 /* Narrow down the scope of possibilities to have to understand. */
6993 gdb_assert (this_cu->is_debug_types);
6994 gdb_assert (abbrev_table == NULL);
9e021579 6995 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6996 return;
6997 }
6998
dee91e82 6999 /* This is cheap if the section is already read in. */
96b79293 7000 section->read (objfile);
dee91e82 7001
9c541725 7002 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7003
7004 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7005
7006 if (use_existing_cu && this_cu->cu != NULL)
7007 {
7008 cu = this_cu->cu;
42e7ad6c
DE
7009 /* If this CU is from a DWO file we need to start over, we need to
7010 refetch the attributes from the skeleton CU.
7011 This could be optimized by retrieving those attributes from when we
7012 were here the first time: the previous comp_unit_die was stored in
7013 comp_unit_obstack. But there's no data yet that we need this
7014 optimization. */
7015 if (cu->dwo_unit != NULL)
7016 rereading_dwo_cu = 1;
dee91e82
DE
7017 }
7018 else
7019 {
7020 /* If !use_existing_cu, this_cu->cu must be NULL. */
7021 gdb_assert (this_cu->cu == NULL);
9e021579 7022 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7023 cu = m_new_cu.get ();
42e7ad6c 7024 }
dee91e82 7025
b0c7bfa9 7026 /* Get the header. */
9c541725 7027 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7028 {
7029 /* We already have the header, there's no need to read it in again. */
9c541725 7030 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7031 }
7032 else
7033 {
3019eac3 7034 if (this_cu->is_debug_types)
dee91e82 7035 {
ed2dc618
SM
7036 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7037 &cu->header, section,
4bdcc0c1 7038 abbrev_section, info_ptr,
43988095 7039 rcuh_kind::TYPE);
dee91e82 7040
42e7ad6c
DE
7041 /* Since per_cu is the first member of struct signatured_type,
7042 we can go from a pointer to one to a pointer to the other. */
7043 sig_type = (struct signatured_type *) this_cu;
43988095 7044 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7045 gdb_assert (sig_type->type_offset_in_tu
7046 == cu->header.type_cu_offset_in_tu);
7047 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7048
42e7ad6c
DE
7049 /* LENGTH has not been set yet for type units if we're
7050 using .gdb_index. */
4057dfde 7051 this_cu->length = cu->header.get_length ();
3019eac3
DE
7052
7053 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7054 sig_type->type_offset_in_section =
7055 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7056
7057 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7058 }
7059 else
7060 {
ed2dc618
SM
7061 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7062 &cu->header, section,
4bdcc0c1 7063 abbrev_section,
43988095
JK
7064 info_ptr,
7065 rcuh_kind::COMPILE);
dee91e82 7066
9c541725 7067 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7068 if (this_cu->length == 0)
7069 this_cu->length = cu->header.get_length ();
7070 else
7071 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7072 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7073 }
7074 }
10b3939b 7075
6caca83c 7076 /* Skip dummy compilation units. */
dee91e82 7077 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7078 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7079 {
7080 dummy_p = true;
7081 return;
7082 }
6caca83c 7083
433df2d4
DE
7084 /* If we don't have them yet, read the abbrevs for this compilation unit.
7085 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7086 done. */
f4dc4d17 7087 if (abbrev_table != NULL)
685af9cd
TT
7088 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7089 else
f4dc4d17 7090 {
c0ab21c2 7091 m_abbrev_table_holder
86de1d91
TT
7092 = abbrev_table::read (objfile, abbrev_section,
7093 cu->header.abbrev_sect_off);
c0ab21c2 7094 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7095 }
af703f96 7096
dee91e82 7097 /* Read the top level CU/TU die. */
c0ab21c2 7098 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7099 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7100
58f0c718 7101 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7102 {
7103 dummy_p = true;
7104 return;
7105 }
58f0c718 7106
b0c7bfa9 7107 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7108 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7109 table from the DWO file and pass the ownership over to us. It will be
7110 referenced from READER, so we must make sure to free it after we're done
7111 with READER.
7112
b0c7bfa9
DE
7113 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7114 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7115 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7116 if (dwo_name != nullptr)
3019eac3 7117 {
3019eac3 7118 struct dwo_unit *dwo_unit;
b0c7bfa9 7119 struct die_info *dwo_comp_unit_die;
3019eac3 7120
3e225074 7121 if (comp_unit_die->has_children)
6a506a2d 7122 {
b98664d3 7123 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7124 " has children (offset %s) [in module %s]"),
7125 sect_offset_str (this_cu->sect_off),
7126 bfd_get_filename (abfd));
6a506a2d 7127 }
4ab09049 7128 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7129 if (dwo_unit != NULL)
3019eac3 7130 {
4ab09049 7131 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7132 comp_unit_die, NULL,
c0ab21c2 7133 this, &info_ptr,
3e225074 7134 &dwo_comp_unit_die,
c0ab21c2 7135 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7136 {
7137 /* Dummy die. */
c0ab21c2 7138 dummy_p = true;
6a506a2d
DE
7139 return;
7140 }
7141 comp_unit_die = dwo_comp_unit_die;
7142 }
7143 else
7144 {
7145 /* Yikes, we couldn't find the rest of the DIE, we only have
7146 the stub. A complaint has already been logged. There's
7147 not much more we can do except pass on the stub DIE to
7148 die_reader_func. We don't want to throw an error on bad
7149 debug info. */
3019eac3
DE
7150 }
7151 }
c0ab21c2 7152}
3019eac3 7153
6751ebae
TT
7154void
7155cutu_reader::keep ()
c0ab21c2 7156{
b0c7bfa9 7157 /* Done, clean up. */
6751ebae
TT
7158 gdb_assert (!dummy_p);
7159 if (m_new_cu != NULL)
348e048f 7160 {
e3beb21d
SM
7161 /* We know that m_this_cu->cu is set, since we are in the process of
7162 parsing the CU. */
7163 gdb_assert (m_this_cu->cu != nullptr);
7164 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7165
fcd3b13d 7166 /* Link this CU into read_in_chain. */
5989a64e
SM
7167 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7168 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7169 /* The chain owns it now. */
c0ab21c2 7170 m_new_cu.release ();
348e048f 7171 }
dee91e82
DE
7172}
7173
18a8505e
AT
7174/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7175 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7176 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7177
7178 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7179 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7180
7181 We fill in THIS_CU->length.
7182
dee91e82 7183 THIS_CU->cu is always freed when done.
3019eac3 7184 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7185 to care whether it refers to the "main" CU or the DWO CU.
7186
7187 When parent_cu is passed, it is used to provide a default value for
7188 str_offsets_base and addr_base from the parent. */
dee91e82 7189
ab432490
SM
7190cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7191 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7192 struct dwarf2_cu *parent_cu,
7193 struct dwo_file *dwo_file)
7194 : die_reader_specs {},
7195 m_this_cu (this_cu)
dee91e82
DE
7196{
7197 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7198 struct dwarf2_section_info *section = this_cu->section;
96b79293 7199 bfd *abfd = section->get_bfd_owner ();
33e80786 7200 struct dwarf2_section_info *abbrev_section;
d521ce57 7201 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7202
b4f54984 7203 if (dwarf_die_debug)
9d8780f0 7204 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7205 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7206 sect_offset_str (this_cu->sect_off));
09406207 7207
dee91e82
DE
7208 gdb_assert (this_cu->cu == NULL);
7209
33e80786
DE
7210 abbrev_section = (dwo_file != NULL
7211 ? &dwo_file->sections.abbrev
7212 : get_abbrev_section_for_cu (this_cu));
7213
dee91e82 7214 /* This is cheap if the section is already read in. */
96b79293 7215 section->read (objfile);
dee91e82 7216
9e021579 7217 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7218
9c541725 7219 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7220 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7221 &m_new_cu->header, section,
4bdcc0c1 7222 abbrev_section, info_ptr,
43988095
JK
7223 (this_cu->is_debug_types
7224 ? rcuh_kind::TYPE
7225 : rcuh_kind::COMPILE));
dee91e82 7226
18a8505e
AT
7227 if (parent_cu != nullptr)
7228 {
c0ab21c2
TT
7229 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7230 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7231 }
4057dfde 7232 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7233
7234 /* Skip dummy compilation units. */
7235 if (info_ptr >= begin_info_ptr + this_cu->length
7236 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7237 {
7238 dummy_p = true;
7239 return;
7240 }
72bf9492 7241
c0ab21c2 7242 m_abbrev_table_holder
86de1d91
TT
7243 = abbrev_table::read (objfile, abbrev_section,
7244 m_new_cu->header.abbrev_sect_off);
dee91e82 7245
c0ab21c2
TT
7246 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7247 m_abbrev_table_holder.get ());
3e225074 7248 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7249}
7250
0018ea6f
DE
7251\f
7252/* Type Unit Groups.
dee91e82 7253
0018ea6f
DE
7254 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7255 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7256 so that all types coming from the same compilation (.o file) are grouped
7257 together. A future step could be to put the types in the same symtab as
7258 the CU the types ultimately came from. */
ff013f42 7259
f4dc4d17
DE
7260static hashval_t
7261hash_type_unit_group (const void *item)
7262{
9a3c8263
SM
7263 const struct type_unit_group *tu_group
7264 = (const struct type_unit_group *) item;
f4dc4d17 7265
094b34ac 7266 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7267}
348e048f
DE
7268
7269static int
f4dc4d17 7270eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7271{
9a3c8263
SM
7272 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7273 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7274
094b34ac 7275 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7276}
348e048f 7277
f4dc4d17
DE
7278/* Allocate a hash table for type unit groups. */
7279
eaa5fa8b 7280static htab_up
298e9637 7281allocate_type_unit_groups_table ()
f4dc4d17 7282{
eaa5fa8b
TT
7283 return htab_up (htab_create_alloc (3,
7284 hash_type_unit_group,
7285 eq_type_unit_group,
7286 NULL, xcalloc, xfree));
f4dc4d17 7287}
dee91e82 7288
f4dc4d17
DE
7289/* Type units that don't have DW_AT_stmt_list are grouped into their own
7290 partial symtabs. We combine several TUs per psymtab to not let the size
7291 of any one psymtab grow too big. */
7292#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7293#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7294
094b34ac 7295/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7296 Create the type_unit_group object used to hold one or more TUs. */
7297
7298static struct type_unit_group *
094b34ac 7299create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7300{
5e22e966 7301 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7302 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7303 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7304 struct type_unit_group *tu_group;
f4dc4d17 7305
5989a64e 7306 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7307 struct type_unit_group);
094b34ac 7308 per_cu = &tu_group->per_cu;
518817b3 7309 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 7310 per_cu->per_bfd = per_bfd;
f4dc4d17 7311
1859c670 7312 if (per_bfd->using_index)
094b34ac 7313 {
1859c670 7314 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7315 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7316 }
7317 else
7318 {
9c541725 7319 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7320 dwarf2_psymtab *pst;
528e1572 7321 std::string name;
094b34ac
DE
7322
7323 /* Give the symtab a useful name for debug purposes. */
7324 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7325 name = string_printf ("<type_units_%d>",
7326 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7327 else
528e1572 7328 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7329
7aa104c4 7330 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7331 pst->anonymous = true;
094b34ac 7332 }
f4dc4d17 7333
094b34ac 7334 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7335 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7336
7337 return tu_group;
7338}
7339
094b34ac
DE
7340/* Look up the type_unit_group for type unit CU, and create it if necessary.
7341 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7342
7343static struct type_unit_group *
ff39bb5e 7344get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7345{
5e22e966 7346 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7347 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7348 struct type_unit_group *tu_group;
7349 void **slot;
7350 unsigned int line_offset;
7351 struct type_unit_group type_unit_group_for_lookup;
7352
5989a64e
SM
7353 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7354 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7355
7356 /* Do we need to create a new group, or can we use an existing one? */
7357
7358 if (stmt_list)
7359 {
7360 line_offset = DW_UNSND (stmt_list);
7361 ++tu_stats->nr_symtab_sharers;
7362 }
7363 else
7364 {
7365 /* Ugh, no stmt_list. Rare, but we have to handle it.
7366 We can do various things here like create one group per TU or
7367 spread them over multiple groups to split up the expansion work.
7368 To avoid worst case scenarios (too many groups or too large groups)
7369 we, umm, group them in bunches. */
7370 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7371 | (tu_stats->nr_stmt_less_type_units
7372 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7373 ++tu_stats->nr_stmt_less_type_units;
7374 }
7375
094b34ac 7376 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7377 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7378 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7379 &type_unit_group_for_lookup, INSERT);
7380 if (*slot != NULL)
7381 {
9a3c8263 7382 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7383 gdb_assert (tu_group != NULL);
7384 }
7385 else
7386 {
9c541725 7387 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7388 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7389 *slot = tu_group;
7390 ++tu_stats->nr_symtabs;
7391 }
7392
7393 return tu_group;
7394}
0018ea6f
DE
7395\f
7396/* Partial symbol tables. */
7397
7398/* Create a psymtab named NAME and assign it to PER_CU.
7399
7400 The caller must fill in the following details:
7401 dirname, textlow, texthigh. */
7402
891813be 7403static dwarf2_psymtab *
7aa104c4
SM
7404create_partial_symtab (dwarf2_per_cu_data *per_cu,
7405 dwarf2_per_objfile *per_objfile,
7406 const char *name)
0018ea6f 7407{
7aa104c4 7408 struct objfile *objfile = per_objfile->objfile;
891813be 7409 dwarf2_psymtab *pst;
0018ea6f 7410
9f4e76a4 7411 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7412
6d94535f 7413 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7414
7415 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7416 per_cu->v.psymtab = pst;
7417
7418 return pst;
7419}
7420
c0ab21c2 7421/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7422
7423static void
7424process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7425 const gdb_byte *info_ptr,
0018ea6f 7426 struct die_info *comp_unit_die,
c0ab21c2 7427 enum language pretend_language)
0018ea6f
DE
7428{
7429 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7431 struct objfile *objfile = per_objfile->objfile;
08feed99 7432 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7433 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7434 CORE_ADDR baseaddr;
7435 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7436 dwarf2_psymtab *pst;
3a2b436a 7437 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7438 const char *filename;
0018ea6f 7439
0018ea6f
DE
7440 gdb_assert (! per_cu->is_debug_types);
7441
c0ab21c2 7442 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7443
0018ea6f 7444 /* Allocate a new partial symbol table structure. */
2e927613
TV
7445 gdb::unique_xmalloc_ptr<char> debug_filename;
7446 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7447 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7448 if (filename == NULL)
0018ea6f 7449 filename = "";
2e927613
TV
7450 else if (strcmp (filename, artificial) == 0)
7451 {
7452 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7453 sect_offset_str (per_cu->sect_off),
7454 (char *) NULL));
2e927613
TV
7455 filename = debug_filename.get ();
7456 }
0018ea6f 7457
7aa104c4 7458 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7459
7460 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7461 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7462
b3b3bada 7463 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7464
7465 dwarf2_find_base_address (comp_unit_die, cu);
7466
7467 /* Possibly set the default values of LOWPC and HIGHPC from
7468 `DW_AT_ranges'. */
3a2b436a
JK
7469 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7470 &best_highpc, cu, pst);
7471 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7472 {
7473 CORE_ADDR low
7474 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7475 - baseaddr);
7476 CORE_ADDR high
7477 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7478 - baseaddr - 1);
7479 /* Store the contiguous range if it is not empty; it can be
7480 empty for CUs with no code. */
d320c2b5
TT
7481 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7482 low, high, pst);
79748972 7483 }
0018ea6f
DE
7484
7485 /* Check if comp unit has_children.
7486 If so, read the rest of the partial symbols from this comp unit.
7487 If not, there's no more debug_info for this comp unit. */
3e225074 7488 if (comp_unit_die->has_children)
0018ea6f
DE
7489 {
7490 struct partial_die_info *first_die;
7491 CORE_ADDR lowpc, highpc;
7492
7493 lowpc = ((CORE_ADDR) -1);
7494 highpc = ((CORE_ADDR) 0);
7495
7496 first_die = load_partial_dies (reader, info_ptr, 1);
7497
7498 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7499 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7500
7501 /* If we didn't find a lowpc, set it to highpc to avoid
7502 complaints from `maint check'. */
7503 if (lowpc == ((CORE_ADDR) -1))
7504 lowpc = highpc;
7505
7506 /* If the compilation unit didn't have an explicit address range,
7507 then use the information extracted from its child dies. */
e385593e 7508 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7509 {
7510 best_lowpc = lowpc;
7511 best_highpc = highpc;
7512 }
7513 }
4ae976d1 7514 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7515 best_lowpc + baseaddr)
7516 - baseaddr);
4ae976d1 7517 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7518 best_highpc + baseaddr)
7519 - baseaddr);
0018ea6f 7520
8763cede 7521 end_psymtab_common (objfile, pst);
0018ea6f 7522
ae640021 7523 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7524 {
7525 int i;
ae640021 7526 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7527
7528 /* Fill in 'dependencies' here; we fill in 'users' in a
7529 post-pass. */
7530 pst->number_of_dependencies = len;
a9342b62
TT
7531 pst->dependencies
7532 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7533 for (i = 0; i < len; ++i)
7534 {
7535 pst->dependencies[i]
7536 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7537 }
0018ea6f 7538
ae640021 7539 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7540 }
7541
7542 /* Get the list of files included in the current compilation unit,
7543 and build a psymtab for each of them. */
7544 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7545
b4f54984 7546 if (dwarf_read_debug)
b926417a
TT
7547 fprintf_unfiltered (gdb_stdlog,
7548 "Psymtab for %s unit @%s: %s - %s"
7549 ", %d global, %d static syms\n",
7550 per_cu->is_debug_types ? "type" : "comp",
7551 sect_offset_str (per_cu->sect_off),
7552 paddress (gdbarch, pst->text_low (objfile)),
7553 paddress (gdbarch, pst->text_high (objfile)),
7554 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7555}
7556
7557/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7558 Process compilation unit THIS_CU for a psymtab. */
7559
7560static void
ab432490
SM
7561process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7562 dwarf2_per_objfile *per_objfile,
135f5437 7563 bool want_partial_unit,
b93601f3 7564 enum language pretend_language)
0018ea6f
DE
7565{
7566 /* If this compilation unit was already read in, free the
7567 cached copy in order to read it in again. This is
7568 necessary because we skipped some symbols when we first
7569 read in the compilation unit (see load_partial_dies).
7570 This problem could be avoided, but the benefit is unclear. */
7571 if (this_cu->cu != NULL)
7572 free_one_cached_comp_unit (this_cu);
7573
ab432490 7574 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7575
58990295
TV
7576 switch (reader.comp_unit_die->tag)
7577 {
7578 case DW_TAG_compile_unit:
7579 this_cu->unit_type = DW_UT_compile;
7580 break;
7581 case DW_TAG_partial_unit:
7582 this_cu->unit_type = DW_UT_partial;
7583 break;
7584 default:
7585 abort ();
7586 }
7587
c0ab21c2 7588 if (reader.dummy_p)
f1902523 7589 {
c0ab21c2 7590 /* Nothing. */
f1902523 7591 }
c0ab21c2 7592 else if (this_cu->is_debug_types)
3e225074
TT
7593 build_type_psymtabs_reader (&reader, reader.info_ptr,
7594 reader.comp_unit_die);
135f5437
TT
7595 else if (want_partial_unit
7596 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7597 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7598 reader.comp_unit_die,
c0ab21c2 7599 pretend_language);
0018ea6f 7600
58990295
TV
7601 this_cu->lang = this_cu->cu->language;
7602
0018ea6f 7603 /* Age out any secondary CUs. */
ed2dc618 7604 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7605}
f4dc4d17
DE
7606
7607/* Reader function for build_type_psymtabs. */
7608
7609static void
7610build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7611 const gdb_byte *info_ptr,
3e225074 7612 struct die_info *type_unit_die)
f4dc4d17 7613{
5e22e966 7614 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7615 struct objfile *objfile = dwarf2_per_objfile->objfile;
7616 struct dwarf2_cu *cu = reader->cu;
7617 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7618 struct signatured_type *sig_type;
f4dc4d17
DE
7619 struct type_unit_group *tu_group;
7620 struct attribute *attr;
7621 struct partial_die_info *first_die;
7622 CORE_ADDR lowpc, highpc;
891813be 7623 dwarf2_psymtab *pst;
f4dc4d17 7624
0186c6a7
DE
7625 gdb_assert (per_cu->is_debug_types);
7626 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7627
3e225074 7628 if (! type_unit_die->has_children)
f4dc4d17
DE
7629 return;
7630
052c8bb8 7631 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7632 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7633
df07e2c7 7634 if (tu_group->tus == nullptr)
a8b3b8e9 7635 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7636 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7637
7638 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7639 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7640 pst->anonymous = true;
f4dc4d17
DE
7641
7642 first_die = load_partial_dies (reader, info_ptr, 1);
7643
7644 lowpc = (CORE_ADDR) -1;
7645 highpc = (CORE_ADDR) 0;
7646 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7647
8763cede 7648 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7649}
7650
73051182
DE
7651/* Struct used to sort TUs by their abbreviation table offset. */
7652
7653struct tu_abbrev_offset
7654{
b2bdb8cf
SM
7655 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7656 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7657 {}
7658
7659 signatured_type *sig_type;
73051182
DE
7660 sect_offset abbrev_offset;
7661};
7662
484cf504 7663/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7664
484cf504
TT
7665static bool
7666sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7667 const struct tu_abbrev_offset &b)
73051182 7668{
484cf504 7669 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7670}
7671
7672/* Efficiently read all the type units.
7673 This does the bulk of the work for build_type_psymtabs.
7674
7675 The efficiency is because we sort TUs by the abbrev table they use and
7676 only read each abbrev table once. In one program there are 200K TUs
7677 sharing 8K abbrev tables.
7678
7679 The main purpose of this function is to support building the
5989a64e 7680 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7681 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7682 can collapse the search space by grouping them by stmt_list.
7683 The savings can be significant, in the same program from above the 200K TUs
7684 share 8K stmt_list tables.
7685
7686 FUNC is expected to call get_type_unit_group, which will create the
7687 struct type_unit_group if necessary and add it to
5989a64e 7688 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7689
7690static void
ed2dc618 7691build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7692{
5989a64e 7693 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7694 abbrev_table_up abbrev_table;
73051182 7695 sect_offset abbrev_offset;
73051182
DE
7696
7697 /* It's up to the caller to not call us multiple times. */
5989a64e 7698 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7699
5989a64e 7700 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7701 return;
7702
7703 /* TUs typically share abbrev tables, and there can be way more TUs than
7704 abbrev tables. Sort by abbrev table to reduce the number of times we
7705 read each abbrev table in.
7706 Alternatives are to punt or to maintain a cache of abbrev tables.
7707 This is simpler and efficient enough for now.
7708
7709 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7710 symtab to use). Typically TUs with the same abbrev offset have the same
7711 stmt_list value too so in practice this should work well.
7712
7713 The basic algorithm here is:
7714
7715 sort TUs by abbrev table
7716 for each TU with same abbrev table:
7717 read abbrev table if first user
7718 read TU top level DIE
7719 [IWBN if DWO skeletons had DW_AT_stmt_list]
7720 call FUNC */
7721
b4f54984 7722 if (dwarf_read_debug)
73051182
DE
7723 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7724
7725 /* Sort in a separate table to maintain the order of all_type_units
7726 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7727 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7728 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7729
5989a64e 7730 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7731 sorted_by_abbrev.emplace_back
7732 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7733 sig_type->per_cu.section,
7734 sig_type->per_cu.sect_off));
73051182 7735
484cf504
TT
7736 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7737 sort_tu_by_abbrev_offset);
73051182 7738
9c541725 7739 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7740
b2bdb8cf 7741 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7742 {
73051182
DE
7743 /* Switch to the next abbrev table if necessary. */
7744 if (abbrev_table == NULL
b2bdb8cf 7745 || tu.abbrev_offset != abbrev_offset)
73051182 7746 {
b2bdb8cf 7747 abbrev_offset = tu.abbrev_offset;
73051182 7748 abbrev_table =
86de1d91 7749 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7750 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7751 abbrev_offset);
73051182
DE
7752 ++tu_stats->nr_uniq_abbrev_tables;
7753 }
7754
ab432490
SM
7755 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7756 abbrev_table.get (), 0, false);
c0ab21c2
TT
7757 if (!reader.dummy_p)
7758 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7759 reader.comp_unit_die);
73051182 7760 }
6aa5f3a6 7761}
73051182 7762
6aa5f3a6
DE
7763/* Print collected type unit statistics. */
7764
7765static void
ed2dc618 7766print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7767{
5989a64e 7768 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7769
7770 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7771 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7772 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7773 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7774 tu_stats->nr_uniq_abbrev_tables);
7775 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7776 tu_stats->nr_symtabs);
7777 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7778 tu_stats->nr_symtab_sharers);
7779 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7780 tu_stats->nr_stmt_less_type_units);
7781 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7782 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7783}
7784
f4dc4d17
DE
7785/* Traversal function for build_type_psymtabs. */
7786
7787static int
7788build_type_psymtab_dependencies (void **slot, void *info)
7789{
ed2dc618
SM
7790 struct dwarf2_per_objfile *dwarf2_per_objfile
7791 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7792 struct objfile *objfile = dwarf2_per_objfile->objfile;
7793 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7794 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7795 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7796 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7797 int i;
7798
7799 gdb_assert (len > 0);
197400e8 7800 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7801
7802 pst->number_of_dependencies = len;
a9342b62 7803 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7804 for (i = 0; i < len; ++i)
f4dc4d17 7805 {
df07e2c7 7806 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7807 gdb_assert (iter->per_cu.is_debug_types);
7808 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7809 iter->type_unit_group = tu_group;
f4dc4d17
DE
7810 }
7811
df07e2c7
AB
7812 delete tu_group->tus;
7813 tu_group->tus = nullptr;
348e048f
DE
7814
7815 return 1;
7816}
7817
7818/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7819 Build partial symbol tables for the .debug_types comp-units. */
7820
7821static void
ed2dc618 7822build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7823{
ed2dc618 7824 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7825 return;
7826
ed2dc618 7827 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7828}
f4dc4d17 7829
6aa5f3a6
DE
7830/* Traversal function for process_skeletonless_type_unit.
7831 Read a TU in a DWO file and build partial symbols for it. */
7832
7833static int
7834process_skeletonless_type_unit (void **slot, void *info)
7835{
7836 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7837 struct dwarf2_per_objfile *dwarf2_per_objfile
7838 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7839 struct signatured_type find_entry, *entry;
7840
7841 /* If this TU doesn't exist in the global table, add it and read it in. */
7842
5989a64e
SM
7843 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7844 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7845
7846 find_entry.signature = dwo_unit->signature;
5989a64e 7847 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7848 &find_entry, INSERT);
6aa5f3a6
DE
7849 /* If we've already seen this type there's nothing to do. What's happening
7850 is we're doing our own version of comdat-folding here. */
7851 if (*slot != NULL)
7852 return 1;
7853
7854 /* This does the job that create_all_type_units would have done for
7855 this TU. */
ed2dc618
SM
7856 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7857 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7858 *slot = entry;
7859
7860 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7861 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7862 if (!reader.dummy_p)
7863 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7864 reader.comp_unit_die);
6aa5f3a6
DE
7865
7866 return 1;
7867}
7868
7869/* Traversal function for process_skeletonless_type_units. */
7870
7871static int
7872process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7873{
7874 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7875
7876 if (dwo_file->tus != NULL)
b0b6a987
TT
7877 htab_traverse_noresize (dwo_file->tus.get (),
7878 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7879
7880 return 1;
7881}
7882
7883/* Scan all TUs of DWO files, verifying we've processed them.
7884 This is needed in case a TU was emitted without its skeleton.
7885 Note: This can't be done until we know what all the DWO files are. */
7886
7887static void
ed2dc618 7888process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7889{
7890 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7891 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7892 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7893 {
5989a64e 7894 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7895 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7896 dwarf2_per_objfile);
6aa5f3a6 7897 }
348e048f
DE
7898}
7899
ed2dc618 7900/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7901
7902static void
ed2dc618 7903set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7904{
5989a64e 7905 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7906 {
891813be 7907 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7908
36586728
TT
7909 if (pst == NULL)
7910 continue;
7911
b76e467d 7912 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7913 {
7914 /* Set the 'user' field only if it is not already set. */
7915 if (pst->dependencies[j]->user == NULL)
7916 pst->dependencies[j]->user = pst;
7917 }
7918 }
7919}
7920
93311388
DE
7921/* Build the partial symbol table by doing a quick pass through the
7922 .debug_info and .debug_abbrev sections. */
72bf9492 7923
93311388 7924static void
ed2dc618 7925dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7926{
ed2dc618 7927 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7928
b4f54984 7929 if (dwarf_read_debug)
45cfd468
DE
7930 {
7931 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7932 objfile_name (objfile));
45cfd468
DE
7933 }
7934
76935768 7935 scoped_restore restore_reading_psyms
5989a64e 7936 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7937 true);
98bfdba5 7938
5989a64e 7939 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7940
93311388
DE
7941 /* Any cached compilation units will be linked by the per-objfile
7942 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7943 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7944
ed2dc618 7945 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7946
ed2dc618 7947 create_all_comp_units (dwarf2_per_objfile);
c906108c 7948
60606b2c
TT
7949 /* Create a temporary address map on a temporary obstack. We later
7950 copy this to the final obstack. */
8268c778 7951 auto_obstack temp_obstack;
791afaa2
TT
7952
7953 scoped_restore save_psymtabs_addrmap
d320c2b5 7954 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7955 addrmap_create_mutable (&temp_obstack));
72bf9492 7956
5989a64e 7957 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7958 {
7959 if (per_cu->v.psymtab != NULL)
7960 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7961 continue;
ab432490
SM
7962 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7963 language_minimal);
3d5afab3 7964 }
ff013f42 7965
6aa5f3a6 7966 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7967 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7968
7969 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7970 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7971 {
5989a64e 7972 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7973 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7974 }
7975
b4f54984 7976 if (dwarf_read_debug)
ed2dc618 7977 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7978
ed2dc618 7979 set_partial_user (dwarf2_per_objfile);
95554aad 7980
d320c2b5
TT
7981 objfile->partial_symtabs->psymtabs_addrmap
7982 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7983 objfile->partial_symtabs->obstack ());
791afaa2
TT
7984 /* At this point we want to keep the address map. */
7985 save_psymtabs_addrmap.release ();
ff013f42 7986
b4f54984 7987 if (dwarf_read_debug)
45cfd468 7988 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7989 objfile_name (objfile));
ae038cb0
DJ
7990}
7991
dee91e82
DE
7992/* Load the partial DIEs for a secondary CU into memory.
7993 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7994
dee91e82 7995static void
ab432490
SM
7996load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7997 dwarf2_per_objfile *per_objfile)
dee91e82 7998{
ab432490 7999 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
8000
8001 if (!reader.dummy_p)
8002 {
8003 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8004 language_minimal);
8005
8006 /* Check if comp unit has_children.
8007 If so, read the rest of the partial symbols from this comp unit.
8008 If not, there's no more debug_info for this comp unit. */
3e225074 8009 if (reader.comp_unit_die->has_children)
c0ab21c2 8010 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8011
8012 reader.keep ();
c0ab21c2 8013 }
ae038cb0
DJ
8014}
8015
ae038cb0 8016static void
ed2dc618 8017read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8018 struct dwarf2_section_info *section,
f1902523 8019 struct dwarf2_section_info *abbrev_section,
b76e467d 8020 unsigned int is_dwz)
ae038cb0 8021{
d521ce57 8022 const gdb_byte *info_ptr;
ed2dc618 8023 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8024
b4f54984 8025 if (dwarf_read_debug)
bf6af496 8026 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8027 section->get_name (),
8028 section->get_file_name ());
bf6af496 8029
96b79293 8030 section->read (objfile);
ae038cb0 8031
36586728 8032 info_ptr = section->buffer;
6e70227d 8033
36586728 8034 while (info_ptr < section->buffer + section->size)
ae038cb0 8035 {
ae038cb0 8036 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8037
9c541725 8038 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8039
f1902523 8040 comp_unit_head cu_header;
ed2dc618
SM
8041 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8042 abbrev_section, info_ptr,
8043 rcuh_kind::COMPILE);
ae038cb0
DJ
8044
8045 /* Save the compilation unit for later lookup. */
f1902523 8046 if (cu_header.unit_type != DW_UT_type)
5989a64e 8047 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8048 else
8049 {
5989a64e 8050 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8051 sig_type->signature = cu_header.signature;
8052 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8053 this_cu = &sig_type->per_cu;
8054 }
8055 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8056 this_cu->sect_off = sect_off;
f1902523 8057 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8058 this_cu->is_dwz = is_dwz;
e3b94546 8059 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8060 this_cu->section = section;
ae038cb0 8061
5989a64e 8062 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8063
8064 info_ptr = info_ptr + this_cu->length;
8065 }
36586728
TT
8066}
8067
8068/* Create a list of all compilation units in OBJFILE.
8069 This is only done for -readnow and building partial symtabs. */
8070
8071static void
ed2dc618 8072create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8073{
5989a64e
SM
8074 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8075 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8076 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8077
c3699833 8078 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8079 if (dwz != NULL)
ed2dc618 8080 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8081 1);
c906108c
SS
8082}
8083
5734ee8b 8084/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8085 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8086 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8087 DW_AT_ranges). See the comments of add_partial_subprogram on how
8088 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8089
72bf9492
DJ
8090static void
8091scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8092 CORE_ADDR *highpc, int set_addrmap,
8093 struct dwarf2_cu *cu)
c906108c 8094{
72bf9492 8095 struct partial_die_info *pdi;
c906108c 8096
91c24f0a
DC
8097 /* Now, march along the PDI's, descending into ones which have
8098 interesting children but skipping the children of the other ones,
8099 until we reach the end of the compilation unit. */
c906108c 8100
72bf9492 8101 pdi = first_die;
91c24f0a 8102
72bf9492
DJ
8103 while (pdi != NULL)
8104 {
52356b79 8105 pdi->fixup (cu);
c906108c 8106
f55ee35c 8107 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8108 children, so we need to look at them. Ditto for anonymous
8109 enums. */
933c6fe4 8110
72bf9492 8111 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8112 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8113 || pdi->tag == DW_TAG_imported_unit
8114 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8115 {
72bf9492 8116 switch (pdi->tag)
c906108c
SS
8117 {
8118 case DW_TAG_subprogram:
b1dc1806 8119 case DW_TAG_inlined_subroutine:
cdc07690 8120 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8121 break;
72929c62 8122 case DW_TAG_constant:
c906108c
SS
8123 case DW_TAG_variable:
8124 case DW_TAG_typedef:
91c24f0a 8125 case DW_TAG_union_type:
317d2668
TV
8126 if (!pdi->is_declaration
8127 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8128 {
72bf9492 8129 add_partial_symbol (pdi, cu);
63d06c5c
DC
8130 }
8131 break;
c906108c 8132 case DW_TAG_class_type:
680b30c7 8133 case DW_TAG_interface_type:
c906108c 8134 case DW_TAG_structure_type:
72bf9492 8135 if (!pdi->is_declaration)
c906108c 8136 {
72bf9492 8137 add_partial_symbol (pdi, cu);
c906108c 8138 }
b7fee5a3
KS
8139 if ((cu->language == language_rust
8140 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8141 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8142 set_addrmap, cu);
c906108c 8143 break;
91c24f0a 8144 case DW_TAG_enumeration_type:
72bf9492
DJ
8145 if (!pdi->is_declaration)
8146 add_partial_enumeration (pdi, cu);
c906108c
SS
8147 break;
8148 case DW_TAG_base_type:
a02abb62 8149 case DW_TAG_subrange_type:
c906108c 8150 /* File scope base type definitions are added to the partial
c5aa993b 8151 symbol table. */
72bf9492 8152 add_partial_symbol (pdi, cu);
c906108c 8153 break;
d9fa45fe 8154 case DW_TAG_namespace:
cdc07690 8155 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8156 break;
5d7cb8df 8157 case DW_TAG_module:
59c35742
AB
8158 if (!pdi->is_declaration)
8159 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8160 break;
95554aad
TT
8161 case DW_TAG_imported_unit:
8162 {
8163 struct dwarf2_per_cu_data *per_cu;
8164
f4dc4d17
DE
8165 /* For now we don't handle imported units in type units. */
8166 if (cu->per_cu->is_debug_types)
8167 {
8168 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8169 " supported in type units [in module %s]"),
5e22e966 8170 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8171 }
8172
e3b94546 8173 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8174 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8175
8176 /* Go read the partial unit, if needed. */
8177 if (per_cu->v.psymtab == NULL)
ab432490
SM
8178 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8179 cu->language);
95554aad 8180
ae640021 8181 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8182 }
8183 break;
74921315
KS
8184 case DW_TAG_imported_declaration:
8185 add_partial_symbol (pdi, cu);
8186 break;
c906108c
SS
8187 default:
8188 break;
8189 }
8190 }
8191
72bf9492
DJ
8192 /* If the die has a sibling, skip to the sibling. */
8193
8194 pdi = pdi->die_sibling;
8195 }
8196}
8197
8198/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8199
72bf9492 8200 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8201 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8202 Enumerators are an exception; they use the scope of their parent
8203 enumeration type, i.e. the name of the enumeration type is not
8204 prepended to the enumerator.
91c24f0a 8205
72bf9492
DJ
8206 There are two complexities. One is DW_AT_specification; in this
8207 case "parent" means the parent of the target of the specification,
8208 instead of the direct parent of the DIE. The other is compilers
8209 which do not emit DW_TAG_namespace; in this case we try to guess
8210 the fully qualified name of structure types from their members'
8211 linkage names. This must be done using the DIE's children rather
8212 than the children of any DW_AT_specification target. We only need
8213 to do this for structures at the top level, i.e. if the target of
8214 any DW_AT_specification (if any; otherwise the DIE itself) does not
8215 have a parent. */
8216
8217/* Compute the scope prefix associated with PDI's parent, in
8218 compilation unit CU. The result will be allocated on CU's
8219 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8220 field. NULL is returned if no prefix is necessary. */
15d034d0 8221static const char *
72bf9492
DJ
8222partial_die_parent_scope (struct partial_die_info *pdi,
8223 struct dwarf2_cu *cu)
8224{
15d034d0 8225 const char *grandparent_scope;
72bf9492 8226 struct partial_die_info *parent, *real_pdi;
91c24f0a 8227
72bf9492
DJ
8228 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8229 then this means the parent of the specification DIE. */
8230
8231 real_pdi = pdi;
72bf9492 8232 while (real_pdi->has_specification)
fb816e8b 8233 {
122cf0f2
AB
8234 auto res = find_partial_die (real_pdi->spec_offset,
8235 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8236 real_pdi = res.pdi;
8237 cu = res.cu;
8238 }
72bf9492
DJ
8239
8240 parent = real_pdi->die_parent;
8241 if (parent == NULL)
8242 return NULL;
8243
8244 if (parent->scope_set)
8245 return parent->scope;
8246
52356b79 8247 parent->fixup (cu);
72bf9492 8248
10b3939b 8249 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8250
acebe513
UW
8251 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8252 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8253 Work around this problem here. */
8254 if (cu->language == language_cplus
6e70227d 8255 && parent->tag == DW_TAG_namespace
acebe513
UW
8256 && strcmp (parent->name, "::") == 0
8257 && grandparent_scope == NULL)
8258 {
8259 parent->scope = NULL;
8260 parent->scope_set = 1;
8261 return NULL;
8262 }
8263
0a4b0913 8264 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8265 if (pdi->tag == DW_TAG_enumerator)
8266 /* Enumerators should not get the name of the enumeration as a prefix. */
8267 parent->scope = grandparent_scope;
8268 else if (parent->tag == DW_TAG_namespace
f55ee35c 8269 || parent->tag == DW_TAG_module
72bf9492
DJ
8270 || parent->tag == DW_TAG_structure_type
8271 || parent->tag == DW_TAG_class_type
680b30c7 8272 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8273 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8274 || parent->tag == DW_TAG_enumeration_type
8275 || (cu->language == language_fortran
8276 && parent->tag == DW_TAG_subprogram
8277 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8278 {
8279 if (grandparent_scope == NULL)
8280 parent->scope = parent->name;
8281 else
3e43a32a
MS
8282 parent->scope = typename_concat (&cu->comp_unit_obstack,
8283 grandparent_scope,
f55ee35c 8284 parent->name, 0, cu);
72bf9492 8285 }
72bf9492
DJ
8286 else
8287 {
8288 /* FIXME drow/2004-04-01: What should we be doing with
8289 function-local names? For partial symbols, we should probably be
8290 ignoring them. */
fa9c3fa0
TT
8291 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8292 dwarf_tag_name (parent->tag),
8293 sect_offset_str (pdi->sect_off));
72bf9492 8294 parent->scope = grandparent_scope;
c906108c
SS
8295 }
8296
72bf9492
DJ
8297 parent->scope_set = 1;
8298 return parent->scope;
8299}
8300
8301/* Return the fully scoped name associated with PDI, from compilation unit
8302 CU. The result will be allocated with malloc. */
4568ecf9 8303
43816ebc 8304static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8305partial_die_full_name (struct partial_die_info *pdi,
8306 struct dwarf2_cu *cu)
8307{
15d034d0 8308 const char *parent_scope;
72bf9492 8309
98bfdba5
PA
8310 /* If this is a template instantiation, we can not work out the
8311 template arguments from partial DIEs. So, unfortunately, we have
8312 to go through the full DIEs. At least any work we do building
8313 types here will be reused if full symbols are loaded later. */
8314 if (pdi->has_template_arguments)
8315 {
52356b79 8316 pdi->fixup (cu);
98bfdba5
PA
8317
8318 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8319 {
8320 struct die_info *die;
8321 struct attribute attr;
8322 struct dwarf2_cu *ref_cu = cu;
8323
b64f50a1 8324 /* DW_FORM_ref_addr is using section offset. */
b4069958 8325 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8326 attr.form = DW_FORM_ref_addr;
9c541725 8327 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8328 die = follow_die_ref (NULL, &attr, &ref_cu);
8329
43816ebc 8330 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8331 }
8332 }
8333
72bf9492
DJ
8334 parent_scope = partial_die_parent_scope (pdi, cu);
8335 if (parent_scope == NULL)
8336 return NULL;
8337 else
43816ebc
TT
8338 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8339 pdi->name, 0, cu));
c906108c
SS
8340}
8341
8342static void
72bf9492 8343add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8344{
5e22e966 8345 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8346 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8347 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8348 CORE_ADDR addr = 0;
15d034d0 8349 const char *actual_name = NULL;
e142c38c
DJ
8350 CORE_ADDR baseaddr;
8351
b3b3bada 8352 baseaddr = objfile->text_section_offset ();
c906108c 8353
43816ebc
TT
8354 gdb::unique_xmalloc_ptr<char> built_actual_name
8355 = partial_die_full_name (pdi, cu);
15d034d0 8356 if (built_actual_name != NULL)
43816ebc 8357 actual_name = built_actual_name.get ();
63d06c5c 8358
72bf9492
DJ
8359 if (actual_name == NULL)
8360 actual_name = pdi->name;
8361
76e288d1
TT
8362 partial_symbol psymbol;
8363 memset (&psymbol, 0, sizeof (psymbol));
8364 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8365 psymbol.ginfo.section = -1;
8366
8367 /* The code below indicates that the psymbol should be installed by
8368 setting this. */
8369 gdb::optional<psymbol_placement> where;
8370
c906108c
SS
8371 switch (pdi->tag)
8372 {
b1dc1806 8373 case DW_TAG_inlined_subroutine:
c906108c 8374 case DW_TAG_subprogram:
79748972
TT
8375 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8376 - baseaddr);
0a4b0913
AB
8377 if (pdi->is_external
8378 || cu->language == language_ada
8379 || (cu->language == language_fortran
8380 && pdi->die_parent != NULL
8381 && pdi->die_parent->tag == DW_TAG_subprogram))
8382 {
8383 /* Normally, only "external" DIEs are part of the global scope.
8384 But in Ada and Fortran, we want to be able to access nested
8385 procedures globally. So all Ada and Fortran subprograms are
8386 stored in the global scope. */
76e288d1 8387 where = psymbol_placement::GLOBAL;
c906108c
SS
8388 }
8389 else
76e288d1
TT
8390 where = psymbol_placement::STATIC;
8391
8392 psymbol.domain = VAR_DOMAIN;
8393 psymbol.aclass = LOC_BLOCK;
8394 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8395 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8396
8397 if (pdi->main_subprogram && actual_name != NULL)
8398 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8399 break;
72929c62 8400 case DW_TAG_constant:
76e288d1
TT
8401 psymbol.domain = VAR_DOMAIN;
8402 psymbol.aclass = LOC_STATIC;
8403 where = (pdi->is_external
8404 ? psymbol_placement::GLOBAL
8405 : psymbol_placement::STATIC);
72929c62 8406 break;
c906108c 8407 case DW_TAG_variable:
95554aad
TT
8408 if (pdi->d.locdesc)
8409 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8410
95554aad 8411 if (pdi->d.locdesc
caac4577 8412 && addr == 0
5989a64e 8413 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8414 {
8415 /* A global or static variable may also have been stripped
8416 out by the linker if unused, in which case its address
8417 will be nullified; do not add such variables into partial
8418 symbol table then. */
8419 }
8420 else if (pdi->is_external)
c906108c
SS
8421 {
8422 /* Global Variable.
8423 Don't enter into the minimal symbol tables as there is
8424 a minimal symbol table entry from the ELF symbols already.
8425 Enter into partial symbol table if it has a location
8426 descriptor or a type.
8427 If the location descriptor is missing, new_symbol will create
8428 a LOC_UNRESOLVED symbol, the address of the variable will then
8429 be determined from the minimal symbol table whenever the variable
8430 is referenced.
8431 The address for the partial symbol table entry is not
8432 used by GDB, but it comes in handy for debugging partial symbol
8433 table building. */
8434
95554aad 8435 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8436 {
8437 psymbol.domain = VAR_DOMAIN;
8438 psymbol.aclass = LOC_STATIC;
8439 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8440 psymbol.ginfo.value.address = addr;
8441 where = psymbol_placement::GLOBAL;
8442 }
c906108c
SS
8443 }
8444 else
8445 {
ff908ebf
AW
8446 int has_loc = pdi->d.locdesc != NULL;
8447
8448 /* Static Variable. Skip symbols whose value we cannot know (those
8449 without location descriptors or constant values). */
8450 if (!has_loc && !pdi->has_const_value)
43816ebc 8451 return;
ff908ebf 8452
76e288d1
TT
8453 psymbol.domain = VAR_DOMAIN;
8454 psymbol.aclass = LOC_STATIC;
8455 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8456 if (has_loc)
8457 psymbol.ginfo.value.address = addr;
8458 where = psymbol_placement::STATIC;
c906108c
SS
8459 }
8460 break;
8461 case DW_TAG_typedef:
8462 case DW_TAG_base_type:
a02abb62 8463 case DW_TAG_subrange_type:
76e288d1
TT
8464 psymbol.domain = VAR_DOMAIN;
8465 psymbol.aclass = LOC_TYPEDEF;
8466 where = psymbol_placement::STATIC;
c906108c 8467 break;
74921315 8468 case DW_TAG_imported_declaration:
72bf9492 8469 case DW_TAG_namespace:
76e288d1
TT
8470 psymbol.domain = VAR_DOMAIN;
8471 psymbol.aclass = LOC_TYPEDEF;
8472 where = psymbol_placement::GLOBAL;
72bf9492 8473 break;
530e8392 8474 case DW_TAG_module:
a5fd13a9
BH
8475 /* With Fortran 77 there might be a "BLOCK DATA" module
8476 available without any name. If so, we skip the module as it
8477 doesn't bring any value. */
8478 if (actual_name != nullptr)
76e288d1
TT
8479 {
8480 psymbol.domain = MODULE_DOMAIN;
8481 psymbol.aclass = LOC_TYPEDEF;
8482 where = psymbol_placement::GLOBAL;
8483 }
530e8392 8484 break;
c906108c 8485 case DW_TAG_class_type:
680b30c7 8486 case DW_TAG_interface_type:
c906108c
SS
8487 case DW_TAG_structure_type:
8488 case DW_TAG_union_type:
8489 case DW_TAG_enumeration_type:
fa4028e9
JB
8490 /* Skip external references. The DWARF standard says in the section
8491 about "Structure, Union, and Class Type Entries": "An incomplete
8492 structure, union or class type is represented by a structure,
8493 union or class entry that does not have a byte size attribute
8494 and that has a DW_AT_declaration attribute." */
8495 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8496 return;
fa4028e9 8497
63d06c5c
DC
8498 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8499 static vs. global. */
76e288d1
TT
8500 psymbol.domain = STRUCT_DOMAIN;
8501 psymbol.aclass = LOC_TYPEDEF;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
c906108c
SS
8505 break;
8506 case DW_TAG_enumerator:
76e288d1
TT
8507 psymbol.domain = VAR_DOMAIN;
8508 psymbol.aclass = LOC_CONST;
8509 where = (cu->language == language_cplus
8510 ? psymbol_placement::GLOBAL
8511 : psymbol_placement::STATIC);
c906108c
SS
8512 break;
8513 default:
8514 break;
8515 }
76e288d1
TT
8516
8517 if (where.has_value ())
8518 {
f049a313
TT
8519 if (built_actual_name != nullptr)
8520 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8521 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8522 psymbol.ginfo.set_linkage_name (actual_name);
8523 else
8524 {
8525 psymbol.ginfo.set_demangled_name (actual_name,
8526 &objfile->objfile_obstack);
8527 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8528 }
76e288d1
TT
8529 add_psymbol_to_list (psymbol, *where, objfile);
8530 }
c906108c
SS
8531}
8532
5c4e30ca
DC
8533/* Read a partial die corresponding to a namespace; also, add a symbol
8534 corresponding to that namespace to the symbol table. NAMESPACE is
8535 the name of the enclosing namespace. */
91c24f0a 8536
72bf9492
DJ
8537static void
8538add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8539 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8540 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8541{
72bf9492 8542 /* Add a symbol for the namespace. */
e7c27a73 8543
72bf9492 8544 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8545
8546 /* Now scan partial symbols in that namespace. */
8547
91c24f0a 8548 if (pdi->has_children)
cdc07690 8549 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8550}
8551
5d7cb8df
JK
8552/* Read a partial die corresponding to a Fortran module. */
8553
8554static void
8555add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8556 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8557{
530e8392
KB
8558 /* Add a symbol for the namespace. */
8559
8560 add_partial_symbol (pdi, cu);
8561
f55ee35c 8562 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8563
8564 if (pdi->has_children)
cdc07690 8565 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8566}
8567
b1dc1806
XR
8568/* Read a partial die corresponding to a subprogram or an inlined
8569 subprogram and create a partial symbol for that subprogram.
8570 When the CU language allows it, this routine also defines a partial
8571 symbol for each nested subprogram that this subprogram contains.
8572 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8573 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8574
cdc07690
YQ
8575 PDI may also be a lexical block, in which case we simply search
8576 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8577 Again, this is only performed when the CU language allows this
8578 type of definitions. */
8579
8580static void
8581add_partial_subprogram (struct partial_die_info *pdi,
8582 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8583 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8584{
b1dc1806 8585 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8586 {
8587 if (pdi->has_pc_info)
8588 {
8589 if (pdi->lowpc < *lowpc)
8590 *lowpc = pdi->lowpc;
8591 if (pdi->highpc > *highpc)
8592 *highpc = pdi->highpc;
cdc07690 8593 if (set_addrmap)
5734ee8b 8594 {
5e22e966 8595 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8596 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8597 CORE_ADDR baseaddr;
b926417a
TT
8598 CORE_ADDR this_highpc;
8599 CORE_ADDR this_lowpc;
5734ee8b 8600
b3b3bada 8601 baseaddr = objfile->text_section_offset ();
b926417a
TT
8602 this_lowpc
8603 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8604 pdi->lowpc + baseaddr)
8605 - baseaddr);
8606 this_highpc
8607 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8608 pdi->highpc + baseaddr)
8609 - baseaddr);
d320c2b5 8610 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8611 this_lowpc, this_highpc - 1,
9291a0cd 8612 cu->per_cu->v.psymtab);
5734ee8b 8613 }
481860b3
GB
8614 }
8615
8616 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8617 {
bc30ff58 8618 if (!pdi->is_declaration)
e8d05480
JB
8619 /* Ignore subprogram DIEs that do not have a name, they are
8620 illegal. Do not emit a complaint at this point, we will
8621 do so when we convert this psymtab into a symtab. */
8622 if (pdi->name)
8623 add_partial_symbol (pdi, cu);
bc30ff58
JB
8624 }
8625 }
6e70227d 8626
bc30ff58
JB
8627 if (! pdi->has_children)
8628 return;
8629
0a4b0913 8630 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8631 {
8632 pdi = pdi->die_child;
8633 while (pdi != NULL)
8634 {
52356b79 8635 pdi->fixup (cu);
bc30ff58 8636 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8637 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8638 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8639 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8640 pdi = pdi->die_sibling;
8641 }
8642 }
8643}
8644
91c24f0a
DC
8645/* Read a partial die corresponding to an enumeration type. */
8646
72bf9492
DJ
8647static void
8648add_partial_enumeration (struct partial_die_info *enum_pdi,
8649 struct dwarf2_cu *cu)
91c24f0a 8650{
72bf9492 8651 struct partial_die_info *pdi;
91c24f0a
DC
8652
8653 if (enum_pdi->name != NULL)
72bf9492
DJ
8654 add_partial_symbol (enum_pdi, cu);
8655
8656 pdi = enum_pdi->die_child;
8657 while (pdi)
91c24f0a 8658 {
72bf9492 8659 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8660 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8661 else
72bf9492
DJ
8662 add_partial_symbol (pdi, cu);
8663 pdi = pdi->die_sibling;
91c24f0a 8664 }
91c24f0a
DC
8665}
8666
6caca83c
CC
8667/* Return the initial uleb128 in the die at INFO_PTR. */
8668
8669static unsigned int
d521ce57 8670peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8671{
8672 unsigned int bytes_read;
8673
8674 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8675}
8676
685af9cd
TT
8677/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8678 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8679
4bb7a0a7
DJ
8680 Return the corresponding abbrev, or NULL if the number is zero (indicating
8681 an empty DIE). In either case *BYTES_READ will be set to the length of
8682 the initial number. */
8683
8684static struct abbrev_info *
685af9cd
TT
8685peek_die_abbrev (const die_reader_specs &reader,
8686 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8687{
685af9cd 8688 dwarf2_cu *cu = reader.cu;
5e22e966 8689 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8690 unsigned int abbrev_number
8691 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8692
8693 if (abbrev_number == 0)
8694 return NULL;
8695
685af9cd 8696 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8697 if (!abbrev)
8698 {
422b9917 8699 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8700 " at offset %s [in module %s]"),
422b9917 8701 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8702 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8703 }
8704
8705 return abbrev;
8706}
8707
93311388
DE
8708/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8709 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8710 DIE. Any children of the skipped DIEs will also be skipped. */
8711
d521ce57
TT
8712static const gdb_byte *
8713skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8714{
4bb7a0a7
DJ
8715 while (1)
8716 {
685af9cd
TT
8717 unsigned int bytes_read;
8718 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8719
4bb7a0a7
DJ
8720 if (abbrev == NULL)
8721 return info_ptr + bytes_read;
8722 else
dee91e82 8723 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8724 }
8725}
8726
93311388
DE
8727/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8728 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8729 abbrev corresponding to that skipped uleb128 should be passed in
8730 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8731 children. */
8732
d521ce57
TT
8733static const gdb_byte *
8734skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8735 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8736{
8737 unsigned int bytes_read;
8738 struct attribute attr;
dee91e82
DE
8739 bfd *abfd = reader->abfd;
8740 struct dwarf2_cu *cu = reader->cu;
d521ce57 8741 const gdb_byte *buffer = reader->buffer;
f664829e 8742 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8743 unsigned int form, i;
8744
8745 for (i = 0; i < abbrev->num_attrs; i++)
8746 {
8747 /* The only abbrev we care about is DW_AT_sibling. */
8748 if (abbrev->attrs[i].name == DW_AT_sibling)
8749 {
18a8505e
AT
8750 bool ignored;
8751 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8752 &ignored);
4bb7a0a7 8753 if (attr.form == DW_FORM_ref_addr)
b98664d3 8754 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8755 else
b9502d3f 8756 {
0826b30a 8757 sect_offset off = attr.get_ref_die_offset ();
9c541725 8758 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8759
8760 if (sibling_ptr < info_ptr)
b98664d3 8761 complaint (_("DW_AT_sibling points backwards"));
22869d73 8762 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8763 reader->die_section->overflow_complaint ();
b9502d3f
WN
8764 else
8765 return sibling_ptr;
8766 }
4bb7a0a7
DJ
8767 }
8768
8769 /* If it isn't DW_AT_sibling, skip this attribute. */
8770 form = abbrev->attrs[i].form;
8771 skip_attribute:
8772 switch (form)
8773 {
4bb7a0a7 8774 case DW_FORM_ref_addr:
ae411497
TT
8775 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8776 and later it is offset sized. */
8777 if (cu->header.version == 2)
8778 info_ptr += cu->header.addr_size;
8779 else
8780 info_ptr += cu->header.offset_size;
8781 break;
36586728
TT
8782 case DW_FORM_GNU_ref_alt:
8783 info_ptr += cu->header.offset_size;
8784 break;
ae411497 8785 case DW_FORM_addr:
4bb7a0a7
DJ
8786 info_ptr += cu->header.addr_size;
8787 break;
8788 case DW_FORM_data1:
8789 case DW_FORM_ref1:
8790 case DW_FORM_flag:
8fe0f950 8791 case DW_FORM_strx1:
4bb7a0a7
DJ
8792 info_ptr += 1;
8793 break;
2dc7f7b3 8794 case DW_FORM_flag_present:
43988095 8795 case DW_FORM_implicit_const:
2dc7f7b3 8796 break;
4bb7a0a7
DJ
8797 case DW_FORM_data2:
8798 case DW_FORM_ref2:
8fe0f950 8799 case DW_FORM_strx2:
4bb7a0a7
DJ
8800 info_ptr += 2;
8801 break;
8fe0f950
AT
8802 case DW_FORM_strx3:
8803 info_ptr += 3;
8804 break;
4bb7a0a7
DJ
8805 case DW_FORM_data4:
8806 case DW_FORM_ref4:
8fe0f950 8807 case DW_FORM_strx4:
4bb7a0a7
DJ
8808 info_ptr += 4;
8809 break;
8810 case DW_FORM_data8:
8811 case DW_FORM_ref8:
55f1336d 8812 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8813 info_ptr += 8;
8814 break;
0224619f
JK
8815 case DW_FORM_data16:
8816 info_ptr += 16;
8817 break;
4bb7a0a7 8818 case DW_FORM_string:
9b1c24c8 8819 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8820 info_ptr += bytes_read;
8821 break;
2dc7f7b3 8822 case DW_FORM_sec_offset:
4bb7a0a7 8823 case DW_FORM_strp:
36586728 8824 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8825 info_ptr += cu->header.offset_size;
8826 break;
2dc7f7b3 8827 case DW_FORM_exprloc:
4bb7a0a7
DJ
8828 case DW_FORM_block:
8829 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8830 info_ptr += bytes_read;
8831 break;
8832 case DW_FORM_block1:
8833 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8834 break;
8835 case DW_FORM_block2:
8836 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8837 break;
8838 case DW_FORM_block4:
8839 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8840 break;
336d760d 8841 case DW_FORM_addrx:
cf532bd1 8842 case DW_FORM_strx:
4bb7a0a7
DJ
8843 case DW_FORM_sdata:
8844 case DW_FORM_udata:
8845 case DW_FORM_ref_udata:
3019eac3
DE
8846 case DW_FORM_GNU_addr_index:
8847 case DW_FORM_GNU_str_index:
18a8505e 8848 case DW_FORM_rnglistx:
41144253 8849 case DW_FORM_loclistx:
d521ce57 8850 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8851 break;
8852 case DW_FORM_indirect:
8853 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8854 info_ptr += bytes_read;
8855 /* We need to continue parsing from here, so just go back to
8856 the top. */
8857 goto skip_attribute;
8858
8859 default:
3e43a32a
MS
8860 error (_("Dwarf Error: Cannot handle %s "
8861 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8862 dwarf_form_name (form),
8863 bfd_get_filename (abfd));
8864 }
8865 }
8866
8867 if (abbrev->has_children)
dee91e82 8868 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8869 else
8870 return info_ptr;
8871}
8872
93311388 8873/* Locate ORIG_PDI's sibling.
dee91e82 8874 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8875
d521ce57 8876static const gdb_byte *
dee91e82
DE
8877locate_pdi_sibling (const struct die_reader_specs *reader,
8878 struct partial_die_info *orig_pdi,
d521ce57 8879 const gdb_byte *info_ptr)
91c24f0a
DC
8880{
8881 /* Do we know the sibling already? */
72bf9492 8882
91c24f0a
DC
8883 if (orig_pdi->sibling)
8884 return orig_pdi->sibling;
8885
8886 /* Are there any children to deal with? */
8887
8888 if (!orig_pdi->has_children)
8889 return info_ptr;
8890
4bb7a0a7 8891 /* Skip the children the long way. */
91c24f0a 8892
dee91e82 8893 return skip_children (reader, info_ptr);
91c24f0a
DC
8894}
8895
257e7a09 8896/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8897 not NULL. */
c906108c 8898
891813be
TT
8899void
8900dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8901{
ed2dc618
SM
8902 struct dwarf2_per_objfile *dwarf2_per_objfile
8903 = get_dwarf2_per_objfile (objfile);
8904
af758d11
SM
8905 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8906
077cbab2
TT
8907 /* If this psymtab is constructed from a debug-only objfile, the
8908 has_section_at_zero flag will not necessarily be correct. We
8909 can get the correct value for this flag by looking at the data
8910 associated with the (presumably stripped) associated objfile. */
8911 if (objfile->separate_debug_objfile_backlink)
c906108c 8912 {
077cbab2
TT
8913 struct dwarf2_per_objfile *dpo_backlink
8914 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8915
5989a64e
SM
8916 dwarf2_per_objfile->per_bfd->has_section_at_zero
8917 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8918 }
98bfdba5 8919
8566b89b 8920 expand_psymtab (objfile);
95554aad 8921
ed2dc618 8922 process_cu_includes (dwarf2_per_objfile);
c906108c 8923}
9cdd5dbd
DE
8924\f
8925/* Reading in full CUs. */
c906108c 8926
10b3939b
DJ
8927/* Add PER_CU to the queue. */
8928
8929static void
95554aad
TT
8930queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8931 enum language pretend_language)
10b3939b 8932{
10b3939b 8933 per_cu->queued = 1;
1859c670 8934 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8935}
8936
89e63ee4
DE
8937/* If PER_CU is not yet queued, add it to the queue.
8938 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8939 dependency.
0907af0c 8940 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8941 meaning either PER_CU is already queued or it is already loaded.
8942
8943 N.B. There is an invariant here that if a CU is queued then it is loaded.
8944 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8945
8946static int
89e63ee4 8947maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8948 struct dwarf2_per_cu_data *per_cu,
8949 enum language pretend_language)
8950{
8951 /* We may arrive here during partial symbol reading, if we need full
8952 DIEs to process an unusual case (e.g. template arguments). Do
8953 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8954 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8955 {
8956 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8957 return 1;
8958 return 0;
8959 }
8960
8961 /* Mark the dependence relation so that we don't flush PER_CU
8962 too early. */
89e63ee4
DE
8963 if (dependent_cu != NULL)
8964 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8965
8966 /* If it's already on the queue, we have nothing to do. */
8967 if (per_cu->queued)
8968 return 0;
8969
8970 /* If the compilation unit is already loaded, just mark it as
8971 used. */
8972 if (per_cu->cu != NULL)
8973 {
8974 per_cu->cu->last_used = 0;
8975 return 0;
8976 }
8977
8978 /* Add it to the queue. */
8979 queue_comp_unit (per_cu, pretend_language);
8980
8981 return 1;
8982}
8983
10b3939b
DJ
8984/* Process the queue. */
8985
8986static void
ed2dc618 8987process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8988{
b4f54984 8989 if (dwarf_read_debug)
45cfd468
DE
8990 {
8991 fprintf_unfiltered (gdb_stdlog,
8992 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8993 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8994 }
8995
03dd20cc
DJ
8996 /* The queue starts out with one item, but following a DIE reference
8997 may load a new CU, adding it to the end of the queue. */
5989a64e 8998 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8999 {
5989a64e 9000 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 9001
af758d11 9002 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 9003 /* Skip dummy CUs. */
39856def 9004 && item.per_cu->cu != NULL)
f4dc4d17 9005 {
39856def 9006 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9007 unsigned int debug_print_threshold;
247f5c4f 9008 char buf[100];
f4dc4d17 9009
247f5c4f 9010 if (per_cu->is_debug_types)
f4dc4d17 9011 {
247f5c4f
DE
9012 struct signatured_type *sig_type =
9013 (struct signatured_type *) per_cu;
9014
9d8780f0 9015 sprintf (buf, "TU %s at offset %s",
73be47f5 9016 hex_string (sig_type->signature),
9d8780f0 9017 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9018 /* There can be 100s of TUs.
9019 Only print them in verbose mode. */
9020 debug_print_threshold = 2;
f4dc4d17 9021 }
247f5c4f 9022 else
73be47f5 9023 {
9d8780f0
SM
9024 sprintf (buf, "CU at offset %s",
9025 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9026 debug_print_threshold = 1;
9027 }
247f5c4f 9028
b4f54984 9029 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9030 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9031
9032 if (per_cu->is_debug_types)
47b14e86
SM
9033 process_full_type_unit (per_cu, dwarf2_per_objfile,
9034 item.pretend_language);
f4dc4d17 9035 else
47b14e86
SM
9036 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9037 item.pretend_language);
f4dc4d17 9038
b4f54984 9039 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9040 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9041 }
10b3939b 9042
39856def 9043 item.per_cu->queued = 0;
5989a64e 9044 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9045 }
9046
b4f54984 9047 if (dwarf_read_debug)
45cfd468
DE
9048 {
9049 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9050 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9051 }
10b3939b
DJ
9052}
9053
10b3939b
DJ
9054/* Read in full symbols for PST, and anything it depends on. */
9055
8566b89b
TT
9056void
9057dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9058{
af758d11 9059 gdb_assert (!readin_p (objfile));
95554aad 9060
48993951 9061 expand_dependencies (objfile);
aaa75496 9062
97a1449a
SM
9063 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9064 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9065 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9066}
9067
af758d11
SM
9068/* See psympriv.h. */
9069
9070bool
9071dwarf2_psymtab::readin_p (struct objfile *objfile) const
9072{
9073 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9074 return per_objfile->symtab_set_p (per_cu_data);
9075}
9076
9077/* See psympriv.h. */
9078
9079compunit_symtab *
9080dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9081{
9082 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9083 return per_objfile->get_symtab (per_cu_data);
9084}
9085
dee91e82
DE
9086/* Trivial hash function for die_info: the hash value of a DIE
9087 is its offset in .debug_info for this objfile. */
10b3939b 9088
dee91e82
DE
9089static hashval_t
9090die_hash (const void *item)
10b3939b 9091{
9a3c8263 9092 const struct die_info *die = (const struct die_info *) item;
6502dd73 9093
9c541725 9094 return to_underlying (die->sect_off);
dee91e82 9095}
63d06c5c 9096
dee91e82
DE
9097/* Trivial comparison function for die_info structures: two DIEs
9098 are equal if they have the same offset. */
98bfdba5 9099
dee91e82
DE
9100static int
9101die_eq (const void *item_lhs, const void *item_rhs)
9102{
9a3c8263
SM
9103 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9104 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9105
9c541725 9106 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9107}
c906108c 9108
c0ab21c2 9109/* Load the DIEs associated with PER_CU into memory. */
c906108c 9110
dee91e82 9111static void
ab432490
SM
9112load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9113 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9114 bool skip_partial,
9115 enum language pretend_language)
dee91e82 9116{
c0ab21c2
TT
9117 gdb_assert (! this_cu->is_debug_types);
9118
ab432490 9119 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9120 if (reader.dummy_p)
9121 return;
9122
9123 struct dwarf2_cu *cu = reader.cu;
9124 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9125
dee91e82
DE
9126 gdb_assert (cu->die_hash == NULL);
9127 cu->die_hash =
9128 htab_create_alloc_ex (cu->header.length / 12,
9129 die_hash,
9130 die_eq,
9131 NULL,
9132 &cu->comp_unit_obstack,
9133 hashtab_obstack_allocate,
9134 dummy_obstack_deallocate);
e142c38c 9135
3e225074 9136 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9137 reader.comp_unit_die->child
9138 = read_die_and_siblings (&reader, reader.info_ptr,
9139 &info_ptr, reader.comp_unit_die);
9140 cu->dies = reader.comp_unit_die;
dee91e82 9141 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9142
9143 /* We try not to read any attributes in this function, because not
9cdd5dbd 9144 all CUs needed for references have been loaded yet, and symbol
10b3939b 9145 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9146 or we won't be able to build types correctly.
9147 Similarly, if we do not read the producer, we can not apply
9148 producer-specific interpretation. */
c0ab21c2 9149 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9150
9151 reader.keep ();
10b3939b
DJ
9152}
9153
3da10d80
KS
9154/* Add a DIE to the delayed physname list. */
9155
9156static void
9157add_to_method_list (struct type *type, int fnfield_index, int index,
9158 const char *name, struct die_info *die,
9159 struct dwarf2_cu *cu)
9160{
9161 struct delayed_method_info mi;
9162 mi.type = type;
9163 mi.fnfield_index = fnfield_index;
9164 mi.index = index;
9165 mi.name = name;
9166 mi.die = die;
c89b44cd 9167 cu->method_list.push_back (mi);
3da10d80
KS
9168}
9169
3693fdb3
PA
9170/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9171 "const" / "volatile". If so, decrements LEN by the length of the
9172 modifier and return true. Otherwise return false. */
9173
9174template<size_t N>
9175static bool
9176check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9177{
9178 size_t mod_len = sizeof (mod) - 1;
9179 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9180 {
9181 len -= mod_len;
9182 return true;
9183 }
9184 return false;
9185}
9186
3da10d80
KS
9187/* Compute the physnames of any methods on the CU's method list.
9188
9189 The computation of method physnames is delayed in order to avoid the
9190 (bad) condition that one of the method's formal parameters is of an as yet
9191 incomplete type. */
9192
9193static void
9194compute_delayed_physnames (struct dwarf2_cu *cu)
9195{
3693fdb3 9196 /* Only C++ delays computing physnames. */
c89b44cd 9197 if (cu->method_list.empty ())
3693fdb3
PA
9198 return;
9199 gdb_assert (cu->language == language_cplus);
9200
52941706 9201 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9202 {
1d06ead6 9203 const char *physname;
3da10d80 9204 struct fn_fieldlist *fn_flp
c89b44cd
TT
9205 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9206 physname = dwarf2_physname (mi.name, mi.die, cu);
9207 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9208 = physname ? physname : "";
3693fdb3
PA
9209
9210 /* Since there's no tag to indicate whether a method is a
9211 const/volatile overload, extract that information out of the
9212 demangled name. */
9213 if (physname != NULL)
9214 {
9215 size_t len = strlen (physname);
9216
9217 while (1)
9218 {
9219 if (physname[len] == ')') /* shortcut */
9220 break;
9221 else if (check_modifier (physname, len, " const"))
c89b44cd 9222 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9223 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9224 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9225 else
9226 break;
9227 }
9228 }
3da10d80 9229 }
c89b44cd
TT
9230
9231 /* The list is no longer needed. */
9232 cu->method_list.clear ();
3da10d80
KS
9233}
9234
a766d390
DE
9235/* Go objects should be embedded in a DW_TAG_module DIE,
9236 and it's not clear if/how imported objects will appear.
9237 To keep Go support simple until that's worked out,
9238 go back through what we've read and create something usable.
9239 We could do this while processing each DIE, and feels kinda cleaner,
9240 but that way is more invasive.
9241 This is to, for example, allow the user to type "p var" or "b main"
9242 without having to specify the package name, and allow lookups
9243 of module.object to work in contexts that use the expression
9244 parser. */
9245
9246static void
9247fixup_go_packaging (struct dwarf2_cu *cu)
9248{
421d1616 9249 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9250 struct pending *list;
9251 int i;
9252
c24bdb02 9253 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9254 list != NULL;
9255 list = list->next)
a766d390
DE
9256 {
9257 for (i = 0; i < list->nsyms; ++i)
9258 {
9259 struct symbol *sym = list->symbol[i];
9260
c1b5c1eb 9261 if (sym->language () == language_go
a766d390
DE
9262 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9263 {
421d1616
TT
9264 gdb::unique_xmalloc_ptr<char> this_package_name
9265 (go_symbol_package_name (sym));
a766d390
DE
9266
9267 if (this_package_name == NULL)
9268 continue;
9269 if (package_name == NULL)
421d1616 9270 package_name = std::move (this_package_name);
a766d390
DE
9271 else
9272 {
5e22e966 9273 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9274 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9275 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9276 (symbol_symtab (sym) != NULL
9277 ? symtab_to_filename_for_display
9278 (symbol_symtab (sym))
e3b94546 9279 : objfile_name (objfile)),
421d1616 9280 this_package_name.get (), package_name.get ());
a766d390
DE
9281 }
9282 }
9283 }
9284 }
9285
9286 if (package_name != NULL)
9287 {
5e22e966 9288 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9289 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9290 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9291 saved_package_name);
a766d390
DE
9292 struct symbol *sym;
9293
8c14c3a3 9294 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9295 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9296 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9297 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9298 e.g., "main" finds the "main" module and not C's main(). */
9299 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9300 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9301 SYMBOL_TYPE (sym) = type;
9302
c24bdb02 9303 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9304 }
9305}
9306
c9317f21
TT
9307/* Allocate a fully-qualified name consisting of the two parts on the
9308 obstack. */
9309
9310static const char *
9311rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9312{
9313 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9314}
9315
9c6a1327
TT
9316/* A helper that allocates a variant part to attach to a Rust enum
9317 type. OBSTACK is where the results should be allocated. TYPE is
9318 the type we're processing. DISCRIMINANT_INDEX is the index of the
9319 discriminant. It must be the index of one of the fields of TYPE.
9320 DEFAULT_INDEX is the index of the default field; or -1 if there is
9321 no default. RANGES is indexed by "effective" field number (the
9322 field index, but omitting the discriminant and default fields) and
9323 must hold the discriminant values used by the variants. Note that
9324 RANGES must have a lifetime at least as long as OBSTACK -- either
9325 already allocated on it, or static. */
c9317f21 9326
9c6a1327
TT
9327static void
9328alloc_rust_variant (struct obstack *obstack, struct type *type,
9329 int discriminant_index, int default_index,
9330 gdb::array_view<discriminant_range> ranges)
9331{
9332 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9333 must be handled by the caller. */
9334 gdb_assert (discriminant_index >= 0
1f704f76 9335 && discriminant_index < type->num_fields ());
c9317f21 9336 gdb_assert (default_index == -1
1f704f76 9337 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9338
9c6a1327 9339 /* We have one variant for each non-discriminant field. */
1f704f76 9340 int n_variants = type->num_fields () - 1;
c9317f21 9341
9c6a1327
TT
9342 variant *variants = new (obstack) variant[n_variants];
9343 int var_idx = 0;
9344 int range_idx = 0;
1f704f76 9345 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9346 {
9347 if (i == discriminant_index)
9348 continue;
c9317f21 9349
9c6a1327
TT
9350 variants[var_idx].first_field = i;
9351 variants[var_idx].last_field = i + 1;
9352
9353 /* The default field does not need a range, but other fields do.
9354 We skipped the discriminant above. */
9355 if (i != default_index)
9356 {
9357 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9358 ++range_idx;
9359 }
c9317f21 9360
9c6a1327
TT
9361 ++var_idx;
9362 }
9363
9364 gdb_assert (range_idx == ranges.size ());
9365 gdb_assert (var_idx == n_variants);
9366
9367 variant_part *part = new (obstack) variant_part;
9368 part->discriminant_index = discriminant_index;
9369 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9370 discriminant_index));
9371 part->variants = gdb::array_view<variant> (variants, n_variants);
9372
9373 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9374 gdb::array_view<variant_part> *prop_value
9375 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9376
9c6a1327
TT
9377 struct dynamic_prop prop;
9378 prop.kind = PROP_VARIANT_PARTS;
9379 prop.data.variant_parts = prop_value;
9380
5c54719c 9381 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9382}
9383
9384/* Some versions of rustc emitted enums in an unusual way.
9385
9386 Ordinary enums were emitted as unions. The first element of each
9387 structure in the union was named "RUST$ENUM$DISR". This element
9388 held the discriminant.
9389
9390 These versions of Rust also implemented the "non-zero"
9391 optimization. When the enum had two values, and one is empty and
9392 the other holds a pointer that cannot be zero, the pointer is used
9393 as the discriminant, with a zero value meaning the empty variant.
9394 Here, the union's first member is of the form
9395 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9396 where the fieldnos are the indices of the fields that should be
9397 traversed in order to find the field (which may be several fields deep)
9398 and the variantname is the name of the variant of the case when the
9399 field is zero.
9400
9401 This function recognizes whether TYPE is of one of these forms,
9402 and, if so, smashes it to be a variant type. */
9403
9404static void
9405quirk_rust_enum (struct type *type, struct objfile *objfile)
9406{
78134374 9407 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9408
9409 /* We don't need to deal with empty enums. */
1f704f76 9410 if (type->num_fields () == 0)
c9317f21
TT
9411 return;
9412
9413#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9414 if (type->num_fields () == 1
c9317f21
TT
9415 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9416 {
9417 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9418
9419 /* Decode the field name to find the offset of the
9420 discriminant. */
9421 ULONGEST bit_offset = 0;
9422 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9423 while (name[0] >= '0' && name[0] <= '9')
9424 {
9425 char *tail;
9426 unsigned long index = strtoul (name, &tail, 10);
9427 name = tail;
9428 if (*name != '$'
1f704f76 9429 || index >= field_type->num_fields ()
c9317f21
TT
9430 || (TYPE_FIELD_LOC_KIND (field_type, index)
9431 != FIELD_LOC_KIND_BITPOS))
9432 {
b98664d3 9433 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9434 "[in module %s]"),
9435 TYPE_FIELD_NAME (type, 0),
9436 objfile_name (objfile));
9437 return;
9438 }
9439 ++name;
9440
9441 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9442 field_type = TYPE_FIELD_TYPE (field_type, index);
9443 }
9444
9c6a1327
TT
9445 /* Smash this type to be a structure type. We have to do this
9446 because the type has already been recorded. */
67607e24 9447 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9448 type->set_num_fields (3);
9c6a1327 9449 /* Save the field we care about. */
ceacbf6e 9450 struct field saved_field = type->field (0);
3cabb6b0
SM
9451 type->set_fields
9452 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9453
9c6a1327
TT
9454 /* Put the discriminant at index 0. */
9455 TYPE_FIELD_TYPE (type, 0) = field_type;
9456 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9457 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9458 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9459
9460 /* The order of fields doesn't really matter, so put the real
9461 field at index 1 and the data-less field at index 2. */
ceacbf6e 9462 type->field (1) = saved_field;
9c6a1327 9463 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9464 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9465 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9466 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9467 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9468
9469 const char *dataless_name
7d93a1e0 9470 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9471 name);
9472 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9473 dataless_name);
9c6a1327 9474 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9475 /* NAME points into the original discriminant name, which
9476 already has the correct lifetime. */
9c6a1327 9477 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9478 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9479
9c6a1327
TT
9480 /* Indicate that this is a variant type. */
9481 static discriminant_range ranges[1] = { { 0, 0 } };
9482 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9483 }
77c2dba3
TT
9484 /* A union with a single anonymous field is probably an old-style
9485 univariant enum. */
1f704f76 9486 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9487 {
c9317f21
TT
9488 /* Smash this type to be a structure type. We have to do this
9489 because the type has already been recorded. */
67607e24 9490 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9491
9c6a1327 9492 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9493 const char *variant_name
7d93a1e0 9494 = rust_last_path_segment (field_type->name ());
9c6a1327 9495 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9496 field_type->set_name
9497 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9498 type->name (), variant_name));
c9317f21
TT
9499 }
9500 else
9501 {
9502 struct type *disr_type = nullptr;
1f704f76 9503 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9504 {
9505 disr_type = TYPE_FIELD_TYPE (type, i);
9506
78134374 9507 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9508 {
9509 /* All fields of a true enum will be structs. */
9510 return;
9511 }
1f704f76 9512 else if (disr_type->num_fields () == 0)
c9317f21
TT
9513 {
9514 /* Could be data-less variant, so keep going. */
a037790e 9515 disr_type = nullptr;
c9317f21
TT
9516 }
9517 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9518 "RUST$ENUM$DISR") != 0)
9519 {
9520 /* Not a Rust enum. */
9521 return;
9522 }
9523 else
9524 {
9525 /* Found one. */
9526 break;
9527 }
9528 }
9529
9530 /* If we got here without a discriminant, then it's probably
9531 just a union. */
9532 if (disr_type == nullptr)
9533 return;
9534
9535 /* Smash this type to be a structure type. We have to do this
9536 because the type has already been recorded. */
67607e24 9537 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9538
9c6a1327 9539 /* Make space for the discriminant field. */
ceacbf6e 9540 struct field *disr_field = &disr_type->field (0);
9c6a1327 9541 field *new_fields
1f704f76 9542 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9543 * sizeof (struct field)));
80fc5e77 9544 memcpy (new_fields + 1, type->fields (),
1f704f76 9545 type->num_fields () * sizeof (struct field));
3cabb6b0 9546 type->set_fields (new_fields);
1f704f76 9547 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9548
9549 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9550 type->field (0) = *disr_field;
9c6a1327
TT
9551 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9552 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9553
9554 /* We need a way to find the correct discriminant given a
9555 variant name. For convenience we build a map here. */
9556 struct type *enum_type = FIELD_TYPE (*disr_field);
9557 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9558 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9559 {
9560 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9561 {
9562 const char *name
9563 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9564 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9565 }
9566 }
9567
1f704f76 9568 int n_fields = type->num_fields ();
9c6a1327
TT
9569 /* We don't need a range entry for the discriminant, but we do
9570 need one for every other field, as there is no default
9571 variant. */
9572 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9573 discriminant_range,
9574 n_fields - 1);
c9317f21
TT
9575 /* Skip the discriminant here. */
9576 for (int i = 1; i < n_fields; ++i)
9577 {
9578 /* Find the final word in the name of this variant's type.
9579 That name can be used to look up the correct
9580 discriminant. */
9581 const char *variant_name
7d93a1e0 9582 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9583
9584 auto iter = discriminant_map.find (variant_name);
9585 if (iter != discriminant_map.end ())
9c6a1327
TT
9586 {
9587 ranges[i].low = iter->second;
9588 ranges[i].high = iter->second;
9589 }
c9317f21 9590
bedda9ac 9591 /* Remove the discriminant field, if it exists. */
9c6a1327 9592 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9593 if (sub_type->num_fields () > 0)
bedda9ac 9594 {
5e33d5f4 9595 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9596 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9597 }
9c6a1327 9598 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9599 sub_type->set_name
9600 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9601 type->name (), variant_name));
c9317f21 9602 }
9c6a1327
TT
9603
9604 /* Indicate that this is a variant type. */
9605 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9606 gdb::array_view<discriminant_range> (ranges,
9607 n_fields - 1));
c9317f21
TT
9608 }
9609}
9610
9611/* Rewrite some Rust unions to be structures with variants parts. */
9612
9613static void
9614rust_union_quirks (struct dwarf2_cu *cu)
9615{
9616 gdb_assert (cu->language == language_rust);
52941706 9617 for (type *type_ : cu->rust_unions)
5e22e966 9618 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9619 /* We don't need this any more. */
9620 cu->rust_unions.clear ();
c9317f21
TT
9621}
9622
95554aad
TT
9623/* A helper function for computing the list of all symbol tables
9624 included by PER_CU. */
9625
9626static void
4c39bc03 9627recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9628 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9629 dwarf2_per_cu_data *per_cu,
9630 dwarf2_per_objfile *per_objfile,
43f3e411 9631 struct compunit_symtab *immediate_parent)
95554aad 9632{
af758d11 9633 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9634 if (*slot != NULL)
9635 {
9636 /* This inclusion and its children have been processed. */
9637 return;
9638 }
9639
9640 *slot = per_cu;
af758d11 9641
95554aad 9642 /* Only add a CU if it has a symbol table. */
43182c09 9643 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9644 if (cust != NULL)
ec94af83
DE
9645 {
9646 /* If this is a type unit only add its symbol table if we haven't
9647 seen it yet (type unit per_cu's can share symtabs). */
9648 if (per_cu->is_debug_types)
9649 {
43f3e411 9650 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9651 if (*slot == NULL)
9652 {
43f3e411 9653 *slot = cust;
4c39bc03 9654 result->push_back (cust);
43f3e411
DE
9655 if (cust->user == NULL)
9656 cust->user = immediate_parent;
ec94af83
DE
9657 }
9658 }
9659 else
f9125b6c 9660 {
4c39bc03 9661 result->push_back (cust);
43f3e411
DE
9662 if (cust->user == NULL)
9663 cust->user = immediate_parent;
f9125b6c 9664 }
ec94af83 9665 }
95554aad 9666
ae640021
AB
9667 if (!per_cu->imported_symtabs_empty ())
9668 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9669 {
9670 recursively_compute_inclusions (result, all_children,
43182c09
SM
9671 all_type_symtabs, ptr, per_objfile,
9672 cust);
ae640021 9673 }
95554aad
TT
9674}
9675
43f3e411 9676/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9677 PER_CU. */
9678
9679static void
43182c09
SM
9680compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9681 dwarf2_per_objfile *per_objfile)
95554aad 9682{
f4dc4d17
DE
9683 gdb_assert (! per_cu->is_debug_types);
9684
ae640021 9685 if (!per_cu->imported_symtabs_empty ())
95554aad 9686 {
ae640021 9687 int len;
4c39bc03 9688 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9689 htab_t all_children, all_type_symtabs;
43182c09 9690 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9691
9692 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9693 if (cust == NULL)
95554aad
TT
9694 return;
9695
9696 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9697 NULL, xcalloc, xfree);
ec94af83
DE
9698 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9699 NULL, xcalloc, xfree);
95554aad 9700
ae640021 9701 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9702 {
9703 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9704 all_type_symtabs, ptr, per_objfile,
9705 cust);
ec94af83 9706 }
95554aad 9707
ec94af83 9708 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9709 len = result_symtabs.size ();
43f3e411 9710 cust->includes
ed2dc618 9711 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9712 struct compunit_symtab *, len + 1);
4c39bc03
TT
9713 memcpy (cust->includes, result_symtabs.data (),
9714 len * sizeof (compunit_symtab *));
43f3e411 9715 cust->includes[len] = NULL;
95554aad 9716
95554aad 9717 htab_delete (all_children);
ec94af83 9718 htab_delete (all_type_symtabs);
95554aad
TT
9719 }
9720}
9721
9722/* Compute the 'includes' field for the symtabs of all the CUs we just
9723 read. */
9724
9725static void
ed2dc618 9726process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9727{
5989a64e 9728 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9729 {
9730 if (! iter->is_debug_types)
43182c09 9731 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9732 }
95554aad 9733
5989a64e 9734 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9735}
9736
9cdd5dbd 9737/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9738 already been loaded into memory. */
9739
9740static void
47b14e86
SM
9741process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9742 dwarf2_per_objfile *dwarf2_per_objfile,
95554aad 9743 enum language pretend_language)
10b3939b 9744{
10b3939b 9745 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9746 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9747 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9748 CORE_ADDR lowpc, highpc;
43f3e411 9749 struct compunit_symtab *cust;
10b3939b 9750 CORE_ADDR baseaddr;
4359dff1 9751 struct block *static_block;
3e29f34a 9752 CORE_ADDR addr;
10b3939b 9753
b3b3bada 9754 baseaddr = objfile->text_section_offset ();
10b3939b 9755
c89b44cd
TT
9756 /* Clear the list here in case something was left over. */
9757 cu->method_list.clear ();
10b3939b 9758
95554aad
TT
9759 cu->language = pretend_language;
9760 cu->language_defn = language_def (cu->language);
9761
c906108c 9762 /* Do line number decoding in read_file_scope () */
10b3939b 9763 process_die (cu->dies, cu);
c906108c 9764
a766d390
DE
9765 /* For now fudge the Go package. */
9766 if (cu->language == language_go)
9767 fixup_go_packaging (cu);
9768
5f48f8f3 9769 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9770 should be complete, and it should now be safe to compute all of the
9771 physnames. */
9772 compute_delayed_physnames (cu);
3da10d80 9773
c9317f21
TT
9774 if (cu->language == language_rust)
9775 rust_union_quirks (cu);
9776
fae299cd
DC
9777 /* Some compilers don't define a DW_AT_high_pc attribute for the
9778 compilation unit. If the DW_AT_high_pc is missing, synthesize
9779 it, by scanning the DIE's below the compilation unit. */
10b3939b 9780 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9781
3e29f34a 9782 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9783 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9784
9785 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9786 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9787 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9788 addrmap to help ensure it has an accurate map of pc values belonging to
9789 this comp unit. */
9790 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9791
c24bdb02 9792 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9793 SECT_OFF_TEXT (objfile),
9794 0);
c906108c 9795
43f3e411 9796 if (cust != NULL)
c906108c 9797 {
df15bd07 9798 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9799
8be455d7
JK
9800 /* Set symtab language to language from DW_AT_language. If the
9801 compilation is from a C file generated by language preprocessors, do
9802 not set the language if it was already deduced by start_subfile. */
43f3e411 9803 if (!(cu->language == language_c
40e3ad0e 9804 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9805 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9806
9807 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9808 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9809 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9810 there were bugs in prologue debug info, fixed later in GCC-4.5
9811 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9812
9813 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9814 needed, it would be wrong due to missing DW_AT_producer there.
9815
9816 Still one can confuse GDB by using non-standard GCC compilation
9817 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9818 */
ab260dad 9819 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9820 cust->locations_valid = 1;
e0d00bc7
JK
9821
9822 if (gcc_4_minor >= 5)
43f3e411 9823 cust->epilogue_unwind_valid = 1;
96408a79 9824
43f3e411 9825 cust->call_site_htab = cu->call_site_htab;
c906108c 9826 }
9291a0cd 9827
af758d11 9828 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9829
95554aad 9830 /* Push it for inclusion processing later. */
5989a64e 9831 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9832
9833 /* Not needed any more. */
c24bdb02 9834 cu->reset_builder ();
f4dc4d17 9835}
45cfd468 9836
f4dc4d17
DE
9837/* Generate full symbol information for type unit PER_CU, whose DIEs have
9838 already been loaded into memory. */
9839
9840static void
47b14e86
SM
9841process_full_type_unit (dwarf2_per_cu_data *per_cu,
9842 dwarf2_per_objfile *dwarf2_per_objfile,
f4dc4d17
DE
9843 enum language pretend_language)
9844{
9845 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9846 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9847 struct compunit_symtab *cust;
0186c6a7
DE
9848 struct signatured_type *sig_type;
9849
9850 gdb_assert (per_cu->is_debug_types);
9851 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9852
c89b44cd
TT
9853 /* Clear the list here in case something was left over. */
9854 cu->method_list.clear ();
f4dc4d17 9855
f4dc4d17
DE
9856 cu->language = pretend_language;
9857 cu->language_defn = language_def (cu->language);
9858
9859 /* The symbol tables are set up in read_type_unit_scope. */
9860 process_die (cu->dies, cu);
9861
9862 /* For now fudge the Go package. */
9863 if (cu->language == language_go)
9864 fixup_go_packaging (cu);
9865
5f48f8f3 9866 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9867 should be complete, and it should now be safe to compute all of the
9868 physnames. */
9869 compute_delayed_physnames (cu);
f4dc4d17 9870
c9317f21
TT
9871 if (cu->language == language_rust)
9872 rust_union_quirks (cu);
9873
f4dc4d17
DE
9874 /* TUs share symbol tables.
9875 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9876 of it with end_expandable_symtab. Otherwise, complete the addition of
9877 this TU's symbols to the existing symtab. */
43f3e411 9878 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9879 {
c24bdb02
KS
9880 buildsym_compunit *builder = cu->get_builder ();
9881 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9882 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9883
43f3e411 9884 if (cust != NULL)
f4dc4d17
DE
9885 {
9886 /* Set symtab language to language from DW_AT_language. If the
9887 compilation is from a C file generated by language preprocessors,
9888 do not set the language if it was already deduced by
9889 start_subfile. */
43f3e411
DE
9890 if (!(cu->language == language_c
9891 && COMPUNIT_FILETABS (cust)->language != language_c))
9892 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9893 }
9894 }
9895 else
9896 {
c24bdb02 9897 cu->get_builder ()->augment_type_symtab ();
43f3e411 9898 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9899 }
9900
af758d11 9901 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9902
9903 /* Not needed any more. */
c24bdb02 9904 cu->reset_builder ();
c906108c
SS
9905}
9906
95554aad
TT
9907/* Process an imported unit DIE. */
9908
9909static void
9910process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9911{
9912 struct attribute *attr;
9913
f4dc4d17
DE
9914 /* For now we don't handle imported units in type units. */
9915 if (cu->per_cu->is_debug_types)
9916 {
9917 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9918 " supported in type units [in module %s]"),
5e22e966 9919 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9920 }
9921
95554aad
TT
9922 attr = dwarf2_attr (die, DW_AT_import, cu);
9923 if (attr != NULL)
9924 {
0826b30a 9925 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9926 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9927 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9928 dwarf2_per_cu_data *per_cu
ab432490 9929 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9930
58990295
TV
9931 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9932 into another compilation unit, at root level. Regard this as a hint,
9933 and ignore it. */
9934 if (die->parent && die->parent->parent == NULL
9935 && per_cu->unit_type == DW_UT_compile
9936 && per_cu->lang == language_cplus)
9937 return;
9938
69d751e3 9939 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9940 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 9941 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9942
ae640021 9943 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9944 }
9945}
9946
4c8aa72d
PA
9947/* RAII object that represents a process_die scope: i.e.,
9948 starts/finishes processing a DIE. */
9949class process_die_scope
adde2bff 9950{
4c8aa72d
PA
9951public:
9952 process_die_scope (die_info *die, dwarf2_cu *cu)
9953 : m_die (die), m_cu (cu)
9954 {
9955 /* We should only be processing DIEs not already in process. */
9956 gdb_assert (!m_die->in_process);
9957 m_die->in_process = true;
9958 }
8c3cb9fa 9959
4c8aa72d
PA
9960 ~process_die_scope ()
9961 {
9962 m_die->in_process = false;
9963
9964 /* If we're done processing the DIE for the CU that owns the line
9965 header, we don't need the line header anymore. */
9966 if (m_cu->line_header_die_owner == m_die)
9967 {
9968 delete m_cu->line_header;
9969 m_cu->line_header = NULL;
9970 m_cu->line_header_die_owner = NULL;
9971 }
9972 }
9973
9974private:
9975 die_info *m_die;
9976 dwarf2_cu *m_cu;
9977};
adde2bff 9978
c906108c
SS
9979/* Process a die and its children. */
9980
9981static void
e7c27a73 9982process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9983{
4c8aa72d 9984 process_die_scope scope (die, cu);
adde2bff 9985
c906108c
SS
9986 switch (die->tag)
9987 {
9988 case DW_TAG_padding:
9989 break;
9990 case DW_TAG_compile_unit:
95554aad 9991 case DW_TAG_partial_unit:
e7c27a73 9992 read_file_scope (die, cu);
c906108c 9993 break;
348e048f
DE
9994 case DW_TAG_type_unit:
9995 read_type_unit_scope (die, cu);
9996 break;
c906108c 9997 case DW_TAG_subprogram:
0a4b0913
AB
9998 /* Nested subprograms in Fortran get a prefix. */
9999 if (cu->language == language_fortran
10000 && die->parent != NULL
10001 && die->parent->tag == DW_TAG_subprogram)
10002 cu->processing_has_namespace_info = true;
10003 /* Fall through. */
c906108c 10004 case DW_TAG_inlined_subroutine:
edb3359d 10005 read_func_scope (die, cu);
c906108c
SS
10006 break;
10007 case DW_TAG_lexical_block:
14898363
L
10008 case DW_TAG_try_block:
10009 case DW_TAG_catch_block:
e7c27a73 10010 read_lexical_block_scope (die, cu);
c906108c 10011 break;
216f72a1 10012 case DW_TAG_call_site:
96408a79
SA
10013 case DW_TAG_GNU_call_site:
10014 read_call_site_scope (die, cu);
10015 break;
c906108c 10016 case DW_TAG_class_type:
680b30c7 10017 case DW_TAG_interface_type:
c906108c
SS
10018 case DW_TAG_structure_type:
10019 case DW_TAG_union_type:
134d01f1 10020 process_structure_scope (die, cu);
c906108c
SS
10021 break;
10022 case DW_TAG_enumeration_type:
134d01f1 10023 process_enumeration_scope (die, cu);
c906108c 10024 break;
134d01f1 10025
f792889a
DJ
10026 /* These dies have a type, but processing them does not create
10027 a symbol or recurse to process the children. Therefore we can
10028 read them on-demand through read_type_die. */
c906108c 10029 case DW_TAG_subroutine_type:
72019c9c 10030 case DW_TAG_set_type:
c906108c 10031 case DW_TAG_array_type:
c906108c 10032 case DW_TAG_pointer_type:
c906108c 10033 case DW_TAG_ptr_to_member_type:
c906108c 10034 case DW_TAG_reference_type:
4297a3f0 10035 case DW_TAG_rvalue_reference_type:
c906108c 10036 case DW_TAG_string_type:
c906108c 10037 break;
134d01f1 10038
c906108c 10039 case DW_TAG_base_type:
a02abb62 10040 case DW_TAG_subrange_type:
cb249c71 10041 case DW_TAG_typedef:
134d01f1
DJ
10042 /* Add a typedef symbol for the type definition, if it has a
10043 DW_AT_name. */
f792889a 10044 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10045 break;
c906108c 10046 case DW_TAG_common_block:
e7c27a73 10047 read_common_block (die, cu);
c906108c
SS
10048 break;
10049 case DW_TAG_common_inclusion:
10050 break;
d9fa45fe 10051 case DW_TAG_namespace:
9068261f 10052 cu->processing_has_namespace_info = true;
e7c27a73 10053 read_namespace (die, cu);
d9fa45fe 10054 break;
5d7cb8df 10055 case DW_TAG_module:
9068261f 10056 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10057 read_module (die, cu);
10058 break;
d9fa45fe 10059 case DW_TAG_imported_declaration:
9068261f 10060 cu->processing_has_namespace_info = true;
74921315
KS
10061 if (read_namespace_alias (die, cu))
10062 break;
86a73007
TT
10063 /* The declaration is not a global namespace alias. */
10064 /* Fall through. */
d9fa45fe 10065 case DW_TAG_imported_module:
9068261f 10066 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10067 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10068 || cu->language != language_fortran))
b98664d3 10069 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10070 dwarf_tag_name (die->tag));
10071 read_import_statement (die, cu);
d9fa45fe 10072 break;
95554aad
TT
10073
10074 case DW_TAG_imported_unit:
10075 process_imported_unit_die (die, cu);
10076 break;
10077
71a3c369
TT
10078 case DW_TAG_variable:
10079 read_variable (die, cu);
10080 break;
10081
c906108c 10082 default:
e7c27a73 10083 new_symbol (die, NULL, cu);
c906108c
SS
10084 break;
10085 }
10086}
ca69b9e6
DE
10087\f
10088/* DWARF name computation. */
c906108c 10089
94af9270
KS
10090/* A helper function for dwarf2_compute_name which determines whether DIE
10091 needs to have the name of the scope prepended to the name listed in the
10092 die. */
10093
10094static int
10095die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10096{
1c809c68
TT
10097 struct attribute *attr;
10098
94af9270
KS
10099 switch (die->tag)
10100 {
10101 case DW_TAG_namespace:
10102 case DW_TAG_typedef:
10103 case DW_TAG_class_type:
10104 case DW_TAG_interface_type:
10105 case DW_TAG_structure_type:
10106 case DW_TAG_union_type:
10107 case DW_TAG_enumeration_type:
10108 case DW_TAG_enumerator:
10109 case DW_TAG_subprogram:
08a76f8a 10110 case DW_TAG_inlined_subroutine:
94af9270 10111 case DW_TAG_member:
74921315 10112 case DW_TAG_imported_declaration:
94af9270
KS
10113 return 1;
10114
10115 case DW_TAG_variable:
c2b0a229 10116 case DW_TAG_constant:
94af9270
KS
10117 /* We only need to prefix "globally" visible variables. These include
10118 any variable marked with DW_AT_external or any variable that
10119 lives in a namespace. [Variables in anonymous namespaces
10120 require prefixing, but they are not DW_AT_external.] */
10121
10122 if (dwarf2_attr (die, DW_AT_specification, cu))
10123 {
10124 struct dwarf2_cu *spec_cu = cu;
9a619af0 10125
94af9270
KS
10126 return die_needs_namespace (die_specification (die, &spec_cu),
10127 spec_cu);
10128 }
10129
1c809c68 10130 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10131 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10132 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10133 return 0;
10134 /* A variable in a lexical block of some kind does not need a
10135 namespace, even though in C++ such variables may be external
10136 and have a mangled name. */
10137 if (die->parent->tag == DW_TAG_lexical_block
10138 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10139 || die->parent->tag == DW_TAG_catch_block
10140 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10141 return 0;
10142 return 1;
94af9270
KS
10143
10144 default:
10145 return 0;
10146 }
10147}
10148
73b9be8b
KS
10149/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10150 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10151 defined for the given DIE. */
10152
10153static struct attribute *
10154dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10155{
10156 struct attribute *attr;
10157
10158 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10159 if (attr == NULL)
10160 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10161
10162 return attr;
10163}
10164
10165/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10166 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10167 defined for the given DIE. */
10168
10169static const char *
10170dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10171{
10172 const char *linkage_name;
10173
10174 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10175 if (linkage_name == NULL)
10176 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10177
787de330
TT
10178 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10179 See https://github.com/rust-lang/rust/issues/32925. */
10180 if (cu->language == language_rust && linkage_name != NULL
10181 && strchr (linkage_name, '{') != NULL)
10182 linkage_name = NULL;
10183
73b9be8b
KS
10184 return linkage_name;
10185}
10186
94af9270 10187/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10188 compute the physname for the object, which include a method's:
9c37b5ae 10189 - formal parameters (C++),
a766d390 10190 - receiver type (Go),
a766d390
DE
10191
10192 The term "physname" is a bit confusing.
10193 For C++, for example, it is the demangled name.
10194 For Go, for example, it's the mangled name.
94af9270 10195
af6b7be1
JB
10196 For Ada, return the DIE's linkage name rather than the fully qualified
10197 name. PHYSNAME is ignored..
10198
5989a64e 10199 The result is allocated on the objfile->per_bfd's obstack and
45940949 10200 canonicalized. */
94af9270
KS
10201
10202static const char *
15d034d0
TT
10203dwarf2_compute_name (const char *name,
10204 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10205 int physname)
10206{
5e22e966 10207 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10208
94af9270
KS
10209 if (name == NULL)
10210 name = dwarf2_name (die, cu);
10211
2ee7123e
DE
10212 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10213 but otherwise compute it by typename_concat inside GDB.
10214 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10215 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10216 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10217 will set the demangled name to the result of dwarf2_full_name, and it is
10218 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10219 if (cu->language == language_ada
10220 || (cu->language == language_fortran && physname))
10221 {
10222 /* For Ada unit, we prefer the linkage name over the name, as
10223 the former contains the exported name, which the user expects
10224 to be able to reference. Ideally, we want the user to be able
10225 to reference this entity using either natural or linkage name,
10226 but we haven't started looking at this enhancement yet. */
73b9be8b 10227 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10228
2ee7123e
DE
10229 if (linkage_name != NULL)
10230 return linkage_name;
f55ee35c
JK
10231 }
10232
94af9270
KS
10233 /* These are the only languages we know how to qualify names in. */
10234 if (name != NULL
9c37b5ae 10235 && (cu->language == language_cplus
c44af4eb
TT
10236 || cu->language == language_fortran || cu->language == language_d
10237 || cu->language == language_rust))
94af9270
KS
10238 {
10239 if (die_needs_namespace (die, cu))
10240 {
0d5cff50 10241 const char *prefix;
34a68019 10242 const char *canonical_name = NULL;
94af9270 10243
d7e74731
PA
10244 string_file buf;
10245
94af9270 10246 prefix = determine_prefix (die, cu);
94af9270
KS
10247 if (*prefix != '\0')
10248 {
43816ebc
TT
10249 gdb::unique_xmalloc_ptr<char> prefixed_name
10250 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10251
43816ebc 10252 buf.puts (prefixed_name.get ());
94af9270
KS
10253 }
10254 else
d7e74731 10255 buf.puts (name);
94af9270 10256
98bfdba5
PA
10257 /* Template parameters may be specified in the DIE's DW_AT_name, or
10258 as children with DW_TAG_template_type_param or
10259 DW_TAG_value_type_param. If the latter, add them to the name
10260 here. If the name already has template parameters, then
10261 skip this step; some versions of GCC emit both, and
10262 it is more efficient to use the pre-computed name.
10263
10264 Something to keep in mind about this process: it is very
10265 unlikely, or in some cases downright impossible, to produce
10266 something that will match the mangled name of a function.
10267 If the definition of the function has the same debug info,
10268 we should be able to match up with it anyway. But fallbacks
10269 using the minimal symbol, for instance to find a method
10270 implemented in a stripped copy of libstdc++, will not work.
10271 If we do not have debug info for the definition, we will have to
10272 match them up some other way.
10273
10274 When we do name matching there is a related problem with function
10275 templates; two instantiated function templates are allowed to
10276 differ only by their return types, which we do not add here. */
10277
10278 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10279 {
10280 struct attribute *attr;
10281 struct die_info *child;
10282 int first = 1;
10283
10284 die->building_fullname = 1;
10285
10286 for (child = die->child; child != NULL; child = child->sibling)
10287 {
10288 struct type *type;
12df843f 10289 LONGEST value;
d521ce57 10290 const gdb_byte *bytes;
98bfdba5
PA
10291 struct dwarf2_locexpr_baton *baton;
10292 struct value *v;
10293
10294 if (child->tag != DW_TAG_template_type_param
10295 && child->tag != DW_TAG_template_value_param)
10296 continue;
10297
10298 if (first)
10299 {
d7e74731 10300 buf.puts ("<");
98bfdba5
PA
10301 first = 0;
10302 }
10303 else
d7e74731 10304 buf.puts (", ");
98bfdba5
PA
10305
10306 attr = dwarf2_attr (child, DW_AT_type, cu);
10307 if (attr == NULL)
10308 {
b98664d3 10309 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10310 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10311 continue;
10312 }
10313 type = die_type (child, cu);
10314
10315 if (child->tag == DW_TAG_template_type_param)
10316 {
c1ec8cea
TT
10317 c_print_type (type, "", &buf, -1, 0, cu->language,
10318 &type_print_raw_options);
98bfdba5
PA
10319 continue;
10320 }
10321
10322 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10323 if (attr == NULL)
10324 {
b98664d3 10325 complaint (_("template parameter missing "
3e43a32a 10326 "DW_AT_const_value"));
d7e74731 10327 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10328 continue;
10329 }
10330
10331 dwarf2_const_value_attr (attr, type, name,
10332 &cu->comp_unit_obstack, cu,
10333 &value, &bytes, &baton);
10334
10335 if (TYPE_NOSIGN (type))
10336 /* GDB prints characters as NUMBER 'CHAR'. If that's
10337 changed, this can use value_print instead. */
d7e74731 10338 c_printchar (value, type, &buf);
98bfdba5
PA
10339 else
10340 {
10341 struct value_print_options opts;
10342
10343 if (baton != NULL)
10344 v = dwarf2_evaluate_loc_desc (type, NULL,
10345 baton->data,
10346 baton->size,
10347 baton->per_cu);
10348 else if (bytes != NULL)
10349 {
10350 v = allocate_value (type);
10351 memcpy (value_contents_writeable (v), bytes,
10352 TYPE_LENGTH (type));
10353 }
10354 else
10355 v = value_from_longest (type, value);
10356
3e43a32a
MS
10357 /* Specify decimal so that we do not depend on
10358 the radix. */
98bfdba5
PA
10359 get_formatted_print_options (&opts, 'd');
10360 opts.raw = 1;
d7e74731 10361 value_print (v, &buf, &opts);
98bfdba5 10362 release_value (v);
98bfdba5
PA
10363 }
10364 }
10365
10366 die->building_fullname = 0;
10367
10368 if (!first)
10369 {
10370 /* Close the argument list, with a space if necessary
10371 (nested templates). */
d7e74731
PA
10372 if (!buf.empty () && buf.string ().back () == '>')
10373 buf.puts (" >");
98bfdba5 10374 else
d7e74731 10375 buf.puts (">");
98bfdba5
PA
10376 }
10377 }
10378
9c37b5ae 10379 /* For C++ methods, append formal parameter type
94af9270 10380 information, if PHYSNAME. */
6e70227d 10381
94af9270 10382 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10383 && cu->language == language_cplus)
94af9270
KS
10384 {
10385 struct type *type = read_type_die (die, cu);
10386
d7e74731 10387 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10388 &type_print_raw_options);
94af9270 10389
9c37b5ae 10390 if (cu->language == language_cplus)
94af9270 10391 {
60430eff
DJ
10392 /* Assume that an artificial first parameter is
10393 "this", but do not crash if it is not. RealView
10394 marks unnamed (and thus unused) parameters as
10395 artificial; there is no way to differentiate
10396 the two cases. */
1f704f76 10397 if (type->num_fields () > 0
94af9270 10398 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10399 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10400 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10401 0))))
d7e74731 10402 buf.puts (" const");
94af9270
KS
10403 }
10404 }
10405
d7e74731 10406 const std::string &intermediate_name = buf.string ();
94af9270
KS
10407
10408 if (cu->language == language_cplus)
34a68019 10409 canonical_name
322a8516 10410 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10411 objfile);
34a68019
TT
10412
10413 /* If we only computed INTERMEDIATE_NAME, or if
10414 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10415 intern it. */
322a8516 10416 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10417 name = objfile->intern (intermediate_name);
34a68019
TT
10418 else
10419 name = canonical_name;
94af9270
KS
10420 }
10421 }
10422
10423 return name;
10424}
10425
0114d602
DJ
10426/* Return the fully qualified name of DIE, based on its DW_AT_name.
10427 If scope qualifiers are appropriate they will be added. The result
34a68019 10428 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10429 not have a name. NAME may either be from a previous call to
10430 dwarf2_name or NULL.
10431
9c37b5ae 10432 The output string will be canonicalized (if C++). */
0114d602
DJ
10433
10434static const char *
15d034d0 10435dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10436{
94af9270
KS
10437 return dwarf2_compute_name (name, die, cu, 0);
10438}
0114d602 10439
94af9270
KS
10440/* Construct a physname for the given DIE in CU. NAME may either be
10441 from a previous call to dwarf2_name or NULL. The result will be
10442 allocated on the objfile_objstack or NULL if the DIE does not have a
10443 name.
0114d602 10444
9c37b5ae 10445 The output string will be canonicalized (if C++). */
0114d602 10446
94af9270 10447static const char *
15d034d0 10448dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10449{
5e22e966 10450 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10451 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10452 int need_copy = 1;
10453
10454 /* In this case dwarf2_compute_name is just a shortcut not building anything
10455 on its own. */
10456 if (!die_needs_namespace (die, cu))
10457 return dwarf2_compute_name (name, die, cu, 1);
10458
906bb4c5
TT
10459 if (cu->language != language_rust)
10460 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10461
10462 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10463 has computed. */
791afaa2 10464 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10465 if (mangled != NULL)
900e11f9 10466 {
900e11f9 10467
59cc4834
JB
10468 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10469 {
10470 /* Do nothing (do not demangle the symbol name). */
10471 }
10472 else if (cu->language == language_go)
a766d390 10473 {
5e2db402
TT
10474 /* This is a lie, but we already lie to the caller new_symbol.
10475 new_symbol assumes we return the mangled name.
a766d390 10476 This just undoes that lie until things are cleaned up. */
a766d390
DE
10477 }
10478 else
10479 {
0eb876f5
JB
10480 /* Use DMGL_RET_DROP for C++ template functions to suppress
10481 their return type. It is easier for GDB users to search
10482 for such functions as `name(params)' than `long name(params)'.
10483 In such case the minimal symbol names do not match the full
10484 symbol names but for template functions there is never a need
10485 to look up their definition from their declaration so
10486 the only disadvantage remains the minimal symbol variant
10487 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10488 demangled.reset (gdb_demangle (mangled,
10489 (DMGL_PARAMS | DMGL_ANSI
10490 | DMGL_RET_DROP)));
a766d390 10491 }
900e11f9 10492 if (demangled)
791afaa2 10493 canon = demangled.get ();
900e11f9
JK
10494 else
10495 {
10496 canon = mangled;
10497 need_copy = 0;
10498 }
10499 }
10500
10501 if (canon == NULL || check_physname)
10502 {
10503 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10504
10505 if (canon != NULL && strcmp (physname, canon) != 0)
10506 {
10507 /* It may not mean a bug in GDB. The compiler could also
10508 compute DW_AT_linkage_name incorrectly. But in such case
10509 GDB would need to be bug-to-bug compatible. */
10510
b98664d3 10511 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10512 "(from linkage <%s>) - DIE at %s [in module %s]"),
10513 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10514 objfile_name (objfile));
900e11f9
JK
10515
10516 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10517 is available here - over computed PHYSNAME. It is safer
10518 against both buggy GDB and buggy compilers. */
10519
10520 retval = canon;
10521 }
10522 else
10523 {
10524 retval = physname;
10525 need_copy = 0;
10526 }
10527 }
10528 else
10529 retval = canon;
10530
10531 if (need_copy)
be1e3d3e 10532 retval = objfile->intern (retval);
900e11f9 10533
900e11f9 10534 return retval;
0114d602
DJ
10535}
10536
74921315
KS
10537/* Inspect DIE in CU for a namespace alias. If one exists, record
10538 a new symbol for it.
10539
10540 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10541
10542static int
10543read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10544{
10545 struct attribute *attr;
10546
10547 /* If the die does not have a name, this is not a namespace
10548 alias. */
10549 attr = dwarf2_attr (die, DW_AT_name, cu);
10550 if (attr != NULL)
10551 {
10552 int num;
10553 struct die_info *d = die;
10554 struct dwarf2_cu *imported_cu = cu;
10555
10556 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10557 keep inspecting DIEs until we hit the underlying import. */
10558#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10559 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10560 {
10561 attr = dwarf2_attr (d, DW_AT_import, cu);
10562 if (attr == NULL)
10563 break;
10564
10565 d = follow_die_ref (d, attr, &imported_cu);
10566 if (d->tag != DW_TAG_imported_declaration)
10567 break;
10568 }
10569
10570 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10571 {
b98664d3 10572 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10573 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10574 return 0;
10575 }
10576
10577 if (attr != NULL)
10578 {
10579 struct type *type;
0826b30a 10580 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10581
9c541725 10582 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10583 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10584 {
10585 /* This declaration is a global namespace alias. Add
10586 a symbol for it whose type is the aliased namespace. */
10587 new_symbol (die, type, cu);
10588 return 1;
10589 }
10590 }
10591 }
10592
10593 return 0;
10594}
10595
22cee43f 10596/* Return the using directives repository (global or local?) to use in the
804d2729 10597 current context for CU.
22cee43f
PMR
10598
10599 For Ada, imported declarations can materialize renamings, which *may* be
10600 global. However it is impossible (for now?) in DWARF to distinguish
10601 "external" imported declarations and "static" ones. As all imported
10602 declarations seem to be static in all other languages, make them all CU-wide
10603 global only in Ada. */
10604
10605static struct using_direct **
804d2729 10606using_directives (struct dwarf2_cu *cu)
22cee43f 10607{
c24bdb02
KS
10608 if (cu->language == language_ada
10609 && cu->get_builder ()->outermost_context_p ())
10610 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10611 else
c24bdb02 10612 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10613}
10614
27aa8d6a
SW
10615/* Read the import statement specified by the given die and record it. */
10616
10617static void
10618read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10619{
5e22e966 10620 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10621 struct attribute *import_attr;
32019081 10622 struct die_info *imported_die, *child_die;
de4affc9 10623 struct dwarf2_cu *imported_cu;
27aa8d6a 10624 const char *imported_name;
794684b6 10625 const char *imported_name_prefix;
13387711
SW
10626 const char *canonical_name;
10627 const char *import_alias;
10628 const char *imported_declaration = NULL;
794684b6 10629 const char *import_prefix;
eb1e02fd 10630 std::vector<const char *> excludes;
13387711 10631
27aa8d6a
SW
10632 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10633 if (import_attr == NULL)
10634 {
b98664d3 10635 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10636 dwarf_tag_name (die->tag));
10637 return;
10638 }
10639
de4affc9
CC
10640 imported_cu = cu;
10641 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10642 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10643 if (imported_name == NULL)
10644 {
10645 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10646
10647 The import in the following code:
10648 namespace A
10649 {
10650 typedef int B;
10651 }
10652
10653 int main ()
10654 {
10655 using A::B;
10656 B b;
10657 return b;
10658 }
10659
10660 ...
10661 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10662 <52> DW_AT_decl_file : 1
10663 <53> DW_AT_decl_line : 6
10664 <54> DW_AT_import : <0x75>
10665 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10666 <59> DW_AT_name : B
10667 <5b> DW_AT_decl_file : 1
10668 <5c> DW_AT_decl_line : 2
10669 <5d> DW_AT_type : <0x6e>
10670 ...
10671 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10672 <76> DW_AT_byte_size : 4
10673 <77> DW_AT_encoding : 5 (signed)
10674
10675 imports the wrong die ( 0x75 instead of 0x58 ).
10676 This case will be ignored until the gcc bug is fixed. */
10677 return;
10678 }
10679
82856980
SW
10680 /* Figure out the local name after import. */
10681 import_alias = dwarf2_name (die, cu);
27aa8d6a 10682
794684b6
SW
10683 /* Figure out where the statement is being imported to. */
10684 import_prefix = determine_prefix (die, cu);
10685
10686 /* Figure out what the scope of the imported die is and prepend it
10687 to the name of the imported die. */
de4affc9 10688 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10689
f55ee35c
JK
10690 if (imported_die->tag != DW_TAG_namespace
10691 && imported_die->tag != DW_TAG_module)
794684b6 10692 {
13387711
SW
10693 imported_declaration = imported_name;
10694 canonical_name = imported_name_prefix;
794684b6 10695 }
13387711 10696 else if (strlen (imported_name_prefix) > 0)
12aaed36 10697 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10698 imported_name_prefix,
10699 (cu->language == language_d ? "." : "::"),
10700 imported_name, (char *) NULL);
13387711
SW
10701 else
10702 canonical_name = imported_name;
794684b6 10703
32019081
JK
10704 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10705 for (child_die = die->child; child_die && child_die->tag;
436c571c 10706 child_die = child_die->sibling)
32019081
JK
10707 {
10708 /* DWARF-4: A Fortran use statement with a “rename list” may be
10709 represented by an imported module entry with an import attribute
10710 referring to the module and owned entries corresponding to those
10711 entities that are renamed as part of being imported. */
10712
10713 if (child_die->tag != DW_TAG_imported_declaration)
10714 {
b98664d3 10715 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10716 "- DIE at %s [in module %s]"),
10717 sect_offset_str (child_die->sect_off),
10718 objfile_name (objfile));
32019081
JK
10719 continue;
10720 }
10721
10722 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10723 if (import_attr == NULL)
10724 {
b98664d3 10725 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10726 dwarf_tag_name (child_die->tag));
10727 continue;
10728 }
10729
10730 imported_cu = cu;
10731 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10732 &imported_cu);
10733 imported_name = dwarf2_name (imported_die, imported_cu);
10734 if (imported_name == NULL)
10735 {
b98664d3 10736 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10737 "imported name - DIE at %s [in module %s]"),
10738 sect_offset_str (child_die->sect_off),
10739 objfile_name (objfile));
32019081
JK
10740 continue;
10741 }
10742
eb1e02fd 10743 excludes.push_back (imported_name);
32019081
JK
10744
10745 process_die (child_die, cu);
10746 }
10747
804d2729 10748 add_using_directive (using_directives (cu),
22cee43f
PMR
10749 import_prefix,
10750 canonical_name,
10751 import_alias,
10752 imported_declaration,
10753 excludes,
10754 0,
10755 &objfile->objfile_obstack);
27aa8d6a
SW
10756}
10757
5230b05a
WT
10758/* ICC<14 does not output the required DW_AT_declaration on incomplete
10759 types, but gives them a size of zero. Starting with version 14,
10760 ICC is compatible with GCC. */
10761
9068261f 10762static bool
5230b05a
WT
10763producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10764{
10765 if (!cu->checked_producer)
10766 check_producer (cu);
10767
10768 return cu->producer_is_icc_lt_14;
10769}
10770
eb77c9df
AB
10771/* ICC generates a DW_AT_type for C void functions. This was observed on
10772 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10773 which says that void functions should not have a DW_AT_type. */
10774
10775static bool
10776producer_is_icc (struct dwarf2_cu *cu)
10777{
10778 if (!cu->checked_producer)
10779 check_producer (cu);
10780
10781 return cu->producer_is_icc;
10782}
10783
1b80a9fa
JK
10784/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10785 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10786 this, it was first present in GCC release 4.3.0. */
10787
9068261f 10788static bool
1b80a9fa
JK
10789producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10790{
10791 if (!cu->checked_producer)
10792 check_producer (cu);
10793
10794 return cu->producer_is_gcc_lt_4_3;
10795}
10796
d721ba37
PA
10797static file_and_directory
10798find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10799{
d721ba37
PA
10800 file_and_directory res;
10801
9291a0cd
TT
10802 /* Find the filename. Do not use dwarf2_name here, since the filename
10803 is not a source language identifier. */
d721ba37
PA
10804 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10805 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10806
d721ba37
PA
10807 if (res.comp_dir == NULL
10808 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10809 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10810 {
d721ba37
PA
10811 res.comp_dir_storage = ldirname (res.name);
10812 if (!res.comp_dir_storage.empty ())
10813 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10814 }
d721ba37 10815 if (res.comp_dir != NULL)
9291a0cd
TT
10816 {
10817 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10818 directory, get rid of it. */
d721ba37 10819 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10820
d721ba37
PA
10821 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10822 res.comp_dir = cp + 1;
9291a0cd
TT
10823 }
10824
d721ba37
PA
10825 if (res.name == NULL)
10826 res.name = "<unknown>";
10827
10828 return res;
9291a0cd
TT
10829}
10830
f4dc4d17
DE
10831/* Handle DW_AT_stmt_list for a compilation unit.
10832 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10833 COMP_DIR is the compilation directory. LOWPC is passed to
10834 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10835
10836static void
10837handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10838 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10839{
5e22e966 10840 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10841 struct attribute *attr;
527f3840
JK
10842 struct line_header line_header_local;
10843 hashval_t line_header_local_hash;
527f3840
JK
10844 void **slot;
10845 int decode_mapping;
2ab95328 10846
f4dc4d17
DE
10847 gdb_assert (! cu->per_cu->is_debug_types);
10848
2ab95328 10849 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10850 if (attr == NULL)
10851 return;
10852
9c541725 10853 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10854
10855 /* The line header hash table is only created if needed (it exists to
10856 prevent redundant reading of the line table for partial_units).
10857 If we're given a partial_unit, we'll need it. If we're given a
10858 compile_unit, then use the line header hash table if it's already
10859 created, but don't create one just yet. */
10860
5989a64e 10861 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10862 && die->tag == DW_TAG_partial_unit)
2ab95328 10863 {
5989a64e 10864 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10865 .reset (htab_create_alloc (127, line_header_hash_voidp,
10866 line_header_eq_voidp,
10867 free_line_header_voidp,
10868 xcalloc, xfree));
527f3840 10869 }
2ab95328 10870
9c541725 10871 line_header_local.sect_off = line_offset;
527f3840
JK
10872 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10873 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10874 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10875 {
5989a64e 10876 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10877 &line_header_local,
10878 line_header_local_hash, NO_INSERT);
10879
10880 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10881 is not present in *SLOT (since if there is something in *SLOT then
10882 it will be for a partial_unit). */
10883 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10884 {
527f3840 10885 gdb_assert (*slot != NULL);
9a3c8263 10886 cu->line_header = (struct line_header *) *slot;
527f3840 10887 return;
dee91e82 10888 }
2ab95328 10889 }
527f3840
JK
10890
10891 /* dwarf_decode_line_header does not yet provide sufficient information.
10892 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10893 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10894 if (lh == NULL)
527f3840 10895 return;
4c8aa72d
PA
10896
10897 cu->line_header = lh.release ();
10898 cu->line_header_die_owner = die;
527f3840 10899
5989a64e 10900 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10901 slot = NULL;
10902 else
10903 {
5989a64e 10904 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10905 &line_header_local,
10906 line_header_local_hash, INSERT);
10907 gdb_assert (slot != NULL);
10908 }
10909 if (slot != NULL && *slot == NULL)
10910 {
10911 /* This newly decoded line number information unit will be owned
10912 by line_header_hash hash table. */
10913 *slot = cu->line_header;
4c8aa72d 10914 cu->line_header_die_owner = NULL;
527f3840
JK
10915 }
10916 else
10917 {
10918 /* We cannot free any current entry in (*slot) as that struct line_header
10919 may be already used by multiple CUs. Create only temporary decoded
10920 line_header for this CU - it may happen at most once for each line
10921 number information unit. And if we're not using line_header_hash
10922 then this is what we want as well. */
10923 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10924 }
10925 decode_mapping = (die->tag != DW_TAG_partial_unit);
10926 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10927 decode_mapping);
fff8551c 10928
2ab95328
TT
10929}
10930
95554aad 10931/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10932
c906108c 10933static void
e7c27a73 10934read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10935{
5e22e966 10936 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10937 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10938 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10939 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10940 CORE_ADDR highpc = ((CORE_ADDR) 0);
10941 struct attribute *attr;
c906108c 10942 struct die_info *child_die;
e142c38c 10943 CORE_ADDR baseaddr;
6e70227d 10944
380618d6 10945 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10946 baseaddr = objfile->text_section_offset ();
c906108c 10947
fae299cd 10948 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10949
10950 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10951 from finish_block. */
2acceee2 10952 if (lowpc == ((CORE_ADDR) -1))
c906108c 10953 lowpc = highpc;
3e29f34a 10954 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10955
d721ba37 10956 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10957
f4b8a18d
KW
10958 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10959 standardised yet. As a workaround for the language detection we fall
10960 back to the DW_AT_producer string. */
10961 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10962 cu->language = language_opencl;
10963
3019eac3
DE
10964 /* Similar hack for Go. */
10965 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10966 set_cu_language (DW_LANG_Go, cu);
10967
c24bdb02 10968 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10969
10970 /* Decode line number information if present. We do this before
10971 processing child DIEs, so that the line header table is available
10972 for DW_AT_decl_file. */
d721ba37 10973 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10974
10975 /* Process all dies in compilation unit. */
10976 if (die->child != NULL)
10977 {
10978 child_die = die->child;
10979 while (child_die && child_die->tag)
10980 {
10981 process_die (child_die, cu);
436c571c 10982 child_die = child_die->sibling;
3019eac3
DE
10983 }
10984 }
10985
10986 /* Decode macro information, if present. Dwarf 2 macro information
10987 refers to information in the line number info statement program
10988 header, so we can only read it if we've read the header
10989 successfully. */
0af92d60
JK
10990 attr = dwarf2_attr (die, DW_AT_macros, cu);
10991 if (attr == NULL)
10992 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10993 if (attr && cu->line_header)
10994 {
10995 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10996 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10997
43f3e411 10998 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10999 }
11000 else
11001 {
11002 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11003 if (attr && cu->line_header)
11004 {
11005 unsigned int macro_offset = DW_UNSND (attr);
11006
43f3e411 11007 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11008 }
11009 }
3019eac3
DE
11010}
11011
c24bdb02
KS
11012void
11013dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11014{
f4dc4d17
DE
11015 struct type_unit_group *tu_group;
11016 int first_time;
3019eac3 11017 struct attribute *attr;
9c541725 11018 unsigned int i;
0186c6a7 11019 struct signatured_type *sig_type;
3019eac3 11020
f4dc4d17 11021 gdb_assert (per_cu->is_debug_types);
0186c6a7 11022 sig_type = (struct signatured_type *) per_cu;
3019eac3 11023
c24bdb02 11024 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11025
f4dc4d17 11026 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11027 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11028 if (sig_type->type_unit_group == NULL)
c24bdb02 11029 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11030 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11031
11032 /* If we've already processed this stmt_list there's no real need to
11033 do it again, we could fake it and just recreate the part we need
11034 (file name,index -> symtab mapping). If data shows this optimization
11035 is useful we can do it then. */
43f3e411 11036 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11037
11038 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11039 debug info. */
fff8551c 11040 line_header_up lh;
f4dc4d17 11041 if (attr != NULL)
3019eac3 11042 {
9c541725 11043 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11044 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11045 }
11046 if (lh == NULL)
11047 {
11048 if (first_time)
c24bdb02 11049 start_symtab ("", NULL, 0);
f4dc4d17
DE
11050 else
11051 {
11052 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11053 gdb_assert (m_builder == nullptr);
804d2729 11054 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11055 m_builder.reset (new struct buildsym_compunit
11056 (COMPUNIT_OBJFILE (cust), "",
11057 COMPUNIT_DIRNAME (cust),
11058 compunit_language (cust),
11059 0, cust));
770479f2 11060 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11061 }
f4dc4d17 11062 return;
3019eac3
DE
11063 }
11064
c24bdb02
KS
11065 line_header = lh.release ();
11066 line_header_die_owner = die;
3019eac3 11067
f4dc4d17
DE
11068 if (first_time)
11069 {
c24bdb02 11070 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11071
1fd60fc0
DE
11072 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11073 still initializing it, and our caller (a few levels up)
11074 process_full_type_unit still needs to know if this is the first
11075 time. */
11076
4ac93832
TT
11077 tu_group->symtabs
11078 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11079 struct symtab *, line_header->file_names_size ());
3019eac3 11080
7ba99d21
AT
11081 auto &file_names = line_header->file_names ();
11082 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11083 {
7ba99d21 11084 file_entry &fe = file_names[i];
c24bdb02
KS
11085 dwarf2_start_subfile (this, fe.name,
11086 fe.include_dir (line_header));
11087 buildsym_compunit *b = get_builder ();
11088 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11089 {
4c8aa72d
PA
11090 /* NOTE: start_subfile will recognize when it's been
11091 passed a file it has already seen. So we can't
11092 assume there's a simple mapping from
11093 cu->line_header->file_names to subfiles, plus
11094 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11095 b->get_current_subfile ()->symtab
11096 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11097 }
11098
c24bdb02 11099 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11100 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11101 }
11102 }
11103 else
3019eac3 11104 {
c24bdb02 11105 gdb_assert (m_builder == nullptr);
804d2729 11106 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11107 m_builder.reset (new struct buildsym_compunit
11108 (COMPUNIT_OBJFILE (cust), "",
11109 COMPUNIT_DIRNAME (cust),
11110 compunit_language (cust),
11111 0, cust));
770479f2 11112 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11113
7ba99d21
AT
11114 auto &file_names = line_header->file_names ();
11115 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11116 {
7ba99d21 11117 file_entry &fe = file_names[i];
4c8aa72d 11118 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11119 }
3019eac3
DE
11120 }
11121
f4dc4d17
DE
11122 /* The main symtab is allocated last. Type units don't have DW_AT_name
11123 so they don't have a "real" (so to speak) symtab anyway.
11124 There is later code that will assign the main symtab to all symbols
11125 that don't have one. We need to handle the case of a symbol with a
11126 missing symtab (DW_AT_decl_file) anyway. */
11127}
3019eac3 11128
f4dc4d17
DE
11129/* Process DW_TAG_type_unit.
11130 For TUs we want to skip the first top level sibling if it's not the
11131 actual type being defined by this TU. In this case the first top
11132 level sibling is there to provide context only. */
3019eac3 11133
f4dc4d17
DE
11134static void
11135read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11136{
11137 struct die_info *child_die;
3019eac3 11138
f4dc4d17
DE
11139 prepare_one_comp_unit (cu, die, language_minimal);
11140
11141 /* Initialize (or reinitialize) the machinery for building symtabs.
11142 We do this before processing child DIEs, so that the line header table
11143 is available for DW_AT_decl_file. */
c24bdb02 11144 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11145
11146 if (die->child != NULL)
11147 {
11148 child_die = die->child;
11149 while (child_die && child_die->tag)
11150 {
11151 process_die (child_die, cu);
436c571c 11152 child_die = child_die->sibling;
f4dc4d17
DE
11153 }
11154 }
3019eac3
DE
11155}
11156\f
80626a55
DE
11157/* DWO/DWP files.
11158
11159 http://gcc.gnu.org/wiki/DebugFission
11160 http://gcc.gnu.org/wiki/DebugFissionDWP
11161
11162 To simplify handling of both DWO files ("object" files with the DWARF info)
11163 and DWP files (a file with the DWOs packaged up into one file), we treat
11164 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11165
11166static hashval_t
11167hash_dwo_file (const void *item)
11168{
9a3c8263 11169 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11170 hashval_t hash;
3019eac3 11171
a2ce51a0
DE
11172 hash = htab_hash_string (dwo_file->dwo_name);
11173 if (dwo_file->comp_dir != NULL)
11174 hash += htab_hash_string (dwo_file->comp_dir);
11175 return hash;
3019eac3
DE
11176}
11177
11178static int
11179eq_dwo_file (const void *item_lhs, const void *item_rhs)
11180{
9a3c8263
SM
11181 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11182 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11183
a2ce51a0
DE
11184 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11185 return 0;
11186 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11187 return lhs->comp_dir == rhs->comp_dir;
11188 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11189}
11190
11191/* Allocate a hash table for DWO files. */
11192
51ac9db5 11193static htab_up
298e9637 11194allocate_dwo_file_hash_table ()
3019eac3 11195{
51ac9db5
SM
11196 auto delete_dwo_file = [] (void *item)
11197 {
11198 struct dwo_file *dwo_file = (struct dwo_file *) item;
11199
11200 delete dwo_file;
11201 };
11202
bc68fb19
TT
11203 return htab_up (htab_create_alloc (41,
11204 hash_dwo_file,
11205 eq_dwo_file,
11206 delete_dwo_file,
11207 xcalloc, xfree));
3019eac3
DE
11208}
11209
80626a55
DE
11210/* Lookup DWO file DWO_NAME. */
11211
11212static void **
ed2dc618
SM
11213lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11214 const char *dwo_name,
11215 const char *comp_dir)
80626a55
DE
11216{
11217 struct dwo_file find_entry;
11218 void **slot;
11219
5989a64e
SM
11220 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11221 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11222
0ac5b59e
DE
11223 find_entry.dwo_name = dwo_name;
11224 find_entry.comp_dir = comp_dir;
5989a64e 11225 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11226 INSERT);
80626a55
DE
11227
11228 return slot;
11229}
11230
3019eac3
DE
11231static hashval_t
11232hash_dwo_unit (const void *item)
11233{
9a3c8263 11234 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11235
11236 /* This drops the top 32 bits of the id, but is ok for a hash. */
11237 return dwo_unit->signature;
11238}
11239
11240static int
11241eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11242{
9a3c8263
SM
11243 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11244 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11245
11246 /* The signature is assumed to be unique within the DWO file.
11247 So while object file CU dwo_id's always have the value zero,
11248 that's OK, assuming each object file DWO file has only one CU,
11249 and that's the rule for now. */
11250 return lhs->signature == rhs->signature;
11251}
11252
11253/* Allocate a hash table for DWO CUs,TUs.
11254 There is one of these tables for each of CUs,TUs for each DWO file. */
11255
b0b6a987 11256static htab_up
298e9637 11257allocate_dwo_unit_table ()
3019eac3
DE
11258{
11259 /* Start out with a pretty small number.
11260 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11261 return htab_up (htab_create_alloc (3,
11262 hash_dwo_unit,
11263 eq_dwo_unit,
11264 NULL, xcalloc, xfree));
3019eac3
DE
11265}
11266
19c3d4c9 11267/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11268
11269static void
19c3d4c9
DE
11270create_dwo_cu_reader (const struct die_reader_specs *reader,
11271 const gdb_byte *info_ptr,
11272 struct die_info *comp_unit_die,
c0ab21c2
TT
11273 struct dwo_file *dwo_file,
11274 struct dwo_unit *dwo_unit)
3019eac3
DE
11275{
11276 struct dwarf2_cu *cu = reader->cu;
9c541725 11277 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11278 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11279
a084a2a6
AT
11280 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11281 if (!signature.has_value ())
3019eac3 11282 {
b98664d3 11283 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11284 " its dwo_id [in module %s]"),
9d8780f0 11285 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11286 return;
11287 }
11288
3019eac3 11289 dwo_unit->dwo_file = dwo_file;
a084a2a6 11290 dwo_unit->signature = *signature;
8a0459fd 11291 dwo_unit->section = section;
9c541725 11292 dwo_unit->sect_off = sect_off;
3019eac3
DE
11293 dwo_unit->length = cu->per_cu->length;
11294
b4f54984 11295 if (dwarf_read_debug)
9d8780f0
SM
11296 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11297 sect_offset_str (sect_off),
9c541725 11298 hex_string (dwo_unit->signature));
3019eac3
DE
11299}
11300
33c5cd75 11301/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11302 Note: This function processes DWO files only, not DWP files. */
3019eac3 11303
33c5cd75 11304static void
ed2dc618 11305create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11306 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11307 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11308{
11309 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11310 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11311 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11312
96b79293 11313 section.read (objfile);
33c5cd75 11314 info_ptr = section.buffer;
3019eac3
DE
11315
11316 if (info_ptr == NULL)
33c5cd75 11317 return;
3019eac3 11318
b4f54984 11319 if (dwarf_read_debug)
19c3d4c9
DE
11320 {
11321 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11322 section.get_name (),
11323 section.get_file_name ());
19c3d4c9 11324 }
3019eac3 11325
33c5cd75 11326 end_ptr = info_ptr + section.size;
3019eac3
DE
11327 while (info_ptr < end_ptr)
11328 {
11329 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11330 struct dwo_unit read_unit {};
33c5cd75
DB
11331 struct dwo_unit *dwo_unit;
11332 void **slot;
11333 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11334
11335 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11336 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 11337 per_cu.per_bfd = per_bfd;
3019eac3 11338 per_cu.is_debug_types = 0;
33c5cd75
DB
11339 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11340 per_cu.section = &section;
11341
ab432490 11342 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11343 if (!reader.dummy_p)
11344 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11345 &dwo_file, &read_unit);
33c5cd75
DB
11346 info_ptr += per_cu.length;
11347
11348 // If the unit could not be parsed, skip it.
c0ab21c2 11349 if (read_unit.dwo_file == NULL)
33c5cd75 11350 continue;
3019eac3 11351
33c5cd75 11352 if (cus_htab == NULL)
298e9637 11353 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11354
1859c670 11355 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11356 struct dwo_unit);
c0ab21c2 11357 *dwo_unit = read_unit;
b0b6a987 11358 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11359 gdb_assert (slot != NULL);
11360 if (*slot != NULL)
19c3d4c9 11361 {
33c5cd75
DB
11362 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11363 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11364
b98664d3 11365 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11366 " the entry at offset %s, signature %s"),
11367 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11368 hex_string (dwo_unit->signature));
19c3d4c9 11369 }
33c5cd75 11370 *slot = (void *)dwo_unit;
3019eac3 11371 }
3019eac3
DE
11372}
11373
80626a55
DE
11374/* DWP file .debug_{cu,tu}_index section format:
11375 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11376
d2415c6c
DE
11377 DWP Version 1:
11378
80626a55
DE
11379 Both index sections have the same format, and serve to map a 64-bit
11380 signature to a set of section numbers. Each section begins with a header,
11381 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11382 indexes, and a pool of 32-bit section numbers. The index sections will be
11383 aligned at 8-byte boundaries in the file.
11384
d2415c6c
DE
11385 The index section header consists of:
11386
11387 V, 32 bit version number
11388 -, 32 bits unused
11389 N, 32 bit number of compilation units or type units in the index
11390 M, 32 bit number of slots in the hash table
80626a55 11391
d2415c6c 11392 Numbers are recorded using the byte order of the application binary.
80626a55 11393
d2415c6c
DE
11394 The hash table begins at offset 16 in the section, and consists of an array
11395 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11396 order of the application binary). Unused slots in the hash table are 0.
11397 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11398
d2415c6c
DE
11399 The parallel table begins immediately after the hash table
11400 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11401 array of 32-bit indexes (using the byte order of the application binary),
11402 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11403 table contains a 32-bit index into the pool of section numbers. For unused
11404 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11405
73869dc2
DE
11406 The pool of section numbers begins immediately following the hash table
11407 (at offset 16 + 12 * M from the beginning of the section). The pool of
11408 section numbers consists of an array of 32-bit words (using the byte order
11409 of the application binary). Each item in the array is indexed starting
11410 from 0. The hash table entry provides the index of the first section
11411 number in the set. Additional section numbers in the set follow, and the
11412 set is terminated by a 0 entry (section number 0 is not used in ELF).
11413
11414 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11415 section must be the first entry in the set, and the .debug_abbrev.dwo must
11416 be the second entry. Other members of the set may follow in any order.
11417
11418 ---
11419
11420 DWP Version 2:
11421
11422 DWP Version 2 combines all the .debug_info, etc. sections into one,
11423 and the entries in the index tables are now offsets into these sections.
11424 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11425 section.
11426
11427 Index Section Contents:
11428 Header
11429 Hash Table of Signatures dwp_hash_table.hash_table
11430 Parallel Table of Indices dwp_hash_table.unit_table
11431 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11432 Table of Section Sizes dwp_hash_table.v2.sizes
11433
11434 The index section header consists of:
11435
11436 V, 32 bit version number
11437 L, 32 bit number of columns in the table of section offsets
11438 N, 32 bit number of compilation units or type units in the index
11439 M, 32 bit number of slots in the hash table
11440
11441 Numbers are recorded using the byte order of the application binary.
11442
11443 The hash table has the same format as version 1.
11444 The parallel table of indices has the same format as version 1,
11445 except that the entries are origin-1 indices into the table of sections
11446 offsets and the table of section sizes.
11447
11448 The table of offsets begins immediately following the parallel table
11449 (at offset 16 + 12 * M from the beginning of the section). The table is
11450 a two-dimensional array of 32-bit words (using the byte order of the
11451 application binary), with L columns and N+1 rows, in row-major order.
11452 Each row in the array is indexed starting from 0. The first row provides
11453 a key to the remaining rows: each column in this row provides an identifier
11454 for a debug section, and the offsets in the same column of subsequent rows
11455 refer to that section. The section identifiers are:
11456
11457 DW_SECT_INFO 1 .debug_info.dwo
11458 DW_SECT_TYPES 2 .debug_types.dwo
11459 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11460 DW_SECT_LINE 4 .debug_line.dwo
11461 DW_SECT_LOC 5 .debug_loc.dwo
11462 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11463 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11464 DW_SECT_MACRO 8 .debug_macro.dwo
11465
11466 The offsets provided by the CU and TU index sections are the base offsets
11467 for the contributions made by each CU or TU to the corresponding section
11468 in the package file. Each CU and TU header contains an abbrev_offset
11469 field, used to find the abbreviations table for that CU or TU within the
11470 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11471 be interpreted as relative to the base offset given in the index section.
11472 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11473 should be interpreted as relative to the base offset for .debug_line.dwo,
11474 and offsets into other debug sections obtained from DWARF attributes should
11475 also be interpreted as relative to the corresponding base offset.
11476
11477 The table of sizes begins immediately following the table of offsets.
11478 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11479 with L columns and N rows, in row-major order. Each row in the array is
11480 indexed starting from 1 (row 0 is shared by the two tables).
11481
11482 ---
11483
11484 Hash table lookup is handled the same in version 1 and 2:
11485
11486 We assume that N and M will not exceed 2^32 - 1.
11487 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11488
d2415c6c
DE
11489 Given a 64-bit compilation unit signature or a type signature S, an entry
11490 in the hash table is located as follows:
80626a55 11491
d2415c6c
DE
11492 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11493 the low-order k bits all set to 1.
80626a55 11494
d2415c6c 11495 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11496
d2415c6c
DE
11497 3) If the hash table entry at index H matches the signature, use that
11498 entry. If the hash table entry at index H is unused (all zeroes),
11499 terminate the search: the signature is not present in the table.
80626a55 11500
d2415c6c 11501 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11502
d2415c6c 11503 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11504 to stop at an unused slot or find the match. */
80626a55
DE
11505
11506/* Create a hash table to map DWO IDs to their CU/TU entry in
11507 .debug_{info,types}.dwo in DWP_FILE.
11508 Returns NULL if there isn't one.
11509 Note: This function processes DWP files only, not DWO files. */
11510
11511static struct dwp_hash_table *
ed2dc618
SM
11512create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11513 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11514{
11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11516 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11517 const gdb_byte *index_ptr, *index_end;
80626a55 11518 struct dwarf2_section_info *index;
73869dc2 11519 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11520 struct dwp_hash_table *htab;
11521
11522 if (is_debug_types)
11523 index = &dwp_file->sections.tu_index;
11524 else
11525 index = &dwp_file->sections.cu_index;
11526
96b79293 11527 if (index->empty ())
80626a55 11528 return NULL;
96b79293 11529 index->read (objfile);
80626a55
DE
11530
11531 index_ptr = index->buffer;
11532 index_end = index_ptr + index->size;
11533
11534 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11535 index_ptr += 4;
11536 if (version == 2)
11537 nr_columns = read_4_bytes (dbfd, index_ptr);
11538 else
11539 nr_columns = 0;
11540 index_ptr += 4;
80626a55
DE
11541 nr_units = read_4_bytes (dbfd, index_ptr);
11542 index_ptr += 4;
11543 nr_slots = read_4_bytes (dbfd, index_ptr);
11544 index_ptr += 4;
11545
73869dc2 11546 if (version != 1 && version != 2)
80626a55 11547 {
21aa081e 11548 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11549 " [in module %s]"),
21aa081e 11550 pulongest (version), dwp_file->name);
80626a55
DE
11551 }
11552 if (nr_slots != (nr_slots & -nr_slots))
11553 {
21aa081e 11554 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11555 " is not power of 2 [in module %s]"),
21aa081e 11556 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11557 }
11558
5989a64e 11559 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11560 htab->version = version;
11561 htab->nr_columns = nr_columns;
80626a55
DE
11562 htab->nr_units = nr_units;
11563 htab->nr_slots = nr_slots;
11564 htab->hash_table = index_ptr;
11565 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11566
11567 /* Exit early if the table is empty. */
11568 if (nr_slots == 0 || nr_units == 0
11569 || (version == 2 && nr_columns == 0))
11570 {
11571 /* All must be zero. */
11572 if (nr_slots != 0 || nr_units != 0
11573 || (version == 2 && nr_columns != 0))
11574 {
b98664d3 11575 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11576 " all zero [in modules %s]"),
11577 dwp_file->name);
11578 }
11579 return htab;
11580 }
11581
11582 if (version == 1)
11583 {
11584 htab->section_pool.v1.indices =
11585 htab->unit_table + sizeof (uint32_t) * nr_slots;
11586 /* It's harder to decide whether the section is too small in v1.
11587 V1 is deprecated anyway so we punt. */
11588 }
11589 else
11590 {
11591 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11592 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11593 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11594 /* Reverse map for error checking. */
11595 int ids_seen[DW_SECT_MAX + 1];
11596 int i;
11597
11598 if (nr_columns < 2)
11599 {
11600 error (_("Dwarf Error: bad DWP hash table, too few columns"
11601 " in section table [in module %s]"),
11602 dwp_file->name);
11603 }
11604 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11605 {
11606 error (_("Dwarf Error: bad DWP hash table, too many columns"
11607 " in section table [in module %s]"),
11608 dwp_file->name);
11609 }
04fd5eed
GB
11610 memset (ids, 255, sizeof_ids);
11611 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11612 for (i = 0; i < nr_columns; ++i)
11613 {
11614 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11615
11616 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11617 {
11618 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11619 " in section table [in module %s]"),
11620 id, dwp_file->name);
11621 }
11622 if (ids_seen[id] != -1)
11623 {
11624 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11625 " id %d in section table [in module %s]"),
11626 id, dwp_file->name);
11627 }
11628 ids_seen[id] = i;
11629 ids[i] = id;
11630 }
11631 /* Must have exactly one info or types section. */
11632 if (((ids_seen[DW_SECT_INFO] != -1)
11633 + (ids_seen[DW_SECT_TYPES] != -1))
11634 != 1)
11635 {
11636 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11637 " DWO info/types section [in module %s]"),
11638 dwp_file->name);
11639 }
11640 /* Must have an abbrev section. */
11641 if (ids_seen[DW_SECT_ABBREV] == -1)
11642 {
11643 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11644 " section [in module %s]"),
11645 dwp_file->name);
11646 }
11647 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11648 htab->section_pool.v2.sizes =
11649 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11650 * nr_units * nr_columns);
11651 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11652 * nr_units * nr_columns))
11653 > index_end)
11654 {
11655 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11656 " [in module %s]"),
11657 dwp_file->name);
11658 }
11659 }
80626a55
DE
11660
11661 return htab;
11662}
11663
11664/* Update SECTIONS with the data from SECTP.
11665
11666 This function is like the other "locate" section routines that are
11667 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11668 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11669
11670 The result is non-zero for success, or zero if an error was found. */
11671
11672static int
73869dc2
DE
11673locate_v1_virtual_dwo_sections (asection *sectp,
11674 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11675{
11676 const struct dwop_section_names *names = &dwop_section_names;
11677
11678 if (section_is_p (sectp->name, &names->abbrev_dwo))
11679 {
11680 /* There can be only one. */
049412e3 11681 if (sections->abbrev.s.section != NULL)
80626a55 11682 return 0;
049412e3 11683 sections->abbrev.s.section = sectp;
fd361982 11684 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11685 }
11686 else if (section_is_p (sectp->name, &names->info_dwo)
11687 || section_is_p (sectp->name, &names->types_dwo))
11688 {
11689 /* There can be only one. */
049412e3 11690 if (sections->info_or_types.s.section != NULL)
80626a55 11691 return 0;
049412e3 11692 sections->info_or_types.s.section = sectp;
fd361982 11693 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11694 }
11695 else if (section_is_p (sectp->name, &names->line_dwo))
11696 {
11697 /* There can be only one. */
049412e3 11698 if (sections->line.s.section != NULL)
80626a55 11699 return 0;
049412e3 11700 sections->line.s.section = sectp;
fd361982 11701 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11702 }
11703 else if (section_is_p (sectp->name, &names->loc_dwo))
11704 {
11705 /* There can be only one. */
049412e3 11706 if (sections->loc.s.section != NULL)
80626a55 11707 return 0;
049412e3 11708 sections->loc.s.section = sectp;
fd361982 11709 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11710 }
11711 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11712 {
11713 /* There can be only one. */
049412e3 11714 if (sections->macinfo.s.section != NULL)
80626a55 11715 return 0;
049412e3 11716 sections->macinfo.s.section = sectp;
fd361982 11717 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11718 }
11719 else if (section_is_p (sectp->name, &names->macro_dwo))
11720 {
11721 /* There can be only one. */
049412e3 11722 if (sections->macro.s.section != NULL)
80626a55 11723 return 0;
049412e3 11724 sections->macro.s.section = sectp;
fd361982 11725 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11726 }
11727 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11728 {
11729 /* There can be only one. */
049412e3 11730 if (sections->str_offsets.s.section != NULL)
80626a55 11731 return 0;
049412e3 11732 sections->str_offsets.s.section = sectp;
fd361982 11733 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11734 }
11735 else
11736 {
11737 /* No other kind of section is valid. */
11738 return 0;
11739 }
11740
11741 return 1;
11742}
11743
73869dc2
DE
11744/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11745 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11746 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11747 This is for DWP version 1 files. */
80626a55
DE
11748
11749static struct dwo_unit *
ed2dc618
SM
11750create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11751 struct dwp_file *dwp_file,
73869dc2
DE
11752 uint32_t unit_index,
11753 const char *comp_dir,
11754 ULONGEST signature, int is_debug_types)
80626a55 11755{
73869dc2
DE
11756 const struct dwp_hash_table *dwp_htab =
11757 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11758 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11759 const char *kind = is_debug_types ? "TU" : "CU";
11760 struct dwo_file *dwo_file;
11761 struct dwo_unit *dwo_unit;
73869dc2 11762 struct virtual_v1_dwo_sections sections;
80626a55 11763 void **dwo_file_slot;
80626a55
DE
11764 int i;
11765
73869dc2
DE
11766 gdb_assert (dwp_file->version == 1);
11767
b4f54984 11768 if (dwarf_read_debug)
80626a55 11769 {
73869dc2 11770 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11771 kind,
73869dc2 11772 pulongest (unit_index), hex_string (signature),
80626a55
DE
11773 dwp_file->name);
11774 }
11775
19ac8c2e 11776 /* Fetch the sections of this DWO unit.
80626a55
DE
11777 Put a limit on the number of sections we look for so that bad data
11778 doesn't cause us to loop forever. */
11779
73869dc2 11780#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11781 (1 /* .debug_info or .debug_types */ \
11782 + 1 /* .debug_abbrev */ \
11783 + 1 /* .debug_line */ \
11784 + 1 /* .debug_loc */ \
11785 + 1 /* .debug_str_offsets */ \
19ac8c2e 11786 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11787 + 1 /* trailing zero */)
11788
11789 memset (&sections, 0, sizeof (sections));
80626a55 11790
73869dc2 11791 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11792 {
11793 asection *sectp;
11794 uint32_t section_nr =
11795 read_4_bytes (dbfd,
73869dc2
DE
11796 dwp_htab->section_pool.v1.indices
11797 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11798
11799 if (section_nr == 0)
11800 break;
11801 if (section_nr >= dwp_file->num_sections)
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, section number too large"
11804 " [in module %s]"),
11805 dwp_file->name);
11806 }
11807
11808 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11809 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11810 {
11811 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11812 " [in module %s]"),
11813 dwp_file->name);
11814 }
11815 }
11816
11817 if (i < 2
96b79293
TT
11818 || sections.info_or_types.empty ()
11819 || sections.abbrev.empty ())
80626a55
DE
11820 {
11821 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11822 " [in module %s]"),
11823 dwp_file->name);
11824 }
73869dc2 11825 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11826 {
11827 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11828 " [in module %s]"),
11829 dwp_file->name);
11830 }
11831
11832 /* It's easier for the rest of the code if we fake a struct dwo_file and
11833 have dwo_unit "live" in that. At least for now.
11834
11835 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11836 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11837 file, we can combine them back into a virtual DWO file to save space
11838 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11839 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11840
791afaa2
TT
11841 std::string virtual_dwo_name =
11842 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11843 sections.abbrev.get_id (),
11844 sections.line.get_id (),
11845 sections.loc.get_id (),
11846 sections.str_offsets.get_id ());
80626a55 11847 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11848 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11849 virtual_dwo_name.c_str (),
11850 comp_dir);
80626a55
DE
11851 /* Create one if necessary. */
11852 if (*dwo_file_slot == NULL)
11853 {
b4f54984 11854 if (dwarf_read_debug)
80626a55
DE
11855 {
11856 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11857 virtual_dwo_name.c_str ());
80626a55 11858 }
51ac9db5 11859 dwo_file = new struct dwo_file;
45940949 11860 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11861 dwo_file->comp_dir = comp_dir;
80626a55
DE
11862 dwo_file->sections.abbrev = sections.abbrev;
11863 dwo_file->sections.line = sections.line;
11864 dwo_file->sections.loc = sections.loc;
11865 dwo_file->sections.macinfo = sections.macinfo;
11866 dwo_file->sections.macro = sections.macro;
11867 dwo_file->sections.str_offsets = sections.str_offsets;
11868 /* The "str" section is global to the entire DWP file. */
11869 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11870 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11871 there's no need to record it in dwo_file.
11872 Also, we can't simply record type sections in dwo_file because
11873 we record a pointer into the vector in dwo_unit. As we collect more
11874 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11875 for it, invalidating all copies of pointers into the previous
11876 contents. */
80626a55
DE
11877 *dwo_file_slot = dwo_file;
11878 }
11879 else
11880 {
b4f54984 11881 if (dwarf_read_debug)
80626a55
DE
11882 {
11883 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11884 virtual_dwo_name.c_str ());
80626a55 11885 }
9a3c8263 11886 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11887 }
80626a55 11888
5989a64e 11889 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11890 dwo_unit->dwo_file = dwo_file;
11891 dwo_unit->signature = signature;
8d749320 11892 dwo_unit->section =
5989a64e 11893 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11894 *dwo_unit->section = sections.info_or_types;
57d63ce2 11895 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11896
11897 return dwo_unit;
11898}
11899
73869dc2
DE
11900/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11901 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11902 piece within that section used by a TU/CU, return a virtual section
11903 of just that piece. */
11904
11905static struct dwarf2_section_info
ed2dc618
SM
11906create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11907 struct dwarf2_section_info *section,
73869dc2
DE
11908 bfd_size_type offset, bfd_size_type size)
11909{
11910 struct dwarf2_section_info result;
11911 asection *sectp;
11912
11913 gdb_assert (section != NULL);
11914 gdb_assert (!section->is_virtual);
11915
11916 memset (&result, 0, sizeof (result));
11917 result.s.containing_section = section;
dc4ccb6f 11918 result.is_virtual = true;
73869dc2
DE
11919
11920 if (size == 0)
11921 return result;
11922
96b79293 11923 sectp = section->get_bfd_section ();
73869dc2
DE
11924
11925 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11926 bounds of the real section. This is a pretty-rare event, so just
11927 flag an error (easier) instead of a warning and trying to cope. */
11928 if (sectp == NULL
fd361982 11929 || offset + size > bfd_section_size (sectp))
73869dc2 11930 {
73869dc2
DE
11931 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11932 " in section %s [in module %s]"),
fd361982 11933 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11934 objfile_name (dwarf2_per_objfile->objfile));
11935 }
11936
11937 result.virtual_offset = offset;
11938 result.size = size;
11939 return result;
11940}
11941
11942/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11943 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11944 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11945 This is for DWP version 2 files. */
11946
11947static struct dwo_unit *
ed2dc618
SM
11948create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11949 struct dwp_file *dwp_file,
73869dc2
DE
11950 uint32_t unit_index,
11951 const char *comp_dir,
11952 ULONGEST signature, int is_debug_types)
11953{
73869dc2
DE
11954 const struct dwp_hash_table *dwp_htab =
11955 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11956 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11957 const char *kind = is_debug_types ? "TU" : "CU";
11958 struct dwo_file *dwo_file;
11959 struct dwo_unit *dwo_unit;
11960 struct virtual_v2_dwo_sections sections;
11961 void **dwo_file_slot;
73869dc2
DE
11962 int i;
11963
11964 gdb_assert (dwp_file->version == 2);
11965
b4f54984 11966 if (dwarf_read_debug)
73869dc2
DE
11967 {
11968 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11969 kind,
11970 pulongest (unit_index), hex_string (signature),
11971 dwp_file->name);
11972 }
11973
11974 /* Fetch the section offsets of this DWO unit. */
11975
11976 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11977
11978 for (i = 0; i < dwp_htab->nr_columns; ++i)
11979 {
11980 uint32_t offset = read_4_bytes (dbfd,
11981 dwp_htab->section_pool.v2.offsets
11982 + (((unit_index - 1) * dwp_htab->nr_columns
11983 + i)
11984 * sizeof (uint32_t)));
11985 uint32_t size = read_4_bytes (dbfd,
11986 dwp_htab->section_pool.v2.sizes
11987 + (((unit_index - 1) * dwp_htab->nr_columns
11988 + i)
11989 * sizeof (uint32_t)));
11990
11991 switch (dwp_htab->section_pool.v2.section_ids[i])
11992 {
11993 case DW_SECT_INFO:
11994 case DW_SECT_TYPES:
11995 sections.info_or_types_offset = offset;
11996 sections.info_or_types_size = size;
11997 break;
11998 case DW_SECT_ABBREV:
11999 sections.abbrev_offset = offset;
12000 sections.abbrev_size = size;
12001 break;
12002 case DW_SECT_LINE:
12003 sections.line_offset = offset;
12004 sections.line_size = size;
12005 break;
12006 case DW_SECT_LOC:
12007 sections.loc_offset = offset;
12008 sections.loc_size = size;
12009 break;
12010 case DW_SECT_STR_OFFSETS:
12011 sections.str_offsets_offset = offset;
12012 sections.str_offsets_size = size;
12013 break;
12014 case DW_SECT_MACINFO:
12015 sections.macinfo_offset = offset;
12016 sections.macinfo_size = size;
12017 break;
12018 case DW_SECT_MACRO:
12019 sections.macro_offset = offset;
12020 sections.macro_size = size;
12021 break;
12022 }
12023 }
12024
12025 /* It's easier for the rest of the code if we fake a struct dwo_file and
12026 have dwo_unit "live" in that. At least for now.
12027
12028 The DWP file can be made up of a random collection of CUs and TUs.
12029 However, for each CU + set of TUs that came from the same original DWO
12030 file, we can combine them back into a virtual DWO file to save space
12031 (fewer struct dwo_file objects to allocate). Remember that for really
12032 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12033
791afaa2
TT
12034 std::string virtual_dwo_name =
12035 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12036 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12037 (long) (sections.line_size ? sections.line_offset : 0),
12038 (long) (sections.loc_size ? sections.loc_offset : 0),
12039 (long) (sections.str_offsets_size
12040 ? sections.str_offsets_offset : 0));
73869dc2 12041 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12042 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12043 virtual_dwo_name.c_str (),
12044 comp_dir);
73869dc2
DE
12045 /* Create one if necessary. */
12046 if (*dwo_file_slot == NULL)
12047 {
b4f54984 12048 if (dwarf_read_debug)
73869dc2
DE
12049 {
12050 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12051 virtual_dwo_name.c_str ());
73869dc2 12052 }
51ac9db5 12053 dwo_file = new struct dwo_file;
45940949 12054 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12055 dwo_file->comp_dir = comp_dir;
12056 dwo_file->sections.abbrev =
ed2dc618 12057 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12058 sections.abbrev_offset, sections.abbrev_size);
12059 dwo_file->sections.line =
ed2dc618 12060 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12061 sections.line_offset, sections.line_size);
12062 dwo_file->sections.loc =
ed2dc618 12063 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12064 sections.loc_offset, sections.loc_size);
12065 dwo_file->sections.macinfo =
ed2dc618 12066 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12067 sections.macinfo_offset, sections.macinfo_size);
12068 dwo_file->sections.macro =
ed2dc618 12069 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12070 sections.macro_offset, sections.macro_size);
12071 dwo_file->sections.str_offsets =
ed2dc618
SM
12072 create_dwp_v2_section (dwarf2_per_objfile,
12073 &dwp_file->sections.str_offsets,
73869dc2
DE
12074 sections.str_offsets_offset,
12075 sections.str_offsets_size);
12076 /* The "str" section is global to the entire DWP file. */
12077 dwo_file->sections.str = dwp_file->sections.str;
12078 /* The info or types section is assigned below to dwo_unit,
12079 there's no need to record it in dwo_file.
12080 Also, we can't simply record type sections in dwo_file because
12081 we record a pointer into the vector in dwo_unit. As we collect more
12082 types we'll grow the vector and eventually have to reallocate space
12083 for it, invalidating all copies of pointers into the previous
12084 contents. */
12085 *dwo_file_slot = dwo_file;
12086 }
12087 else
12088 {
b4f54984 12089 if (dwarf_read_debug)
73869dc2
DE
12090 {
12091 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12092 virtual_dwo_name.c_str ());
73869dc2 12093 }
9a3c8263 12094 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12095 }
73869dc2 12096
5989a64e 12097 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12098 dwo_unit->dwo_file = dwo_file;
12099 dwo_unit->signature = signature;
8d749320 12100 dwo_unit->section =
5989a64e 12101 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12102 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12103 is_debug_types
73869dc2
DE
12104 ? &dwp_file->sections.types
12105 : &dwp_file->sections.info,
12106 sections.info_or_types_offset,
12107 sections.info_or_types_size);
12108 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12109
12110 return dwo_unit;
12111}
12112
57d63ce2
DE
12113/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12114 Returns NULL if the signature isn't found. */
80626a55
DE
12115
12116static struct dwo_unit *
ed2dc618
SM
12117lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12118 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12119 ULONGEST signature, int is_debug_types)
80626a55 12120{
57d63ce2
DE
12121 const struct dwp_hash_table *dwp_htab =
12122 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12123 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12124 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12125 uint32_t hash = signature & mask;
12126 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12127 unsigned int i;
12128 void **slot;
870f88f7 12129 struct dwo_unit find_dwo_cu;
80626a55
DE
12130
12131 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12132 find_dwo_cu.signature = signature;
19ac8c2e 12133 slot = htab_find_slot (is_debug_types
48b490f2
TT
12134 ? dwp_file->loaded_tus.get ()
12135 : dwp_file->loaded_cus.get (),
19ac8c2e 12136 &find_dwo_cu, INSERT);
80626a55
DE
12137
12138 if (*slot != NULL)
9a3c8263 12139 return (struct dwo_unit *) *slot;
80626a55
DE
12140
12141 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12142 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12143 {
12144 ULONGEST signature_in_table;
12145
12146 signature_in_table =
57d63ce2 12147 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12148 if (signature_in_table == signature)
12149 {
57d63ce2
DE
12150 uint32_t unit_index =
12151 read_4_bytes (dbfd,
12152 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12153
73869dc2
DE
12154 if (dwp_file->version == 1)
12155 {
ed2dc618
SM
12156 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12157 dwp_file, unit_index,
73869dc2
DE
12158 comp_dir, signature,
12159 is_debug_types);
12160 }
12161 else
12162 {
ed2dc618
SM
12163 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12164 dwp_file, unit_index,
73869dc2
DE
12165 comp_dir, signature,
12166 is_debug_types);
12167 }
9a3c8263 12168 return (struct dwo_unit *) *slot;
80626a55
DE
12169 }
12170 if (signature_in_table == 0)
12171 return NULL;
12172 hash = (hash + hash2) & mask;
12173 }
12174
12175 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12176 " [in module %s]"),
12177 dwp_file->name);
12178}
12179
ab5088bf 12180/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12181 Open the file specified by FILE_NAME and hand it off to BFD for
12182 preliminary analysis. Return a newly initialized bfd *, which
12183 includes a canonicalized copy of FILE_NAME.
80626a55 12184 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12185 SEARCH_CWD is true if the current directory is to be searched.
12186 It will be searched before debug-file-directory.
13aaf454
DE
12187 If successful, the file is added to the bfd include table of the
12188 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12189 If unable to find/open the file, return NULL.
3019eac3
DE
12190 NOTE: This function is derived from symfile_bfd_open. */
12191
192b62ce 12192static gdb_bfd_ref_ptr
ed2dc618
SM
12193try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12194 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12195{
24b9144d 12196 int desc;
9c02c129
DE
12197 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12198 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12199 to debug_file_directory. */
e0cc99a6 12200 const char *search_path;
9c02c129
DE
12201 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12202
e0cc99a6 12203 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12204 if (search_cwd)
12205 {
12206 if (*debug_file_directory != '\0')
e0cc99a6
TT
12207 {
12208 search_path_holder.reset (concat (".", dirname_separator_string,
12209 debug_file_directory,
12210 (char *) NULL));
12211 search_path = search_path_holder.get ();
12212 }
6ac97d4c 12213 else
e0cc99a6 12214 search_path = ".";
6ac97d4c 12215 }
9c02c129 12216 else
e0cc99a6 12217 search_path = debug_file_directory;
3019eac3 12218
24b9144d 12219 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12220 if (is_dwp)
12221 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12222
12223 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12224 desc = openp (search_path, flags, file_name,
3019eac3
DE
12225 O_RDONLY | O_BINARY, &absolute_name);
12226 if (desc < 0)
12227 return NULL;
12228
e0cc99a6
TT
12229 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12230 gnutarget, desc));
9c02c129
DE
12231 if (sym_bfd == NULL)
12232 return NULL;
192b62ce 12233 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12234
192b62ce
TT
12235 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12236 return NULL;
3019eac3 12237
13aaf454
DE
12238 /* Success. Record the bfd as having been included by the objfile's bfd.
12239 This is important because things like demangled_names_hash lives in the
12240 objfile's per_bfd space and may have references to things like symbol
12241 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12242 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12243
3019eac3
DE
12244 return sym_bfd;
12245}
12246
ab5088bf 12247/* Try to open DWO file FILE_NAME.
3019eac3
DE
12248 COMP_DIR is the DW_AT_comp_dir attribute.
12249 The result is the bfd handle of the file.
12250 If there is a problem finding or opening the file, return NULL.
12251 Upon success, the canonicalized path of the file is stored in the bfd,
12252 same as symfile_bfd_open. */
12253
192b62ce 12254static gdb_bfd_ref_ptr
ed2dc618
SM
12255open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12256 const char *file_name, const char *comp_dir)
3019eac3 12257{
80626a55 12258 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12259 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12260 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12261
12262 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12263
12264 if (comp_dir != NULL)
12265 {
43816ebc
TT
12266 gdb::unique_xmalloc_ptr<char> path_to_try
12267 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12268
12269 /* NOTE: If comp_dir is a relative path, this will also try the
12270 search path, which seems useful. */
ed2dc618 12271 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12272 path_to_try.get (),
ed2dc618 12273 0 /*is_dwp*/,
192b62ce 12274 1 /*search_cwd*/));
3019eac3
DE
12275 if (abfd != NULL)
12276 return abfd;
12277 }
12278
12279 /* That didn't work, try debug-file-directory, which, despite its name,
12280 is a list of paths. */
12281
12282 if (*debug_file_directory == '\0')
12283 return NULL;
12284
ed2dc618
SM
12285 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12286 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12287}
12288
80626a55
DE
12289/* This function is mapped across the sections and remembers the offset and
12290 size of each of the DWO debugging sections we are interested in. */
12291
12292static void
12293dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12294{
9a3c8263 12295 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12296 const struct dwop_section_names *names = &dwop_section_names;
12297
12298 if (section_is_p (sectp->name, &names->abbrev_dwo))
12299 {
049412e3 12300 dwo_sections->abbrev.s.section = sectp;
fd361982 12301 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12302 }
12303 else if (section_is_p (sectp->name, &names->info_dwo))
12304 {
049412e3 12305 dwo_sections->info.s.section = sectp;
fd361982 12306 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12307 }
12308 else if (section_is_p (sectp->name, &names->line_dwo))
12309 {
049412e3 12310 dwo_sections->line.s.section = sectp;
fd361982 12311 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12312 }
12313 else if (section_is_p (sectp->name, &names->loc_dwo))
12314 {
049412e3 12315 dwo_sections->loc.s.section = sectp;
fd361982 12316 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12317 }
41144253 12318 else if (section_is_p (sectp->name, &names->loclists_dwo))
12319 {
12320 dwo_sections->loclists.s.section = sectp;
12321 dwo_sections->loclists.size = bfd_section_size (sectp);
12322 }
80626a55
DE
12323 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12324 {
049412e3 12325 dwo_sections->macinfo.s.section = sectp;
fd361982 12326 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12327 }
12328 else if (section_is_p (sectp->name, &names->macro_dwo))
12329 {
049412e3 12330 dwo_sections->macro.s.section = sectp;
fd361982 12331 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12332 }
12333 else if (section_is_p (sectp->name, &names->str_dwo))
12334 {
049412e3 12335 dwo_sections->str.s.section = sectp;
fd361982 12336 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12337 }
12338 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12339 {
049412e3 12340 dwo_sections->str_offsets.s.section = sectp;
fd361982 12341 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12342 }
12343 else if (section_is_p (sectp->name, &names->types_dwo))
12344 {
12345 struct dwarf2_section_info type_section;
12346
12347 memset (&type_section, 0, sizeof (type_section));
049412e3 12348 type_section.s.section = sectp;
fd361982 12349 type_section.size = bfd_section_size (sectp);
fd5866f6 12350 dwo_sections->types.push_back (type_section);
80626a55
DE
12351 }
12352}
12353
ab5088bf 12354/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12355 by PER_CU. This is for the non-DWP case.
80626a55 12356 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12357
12358static struct dwo_file *
4ab09049
SM
12359open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12360 const char *comp_dir)
3019eac3 12361{
4ab09049 12362 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12363
fb1eb2f9 12364 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12365 if (dbfd == NULL)
12366 {
b4f54984 12367 if (dwarf_read_debug)
80626a55
DE
12368 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12369 return NULL;
12370 }
263db9a1 12371
51ac9db5 12372 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12373 dwo_file->dwo_name = dwo_name;
12374 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12375 dwo_file->dbfd = std::move (dbfd);
3019eac3 12376
fb1eb2f9 12377 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12378 &dwo_file->sections);
3019eac3 12379
4ab09049 12380 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12381 dwo_file->sections.info, dwo_file->cus);
3019eac3 12382
263db9a1 12383 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12384 dwo_file->sections.types, dwo_file->tus);
3019eac3 12385
b4f54984 12386 if (dwarf_read_debug)
80626a55
DE
12387 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12388
263db9a1 12389 return dwo_file.release ();
3019eac3
DE
12390}
12391
80626a55 12392/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12393 size of each of the DWP debugging sections common to version 1 and 2 that
12394 we are interested in. */
3019eac3 12395
80626a55 12396static void
73869dc2
DE
12397dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12398 void *dwp_file_ptr)
3019eac3 12399{
9a3c8263 12400 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12401 const struct dwop_section_names *names = &dwop_section_names;
12402 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12403
80626a55 12404 /* Record the ELF section number for later lookup: this is what the
73869dc2 12405 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12406 gdb_assert (elf_section_nr < dwp_file->num_sections);
12407 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12408
80626a55
DE
12409 /* Look for specific sections that we need. */
12410 if (section_is_p (sectp->name, &names->str_dwo))
12411 {
049412e3 12412 dwp_file->sections.str.s.section = sectp;
fd361982 12413 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12414 }
12415 else if (section_is_p (sectp->name, &names->cu_index))
12416 {
049412e3 12417 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12418 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12419 }
12420 else if (section_is_p (sectp->name, &names->tu_index))
12421 {
049412e3 12422 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12423 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12424 }
12425}
3019eac3 12426
73869dc2
DE
12427/* This function is mapped across the sections and remembers the offset and
12428 size of each of the DWP version 2 debugging sections that we are interested
12429 in. This is split into a separate function because we don't know if we
12430 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12431
12432static void
12433dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12434{
9a3c8263 12435 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12436 const struct dwop_section_names *names = &dwop_section_names;
12437 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12438
12439 /* Record the ELF section number for later lookup: this is what the
12440 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12441 gdb_assert (elf_section_nr < dwp_file->num_sections);
12442 dwp_file->elf_sections[elf_section_nr] = sectp;
12443
12444 /* Look for specific sections that we need. */
12445 if (section_is_p (sectp->name, &names->abbrev_dwo))
12446 {
049412e3 12447 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12448 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12449 }
12450 else if (section_is_p (sectp->name, &names->info_dwo))
12451 {
049412e3 12452 dwp_file->sections.info.s.section = sectp;
fd361982 12453 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12454 }
12455 else if (section_is_p (sectp->name, &names->line_dwo))
12456 {
049412e3 12457 dwp_file->sections.line.s.section = sectp;
fd361982 12458 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12459 }
12460 else if (section_is_p (sectp->name, &names->loc_dwo))
12461 {
049412e3 12462 dwp_file->sections.loc.s.section = sectp;
fd361982 12463 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12464 }
12465 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12466 {
049412e3 12467 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12468 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12469 }
12470 else if (section_is_p (sectp->name, &names->macro_dwo))
12471 {
049412e3 12472 dwp_file->sections.macro.s.section = sectp;
fd361982 12473 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12474 }
12475 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12476 {
049412e3 12477 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12478 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12479 }
12480 else if (section_is_p (sectp->name, &names->types_dwo))
12481 {
049412e3 12482 dwp_file->sections.types.s.section = sectp;
fd361982 12483 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12484 }
12485}
12486
80626a55 12487/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12488
80626a55
DE
12489static hashval_t
12490hash_dwp_loaded_cutus (const void *item)
12491{
9a3c8263 12492 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12493
80626a55
DE
12494 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12495 return dwo_unit->signature;
3019eac3
DE
12496}
12497
80626a55 12498/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12499
80626a55
DE
12500static int
12501eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12502{
9a3c8263
SM
12503 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12504 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12505
80626a55
DE
12506 return dua->signature == dub->signature;
12507}
3019eac3 12508
80626a55 12509/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12510
48b490f2 12511static htab_up
298e9637 12512allocate_dwp_loaded_cutus_table ()
80626a55 12513{
48b490f2
TT
12514 return htab_up (htab_create_alloc (3,
12515 hash_dwp_loaded_cutus,
12516 eq_dwp_loaded_cutus,
12517 NULL, xcalloc, xfree));
80626a55 12518}
3019eac3 12519
ab5088bf
DE
12520/* Try to open DWP file FILE_NAME.
12521 The result is the bfd handle of the file.
12522 If there is a problem finding or opening the file, return NULL.
12523 Upon success, the canonicalized path of the file is stored in the bfd,
12524 same as symfile_bfd_open. */
12525
192b62ce 12526static gdb_bfd_ref_ptr
ed2dc618
SM
12527open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12528 const char *file_name)
ab5088bf 12529{
ed2dc618
SM
12530 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12531 1 /*is_dwp*/,
192b62ce 12532 1 /*search_cwd*/));
6ac97d4c
DE
12533 if (abfd != NULL)
12534 return abfd;
12535
12536 /* Work around upstream bug 15652.
12537 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12538 [Whether that's a "bug" is debatable, but it is getting in our way.]
12539 We have no real idea where the dwp file is, because gdb's realpath-ing
12540 of the executable's path may have discarded the needed info.
12541 [IWBN if the dwp file name was recorded in the executable, akin to
12542 .gnu_debuglink, but that doesn't exist yet.]
12543 Strip the directory from FILE_NAME and search again. */
12544 if (*debug_file_directory != '\0')
12545 {
12546 /* Don't implicitly search the current directory here.
12547 If the user wants to search "." to handle this case,
12548 it must be added to debug-file-directory. */
ed2dc618
SM
12549 return try_open_dwop_file (dwarf2_per_objfile,
12550 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12551 0 /*search_cwd*/);
12552 }
12553
12554 return NULL;
ab5088bf
DE
12555}
12556
80626a55
DE
12557/* Initialize the use of the DWP file for the current objfile.
12558 By convention the name of the DWP file is ${objfile}.dwp.
12559 The result is NULL if it can't be found. */
a766d390 12560
400174b1 12561static std::unique_ptr<struct dwp_file>
ed2dc618 12562open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12563{
12564 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12565
82bf32bc
JK
12566 /* Try to find first .dwp for the binary file before any symbolic links
12567 resolving. */
6c447423
DE
12568
12569 /* If the objfile is a debug file, find the name of the real binary
12570 file and get the name of dwp file from there. */
d721ba37 12571 std::string dwp_name;
6c447423
DE
12572 if (objfile->separate_debug_objfile_backlink != NULL)
12573 {
12574 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12575 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12576
d721ba37 12577 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12578 }
12579 else
d721ba37
PA
12580 dwp_name = objfile->original_name;
12581
12582 dwp_name += ".dwp";
80626a55 12583
ed2dc618 12584 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12585 if (dbfd == NULL
12586 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12587 {
12588 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12589 dwp_name = objfile_name (objfile);
12590 dwp_name += ".dwp";
ed2dc618 12591 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12592 }
12593
80626a55
DE
12594 if (dbfd == NULL)
12595 {
b4f54984 12596 if (dwarf_read_debug)
d721ba37 12597 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12598 return std::unique_ptr<dwp_file> ();
3019eac3 12599 }
400174b1
TT
12600
12601 const char *name = bfd_get_filename (dbfd.get ());
12602 std::unique_ptr<struct dwp_file> dwp_file
12603 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12604
0a0f4c01 12605 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12606 dwp_file->elf_sections =
5989a64e 12607 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12608 dwp_file->num_sections, asection *);
12609
400174b1
TT
12610 bfd_map_over_sections (dwp_file->dbfd.get (),
12611 dwarf2_locate_common_dwp_sections,
12612 dwp_file.get ());
80626a55 12613
400174b1
TT
12614 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12615 0);
80626a55 12616
400174b1
TT
12617 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12618 1);
80626a55 12619
73869dc2 12620 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12621 if (dwp_file->cus && dwp_file->tus
12622 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12623 {
12624 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12625 pretty bizarre. We use pulongest here because that's the established
4d65956b 12626 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12627 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12628 " TU version %s [in DWP file %s]"),
12629 pulongest (dwp_file->cus->version),
d721ba37 12630 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12631 }
08302ed2
DE
12632
12633 if (dwp_file->cus)
12634 dwp_file->version = dwp_file->cus->version;
12635 else if (dwp_file->tus)
12636 dwp_file->version = dwp_file->tus->version;
12637 else
12638 dwp_file->version = 2;
73869dc2
DE
12639
12640 if (dwp_file->version == 2)
400174b1
TT
12641 bfd_map_over_sections (dwp_file->dbfd.get (),
12642 dwarf2_locate_v2_dwp_sections,
12643 dwp_file.get ());
73869dc2 12644
298e9637
SM
12645 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12646 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12647
b4f54984 12648 if (dwarf_read_debug)
80626a55
DE
12649 {
12650 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12651 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12652 " %s CUs, %s TUs\n",
12653 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12654 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12655 }
12656
12657 return dwp_file;
3019eac3 12658}
c906108c 12659
ab5088bf
DE
12660/* Wrapper around open_and_init_dwp_file, only open it once. */
12661
12662static struct dwp_file *
ed2dc618 12663get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12664{
5989a64e 12665 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12666 {
5989a64e 12667 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12668 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12669 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12670 }
5989a64e 12671 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12672}
12673
80626a55
DE
12674/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12675 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12676 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12677 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12678 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12679
12680 This is called, for example, when wanting to read a variable with a
12681 complex location. Therefore we don't want to do file i/o for every call.
12682 Therefore we don't want to look for a DWO file on every call.
12683 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12684 then we check if we've already seen DWO_NAME, and only THEN do we check
12685 for a DWO file.
12686
1c658ad5 12687 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12688 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12689
3019eac3 12690static struct dwo_unit *
4ab09049 12691lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12692 ULONGEST signature, int is_debug_types)
3019eac3 12693{
4ab09049 12694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12695 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12696 const char *kind = is_debug_types ? "TU" : "CU";
12697 void **dwo_file_slot;
3019eac3 12698 struct dwo_file *dwo_file;
80626a55 12699 struct dwp_file *dwp_file;
cb1df416 12700
6a506a2d
DE
12701 /* First see if there's a DWP file.
12702 If we have a DWP file but didn't find the DWO inside it, don't
12703 look for the original DWO file. It makes gdb behave differently
12704 depending on whether one is debugging in the build tree. */
cf2c3c16 12705
ed2dc618 12706 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12707 if (dwp_file != NULL)
cf2c3c16 12708 {
80626a55
DE
12709 const struct dwp_hash_table *dwp_htab =
12710 is_debug_types ? dwp_file->tus : dwp_file->cus;
12711
12712 if (dwp_htab != NULL)
12713 {
12714 struct dwo_unit *dwo_cutu =
ed2dc618 12715 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12716 signature, is_debug_types);
80626a55
DE
12717
12718 if (dwo_cutu != NULL)
12719 {
b4f54984 12720 if (dwarf_read_debug)
80626a55
DE
12721 {
12722 fprintf_unfiltered (gdb_stdlog,
12723 "Virtual DWO %s %s found: @%s\n",
12724 kind, hex_string (signature),
12725 host_address_to_string (dwo_cutu));
12726 }
12727 return dwo_cutu;
12728 }
12729 }
12730 }
6a506a2d 12731 else
80626a55 12732 {
6a506a2d 12733 /* No DWP file, look for the DWO file. */
80626a55 12734
ed2dc618
SM
12735 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12736 dwo_name, comp_dir);
6a506a2d 12737 if (*dwo_file_slot == NULL)
80626a55 12738 {
6a506a2d 12739 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12740 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12741 }
6a506a2d 12742 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12743 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12744
6a506a2d 12745 if (dwo_file != NULL)
19c3d4c9 12746 {
6a506a2d
DE
12747 struct dwo_unit *dwo_cutu = NULL;
12748
12749 if (is_debug_types && dwo_file->tus)
12750 {
12751 struct dwo_unit find_dwo_cutu;
12752
12753 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12754 find_dwo_cutu.signature = signature;
9a3c8263 12755 dwo_cutu
b0b6a987
TT
12756 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12757 &find_dwo_cutu);
6a506a2d 12758 }
33c5cd75 12759 else if (!is_debug_types && dwo_file->cus)
80626a55 12760 {
33c5cd75
DB
12761 struct dwo_unit find_dwo_cutu;
12762
12763 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12764 find_dwo_cutu.signature = signature;
b0b6a987 12765 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12766 &find_dwo_cutu);
6a506a2d
DE
12767 }
12768
12769 if (dwo_cutu != NULL)
12770 {
b4f54984 12771 if (dwarf_read_debug)
6a506a2d
DE
12772 {
12773 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12774 kind, dwo_name, hex_string (signature),
12775 host_address_to_string (dwo_cutu));
12776 }
12777 return dwo_cutu;
80626a55
DE
12778 }
12779 }
2e276125 12780 }
9cdd5dbd 12781
80626a55
DE
12782 /* We didn't find it. This could mean a dwo_id mismatch, or
12783 someone deleted the DWO/DWP file, or the search path isn't set up
12784 correctly to find the file. */
12785
b4f54984 12786 if (dwarf_read_debug)
80626a55
DE
12787 {
12788 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12789 kind, dwo_name, hex_string (signature));
12790 }
3019eac3 12791
6656a72d
DE
12792 /* This is a warning and not a complaint because it can be caused by
12793 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12794 {
12795 /* Print the name of the DWP file if we looked there, helps the user
12796 better diagnose the problem. */
791afaa2 12797 std::string dwp_text;
43942612
DE
12798
12799 if (dwp_file != NULL)
791afaa2
TT
12800 dwp_text = string_printf (" [in DWP file %s]",
12801 lbasename (dwp_file->name));
43942612 12802
9d8780f0 12803 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12804 " [in module %s]"),
4ab09049
SM
12805 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12806 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12807 }
3019eac3 12808 return NULL;
5fb290d7
DJ
12809}
12810
80626a55
DE
12811/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12812 See lookup_dwo_cutu_unit for details. */
12813
12814static struct dwo_unit *
4ab09049 12815lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12816 ULONGEST signature)
12817{
4ab09049
SM
12818 gdb_assert (!cu->per_cu->is_debug_types);
12819
12820 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12821}
12822
12823/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12824 See lookup_dwo_cutu_unit for details. */
12825
12826static struct dwo_unit *
4ab09049 12827lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12828{
4ab09049
SM
12829 gdb_assert (cu->per_cu->is_debug_types);
12830
12831 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12832
12833 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12834}
12835
89e63ee4
DE
12836/* Traversal function for queue_and_load_all_dwo_tus. */
12837
12838static int
12839queue_and_load_dwo_tu (void **slot, void *info)
12840{
12841 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12842 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12843 ULONGEST signature = dwo_unit->signature;
d460f660 12844 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12845
12846 if (sig_type != NULL)
12847 {
12848 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12849
12850 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12851 a real dependency of PER_CU on SIG_TYPE. That is detected later
12852 while processing PER_CU. */
d460f660
SM
12853 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12854 load_full_type_unit (sig_cu, cu->per_objfile);
12855 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12856 }
12857
12858 return 1;
12859}
12860
12861/* Queue all TUs contained in the DWO of PER_CU to be read in.
12862 The DWO may have the only definition of the type, though it may not be
12863 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12864 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12865
12866static void
12867queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12868{
12869 struct dwo_unit *dwo_unit;
12870 struct dwo_file *dwo_file;
12871
12872 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12873 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12874 gdb_assert (per_cu->cu != NULL);
12875
12876 dwo_unit = per_cu->cu->dwo_unit;
12877 gdb_assert (dwo_unit != NULL);
12878
12879 dwo_file = dwo_unit->dwo_file;
12880 if (dwo_file->tus != NULL)
b0b6a987 12881 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12882 per_cu->cu);
89e63ee4
DE
12883}
12884
3019eac3 12885/* Read in various DIEs. */
348e048f 12886
d389af10 12887/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12888 Inherit only the children of the DW_AT_abstract_origin DIE not being
12889 already referenced by DW_AT_abstract_origin from the children of the
12890 current DIE. */
d389af10
JK
12891
12892static void
12893inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12894{
12895 struct die_info *child_die;
791afaa2 12896 sect_offset *offsetp;
d389af10
JK
12897 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12898 struct die_info *origin_die;
12899 /* Iterator of the ORIGIN_DIE children. */
12900 struct die_info *origin_child_die;
d389af10 12901 struct attribute *attr;
cd02d79d
PA
12902 struct dwarf2_cu *origin_cu;
12903 struct pending **origin_previous_list_in_scope;
d389af10
JK
12904
12905 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12906 if (!attr)
12907 return;
12908
cd02d79d
PA
12909 /* Note that following die references may follow to a die in a
12910 different cu. */
12911
12912 origin_cu = cu;
12913 origin_die = follow_die_ref (die, attr, &origin_cu);
12914
12915 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12916 symbols in. */
12917 origin_previous_list_in_scope = origin_cu->list_in_scope;
12918 origin_cu->list_in_scope = cu->list_in_scope;
12919
edb3359d
DJ
12920 if (die->tag != origin_die->tag
12921 && !(die->tag == DW_TAG_inlined_subroutine
12922 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12923 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12924 sect_offset_str (die->sect_off),
12925 sect_offset_str (origin_die->sect_off));
d389af10 12926
791afaa2 12927 std::vector<sect_offset> offsets;
d389af10 12928
3ea89b92
PMR
12929 for (child_die = die->child;
12930 child_die && child_die->tag;
436c571c 12931 child_die = child_die->sibling)
3ea89b92
PMR
12932 {
12933 struct die_info *child_origin_die;
12934 struct dwarf2_cu *child_origin_cu;
12935
12936 /* We are trying to process concrete instance entries:
216f72a1 12937 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12938 it's not relevant to our analysis here. i.e. detecting DIEs that are
12939 present in the abstract instance but not referenced in the concrete
12940 one. */
216f72a1
JK
12941 if (child_die->tag == DW_TAG_call_site
12942 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12943 continue;
12944
c38f313d
DJ
12945 /* For each CHILD_DIE, find the corresponding child of
12946 ORIGIN_DIE. If there is more than one layer of
12947 DW_AT_abstract_origin, follow them all; there shouldn't be,
12948 but GCC versions at least through 4.4 generate this (GCC PR
12949 40573). */
3ea89b92
PMR
12950 child_origin_die = child_die;
12951 child_origin_cu = cu;
c38f313d
DJ
12952 while (1)
12953 {
cd02d79d
PA
12954 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12955 child_origin_cu);
c38f313d
DJ
12956 if (attr == NULL)
12957 break;
cd02d79d
PA
12958 child_origin_die = follow_die_ref (child_origin_die, attr,
12959 &child_origin_cu);
c38f313d
DJ
12960 }
12961
d389af10
JK
12962 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12963 counterpart may exist. */
c38f313d 12964 if (child_origin_die != child_die)
d389af10 12965 {
edb3359d
DJ
12966 if (child_die->tag != child_origin_die->tag
12967 && !(child_die->tag == DW_TAG_inlined_subroutine
12968 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12969 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12970 "different tags"),
9d8780f0
SM
12971 sect_offset_str (child_die->sect_off),
12972 sect_offset_str (child_origin_die->sect_off));
c38f313d 12973 if (child_origin_die->parent != origin_die)
b98664d3 12974 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12975 "different parents"),
9d8780f0
SM
12976 sect_offset_str (child_die->sect_off),
12977 sect_offset_str (child_origin_die->sect_off));
c38f313d 12978 else
791afaa2 12979 offsets.push_back (child_origin_die->sect_off);
d389af10 12980 }
d389af10 12981 }
791afaa2
TT
12982 std::sort (offsets.begin (), offsets.end ());
12983 sect_offset *offsets_end = offsets.data () + offsets.size ();
12984 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12985 if (offsetp[-1] == *offsetp)
b98664d3 12986 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12987 "to DIE %s as their abstract origin"),
12988 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12989
791afaa2 12990 offsetp = offsets.data ();
d389af10
JK
12991 origin_child_die = origin_die->child;
12992 while (origin_child_die && origin_child_die->tag)
12993 {
12994 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12995 while (offsetp < offsets_end
9c541725 12996 && *offsetp < origin_child_die->sect_off)
d389af10 12997 offsetp++;
b64f50a1 12998 if (offsetp >= offsets_end
9c541725 12999 || *offsetp > origin_child_die->sect_off)
d389af10 13000 {
adde2bff
DE
13001 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13002 Check whether we're already processing ORIGIN_CHILD_DIE.
13003 This can happen with mutually referenced abstract_origins.
13004 PR 16581. */
13005 if (!origin_child_die->in_process)
13006 process_die (origin_child_die, origin_cu);
d389af10 13007 }
436c571c 13008 origin_child_die = origin_child_die->sibling;
d389af10 13009 }
cd02d79d 13010 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13011
13012 if (cu != origin_cu)
13013 compute_delayed_physnames (origin_cu);
d389af10
JK
13014}
13015
c906108c 13016static void
e7c27a73 13017read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13018{
5e22e966 13019 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13020 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13021 struct context_stack *newobj;
c906108c
SS
13022 CORE_ADDR lowpc;
13023 CORE_ADDR highpc;
13024 struct die_info *child_die;
edb3359d 13025 struct attribute *attr, *call_line, *call_file;
15d034d0 13026 const char *name;
e142c38c 13027 CORE_ADDR baseaddr;
801e3a5b 13028 struct block *block;
edb3359d 13029 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13030 std::vector<struct symbol *> template_args;
34eaf542 13031 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13032
13033 if (inlined_func)
13034 {
13035 /* If we do not have call site information, we can't show the
13036 caller of this inlined function. That's too confusing, so
13037 only use the scope for local variables. */
13038 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13039 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13040 if (call_line == NULL || call_file == NULL)
13041 {
13042 read_lexical_block_scope (die, cu);
13043 return;
13044 }
13045 }
c906108c 13046
b3b3bada 13047 baseaddr = objfile->text_section_offset ();
e142c38c 13048
94af9270 13049 name = dwarf2_name (die, cu);
c906108c 13050
e8d05480
JB
13051 /* Ignore functions with missing or empty names. These are actually
13052 illegal according to the DWARF standard. */
13053 if (name == NULL)
13054 {
b98664d3 13055 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13056 sect_offset_str (die->sect_off));
e8d05480
JB
13057 return;
13058 }
13059
13060 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13061 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13062 <= PC_BOUNDS_INVALID)
e8d05480 13063 {
ae4d0c03
PM
13064 attr = dwarf2_attr (die, DW_AT_external, cu);
13065 if (!attr || !DW_UNSND (attr))
b98664d3 13066 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13067 "for subprogram DIE at %s"),
13068 sect_offset_str (die->sect_off));
e8d05480
JB
13069 return;
13070 }
c906108c 13071
3e29f34a
MR
13072 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13073 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13074
34eaf542
TT
13075 /* If we have any template arguments, then we must allocate a
13076 different sort of symbol. */
436c571c 13077 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13078 {
13079 if (child_die->tag == DW_TAG_template_type_param
13080 || child_die->tag == DW_TAG_template_value_param)
13081 {
8c14c3a3 13082 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13083 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13084 break;
13085 }
13086 }
13087
c24bdb02 13088 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13089 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13090 (struct symbol *) templ_func);
4c2df51b 13091
81873cc8 13092 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13093 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13094 cu->language);
13095
4cecd739
DJ
13096 /* If there is a location expression for DW_AT_frame_base, record
13097 it. */
e142c38c 13098 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13099 if (attr != nullptr)
fe978cb0 13100 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13101
63e43d3a
PMR
13102 /* If there is a location for the static link, record it. */
13103 newobj->static_link = NULL;
13104 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13105 if (attr != nullptr)
63e43d3a 13106 {
224c3ddb
SM
13107 newobj->static_link
13108 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13109 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13110 cu->per_cu->addr_type ());
63e43d3a
PMR
13111 }
13112
c24bdb02 13113 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13114
639d11d3 13115 if (die->child != NULL)
c906108c 13116 {
639d11d3 13117 child_die = die->child;
c906108c
SS
13118 while (child_die && child_die->tag)
13119 {
34eaf542
TT
13120 if (child_die->tag == DW_TAG_template_type_param
13121 || child_die->tag == DW_TAG_template_value_param)
13122 {
13123 struct symbol *arg = new_symbol (child_die, NULL, cu);
13124
f1078f66 13125 if (arg != NULL)
2f4732b0 13126 template_args.push_back (arg);
34eaf542
TT
13127 }
13128 else
13129 process_die (child_die, cu);
436c571c 13130 child_die = child_die->sibling;
c906108c
SS
13131 }
13132 }
13133
d389af10
JK
13134 inherit_abstract_dies (die, cu);
13135
4a811a97
UW
13136 /* If we have a DW_AT_specification, we might need to import using
13137 directives from the context of the specification DIE. See the
13138 comment in determine_prefix. */
13139 if (cu->language == language_cplus
13140 && dwarf2_attr (die, DW_AT_specification, cu))
13141 {
13142 struct dwarf2_cu *spec_cu = cu;
13143 struct die_info *spec_die = die_specification (die, &spec_cu);
13144
13145 while (spec_die)
13146 {
13147 child_die = spec_die->child;
13148 while (child_die && child_die->tag)
13149 {
13150 if (child_die->tag == DW_TAG_imported_module)
13151 process_die (child_die, spec_cu);
436c571c 13152 child_die = child_die->sibling;
4a811a97
UW
13153 }
13154
13155 /* In some cases, GCC generates specification DIEs that
13156 themselves contain DW_AT_specification attributes. */
13157 spec_die = die_specification (spec_die, &spec_cu);
13158 }
13159 }
13160
c24bdb02 13161 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13162 /* Make a block for the local symbols within. */
c24bdb02 13163 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13164 cstk.static_link, lowpc, highpc);
801e3a5b 13165
df8a16a1 13166 /* For C++, set the block's scope. */
45280282
IB
13167 if ((cu->language == language_cplus
13168 || cu->language == language_fortran
c44af4eb
TT
13169 || cu->language == language_d
13170 || cu->language == language_rust)
4d4ec4e5 13171 && cu->processing_has_namespace_info)
195a3f6c
TT
13172 block_set_scope (block, determine_prefix (die, cu),
13173 &objfile->objfile_obstack);
df8a16a1 13174
801e3a5b
JB
13175 /* If we have address ranges, record them. */
13176 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13177
a60f3166 13178 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13179
34eaf542 13180 /* Attach template arguments to function. */
2f4732b0 13181 if (!template_args.empty ())
34eaf542
TT
13182 {
13183 gdb_assert (templ_func != NULL);
13184
2f4732b0 13185 templ_func->n_template_arguments = template_args.size ();
34eaf542 13186 templ_func->template_arguments
8d749320
SM
13187 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13188 templ_func->n_template_arguments);
34eaf542 13189 memcpy (templ_func->template_arguments,
2f4732b0 13190 template_args.data (),
34eaf542 13191 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13192
13193 /* Make sure that the symtab is set on the new symbols. Even
13194 though they don't appear in this symtab directly, other parts
13195 of gdb assume that symbols do, and this is reasonably
13196 true. */
8634679f 13197 for (symbol *sym : template_args)
3e1d3d8c 13198 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13199 }
13200
208d8187
JB
13201 /* In C++, we can have functions nested inside functions (e.g., when
13202 a function declares a class that has methods). This means that
13203 when we finish processing a function scope, we may need to go
13204 back to building a containing block's symbol lists. */
c24bdb02
KS
13205 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13206 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13207
921e78cf
JB
13208 /* If we've finished processing a top-level function, subsequent
13209 symbols go in the file symbol list. */
c24bdb02
KS
13210 if (cu->get_builder ()->outermost_context_p ())
13211 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13212}
13213
13214/* Process all the DIES contained within a lexical block scope. Start
13215 a new scope, process the dies, and then close the scope. */
13216
13217static void
e7c27a73 13218read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13219{
5e22e966 13220 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13221 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13222 CORE_ADDR lowpc, highpc;
13223 struct die_info *child_die;
e142c38c
DJ
13224 CORE_ADDR baseaddr;
13225
b3b3bada 13226 baseaddr = objfile->text_section_offset ();
c906108c
SS
13227
13228 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13229 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13230 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13231 be nasty. Might be easier to properly extend generic blocks to
af34e669 13232 describe ranges. */
e385593e
JK
13233 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13234 {
13235 case PC_BOUNDS_NOT_PRESENT:
13236 /* DW_TAG_lexical_block has no attributes, process its children as if
13237 there was no wrapping by that DW_TAG_lexical_block.
13238 GCC does no longer produces such DWARF since GCC r224161. */
13239 for (child_die = die->child;
13240 child_die != NULL && child_die->tag;
436c571c 13241 child_die = child_die->sibling)
4f7bc5ed
TT
13242 {
13243 /* We might already be processing this DIE. This can happen
13244 in an unusual circumstance -- where a subroutine A
13245 appears lexically in another subroutine B, but A actually
13246 inlines B. The recursion is broken here, rather than in
13247 inherit_abstract_dies, because it seems better to simply
13248 drop concrete children here. */
13249 if (!child_die->in_process)
13250 process_die (child_die, cu);
13251 }
e385593e
JK
13252 return;
13253 case PC_BOUNDS_INVALID:
13254 return;
13255 }
3e29f34a
MR
13256 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13257 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13258
c24bdb02 13259 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13260 if (die->child != NULL)
c906108c 13261 {
639d11d3 13262 child_die = die->child;
c906108c
SS
13263 while (child_die && child_die->tag)
13264 {
e7c27a73 13265 process_die (child_die, cu);
436c571c 13266 child_die = child_die->sibling;
c906108c
SS
13267 }
13268 }
3ea89b92 13269 inherit_abstract_dies (die, cu);
c24bdb02 13270 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13271
c24bdb02
KS
13272 if (*cu->get_builder ()->get_local_symbols () != NULL
13273 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13274 {
801e3a5b 13275 struct block *block
c24bdb02 13276 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13277 cstk.start_addr, highpc);
801e3a5b
JB
13278
13279 /* Note that recording ranges after traversing children, as we
13280 do here, means that recording a parent's ranges entails
13281 walking across all its children's ranges as they appear in
13282 the address map, which is quadratic behavior.
13283
13284 It would be nicer to record the parent's ranges before
13285 traversing its children, simply overriding whatever you find
13286 there. But since we don't even decide whether to create a
13287 block until after we've traversed its children, that's hard
13288 to do. */
13289 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13290 }
c24bdb02
KS
13291 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13292 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13293}
13294
216f72a1 13295/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13296
13297static void
13298read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13299{
5e22e966 13300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13301 struct objfile *objfile = per_objfile->objfile;
08feed99 13302 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13303 CORE_ADDR pc, baseaddr;
13304 struct attribute *attr;
13305 struct call_site *call_site, call_site_local;
13306 void **slot;
13307 int nparams;
13308 struct die_info *child_die;
13309
b3b3bada 13310 baseaddr = objfile->text_section_offset ();
96408a79 13311
216f72a1
JK
13312 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13313 if (attr == NULL)
13314 {
13315 /* This was a pre-DWARF-5 GNU extension alias
13316 for DW_AT_call_return_pc. */
13317 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13318 }
96408a79
SA
13319 if (!attr)
13320 {
b98664d3 13321 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13322 "DIE %s [in module %s]"),
13323 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13324 return;
13325 }
cd6c91b4 13326 pc = attr->value_as_address () + baseaddr;
3e29f34a 13327 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13328
13329 if (cu->call_site_htab == NULL)
13330 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13331 NULL, &objfile->objfile_obstack,
13332 hashtab_obstack_allocate, NULL);
13333 call_site_local.pc = pc;
13334 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13335 if (*slot != NULL)
13336 {
b98664d3 13337 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13338 "DIE %s [in module %s]"),
13339 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13340 objfile_name (objfile));
96408a79
SA
13341 return;
13342 }
13343
13344 /* Count parameters at the caller. */
13345
13346 nparams = 0;
13347 for (child_die = die->child; child_die && child_die->tag;
436c571c 13348 child_die = child_die->sibling)
96408a79 13349 {
216f72a1
JK
13350 if (child_die->tag != DW_TAG_call_site_parameter
13351 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13352 {
b98664d3 13353 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13354 "DW_TAG_call_site child DIE %s [in module %s]"),
13355 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13356 objfile_name (objfile));
96408a79
SA
13357 continue;
13358 }
13359
13360 nparams++;
13361 }
13362
224c3ddb
SM
13363 call_site
13364 = ((struct call_site *)
13365 obstack_alloc (&objfile->objfile_obstack,
13366 sizeof (*call_site)
13367 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13368 *slot = call_site;
13369 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13370 call_site->pc = pc;
13371
216f72a1
JK
13372 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13373 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13374 {
13375 struct die_info *func_die;
13376
13377 /* Skip also over DW_TAG_inlined_subroutine. */
13378 for (func_die = die->parent;
13379 func_die && func_die->tag != DW_TAG_subprogram
13380 && func_die->tag != DW_TAG_subroutine_type;
13381 func_die = func_die->parent);
13382
216f72a1
JK
13383 /* DW_AT_call_all_calls is a superset
13384 of DW_AT_call_all_tail_calls. */
96408a79 13385 if (func_die
216f72a1 13386 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13388 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13389 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13390 {
13391 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13392 not complete. But keep CALL_SITE for look ups via call_site_htab,
13393 both the initial caller containing the real return address PC and
13394 the final callee containing the current PC of a chain of tail
13395 calls do not need to have the tail call list complete. But any
13396 function candidate for a virtual tail call frame searched via
13397 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13398 determined unambiguously. */
13399 }
13400 else
13401 {
13402 struct type *func_type = NULL;
13403
13404 if (func_die)
13405 func_type = get_die_type (func_die, cu);
13406 if (func_type != NULL)
13407 {
78134374 13408 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13409
13410 /* Enlist this call site to the function. */
13411 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13412 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13413 }
13414 else
b98664d3 13415 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13416 "DIE %s [in module %s]"),
13417 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13418 }
13419 }
13420
216f72a1
JK
13421 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13422 if (attr == NULL)
13423 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13424 if (attr == NULL)
13425 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13426 if (attr == NULL)
216f72a1
JK
13427 {
13428 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13429 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13430 }
96408a79 13431 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13432 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13433 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13434 else if (attr->form_is_block ())
96408a79
SA
13435 {
13436 struct dwarf2_locexpr_baton *dlbaton;
13437
8d749320 13438 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13439 dlbaton->data = DW_BLOCK (attr)->data;
13440 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13441 dlbaton->per_objfile = per_objfile;
96408a79
SA
13442 dlbaton->per_cu = cu->per_cu;
13443
13444 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13445 }
cd6c91b4 13446 else if (attr->form_is_ref ())
96408a79 13447 {
96408a79
SA
13448 struct dwarf2_cu *target_cu = cu;
13449 struct die_info *target_die;
13450
ac9ec31b 13451 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13452 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13453 if (die_is_declaration (target_die, target_cu))
13454 {
7d45c7c3 13455 const char *target_physname;
9112db09
JK
13456
13457 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13458 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13459 if (target_physname == NULL)
9112db09 13460 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13461 if (target_physname == NULL)
b98664d3 13462 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13463 "physname, for referencing DIE %s [in module %s]"),
13464 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13465 else
7d455152 13466 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13467 }
13468 else
13469 {
13470 CORE_ADDR lowpc;
13471
13472 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13473 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13474 <= PC_BOUNDS_INVALID)
b98664d3 13475 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13476 "low pc, for referencing DIE %s [in module %s]"),
13477 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13478 else
3e29f34a
MR
13479 {
13480 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13481 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13482 }
96408a79
SA
13483 }
13484 }
13485 else
b98664d3 13486 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13487 "block nor reference, for DIE %s [in module %s]"),
13488 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13489
13490 call_site->per_cu = cu->per_cu;
13491
13492 for (child_die = die->child;
13493 child_die && child_die->tag;
436c571c 13494 child_die = child_die->sibling)
96408a79 13495 {
96408a79 13496 struct call_site_parameter *parameter;
1788b2d3 13497 struct attribute *loc, *origin;
96408a79 13498
216f72a1
JK
13499 if (child_die->tag != DW_TAG_call_site_parameter
13500 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13501 {
13502 /* Already printed the complaint above. */
13503 continue;
13504 }
13505
13506 gdb_assert (call_site->parameter_count < nparams);
13507 parameter = &call_site->parameter[call_site->parameter_count];
13508
1788b2d3
JK
13509 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13510 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13511 register is contained in DW_AT_call_value. */
96408a79 13512
24c5c679 13513 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13514 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13515 if (origin == NULL)
13516 {
13517 /* This was a pre-DWARF-5 GNU extension alias
13518 for DW_AT_call_parameter. */
13519 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13520 }
cd6c91b4 13521 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13522 {
1788b2d3 13523 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13524
0826b30a 13525 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13526 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13527 {
13528 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13529 binding can be done only inside one CU. Such referenced DIE
13530 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13531 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13532 "DW_TAG_call_site child DIE %s [in module %s]"),
13533 sect_offset_str (child_die->sect_off),
9c541725 13534 objfile_name (objfile));
d76b7dbc
JK
13535 continue;
13536 }
9c541725
PA
13537 parameter->u.param_cu_off
13538 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13539 }
4fc6c0d5 13540 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13541 {
b98664d3 13542 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13543 "DW_TAG_call_site child DIE %s [in module %s]"),
13544 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13545 continue;
13546 }
24c5c679 13547 else
96408a79 13548 {
24c5c679
JK
13549 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13550 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13551 if (parameter->u.dwarf_reg != -1)
13552 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13553 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13554 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13555 &parameter->u.fb_offset))
13556 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13557 else
13558 {
b98664d3 13559 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13560 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13561 "DW_TAG_call_site child DIE %s "
24c5c679 13562 "[in module %s]"),
9d8780f0 13563 sect_offset_str (child_die->sect_off),
9c541725 13564 objfile_name (objfile));
24c5c679
JK
13565 continue;
13566 }
96408a79
SA
13567 }
13568
216f72a1
JK
13569 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13570 if (attr == NULL)
13571 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13572 if (attr == NULL || !attr->form_is_block ())
96408a79 13573 {
b98664d3 13574 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13575 "DW_TAG_call_site child DIE %s [in module %s]"),
13576 sect_offset_str (child_die->sect_off),
9c541725 13577 objfile_name (objfile));
96408a79
SA
13578 continue;
13579 }
13580 parameter->value = DW_BLOCK (attr)->data;
13581 parameter->value_size = DW_BLOCK (attr)->size;
13582
13583 /* Parameters are not pre-cleared by memset above. */
13584 parameter->data_value = NULL;
13585 parameter->data_value_size = 0;
13586 call_site->parameter_count++;
13587
216f72a1
JK
13588 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13589 if (attr == NULL)
13590 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13591 if (attr != nullptr)
96408a79 13592 {
4fc6c0d5 13593 if (!attr->form_is_block ())
b98664d3 13594 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13595 "DW_TAG_call_site child DIE %s [in module %s]"),
13596 sect_offset_str (child_die->sect_off),
9c541725 13597 objfile_name (objfile));
96408a79
SA
13598 else
13599 {
13600 parameter->data_value = DW_BLOCK (attr)->data;
13601 parameter->data_value_size = DW_BLOCK (attr)->size;
13602 }
13603 }
13604 }
13605}
13606
71a3c369
TT
13607/* Helper function for read_variable. If DIE represents a virtual
13608 table, then return the type of the concrete object that is
13609 associated with the virtual table. Otherwise, return NULL. */
13610
13611static struct type *
13612rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13613{
13614 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13615 if (attr == NULL)
13616 return NULL;
13617
13618 /* Find the type DIE. */
13619 struct die_info *type_die = NULL;
13620 struct dwarf2_cu *type_cu = cu;
13621
cd6c91b4 13622 if (attr->form_is_ref ())
71a3c369
TT
13623 type_die = follow_die_ref (die, attr, &type_cu);
13624 if (type_die == NULL)
13625 return NULL;
13626
13627 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13628 return NULL;
13629 return die_containing_type (type_die, type_cu);
13630}
13631
13632/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13633
13634static void
13635read_variable (struct die_info *die, struct dwarf2_cu *cu)
13636{
13637 struct rust_vtable_symbol *storage = NULL;
13638
13639 if (cu->language == language_rust)
13640 {
13641 struct type *containing_type = rust_containing_type (die, cu);
13642
13643 if (containing_type != NULL)
13644 {
5e22e966 13645 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13646
8c14c3a3 13647 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13648 storage->concrete_type = containing_type;
cf724bc9 13649 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13650 }
13651 }
13652
e4a62c65
TV
13653 struct symbol *res = new_symbol (die, NULL, cu, storage);
13654 struct attribute *abstract_origin
13655 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13656 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13657 if (res == NULL && loc && abstract_origin)
13658 {
13659 /* We have a variable without a name, but with a location and an abstract
13660 origin. This may be a concrete instance of an abstract variable
13661 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13662 later. */
13663 struct dwarf2_cu *origin_cu = cu;
13664 struct die_info *origin_die
13665 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13666 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13667 per_objfile->per_bfd->abstract_to_concrete
13668 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13669 }
71a3c369
TT
13670}
13671
43988095
JK
13672/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13673 reading .debug_rnglists.
13674 Callback's type should be:
13675 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13676 Return true if the attributes are present and valid, otherwise,
13677 return false. */
13678
13679template <typename Callback>
13680static bool
13681dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13682 Callback &&callback)
13683{
5e22e966 13684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13685 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13686 bfd *obfd = objfile->obfd;
43988095 13687 /* Base address selection entry. */
2b24b6e4 13688 gdb::optional<CORE_ADDR> base;
43988095 13689 const gdb_byte *buffer;
43988095
JK
13690 CORE_ADDR baseaddr;
13691 bool overflow = false;
13692
43988095
JK
13693 base = cu->base_address;
13694
5989a64e
SM
13695 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13696 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13697 {
b98664d3 13698 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13699 offset);
13700 return false;
13701 }
5989a64e 13702 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13703
b3b3bada 13704 baseaddr = objfile->text_section_offset ();
43988095
JK
13705
13706 while (1)
13707 {
7814882a
JK
13708 /* Initialize it due to a false compiler warning. */
13709 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13710 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13711 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13712 unsigned int bytes_read;
13713
13714 if (buffer == buf_end)
13715 {
13716 overflow = true;
13717 break;
13718 }
13719 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13720 switch (rlet)
13721 {
13722 case DW_RLE_end_of_list:
13723 break;
13724 case DW_RLE_base_address:
13725 if (buffer + cu->header.addr_size > buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
c8a7a66f 13730 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13731 buffer += bytes_read;
13732 break;
13733 case DW_RLE_start_length:
13734 if (buffer + cu->header.addr_size > buf_end)
13735 {
13736 overflow = true;
13737 break;
13738 }
c8a7a66f
TT
13739 range_beginning = cu->header.read_address (obfd, buffer,
13740 &bytes_read);
43988095
JK
13741 buffer += bytes_read;
13742 range_end = (range_beginning
13743 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13744 buffer += bytes_read;
13745 if (buffer > buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 break;
13751 case DW_RLE_offset_pair:
13752 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13753 buffer += bytes_read;
13754 if (buffer > buf_end)
13755 {
13756 overflow = true;
13757 break;
13758 }
13759 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13760 buffer += bytes_read;
13761 if (buffer > buf_end)
13762 {
13763 overflow = true;
13764 break;
13765 }
13766 break;
13767 case DW_RLE_start_end:
13768 if (buffer + 2 * cu->header.addr_size > buf_end)
13769 {
13770 overflow = true;
13771 break;
13772 }
c8a7a66f
TT
13773 range_beginning = cu->header.read_address (obfd, buffer,
13774 &bytes_read);
43988095 13775 buffer += bytes_read;
c8a7a66f 13776 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13777 buffer += bytes_read;
13778 break;
13779 default:
b98664d3 13780 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13781 return false;
13782 }
13783 if (rlet == DW_RLE_end_of_list || overflow)
13784 break;
13785 if (rlet == DW_RLE_base_address)
13786 continue;
13787
2b24b6e4 13788 if (!base.has_value ())
43988095
JK
13789 {
13790 /* We have no valid base address for the ranges
13791 data. */
b98664d3 13792 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13793 return false;
13794 }
13795
13796 if (range_beginning > range_end)
13797 {
13798 /* Inverted range entries are invalid. */
b98664d3 13799 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13800 return false;
13801 }
13802
13803 /* Empty range entries have no effect. */
13804 if (range_beginning == range_end)
13805 continue;
13806
2b24b6e4
TT
13807 range_beginning += *base;
13808 range_end += *base;
43988095
JK
13809
13810 /* A not-uncommon case of bad debug info.
13811 Don't pollute the addrmap with bad data. */
13812 if (range_beginning + baseaddr == 0
5989a64e 13813 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13814 {
b98664d3 13815 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13816 " [in module %s]"), objfile_name (objfile));
13817 continue;
13818 }
13819
13820 callback (range_beginning, range_end);
13821 }
13822
13823 if (overflow)
13824 {
b98664d3 13825 complaint (_("Offset %d is not terminated "
43988095
JK
13826 "for DW_AT_ranges attribute"),
13827 offset);
13828 return false;
13829 }
13830
13831 return true;
13832}
13833
13834/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13835 Callback's type should be:
13836 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13837 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13838
43988095 13839template <typename Callback>
43039443 13840static int
5f46c5a5 13841dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13842 Callback &&callback)
43039443 13843{
5e22e966
SM
13844 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13845 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13846 struct comp_unit_head *cu_header = &cu->header;
13847 bfd *obfd = objfile->obfd;
13848 unsigned int addr_size = cu_header->addr_size;
13849 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13850 /* Base address selection entry. */
2b24b6e4 13851 gdb::optional<CORE_ADDR> base;
43039443 13852 unsigned int dummy;
d521ce57 13853 const gdb_byte *buffer;
ff013f42 13854 CORE_ADDR baseaddr;
43039443 13855
43988095
JK
13856 if (cu_header->version >= 5)
13857 return dwarf2_rnglists_process (offset, cu, callback);
13858
d00adf39 13859 base = cu->base_address;
43039443 13860
5e22e966
SM
13861 per_objfile->per_bfd->ranges.read (objfile);
13862 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13863 {
b98664d3 13864 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13865 offset);
13866 return 0;
13867 }
5e22e966 13868 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13869
b3b3bada 13870 baseaddr = objfile->text_section_offset ();
ff013f42 13871
43039443
JK
13872 while (1)
13873 {
13874 CORE_ADDR range_beginning, range_end;
13875
c8a7a66f 13876 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13877 buffer += addr_size;
c8a7a66f 13878 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13879 buffer += addr_size;
13880 offset += 2 * addr_size;
13881
13882 /* An end of list marker is a pair of zero addresses. */
13883 if (range_beginning == 0 && range_end == 0)
13884 /* Found the end of list entry. */
13885 break;
13886
13887 /* Each base address selection entry is a pair of 2 values.
13888 The first is the largest possible address, the second is
13889 the base address. Check for a base address here. */
13890 if ((range_beginning & mask) == mask)
13891 {
28d2bfb9
AB
13892 /* If we found the largest possible address, then we already
13893 have the base address in range_end. */
13894 base = range_end;
43039443
JK
13895 continue;
13896 }
13897
2b24b6e4 13898 if (!base.has_value ())
43039443
JK
13899 {
13900 /* We have no valid base address for the ranges
13901 data. */
b98664d3 13902 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13903 return 0;
13904 }
13905
9277c30c
UW
13906 if (range_beginning > range_end)
13907 {
13908 /* Inverted range entries are invalid. */
b98664d3 13909 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13910 return 0;
13911 }
13912
13913 /* Empty range entries have no effect. */
13914 if (range_beginning == range_end)
13915 continue;
13916
2b24b6e4
TT
13917 range_beginning += *base;
13918 range_end += *base;
43039443 13919
01093045
DE
13920 /* A not-uncommon case of bad debug info.
13921 Don't pollute the addrmap with bad data. */
13922 if (range_beginning + baseaddr == 0
5e22e966 13923 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13924 {
b98664d3 13925 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13926 " [in module %s]"), objfile_name (objfile));
01093045
DE
13927 continue;
13928 }
13929
5f46c5a5
JK
13930 callback (range_beginning, range_end);
13931 }
13932
13933 return 1;
13934}
13935
13936/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13937 Return 1 if the attributes are present and valid, otherwise, return 0.
13938 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13939
13940static int
13941dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13942 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13943 dwarf2_psymtab *ranges_pst)
5f46c5a5 13944{
5e22e966 13945 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13946 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13947 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13948 int low_set = 0;
13949 CORE_ADDR low = 0;
13950 CORE_ADDR high = 0;
13951 int retval;
13952
13953 retval = dwarf2_ranges_process (offset, cu,
13954 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13955 {
9277c30c 13956 if (ranges_pst != NULL)
3e29f34a
MR
13957 {
13958 CORE_ADDR lowpc;
13959 CORE_ADDR highpc;
13960
79748972
TT
13961 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13962 range_beginning + baseaddr)
13963 - baseaddr);
13964 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13965 range_end + baseaddr)
13966 - baseaddr);
d320c2b5
TT
13967 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13968 lowpc, highpc - 1, ranges_pst);
3e29f34a 13969 }
ff013f42 13970
43039443
JK
13971 /* FIXME: This is recording everything as a low-high
13972 segment of consecutive addresses. We should have a
13973 data structure for discontiguous block ranges
13974 instead. */
13975 if (! low_set)
13976 {
13977 low = range_beginning;
13978 high = range_end;
13979 low_set = 1;
13980 }
13981 else
13982 {
13983 if (range_beginning < low)
13984 low = range_beginning;
13985 if (range_end > high)
13986 high = range_end;
13987 }
5f46c5a5
JK
13988 });
13989 if (!retval)
13990 return 0;
43039443
JK
13991
13992 if (! low_set)
13993 /* If the first entry is an end-of-list marker, the range
13994 describes an empty scope, i.e. no instructions. */
13995 return 0;
13996
13997 if (low_return)
13998 *low_return = low;
13999 if (high_return)
14000 *high_return = high;
14001 return 1;
14002}
14003
3a2b436a
JK
14004/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14005 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14006 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14007
3a2b436a 14008static enum pc_bounds_kind
af34e669 14009dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14010 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14011 dwarf2_psymtab *pst)
c906108c 14012{
5e22e966 14013 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14014 struct attribute *attr;
91da1414 14015 struct attribute *attr_high;
af34e669
DJ
14016 CORE_ADDR low = 0;
14017 CORE_ADDR high = 0;
e385593e 14018 enum pc_bounds_kind ret;
c906108c 14019
91da1414
MW
14020 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14021 if (attr_high)
af34e669 14022 {
e142c38c 14023 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14024 if (attr != nullptr)
91da1414 14025 {
cd6c91b4
TT
14026 low = attr->value_as_address ();
14027 high = attr_high->value_as_address ();
14028 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14029 high += low;
91da1414 14030 }
af34e669
DJ
14031 else
14032 /* Found high w/o low attribute. */
e385593e 14033 return PC_BOUNDS_INVALID;
af34e669
DJ
14034
14035 /* Found consecutive range of addresses. */
3a2b436a 14036 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14037 }
c906108c 14038 else
af34e669 14039 {
e142c38c 14040 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14041 if (attr != NULL)
14042 {
18a8505e 14043 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14044 We take advantage of the fact that DW_AT_ranges does not appear
14045 in DW_TAG_compile_unit of DWO files. */
14046 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14047 unsigned int ranges_offset = (DW_UNSND (attr)
14048 + (need_ranges_base
14049 ? cu->ranges_base
14050 : 0));
2e3cf129 14051
af34e669 14052 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14053 .debug_ranges section. */
2e3cf129 14054 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14055 return PC_BOUNDS_INVALID;
43039443 14056 /* Found discontinuous range of addresses. */
3a2b436a 14057 ret = PC_BOUNDS_RANGES;
af34e669 14058 }
e385593e
JK
14059 else
14060 return PC_BOUNDS_NOT_PRESENT;
af34e669 14061 }
c906108c 14062
48fbe735 14063 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14064 if (high <= low)
e385593e 14065 return PC_BOUNDS_INVALID;
c906108c
SS
14066
14067 /* When using the GNU linker, .gnu.linkonce. sections are used to
14068 eliminate duplicate copies of functions and vtables and such.
14069 The linker will arbitrarily choose one and discard the others.
14070 The AT_*_pc values for such functions refer to local labels in
14071 these sections. If the section from that file was discarded, the
14072 labels are not in the output, so the relocs get a value of 0.
14073 If this is a discarded function, mark the pc bounds as invalid,
14074 so that GDB will ignore it. */
5989a64e 14075 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14076 return PC_BOUNDS_INVALID;
c906108c
SS
14077
14078 *lowpc = low;
96408a79
SA
14079 if (highpc)
14080 *highpc = high;
af34e669 14081 return ret;
c906108c
SS
14082}
14083
b084d499
JB
14084/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14085 its low and high PC addresses. Do nothing if these addresses could not
14086 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14087 and HIGHPC to the high address if greater than HIGHPC. */
14088
14089static void
14090dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14091 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14092 struct dwarf2_cu *cu)
14093{
14094 CORE_ADDR low, high;
14095 struct die_info *child = die->child;
14096
e385593e 14097 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14098 {
325fac50
PA
14099 *lowpc = std::min (*lowpc, low);
14100 *highpc = std::max (*highpc, high);
b084d499
JB
14101 }
14102
14103 /* If the language does not allow nested subprograms (either inside
14104 subprograms or lexical blocks), we're done. */
14105 if (cu->language != language_ada)
14106 return;
6e70227d 14107
b084d499
JB
14108 /* Check all the children of the given DIE. If it contains nested
14109 subprograms, then check their pc bounds. Likewise, we need to
14110 check lexical blocks as well, as they may also contain subprogram
14111 definitions. */
14112 while (child && child->tag)
14113 {
14114 if (child->tag == DW_TAG_subprogram
14115 || child->tag == DW_TAG_lexical_block)
14116 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14117 child = child->sibling;
b084d499
JB
14118 }
14119}
14120
fae299cd
DC
14121/* Get the low and high pc's represented by the scope DIE, and store
14122 them in *LOWPC and *HIGHPC. If the correct values can't be
14123 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14124
14125static void
14126get_scope_pc_bounds (struct die_info *die,
14127 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14128 struct dwarf2_cu *cu)
14129{
14130 CORE_ADDR best_low = (CORE_ADDR) -1;
14131 CORE_ADDR best_high = (CORE_ADDR) 0;
14132 CORE_ADDR current_low, current_high;
14133
3a2b436a 14134 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14135 >= PC_BOUNDS_RANGES)
fae299cd
DC
14136 {
14137 best_low = current_low;
14138 best_high = current_high;
14139 }
14140 else
14141 {
14142 struct die_info *child = die->child;
14143
14144 while (child && child->tag)
14145 {
14146 switch (child->tag) {
14147 case DW_TAG_subprogram:
b084d499 14148 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14149 break;
14150 case DW_TAG_namespace:
f55ee35c 14151 case DW_TAG_module:
fae299cd
DC
14152 /* FIXME: carlton/2004-01-16: Should we do this for
14153 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14154 that current GCC's always emit the DIEs corresponding
14155 to definitions of methods of classes as children of a
14156 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14157 the DIEs giving the declarations, which could be
14158 anywhere). But I don't see any reason why the
14159 standards says that they have to be there. */
14160 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14161
14162 if (current_low != ((CORE_ADDR) -1))
14163 {
325fac50
PA
14164 best_low = std::min (best_low, current_low);
14165 best_high = std::max (best_high, current_high);
fae299cd
DC
14166 }
14167 break;
14168 default:
0963b4bd 14169 /* Ignore. */
fae299cd
DC
14170 break;
14171 }
14172
436c571c 14173 child = child->sibling;
fae299cd
DC
14174 }
14175 }
14176
14177 *lowpc = best_low;
14178 *highpc = best_high;
14179}
14180
801e3a5b
JB
14181/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14182 in DIE. */
380bca97 14183
801e3a5b
JB
14184static void
14185dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14186 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14187{
5e22e966 14188 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14189 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14190 struct attribute *attr;
91da1414 14191 struct attribute *attr_high;
801e3a5b 14192
91da1414
MW
14193 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14194 if (attr_high)
801e3a5b 14195 {
801e3a5b 14196 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14197 if (attr != nullptr)
801e3a5b 14198 {
cd6c91b4
TT
14199 CORE_ADDR low = attr->value_as_address ();
14200 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14201
cd6c91b4 14202 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14203 high += low;
9a619af0 14204
3e29f34a
MR
14205 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14206 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14207 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14208 }
14209 }
14210
14211 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14212 if (attr != nullptr)
801e3a5b 14213 {
18a8505e 14214 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14215 We take advantage of the fact that DW_AT_ranges does not appear
14216 in DW_TAG_compile_unit of DWO files. */
14217 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14218
14219 /* The value of the DW_AT_ranges attribute is the offset of the
14220 address range list in the .debug_ranges section. */
ab435259
DE
14221 unsigned long offset = (DW_UNSND (attr)
14222 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14223
2d5f09ec 14224 std::vector<blockrange> blockvec;
5f46c5a5
JK
14225 dwarf2_ranges_process (offset, cu,
14226 [&] (CORE_ADDR start, CORE_ADDR end)
14227 {
58fdfd2c
JK
14228 start += baseaddr;
14229 end += baseaddr;
5f46c5a5
JK
14230 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14231 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14232 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14233 blockvec.emplace_back (start, end);
5f46c5a5 14234 });
2d5f09ec
KB
14235
14236 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14237 }
14238}
14239
685b1105
JK
14240/* Check whether the producer field indicates either of GCC < 4.6, or the
14241 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14242
685b1105
JK
14243static void
14244check_producer (struct dwarf2_cu *cu)
60d5a603 14245{
38360086 14246 int major, minor;
60d5a603
JK
14247
14248 if (cu->producer == NULL)
14249 {
14250 /* For unknown compilers expect their behavior is DWARF version
14251 compliant.
14252
14253 GCC started to support .debug_types sections by -gdwarf-4 since
14254 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14255 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14256 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14257 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14258 }
b1ffba5a 14259 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14260 {
38360086
MW
14261 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14262 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14263 }
5230b05a 14264 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14265 {
14266 cu->producer_is_icc = true;
14267 cu->producer_is_icc_lt_14 = major < 14;
14268 }
c258c396
JD
14269 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14270 cu->producer_is_codewarrior = true;
685b1105
JK
14271 else
14272 {
14273 /* For other non-GCC compilers, expect their behavior is DWARF version
14274 compliant. */
60d5a603
JK
14275 }
14276
9068261f 14277 cu->checked_producer = true;
685b1105 14278}
ba919b58 14279
685b1105
JK
14280/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14281 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14282 during 4.6.0 experimental. */
14283
9068261f 14284static bool
685b1105
JK
14285producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14286{
14287 if (!cu->checked_producer)
14288 check_producer (cu);
14289
14290 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14291}
14292
c258c396
JD
14293
14294/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14295 with incorrect is_stmt attributes. */
14296
14297static bool
14298producer_is_codewarrior (struct dwarf2_cu *cu)
14299{
14300 if (!cu->checked_producer)
14301 check_producer (cu);
14302
14303 return cu->producer_is_codewarrior;
14304}
14305
405feb71 14306/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14307 DW_AT_accessibility. */
14308
14309static enum dwarf_access_attribute
14310dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14311{
14312 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14313 {
14314 /* The default DWARF 2 accessibility for members is public, the default
14315 accessibility for inheritance is private. */
14316
14317 if (die->tag != DW_TAG_inheritance)
14318 return DW_ACCESS_public;
14319 else
14320 return DW_ACCESS_private;
14321 }
14322 else
14323 {
14324 /* DWARF 3+ defines the default accessibility a different way. The same
14325 rules apply now for DW_TAG_inheritance as for the members and it only
14326 depends on the container kind. */
14327
14328 if (die->parent->tag == DW_TAG_class_type)
14329 return DW_ACCESS_private;
14330 else
14331 return DW_ACCESS_public;
14332 }
14333}
14334
74ac6d43
TT
14335/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14336 offset. If the attribute was not found return 0, otherwise return
14337 1. If it was found but could not properly be handled, set *OFFSET
14338 to 0. */
14339
14340static int
14341handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14342 LONGEST *offset)
14343{
14344 struct attribute *attr;
14345
14346 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14347 if (attr != NULL)
14348 {
14349 *offset = 0;
14350
14351 /* Note that we do not check for a section offset first here.
14352 This is because DW_AT_data_member_location is new in DWARF 4,
14353 so if we see it, we can assume that a constant form is really
14354 a constant and not a section offset. */
cd6c91b4 14355 if (attr->form_is_constant ())
0826b30a 14356 *offset = attr->constant_value (0);
cd6c91b4 14357 else if (attr->form_is_section_offset ())
74ac6d43 14358 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14359 else if (attr->form_is_block ())
74ac6d43
TT
14360 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14361 else
14362 dwarf2_complex_location_expr_complaint ();
14363
14364 return 1;
14365 }
14366
14367 return 0;
14368}
14369
7d79de9a
TT
14370/* Look for DW_AT_data_member_location and store the results in FIELD. */
14371
14372static void
14373handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14374 struct field *field)
14375{
14376 struct attribute *attr;
14377
14378 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14379 if (attr != NULL)
14380 {
14381 if (attr->form_is_constant ())
14382 {
14383 LONGEST offset = attr->constant_value (0);
14384 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14385 }
14386 else if (attr->form_is_section_offset ())
14387 dwarf2_complex_location_expr_complaint ();
14388 else if (attr->form_is_block ())
14389 {
14390 bool handled;
14391 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14392 if (handled)
14393 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14394 else
14395 {
5e22e966
SM
14396 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14397 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14398 struct dwarf2_locexpr_baton *dlbaton
14399 = XOBNEW (&objfile->objfile_obstack,
14400 struct dwarf2_locexpr_baton);
14401 dlbaton->data = DW_BLOCK (attr)->data;
14402 dlbaton->size = DW_BLOCK (attr)->size;
14403 /* When using this baton, we want to compute the address
14404 of the field, not the value. This is why
14405 is_reference is set to false here. */
14406 dlbaton->is_reference = false;
5e22e966 14407 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14408 dlbaton->per_cu = cu->per_cu;
14409
14410 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14411 }
14412 }
14413 else
14414 dwarf2_complex_location_expr_complaint ();
14415 }
14416}
14417
c906108c
SS
14418/* Add an aggregate field to the field list. */
14419
14420static void
107d2387 14421dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14422 struct dwarf2_cu *cu)
6e70227d 14423{
5e22e966 14424 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14425 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14426 struct nextfield *new_field;
14427 struct attribute *attr;
14428 struct field *fp;
15d034d0 14429 const char *fieldname = "";
c906108c 14430
7d0ccb61
DJ
14431 if (die->tag == DW_TAG_inheritance)
14432 {
be2daae6
TT
14433 fip->baseclasses.emplace_back ();
14434 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14435 }
14436 else
14437 {
be2daae6
TT
14438 fip->fields.emplace_back ();
14439 new_field = &fip->fields.back ();
7d0ccb61 14440 }
be2daae6 14441
9c6a1327
TT
14442 new_field->offset = die->sect_off;
14443
e142c38c 14444 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14445 if (attr != nullptr)
c906108c 14446 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14447 else
14448 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14449 if (new_field->accessibility != DW_ACCESS_public)
14450 fip->non_public_fields = 1;
60d5a603 14451
e142c38c 14452 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14453 if (attr != nullptr)
c906108c 14454 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14455 else
14456 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14457
14458 fp = &new_field->field;
a9a9bd0f 14459
e142c38c 14460 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14461 {
a9a9bd0f 14462 /* Data member other than a C++ static data member. */
6e70227d 14463
c906108c 14464 /* Get type of field. */
e7c27a73 14465 fp->type = die_type (die, cu);
c906108c 14466
d6a843b5 14467 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14468
c906108c 14469 /* Get bit size of field (zero if none). */
e142c38c 14470 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14471 if (attr != nullptr)
c906108c
SS
14472 {
14473 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14474 }
14475 else
14476 {
14477 FIELD_BITSIZE (*fp) = 0;
14478 }
14479
14480 /* Get bit offset of field. */
7d79de9a 14481 handle_data_member_location (die, cu, fp);
e142c38c 14482 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14483 if (attr != nullptr)
c906108c 14484 {
d5a22e77 14485 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14486 {
14487 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14488 additional bit offset from the MSB of the containing
14489 anonymous object to the MSB of the field. We don't
14490 have to do anything special since we don't need to
14491 know the size of the anonymous object. */
f41f5e61 14492 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14493 }
14494 else
14495 {
14496 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14497 MSB of the anonymous object, subtract off the number of
14498 bits from the MSB of the field to the MSB of the
14499 object, and then subtract off the number of bits of
14500 the field itself. The result is the bit offset of
14501 the LSB of the field. */
c906108c
SS
14502 int anonymous_size;
14503 int bit_offset = DW_UNSND (attr);
14504
e142c38c 14505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14506 if (attr != nullptr)
c906108c
SS
14507 {
14508 /* The size of the anonymous object containing
14509 the bit field is explicit, so use the
14510 indicated size (in bytes). */
14511 anonymous_size = DW_UNSND (attr);
14512 }
14513 else
14514 {
14515 /* The size of the anonymous object containing
14516 the bit field must be inferred from the type
14517 attribute of the data member containing the
14518 bit field. */
14519 anonymous_size = TYPE_LENGTH (fp->type);
14520 }
f41f5e61
PA
14521 SET_FIELD_BITPOS (*fp,
14522 (FIELD_BITPOS (*fp)
14523 + anonymous_size * bits_per_byte
14524 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14525 }
14526 }
da5b30da
AA
14527 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14528 if (attr != NULL)
14529 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14530 + attr->constant_value (0)));
c906108c
SS
14531
14532 /* Get name of field. */
39cbfefa
DJ
14533 fieldname = dwarf2_name (die, cu);
14534 if (fieldname == NULL)
14535 fieldname = "";
d8151005
DJ
14536
14537 /* The name is already allocated along with this objfile, so we don't
14538 need to duplicate it for the type. */
14539 fp->name = fieldname;
c906108c
SS
14540
14541 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14542 pointer or virtual base class pointer) to private. */
e142c38c 14543 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14544 {
d48cc9dd 14545 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14546 new_field->accessibility = DW_ACCESS_private;
14547 fip->non_public_fields = 1;
14548 }
14549 }
a9a9bd0f 14550 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14551 {
a9a9bd0f
DC
14552 /* C++ static member. */
14553
14554 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14555 is a declaration, but all versions of G++ as of this writing
14556 (so through at least 3.2.1) incorrectly generate
14557 DW_TAG_variable tags. */
6e70227d 14558
ff355380 14559 const char *physname;
c906108c 14560
a9a9bd0f 14561 /* Get name of field. */
39cbfefa
DJ
14562 fieldname = dwarf2_name (die, cu);
14563 if (fieldname == NULL)
c906108c
SS
14564 return;
14565
254e6b9e 14566 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14567 if (attr
14568 /* Only create a symbol if this is an external value.
14569 new_symbol checks this and puts the value in the global symbol
14570 table, which we want. If it is not external, new_symbol
14571 will try to put the value in cu->list_in_scope which is wrong. */
14572 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14573 {
14574 /* A static const member, not much different than an enum as far as
14575 we're concerned, except that we can support more types. */
14576 new_symbol (die, NULL, cu);
14577 }
14578
2df3850c 14579 /* Get physical name. */
ff355380 14580 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14581
d8151005
DJ
14582 /* The name is already allocated along with this objfile, so we don't
14583 need to duplicate it for the type. */
14584 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14585 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14586 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14587 }
14588 else if (die->tag == DW_TAG_inheritance)
14589 {
74ac6d43 14590 /* C++ base class field. */
7d79de9a 14591 handle_data_member_location (die, cu, fp);
c906108c 14592 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14593 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14594 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14595 }
2ddeaf8a
TT
14596 else
14597 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14598}
14599
883fd55a
KS
14600/* Can the type given by DIE define another type? */
14601
14602static bool
14603type_can_define_types (const struct die_info *die)
14604{
14605 switch (die->tag)
14606 {
14607 case DW_TAG_typedef:
14608 case DW_TAG_class_type:
14609 case DW_TAG_structure_type:
14610 case DW_TAG_union_type:
14611 case DW_TAG_enumeration_type:
14612 return true;
14613
14614 default:
14615 return false;
14616 }
14617}
14618
14619/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14620
14621static void
883fd55a
KS
14622dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14623 struct dwarf2_cu *cu)
6e70227d 14624{
be2daae6
TT
14625 struct decl_field fp;
14626 memset (&fp, 0, sizeof (fp));
98751a41 14627
883fd55a 14628 gdb_assert (type_can_define_types (die));
98751a41 14629
883fd55a 14630 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14631 fp.name = dwarf2_name (die, cu);
14632 fp.type = read_type_die (die, cu);
98751a41 14633
c191a687
KS
14634 /* Save accessibility. */
14635 enum dwarf_access_attribute accessibility;
14636 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14637 if (attr != NULL)
14638 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14639 else
14640 accessibility = dwarf2_default_access_attribute (die, cu);
14641 switch (accessibility)
14642 {
14643 case DW_ACCESS_public:
14644 /* The assumed value if neither private nor protected. */
14645 break;
14646 case DW_ACCESS_private:
be2daae6 14647 fp.is_private = 1;
c191a687
KS
14648 break;
14649 case DW_ACCESS_protected:
be2daae6 14650 fp.is_protected = 1;
c191a687
KS
14651 break;
14652 default:
b98664d3 14653 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14654 }
14655
883fd55a 14656 if (die->tag == DW_TAG_typedef)
be2daae6 14657 fip->typedef_field_list.push_back (fp);
883fd55a 14658 else
be2daae6 14659 fip->nested_types_list.push_back (fp);
98751a41
JK
14660}
14661
9c6a1327
TT
14662/* A convenience typedef that's used when finding the discriminant
14663 field for a variant part. */
1b95cdb7
SM
14664typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14665 offset_map_type;
9c6a1327
TT
14666
14667/* Compute the discriminant range for a given variant. OBSTACK is
14668 where the results will be stored. VARIANT is the variant to
14669 process. IS_UNSIGNED indicates whether the discriminant is signed
14670 or unsigned. */
14671
14672static const gdb::array_view<discriminant_range>
14673convert_variant_range (struct obstack *obstack, const variant_field &variant,
14674 bool is_unsigned)
14675{
14676 std::vector<discriminant_range> ranges;
14677
14678 if (variant.default_branch)
14679 return {};
14680
14681 if (variant.discr_list_data == nullptr)
14682 {
14683 discriminant_range r
14684 = {variant.discriminant_value, variant.discriminant_value};
14685 ranges.push_back (r);
14686 }
14687 else
14688 {
14689 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14690 variant.discr_list_data->size);
14691 while (!data.empty ())
14692 {
14693 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14694 {
14695 complaint (_("invalid discriminant marker: %d"), data[0]);
14696 break;
14697 }
14698 bool is_range = data[0] == DW_DSC_range;
14699 data = data.slice (1);
14700
14701 ULONGEST low, high;
14702 unsigned int bytes_read;
14703
14704 if (data.empty ())
14705 {
14706 complaint (_("DW_AT_discr_list missing low value"));
14707 break;
14708 }
14709 if (is_unsigned)
14710 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14711 else
14712 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14713 &bytes_read);
14714 data = data.slice (bytes_read);
14715
14716 if (is_range)
14717 {
14718 if (data.empty ())
14719 {
14720 complaint (_("DW_AT_discr_list missing high value"));
14721 break;
14722 }
14723 if (is_unsigned)
14724 high = read_unsigned_leb128 (nullptr, data.data (),
14725 &bytes_read);
14726 else
14727 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14728 &bytes_read);
14729 data = data.slice (bytes_read);
14730 }
14731 else
14732 high = low;
14733
14734 ranges.push_back ({ low, high });
14735 }
14736 }
14737
14738 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14739 ranges.size ());
14740 std::copy (ranges.begin (), ranges.end (), result);
14741 return gdb::array_view<discriminant_range> (result, ranges.size ());
14742}
14743
14744static const gdb::array_view<variant_part> create_variant_parts
14745 (struct obstack *obstack,
14746 const offset_map_type &offset_map,
14747 struct field_info *fi,
14748 const std::vector<variant_part_builder> &variant_parts);
14749
14750/* Fill in a "struct variant" for a given variant field. RESULT is
14751 the variant to fill in. OBSTACK is where any needed allocations
14752 will be done. OFFSET_MAP holds the mapping from section offsets to
14753 fields for the type. FI describes the fields of the type we're
14754 processing. FIELD is the variant field we're converting. */
14755
14756static void
14757create_one_variant (variant &result, struct obstack *obstack,
14758 const offset_map_type &offset_map,
14759 struct field_info *fi, const variant_field &field)
14760{
14761 result.discriminants = convert_variant_range (obstack, field, false);
14762 result.first_field = field.first_field + fi->baseclasses.size ();
14763 result.last_field = field.last_field + fi->baseclasses.size ();
14764 result.parts = create_variant_parts (obstack, offset_map, fi,
14765 field.variant_parts);
14766}
14767
14768/* Fill in a "struct variant_part" for a given variant part. RESULT
14769 is the variant part to fill in. OBSTACK is where any needed
14770 allocations will be done. OFFSET_MAP holds the mapping from
14771 section offsets to fields for the type. FI describes the fields of
14772 the type we're processing. BUILDER is the variant part to be
14773 converted. */
14774
14775static void
14776create_one_variant_part (variant_part &result,
14777 struct obstack *obstack,
14778 const offset_map_type &offset_map,
14779 struct field_info *fi,
14780 const variant_part_builder &builder)
14781{
14782 auto iter = offset_map.find (builder.discriminant_offset);
14783 if (iter == offset_map.end ())
14784 {
14785 result.discriminant_index = -1;
14786 /* Doesn't matter. */
14787 result.is_unsigned = false;
14788 }
14789 else
14790 {
14791 result.discriminant_index = iter->second;
14792 result.is_unsigned
14793 = TYPE_UNSIGNED (FIELD_TYPE
14794 (fi->fields[result.discriminant_index].field));
14795 }
14796
14797 size_t n = builder.variants.size ();
14798 variant *output = new (obstack) variant[n];
14799 for (size_t i = 0; i < n; ++i)
14800 create_one_variant (output[i], obstack, offset_map, fi,
14801 builder.variants[i]);
14802
14803 result.variants = gdb::array_view<variant> (output, n);
14804}
14805
14806/* Create a vector of variant parts that can be attached to a type.
14807 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14808 holds the mapping from section offsets to fields for the type. FI
14809 describes the fields of the type we're processing. VARIANT_PARTS
14810 is the vector to convert. */
14811
14812static const gdb::array_view<variant_part>
14813create_variant_parts (struct obstack *obstack,
14814 const offset_map_type &offset_map,
14815 struct field_info *fi,
14816 const std::vector<variant_part_builder> &variant_parts)
14817{
14818 if (variant_parts.empty ())
14819 return {};
14820
14821 size_t n = variant_parts.size ();
14822 variant_part *result = new (obstack) variant_part[n];
14823 for (size_t i = 0; i < n; ++i)
14824 create_one_variant_part (result[i], obstack, offset_map, fi,
14825 variant_parts[i]);
14826
14827 return gdb::array_view<variant_part> (result, n);
14828}
14829
14830/* Compute the variant part vector for FIP, attaching it to TYPE when
14831 done. */
14832
14833static void
14834add_variant_property (struct field_info *fip, struct type *type,
14835 struct dwarf2_cu *cu)
14836{
14837 /* Map section offsets of fields to their field index. Note the
14838 field index here does not take the number of baseclasses into
14839 account. */
14840 offset_map_type offset_map;
14841 for (int i = 0; i < fip->fields.size (); ++i)
14842 offset_map[fip->fields[i].offset] = i;
14843
5e22e966 14844 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14845 gdb::array_view<variant_part> parts
14846 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14847 fip->variant_parts);
14848
14849 struct dynamic_prop prop;
14850 prop.kind = PROP_VARIANT_PARTS;
14851 prop.data.variant_parts
14852 = ((gdb::array_view<variant_part> *)
14853 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14854
5c54719c 14855 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14856}
14857
c906108c
SS
14858/* Create the vector of fields, and attach it to the type. */
14859
14860static void
fba45db2 14861dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14862 struct dwarf2_cu *cu)
c906108c 14863{
317f7127 14864 int nfields = fip->nfields ();
c906108c
SS
14865
14866 /* Record the field count, allocate space for the array of fields,
14867 and create blank accessibility bitfields if necessary. */
5e33d5f4 14868 type->set_num_fields (nfields);
3cabb6b0
SM
14869 type->set_fields
14870 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14871
b4ba55a1 14872 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14873 {
14874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14875
14876 TYPE_FIELD_PRIVATE_BITS (type) =
14877 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14878 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14879
14880 TYPE_FIELD_PROTECTED_BITS (type) =
14881 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14882 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14883
774b6a14
TT
14884 TYPE_FIELD_IGNORE_BITS (type) =
14885 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14886 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14887 }
14888
14889 /* If the type has baseclasses, allocate and clear a bit vector for
14890 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14891 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14892 {
be2daae6 14893 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14894 unsigned char *pointer;
c906108c
SS
14895
14896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14897 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14898 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14899 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14900 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14901 }
14902
9c6a1327
TT
14903 if (!fip->variant_parts.empty ())
14904 add_variant_property (fip, type, cu);
2ddeaf8a 14905
be2daae6
TT
14906 /* Copy the saved-up fields into the field vector. */
14907 for (int i = 0; i < nfields; ++i)
c906108c 14908 {
be2daae6
TT
14909 struct nextfield &field
14910 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14911 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14912
ceacbf6e 14913 type->field (i) = field.field;
be2daae6 14914 switch (field.accessibility)
c906108c 14915 {
c5aa993b 14916 case DW_ACCESS_private:
b4ba55a1 14917 if (cu->language != language_ada)
be2daae6 14918 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14919 break;
c906108c 14920
c5aa993b 14921 case DW_ACCESS_protected:
b4ba55a1 14922 if (cu->language != language_ada)
be2daae6 14923 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14924 break;
c906108c 14925
c5aa993b
JM
14926 case DW_ACCESS_public:
14927 break;
c906108c 14928
c5aa993b
JM
14929 default:
14930 /* Unknown accessibility. Complain and treat it as public. */
14931 {
b98664d3 14932 complaint (_("unsupported accessibility %d"),
be2daae6 14933 field.accessibility);
c5aa993b
JM
14934 }
14935 break;
c906108c 14936 }
be2daae6 14937 if (i < fip->baseclasses.size ())
c906108c 14938 {
be2daae6 14939 switch (field.virtuality)
c906108c 14940 {
c5aa993b
JM
14941 case DW_VIRTUALITY_virtual:
14942 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14943 if (cu->language == language_ada)
a73c6dcd 14944 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14945 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14946 break;
c906108c
SS
14947 }
14948 }
c906108c
SS
14949 }
14950}
14951
7d27a96d
TT
14952/* Return true if this member function is a constructor, false
14953 otherwise. */
14954
14955static int
14956dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14957{
14958 const char *fieldname;
fe978cb0 14959 const char *type_name;
7d27a96d
TT
14960 int len;
14961
14962 if (die->parent == NULL)
14963 return 0;
14964
14965 if (die->parent->tag != DW_TAG_structure_type
14966 && die->parent->tag != DW_TAG_union_type
14967 && die->parent->tag != DW_TAG_class_type)
14968 return 0;
14969
14970 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14971 type_name = dwarf2_name (die->parent, cu);
14972 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14973 return 0;
14974
14975 len = strlen (fieldname);
fe978cb0
PA
14976 return (strncmp (fieldname, type_name, len) == 0
14977 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14978}
14979
e35000a7
TBA
14980/* Check if the given VALUE is a recognized enum
14981 dwarf_defaulted_attribute constant according to DWARF5 spec,
14982 Table 7.24. */
14983
14984static bool
14985is_valid_DW_AT_defaulted (ULONGEST value)
14986{
14987 switch (value)
14988 {
14989 case DW_DEFAULTED_no:
14990 case DW_DEFAULTED_in_class:
14991 case DW_DEFAULTED_out_of_class:
14992 return true;
14993 }
14994
3142e908 14995 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14996 return false;
14997}
14998
c906108c
SS
14999/* Add a member function to the proper fieldlist. */
15000
15001static void
107d2387 15002dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15003 struct type *type, struct dwarf2_cu *cu)
c906108c 15004{
5e22e966 15005 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15006 struct attribute *attr;
c906108c 15007 int i;
be2daae6 15008 struct fnfieldlist *flp = nullptr;
c906108c 15009 struct fn_field *fnp;
15d034d0 15010 const char *fieldname;
f792889a 15011 struct type *this_type;
60d5a603 15012 enum dwarf_access_attribute accessibility;
c906108c 15013
b4ba55a1 15014 if (cu->language == language_ada)
a73c6dcd 15015 error (_("unexpected member function in Ada type"));
b4ba55a1 15016
2df3850c 15017 /* Get name of member function. */
39cbfefa
DJ
15018 fieldname = dwarf2_name (die, cu);
15019 if (fieldname == NULL)
2df3850c 15020 return;
c906108c 15021
c906108c 15022 /* Look up member function name in fieldlist. */
be2daae6 15023 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15024 {
27bfe10e 15025 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15026 {
15027 flp = &fip->fnfieldlists[i];
15028 break;
15029 }
c906108c
SS
15030 }
15031
be2daae6
TT
15032 /* Create a new fnfieldlist if necessary. */
15033 if (flp == nullptr)
c906108c 15034 {
be2daae6
TT
15035 fip->fnfieldlists.emplace_back ();
15036 flp = &fip->fnfieldlists.back ();
c906108c 15037 flp->name = fieldname;
be2daae6 15038 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15039 }
15040
be2daae6
TT
15041 /* Create a new member function field and add it to the vector of
15042 fnfieldlists. */
15043 flp->fnfields.emplace_back ();
15044 fnp = &flp->fnfields.back ();
3da10d80
KS
15045
15046 /* Delay processing of the physname until later. */
9c37b5ae 15047 if (cu->language == language_cplus)
be2daae6
TT
15048 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15049 die, cu);
3da10d80
KS
15050 else
15051 {
1d06ead6 15052 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15053 fnp->physname = physname ? physname : "";
15054 }
15055
c906108c 15056 fnp->type = alloc_type (objfile);
f792889a 15057 this_type = read_type_die (die, cu);
78134374 15058 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15059 {
1f704f76 15060 int nparams = this_type->num_fields ();
c906108c 15061
f792889a 15062 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15063 of the method itself (TYPE_CODE_METHOD). */
15064 smash_to_method_type (fnp->type, type,
f792889a 15065 TYPE_TARGET_TYPE (this_type),
80fc5e77 15066 this_type->fields (),
1f704f76 15067 this_type->num_fields (),
f792889a 15068 TYPE_VARARGS (this_type));
c906108c
SS
15069
15070 /* Handle static member functions.
c5aa993b 15071 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15072 member functions. G++ helps GDB by marking the first
15073 parameter for non-static member functions (which is the this
15074 pointer) as artificial. We obtain this information from
15075 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15076 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15077 fnp->voffset = VOFFSET_STATIC;
15078 }
15079 else
b98664d3 15080 complaint (_("member function type missing for '%s'"),
3da10d80 15081 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15082
15083 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15084 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15085 fnp->fcontext = die_containing_type (die, cu);
c906108c 15086
3e43a32a
MS
15087 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15088 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15089
15090 /* Get accessibility. */
e142c38c 15091 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15092 if (attr != nullptr)
aead7601 15093 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15094 else
15095 accessibility = dwarf2_default_access_attribute (die, cu);
15096 switch (accessibility)
c906108c 15097 {
60d5a603
JK
15098 case DW_ACCESS_private:
15099 fnp->is_private = 1;
15100 break;
15101 case DW_ACCESS_protected:
15102 fnp->is_protected = 1;
15103 break;
c906108c
SS
15104 }
15105
b02dede2 15106 /* Check for artificial methods. */
e142c38c 15107 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15108 if (attr && DW_UNSND (attr) != 0)
15109 fnp->is_artificial = 1;
15110
e35000a7
TBA
15111 /* Check for defaulted methods. */
15112 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15113 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15114 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15115
15116 /* Check for deleted methods. */
15117 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15118 if (attr != nullptr && DW_UNSND (attr) != 0)
15119 fnp->is_deleted = 1;
15120
7d27a96d
TT
15121 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15122
0d564a31 15123 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15124 function. For older versions of GCC, this is an offset in the
15125 appropriate virtual table, as specified by DW_AT_containing_type.
15126 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15127 to the object address. */
15128
e142c38c 15129 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15130 if (attr != nullptr)
8e19ed76 15131 {
4fc6c0d5 15132 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15133 {
aec5aa8b
TT
15134 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15135 {
15136 /* Old-style GCC. */
15137 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15138 }
15139 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15140 || (DW_BLOCK (attr)->size > 1
15141 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15142 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15143 {
aec5aa8b
TT
15144 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15145 if ((fnp->voffset % cu->header.addr_size) != 0)
15146 dwarf2_complex_location_expr_complaint ();
15147 else
15148 fnp->voffset /= cu->header.addr_size;
15149 fnp->voffset += 2;
15150 }
15151 else
15152 dwarf2_complex_location_expr_complaint ();
15153
15154 if (!fnp->fcontext)
7e993ebf
KS
15155 {
15156 /* If there is no `this' field and no DW_AT_containing_type,
15157 we cannot actually find a base class context for the
15158 vtable! */
1f704f76 15159 if (this_type->num_fields () == 0
7e993ebf
KS
15160 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15161 {
b98664d3 15162 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15163 "function \"%s\" (offset %s)"),
15164 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15165 }
15166 else
15167 {
15168 fnp->fcontext
15169 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15170 }
15171 }
aec5aa8b 15172 }
cd6c91b4 15173 else if (attr->form_is_section_offset ())
8e19ed76 15174 {
4d3c2250 15175 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15176 }
15177 else
15178 {
4d3c2250
KB
15179 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15180 fieldname);
8e19ed76 15181 }
0d564a31 15182 }
d48cc9dd
DJ
15183 else
15184 {
15185 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15186 if (attr && DW_UNSND (attr))
15187 {
15188 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15189 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15190 "but the vtable offset is not specified"),
9d8780f0 15191 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15192 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15193 TYPE_CPLUS_DYNAMIC (type) = 1;
15194 }
15195 }
c906108c
SS
15196}
15197
15198/* Create the vector of member function fields, and attach it to the type. */
15199
15200static void
fba45db2 15201dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15202 struct dwarf2_cu *cu)
c906108c 15203{
b4ba55a1 15204 if (cu->language == language_ada)
a73c6dcd 15205 error (_("unexpected member functions in Ada type"));
b4ba55a1 15206
c906108c
SS
15207 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15208 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15209 TYPE_ALLOC (type,
15210 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15211
be2daae6 15212 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15213 {
be2daae6 15214 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15215 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15216
be2daae6
TT
15217 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15218 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15219 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15220 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15221
15222 for (int k = 0; k < nf.fnfields.size (); ++k)
15223 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15224 }
15225
be2daae6 15226 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15227}
15228
1168df01
JB
15229/* Returns non-zero if NAME is the name of a vtable member in CU's
15230 language, zero otherwise. */
15231static int
15232is_vtable_name (const char *name, struct dwarf2_cu *cu)
15233{
15234 static const char vptr[] = "_vptr";
15235
9c37b5ae
TT
15236 /* Look for the C++ form of the vtable. */
15237 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15238 return 1;
15239
15240 return 0;
15241}
15242
c0dd20ea 15243/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15244 functions, with the ABI-specified layout. If TYPE describes
15245 such a structure, smash it into a member function type.
61049d3b
DJ
15246
15247 GCC shouldn't do this; it should just output pointer to member DIEs.
15248 This is GCC PR debug/28767. */
c0dd20ea 15249
0b92b5bb
TT
15250static void
15251quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15252{
09e2d7c7 15253 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15254
15255 /* Check for a structure with no name and two children. */
1f704f76 15256 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15257 return;
c0dd20ea
DJ
15258
15259 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15260 if (TYPE_FIELD_NAME (type, 0) == NULL
15261 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15262 || TYPE_FIELD_NAME (type, 1) == NULL
15263 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15264 return;
c0dd20ea
DJ
15265
15266 /* Find the type of the method. */
0b92b5bb 15267 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15268 if (pfn_type == NULL
78134374
SM
15269 || pfn_type->code () != TYPE_CODE_PTR
15270 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15271 return;
c0dd20ea
DJ
15272
15273 /* Look for the "this" argument. */
15274 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15275 if (pfn_type->num_fields () == 0
0b92b5bb 15276 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15277 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15278 return;
c0dd20ea 15279
09e2d7c7 15280 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15281 new_type = alloc_type (objfile);
09e2d7c7 15282 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15283 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15284 TYPE_VARARGS (pfn_type));
0b92b5bb 15285 smash_to_methodptr_type (type, new_type);
c0dd20ea 15286}
1168df01 15287
2b4424c3
TT
15288/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15289 appropriate error checking and issuing complaints if there is a
15290 problem. */
15291
15292static ULONGEST
15293get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15294{
15295 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15296
15297 if (attr == nullptr)
15298 return 0;
15299
cd6c91b4 15300 if (!attr->form_is_constant ())
2b4424c3 15301 {
b98664d3 15302 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15303 " - DIE at %s [in module %s]"),
15304 sect_offset_str (die->sect_off),
5e22e966 15305 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15306 return 0;
15307 }
15308
15309 ULONGEST align;
15310 if (attr->form == DW_FORM_sdata)
15311 {
15312 LONGEST val = DW_SND (attr);
15313 if (val < 0)
15314 {
b98664d3 15315 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15316 " - DIE at %s [in module %s]"),
15317 sect_offset_str (die->sect_off),
5e22e966 15318 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15319 return 0;
15320 }
15321 align = val;
15322 }
15323 else
15324 align = DW_UNSND (attr);
15325
15326 if (align == 0)
15327 {
b98664d3 15328 complaint (_("DW_AT_alignment value must not be zero"
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 if ((align & (align - 1)) != 0)
15335 {
b98664d3 15336 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15337 " - DIE at %s [in module %s]"),
15338 sect_offset_str (die->sect_off),
5e22e966 15339 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15340 return 0;
15341 }
15342
15343 return align;
15344}
15345
15346/* If the DIE has a DW_AT_alignment attribute, use its value to set
15347 the alignment for TYPE. */
15348
15349static void
15350maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15351 struct type *type)
15352{
15353 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15354 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15355 " - DIE at %s [in module %s]"),
15356 sect_offset_str (die->sect_off),
5e22e966 15357 objfile_name (cu->per_objfile->objfile));
2b4424c3 15358}
685b1105 15359
e35000a7
TBA
15360/* Check if the given VALUE is a valid enum dwarf_calling_convention
15361 constant for a type, according to DWARF5 spec, Table 5.5. */
15362
15363static bool
15364is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15365{
15366 switch (value)
15367 {
15368 case DW_CC_normal:
15369 case DW_CC_pass_by_reference:
15370 case DW_CC_pass_by_value:
15371 return true;
15372
15373 default:
15374 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15375 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15376 return false;
15377 }
15378}
15379
d0922fcf
TBA
15380/* Check if the given VALUE is a valid enum dwarf_calling_convention
15381 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15382 also according to GNU-specific values (see include/dwarf2.h). */
15383
15384static bool
15385is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15386{
15387 switch (value)
15388 {
15389 case DW_CC_normal:
15390 case DW_CC_program:
15391 case DW_CC_nocall:
15392 return true;
15393
15394 case DW_CC_GNU_renesas_sh:
15395 case DW_CC_GNU_borland_fastcall_i386:
15396 case DW_CC_GDB_IBM_OpenCL:
15397 return true;
15398
15399 default:
15400 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15401 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15402 return false;
15403 }
15404}
15405
c906108c 15406/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15407 (definition) to create a type for the structure or union. Fill in
15408 the type's name and general properties; the members will not be
83655187
DE
15409 processed until process_structure_scope. A symbol table entry for
15410 the type will also not be done until process_structure_scope (assuming
15411 the type has a name).
c906108c 15412
c767944b
DJ
15413 NOTE: we need to call these functions regardless of whether or not the
15414 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15415 structure or union. This gets the type entered into our set of
83655187 15416 user defined types. */
c906108c 15417
f792889a 15418static struct type *
134d01f1 15419read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15420{
5e22e966 15421 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15422 struct type *type;
15423 struct attribute *attr;
15d034d0 15424 const char *name;
c906108c 15425
348e048f
DE
15426 /* If the definition of this type lives in .debug_types, read that type.
15427 Don't follow DW_AT_specification though, that will take us back up
15428 the chain and we want to go down. */
052c8bb8 15429 attr = die->attr (DW_AT_signature);
435d3d88 15430 if (attr != nullptr)
348e048f 15431 {
ac9ec31b 15432 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15433
ac9ec31b 15434 /* The type's CU may not be the same as CU.
02142a6c 15435 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15436 return set_die_type (die, type, cu);
15437 }
15438
c0dd20ea 15439 type = alloc_type (objfile);
c906108c 15440 INIT_CPLUS_SPECIFIC (type);
93311388 15441
39cbfefa
DJ
15442 name = dwarf2_name (die, cu);
15443 if (name != NULL)
c906108c 15444 {
987504bb 15445 if (cu->language == language_cplus
c44af4eb
TT
15446 || cu->language == language_d
15447 || cu->language == language_rust)
63d06c5c 15448 {
15d034d0 15449 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15450
15451 /* dwarf2_full_name might have already finished building the DIE's
15452 type. If so, there is no need to continue. */
15453 if (get_die_type (die, cu) != NULL)
15454 return get_die_type (die, cu);
15455
d0e39ea2 15456 type->set_name (full_name);
63d06c5c
DC
15457 }
15458 else
15459 {
d8151005
DJ
15460 /* The name is already allocated along with this objfile, so
15461 we don't need to duplicate it for the type. */
d0e39ea2 15462 type->set_name (name);
63d06c5c 15463 }
c906108c
SS
15464 }
15465
15466 if (die->tag == DW_TAG_structure_type)
15467 {
67607e24 15468 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15469 }
15470 else if (die->tag == DW_TAG_union_type)
15471 {
67607e24 15472 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15473 }
15474 else
15475 {
67607e24 15476 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15477 }
15478
0cc2414c
TT
15479 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15480 TYPE_DECLARED_CLASS (type) = 1;
15481
e35000a7
TBA
15482 /* Store the calling convention in the type if it's available in
15483 the die. Otherwise the calling convention remains set to
15484 the default value DW_CC_normal. */
15485 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15486 if (attr != nullptr
15487 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15488 {
15489 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15490 TYPE_CPLUS_CALLING_CONVENTION (type)
15491 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15492 }
15493
e142c38c 15494 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15495 if (attr != nullptr)
c906108c 15496 {
cd6c91b4 15497 if (attr->form_is_constant ())
155bfbd3
JB
15498 TYPE_LENGTH (type) = DW_UNSND (attr);
15499 else
15500 {
f8e89861
TT
15501 struct dynamic_prop prop;
15502 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15503 cu->per_cu->addr_type ()))
5c54719c 15504 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15505 TYPE_LENGTH (type) = 0;
15506 }
c906108c
SS
15507 }
15508 else
15509 {
15510 TYPE_LENGTH (type) = 0;
15511 }
15512
2b4424c3
TT
15513 maybe_set_alignment (cu, die, type);
15514
5230b05a 15515 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15516 {
5230b05a
WT
15517 /* ICC<14 does not output the required DW_AT_declaration on
15518 incomplete types, but gives them a size of zero. */
422b1cb0 15519 TYPE_STUB (type) = 1;
685b1105
JK
15520 }
15521 else
15522 TYPE_STUB_SUPPORTED (type) = 1;
15523
dc718098 15524 if (die_is_declaration (die, cu))
876cecd0 15525 TYPE_STUB (type) = 1;
a6c727b2
DJ
15526 else if (attr == NULL && die->child == NULL
15527 && producer_is_realview (cu->producer))
15528 /* RealView does not output the required DW_AT_declaration
15529 on incomplete types. */
15530 TYPE_STUB (type) = 1;
dc718098 15531
c906108c
SS
15532 /* We need to add the type field to the die immediately so we don't
15533 infinitely recurse when dealing with pointers to the structure
0963b4bd 15534 type within the structure itself. */
1c379e20 15535 set_die_type (die, type, cu);
c906108c 15536
7e314c57
JK
15537 /* set_die_type should be already done. */
15538 set_descriptive_type (type, die, cu);
15539
c767944b
DJ
15540 return type;
15541}
15542
9c6a1327
TT
15543static void handle_struct_member_die
15544 (struct die_info *child_die,
15545 struct type *type,
15546 struct field_info *fi,
15547 std::vector<struct symbol *> *template_args,
15548 struct dwarf2_cu *cu);
15549
15550/* A helper for handle_struct_member_die that handles
15551 DW_TAG_variant_part. */
15552
15553static void
15554handle_variant_part (struct die_info *die, struct type *type,
15555 struct field_info *fi,
15556 std::vector<struct symbol *> *template_args,
15557 struct dwarf2_cu *cu)
15558{
15559 variant_part_builder *new_part;
15560 if (fi->current_variant_part == nullptr)
15561 {
15562 fi->variant_parts.emplace_back ();
15563 new_part = &fi->variant_parts.back ();
15564 }
15565 else if (!fi->current_variant_part->processing_variant)
15566 {
15567 complaint (_("nested DW_TAG_variant_part seen "
15568 "- DIE at %s [in module %s]"),
15569 sect_offset_str (die->sect_off),
5e22e966 15570 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15571 return;
15572 }
15573 else
15574 {
15575 variant_field &current = fi->current_variant_part->variants.back ();
15576 current.variant_parts.emplace_back ();
15577 new_part = &current.variant_parts.back ();
15578 }
15579
15580 /* When we recurse, we want callees to add to this new variant
15581 part. */
15582 scoped_restore save_current_variant_part
15583 = make_scoped_restore (&fi->current_variant_part, new_part);
15584
15585 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15586 if (discr == NULL)
15587 {
15588 /* It's a univariant form, an extension we support. */
15589 }
15590 else if (discr->form_is_ref ())
15591 {
15592 struct dwarf2_cu *target_cu = cu;
15593 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15594
15595 new_part->discriminant_offset = target_die->sect_off;
15596 }
15597 else
15598 {
15599 complaint (_("DW_AT_discr does not have DIE reference form"
15600 " - DIE at %s [in module %s]"),
15601 sect_offset_str (die->sect_off),
5e22e966 15602 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15603 }
15604
15605 for (die_info *child_die = die->child;
15606 child_die != NULL;
15607 child_die = child_die->sibling)
15608 handle_struct_member_die (child_die, type, fi, template_args, cu);
15609}
15610
15611/* A helper for handle_struct_member_die that handles
15612 DW_TAG_variant. */
15613
15614static void
15615handle_variant (struct die_info *die, struct type *type,
15616 struct field_info *fi,
15617 std::vector<struct symbol *> *template_args,
15618 struct dwarf2_cu *cu)
15619{
15620 if (fi->current_variant_part == nullptr)
15621 {
15622 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
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 if (fi->current_variant_part->processing_variant)
15629 {
15630 complaint (_("nested DW_TAG_variant seen "
15631 "- DIE at %s [in module %s]"),
15632 sect_offset_str (die->sect_off),
5e22e966 15633 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15634 return;
15635 }
15636
15637 scoped_restore save_processing_variant
15638 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15639 true);
15640
15641 fi->current_variant_part->variants.emplace_back ();
15642 variant_field &variant = fi->current_variant_part->variants.back ();
15643 variant.first_field = fi->fields.size ();
15644
15645 /* In a variant we want to get the discriminant and also add a
15646 field for our sole member child. */
15647 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15648 if (discr == nullptr)
15649 {
15650 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15651 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15652 variant.default_branch = true;
15653 else
15654 variant.discr_list_data = DW_BLOCK (discr);
15655 }
15656 else
15657 variant.discriminant_value = DW_UNSND (discr);
15658
15659 for (die_info *variant_child = die->child;
15660 variant_child != NULL;
15661 variant_child = variant_child->sibling)
15662 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15663
15664 variant.last_field = fi->fields.size ();
15665}
15666
2ddeaf8a
TT
15667/* A helper for process_structure_scope that handles a single member
15668 DIE. */
15669
15670static void
15671handle_struct_member_die (struct die_info *child_die, struct type *type,
15672 struct field_info *fi,
15673 std::vector<struct symbol *> *template_args,
15674 struct dwarf2_cu *cu)
15675{
15676 if (child_die->tag == DW_TAG_member
9c6a1327 15677 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15678 {
15679 /* NOTE: carlton/2002-11-05: A C++ static data member
15680 should be a DW_TAG_member that is a declaration, but
15681 all versions of G++ as of this writing (so through at
15682 least 3.2.1) incorrectly generate DW_TAG_variable
15683 tags for them instead. */
15684 dwarf2_add_field (fi, child_die, cu);
15685 }
15686 else if (child_die->tag == DW_TAG_subprogram)
15687 {
15688 /* Rust doesn't have member functions in the C++ sense.
15689 However, it does emit ordinary functions as children
15690 of a struct DIE. */
15691 if (cu->language == language_rust)
15692 read_func_scope (child_die, cu);
15693 else
15694 {
15695 /* C++ member function. */
15696 dwarf2_add_member_fn (fi, child_die, type, cu);
15697 }
15698 }
15699 else if (child_die->tag == DW_TAG_inheritance)
15700 {
15701 /* C++ base class field. */
15702 dwarf2_add_field (fi, child_die, cu);
15703 }
15704 else if (type_can_define_types (child_die))
15705 dwarf2_add_type_defn (fi, child_die, cu);
15706 else if (child_die->tag == DW_TAG_template_type_param
15707 || child_die->tag == DW_TAG_template_value_param)
15708 {
15709 struct symbol *arg = new_symbol (child_die, NULL, cu);
15710
15711 if (arg != NULL)
15712 template_args->push_back (arg);
15713 }
9c6a1327
TT
15714 else if (child_die->tag == DW_TAG_variant_part)
15715 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15716 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15717 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15718}
15719
c767944b
DJ
15720/* Finish creating a structure or union type, including filling in
15721 its members and creating a symbol for it. */
15722
15723static void
15724process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15725{
5e22e966 15726 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15727 struct die_info *child_die;
c767944b
DJ
15728 struct type *type;
15729
15730 type = get_die_type (die, cu);
15731 if (type == NULL)
15732 type = read_structure_type (die, cu);
15733
3e1d3d8c 15734 bool has_template_parameters = false;
e142c38c 15735 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15736 {
15737 struct field_info fi;
2f4732b0 15738 std::vector<struct symbol *> template_args;
c906108c 15739
639d11d3 15740 child_die = die->child;
c906108c
SS
15741
15742 while (child_die && child_die->tag)
15743 {
2ddeaf8a 15744 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15745 child_die = child_die->sibling;
c906108c
SS
15746 }
15747
34eaf542 15748 /* Attach template arguments to type. */
2f4732b0 15749 if (!template_args.empty ())
34eaf542 15750 {
3e1d3d8c 15751 has_template_parameters = true;
34eaf542 15752 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15753 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15754 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15755 = XOBNEWVEC (&objfile->objfile_obstack,
15756 struct symbol *,
15757 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15758 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15759 template_args.data (),
34eaf542
TT
15760 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15761 * sizeof (struct symbol *)));
34eaf542
TT
15762 }
15763
c906108c 15764 /* Attach fields and member functions to the type. */
317f7127 15765 if (fi.nfields () > 0)
e7c27a73 15766 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15767 if (!fi.fnfieldlists.empty ())
c906108c 15768 {
e7c27a73 15769 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15770
c5aa993b 15771 /* Get the type which refers to the base class (possibly this
c906108c 15772 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15773 class from the DW_AT_containing_type attribute. This use of
15774 DW_AT_containing_type is a GNU extension. */
c906108c 15775
e142c38c 15776 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15777 {
e7c27a73 15778 struct type *t = die_containing_type (die, cu);
c906108c 15779
ae6ae975 15780 set_type_vptr_basetype (type, t);
c906108c
SS
15781 if (type == t)
15782 {
c906108c
SS
15783 int i;
15784
15785 /* Our own class provides vtbl ptr. */
1f704f76 15786 for (i = t->num_fields () - 1;
c906108c
SS
15787 i >= TYPE_N_BASECLASSES (t);
15788 --i)
15789 {
0d5cff50 15790 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15791
1168df01 15792 if (is_vtable_name (fieldname, cu))
c906108c 15793 {
ae6ae975 15794 set_type_vptr_fieldno (type, i);
c906108c
SS
15795 break;
15796 }
15797 }
15798
15799 /* Complain if virtual function table field not found. */
15800 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15801 complaint (_("virtual function table pointer "
3e43a32a 15802 "not found when defining class '%s'"),
7d93a1e0 15803 type->name () ? type->name () : "");
c906108c
SS
15804 }
15805 else
15806 {
ae6ae975 15807 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15808 }
15809 }
f6235d4c 15810 else if (cu->producer
61012eef 15811 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15812 {
15813 /* The IBM XLC compiler does not provide direct indication
15814 of the containing type, but the vtable pointer is
15815 always named __vfp. */
15816
15817 int i;
15818
1f704f76 15819 for (i = type->num_fields () - 1;
f6235d4c
EZ
15820 i >= TYPE_N_BASECLASSES (type);
15821 --i)
15822 {
15823 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15824 {
ae6ae975
DE
15825 set_type_vptr_fieldno (type, i);
15826 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15827 break;
15828 }
15829 }
15830 }
c906108c 15831 }
98751a41
JK
15832
15833 /* Copy fi.typedef_field_list linked list elements content into the
15834 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15835 if (!fi.typedef_field_list.empty ())
98751a41 15836 {
be2daae6 15837 int count = fi.typedef_field_list.size ();
98751a41 15838
a0d7a4ff 15839 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15840 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15841 = ((struct decl_field *)
be2daae6
TT
15842 TYPE_ALLOC (type,
15843 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15844 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15845
be2daae6
TT
15846 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15847 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15848 }
c767944b 15849
883fd55a
KS
15850 /* Copy fi.nested_types_list linked list elements content into the
15851 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15852 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15853 {
be2daae6 15854 int count = fi.nested_types_list.size ();
883fd55a
KS
15855
15856 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15857 TYPE_NESTED_TYPES_ARRAY (type)
15858 = ((struct decl_field *)
be2daae6
TT
15859 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15860 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15861
be2daae6
TT
15862 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15863 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15864 }
c906108c 15865 }
63d06c5c 15866
bb5ed363 15867 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15868 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15869 cu->rust_unions.push_back (type);
0b92b5bb 15870
90aeadfc
DC
15871 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15872 snapshots) has been known to create a die giving a declaration
15873 for a class that has, as a child, a die giving a definition for a
15874 nested class. So we have to process our children even if the
15875 current die is a declaration. Normally, of course, a declaration
15876 won't have any children at all. */
134d01f1 15877
ca040673
DE
15878 child_die = die->child;
15879
90aeadfc
DC
15880 while (child_die != NULL && child_die->tag)
15881 {
15882 if (child_die->tag == DW_TAG_member
15883 || child_die->tag == DW_TAG_variable
34eaf542
TT
15884 || child_die->tag == DW_TAG_inheritance
15885 || child_die->tag == DW_TAG_template_value_param
15886 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15887 {
90aeadfc 15888 /* Do nothing. */
134d01f1 15889 }
90aeadfc
DC
15890 else
15891 process_die (child_die, cu);
134d01f1 15892
436c571c 15893 child_die = child_die->sibling;
134d01f1
DJ
15894 }
15895
fa4028e9
JB
15896 /* Do not consider external references. According to the DWARF standard,
15897 these DIEs are identified by the fact that they have no byte_size
15898 attribute, and a declaration attribute. */
15899 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15900 || !die_is_declaration (die, cu)
15901 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15902 {
15903 struct symbol *sym = new_symbol (die, type, cu);
15904
15905 if (has_template_parameters)
15906 {
a776957c
TT
15907 struct symtab *symtab;
15908 if (sym != nullptr)
15909 symtab = symbol_symtab (sym);
15910 else if (cu->line_header != nullptr)
15911 {
15912 /* Any related symtab will do. */
15913 symtab
7ba99d21 15914 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15915 }
15916 else
15917 {
15918 symtab = nullptr;
15919 complaint (_("could not find suitable "
15920 "symtab for template parameter"
15921 " - DIE at %s [in module %s]"),
15922 sect_offset_str (die->sect_off),
15923 objfile_name (objfile));
15924 }
15925
15926 if (symtab != nullptr)
15927 {
15928 /* Make sure that the symtab is set on the new symbols.
15929 Even though they don't appear in this symtab directly,
15930 other parts of gdb assume that symbols do, and this is
15931 reasonably true. */
15932 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15933 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15934 }
3e1d3d8c
TT
15935 }
15936 }
134d01f1
DJ
15937}
15938
ed6acedd
TT
15939/* Assuming DIE is an enumeration type, and TYPE is its associated
15940 type, update TYPE using some information only available in DIE's
15941 children. In particular, the fields are computed. */
55426c9d
JB
15942
15943static void
15944update_enumeration_type_from_children (struct die_info *die,
15945 struct type *type,
15946 struct dwarf2_cu *cu)
15947{
60f7655a 15948 struct die_info *child_die;
55426c9d
JB
15949 int unsigned_enum = 1;
15950 int flag_enum = 1;
55426c9d 15951
8268c778 15952 auto_obstack obstack;
ed6acedd 15953 std::vector<struct field> fields;
55426c9d 15954
60f7655a
DE
15955 for (child_die = die->child;
15956 child_die != NULL && child_die->tag;
436c571c 15957 child_die = child_die->sibling)
55426c9d
JB
15958 {
15959 struct attribute *attr;
15960 LONGEST value;
15961 const gdb_byte *bytes;
15962 struct dwarf2_locexpr_baton *baton;
15963 const char *name;
60f7655a 15964
55426c9d
JB
15965 if (child_die->tag != DW_TAG_enumerator)
15966 continue;
15967
15968 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15969 if (attr == NULL)
15970 continue;
15971
15972 name = dwarf2_name (child_die, cu);
15973 if (name == NULL)
15974 name = "<anonymous enumerator>";
15975
15976 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15977 &value, &bytes, &baton);
15978 if (value < 0)
15979 {
15980 unsigned_enum = 0;
15981 flag_enum = 0;
15982 }
55426c9d 15983 else
edd45eb0
SM
15984 {
15985 if (count_one_bits_ll (value) >= 2)
15986 flag_enum = 0;
edd45eb0 15987 }
55426c9d 15988
ed6acedd
TT
15989 fields.emplace_back ();
15990 struct field &field = fields.back ();
15991 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15992 SET_FIELD_ENUMVAL (field, value);
15993 }
15994
15995 if (!fields.empty ())
15996 {
5e33d5f4 15997 type->set_num_fields (fields.size ());
3cabb6b0
SM
15998 type->set_fields
15999 ((struct field *)
16000 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16001 memcpy (type->fields (), fields.data (),
ed6acedd 16002 sizeof (struct field) * fields.size ());
55426c9d
JB
16003 }
16004
16005 if (unsigned_enum)
16006 TYPE_UNSIGNED (type) = 1;
16007 if (flag_enum)
16008 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16009}
16010
134d01f1
DJ
16011/* Given a DW_AT_enumeration_type die, set its type. We do not
16012 complete the type's fields yet, or create any symbols. */
c906108c 16013
f792889a 16014static struct type *
134d01f1 16015read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16016{
5e22e966 16017 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16018 struct type *type;
c906108c 16019 struct attribute *attr;
0114d602 16020 const char *name;
134d01f1 16021
348e048f
DE
16022 /* If the definition of this type lives in .debug_types, read that type.
16023 Don't follow DW_AT_specification though, that will take us back up
16024 the chain and we want to go down. */
052c8bb8 16025 attr = die->attr (DW_AT_signature);
435d3d88 16026 if (attr != nullptr)
348e048f 16027 {
ac9ec31b 16028 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16029
ac9ec31b 16030 /* The type's CU may not be the same as CU.
02142a6c 16031 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16032 return set_die_type (die, type, cu);
16033 }
16034
c906108c
SS
16035 type = alloc_type (objfile);
16036
67607e24 16037 type->set_code (TYPE_CODE_ENUM);
94af9270 16038 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16039 if (name != NULL)
d0e39ea2 16040 type->set_name (name);
c906108c 16041
0626fc76
TT
16042 attr = dwarf2_attr (die, DW_AT_type, cu);
16043 if (attr != NULL)
16044 {
16045 struct type *underlying_type = die_type (die, cu);
16046
16047 TYPE_TARGET_TYPE (type) = underlying_type;
16048 }
16049
e142c38c 16050 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16051 if (attr != nullptr)
c906108c
SS
16052 {
16053 TYPE_LENGTH (type) = DW_UNSND (attr);
16054 }
16055 else
16056 {
16057 TYPE_LENGTH (type) = 0;
16058 }
16059
2b4424c3
TT
16060 maybe_set_alignment (cu, die, type);
16061
137033e9
JB
16062 /* The enumeration DIE can be incomplete. In Ada, any type can be
16063 declared as private in the package spec, and then defined only
16064 inside the package body. Such types are known as Taft Amendment
16065 Types. When another package uses such a type, an incomplete DIE
16066 may be generated by the compiler. */
02eb380e 16067 if (die_is_declaration (die, cu))
876cecd0 16068 TYPE_STUB (type) = 1;
02eb380e 16069
0626fc76
TT
16070 /* If this type has an underlying type that is not a stub, then we
16071 may use its attributes. We always use the "unsigned" attribute
16072 in this situation, because ordinarily we guess whether the type
16073 is unsigned -- but the guess can be wrong and the underlying type
16074 can tell us the reality. However, we defer to a local size
16075 attribute if one exists, because this lets the compiler override
16076 the underlying type if needed. */
16077 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16078 {
9e7c9a03
HD
16079 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16080 underlying_type = check_typedef (underlying_type);
16081 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16082 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16083 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16084 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16085 && TYPE_RAW_ALIGN (underlying_type) != 0)
16086 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16087 }
16088
3d567982
TT
16089 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16090
ed6acedd
TT
16091 set_die_type (die, type, cu);
16092
16093 /* Finish the creation of this type by using the enum's children.
16094 Note that, as usual, this must come after set_die_type to avoid
16095 infinite recursion when trying to compute the names of the
16096 enumerators. */
16097 update_enumeration_type_from_children (die, type, cu);
16098
16099 return type;
134d01f1
DJ
16100}
16101
16102/* Given a pointer to a die which begins an enumeration, process all
16103 the dies that define the members of the enumeration, and create the
16104 symbol for the enumeration type.
16105
16106 NOTE: We reverse the order of the element list. */
16107
16108static void
16109process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16110{
f792889a 16111 struct type *this_type;
134d01f1 16112
f792889a
DJ
16113 this_type = get_die_type (die, cu);
16114 if (this_type == NULL)
16115 this_type = read_enumeration_type (die, cu);
9dc481d3 16116
639d11d3 16117 if (die->child != NULL)
c906108c 16118 {
9dc481d3 16119 struct die_info *child_die;
15d034d0 16120 const char *name;
9dc481d3 16121
639d11d3 16122 child_die = die->child;
c906108c
SS
16123 while (child_die && child_die->tag)
16124 {
16125 if (child_die->tag != DW_TAG_enumerator)
16126 {
e7c27a73 16127 process_die (child_die, cu);
c906108c
SS
16128 }
16129 else
16130 {
39cbfefa
DJ
16131 name = dwarf2_name (child_die, cu);
16132 if (name)
ed6acedd 16133 new_symbol (child_die, this_type, cu);
c906108c
SS
16134 }
16135
436c571c 16136 child_die = child_die->sibling;
c906108c 16137 }
c906108c 16138 }
134d01f1 16139
6c83ed52
TT
16140 /* If we are reading an enum from a .debug_types unit, and the enum
16141 is a declaration, and the enum is not the signatured type in the
16142 unit, then we do not want to add a symbol for it. Adding a
16143 symbol would in some cases obscure the true definition of the
16144 enum, giving users an incomplete type when the definition is
16145 actually available. Note that we do not want to do this for all
16146 enums which are just declarations, because C++0x allows forward
16147 enum declarations. */
3019eac3 16148 if (cu->per_cu->is_debug_types
6c83ed52
TT
16149 && die_is_declaration (die, cu))
16150 {
52dc124a 16151 struct signatured_type *sig_type;
6c83ed52 16152
c0f78cd4 16153 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16154 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16155 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16156 return;
16157 }
16158
f792889a 16159 new_symbol (die, this_type, cu);
c906108c
SS
16160}
16161
16162/* Extract all information from a DW_TAG_array_type DIE and put it in
16163 the DIE's type field. For now, this only handles one dimensional
16164 arrays. */
16165
f792889a 16166static struct type *
e7c27a73 16167read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16168{
5e22e966 16169 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16170 struct die_info *child_die;
7e314c57 16171 struct type *type;
c906108c 16172 struct type *element_type, *range_type, *index_type;
c906108c 16173 struct attribute *attr;
15d034d0 16174 const char *name;
a405673c 16175 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16176 unsigned int bit_stride = 0;
c906108c 16177
e7c27a73 16178 element_type = die_type (die, cu);
c906108c 16179
7e314c57
JK
16180 /* The die_type call above may have already set the type for this DIE. */
16181 type = get_die_type (die, cu);
16182 if (type)
16183 return type;
16184
dc53a7ad
JB
16185 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16186 if (attr != NULL)
a405673c
JB
16187 {
16188 int stride_ok;
09ba997f 16189 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16190
16191 byte_stride_prop
16192 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16193 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16194 prop_type);
a405673c
JB
16195 if (!stride_ok)
16196 {
b98664d3 16197 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16198 " - DIE at %s [in module %s]"),
16199 sect_offset_str (die->sect_off),
5e22e966 16200 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16201 /* Ignore this attribute. We will likely not be able to print
16202 arrays of this type correctly, but there is little we can do
16203 to help if we cannot read the attribute's value. */
16204 byte_stride_prop = NULL;
16205 }
16206 }
dc53a7ad
JB
16207
16208 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16209 if (attr != NULL)
16210 bit_stride = DW_UNSND (attr);
16211
c906108c
SS
16212 /* Irix 6.2 native cc creates array types without children for
16213 arrays with unspecified length. */
639d11d3 16214 if (die->child == NULL)
c906108c 16215 {
46bf5051 16216 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16217 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16218 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16219 byte_stride_prop, bit_stride);
f792889a 16220 return set_die_type (die, type, cu);
c906108c
SS
16221 }
16222
791afaa2 16223 std::vector<struct type *> range_types;
639d11d3 16224 child_die = die->child;
c906108c
SS
16225 while (child_die && child_die->tag)
16226 {
16227 if (child_die->tag == DW_TAG_subrange_type)
16228 {
f792889a 16229 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16230
f792889a 16231 if (child_type != NULL)
a02abb62 16232 {
0963b4bd
MS
16233 /* The range type was succesfully read. Save it for the
16234 array type creation. */
791afaa2 16235 range_types.push_back (child_type);
a02abb62 16236 }
c906108c 16237 }
436c571c 16238 child_die = child_die->sibling;
c906108c
SS
16239 }
16240
16241 /* Dwarf2 dimensions are output from left to right, create the
16242 necessary array types in backwards order. */
7ca2d3a3 16243
c906108c 16244 type = element_type;
7ca2d3a3
DL
16245
16246 if (read_array_order (die, cu) == DW_ORD_col_major)
16247 {
16248 int i = 0;
9a619af0 16249
791afaa2 16250 while (i < range_types.size ())
dc53a7ad 16251 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16252 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16253 }
16254 else
16255 {
791afaa2 16256 size_t ndim = range_types.size ();
7ca2d3a3 16257 while (ndim-- > 0)
dc53a7ad 16258 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16259 byte_stride_prop, bit_stride);
7ca2d3a3 16260 }
c906108c 16261
f5f8a009
EZ
16262 /* Understand Dwarf2 support for vector types (like they occur on
16263 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16264 array type. This is not part of the Dwarf2/3 standard yet, but a
16265 custom vendor extension. The main difference between a regular
16266 array and the vector variant is that vectors are passed by value
16267 to functions. */
e142c38c 16268 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16269 if (attr != nullptr)
ea37ba09 16270 make_vector_type (type);
f5f8a009 16271
dbc98a8b
KW
16272 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16273 implementation may choose to implement triple vectors using this
16274 attribute. */
16275 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16276 if (attr != nullptr)
dbc98a8b
KW
16277 {
16278 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16279 TYPE_LENGTH (type) = DW_UNSND (attr);
16280 else
b98664d3 16281 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16282 "than the total size of elements"));
dbc98a8b
KW
16283 }
16284
39cbfefa
DJ
16285 name = dwarf2_name (die, cu);
16286 if (name)
d0e39ea2 16287 type->set_name (name);
6e70227d 16288
2b4424c3
TT
16289 maybe_set_alignment (cu, die, type);
16290
0963b4bd 16291 /* Install the type in the die. */
7e314c57
JK
16292 set_die_type (die, type, cu);
16293
16294 /* set_die_type should be already done. */
b4ba55a1
JB
16295 set_descriptive_type (type, die, cu);
16296
7e314c57 16297 return type;
c906108c
SS
16298}
16299
7ca2d3a3 16300static enum dwarf_array_dim_ordering
6e70227d 16301read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16302{
16303 struct attribute *attr;
16304
16305 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16306
435d3d88 16307 if (attr != nullptr)
aead7601 16308 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16309
0963b4bd
MS
16310 /* GNU F77 is a special case, as at 08/2004 array type info is the
16311 opposite order to the dwarf2 specification, but data is still
16312 laid out as per normal fortran.
7ca2d3a3 16313
0963b4bd
MS
16314 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16315 version checking. */
7ca2d3a3 16316
905e0470
PM
16317 if (cu->language == language_fortran
16318 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16319 {
16320 return DW_ORD_row_major;
16321 }
16322
6e70227d 16323 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16324 {
16325 case array_column_major:
16326 return DW_ORD_col_major;
16327 case array_row_major:
16328 default:
16329 return DW_ORD_row_major;
16330 };
16331}
16332
72019c9c 16333/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16334 the DIE's type field. */
72019c9c 16335
f792889a 16336static struct type *
72019c9c
GM
16337read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16338{
7e314c57
JK
16339 struct type *domain_type, *set_type;
16340 struct attribute *attr;
f792889a 16341
7e314c57
JK
16342 domain_type = die_type (die, cu);
16343
16344 /* The die_type call above may have already set the type for this DIE. */
16345 set_type = get_die_type (die, cu);
16346 if (set_type)
16347 return set_type;
16348
16349 set_type = create_set_type (NULL, domain_type);
16350
16351 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16352 if (attr != nullptr)
d09039dd 16353 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16354
2b4424c3
TT
16355 maybe_set_alignment (cu, die, set_type);
16356
f792889a 16357 return set_die_type (die, set_type, cu);
72019c9c 16358}
7ca2d3a3 16359
0971de02
TT
16360/* A helper for read_common_block that creates a locexpr baton.
16361 SYM is the symbol which we are marking as computed.
16362 COMMON_DIE is the DIE for the common block.
16363 COMMON_LOC is the location expression attribute for the common
16364 block itself.
16365 MEMBER_LOC is the location expression attribute for the particular
16366 member of the common block that we are processing.
16367 CU is the CU from which the above come. */
16368
16369static void
16370mark_common_block_symbol_computed (struct symbol *sym,
16371 struct die_info *common_die,
16372 struct attribute *common_loc,
16373 struct attribute *member_loc,
16374 struct dwarf2_cu *cu)
16375{
5e22e966 16376 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16377 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16378 struct dwarf2_locexpr_baton *baton;
16379 gdb_byte *ptr;
16380 unsigned int cu_off;
08feed99 16381 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16382 LONGEST offset = 0;
16383
16384 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16385 gdb_assert (common_loc->form_is_block ());
16386 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16387 || member_loc->form_is_constant ());
0971de02 16388
8d749320 16389 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16390 baton->per_objfile = per_objfile;
0971de02
TT
16391 baton->per_cu = cu->per_cu;
16392 gdb_assert (baton->per_cu);
16393
16394 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16395
cd6c91b4 16396 if (member_loc->form_is_constant ())
0971de02 16397 {
0826b30a 16398 offset = member_loc->constant_value (0);
0971de02
TT
16399 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16400 }
16401 else
16402 baton->size += DW_BLOCK (member_loc)->size;
16403
224c3ddb 16404 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16405 baton->data = ptr;
16406
16407 *ptr++ = DW_OP_call4;
9c541725 16408 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16409 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16410 ptr += 4;
16411
cd6c91b4 16412 if (member_loc->form_is_constant ())
0971de02
TT
16413 {
16414 *ptr++ = DW_OP_addr;
16415 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16416 ptr += cu->header.addr_size;
16417 }
16418 else
16419 {
16420 /* We have to copy the data here, because DW_OP_call4 will only
16421 use a DW_AT_location attribute. */
16422 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16423 ptr += DW_BLOCK (member_loc)->size;
16424 }
16425
16426 *ptr++ = DW_OP_plus;
16427 gdb_assert (ptr - baton->data == baton->size);
16428
0971de02 16429 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16430 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16431}
16432
4357ac6c
TT
16433/* Create appropriate locally-scoped variables for all the
16434 DW_TAG_common_block entries. Also create a struct common_block
16435 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16436 is used to separate the common blocks name namespace from regular
4357ac6c 16437 variable names. */
c906108c
SS
16438
16439static void
e7c27a73 16440read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16441{
0971de02
TT
16442 struct attribute *attr;
16443
16444 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16445 if (attr != nullptr)
0971de02
TT
16446 {
16447 /* Support the .debug_loc offsets. */
4fc6c0d5 16448 if (attr->form_is_block ())
0971de02
TT
16449 {
16450 /* Ok. */
16451 }
cd6c91b4 16452 else if (attr->form_is_section_offset ())
0971de02
TT
16453 {
16454 dwarf2_complex_location_expr_complaint ();
16455 attr = NULL;
16456 }
16457 else
16458 {
16459 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16460 "common block member");
16461 attr = NULL;
16462 }
16463 }
16464
639d11d3 16465 if (die->child != NULL)
c906108c 16466 {
5e22e966 16467 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16468 struct die_info *child_die;
16469 size_t n_entries = 0, size;
16470 struct common_block *common_block;
16471 struct symbol *sym;
74ac6d43 16472
4357ac6c
TT
16473 for (child_die = die->child;
16474 child_die && child_die->tag;
436c571c 16475 child_die = child_die->sibling)
4357ac6c
TT
16476 ++n_entries;
16477
16478 size = (sizeof (struct common_block)
16479 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16480 common_block
16481 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16482 size);
4357ac6c
TT
16483 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16484 common_block->n_entries = 0;
16485
16486 for (child_die = die->child;
16487 child_die && child_die->tag;
436c571c 16488 child_die = child_die->sibling)
4357ac6c
TT
16489 {
16490 /* Create the symbol in the DW_TAG_common_block block in the current
16491 symbol scope. */
e7c27a73 16492 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16493 if (sym != NULL)
16494 {
16495 struct attribute *member_loc;
16496
16497 common_block->contents[common_block->n_entries++] = sym;
16498
16499 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16500 cu);
16501 if (member_loc)
16502 {
16503 /* GDB has handled this for a long time, but it is
16504 not specified by DWARF. It seems to have been
16505 emitted by gfortran at least as recently as:
16506 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16507 complaint (_("Variable in common block has "
0971de02 16508 "DW_AT_data_member_location "
9d8780f0
SM
16509 "- DIE at %s [in module %s]"),
16510 sect_offset_str (child_die->sect_off),
518817b3 16511 objfile_name (objfile));
0971de02 16512
cd6c91b4 16513 if (member_loc->form_is_section_offset ())
0971de02 16514 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16515 else if (member_loc->form_is_constant ()
4fc6c0d5 16516 || member_loc->form_is_block ())
0971de02 16517 {
435d3d88 16518 if (attr != nullptr)
0971de02
TT
16519 mark_common_block_symbol_computed (sym, die, attr,
16520 member_loc, cu);
16521 }
16522 else
16523 dwarf2_complex_location_expr_complaint ();
16524 }
16525 }
c906108c 16526 }
4357ac6c
TT
16527
16528 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16529 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16530 }
16531}
16532
0114d602 16533/* Create a type for a C++ namespace. */
d9fa45fe 16534
0114d602
DJ
16535static struct type *
16536read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16537{
5e22e966 16538 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16539 const char *previous_prefix, *name;
9219021c 16540 int is_anonymous;
0114d602
DJ
16541 struct type *type;
16542
16543 /* For extensions, reuse the type of the original namespace. */
16544 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16545 {
16546 struct die_info *ext_die;
16547 struct dwarf2_cu *ext_cu = cu;
9a619af0 16548
0114d602
DJ
16549 ext_die = dwarf2_extension (die, &ext_cu);
16550 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16551
16552 /* EXT_CU may not be the same as CU.
02142a6c 16553 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16554 return set_die_type (die, type, cu);
16555 }
9219021c 16556
e142c38c 16557 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16558
16559 /* Now build the name of the current namespace. */
16560
0114d602
DJ
16561 previous_prefix = determine_prefix (die, cu);
16562 if (previous_prefix[0] != '\0')
16563 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16564 previous_prefix, name, 0, cu);
0114d602
DJ
16565
16566 /* Create the type. */
19f392bc 16567 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16568
60531b24 16569 return set_die_type (die, type, cu);
0114d602
DJ
16570}
16571
22cee43f 16572/* Read a namespace scope. */
0114d602
DJ
16573
16574static void
16575read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16576{
5e22e966 16577 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16578 int is_anonymous;
9219021c 16579
5c4e30ca
DC
16580 /* Add a symbol associated to this if we haven't seen the namespace
16581 before. Also, add a using directive if it's an anonymous
16582 namespace. */
9219021c 16583
f2f0e013 16584 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16585 {
16586 struct type *type;
16587
0114d602 16588 type = read_type_die (die, cu);
e7c27a73 16589 new_symbol (die, type, cu);
5c4e30ca 16590
e8e80198 16591 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16592 if (is_anonymous)
0114d602
DJ
16593 {
16594 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16595
eb1e02fd 16596 std::vector<const char *> excludes;
804d2729 16597 add_using_directive (using_directives (cu),
7d93a1e0 16598 previous_prefix, type->name (), NULL,
eb1e02fd 16599 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16600 }
5c4e30ca 16601 }
9219021c 16602
639d11d3 16603 if (die->child != NULL)
d9fa45fe 16604 {
639d11d3 16605 struct die_info *child_die = die->child;
6e70227d 16606
d9fa45fe
DC
16607 while (child_die && child_die->tag)
16608 {
e7c27a73 16609 process_die (child_die, cu);
436c571c 16610 child_die = child_die->sibling;
d9fa45fe
DC
16611 }
16612 }
38d518c9
EZ
16613}
16614
f55ee35c
JK
16615/* Read a Fortran module as type. This DIE can be only a declaration used for
16616 imported module. Still we need that type as local Fortran "use ... only"
16617 declaration imports depend on the created type in determine_prefix. */
16618
16619static struct type *
16620read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16621{
5e22e966 16622 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16623 const char *module_name;
f55ee35c
JK
16624 struct type *type;
16625
16626 module_name = dwarf2_name (die, cu);
19f392bc 16627 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16628
f55ee35c
JK
16629 return set_die_type (die, type, cu);
16630}
16631
5d7cb8df
JK
16632/* Read a Fortran module. */
16633
16634static void
16635read_module (struct die_info *die, struct dwarf2_cu *cu)
16636{
16637 struct die_info *child_die = die->child;
530e8392
KB
16638 struct type *type;
16639
16640 type = read_type_die (die, cu);
16641 new_symbol (die, type, cu);
5d7cb8df 16642
5d7cb8df
JK
16643 while (child_die && child_die->tag)
16644 {
16645 process_die (child_die, cu);
436c571c 16646 child_die = child_die->sibling;
5d7cb8df
JK
16647 }
16648}
16649
38d518c9
EZ
16650/* Return the name of the namespace represented by DIE. Set
16651 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16652 namespace. */
16653
16654static const char *
e142c38c 16655namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16656{
16657 struct die_info *current_die;
16658 const char *name = NULL;
16659
16660 /* Loop through the extensions until we find a name. */
16661
16662 for (current_die = die;
16663 current_die != NULL;
f2f0e013 16664 current_die = dwarf2_extension (die, &cu))
38d518c9 16665 {
96553a0c
DE
16666 /* We don't use dwarf2_name here so that we can detect the absence
16667 of a name -> anonymous namespace. */
7d45c7c3 16668 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16669
38d518c9
EZ
16670 if (name != NULL)
16671 break;
16672 }
16673
16674 /* Is it an anonymous namespace? */
16675
16676 *is_anonymous = (name == NULL);
16677 if (*is_anonymous)
2b1dbab0 16678 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16679
16680 return name;
d9fa45fe
DC
16681}
16682
c906108c
SS
16683/* Extract all information from a DW_TAG_pointer_type DIE and add to
16684 the user defined type vector. */
16685
f792889a 16686static struct type *
e7c27a73 16687read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16688{
5e22e966 16689 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16690 struct comp_unit_head *cu_header = &cu->header;
c906108c 16691 struct type *type;
8b2dbe47
KB
16692 struct attribute *attr_byte_size;
16693 struct attribute *attr_address_class;
16694 int byte_size, addr_class;
7e314c57
JK
16695 struct type *target_type;
16696
16697 target_type = die_type (die, cu);
c906108c 16698
7e314c57
JK
16699 /* The die_type call above may have already set the type for this DIE. */
16700 type = get_die_type (die, cu);
16701 if (type)
16702 return type;
16703
16704 type = lookup_pointer_type (target_type);
8b2dbe47 16705
e142c38c 16706 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16707 if (attr_byte_size)
16708 byte_size = DW_UNSND (attr_byte_size);
c906108c 16709 else
8b2dbe47
KB
16710 byte_size = cu_header->addr_size;
16711
e142c38c 16712 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16713 if (attr_address_class)
16714 addr_class = DW_UNSND (attr_address_class);
16715 else
16716 addr_class = DW_ADDR_none;
16717
2b4424c3
TT
16718 ULONGEST alignment = get_alignment (cu, die);
16719
16720 /* If the pointer size, alignment, or address class is different
16721 than the default, create a type variant marked as such and set
16722 the length accordingly. */
16723 if (TYPE_LENGTH (type) != byte_size
16724 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16725 && alignment != TYPE_RAW_ALIGN (type))
16726 || addr_class != DW_ADDR_none)
c906108c 16727 {
5e2b427d 16728 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16729 {
16730 int type_flags;
16731
849957d9 16732 type_flags = gdbarch_address_class_type_flags
5e2b427d 16733 (gdbarch, byte_size, addr_class);
876cecd0
TT
16734 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16735 == 0);
8b2dbe47
KB
16736 type = make_type_with_address_space (type, type_flags);
16737 }
16738 else if (TYPE_LENGTH (type) != byte_size)
16739 {
b98664d3 16740 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16741 }
2b4424c3
TT
16742 else if (TYPE_RAW_ALIGN (type) != alignment)
16743 {
b98664d3 16744 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16745 " - DIE at %s [in module %s]"),
16746 sect_offset_str (die->sect_off),
5e22e966 16747 objfile_name (cu->per_objfile->objfile));
2b4424c3 16748 }
6e70227d 16749 else
9a619af0
MS
16750 {
16751 /* Should we also complain about unhandled address classes? */
16752 }
c906108c 16753 }
8b2dbe47
KB
16754
16755 TYPE_LENGTH (type) = byte_size;
2b4424c3 16756 set_type_align (type, alignment);
f792889a 16757 return set_die_type (die, type, cu);
c906108c
SS
16758}
16759
16760/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16761 the user defined type vector. */
16762
f792889a 16763static struct type *
e7c27a73 16764read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16765{
16766 struct type *type;
16767 struct type *to_type;
16768 struct type *domain;
16769
e7c27a73
DJ
16770 to_type = die_type (die, cu);
16771 domain = die_containing_type (die, cu);
0d5de010 16772
7e314c57
JK
16773 /* The calls above may have already set the type for this DIE. */
16774 type = get_die_type (die, cu);
16775 if (type)
16776 return type;
16777
78134374 16778 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16779 type = lookup_methodptr_type (to_type);
78134374 16780 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16781 {
5e22e966 16782 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16783
16784 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16785 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16786 TYPE_VARARGS (to_type));
16787 type = lookup_methodptr_type (new_type);
16788 }
0d5de010
DJ
16789 else
16790 type = lookup_memberptr_type (to_type, domain);
c906108c 16791
f792889a 16792 return set_die_type (die, type, cu);
c906108c
SS
16793}
16794
4297a3f0 16795/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16796 the user defined type vector. */
16797
f792889a 16798static struct type *
4297a3f0
AV
16799read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16800 enum type_code refcode)
c906108c 16801{
e7c27a73 16802 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16803 struct type *type, *target_type;
c906108c
SS
16804 struct attribute *attr;
16805
4297a3f0
AV
16806 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16807
7e314c57
JK
16808 target_type = die_type (die, cu);
16809
16810 /* The die_type call above may have already set the type for this DIE. */
16811 type = get_die_type (die, cu);
16812 if (type)
16813 return type;
16814
4297a3f0 16815 type = lookup_reference_type (target_type, refcode);
e142c38c 16816 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16817 if (attr != nullptr)
c906108c
SS
16818 {
16819 TYPE_LENGTH (type) = DW_UNSND (attr);
16820 }
16821 else
16822 {
107d2387 16823 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16824 }
2b4424c3 16825 maybe_set_alignment (cu, die, type);
f792889a 16826 return set_die_type (die, type, cu);
c906108c
SS
16827}
16828
cf363f18
MW
16829/* Add the given cv-qualifiers to the element type of the array. GCC
16830 outputs DWARF type qualifiers that apply to an array, not the
16831 element type. But GDB relies on the array element type to carry
16832 the cv-qualifiers. This mimics section 6.7.3 of the C99
16833 specification. */
16834
16835static struct type *
16836add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16837 struct type *base_type, int cnst, int voltl)
16838{
16839 struct type *el_type, *inner_array;
16840
16841 base_type = copy_type (base_type);
16842 inner_array = base_type;
16843
78134374 16844 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16845 {
16846 TYPE_TARGET_TYPE (inner_array) =
16847 copy_type (TYPE_TARGET_TYPE (inner_array));
16848 inner_array = TYPE_TARGET_TYPE (inner_array);
16849 }
16850
16851 el_type = TYPE_TARGET_TYPE (inner_array);
16852 cnst |= TYPE_CONST (el_type);
16853 voltl |= TYPE_VOLATILE (el_type);
16854 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16855
16856 return set_die_type (die, base_type, cu);
16857}
16858
f792889a 16859static struct type *
e7c27a73 16860read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16861{
f792889a 16862 struct type *base_type, *cv_type;
c906108c 16863
e7c27a73 16864 base_type = die_type (die, cu);
7e314c57
JK
16865
16866 /* The die_type call above may have already set the type for this DIE. */
16867 cv_type = get_die_type (die, cu);
16868 if (cv_type)
16869 return cv_type;
16870
2f608a3a
KW
16871 /* In case the const qualifier is applied to an array type, the element type
16872 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16873 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16874 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16875
f792889a
DJ
16876 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16877 return set_die_type (die, cv_type, cu);
c906108c
SS
16878}
16879
f792889a 16880static struct type *
e7c27a73 16881read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16882{
f792889a 16883 struct type *base_type, *cv_type;
c906108c 16884
e7c27a73 16885 base_type = die_type (die, cu);
7e314c57
JK
16886
16887 /* The die_type call above may have already set the type for this DIE. */
16888 cv_type = get_die_type (die, cu);
16889 if (cv_type)
16890 return cv_type;
16891
cf363f18
MW
16892 /* In case the volatile qualifier is applied to an array type, the
16893 element type is so qualified, not the array type (section 6.7.3
16894 of C99). */
78134374 16895 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16896 return add_array_cv_type (die, cu, base_type, 0, 1);
16897
f792889a
DJ
16898 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16899 return set_die_type (die, cv_type, cu);
c906108c
SS
16900}
16901
06d66ee9
TT
16902/* Handle DW_TAG_restrict_type. */
16903
16904static struct type *
16905read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16906{
16907 struct type *base_type, *cv_type;
16908
16909 base_type = die_type (die, cu);
16910
16911 /* The die_type call above may have already set the type for this DIE. */
16912 cv_type = get_die_type (die, cu);
16913 if (cv_type)
16914 return cv_type;
16915
16916 cv_type = make_restrict_type (base_type);
16917 return set_die_type (die, cv_type, cu);
16918}
16919
a2c2acaf
MW
16920/* Handle DW_TAG_atomic_type. */
16921
16922static struct type *
16923read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16924{
16925 struct type *base_type, *cv_type;
16926
16927 base_type = die_type (die, cu);
16928
16929 /* The die_type call above may have already set the type for this DIE. */
16930 cv_type = get_die_type (die, cu);
16931 if (cv_type)
16932 return cv_type;
16933
16934 cv_type = make_atomic_type (base_type);
16935 return set_die_type (die, cv_type, cu);
16936}
16937
c906108c
SS
16938/* Extract all information from a DW_TAG_string_type DIE and add to
16939 the user defined type vector. It isn't really a user defined type,
16940 but it behaves like one, with other DIE's using an AT_user_def_type
16941 attribute to reference it. */
16942
f792889a 16943static struct type *
e7c27a73 16944read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16945{
5e22e966 16946 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16947 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16948 struct type *type, *range_type, *index_type, *char_type;
16949 struct attribute *attr;
216a7e6b
AB
16950 struct dynamic_prop prop;
16951 bool length_is_constant = true;
16952 LONGEST length;
16953
16954 /* There are a couple of places where bit sizes might be made use of
16955 when parsing a DW_TAG_string_type, however, no producer that we know
16956 of make use of these. Handling bit sizes that are a multiple of the
16957 byte size is easy enough, but what about other bit sizes? Lets deal
16958 with that problem when we have to. Warn about these attributes being
16959 unsupported, then parse the type and ignore them like we always
16960 have. */
16961 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16962 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16963 {
16964 static bool warning_printed = false;
16965 if (!warning_printed)
16966 {
16967 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16968 "currently supported on DW_TAG_string_type."));
16969 warning_printed = true;
16970 }
16971 }
c906108c 16972
e142c38c 16973 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16974 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16975 {
16976 /* The string length describes the location at which the length of
16977 the string can be found. The size of the length field can be
16978 specified with one of the attributes below. */
16979 struct type *prop_type;
16980 struct attribute *len
16981 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16982 if (len == nullptr)
16983 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16984 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16985 {
16986 /* Pass 0 as the default as we know this attribute is constant
16987 and the default value will not be returned. */
0826b30a 16988 LONGEST sz = len->constant_value (0);
09ba997f 16989 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16990 }
16991 else
16992 {
16993 /* If the size is not specified then we assume it is the size of
16994 an address on this target. */
09ba997f 16995 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16996 }
16997
16998 /* Convert the attribute into a dynamic property. */
16999 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17000 length = 1;
17001 else
17002 length_is_constant = false;
17003 }
17004 else if (attr != nullptr)
17005 {
17006 /* This DW_AT_string_length just contains the length with no
17007 indirection. There's no need to create a dynamic property in this
17008 case. Pass 0 for the default value as we know it will not be
17009 returned in this case. */
0826b30a 17010 length = attr->constant_value (0);
216a7e6b
AB
17011 }
17012 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17013 {
216a7e6b 17014 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17015 length = attr->constant_value (1);
c906108c
SS
17016 }
17017 else
17018 {
216a7e6b
AB
17019 /* Use 1 as a fallback length if we have nothing else. */
17020 length = 1;
c906108c 17021 }
6ccb9162 17022
46bf5051 17023 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17024 if (length_is_constant)
17025 range_type = create_static_range_type (NULL, index_type, 1, length);
17026 else
17027 {
17028 struct dynamic_prop low_bound;
17029
17030 low_bound.kind = PROP_CONST;
17031 low_bound.data.const_val = 1;
17032 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17033 }
3b7538c0
UW
17034 char_type = language_string_char_type (cu->language_defn, gdbarch);
17035 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17036
f792889a 17037 return set_die_type (die, type, cu);
c906108c
SS
17038}
17039
4d804846
JB
17040/* Assuming that DIE corresponds to a function, returns nonzero
17041 if the function is prototyped. */
17042
17043static int
17044prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17045{
17046 struct attribute *attr;
17047
17048 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17049 if (attr && (DW_UNSND (attr) != 0))
17050 return 1;
17051
17052 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17053 is only meaningful for C, but the concept also extends to other
4d804846
JB
17054 languages that allow unprototyped functions (Eg: Objective C).
17055 For all other languages, assume that functions are always
17056 prototyped. */
17057 if (cu->language != language_c
17058 && cu->language != language_objc
17059 && cu->language != language_opencl)
17060 return 1;
17061
17062 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17063 prototyped and unprototyped functions; default to prototyped,
17064 since that is more common in modern code (and RealView warns
17065 about unprototyped functions). */
17066 if (producer_is_realview (cu->producer))
17067 return 1;
17068
17069 return 0;
17070}
17071
c906108c
SS
17072/* Handle DIES due to C code like:
17073
17074 struct foo
c5aa993b
JM
17075 {
17076 int (*funcp)(int a, long l);
17077 int b;
17078 };
c906108c 17079
0963b4bd 17080 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17081
f792889a 17082static struct type *
e7c27a73 17083read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17084{
5e22e966 17085 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17086 struct type *type; /* Type that this function returns. */
17087 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17088 struct attribute *attr;
17089
e7c27a73 17090 type = die_type (die, cu);
7e314c57
JK
17091
17092 /* The die_type call above may have already set the type for this DIE. */
17093 ftype = get_die_type (die, cu);
17094 if (ftype)
17095 return ftype;
17096
0c8b41f1 17097 ftype = lookup_function_type (type);
c906108c 17098
4d804846 17099 if (prototyped_function_p (die, cu))
a6c727b2 17100 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17101
c055b101
CV
17102 /* Store the calling convention in the type if it's available in
17103 the subroutine die. Otherwise set the calling convention to
17104 the default value DW_CC_normal. */
17105 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17106 if (attr != nullptr
17107 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17108 TYPE_CALLING_CONVENTION (ftype)
17109 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17110 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17111 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17112 else
17113 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17114
743649fd
MW
17115 /* Record whether the function returns normally to its caller or not
17116 if the DWARF producer set that information. */
17117 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17118 if (attr && (DW_UNSND (attr) != 0))
17119 TYPE_NO_RETURN (ftype) = 1;
17120
76c10ea2
GM
17121 /* We need to add the subroutine type to the die immediately so
17122 we don't infinitely recurse when dealing with parameters
0963b4bd 17123 declared as the same subroutine type. */
76c10ea2 17124 set_die_type (die, ftype, cu);
6e70227d 17125
639d11d3 17126 if (die->child != NULL)
c906108c 17127 {
bb5ed363 17128 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17129 struct die_info *child_die;
8072405b 17130 int nparams, iparams;
c906108c
SS
17131
17132 /* Count the number of parameters.
17133 FIXME: GDB currently ignores vararg functions, but knows about
17134 vararg member functions. */
8072405b 17135 nparams = 0;
639d11d3 17136 child_die = die->child;
c906108c
SS
17137 while (child_die && child_die->tag)
17138 {
17139 if (child_die->tag == DW_TAG_formal_parameter)
17140 nparams++;
17141 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17142 TYPE_VARARGS (ftype) = 1;
436c571c 17143 child_die = child_die->sibling;
c906108c
SS
17144 }
17145
17146 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17147 ftype->set_num_fields (nparams);
3cabb6b0
SM
17148 ftype->set_fields
17149 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17150
8072405b
JK
17151 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17152 even if we error out during the parameters reading below. */
17153 for (iparams = 0; iparams < nparams; iparams++)
17154 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17155
17156 iparams = 0;
639d11d3 17157 child_die = die->child;
c906108c
SS
17158 while (child_die && child_die->tag)
17159 {
17160 if (child_die->tag == DW_TAG_formal_parameter)
17161 {
3ce3b1ba
PA
17162 struct type *arg_type;
17163
17164 /* DWARF version 2 has no clean way to discern C++
17165 static and non-static member functions. G++ helps
17166 GDB by marking the first parameter for non-static
17167 member functions (which is the this pointer) as
17168 artificial. We pass this information to
17169 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17170
17171 DWARF version 3 added DW_AT_object_pointer, which GCC
17172 4.5 does not yet generate. */
e142c38c 17173 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17174 if (attr != nullptr)
c906108c
SS
17175 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17176 else
9c37b5ae 17177 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17178 arg_type = die_type (child_die, cu);
17179
17180 /* RealView does not mark THIS as const, which the testsuite
17181 expects. GCC marks THIS as const in method definitions,
17182 but not in the class specifications (GCC PR 43053). */
17183 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17184 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17185 {
17186 int is_this = 0;
17187 struct dwarf2_cu *arg_cu = cu;
17188 const char *name = dwarf2_name (child_die, cu);
17189
17190 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17191 if (attr != nullptr)
3ce3b1ba
PA
17192 {
17193 /* If the compiler emits this, use it. */
17194 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17195 is_this = 1;
17196 }
17197 else if (name && strcmp (name, "this") == 0)
17198 /* Function definitions will have the argument names. */
17199 is_this = 1;
17200 else if (name == NULL && iparams == 0)
17201 /* Declarations may not have the names, so like
17202 elsewhere in GDB, assume an artificial first
17203 argument is "this". */
17204 is_this = 1;
17205
17206 if (is_this)
17207 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17208 arg_type, 0);
17209 }
17210
17211 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17212 iparams++;
17213 }
436c571c 17214 child_die = child_die->sibling;
c906108c
SS
17215 }
17216 }
17217
76c10ea2 17218 return ftype;
c906108c
SS
17219}
17220
f792889a 17221static struct type *
e7c27a73 17222read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17223{
5e22e966 17224 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17225 const char *name = NULL;
3c8e0968 17226 struct type *this_type, *target_type;
c906108c 17227
94af9270 17228 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17229 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17230 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17231 set_die_type (die, this_type, cu);
3c8e0968
DE
17232 target_type = die_type (die, cu);
17233 if (target_type != this_type)
17234 TYPE_TARGET_TYPE (this_type) = target_type;
17235 else
17236 {
17237 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17238 spec and cause infinite loops in GDB. */
b98664d3 17239 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17240 "- DIE at %s [in module %s]"),
17241 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17242 TYPE_TARGET_TYPE (this_type) = NULL;
17243 }
e4003a34
TV
17244 if (name == NULL)
17245 {
17246 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17247 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17248 Handle these by just returning the target type, rather than
17249 constructing an anonymous typedef type and trying to handle this
17250 elsewhere. */
17251 set_die_type (die, target_type, cu);
17252 return target_type;
17253 }
f792889a 17254 return this_type;
c906108c
SS
17255}
17256
9b790ce7
UW
17257/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17258 (which may be different from NAME) to the architecture back-end to allow
17259 it to guess the correct format if necessary. */
17260
17261static struct type *
17262dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17263 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17264{
08feed99 17265 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17266 const struct floatformat **format;
17267 struct type *type;
17268
17269 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17270 if (format)
103a685e 17271 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17272 else
77b7c781 17273 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17274
17275 return type;
17276}
17277
eb77c9df
AB
17278/* Allocate an integer type of size BITS and name NAME. */
17279
17280static struct type *
17281dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17282 int bits, int unsigned_p, const char *name)
17283{
17284 struct type *type;
17285
17286 /* Versions of Intel's C Compiler generate an integer type called "void"
17287 instead of using DW_TAG_unspecified_type. This has been seen on
17288 at least versions 14, 17, and 18. */
35ee2dc2
AB
17289 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17290 && strcmp (name, "void") == 0)
eb77c9df
AB
17291 type = objfile_type (objfile)->builtin_void;
17292 else
17293 type = init_integer_type (objfile, bits, unsigned_p, name);
17294
17295 return type;
17296}
17297
8bdc1658
AB
17298/* Initialise and return a floating point type of size BITS suitable for
17299 use as a component of a complex number. The NAME_HINT is passed through
17300 when initialising the floating point type and is the name of the complex
17301 type.
17302
17303 As DWARF doesn't currently provide an explicit name for the components
17304 of a complex number, but it can be helpful to have these components
17305 named, we try to select a suitable name based on the size of the
17306 component. */
17307static struct type *
17308dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17309 struct objfile *objfile,
103a685e
TT
17310 int bits, const char *name_hint,
17311 enum bfd_endian byte_order)
8bdc1658 17312{
08feed99 17313 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17314 struct type *tt = nullptr;
17315
35add35e
AB
17316 /* Try to find a suitable floating point builtin type of size BITS.
17317 We're going to use the name of this type as the name for the complex
17318 target type that we are about to create. */
1db455a7 17319 switch (cu->language)
8bdc1658 17320 {
1db455a7
AB
17321 case language_fortran:
17322 switch (bits)
17323 {
17324 case 32:
17325 tt = builtin_f_type (gdbarch)->builtin_real;
17326 break;
17327 case 64:
17328 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17329 break;
17330 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17331 case 128:
17332 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17333 break;
17334 }
8bdc1658 17335 break;
1db455a7
AB
17336 default:
17337 switch (bits)
17338 {
17339 case 32:
17340 tt = builtin_type (gdbarch)->builtin_float;
17341 break;
17342 case 64:
17343 tt = builtin_type (gdbarch)->builtin_double;
17344 break;
17345 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17346 case 128:
17347 tt = builtin_type (gdbarch)->builtin_long_double;
17348 break;
17349 }
8bdc1658
AB
17350 break;
17351 }
17352
35add35e
AB
17353 /* If the type we found doesn't match the size we were looking for, then
17354 pretend we didn't find a type at all, the complex target type we
17355 create will then be nameless. */
a12e5744 17356 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17357 tt = nullptr;
17358
7d93a1e0 17359 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17360 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17361}
17362
c906108c
SS
17363/* Find a representation of a given base type and install
17364 it in the TYPE field of the die. */
17365
f792889a 17366static struct type *
e7c27a73 17367read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17368{
5e22e966 17369 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17370 struct type *type;
17371 struct attribute *attr;
19f392bc 17372 int encoding = 0, bits = 0;
15d034d0 17373 const char *name;
34877895 17374 gdbarch *arch;
c906108c 17375
e142c38c 17376 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17377 if (attr != nullptr)
34877895 17378 encoding = DW_UNSND (attr);
e142c38c 17379 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17380 if (attr != nullptr)
34877895 17381 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17382 name = dwarf2_name (die, cu);
6ccb9162 17383 if (!name)
34877895 17384 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17385
08feed99 17386 arch = objfile->arch ();
103a685e
TT
17387 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17388
34877895
PJ
17389 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17390 if (attr)
103a685e
TT
17391 {
17392 int endianity = DW_UNSND (attr);
17393
17394 switch (endianity)
17395 {
17396 case DW_END_big:
17397 byte_order = BFD_ENDIAN_BIG;
17398 break;
17399 case DW_END_little:
17400 byte_order = BFD_ENDIAN_LITTLE;
17401 break;
17402 default:
17403 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17404 break;
17405 }
17406 }
6ccb9162
UW
17407
17408 switch (encoding)
c906108c 17409 {
6ccb9162
UW
17410 case DW_ATE_address:
17411 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17412 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17413 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17414 break;
17415 case DW_ATE_boolean:
19f392bc 17416 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17417 break;
17418 case DW_ATE_complex_float:
103a685e
TT
17419 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17420 byte_order);
78134374 17421 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17422 {
17423 if (name == nullptr)
17424 {
17425 struct obstack *obstack
5e22e966 17426 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17427 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17428 nullptr);
17429 }
17430 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17431 }
17432 else
17433 type = init_complex_type (name, type);
6ccb9162
UW
17434 break;
17435 case DW_ATE_decimal_float:
19f392bc 17436 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17437 break;
17438 case DW_ATE_float:
103a685e 17439 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17440 break;
17441 case DW_ATE_signed:
eb77c9df 17442 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17443 break;
17444 case DW_ATE_unsigned:
3b2b8fea
TT
17445 if (cu->language == language_fortran
17446 && name
61012eef 17447 && startswith (name, "character("))
19f392bc
UW
17448 type = init_character_type (objfile, bits, 1, name);
17449 else
eb77c9df 17450 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17451 break;
17452 case DW_ATE_signed_char:
6e70227d 17453 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17454 || cu->language == language_pascal
17455 || cu->language == language_fortran)
19f392bc
UW
17456 type = init_character_type (objfile, bits, 0, name);
17457 else
eb77c9df 17458 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17459 break;
17460 case DW_ATE_unsigned_char:
868a0084 17461 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17462 || cu->language == language_pascal
c44af4eb
TT
17463 || cu->language == language_fortran
17464 || cu->language == language_rust)
19f392bc
UW
17465 type = init_character_type (objfile, bits, 1, name);
17466 else
eb77c9df 17467 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17468 break;
75079b2b 17469 case DW_ATE_UTF:
53e710ac 17470 {
53e710ac
PA
17471 if (bits == 16)
17472 type = builtin_type (arch)->builtin_char16;
17473 else if (bits == 32)
17474 type = builtin_type (arch)->builtin_char32;
17475 else
17476 {
b98664d3 17477 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17478 bits);
eb77c9df 17479 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17480 }
17481 return set_die_type (die, type, cu);
17482 }
75079b2b
TT
17483 break;
17484
6ccb9162 17485 default:
b98664d3 17486 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17487 dwarf_type_encoding_name (encoding));
77b7c781 17488 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17489 break;
c906108c 17490 }
6ccb9162 17491
0114d602 17492 if (name && strcmp (name, "char") == 0)
876cecd0 17493 TYPE_NOSIGN (type) = 1;
0114d602 17494
2b4424c3
TT
17495 maybe_set_alignment (cu, die, type);
17496
103a685e 17497 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17498
f792889a 17499 return set_die_type (die, type, cu);
c906108c
SS
17500}
17501
80180f79
SA
17502/* Parse dwarf attribute if it's a block, reference or constant and put the
17503 resulting value of the attribute into struct bound_prop.
17504 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17505
17506static int
17507attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17508 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17509 struct type *default_type)
80180f79
SA
17510{
17511 struct dwarf2_property_baton *baton;
5e22e966 17512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17513 struct objfile *objfile = per_objfile->objfile;
17514 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17515
9a49df9d
AB
17516 gdb_assert (default_type != NULL);
17517
80180f79
SA
17518 if (attr == NULL || prop == NULL)
17519 return 0;
17520
4fc6c0d5 17521 if (attr->form_is_block ())
80180f79 17522 {
8d749320 17523 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17524 baton->property_type = default_type;
80180f79 17525 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17526 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17527 baton->locexpr.size = DW_BLOCK (attr)->size;
17528 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17529 switch (attr->name)
17530 {
17531 case DW_AT_string_length:
17532 baton->locexpr.is_reference = true;
17533 break;
17534 default:
17535 baton->locexpr.is_reference = false;
17536 break;
17537 }
80180f79
SA
17538 prop->data.baton = baton;
17539 prop->kind = PROP_LOCEXPR;
17540 gdb_assert (prop->data.baton != NULL);
17541 }
cd6c91b4 17542 else if (attr->form_is_ref ())
80180f79
SA
17543 {
17544 struct dwarf2_cu *target_cu = cu;
17545 struct die_info *target_die;
17546 struct attribute *target_attr;
17547
17548 target_die = follow_die_ref (die, attr, &target_cu);
17549 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17550 if (target_attr == NULL)
17551 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17552 target_cu);
80180f79
SA
17553 if (target_attr == NULL)
17554 return 0;
17555
df25ebbd 17556 switch (target_attr->name)
80180f79 17557 {
df25ebbd 17558 case DW_AT_location:
cd6c91b4 17559 if (target_attr->form_is_section_offset ())
df25ebbd 17560 {
8d749320 17561 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17562 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17563 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17564 prop->data.baton = baton;
17565 prop->kind = PROP_LOCLIST;
17566 gdb_assert (prop->data.baton != NULL);
17567 }
4fc6c0d5 17568 else if (target_attr->form_is_block ())
df25ebbd 17569 {
8d749320 17570 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17571 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17572 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17573 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17574 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17575 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17576 baton->locexpr.is_reference = true;
df25ebbd
JB
17577 prop->data.baton = baton;
17578 prop->kind = PROP_LOCEXPR;
17579 gdb_assert (prop->data.baton != NULL);
17580 }
17581 else
17582 {
17583 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17584 "dynamic property");
17585 return 0;
17586 }
17587 break;
17588 case DW_AT_data_member_location:
17589 {
17590 LONGEST offset;
17591
17592 if (!handle_data_member_location (target_die, target_cu,
17593 &offset))
17594 return 0;
17595
8d749320 17596 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17597 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17598 target_cu);
df25ebbd
JB
17599 baton->offset_info.offset = offset;
17600 baton->offset_info.type = die_type (target_die, target_cu);
17601 prop->data.baton = baton;
17602 prop->kind = PROP_ADDR_OFFSET;
17603 break;
17604 }
80180f79
SA
17605 }
17606 }
cd6c91b4 17607 else if (attr->form_is_constant ())
80180f79 17608 {
0826b30a 17609 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17610 prop->kind = PROP_CONST;
17611 }
17612 else
17613 {
17614 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17615 dwarf2_name (die, cu));
17616 return 0;
17617 }
17618
17619 return 1;
17620}
17621
09ba997f 17622/* See read.h. */
9a49df9d 17623
09ba997f
TT
17624struct type *
17625dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17626{
09ba997f 17627 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17628 struct type *int_type;
17629
17630 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17631#define TRY_TYPE(F) \
17632 int_type = (unsigned_p \
17633 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17634 : objfile_type (objfile)->builtin_ ## F); \
17635 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17636 return int_type
17637
17638 TRY_TYPE (char);
17639 TRY_TYPE (short);
17640 TRY_TYPE (int);
17641 TRY_TYPE (long);
17642 TRY_TYPE (long_long);
17643
17644#undef TRY_TYPE
17645
17646 gdb_assert_not_reached ("unable to find suitable integer type");
17647}
17648
09ba997f 17649/* See read.h. */
11a8b164 17650
09ba997f
TT
17651struct type *
17652dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17653{
09ba997f
TT
17654 int addr_size = this->addr_size ();
17655 return int_type (addr_size, unsigned_p);
11a8b164
AB
17656}
17657
b86352cf
AB
17658/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17659 present (which is valid) then compute the default type based on the
17660 compilation units address size. */
17661
17662static struct type *
17663read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17664{
17665 struct type *index_type = die_type (die, cu);
17666
17667 /* Dwarf-2 specifications explicitly allows to create subrange types
17668 without specifying a base type.
17669 In that case, the base type must be set to the type of
17670 the lower bound, upper bound or count, in that order, if any of these
17671 three attributes references an object that has a type.
17672 If no base type is found, the Dwarf-2 specifications say that
17673 a signed integer type of size equal to the size of an address should
17674 be used.
17675 For the following C code: `extern char gdb_int [];'
17676 GCC produces an empty range DIE.
17677 FIXME: muller/2010-05-28: Possible references to object for low bound,
17678 high bound or count are not yet handled by this code. */
78134374 17679 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17680 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17681
17682 return index_type;
17683}
17684
a02abb62
JB
17685/* Read the given DW_AT_subrange DIE. */
17686
f792889a 17687static struct type *
a02abb62
JB
17688read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17689{
4c9ad8c2 17690 struct type *base_type, *orig_base_type;
a02abb62
JB
17691 struct type *range_type;
17692 struct attribute *attr;
729efb13 17693 struct dynamic_prop low, high;
4fae6e18 17694 int low_default_is_valid;
c451ebe5 17695 int high_bound_is_count = 0;
15d034d0 17696 const char *name;
d359392f 17697 ULONGEST negative_mask;
e77813c8 17698
b86352cf
AB
17699 orig_base_type = read_subrange_index_type (die, cu);
17700
4c9ad8c2
TT
17701 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17702 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17703 creating the range type, but we use the result of check_typedef
17704 when examining properties of the type. */
17705 base_type = check_typedef (orig_base_type);
a02abb62 17706
7e314c57
JK
17707 /* The die_type call above may have already set the type for this DIE. */
17708 range_type = get_die_type (die, cu);
17709 if (range_type)
17710 return range_type;
17711
729efb13
SA
17712 low.kind = PROP_CONST;
17713 high.kind = PROP_CONST;
17714 high.data.const_val = 0;
17715
4fae6e18
JK
17716 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17717 omitting DW_AT_lower_bound. */
17718 switch (cu->language)
6e70227d 17719 {
4fae6e18
JK
17720 case language_c:
17721 case language_cplus:
729efb13 17722 low.data.const_val = 0;
4fae6e18
JK
17723 low_default_is_valid = 1;
17724 break;
17725 case language_fortran:
729efb13 17726 low.data.const_val = 1;
4fae6e18
JK
17727 low_default_is_valid = 1;
17728 break;
17729 case language_d:
4fae6e18 17730 case language_objc:
c44af4eb 17731 case language_rust:
729efb13 17732 low.data.const_val = 0;
4fae6e18
JK
17733 low_default_is_valid = (cu->header.version >= 4);
17734 break;
17735 case language_ada:
17736 case language_m2:
17737 case language_pascal:
729efb13 17738 low.data.const_val = 1;
4fae6e18
JK
17739 low_default_is_valid = (cu->header.version >= 4);
17740 break;
17741 default:
729efb13 17742 low.data.const_val = 0;
4fae6e18
JK
17743 low_default_is_valid = 0;
17744 break;
a02abb62
JB
17745 }
17746
e142c38c 17747 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17748 if (attr != nullptr)
9a49df9d 17749 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17750 else if (!low_default_is_valid)
b98664d3 17751 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17752 "- DIE at %s [in module %s]"),
17753 sect_offset_str (die->sect_off),
5e22e966 17754 objfile_name (cu->per_objfile->objfile));
a02abb62 17755
506f5c41
TV
17756 struct attribute *attr_ub, *attr_count;
17757 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17758 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17759 {
506f5c41 17760 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17761 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17762 {
c451ebe5
SA
17763 /* If bounds are constant do the final calculation here. */
17764 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17765 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17766 else
17767 high_bound_is_count = 1;
c2ff108b 17768 }
506f5c41
TV
17769 else
17770 {
17771 if (attr_ub != NULL)
17772 complaint (_("Unresolved DW_AT_upper_bound "
17773 "- DIE at %s [in module %s]"),
17774 sect_offset_str (die->sect_off),
5e22e966 17775 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17776 if (attr_count != NULL)
17777 complaint (_("Unresolved DW_AT_count "
17778 "- DIE at %s [in module %s]"),
17779 sect_offset_str (die->sect_off),
5e22e966 17780 objfile_name (cu->per_objfile->objfile));
506f5c41 17781 }
e77813c8 17782 }
a02abb62 17783
4e962e74
TT
17784 LONGEST bias = 0;
17785 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17786 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17787 bias = bias_attr->constant_value (0);
4e962e74 17788
dbb9c2b1
JB
17789 /* Normally, the DWARF producers are expected to use a signed
17790 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17791 But this is unfortunately not always the case, as witnessed
17792 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17793 is used instead. To work around that ambiguity, we treat
17794 the bounds as signed, and thus sign-extend their values, when
17795 the base type is signed. */
6e70227d 17796 negative_mask =
d359392f 17797 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17798 if (low.kind == PROP_CONST
17799 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17800 low.data.const_val |= negative_mask;
17801 if (high.kind == PROP_CONST
17802 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17803 high.data.const_val |= negative_mask;
43bbcdc2 17804
5bbd8269
AB
17805 /* Check for bit and byte strides. */
17806 struct dynamic_prop byte_stride_prop;
17807 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17808 if (attr_byte_stride != nullptr)
17809 {
09ba997f 17810 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17811 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17812 prop_type);
17813 }
17814
17815 struct dynamic_prop bit_stride_prop;
17816 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17817 if (attr_bit_stride != nullptr)
17818 {
17819 /* It only makes sense to have either a bit or byte stride. */
17820 if (attr_byte_stride != nullptr)
17821 {
17822 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17823 "- DIE at %s [in module %s]"),
17824 sect_offset_str (die->sect_off),
5e22e966 17825 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17826 attr_bit_stride = nullptr;
17827 }
17828 else
17829 {
09ba997f 17830 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17831 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17832 prop_type);
17833 }
17834 }
17835
17836 if (attr_byte_stride != nullptr
17837 || attr_bit_stride != nullptr)
17838 {
17839 bool byte_stride_p = (attr_byte_stride != nullptr);
17840 struct dynamic_prop *stride
17841 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17842
17843 range_type
17844 = create_range_type_with_stride (NULL, orig_base_type, &low,
17845 &high, bias, stride, byte_stride_p);
17846 }
17847 else
17848 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17849
c451ebe5
SA
17850 if (high_bound_is_count)
17851 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17852
c2ff108b
JK
17853 /* Ada expects an empty array on no boundary attributes. */
17854 if (attr == NULL && cu->language != language_ada)
729efb13 17855 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17856
39cbfefa
DJ
17857 name = dwarf2_name (die, cu);
17858 if (name)
d0e39ea2 17859 range_type->set_name (name);
6e70227d 17860
e142c38c 17861 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17862 if (attr != nullptr)
a02abb62
JB
17863 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17864
2b4424c3
TT
17865 maybe_set_alignment (cu, die, range_type);
17866
7e314c57
JK
17867 set_die_type (die, range_type, cu);
17868
17869 /* set_die_type should be already done. */
b4ba55a1
JB
17870 set_descriptive_type (range_type, die, cu);
17871
7e314c57 17872 return range_type;
a02abb62 17873}
6e70227d 17874
f792889a 17875static struct type *
81a17f79
JB
17876read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17877{
17878 struct type *type;
81a17f79 17879
5e22e966 17880 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17881 type->set_name (dwarf2_name (die, cu));
81a17f79 17882
74a2f8ff 17883 /* In Ada, an unspecified type is typically used when the description
85102364 17884 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17885 such a type, we treat it as a stub, and try to resolve it later on,
17886 when needed. */
17887 if (cu->language == language_ada)
17888 TYPE_STUB (type) = 1;
17889
f792889a 17890 return set_die_type (die, type, cu);
81a17f79 17891}
a02abb62 17892
639d11d3
DC
17893/* Read a single die and all its descendents. Set the die's sibling
17894 field to NULL; set other fields in the die correctly, and set all
17895 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17896 location of the info_ptr after reading all of those dies. PARENT
17897 is the parent of the die in question. */
17898
17899static struct die_info *
dee91e82 17900read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17901 const gdb_byte *info_ptr,
17902 const gdb_byte **new_info_ptr,
dee91e82 17903 struct die_info *parent)
639d11d3
DC
17904{
17905 struct die_info *die;
d521ce57 17906 const gdb_byte *cur_ptr;
639d11d3 17907
3e225074 17908 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17909 if (die == NULL)
17910 {
17911 *new_info_ptr = cur_ptr;
17912 return NULL;
17913 }
93311388 17914 store_in_ref_table (die, reader->cu);
639d11d3 17915
3e225074 17916 if (die->has_children)
bf6af496 17917 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17918 else
17919 {
17920 die->child = NULL;
17921 *new_info_ptr = cur_ptr;
17922 }
17923
17924 die->sibling = NULL;
17925 die->parent = parent;
17926 return die;
17927}
17928
17929/* Read a die, all of its descendents, and all of its siblings; set
17930 all of the fields of all of the dies correctly. Arguments are as
17931 in read_die_and_children. */
17932
17933static struct die_info *
bf6af496 17934read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17935 const gdb_byte *info_ptr,
17936 const gdb_byte **new_info_ptr,
bf6af496 17937 struct die_info *parent)
639d11d3
DC
17938{
17939 struct die_info *first_die, *last_sibling;
d521ce57 17940 const gdb_byte *cur_ptr;
639d11d3 17941
c906108c 17942 cur_ptr = info_ptr;
639d11d3
DC
17943 first_die = last_sibling = NULL;
17944
17945 while (1)
c906108c 17946 {
639d11d3 17947 struct die_info *die
dee91e82 17948 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17949
1d325ec1 17950 if (die == NULL)
c906108c 17951 {
639d11d3
DC
17952 *new_info_ptr = cur_ptr;
17953 return first_die;
c906108c 17954 }
1d325ec1
DJ
17955
17956 if (!first_die)
17957 first_die = die;
c906108c 17958 else
1d325ec1
DJ
17959 last_sibling->sibling = die;
17960
17961 last_sibling = die;
c906108c 17962 }
c906108c
SS
17963}
17964
bf6af496
DE
17965/* Read a die, all of its descendents, and all of its siblings; set
17966 all of the fields of all of the dies correctly. Arguments are as
17967 in read_die_and_children.
17968 This the main entry point for reading a DIE and all its children. */
17969
17970static struct die_info *
17971read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17972 const gdb_byte *info_ptr,
17973 const gdb_byte **new_info_ptr,
bf6af496
DE
17974 struct die_info *parent)
17975{
17976 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17977 new_info_ptr, parent);
17978
b4f54984 17979 if (dwarf_die_debug)
bf6af496
DE
17980 {
17981 fprintf_unfiltered (gdb_stdlog,
17982 "Read die from %s@0x%x of %s:\n",
96b79293 17983 reader->die_section->get_name (),
bf6af496
DE
17984 (unsigned) (info_ptr - reader->die_section->buffer),
17985 bfd_get_filename (reader->abfd));
b4f54984 17986 dump_die (die, dwarf_die_debug);
bf6af496
DE
17987 }
17988
17989 return die;
17990}
17991
3019eac3
DE
17992/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17993 attributes.
17994 The caller is responsible for filling in the extra attributes
17995 and updating (*DIEP)->num_attrs.
17996 Set DIEP to point to a newly allocated die with its information,
3e225074 17997 except for its child, sibling, and parent fields. */
93311388 17998
d521ce57 17999static const gdb_byte *
3019eac3 18000read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18001 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18002 int num_extra_attrs)
93311388 18003{
b64f50a1 18004 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18005 struct abbrev_info *abbrev;
18006 struct die_info *die;
18007 struct dwarf2_cu *cu = reader->cu;
18008 bfd *abfd = reader->abfd;
18009
9c541725 18010 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18011 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18012 info_ptr += bytes_read;
18013 if (!abbrev_number)
18014 {
18015 *diep = NULL;
93311388
DE
18016 return info_ptr;
18017 }
18018
685af9cd 18019 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18020 if (!abbrev)
348e048f
DE
18021 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18022 abbrev_number,
18023 bfd_get_filename (abfd));
18024
3019eac3 18025 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18026 die->sect_off = sect_off;
93311388
DE
18027 die->tag = abbrev->tag;
18028 die->abbrev = abbrev_number;
3e225074 18029 die->has_children = abbrev->has_children;
93311388 18030
3019eac3
DE
18031 /* Make the result usable.
18032 The caller needs to update num_attrs after adding the extra
18033 attributes. */
93311388
DE
18034 die->num_attrs = abbrev->num_attrs;
18035
18a8505e 18036 std::vector<int> indexes_that_need_reprocess;
93311388 18037 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18038 {
18039 bool need_reprocess;
18040 info_ptr =
18041 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18042 info_ptr, &need_reprocess);
18043 if (need_reprocess)
18044 indexes_that_need_reprocess.push_back (i);
18045 }
18046
052c8bb8 18047 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18048 if (attr != nullptr)
18049 cu->str_offsets_base = DW_UNSND (attr);
93311388 18050
41144253 18051 attr = die->attr (DW_AT_loclists_base);
18052 if (attr != nullptr)
18053 cu->loclist_base = DW_UNSND (attr);
18054
a39fdb41 18055 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18056 if (maybe_addr_base.has_value ())
18057 cu->addr_base = *maybe_addr_base;
18058 for (int index : indexes_that_need_reprocess)
18059 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18060 *diep = die;
93311388
DE
18061 return info_ptr;
18062}
18063
3019eac3
DE
18064/* Read a die and all its attributes.
18065 Set DIEP to point to a newly allocated die with its information,
3e225074 18066 except for its child, sibling, and parent fields. */
3019eac3 18067
d521ce57 18068static const gdb_byte *
3019eac3 18069read_full_die (const struct die_reader_specs *reader,
3e225074 18070 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18071{
d521ce57 18072 const gdb_byte *result;
bf6af496 18073
3e225074 18074 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18075
b4f54984 18076 if (dwarf_die_debug)
bf6af496
DE
18077 {
18078 fprintf_unfiltered (gdb_stdlog,
18079 "Read die from %s@0x%x of %s:\n",
96b79293 18080 reader->die_section->get_name (),
bf6af496
DE
18081 (unsigned) (info_ptr - reader->die_section->buffer),
18082 bfd_get_filename (reader->abfd));
b4f54984 18083 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18084 }
18085
18086 return result;
3019eac3 18087}
433df2d4 18088\f
c906108c 18089
72bf9492
DJ
18090/* Returns nonzero if TAG represents a type that we might generate a partial
18091 symbol for. */
18092
18093static int
18094is_type_tag_for_partial (int tag)
18095{
18096 switch (tag)
18097 {
18098#if 0
18099 /* Some types that would be reasonable to generate partial symbols for,
18100 that we don't at present. */
18101 case DW_TAG_array_type:
18102 case DW_TAG_file_type:
18103 case DW_TAG_ptr_to_member_type:
18104 case DW_TAG_set_type:
18105 case DW_TAG_string_type:
18106 case DW_TAG_subroutine_type:
18107#endif
18108 case DW_TAG_base_type:
18109 case DW_TAG_class_type:
680b30c7 18110 case DW_TAG_interface_type:
72bf9492
DJ
18111 case DW_TAG_enumeration_type:
18112 case DW_TAG_structure_type:
18113 case DW_TAG_subrange_type:
18114 case DW_TAG_typedef:
18115 case DW_TAG_union_type:
18116 return 1;
18117 default:
18118 return 0;
18119 }
18120}
18121
18122/* Load all DIEs that are interesting for partial symbols into memory. */
18123
18124static struct partial_die_info *
dee91e82 18125load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18126 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18127{
dee91e82 18128 struct dwarf2_cu *cu = reader->cu;
5e22e966 18129 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18130 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18131 unsigned int bytes_read;
5afb4e99 18132 unsigned int load_all = 0;
72bf9492
DJ
18133 int nesting_level = 1;
18134
18135 parent_die = NULL;
18136 last_die = NULL;
18137
7adf1e79
DE
18138 gdb_assert (cu->per_cu != NULL);
18139 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18140 load_all = 1;
18141
72bf9492
DJ
18142 cu->partial_dies
18143 = htab_create_alloc_ex (cu->header.length / 12,
18144 partial_die_hash,
18145 partial_die_eq,
18146 NULL,
18147 &cu->comp_unit_obstack,
18148 hashtab_obstack_allocate,
18149 dummy_obstack_deallocate);
18150
72bf9492
DJ
18151 while (1)
18152 {
685af9cd 18153 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18154
18155 /* A NULL abbrev means the end of a series of children. */
18156 if (abbrev == NULL)
18157 {
18158 if (--nesting_level == 0)
cd9983dd
YQ
18159 return first_die;
18160
72bf9492
DJ
18161 info_ptr += bytes_read;
18162 last_die = parent_die;
18163 parent_die = parent_die->die_parent;
18164 continue;
18165 }
18166
98bfdba5
PA
18167 /* Check for template arguments. We never save these; if
18168 they're seen, we just mark the parent, and go on our way. */
18169 if (parent_die != NULL
18170 && cu->language == language_cplus
18171 && (abbrev->tag == DW_TAG_template_type_param
18172 || abbrev->tag == DW_TAG_template_value_param))
18173 {
18174 parent_die->has_template_arguments = 1;
18175
18176 if (!load_all)
18177 {
18178 /* We don't need a partial DIE for the template argument. */
dee91e82 18179 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18180 continue;
18181 }
18182 }
18183
0d99eb77 18184 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18185 Skip their other children. */
18186 if (!load_all
18187 && cu->language == language_cplus
18188 && parent_die != NULL
18189 && parent_die->tag == DW_TAG_subprogram)
18190 {
dee91e82 18191 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18192 continue;
18193 }
18194
5afb4e99
DJ
18195 /* Check whether this DIE is interesting enough to save. Normally
18196 we would not be interested in members here, but there may be
18197 later variables referencing them via DW_AT_specification (for
18198 static members). */
18199 if (!load_all
18200 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18201 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18202 && abbrev->tag != DW_TAG_enumerator
18203 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18204 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18205 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18206 && abbrev->tag != DW_TAG_variable
5afb4e99 18207 && abbrev->tag != DW_TAG_namespace
f55ee35c 18208 && abbrev->tag != DW_TAG_module
95554aad 18209 && abbrev->tag != DW_TAG_member
74921315
KS
18210 && abbrev->tag != DW_TAG_imported_unit
18211 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18212 {
18213 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18214 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18215 continue;
18216 }
18217
6f06d47b
YQ
18218 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18219 abbrev);
cd9983dd 18220
48fbe735 18221 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18222
18223 /* This two-pass algorithm for processing partial symbols has a
18224 high cost in cache pressure. Thus, handle some simple cases
18225 here which cover the majority of C partial symbols. DIEs
18226 which neither have specification tags in them, nor could have
18227 specification tags elsewhere pointing at them, can simply be
18228 processed and discarded.
18229
18230 This segment is also optional; scan_partial_symbols and
18231 add_partial_symbol will handle these DIEs if we chain
18232 them in normally. When compilers which do not emit large
18233 quantities of duplicate debug information are more common,
18234 this code can probably be removed. */
18235
18236 /* Any complete simple types at the top level (pretty much all
18237 of them, for a language without namespaces), can be processed
18238 directly. */
18239 if (parent_die == NULL
cd9983dd
YQ
18240 && pdi.has_specification == 0
18241 && pdi.is_declaration == 0
18242 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18243 || pdi.tag == DW_TAG_base_type
18244 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18245 {
cd9983dd 18246 if (building_psymtab && pdi.name != NULL)
31edb802 18247 add_psymbol_to_list (pdi.name, false,
79748972 18248 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18249 psymbol_placement::STATIC,
1762568f 18250 0, cu->language, objfile);
cd9983dd 18251 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18252 continue;
18253 }
18254
d8228535
JK
18255 /* The exception for DW_TAG_typedef with has_children above is
18256 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18257 type_name_or_error will error on such types later.
d8228535
JK
18258
18259 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18260 it could not find the child DIEs referenced later, this is checked
18261 above. In correct DWARF DW_TAG_typedef should have no children. */
18262
cd9983dd 18263 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18264 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18265 "- DIE at %s [in module %s]"),
cd9983dd 18266 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18267
72bf9492
DJ
18268 /* If we're at the second level, and we're an enumerator, and
18269 our parent has no specification (meaning possibly lives in a
18270 namespace elsewhere), then we can add the partial symbol now
18271 instead of queueing it. */
cd9983dd 18272 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18273 && parent_die != NULL
18274 && parent_die->die_parent == NULL
18275 && parent_die->tag == DW_TAG_enumeration_type
18276 && parent_die->has_specification == 0)
18277 {
cd9983dd 18278 if (pdi.name == NULL)
b98664d3 18279 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18280 else if (building_psymtab)
31edb802 18281 add_psymbol_to_list (pdi.name, false,
79748972 18282 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18283 cu->language == language_cplus
75aedd27
TT
18284 ? psymbol_placement::GLOBAL
18285 : psymbol_placement::STATIC,
1762568f 18286 0, cu->language, objfile);
72bf9492 18287
cd9983dd 18288 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18289 continue;
18290 }
18291
cd9983dd 18292 struct partial_die_info *part_die
6f06d47b 18293 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18294
72bf9492
DJ
18295 /* We'll save this DIE so link it in. */
18296 part_die->die_parent = parent_die;
18297 part_die->die_sibling = NULL;
18298 part_die->die_child = NULL;
18299
18300 if (last_die && last_die == parent_die)
18301 last_die->die_child = part_die;
18302 else if (last_die)
18303 last_die->die_sibling = part_die;
18304
18305 last_die = part_die;
18306
18307 if (first_die == NULL)
18308 first_die = part_die;
18309
18310 /* Maybe add the DIE to the hash table. Not all DIEs that we
18311 find interesting need to be in the hash table, because we
18312 also have the parent/sibling/child chains; only those that we
18313 might refer to by offset later during partial symbol reading.
18314
18315 For now this means things that might have be the target of a
18316 DW_AT_specification, DW_AT_abstract_origin, or
18317 DW_AT_extension. DW_AT_extension will refer only to
18318 namespaces; DW_AT_abstract_origin refers to functions (and
18319 many things under the function DIE, but we do not recurse
18320 into function DIEs during partial symbol reading) and
18321 possibly variables as well; DW_AT_specification refers to
18322 declarations. Declarations ought to have the DW_AT_declaration
18323 flag. It happens that GCC forgets to put it in sometimes, but
18324 only for functions, not for types.
18325
18326 Adding more things than necessary to the hash table is harmless
18327 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18328 wasted time in find_partial_die, when we reread the compilation
18329 unit with load_all_dies set. */
72bf9492 18330
5afb4e99 18331 if (load_all
72929c62 18332 || abbrev->tag == DW_TAG_constant
5afb4e99 18333 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18334 || abbrev->tag == DW_TAG_variable
18335 || abbrev->tag == DW_TAG_namespace
18336 || part_die->is_declaration)
18337 {
18338 void **slot;
18339
18340 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18341 to_underlying (part_die->sect_off),
18342 INSERT);
72bf9492
DJ
18343 *slot = part_die;
18344 }
18345
72bf9492 18346 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18347 we have no reason to follow the children of structures; for other
98bfdba5
PA
18348 languages we have to, so that we can get at method physnames
18349 to infer fully qualified class names, for DW_AT_specification,
18350 and for C++ template arguments. For C++, we also look one level
18351 inside functions to find template arguments (if the name of the
18352 function does not already contain the template arguments).
bc30ff58 18353
0a4b0913
AB
18354 For Ada and Fortran, we need to scan the children of subprograms
18355 and lexical blocks as well because these languages allow the
18356 definition of nested entities that could be interesting for the
18357 debugger, such as nested subprograms for instance. */
72bf9492 18358 if (last_die->has_children
5afb4e99
DJ
18359 && (load_all
18360 || last_die->tag == DW_TAG_namespace
f55ee35c 18361 || last_die->tag == DW_TAG_module
72bf9492 18362 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18363 || (cu->language == language_cplus
18364 && last_die->tag == DW_TAG_subprogram
18365 && (last_die->name == NULL
18366 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18367 || (cu->language != language_c
18368 && (last_die->tag == DW_TAG_class_type
680b30c7 18369 || last_die->tag == DW_TAG_interface_type
72bf9492 18370 || last_die->tag == DW_TAG_structure_type
bc30ff58 18371 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18372 || ((cu->language == language_ada
18373 || cu->language == language_fortran)
bc30ff58
JB
18374 && (last_die->tag == DW_TAG_subprogram
18375 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18376 {
18377 nesting_level++;
18378 parent_die = last_die;
18379 continue;
18380 }
18381
18382 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18383 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18384
18385 /* Back to the top, do it again. */
18386 }
18387}
18388
6f06d47b
YQ
18389partial_die_info::partial_die_info (sect_offset sect_off_,
18390 struct abbrev_info *abbrev)
18391 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18392{
18393}
18394
35cc7ed7
YQ
18395/* Read a minimal amount of information into the minimal die structure.
18396 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18397
48fbe735
YQ
18398const gdb_byte *
18399partial_die_info::read (const struct die_reader_specs *reader,
18400 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18401{
dee91e82 18402 struct dwarf2_cu *cu = reader->cu;
5e22e966 18403 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18404 unsigned int i;
c5aa993b 18405 int has_low_pc_attr = 0;
c906108c 18406 int has_high_pc_attr = 0;
91da1414 18407 int high_pc_relative = 0;
c906108c 18408
fd0a254f 18409 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18410 {
e7da7f8f 18411 attribute attr;
18a8505e 18412 bool need_reprocess;
e7da7f8f 18413 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18414 info_ptr, &need_reprocess);
18415 /* String and address offsets that need to do the reprocessing have
18416 already been read at this point, so there is no need to wait until
18417 the loop terminates to do the reprocessing. */
18418 if (need_reprocess)
e7da7f8f 18419 read_attribute_reprocess (reader, &attr);
c906108c 18420 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18421 partial symbol table. */
c906108c
SS
18422 switch (attr.name)
18423 {
18424 case DW_AT_name:
48fbe735 18425 switch (tag)
71c25dea
TT
18426 {
18427 case DW_TAG_compile_unit:
95554aad 18428 case DW_TAG_partial_unit:
348e048f 18429 case DW_TAG_type_unit:
71c25dea
TT
18430 /* Compilation units have a DW_AT_name that is a filename, not
18431 a source language identifier. */
18432 case DW_TAG_enumeration_type:
18433 case DW_TAG_enumerator:
18434 /* These tags always have simple identifiers already; no need
18435 to canonicalize them. */
48fbe735 18436 name = DW_STRING (&attr);
71c25dea
TT
18437 break;
18438 default:
48fbe735
YQ
18439 {
18440 struct objfile *objfile = dwarf2_per_objfile->objfile;
18441
18442 name
be1e3d3e 18443 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18444 }
71c25dea
TT
18445 break;
18446 }
c906108c 18447 break;
31ef98ae 18448 case DW_AT_linkage_name:
c906108c 18449 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18450 /* Note that both forms of linkage name might appear. We
18451 assume they will be the same, and we only store the last
18452 one we see. */
e61108c9 18453 linkage_name = attr.value_as_string ();
787de330
TT
18454 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18455 See https://github.com/rust-lang/rust/issues/32925. */
18456 if (cu->language == language_rust && linkage_name != NULL
18457 && strchr (linkage_name, '{') != NULL)
18458 linkage_name = NULL;
c906108c
SS
18459 break;
18460 case DW_AT_low_pc:
18461 has_low_pc_attr = 1;
cd6c91b4 18462 lowpc = attr.value_as_address ();
c906108c
SS
18463 break;
18464 case DW_AT_high_pc:
18465 has_high_pc_attr = 1;
cd6c91b4
TT
18466 highpc = attr.value_as_address ();
18467 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18468 high_pc_relative = 1;
c906108c
SS
18469 break;
18470 case DW_AT_location:
0963b4bd 18471 /* Support the .debug_loc offsets. */
4fc6c0d5 18472 if (attr.form_is_block ())
8e19ed76 18473 {
48fbe735 18474 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18475 }
cd6c91b4 18476 else if (attr.form_is_section_offset ())
8e19ed76 18477 {
4d3c2250 18478 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18479 }
18480 else
18481 {
4d3c2250
KB
18482 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18483 "partial symbol information");
8e19ed76 18484 }
c906108c 18485 break;
c906108c 18486 case DW_AT_external:
48fbe735 18487 is_external = DW_UNSND (&attr);
c906108c
SS
18488 break;
18489 case DW_AT_declaration:
48fbe735 18490 is_declaration = DW_UNSND (&attr);
c906108c
SS
18491 break;
18492 case DW_AT_type:
48fbe735 18493 has_type = 1;
c906108c
SS
18494 break;
18495 case DW_AT_abstract_origin:
18496 case DW_AT_specification:
72bf9492 18497 case DW_AT_extension:
48fbe735 18498 has_specification = 1;
0826b30a 18499 spec_offset = attr.get_ref_die_offset ();
48fbe735 18500 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18501 || cu->per_cu->is_dwz);
c906108c
SS
18502 break;
18503 case DW_AT_sibling:
18504 /* Ignore absolute siblings, they might point outside of
18505 the current compile unit. */
18506 if (attr.form == DW_FORM_ref_addr)
b98664d3 18507 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18508 else
b9502d3f 18509 {
48fbe735 18510 const gdb_byte *buffer = reader->buffer;
0826b30a 18511 sect_offset off = attr.get_ref_die_offset ();
9c541725 18512 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18513
18514 if (sibling_ptr < info_ptr)
b98664d3 18515 complaint (_("DW_AT_sibling points backwards"));
22869d73 18516 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18517 reader->die_section->overflow_complaint ();
b9502d3f 18518 else
48fbe735 18519 sibling = sibling_ptr;
b9502d3f 18520 }
c906108c 18521 break;
fa4028e9 18522 case DW_AT_byte_size:
48fbe735 18523 has_byte_size = 1;
fa4028e9 18524 break;
ff908ebf 18525 case DW_AT_const_value:
48fbe735 18526 has_const_value = 1;
ff908ebf 18527 break;
68511cec
CES
18528 case DW_AT_calling_convention:
18529 /* DWARF doesn't provide a way to identify a program's source-level
18530 entry point. DW_AT_calling_convention attributes are only meant
18531 to describe functions' calling conventions.
18532
18533 However, because it's a necessary piece of information in
0c1b455e
TT
18534 Fortran, and before DWARF 4 DW_CC_program was the only
18535 piece of debugging information whose definition refers to
18536 a 'main program' at all, several compilers marked Fortran
18537 main programs with DW_CC_program --- even when those
18538 functions use the standard calling conventions.
18539
18540 Although DWARF now specifies a way to provide this
18541 information, we support this practice for backward
18542 compatibility. */
68511cec 18543 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18544 && cu->language == language_fortran)
48fbe735 18545 main_subprogram = 1;
68511cec 18546 break;
481860b3
GB
18547 case DW_AT_inline:
18548 if (DW_UNSND (&attr) == DW_INL_inlined
18549 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18550 may_be_inlined = 1;
481860b3 18551 break;
95554aad
TT
18552
18553 case DW_AT_import:
48fbe735 18554 if (tag == DW_TAG_imported_unit)
36586728 18555 {
0826b30a 18556 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18557 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18558 || cu->per_cu->is_dwz);
18559 }
95554aad
TT
18560 break;
18561
0c1b455e 18562 case DW_AT_main_subprogram:
48fbe735 18563 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18564 break;
18565
05caa1d2
TT
18566 case DW_AT_ranges:
18567 {
18568 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18569 but that requires a full DIE, so instead we just
18570 reimplement it. */
18571 int need_ranges_base = tag != DW_TAG_compile_unit;
18572 unsigned int ranges_offset = (DW_UNSND (&attr)
18573 + (need_ranges_base
18574 ? cu->ranges_base
18575 : 0));
18576
18577 /* Value of the DW_AT_ranges attribute is the offset in the
18578 .debug_ranges section. */
18579 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18580 nullptr))
18581 has_pc_info = 1;
18582 }
18583 break;
18584
c906108c
SS
18585 default:
18586 break;
18587 }
18588 }
18589
10d06d82
TT
18590 /* For Ada, if both the name and the linkage name appear, we prefer
18591 the latter. This lets "catch exception" work better, regardless
18592 of the order in which the name and linkage name were emitted.
18593 Really, though, this is just a workaround for the fact that gdb
18594 doesn't store both the name and the linkage name. */
18595 if (cu->language == language_ada && linkage_name != nullptr)
18596 name = linkage_name;
18597
91da1414 18598 if (high_pc_relative)
48fbe735 18599 highpc += lowpc;
91da1414 18600
9373cf26
JK
18601 if (has_low_pc_attr && has_high_pc_attr)
18602 {
18603 /* When using the GNU linker, .gnu.linkonce. sections are used to
18604 eliminate duplicate copies of functions and vtables and such.
18605 The linker will arbitrarily choose one and discard the others.
18606 The AT_*_pc values for such functions refer to local labels in
18607 these sections. If the section from that file was discarded, the
18608 labels are not in the output, so the relocs get a value of 0.
18609 If this is a discarded function, mark the pc bounds as invalid,
18610 so that GDB will ignore it. */
5989a64e 18611 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18612 {
48fbe735 18613 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18614 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18615
b98664d3 18616 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18617 "for DIE at %s [in module %s]"),
48fbe735
YQ
18618 paddress (gdbarch, lowpc),
18619 sect_offset_str (sect_off),
9d8780f0 18620 objfile_name (objfile));
9373cf26
JK
18621 }
18622 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18623 else if (lowpc >= highpc)
9373cf26 18624 {
48fbe735 18625 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18626 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18627
b98664d3 18628 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18629 "for DIE at %s [in module %s]"),
48fbe735
YQ
18630 paddress (gdbarch, lowpc),
18631 paddress (gdbarch, highpc),
18632 sect_offset_str (sect_off),
9c541725 18633 objfile_name (objfile));
9373cf26
JK
18634 }
18635 else
48fbe735 18636 has_pc_info = 1;
9373cf26 18637 }
85cbf3d3 18638
c906108c
SS
18639 return info_ptr;
18640}
18641
72bf9492
DJ
18642/* Find a cached partial DIE at OFFSET in CU. */
18643
d590ff25
YQ
18644struct partial_die_info *
18645dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18646{
18647 struct partial_die_info *lookup_die = NULL;
6f06d47b 18648 struct partial_die_info part_die (sect_off);
72bf9492 18649
9a3c8263 18650 lookup_die = ((struct partial_die_info *)
d590ff25 18651 htab_find_with_hash (partial_dies, &part_die,
9c541725 18652 to_underlying (sect_off)));
72bf9492 18653
72bf9492
DJ
18654 return lookup_die;
18655}
18656
348e048f
DE
18657/* Find a partial DIE at OFFSET, which may or may not be in CU,
18658 except in the case of .debug_types DIEs which do not reference
18659 outside their CU (they do however referencing other types via
55f1336d 18660 DW_FORM_ref_sig8). */
72bf9492 18661
122cf0f2 18662static const struct cu_partial_die_info
9c541725 18663find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18664{
5e22e966 18665 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18666 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18667 struct dwarf2_per_cu_data *per_cu = NULL;
18668 struct partial_die_info *pd = NULL;
72bf9492 18669
36586728 18670 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18671 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18672 {
d590ff25 18673 pd = cu->find_partial_die (sect_off);
5afb4e99 18674 if (pd != NULL)
fb816e8b 18675 return { cu, pd };
0d99eb77
DE
18676 /* We missed recording what we needed.
18677 Load all dies and try again. */
18678 per_cu = cu->per_cu;
5afb4e99 18679 }
0d99eb77
DE
18680 else
18681 {
18682 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18683 if (cu->per_cu->is_debug_types)
0d99eb77 18684 {
9d8780f0
SM
18685 error (_("Dwarf Error: Type Unit at offset %s contains"
18686 " external reference to offset %s [in module %s].\n"),
18687 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18688 bfd_get_filename (objfile->obfd));
18689 }
9c541725 18690 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18691 dwarf2_per_objfile);
72bf9492 18692
0d99eb77 18693 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18694 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18695
0d99eb77 18696 per_cu->cu->last_used = 0;
d590ff25 18697 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18698 }
5afb4e99 18699
dee91e82
DE
18700 /* If we didn't find it, and not all dies have been loaded,
18701 load them all and try again. */
18702
5afb4e99
DJ
18703 if (pd == NULL && per_cu->load_all_dies == 0)
18704 {
5afb4e99 18705 per_cu->load_all_dies = 1;
fd820528
DE
18706
18707 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18708 THIS_CU->cu may already be in use. So we can't just free it and
18709 replace its DIEs with the ones we read in. Instead, we leave those
18710 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18711 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18712 set. */
ab432490 18713 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18714
d590ff25 18715 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18716 }
18717
18718 if (pd == NULL)
18719 internal_error (__FILE__, __LINE__,
9d8780f0 18720 _("could not find partial DIE %s "
3e43a32a 18721 "in cache [from module %s]\n"),
9d8780f0 18722 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18723 return { per_cu->cu, pd };
72bf9492
DJ
18724}
18725
abc72ce4
DE
18726/* See if we can figure out if the class lives in a namespace. We do
18727 this by looking for a member function; its demangled name will
18728 contain namespace info, if there is any. */
18729
18730static void
18731guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18732 struct dwarf2_cu *cu)
18733{
18734 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18735 what template types look like, because the demangler
18736 frequently doesn't give the same name as the debug info. We
18737 could fix this by only using the demangled name to get the
18738 prefix (but see comment in read_structure_type). */
18739
18740 struct partial_die_info *real_pdi;
18741 struct partial_die_info *child_pdi;
18742
18743 /* If this DIE (this DIE's specification, if any) has a parent, then
18744 we should not do this. We'll prepend the parent's fully qualified
18745 name when we create the partial symbol. */
18746
18747 real_pdi = struct_pdi;
18748 while (real_pdi->has_specification)
fb816e8b 18749 {
122cf0f2
AB
18750 auto res = find_partial_die (real_pdi->spec_offset,
18751 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18752 real_pdi = res.pdi;
18753 cu = res.cu;
18754 }
abc72ce4
DE
18755
18756 if (real_pdi->die_parent != NULL)
18757 return;
18758
18759 for (child_pdi = struct_pdi->die_child;
18760 child_pdi != NULL;
18761 child_pdi = child_pdi->die_sibling)
18762 {
18763 if (child_pdi->tag == DW_TAG_subprogram
18764 && child_pdi->linkage_name != NULL)
18765 {
43816ebc
TT
18766 gdb::unique_xmalloc_ptr<char> actual_class_name
18767 (language_class_name_from_physname (cu->language_defn,
18768 child_pdi->linkage_name));
abc72ce4
DE
18769 if (actual_class_name != NULL)
18770 {
5e22e966 18771 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18772 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18773 }
18774 break;
18775 }
18776 }
18777}
18778
25c11aca
TV
18779/* Return true if a DIE with TAG may have the DW_AT_const_value
18780 attribute. */
18781
18782static bool
18783can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18784{
18785 switch (tag)
18786 {
18787 case DW_TAG_constant:
18788 case DW_TAG_enumerator:
18789 case DW_TAG_formal_parameter:
18790 case DW_TAG_template_value_param:
18791 case DW_TAG_variable:
18792 return true;
18793 }
18794
18795 return false;
18796}
18797
52356b79
YQ
18798void
18799partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18800{
abc72ce4
DE
18801 /* Once we've fixed up a die, there's no point in doing so again.
18802 This also avoids a memory leak if we were to call
18803 guess_partial_die_structure_name multiple times. */
52356b79 18804 if (fixup_called)
abc72ce4
DE
18805 return;
18806
72bf9492
DJ
18807 /* If we found a reference attribute and the DIE has no name, try
18808 to find a name in the referred to DIE. */
18809
52356b79 18810 if (name == NULL && has_specification)
72bf9492
DJ
18811 {
18812 struct partial_die_info *spec_die;
72bf9492 18813
122cf0f2 18814 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18815 spec_die = res.pdi;
18816 cu = res.cu;
72bf9492 18817
52356b79 18818 spec_die->fixup (cu);
72bf9492
DJ
18819
18820 if (spec_die->name)
18821 {
52356b79 18822 name = spec_die->name;
72bf9492
DJ
18823
18824 /* Copy DW_AT_external attribute if it is set. */
18825 if (spec_die->is_external)
52356b79 18826 is_external = spec_die->is_external;
72bf9492
DJ
18827 }
18828 }
18829
25c11aca
TV
18830 if (!has_const_value && has_specification
18831 && can_have_DW_AT_const_value_p (tag))
18832 {
18833 struct partial_die_info *spec_die;
18834
18835 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18836 spec_die = res.pdi;
18837 cu = res.cu;
18838
18839 spec_die->fixup (cu);
18840
18841 if (spec_die->has_const_value)
18842 {
18843 /* Copy DW_AT_const_value attribute if it is set. */
18844 has_const_value = spec_die->has_const_value;
18845 }
18846 }
18847
72bf9492 18848 /* Set default names for some unnamed DIEs. */
72bf9492 18849
52356b79
YQ
18850 if (name == NULL && tag == DW_TAG_namespace)
18851 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18852
abc72ce4
DE
18853 /* If there is no parent die to provide a namespace, and there are
18854 children, see if we can determine the namespace from their linkage
122d1940 18855 name. */
abc72ce4 18856 if (cu->language == language_cplus
5e22e966 18857 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18858 && die_parent == NULL
18859 && has_children
18860 && (tag == DW_TAG_class_type
18861 || tag == DW_TAG_structure_type
18862 || tag == DW_TAG_union_type))
18863 guess_partial_die_structure_name (this, cu);
abc72ce4 18864
53832f31
TT
18865 /* GCC might emit a nameless struct or union that has a linkage
18866 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18867 if (name == NULL
18868 && (tag == DW_TAG_class_type
18869 || tag == DW_TAG_interface_type
18870 || tag == DW_TAG_structure_type
18871 || tag == DW_TAG_union_type)
18872 && linkage_name != NULL)
53832f31 18873 {
43816ebc
TT
18874 gdb::unique_xmalloc_ptr<char> demangled
18875 (gdb_demangle (linkage_name, DMGL_TYPES));
18876 if (demangled != nullptr)
53832f31 18877 {
96408a79
SA
18878 const char *base;
18879
18880 /* Strip any leading namespaces/classes, keep only the base name.
18881 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18882 base = strrchr (demangled.get (), ':');
18883 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18884 base++;
18885 else
43816ebc 18886 base = demangled.get ();
96408a79 18887
5e22e966 18888 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18889 name = objfile->intern (base);
53832f31
TT
18890 }
18891 }
18892
52356b79 18893 fixup_called = 1;
72bf9492
DJ
18894}
18895
41144253 18896/* Read the .debug_loclists header contents from the given SECTION in the
18897 HEADER. */
18898static void
18899read_loclist_header (struct loclist_header *header,
18900 struct dwarf2_section_info *section)
18901{
18902 unsigned int bytes_read;
18903 bfd *abfd = section->get_bfd_owner ();
18904 const gdb_byte *info_ptr = section->buffer;
18905 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18906 info_ptr += bytes_read;
18907 header->version = read_2_bytes (abfd, info_ptr);
18908 info_ptr += 2;
18909 header->addr_size = read_1_byte (abfd, info_ptr);
18910 info_ptr += 1;
18911 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18912 info_ptr += 1;
18913 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18914}
18915
18916/* Return the DW_AT_loclists_base value for the CU. */
18917static ULONGEST
18918lookup_loclist_base (struct dwarf2_cu *cu)
18919{
18920 /* For the .dwo unit, the loclist_base points to the first offset following
18921 the header. The header consists of the following entities-
18922 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18923 bit format)
18924 2. version (2 bytes)
18925 3. address size (1 byte)
18926 4. segment selector size (1 byte)
18927 5. offset entry count (4 bytes)
18928 These sizes are derived as per the DWARFv5 standard. */
18929 if (cu->dwo_unit != nullptr)
18930 {
18931 if (cu->header.initial_length_size == 4)
18932 return LOCLIST_HEADER_SIZE32;
18933 return LOCLIST_HEADER_SIZE64;
18934 }
18935 return cu->loclist_base;
18936}
18937
18938/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18939 array of offsets in the .debug_loclists section. */
18940static CORE_ADDR
18941read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18942{
5e22e966 18943 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18944 struct objfile *objfile = dwarf2_per_objfile->objfile;
18945 bfd *abfd = objfile->obfd;
18946 ULONGEST loclist_base = lookup_loclist_base (cu);
18947 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18948
18949 section->read (objfile);
18950 if (section->buffer == NULL)
18951 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18952 "section [in module %s]"), objfile_name (objfile));
18953 struct loclist_header header;
18954 read_loclist_header (&header, section);
18955 if (loclist_index >= header.offset_entry_count)
18956 complaint (_("DW_FORM_loclistx pointing outside of "
18957 ".debug_loclists offset array [in module %s]"),
18958 objfile_name (objfile));
18959 if (loclist_base + loclist_index * cu->header.offset_size
18960 >= section->size)
18961 complaint (_("DW_FORM_loclistx pointing outside of "
18962 ".debug_loclists section [in module %s]"),
18963 objfile_name (objfile));
18964 const gdb_byte *info_ptr
18965 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18966
18967 if (cu->header.offset_size == 4)
18968 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18969 else
18970 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18971}
18972
18a8505e
AT
18973/* Process the attributes that had to be skipped in the first round. These
18974 attributes are the ones that need str_offsets_base or addr_base attributes.
18975 They could not have been processed in the first round, because at the time
18976 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18977static void
18978read_attribute_reprocess (const struct die_reader_specs *reader,
18979 struct attribute *attr)
18a8505e
AT
18980{
18981 struct dwarf2_cu *cu = reader->cu;
18982 switch (attr->form)
18983 {
18984 case DW_FORM_addrx:
18985 case DW_FORM_GNU_addr_index:
18986 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18987 break;
41144253 18988 case DW_FORM_loclistx:
18989 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18990 break;
18a8505e
AT
18991 case DW_FORM_strx:
18992 case DW_FORM_strx1:
18993 case DW_FORM_strx2:
18994 case DW_FORM_strx3:
18995 case DW_FORM_strx4:
18996 case DW_FORM_GNU_str_index:
18997 {
18998 unsigned int str_index = DW_UNSND (attr);
18999 if (reader->dwo_file != NULL)
19000 {
19001 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19002 DW_STRING_IS_CANONICAL (attr) = 0;
19003 }
19004 else
19005 {
19006 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19007 DW_STRING_IS_CANONICAL (attr) = 0;
19008 }
19009 break;
19010 }
19011 default:
19012 gdb_assert_not_reached (_("Unexpected DWARF form."));
19013 }
19014}
19015
a8329558 19016/* Read an attribute value described by an attribute form. */
c906108c 19017
d521ce57 19018static const gdb_byte *
dee91e82
DE
19019read_attribute_value (const struct die_reader_specs *reader,
19020 struct attribute *attr, unsigned form,
18a8505e
AT
19021 LONGEST implicit_const, const gdb_byte *info_ptr,
19022 bool *need_reprocess)
c906108c 19023{
dee91e82 19024 struct dwarf2_cu *cu = reader->cu;
5e22e966 19025 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19026 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19027 bfd *abfd = reader->abfd;
e7c27a73 19028 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19029 unsigned int bytes_read;
19030 struct dwarf_block *blk;
18a8505e 19031 *need_reprocess = false;
c906108c 19032
aead7601 19033 attr->form = (enum dwarf_form) form;
a8329558 19034 switch (form)
c906108c 19035 {
c906108c 19036 case DW_FORM_ref_addr:
ae411497 19037 if (cu->header.version == 2)
c8a7a66f
TT
19038 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19039 &bytes_read);
ae411497 19040 else
8266302d
TT
19041 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19042 &bytes_read);
ae411497
TT
19043 info_ptr += bytes_read;
19044 break;
36586728 19045 case DW_FORM_GNU_ref_alt:
8266302d 19046 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19047 info_ptr += bytes_read;
19048 break;
ae411497 19049 case DW_FORM_addr:
08feed99
TT
19050 {
19051 struct gdbarch *gdbarch = objfile->arch ();
19052 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19053 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19054 info_ptr += bytes_read;
19055 }
c906108c
SS
19056 break;
19057 case DW_FORM_block2:
7b5a2f43 19058 blk = dwarf_alloc_block (cu);
c906108c
SS
19059 blk->size = read_2_bytes (abfd, info_ptr);
19060 info_ptr += 2;
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_block4:
7b5a2f43 19066 blk = dwarf_alloc_block (cu);
c906108c
SS
19067 blk->size = read_4_bytes (abfd, info_ptr);
19068 info_ptr += 4;
19069 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19070 info_ptr += blk->size;
19071 DW_BLOCK (attr) = blk;
19072 break;
19073 case DW_FORM_data2:
19074 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19075 info_ptr += 2;
19076 break;
19077 case DW_FORM_data4:
19078 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19079 info_ptr += 4;
19080 break;
19081 case DW_FORM_data8:
19082 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19083 info_ptr += 8;
19084 break;
0224619f
JK
19085 case DW_FORM_data16:
19086 blk = dwarf_alloc_block (cu);
19087 blk->size = 16;
19088 blk->data = read_n_bytes (abfd, info_ptr, 16);
19089 info_ptr += 16;
19090 DW_BLOCK (attr) = blk;
19091 break;
2dc7f7b3 19092 case DW_FORM_sec_offset:
8266302d 19093 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19094 info_ptr += bytes_read;
19095 break;
41144253 19096 case DW_FORM_loclistx:
19097 {
19098 *need_reprocess = true;
19099 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19100 info_ptr += bytes_read;
19101 }
19102 break;
c906108c 19103 case DW_FORM_string:
9b1c24c8 19104 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19105 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19106 info_ptr += bytes_read;
19107 break;
4bdf3d34 19108 case DW_FORM_strp:
36586728
TT
19109 if (!cu->per_cu->is_dwz)
19110 {
ed2dc618
SM
19111 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19112 abfd, info_ptr, cu_header,
36586728
TT
19113 &bytes_read);
19114 DW_STRING_IS_CANONICAL (attr) = 0;
19115 info_ptr += bytes_read;
19116 break;
19117 }
19118 /* FALLTHROUGH */
43988095
JK
19119 case DW_FORM_line_strp:
19120 if (!cu->per_cu->is_dwz)
19121 {
86c0bb4c
TT
19122 DW_STRING (attr)
19123 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19124 &bytes_read);
43988095
JK
19125 DW_STRING_IS_CANONICAL (attr) = 0;
19126 info_ptr += bytes_read;
19127 break;
19128 }
19129 /* FALLTHROUGH */
36586728
TT
19130 case DW_FORM_GNU_strp_alt:
19131 {
c3699833 19132 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19133 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19134 &bytes_read);
36586728 19135
0314b390 19136 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19137 DW_STRING_IS_CANONICAL (attr) = 0;
19138 info_ptr += bytes_read;
19139 }
4bdf3d34 19140 break;
2dc7f7b3 19141 case DW_FORM_exprloc:
c906108c 19142 case DW_FORM_block:
7b5a2f43 19143 blk = dwarf_alloc_block (cu);
c906108c
SS
19144 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19145 info_ptr += bytes_read;
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_block1:
7b5a2f43 19151 blk = dwarf_alloc_block (cu);
c906108c
SS
19152 blk->size = read_1_byte (abfd, info_ptr);
19153 info_ptr += 1;
19154 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19155 info_ptr += blk->size;
19156 DW_BLOCK (attr) = blk;
19157 break;
19158 case DW_FORM_data1:
19159 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19160 info_ptr += 1;
19161 break;
19162 case DW_FORM_flag:
19163 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19164 info_ptr += 1;
19165 break;
2dc7f7b3
TT
19166 case DW_FORM_flag_present:
19167 DW_UNSND (attr) = 1;
19168 break;
c906108c
SS
19169 case DW_FORM_sdata:
19170 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19171 info_ptr += bytes_read;
19172 break;
19173 case DW_FORM_udata:
18a8505e 19174 case DW_FORM_rnglistx:
c906108c
SS
19175 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19176 info_ptr += bytes_read;
19177 break;
19178 case DW_FORM_ref1:
9c541725 19179 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19180 + read_1_byte (abfd, info_ptr));
c906108c
SS
19181 info_ptr += 1;
19182 break;
19183 case DW_FORM_ref2:
9c541725 19184 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19185 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19186 info_ptr += 2;
19187 break;
19188 case DW_FORM_ref4:
9c541725 19189 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19190 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19191 info_ptr += 4;
19192 break;
613e1657 19193 case DW_FORM_ref8:
9c541725 19194 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19195 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19196 info_ptr += 8;
19197 break;
55f1336d 19198 case DW_FORM_ref_sig8:
ac9ec31b 19199 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19200 info_ptr += 8;
19201 break;
c906108c 19202 case DW_FORM_ref_udata:
9c541725 19203 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19204 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19205 info_ptr += bytes_read;
19206 break;
c906108c 19207 case DW_FORM_indirect:
a8329558
KW
19208 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19209 info_ptr += bytes_read;
43988095
JK
19210 if (form == DW_FORM_implicit_const)
19211 {
19212 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19213 info_ptr += bytes_read;
19214 }
19215 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19216 info_ptr, need_reprocess);
43988095
JK
19217 break;
19218 case DW_FORM_implicit_const:
19219 DW_SND (attr) = implicit_const;
a8329558 19220 break;
336d760d 19221 case DW_FORM_addrx:
3019eac3 19222 case DW_FORM_GNU_addr_index:
18a8505e
AT
19223 *need_reprocess = true;
19224 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19225 info_ptr += bytes_read;
19226 break;
cf532bd1 19227 case DW_FORM_strx:
15f18d14
AT
19228 case DW_FORM_strx1:
19229 case DW_FORM_strx2:
19230 case DW_FORM_strx3:
19231 case DW_FORM_strx4:
3019eac3 19232 case DW_FORM_GNU_str_index:
3019eac3 19233 {
15f18d14
AT
19234 ULONGEST str_index;
19235 if (form == DW_FORM_strx1)
19236 {
19237 str_index = read_1_byte (abfd, info_ptr);
19238 info_ptr += 1;
19239 }
19240 else if (form == DW_FORM_strx2)
19241 {
19242 str_index = read_2_bytes (abfd, info_ptr);
19243 info_ptr += 2;
19244 }
19245 else if (form == DW_FORM_strx3)
19246 {
19247 str_index = read_3_bytes (abfd, info_ptr);
19248 info_ptr += 3;
19249 }
19250 else if (form == DW_FORM_strx4)
19251 {
19252 str_index = read_4_bytes (abfd, info_ptr);
19253 info_ptr += 4;
19254 }
19255 else
19256 {
19257 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19258 info_ptr += bytes_read;
19259 }
18a8505e
AT
19260 *need_reprocess = true;
19261 DW_UNSND (attr) = str_index;
19262 }
3019eac3 19263 break;
c906108c 19264 default:
8a3fe4f8 19265 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19266 dwarf_form_name (form),
19267 bfd_get_filename (abfd));
c906108c 19268 }
28e94949 19269
36586728 19270 /* Super hack. */
cd6c91b4 19271 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19272 attr->form = DW_FORM_GNU_ref_alt;
19273
28e94949
JB
19274 /* We have seen instances where the compiler tried to emit a byte
19275 size attribute of -1 which ended up being encoded as an unsigned
19276 0xffffffff. Although 0xffffffff is technically a valid size value,
19277 an object of this size seems pretty unlikely so we can relatively
19278 safely treat these cases as if the size attribute was invalid and
19279 treat them as zero by default. */
19280 if (attr->name == DW_AT_byte_size
19281 && form == DW_FORM_data4
19282 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19283 {
19284 complaint
b98664d3 19285 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19286 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19287 DW_UNSND (attr) = 0;
19288 }
28e94949 19289
c906108c
SS
19290 return info_ptr;
19291}
19292
a8329558
KW
19293/* Read an attribute described by an abbreviated attribute. */
19294
d521ce57 19295static const gdb_byte *
dee91e82
DE
19296read_attribute (const struct die_reader_specs *reader,
19297 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19298 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19299{
19300 attr->name = abbrev->name;
43988095 19301 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19302 abbrev->implicit_const, info_ptr,
19303 need_reprocess);
a8329558
KW
19304}
19305
43988095
JK
19306/* Return pointer to string at .debug_str offset STR_OFFSET. */
19307
19308static const char *
ed2dc618 19309read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19310 LONGEST str_offset)
43988095 19311{
5989a64e
SM
19312 return dwarf2_per_objfile->per_bfd->str.read_string
19313 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19314}
19315
43988095
JK
19316/* Return pointer to string at .debug_str offset as read from BUF.
19317 BUF is assumed to be in a compilation unit described by CU_HEADER.
19318 Return *BYTES_READ_PTR count of bytes read from BUF. */
19319
d521ce57 19320static const char *
ed2dc618
SM
19321read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19322 const gdb_byte *buf,
cf2c3c16
TT
19323 const struct comp_unit_head *cu_header,
19324 unsigned int *bytes_read_ptr)
19325{
8266302d 19326 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19327
4f44ae6c 19328 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19329}
19330
86c0bb4c 19331/* See read.h. */
43988095 19332
86c0bb4c
TT
19333const char *
19334dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19335 const struct comp_unit_head *cu_header,
19336 unsigned int *bytes_read_ptr)
43988095 19337{
86c0bb4c 19338 bfd *abfd = objfile->obfd;
8266302d 19339 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19340
5989a64e 19341 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19342}
19343
3019eac3 19344/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19345 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19346 ADDR_SIZE is the size of addresses from the CU header. */
19347
19348static CORE_ADDR
ed2dc618 19349read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19350 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19351 int addr_size)
3019eac3
DE
19352{
19353 struct objfile *objfile = dwarf2_per_objfile->objfile;
19354 bfd *abfd = objfile->obfd;
19355 const gdb_byte *info_ptr;
18a8505e 19356 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19357
5989a64e
SM
19358 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19359 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19360 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19361 objfile_name (objfile));
18a8505e 19362 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19363 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19364 error (_("DW_FORM_addr_index pointing outside of "
19365 ".debug_addr section [in module %s]"),
4262abfb 19366 objfile_name (objfile));
5989a64e 19367 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19368 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19369 if (addr_size == 4)
19370 return bfd_get_32 (abfd, info_ptr);
19371 else
19372 return bfd_get_64 (abfd, info_ptr);
19373}
19374
19375/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19376
19377static CORE_ADDR
19378read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19379{
5e22e966 19380 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19381 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19382}
19383
19384/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19385
19386static CORE_ADDR
d521ce57 19387read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19388 unsigned int *bytes_read)
19389{
5e22e966 19390 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19391 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19392
19393 return read_addr_index (cu, addr_index);
19394}
19395
450a1bfc 19396/* See read.h. */
3019eac3
DE
19397
19398CORE_ADDR
450a1bfc 19399dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19400{
ed2dc618 19401 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19402 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19403 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19404 int addr_size;
19405
3019eac3
DE
19406 /* We need addr_base and addr_size.
19407 If we don't have PER_CU->cu, we have to get it.
19408 Nasty, but the alternative is storing the needed info in PER_CU,
19409 which at this point doesn't seem justified: it's not clear how frequently
19410 it would get used and it would increase the size of every PER_CU.
19411 Entry points like dwarf2_per_cu_addr_size do a similar thing
19412 so we're not in uncharted territory here.
19413 Alas we need to be a bit more complicated as addr_base is contained
19414 in the DIE.
19415
19416 We don't need to read the entire CU(/TU).
19417 We just need the header and top level die.
a1b64ce1 19418
3019eac3 19419 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19420 For now we skip this optimization. */
3019eac3
DE
19421
19422 if (cu != NULL)
19423 {
19424 addr_base = cu->addr_base;
19425 addr_size = cu->header.addr_size;
19426 }
19427 else
19428 {
ab432490 19429 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19430 addr_base = reader.cu->addr_base;
19431 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19432 }
19433
ed2dc618
SM
19434 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19435 addr_size);
3019eac3
DE
19436}
19437
18a8505e
AT
19438/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19439 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19440 DWO file. */
3019eac3 19441
d521ce57 19442static const char *
18a8505e
AT
19443read_str_index (struct dwarf2_cu *cu,
19444 struct dwarf2_section_info *str_section,
19445 struct dwarf2_section_info *str_offsets_section,
19446 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19447{
5e22e966 19448 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19449 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19450 const char *objf_name = objfile_name (objfile);
3019eac3 19451 bfd *abfd = objfile->obfd;
d521ce57 19452 const gdb_byte *info_ptr;
3019eac3 19453 ULONGEST str_offset;
cf532bd1 19454 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19455
96b79293
TT
19456 str_section->read (objfile);
19457 str_offsets_section->read (objfile);
73869dc2 19458 if (str_section->buffer == NULL)
18a8505e 19459 error (_("%s used without %s section"
9d8780f0 19460 " in CU at offset %s [in module %s]"),
96b79293 19461 form_name, str_section->get_name (),
18a8505e 19462 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19463 if (str_offsets_section->buffer == NULL)
18a8505e 19464 error (_("%s used without %s section"
9d8780f0 19465 " in CU at offset %s [in module %s]"),
96b79293 19466 form_name, str_section->get_name (),
18a8505e 19467 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19468 info_ptr = (str_offsets_section->buffer
18a8505e 19469 + str_offsets_base
3019eac3
DE
19470 + str_index * cu->header.offset_size);
19471 if (cu->header.offset_size == 4)
19472 str_offset = bfd_get_32 (abfd, info_ptr);
19473 else
19474 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19475 if (str_offset >= str_section->size)
57d63ce2 19476 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19477 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19478 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19479 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19480}
19481
18a8505e
AT
19482/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19483
19484static const char *
19485read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19486{
19487 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19488 ? reader->cu->header.addr_size : 0;
19489 return read_str_index (reader->cu,
19490 &reader->dwo_file->sections.str,
19491 &reader->dwo_file->sections.str_offsets,
19492 str_offsets_base, str_index);
19493}
19494
19495/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19496
19497static const char *
19498read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19499{
5e22e966 19500 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19501 const char *objf_name = objfile_name (objfile);
19502 static const char form_name[] = "DW_FORM_GNU_str_index";
19503 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19504
19505 if (!cu->str_offsets_base.has_value ())
19506 error (_("%s used in Fission stub without %s"
19507 " in CU at offset 0x%lx [in module %s]"),
19508 form_name, str_offsets_attr_name,
19509 (long) cu->header.offset_size, objf_name);
19510
19511 return read_str_index (cu,
5e22e966
SM
19512 &cu->per_objfile->per_bfd->str,
19513 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19514 *cu->str_offsets_base, str_index);
19515}
19516
3019eac3
DE
19517/* Return the length of an LEB128 number in BUF. */
19518
19519static int
19520leb128_size (const gdb_byte *buf)
19521{
19522 const gdb_byte *begin = buf;
19523 gdb_byte byte;
19524
19525 while (1)
19526 {
19527 byte = *buf++;
19528 if ((byte & 128) == 0)
19529 return buf - begin;
19530 }
19531}
19532
c906108c 19533static void
e142c38c 19534set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19535{
19536 switch (lang)
19537 {
19538 case DW_LANG_C89:
76bee0cc 19539 case DW_LANG_C99:
0cfd832f 19540 case DW_LANG_C11:
c906108c 19541 case DW_LANG_C:
d1be3247 19542 case DW_LANG_UPC:
e142c38c 19543 cu->language = language_c;
c906108c 19544 break;
9c37b5ae 19545 case DW_LANG_Java:
c906108c 19546 case DW_LANG_C_plus_plus:
0cfd832f
MW
19547 case DW_LANG_C_plus_plus_11:
19548 case DW_LANG_C_plus_plus_14:
e142c38c 19549 cu->language = language_cplus;
c906108c 19550 break;
6aecb9c2
JB
19551 case DW_LANG_D:
19552 cu->language = language_d;
19553 break;
c906108c
SS
19554 case DW_LANG_Fortran77:
19555 case DW_LANG_Fortran90:
b21b22e0 19556 case DW_LANG_Fortran95:
f7de9aab
MW
19557 case DW_LANG_Fortran03:
19558 case DW_LANG_Fortran08:
e142c38c 19559 cu->language = language_fortran;
c906108c 19560 break;
a766d390
DE
19561 case DW_LANG_Go:
19562 cu->language = language_go;
19563 break;
c906108c 19564 case DW_LANG_Mips_Assembler:
e142c38c 19565 cu->language = language_asm;
c906108c
SS
19566 break;
19567 case DW_LANG_Ada83:
8aaf0b47 19568 case DW_LANG_Ada95:
bc5f45f8
JB
19569 cu->language = language_ada;
19570 break;
72019c9c
GM
19571 case DW_LANG_Modula2:
19572 cu->language = language_m2;
19573 break;
fe8e67fd
PM
19574 case DW_LANG_Pascal83:
19575 cu->language = language_pascal;
19576 break;
22566fbd
DJ
19577 case DW_LANG_ObjC:
19578 cu->language = language_objc;
19579 break;
c44af4eb
TT
19580 case DW_LANG_Rust:
19581 case DW_LANG_Rust_old:
19582 cu->language = language_rust;
19583 break;
c906108c
SS
19584 case DW_LANG_Cobol74:
19585 case DW_LANG_Cobol85:
c906108c 19586 default:
e142c38c 19587 cu->language = language_minimal;
c906108c
SS
19588 break;
19589 }
e142c38c 19590 cu->language_defn = language_def (cu->language);
c906108c
SS
19591}
19592
19593/* Return the named attribute or NULL if not there. */
19594
19595static struct attribute *
e142c38c 19596dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19597{
a48e046c 19598 for (;;)
c906108c 19599 {
a48e046c
TT
19600 unsigned int i;
19601 struct attribute *spec = NULL;
19602
19603 for (i = 0; i < die->num_attrs; ++i)
19604 {
19605 if (die->attrs[i].name == name)
19606 return &die->attrs[i];
19607 if (die->attrs[i].name == DW_AT_specification
19608 || die->attrs[i].name == DW_AT_abstract_origin)
19609 spec = &die->attrs[i];
19610 }
19611
19612 if (!spec)
19613 break;
c906108c 19614
f2f0e013 19615 die = follow_die_ref (die, spec, &cu);
f2f0e013 19616 }
c5aa993b 19617
c906108c
SS
19618 return NULL;
19619}
19620
7d45c7c3
KB
19621/* Return the string associated with a string-typed attribute, or NULL if it
19622 is either not found or is of an incorrect type. */
19623
19624static const char *
19625dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19626{
19627 struct attribute *attr;
19628 const char *str = NULL;
19629
19630 attr = dwarf2_attr (die, name, cu);
19631
19632 if (attr != NULL)
19633 {
e61108c9
TT
19634 str = attr->value_as_string ();
19635 if (str == nullptr)
b98664d3 19636 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19637 "DIE at %s in module %s"),
19638 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19639 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19640 }
19641
19642 return str;
19643}
19644
a084a2a6 19645/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19646 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19647static const char *
19648dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19649{
19650 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19651 if (dwo_name == nullptr)
19652 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19653 return dwo_name;
19654}
19655
05cf31d1
JB
19656/* Return non-zero iff the attribute NAME is defined for the given DIE,
19657 and holds a non-zero value. This function should only be used for
2dc7f7b3 19658 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19659
19660static int
19661dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19662{
19663 struct attribute *attr = dwarf2_attr (die, name, cu);
19664
19665 return (attr && DW_UNSND (attr));
19666}
19667
3ca72b44 19668static int
e142c38c 19669die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19670{
05cf31d1
JB
19671 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19672 which value is non-zero. However, we have to be careful with
19673 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19674 (via dwarf2_flag_true_p) follows this attribute. So we may
19675 end up accidently finding a declaration attribute that belongs
19676 to a different DIE referenced by the specification attribute,
19677 even though the given DIE does not have a declaration attribute. */
19678 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19679 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19680}
19681
63d06c5c 19682/* Return the die giving the specification for DIE, if there is
f2f0e013 19683 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19684 containing the return value on output. If there is no
19685 specification, but there is an abstract origin, that is
19686 returned. */
63d06c5c
DC
19687
19688static struct die_info *
f2f0e013 19689die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19690{
f2f0e013
DJ
19691 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19692 *spec_cu);
63d06c5c 19693
edb3359d
DJ
19694 if (spec_attr == NULL)
19695 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19696
63d06c5c
DC
19697 if (spec_attr == NULL)
19698 return NULL;
19699 else
f2f0e013 19700 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19701}
c906108c 19702
527f3840
JK
19703/* Stub for free_line_header to match void * callback types. */
19704
19705static void
19706free_line_header_voidp (void *arg)
19707{
9a3c8263 19708 struct line_header *lh = (struct line_header *) arg;
527f3840 19709
fff8551c 19710 delete lh;
527f3840
JK
19711}
19712
83769d0b 19713/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19714
19715static struct dwarf2_section_info *
19716get_debug_line_section (struct dwarf2_cu *cu)
19717{
19718 struct dwarf2_section_info *section;
5e22e966 19719 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19720
19721 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19722 DWO file. */
19723 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19724 section = &cu->dwo_unit->dwo_file->sections.line;
19725 else if (cu->per_cu->is_dwz)
19726 {
c3699833 19727 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19728
19729 section = &dwz->line;
19730 }
19731 else
5989a64e 19732 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19733
19734 return section;
19735}
19736
debd256d 19737/* Read the statement program header starting at OFFSET in
3019eac3 19738 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19739 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19740 Returns NULL if there is a problem reading the header, e.g., if it
19741 has a version we don't understand.
debd256d
JB
19742
19743 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19744 the returned object point into the dwarf line section buffer,
19745 and must not be freed. */
ae2de4f8 19746
fff8551c 19747static line_header_up
9c541725 19748dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19749{
3019eac3 19750 struct dwarf2_section_info *section;
5e22e966 19751 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19752
36586728 19753 section = get_debug_line_section (cu);
96b79293 19754 section->read (dwarf2_per_objfile->objfile);
3019eac3 19755 if (section->buffer == NULL)
debd256d 19756 {
3019eac3 19757 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19758 complaint (_("missing .debug_line.dwo section"));
3019eac3 19759 else
b98664d3 19760 complaint (_("missing .debug_line section"));
debd256d
JB
19761 return 0;
19762 }
19763
0df7ad3a
TT
19764 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19765 dwarf2_per_objfile, section,
19766 &cu->header);
debd256d 19767}
c906108c 19768
c6da4cef 19769/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19770 Return the file name of the psymtab for the given file_entry.
c6da4cef 19771 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19772 If space for the result is malloc'd, *NAME_HOLDER will be set.
19773 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19774
d521ce57 19775static const char *
7ba99d21 19776psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19777 const dwarf2_psymtab *pst,
c89b44cd
TT
19778 const char *comp_dir,
19779 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19780{
d521ce57
TT
19781 const char *include_name = fe.name;
19782 const char *include_name_to_compare = include_name;
72b9f47f 19783 const char *pst_filename;
c6da4cef
DE
19784 int file_is_pst;
19785
8c43009f 19786 const char *dir_name = fe.include_dir (lh);
c6da4cef 19787
c89b44cd 19788 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19789 if (!IS_ABSOLUTE_PATH (include_name)
19790 && (dir_name != NULL || comp_dir != NULL))
19791 {
19792 /* Avoid creating a duplicate psymtab for PST.
19793 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19794 Before we do the comparison, however, we need to account
19795 for DIR_NAME and COMP_DIR.
19796 First prepend dir_name (if non-NULL). If we still don't
19797 have an absolute path prepend comp_dir (if non-NULL).
19798 However, the directory we record in the include-file's
19799 psymtab does not contain COMP_DIR (to match the
19800 corresponding symtab(s)).
19801
19802 Example:
19803
19804 bash$ cd /tmp
19805 bash$ gcc -g ./hello.c
19806 include_name = "hello.c"
19807 dir_name = "."
19808 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19809 DW_AT_name = "./hello.c"
19810
19811 */
c6da4cef
DE
19812
19813 if (dir_name != NULL)
19814 {
c89b44cd
TT
19815 name_holder->reset (concat (dir_name, SLASH_STRING,
19816 include_name, (char *) NULL));
19817 include_name = name_holder->get ();
c6da4cef 19818 include_name_to_compare = include_name;
c6da4cef
DE
19819 }
19820 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19821 {
c89b44cd
TT
19822 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19823 include_name, (char *) NULL));
19824 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19825 }
19826 }
19827
19828 pst_filename = pst->filename;
c89b44cd 19829 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19830 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19831 {
c89b44cd
TT
19832 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19833 pst_filename, (char *) NULL));
19834 pst_filename = copied_name.get ();
c6da4cef
DE
19835 }
19836
1e3fad37 19837 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19838
c6da4cef
DE
19839 if (file_is_pst)
19840 return NULL;
19841 return include_name;
19842}
19843
d9b3de22
DE
19844/* State machine to track the state of the line number program. */
19845
6f77053d 19846class lnp_state_machine
d9b3de22 19847{
6f77053d
PA
19848public:
19849 /* Initialize a machine state for the start of a line number
19850 program. */
804d2729
TT
19851 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19852 bool record_lines_p);
6f77053d 19853
8c43009f
PA
19854 file_entry *current_file ()
19855 {
19856 /* lh->file_names is 0-based, but the file name numbers in the
19857 statement program are 1-based. */
6f77053d
PA
19858 return m_line_header->file_name_at (m_file);
19859 }
19860
19861 /* Record the line in the state machine. END_SEQUENCE is true if
19862 we're processing the end of a sequence. */
19863 void record_line (bool end_sequence);
19864
7ab6656f
OJ
19865 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19866 nop-out rest of the lines in this sequence. */
6f77053d
PA
19867 void check_line_address (struct dwarf2_cu *cu,
19868 const gdb_byte *line_ptr,
7ab6656f 19869 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19870
19871 void handle_set_discriminator (unsigned int discriminator)
19872 {
19873 m_discriminator = discriminator;
19874 m_line_has_non_zero_discriminator |= discriminator != 0;
19875 }
19876
19877 /* Handle DW_LNE_set_address. */
19878 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19879 {
19880 m_op_index = 0;
19881 address += baseaddr;
19882 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19883 }
19884
19885 /* Handle DW_LNS_advance_pc. */
19886 void handle_advance_pc (CORE_ADDR adjust);
19887
19888 /* Handle a special opcode. */
19889 void handle_special_opcode (unsigned char op_code);
19890
19891 /* Handle DW_LNS_advance_line. */
19892 void handle_advance_line (int line_delta)
19893 {
19894 advance_line (line_delta);
19895 }
19896
19897 /* Handle DW_LNS_set_file. */
19898 void handle_set_file (file_name_index file);
19899
19900 /* Handle DW_LNS_negate_stmt. */
19901 void handle_negate_stmt ()
19902 {
19903 m_is_stmt = !m_is_stmt;
19904 }
19905
19906 /* Handle DW_LNS_const_add_pc. */
19907 void handle_const_add_pc ();
19908
19909 /* Handle DW_LNS_fixed_advance_pc. */
19910 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19911 {
19912 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19913 m_op_index = 0;
19914 }
19915
19916 /* Handle DW_LNS_copy. */
19917 void handle_copy ()
19918 {
19919 record_line (false);
19920 m_discriminator = 0;
19921 }
19922
19923 /* Handle DW_LNE_end_sequence. */
19924 void handle_end_sequence ()
19925 {
804d2729 19926 m_currently_recording_lines = true;
6f77053d
PA
19927 }
19928
19929private:
19930 /* Advance the line by LINE_DELTA. */
19931 void advance_line (int line_delta)
19932 {
19933 m_line += line_delta;
19934
19935 if (line_delta != 0)
19936 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19937 }
19938
804d2729
TT
19939 struct dwarf2_cu *m_cu;
19940
6f77053d
PA
19941 gdbarch *m_gdbarch;
19942
19943 /* True if we're recording lines.
19944 Otherwise we're building partial symtabs and are just interested in
19945 finding include files mentioned by the line number program. */
19946 bool m_record_lines_p;
19947
8c43009f 19948 /* The line number header. */
6f77053d 19949 line_header *m_line_header;
8c43009f 19950
6f77053d
PA
19951 /* These are part of the standard DWARF line number state machine,
19952 and initialized according to the DWARF spec. */
d9b3de22 19953
6f77053d 19954 unsigned char m_op_index = 0;
7ba99d21
AT
19955 /* The line table index of the current file. */
19956 file_name_index m_file = 1;
6f77053d
PA
19957 unsigned int m_line = 1;
19958
19959 /* These are initialized in the constructor. */
19960
19961 CORE_ADDR m_address;
19962 bool m_is_stmt;
19963 unsigned int m_discriminator;
d9b3de22
DE
19964
19965 /* Additional bits of state we need to track. */
19966
19967 /* The last file that we called dwarf2_start_subfile for.
19968 This is only used for TLLs. */
6f77053d 19969 unsigned int m_last_file = 0;
d9b3de22 19970 /* The last file a line number was recorded for. */
6f77053d 19971 struct subfile *m_last_subfile = NULL;
d9b3de22 19972
804d2729
TT
19973 /* When true, record the lines we decode. */
19974 bool m_currently_recording_lines = false;
d9b3de22
DE
19975
19976 /* The last line number that was recorded, used to coalesce
19977 consecutive entries for the same line. This can happen, for
19978 example, when discriminators are present. PR 17276. */
6f77053d
PA
19979 unsigned int m_last_line = 0;
19980 bool m_line_has_non_zero_discriminator = false;
8c43009f 19981};
d9b3de22 19982
6f77053d
PA
19983void
19984lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19985{
19986 CORE_ADDR addr_adj = (((m_op_index + adjust)
19987 / m_line_header->maximum_ops_per_instruction)
19988 * m_line_header->minimum_instruction_length);
19989 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19990 m_op_index = ((m_op_index + adjust)
19991 % m_line_header->maximum_ops_per_instruction);
19992}
d9b3de22 19993
6f77053d
PA
19994void
19995lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19996{
6f77053d 19997 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19998 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19999 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20000 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20001 / m_line_header->maximum_ops_per_instruction)
20002 * m_line_header->minimum_instruction_length);
20003 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20004 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20005 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20006
258bf0ee 20007 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20008 advance_line (line_delta);
20009 record_line (false);
20010 m_discriminator = 0;
20011}
d9b3de22 20012
6f77053d
PA
20013void
20014lnp_state_machine::handle_set_file (file_name_index file)
20015{
20016 m_file = file;
20017
20018 const file_entry *fe = current_file ();
20019 if (fe == NULL)
20020 dwarf2_debug_line_missing_file_complaint ();
20021 else if (m_record_lines_p)
20022 {
20023 const char *dir = fe->include_dir (m_line_header);
20024
c24bdb02 20025 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20026 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20027 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20028 }
20029}
20030
20031void
20032lnp_state_machine::handle_const_add_pc ()
20033{
20034 CORE_ADDR adjust
20035 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20036
20037 CORE_ADDR addr_adj
20038 = (((m_op_index + adjust)
20039 / m_line_header->maximum_ops_per_instruction)
20040 * m_line_header->minimum_instruction_length);
20041
20042 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20043 m_op_index = ((m_op_index + adjust)
20044 % m_line_header->maximum_ops_per_instruction);
20045}
d9b3de22 20046
a05a36a5
DE
20047/* Return non-zero if we should add LINE to the line number table.
20048 LINE is the line to add, LAST_LINE is the last line that was added,
20049 LAST_SUBFILE is the subfile for LAST_LINE.
20050 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20051 had a non-zero discriminator.
20052
20053 We have to be careful in the presence of discriminators.
20054 E.g., for this line:
20055
20056 for (i = 0; i < 100000; i++);
20057
20058 clang can emit four line number entries for that one line,
20059 each with a different discriminator.
20060 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20061
20062 However, we want gdb to coalesce all four entries into one.
20063 Otherwise the user could stepi into the middle of the line and
20064 gdb would get confused about whether the pc really was in the
20065 middle of the line.
20066
20067 Things are further complicated by the fact that two consecutive
20068 line number entries for the same line is a heuristic used by gcc
20069 to denote the end of the prologue. So we can't just discard duplicate
20070 entries, we have to be selective about it. The heuristic we use is
20071 that we only collapse consecutive entries for the same line if at least
20072 one of those entries has a non-zero discriminator. PR 17276.
20073
20074 Note: Addresses in the line number state machine can never go backwards
20075 within one sequence, thus this coalescing is ok. */
20076
20077static int
804d2729
TT
20078dwarf_record_line_p (struct dwarf2_cu *cu,
20079 unsigned int line, unsigned int last_line,
a05a36a5
DE
20080 int line_has_non_zero_discriminator,
20081 struct subfile *last_subfile)
20082{
c24bdb02 20083 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20084 return 1;
20085 if (line != last_line)
20086 return 1;
20087 /* Same line for the same file that we've seen already.
20088 As a last check, for pr 17276, only record the line if the line
20089 has never had a non-zero discriminator. */
20090 if (!line_has_non_zero_discriminator)
20091 return 1;
20092 return 0;
20093}
20094
804d2729
TT
20095/* Use the CU's builder to record line number LINE beginning at
20096 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20097
20098static void
d9b3de22 20099dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20100 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20101 struct dwarf2_cu *cu)
252a6764
DE
20102{
20103 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20104
27e0867f
DE
20105 if (dwarf_line_debug)
20106 {
20107 fprintf_unfiltered (gdb_stdlog,
20108 "Recording line %u, file %s, address %s\n",
20109 line, lbasename (subfile->name),
20110 paddress (gdbarch, address));
20111 }
20112
804d2729 20113 if (cu != nullptr)
8c95582d 20114 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20115}
20116
20117/* Subroutine of dwarf_decode_lines_1 to simplify it.
20118 Mark the end of a set of line number records.
d9b3de22 20119 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20120 If SUBFILE is NULL the request is ignored. */
20121
20122static void
20123dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20124 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20125{
27e0867f
DE
20126 if (subfile == NULL)
20127 return;
20128
20129 if (dwarf_line_debug)
20130 {
20131 fprintf_unfiltered (gdb_stdlog,
20132 "Finishing current line, file %s, address %s\n",
20133 lbasename (subfile->name),
20134 paddress (gdbarch, address));
20135 }
20136
8c95582d 20137 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20138}
20139
6f77053d
PA
20140void
20141lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20142{
d9b3de22
DE
20143 if (dwarf_line_debug)
20144 {
20145 fprintf_unfiltered (gdb_stdlog,
20146 "Processing actual line %u: file %u,"
94a72be7 20147 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20148 m_line, m_file,
6f77053d 20149 paddress (m_gdbarch, m_address),
94a72be7
AB
20150 m_is_stmt, m_discriminator,
20151 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20152 }
20153
6f77053d 20154 file_entry *fe = current_file ();
8c43009f
PA
20155
20156 if (fe == NULL)
d9b3de22
DE
20157 dwarf2_debug_line_missing_file_complaint ();
20158 /* For now we ignore lines not starting on an instruction boundary.
20159 But not when processing end_sequence for compatibility with the
20160 previous version of the code. */
6f77053d 20161 else if (m_op_index == 0 || end_sequence)
d9b3de22 20162 {
8c43009f 20163 fe->included_p = 1;
8c95582d 20164 if (m_record_lines_p)
d9b3de22 20165 {
c24bdb02 20166 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20167 || end_sequence)
d9b3de22 20168 {
804d2729
TT
20169 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20170 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20171 }
20172
20173 if (!end_sequence)
20174 {
8c95582d
AB
20175 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20176
804d2729 20177 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20178 m_line_has_non_zero_discriminator,
20179 m_last_subfile))
d9b3de22 20180 {
c24bdb02 20181 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20182 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20183 builder->get_current_subfile (),
8c95582d 20184 m_line, m_address, is_stmt,
804d2729 20185 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20186 }
c24bdb02 20187 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20188 m_last_line = m_line;
d9b3de22
DE
20189 }
20190 }
20191 }
20192}
20193
804d2729
TT
20194lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20195 line_header *lh, bool record_lines_p)
d9b3de22 20196{
804d2729 20197 m_cu = cu;
6f77053d
PA
20198 m_gdbarch = arch;
20199 m_record_lines_p = record_lines_p;
20200 m_line_header = lh;
d9b3de22 20201
804d2729 20202 m_currently_recording_lines = true;
d9b3de22 20203
d9b3de22
DE
20204 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20205 was a line entry for it so that the backend has a chance to adjust it
20206 and also record it in case it needs it. This is currently used by MIPS
20207 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20208 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20209 m_is_stmt = lh->default_is_stmt;
20210 m_discriminator = 0;
252a6764
DE
20211}
20212
6f77053d
PA
20213void
20214lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20215 const gdb_byte *line_ptr,
7ab6656f 20216 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20217{
7ab6656f
OJ
20218 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20219 the pc range of the CU. However, we restrict the test to only ADDRESS
20220 values of zero to preserve GDB's previous behaviour which is to handle
20221 the specific case of a function being GC'd by the linker. */
924c2928 20222
7ab6656f 20223 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20224 {
20225 /* This line table is for a function which has been
20226 GCd by the linker. Ignore it. PR gdb/12528 */
20227
5e22e966 20228 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20229 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20230
b98664d3 20231 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20232 line_offset, objfile_name (objfile));
804d2729
TT
20233 m_currently_recording_lines = false;
20234 /* Note: m_currently_recording_lines is left as false until we see
20235 DW_LNE_end_sequence. */
924c2928
DE
20236 }
20237}
20238
f3f5162e 20239/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20240 Process the line number information in LH.
20241 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20242 program in order to set included_p for every referenced header. */
debd256d 20243
c906108c 20244static void
43f3e411
DE
20245dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20246 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20247{
d521ce57
TT
20248 const gdb_byte *line_ptr, *extended_end;
20249 const gdb_byte *line_end;
a8c50c1f 20250 unsigned int bytes_read, extended_len;
699ca60a 20251 unsigned char op_code, extended_op;
e142c38c 20252 CORE_ADDR baseaddr;
5e22e966 20253 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20254 bfd *abfd = objfile->obfd;
08feed99 20255 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20256 /* True if we're recording line info (as opposed to building partial
20257 symtabs and just interested in finding include files mentioned by
20258 the line number program). */
20259 bool record_lines_p = !decode_for_pst_p;
e142c38c 20260
b3b3bada 20261 baseaddr = objfile->text_section_offset ();
c906108c 20262
debd256d
JB
20263 line_ptr = lh->statement_program_start;
20264 line_end = lh->statement_program_end;
c906108c
SS
20265
20266 /* Read the statement sequences until there's nothing left. */
20267 while (line_ptr < line_end)
20268 {
6f77053d
PA
20269 /* The DWARF line number program state machine. Reset the state
20270 machine at the start of each sequence. */
804d2729 20271 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20272 bool end_sequence = false;
d9b3de22 20273
8c43009f 20274 if (record_lines_p)
c906108c 20275 {
8c43009f
PA
20276 /* Start a subfile for the current file of the state
20277 machine. */
20278 const file_entry *fe = state_machine.current_file ();
20279
20280 if (fe != NULL)
804d2729 20281 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20282 }
20283
a738430d 20284 /* Decode the table. */
d9b3de22 20285 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20286 {
20287 op_code = read_1_byte (abfd, line_ptr);
20288 line_ptr += 1;
9aa1fe7e 20289
debd256d 20290 if (op_code >= lh->opcode_base)
6e70227d 20291 {
8e07a239 20292 /* Special opcode. */
6f77053d 20293 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20294 }
20295 else switch (op_code)
c906108c
SS
20296 {
20297 case DW_LNS_extended_op:
3e43a32a
MS
20298 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20299 &bytes_read);
473b7be6 20300 line_ptr += bytes_read;
a8c50c1f 20301 extended_end = line_ptr + extended_len;
c906108c
SS
20302 extended_op = read_1_byte (abfd, line_ptr);
20303 line_ptr += 1;
20304 switch (extended_op)
20305 {
20306 case DW_LNE_end_sequence:
6f77053d
PA
20307 state_machine.handle_end_sequence ();
20308 end_sequence = true;
c906108c
SS
20309 break;
20310 case DW_LNE_set_address:
d9b3de22
DE
20311 {
20312 CORE_ADDR address
c8a7a66f 20313 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20314 line_ptr += bytes_read;
6f77053d
PA
20315
20316 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20317 lowpc - baseaddr, address);
6f77053d 20318 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20319 }
c906108c
SS
20320 break;
20321 case DW_LNE_define_file:
debd256d 20322 {
d521ce57 20323 const char *cur_file;
ecfb656c
PA
20324 unsigned int mod_time, length;
20325 dir_index dindex;
6e70227d 20326
3e43a32a
MS
20327 cur_file = read_direct_string (abfd, line_ptr,
20328 &bytes_read);
debd256d 20329 line_ptr += bytes_read;
ecfb656c 20330 dindex = (dir_index)
debd256d
JB
20331 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20332 line_ptr += bytes_read;
20333 mod_time =
20334 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20335 line_ptr += bytes_read;
20336 length =
20337 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20338 line_ptr += bytes_read;
ecfb656c 20339 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20340 }
c906108c 20341 break;
d0c6ba3d 20342 case DW_LNE_set_discriminator:
6f77053d
PA
20343 {
20344 /* The discriminator is not interesting to the
20345 debugger; just ignore it. We still need to
20346 check its value though:
20347 if there are consecutive entries for the same
20348 (non-prologue) line we want to coalesce them.
20349 PR 17276. */
20350 unsigned int discr
20351 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20352 line_ptr += bytes_read;
20353
20354 state_machine.handle_set_discriminator (discr);
20355 }
d0c6ba3d 20356 break;
c906108c 20357 default:
b98664d3 20358 complaint (_("mangled .debug_line section"));
debd256d 20359 return;
c906108c 20360 }
a8c50c1f
DJ
20361 /* Make sure that we parsed the extended op correctly. If e.g.
20362 we expected a different address size than the producer used,
20363 we may have read the wrong number of bytes. */
20364 if (line_ptr != extended_end)
20365 {
b98664d3 20366 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20367 return;
20368 }
c906108c
SS
20369 break;
20370 case DW_LNS_copy:
6f77053d 20371 state_machine.handle_copy ();
c906108c
SS
20372 break;
20373 case DW_LNS_advance_pc:
2dc7f7b3
TT
20374 {
20375 CORE_ADDR adjust
20376 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20377 line_ptr += bytes_read;
6f77053d
PA
20378
20379 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20380 }
c906108c
SS
20381 break;
20382 case DW_LNS_advance_line:
a05a36a5
DE
20383 {
20384 int line_delta
20385 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20386 line_ptr += bytes_read;
6f77053d
PA
20387
20388 state_machine.handle_advance_line (line_delta);
a05a36a5 20389 }
c906108c
SS
20390 break;
20391 case DW_LNS_set_file:
d9b3de22 20392 {
6f77053d 20393 file_name_index file
ecfb656c
PA
20394 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20395 &bytes_read);
d9b3de22 20396 line_ptr += bytes_read;
8c43009f 20397
6f77053d 20398 state_machine.handle_set_file (file);
d9b3de22 20399 }
c906108c
SS
20400 break;
20401 case DW_LNS_set_column:
0ad93d4f 20402 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20403 line_ptr += bytes_read;
20404 break;
20405 case DW_LNS_negate_stmt:
6f77053d 20406 state_machine.handle_negate_stmt ();
c906108c
SS
20407 break;
20408 case DW_LNS_set_basic_block:
c906108c 20409 break;
c2c6d25f
JM
20410 /* Add to the address register of the state machine the
20411 address increment value corresponding to special opcode
a738430d
MK
20412 255. I.e., this value is scaled by the minimum
20413 instruction length since special opcode 255 would have
b021a221 20414 scaled the increment. */
c906108c 20415 case DW_LNS_const_add_pc:
6f77053d 20416 state_machine.handle_const_add_pc ();
c906108c
SS
20417 break;
20418 case DW_LNS_fixed_advance_pc:
3e29f34a 20419 {
6f77053d 20420 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20421 line_ptr += 2;
6f77053d
PA
20422
20423 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20424 }
c906108c 20425 break;
9aa1fe7e 20426 default:
a738430d
MK
20427 {
20428 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20429 int i;
a738430d 20430
debd256d 20431 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20432 {
20433 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20434 line_ptr += bytes_read;
20435 }
20436 }
c906108c
SS
20437 }
20438 }
d9b3de22
DE
20439
20440 if (!end_sequence)
20441 dwarf2_debug_line_missing_end_sequence_complaint ();
20442
20443 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20444 in which case we still finish recording the last line). */
6f77053d 20445 state_machine.record_line (true);
c906108c 20446 }
f3f5162e
DE
20447}
20448
20449/* Decode the Line Number Program (LNP) for the given line_header
20450 structure and CU. The actual information extracted and the type
20451 of structures created from the LNP depends on the value of PST.
20452
20453 1. If PST is NULL, then this procedure uses the data from the program
20454 to create all necessary symbol tables, and their linetables.
20455
20456 2. If PST is not NULL, this procedure reads the program to determine
20457 the list of files included by the unit represented by PST, and
20458 builds all the associated partial symbol tables.
20459
20460 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20461 It is used for relative paths in the line table.
20462 NOTE: When processing partial symtabs (pst != NULL),
20463 comp_dir == pst->dirname.
20464
20465 NOTE: It is important that psymtabs have the same file name (via strcmp)
20466 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20467 symtab we don't use it in the name of the psymtabs we create.
20468 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20469 A good testcase for this is mb-inline.exp.
20470
527f3840
JK
20471 LOWPC is the lowest address in CU (or 0 if not known).
20472
20473 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20474 for its PC<->lines mapping information. Otherwise only the filename
20475 table is read in. */
f3f5162e
DE
20476
20477static void
20478dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20479 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20480 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20481{
5e22e966 20482 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20483 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20484
527f3840
JK
20485 if (decode_mapping)
20486 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20487
20488 if (decode_for_pst_p)
20489 {
aaa75496
JB
20490 /* Now that we're done scanning the Line Header Program, we can
20491 create the psymtab of each included file. */
7ba99d21
AT
20492 for (auto &file_entry : lh->file_names ())
20493 if (file_entry.included_p == 1)
aaa75496 20494 {
c89b44cd 20495 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20496 const char *include_name =
7ba99d21
AT
20497 psymtab_include_file_name (lh, file_entry, pst,
20498 comp_dir, &name_holder);
c6da4cef 20499 if (include_name != NULL)
aaa75496
JB
20500 dwarf2_create_include_psymtab (include_name, pst, objfile);
20501 }
20502 }
cb1df416
DJ
20503 else
20504 {
20505 /* Make sure a symtab is created for every file, even files
20506 which contain only variables (i.e. no code with associated
20507 line numbers). */
c24bdb02
KS
20508 buildsym_compunit *builder = cu->get_builder ();
20509 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20510
7ba99d21 20511 for (auto &fe : lh->file_names ())
cb1df416 20512 {
804d2729 20513 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20514 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20515 {
c24bdb02 20516 builder->get_current_subfile ()->symtab
804d2729 20517 = allocate_symtab (cust,
c24bdb02 20518 builder->get_current_subfile ()->name);
43f3e411 20519 }
c24bdb02 20520 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20521 }
20522 }
c906108c
SS
20523}
20524
20525/* Start a subfile for DWARF. FILENAME is the name of the file and
20526 DIRNAME the name of the source directory which contains FILENAME
4d663531 20527 or NULL if not known.
c906108c
SS
20528 This routine tries to keep line numbers from identical absolute and
20529 relative file names in a common subfile.
20530
20531 Using the `list' example from the GDB testsuite, which resides in
20532 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20533 of /srcdir/list0.c yields the following debugging information for list0.c:
20534
c5aa993b 20535 DW_AT_name: /srcdir/list0.c
4d663531 20536 DW_AT_comp_dir: /compdir
357e46e7 20537 files.files[0].name: list0.h
c5aa993b 20538 files.files[0].dir: /srcdir
357e46e7 20539 files.files[1].name: list0.c
c5aa993b 20540 files.files[1].dir: /srcdir
c906108c
SS
20541
20542 The line number information for list0.c has to end up in a single
4f1520fb
FR
20543 subfile, so that `break /srcdir/list0.c:1' works as expected.
20544 start_subfile will ensure that this happens provided that we pass the
20545 concatenation of files.files[1].dir and files.files[1].name as the
20546 subfile's name. */
c906108c
SS
20547
20548static void
804d2729
TT
20549dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20550 const char *dirname)
c906108c 20551{
43816ebc 20552 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20553
4d663531 20554 /* In order not to lose the line information directory,
4f1520fb
FR
20555 we concatenate it to the filename when it makes sense.
20556 Note that the Dwarf3 standard says (speaking of filenames in line
20557 information): ``The directory index is ignored for file names
20558 that represent full path names''. Thus ignoring dirname in the
20559 `else' branch below isn't an issue. */
c906108c 20560
d5166ae1 20561 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20562 {
43816ebc
TT
20563 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20564 filename = copy.get ();
d521ce57 20565 }
c906108c 20566
c24bdb02 20567 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20568}
20569
804d2729
TT
20570/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20571 buildsym_compunit constructor. */
f4dc4d17 20572
c24bdb02
KS
20573struct compunit_symtab *
20574dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20575 CORE_ADDR low_pc)
f4dc4d17 20576{
c24bdb02 20577 gdb_assert (m_builder == nullptr);
43f3e411 20578
c24bdb02
KS
20579 m_builder.reset (new struct buildsym_compunit
20580 (per_cu->dwarf2_per_objfile->objfile,
20581 name, comp_dir, language, low_pc));
93b8bea4 20582
c24bdb02 20583 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20584
c24bdb02
KS
20585 get_builder ()->record_debugformat ("DWARF 2");
20586 get_builder ()->record_producer (producer);
f4dc4d17 20587
c24bdb02 20588 processing_has_namespace_info = false;
43f3e411 20589
c24bdb02 20590 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20591}
20592
4c2df51b
DJ
20593static void
20594var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20595 struct dwarf2_cu *cu)
4c2df51b 20596{
5e22e966 20597 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20598 struct comp_unit_head *cu_header = &cu->header;
20599
4c2df51b
DJ
20600 /* NOTE drow/2003-01-30: There used to be a comment and some special
20601 code here to turn a symbol with DW_AT_external and a
20602 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20603 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20604 with some versions of binutils) where shared libraries could have
20605 relocations against symbols in their debug information - the
20606 minimal symbol would have the right address, but the debug info
20607 would not. It's no longer necessary, because we will explicitly
20608 apply relocations when we read in the debug information now. */
20609
20610 /* A DW_AT_location attribute with no contents indicates that a
20611 variable has been optimized away. */
4fc6c0d5 20612 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20613 {
f1e6e072 20614 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20615 return;
20616 }
20617
20618 /* Handle one degenerate form of location expression specially, to
20619 preserve GDB's previous behavior when section offsets are
336d760d
AT
20620 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20621 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20622
4fc6c0d5 20623 if (attr->form_is_block ()
3019eac3
DE
20624 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20625 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20626 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20627 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20628 && (DW_BLOCK (attr)->size
20629 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20630 {
891d2f0b 20631 unsigned int dummy;
4c2df51b 20632
3019eac3 20633 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20634 SET_SYMBOL_VALUE_ADDRESS
20635 (sym, cu->header.read_address (objfile->obfd,
20636 DW_BLOCK (attr)->data + 1,
20637 &dummy));
3019eac3 20638 else
38583298
TT
20639 SET_SYMBOL_VALUE_ADDRESS
20640 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20641 &dummy));
f1e6e072 20642 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20643 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20644 SET_SYMBOL_VALUE_ADDRESS
20645 (sym,
20646 SYMBOL_VALUE_ADDRESS (sym)
20647 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20648 return;
20649 }
20650
20651 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20652 expression evaluator, and use LOC_COMPUTED only when necessary
20653 (i.e. when the value of a register or memory location is
20654 referenced, or a thread-local block, etc.). Then again, it might
20655 not be worthwhile. I'm assuming that it isn't unless performance
20656 or memory numbers show me otherwise. */
20657
f1e6e072 20658 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20659
f1e6e072 20660 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20661 cu->has_loclist = true;
4c2df51b
DJ
20662}
20663
c906108c
SS
20664/* Given a pointer to a DWARF information entry, figure out if we need
20665 to make a symbol table entry for it, and if so, create a new entry
20666 and return a pointer to it.
20667 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20668 used the passed type.
20669 If SPACE is not NULL, use it to hold the new symbol. If it is
20670 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20671
20672static struct symbol *
5e2db402
TT
20673new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20674 struct symbol *space)
c906108c 20675{
5e22e966 20676 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20677 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20678 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20679 struct symbol *sym = NULL;
15d034d0 20680 const char *name;
c906108c
SS
20681 struct attribute *attr = NULL;
20682 struct attribute *attr2 = NULL;
e142c38c 20683 CORE_ADDR baseaddr;
e37fd15a
SW
20684 struct pending **list_to_add = NULL;
20685
edb3359d 20686 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20687
b3b3bada 20688 baseaddr = objfile->text_section_offset ();
c906108c 20689
94af9270 20690 name = dwarf2_name (die, cu);
c906108c
SS
20691 if (name)
20692 {
34eaf542 20693 int suppress_add = 0;
94af9270 20694
34eaf542
TT
20695 if (space)
20696 sym = space;
20697 else
8c14c3a3 20698 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20699 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20700
20701 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20702 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20703 /* Fortran does not have mangling standard and the mangling does differ
20704 between gfortran, iFort etc. */
bcfe6157
TT
20705 const char *physname
20706 = (cu->language == language_fortran
20707 ? dwarf2_full_name (name, die, cu)
20708 : dwarf2_physname (name, die, cu));
20709 const char *linkagename = dw2_linkage_name (die, cu);
20710
20711 if (linkagename == nullptr || cu->language == language_ada)
20712 sym->set_linkage_name (physname);
20713 else
20714 {
20715 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20716 sym->set_linkage_name (linkagename);
20717 }
f55ee35c 20718
c906108c 20719 /* Default assumptions.
c5aa993b 20720 Use the passed type or decode it from the die. */
176620f1 20721 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20722 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20723 if (type != NULL)
20724 SYMBOL_TYPE (sym) = type;
20725 else
e7c27a73 20726 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20727 attr = dwarf2_attr (die,
20728 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20729 cu);
435d3d88 20730 if (attr != nullptr)
c906108c
SS
20731 {
20732 SYMBOL_LINE (sym) = DW_UNSND (attr);
20733 }
cb1df416 20734
edb3359d
DJ
20735 attr = dwarf2_attr (die,
20736 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20737 cu);
435d3d88 20738 if (attr != nullptr)
cb1df416 20739 {
ecfb656c 20740 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20741 struct file_entry *fe;
9a619af0 20742
ecfb656c
PA
20743 if (cu->line_header != NULL)
20744 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20745 else
20746 fe = NULL;
20747
20748 if (fe == NULL)
b98664d3 20749 complaint (_("file index out of range"));
8c43009f
PA
20750 else
20751 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20752 }
20753
c906108c
SS
20754 switch (die->tag)
20755 {
20756 case DW_TAG_label:
e142c38c 20757 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20758 if (attr != nullptr)
3e29f34a
MR
20759 {
20760 CORE_ADDR addr;
20761
cd6c91b4 20762 addr = attr->value_as_address ();
3e29f34a 20763 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20764 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20765 }
0f5238ed
TT
20766 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20767 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20768 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20769 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20770 break;
20771 case DW_TAG_subprogram:
20772 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20773 finish_block. */
f1e6e072 20774 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20775 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20776 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20777 || cu->language == language_ada
20778 || cu->language == language_fortran)
c906108c 20779 {
2cfa0c8d 20780 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20781 Ada and Fortran subprograms, whether marked external or
20782 not, are always stored as a global symbol, because we want
20783 to be able to access them globally. For instance, we want
20784 to be able to break on a nested subprogram without having
20785 to specify the context. */
c24bdb02 20786 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20787 }
20788 else
20789 {
e37fd15a 20790 list_to_add = cu->list_in_scope;
c906108c
SS
20791 }
20792 break;
edb3359d
DJ
20793 case DW_TAG_inlined_subroutine:
20794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20795 finish_block. */
f1e6e072 20796 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20797 SYMBOL_INLINED (sym) = 1;
481860b3 20798 list_to_add = cu->list_in_scope;
edb3359d 20799 break;
34eaf542
TT
20800 case DW_TAG_template_value_param:
20801 suppress_add = 1;
20802 /* Fall through. */
72929c62 20803 case DW_TAG_constant:
c906108c 20804 case DW_TAG_variable:
254e6b9e 20805 case DW_TAG_member:
0963b4bd
MS
20806 /* Compilation with minimal debug info may result in
20807 variables with missing type entries. Change the
20808 misleading `void' type to something sensible. */
78134374 20809 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20810 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20811
e142c38c 20812 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20813 /* In the case of DW_TAG_member, we should only be called for
20814 static const members. */
20815 if (die->tag == DW_TAG_member)
20816 {
3863f96c
DE
20817 /* dwarf2_add_field uses die_is_declaration,
20818 so we do the same. */
254e6b9e
DE
20819 gdb_assert (die_is_declaration (die, cu));
20820 gdb_assert (attr);
20821 }
435d3d88 20822 if (attr != nullptr)
c906108c 20823 {
e7c27a73 20824 dwarf2_const_value (attr, sym, cu);
e142c38c 20825 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20826 if (!suppress_add)
34eaf542
TT
20827 {
20828 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20829 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20830 else
e37fd15a 20831 list_to_add = cu->list_in_scope;
34eaf542 20832 }
c906108c
SS
20833 break;
20834 }
e142c38c 20835 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20836 if (attr != nullptr)
c906108c 20837 {
e7c27a73 20838 var_decode_location (attr, sym, cu);
e142c38c 20839 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20840
20841 /* Fortran explicitly imports any global symbols to the local
20842 scope by DW_TAG_common_block. */
20843 if (cu->language == language_fortran && die->parent
20844 && die->parent->tag == DW_TAG_common_block)
20845 attr2 = NULL;
20846
caac4577
JG
20847 if (SYMBOL_CLASS (sym) == LOC_STATIC
20848 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20849 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20850 {
20851 /* When a static variable is eliminated by the linker,
20852 the corresponding debug information is not stripped
20853 out, but the variable address is set to null;
20854 do not add such variables into symbol table. */
20855 }
20856 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20857 {
4b610737
TT
20858 if (SYMBOL_CLASS (sym) == LOC_STATIC
20859 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20860 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20861 {
20862 /* A global static variable might be subject to
20863 copy relocation. We first check for a local
20864 minsym, though, because maybe the symbol was
20865 marked hidden, in which case this would not
20866 apply. */
20867 bound_minimal_symbol found
20868 = (lookup_minimal_symbol_linkage
987012b8 20869 (sym->linkage_name (), objfile));
4b610737
TT
20870 if (found.minsym != nullptr)
20871 sym->maybe_copied = 1;
20872 }
f55ee35c 20873
1c809c68
TT
20874 /* A variable with DW_AT_external is never static,
20875 but it may be block-scoped. */
804d2729 20876 list_to_add
c24bdb02
KS
20877 = ((cu->list_in_scope
20878 == cu->get_builder ()->get_file_symbols ())
20879 ? cu->get_builder ()->get_global_symbols ()
804d2729 20880 : cu->list_in_scope);
1c809c68 20881 }
c906108c 20882 else
e37fd15a 20883 list_to_add = cu->list_in_scope;
c906108c
SS
20884 }
20885 else
20886 {
20887 /* We do not know the address of this symbol.
c5aa993b
JM
20888 If it is an external symbol and we have type information
20889 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20890 The address of the variable will then be determined from
20891 the minimal symbol table whenever the variable is
20892 referenced. */
e142c38c 20893 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20894
20895 /* Fortran explicitly imports any global symbols to the local
20896 scope by DW_TAG_common_block. */
20897 if (cu->language == language_fortran && die->parent
20898 && die->parent->tag == DW_TAG_common_block)
20899 {
20900 /* SYMBOL_CLASS doesn't matter here because
20901 read_common_block is going to reset it. */
20902 if (!suppress_add)
20903 list_to_add = cu->list_in_scope;
20904 }
20905 else if (attr2 && (DW_UNSND (attr2) != 0)
20906 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20907 {
0fe7935b
DJ
20908 /* A variable with DW_AT_external is never static, but it
20909 may be block-scoped. */
804d2729 20910 list_to_add
c24bdb02
KS
20911 = ((cu->list_in_scope
20912 == cu->get_builder ()->get_file_symbols ())
20913 ? cu->get_builder ()->get_global_symbols ()
804d2729 20914 : cu->list_in_scope);
0fe7935b 20915
f1e6e072 20916 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20917 }
442ddf59
JK
20918 else if (!die_is_declaration (die, cu))
20919 {
20920 /* Use the default LOC_OPTIMIZED_OUT class. */
20921 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20922 if (!suppress_add)
20923 list_to_add = cu->list_in_scope;
442ddf59 20924 }
c906108c
SS
20925 }
20926 break;
20927 case DW_TAG_formal_parameter:
a60f3166
TT
20928 {
20929 /* If we are inside a function, mark this as an argument. If
20930 not, we might be looking at an argument to an inlined function
20931 when we do not have enough information to show inlined frames;
20932 pretend it's a local variable in that case so that the user can
20933 still see it. */
804d2729 20934 struct context_stack *curr
c24bdb02 20935 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20936 if (curr != nullptr && curr->name != nullptr)
20937 SYMBOL_IS_ARGUMENT (sym) = 1;
20938 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20939 if (attr != nullptr)
a60f3166
TT
20940 {
20941 var_decode_location (attr, sym, cu);
20942 }
20943 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20944 if (attr != nullptr)
a60f3166
TT
20945 {
20946 dwarf2_const_value (attr, sym, cu);
20947 }
f346a30d 20948
a60f3166
TT
20949 list_to_add = cu->list_in_scope;
20950 }
c906108c
SS
20951 break;
20952 case DW_TAG_unspecified_parameters:
20953 /* From varargs functions; gdb doesn't seem to have any
20954 interest in this information, so just ignore it for now.
20955 (FIXME?) */
20956 break;
34eaf542
TT
20957 case DW_TAG_template_type_param:
20958 suppress_add = 1;
20959 /* Fall through. */
c906108c 20960 case DW_TAG_class_type:
680b30c7 20961 case DW_TAG_interface_type:
c906108c
SS
20962 case DW_TAG_structure_type:
20963 case DW_TAG_union_type:
72019c9c 20964 case DW_TAG_set_type:
c906108c 20965 case DW_TAG_enumeration_type:
f1e6e072 20966 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20967 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20968
63d06c5c 20969 {
9c37b5ae 20970 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20971 really ever be static objects: otherwise, if you try
20972 to, say, break of a class's method and you're in a file
20973 which doesn't mention that class, it won't work unless
20974 the check for all static symbols in lookup_symbol_aux
20975 saves you. See the OtherFileClass tests in
20976 gdb.c++/namespace.exp. */
20977
e37fd15a 20978 if (!suppress_add)
34eaf542 20979 {
c24bdb02 20980 buildsym_compunit *builder = cu->get_builder ();
804d2729 20981 list_to_add
c24bdb02 20982 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20983 && cu->language == language_cplus
c24bdb02 20984 ? builder->get_global_symbols ()
804d2729 20985 : cu->list_in_scope);
63d06c5c 20986
64382290 20987 /* The semantics of C++ state that "struct foo {
9c37b5ae 20988 ... }" also defines a typedef for "foo". */
64382290 20989 if (cu->language == language_cplus
45280282 20990 || cu->language == language_ada
c44af4eb
TT
20991 || cu->language == language_d
20992 || cu->language == language_rust)
64382290
TT
20993 {
20994 /* The symbol's name is already allocated along
20995 with this objfile, so we don't need to
20996 duplicate it for the type. */
7d93a1e0 20997 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 20998 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 20999 }
63d06c5c
DC
21000 }
21001 }
c906108c
SS
21002 break;
21003 case DW_TAG_typedef:
f1e6e072 21004 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21005 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21006 list_to_add = cu->list_in_scope;
63d06c5c 21007 break;
c906108c 21008 case DW_TAG_base_type:
a02abb62 21009 case DW_TAG_subrange_type:
f1e6e072 21010 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21011 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21012 list_to_add = cu->list_in_scope;
c906108c
SS
21013 break;
21014 case DW_TAG_enumerator:
e142c38c 21015 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21016 if (attr != nullptr)
c906108c 21017 {
e7c27a73 21018 dwarf2_const_value (attr, sym, cu);
c906108c 21019 }
63d06c5c
DC
21020 {
21021 /* NOTE: carlton/2003-11-10: See comment above in the
21022 DW_TAG_class_type, etc. block. */
21023
804d2729 21024 list_to_add
c24bdb02 21025 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21026 && cu->language == language_cplus
c24bdb02 21027 ? cu->get_builder ()->get_global_symbols ()
804d2729 21028 : cu->list_in_scope);
63d06c5c 21029 }
c906108c 21030 break;
74921315 21031 case DW_TAG_imported_declaration:
5c4e30ca 21032 case DW_TAG_namespace:
f1e6e072 21033 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21034 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21035 break;
530e8392
KB
21036 case DW_TAG_module:
21037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21038 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21039 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21040 break;
4357ac6c 21041 case DW_TAG_common_block:
f1e6e072 21042 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21043 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21044 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21045 break;
c906108c
SS
21046 default:
21047 /* Not a tag we recognize. Hopefully we aren't processing
21048 trash data, but since we must specifically ignore things
21049 we don't recognize, there is nothing else we should do at
0963b4bd 21050 this point. */
b98664d3 21051 complaint (_("unsupported tag: '%s'"),
4d3c2250 21052 dwarf_tag_name (die->tag));
c906108c
SS
21053 break;
21054 }
df8a16a1 21055
e37fd15a
SW
21056 if (suppress_add)
21057 {
21058 sym->hash_next = objfile->template_symbols;
21059 objfile->template_symbols = sym;
21060 list_to_add = NULL;
21061 }
21062
21063 if (list_to_add != NULL)
d3cb6808 21064 add_symbol_to_list (sym, list_to_add);
e37fd15a 21065
df8a16a1
DJ
21066 /* For the benefit of old versions of GCC, check for anonymous
21067 namespaces based on the demangled name. */
4d4ec4e5 21068 if (!cu->processing_has_namespace_info
94af9270 21069 && cu->language == language_cplus)
c24bdb02 21070 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21071 }
21072 return (sym);
21073}
21074
98bfdba5
PA
21075/* Given an attr with a DW_FORM_dataN value in host byte order,
21076 zero-extend it as appropriate for the symbol's type. The DWARF
21077 standard (v4) is not entirely clear about the meaning of using
21078 DW_FORM_dataN for a constant with a signed type, where the type is
21079 wider than the data. The conclusion of a discussion on the DWARF
21080 list was that this is unspecified. We choose to always zero-extend
21081 because that is the interpretation long in use by GCC. */
c906108c 21082
98bfdba5 21083static gdb_byte *
ff39bb5e 21084dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21085 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21086{
5e22e966 21087 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21088 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21089 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21090 LONGEST l = DW_UNSND (attr);
21091
21092 if (bits < sizeof (*value) * 8)
21093 {
21094 l &= ((LONGEST) 1 << bits) - 1;
21095 *value = l;
21096 }
21097 else if (bits == sizeof (*value) * 8)
21098 *value = l;
21099 else
21100 {
224c3ddb 21101 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21102 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21103 return bytes;
21104 }
21105
21106 return NULL;
21107}
21108
21109/* Read a constant value from an attribute. Either set *VALUE, or if
21110 the value does not fit in *VALUE, set *BYTES - either already
21111 allocated on the objfile obstack, or newly allocated on OBSTACK,
21112 or, set *BATON, if we translated the constant to a location
21113 expression. */
21114
21115static void
ff39bb5e 21116dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21117 const char *name, struct obstack *obstack,
21118 struct dwarf2_cu *cu,
d521ce57 21119 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21120 struct dwarf2_locexpr_baton **baton)
21121{
5e22e966 21122 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21123 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21124 struct comp_unit_head *cu_header = &cu->header;
c906108c 21125 struct dwarf_block *blk;
98bfdba5
PA
21126 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21127 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21128
21129 *value = 0;
21130 *bytes = NULL;
21131 *baton = NULL;
c906108c
SS
21132
21133 switch (attr->form)
21134 {
21135 case DW_FORM_addr:
336d760d 21136 case DW_FORM_addrx:
3019eac3 21137 case DW_FORM_GNU_addr_index:
ac56253d 21138 {
ac56253d
TT
21139 gdb_byte *data;
21140
98bfdba5
PA
21141 if (TYPE_LENGTH (type) != cu_header->addr_size)
21142 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21143 cu_header->addr_size,
98bfdba5 21144 TYPE_LENGTH (type));
ac56253d
TT
21145 /* Symbols of this form are reasonably rare, so we just
21146 piggyback on the existing location code rather than writing
21147 a new implementation of symbol_computed_ops. */
8d749320 21148 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21149 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21150 (*baton)->per_cu = cu->per_cu;
21151 gdb_assert ((*baton)->per_cu);
ac56253d 21152
98bfdba5 21153 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21154 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21155 (*baton)->data = data;
ac56253d
TT
21156
21157 data[0] = DW_OP_addr;
21158 store_unsigned_integer (&data[1], cu_header->addr_size,
21159 byte_order, DW_ADDR (attr));
21160 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21161 }
c906108c 21162 break;
4ac36638 21163 case DW_FORM_string:
93b5768b 21164 case DW_FORM_strp:
cf532bd1 21165 case DW_FORM_strx:
3019eac3 21166 case DW_FORM_GNU_str_index:
36586728 21167 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21168 /* DW_STRING is already allocated on the objfile obstack, point
21169 directly to it. */
d521ce57 21170 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21171 break;
c906108c
SS
21172 case DW_FORM_block1:
21173 case DW_FORM_block2:
21174 case DW_FORM_block4:
21175 case DW_FORM_block:
2dc7f7b3 21176 case DW_FORM_exprloc:
0224619f 21177 case DW_FORM_data16:
c906108c 21178 blk = DW_BLOCK (attr);
98bfdba5
PA
21179 if (TYPE_LENGTH (type) != blk->size)
21180 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21181 TYPE_LENGTH (type));
21182 *bytes = blk->data;
c906108c 21183 break;
2df3850c
JM
21184
21185 /* The DW_AT_const_value attributes are supposed to carry the
21186 symbol's value "represented as it would be on the target
21187 architecture." By the time we get here, it's already been
21188 converted to host endianness, so we just need to sign- or
21189 zero-extend it as appropriate. */
21190 case DW_FORM_data1:
3aef2284 21191 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21192 break;
c906108c 21193 case DW_FORM_data2:
3aef2284 21194 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21195 break;
c906108c 21196 case DW_FORM_data4:
3aef2284 21197 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21198 break;
c906108c 21199 case DW_FORM_data8:
3aef2284 21200 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21201 break;
21202
c906108c 21203 case DW_FORM_sdata:
663c44ac 21204 case DW_FORM_implicit_const:
98bfdba5 21205 *value = DW_SND (attr);
2df3850c
JM
21206 break;
21207
c906108c 21208 case DW_FORM_udata:
98bfdba5 21209 *value = DW_UNSND (attr);
c906108c 21210 break;
2df3850c 21211
c906108c 21212 default:
b98664d3 21213 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21214 dwarf_form_name (attr->form));
98bfdba5 21215 *value = 0;
c906108c
SS
21216 break;
21217 }
21218}
21219
2df3850c 21220
98bfdba5
PA
21221/* Copy constant value from an attribute to a symbol. */
21222
2df3850c 21223static void
ff39bb5e 21224dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21225 struct dwarf2_cu *cu)
2df3850c 21226{
5e22e966 21227 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21228 LONGEST value;
d521ce57 21229 const gdb_byte *bytes;
98bfdba5 21230 struct dwarf2_locexpr_baton *baton;
2df3850c 21231
98bfdba5 21232 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21233 sym->print_name (),
98bfdba5
PA
21234 &objfile->objfile_obstack, cu,
21235 &value, &bytes, &baton);
2df3850c 21236
98bfdba5
PA
21237 if (baton != NULL)
21238 {
98bfdba5 21239 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21240 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21241 }
21242 else if (bytes != NULL)
21243 {
21244 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21245 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21246 }
21247 else
21248 {
21249 SYMBOL_VALUE (sym) = value;
f1e6e072 21250 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21251 }
2df3850c
JM
21252}
21253
c906108c
SS
21254/* Return the type of the die in question using its DW_AT_type attribute. */
21255
21256static struct type *
e7c27a73 21257die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21258{
c906108c 21259 struct attribute *type_attr;
c906108c 21260
e142c38c 21261 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21262 if (!type_attr)
21263 {
5e22e966 21264 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21265 /* A missing DW_AT_type represents a void type. */
518817b3 21266 return objfile_type (objfile)->builtin_void;
c906108c 21267 }
348e048f 21268
673bfd45 21269 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21270}
21271
b4ba55a1
JB
21272/* True iff CU's producer generates GNAT Ada auxiliary information
21273 that allows to find parallel types through that information instead
21274 of having to do expensive parallel lookups by type name. */
21275
21276static int
21277need_gnat_info (struct dwarf2_cu *cu)
21278{
de4cb04a
JB
21279 /* Assume that the Ada compiler was GNAT, which always produces
21280 the auxiliary information. */
21281 return (cu->language == language_ada);
b4ba55a1
JB
21282}
21283
b4ba55a1
JB
21284/* Return the auxiliary type of the die in question using its
21285 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21286 attribute is not present. */
21287
21288static struct type *
21289die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21290{
b4ba55a1 21291 struct attribute *type_attr;
b4ba55a1
JB
21292
21293 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21294 if (!type_attr)
21295 return NULL;
21296
673bfd45 21297 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21298}
21299
21300/* If DIE has a descriptive_type attribute, then set the TYPE's
21301 descriptive type accordingly. */
21302
21303static void
21304set_descriptive_type (struct type *type, struct die_info *die,
21305 struct dwarf2_cu *cu)
21306{
21307 struct type *descriptive_type = die_descriptive_type (die, cu);
21308
21309 if (descriptive_type)
21310 {
21311 ALLOCATE_GNAT_AUX_TYPE (type);
21312 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21313 }
21314}
21315
c906108c
SS
21316/* Return the containing type of the die in question using its
21317 DW_AT_containing_type attribute. */
21318
21319static struct type *
e7c27a73 21320die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21321{
c906108c 21322 struct attribute *type_attr;
5e22e966 21323 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21324
e142c38c 21325 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21326 if (!type_attr)
21327 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21328 "[in module %s]"), objfile_name (objfile));
33ac96f0 21329
673bfd45 21330 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21331}
21332
ac9ec31b
DE
21333/* Return an error marker type to use for the ill formed type in DIE/CU. */
21334
21335static struct type *
21336build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21337{
5e22e966 21338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21339 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21340 char *saved;
ac9ec31b 21341
528e1572
SM
21342 std::string message
21343 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21344 objfile_name (objfile),
21345 sect_offset_str (cu->header.sect_off),
21346 sect_offset_str (die->sect_off));
efba19b0 21347 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21348
19f392bc 21349 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21350}
21351
673bfd45 21352/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21353 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21354 DW_AT_containing_type.
673bfd45
DE
21355 If there is no type substitute an error marker. */
21356
c906108c 21357static struct type *
ff39bb5e 21358lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21359 struct dwarf2_cu *cu)
c906108c 21360{
5e22e966 21361 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21362 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21363 struct type *this_type;
21364
ac9ec31b
DE
21365 gdb_assert (attr->name == DW_AT_type
21366 || attr->name == DW_AT_GNAT_descriptive_type
21367 || attr->name == DW_AT_containing_type);
21368
673bfd45
DE
21369 /* First see if we have it cached. */
21370
36586728
TT
21371 if (attr->form == DW_FORM_GNU_ref_alt)
21372 {
21373 struct dwarf2_per_cu_data *per_cu;
0826b30a 21374 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21375
ed2dc618
SM
21376 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21377 dwarf2_per_objfile);
9c541725 21378 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21379 }
cd6c91b4 21380 else if (attr->form_is_ref ())
673bfd45 21381 {
0826b30a 21382 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21383
9c541725 21384 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21385 }
55f1336d 21386 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21387 {
ac9ec31b 21388 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21389
ac9ec31b 21390 return get_signatured_type (die, signature, cu);
673bfd45
DE
21391 }
21392 else
21393 {
b98664d3 21394 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21395 " at %s [in module %s]"),
21396 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21397 objfile_name (objfile));
ac9ec31b 21398 return build_error_marker_type (cu, die);
673bfd45
DE
21399 }
21400
21401 /* If not cached we need to read it in. */
21402
21403 if (this_type == NULL)
21404 {
ac9ec31b 21405 struct die_info *type_die = NULL;
673bfd45
DE
21406 struct dwarf2_cu *type_cu = cu;
21407
cd6c91b4 21408 if (attr->form_is_ref ())
ac9ec31b
DE
21409 type_die = follow_die_ref (die, attr, &type_cu);
21410 if (type_die == NULL)
21411 return build_error_marker_type (cu, die);
21412 /* If we find the type now, it's probably because the type came
3019eac3
DE
21413 from an inter-CU reference and the type's CU got expanded before
21414 ours. */
ac9ec31b 21415 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21416 }
21417
21418 /* If we still don't have a type use an error marker. */
21419
21420 if (this_type == NULL)
ac9ec31b 21421 return build_error_marker_type (cu, die);
673bfd45 21422
f792889a 21423 return this_type;
c906108c
SS
21424}
21425
673bfd45
DE
21426/* Return the type in DIE, CU.
21427 Returns NULL for invalid types.
21428
02142a6c 21429 This first does a lookup in die_type_hash,
673bfd45
DE
21430 and only reads the die in if necessary.
21431
21432 NOTE: This can be called when reading in partial or full symbols. */
21433
f792889a 21434static struct type *
e7c27a73 21435read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21436{
f792889a
DJ
21437 struct type *this_type;
21438
21439 this_type = get_die_type (die, cu);
21440 if (this_type)
21441 return this_type;
21442
673bfd45
DE
21443 return read_type_die_1 (die, cu);
21444}
21445
21446/* Read the type in DIE, CU.
21447 Returns NULL for invalid types. */
21448
21449static struct type *
21450read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21451{
21452 struct type *this_type = NULL;
21453
c906108c
SS
21454 switch (die->tag)
21455 {
21456 case DW_TAG_class_type:
680b30c7 21457 case DW_TAG_interface_type:
c906108c
SS
21458 case DW_TAG_structure_type:
21459 case DW_TAG_union_type:
f792889a 21460 this_type = read_structure_type (die, cu);
c906108c
SS
21461 break;
21462 case DW_TAG_enumeration_type:
f792889a 21463 this_type = read_enumeration_type (die, cu);
c906108c
SS
21464 break;
21465 case DW_TAG_subprogram:
21466 case DW_TAG_subroutine_type:
edb3359d 21467 case DW_TAG_inlined_subroutine:
f792889a 21468 this_type = read_subroutine_type (die, cu);
c906108c
SS
21469 break;
21470 case DW_TAG_array_type:
f792889a 21471 this_type = read_array_type (die, cu);
c906108c 21472 break;
72019c9c 21473 case DW_TAG_set_type:
f792889a 21474 this_type = read_set_type (die, cu);
72019c9c 21475 break;
c906108c 21476 case DW_TAG_pointer_type:
f792889a 21477 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21478 break;
21479 case DW_TAG_ptr_to_member_type:
f792889a 21480 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21481 break;
21482 case DW_TAG_reference_type:
4297a3f0
AV
21483 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21484 break;
21485 case DW_TAG_rvalue_reference_type:
21486 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21487 break;
21488 case DW_TAG_const_type:
f792889a 21489 this_type = read_tag_const_type (die, cu);
c906108c
SS
21490 break;
21491 case DW_TAG_volatile_type:
f792889a 21492 this_type = read_tag_volatile_type (die, cu);
c906108c 21493 break;
06d66ee9
TT
21494 case DW_TAG_restrict_type:
21495 this_type = read_tag_restrict_type (die, cu);
21496 break;
c906108c 21497 case DW_TAG_string_type:
f792889a 21498 this_type = read_tag_string_type (die, cu);
c906108c
SS
21499 break;
21500 case DW_TAG_typedef:
f792889a 21501 this_type = read_typedef (die, cu);
c906108c 21502 break;
a02abb62 21503 case DW_TAG_subrange_type:
f792889a 21504 this_type = read_subrange_type (die, cu);
a02abb62 21505 break;
c906108c 21506 case DW_TAG_base_type:
f792889a 21507 this_type = read_base_type (die, cu);
c906108c 21508 break;
81a17f79 21509 case DW_TAG_unspecified_type:
f792889a 21510 this_type = read_unspecified_type (die, cu);
81a17f79 21511 break;
0114d602
DJ
21512 case DW_TAG_namespace:
21513 this_type = read_namespace_type (die, cu);
21514 break;
f55ee35c
JK
21515 case DW_TAG_module:
21516 this_type = read_module_type (die, cu);
21517 break;
a2c2acaf
MW
21518 case DW_TAG_atomic_type:
21519 this_type = read_tag_atomic_type (die, cu);
21520 break;
c906108c 21521 default:
b98664d3 21522 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21523 dwarf_tag_name (die->tag));
c906108c
SS
21524 break;
21525 }
63d06c5c 21526
f792889a 21527 return this_type;
63d06c5c
DC
21528}
21529
abc72ce4
DE
21530/* See if we can figure out if the class lives in a namespace. We do
21531 this by looking for a member function; its demangled name will
21532 contain namespace info, if there is any.
21533 Return the computed name or NULL.
21534 Space for the result is allocated on the objfile's obstack.
21535 This is the full-die version of guess_partial_die_structure_name.
21536 In this case we know DIE has no useful parent. */
21537
43816ebc 21538static const char *
abc72ce4
DE
21539guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21540{
21541 struct die_info *spec_die;
21542 struct dwarf2_cu *spec_cu;
21543 struct die_info *child;
5e22e966 21544 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21545
21546 spec_cu = cu;
21547 spec_die = die_specification (die, &spec_cu);
21548 if (spec_die != NULL)
21549 {
21550 die = spec_die;
21551 cu = spec_cu;
21552 }
21553
21554 for (child = die->child;
21555 child != NULL;
21556 child = child->sibling)
21557 {
21558 if (child->tag == DW_TAG_subprogram)
21559 {
73b9be8b 21560 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21561
7d45c7c3 21562 if (linkage_name != NULL)
abc72ce4 21563 {
43816ebc
TT
21564 gdb::unique_xmalloc_ptr<char> actual_name
21565 (language_class_name_from_physname (cu->language_defn,
21566 linkage_name));
21567 const char *name = NULL;
abc72ce4
DE
21568
21569 if (actual_name != NULL)
21570 {
15d034d0 21571 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21572
21573 if (die_name != NULL
43816ebc 21574 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21575 {
21576 /* Strip off the class name from the full name.
21577 We want the prefix. */
21578 int die_name_len = strlen (die_name);
43816ebc
TT
21579 int actual_name_len = strlen (actual_name.get ());
21580 const char *ptr = actual_name.get ();
abc72ce4
DE
21581
21582 /* Test for '::' as a sanity check. */
21583 if (actual_name_len > die_name_len + 2
43816ebc 21584 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21585 name = obstack_strndup (
e3b94546 21586 &objfile->per_bfd->storage_obstack,
43816ebc 21587 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21588 }
21589 }
abc72ce4
DE
21590 return name;
21591 }
21592 }
21593 }
21594
21595 return NULL;
21596}
21597
96408a79
SA
21598/* GCC might emit a nameless typedef that has a linkage name. Determine the
21599 prefix part in such case. See
21600 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21601
a121b7c1 21602static const char *
96408a79
SA
21603anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21604{
21605 struct attribute *attr;
e6a959d6 21606 const char *base;
96408a79
SA
21607
21608 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21609 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21610 return NULL;
21611
7d45c7c3 21612 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21613 return NULL;
21614
73b9be8b 21615 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21616 if (attr == NULL || DW_STRING (attr) == NULL)
21617 return NULL;
21618
21619 /* dwarf2_name had to be already called. */
21620 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21621
21622 /* Strip the base name, keep any leading namespaces/classes. */
21623 base = strrchr (DW_STRING (attr), ':');
21624 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21625 return "";
21626
5e22e966 21627 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21628 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21629 DW_STRING (attr),
21630 &base[-1] - DW_STRING (attr));
96408a79
SA
21631}
21632
fdde2d81 21633/* Return the name of the namespace/class that DIE is defined within,
0114d602 21634 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21635
0114d602
DJ
21636 For example, if we're within the method foo() in the following
21637 code:
21638
21639 namespace N {
21640 class C {
21641 void foo () {
21642 }
21643 };
21644 }
21645
21646 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21647
0d5cff50 21648static const char *
e142c38c 21649determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21650{
5e22e966 21651 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21652 struct die_info *parent, *spec_die;
21653 struct dwarf2_cu *spec_cu;
21654 struct type *parent_type;
a121b7c1 21655 const char *retval;
63d06c5c 21656
9c37b5ae 21657 if (cu->language != language_cplus
c44af4eb
TT
21658 && cu->language != language_fortran && cu->language != language_d
21659 && cu->language != language_rust)
0114d602
DJ
21660 return "";
21661
96408a79
SA
21662 retval = anonymous_struct_prefix (die, cu);
21663 if (retval)
21664 return retval;
21665
0114d602
DJ
21666 /* We have to be careful in the presence of DW_AT_specification.
21667 For example, with GCC 3.4, given the code
21668
21669 namespace N {
21670 void foo() {
21671 // Definition of N::foo.
21672 }
21673 }
21674
21675 then we'll have a tree of DIEs like this:
21676
21677 1: DW_TAG_compile_unit
21678 2: DW_TAG_namespace // N
21679 3: DW_TAG_subprogram // declaration of N::foo
21680 4: DW_TAG_subprogram // definition of N::foo
21681 DW_AT_specification // refers to die #3
21682
21683 Thus, when processing die #4, we have to pretend that we're in
21684 the context of its DW_AT_specification, namely the contex of die
21685 #3. */
21686 spec_cu = cu;
21687 spec_die = die_specification (die, &spec_cu);
21688 if (spec_die == NULL)
21689 parent = die->parent;
21690 else
63d06c5c 21691 {
0114d602
DJ
21692 parent = spec_die->parent;
21693 cu = spec_cu;
63d06c5c 21694 }
0114d602
DJ
21695
21696 if (parent == NULL)
21697 return "";
98bfdba5
PA
21698 else if (parent->building_fullname)
21699 {
21700 const char *name;
21701 const char *parent_name;
21702
21703 /* It has been seen on RealView 2.2 built binaries,
21704 DW_TAG_template_type_param types actually _defined_ as
21705 children of the parent class:
21706
21707 enum E {};
21708 template class <class Enum> Class{};
21709 Class<enum E> class_e;
21710
21711 1: DW_TAG_class_type (Class)
21712 2: DW_TAG_enumeration_type (E)
21713 3: DW_TAG_enumerator (enum1:0)
21714 3: DW_TAG_enumerator (enum2:1)
21715 ...
21716 2: DW_TAG_template_type_param
21717 DW_AT_type DW_FORM_ref_udata (E)
21718
21719 Besides being broken debug info, it can put GDB into an
21720 infinite loop. Consider:
21721
21722 When we're building the full name for Class<E>, we'll start
21723 at Class, and go look over its template type parameters,
21724 finding E. We'll then try to build the full name of E, and
21725 reach here. We're now trying to build the full name of E,
21726 and look over the parent DIE for containing scope. In the
21727 broken case, if we followed the parent DIE of E, we'd again
21728 find Class, and once again go look at its template type
21729 arguments, etc., etc. Simply don't consider such parent die
21730 as source-level parent of this die (it can't be, the language
21731 doesn't allow it), and break the loop here. */
21732 name = dwarf2_name (die, cu);
21733 parent_name = dwarf2_name (parent, cu);
b98664d3 21734 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21735 name ? name : "<unknown>",
21736 parent_name ? parent_name : "<unknown>");
21737 return "";
21738 }
63d06c5c 21739 else
0114d602
DJ
21740 switch (parent->tag)
21741 {
63d06c5c 21742 case DW_TAG_namespace:
0114d602 21743 parent_type = read_type_die (parent, cu);
acebe513
UW
21744 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21745 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21746 Work around this problem here. */
21747 if (cu->language == language_cplus
7d93a1e0 21748 && strcmp (parent_type->name (), "::") == 0)
acebe513 21749 return "";
0114d602 21750 /* We give a name to even anonymous namespaces. */
7d93a1e0 21751 return parent_type->name ();
63d06c5c 21752 case DW_TAG_class_type:
680b30c7 21753 case DW_TAG_interface_type:
63d06c5c 21754 case DW_TAG_structure_type:
0114d602 21755 case DW_TAG_union_type:
f55ee35c 21756 case DW_TAG_module:
0114d602 21757 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21758 if (parent_type->name () != NULL)
21759 return parent_type->name ();
0114d602
DJ
21760 else
21761 /* An anonymous structure is only allowed non-static data
21762 members; no typedefs, no member functions, et cetera.
21763 So it does not need a prefix. */
21764 return "";
abc72ce4 21765 case DW_TAG_compile_unit:
95554aad 21766 case DW_TAG_partial_unit:
abc72ce4
DE
21767 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21768 if (cu->language == language_cplus
5989a64e 21769 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21770 && die->child != NULL
21771 && (die->tag == DW_TAG_class_type
21772 || die->tag == DW_TAG_structure_type
21773 || die->tag == DW_TAG_union_type))
21774 {
43816ebc 21775 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21776 if (name != NULL)
21777 return name;
21778 }
21779 return "";
0a4b0913
AB
21780 case DW_TAG_subprogram:
21781 /* Nested subroutines in Fortran get a prefix with the name
21782 of the parent's subroutine. */
21783 if (cu->language == language_fortran)
21784 {
21785 if ((die->tag == DW_TAG_subprogram)
21786 && (dwarf2_name (parent, cu) != NULL))
21787 return dwarf2_name (parent, cu);
21788 }
21789 return determine_prefix (parent, cu);
3d567982
TT
21790 case DW_TAG_enumeration_type:
21791 parent_type = read_type_die (parent, cu);
21792 if (TYPE_DECLARED_CLASS (parent_type))
21793 {
7d93a1e0
SM
21794 if (parent_type->name () != NULL)
21795 return parent_type->name ();
3d567982
TT
21796 return "";
21797 }
21798 /* Fall through. */
63d06c5c 21799 default:
8176b9b8 21800 return determine_prefix (parent, cu);
63d06c5c 21801 }
63d06c5c
DC
21802}
21803
3e43a32a
MS
21804/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21805 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21806 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21807 an obconcat, otherwise allocate storage for the result. The CU argument is
21808 used to determine the language and hence, the appropriate separator. */
987504bb 21809
f55ee35c 21810#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21811
21812static char *
f55ee35c
JK
21813typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21814 int physname, struct dwarf2_cu *cu)
63d06c5c 21815{
f55ee35c 21816 const char *lead = "";
5c315b68 21817 const char *sep;
63d06c5c 21818
3e43a32a
MS
21819 if (suffix == NULL || suffix[0] == '\0'
21820 || prefix == NULL || prefix[0] == '\0')
987504bb 21821 sep = "";
45280282
IB
21822 else if (cu->language == language_d)
21823 {
21824 /* For D, the 'main' function could be defined in any module, but it
21825 should never be prefixed. */
21826 if (strcmp (suffix, "D main") == 0)
21827 {
21828 prefix = "";
21829 sep = "";
21830 }
21831 else
21832 sep = ".";
21833 }
f55ee35c
JK
21834 else if (cu->language == language_fortran && physname)
21835 {
21836 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21837 DW_AT_MIPS_linkage_name is preferred and used instead. */
21838
21839 lead = "__";
21840 sep = "_MOD_";
21841 }
987504bb
JJ
21842 else
21843 sep = "::";
63d06c5c 21844
6dd47d34
DE
21845 if (prefix == NULL)
21846 prefix = "";
21847 if (suffix == NULL)
21848 suffix = "";
21849
987504bb
JJ
21850 if (obs == NULL)
21851 {
3e43a32a 21852 char *retval
224c3ddb
SM
21853 = ((char *)
21854 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21855
f55ee35c
JK
21856 strcpy (retval, lead);
21857 strcat (retval, prefix);
6dd47d34
DE
21858 strcat (retval, sep);
21859 strcat (retval, suffix);
63d06c5c
DC
21860 return retval;
21861 }
987504bb
JJ
21862 else
21863 {
21864 /* We have an obstack. */
f55ee35c 21865 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21866 }
63d06c5c
DC
21867}
21868
71c25dea
TT
21869/* Get name of a die, return NULL if not found. */
21870
15d034d0
TT
21871static const char *
21872dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21873 struct objfile *objfile)
71c25dea
TT
21874{
21875 if (name && cu->language == language_cplus)
21876 {
596dc4ad
TT
21877 gdb::unique_xmalloc_ptr<char> canon_name
21878 = cp_canonicalize_string (name);
71c25dea 21879
596dc4ad
TT
21880 if (canon_name != nullptr)
21881 name = objfile->intern (canon_name.get ());
71c25dea
TT
21882 }
21883
21884 return name;
c906108c
SS
21885}
21886
96553a0c
DE
21887/* Get name of a die, return NULL if not found.
21888 Anonymous namespaces are converted to their magic string. */
9219021c 21889
15d034d0 21890static const char *
e142c38c 21891dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21892{
21893 struct attribute *attr;
5e22e966 21894 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21895
e142c38c 21896 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21897 if ((!attr || !DW_STRING (attr))
96553a0c 21898 && die->tag != DW_TAG_namespace
53832f31
TT
21899 && die->tag != DW_TAG_class_type
21900 && die->tag != DW_TAG_interface_type
21901 && die->tag != DW_TAG_structure_type
21902 && die->tag != DW_TAG_union_type)
71c25dea
TT
21903 return NULL;
21904
21905 switch (die->tag)
21906 {
21907 case DW_TAG_compile_unit:
95554aad 21908 case DW_TAG_partial_unit:
71c25dea
TT
21909 /* Compilation units have a DW_AT_name that is a filename, not
21910 a source language identifier. */
21911 case DW_TAG_enumeration_type:
21912 case DW_TAG_enumerator:
21913 /* These tags always have simple identifiers already; no need
21914 to canonicalize them. */
21915 return DW_STRING (attr);
907af001 21916
96553a0c
DE
21917 case DW_TAG_namespace:
21918 if (attr != NULL && DW_STRING (attr) != NULL)
21919 return DW_STRING (attr);
21920 return CP_ANONYMOUS_NAMESPACE_STR;
21921
907af001
UW
21922 case DW_TAG_class_type:
21923 case DW_TAG_interface_type:
21924 case DW_TAG_structure_type:
21925 case DW_TAG_union_type:
21926 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21927 structures or unions. These were of the form "._%d" in GCC 4.1,
21928 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21929 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21930 if (attr && DW_STRING (attr)
61012eef
GB
21931 && (startswith (DW_STRING (attr), "._")
21932 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21933 return NULL;
53832f31
TT
21934
21935 /* GCC might emit a nameless typedef that has a linkage name. See
21936 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21937 if (!attr || DW_STRING (attr) == NULL)
21938 {
73b9be8b 21939 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21940 if (attr == NULL || DW_STRING (attr) == NULL)
21941 return NULL;
21942
df5c6c50
JK
21943 /* Avoid demangling DW_STRING (attr) the second time on a second
21944 call for the same DIE. */
21945 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21946 {
43816ebc
TT
21947 gdb::unique_xmalloc_ptr<char> demangled
21948 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21949 if (demangled == nullptr)
21950 return nullptr;
43816ebc 21951
be1e3d3e 21952 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21953 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21954 }
67430cd0
TT
21955
21956 /* Strip any leading namespaces/classes, keep only the base name.
21957 DW_AT_name for named DIEs does not contain the prefixes. */
21958 const char *base = strrchr (DW_STRING (attr), ':');
21959 if (base && base > DW_STRING (attr) && base[-1] == ':')
21960 return &base[1];
21961 else
21962 return DW_STRING (attr);
53832f31 21963 }
907af001
UW
21964 break;
21965
71c25dea 21966 default:
907af001
UW
21967 break;
21968 }
21969
21970 if (!DW_STRING_IS_CANONICAL (attr))
21971 {
be1e3d3e
TT
21972 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21973 objfile);
907af001 21974 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21975 }
907af001 21976 return DW_STRING (attr);
9219021c
DC
21977}
21978
21979/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21980 is none. *EXT_CU is the CU containing DIE on input, and the CU
21981 containing the return value on output. */
9219021c
DC
21982
21983static struct die_info *
f2f0e013 21984dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21985{
21986 struct attribute *attr;
9219021c 21987
f2f0e013 21988 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21989 if (attr == NULL)
21990 return NULL;
21991
f2f0e013 21992 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21993}
21994
f9aca02d 21995static void
d97bc12b 21996dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21997{
21998 unsigned int i;
21999
d97bc12b 22000 print_spaces (indent, f);
9d8780f0 22001 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22002 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22003 sect_offset_str (die->sect_off));
d97bc12b
DE
22004
22005 if (die->parent != NULL)
22006 {
22007 print_spaces (indent, f);
9d8780f0
SM
22008 fprintf_unfiltered (f, " parent at offset: %s\n",
22009 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22010 }
22011
22012 print_spaces (indent, f);
22013 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22014 dwarf_bool_name (die->child != NULL));
c906108c 22015
d97bc12b
DE
22016 print_spaces (indent, f);
22017 fprintf_unfiltered (f, " attributes:\n");
22018
c906108c
SS
22019 for (i = 0; i < die->num_attrs; ++i)
22020 {
d97bc12b
DE
22021 print_spaces (indent, f);
22022 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22023 dwarf_attr_name (die->attrs[i].name),
22024 dwarf_form_name (die->attrs[i].form));
d97bc12b 22025
c906108c
SS
22026 switch (die->attrs[i].form)
22027 {
c906108c 22028 case DW_FORM_addr:
336d760d 22029 case DW_FORM_addrx:
3019eac3 22030 case DW_FORM_GNU_addr_index:
d97bc12b 22031 fprintf_unfiltered (f, "address: ");
5af949e3 22032 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22033 break;
22034 case DW_FORM_block2:
22035 case DW_FORM_block4:
22036 case DW_FORM_block:
22037 case DW_FORM_block1:
56eb65bd
SP
22038 fprintf_unfiltered (f, "block: size %s",
22039 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22040 break;
2dc7f7b3 22041 case DW_FORM_exprloc:
56eb65bd
SP
22042 fprintf_unfiltered (f, "expression: size %s",
22043 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22044 break;
0224619f
JK
22045 case DW_FORM_data16:
22046 fprintf_unfiltered (f, "constant of 16 bytes");
22047 break;
4568ecf9
DE
22048 case DW_FORM_ref_addr:
22049 fprintf_unfiltered (f, "ref address: ");
22050 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22051 break;
36586728
TT
22052 case DW_FORM_GNU_ref_alt:
22053 fprintf_unfiltered (f, "alt ref address: ");
22054 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22055 break;
10b3939b
DJ
22056 case DW_FORM_ref1:
22057 case DW_FORM_ref2:
22058 case DW_FORM_ref4:
4568ecf9
DE
22059 case DW_FORM_ref8:
22060 case DW_FORM_ref_udata:
d97bc12b 22061 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22062 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22063 break;
c906108c
SS
22064 case DW_FORM_data1:
22065 case DW_FORM_data2:
22066 case DW_FORM_data4:
ce5d95e1 22067 case DW_FORM_data8:
c906108c
SS
22068 case DW_FORM_udata:
22069 case DW_FORM_sdata:
43bbcdc2
PH
22070 fprintf_unfiltered (f, "constant: %s",
22071 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22072 break;
2dc7f7b3
TT
22073 case DW_FORM_sec_offset:
22074 fprintf_unfiltered (f, "section offset: %s",
22075 pulongest (DW_UNSND (&die->attrs[i])));
22076 break;
55f1336d 22077 case DW_FORM_ref_sig8:
ac9ec31b
DE
22078 fprintf_unfiltered (f, "signature: %s",
22079 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22080 break;
c906108c 22081 case DW_FORM_string:
4bdf3d34 22082 case DW_FORM_strp:
43988095 22083 case DW_FORM_line_strp:
cf532bd1 22084 case DW_FORM_strx:
3019eac3 22085 case DW_FORM_GNU_str_index:
36586728 22086 case DW_FORM_GNU_strp_alt:
8285870a 22087 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22088 DW_STRING (&die->attrs[i])
8285870a
JK
22089 ? DW_STRING (&die->attrs[i]) : "",
22090 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22091 break;
22092 case DW_FORM_flag:
22093 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22094 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22095 else
d97bc12b 22096 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22097 break;
2dc7f7b3
TT
22098 case DW_FORM_flag_present:
22099 fprintf_unfiltered (f, "flag: TRUE");
22100 break;
a8329558 22101 case DW_FORM_indirect:
0963b4bd
MS
22102 /* The reader will have reduced the indirect form to
22103 the "base form" so this form should not occur. */
5f48f8f3 22104 fprintf_unfiltered (f,
3e43a32a 22105 "unexpected attribute form: DW_FORM_indirect");
a8329558 22106 break;
663c44ac
JK
22107 case DW_FORM_implicit_const:
22108 fprintf_unfiltered (f, "constant: %s",
22109 plongest (DW_SND (&die->attrs[i])));
22110 break;
c906108c 22111 default:
d97bc12b 22112 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22113 die->attrs[i].form);
d97bc12b 22114 break;
c906108c 22115 }
d97bc12b 22116 fprintf_unfiltered (f, "\n");
c906108c
SS
22117 }
22118}
22119
f9aca02d 22120static void
d97bc12b 22121dump_die_for_error (struct die_info *die)
c906108c 22122{
d97bc12b
DE
22123 dump_die_shallow (gdb_stderr, 0, die);
22124}
22125
22126static void
22127dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22128{
22129 int indent = level * 4;
22130
22131 gdb_assert (die != NULL);
22132
22133 if (level >= max_level)
22134 return;
22135
22136 dump_die_shallow (f, indent, die);
22137
22138 if (die->child != NULL)
c906108c 22139 {
d97bc12b
DE
22140 print_spaces (indent, f);
22141 fprintf_unfiltered (f, " Children:");
22142 if (level + 1 < max_level)
22143 {
22144 fprintf_unfiltered (f, "\n");
22145 dump_die_1 (f, level + 1, max_level, die->child);
22146 }
22147 else
22148 {
3e43a32a
MS
22149 fprintf_unfiltered (f,
22150 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22151 }
22152 }
22153
22154 if (die->sibling != NULL && level > 0)
22155 {
22156 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22157 }
22158}
22159
d97bc12b
DE
22160/* This is called from the pdie macro in gdbinit.in.
22161 It's not static so gcc will keep a copy callable from gdb. */
22162
22163void
22164dump_die (struct die_info *die, int max_level)
22165{
22166 dump_die_1 (gdb_stdlog, 0, max_level, die);
22167}
22168
f9aca02d 22169static void
51545339 22170store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22171{
51545339 22172 void **slot;
c906108c 22173
9c541725
PA
22174 slot = htab_find_slot_with_hash (cu->die_hash, die,
22175 to_underlying (die->sect_off),
b64f50a1 22176 INSERT);
51545339
DJ
22177
22178 *slot = die;
c906108c
SS
22179}
22180
348e048f
DE
22181/* Follow reference or signature attribute ATTR of SRC_DIE.
22182 On entry *REF_CU is the CU of SRC_DIE.
22183 On exit *REF_CU is the CU of the result. */
22184
22185static struct die_info *
ff39bb5e 22186follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22187 struct dwarf2_cu **ref_cu)
22188{
22189 struct die_info *die;
22190
cd6c91b4 22191 if (attr->form_is_ref ())
348e048f 22192 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22193 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22194 die = follow_die_sig (src_die, attr, ref_cu);
22195 else
22196 {
22197 dump_die_for_error (src_die);
22198 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22199 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22200 }
22201
22202 return die;
03dd20cc
DJ
22203}
22204
5c631832 22205/* Follow reference OFFSET.
673bfd45
DE
22206 On entry *REF_CU is the CU of the source die referencing OFFSET.
22207 On exit *REF_CU is the CU of the result.
22208 Returns NULL if OFFSET is invalid. */
f504f079 22209
f9aca02d 22210static struct die_info *
9c541725 22211follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22212 struct dwarf2_cu **ref_cu)
c906108c 22213{
10b3939b 22214 struct die_info temp_die;
f2f0e013 22215 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22216 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22217
348e048f
DE
22218 gdb_assert (cu->per_cu != NULL);
22219
98bfdba5
PA
22220 target_cu = cu;
22221
3019eac3 22222 if (cu->per_cu->is_debug_types)
348e048f
DE
22223 {
22224 /* .debug_types CUs cannot reference anything outside their CU.
22225 If they need to, they have to reference a signatured type via
55f1336d 22226 DW_FORM_ref_sig8. */
4057dfde 22227 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22228 return NULL;
348e048f 22229 }
36586728 22230 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22231 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22232 {
22233 struct dwarf2_per_cu_data *per_cu;
9a619af0 22234
9c541725 22235 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22236 dwarf2_per_objfile);
03dd20cc
DJ
22237
22238 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22239 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 22240 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22241
10b3939b
DJ
22242 target_cu = per_cu->cu;
22243 }
98bfdba5
PA
22244 else if (cu->dies == NULL)
22245 {
22246 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22247 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22248 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22249 language_minimal);
98bfdba5 22250 }
c906108c 22251
f2f0e013 22252 *ref_cu = target_cu;
9c541725 22253 temp_die.sect_off = sect_off;
c24bdb02
KS
22254
22255 if (target_cu != cu)
22256 target_cu->ancestor = cu;
22257
9a3c8263 22258 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22259 &temp_die,
22260 to_underlying (sect_off));
5c631832 22261}
10b3939b 22262
5c631832
JK
22263/* Follow reference attribute ATTR of SRC_DIE.
22264 On entry *REF_CU is the CU of SRC_DIE.
22265 On exit *REF_CU is the CU of the result. */
22266
22267static struct die_info *
ff39bb5e 22268follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22269 struct dwarf2_cu **ref_cu)
22270{
0826b30a 22271 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22272 struct dwarf2_cu *cu = *ref_cu;
22273 struct die_info *die;
22274
9c541725 22275 die = follow_die_offset (sect_off,
36586728
TT
22276 (attr->form == DW_FORM_GNU_ref_alt
22277 || cu->per_cu->is_dwz),
22278 ref_cu);
5c631832 22279 if (!die)
9d8780f0
SM
22280 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22281 "at %s [in module %s]"),
22282 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22283 objfile_name (cu->per_objfile->objfile));
348e048f 22284
5c631832
JK
22285 return die;
22286}
22287
d4c9a4f8 22288/* See read.h. */
5c631832
JK
22289
22290struct dwarf2_locexpr_baton
9c541725 22291dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22292 dwarf2_per_cu_data *per_cu,
8b9737bf 22293 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22294 void *baton, bool resolve_abstract_p)
5c631832 22295{
918dd910 22296 struct dwarf2_cu *cu;
5c631832
JK
22297 struct die_info *die;
22298 struct attribute *attr;
22299 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22300 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22301 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22302
918dd910 22303 if (per_cu->cu == NULL)
ab432490 22304 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22305 cu = per_cu->cu;
cc12ce38
DE
22306 if (cu == NULL)
22307 {
22308 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22309 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22310 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22311 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22312 }
918dd910 22313
9c541725 22314 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22315 if (!die)
9d8780f0
SM
22316 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22317 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22318
22319 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22320 if (!attr && resolve_abstract_p
5989a64e
SM
22321 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22322 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22323 {
22324 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22325 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22326 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22327
3360b6e7 22328 for (const auto &cand_off
5989a64e 22329 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22330 {
3360b6e7
TV
22331 struct dwarf2_cu *cand_cu = cu;
22332 struct die_info *cand
22333 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22334 if (!cand
22335 || !cand->parent
e4a62c65
TV
22336 || cand->parent->tag != DW_TAG_subprogram)
22337 continue;
22338
22339 CORE_ADDR pc_low, pc_high;
22340 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22341 if (pc_low == ((CORE_ADDR) -1))
22342 continue;
22343 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22344 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22345 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22346 continue;
22347
22348 die = cand;
22349 attr = dwarf2_attr (die, DW_AT_location, cu);
22350 break;
22351 }
22352 }
22353
5c631832
JK
22354 if (!attr)
22355 {
e103e986
JK
22356 /* DWARF: "If there is no such attribute, then there is no effect.".
22357 DATA is ignored if SIZE is 0. */
5c631832 22358
e103e986 22359 retval.data = NULL;
5c631832
JK
22360 retval.size = 0;
22361 }
cd6c91b4 22362 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22363 {
22364 struct dwarf2_loclist_baton loclist_baton;
22365 CORE_ADDR pc = (*get_frame_pc) (baton);
22366 size_t size;
22367
22368 fill_in_loclist_baton (cu, &loclist_baton, attr);
22369
22370 retval.data = dwarf2_find_location_expression (&loclist_baton,
22371 &size, pc);
22372 retval.size = size;
22373 }
5c631832
JK
22374 else
22375 {
4fc6c0d5 22376 if (!attr->form_is_block ())
9d8780f0 22377 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22378 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22379 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22380
22381 retval.data = DW_BLOCK (attr)->data;
22382 retval.size = DW_BLOCK (attr)->size;
22383 }
a50264ba 22384 retval.per_objfile = dwarf2_per_objfile;
5c631832 22385 retval.per_cu = cu->per_cu;
918dd910 22386
ed2dc618 22387 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22388
5c631832 22389 return retval;
348e048f
DE
22390}
22391
d4c9a4f8 22392/* See read.h. */
8b9737bf
TT
22393
22394struct dwarf2_locexpr_baton
22395dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22396 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22397 CORE_ADDR (*get_frame_pc) (void *baton),
22398 void *baton)
22399{
9c541725 22400 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22401
9c541725 22402 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22403}
22404
b6807d98
TT
22405/* Write a constant of a given type as target-ordered bytes into
22406 OBSTACK. */
22407
22408static const gdb_byte *
22409write_constant_as_bytes (struct obstack *obstack,
22410 enum bfd_endian byte_order,
22411 struct type *type,
22412 ULONGEST value,
22413 LONGEST *len)
22414{
22415 gdb_byte *result;
22416
22417 *len = TYPE_LENGTH (type);
224c3ddb 22418 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22419 store_unsigned_integer (result, *len, byte_order, value);
22420
22421 return result;
22422}
22423
d4c9a4f8 22424/* See read.h. */
b6807d98
TT
22425
22426const gdb_byte *
9c541725 22427dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22428 dwarf2_per_cu_data *per_cu,
22429 obstack *obstack,
b6807d98
TT
22430 LONGEST *len)
22431{
22432 struct dwarf2_cu *cu;
22433 struct die_info *die;
22434 struct attribute *attr;
22435 const gdb_byte *result = NULL;
22436 struct type *type;
22437 LONGEST value;
22438 enum bfd_endian byte_order;
e3b94546 22439 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22440
b6807d98 22441 if (per_cu->cu == NULL)
ab432490 22442 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
b6807d98 22443 cu = per_cu->cu;
cc12ce38
DE
22444 if (cu == NULL)
22445 {
22446 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22447 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22448 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22449 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22450 }
b6807d98 22451
9c541725 22452 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22453 if (!die)
9d8780f0
SM
22454 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22455 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22456
22457 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22458 if (attr == NULL)
22459 return NULL;
22460
e3b94546 22461 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22462 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22463
22464 switch (attr->form)
22465 {
22466 case DW_FORM_addr:
336d760d 22467 case DW_FORM_addrx:
b6807d98
TT
22468 case DW_FORM_GNU_addr_index:
22469 {
22470 gdb_byte *tem;
22471
22472 *len = cu->header.addr_size;
224c3ddb 22473 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22474 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22475 result = tem;
22476 }
22477 break;
22478 case DW_FORM_string:
22479 case DW_FORM_strp:
cf532bd1 22480 case DW_FORM_strx:
b6807d98
TT
22481 case DW_FORM_GNU_str_index:
22482 case DW_FORM_GNU_strp_alt:
22483 /* DW_STRING is already allocated on the objfile obstack, point
22484 directly to it. */
22485 result = (const gdb_byte *) DW_STRING (attr);
22486 *len = strlen (DW_STRING (attr));
22487 break;
22488 case DW_FORM_block1:
22489 case DW_FORM_block2:
22490 case DW_FORM_block4:
22491 case DW_FORM_block:
22492 case DW_FORM_exprloc:
0224619f 22493 case DW_FORM_data16:
b6807d98
TT
22494 result = DW_BLOCK (attr)->data;
22495 *len = DW_BLOCK (attr)->size;
22496 break;
22497
22498 /* The DW_AT_const_value attributes are supposed to carry the
22499 symbol's value "represented as it would be on the target
22500 architecture." By the time we get here, it's already been
22501 converted to host endianness, so we just need to sign- or
22502 zero-extend it as appropriate. */
22503 case DW_FORM_data1:
22504 type = die_type (die, cu);
22505 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22506 if (result == NULL)
22507 result = write_constant_as_bytes (obstack, byte_order,
22508 type, value, len);
22509 break;
22510 case DW_FORM_data2:
22511 type = die_type (die, cu);
22512 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22513 if (result == NULL)
22514 result = write_constant_as_bytes (obstack, byte_order,
22515 type, value, len);
22516 break;
22517 case DW_FORM_data4:
22518 type = die_type (die, cu);
22519 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22520 if (result == NULL)
22521 result = write_constant_as_bytes (obstack, byte_order,
22522 type, value, len);
22523 break;
22524 case DW_FORM_data8:
22525 type = die_type (die, cu);
22526 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22527 if (result == NULL)
22528 result = write_constant_as_bytes (obstack, byte_order,
22529 type, value, len);
22530 break;
22531
22532 case DW_FORM_sdata:
663c44ac 22533 case DW_FORM_implicit_const:
b6807d98
TT
22534 type = die_type (die, cu);
22535 result = write_constant_as_bytes (obstack, byte_order,
22536 type, DW_SND (attr), len);
22537 break;
22538
22539 case DW_FORM_udata:
22540 type = die_type (die, cu);
22541 result = write_constant_as_bytes (obstack, byte_order,
22542 type, DW_UNSND (attr), len);
22543 break;
22544
22545 default:
b98664d3 22546 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22547 dwarf_form_name (attr->form));
22548 break;
22549 }
22550
22551 return result;
22552}
22553
d4c9a4f8 22554/* See read.h. */
7942e96e
AA
22555
22556struct type *
9c541725 22557dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22558 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22559{
22560 struct dwarf2_cu *cu;
22561 struct die_info *die;
22562
7942e96e 22563 if (per_cu->cu == NULL)
ab432490 22564 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
7942e96e
AA
22565 cu = per_cu->cu;
22566 if (!cu)
22567 return NULL;
22568
9c541725 22569 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22570 if (!die)
22571 return NULL;
22572
22573 return die_type (die, cu);
22574}
22575
8cb5117c 22576/* See read.h. */
8a9b8146
TT
22577
22578struct type *
b64f50a1 22579dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22580 struct dwarf2_per_cu_data *per_cu)
22581{
9c541725 22582 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22583 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22584}
22585
ac9ec31b 22586/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22587 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22588 On exit *REF_CU is the CU of the result.
22589 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22590
22591static struct die_info *
ac9ec31b
DE
22592follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22593 struct dwarf2_cu **ref_cu)
348e048f 22594{
348e048f 22595 struct die_info temp_die;
c24bdb02 22596 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22597 struct die_info *die;
22598
ac9ec31b
DE
22599 /* While it might be nice to assert sig_type->type == NULL here,
22600 we can get here for DW_AT_imported_declaration where we need
22601 the DIE not the type. */
348e048f
DE
22602
22603 /* If necessary, add it to the queue and load its DIEs. */
22604
95554aad 22605 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
ab432490 22606 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
348e048f 22607
348e048f 22608 sig_cu = sig_type->per_cu.cu;
69d751e3 22609 gdb_assert (sig_cu != NULL);
9c541725
PA
22610 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22611 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22612 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22613 to_underlying (temp_die.sect_off));
348e048f
DE
22614 if (die)
22615 {
5e22e966 22616 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
ed2dc618 22617
796a7ff8
DE
22618 /* For .gdb_index version 7 keep track of included TUs.
22619 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22620 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22621 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22622 {
ae640021 22623 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22624 }
22625
348e048f 22626 *ref_cu = sig_cu;
c24bdb02
KS
22627 if (sig_cu != cu)
22628 sig_cu->ancestor = cu;
22629
348e048f
DE
22630 return die;
22631 }
22632
ac9ec31b
DE
22633 return NULL;
22634}
22635
22636/* Follow signatured type referenced by ATTR in SRC_DIE.
22637 On entry *REF_CU is the CU of SRC_DIE.
22638 On exit *REF_CU is the CU of the result.
22639 The result is the DIE of the type.
22640 If the referenced type cannot be found an error is thrown. */
22641
22642static struct die_info *
ff39bb5e 22643follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22644 struct dwarf2_cu **ref_cu)
22645{
22646 ULONGEST signature = DW_SIGNATURE (attr);
22647 struct signatured_type *sig_type;
22648 struct die_info *die;
22649
22650 gdb_assert (attr->form == DW_FORM_ref_sig8);
22651
a2ce51a0 22652 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22653 /* sig_type will be NULL if the signatured type is missing from
22654 the debug info. */
22655 if (sig_type == NULL)
22656 {
22657 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22658 " from DIE at %s [in module %s]"),
22659 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22660 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22661 }
22662
22663 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22664 if (die == NULL)
22665 {
22666 dump_die_for_error (src_die);
22667 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22668 " from DIE at %s [in module %s]"),
22669 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22670 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22671 }
22672
22673 return die;
22674}
22675
22676/* Get the type specified by SIGNATURE referenced in DIE/CU,
22677 reading in and processing the type unit if necessary. */
22678
22679static struct type *
22680get_signatured_type (struct die_info *die, ULONGEST signature,
22681 struct dwarf2_cu *cu)
22682{
5e22e966 22683 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22684 struct signatured_type *sig_type;
22685 struct dwarf2_cu *type_cu;
22686 struct die_info *type_die;
22687 struct type *type;
22688
a2ce51a0 22689 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22690 /* sig_type will be NULL if the signatured type is missing from
22691 the debug info. */
22692 if (sig_type == NULL)
22693 {
b98664d3 22694 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22695 " from DIE at %s [in module %s]"),
22696 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22697 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22698 return build_error_marker_type (cu, die);
22699 }
22700
22701 /* If we already know the type we're done. */
22702 if (sig_type->type != NULL)
22703 return sig_type->type;
22704
22705 type_cu = cu;
22706 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22707 if (type_die != NULL)
22708 {
22709 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22710 is created. This is important, for example, because for c++ classes
22711 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22712 type = read_type_die (type_die, type_cu);
22713 if (type == NULL)
22714 {
b98664d3 22715 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22716 " referenced from DIE at %s [in module %s]"),
22717 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22718 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22719 type = build_error_marker_type (cu, die);
22720 }
22721 }
22722 else
22723 {
b98664d3 22724 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22725 " from DIE at %s [in module %s]"),
22726 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22727 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22728 type = build_error_marker_type (cu, die);
22729 }
22730 sig_type->type = type;
22731
22732 return type;
22733}
22734
22735/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22736 reading in and processing the type unit if necessary. */
22737
22738static struct type *
ff39bb5e 22739get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22740 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22741{
22742 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22743 if (attr->form_is_ref ())
ac9ec31b
DE
22744 {
22745 struct dwarf2_cu *type_cu = cu;
22746 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22747
22748 return read_type_die (type_die, type_cu);
22749 }
22750 else if (attr->form == DW_FORM_ref_sig8)
22751 {
22752 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22753 }
22754 else
22755 {
5e22e966 22756 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22757
b98664d3 22758 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22759 " at %s [in module %s]"),
22760 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22761 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22762 return build_error_marker_type (cu, die);
22763 }
348e048f
DE
22764}
22765
e5fe5e75 22766/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22767
22768static void
ab432490
SM
22769load_full_type_unit (dwarf2_per_cu_data *per_cu,
22770 dwarf2_per_objfile *per_objfile)
348e048f 22771{
52dc124a 22772 struct signatured_type *sig_type;
348e048f 22773
f4dc4d17 22774 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22775 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22776
6721b2ec
DE
22777 /* We have the per_cu, but we need the signatured_type.
22778 Fortunately this is an easy translation. */
22779 gdb_assert (per_cu->is_debug_types);
22780 sig_type = (struct signatured_type *) per_cu;
348e048f 22781
6721b2ec 22782 gdb_assert (per_cu->cu == NULL);
348e048f 22783
ab432490 22784 read_signatured_type (sig_type, per_objfile);
348e048f 22785
6721b2ec 22786 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22787}
22788
3019eac3
DE
22789/* Read in a signatured type and build its CU and DIEs.
22790 If the type is a stub for the real type in a DWO file,
22791 read in the real type from the DWO file as well. */
dee91e82
DE
22792
22793static void
ab432490
SM
22794read_signatured_type (signatured_type *sig_type,
22795 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22796{
22797 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22798
3019eac3 22799 gdb_assert (per_cu->is_debug_types);
dee91e82 22800 gdb_assert (per_cu->cu == NULL);
348e048f 22801
ab432490 22802 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22803
22804 if (!reader.dummy_p)
22805 {
22806 struct dwarf2_cu *cu = reader.cu;
22807 const gdb_byte *info_ptr = reader.info_ptr;
22808
22809 gdb_assert (cu->die_hash == NULL);
22810 cu->die_hash =
22811 htab_create_alloc_ex (cu->header.length / 12,
22812 die_hash,
22813 die_eq,
22814 NULL,
22815 &cu->comp_unit_obstack,
22816 hashtab_obstack_allocate,
22817 dummy_obstack_deallocate);
22818
3e225074 22819 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22820 reader.comp_unit_die->child
22821 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22822 reader.comp_unit_die);
22823 cu->dies = reader.comp_unit_die;
22824 /* comp_unit_die is not stored in die_hash, no need. */
22825
22826 /* We try not to read any attributes in this function, because
22827 not all CUs needed for references have been loaded yet, and
22828 symbol table processing isn't initialized. But we have to
22829 set the CU language, or we won't be able to build types
22830 correctly. Similarly, if we do not read the producer, we can
22831 not apply producer-specific interpretation. */
22832 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22833
22834 reader.keep ();
c0ab21c2
TT
22835 }
22836
7ee85ab1 22837 sig_type->per_cu.tu_read = 1;
c906108c
SS
22838}
22839
c906108c
SS
22840/* Decode simple location descriptions.
22841 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22842 the location and return the value. If COMPUTED is non-null, it is
22843 set to true to indicate that decoding was successful, and false
22844 otherwise. If COMPUTED is null, then this function may emit a
22845 complaint. */
c906108c
SS
22846
22847static CORE_ADDR
7d79de9a 22848decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22849{
5e22e966 22850 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22851 size_t i;
22852 size_t size = blk->size;
d521ce57 22853 const gdb_byte *data = blk->data;
21ae7a4d
JK
22854 CORE_ADDR stack[64];
22855 int stacki;
22856 unsigned int bytes_read, unsnd;
22857 gdb_byte op;
c906108c 22858
7d79de9a
TT
22859 if (computed != nullptr)
22860 *computed = false;
22861
21ae7a4d
JK
22862 i = 0;
22863 stacki = 0;
22864 stack[stacki] = 0;
22865 stack[++stacki] = 0;
22866
22867 while (i < size)
22868 {
22869 op = data[i++];
22870 switch (op)
22871 {
22872 case DW_OP_lit0:
22873 case DW_OP_lit1:
22874 case DW_OP_lit2:
22875 case DW_OP_lit3:
22876 case DW_OP_lit4:
22877 case DW_OP_lit5:
22878 case DW_OP_lit6:
22879 case DW_OP_lit7:
22880 case DW_OP_lit8:
22881 case DW_OP_lit9:
22882 case DW_OP_lit10:
22883 case DW_OP_lit11:
22884 case DW_OP_lit12:
22885 case DW_OP_lit13:
22886 case DW_OP_lit14:
22887 case DW_OP_lit15:
22888 case DW_OP_lit16:
22889 case DW_OP_lit17:
22890 case DW_OP_lit18:
22891 case DW_OP_lit19:
22892 case DW_OP_lit20:
22893 case DW_OP_lit21:
22894 case DW_OP_lit22:
22895 case DW_OP_lit23:
22896 case DW_OP_lit24:
22897 case DW_OP_lit25:
22898 case DW_OP_lit26:
22899 case DW_OP_lit27:
22900 case DW_OP_lit28:
22901 case DW_OP_lit29:
22902 case DW_OP_lit30:
22903 case DW_OP_lit31:
22904 stack[++stacki] = op - DW_OP_lit0;
22905 break;
f1bea926 22906
21ae7a4d
JK
22907 case DW_OP_reg0:
22908 case DW_OP_reg1:
22909 case DW_OP_reg2:
22910 case DW_OP_reg3:
22911 case DW_OP_reg4:
22912 case DW_OP_reg5:
22913 case DW_OP_reg6:
22914 case DW_OP_reg7:
22915 case DW_OP_reg8:
22916 case DW_OP_reg9:
22917 case DW_OP_reg10:
22918 case DW_OP_reg11:
22919 case DW_OP_reg12:
22920 case DW_OP_reg13:
22921 case DW_OP_reg14:
22922 case DW_OP_reg15:
22923 case DW_OP_reg16:
22924 case DW_OP_reg17:
22925 case DW_OP_reg18:
22926 case DW_OP_reg19:
22927 case DW_OP_reg20:
22928 case DW_OP_reg21:
22929 case DW_OP_reg22:
22930 case DW_OP_reg23:
22931 case DW_OP_reg24:
22932 case DW_OP_reg25:
22933 case DW_OP_reg26:
22934 case DW_OP_reg27:
22935 case DW_OP_reg28:
22936 case DW_OP_reg29:
22937 case DW_OP_reg30:
22938 case DW_OP_reg31:
22939 stack[++stacki] = op - DW_OP_reg0;
22940 if (i < size)
7d79de9a
TT
22941 {
22942 if (computed == nullptr)
22943 dwarf2_complex_location_expr_complaint ();
22944 else
22945 return 0;
22946 }
21ae7a4d 22947 break;
c906108c 22948
21ae7a4d
JK
22949 case DW_OP_regx:
22950 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22951 i += bytes_read;
22952 stack[++stacki] = unsnd;
22953 if (i < size)
7d79de9a
TT
22954 {
22955 if (computed == nullptr)
22956 dwarf2_complex_location_expr_complaint ();
22957 else
22958 return 0;
22959 }
21ae7a4d 22960 break;
c906108c 22961
21ae7a4d 22962 case DW_OP_addr:
c8a7a66f
TT
22963 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22964 &bytes_read);
21ae7a4d
JK
22965 i += bytes_read;
22966 break;
d53d4ac5 22967
21ae7a4d
JK
22968 case DW_OP_const1u:
22969 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22970 i += 1;
22971 break;
22972
22973 case DW_OP_const1s:
22974 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22975 i += 1;
22976 break;
22977
22978 case DW_OP_const2u:
22979 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22980 i += 2;
22981 break;
22982
22983 case DW_OP_const2s:
22984 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22985 i += 2;
22986 break;
d53d4ac5 22987
21ae7a4d
JK
22988 case DW_OP_const4u:
22989 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22990 i += 4;
22991 break;
22992
22993 case DW_OP_const4s:
22994 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22995 i += 4;
22996 break;
22997
585861ea
JK
22998 case DW_OP_const8u:
22999 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23000 i += 8;
23001 break;
23002
21ae7a4d
JK
23003 case DW_OP_constu:
23004 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23005 &bytes_read);
23006 i += bytes_read;
23007 break;
23008
23009 case DW_OP_consts:
23010 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23011 i += bytes_read;
23012 break;
23013
23014 case DW_OP_dup:
23015 stack[stacki + 1] = stack[stacki];
23016 stacki++;
23017 break;
23018
23019 case DW_OP_plus:
23020 stack[stacki - 1] += stack[stacki];
23021 stacki--;
23022 break;
23023
23024 case DW_OP_plus_uconst:
23025 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_minus:
23031 stack[stacki - 1] -= stack[stacki];
23032 stacki--;
23033 break;
23034
23035 case DW_OP_deref:
23036 /* If we're not the last op, then we definitely can't encode
23037 this using GDB's address_class enum. This is valid for partial
23038 global symbols, although the variable's address will be bogus
23039 in the psymtab. */
23040 if (i < size)
7d79de9a
TT
23041 {
23042 if (computed == nullptr)
23043 dwarf2_complex_location_expr_complaint ();
23044 else
23045 return 0;
23046 }
21ae7a4d
JK
23047 break;
23048
23049 case DW_OP_GNU_push_tls_address:
4aa4e28b 23050 case DW_OP_form_tls_address:
21ae7a4d
JK
23051 /* The top of the stack has the offset from the beginning
23052 of the thread control block at which the variable is located. */
23053 /* Nothing should follow this operator, so the top of stack would
23054 be returned. */
23055 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23056 address will be bogus in the psymtab. Make it always at least
23057 non-zero to not look as a variable garbage collected by linker
23058 which have DW_OP_addr 0. */
21ae7a4d 23059 if (i < size)
7d79de9a
TT
23060 {
23061 if (computed == nullptr)
23062 dwarf2_complex_location_expr_complaint ();
23063 else
23064 return 0;
23065 }
585861ea 23066 stack[stacki]++;
21ae7a4d
JK
23067 break;
23068
23069 case DW_OP_GNU_uninit:
7d79de9a
TT
23070 if (computed != nullptr)
23071 return 0;
21ae7a4d
JK
23072 break;
23073
336d760d 23074 case DW_OP_addrx:
3019eac3 23075 case DW_OP_GNU_addr_index:
49f6c839 23076 case DW_OP_GNU_const_index:
3019eac3
DE
23077 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23078 &bytes_read);
23079 i += bytes_read;
23080 break;
23081
21ae7a4d 23082 default:
7d79de9a
TT
23083 if (computed == nullptr)
23084 {
23085 const char *name = get_DW_OP_name (op);
21ae7a4d 23086
7d79de9a
TT
23087 if (name)
23088 complaint (_("unsupported stack op: '%s'"),
23089 name);
23090 else
23091 complaint (_("unsupported stack op: '%02x'"),
23092 op);
23093 }
21ae7a4d
JK
23094
23095 return (stack[stacki]);
d53d4ac5 23096 }
3c6e0cb3 23097
21ae7a4d
JK
23098 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23099 outside of the allocated space. Also enforce minimum>0. */
23100 if (stacki >= ARRAY_SIZE (stack) - 1)
23101 {
7d79de9a
TT
23102 if (computed == nullptr)
23103 complaint (_("location description stack overflow"));
21ae7a4d
JK
23104 return 0;
23105 }
23106
23107 if (stacki <= 0)
23108 {
7d79de9a
TT
23109 if (computed == nullptr)
23110 complaint (_("location description stack underflow"));
21ae7a4d
JK
23111 return 0;
23112 }
23113 }
7d79de9a
TT
23114
23115 if (computed != nullptr)
23116 *computed = true;
21ae7a4d 23117 return (stack[stacki]);
c906108c
SS
23118}
23119
23120/* memory allocation interface */
23121
c906108c 23122static struct dwarf_block *
7b5a2f43 23123dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23124{
8d749320 23125 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23126}
23127
c906108c 23128static struct die_info *
b60c80d6 23129dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23130{
23131 struct die_info *die;
b60c80d6
DJ
23132 size_t size = sizeof (struct die_info);
23133
23134 if (num_attrs > 1)
23135 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23136
b60c80d6 23137 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23138 memset (die, 0, sizeof (struct die_info));
23139 return (die);
23140}
2e276125
JB
23141
23142\f
a036ba48 23143
c90ec28a 23144/* Macro support. */
cf2c3c16 23145
9eac9650
TT
23146/* An overload of dwarf_decode_macros that finds the correct section
23147 and ensures it is read in before calling the other overload. */
23148
23149static void
23150dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23151 int section_is_gnu)
23152{
5e22e966 23153 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23154 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23155 const struct line_header *lh = cu->line_header;
9eac9650
TT
23156 unsigned int offset_size = cu->header.offset_size;
23157 struct dwarf2_section_info *section;
23158 const char *section_name;
23159
23160 if (cu->dwo_unit != nullptr)
23161 {
23162 if (section_is_gnu)
23163 {
23164 section = &cu->dwo_unit->dwo_file->sections.macro;
23165 section_name = ".debug_macro.dwo";
23166 }
23167 else
23168 {
23169 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23170 section_name = ".debug_macinfo.dwo";
23171 }
23172 }
23173 else
23174 {
23175 if (section_is_gnu)
23176 {
5989a64e 23177 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23178 section_name = ".debug_macro";
23179 }
23180 else
23181 {
5989a64e 23182 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23183 section_name = ".debug_macinfo";
23184 }
23185 }
23186
23187 section->read (objfile);
23188 if (section->buffer == nullptr)
23189 {
23190 complaint (_("missing %s section"), section_name);
23191 return;
23192 }
23193
23194 buildsym_compunit *builder = cu->get_builder ();
23195
23196 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23197 offset_size, offset, section_is_gnu);
23198}
23199
3019eac3
DE
23200/* Return the .debug_loc section to use for CU.
23201 For DWO files use .debug_loc.dwo. */
23202
23203static struct dwarf2_section_info *
23204cu_debug_loc_section (struct dwarf2_cu *cu)
23205{
5e22e966 23206 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23207
3019eac3 23208 if (cu->dwo_unit)
43988095
JK
23209 {
23210 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23211
43988095
JK
23212 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23213 }
5989a64e
SM
23214 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23215 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23216}
23217
8cf6f0b1
TT
23218/* A helper function that fills in a dwarf2_loclist_baton. */
23219
23220static void
23221fill_in_loclist_baton (struct dwarf2_cu *cu,
23222 struct dwarf2_loclist_baton *baton,
ff39bb5e 23223 const struct attribute *attr)
8cf6f0b1 23224{
5e22e966 23225 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23226 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23227
96b79293 23228 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23229
a50264ba 23230 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23231 baton->per_cu = cu->per_cu;
23232 gdb_assert (baton->per_cu);
23233 /* We don't know how long the location list is, but make sure we
23234 don't run off the edge of the section. */
3019eac3
DE
23235 baton->size = section->size - DW_UNSND (attr);
23236 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23237 if (cu->base_address.has_value ())
23238 baton->base_address = *cu->base_address;
23239 else
23240 baton->base_address = 0;
f664829e 23241 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23242}
23243
4c2df51b 23244static void
ff39bb5e 23245dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23246 struct dwarf2_cu *cu, int is_block)
4c2df51b 23247{
5e22e966 23248 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23249 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23250 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23251
cd6c91b4 23252 if (attr->form_is_section_offset ()
3019eac3 23253 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23254 the section. If so, fall through to the complaint in the
23255 other branch. */
2c7d5afc 23256 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23257 {
0d53c4c4 23258 struct dwarf2_loclist_baton *baton;
4c2df51b 23259
8d749320 23260 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23261
8cf6f0b1 23262 fill_in_loclist_baton (cu, baton, attr);
be391dca 23263
2b24b6e4 23264 if (!cu->base_address.has_value ())
b98664d3 23265 complaint (_("Location list used without "
3e43a32a 23266 "specifying the CU base address."));
4c2df51b 23267
f1e6e072
TT
23268 SYMBOL_ACLASS_INDEX (sym) = (is_block
23269 ? dwarf2_loclist_block_index
23270 : dwarf2_loclist_index);
0d53c4c4
DJ
23271 SYMBOL_LOCATION_BATON (sym) = baton;
23272 }
23273 else
23274 {
23275 struct dwarf2_locexpr_baton *baton;
23276
8d749320 23277 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23278 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23279 baton->per_cu = cu->per_cu;
23280 gdb_assert (baton->per_cu);
0d53c4c4 23281
4fc6c0d5 23282 if (attr->form_is_block ())
0d53c4c4
DJ
23283 {
23284 /* Note that we're just copying the block's data pointer
23285 here, not the actual data. We're still pointing into the
6502dd73
DJ
23286 info_buffer for SYM's objfile; right now we never release
23287 that buffer, but when we do clean up properly this may
23288 need to change. */
0d53c4c4
DJ
23289 baton->size = DW_BLOCK (attr)->size;
23290 baton->data = DW_BLOCK (attr)->data;
23291 }
23292 else
23293 {
23294 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23295 sym->natural_name ());
0d53c4c4 23296 baton->size = 0;
0d53c4c4 23297 }
6e70227d 23298
f1e6e072
TT
23299 SYMBOL_ACLASS_INDEX (sym) = (is_block
23300 ? dwarf2_locexpr_block_index
23301 : dwarf2_locexpr_index);
0d53c4c4
DJ
23302 SYMBOL_LOCATION_BATON (sym) = baton;
23303 }
4c2df51b 23304}
6502dd73 23305
09ba997f 23306/* See read.h. */
ae0d2f24
UW
23307
23308struct objfile *
09ba997f 23309dwarf2_per_cu_data::objfile () const
ae0d2f24 23310{
09ba997f 23311 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23312
23313 /* Return the master objfile, so that we can report and look up the
23314 correct file containing this variable. */
23315 if (objfile->separate_debug_objfile_backlink)
23316 objfile = objfile->separate_debug_objfile_backlink;
23317
23318 return objfile;
23319}
23320
96408a79
SA
23321/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23322 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23323 CU_HEADERP first. */
23324
23325static const struct comp_unit_head *
23326per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23327 const struct dwarf2_per_cu_data *per_cu)
96408a79 23328{
d521ce57 23329 const gdb_byte *info_ptr;
96408a79
SA
23330
23331 if (per_cu->cu)
23332 return &per_cu->cu->header;
23333
9c541725 23334 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23335
23336 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23337 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23338 rcuh_kind::COMPILE);
96408a79
SA
23339
23340 return cu_headerp;
23341}
23342
09ba997f 23343/* See read.h. */
ae0d2f24 23344
98714339 23345int
09ba997f 23346dwarf2_per_cu_data::addr_size () const
ae0d2f24 23347{
96408a79
SA
23348 struct comp_unit_head cu_header_local;
23349 const struct comp_unit_head *cu_headerp;
c471e790 23350
09ba997f 23351 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23352
23353 return cu_headerp->addr_size;
ae0d2f24
UW
23354}
23355
09ba997f 23356/* See read.h. */
9eae7c52
TT
23357
23358int
09ba997f 23359dwarf2_per_cu_data::offset_size () const
9eae7c52 23360{
96408a79
SA
23361 struct comp_unit_head cu_header_local;
23362 const struct comp_unit_head *cu_headerp;
9c6c53f7 23363
09ba997f 23364 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23365
23366 return cu_headerp->offset_size;
23367}
23368
09ba997f 23369/* See read.h. */
96408a79
SA
23370
23371int
09ba997f 23372dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23373{
23374 struct comp_unit_head cu_header_local;
23375 const struct comp_unit_head *cu_headerp;
23376
09ba997f 23377 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23378
23379 if (cu_headerp->version == 2)
23380 return cu_headerp->addr_size;
23381 else
23382 return cu_headerp->offset_size;
181cebd4
JK
23383}
23384
09ba997f 23385/* See read.h. */
9aa1f1e3
TT
23386
23387CORE_ADDR
09ba997f 23388dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23389{
09ba997f
TT
23390 struct objfile *objfile = dwarf2_per_objfile->objfile;
23391
23392 return objfile->text_section_offset ();
9aa1f1e3
TT
23393}
23394
09ba997f
TT
23395/* See read.h. */
23396
23397struct type *
23398dwarf2_per_cu_data::addr_type () const
9a49df9d 23399{
09ba997f 23400 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23401 struct type *void_type = objfile_type (objfile)->builtin_void;
23402 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23403 int addr_size = this->addr_size ();
9a49df9d
AB
23404
23405 if (TYPE_LENGTH (addr_type) == addr_size)
23406 return addr_type;
23407
09ba997f 23408 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23409 return addr_type;
23410}
23411
22b6cd70
TT
23412/* A helper function for dwarf2_find_containing_comp_unit that returns
23413 the index of the result, and that searches a vector. It will
23414 return a result even if the offset in question does not actually
23415 occur in any CU. This is separate so that it can be unit
23416 tested. */
ae038cb0 23417
22b6cd70
TT
23418static int
23419dwarf2_find_containing_comp_unit
23420 (sect_offset sect_off,
23421 unsigned int offset_in_dwz,
23422 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23423{
ae038cb0
DJ
23424 int low, high;
23425
ae038cb0 23426 low = 0;
22b6cd70 23427 high = all_comp_units.size () - 1;
ae038cb0
DJ
23428 while (high > low)
23429 {
36586728 23430 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23431 int mid = low + (high - low) / 2;
9a619af0 23432
22b6cd70 23433 mid_cu = all_comp_units[mid];
36586728 23434 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23435 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23436 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23437 high = mid;
23438 else
23439 low = mid + 1;
23440 }
23441 gdb_assert (low == high);
22b6cd70
TT
23442 return low;
23443}
23444
23445/* Locate the .debug_info compilation unit from CU's objfile which contains
23446 the DIE at OFFSET. Raises an error on failure. */
23447
23448static struct dwarf2_per_cu_data *
23449dwarf2_find_containing_comp_unit (sect_offset sect_off,
23450 unsigned int offset_in_dwz,
23451 struct dwarf2_per_objfile *dwarf2_per_objfile)
23452{
23453 int low
23454 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23455 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23456 struct dwarf2_per_cu_data *this_cu
5989a64e 23457 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23458
45b8ae0c 23459 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23460 {
36586728 23461 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23462 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23463 "offset %s [in module %s]"),
23464 sect_offset_str (sect_off),
ed2dc618 23465 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23466
5989a64e 23467 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23468 <= sect_off);
5989a64e 23469 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23470 }
23471 else
23472 {
5989a64e 23473 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23474 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23475 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23476 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23477 return this_cu;
23478 }
23479}
23480
22b6cd70
TT
23481#if GDB_SELF_TEST
23482
23483namespace selftests {
23484namespace find_containing_comp_unit {
23485
23486static void
23487run_test ()
23488{
23489 struct dwarf2_per_cu_data one {};
23490 struct dwarf2_per_cu_data two {};
23491 struct dwarf2_per_cu_data three {};
23492 struct dwarf2_per_cu_data four {};
23493
23494 one.length = 5;
23495 two.sect_off = sect_offset (one.length);
23496 two.length = 7;
23497
23498 three.length = 5;
23499 three.is_dwz = 1;
23500 four.sect_off = sect_offset (three.length);
23501 four.length = 7;
23502 four.is_dwz = 1;
23503
23504 std::vector<dwarf2_per_cu_data *> units;
23505 units.push_back (&one);
23506 units.push_back (&two);
23507 units.push_back (&three);
23508 units.push_back (&four);
23509
23510 int result;
23511
23512 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23515 SELF_CHECK (units[result] == &one);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23517 SELF_CHECK (units[result] == &two);
23518
23519 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23522 SELF_CHECK (units[result] == &three);
23523 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23524 SELF_CHECK (units[result] == &four);
23525}
23526
23527}
23528}
23529
23530#endif /* GDB_SELF_TEST */
23531
9e021579 23532/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23533
9e021579
SM
23534dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23535 dwarf2_per_objfile *per_objfile)
23536 : per_cu (per_cu),
23537 per_objfile (per_objfile),
9068261f
AB
23538 mark (false),
23539 has_loclist (false),
23540 checked_producer (false),
23541 producer_is_gxx_lt_4_6 (false),
23542 producer_is_gcc_lt_4_3 (false),
eb77c9df 23543 producer_is_icc (false),
9068261f 23544 producer_is_icc_lt_14 (false),
c258c396 23545 producer_is_codewarrior (false),
9068261f 23546 processing_has_namespace_info (false)
93311388 23547{
fcd3b13d
SM
23548 per_cu->cu = this;
23549}
23550
23551/* Destroy a dwarf2_cu. */
23552
23553dwarf2_cu::~dwarf2_cu ()
23554{
23555 per_cu->cu = NULL;
9816fde3
JK
23556}
23557
23558/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23559
23560static void
95554aad
TT
23561prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23562 enum language pretend_language)
9816fde3
JK
23563{
23564 struct attribute *attr;
23565
23566 /* Set the language we're debugging. */
23567 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23568 if (attr != nullptr)
9816fde3
JK
23569 set_cu_language (DW_UNSND (attr), cu);
23570 else
9cded63f 23571 {
95554aad 23572 cu->language = pretend_language;
9cded63f
TT
23573 cu->language_defn = language_def (cu->language);
23574 }
dee91e82 23575
7d45c7c3 23576 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23577}
23578
ae038cb0
DJ
23579/* Increase the age counter on each cached compilation unit, and free
23580 any that are too old. */
23581
23582static void
ed2dc618 23583age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23584{
23585 struct dwarf2_per_cu_data *per_cu, **last_chain;
23586
5989a64e
SM
23587 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23588 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23589 while (per_cu != NULL)
23590 {
23591 per_cu->cu->last_used ++;
b4f54984 23592 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23593 dwarf2_mark (per_cu->cu);
23594 per_cu = per_cu->cu->read_in_chain;
23595 }
23596
5989a64e
SM
23597 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23598 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23599 while (per_cu != NULL)
23600 {
23601 struct dwarf2_per_cu_data *next_cu;
23602
23603 next_cu = per_cu->cu->read_in_chain;
23604
23605 if (!per_cu->cu->mark)
23606 {
fcd3b13d 23607 delete per_cu->cu;
ae038cb0
DJ
23608 *last_chain = next_cu;
23609 }
23610 else
23611 last_chain = &per_cu->cu->read_in_chain;
23612
23613 per_cu = next_cu;
23614 }
23615}
23616
23617/* Remove a single compilation unit from the cache. */
23618
23619static void
dee91e82 23620free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23621{
23622 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23623 struct dwarf2_per_objfile *dwarf2_per_objfile
23624 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23625
5989a64e
SM
23626 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23627 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23628 while (per_cu != NULL)
23629 {
23630 struct dwarf2_per_cu_data *next_cu;
23631
23632 next_cu = per_cu->cu->read_in_chain;
23633
dee91e82 23634 if (per_cu == target_per_cu)
ae038cb0 23635 {
fcd3b13d 23636 delete per_cu->cu;
dee91e82 23637 per_cu->cu = NULL;
ae038cb0
DJ
23638 *last_chain = next_cu;
23639 break;
23640 }
23641 else
23642 last_chain = &per_cu->cu->read_in_chain;
23643
23644 per_cu = next_cu;
23645 }
23646}
23647
dee91e82
DE
23648/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23649 We store these in a hash table separate from the DIEs, and preserve them
23650 when the DIEs are flushed out of cache.
23651
23652 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23653 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23654 or the type may come from a DWO file. Furthermore, while it's more logical
23655 to use per_cu->section+offset, with Fission the section with the data is in
23656 the DWO file but we don't know that section at the point we need it.
23657 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23658 because we can enter the lookup routine, get_die_type_at_offset, from
23659 outside this file, and thus won't necessarily have PER_CU->cu.
23660 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23661
dee91e82 23662struct dwarf2_per_cu_offset_and_type
1c379e20 23663{
dee91e82 23664 const struct dwarf2_per_cu_data *per_cu;
9c541725 23665 sect_offset sect_off;
1c379e20
DJ
23666 struct type *type;
23667};
23668
dee91e82 23669/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23670
23671static hashval_t
dee91e82 23672per_cu_offset_and_type_hash (const void *item)
1c379e20 23673{
9a3c8263
SM
23674 const struct dwarf2_per_cu_offset_and_type *ofs
23675 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23676
9c541725 23677 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23678}
23679
dee91e82 23680/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23681
23682static int
dee91e82 23683per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23684{
9a3c8263
SM
23685 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23686 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23687 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23688 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23689
dee91e82 23690 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23691 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23692}
23693
23694/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23695 table if necessary. For convenience, return TYPE.
23696
23697 The DIEs reading must have careful ordering to:
85102364 23698 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23699 reading current DIE.
23700 * Not trying to dereference contents of still incompletely read in types
23701 while reading in other DIEs.
23702 * Enable referencing still incompletely read in types just by a pointer to
23703 the type without accessing its fields.
23704
23705 Therefore caller should follow these rules:
23706 * Try to fetch any prerequisite types we may need to build this DIE type
23707 before building the type and calling set_die_type.
e71ec853 23708 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23709 possible before fetching more types to complete the current type.
23710 * Make the type as complete as possible before fetching more types. */
1c379e20 23711
f792889a 23712static struct type *
1c379e20
DJ
23713set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23714{
5e22e966 23715 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23716 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23717 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23718 struct attribute *attr;
23719 struct dynamic_prop prop;
1c379e20 23720
b4ba55a1
JB
23721 /* For Ada types, make sure that the gnat-specific data is always
23722 initialized (if not already set). There are a few types where
23723 we should not be doing so, because the type-specific area is
23724 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23725 where the type-specific area is used to store the floatformat).
23726 But this is not a problem, because the gnat-specific information
23727 is actually not needed for these types. */
23728 if (need_gnat_info (cu)
78134374
SM
23729 && type->code () != TYPE_CODE_FUNC
23730 && type->code () != TYPE_CODE_FLT
23731 && type->code () != TYPE_CODE_METHODPTR
23732 && type->code () != TYPE_CODE_MEMBERPTR
23733 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23734 && !HAVE_GNAT_AUX_INFO (type))
23735 INIT_GNAT_SPECIFIC (type);
23736
3f2f83dd
KB
23737 /* Read DW_AT_allocated and set in type. */
23738 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23739 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23740 {
09ba997f 23741 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23742 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23743 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23744 }
23745 else if (attr != NULL)
23746 {
b98664d3 23747 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23748 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23749 sect_offset_str (die->sect_off));
3f2f83dd
KB
23750 }
23751
23752 /* Read DW_AT_associated and set in type. */
23753 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23754 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23755 {
09ba997f 23756 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23757 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23758 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23759 }
23760 else if (attr != NULL)
23761 {
b98664d3 23762 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23763 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23764 sect_offset_str (die->sect_off));
3f2f83dd
KB
23765 }
23766
3cdcd0ce
JB
23767 /* Read DW_AT_data_location and set in type. */
23768 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23769 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23770 cu->per_cu->addr_type ()))
5c54719c 23771 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23772
ae090bdb
SM
23773 if (dwarf2_per_objfile->die_type_hash == NULL)
23774 dwarf2_per_objfile->die_type_hash
0335378b
TT
23775 = htab_up (htab_create_alloc (127,
23776 per_cu_offset_and_type_hash,
23777 per_cu_offset_and_type_eq,
23778 NULL, xcalloc, xfree));
1c379e20 23779
dee91e82 23780 ofs.per_cu = cu->per_cu;
9c541725 23781 ofs.sect_off = die->sect_off;
1c379e20 23782 ofs.type = type;
dee91e82 23783 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23784 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23785 if (*slot)
b98664d3 23786 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23787 sect_offset_str (die->sect_off));
8d749320
SM
23788 *slot = XOBNEW (&objfile->objfile_obstack,
23789 struct dwarf2_per_cu_offset_and_type);
1c379e20 23790 **slot = ofs;
f792889a 23791 return type;
1c379e20
DJ
23792}
23793
9c541725 23794/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23795 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23796
23797static struct type *
9c541725 23798get_die_type_at_offset (sect_offset sect_off,
673bfd45 23799 struct dwarf2_per_cu_data *per_cu)
1c379e20 23800{
dee91e82 23801 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23802 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23803
ae090bdb 23804 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23805 return NULL;
1c379e20 23806
dee91e82 23807 ofs.per_cu = per_cu;
9c541725 23808 ofs.sect_off = sect_off;
9a3c8263 23809 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23810 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23811 if (slot)
23812 return slot->type;
23813 else
23814 return NULL;
23815}
23816
02142a6c 23817/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23818 or return NULL if DIE does not have a saved type. */
23819
23820static struct type *
23821get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23822{
9c541725 23823 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23824}
23825
10b3939b
DJ
23826/* Add a dependence relationship from CU to REF_PER_CU. */
23827
23828static void
23829dwarf2_add_dependence (struct dwarf2_cu *cu,
23830 struct dwarf2_per_cu_data *ref_per_cu)
23831{
23832 void **slot;
23833
23834 if (cu->dependencies == NULL)
23835 cu->dependencies
23836 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23837 NULL, &cu->comp_unit_obstack,
23838 hashtab_obstack_allocate,
23839 dummy_obstack_deallocate);
23840
23841 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23842 if (*slot == NULL)
23843 *slot = ref_per_cu;
23844}
1c379e20 23845
f504f079
DE
23846/* Subroutine of dwarf2_mark to pass to htab_traverse.
23847 Set the mark field in every compilation unit in the
ae038cb0
DJ
23848 cache that we must keep because we are keeping CU. */
23849
10b3939b
DJ
23850static int
23851dwarf2_mark_helper (void **slot, void *data)
23852{
23853 struct dwarf2_per_cu_data *per_cu;
23854
23855 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23856
23857 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23858 reading of the chain. As such dependencies remain valid it is not much
23859 useful to track and undo them during QUIT cleanups. */
23860 if (per_cu->cu == NULL)
23861 return 1;
23862
10b3939b
DJ
23863 if (per_cu->cu->mark)
23864 return 1;
9068261f 23865 per_cu->cu->mark = true;
10b3939b
DJ
23866
23867 if (per_cu->cu->dependencies != NULL)
23868 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23869
23870 return 1;
23871}
23872
f504f079
DE
23873/* Set the mark field in CU and in every other compilation unit in the
23874 cache that we must keep because we are keeping CU. */
23875
ae038cb0
DJ
23876static void
23877dwarf2_mark (struct dwarf2_cu *cu)
23878{
23879 if (cu->mark)
23880 return;
9068261f 23881 cu->mark = true;
10b3939b
DJ
23882 if (cu->dependencies != NULL)
23883 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23884}
23885
23886static void
23887dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23888{
23889 while (per_cu)
23890 {
9068261f 23891 per_cu->cu->mark = false;
ae038cb0
DJ
23892 per_cu = per_cu->cu->read_in_chain;
23893 }
72bf9492
DJ
23894}
23895
72bf9492
DJ
23896/* Trivial hash function for partial_die_info: the hash value of a DIE
23897 is its offset in .debug_info for this objfile. */
23898
23899static hashval_t
23900partial_die_hash (const void *item)
23901{
9a3c8263
SM
23902 const struct partial_die_info *part_die
23903 = (const struct partial_die_info *) item;
9a619af0 23904
9c541725 23905 return to_underlying (part_die->sect_off);
72bf9492
DJ
23906}
23907
23908/* Trivial comparison function for partial_die_info structures: two DIEs
23909 are equal if they have the same offset. */
23910
23911static int
23912partial_die_eq (const void *item_lhs, const void *item_rhs)
23913{
9a3c8263
SM
23914 const struct partial_die_info *part_die_lhs
23915 = (const struct partial_die_info *) item_lhs;
23916 const struct partial_die_info *part_die_rhs
23917 = (const struct partial_die_info *) item_rhs;
9a619af0 23918
9c541725 23919 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23920}
23921
3c3bb058
AB
23922struct cmd_list_element *set_dwarf_cmdlist;
23923struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23924
9291a0cd 23925static void
cd4fb1b2
SM
23926show_check_physname (struct ui_file *file, int from_tty,
23927 struct cmd_list_element *c, const char *value)
9291a0cd 23928{
cd4fb1b2
SM
23929 fprintf_filtered (file,
23930 _("Whether to check \"physname\" is %s.\n"),
23931 value);
9291a0cd
TT
23932}
23933
6c265988 23934void _initialize_dwarf2_read ();
cd4fb1b2 23935void
6c265988 23936_initialize_dwarf2_read ()
9291a0cd 23937{
0743fc83 23938 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23939Set DWARF specific variables.\n\
590042fc 23940Configure DWARF variables such as the cache size."),
0743fc83
TT
23941 &set_dwarf_cmdlist, "maintenance set dwarf ",
23942 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23943
0743fc83 23944 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23945Show DWARF specific variables.\n\
23946Show DWARF variables such as the cache size."),
0743fc83
TT
23947 &show_dwarf_cmdlist, "maintenance show dwarf ",
23948 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23949
cd4fb1b2
SM
23950 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23951 &dwarf_max_cache_age, _("\
23952Set the upper bound on the age of cached DWARF compilation units."), _("\
23953Show the upper bound on the age of cached DWARF compilation units."), _("\
23954A higher limit means that cached compilation units will be stored\n\
23955in memory longer, and more total memory will be used. Zero disables\n\
23956caching, which can slow down startup."),
23957 NULL,
23958 show_dwarf_max_cache_age,
23959 &set_dwarf_cmdlist,
23960 &show_dwarf_cmdlist);
156942c7 23961
cd4fb1b2
SM
23962 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23963Set debugging of the DWARF reader."), _("\
23964Show debugging of the DWARF reader."), _("\
23965When enabled (non-zero), debugging messages are printed during DWARF\n\
23966reading and symtab expansion. A value of 1 (one) provides basic\n\
23967information. A value greater than 1 provides more verbose information."),
23968 NULL,
23969 NULL,
23970 &setdebuglist, &showdebuglist);
9291a0cd 23971
cd4fb1b2
SM
23972 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23973Set debugging of the DWARF DIE reader."), _("\
23974Show debugging of the DWARF DIE reader."), _("\
23975When enabled (non-zero), DIEs are dumped after they are read in.\n\
23976The value is the maximum depth to print."),
23977 NULL,
23978 NULL,
23979 &setdebuglist, &showdebuglist);
9291a0cd 23980
cd4fb1b2
SM
23981 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23982Set debugging of the dwarf line reader."), _("\
23983Show debugging of the dwarf line reader."), _("\
23984When enabled (non-zero), line number entries are dumped as they are read in.\n\
23985A value of 1 (one) provides basic information.\n\
23986A value greater than 1 provides more verbose information."),
23987 NULL,
23988 NULL,
23989 &setdebuglist, &showdebuglist);
437afbb8 23990
cd4fb1b2
SM
23991 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23992Set cross-checking of \"physname\" code against demangler."), _("\
23993Show cross-checking of \"physname\" code against demangler."), _("\
23994When enabled, GDB's internal \"physname\" code is checked against\n\
23995the demangler."),
23996 NULL, show_check_physname,
23997 &setdebuglist, &showdebuglist);
900e11f9 23998
e615022a
DE
23999 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24000 no_class, &use_deprecated_index_sections, _("\
24001Set whether to use deprecated gdb_index sections."), _("\
24002Show whether to use deprecated gdb_index sections."), _("\
24003When enabled, deprecated .gdb_index sections are used anyway.\n\
24004Normally they are ignored either because of a missing feature or\n\
24005performance issue.\n\
24006Warning: This option must be enabled before gdb reads the file."),
24007 NULL,
24008 NULL,
24009 &setlist, &showlist);
24010
f1e6e072
TT
24011 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24012 &dwarf2_locexpr_funcs);
24013 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24014 &dwarf2_loclist_funcs);
24015
24016 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24017 &dwarf2_block_frame_base_locexpr_funcs);
24018 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24019 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24020
24021#if GDB_SELF_TEST
24022 selftests::register_test ("dw2_expand_symtabs_matching",
24023 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24024 selftests::register_test ("dwarf2_find_containing_comp_unit",
24025 selftests::find_containing_comp_unit::run_test);
c62446b1 24026#endif
6502dd73 24027}
This page took 5.30835 seconds and 4 git commands to generate.